Skip to content

Commit bd0baf6

Browse files
[SILGen] Fix swift_once initializer for lazy global var
`Builtin.once` has type `(Builtin.RawPointer, (Builtin.RawPointer) -> ())` at Swift level, but lazy global init passes its initializer as `() -> ()`, so their callee and caller signatures doesn't match.
1 parent d0492c2 commit bd0baf6

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

include/swift/AST/Builtins.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ BUILTIN_MISC_OPERATION(IntToFPWithOverflow, "itofp_with_overflow", "n", Special)
699699
/// zeroInitializer has type <T> () -> T
700700
BUILTIN_MISC_OPERATION(ZeroInitializer, "zeroInitializer", "n", Special)
701701

702-
/// once has type (Builtin.RawPointer, () -> ())
702+
/// once has type (Builtin.RawPointer, (Builtin.RawPointer) -> ())
703703
BUILTIN_MISC_OPERATION(Once, "once", "", Special)
704704
/// onceWithContext has type (Builtin.RawPointer, (Builtin.RawPointer) -> (), Builtin.RawPointer)
705705
BUILTIN_MISC_OPERATION(OnceWithContext, "onceWithContext", "", Special)

lib/SILGen/SILGen.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,9 +1650,7 @@ SILFunction *SILGenModule::emitLazyGlobalInitializer(StringRef funcName,
16501650
auto *onceBuiltin =
16511651
cast<FuncDecl>(getBuiltinValueDecl(C, C.getIdentifier("once")));
16521652
auto blockParam = onceBuiltin->getParameters()->get(1);
1653-
auto *type = blockParam->getType()->castTo<FunctionType>();
1654-
Type initType = FunctionType::get({}, TupleType::getEmpty(C),
1655-
type->getExtInfo());
1653+
auto *initType = blockParam->getType()->castTo<FunctionType>();
16561654
auto initSILType = cast<SILFunctionType>(
16571655
Types.getLoweredRValueType(TypeExpansionContext::minimal(), initType));
16581656

lib/SILGen/SILGenGlobalVariable.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ void SILGenFunction::emitLazyGlobalInitializer(PatternBindingDecl *binding,
228228
unsigned pbdEntry) {
229229
MagicFunctionName = SILGenModule::getMagicFunctionName(binding->getDeclContext());
230230

231+
// Add context pointer argument required to pass to `Builtin.once`
232+
SILBasicBlock &entry = *F.begin();
233+
SILType rawPointerSILTy
234+
= getLoweredLoadableType(getASTContext().TheRawPointerType);
235+
entry.createFunctionArgument(rawPointerSILTy);
236+
231237
{
232238
Scope scope(Cleanups, binding);
233239

0 commit comments

Comments
 (0)