Skip to content

Commit 5a46d84

Browse files
committed
[NFC] IRGen: Move these functions.
1 parent d00b73c commit 5a46d84

File tree

4 files changed

+89
-82
lines changed

4 files changed

+89
-82
lines changed

lib/IRGen/GenCall.cpp

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5174,85 +5174,6 @@ Address irgen::emitAllocYieldManyCoroutineBuffer(IRGenFunction &IGF) {
51745174
getYieldManyCoroutineBufferAlignment(IGF.IGM));
51755175
}
51765176

5177-
static llvm::Constant *getAddrOfSwiftCCMalloc(IRGenModule &IGM) {
5178-
auto mallocFnPtr = IGM.getMallocFunctionPointer();
5179-
auto sig = mallocFnPtr.getSignature();
5180-
if (sig.getCallingConv() == IGM.SwiftCC) {
5181-
return IGM.getMallocFn();
5182-
}
5183-
return IGM.getOrCreateHelperFunction(
5184-
"_swift_malloc", sig.getType()->getReturnType(), sig.getType()->params(),
5185-
[](IRGenFunction &IGF) {
5186-
auto parameters = IGF.collectParameters();
5187-
auto *size = parameters.claimNext();
5188-
auto malloc = IGF.IGM.getMallocFunctionPointer();
5189-
auto *call = IGF.Builder.CreateCall(malloc, {size});
5190-
IGF.Builder.CreateRet(call);
5191-
});
5192-
}
5193-
5194-
static llvm::Constant *getAddrOfSwiftCCFree(IRGenModule &IGM) {
5195-
auto freeFnPtr = IGM.getFreeFunctionPointer();
5196-
auto sig = freeFnPtr.getSignature();
5197-
if (sig.getCallingConv() == IGM.SwiftCC) {
5198-
return IGM.getFreeFn();
5199-
}
5200-
return IGM.getOrCreateHelperFunction(
5201-
"_swift_free", sig.getType()->getReturnType(), sig.getType()->params(),
5202-
[](IRGenFunction &IGF) {
5203-
auto parameters = IGF.collectParameters();
5204-
auto *ptr = parameters.claimNext();
5205-
auto free = IGF.IGM.getFreeFunctionPointer();
5206-
IGF.Builder.CreateCall(free, {ptr});
5207-
IGF.Builder.CreateRetVoid();
5208-
});
5209-
}
5210-
5211-
static llvm::Constant *getAddrOfGlobalCoroAllocator(
5212-
IRGenModule &IGM, CoroAllocatorKind kind, bool shouldDeallocateImmediately,
5213-
llvm::Constant *allocFn, llvm::Constant *deallocFn) {
5214-
auto entity = LinkEntity::forCoroAllocator(kind);
5215-
auto taskAllocator = IGM.getOrCreateLazyGlobalVariable(
5216-
entity,
5217-
[&](ConstantInitBuilder &builder) -> ConstantInitFuture {
5218-
auto allocator = builder.beginStruct(IGM.CoroAllocatorTy);
5219-
auto flags = CoroAllocatorFlags(kind);
5220-
flags.setShouldDeallocateImmediately(shouldDeallocateImmediately);
5221-
allocator.addInt32(flags.getOpaqueValue());
5222-
allocator.add(allocFn);
5223-
allocator.add(deallocFn);
5224-
return allocator.finishAndCreateFuture();
5225-
},
5226-
[&](llvm::GlobalVariable *var) { var->setConstant(true); });
5227-
return taskAllocator;
5228-
}
5229-
llvm::Constant *IRGenModule::getAddrOfGlobalCoroMallocAllocator() {
5230-
return getAddrOfGlobalCoroAllocator(*this, CoroAllocatorKind::Malloc,
5231-
/*shouldDeallocateImmediately=*/true,
5232-
getAddrOfSwiftCCMalloc(*this),
5233-
getAddrOfSwiftCCFree(*this));
5234-
}
5235-
llvm::Constant *IRGenModule::getAddrOfGlobalCoroAsyncTaskAllocator() {
5236-
return getAddrOfGlobalCoroAllocator(*this, CoroAllocatorKind::Async,
5237-
/*shouldDeallocateImmediately=*/false,
5238-
getTaskAllocFn(), getTaskDeallocFn());
5239-
}
5240-
llvm::Value *
5241-
irgen::emitYieldOnce2CoroutineAllocator(IRGenFunction &IGF,
5242-
std::optional<CoroAllocatorKind> kind) {
5243-
if (!kind) {
5244-
return IGF.getCoroutineAllocator();
5245-
}
5246-
switch (*kind) {
5247-
case CoroAllocatorKind::Stack:
5248-
return llvm::ConstantPointerNull::get(IGF.IGM.CoroAllocatorPtrTy);
5249-
case CoroAllocatorKind::Async:
5250-
return IGF.IGM.getAddrOfGlobalCoroAsyncTaskAllocator();
5251-
case CoroAllocatorKind::Malloc:
5252-
return IGF.IGM.getAddrOfGlobalCoroMallocAllocator();
5253-
}
5254-
llvm_unreachable("unhandled case");
5255-
}
52565177
StackAddress irgen::emitAllocYieldOnce2CoroutineFrame(IRGenFunction &IGF,
52575178
llvm::Value *size) {
52585179
return emitAllocCoroStaticFrame(IGF, size);

lib/IRGen/GenCall.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,6 @@ namespace irgen {
220220
CanSILFunctionType coroutineType,
221221
NativeCCEntryPointArgumentEmission &emission);
222222

223-
llvm::Value *
224-
emitYieldOnce2CoroutineAllocator(IRGenFunction &IGF,
225-
std::optional<CoroAllocatorKind> kind);
226223
StackAddress emitAllocYieldOnce2CoroutineFrame(IRGenFunction &IGF,
227224
llvm::Value *size);
228225
void emitDeallocYieldOnce2CoroutineFrame(IRGenFunction &IGF,

lib/IRGen/GenCoro.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "swift/ABI/Coro.h"
1314
#include "swift/ABI/MetadataValues.h"
1415
#include "swift/Basic/Assertions.h"
1516
#include "swift/IRGen/Linking.h"
1617
#include "llvm/IR/Function.h"
1718
#include "llvm/IR/Instructions.h"
1819
#include "llvm/IR/Module.h"
1920

21+
#include "ConstantBuilder.h"
2022
#include "Explosion.h"
2123
#include "GenCoro.h"
2224
#include "IRGenFunction.h"
@@ -811,3 +813,86 @@ llvm::Constant *swift::irgen::getCoroDeallocFn(IRGenModule &IGM) {
811813
IGM.addSwiftCoroAttributes(attrs, 0);
812814
});
813815
}
816+
817+
static llvm::Constant *getAddrOfGlobalCoroAllocator(
818+
IRGenModule &IGM, CoroAllocatorKind kind, bool shouldDeallocateImmediately,
819+
llvm::Constant *allocFn, llvm::Constant *deallocFn) {
820+
auto entity = LinkEntity::forCoroAllocator(kind);
821+
auto taskAllocator = IGM.getOrCreateLazyGlobalVariable(
822+
entity,
823+
[&](ConstantInitBuilder &builder) -> ConstantInitFuture {
824+
auto allocator = builder.beginStruct(IGM.CoroAllocatorTy);
825+
auto flags = CoroAllocatorFlags(kind);
826+
flags.setShouldDeallocateImmediately(shouldDeallocateImmediately);
827+
allocator.addInt32(flags.getOpaqueValue());
828+
allocator.add(allocFn);
829+
allocator.add(deallocFn);
830+
return allocator.finishAndCreateFuture();
831+
},
832+
[&](llvm::GlobalVariable *var) { var->setConstant(true); });
833+
return taskAllocator;
834+
}
835+
836+
static llvm::Constant *getAddrOfSwiftCCMalloc(IRGenModule &IGM) {
837+
auto mallocFnPtr = IGM.getMallocFunctionPointer();
838+
auto sig = mallocFnPtr.getSignature();
839+
if (sig.getCallingConv() == IGM.SwiftCC) {
840+
return IGM.getMallocFn();
841+
}
842+
return IGM.getOrCreateHelperFunction(
843+
"_swift_malloc", sig.getType()->getReturnType(), sig.getType()->params(),
844+
[](IRGenFunction &IGF) {
845+
auto parameters = IGF.collectParameters();
846+
auto *size = parameters.claimNext();
847+
auto malloc = IGF.IGM.getMallocFunctionPointer();
848+
auto *call = IGF.Builder.CreateCall(malloc, {size});
849+
IGF.Builder.CreateRet(call);
850+
});
851+
}
852+
853+
static llvm::Constant *getAddrOfSwiftCCFree(IRGenModule &IGM) {
854+
auto freeFnPtr = IGM.getFreeFunctionPointer();
855+
auto sig = freeFnPtr.getSignature();
856+
if (sig.getCallingConv() == IGM.SwiftCC) {
857+
return IGM.getFreeFn();
858+
}
859+
return IGM.getOrCreateHelperFunction(
860+
"_swift_free", sig.getType()->getReturnType(), sig.getType()->params(),
861+
[](IRGenFunction &IGF) {
862+
auto parameters = IGF.collectParameters();
863+
auto *ptr = parameters.claimNext();
864+
auto free = IGF.IGM.getFreeFunctionPointer();
865+
IGF.Builder.CreateCall(free, {ptr});
866+
IGF.Builder.CreateRetVoid();
867+
});
868+
}
869+
870+
llvm::Constant *IRGenModule::getAddrOfGlobalCoroMallocAllocator() {
871+
return getAddrOfGlobalCoroAllocator(*this, CoroAllocatorKind::Malloc,
872+
/*shouldDeallocateImmediately=*/true,
873+
getAddrOfSwiftCCMalloc(*this),
874+
getAddrOfSwiftCCFree(*this));
875+
}
876+
877+
llvm::Constant *IRGenModule::getAddrOfGlobalCoroAsyncTaskAllocator() {
878+
return getAddrOfGlobalCoroAllocator(*this, CoroAllocatorKind::Async,
879+
/*shouldDeallocateImmediately=*/false,
880+
getTaskAllocFn(), getTaskDeallocFn());
881+
}
882+
883+
llvm::Value *
884+
irgen::emitYieldOnce2CoroutineAllocator(IRGenFunction &IGF,
885+
std::optional<CoroAllocatorKind> kind) {
886+
if (!kind) {
887+
return IGF.getCoroutineAllocator();
888+
}
889+
switch (*kind) {
890+
case CoroAllocatorKind::Stack:
891+
return llvm::ConstantPointerNull::get(IGF.IGM.CoroAllocatorPtrTy);
892+
case CoroAllocatorKind::Async:
893+
return IGF.IGM.getAddrOfGlobalCoroAsyncTaskAllocator();
894+
case CoroAllocatorKind::Malloc:
895+
return IGF.IGM.getAddrOfGlobalCoroMallocAllocator();
896+
}
897+
llvm_unreachable("unhandled case");
898+
}

lib/IRGen/GenCoro.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@ class IRGenModule;
1818
llvm::Constant *getCoroAllocFn(IRGenModule &IGM);
1919
llvm::Constant *getCoroDeallocFn(IRGenModule &IGM);
2020

21+
llvm::Value *
22+
emitYieldOnce2CoroutineAllocator(IRGenFunction &IGF,
23+
std::optional<CoroAllocatorKind> kind);
24+
2125
} // end namespace irgen
2226
} // end namespace swift

0 commit comments

Comments
 (0)