@@ -5391,11 +5391,19 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
53915391 V->getType ()->isIntegerTy ())
53925392 V = Builder.CreateZExt (V, ArgInfo.getCoerceToType ());
53935393
5394- // If the argument doesn't match, perform a bitcast to coerce it. This
5395- // can happen due to trivial type mismatches.
5394+ // If the argument doesn't match, we are either trying to pass an
5395+ // alloca-ed sret argument directly, and the alloca AS does not match
5396+ // the default AS, case in which we AS cast it, or we have a trivial
5397+ // type mismatch, and thus perform a bitcast to coerce it.
53965398 if (FirstIRArg < IRFuncTy->getNumParams () &&
5397- V->getType () != IRFuncTy->getParamType (FirstIRArg))
5398- V = Builder.CreateBitCast (V, IRFuncTy->getParamType (FirstIRArg));
5399+ V->getType () != IRFuncTy->getParamType (FirstIRArg)) {
5400+ auto IRTy = IRFuncTy->getParamType (FirstIRArg);
5401+ auto MaybeSRetArg = dyn_cast_or_null<llvm::Argument>(V);
5402+ if (MaybeSRetArg && MaybeSRetArg->hasStructRetAttr ())
5403+ V = Builder.CreateAddrSpaceCast (V, IRTy);
5404+ else
5405+ V = Builder.CreateBitCast (V, IRTy);
5406+ }
53995407
54005408 if (ArgHasMaybeUndefAttr)
54015409 V = Builder.CreateFreeze (V);
0 commit comments