Skip to content

Commit cccc281

Browse files
committed
Disallow non-constant globals with @_section attribute
1 parent 6f60b67 commit cccc281

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ ERROR(performance_unknown_callees,none,
332332
"called function is not known at compile time and can have unpredictable performance", ())
333333
ERROR(performance_callee_unavailable,none,
334334
"called function is not available in this module and can have unpredictable performance", ())
335+
ERROR(section_attr_on_non_const_global,none,
336+
"global variable must be a compile-time constant to use @_section attribute", ())
335337
NOTE(performance_called_from,none,
336338
"called from here", ())
337339

lib/SILOptimizer/Mandatory/PerformanceDiagnostics.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,14 @@ class PerformanceDiagnosticsPass : public SILModuleTransform {
476476
SILModule *module = getModule();
477477

478478
PerformanceDiagnostics diagnoser(*module, getAnalysis<BasicCalleeAnalysis>());
479+
480+
// Check that @_section is only on constant globals
481+
for (SILGlobalVariable &g : module->getSILGlobals()) {
482+
if (!g.getStaticInitializerValue() && g.getSectionAttr())
483+
module->getASTContext().Diags.diagnose(
484+
g.getDecl()->getLoc(), diag::section_attr_on_non_const_global);
485+
}
486+
479487
bool annotatedFunctionsFound = false;
480488

481489
for (SILFunction &function : *module) {

test/IRGen/section_non_const.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -parse-as-library -emit-sil %s -o /dev/null -verify
2+
3+
// REQUIRES: swift_in_compiler
4+
5+
@_section("__TEXT,__mysection") var g0: Int = 1
6+
@_section("__TEXT,__mysection") var g1: (Int, Int) = (1, 2)
7+
@_section("__TEXT,__mysection") var g2: [Int] = [1, 2, 3] // expected-error {{global variable must be a compile-time constant to use @_section attribute}}
8+
@_section("__TEXT,__mysection") var g3: [Int:Int] = [:] // expected-error {{global variable must be a compile-time constant to use @_section attribute}}
9+
@_section("__TEXT,__mysection") var g4: UInt = 42
10+
@_section("__TEXT,__mysection") var g5: String = "hello" // expected-error {{global variable must be a compile-time constant to use @_section attribute}}
11+
@_section("__TEXT,__mysection") var g6: Any = 1 // expected-error {{global variable must be a compile-time constant to use @_section attribute}}
12+
@_section("__TEXT,__mysection") var g7: UInt8 = 42
13+
@_section("__TEXT,__mysection") var g8: Int = 5 * 5

0 commit comments

Comments
 (0)