Skip to content

Commit ee0c228

Browse files
Merge pull request #58723 from aschwaighofer/transfer_prespecialize_targetFunction_earlier
GenericSpecialization: Move once initialization of pre-specializations earlier before the first query for pre-specialziations
2 parents eecbc4f + 104fd66 commit ee0c228

File tree

4 files changed

+63
-45
lines changed

4 files changed

+63
-45
lines changed

lib/SILOptimizer/Transforms/GenericSpecializer.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,44 @@
3333
using namespace swift;
3434

3535
namespace {
36+
static void transferSpecializeAttributeTargets(SILModule &M,
37+
SILOptFunctionBuilder &builder,
38+
Decl *d) {
39+
auto *vd = cast<AbstractFunctionDecl>(d);
40+
for (auto *A : vd->getAttrs().getAttributes<SpecializeAttr>()) {
41+
auto *SA = cast<SpecializeAttr>(A);
42+
// Filter _spi.
43+
auto spiGroups = SA->getSPIGroups();
44+
auto hasSPIGroup = !spiGroups.empty();
45+
if (hasSPIGroup) {
46+
if (vd->getModuleContext() != M.getSwiftModule() &&
47+
!M.getSwiftModule()->isImportedAsSPI(SA, vd)) {
48+
continue;
49+
}
50+
}
51+
if (auto *targetFunctionDecl = SA->getTargetFunctionDecl(vd)) {
52+
auto target = SILDeclRef(targetFunctionDecl);
53+
auto targetSILFunction = builder.getOrCreateFunction(
54+
SILLocation(vd), target, NotForDefinition,
55+
[&builder](SILLocation loc, SILDeclRef constant) -> SILFunction * {
56+
return builder.getOrCreateFunction(loc, constant, NotForDefinition);
57+
});
58+
auto kind = SA->getSpecializationKind() ==
59+
SpecializeAttr::SpecializationKind::Full
60+
? SILSpecializeAttr::SpecializationKind::Full
61+
: SILSpecializeAttr::SpecializationKind::Partial;
62+
Identifier spiGroupIdent;
63+
if (hasSPIGroup) {
64+
spiGroupIdent = spiGroups[0];
65+
}
66+
auto availability = AvailabilityInference::annotatedAvailableRangeForAttr(
67+
SA, M.getSwiftModule()->getASTContext());
68+
targetSILFunction->addSpecializeAttr(SILSpecializeAttr::create(
69+
M, SA->getSpecializedSignature(), SA->isExported(), kind, nullptr,
70+
spiGroupIdent, vd->getModuleContext(), availability));
71+
}
72+
}
73+
}
3674

3775
static bool specializeAppliesInFunction(SILFunction &F,
3876
SILTransform *transform,
@@ -60,6 +98,13 @@ static bool specializeAppliesInFunction(SILFunction &F,
6098
auto *Callee = Apply.getReferencedFunctionOrNull();
6199
if (!Callee)
62100
continue;
101+
102+
FunctionBuilder.getModule().performOnceForPrespecializedImportedExtensions(
103+
[&FunctionBuilder](AbstractFunctionDecl *pre) {
104+
transferSpecializeAttributeTargets(FunctionBuilder.getModule(), FunctionBuilder,
105+
pre);
106+
});
107+
63108
if (!Callee->isDefinition() && !Callee->hasPrespecialization()) {
64109
ORE.emit([&]() {
65110
using namespace OptRemark;

lib/SILOptimizer/Utils/Generics.cpp

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,45 +2552,6 @@ usePrespecialized(SILOptFunctionBuilder &funcBuilder, ApplySite apply,
25522552
return nullptr;
25532553
}
25542554

2555-
static void transferSpecializeAttributeTargets(SILModule &M,
2556-
SILOptFunctionBuilder &builder,
2557-
Decl *d) {
2558-
auto *vd = cast<AbstractFunctionDecl>(d);
2559-
for (auto *A : vd->getAttrs().getAttributes<SpecializeAttr>()) {
2560-
auto *SA = cast<SpecializeAttr>(A);
2561-
// Filter _spi.
2562-
auto spiGroups = SA->getSPIGroups();
2563-
auto hasSPIGroup = !spiGroups.empty();
2564-
if (hasSPIGroup) {
2565-
if (vd->getModuleContext() != M.getSwiftModule() &&
2566-
!M.getSwiftModule()->isImportedAsSPI(SA, vd)) {
2567-
continue;
2568-
}
2569-
}
2570-
if (auto *targetFunctionDecl = SA->getTargetFunctionDecl(vd)) {
2571-
auto target = SILDeclRef(targetFunctionDecl);
2572-
auto targetSILFunction = builder.getOrCreateFunction(
2573-
SILLocation(vd), target, NotForDefinition,
2574-
[&builder](SILLocation loc, SILDeclRef constant) -> SILFunction * {
2575-
return builder.getOrCreateFunction(loc, constant, NotForDefinition);
2576-
});
2577-
auto kind = SA->getSpecializationKind() ==
2578-
SpecializeAttr::SpecializationKind::Full
2579-
? SILSpecializeAttr::SpecializationKind::Full
2580-
: SILSpecializeAttr::SpecializationKind::Partial;
2581-
Identifier spiGroupIdent;
2582-
if (hasSPIGroup) {
2583-
spiGroupIdent = spiGroups[0];
2584-
}
2585-
auto availability = AvailabilityInference::annotatedAvailableRangeForAttr(
2586-
SA, M.getSwiftModule()->getASTContext());
2587-
targetSILFunction->addSpecializeAttr(SILSpecializeAttr::create(
2588-
M, SA->getSpecializedSignature(), SA->isExported(), kind, nullptr,
2589-
spiGroupIdent, vd->getModuleContext(), availability));
2590-
}
2591-
}
2592-
}
2593-
25942555
void swift::trySpecializeApplyOfGeneric(
25952556
SILOptFunctionBuilder &FuncBuilder,
25962557
ApplySite Apply, DeadInstructionSet &DeadApplies,
@@ -2646,12 +2607,6 @@ void swift::trySpecializeApplyOfGeneric(
26462607
SILFunction *prespecializedF = nullptr;
26472608
ReabstractionInfo prespecializedReInfo;
26482609

2649-
FuncBuilder.getModule().performOnceForPrespecializedImportedExtensions(
2650-
[&FuncBuilder](AbstractFunctionDecl *pre) {
2651-
transferSpecializeAttributeTargets(FuncBuilder.getModule(), FuncBuilder,
2652-
pre);
2653-
});
2654-
26552610
if ((prespecializedF = usePrespecialized(FuncBuilder, Apply, RefF, ReInfo,
26562611
prespecializedReInfo))) {
26572612
ReInfo = prespecializedReInfo;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public func someFunc<T>(_ t: T) {
2+
print(t)
3+
}
4+
5+
@_specialize(exported: true, target: someFunc(_:), where T == Int)
6+
@usableFromInline
7+
func __specialize_someFunc<T>(_: T) {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -module-name A -emit-module-path %t/A.swiftmodule %S/Inputs/prespecialize_import_module.swift
3+
// RUN: %target-swift-frontend -O -emit-sil -module-name B -I %t %s | %FileCheck %s
4+
import A
5+
6+
// CHECK-LABEL: sil{{.*}} @$s1B4testyyF
7+
public func test() {
8+
// CHECK: s1A8someFuncyyxlFSi_Ts5
9+
someFunc(5)
10+
}
11+
// CHECK: end sil function '$s1B4testyyF'

0 commit comments

Comments
 (0)