Skip to content

Commit b38d3ee

Browse files
committed
[CoroutineAccessors] Use SwiftCC'd malloc/free.
Calls to the allocation functions use the same convention regardless of which function, so if the functino being called is malloc and free, provide a thunk with the appropriate convention.
1 parent 1917496 commit b38d3ee

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

lib/IRGen/GenCall.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5289,6 +5289,40 @@ Address irgen::emitAllocYieldManyCoroutineBuffer(IRGenFunction &IGF) {
52895289
getYieldManyCoroutineBufferAlignment(IGF.IGM));
52905290
}
52915291

5292+
static llvm::Constant *getAddrOfSwiftCCMalloc(IRGenModule &IGM) {
5293+
auto mallocFnPtr = IGM.getMallocFunctionPointer();
5294+
auto sig = mallocFnPtr.getSignature();
5295+
if (sig.getCallingConv() == IGM.SwiftCC) {
5296+
return IGM.getMallocFn();
5297+
}
5298+
return IGM.getOrCreateHelperFunction(
5299+
"_swift_malloc", sig.getType()->getReturnType(), sig.getType()->params(),
5300+
[](IRGenFunction &IGF) {
5301+
auto parameters = IGF.collectParameters();
5302+
auto *size = parameters.claimNext();
5303+
auto malloc = IGF.IGM.getMallocFunctionPointer();
5304+
auto *call = IGF.Builder.CreateCall(malloc, {size});
5305+
IGF.Builder.CreateRet(call);
5306+
});
5307+
}
5308+
5309+
static llvm::Constant *getAddrOfSwiftCCFree(IRGenModule &IGM) {
5310+
auto freeFnPtr = IGM.getFreeFunctionPointer();
5311+
auto sig = freeFnPtr.getSignature();
5312+
if (sig.getCallingConv() == IGM.SwiftCC) {
5313+
return IGM.getFreeFn();
5314+
}
5315+
return IGM.getOrCreateHelperFunction(
5316+
"_swift_free", sig.getType()->getReturnType(), sig.getType()->params(),
5317+
[](IRGenFunction &IGF) {
5318+
auto parameters = IGF.collectParameters();
5319+
auto *ptr = parameters.claimNext();
5320+
auto free = IGF.IGM.getFreeFunctionPointer();
5321+
IGF.Builder.CreateCall(free, {ptr});
5322+
IGF.Builder.CreateRetVoid();
5323+
});
5324+
}
5325+
52925326
static llvm::Constant *getAddrOfGlobalCoroAllocator(
52935327
IRGenModule &IGM, CoroAllocatorKind kind, bool shouldDeallocateImmediately,
52945328
llvm::Constant *allocFn, llvm::Constant *deallocFn) {
@@ -5310,7 +5344,8 @@ static llvm::Constant *getAddrOfGlobalCoroAllocator(
53105344
llvm::Constant *IRGenModule::getAddrOfGlobalCoroMallocAllocator() {
53115345
return getAddrOfGlobalCoroAllocator(*this, CoroAllocatorKind::Malloc,
53125346
/*shouldDeallocateImmediately=*/true,
5313-
getMallocFn(), getFreeFn());
5347+
getAddrOfSwiftCCMalloc(*this),
5348+
getAddrOfSwiftCCFree(*this));
53145349
}
53155350
llvm::Constant *IRGenModule::getAddrOfGlobalCoroAsyncTaskAllocator() {
53165351
return getAddrOfGlobalCoroAllocator(*this, CoroAllocatorKind::Async,

0 commit comments

Comments
 (0)