Skip to content

Commit 52cff19

Browse files
authored
Merge pull request #77400 from eeckstein/fix-specialize-attr
SIL: avoid creating SIL functions for not used imported specialization attributes
2 parents 3b53937 + 15c6abf commit 52cff19

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

include/swift/SIL/SILModule.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,10 @@ class SILModule {
399399

400400
BasicBlockNameMapType basicBlockNames;
401401

402+
// Specialization attributes which need to be added to a function once it is created.
403+
// The key of this map is the function name.
404+
llvm::StringMap<std::vector<SILSpecializeAttr *>> pendingSpecializeAttrs;
405+
402406
SILModule(llvm::PointerUnion<FileUnit *, ModuleDecl *> context,
403407
Lowering::TypeConverter &TC, const SILOptions &Options,
404408
const IRGenOptions *irgenOptions = nullptr);
@@ -1081,6 +1085,10 @@ class SILModule {
10811085
/// Gather prespecialized from extensions.
10821086
void performOnceForPrespecializedImportedExtensions(
10831087
llvm::function_ref<void(AbstractFunctionDecl *)> action);
1088+
1089+
void addPendingSpecializeAttr(StringRef functionName, SILSpecializeAttr *attr) {
1090+
pendingSpecializeAttrs[functionName].push_back(attr);
1091+
}
10841092
};
10851093

10861094
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SILModule &M){

lib/SIL/IR/SILFunction.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ SILFunction *SILFunction::create(
194194
else
195195
M.functions.push_back(fn);
196196

197+
auto iter = M.pendingSpecializeAttrs.find(name);
198+
if (iter != M.pendingSpecializeAttrs.end()) {
199+
for (auto *attr : iter->second) {
200+
fn->addSpecializeAttr(attr);
201+
}
202+
M.pendingSpecializeAttrs.erase(iter);
203+
}
204+
197205
return fn;
198206
}
199207

lib/SILOptimizer/Transforms/GenericSpecializer.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ static void transferSpecializeAttributeTargets(SILModule &M,
5151
}
5252
}
5353
if (auto *targetFunctionDecl = SA->getTargetFunctionDecl(vd)) {
54-
auto target = SILDeclRef(targetFunctionDecl);
55-
auto targetSILFunction = builder.getOrCreateFunction(
56-
SILLocation(vd), target, NotForDefinition,
57-
[&builder](SILLocation loc, SILDeclRef constant) -> SILFunction * {
58-
return builder.getOrCreateFunction(loc, constant, NotForDefinition);
59-
});
6054
auto kind = SA->getSpecializationKind() ==
6155
SpecializeAttr::SpecializationKind::Full
6256
? SILSpecializeAttr::SpecializationKind::Full
@@ -68,10 +62,18 @@ static void transferSpecializeAttributeTargets(SILModule &M,
6862
auto availability = AvailabilityInference::annotatedAvailableRangeForAttr(
6963
SA, M.getSwiftModule()->getASTContext());
7064

71-
targetSILFunction->addSpecializeAttr(SILSpecializeAttr::create(
65+
auto *attr = SILSpecializeAttr::create(
7266
M, SA->getSpecializedSignature(vd), SA->getTypeErasedParams(),
7367
SA->isExported(), kind, nullptr,
74-
spiGroupIdent, vd->getModuleContext(), availability));
68+
spiGroupIdent, vd->getModuleContext(), availability);
69+
70+
auto target = SILDeclRef(targetFunctionDecl);
71+
std::string targetName = target.mangle();
72+
if (SILFunction *targetSILFunction = M.lookUpFunction(targetName)) {
73+
targetSILFunction->addSpecializeAttr(attr);
74+
} else {
75+
M.addPendingSpecializeAttr(targetName, attr);
76+
}
7577
}
7678
}
7779
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %target-swift-frontend -primary-file %s -O -emit-sil | %FileCheck %s
2+
3+
// Check that no Array code is de-serialized due to specialization attributes
4+
5+
// CHECK-NOT: Array
6+
7+
@inline(never)
8+
func foo<T>(_ x: T) {
9+
}
10+
11+
public func test() {
12+
foo(27)
13+
}

0 commit comments

Comments
 (0)