https://github.com/isanbard updated https://github.com/llvm/llvm-project/pull/235941
>From 11eb8e7dfa4698958f1c3fce2e404a674f4980fa Mon Sep 17 00:00:00 2001 From: Bill Wendling <[email protected]> Date: Mon, 26 Oct 2026 14:29:40 -0700 Subject: [PATCH 1/2] [SelectionDAG] Don't crash after an error in a callbr's asm When a callbr's asm has an error, such as a constraint it can't satisfy, visitInlineAsm() reports it and returns before creating the INLINEASM_BR. But the callbr's block still got edges to the indirect targets, which BranchFolding rejects since nothing branches there, and a landing pad in an indirect target looked for the registers that the INLINEASM_BR would have defined. Don't add the edges to the indirect targets, and make a landing pad's value poison, since nothing reaches it. Fixes #98379 and #172657. Part of #173609, whose asm is still rejected, since LLVM picks the "k" alternative of "+kr", which needs AVX-512. Assisted-by: Claude Opus 5.5 --- .../SelectionDAG/SelectionDAGBuilder.cpp | 13 +++- llvm/test/CodeGen/X86/callbr-asm-error.ll | 69 +++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 llvm/test/CodeGen/X86/callbr-asm-error.ll diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index 999229357c5ee8..80c82d4691b255 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -3664,7 +3664,10 @@ void SelectionDAGBuilder::visitCallBr(const CallBrInst &I) { // this changes, we might need to enhance // Target->setIsInlineAsmBrIndirectTarget or add something similar for // intrinsic indirect branches. - if (I.isInlineAsm()) { + + // An asm that failed to lower, which reported an error, has no INLINEASM_BR + // to branch to the indirect targets. + if (I.isInlineAsm() && CallBrOutputs.contains(&I)) { for (BasicBlock *Dest : I.getIndirectDests()) { MachineBasicBlock *Target = FuncInfo.getMBB(Dest); Target->setIsInlineAsmBrIndirectTarget(); @@ -13308,7 +13311,13 @@ void SelectionDAGBuilder::visitCallBrLandingPad(const CallInst &I) { const auto *CBR = cast<CallBrInst>(I.getParent()->getUniquePredecessor()->getTerminator()); auto It = CallBrOutputs.find(CBR); - assert(It != CallBrOutputs.end() && "callbr asm wasn't lowered"); + + // If the asm failed to lower, which reported an error, then nothing branches + // here. + if (It == CallBrOutputs.end()) { + setValue(&I, getValue(PoisonValue::get(I.getType()))); + return; + } SDValue Chain = DAG.getRoot(); SmallVector<SDValue, 8> OutputValues; diff --git a/llvm/test/CodeGen/X86/callbr-asm-error.ll b/llvm/test/CodeGen/X86/callbr-asm-error.ll new file mode 100644 index 00000000000000..4271693b9f5aed --- /dev/null +++ b/llvm/test/CodeGen/X86/callbr-asm-error.ll @@ -0,0 +1,69 @@ +; RUN: not llc -mtriple=x86_64-linux-gnu -verify-machineinstrs < %s 2>&1 | FileCheck %s + +; After an error in a callbr's asm, nothing branches to its indirect targets, +; and their outputs are undefined. This uses the default -O2 pipeline, since +; BranchFolding is what rejected an indirect target with no INLINEASM_BR. + +; CHECK: error: could not allocate output register for constraint '{foo}' +define i32 @bad_register() { +entry: + %r = callbr i32 asm "", "={foo},!i"() + to label %a [label %b] +a: + ret i32 %r +b: + ret i32 %r +} + +; CHECK: error: invalid operand for inline asm constraint 'i' +define i32 @not_an_immediate(i32 %x) { +entry: + %r = callbr i32 asm "", "=r,i,!i"(i32 %x) + to label %a [label %b] +a: + ret i32 %r +b: + ret i32 %r +} + +; A struct of outputs, where 'k' needs AVX-512. +; CHECK: error: could not allocate output register for constraint 'k' +define i32 @struct_output() { +entry: + %r = callbr { i32, i32 } asm "", "=r,=kr,!i"() + to label %a [label %b] +a: + ret i32 0 +b: + %r1 = extractvalue { i32, i32 } %r, 1 + ret i32 %r1 +} + +; The indirect target doesn't use the output, so it has no landing pad. +; CHECK: error: could not allocate output register for constraint '{foo}' +define i32 @no_landing_pad(i1 %c) { +entry: + br i1 %c, label %asm, label %a +asm: + %r = callbr i32 asm "", "={foo},!i"() + to label %a [label %b] +a: + ret i32 0 +b: + ret i32 1 +} + +; A phi in an indirect target loses its value from the callbr's block. +; CHECK: error: could not allocate input reg for constraint '{foo}' +define i32 @phi_in_indirect_target(i1 %c) { +entry: + br i1 %c, label %asm, label %b +asm: + callbr void asm "", "{foo},!i"(i32 0) + to label %a [label %b] +a: + ret i32 0 +b: + %p = phi i32 [ 1, %entry ], [ 2, %asm ] + ret i32 %p +} >From b215107666757bcc258b36ffce705a98ba0bf32a Mon Sep 17 00:00:00 2001 From: Bill Wendling <[email protected]> Date: Wed, 4 Nov 2026 23:21:14 -0800 Subject: [PATCH 2/2] Keep the comments on the indirect-target marking together --- llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index 2314b36f0c02fb..6709ca7045645a 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -3668,12 +3668,13 @@ void SelectionDAGBuilder::visitCallBr(const CallBrInst &I) { // Update successor info. addSuccessorWithProb(CallBrMBB, Return, BranchProbability::getOne()); + // TODO: For most of the cases where there is an intrinsic callbr, we're // having exactly one indirect target, which will be unreachable. As soon as // this changes, we might need to enhance // Target->setIsInlineAsmBrIndirectTarget or add something similar for // intrinsic indirect branches. - + // // An asm that failed to lower, which reported an error, has no INLINEASM_BR // to branch to the indirect targets. if (I.isInlineAsm() && CallBrOutputs.contains(&I)) { _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
