Skip to content

Commit ae313fe

Browse files
committed
SIL: allow creating convert_function and thin_to_thick_function instructions in an initializer of a SILGlobalVariable.
This means: don't require to have a SILFunction in the SILBuilder.
1 parent 542a378 commit ae313fe

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ class SILBuilder {
201201
bool isInsertingIntoGlobal() const { return F == nullptr; }
202202

203203
TypeExpansionContext getTypeExpansionContext() const {
204+
if (!F)
205+
return TypeExpansionContext::minimal();
204206
return TypeExpansionContext(getFunction());
205207
}
206208

@@ -1027,7 +1029,7 @@ class SILBuilder {
10271029
SILType Ty,
10281030
bool WithoutActuallyEscaping) {
10291031
return insert(ConvertFunctionInst::create(getSILDebugLocation(Loc), Op, Ty,
1030-
getFunction(), C.OpenedArchetypes,
1032+
getModule(), F, C.OpenedArchetypes,
10311033
WithoutActuallyEscaping));
10321034
}
10331035

@@ -1157,7 +1159,7 @@ class SILBuilder {
11571159
ThinToThickFunctionInst *createThinToThickFunction(SILLocation Loc,
11581160
SILValue Op, SILType Ty) {
11591161
return insert(ThinToThickFunctionInst::create(
1160-
getSILDebugLocation(Loc), Op, Ty, getFunction(), C.OpenedArchetypes));
1162+
getSILDebugLocation(Loc), Op, Ty, getModule(), F, C.OpenedArchetypes));
11611163
}
11621164

11631165
ThickToObjCMetatypeInst *createThickToObjCMetatype(SILLocation Loc,

include/swift/SIL/SILInstruction.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4815,7 +4815,7 @@ class ConvertFunctionInst final
48154815

48164816
static ConvertFunctionInst *create(SILDebugLocation DebugLoc,
48174817
SILValue Operand, SILType Ty,
4818-
SILFunction &F,
4818+
SILModule &Mod, SILFunction *F,
48194819
SILOpenedArchetypesState &OpenedArchetypes,
48204820
bool WithoutActuallyEscaping);
48214821

@@ -5165,8 +5165,8 @@ class ThinToThickFunctionInst final
51655165
Operand.getOwnershipKind()) {}
51665166

51675167
static ThinToThickFunctionInst *
5168-
create(SILDebugLocation DebugLoc, SILValue Operand, SILType Ty,
5169-
SILFunction &F, SILOpenedArchetypesState &OpenedArchetypes);
5168+
create(SILDebugLocation DebugLoc, SILValue Operand, SILType Ty, SILModule &Mod,
5169+
SILFunction *F, SILOpenedArchetypesState &OpenedArchetypes);
51705170

51715171
public:
51725172
/// Return the callee of the thin_to_thick_function.

lib/SIL/IR/SILInstructions.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2317,12 +2317,14 @@ UpcastInst *UpcastInst::create(SILDebugLocation DebugLoc, SILValue Operand,
23172317

23182318
ThinToThickFunctionInst *
23192319
ThinToThickFunctionInst::create(SILDebugLocation DebugLoc, SILValue Operand,
2320-
SILType Ty, SILFunction &F,
2320+
SILType Ty, SILModule &Mod, SILFunction *F,
23212321
SILOpenedArchetypesState &OpenedArchetypes) {
2322-
SILModule &Mod = F.getModule();
23232322
SmallVector<SILValue, 8> TypeDependentOperands;
2324-
collectTypeDependentOperands(TypeDependentOperands, OpenedArchetypes, F,
2325-
Ty.getASTType());
2323+
if (F) {
2324+
assert(&F->getModule() == &Mod);
2325+
collectTypeDependentOperands(TypeDependentOperands, OpenedArchetypes, *F,
2326+
Ty.getASTType());
2327+
}
23262328
unsigned size =
23272329
totalSizeToAlloc<swift::Operand>(1 + TypeDependentOperands.size());
23282330
void *Buffer = Mod.allocateInst(size, alignof(ThinToThickFunctionInst));
@@ -2346,12 +2348,15 @@ PointerToThinFunctionInst::create(SILDebugLocation DebugLoc, SILValue Operand,
23462348
}
23472349

23482350
ConvertFunctionInst *ConvertFunctionInst::create(
2349-
SILDebugLocation DebugLoc, SILValue Operand, SILType Ty, SILFunction &F,
2351+
SILDebugLocation DebugLoc, SILValue Operand, SILType Ty, SILModule &Mod,
2352+
SILFunction *F,
23502353
SILOpenedArchetypesState &OpenedArchetypes, bool WithoutActuallyEscaping) {
2351-
SILModule &Mod = F.getModule();
23522354
SmallVector<SILValue, 8> TypeDependentOperands;
2353-
collectTypeDependentOperands(TypeDependentOperands, OpenedArchetypes, F,
2354-
Ty.getASTType());
2355+
if (F) {
2356+
assert(&F->getModule() == &Mod);
2357+
collectTypeDependentOperands(TypeDependentOperands, OpenedArchetypes, *F,
2358+
Ty.getASTType());
2359+
}
23552360
unsigned size =
23562361
totalSizeToAlloc<swift::Operand>(1 + TypeDependentOperands.size());
23572362
void *Buffer = Mod.allocateInst(size, alignof(ConvertFunctionInst));
@@ -2362,14 +2367,14 @@ ConvertFunctionInst *ConvertFunctionInst::create(
23622367
//
23632368
// *NOTE* We purposely do not use an early return here to ensure that in
23642369
// builds without assertions this whole if statement is optimized out.
2365-
if (F.getModule().getStage() != SILStage::Lowered) {
2370+
if (Mod.getStage() != SILStage::Lowered) {
23662371
// Make sure we are not performing ABI-incompatible conversions.
23672372
CanSILFunctionType opTI =
23682373
CFI->getOperand()->getType().castTo<SILFunctionType>();
23692374
(void)opTI;
23702375
CanSILFunctionType resTI = CFI->getType().castTo<SILFunctionType>();
23712376
(void)resTI;
2372-
assert(opTI->isABICompatibleWith(resTI, F).isCompatible() &&
2377+
assert((!F || opTI->isABICompatibleWith(resTI, *F).isCompatible()) &&
23732378
"Can not convert in between ABI incompatible function types");
23742379
}
23752380
return CFI;

0 commit comments

Comments
 (0)