Skip to content

Commit 081b5cd

Browse files
committed
[SIL] Serialize section name correctly and model it on global variables
Fixes rdar://162549960.
1 parent 3e1ea3c commit 081b5cd

File tree

15 files changed

+58
-19
lines changed

15 files changed

+58
-19
lines changed

include/swift/SIL/SILGlobalVariable.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class SILGlobalVariable
6464
/// the mangled name of the variable will be used instead.
6565
StringRef AsmName;
6666

67+
/// Name of a section if @_section attribute was used, otherwise empty.
68+
StringRef Section;
69+
6770
/// The lowered type of the variable.
6871
SILType LoweredType;
6972

@@ -159,6 +162,10 @@ class SILGlobalVariable
159162
StringRef asmName() const { return AsmName; }
160163
void setAsmName(StringRef value) { AsmName = value; }
161164

165+
/// Return custom section name if @_section was used, otherwise empty
166+
StringRef section() const { return Section; }
167+
void setSection(StringRef value) { Section = value; }
168+
162169
void setDeclaration(bool isD) { IsDeclaration = isD; }
163170

164171
/// True if this is a definition of the variable.
@@ -240,15 +247,6 @@ class SILGlobalVariable
240247

241248
void setMarkedAsUsed(bool used) { IsUsed = used; }
242249

243-
/// Returns a SectionAttr if this global variable has `@_section` attribute.
244-
SectionAttr *getSectionAttr() const {
245-
auto *V = getDecl();
246-
if (!V)
247-
return nullptr;
248-
249-
return V->getAttrs().getAttribute<SectionAttr>();
250-
}
251-
252250
/// Return whether this variable corresponds to a Clang node.
253251
bool hasClangNode() const;
254252

lib/IRGen/GenDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2865,8 +2865,8 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
28652865
addUsedGlobal(gvar);
28662866
else if (var->shouldBePreservedForDebugger() && forDefinition)
28672867
addUsedGlobal(gvar);
2868-
if (auto *sectionAttr = var->getSectionAttr())
2869-
gvar->setSection(sectionAttr->Name);
2868+
if (!var->section().empty())
2869+
gvar->setSection(var->section());
28702870
}
28712871
if (forDefinition && !gvar->hasInitializer()) {
28722872
if (initVal) {

lib/SIL/IR/SILGlobalVariable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ bool SILGlobalVariable::isPossiblyUsedExternally() const {
9191
if (markedAsUsed())
9292
return true;
9393

94-
if (getSectionAttr())
94+
if (!section().empty())
9595
return true;
9696

9797
SILLinkage linkage = getLinkage();
@@ -151,7 +151,7 @@ SILInstruction *SILGlobalVariable::getStaticInitializerValue() {
151151
}
152152

153153
bool SILGlobalVariable::mustBeInitializedStatically() const {
154-
if (getSectionAttr())
154+
if (!section().empty())
155155
return true;
156156

157157
auto *decl = getDecl();

lib/SIL/IR/SILPrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3783,6 +3783,9 @@ void SILGlobalVariable::print(llvm::raw_ostream &OS, bool Verbose) const {
37833783
if (!asmName().empty())
37843784
OS << "[asmname \"" << asmName() << "\"] ";
37853785

3786+
if (!section().empty())
3787+
OS << "[section \"" << section() << "\"] ";
3788+
37863789
printName(OS);
37873790
OS << " : " << LoweredType;
37883791

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7577,6 +7577,7 @@ bool SILParserState::parseSILGlobal(Parser &P) {
75777577
SerializedKind_t isSerialized = IsNotSerialized;
75787578
bool isMarkedAsUsed = false;
75797579
StringRef asmName;
7580+
StringRef section;
75807581
bool isLet = false;
75817582

75827583
SILParser State(P);
@@ -7585,7 +7586,7 @@ bool SILParserState::parseSILGlobal(Parser &P) {
75857586
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
75867587
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
75877588
nullptr, nullptr, nullptr, &isMarkedAsUsed, &asmName,
7588-
nullptr, &isLet, nullptr, nullptr, nullptr, nullptr,
7589+
&section, &isLet, nullptr, nullptr, nullptr, nullptr,
75897590
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
75907591
State, M) ||
75917592
P.parseToken(tok::at_sign, diag::expected_sil_value_name) ||
@@ -7615,6 +7616,7 @@ bool SILParserState::parseSILGlobal(Parser &P) {
76157616
GV->setLet(isLet);
76167617
GV->setMarkedAsUsed(isMarkedAsUsed);
76177618
GV->setAsmName(asmName);
7619+
GV->setSection(section);
76187620

76197621
// Parse static initializer if exists.
76207622
if (State.P.consumeIf(tok::equal) && State.P.consumeIf(tok::l_brace)) {

lib/SILGen/SILGenGlobalVariable.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ SILGlobalVariable *SILGenModule::getSILGlobalVariable(VarDecl *gDecl,
7474
M, silLinkage, IsNotSerialized, mangledName, silTy, std::nullopt, gDecl);
7575
silGlobal->setDeclaration(!forDef);
7676

77+
if (auto sectionAttr = gDecl->getAttrs().getAttribute<SectionAttr>())
78+
silGlobal->setSection(sectionAttr->Name);
79+
7780
return silGlobal;
7881
}
7982

lib/SILOptimizer/Mandatory/PerformanceDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ class PerformanceDiagnosticsPass : public SILModuleTransform {
730730
"global inst", &g);
731731

732732
auto *decl = g.getDecl();
733-
if (g.getSectionAttr()) {
733+
if (!g.section().empty()) {
734734
module->getASTContext().Diags.diagnose(
735735
g.getDecl()->getLoc(), diag::bad_attr_on_non_const_global,
736736
"@_section");

lib/Serialization/DeserializeSIL.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,9 @@ llvm::Expected<SILFunction *> SILDeserializer::readSILFunctionChecked(
10201020
case ExtraStringFlavor::AsmName:
10211021
fn->setAsmName(blobData);
10221022
break;
1023+
case ExtraStringFlavor::Section:
1024+
fn->setSection(blobData);
1025+
break;
10231026
}
10241027
continue;
10251028
}
@@ -4166,6 +4169,9 @@ SILGlobalVariable *SILDeserializer::readGlobalVar(StringRef Name) {
41664169
case ExtraStringFlavor::AsmName:
41674170
v->setAsmName(blobData);
41684171
break;
4172+
case ExtraStringFlavor::Section:
4173+
v->setSection(blobData);
4174+
break;
41694175
}
41704176
continue;
41714177
}
@@ -4180,6 +4186,7 @@ SILGlobalVariable *SILDeserializer::readGlobalVar(StringRef Name) {
41804186
if (entry.Kind == llvm::BitstreamEntry::EndBlock)
41814187
return v;
41824188

4189+
scratch.clear();
41834190
maybeKind = SILCursor.readRecord(entry.ID, scratch, &blobData);
41844191
if (!maybeKind)
41854192
MF->fatal(maybeKind.takeError());

lib/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5858
/// describe what change you made. The content of this comment isn't important;
5959
/// it just ensures a conflict if two people change the module format.
6060
/// Don't worry about adhering to the 80-column limit for this line.
61-
const uint16_t SWIFTMODULE_VERSION_MINOR = 966; // SIL asmname
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 966; // SIL asmname/section
6262

6363
/// A standard hash seed used for all string hashes in a serialized module.
6464
///

lib/Serialization/SILFormat.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ enum class KeyPathComputedComponentIdKindEncoding : uint8_t {
8484
enum class ExtraStringFlavor : uint8_t {
8585
/// asmname attributes
8686
AsmName,
87+
/// section attribute
88+
Section,
8789
};
8890

8991
/// The record types within the "sil-index" block.

0 commit comments

Comments
 (0)