Skip to content

Commit 1a75b08

Browse files
[SILGen] Fix swift_once initializer signature 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 ecf8a35 commit 1a75b08

File tree

8 files changed

+32
-28
lines changed

8 files changed

+32
-28
lines changed

include/swift/AST/Builtins.def

+1-1
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

+1-3
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

+6
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 unused 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

test/IRGen/lazy_globals.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// CHECK: @"$s12lazy_globals1ySivp" = hidden global %TSi zeroinitializer, align 8
88
// CHECK: @"$s12lazy_globals1zSivp" = hidden global %TSi zeroinitializer, align 8
99

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

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

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

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

test/SILGen/lazy_globals.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
// RUN: %target-swift-emit-silgen -parse-as-library %s | %FileCheck %s
22

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

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

19-
// CHECK: sil private [global_init_once_fn] [ossa] @[[T:.*]]WZ : $@convention(c) () -> () {
19+
// CHECK: sil private [global_init_once_fn] [ossa] @[[T:.*]]WZ : $@convention(c) (Builtin.RawPointer) -> () {
2020
// CHECK: alloc_global @$s12lazy_globals3FooV3fooSivpZ
2121
// CHECK: [[XADDR:%.*]] = global_addr @$s12lazy_globals3FooV3fooSivpZ : $*Int
2222
// CHECK: store {{.*}} to [trivial] [[XADDR]] : $*Int
@@ -26,8 +26,8 @@ struct Foo {
2626
// CHECK: sil hidden [global_init] [ossa] @$s12lazy_globals3FooV3fooSivau : $@convention(thin) () -> Builtin.RawPointer {
2727
// CHECK: [[TOKEN_ADDR:%.*]] = global_addr @[[T]]Wz : $*Builtin.Word
2828
// CHECK: [[TOKEN_PTR:%.*]] = address_to_pointer [[TOKEN_ADDR]] : $*Builtin.Word to $Builtin.RawPointer
29-
// CHECK: [[INIT_FUNC:%.*]] = function_ref @[[T]]WZ : $@convention(c) () -> ()
30-
// CHECK: builtin "once"([[TOKEN_PTR]] : $Builtin.RawPointer, [[INIT_FUNC]] : $@convention(c) () -> ()) : $()
29+
// CHECK: [[INIT_FUNC:%.*]] = function_ref @[[T]]WZ : $@convention(c) (Builtin.RawPointer) -> ()
30+
// CHECK: builtin "once"([[TOKEN_PTR]] : $Builtin.RawPointer, [[INIT_FUNC]] : $@convention(c) (Builtin.RawPointer) -> ()) : $()
3131
// CHECK: [[GLOBAL_ADDR:%.*]] = global_addr @$s12lazy_globals3FooV3fooSivpZ : $*Int
3232
// CHECK: [[GLOBAL_PTR:%.*]] = address_to_pointer [[GLOBAL_ADDR]] : $*Int to $Builtin.RawPointer
3333
// CHECK: return [[GLOBAL_PTR]] : $Builtin.RawPointer
@@ -40,7 +40,7 @@ struct Foo {
4040
static var initialized: Int = 57
4141
}
4242

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

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

66-
// CHECK: sil private [global_init_once_fn] [ossa] @[[T:.*2a1.*2b1.*]]WZ : $@convention(c) () -> () {
66+
// CHECK: sil private [global_init_once_fn] [ossa] @[[T:.*2a1.*2b1.*]]WZ : $@convention(c) (Builtin.RawPointer) -> () {
6767
// CHECK: function_ref @$s12lazy_globals1fSi_SityF : $@convention(thin) () -> (Int, Int)
6868
// CHECK: sil hidden [global_init] [ossa] @$s12lazy_globals2a1Sivau : $@convention(thin) () -> Builtin.RawPointer
69-
// CHECK: function_ref @[[T]]WZ : $@convention(c) () -> ()
69+
// CHECK: function_ref @[[T]]WZ : $@convention(c) (Builtin.RawPointer) -> ()
7070
// CHECK: global_addr @$s12lazy_globals2a1Sivp : $*Int
7171
// CHECK: sil hidden [global_init] [ossa] @$s12lazy_globals2b1Sivau : $@convention(thin) () -> Builtin.RawPointer {
72-
// CHECK: function_ref @[[T]]WZ : $@convention(c) () -> ()
72+
// CHECK: function_ref @[[T]]WZ : $@convention(c) (Builtin.RawPointer) -> ()
7373
// CHECK: global_addr @$s12lazy_globals2b1Sivp : $*Int
7474
var (a1, b1) = f()
7575

test/SILGen/observers.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,10 @@ class BBQ {
251251

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

test/SILOptimizer/access_marker_verify.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ func testShims() -> UInt32 {
635635

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

647647
var globalString2 = globalString1
648-
// CHECK-LABEL: sil private [global_init_once_fn] [ossa] @{{.*}}WZ : $@convention(c) () -> () {
648+
// CHECK-LABEL: sil private [global_init_once_fn] [ossa] @{{.*}}WZ : $@convention(c) (Builtin.RawPointer) -> () {
649649
// CHECK: alloc_global @$s20access_marker_verify13globalString2SSvp
650650
// CHECK: [[GA:%.*]] = global_addr @$s20access_marker_verify13globalString2SSvp : $*String
651651
// CHECK: apply
@@ -1045,7 +1045,7 @@ func testPointerInit(x: Int, y: UnsafeMutablePointer<Int>) {
10451045
class testInitExistentialGlobal {
10461046
static var testProperty: P = StructP()
10471047
}
1048-
// CHECK-LABEL: sil private [global_init_once_fn] [ossa] @{{.*}}WZ : $@convention(c) () -> () {
1048+
// CHECK-LABEL: sil private [global_init_once_fn] [ossa] @{{.*}}WZ : $@convention(c) (Builtin.RawPointer) -> () {
10491049
// CHECK: alloc_global @$s20access_marker_verify25testInitExistentialGlobalC0D8PropertyAA1P_pvpZ
10501050
// CHECK: [[GADR:%.*]] = global_addr @$s20access_marker_verify25testInitExistentialGlobalC0D8PropertyAA1P_pvpZ : $*P
10511051
// CHECK: %{{.*}} = apply %{{.*}}({{.*}}) : $@convention(method) (@thin StructP.Type) -> StructP

test/SILOptimizer/access_marker_verify_objc.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import Foundation
1212
// --- initializer `let` of CFString.
1313
// The verifier should ignore this.
1414

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

0 commit comments

Comments
 (0)