Skip to content

[SILGen] Fix swift_once initializer signature for lazy global var #42155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/Builtins.def
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ BUILTIN_MISC_OPERATION(IntToFPWithOverflow, "itofp_with_overflow", "n", Special)
/// zeroInitializer has type <T> () -> T
BUILTIN_MISC_OPERATION(ZeroInitializer, "zeroInitializer", "n", Special)

/// once has type (Builtin.RawPointer, () -> ())
/// once has type (Builtin.RawPointer, (Builtin.RawPointer) -> ())
BUILTIN_MISC_OPERATION(Once, "once", "", Special)
/// onceWithContext has type (Builtin.RawPointer, (Builtin.RawPointer) -> (), Builtin.RawPointer)
BUILTIN_MISC_OPERATION(OnceWithContext, "onceWithContext", "", Special)
Expand Down
4 changes: 1 addition & 3 deletions lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1650,9 +1650,7 @@ SILFunction *SILGenModule::emitLazyGlobalInitializer(StringRef funcName,
auto *onceBuiltin =
cast<FuncDecl>(getBuiltinValueDecl(C, C.getIdentifier("once")));
auto blockParam = onceBuiltin->getParameters()->get(1);
auto *type = blockParam->getType()->castTo<FunctionType>();
Type initType = FunctionType::get({}, TupleType::getEmpty(C),
type->getExtInfo());
auto *initType = blockParam->getType()->castTo<FunctionType>();
auto initSILType = cast<SILFunctionType>(
Types.getLoweredRValueType(TypeExpansionContext::minimal(), initType));

Expand Down
6 changes: 6 additions & 0 deletions lib/SILGen/SILGenGlobalVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ void SILGenFunction::emitLazyGlobalInitializer(PatternBindingDecl *binding,
unsigned pbdEntry) {
MagicFunctionName = SILGenModule::getMagicFunctionName(binding->getDeclContext());

// Add unused context pointer argument required to pass to `Builtin.once`
SILBasicBlock &entry = *F.begin();
SILType rawPointerSILTy =
getLoweredLoadableType(getASTContext().TheRawPointerType);
entry.createFunctionArgument(rawPointerSILTy);

{
Scope scope(Cleanups, binding);

Expand Down
8 changes: 4 additions & 4 deletions test/IRGen/lazy_globals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// CHECK: @"$s12lazy_globals1ySivp" = hidden global %TSi zeroinitializer, align 8
// CHECK: @"$s12lazy_globals1zSivp" = hidden global %TSi zeroinitializer, align 8

// CHECK: define internal void @"[[T]]WZ"() {{.*}} {
// CHECK: define internal void @"[[T]]WZ"(i8* %0) {{.*}} {
// CHECK: entry:
// CHECK: store i64 1, i64* getelementptr inbounds (%TSi, %TSi* @"$s12lazy_globals1xSivp", i32 0, i32 0), align 8
// CHECK: store i64 2, i64* getelementptr inbounds (%TSi, %TSi* @"$s12lazy_globals1ySivp", i32 0, i32 0), align 8
Expand All @@ -17,17 +17,17 @@

// CHECK: define hidden swiftcc i8* @"$s12lazy_globals1xSivau"() {{.*}} {
// CHECK: entry:
// CHECK: call void @swift_once(i64* @"[[T]]Wz", i8* bitcast (void ()* @"[[T]]WZ" to i8*), i8* undef)
// CHECK: call void @swift_once(i64* @"[[T]]Wz", i8* bitcast (void (i8*)* @"[[T]]WZ" to i8*), i8* undef)
// CHECK: }

// CHECK: define hidden swiftcc i8* @"$s12lazy_globals1ySivau"() {{.*}} {
// CHECK: entry:
// CHECK: call void @swift_once(i64* @"[[T]]Wz", i8* bitcast (void ()* @"[[T]]WZ" to i8*), i8* undef)
// CHECK: call void @swift_once(i64* @"[[T]]Wz", i8* bitcast (void (i8*)* @"[[T]]WZ" to i8*), i8* undef)
// CHECK: }

// CHECK: define hidden swiftcc i8* @"$s12lazy_globals1zSivau"() {{.*}} {
// CHECK: entry:
// CHECK: call void @swift_once(i64* @"[[T]]Wz", i8* bitcast (void ()* @"[[T]]WZ" to i8*), i8* undef)
// CHECK: call void @swift_once(i64* @"[[T]]Wz", i8* bitcast (void (i8*)* @"[[T]]WZ" to i8*), i8* undef)
// CHECK: }
var (x, y, z) = (1, 2, 3)

Expand Down
24 changes: 12 additions & 12 deletions test/SILGen/lazy_globals.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
// RUN: %target-swift-emit-silgen -parse-as-library %s | %FileCheck %s

// CHECK: sil private [global_init_once_fn] [ossa] @[[T:.*]]WZ : $@convention(c) () -> () {
// CHECK: sil private [global_init_once_fn] [ossa] @[[T:.*]]WZ : $@convention(c) (Builtin.RawPointer) -> () {
// CHECK: alloc_global @$s12lazy_globals1xSiv
// CHECK: [[XADDR:%.*]] = global_addr @$s12lazy_globals1xSivp : $*Int
// CHECK: store {{%.*}} to [trivial] [[XADDR]] : $*Int

// CHECK: sil hidden [global_init] [ossa] @$s12lazy_globals1xSivau : $@convention(thin) () -> Builtin.RawPointer {
// CHECK: [[TOKEN_ADDR:%.*]] = global_addr @[[T]]Wz : $*Builtin.Word
// CHECK: [[TOKEN_PTR:%.*]] = address_to_pointer [[TOKEN_ADDR]] : $*Builtin.Word to $Builtin.RawPointer
// CHECK: [[INIT_FUNC:%.*]] = function_ref @[[T]]WZ : $@convention(c) () -> ()
// CHECK: builtin "once"([[TOKEN_PTR]] : $Builtin.RawPointer, [[INIT_FUNC]] : $@convention(c) () -> ()) : $()
// CHECK: [[INIT_FUNC:%.*]] = function_ref @[[T]]WZ : $@convention(c) (Builtin.RawPointer) -> ()
// CHECK: builtin "once"([[TOKEN_PTR]] : $Builtin.RawPointer, [[INIT_FUNC]] : $@convention(c) (Builtin.RawPointer) -> ()) : $()
// CHECK: [[GLOBAL_ADDR:%.*]] = global_addr @$s12lazy_globals1xSivp : $*Int
// CHECK: [[GLOBAL_PTR:%.*]] = address_to_pointer [[GLOBAL_ADDR]] : $*Int to $Builtin.RawPointer
// CHECK: return [[GLOBAL_PTR]] : $Builtin.RawPointer
// CHECK: }
var x: Int = 0

// CHECK: sil private [global_init_once_fn] [ossa] @[[T:.*]]WZ : $@convention(c) () -> () {
// CHECK: sil private [global_init_once_fn] [ossa] @[[T:.*]]WZ : $@convention(c) (Builtin.RawPointer) -> () {
// CHECK: alloc_global @$s12lazy_globals3FooV3fooSivpZ
// CHECK: [[XADDR:%.*]] = global_addr @$s12lazy_globals3FooV3fooSivpZ : $*Int
// CHECK: store {{.*}} to [trivial] [[XADDR]] : $*Int
Expand All @@ -26,8 +26,8 @@ struct Foo {
// CHECK: sil hidden [global_init] [ossa] @$s12lazy_globals3FooV3fooSivau : $@convention(thin) () -> Builtin.RawPointer {
// CHECK: [[TOKEN_ADDR:%.*]] = global_addr @[[T]]Wz : $*Builtin.Word
// CHECK: [[TOKEN_PTR:%.*]] = address_to_pointer [[TOKEN_ADDR]] : $*Builtin.Word to $Builtin.RawPointer
// CHECK: [[INIT_FUNC:%.*]] = function_ref @[[T]]WZ : $@convention(c) () -> ()
// CHECK: builtin "once"([[TOKEN_PTR]] : $Builtin.RawPointer, [[INIT_FUNC]] : $@convention(c) () -> ()) : $()
// CHECK: [[INIT_FUNC:%.*]] = function_ref @[[T]]WZ : $@convention(c) (Builtin.RawPointer) -> ()
// CHECK: builtin "once"([[TOKEN_PTR]] : $Builtin.RawPointer, [[INIT_FUNC]] : $@convention(c) (Builtin.RawPointer) -> ()) : $()
// CHECK: [[GLOBAL_ADDR:%.*]] = global_addr @$s12lazy_globals3FooV3fooSivpZ : $*Int
// CHECK: [[GLOBAL_PTR:%.*]] = address_to_pointer [[GLOBAL_ADDR]] : $*Int to $Builtin.RawPointer
// CHECK: return [[GLOBAL_PTR]] : $Builtin.RawPointer
Expand All @@ -40,7 +40,7 @@ struct Foo {
static var initialized: Int = 57
}

// CHECK: sil private [global_init_once_fn] [ossa] @[[T:.*3bar.*]]WZ : $@convention(c) () -> () {
// CHECK: sil private [global_init_once_fn] [ossa] @[[T:.*3bar.*]]WZ : $@convention(c) (Builtin.RawPointer) -> () {
// CHECK: alloc_global @$s12lazy_globals3BarO3barSivpZ
// CHECK: [[XADDR:%.*]] = global_addr @$s12lazy_globals3BarO3barSivpZ : $*Int
// CHECK: store {{.*}} to [trivial] [[XADDR]] : $*Int
Expand All @@ -50,8 +50,8 @@ enum Bar {
// CHECK: sil hidden [global_init] [ossa] @$s12lazy_globals3BarO3barSivau : $@convention(thin) () -> Builtin.RawPointer {
// CHECK: [[TOKEN_ADDR:%.*]] = global_addr @[[T]]Wz : $*Builtin.Word
// CHECK: [[TOKEN_PTR:%.*]] = address_to_pointer [[TOKEN_ADDR]] : $*Builtin.Word to $Builtin.RawPointer
// CHECK: [[INIT_FUNC:%.*]] = function_ref @[[T]]WZ : $@convention(c) () -> ()
// CHECK: builtin "once"([[TOKEN_PTR]] : $Builtin.RawPointer, [[INIT_FUNC]] : $@convention(c) () -> ()) : $()
// CHECK: [[INIT_FUNC:%.*]] = function_ref @[[T]]WZ : $@convention(c) (Builtin.RawPointer) -> ()
// CHECK: builtin "once"([[TOKEN_PTR]] : $Builtin.RawPointer, [[INIT_FUNC]] : $@convention(c) (Builtin.RawPointer) -> ()) : $()
// CHECK: [[GLOBAL_ADDR:%.*]] = global_addr @$s12lazy_globals3BarO3barSivpZ : $*Int
// CHECK: [[GLOBAL_PTR:%.*]] = address_to_pointer [[GLOBAL_ADDR]] : $*Int to $Builtin.RawPointer
// CHECK: return [[GLOBAL_PTR]] : $Builtin.RawPointer
Expand All @@ -63,13 +63,13 @@ enum Bar {

func f() -> (Int, Int) { return (1, 2) }

// CHECK: sil private [global_init_once_fn] [ossa] @[[T:.*2a1.*2b1.*]]WZ : $@convention(c) () -> () {
// CHECK: sil private [global_init_once_fn] [ossa] @[[T:.*2a1.*2b1.*]]WZ : $@convention(c) (Builtin.RawPointer) -> () {
// CHECK: function_ref @$s12lazy_globals1fSi_SityF : $@convention(thin) () -> (Int, Int)
// CHECK: sil hidden [global_init] [ossa] @$s12lazy_globals2a1Sivau : $@convention(thin) () -> Builtin.RawPointer
// CHECK: function_ref @[[T]]WZ : $@convention(c) () -> ()
// CHECK: function_ref @[[T]]WZ : $@convention(c) (Builtin.RawPointer) -> ()
// CHECK: global_addr @$s12lazy_globals2a1Sivp : $*Int
// CHECK: sil hidden [global_init] [ossa] @$s12lazy_globals2b1Sivau : $@convention(thin) () -> Builtin.RawPointer {
// CHECK: function_ref @[[T]]WZ : $@convention(c) () -> ()
// CHECK: function_ref @[[T]]WZ : $@convention(c) (Builtin.RawPointer) -> ()
// CHECK: global_addr @$s12lazy_globals2b1Sivp : $*Int
var (a1, b1) = f()

Expand Down
6 changes: 3 additions & 3 deletions test/SILGen/observers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@ class BBQ {

var global_observing_property : Int = zero {
// The variable is initialized with "zero".
// CHECK-LABEL: sil private [global_init_once_fn] [ossa] @{{.*}}WZ : $@convention(c) () -> () {
// CHECK: bb0:
// CHECK-LABEL: sil private [global_init_once_fn] [ossa] @{{.*}}WZ : $@convention(c) (Builtin.RawPointer) -> () {
// CHECK: bb0(%0 : $Builtin.RawPointer):
// CHECK-NEXT: alloc_global @$s9observers25global_observing_propertySiv
// CHECK-NEXT: %1 = global_addr @$s9observers25global_observing_propertySivp : $*Int
// CHECK-NEXT: %2 = global_addr @$s9observers25global_observing_propertySivp : $*Int
// CHECK: observers.zero.unsafeMutableAddressor
// CHECK: return

Expand Down
6 changes: 3 additions & 3 deletions test/SILOptimizer/access_marker_verify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ func testShims() -> UInt32 {

// --- global variable initialization.
var globalString1 = "⓪" // start non-empty
// CHECK-LABEL: sil private [global_init_once_fn] [ossa] @{{.*}}WZ : $@convention(c) () -> () {
// CHECK-LABEL: sil private [global_init_once_fn] [ossa] @{{.*}}WZ : $@convention(c) (Builtin.RawPointer) -> () {
// CHECK: alloc_global @$s20access_marker_verify13globalString1SSvp
// CHECK: [[GA:%.*]] = global_addr @$s20access_marker_verify13globalString1SSvp : $*String
// CHECK: apply
Expand All @@ -645,7 +645,7 @@ var globalString1 = "⓪" // start non-empty
// CHECK-LABEL: } // end sil function '{{.*}}WZ'

var globalString2 = globalString1
// CHECK-LABEL: sil private [global_init_once_fn] [ossa] @{{.*}}WZ : $@convention(c) () -> () {
// CHECK-LABEL: sil private [global_init_once_fn] [ossa] @{{.*}}WZ : $@convention(c) (Builtin.RawPointer) -> () {
// CHECK: alloc_global @$s20access_marker_verify13globalString2SSvp
// CHECK: [[GA:%.*]] = global_addr @$s20access_marker_verify13globalString2SSvp : $*String
// CHECK: apply
Expand Down Expand Up @@ -1045,7 +1045,7 @@ func testPointerInit(x: Int, y: UnsafeMutablePointer<Int>) {
class testInitExistentialGlobal {
static var testProperty: P = StructP()
}
// CHECK-LABEL: sil private [global_init_once_fn] [ossa] @{{.*}}WZ : $@convention(c) () -> () {
// CHECK-LABEL: sil private [global_init_once_fn] [ossa] @{{.*}}WZ : $@convention(c) (Builtin.RawPointer) -> () {
// CHECK: alloc_global @$s20access_marker_verify25testInitExistentialGlobalC0D8PropertyAA1P_pvpZ
// CHECK: [[GADR:%.*]] = global_addr @$s20access_marker_verify25testInitExistentialGlobalC0D8PropertyAA1P_pvpZ : $*P
// CHECK: %{{.*}} = apply %{{.*}}({{.*}}) : $@convention(method) (@thin StructP.Type) -> StructP
Expand Down
4 changes: 2 additions & 2 deletions test/SILOptimizer/access_marker_verify_objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import Foundation
// --- initializer `let` of CFString.
// The verifier should ignore this.

// CHECK_LABEL: sil private @{{.*}}WZ : $@convention(c) () -> () {
// CHECK: bb0:
// CHECK_LABEL: sil private @{{.*}}WZ : $@convention(c) (Builtin.RawPointer) -> () {
// CHECK: bb0(%0 : $Builtin.RawPointer):
// CHECK: alloc_global @$s25access_marker_verify_objc12testCFStringC8cfStringSo0F3RefavpZ
// CHECK: [[GA:%.*]] = global_addr @$s25access_marker_verify_objc12testCFStringC8cfStringSo0F3RefavpZ : $*CFString
// CHECK-NOT: begin_access
Expand Down