Skip to content

Commit 8440a82

Browse files
committed
IRGen: More refactoring in preparation for emitting async function pointers for dispatch thunks
1 parent c36c320 commit 8440a82

File tree

8 files changed

+44
-26
lines changed

8 files changed

+44
-26
lines changed

include/swift/IRGen/Linking.h

+19-9
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ class LinkEntity {
130130
/// is a ConstructorDecl* inside a class.
131131
DispatchThunkInitializer,
132132

133-
/// A method dispatch thunk for an allocating constructor. The pointer is a
134-
/// ConstructorDecl* inside a protocol or a class.
133+
/// A method dispatch thunk for an allocating constructor. The pointer is
134+
/// a ConstructorDecl* inside a protocol or a class.
135135
DispatchThunkAllocator,
136136

137137
/// A method descriptor. The pointer is a FuncDecl* inside a protocol
@@ -295,7 +295,7 @@ class LinkEntity {
295295

296296
/// The same as AsyncFunctionPointer but with a different stored value, for
297297
/// use by TBDGen.
298-
/// The pointer is a AbstractStorageDecl*.
298+
/// The pointer is an AbstractFunctionDecl*.
299299
AsyncFunctionPointerAST,
300300

301301
/// The pointer is a SILFunction*.
@@ -861,7 +861,8 @@ class LinkEntity {
861861
}
862862

863863
static LinkEntity
864-
forSILFunction(SILFunction *F, bool IsDynamicallyReplaceableImplementation) {
864+
forSILFunction(SILFunction *F,
865+
bool IsDynamicallyReplaceableImplementation=false) {
865866
LinkEntity entity;
866867
entity.Pointer = F;
867868
entity.SecondaryPointer = nullptr;
@@ -1100,12 +1101,21 @@ class LinkEntity {
11001101
return entity;
11011102
}
11021103

1103-
static LinkEntity forAsyncFunctionPointer(SILFunction *silFunction) {
1104+
static LinkEntity forAsyncFunctionPointer(LinkEntity other) {
11041105
LinkEntity entity;
1105-
entity.Pointer = silFunction;
1106-
entity.SecondaryPointer = nullptr;
1107-
entity.Data = LINKENTITY_SET_FIELD(
1108-
Kind, unsigned(LinkEntity::Kind::AsyncFunctionPointer));
1106+
1107+
switch (other.getKind()) {
1108+
case LinkEntity::Kind::SILFunction:
1109+
entity.Pointer = other.getSILFunction();
1110+
entity.SecondaryPointer = nullptr;
1111+
entity.Data = LINKENTITY_SET_FIELD(
1112+
Kind, unsigned(LinkEntity::Kind::AsyncFunctionPointer));
1113+
break;
1114+
1115+
default:
1116+
llvm_unreachable("Link entity kind cannot have an async function pointer");
1117+
}
1118+
11091119
return entity;
11101120
}
11111121

lib/IRGen/Callee.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ namespace irgen {
174174

175175
// Temporary only!
176176
explicit FunctionPointer(KindTy kind, llvm::Value *value,
177-
const Signature &signature, bool
178-
isWithoutCtxt = false)
177+
const Signature &signature,
178+
bool isWithoutCtxt = false)
179179
: FunctionPointer(kind, value, PointerAuthInfo(), signature, isWithoutCtxt) {}
180180

181181
static FunctionPointer forDirect(IRGenModule &IGM,

lib/IRGen/GenKeyPath.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ emitKeyPathComponent(IRGenModule &IGM,
920920
idKind = KeyPathComponentHeader::Pointer;
921921
// FIXME: Does this need to be signed?
922922
auto idRef = IGM.getAddrOfLLVMVariableOrGOTEquivalent(
923-
LinkEntity::forSILFunction(id.getFunction(), false));
923+
LinkEntity::forSILFunction(id.getFunction()));
924924

925925
idValue = idRef.getValue();
926926
// If we got an indirect reference, we'll need to resolve it at

lib/IRGen/GenMeta.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -5353,14 +5353,14 @@ bool irgen::methodRequiresReifiedVTableEntry(IRGenModule &IGM,
53535353
}
53545354

53555355
llvm::GlobalValue *irgen::emitAsyncFunctionPointer(IRGenModule &IGM,
5356-
SILFunction *function,
5356+
llvm::Function *function,
5357+
LinkEntity entity,
53575358
Size size) {
53585359
ConstantInitBuilder initBuilder(IGM);
53595360
ConstantStructBuilder builder(
53605361
initBuilder.beginStruct(IGM.AsyncFunctionPointerTy));
5361-
builder.addRelativeAddress(
5362-
IGM.getAddrOfSILFunction(function, NotForDefinition));
5362+
builder.addRelativeAddress(function);
53635363
builder.addInt32(size.getValue());
53645364
return cast<llvm::GlobalValue>(IGM.defineAsyncFunctionPointer(
5365-
function, builder.finishAndCreateFuture()));
5365+
entity, builder.finishAndCreateFuture()));
53665366
}

lib/IRGen/GenMeta.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ namespace irgen {
4747
class Size;
4848
class StructLayout;
4949
class ClassLayout;
50+
class LinkEntity;
5051

5152
bool requiresForeignTypeMetadata(CanType type);
5253
bool requiresForeignTypeMetadata(NominalTypeDecl *decl);
@@ -183,7 +184,9 @@ namespace irgen {
183184
ArrayRef<Requirement> requirements);
184185

185186
llvm::GlobalValue *emitAsyncFunctionPointer(IRGenModule &IGM,
186-
SILFunction *function, Size size);
187+
llvm::Function *function,
188+
LinkEntity entity,
189+
Size size);
187190
} // end namespace irgen
188191
} // end namespace swift
189192

lib/IRGen/GenThunk.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,16 @@ void IRGenModule::emitDispatchThunk(SILDeclRef declRef) {
165165
llvm::Constant *
166166
IRGenModule::getAddrOfAsyncFunctionPointer(SILFunction *function) {
167167
(void)getAddrOfSILFunction(function, NotForDefinition);
168-
auto entity = LinkEntity::forAsyncFunctionPointer(function);
168+
auto entity = LinkEntity::forAsyncFunctionPointer(
169+
LinkEntity::forSILFunction(function));
169170
return getAddrOfLLVMVariable(entity, NotForDefinition, DebugTypeInfo());
170171
}
171172

172-
llvm::Constant *IRGenModule::defineAsyncFunctionPointer(SILFunction *function,
173+
llvm::Constant *IRGenModule::defineAsyncFunctionPointer(LinkEntity entity,
173174
ConstantInit init) {
174-
auto entity = LinkEntity::forAsyncFunctionPointer(function);
175+
auto asyncEntity = LinkEntity::forAsyncFunctionPointer(entity);
175176
auto *var = cast<llvm::GlobalVariable>(
176-
getAddrOfLLVMVariable(entity, init, DebugTypeInfo()));
177+
getAddrOfLLVMVariable(asyncEntity, init, DebugTypeInfo()));
177178
setTrueConstGlobal(var);
178179
return var;
179180
}

lib/IRGen/IRGenModule.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ private: \
13971397
llvm::Constant *getOpaquePtr(llvm::Constant *pointer);
13981398

13991399
llvm::Constant *getAddrOfAsyncFunctionPointer(SILFunction *function);
1400-
llvm::Constant *defineAsyncFunctionPointer(SILFunction *function,
1400+
llvm::Constant *defineAsyncFunctionPointer(LinkEntity entity,
14011401
ConstantInit init);
14021402
SILFunction *getSILFunctionForAsyncFunctionPointer(llvm::Constant *afp);
14031403

lib/IRGen/IRGenSIL.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "swift/Basic/ExternalUnion.h"
2727
#include "swift/Basic/Range.h"
2828
#include "swift/Basic/STLExtras.h"
29+
#include "swift/IRGen/Linking.h"
2930
#include "swift/SIL/ApplySite.h"
3031
#include "swift/SIL/Dominance.h"
3132
#include "swift/SIL/InstructionUtils.h"
@@ -1943,9 +1944,12 @@ void IRGenSILFunction::emitSILFunction() {
19431944
IGM.IRGen.addDynamicReplacement(CurSILFn);
19441945

19451946
auto funcTy = CurSILFn->getLoweredFunctionType();
1946-
if (funcTy->isAsync() && funcTy->getLanguage() == SILFunctionLanguage::Swift)
1947-
emitAsyncFunctionPointer(IGM, CurSILFn,
1947+
if (funcTy->isAsync() && funcTy->getLanguage() == SILFunctionLanguage::Swift) {
1948+
emitAsyncFunctionPointer(IGM,
1949+
CurFn,
1950+
LinkEntity::forSILFunction(CurSILFn),
19481951
getAsyncContextLayout(*this).getSize());
1952+
}
19491953

19501954
// Configure the dominance resolver.
19511955
// TODO: consider re-using a dom analysis from the PassManager
@@ -2278,14 +2282,14 @@ void IRGenSILFunction::visitFunctionRefBaseInst(FunctionRefBaseInst *i) {
22782282
auto *fnPtr = IGM.getAddrOfSILFunction(
22792283
fn, NotForDefinition, false /*isDynamicallyReplaceableImplementation*/,
22802284
isa<PreviousDynamicFunctionRefInst>(i));
2281-
llvm::Value *value;
2285+
llvm::Constant *value;
22822286
auto isSpecialAsyncWithoutCtxtSize =
22832287
fn->isAsync() && (
22842288
fn->getName().equals("swift_task_future_wait") ||
22852289
fn->getName().equals("swift_task_group_wait_next"));
22862290
if (fn->isAsync() && !isSpecialAsyncWithoutCtxtSize) {
22872291
value = IGM.getAddrOfAsyncFunctionPointer(fn);
2288-
value = Builder.CreateBitCast(value, fnPtr->getType());
2292+
value = llvm::ConstantExpr::getBitCast(value, fnPtr->getType());
22892293
} else {
22902294
value = fnPtr;
22912295
}

0 commit comments

Comments
 (0)