[llvm-branch-commits] [clang] [Clang][CodeGen] Add support for _Complex inline asm register outputs (PR #236695)

Bill Wendling via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Nov 1 03:49:48 PST 2026


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

>From 5f665852cfd45159ce7b9b51daefeb74ea8faa20 Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Thu, 29 Oct 2026 01:49:13 -0700
Subject: [PATCH 1/6] [Clang][CodeGen] Add support for _Complex arguments to
 "asm goto"

Clang emitted "=*r" for _Complex register outputs. On a callbr, the
SelectionDAG stores that output only on the fallthrough path, which
would silently skip adding a "store" on the indirect paths (#235940
turns that into an error). The HandleOutputConstraints method acceps
scalar and aggregate kinds, but not _Complex.

We fix this by treating a _Complex as an aggregate for this specific
case. The infrastructure already exists to handle {T, T} structs, so the
asm goto successor stores come for "free."

Fixes: 235946

Assisted-by: Claude for generating testcases
---
 clang/lib/CodeGen/CGStmt.cpp                  |   3 +-
 clang/lib/CodeGen/CodeGenFunction.h           |   4 +
 .../CodeGen/X86/inline-asm-complex-output.c   | 214 ++++++++++++++++++
 3 files changed, 220 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/CodeGen/X86/inline-asm-complex-output.c

diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 05737ce071647d..9d744867f81b0a 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -2941,7 +2941,8 @@ void AsmConstraintsInfo::HandleOutputConstraints() {
     QualType QTy = OutExpr->getType();
     const bool IsScalarOrAggregate =
         CodeGenFunction::hasScalarEvaluationKind(QTy) ||
-        CodeGenFunction::hasAggregateEvaluationKind(QTy);
+        CodeGenFunction::hasAggregateEvaluationKind(QTy) ||
+        CodeGenFunction::hasComplexEvaluationKind(QTy);
 
     if (!Info.allowsMemory() && IsScalarOrAggregate) {
       Constraints += "=" + OutputConstraint;
diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h
index 4cc0a45067a017..f3f4b0d4f1ebfc 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -2746,6 +2746,10 @@ class CodeGenFunction : public CodeGenTypeCache {
     return getEvaluationKind(T) == TEK_Aggregate;
   }
 
+  static bool hasComplexEvaluationKind(QualType T) {
+    return getEvaluationKind(T) == TEK_Complex;
+  }
+
   /// createBasicBlock - Create an LLVM basic block.
   llvm::BasicBlock *createBasicBlock(const Twine &name = "",
                                      llvm::Function *parent = nullptr,
diff --git a/clang/test/CodeGen/X86/inline-asm-complex-output.c b/clang/test/CodeGen/X86/inline-asm-complex-output.c
new file mode 100644
index 00000000000000..78ee1f82166cb2
--- /dev/null
+++ b/clang/test/CodeGen/X86/inline-asm-complex-output.c
@@ -0,0 +1,214 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --check-globals none --version 6
+// REQUIRES: x86-registered-target
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -DERROR -verify %s -o /dev/null
+
+// A _Complex output with a register constraint is returned by value as an
+// integer of the same size, like a struct, and not stored through a pointer
+// ("=*r"). An asm goto then stores it on every path, not only the fallthrough.
+
+#ifndef ERROR
+
+// CHECK-LABEL: define dso_local void @test_complex_float(
+// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[C:%.*]] = alloca { float, float }, align 4
+// CHECK-NEXT:    [[TMP0:%.*]] = call i64 asm "", "=r,~{dirflag},~{fpsr},~{flags}"() #[[ATTR2:[0-9]+]], !srcloc [[META1:![0-9]+]]
+// CHECK-NEXT:    store i64 [[TMP0]], ptr [[C]], align 4
+// CHECK-NEXT:    ret void
+//
+void test_complex_float(void) {
+  _Complex float c;
+  asm("" : "=r"(c));
+}
+
+// CHECK-LABEL: define dso_local void @test_complex_double(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[C:%.*]] = alloca { double, double }, align 8
+// CHECK-NEXT:    [[TMP0:%.*]] = call i128 asm "", "=r,~{dirflag},~{fpsr},~{flags}"() #[[ATTR2]], !srcloc [[META2:![0-9]+]]
+// CHECK-NEXT:    store i128 [[TMP0]], ptr [[C]], align 8
+// CHECK-NEXT:    ret void
+//
+void test_complex_double(void) {
+  _Complex double c;
+  asm("" : "=r"(c));
+}
+
+// CHECK-LABEL: define dso_local void @test_complex_char(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[C:%.*]] = alloca { i8, i8 }, align 1
+// CHECK-NEXT:    [[TMP0:%.*]] = call i16 asm "", "=r,~{dirflag},~{fpsr},~{flags}"() #[[ATTR2]], !srcloc [[META3:![0-9]+]]
+// CHECK-NEXT:    store i16 [[TMP0]], ptr [[C]], align 1
+// CHECK-NEXT:    ret void
+//
+void test_complex_char(void) {
+  _Complex char c;
+  asm("" : "=r"(c));
+}
+
+// CHECK-LABEL: define dso_local void @test_complex_int(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[C:%.*]] = alloca { i32, i32 }, align 4
+// CHECK-NEXT:    [[TMP0:%.*]] = call i64 asm "", "=r,~{dirflag},~{fpsr},~{flags}"() #[[ATTR2]], !srcloc [[META4:![0-9]+]]
+// CHECK-NEXT:    store i64 [[TMP0]], ptr [[C]], align 4
+// CHECK-NEXT:    ret void
+//
+void test_complex_int(void) {
+  _Complex int c;
+  asm("" : "=r"(c));
+}
+
+// CHECK-LABEL: define dso_local void @test_complex_float_sse(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[C:%.*]] = alloca { float, float }, align 4
+// CHECK-NEXT:    [[TMP0:%.*]] = call i64 asm "", "=x,~{dirflag},~{fpsr},~{flags}"() #[[ATTR2]], !srcloc [[META5:![0-9]+]]
+// CHECK-NEXT:    store i64 [[TMP0]], ptr [[C]], align 4
+// CHECK-NEXT:    ret void
+//
+void test_complex_float_sse(void) {
+  _Complex float c;
+  asm("" : "=x"(c));
+}
+
+// CHECK-LABEL: define dso_local void @test_complex_float_inout(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[C:%.*]] = alloca { float, float }, align 4
+// CHECK-NEXT:    [[C_REALP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[C]], i32 0, i32 0
+// CHECK-NEXT:    [[C_IMAGP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[C]], i32 0, i32 1
+// CHECK-NEXT:    store float 1.000000e+00, ptr [[C_REALP]], align 4
+// CHECK-NEXT:    store float 0.000000e+00, ptr [[C_IMAGP]], align 4
+// CHECK-NEXT:    [[TMP0:%.*]] = load i64, ptr [[C]], align 4
+// CHECK-NEXT:    [[TMP1:%.*]] = call i64 asm "", "=r,0,~{dirflag},~{fpsr},~{flags}"(i64 [[TMP0]]) #[[ATTR2]], !srcloc [[META6:![0-9]+]]
+// CHECK-NEXT:    store i64 [[TMP1]], ptr [[C]], align 4
+// CHECK-NEXT:    ret void
+//
+void test_complex_float_inout(void) {
+  _Complex float c = 1;
+  asm("" : "+r"(c));
+}
+
+// A memory constraint still passes a pointer.
+// CHECK-LABEL: define dso_local void @test_complex_float_mem(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[C:%.*]] = alloca { float, float }, align 4
+// CHECK-NEXT:    call void asm "", "=*m,~{dirflag},~{fpsr},~{flags}"(ptr elementtype({ float, float }) [[C]]) #[[ATTR3:[0-9]+]], !srcloc [[META7:![0-9]+]]
+// CHECK-NEXT:    ret void
+//
+void test_complex_float_mem(void) {
+  _Complex float c;
+  asm("" : "=m"(c));
+}
+
+// CHECK-LABEL: define dso_local <2 x float> @test_complex_float_goto(
+// CHECK-SAME: ) #[[ATTR1:[0-9]+]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[RETVAL:%.*]] = alloca { float, float }, align 4
+// CHECK-NEXT:    [[C:%.*]] = alloca { float, float }, align 4
+// CHECK-NEXT:    [[C_REALP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[C]], i32 0, i32 0
+// CHECK-NEXT:    [[C_IMAGP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[C]], i32 0, i32 1
+// CHECK-NEXT:    store float 0.000000e+00, ptr [[C_REALP]], align 4
+// CHECK-NEXT:    store float 0.000000e+00, ptr [[C_IMAGP]], align 4
+// CHECK-NEXT:    [[TMP0:%.*]] = callbr i64 asm "", "=r,!i,~{dirflag},~{fpsr},~{flags}"() #[[ATTR2]]
+// CHECK-NEXT:            to label %[[ASM_FALLTHROUGH:.*]] [label %[[LABEL_SPLIT:.*]]], !srcloc [[META8:![0-9]+]]
+// CHECK:       [[ASM_FALLTHROUGH]]:
+// CHECK-NEXT:    store i64 [[TMP0]], ptr [[C]], align 4
+// CHECK-NEXT:    [[C_REALP1:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[C]], i32 0, i32 0
+// CHECK-NEXT:    [[C_REAL:%.*]] = load float, ptr [[C_REALP1]], align 4
+// CHECK-NEXT:    [[C_IMAGP2:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[C]], i32 0, i32 1
+// CHECK-NEXT:    [[C_IMAG:%.*]] = load float, ptr [[C_IMAGP2]], align 4
+// CHECK-NEXT:    [[RETVAL_REALP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[RETVAL]], i32 0, i32 0
+// CHECK-NEXT:    [[RETVAL_IMAGP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[RETVAL]], i32 0, i32 1
+// CHECK-NEXT:    store float [[C_REAL]], ptr [[RETVAL_REALP]], align 4
+// CHECK-NEXT:    store float [[C_IMAG]], ptr [[RETVAL_IMAGP]], align 4
+// CHECK-NEXT:    br label %[[RETURN:.*]]
+// CHECK:       [[LABEL_SPLIT]]:
+// CHECK-NEXT:    store i64 [[TMP0]], ptr [[C]], align 4
+// CHECK-NEXT:    br label %[[LABEL:.*]]
+// CHECK:       [[LABEL]]:
+// CHECK-NEXT:    [[C_REALP3:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[C]], i32 0, i32 0
+// CHECK-NEXT:    [[C_REAL4:%.*]] = load float, ptr [[C_REALP3]], align 4
+// CHECK-NEXT:    [[C_IMAGP5:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[C]], i32 0, i32 1
+// CHECK-NEXT:    [[C_IMAG6:%.*]] = load float, ptr [[C_IMAGP5]], align 4
+// CHECK-NEXT:    [[NEG_R:%.*]] = fneg float [[C_REAL4]]
+// CHECK-NEXT:    [[NEG_I:%.*]] = fneg float [[C_IMAG6]]
+// CHECK-NEXT:    [[RETVAL_REALP7:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[RETVAL]], i32 0, i32 0
+// CHECK-NEXT:    [[RETVAL_IMAGP8:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[RETVAL]], i32 0, i32 1
+// CHECK-NEXT:    store float [[NEG_R]], ptr [[RETVAL_REALP7]], align 4
+// CHECK-NEXT:    store float [[NEG_I]], ptr [[RETVAL_IMAGP8]], align 4
+// CHECK-NEXT:    br label %[[RETURN]]
+// CHECK:       [[RETURN]]:
+// CHECK-NEXT:    [[TMP1:%.*]] = load <2 x float>, ptr [[RETVAL]], align 4
+// CHECK-NEXT:    ret <2 x float> [[TMP1]]
+//
+_Complex float test_complex_float_goto(void) {
+  _Complex float c = 0;
+  asm goto("" : "=r"(c) : : : label);
+  return c;
+label:
+  return -c;
+}
+
+// CHECK-LABEL: define dso_local { double, double } @test_complex_double_goto(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[RETVAL:%.*]] = alloca { double, double }, align 8
+// CHECK-NEXT:    [[C:%.*]] = alloca { double, double }, align 8
+// CHECK-NEXT:    [[C_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 0
+// CHECK-NEXT:    [[C_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 1
+// CHECK-NEXT:    store double 0.000000e+00, ptr [[C_REALP]], align 8
+// CHECK-NEXT:    store double 0.000000e+00, ptr [[C_IMAGP]], align 8
+// CHECK-NEXT:    [[TMP0:%.*]] = callbr i128 asm "", "=r,!i,~{dirflag},~{fpsr},~{flags}"() #[[ATTR2]]
+// CHECK-NEXT:            to label %[[ASM_FALLTHROUGH:.*]] [label %[[LABEL_SPLIT:.*]]], !srcloc [[META9:![0-9]+]]
+// CHECK:       [[ASM_FALLTHROUGH]]:
+// CHECK-NEXT:    store i128 [[TMP0]], ptr [[C]], align 8
+// CHECK-NEXT:    [[C_REALP1:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 0
+// CHECK-NEXT:    [[C_REAL:%.*]] = load double, ptr [[C_REALP1]], align 8
+// CHECK-NEXT:    [[C_IMAGP2:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 1
+// CHECK-NEXT:    [[C_IMAG:%.*]] = load double, ptr [[C_IMAGP2]], align 8
+// CHECK-NEXT:    [[RETVAL_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[RETVAL]], i32 0, i32 0
+// CHECK-NEXT:    [[RETVAL_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[RETVAL]], i32 0, i32 1
+// CHECK-NEXT:    store double [[C_REAL]], ptr [[RETVAL_REALP]], align 8
+// CHECK-NEXT:    store double [[C_IMAG]], ptr [[RETVAL_IMAGP]], align 8
+// CHECK-NEXT:    br label %[[RETURN:.*]]
+// CHECK:       [[LABEL_SPLIT]]:
+// CHECK-NEXT:    store i128 [[TMP0]], ptr [[C]], align 8
+// CHECK-NEXT:    br label %[[LABEL:.*]]
+// CHECK:       [[LABEL]]:
+// CHECK-NEXT:    [[C_REALP3:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 0
+// CHECK-NEXT:    [[C_REAL4:%.*]] = load double, ptr [[C_REALP3]], align 8
+// CHECK-NEXT:    [[C_IMAGP5:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 1
+// CHECK-NEXT:    [[C_IMAG6:%.*]] = load double, ptr [[C_IMAGP5]], align 8
+// CHECK-NEXT:    [[NEG_R:%.*]] = fneg double [[C_REAL4]]
+// CHECK-NEXT:    [[NEG_I:%.*]] = fneg double [[C_IMAG6]]
+// CHECK-NEXT:    [[RETVAL_REALP7:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[RETVAL]], i32 0, i32 0
+// CHECK-NEXT:    [[RETVAL_IMAGP8:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[RETVAL]], i32 0, i32 1
+// CHECK-NEXT:    store double [[NEG_R]], ptr [[RETVAL_REALP7]], align 8
+// CHECK-NEXT:    store double [[NEG_I]], ptr [[RETVAL_IMAGP8]], align 8
+// CHECK-NEXT:    br label %[[RETURN]]
+// CHECK:       [[RETURN]]:
+// CHECK-NEXT:    [[TMP1:%.*]] = load { double, double }, ptr [[RETVAL]], align 8
+// CHECK-NEXT:    ret { double, double } [[TMP1]]
+//
+_Complex double test_complex_double_goto(void) {
+  _Complex double c = 0;
+  asm goto("" : "=r"(c) : : : label);
+  return c;
+label:
+  return -c;
+}
+
+#else
+
+// No integer type is 256 bits wide.
+void test_complex_long_double(void) {
+  _Complex long double c;
+  asm("" : "=r"(c)); // expected-error {{impossible constraint in asm: cannot store value into a register}}
+}
+
+#endif

>From b4b69afa12db8541f6678950da16dfa74a6d866b Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Thu, 29 Oct 2026 02:58:45 -0700
Subject: [PATCH 2/6] Test the TBAA tag of a _Complex output's store

Assisted-by: Claude Opus 5.5
---
 clang/test/CodeGen/X86/inline-asm-output-tbaa.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/clang/test/CodeGen/X86/inline-asm-output-tbaa.c b/clang/test/CodeGen/X86/inline-asm-output-tbaa.c
index 9c6ab17d6818e0..90cb24d0c899e6 100644
--- a/clang/test/CodeGen/X86/inline-asm-output-tbaa.c
+++ b/clang/test/CodeGen/X86/inline-asm-output-tbaa.c
@@ -2,9 +2,9 @@
 // Clang emits TBAA only when optimizing, so this test needs -O1.
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -O1 -disable-llvm-passes -emit-llvm %s -o - | FileCheck %s
 
-// A struct output with a register constraint is returned as an integer and
-// stored to the destination. The store must not have the integer's TBAA tag,
-// or it wouldn't alias accesses to the destination's members.
+// A struct or _Complex output with a register constraint is returned as an
+// integer and stored to the destination. The store must not have the integer's
+// TBAA tag, or it wouldn't alias accesses to the destination's members.
 
 struct S {
   float a, b;
@@ -17,5 +17,12 @@ void test_struct_output(struct S *p) {
   asm("" : "=r"(*p));
 }
 
+// CHECK-LABEL: define {{.*}}void @test_complex_output(
+// CHECK:         [[RES:%.*]] = call i64 asm "", "=r,
+// CHECK-NEXT:    store i64 [[RES]], ptr %{{.*}}, align 4, !tbaa [[CHAR_TAG]]
+void test_complex_output(_Complex float *p) {
+  asm("" : "=r"(*p));
+}
+
 // CHECK-DAG: [[CHAR_TAG]] = !{[[CHAR:![0-9]+]], [[CHAR]], i64 0}
 // CHECK-DAG: [[CHAR]] = !{!"omnipotent char",

>From 2c7736483f757cd65fee05e49c6652d3f7b05bc3 Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Thu, 29 Oct 2026 03:22:46 -0700
Subject: [PATCH 3/6] [Clang][CodeGen] The predicate 'IsScalarOrAggregate' is
 always true

IsScalarOrAggregate checks whether *any* 'TEK_*' evaluation kind is set,
which it always will be. Go ahead and remove it and assume it's true.
---
 clang/lib/CodeGen/CGStmt.cpp        | 7 +------
 clang/lib/CodeGen/CodeGenFunction.h | 4 ----
 2 files changed, 1 insertion(+), 10 deletions(-)

diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 1a7ca46078266f..c09341f44be407 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -2939,12 +2939,7 @@ void AsmConstraintsInfo::HandleOutputConstraints() {
     // If this is a register output, then make the inline asm return it
     // by-value.  If this is a memory result, return the value by-reference.
     QualType QTy = OutExpr->getType();
-    const bool IsScalarOrAggregate =
-        CodeGenFunction::hasScalarEvaluationKind(QTy) ||
-        CodeGenFunction::hasAggregateEvaluationKind(QTy) ||
-        CodeGenFunction::hasComplexEvaluationKind(QTy);
-
-    if (!Info.allowsMemory() && IsScalarOrAggregate) {
+    if (!Info.allowsMemory()) {
       Constraints += "=" + OutputConstraint;
       ResultRegQualTys.push_back(QTy);
       ResultRegDests.push_back(Dest);
diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h
index f3f4b0d4f1ebfc..4cc0a45067a017 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -2746,10 +2746,6 @@ class CodeGenFunction : public CodeGenTypeCache {
     return getEvaluationKind(T) == TEK_Aggregate;
   }
 
-  static bool hasComplexEvaluationKind(QualType T) {
-    return getEvaluationKind(T) == TEK_Complex;
-  }
-
   /// createBasicBlock - Create an LLVM basic block.
   llvm::BasicBlock *createBasicBlock(const Twine &name = "",
                                      llvm::Function *parent = nullptr,

>From 75bf7e1e0c6bfdfde2d79fb61f5162b24cab0b89 Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Thu, 29 Oct 2026 03:31:16 -0700
Subject: [PATCH 4/6] [Clang][CodeGen] Add testcases for "+r" in "asm goto" and
 mixed complex and scalar outputs

---
 .../CodeGen/X86/inline-asm-complex-output.c   | 140 ++++++++++++++++++
 1 file changed, 140 insertions(+)

diff --git a/clang/test/CodeGen/X86/inline-asm-complex-output.c b/clang/test/CodeGen/X86/inline-asm-complex-output.c
index 78ee1f82166cb2..a64884033662ac 100644
--- a/clang/test/CodeGen/X86/inline-asm-complex-output.c
+++ b/clang/test/CodeGen/X86/inline-asm-complex-output.c
@@ -203,6 +203,146 @@ _Complex double test_complex_double_goto(void) {
   return -c;
 }
 
+// CHECK-LABEL: define dso_local <2 x float> @test_complex_float_inout_goto(
+// CHECK-SAME: <2 x float> noundef [[C_COERCE:%.*]]) #[[ATTR1]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[RETVAL:%.*]] = alloca { float, float }, align 4
+// CHECK-NEXT:    [[C:%.*]] = alloca { float, float }, align 4
+// CHECK-NEXT:    store <2 x float> [[C_COERCE]], ptr [[C]], align 4
+// CHECK-NEXT:    [[TMP0:%.*]] = load i64, ptr [[C]], align 4
+// CHECK-NEXT:    [[TMP1:%.*]] = callbr i64 asm "", "=r,0,!i,~{dirflag},~{fpsr},~{flags}"(i64 [[TMP0]]) #[[ATTR2]]
+// CHECK-NEXT:            to label %[[ASM_FALLTHROUGH:.*]] [label %[[LABEL_SPLIT:.*]]], !srcloc [[META10:![0-9]+]]
+// CHECK:       [[ASM_FALLTHROUGH]]:
+// CHECK-NEXT:    store i64 [[TMP1]], ptr [[C]], align 4
+// CHECK-NEXT:    [[C_REALP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[C]], i32 0, i32 0
+// CHECK-NEXT:    [[C_REAL:%.*]] = load float, ptr [[C_REALP]], align 4
+// CHECK-NEXT:    [[C_IMAGP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[C]], i32 0, i32 1
+// CHECK-NEXT:    [[C_IMAG:%.*]] = load float, ptr [[C_IMAGP]], align 4
+// CHECK-NEXT:    [[RETVAL_REALP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[RETVAL]], i32 0, i32 0
+// CHECK-NEXT:    [[RETVAL_IMAGP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[RETVAL]], i32 0, i32 1
+// CHECK-NEXT:    store float [[C_REAL]], ptr [[RETVAL_REALP]], align 4
+// CHECK-NEXT:    store float [[C_IMAG]], ptr [[RETVAL_IMAGP]], align 4
+// CHECK-NEXT:    br label %[[RETURN:.*]]
+// CHECK:       [[LABEL_SPLIT]]:
+// CHECK-NEXT:    store i64 [[TMP1]], ptr [[C]], align 4
+// CHECK-NEXT:    br label %[[LABEL:.*]]
+// CHECK:       [[LABEL]]:
+// CHECK-NEXT:    [[C_REALP1:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[C]], i32 0, i32 0
+// CHECK-NEXT:    [[C_REAL2:%.*]] = load float, ptr [[C_REALP1]], align 4
+// CHECK-NEXT:    [[C_IMAGP3:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[C]], i32 0, i32 1
+// CHECK-NEXT:    [[C_IMAG4:%.*]] = load float, ptr [[C_IMAGP3]], align 4
+// CHECK-NEXT:    [[NEG_R:%.*]] = fneg float [[C_REAL2]]
+// CHECK-NEXT:    [[NEG_I:%.*]] = fneg float [[C_IMAG4]]
+// CHECK-NEXT:    [[RETVAL_REALP5:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[RETVAL]], i32 0, i32 0
+// CHECK-NEXT:    [[RETVAL_IMAGP6:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[RETVAL]], i32 0, i32 1
+// CHECK-NEXT:    store float [[NEG_R]], ptr [[RETVAL_REALP5]], align 4
+// CHECK-NEXT:    store float [[NEG_I]], ptr [[RETVAL_IMAGP6]], align 4
+// CHECK-NEXT:    br label %[[RETURN]]
+// CHECK:       [[RETURN]]:
+// CHECK-NEXT:    [[TMP2:%.*]] = load <2 x float>, ptr [[RETVAL]], align 4
+// CHECK-NEXT:    ret <2 x float> [[TMP2]]
+//
+_Complex float test_complex_float_inout_goto(_Complex float c) {
+  asm goto("" : "+r"(c) : : : label);
+  return c;
+label:
+  return -c;
+}
+
+// The asm returns both outputs in one struct, and each path stores both.
+// CHECK-LABEL: define dso_local i32 @test_complex_float_and_int_goto(
+// CHECK-SAME: ptr noundef [[P:%.*]]) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[RETVAL:%.*]] = alloca i32, align 4
+// CHECK-NEXT:    [[P_ADDR:%.*]] = alloca ptr, align 8
+// CHECK-NEXT:    [[I:%.*]] = alloca i32, align 4
+// CHECK-NEXT:    store ptr [[P]], ptr [[P_ADDR]], align 8
+// CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[P_ADDR]], align 8
+// CHECK-NEXT:    [[TMP1:%.*]] = callbr { i64, i32 } asm "", "=r,=r,!i,~{dirflag},~{fpsr},~{flags}"() #[[ATTR2]]
+// CHECK-NEXT:            to label %[[ASM_FALLTHROUGH:.*]] [label %[[LABEL_SPLIT:.*]]], !srcloc [[META11:![0-9]+]]
+// CHECK:       [[ASM_FALLTHROUGH]]:
+// CHECK-NEXT:    [[ASMRESULT:%.*]] = extractvalue { i64, i32 } [[TMP1]], 0
+// CHECK-NEXT:    [[ASMRESULT1:%.*]] = extractvalue { i64, i32 } [[TMP1]], 1
+// CHECK-NEXT:    store i64 [[ASMRESULT]], ptr [[TMP0]], align 4
+// CHECK-NEXT:    store i32 [[ASMRESULT1]], ptr [[I]], align 4
+// CHECK-NEXT:    [[TMP2:%.*]] = load i32, ptr [[I]], align 4
+// CHECK-NEXT:    store i32 [[TMP2]], ptr [[RETVAL]], align 4
+// CHECK-NEXT:    br label %[[RETURN:.*]]
+// CHECK:       [[LABEL_SPLIT]]:
+// CHECK-NEXT:    [[ASMRESULT2:%.*]] = extractvalue { i64, i32 } [[TMP1]], 0
+// CHECK-NEXT:    [[ASMRESULT3:%.*]] = extractvalue { i64, i32 } [[TMP1]], 1
+// CHECK-NEXT:    store i64 [[ASMRESULT2]], ptr [[TMP0]], align 4
+// CHECK-NEXT:    store i32 [[ASMRESULT3]], ptr [[I]], align 4
+// CHECK-NEXT:    br label %[[LABEL:.*]]
+// CHECK:       [[LABEL]]:
+// CHECK-NEXT:    [[TMP3:%.*]] = load i32, ptr [[I]], align 4
+// CHECK-NEXT:    [[SUB:%.*]] = sub nsw i32 0, [[TMP3]]
+// CHECK-NEXT:    store i32 [[SUB]], ptr [[RETVAL]], align 4
+// CHECK-NEXT:    br label %[[RETURN]]
+// CHECK:       [[RETURN]]:
+// CHECK-NEXT:    [[TMP4:%.*]] = load i32, ptr [[RETVAL]], align 4
+// CHECK-NEXT:    ret i32 [[TMP4]]
+//
+int test_complex_float_and_int_goto(_Complex float *p) {
+  int i;
+  asm goto("" : "=r"(*p), "=r"(i) : : : label);
+  return i;
+label:
+  return -i;
+}
+
+// CHECK-LABEL: define dso_local void @test_complex_float_volatile(
+// CHECK-SAME: ptr noundef [[P:%.*]]) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[P_ADDR:%.*]] = alloca ptr, align 8
+// CHECK-NEXT:    store ptr [[P]], ptr [[P_ADDR]], align 8
+// CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[P_ADDR]], align 8
+// CHECK-NEXT:    [[TMP1:%.*]] = call i64 asm "", "=r,~{dirflag},~{fpsr},~{flags}"() #[[ATTR2]], !srcloc [[META12:![0-9]+]]
+// CHECK-NEXT:    store volatile i64 [[TMP1]], ptr [[TMP0]], align 4
+// CHECK-NEXT:    ret void
+//
+void test_complex_float_volatile(volatile _Complex float *p) {
+  asm("" : "=r"(*p));
+}
+
+// An output tied to a larger input makes the asm return the input's type, but
+// only the destination's bits are stored.
+// CHECK-LABEL: define dso_local void @test_complex_char_tied_int(
+// CHECK-SAME: ptr noundef [[P:%.*]], i32 noundef [[X:%.*]]) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[P_ADDR:%.*]] = alloca ptr, align 8
+// CHECK-NEXT:    [[X_ADDR:%.*]] = alloca i32, align 4
+// CHECK-NEXT:    store ptr [[P]], ptr [[P_ADDR]], align 8
+// CHECK-NEXT:    store i32 [[X]], ptr [[X_ADDR]], align 4
+// CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[P_ADDR]], align 8
+// CHECK-NEXT:    [[TMP1:%.*]] = load i32, ptr [[X_ADDR]], align 4
+// CHECK-NEXT:    [[TMP2:%.*]] = call i32 asm "", "=r,0,~{dirflag},~{fpsr},~{flags}"(i32 [[TMP1]]) #[[ATTR2]], !srcloc [[META13:![0-9]+]]
+// CHECK-NEXT:    [[TMP3:%.*]] = trunc i32 [[TMP2]] to i16
+// CHECK-NEXT:    store i16 [[TMP3]], ptr [[TMP0]], align 1
+// CHECK-NEXT:    ret void
+//
+void test_complex_char_tied_int(_Complex char *p, int x) {
+  asm("" : "=r"(*p) : "0"(x));
+}
+
+// CHECK-LABEL: define dso_local void @test_complex_char_tied_pointer(
+// CHECK-SAME: ptr noundef [[P:%.*]], ptr noundef [[X:%.*]]) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[P_ADDR:%.*]] = alloca ptr, align 8
+// CHECK-NEXT:    [[X_ADDR:%.*]] = alloca ptr, align 8
+// CHECK-NEXT:    store ptr [[P]], ptr [[P_ADDR]], align 8
+// CHECK-NEXT:    store ptr [[X]], ptr [[X_ADDR]], align 8
+// CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[P_ADDR]], align 8
+// CHECK-NEXT:    [[TMP1:%.*]] = load ptr, ptr [[X_ADDR]], align 8
+// CHECK-NEXT:    [[TMP2:%.*]] = call ptr asm "", "=r,0,~{dirflag},~{fpsr},~{flags}"(ptr [[TMP1]]) #[[ATTR2]], !srcloc [[META14:![0-9]+]]
+// CHECK-NEXT:    [[TMP3:%.*]] = ptrtoint ptr [[TMP2]] to i16
+// CHECK-NEXT:    store i16 [[TMP3]], ptr [[TMP0]], align 1
+// CHECK-NEXT:    ret void
+//
+void test_complex_char_tied_pointer(_Complex char *p, void *x) {
+  asm("" : "=r"(*p) : "0"(x));
+}
+
 #else
 
 // No integer type is 256 bits wide.

>From 46c44daa58b80e9db4701eb855e8d02cee8a33aa Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Thu, 29 Oct 2026 04:01:07 -0700
Subject: [PATCH 5/6] Test a _Complex output in a mask register

Assisted-by: Claude Opus 5.5
---
 .../test/CodeGen/X86/inline-asm-mask-output.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/clang/test/CodeGen/X86/inline-asm-mask-output.c b/clang/test/CodeGen/X86/inline-asm-mask-output.c
index 925d31c799a3e4..409c99551492ff 100644
--- a/clang/test/CodeGen/X86/inline-asm-mask-output.c
+++ b/clang/test/CodeGen/X86/inline-asm-mask-output.c
@@ -2,8 +2,8 @@
 // REQUIRES: x86-registered-target
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -target-feature +avx512f -emit-llvm %s -o - | FileCheck %s
 
-// A struct output in a mask register ("k") is returned as a vector of i1. It's
-// stored to the destination as an integer of the destination's size.
+// A struct or _Complex output in a mask register ("k") is returned as a vector
+// of i1. It's stored to the destination as an integer of the destination's size.
 
 struct C {
   char a, b;
@@ -61,3 +61,18 @@ void test_mask_inout(struct C *p) {
 void test_mask_tied_int(struct C *p, int x) {
   asm("" : "=k"(*p) : "0"(x));
 }
+
+// CHECK-LABEL: define dso_local void @test_complex_mask_output(
+// CHECK-SAME: ptr noundef [[P:%.*]]) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[P_ADDR:%.*]] = alloca ptr, align 8
+// CHECK-NEXT:    store ptr [[P]], ptr [[P_ADDR]], align 8
+// CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[P_ADDR]], align 8
+// CHECK-NEXT:    [[TMP1:%.*]] = call <16 x i1> asm "", "=k,~{dirflag},~{fpsr},~{flags}"() #[[ATTR2]], !srcloc [[META4:![0-9]+]]
+// CHECK-NEXT:    [[TMP2:%.*]] = bitcast <16 x i1> [[TMP1]] to i16
+// CHECK-NEXT:    store i16 [[TMP2]], ptr [[TMP0]], align 1
+// CHECK-NEXT:    ret void
+//
+void test_complex_mask_output(_Complex char *p) {
+  asm("" : "=k"(*p));
+}

>From 7a55d30343b057885c78b74a4ad265925677f578 Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Thu, 29 Oct 2026 21:25:08 -0700
Subject: [PATCH 6/6] Update a _Complex test for the bit-or-pointer cast

Assisted-by: Claude Opus 5.5
---
 clang/test/CodeGen/X86/inline-asm-complex-output.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/clang/test/CodeGen/X86/inline-asm-complex-output.c b/clang/test/CodeGen/X86/inline-asm-complex-output.c
index a64884033662ac..d30a15d8c6df06 100644
--- a/clang/test/CodeGen/X86/inline-asm-complex-output.c
+++ b/clang/test/CodeGen/X86/inline-asm-complex-output.c
@@ -335,8 +335,9 @@ void test_complex_char_tied_int(_Complex char *p, int x) {
 // CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[P_ADDR]], align 8
 // CHECK-NEXT:    [[TMP1:%.*]] = load ptr, ptr [[X_ADDR]], align 8
 // CHECK-NEXT:    [[TMP2:%.*]] = call ptr asm "", "=r,0,~{dirflag},~{fpsr},~{flags}"(ptr [[TMP1]]) #[[ATTR2]], !srcloc [[META14:![0-9]+]]
-// CHECK-NEXT:    [[TMP3:%.*]] = ptrtoint ptr [[TMP2]] to i16
-// CHECK-NEXT:    store i16 [[TMP3]], ptr [[TMP0]], align 1
+// CHECK-NEXT:    [[TMP3:%.*]] = ptrtoint ptr [[TMP2]] to i64
+// CHECK-NEXT:    [[TMP4:%.*]] = trunc i64 [[TMP3]] to i16
+// CHECK-NEXT:    store i16 [[TMP4]], ptr [[TMP0]], align 1
 // CHECK-NEXT:    ret void
 //
 void test_complex_char_tied_pointer(_Complex char *p, void *x) {



More information about the llvm-branch-commits mailing list