[clang] [clang][CodeGen] `sret` args should always point to the `alloca` AS, so use that (PR #114062)

Alex Voicu via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 15 15:11:00 PST 2024


================
@@ -5390,11 +5390,19 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
             V->getType()->isIntegerTy())
           V = Builder.CreateZExt(V, ArgInfo.getCoerceToType());
 
-        // If the argument doesn't match, perform a bitcast to coerce it.  This
-        // can happen due to trivial type mismatches.
+        // If the argument doesn't match, we are either trying to pass an
+        // alloca-ed sret argument directly, and the alloca AS does not match
+        // the default AS, case in which we AS cast it, or we have a trivial
+        // type mismatch, and thus perform a bitcast to coerce it.
         if (FirstIRArg < IRFuncTy->getNumParams() &&
-            V->getType() != IRFuncTy->getParamType(FirstIRArg))
-          V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));
+            V->getType() != IRFuncTy->getParamType(FirstIRArg)) {
+          auto IRTy = IRFuncTy->getParamType(FirstIRArg);
+          auto MaybeSRetArg = dyn_cast_or_null<llvm::Argument>(V);
+          if (MaybeSRetArg && MaybeSRetArg->hasStructRetAttr())
+            V = Builder.CreateAddrSpaceCast(V, IRTy);
----------------
AlexVlx wrote:

OK, I *think* that I've found a possibly acceptable middle ground (both for this and your other objection). Note that I am not rejecting the fact that we probably want `LangAS`es threaded through the indirect interfaces that deal with ASes, but it gives me a bit of trepidation to do it as part of this PR. I'd prefer to wrap this up, as it blocks some other work, and then open a separate PR/discussion around re-doing the interfaces.

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


More information about the cfe-commits mailing list