[llvm-branch-commits] [clang] [llvm] [Clang][CodeGen] Add support for _Complex inline asm register operands (PR #236695)
Bill Wendling via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Nov 19 21:09:14 PST 2026
https://github.com/isanbard updated https://github.com/llvm/llvm-project/pull/236695
>From f3f04664fcc5f4ccceafe55cf298caf230492f53 Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Mon, 2 Nov 2026 14:03:38 -0800
Subject: [PATCH 01/12] [Clang][CodeGen] Don't crash on a struct inline asm
output in a mask register
A struct register output with "=k" crashes when x86's
'adjustInlineAsmType' returned '<N x i1>' for "k". The conversion chain
then bitcast that vector to the struct type, which is invalid IR.
Fix this by casting the vector to an integer that's the same size as the
vector, then truncate it to the destination's size.
Fixes: 8d6e82d501cd ("[X86] Use vXi1 for `k` constraint in inline asm (#77733)")
Assisted-by: Claude Opus 5.5
---
clang/lib/CodeGen/CGStmt.cpp | 21 ++++---
.../test/CodeGen/X86/inline-asm-mask-output.c | 63 +++++++++++++++++++
2 files changed, 76 insertions(+), 8 deletions(-)
create mode 100644 clang/test/CodeGen/X86/inline-asm-mask-output.c
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index f78a6f54820e93..3f02803fa0264b 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -3348,9 +3348,15 @@ void AsmConstraintsInfo::EmitAsmStores(
Builder.CreateCall(FnAssume, IsBooleanValue);
}
+ // ResultTypeRequiresCast elements correspond to the first
+ // ResultTypeRequiresCast.size() elements of RegResults.
+ const bool RequiresCast =
+ i < ResultTypeRequiresCast.size() && ResultTypeRequiresCast[i];
+
// If the result type of the LLVM IR asm doesn't match the result type of
- // the expression, do the conversion.
- if (ResultRegTypes[i] != TruncTy) {
+ // the expression, do the conversion. An output returned as an integer is
+ // converted where it's stored below.
+ if (ResultRegTypes[i] != TruncTy && !RequiresCast) {
// Truncate the integer result to the right size, note that TruncTy can be
// a pointer.
if (TruncTy->isFloatingPointTy())
@@ -3376,9 +3382,7 @@ void AsmConstraintsInfo::EmitAsmStores(
ApplyAtomGroup Grp(CGF.getDebugInfo());
LValue Dest = ResultRegDests[i];
- // ResultTypeRequiresCast elements correspond to the first
- // ResultTypeRequiresCast.size() elements of RegResults.
- if (i < ResultTypeRequiresCast.size() && ResultTypeRequiresCast[i]) {
+ if (RequiresCast) {
unsigned Size = getContext().getTypeSize(ResultRegQualTys[i]);
Address A = Dest.getAddress().withElementType(ResultRegTypes[i]);
@@ -3397,9 +3401,10 @@ void AsmConstraintsInfo::EmitAsmStores(
return;
}
- // An output tied to a larger input has the input's type, which may not be
- // an integer (e.g., a pointer). Convert it to an integer of the same
- // size, then store only the destination's bits.
+ // 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
+ // output ("k" on X86) is a vector of i1. Convert it to an integer of the
+ // same size, then store only the destination's bits.
llvm::Type *IntTy = llvm::IntegerType::get(CTX, Size);
uint64_t TmpSize = CGM.getDataLayout().getTypeSizeInBits(Tmp->getType());
Tmp = Builder.CreateBitOrPointerCast(
diff --git a/clang/test/CodeGen/X86/inline-asm-mask-output.c b/clang/test/CodeGen/X86/inline-asm-mask-output.c
new file mode 100644
index 00000000000000..925d31c799a3e4
--- /dev/null
+++ b/clang/test/CodeGen/X86/inline-asm-mask-output.c
@@ -0,0 +1,63 @@
+// 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 -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.
+
+struct C {
+ char a, b;
+};
+
+// CHECK-LABEL: define dso_local void @test_mask_output(
+// CHECK-SAME: ptr noundef [[P:%.*]]) #[[ATTR0:[0-9]+]] {
+// 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:[0-9]+]], !srcloc [[META1:![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_mask_output(struct C *p) {
+ asm("" : "=k"(*p));
+}
+
+// CHECK-LABEL: define dso_local void @test_mask_inout(
+// 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:%.*]] = load i16, ptr [[TMP0]], align 1
+// CHECK-NEXT: [[TMP2:%.*]] = bitcast i16 [[TMP1]] to <16 x i1>
+// CHECK-NEXT: [[TMP3:%.*]] = call <16 x i1> asm "", "=k,0,~{dirflag},~{fpsr},~{flags}"(<16 x i1> [[TMP2]]) #[[ATTR2]], !srcloc [[META2:![0-9]+]]
+// CHECK-NEXT: [[TMP4:%.*]] = bitcast <16 x i1> [[TMP3]] to i16
+// CHECK-NEXT: store i16 [[TMP4]], ptr [[TMP0]], align 1
+// CHECK-NEXT: ret void
+//
+void test_mask_inout(struct C *p) {
+ asm("" : "+k"(*p));
+}
+
+// An output tied to a larger input is returned as a vector of the input's size.
+// CHECK-LABEL: define dso_local void @test_mask_tied_int(
+// CHECK-SAME: ptr noundef [[P:%.*]], i32 noundef [[X:%.*]]) #[[ATTR1:[0-9]+]] {
+// 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:%.*]] = bitcast i32 [[TMP1]] to <32 x i1>
+// CHECK-NEXT: [[TMP3:%.*]] = call <32 x i1> asm "", "=k,0,~{dirflag},~{fpsr},~{flags}"(<32 x i1> [[TMP2]]) #[[ATTR2]], !srcloc [[META3:![0-9]+]]
+// CHECK-NEXT: [[TMP4:%.*]] = bitcast <32 x i1> [[TMP3]] to i32
+// CHECK-NEXT: [[TMP5:%.*]] = trunc i32 [[TMP4]] to i16
+// CHECK-NEXT: store i16 [[TMP5]], ptr [[TMP0]], align 1
+// CHECK-NEXT: ret void
+//
+void test_mask_tied_int(struct C *p, int x) {
+ asm("" : "=k"(*p) : "0"(x));
+}
>From 10add08a2efe578a016fd11eb5a706d5e995d28b 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 02/12] [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 3f02803fa0264b..50ee5c022d473b 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -2948,7 +2948,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 22f72ead623700..3c0ba2e6e07351 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -2747,6 +2747,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 5fa44e7370cf45fff89d6241c2de884ea593cc6d 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 03/12] 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 3646992960b41702e3f19fd6ab7d909f3cead395 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 04/12] [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 50ee5c022d473b..4e6ef629088790 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -2946,12 +2946,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 3c0ba2e6e07351..22f72ead623700 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -2747,10 +2747,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 c889c45c8a9591fe058a54226bc7aa64e33b0d76 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 05/12] [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 7b38eb8cdb7b836ea42755acbadb701ad19898c7 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 06/12] 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 58f4e459af0eb3f6a2d4c3f56869525c5b397905 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 07/12] 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) {
>From 14a3c75a48a0eb955f2e7f08141d493fc7ff2b6e Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Mon, 2 Nov 2026 15:39:05 -0800
Subject: [PATCH 08/12] [GlobalISel] Fall back on tied inline asm inputs
needing several registers
InlineAsmLowering ties a matching input to a single register, and
asserted that the output it matches uses only one. But an i128 output
with an "r" constraint on AArch64 uses two 64-bit registers, so '"+r"'
on an '__int128' crashed at -O0, where GlobalISel is the default. Fall
back to SelectionDAG, which ties each register, instead.
Assisted-by: Claude Opus 5.5
---
llvm/lib/CodeGen/GlobalISel/InlineAsmLowering.cpp | 10 +++++++++-
llvm/test/CodeGen/AArch64/GlobalISel/arm64-fallback.ll | 10 ++++++++++
2 files changed, 19 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/CodeGen/GlobalISel/InlineAsmLowering.cpp b/llvm/lib/CodeGen/GlobalISel/InlineAsmLowering.cpp
index 19eae930cc26b1..acf9d0b7408d85 100644
--- a/llvm/lib/CodeGen/GlobalISel/InlineAsmLowering.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/InlineAsmLowering.cpp
@@ -406,7 +406,15 @@ bool InlineAsmLowering::lowerInlineAsm(
unsigned InstFlagIdx = StartIdx;
for (unsigned i = 0; i < DefIdx; ++i)
InstFlagIdx += getNumOpRegs(*Inst, InstFlagIdx) + 1;
- assert(getNumOpRegs(*Inst, InstFlagIdx) == 1 && "Wrong flag");
+
+ // Only one register is tied below, so let SelectionDAG tie each
+ // register of an output that needs several, such as an i128 in two
+ // 64-bit registers.
+ if (getNumOpRegs(*Inst, InstFlagIdx) != 1) {
+ LLVM_DEBUG(dbgs() << "Matching input constraint to an output with "
+ "several registers not supported yet\n");
+ return false;
+ }
const InlineAsm::Flag MatchedOperandFlag(Inst->getOperand(InstFlagIdx).getImm());
if (MatchedOperandFlag.isMemKind()) {
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/arm64-fallback.ll b/llvm/test/CodeGen/AArch64/GlobalISel/arm64-fallback.ll
index 55755ff4ef1a3f..9a40648e1f2a85 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/arm64-fallback.ll
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/arm64-fallback.ll
@@ -79,6 +79,16 @@ entry:
ret void
}
+; The output that the input is tied to needs two registers.
+; FALLBACK-WITH-REPORT-ERR: remark: <unknown>:0:0: unable to translate instruction:{{.*}}asm_tied_multireg
+; FALLBACK-WITH-REPORT-ERR: warning: Instruction selection used fallback path for asm_tied_multireg
+; FALLBACK-WITH-REPORT-OUT-LABEL: asm_tied_multireg
+define i128 @asm_tied_multireg(i128 %x) {
+entry:
+ %r = call i128 asm "", "=r,0"(i128 %x)
+ ret i128 %r
+}
+
%struct.foo = type { [8 x i64] }
; FALLBACK-WITH-REPORT-ERR: remark: <unknown>:0:0: unable to translate instruction:{{.*}}ld64b{{.*}}asm_output_ls64
>From 142de6e698fe7a1d527e0019791a60166c2eed43 Mon Sep 17 00:00:00 2001
From: Bill Wendling <morbo at google.com>
Date: Mon, 2 Nov 2026 15:39:14 -0800
Subject: [PATCH 09/12] [Clang][CodeGen] Pass 128-bit aggregate inline asm
register inputs by value
Clang returns a register output of aggregate type, such as '_Complex
double' or a 16-byte struct, by value as an integer of the same size
(i128 for both). EmitAsmInputLValue, however, loaded aggregate register
inputs by value only up to 64 bits and passed larger ones by pointer.
So '"+r"' on a '_Complex double' emitted '"=r,*0"', which SelectionDAG
rejects as an unsupported tied indirect register input, and '"r"' on a
16-byte struct emitted '"*r"', which it rejects as an indirect register
input. Read-write '"rm"' and '"g"' operands hit the same error.
Load power-of-2 aggregate register inputs of up to 128 bits by value,
as with '__int128'. Tied aggregate operands of other sizes are still
passed by pointer and rejected.
Assisted-by: Claude Opus 5.5
---
clang/lib/CodeGen/CGStmt.cpp | 2 +-
.../CodeGen/X86/inline-asm-complex-output.c | 83 +++++++++++++++++--
2 files changed, 76 insertions(+), 9 deletions(-)
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 4e6ef629088790..85b341957225ce 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -2555,7 +2555,7 @@ std::pair<llvm::Value*, llvm::Type *> CodeGenFunction::EmitAsmInputLValue(
llvm::Type *Ty = ConvertType(InputType);
uint64_t Size = CGM.getDataLayout().getTypeSizeInBits(Ty);
- if ((Size <= 64 && llvm::isPowerOf2_64(Size)) ||
+ if ((Size <= 128 && llvm::isPowerOf2_64(Size)) ||
getTargetHooks().isScalarizableAsmOperand(*this, Ty)) {
Ty = llvm::IntegerType::get(getLLVMContext(), Size);
diff --git a/clang/test/CodeGen/X86/inline-asm-complex-output.c b/clang/test/CodeGen/X86/inline-asm-complex-output.c
index d30a15d8c6df06..b5d5e9d77544e4 100644
--- a/clang/test/CodeGen/X86/inline-asm-complex-output.c
+++ b/clang/test/CodeGen/X86/inline-asm-complex-output.c
@@ -92,12 +92,30 @@ void test_complex_float_inout(void) {
asm("" : "+r"(c));
}
+// CHECK-LABEL: define dso_local void @test_complex_double_inout(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT: [[ENTRY:.*:]]
+// 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 1.000000e+00, ptr [[C_REALP]], align 8
+// CHECK-NEXT: store double 0.000000e+00, ptr [[C_IMAGP]], align 8
+// CHECK-NEXT: [[TMP0:%.*]] = load i128, ptr [[C]], align 8
+// CHECK-NEXT: [[TMP1:%.*]] = call i128 asm "", "=r,0,~{dirflag},~{fpsr},~{flags}"(i128 [[TMP0]]) #[[ATTR2]], !srcloc [[META7:![0-9]+]]
+// CHECK-NEXT: store i128 [[TMP1]], ptr [[C]], align 8
+// CHECK-NEXT: ret void
+//
+void test_complex_double_inout(void) {
+ _Complex double 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: call void asm "", "=*m,~{dirflag},~{fpsr},~{flags}"(ptr elementtype({ float, float }) [[C]]) #[[ATTR3:[0-9]+]], !srcloc [[META8:![0-9]+]]
// CHECK-NEXT: ret void
//
void test_complex_float_mem(void) {
@@ -115,7 +133,7 @@ void test_complex_float_mem(void) {
// 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-NEXT: to label %[[ASM_FALLTHROUGH:.*]] [label %[[LABEL_SPLIT:.*]]], !srcloc [[META9:![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
@@ -164,7 +182,7 @@ _Complex float test_complex_float_goto(void) {
// 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-NEXT: to label %[[ASM_FALLTHROUGH:.*]] [label %[[LABEL_SPLIT:.*]]], !srcloc [[META10:![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
@@ -211,7 +229,7 @@ _Complex double test_complex_double_goto(void) {
// 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-NEXT: to label %[[ASM_FALLTHROUGH:.*]] [label %[[LABEL_SPLIT:.*]]], !srcloc [[META11:![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
@@ -249,6 +267,55 @@ _Complex float test_complex_float_inout_goto(_Complex float c) {
return -c;
}
+// CHECK-LABEL: define dso_local { double, double } @test_complex_double_inout_goto(
+// CHECK-SAME: double noundef [[C_COERCE0:%.*]], double noundef [[C_COERCE1:%.*]]) #[[ATTR0]] {
+// CHECK-NEXT: [[ENTRY:.*:]]
+// CHECK-NEXT: [[RETVAL:%.*]] = alloca { double, double }, align 8
+// CHECK-NEXT: [[C:%.*]] = alloca { double, double }, align 8
+// CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 0
+// CHECK-NEXT: store double [[C_COERCE0]], ptr [[TMP0]], align 8
+// CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 1
+// CHECK-NEXT: store double [[C_COERCE1]], ptr [[TMP1]], align 8
+// CHECK-NEXT: [[TMP2:%.*]] = load i128, ptr [[C]], align 8
+// CHECK-NEXT: [[TMP3:%.*]] = callbr i128 asm "", "=r,0,!i,~{dirflag},~{fpsr},~{flags}"(i128 [[TMP2]]) #[[ATTR2]]
+// CHECK-NEXT: to label %[[ASM_FALLTHROUGH:.*]] [label %[[LABEL_SPLIT:.*]]], !srcloc [[META12:![0-9]+]]
+// CHECK: [[ASM_FALLTHROUGH]]:
+// CHECK-NEXT: store i128 [[TMP3]], ptr [[C]], align 8
+// CHECK-NEXT: [[C_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 0
+// CHECK-NEXT: [[C_REAL:%.*]] = load double, ptr [[C_REALP]], align 8
+// CHECK-NEXT: [[C_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 1
+// CHECK-NEXT: [[C_IMAG:%.*]] = load double, ptr [[C_IMAGP]], 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 [[TMP3]], ptr [[C]], align 8
+// CHECK-NEXT: br label %[[LABEL:.*]]
+// CHECK: [[LABEL]]:
+// CHECK-NEXT: [[C_REALP1:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 0
+// CHECK-NEXT: [[C_REAL2:%.*]] = load double, ptr [[C_REALP1]], align 8
+// CHECK-NEXT: [[C_IMAGP3:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 1
+// CHECK-NEXT: [[C_IMAG4:%.*]] = load double, ptr [[C_IMAGP3]], align 8
+// CHECK-NEXT: [[NEG_R:%.*]] = fneg double [[C_REAL2]]
+// CHECK-NEXT: [[NEG_I:%.*]] = fneg double [[C_IMAG4]]
+// CHECK-NEXT: [[RETVAL_REALP5:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[RETVAL]], i32 0, i32 0
+// CHECK-NEXT: [[RETVAL_IMAGP6:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[RETVAL]], i32 0, i32 1
+// CHECK-NEXT: store double [[NEG_R]], ptr [[RETVAL_REALP5]], align 8
+// CHECK-NEXT: store double [[NEG_I]], ptr [[RETVAL_IMAGP6]], align 8
+// CHECK-NEXT: br label %[[RETURN]]
+// CHECK: [[RETURN]]:
+// CHECK-NEXT: [[TMP4:%.*]] = load { double, double }, ptr [[RETVAL]], align 8
+// CHECK-NEXT: ret { double, double } [[TMP4]]
+//
+_Complex double test_complex_double_inout_goto(_Complex double 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]] {
@@ -259,7 +326,7 @@ _Complex float test_complex_float_inout_goto(_Complex float c) {
// 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-NEXT: to label %[[ASM_FALLTHROUGH:.*]] [label %[[LABEL_SPLIT:.*]]], !srcloc [[META13:![0-9]+]]
// CHECK: [[ASM_FALLTHROUGH]]:
// CHECK-NEXT: [[ASMRESULT:%.*]] = extractvalue { i64, i32 } [[TMP1]], 0
// CHECK-NEXT: [[ASMRESULT1:%.*]] = extractvalue { i64, i32 } [[TMP1]], 1
@@ -297,7 +364,7 @@ int test_complex_float_and_int_goto(_Complex float *p) {
// 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: [[TMP1:%.*]] = call i64 asm "", "=r,~{dirflag},~{fpsr},~{flags}"() #[[ATTR2]], !srcloc [[META14:![0-9]+]]
// CHECK-NEXT: store volatile i64 [[TMP1]], ptr [[TMP0]], align 4
// CHECK-NEXT: ret void
//
@@ -316,7 +383,7 @@ void test_complex_float_volatile(volatile _Complex float *p) {
// 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: [[TMP2:%.*]] = call i32 asm "", "=r,0,~{dirflag},~{fpsr},~{flags}"(i32 [[TMP1]]) #[[ATTR2]], !srcloc [[META15:![0-9]+]]
// CHECK-NEXT: [[TMP3:%.*]] = trunc i32 [[TMP2]] to i16
// CHECK-NEXT: store i16 [[TMP3]], ptr [[TMP0]], align 1
// CHECK-NEXT: ret void
@@ -334,7 +401,7 @@ void test_complex_char_tied_int(_Complex char *p, int x) {
// 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: [[TMP2:%.*]] = call ptr asm "", "=r,0,~{dirflag},~{fpsr},~{flags}"(ptr [[TMP1]]) #[[ATTR2]], !srcloc [[META16:![0-9]+]]
// 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
>From df5be51f2b231a44b7931e8cea4aa7f88d6a2ece Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Wed, 4 Nov 2026 23:21:38 -0800
Subject: [PATCH 10/12] Fix two comments that overstate what's handled
---
clang/test/CodeGen/X86/inline-asm-complex-output.c | 2 +-
llvm/lib/CodeGen/GlobalISel/InlineAsmLowering.cpp | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/test/CodeGen/X86/inline-asm-complex-output.c b/clang/test/CodeGen/X86/inline-asm-complex-output.c
index b5d5e9d77544e4..3feb4c50fb9ebf 100644
--- a/clang/test/CodeGen/X86/inline-asm-complex-output.c
+++ b/clang/test/CodeGen/X86/inline-asm-complex-output.c
@@ -3,7 +3,7 @@
// 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
+// A _Complex output with a register-only 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.
diff --git a/llvm/lib/CodeGen/GlobalISel/InlineAsmLowering.cpp b/llvm/lib/CodeGen/GlobalISel/InlineAsmLowering.cpp
index acf9d0b7408d85..b29057ea6f464b 100644
--- a/llvm/lib/CodeGen/GlobalISel/InlineAsmLowering.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/InlineAsmLowering.cpp
@@ -408,8 +408,8 @@ bool InlineAsmLowering::lowerInlineAsm(
InstFlagIdx += getNumOpRegs(*Inst, InstFlagIdx) + 1;
// Only one register is tied below, so let SelectionDAG tie each
- // register of an output that needs several, such as an i128 in two
- // 64-bit registers.
+ // register of an output that needs several, such as an i128 on a
+ // 64-bit target.
if (getNumOpRegs(*Inst, InstFlagIdx) != 1) {
LLVM_DEBUG(dbgs() << "Matching input constraint to an output with "
"several registers not supported yet\n");
>From c362a9776b15efc4eff29cd2d948faf9c60b033a Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Wed, 4 Nov 2026 23:55:48 -0800
Subject: [PATCH 11/12] Widen a tied input for a _Complex output, and pass
128-bit "rm" inputs by address
---
clang/lib/CodeGen/CGStmt.cpp | 12 ++++-
.../CodeGen/X86/inline-asm-complex-output.c | 50 +++++++++++++++++++
2 files changed, 61 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 85b341957225ce..ca9b2625abb91d 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -2553,9 +2553,13 @@ std::pair<llvm::Value*, llvm::Type *> CodeGenFunction::EmitAsmInputLValue(
if (CodeGenFunction::hasScalarEvaluationKind(InputType))
return {EmitLoadOfLValue(InputValue, Loc).getScalarVal(), nullptr};
+ // Load a 128-bit value only when it has to be in registers, or when it's
+ // tied to an output. Otherwise, the asm can use the object itself rather
+ // than a copy of it.
llvm::Type *Ty = ConvertType(InputType);
uint64_t Size = CGM.getDataLayout().getTypeSizeInBits(Ty);
- if ((Size <= 128 && llvm::isPowerOf2_64(Size)) ||
+ if ((Size <= 64 && llvm::isPowerOf2_64(Size)) ||
+ (Size == 128 && (!Info.allowsMemory() || Info.hasTiedOperand())) ||
getTargetHooks().isScalarizableAsmOperand(*this, Ty)) {
Ty = llvm::IntegerType::get(getLLVMContext(), Size);
@@ -3128,7 +3132,13 @@ void AsmConstraintsInfo::HandleInputConstraints() {
if (isa<llvm::PointerType>(Arg->getType()))
Arg = Builder.CreatePtrToInt(Arg, CGF.IntPtrTy);
+ // A struct or _Complex output is returned as an integer of its size,
+ // so that's the type to extend the input to.
llvm::Type *OutputTy = CGF.ConvertType(OutputType);
+ if (OutputTy->isAggregateType())
+ OutputTy = llvm::IntegerType::get(getLLVMContext(),
+ Ctx.getTypeSize(OutputType));
+
if (isa<llvm::IntegerType>(OutputTy))
Arg = Builder.CreateZExt(Arg, OutputTy);
else if (isa<llvm::PointerType>(OutputTy))
diff --git a/clang/test/CodeGen/X86/inline-asm-complex-output.c b/clang/test/CodeGen/X86/inline-asm-complex-output.c
index 3feb4c50fb9ebf..790aee8a58c475 100644
--- a/clang/test/CodeGen/X86/inline-asm-complex-output.c
+++ b/clang/test/CodeGen/X86/inline-asm-complex-output.c
@@ -411,6 +411,56 @@ void test_complex_char_tied_pointer(_Complex char *p, void *x) {
asm("" : "=r"(*p) : "0"(x));
}
+// A tied input narrower than the output is widened to the output's integer
+// type, as for a scalar output, since the backend needs them the same size.
+// CHECK-LABEL: define dso_local void @test_complex_float_tied_narrower_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:%.*]] = zext i32 [[TMP1]] to i64
+// CHECK-NEXT: [[TMP3:%.*]] = call i64 asm "", "=r,0,~{dirflag},~{fpsr},~{flags}"(i64 [[TMP2]]) #[[ATTR2]], !srcloc [[META17:![0-9]+]]
+// CHECK-NEXT: store i64 [[TMP3]], ptr [[TMP0]], align 4
+// CHECK-NEXT: ret void
+//
+void test_complex_float_tied_narrower_int(_Complex float *p, int x) {
+ asm("" : "=r"(*p) : "0"(x));
+}
+
+// A 128-bit input that can be in memory is passed by its address, so the asm
+// gets the object itself, not a copy of it.
+// CHECK-LABEL: define dso_local void @test_complex_double_input_rm(
+// 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: call void asm sideeffect "", "*rm,~{dirflag},~{fpsr},~{flags}"(ptr elementtype({ double, double }) [[TMP0]]) #[[ATTR3]], !srcloc [[META18:![0-9]+]]
+// CHECK-NEXT: ret void
+//
+void test_complex_double_input_rm(_Complex double *p) {
+ asm volatile("" : : "rm"(*p));
+}
+
+// A 128-bit input that has to be in registers is loaded by value.
+// CHECK-LABEL: define dso_local void @test_complex_double_input_r(
+// 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:%.*]] = load i128, ptr [[TMP0]], align 8
+// CHECK-NEXT: call void asm sideeffect "", "r,~{dirflag},~{fpsr},~{flags}"(i128 [[TMP1]]) #[[ATTR3]], !srcloc [[META19:![0-9]+]]
+// CHECK-NEXT: ret void
+//
+void test_complex_double_input_r(_Complex double *p) {
+ asm volatile("" : : "r"(*p));
+}
+
#else
// No integer type is 256 bits wide.
>From 6231ee50a0fe7e273e9d948484bfe79209bb8222 Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Thu, 5 Nov 2026 00:08:08 -0800
Subject: [PATCH 12/12] Test 16-byte struct inputs with "r" and "rm"
---
.../CodeGen/X86/inline-asm-complex-output.c | 32 +++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/clang/test/CodeGen/X86/inline-asm-complex-output.c b/clang/test/CodeGen/X86/inline-asm-complex-output.c
index 790aee8a58c475..375e81921356b1 100644
--- a/clang/test/CodeGen/X86/inline-asm-complex-output.c
+++ b/clang/test/CodeGen/X86/inline-asm-complex-output.c
@@ -461,6 +461,38 @@ void test_complex_double_input_r(_Complex double *p) {
asm volatile("" : : "r"(*p));
}
+struct S16 { long a, b; };
+
+// A 16-byte struct input goes the same way as a _Complex double: by value
+// when it has to be in registers.
+// CHECK-LABEL: define dso_local void @test_struct16_input_r(
+// 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:%.*]] = load i128, ptr [[TMP0]], align 8
+// CHECK-NEXT: call void asm sideeffect "", "r,~{dirflag},~{fpsr},~{flags}"(i128 [[TMP1]]) #[[ATTR3]], !srcloc [[META20:![0-9]+]]
+// CHECK-NEXT: ret void
+//
+void test_struct16_input_r(struct S16 *p) {
+ asm volatile("" : : "r"(*p));
+}
+
+// And by its address when it can be in memory.
+// CHECK-LABEL: define dso_local void @test_struct16_input_rm(
+// 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: call void asm sideeffect "", "*rm,~{dirflag},~{fpsr},~{flags}"(ptr elementtype([[STRUCT_S16:%.*]]) [[TMP0]]) #[[ATTR3]], !srcloc [[META21:![0-9]+]]
+// CHECK-NEXT: ret void
+//
+void test_struct16_input_rm(struct S16 *p) {
+ asm volatile("" : : "rm"(*p));
+}
+
#else
// No integer type is 256 bits wide.
More information about the llvm-branch-commits
mailing list