[clang] [clang] Fix complex rvalue inputs to inline assembly (PR #239874)
Jill Hansalia via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 8 16:50:12 PST 2026
https://github.com/jhansali updated https://github.com/llvm/llvm-project/pull/239874
>From dca393d0cd9a8330566d195dca23cb6e5591d90b Mon Sep 17 00:00:00 2001
From: jhansali <jhansali at asu.edu>
Date: Fri, 6 Nov 2026 17:15:21 -0800
Subject: [PATCH] [clang] Fix complex rvalue inputs to inline assembly
Evaluate complex rvalues into temporary storage before passing them through the existing inline assembly operand handling. Use the CodeGen evaluation kind to include atomic complex types. Add regression coverage for arithmetic evaluation and register and memory operands.
Fixes #239139
---
clang/lib/CodeGen/CGStmt.cpp | 12 ++++++++-
clang/test/CodeGen/asm-complex.c | 45 ++++++++++++++++++++++++++++++++
2 files changed, 56 insertions(+), 1 deletion(-)
create mode 100644 clang/test/CodeGen/asm-complex.c
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index d87cbee8db40c1..0c10a0bc932577 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -2597,7 +2597,17 @@ CodeGenFunction::EmitAsmInput(const TargetInfo::ConstraintInfo &Info,
if (InputExpr->getStmtClass() == Expr::CXXThisExprClass)
return {EmitScalarExpr(InputExpr), nullptr};
InputExpr = InputExpr->IgnoreParenNoopCasts(getContext());
- LValue Dest = EmitLValue(InputExpr);
+ LValue Dest;
+ if (InputExpr->isPRValue() &&
+ getEvaluationKind(InputExpr->getType()) == TEK_Complex) {
+ // Evaluate complex rvalues into temporary storage so they can use the
+ // same register or memory operand handling as other non-scalar inputs.
+ Dest = MakeAddrLValue(CreateMemTemp(InputExpr->getType(), "asm.input"),
+ InputExpr->getType());
+ EmitComplexExprIntoLValue(InputExpr, Dest, /*isInit=*/true);
+ } else {
+ Dest = EmitLValue(InputExpr);
+ }
return EmitAsmInputLValue(Info, Dest, InputExpr->getType(), ConstraintStr,
InputExpr->getExprLoc());
}
diff --git a/clang/test/CodeGen/asm-complex.c b/clang/test/CodeGen/asm-complex.c
new file mode 100644
index 00000000000000..16c99f978f7e4f
--- /dev/null
+++ b/clang/test/CodeGen/asm-complex.c
@@ -0,0 +1,45 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s
+
+void f(_Complex float a, _Complex float b) {
+ // CHECK-LABEL: @f(
+ // CHECK: [[REAL:%.*]] = fadd float
+ // CHECK-NEXT: [[IMAG:%.*]] = fadd float
+ // CHECK-NEXT: [[REAL_PTR:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[TMP:%.*]], i32 0, i32 0
+ // CHECK-NEXT: [[IMAG_PTR:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[TMP]], i32 0, i32 1
+ // CHECK-NEXT: store float [[REAL]], ptr [[REAL_PTR]], align 4
+ // CHECK-NEXT: store float [[IMAG]], ptr [[IMAG_PTR]], align 4
+ // CHECK-NEXT: [[INPUT:%.*]] = load i64, ptr [[TMP]], align 4
+ // CHECK-NEXT: call void asm sideeffect "", "r,~{dirflag},~{fpsr},~{flags}"(i64 [[INPUT]])
+ asm volatile("" :: "r"(a + b));
+}
+
+void double_complex(_Complex double a, _Complex double b) {
+ // CHECK-LABEL: @double_complex(
+ // CHECK: [[REAL:%.*]] = fadd double
+ // CHECK-NEXT: [[IMAG:%.*]] = fadd double
+ // CHECK-NEXT: [[REAL_PTR:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[TMP:%.*]], i32 0, i32 0
+ // CHECK-NEXT: [[IMAG_PTR:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[TMP]], i32 0, i32 1
+ // CHECK-NEXT: store double [[REAL]], ptr [[REAL_PTR]], align 8
+ // CHECK-NEXT: store double [[IMAG]], ptr [[IMAG_PTR]], align 8
+ // CHECK-NEXT: call void asm sideeffect "", "*rm,~{dirflag},~{fpsr},~{flags}"(ptr elementtype({ double, double }) [[TMP]])
+ asm volatile("" :: "rm"(a + b));
+}
+
+void memory_complex(_Complex float *a) {
+ // CHECK-LABEL: @memory_complex(
+ // CHECK: [[ADDR:%.*]] = alloca ptr, align 8
+ // CHECK-NEXT: store ptr %{{.*}}, ptr [[ADDR]], align 8
+ // CHECK-NEXT: [[INPUT:%.*]] = load ptr, ptr [[ADDR]], align 8
+ // CHECK-NEXT: call void asm sideeffect "", "*m,~{dirflag},~{fpsr},~{flags}"(ptr elementtype({ float, float }) [[INPUT]])
+ asm volatile("" :: "m"(*a));
+}
+
+_Atomic(_Complex float) get_atomic_complex(void);
+
+void atomic_complex(void) {
+ // CHECK-LABEL: @atomic_complex(
+ // CHECK: call void @get_atomic_complex(
+ // CHECK: load atomic i64
+ // CHECK: call void asm sideeffect "", "r,~{dirflag},~{fpsr},~{flags}"(i64 %{{.*}})
+ asm volatile("" :: "r"(get_atomic_complex()));
+}
More information about the cfe-commits
mailing list