[clang] [Clang][CodeGen] Report an unstorable asm register output once (PR #239137)

Bill Wendling via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 5 00:22:48 PST 2026


https://github.com/isanbard created https://github.com/llvm/llvm-project/pull/239137

A register output that no integer type is as wide as, such as a 32-byte
struct with "=r", was reported when its store was emitted. An asm goto
emits its stores once for each of its targets, so the error repeated for
each one. It also pointed at the wrong operand when a memory output came
first, since the store's index counts only register outputs.

Report it once, when the output's register type is chosen, at the
output's own expression. This also drops a second error that followed
"output size should not be zero".

Assisted-by: Claude Opus 5.5

>From 44a6b47cf57d19a60f5fbadb7c10292d097d241a Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Thu, 5 Nov 2026 00:13:01 -0800
Subject: [PATCH] [Clang][CodeGen] Report an unstorable asm register output
 once

A register output that no integer type is as wide as, such as a 32-byte
struct with "=r", was reported when its store was emitted. An asm goto
emits its stores once for each of its targets, so the error repeated for
each one. It also pointed at the wrong operand when a memory output came
first, since the store's index counts only register outputs.

Report it once, when the output's register type is chosen, at the
output's own expression. This also drops a second error that followed
"output size should not be zero".

Assisted-by: Claude Opus 5.5
---
 clang/lib/CodeGen/CGStmt.cpp                  | 24 +++++++++++-----
 .../X86/inline-asm-store-to-reg-error.c       | 28 +++++++++++++++++++
 clang/test/CodeGen/inline-asm-size-zero.c     |  2 +-
 3 files changed, 46 insertions(+), 8 deletions(-)
 create mode 100644 clang/test/CodeGen/X86/inline-asm-store-to-reg-error.c

diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 3f02803fa0264b..877d130bd00ad9 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -2967,10 +2967,22 @@ void AsmConstraintsInfo::HandleOutputConstraints() {
       ResultTypeRequiresCast.push_back(RequiresCast);
 
       if (RequiresCast) {
-        if (unsigned Size = getContext().getTypeSize(QTy))
+        if (unsigned Size = getContext().getTypeSize(QTy)) {
           Ty = llvm::IntegerType::get(getLLVMContext(), Size);
-        else
+
+          // The output is stored through an integer type of its size, so
+          // report here if there isn't one. The stores are emitted again for
+          // each target of an asm goto.
+          if (!getTargetHooks().isScalarizableAsmOperand(
+                  CGF, ResultTruncRegTypes.back()) &&
+              getContext()
+                  .getIntTypeForBitwidth(Size, /*Signed=*/false)
+                  .isNull())
+            CGM.getDiags().Report(OutExpr->getExprLoc(),
+                                  diag::err_store_value_to_reg);
+        } else {
           CGM.Error(OutExpr->getExprLoc(), "output size should not be zero");
+        }
       }
 
       ResultRegTypes.push_back(Ty);
@@ -3393,13 +3405,11 @@ void AsmConstraintsInfo::EmitAsmStores(
         continue;
       }
 
+      // HandleOutputConstraints() already reported that no integer type has
+      // the output's size.
       QualType Ty = getContext().getIntTypeForBitwidth(Size, /*Signed=*/false);
-      if (Ty.isNull()) {
-        const Expr *OutExpr = S.getOutputExpr(i);
-        CGM.getDiags().Report(OutExpr->getExprLoc(),
-                              diag::err_store_value_to_reg);
+      if (Ty.isNull())
         return;
-      }
 
       // The output's type may not be an integer: an output tied to a larger
       // input has the input's type (e.g., a pointer), and a mask register
diff --git a/clang/test/CodeGen/X86/inline-asm-store-to-reg-error.c b/clang/test/CodeGen/X86/inline-asm-store-to-reg-error.c
new file mode 100644
index 00000000000000..52fe9dbaa55a84
--- /dev/null
+++ b/clang/test/CodeGen/X86/inline-asm-store-to-reg-error.c
@@ -0,0 +1,28 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -verify -DASM_GOTO %s -o /dev/null
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -verify %s -o /dev/null
+
+// No integer type is 256 bits wide, so a 32-byte struct can't come back from
+// an asm in registers. That's reported once, at the output that can't. Code
+// generation stops after its first error, so each case is its own run.
+
+struct S32 { long a, b, c, d; };
+
+#ifdef ASM_GOTO
+
+// Not once for each place an asm goto stores its outputs.
+void test_asm_goto(struct S32 *p) {
+  asm goto("" : "=r"(*p) : : : l1, l2); // expected-error {{impossible constraint in asm: cannot store value into a register}}
+l1:
+l2:;
+}
+
+#else
+
+// Not at the output before it, which isn't returned in a register.
+void test_after_memory_output(int *m, struct S32 *p) {
+  asm(""
+      : "=m"(*m),
+        "=r"(*p)); // expected-error {{impossible constraint in asm: cannot store value into a register}}
+}
+
+#endif
diff --git a/clang/test/CodeGen/inline-asm-size-zero.c b/clang/test/CodeGen/inline-asm-size-zero.c
index 564f5207d1e711..33c1cfd70de70e 100644
--- a/clang/test/CodeGen/inline-asm-size-zero.c
+++ b/clang/test/CodeGen/inline-asm-size-zero.c
@@ -1,4 +1,4 @@
-// RUN: not %clang_cc1 -S %s -verify -o -
+// RUN: %clang_cc1 -S %s -verify -o -
 
 void foo(void) {
     extern long bar[];



More information about the cfe-commits mailing list