Skip to content

Commit 45fa670

Browse files
authored
Merge pull request #85065 from DougGregor/c-sil-mangled-name
[SIL] Use mangled names + asmname for `@c` functions
2 parents 83481d5 + fad8b24 commit 45fa670

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed

include/swift/SIL/SILDeclRef.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ struct SILDeclRef {
333333
/// Produce a mangled form of this constant.
334334
std::string mangle(ManglingKind MKind = ManglingKind::Default) const;
335335

336+
/// If the symbol has a specific name for use at the LLVM IR level,
337+
/// produce that name. This may be different than the mangled name in SIL.
338+
std::optional<StringRef> getAsmName() const;
339+
336340
/// True if the SILDeclRef references a function.
337341
bool isFunc() const {
338342
return kind == Kind::Func;

lib/IRGen/TBDGen.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,15 @@ void TBDGenVisitor::didVisitDecl(Decl *D) {
452452
}
453453

454454
void TBDGenVisitor::addFunction(SILDeclRef declRef) {
455+
// If there is a specific symbol name this should have at the IR level,
456+
// use it instead.
457+
if (auto asmName = declRef.getAsmName()) {
458+
addSymbol(*asmName, SymbolSource::forSILDeclRef(declRef),
459+
SymbolFlags::Text);
460+
return;
461+
}
462+
463+
455464
addSymbol(declRef.mangle(), SymbolSource::forSILDeclRef(declRef),
456465
SymbolFlags::Text);
457466
}

lib/SIL/IR/SILDeclRef.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,8 +1443,11 @@ std::string SILDeclRef::mangle(ManglingKind MKind) const {
14431443
return NameA->Name.str();
14441444
}
14451445

1446-
// Use a given cdecl name for native-to-foreign thunks.
1447-
if (getDecl()->getAttrs().hasAttribute<CDeclAttr>())
1446+
// Use a given cdecl name for native-to-foreign thunks. Don't do this
1447+
// for functions that only have a C entrypoint.
1448+
if (getDecl()->getAttrs().hasAttribute<CDeclAttr>() &&
1449+
!(getDecl()->hasOnlyCEntryPoint() &&
1450+
!getDecl()->getImplementedObjCDecl())) {
14481451
if (isNativeToForeignThunk() || isForeign) {
14491452
// If this is an @implementation @_cdecl, mangle it like the clang
14501453
// function it implements.
@@ -1455,6 +1458,7 @@ std::string SILDeclRef::mangle(ManglingKind MKind) const {
14551458
}
14561459
return getDecl()->getCDeclName().str();
14571460
}
1461+
}
14581462

14591463
if (SKind == ASTMangler::SymbolKind::DistributedThunk) {
14601464
return mangler.mangleDistributedThunk(cast<FuncDecl>(getDecl()));
@@ -1543,6 +1547,19 @@ std::string SILDeclRef::mangle(ManglingKind MKind) const {
15431547
llvm_unreachable("bad entity kind!");
15441548
}
15451549

1550+
std::optional<StringRef> SILDeclRef::getAsmName() const {
1551+
if (isForeign && isFunc()) {
1552+
auto func = getFuncDecl();
1553+
if (auto *EA = ExternAttr::find(func->getAttrs(), ExternKind::C))
1554+
return EA->getCName(func);
1555+
if (func->hasOnlyCEntryPoint() && !func->getImplementedObjCDecl())
1556+
return func->getCDeclName();
1557+
}
1558+
1559+
return std::nullopt;
1560+
}
1561+
1562+
15461563
// Returns true if the given JVP/VJP SILDeclRef requires a new vtable entry.
15471564
// FIXME(https://github.com/apple/swift/issues/54833): Also consider derived declaration `@derivative` attributes.
15481565
static bool derivativeFunctionRequiresNewVTableEntry(SILDeclRef declRef) {

lib/SIL/IR/SILFunctionBuilder.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,8 @@ void SILFunctionBuilder::addFunctionAttributes(
127127
}
128128
}
129129

130-
if (constant.isFunc() && constant.hasFuncDecl()) {
131-
auto func = constant.getFuncDecl();
132-
if (auto *EA = ExternAttr::find(Attrs, ExternKind::C))
133-
F->setAsmName(EA->getCName(func));
130+
if (auto asmName = constant.getAsmName()) {
131+
F->setAsmName(*asmName);
134132
}
135133
}
136134

test/SILGen/cdecl-official.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66

77
// REQUIRES: swift_feature_CDecl
88

9-
// CHECK-LABEL: sil hidden [ossa] @pear : $@convention(c) (@convention(c) (Int) -> Int) -> () {
9+
// CHECK-LABEL: sil hidden [asmname "pear"] [ossa] @$s5cdecl5appleyyS2iXCFTo : $@convention(c) (@convention(c) (Int) -> Int) -> () {
1010
@c(pear)
1111
func apple(_ f: @convention(c) (Int) -> Int) { }
1212

1313
func acceptSwiftFunc(_ f: (Int) -> Int) { }
1414

1515
// CHECK-LABEL: sil hidden [ossa] @$s5cdecl16forceCEntryPoint{{[_0-9a-zA-Z]*}}F
16-
// CHECK: [[GRAPEFRUIT:%[0-9]+]] = function_ref @grapefruit : $@convention(c) (Int) -> Int
17-
// CHECK: [[PEAR:%[0-9]+]] = function_ref @pear : $@convention(c) (@convention(c) (Int) -> Int) -> ()
16+
// CHECK: [[GRAPEFRUIT:%[0-9]+]] = function_ref @$s5cdecl6orangeyS2iFTo : $@convention(c) (Int) -> Int
17+
// CHECK: [[PEAR:%[0-9]+]] = function_ref @$s5cdecl5appleyyS2iXCFTo : $@convention(c) (@convention(c) (Int) -> Int) -> ()
1818
// CHECK: apply [[PEAR]]([[GRAPEFRUIT]])
1919
func forceCEntryPoint() {
2020
apple(orange)
2121
}
2222

23-
// CHECK-LABEL: sil hidden [ossa] @grapefruit : $@convention(c) (Int) -> Int {
23+
// CHECK-LABEL: sil hidden [asmname "grapefruit"] [ossa] @$s5cdecl6orangeyS2iFTo : $@convention(c) (Int) -> Int {
2424
@c(grapefruit)
2525
func orange(_ x: Int) -> Int {
2626
return x
@@ -36,23 +36,23 @@ func requiresThunk() {
3636
acceptSwiftFunc(orange)
3737
}
3838

39-
// CHECK-LABEL: sil [serialized] [ossa] @cauliflower : $@convention(c) (Int) -> Int {
39+
// CHECK-LABEL: sil [serialized] [asmname "cauliflower"] [ossa] @$s5cdecl8broccoliyS2iFTo : $@convention(c) (Int) -> Int {
4040
// CHECK-NOT: apply
4141
// CHECK: return
4242
@c(cauliflower)
4343
public func broccoli(_ x: Int) -> Int {
4444
return x
4545
}
4646

47-
// CHECK-LABEL: sil private [ossa] @collard_greens : $@convention(c) (Int) -> Int {
47+
// CHECK-LABEL: sil private [asmname "collard_greens"] [ossa] @$s5cdecl4kale33_{{.*}}FTo : $@convention(c) (Int) -> Int {
4848
// CHECK-NOT: apply
4949
// CHECK: return
5050
@c(collard_greens)
5151
private func kale(_ x: Int) -> Int {
5252
return x
5353
}
5454

55-
// CHECK-LABEL: sil private [ossa] @defaultName : $@convention(c) (Int) -> Int {
55+
// CHECK-LABEL: sil private [asmname "defaultName"] [ossa] @$s5cdecl11defaultName33{{.*}}iFTo : $@convention(c) (Int) -> Int {
5656
// CHECK-NOT: apply
5757
// CHECK: return
5858
@c

0 commit comments

Comments
 (0)