Skip to content

Commit d2c7a51

Browse files
committed
SILGen: Stub initializer functions for unavailable global vars.
Part of rdar://107388493
1 parent c4e73b6 commit d2c7a51

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/SILGen/SILGenGlobalVariable.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ void SILGenFunction::emitLazyGlobalInitializer(PatternBindingDecl *binding,
240240

241241
// Add unused context pointer argument required to pass to `Builtin.once`
242242
SILBasicBlock &entry = *F.begin();
243+
244+
if (shouldLowerToUnavailableCodeStub(binding))
245+
emitApplyOfUnavailableCodeReached();
246+
243247
SILType rawPointerSILTy =
244248
getLoweredLoadableType(getASTContext().TheRawPointerType);
245249
entry.createFunctionArgument(rawPointerSILTy);
@@ -279,6 +283,9 @@ static void emitOnceCall(SILGenFunction &SGF, VarDecl *global,
279283
void SILGenFunction::emitGlobalAccessor(VarDecl *global,
280284
SILGlobalVariable *onceToken,
281285
SILFunction *onceFunc) {
286+
if (shouldLowerToUnavailableCodeStub(global))
287+
emitApplyOfUnavailableCodeReached();
288+
282289
emitOnceCall(*this, global, onceToken, onceFunc);
283290

284291
// Return the address of the global variable.

test/SILGen/unavailable_decl_optimization_stub.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,19 @@ enum SomeError: Error { case generic }
2323
public func unavailableThrowingFunc() throws {
2424
throw SomeError.generic
2525
}
26+
27+
// one-time initialization function for globalVar
28+
// CHECK-LABEL: sil {{.*}} @$s4Test9globalVar_WZ
29+
// CHECK: [[FNREF:%.*]] = function_ref @$ss31_diagnoseUnavailableCodeReacheds5NeverOyF
30+
// CHECK-NEXT: [[APPLY:%.*]] = apply [[FNREF]]()
31+
// CHECK: alloc_global
32+
// CHECK: } // end sil function '$s4Test9globalVar_WZ'
33+
//
34+
// globalVar.unsafeMutableAddressor
35+
// CHECK-LABEL: sil {{.*}} @$s4Test9globalVarAA1SVvau
36+
// CHECK: [[FNREF:%.*]] = function_ref @$ss31_diagnoseUnavailableCodeReacheds5NeverOyF
37+
// CHECK-NEXT: [[APPLY:%.*]] = apply [[FNREF]]()
38+
// CHECK: global_addr @$s4Test9globalVar_Wz
39+
// CHECK: } // end sil function '$s4Test9globalVarAA1SVvau'
40+
@available(*, unavailable)
41+
public var globalVar = S()

0 commit comments

Comments
 (0)