Skip to content

Commit 7e88407

Browse files
committed
[SUA][IRGen] Add stub for swift_coroFrameAlloc
rdar://145239850
1 parent cc14548 commit 7e88407

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2102,7 +2102,13 @@ FUNCTION(Free, c, free, C_CC, AlwaysAvailable,
21022102
FUNCTION(CoroFrameAlloc, Swift, swift_coroFrameAlloc, C_CC, AlwaysAvailable,
21032103
RETURNS(Int8PtrTy),
21042104
ARGS(SizeTy, Int64Ty),
2105-
NO_ATTRS,
2105+
ATTRS(NoUnwind),
2106+
EFFECT(Allocating),
2107+
UNKNOWN_MEMEFFECTS)
2108+
FUNCTION(coroFrameAllocStub, Swift, swift_coroFrameAllocStub, C_CC,
2109+
AlwaysAvailable, RETURNS(Int8PtrTy),
2110+
ARGS(SizeTy),
2111+
ATTRS(NoUnwind),
21062112
EFFECT(Allocating),
21072113
UNKNOWN_MEMEFFECTS)
21082114

lib/IRGen/GenCall.cpp

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5094,26 +5094,72 @@ void irgen::emitAsyncFunctionEntry(IRGenFunction &IGF,
50945094
IGF.setupAsync(asyncContextIndex);
50955095
}
50965096

5097+
static llvm::Constant *getCoroFrameAllocStubFn(IRGenModule &IGM) {
5098+
return IGM.getOrCreateHelperFunction(
5099+
"__swift_coroFrameAllocStub", IGM.Int8PtrTy,
5100+
{IGM.SizeTy},
5101+
[&](IRGenFunction &IGF) {
5102+
if (IGM.DebugInfo)
5103+
IGM.DebugInfo->emitArtificialFunction(IGF, IGF.CurFn);
5104+
5105+
auto parameters = IGF.collectParameters();
5106+
auto *size = parameters.claimNext();
5107+
auto *coroFrameAllocFn = IGF.IGM.getOpaquePtr(IGF.IGM.getCoroFrameAllocFn());
5108+
auto *nullSwiftCoroFrameAlloc = IGF.Builder.CreateCmp(
5109+
llvm::CmpInst::Predicate::ICMP_NE, coroFrameAllocFn,
5110+
llvm::ConstantPointerNull::get(
5111+
cast<llvm::PointerType>(coroFrameAllocFn->getType())));
5112+
auto *coroFrameAllocReturn = IGF.createBasicBlock("return-coroFrameAlloc");
5113+
auto *mallocReturn = IGF.createBasicBlock("return-malloc");
5114+
IGF.Builder.CreateCondBr(nullSwiftCoroFrameAlloc, coroFrameAllocReturn, coroFrameAllocReturn);
5115+
5116+
IGF.Builder.emitBlock(coroFrameAllocReturn);
5117+
auto mallocTypeId = IGF.getMallocTypeId();
5118+
auto *coroFrameAllocCall = IGF.Builder.CreateCall(IGF.IGM.getCoroFrameAllocFunctionPointer(), {size, mallocTypeId});
5119+
IGF.Builder.CreateRet(coroFrameAllocCall);
5120+
5121+
// llvm::Constant *allocFn = IGF.IGM.getOpaquePtr(IGF.IGM.getCoroFrameAllocFn());
5122+
// auto fun = cast<llvm::Function>(allocFn);
5123+
// auto fnType = cast<llvm::FunctionType>(fun->getValueType());
5124+
// llvm::CallInst *Call = IGF.Builder.CreateCallWithoutDbgLoc(fnType, allocFn, {size, mallocTypeId});
5125+
// IGF.Builder.CreateRet(Call);
5126+
5127+
IGF.Builder.emitBlock(mallocReturn);
5128+
auto *mallocCall = IGF.Builder.CreateCall(IGF.IGM.getMallocFunctionPointer(), {size});
5129+
IGF.Builder.CreateRet(mallocCall);
5130+
// llvm::Constant *mallocFn = IGF.IGM.getOpaquePtr(IGF.IGM.getMallocFn());
5131+
// auto mallocFun = cast<llvm::Function>(mallocFn);
5132+
// auto mallocFnType = cast<llvm::FunctionType>(mallocFun->getValueType());
5133+
// llvm::CallInst *mallocCall = IGF.Builder.CreateCallWithoutDbgLoc(mallocFnType, mallocFn, {size});
5134+
// IGF.Builder.CreateRet(mallocCall);
5135+
},
5136+
/*setIsNoInline=*/false,
5137+
/*forPrologue=*/false,
5138+
/*isPerformanceConstraint=*/false,
5139+
/*optionalLinkageOverride=*/nullptr, llvm::CallingConv::C);
5140+
}
5141+
50975142
void irgen::emitYieldOnceCoroutineEntry(
50985143
IRGenFunction &IGF, CanSILFunctionType fnType,
50995144
NativeCCEntryPointArgumentEmission &emission) {
51005145
// Use malloc and free as our allocator.
51015146
auto deallocFn = IGF.IGM.getOpaquePtr(IGF.IGM.getFreeFn());
51025147
auto *buffer = emission.getCoroutineBuffer();
5103-
llvm::SmallVector<llvm::Value *, 2> finalArgs;
5104-
llvm::Constant *allocFn = nullptr;
5105-
if (IGF.getOptions().EmitTypeMallocForCoroFrame) {
5106-
auto mallocTypeId = IGF.getMallocTypeId();
5107-
finalArgs.push_back(mallocTypeId);
5108-
allocFn = IGF.IGM.getOpaquePtr(IGF.IGM.getCoroFrameAllocFn());
5109-
} else {
5110-
allocFn = IGF.IGM.getOpaquePtr(IGF.IGM.getMallocFn());
5111-
}
5148+
auto allocFn = IGF.IGM.getOpaquePtr(getCoroFrameAllocStubFn(IGF.IGM));
5149+
// llvm::SmallVector<llvm::Value *, 2> finalArgs;
5150+
// llvm::Constant *allocFn = nullptr;
5151+
// if (IGF.getOptions().EmitTypeMallocForCoroFrame) {
5152+
// auto mallocTypeId = IGF.getMallocTypeId();
5153+
// finalArgs.push_back(mallocTypeId);
5154+
// allocFn = IGF.IGM.getOpaquePtr(IGF.IGM.getCoroFrameAllocFn());
5155+
// } else {
5156+
// allocFn = IGF.IGM.getOpaquePtr(IGF.IGM.getMallocFn());
5157+
// }
51125158
emitRetconCoroutineEntry(IGF, fnType, buffer,
51135159
llvm::Intrinsic::coro_id_retcon_once,
51145160
getYieldOnceCoroutineBufferSize(IGF.IGM),
51155161
getYieldOnceCoroutineBufferAlignment(IGF.IGM), {},
5116-
allocFn, deallocFn, finalArgs);
5162+
allocFn, deallocFn, {});
51175163
}
51185164

51195165
void irgen::emitYieldManyCoroutineEntry(

0 commit comments

Comments
 (0)