[llvm-branch-commits] [clang] [CIR] Pass _Complex inline asm register operands by value (PR #239138)

Bill Wendling via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Nov 19 22:09:28 PST 2026


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

>From 3bc7e97af25994f440e3376059b409c22cf88957 Mon Sep 17 00:00:00 2001
From: Bill Wendling <isanbard at gmail.com>
Date: Thu, 5 Nov 2026 00:17:04 -0800
Subject: [PATCH] [CIR] Pass _Complex inline asm register operands by value

CIR still returned a _Complex register output through memory ("=*r"),
and passed a 128-bit input that has to be in registers by its address
("*r"), which the backend rejects. Classic codegen now passes both by
value.

Do the same: return a _Complex output with a register-only constraint as
an integer of its size, load a 128-bit input when it has to be in
registers or is tied to an output, and extend a narrower tied input to
the integer type of a _Complex or struct output.

Assisted-by: Claude Opus 5.5
---
 clang/lib/CIR/CodeGen/CIRGenAsm.cpp | 17 +++++++++---
 clang/test/CIR/CodeGen/inline-asm.c | 42 +++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenAsm.cpp b/clang/lib/CIR/CodeGen/CIRGenAsm.cpp
index c3c676bd742c12..4fcd6519447f0d 100644
--- a/clang/lib/CIR/CodeGen/CIRGenAsm.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenAsm.cpp
@@ -125,9 +125,13 @@ std::pair<mlir::Value, mlir::Type> CIRGenFunction::emitAsmInputLValue(
     if (hasScalarEvaluationKind(inputType))
       return {emitLoadOfLValue(inputValue, loc).getValue(), mlir::Type()};
 
+    // 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.
     mlir::Type ty = convertType(inputType);
     uint64_t size = cgm.getDataLayout().getTypeSizeInBits(ty);
     if ((size <= 64 && llvm::isPowerOf2_64(size)) ||
+        (size == 128 && (!info.allowsMemory() || info.hasTiedOperand())) ||
         getTargetHooks().isScalarizableAsmOperand(*this, ty)) {
       ty = cir::IntType::get(&getMLIRContext(), size, false);
 
@@ -342,9 +346,7 @@ mlir::LogicalResult CIRGenFunction::emitAsmStmt(const AsmStmt &s) {
     // If this is a register output, then make the inline a sm return it
     // by-value.  If this is a memory result, return the value by-reference.
     QualType qty = outExpr->getType();
-    const bool isScalarOrAggregate =
-        hasScalarEvaluationKind(qty) || hasAggregateEvaluationKind(qty);
-    if (!info.allowsMemory() && isScalarOrAggregate) {
+    if (!info.allowsMemory()) {
       constraints += "=" + outputConstraint;
       resultRegQualTys.push_back(qty);
       resultRegDests.push_back(dest);
@@ -356,7 +358,7 @@ mlir::LogicalResult CIRGenFunction::emitAsmStmt(const AsmStmt &s) {
       const bool requiresCast =
           info.allowsRegister() &&
           (cgm.getTargetCIRGenInfo().isScalarizableAsmOperand(*this, ty) ||
-           isa<cir::RecordType, cir::ArrayType>(ty));
+           isa<cir::RecordType, cir::ArrayType, cir::ComplexType>(ty));
 
       resultTruncRegTypes.push_back(ty);
       resultTypeRequiresCast.push_back(requiresCast);
@@ -491,7 +493,14 @@ mlir::LogicalResult CIRGenFunction::emitAsmStmt(const AsmStmt &s) {
         // Use ptrtoint as appropriate so that we can do our extension.
         if (isa<cir::PointerType>(argValue.getType()))
           argValue = builder.createPtrToInt(argValue, uIntPtrTy);
+        // A struct or _Complex output is returned as an integer of its size,
+        // so that's the type to extend the input to.
         mlir::Type outputTy = convertType(outputType);
+        if (isa<cir::RecordType, cir::ArrayType, cir::ComplexType>(outputTy))
+          outputTy = cir::IntType::get(&getMLIRContext(),
+                                       getContext().getTypeSize(outputType),
+                                       /*isSigned=*/false);
+
         if (isa<cir::IntType>(outputTy))
           argValue = builder.createIntCast(argValue, outputTy);
         else if (isa<cir::PointerType>(outputTy))
diff --git a/clang/test/CIR/CodeGen/inline-asm.c b/clang/test/CIR/CodeGen/inline-asm.c
index 9c556753b4a17f..179d6015914ea7 100644
--- a/clang/test/CIR/CodeGen/inline-asm.c
+++ b/clang/test/CIR/CodeGen/inline-asm.c
@@ -930,3 +930,45 @@ void t35(int i) {
   int g;
   __asm__ volatile("" :: "r"(i), "m"(g));
 }
+
+// A _Complex output with a register constraint is returned by value as an
+// integer of the same size.
+//      CIR: cir.func{{.*}}@t36
+//      CIR: {"" "=r,~{dirflag},~{fpsr},~{flags}"})
+// LLVM: define{{.*}}@t36
+// LLVM: call i64 asm "", "=r,~{dirflag},~{fpsr},~{flags}"()
+void t36(void) {
+  _Complex float c;
+  __asm__("" : "=r"(c));
+}
+
+// A 128-bit _Complex input that has to be in registers is loaded by value.
+//      CIR: cir.func{{.*}}@t37
+//      CIR: {"" "r,~{dirflag},~{fpsr},~{flags}"}) side_effects
+// LLVM: define{{.*}}@t37
+// LLVM: call void asm sideeffect "", "r,~{dirflag},~{fpsr},~{flags}"(i128
+void t37(_Complex double *p) {
+  __asm__ volatile("" :: "r"(*p));
+}
+
+// One that can be in memory is passed by its address.
+//      CIR: cir.func{{.*}}@t38
+//      CIR: {"" "*rm,~{dirflag},~{fpsr},~{flags}"}) side_effects
+// LLVM: define{{.*}}@t38
+// LLVM: call void asm sideeffect "", "*rm,~{dirflag},~{fpsr},~{flags}"(ptr elementtype({ double, double })
+void t38(_Complex double *p) {
+  __asm__ volatile("" :: "rm"(*p));
+}
+
+// A tied input narrower than a _Complex output is widened to the output's
+// integer type. As for a scalar output, CIR sign-extends it and classic
+// codegen zero-extends it; the asm doesn't define the top bits either way.
+//      CIR: cir.func{{.*}}@t39
+//      CIR: {"" "=r,0,~{dirflag},~{fpsr},~{flags}"})
+// LLVM: define{{.*}}@t39
+// CIRLLVMONLY: sext i32 %{{.*}} to i64
+// LLVMONLY: zext i32 %{{.*}} to i64
+// LLVM: call i64 asm "", "=r,0,~{dirflag},~{fpsr},~{flags}"(i64
+void t39(_Complex float *p, int x) {
+  __asm__("" : "=r"(*p) : "0"(x));
+}



More information about the llvm-branch-commits mailing list