Skip to content

Commit a8d1f2f

Browse files
Merge pull request #4408 from swiftwasm/katei/wip-silgen-once
Handle `swift_once` workaround in a proper way
2 parents 961569d + bd0baf6 commit a8d1f2f

File tree

3 files changed

+7
-28
lines changed

3 files changed

+7
-28
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 & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,21 +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-
SmallVector<AnyFunctionType::Param, 1> params;
1655-
// wasm: Lazy global init is used with swift_once which passes a context
1656-
// pointer parameter. If lazy global init doesn't take a context argument,
1657-
// caller and callee signatures are mismatched and it causes runtime
1658-
// exception on WebAssembly runtime. So we need to add dummy argument
1659-
// to consume the context pointer.
1660-
// See also: emitLazyGlobalInitializer
1661-
if (C.LangOpts.Target.isOSBinFormatWasm()) {
1662-
auto dummyParam = AnyFunctionType::Param(C.getUnsafeRawPointerDecl()->getDeclaredInterfaceType()
1663-
->getCanonicalType());
1664-
params.push_back(dummyParam);
1665-
}
1666-
Type initType = FunctionType::get(params, TupleType::getEmpty(C),
1667-
type->getExtInfo());
1653+
auto *initType = blockParam->getType()->castTo<FunctionType>();
16681654
auto initSILType = cast<SILFunctionType>(
16691655
Types.getLoweredRValueType(TypeExpansionContext::minimal(), initType));
16701656

lib/SILGen/SILGenGlobalVariable.cpp

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

231-
ASTContext &C = getASTContext();
232-
// wasm: Lazy global init is used with swift_once which passes a context
233-
// pointer parameter. If lazy global init doesn't take a context argument,
234-
// caller and callee signatures are mismatched and it causes runtime
235-
// exception on WebAssembly runtime. So we need to add dummy argument
236-
// to consume the context pointer.
237-
// See also: emitLazyGlobalInitializer
238-
if (C.LangOpts.Target.isOSBinFormatWasm()) {
239-
auto UnsafeRawPointer = C.getUnsafeRawPointerDecl();
240-
auto UnsafeRawPtrTy = getLoweredType(UnsafeRawPointer->getDeclaredInterfaceType());
241-
F.front().createFunctionArgument(UnsafeRawPtrTy);
242-
}
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);
243236

244237
{
245238
Scope scope(Cleanups, binding);

0 commit comments

Comments
 (0)