[llvm-branch-commits] [llvm] [SelectionDAG] Report an error for a callbr's indirect register output (PR #235940)

Bill Wendling via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Nov 2 16:01:15 PST 2026


https://github.com/isanbard updated https://github.com/llvm/llvm-project/pull/235940

>From 214aeb3f3d612d83ad317e9b2ec1bae8cd1db3d9 Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Mon, 26 Oct 2026 14:25:23 -0700
Subject: [PATCH] [SelectionDAG] Report an error for a callbr's indirect
 register output

An indirect output with a register constraint, e.g. "=*r", is stored to its
pointer after the asm. But a callbr's asm can branch to an indirect target
before then, so the store was silently skipped on the indirect paths. Clang
emits such an output for a _Complex lvalue with a register constraint:

  _Complex float c;
  asm goto("" : "=r"(c) : : : l);

Report an error instead. A callbr without indirect targets has no such
path, so it's still allowed.

Assisted-by: Claude Opus 5.5
---
 .../SelectionDAG/SelectionDAGBuilder.cpp      |  8 ++++
 .../CodeGen/X86/callbr-asm-indirect-output.ll | 47 +++++++++++++++++++
 2 files changed, 55 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/callbr-asm-indirect-output.ll

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index 28995545724531..ff1124c443784a 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -11004,6 +11004,14 @@ void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
 
       // Indirect output manifest as stores. Record output chains.
       if (OpInfo.isIndirect) {
+        // The store is only on the path to a callbr's default target, since
+        // the asm can branch to an indirect target before it.
+        if (IsCallBr && cast<CallBrInst>(Call).getNumIndirectDests())
+          DAG.getContext()->diagnose(DiagnosticInfoInlineAsm(
+              Call, "callbr cannot store indirect output for constraint '" +
+                        Twine(OpInfo.ConstraintCode) +
+                        "' on its indirect paths"));
+
         const Value *Ptr = OpInfo.CallOperandVal;
         assert(Ptr && "Expected value CallOperandVal for indirect asm operand");
         SDValue Store = DAG.getStore(Chain, getCurSDLoc(), Val, getValue(Ptr),
diff --git a/llvm/test/CodeGen/X86/callbr-asm-indirect-output.ll b/llvm/test/CodeGen/X86/callbr-asm-indirect-output.ll
new file mode 100644
index 00000000000000..60a3b079feb900
--- /dev/null
+++ b/llvm/test/CodeGen/X86/callbr-asm-indirect-output.ll
@@ -0,0 +1,47 @@
+; RUN: split-file %s %t
+; RUN: not llc -mtriple=x86_64-linux-gnu < %t/error.ll 2>&1 | FileCheck %s --check-prefix=ERROR
+; RUN: llc -mtriple=x86_64-linux-gnu < %t/ok.ll | FileCheck %s --check-prefix=OK
+
+; An indirect output with a register constraint is stored after the asm, but a
+; callbr's asm can branch to an indirect target before then.
+
+;--- error.ll
+; ERROR: error: callbr cannot store indirect output for constraint 'r' on its indirect paths
+define void @indirect_reg_output(ptr %p) {
+entry:
+  callbr void asm "", "=*r,!i"(ptr elementtype(i32) %p)
+          to label %a [label %b]
+a:
+  ret void
+b:
+  ret void
+}
+
+;--- ok.ll
+; Without indirect targets, the store is on the only path.
+; OK-LABEL: no_indirect_targets:
+; OK:       #APP
+; OK-NEXT:  #NO_APP
+; OK-NEXT:  movl %eax, (%rdi)
+define void @no_indirect_targets(ptr %p) {
+entry:
+  callbr void asm "", "=*r"(ptr elementtype(i32) %p)
+          to label %a []
+a:
+  ret void
+}
+
+; The asm writes a memory output itself.
+; OK-LABEL: indirect_mem_output:
+; OK:       #APP
+; OK-NEXT:  # (%rdi)
+; OK-NEXT:  #NO_APP
+define void @indirect_mem_output(ptr %p) {
+entry:
+  callbr void asm "# $0", "=*m,!i"(ptr elementtype(i32) %p)
+          to label %a [label %b]
+a:
+  ret void
+b:
+  ret void
+}



More information about the llvm-branch-commits mailing list