Skip to content

Commit 6f60b67

Browse files
committed
Add EXPERIMENTAL_FEATURE(SymbolLinkageMarkers, true)
1 parent 80222b8 commit 6f60b67

File tree

7 files changed

+46
-14
lines changed

7 files changed

+46
-14
lines changed

docs/ReferenceGuides/UnderscoredAttributes.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,12 @@ Fully bypasses access control, allowing access to private declarations
832832
in the imported module. The imported module needs to be compiled with
833833
`-Xfrontend -enable-private-imports` for this to work.
834834

835+
## `@_section("section_name")`
836+
837+
Places a global variable or a top-level function into a section of the object
838+
file with the given name. It's the equivalent of clang's
839+
`__attribute__((section))`.
840+
835841
## `@_semantics("uniquely.recognized.id")`
836842

837843
Allows the optimizer to make use of some key invariants in performance critical
@@ -994,6 +1000,12 @@ for more details.
9941000

9951001
This `async` function uses the pre-SE-0338 semantics of unsafely inheriting the caller's executor. This is an underscored feature because the right way of inheriting an executor is to pass in the required executor and switch to it. Unfortunately, there are functions in the standard library which need to inherit their caller's executor but cannot change their ABI because they were not defined as `@_alwaysEmitIntoClient` in the initial release.
9961002

1003+
## `@_used`
1004+
1005+
Marks a global variable or a top-level function as "used externally" even if it
1006+
does not have visible users in the compilation unit. It's the equivalent of
1007+
clang's `__attribute__((used))`.
1008+
9971009
## `@_weakLinked`
9981010

9991011
Allows a declaration to be weakly-referenced, i.e., any references emitted by

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,9 @@ ERROR(cdecl_empty_name,none,
17021702
ERROR(cdecl_throws,none,
17031703
"raising errors from @_cdecl functions is not supported", ())
17041704

1705+
// @_used and @_section
1706+
ERROR(section_linkage_markers_disabled,none,
1707+
"attribute requires '-enable-experimental-feature SymbolLinkageMarkers'", ())
17051708
ERROR(used_not_at_top_level,none,
17061709
"@_used can only be applied to global functions and variables", ())
17071710
ERROR(section_not_at_top_level,none,

include/swift/Basic/Features.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ EXPERIMENTAL_FEATURE(FlowSensitiveConcurrencyCaptures, false)
122122
EXPERIMENTAL_FEATURE(CodeItemMacros, true)
123123
EXPERIMENTAL_FEATURE(TupleConformances, false)
124124

125+
// Whether to enable @_used and @_section attributes
126+
EXPERIMENTAL_FEATURE(SymbolLinkageMarkers, true)
127+
125128
// FIXME: MoveOnlyClasses is not intended to be in production,
126129
// but our tests currently rely on it, and we want to run those
127130
// tests in non-asserts builds too.

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3211,6 +3211,10 @@ static bool usesFeatureTupleConformances(Decl *decl) {
32113211
return false;
32123212
}
32133213

3214+
static bool usesFeatureSymbolLinkageMarkers(Decl *decl) {
3215+
return false;
3216+
}
3217+
32143218
static bool usesFeatureLayoutPrespecialization(Decl *decl) {
32153219
auto &attrs = decl->getAttrs();
32163220
return std::any_of(attrs.begin(), attrs.end(), [](auto *attr) {

lib/Sema/TypeCheckAttr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,12 +2071,22 @@ void AttributeChecker::visitExposeAttr(ExposeAttr *attr) {
20712071
}
20722072

20732073
void AttributeChecker::visitUsedAttr(UsedAttr *attr) {
2074+
if (!Ctx.LangOpts.hasFeature(Feature::SymbolLinkageMarkers)) {
2075+
diagnoseAndRemoveAttr(attr, diag::section_linkage_markers_disabled);
2076+
return;
2077+
}
2078+
20742079
// Only top-level func/var decls are currently supported.
20752080
if (D->getDeclContext()->isTypeContext())
20762081
diagnose(attr->getLocation(), diag::used_not_at_top_level);
20772082
}
20782083

20792084
void AttributeChecker::visitSectionAttr(SectionAttr *attr) {
2085+
if (!Ctx.LangOpts.hasFeature(Feature::SymbolLinkageMarkers)) {
2086+
diagnoseAndRemoveAttr(attr, diag::section_linkage_markers_disabled);
2087+
return;
2088+
}
2089+
20802090
// Only top-level func/var decls are currently supported.
20812091
if (D->getDeclContext()->isTypeContext())
20822092
diagnose(attr->getLocation(), diag::section_not_at_top_level);

test/IRGen/section.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// RUN: %target-swift-frontend -primary-file %s -emit-sil | %FileCheck %s --check-prefix=SIL --check-prefix=SIL-TOPLEVEL
2-
// RUN: %target-swift-frontend -primary-file %s -emit-ir | %FileCheck %s --check-prefix=IR --check-prefix=IR-TOPLEVEL
3-
// RUN: %target-swift-frontend -primary-file %s -emit-sil -parse-as-library | %FileCheck %s --check-prefix=SIL --check-prefix=SIL-LIBRARY
4-
// RUN: %target-swift-frontend -primary-file %s -emit-ir -parse-as-library | %FileCheck %s --check-prefix=IR --check-prefix=IR-LIBRARY
1+
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -primary-file %s -emit-sil | %FileCheck %s --check-prefix=SIL --check-prefix=SIL-TOPLEVEL
2+
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -primary-file %s -emit-ir | %FileCheck %s --check-prefix=IR --check-prefix=IR-TOPLEVEL
3+
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -primary-file %s -emit-sil -parse-as-library | %FileCheck %s --check-prefix=SIL --check-prefix=SIL-LIBRARY
4+
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -primary-file %s -emit-ir -parse-as-library | %FileCheck %s --check-prefix=IR --check-prefix=IR-LIBRARY
55

6-
// RUN: %target-swift-frontend -primary-file %s -S | %FileCheck %s --check-prefix=ASM --check-prefix ASM-%target-os
7-
// RUN: %target-swift-frontend -primary-file %s -S -parse-as-library | %FileCheck %s --check-prefix=ASM --check-prefix ASM-%target-os
6+
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -primary-file %s -S | %FileCheck %s --check-prefix=ASM --check-prefix ASM-%target-os
7+
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -primary-file %s -S -parse-as-library | %FileCheck %s --check-prefix=ASM --check-prefix ASM-%target-os
88

99
// REQUIRES: swift_in_compiler
1010

test/IRGen/used.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// RUN: %target-swift-frontend -primary-file %s -emit-sil | %FileCheck %s --check-prefix=SIL
2-
// RUN: %target-swift-frontend -primary-file %s -O -emit-sil | %FileCheck %s --check-prefix=SIL
3-
// RUN: %target-swift-frontend -primary-file %s -emit-ir | %FileCheck %s --check-prefix=IR
4-
// RUN: %target-swift-frontend -primary-file %s -O -emit-ir | %FileCheck %s --check-prefix=IR
5-
// RUN: %target-swift-frontend -primary-file %s -emit-sil -parse-as-library | %FileCheck %s --check-prefix=SIL
6-
// RUN: %target-swift-frontend -primary-file %s -O -emit-sil -parse-as-library | %FileCheck %s --check-prefix=SIL
7-
// RUN: %target-swift-frontend -primary-file %s -emit-ir -parse-as-library | %FileCheck %s --check-prefix=IR
8-
// RUN: %target-swift-frontend -primary-file %s -O -emit-ir -parse-as-library | %FileCheck %s --check-prefix=IR
1+
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -primary-file %s -emit-sil | %FileCheck %s --check-prefix=SIL
2+
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -primary-file %s -O -emit-sil | %FileCheck %s --check-prefix=SIL
3+
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -primary-file %s -emit-ir | %FileCheck %s --check-prefix=IR
4+
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -primary-file %s -O -emit-ir | %FileCheck %s --check-prefix=IR
5+
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -primary-file %s -emit-sil -parse-as-library | %FileCheck %s --check-prefix=SIL
6+
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -primary-file %s -O -emit-sil -parse-as-library | %FileCheck %s --check-prefix=SIL
7+
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -primary-file %s -emit-ir -parse-as-library | %FileCheck %s --check-prefix=IR
8+
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -primary-file %s -O -emit-ir -parse-as-library | %FileCheck %s --check-prefix=IR
99

1010
// REQUIRES: swift_in_compiler
1111

0 commit comments

Comments
 (0)