Skip to content

Commit 7c017fe

Browse files
committed
[NFC] IRGen: Deduplicate this code.
1 parent f8666b1 commit 7c017fe

File tree

1 file changed

+52
-68
lines changed

1 file changed

+52
-68
lines changed

lib/IRGen/GenCoro.cpp

Lines changed: 52 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -846,38 +846,19 @@ llvm::Constant *swift::irgen::getCoroDeallocFn(IRGenModule &IGM) {
846846
});
847847
}
848848

849-
static llvm::Constant *getAddrOfGlobalCoroAllocator(
850-
IRGenModule &IGM, CoroAllocatorKind kind, bool shouldDeallocateImmediately,
851-
llvm::Constant *allocFn, llvm::Constant *deallocFn) {
852-
auto entity = LinkEntity::forCoroAllocator(kind);
853-
auto taskAllocator = IGM.getOrCreateLazyGlobalVariable(
854-
entity,
855-
[&](ConstantInitBuilder &builder) -> ConstantInitFuture {
856-
auto allocator = builder.beginStruct(IGM.CoroAllocatorTy);
857-
auto flags = CoroAllocatorFlags(kind);
858-
flags.setShouldDeallocateImmediately(shouldDeallocateImmediately);
859-
allocator.addInt32(flags.getOpaqueValue());
860-
allocator.addSignedPointer(
861-
allocFn, IGM.getOptions().PointerAuth.CoroAllocationFunction,
862-
PointerAuthEntity::Special::CoroAllocationFunction);
863-
allocator.addSignedPointer(
864-
deallocFn, IGM.getOptions().PointerAuth.CoroDeallocationFunction,
865-
PointerAuthEntity::Special::CoroDeallocationFunction);
866-
return allocator.finishAndCreateFuture();
867-
},
868-
[&](llvm::GlobalVariable *var) { var->setConstant(true); });
869-
return taskAllocator;
870-
}
849+
using GetAllocFunctionPointer = FunctionPointer (IRGenModule::*)();
871850

872-
static llvm::Constant *getAddrOfSwiftCoroMalloc(IRGenModule &IGM) {
851+
static llvm::Constant *
852+
getAddrOfSwiftCoroAllocThunk(StringRef name, GetAllocFunctionPointer getAlloc,
853+
IRGenModule &IGM) {
873854
auto *ty = IGM.CoroAllocateFnTy;
874855
return IGM.getOrCreateHelperFunction(
875-
"_swift_coro_malloc", ty->getReturnType(), ty->params(),
876-
[](IRGenFunction &IGF) {
856+
name, ty->getReturnType(), ty->params(),
857+
[getAlloc](IRGenFunction &IGF) {
877858
auto parameters = IGF.collectParameters();
878859
parameters.claimNext(); // allocator
879860
auto *size = parameters.claimNext();
880-
auto alloc = IGF.IGM.getMallocFunctionPointer();
861+
auto alloc = (IGF.IGM.*getAlloc)();
881862
auto *call = IGF.Builder.CreateCall(alloc, {size});
882863
IGF.Builder.CreateRet(call);
883864
},
@@ -891,15 +872,18 @@ static llvm::Constant *getAddrOfSwiftCoroMalloc(IRGenModule &IGM) {
891872
});
892873
}
893874

894-
static llvm::Constant *getAddrOfSwiftCoroFree(IRGenModule &IGM) {
875+
using GetDeallocFunctionPointer = FunctionPointer (IRGenModule::*)();
876+
877+
static llvm::Constant *getAddrOfSwiftCoroDeallocThunk(
878+
StringRef name, GetDeallocFunctionPointer getDealloc, IRGenModule &IGM) {
895879
auto *ty = IGM.CoroDeallocateFnTy;
896880
return IGM.getOrCreateHelperFunction(
897-
"_swift_coro_free", ty->getReturnType(), ty->params(),
898-
[](IRGenFunction &IGF) {
881+
name, ty->getReturnType(), ty->params(),
882+
[getDealloc](IRGenFunction &IGF) {
899883
auto parameters = IGF.collectParameters();
900884
parameters.claimNext(); // allocator
901885
auto *ptr = parameters.claimNext();
902-
auto dealloc = IGF.IGM.getFreeFunctionPointer();
886+
auto dealloc = (IGF.IGM.*getDealloc)();
903887
IGF.Builder.CreateCall(dealloc, {ptr});
904888
IGF.Builder.CreateRetVoid();
905889
},
@@ -913,6 +897,39 @@ static llvm::Constant *getAddrOfSwiftCoroFree(IRGenModule &IGM) {
913897
});
914898
}
915899

900+
static llvm::Constant *getAddrOfGlobalCoroAllocator(
901+
IRGenModule &IGM, CoroAllocatorKind kind, bool shouldDeallocateImmediately,
902+
llvm::Constant *allocFn, llvm::Constant *deallocFn) {
903+
auto entity = LinkEntity::forCoroAllocator(kind);
904+
auto taskAllocator = IGM.getOrCreateLazyGlobalVariable(
905+
entity,
906+
[&](ConstantInitBuilder &builder) -> ConstantInitFuture {
907+
auto allocator = builder.beginStruct(IGM.CoroAllocatorTy);
908+
auto flags = CoroAllocatorFlags(kind);
909+
flags.setShouldDeallocateImmediately(shouldDeallocateImmediately);
910+
allocator.addInt32(flags.getOpaqueValue());
911+
allocator.addSignedPointer(
912+
allocFn, IGM.getOptions().PointerAuth.CoroAllocationFunction,
913+
PointerAuthEntity::Special::CoroAllocationFunction);
914+
allocator.addSignedPointer(
915+
deallocFn, IGM.getOptions().PointerAuth.CoroDeallocationFunction,
916+
PointerAuthEntity::Special::CoroDeallocationFunction);
917+
return allocator.finishAndCreateFuture();
918+
},
919+
[&](llvm::GlobalVariable *var) { var->setConstant(true); });
920+
return taskAllocator;
921+
}
922+
923+
static llvm::Constant *getAddrOfSwiftCoroMalloc(IRGenModule &IGM) {
924+
return getAddrOfSwiftCoroAllocThunk(
925+
"_swift_coro_malloc", &IRGenModule::getMallocFunctionPointer, IGM);
926+
}
927+
928+
static llvm::Constant *getAddrOfSwiftCoroFree(IRGenModule &IGM) {
929+
return getAddrOfSwiftCoroDeallocThunk(
930+
"_swift_coro_free", &IRGenModule::getFreeFunctionPointer, IGM);
931+
}
932+
916933
llvm::Constant *IRGenModule::getAddrOfGlobalCoroMallocAllocator() {
917934
return getAddrOfGlobalCoroAllocator(*this, CoroAllocatorKind::Malloc,
918935
/*shouldDeallocateImmediately=*/true,
@@ -921,47 +938,14 @@ llvm::Constant *IRGenModule::getAddrOfGlobalCoroMallocAllocator() {
921938
}
922939

923940
static llvm::Constant *getAddrOfSwiftCoroTaskAlloc(IRGenModule &IGM) {
924-
auto *ty = IGM.CoroAllocateFnTy;
925-
return IGM.getOrCreateHelperFunction(
926-
"_swift_coro_task_alloc", ty->getReturnType(), ty->params(),
927-
[](IRGenFunction &IGF) {
928-
auto parameters = IGF.collectParameters();
929-
parameters.claimNext(); // allocator
930-
auto *size = parameters.claimNext();
931-
auto alloc = IGF.IGM.getTaskAllocFunctionPointer();
932-
auto *call = IGF.Builder.CreateCall(alloc, {size});
933-
IGF.Builder.CreateRet(call);
934-
},
935-
/*setIsNoInline=*/true, /*forPrologue=*/false,
936-
/*isPerformanceConstraint=*/false,
937-
/*optionalLinkage=*/nullptr,
938-
/*specialCallingConv=*/std::nullopt,
939-
/*transformAttributes=*/
940-
[&IGM](llvm::AttributeList &attrs) {
941-
IGM.addSwiftCoroAttributes(attrs, 0);
942-
});
941+
return getAddrOfSwiftCoroAllocThunk(
942+
"_swift_coro_task_alloc", &IRGenModule::getTaskAllocFunctionPointer, IGM);
943943
}
944944

945945
static llvm::Constant *getAddrOfSwiftCoroTaskDealloc(IRGenModule &IGM) {
946-
auto *ty = IGM.CoroDeallocateFnTy;
947-
return IGM.getOrCreateHelperFunction(
948-
"_swift_coro_task_dealloc", ty->getReturnType(), ty->params(),
949-
[](IRGenFunction &IGF) {
950-
auto parameters = IGF.collectParameters();
951-
parameters.claimNext(); // allocator
952-
auto *ptr = parameters.claimNext();
953-
auto dealloc = IGF.IGM.getTaskDeallocFunctionPointer();
954-
IGF.Builder.CreateCall(dealloc, {ptr});
955-
IGF.Builder.CreateRetVoid();
956-
},
957-
/*setIsNoInline=*/true, /*forPrologue=*/false,
958-
/*isPerformanceConstraint=*/false,
959-
/*optionalLinkage=*/nullptr,
960-
/*specialCallingConv=*/std::nullopt,
961-
/*transformAttributes=*/
962-
[&IGM](llvm::AttributeList &attrs) {
963-
IGM.addSwiftCoroAttributes(attrs, 0);
964-
});
946+
return getAddrOfSwiftCoroDeallocThunk(
947+
"_swift_coro_task_dealloc", &IRGenModule::getTaskDeallocFunctionPointer,
948+
IGM);
965949
}
966950

967951
llvm::Constant *IRGenModule::getAddrOfGlobalCoroAsyncTaskAllocator() {

0 commit comments

Comments
 (0)