@@ -1719,8 +1719,12 @@ CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
17191719
17201720 // Add type for sret argument.
17211721 if (IRFunctionArgs.hasSRetArg ()) {
1722- ArgTypes[IRFunctionArgs.getSRetArgNo ()] = llvm::PointerType::get (
1723- getLLVMContext (), FI.getReturnInfo ().getIndirectAddrSpace ());
1722+ QualType Ret = FI.getReturnType ();
1723+ unsigned AddressSpace = CGM.getCodeGenOpts ().UseAllocaASForSrets
1724+ ? FI.getReturnInfo ().getIndirectAddrSpace ()
1725+ : CGM.getTypes ().getTargetAddressSpace (Ret);
1726+ ArgTypes[IRFunctionArgs.getSRetArgNo ()] =
1727+ llvm::PointerType::get (getLLVMContext (), AddressSpace);
17241728 }
17251729
17261730 // Add type for inalloca argument.
@@ -5309,6 +5313,7 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
53095313 // If the call returns a temporary with struct return, create a temporary
53105314 // alloca to hold the result, unless one is given to us.
53115315 Address SRetPtr = Address::invalid ();
5316+ RawAddress SRetAlloca = RawAddress::invalid ();
53125317 llvm::Value *UnusedReturnSizePtr = nullptr ;
53135318 if (RetAI.isIndirect () || RetAI.isInAlloca () || RetAI.isCoerceAndExpand ()) {
53145319 // For virtual function pointer thunks and musttail calls, we must always
@@ -5322,19 +5327,27 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
53225327 } else if (!ReturnValue.isNull ()) {
53235328 SRetPtr = ReturnValue.getAddress ();
53245329 } else {
5325- SRetPtr = CreateMemTempWithoutCast (RetTy, " tmp" );
5330+ SRetPtr = CGM.getCodeGenOpts ().UseAllocaASForSrets
5331+ ? CreateMemTempWithoutCast (RetTy, " tmp" )
5332+ : CreateMemTemp (RetTy, " tmp" , &SRetAlloca);
53265333 if (HaveInsertPoint () && ReturnValue.isUnused ()) {
53275334 llvm::TypeSize size =
53285335 CGM.getDataLayout ().getTypeAllocSize (ConvertTypeForMem (RetTy));
5329- UnusedReturnSizePtr = EmitLifetimeStart (size, SRetPtr.getBasePointer ());
5336+ if (CGM.getCodeGenOpts ().UseAllocaASForSrets )
5337+ UnusedReturnSizePtr =
5338+ EmitLifetimeStart (size, SRetPtr.getBasePointer ());
5339+ else
5340+ UnusedReturnSizePtr =
5341+ EmitLifetimeStart (size, SRetAlloca.getPointer ());
53305342 }
53315343 }
53325344 if (IRFunctionArgs.hasSRetArg ()) {
53335345 // A mismatch between the allocated return value's AS and the target's
53345346 // chosen IndirectAS can happen e.g. when passing the this pointer through
53355347 // a chain involving stores to / loads from the DefaultAS; we address this
53365348 // here, symmetrically with the handling we have for normal pointer args.
5337- if (SRetPtr.getAddressSpace () != RetAI.getIndirectAddrSpace ()) {
5349+ if (CGM.getCodeGenOpts ().UseAllocaASForSrets &&
5350+ (SRetPtr.getAddressSpace () != RetAI.getIndirectAddrSpace ())) {
53385351 llvm::Value *V = SRetPtr.getBasePointer ();
53395352 LangAS SAS = getLangASFromTargetAS (SRetPtr.getAddressSpace ());
53405353 LangAS DAS = getLangASFromTargetAS (RetAI.getIndirectAddrSpace ());
@@ -5916,9 +5929,14 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
59165929 // can't depend on being inside of an ExprWithCleanups, so we need to manually
59175930 // pop this cleanup later on. Being eager about this is OK, since this
59185931 // temporary is 'invisible' outside of the callee.
5919- if (UnusedReturnSizePtr)
5920- pushFullExprCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, SRetPtr,
5921- UnusedReturnSizePtr);
5932+ if (UnusedReturnSizePtr) {
5933+ if (CGM.getCodeGenOpts ().UseAllocaASForSrets )
5934+ pushFullExprCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, SRetPtr,
5935+ UnusedReturnSizePtr);
5936+ else
5937+ pushFullExprCleanup<CallLifetimeEnd>(NormalEHLifetimeMarker, SRetAlloca,
5938+ UnusedReturnSizePtr);
5939+ }
59225940
59235941 llvm::BasicBlock *InvokeDest = CannotThrow ? nullptr : getInvokeDest ();
59245942
0 commit comments