[clang] [llvm] [CodeGen] Add support for multiple constraints (PR #195592)

Bill Wendling via cfe-commits cfe-commits at lists.llvm.org
Mon May 4 08:39:42 PDT 2026


================
@@ -2883,13 +2883,23 @@ void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
     if (!Constraints.empty())
       Constraints += ',';
 
-    // 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.
+    // - If this is a register output, then make the inline asm return it
+    //   by-value.
+    // - If this is a memory output, return the value by reference.
+    // - If this is a register and memory output, treat it like a register
+    //   output at -O[1-3]. This allows the optimizing register allocators to
+    //   choose a register, while the fast register allocator defaults to
+    //   memory.
     QualType QTy = OutExpr->getType();
     const bool IsScalarOrAggregate = hasScalarEvaluationKind(QTy) ||
                                      hasAggregateEvaluationKind(QTy);
-    if (!Info.allowsMemory() && IsScalarOrAggregate) {
+    const bool RegisterMemoryConstraints =
+        CGM.getCodeGenOpts().OptimizationLevel != 0 &&
----------------
bwendling wrote:

I believe that not making opt-level decisions is based on the fact that the middle-end and/or back-end is able to choose a different optimization level. However, if we don't allow for that in some instances (switching from `-O0` in the front-end to `-O[1-3]` later on in particular), we can *finally* support this longstanding deficiency in the compiler; we're losing the questionably useful ability to do something like:

```shell
$ clang -O2 -S -emit-llvm -o - test.c | llc -O0
```

If that's insufficient, I could choose the "optimal" option here and modify the inline asm in `InlineAsmPrepare.cpp`, but that turns out to be as full of pitfalls as most other options we've tried over the years, and I'm rather worried that we'll miss some corner cases that either cause ICE's or wrong code generation.

https://github.com/llvm/llvm-project/pull/195592


More information about the cfe-commits mailing list