Skip to content

Commit d82a767

Browse files
committed
SIL: Add a SILFunction::Purpose for global init once functions
1 parent ee8ad7b commit d82a767

File tree

11 files changed

+26
-15
lines changed

11 files changed

+26
-15
lines changed

include/swift/SIL/SILFunction.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class SILFunction
121121
enum class Purpose : uint8_t {
122122
None,
123123
GlobalInit,
124+
GlobalInitOnceFunction,
124125
LazyPropertyGetter
125126
};
126127

@@ -832,6 +833,10 @@ class SILFunction
832833
/// function itself does not need this attribute. It is private and only
833834
/// called within the addressor.
834835
bool isGlobalInit() const { return specialPurpose == Purpose::GlobalInit; }
836+
837+
bool isGlobalInitOnceFunction() const {
838+
return specialPurpose == Purpose::GlobalInitOnceFunction;
839+
}
835840

836841
bool isLazyPropertyGetter() const {
837842
return specialPurpose == Purpose::LazyPropertyGetter;

lib/SIL/IR/SILPrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,6 +2606,9 @@ void SILFunction::print(SILPrintContext &PrintCtx) const {
26062606
case SILFunction::Purpose::GlobalInit:
26072607
OS << "[global_init] ";
26082608
break;
2609+
case SILFunction::Purpose::GlobalInitOnceFunction:
2610+
OS << "[global_init_once_fn] ";
2611+
break;
26092612
case SILFunction::Purpose::LazyPropertyGetter:
26102613
OS << "[lazy_getter] ";
26112614
break;

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,8 @@ static bool parseDeclSILOptional(bool *isTransparent,
961961
*specialPurpose = SILFunction::Purpose::GlobalInit;
962962
else if (specialPurpose && SP.P.Tok.getText() == "lazy_getter")
963963
*specialPurpose = SILFunction::Purpose::LazyPropertyGetter;
964+
else if (specialPurpose && SP.P.Tok.getText() == "global_init_once_fn")
965+
*specialPurpose = SILFunction::Purpose::GlobalInitOnceFunction;
964966
else if (isWeakImported && SP.P.Tok.getText() == "weak_imported") {
965967
if (M.getASTContext().LangOpts.Target.isOSBinFormatCOFF())
966968
SP.P.diagnose(SP.P.Tok, diag::attr_unsupported_on_target,

lib/SILGen/SILGen.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,7 @@ SILFunction *SILGenModule::emitLazyGlobalInitializer(StringRef funcName,
13661366
auto *f = builder.createFunction(
13671367
SILLinkage::Private, funcName, initSILType, nullptr, SILLocation(binding),
13681368
IsNotBare, IsNotTransparent, IsNotSerialized, IsNotDynamic);
1369+
f->setSpecialPurpose(SILFunction::Purpose::GlobalInitOnceFunction);
13691370
f->setDebugScope(new (M) SILDebugScope(RegularLocation(binding), f));
13701371
auto dc = binding->getDeclContext();
13711372
SILGenFunction(*this, *f, dc).emitLazyGlobalInitializer(binding, pbdEntry);

lib/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
5757
/// Don't worry about adhering to the 80-column limit for this line.
58-
const uint16_t SWIFTMODULE_VERSION_MINOR = 574; // reapply isUserAccessible
58+
const uint16_t SWIFTMODULE_VERSION_MINOR = 575; // GlobalInitOnceFunction SILFunction purpose
5959

6060
/// A standard hash seed used for all string hashes in a serialized module.
6161
///

test/SILGen/default_arguments.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class Foo {
156156
return x
157157
}
158158

159-
// CHECK-LABEL: sil private [ossa] @globalinit_33_E52D764B1F2009F2390B2B8DF62DAEB8_func0
159+
// CHECK-LABEL: sil private [global_init_once_fn] [ossa] @globalinit_33_E52D764B1F2009F2390B2B8DF62DAEB8_func0
160160
// CHECK: string_literal utf8 "Foo"
161161
static let x = Foo(int:0)
162162

test/SILGen/global_resilience.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public var myEmptyGlobal = MyEmptyStruct()
4343

4444
// Mutable addressor for fixed-layout global
4545

46-
// CHECK-LABEL: sil private [ossa] @globalinit_{{.*}}_func1
46+
// CHECK-LABEL: sil private [global_init_once_fn] [ossa] @globalinit_{{.*}}_func1
4747
// CHECK: alloc_global @$s17global_resilience19myFixedLayoutGlobalAA13MyEmptyStructVv
4848
// CHECK: return
4949

@@ -52,7 +52,7 @@ public var myEmptyGlobal = MyEmptyStruct()
5252
// CHECK: global_addr @$s17global_resilience19myFixedLayoutGlobalAA13MyEmptyStructVv
5353
// CHECK: return
5454

55-
// CHECK-OPT-LABEL: sil private @globalinit_{{.*}}_func1
55+
// CHECK-OPT-LABEL: sil private [global_init_once_fn] @globalinit_{{.*}}_func1
5656
// CHECK-OPT: alloc_global @$s17global_resilience19myFixedLayoutGlobalAA13MyEmptyStructVv
5757
// CHECK-OPT: return
5858

test/SILGen/lazy_globals.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %target-swift-emit-silgen -parse-as-library %s | %FileCheck %s
22

3-
// CHECK: sil private [ossa] @globalinit_[[T:.*]]_func0 : $@convention(c) () -> () {
3+
// CHECK: sil private [global_init_once_fn] [ossa] @globalinit_[[T:.*]]_func0 : $@convention(c) () -> () {
44
// CHECK: alloc_global @$s12lazy_globals1xSiv
55
// CHECK: [[XADDR:%.*]] = global_addr @$s12lazy_globals1xSivp : $*Int
66
// CHECK: store {{%.*}} to [trivial] [[XADDR]] : $*Int
@@ -16,7 +16,7 @@
1616
// CHECK: }
1717
var x: Int = 0
1818

19-
// CHECK: sil private [ossa] @globalinit_[[T:.*]]_func1 : $@convention(c) () -> () {
19+
// CHECK: sil private [global_init_once_fn] [ossa] @globalinit_[[T:.*]]_func1 : $@convention(c) () -> () {
2020
// CHECK: alloc_global @$s12lazy_globals3FooV3fooSivpZ
2121
// CHECK: [[XADDR:%.*]] = global_addr @$s12lazy_globals3FooV3fooSivpZ : $*Int
2222
// CHECK: store {{.*}} to [trivial] [[XADDR]] : $*Int
@@ -40,7 +40,7 @@ struct Foo {
4040
static var initialized: Int = 57
4141
}
4242

43-
// CHECK: sil private [ossa] @globalinit_[[T:.*]]_func3 : $@convention(c) () -> () {
43+
// CHECK: sil private [global_init_once_fn] [ossa] @globalinit_[[T:.*]]_func3 : $@convention(c) () -> () {
4444
// CHECK: alloc_global @$s12lazy_globals3BarO3barSivpZ
4545
// CHECK: [[XADDR:%.*]] = global_addr @$s12lazy_globals3BarO3barSivpZ : $*Int
4646
// CHECK: store {{.*}} to [trivial] [[XADDR]] : $*Int
@@ -63,7 +63,7 @@ enum Bar {
6363

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

66-
// CHECK: sil private [ossa] @globalinit_[[T]]_func4 : $@convention(c) () -> () {
66+
// CHECK: sil private [global_init_once_fn] [ossa] @globalinit_[[T]]_func4 : $@convention(c) () -> () {
6767
// CHECK: function_ref @$s12lazy_globals1fSi_SityF : $@convention(thin) () -> (Int, Int)
6868
// CHECK: sil hidden [global_init] [ossa] @$s12lazy_globals2a1Sivau : $@convention(thin) () -> Builtin.RawPointer
6969
// CHECK: function_ref @globalinit_[[T]]_func4 : $@convention(c) () -> ()

test/SILGen/lazy_globals_multiple_vars.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %target-swift-emit-silgen -parse-as-library %s | %FileCheck %s
22

3-
// CHECK: sil private [ossa] [[INIT_A_B:@globalinit_.*]] :
3+
// CHECK: sil private [global_init_once_fn] [ossa] [[INIT_A_B:@globalinit_.*]] :
44
// CHECK: alloc_global @$s26lazy_globals_multiple_vars1aSiv
55
// CHECK: global_addr @$s26lazy_globals_multiple_vars1aSiv
66
// CHECK: alloc_global @$s26lazy_globals_multiple_vars1bSiv
@@ -13,15 +13,15 @@
1313
// CHECK: function_ref [[INIT_A_B]]
1414
var (a, b) = (1, 2)
1515

16-
// CHECK: sil private [ossa] [[INIT_C:@globalinit_.*]] :
16+
// CHECK: sil private [global_init_once_fn] [ossa] [[INIT_C:@globalinit_.*]] :
1717
// CHECK-NOT: global_addr @$s26lazy_globals_multiple_vars1dSiv
1818
// CHECK: alloc_global @$s26lazy_globals_multiple_vars1cSiv
1919
// CHECK: global_addr @$s26lazy_globals_multiple_vars1cSiv
2020
// CHECK-NOT: global_addr @$s26lazy_globals_multiple_vars1dSiv
2121
// CHECK: sil hidden [global_init] [ossa] @$s26lazy_globals_multiple_vars1cSivau
2222
// CHECK: global_addr [[TOKEN_C:@globalinit_.*]] :
2323
// CHECK: function_ref [[INIT_C]]
24-
// CHECK: sil private [ossa] [[INIT_D:@globalinit_.*]] :
24+
// CHECK: sil private [global_init_once_fn] [ossa] [[INIT_D:@globalinit_.*]] :
2525
// CHECK-NOT: global_addr @$s26lazy_globals_multiple_vars1cSiv
2626
// CHECK: alloc_global @$s26lazy_globals_multiple_vars1dSiv
2727
// CHECK: global_addr @$s26lazy_globals_multiple_vars1dSiv

test/SILGen/observers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public struct DidSetWillSetTests {
171171

172172
var global_observing_property : Int = zero {
173173
// The variable is initialized with "zero".
174-
// CHECK-LABEL: sil private [ossa] @globalinit_{{.*}}_func1 : $@convention(c) () -> () {
174+
// CHECK-LABEL: sil private [global_init_once_fn] [ossa] @globalinit_{{.*}}_func1 : $@convention(c) () -> () {
175175
// CHECK: bb0:
176176
// CHECK-NEXT: alloc_global @$s9observers25global_observing_propertySiv
177177
// CHECK-NEXT: %1 = global_addr @$s9observers25global_observing_propertySivp : $*Int

0 commit comments

Comments
 (0)