-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathGenericSpecializer.cpp
202 lines (173 loc) · 7.41 KB
/
GenericSpecializer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//===--- GenericSpecializer.cpp - Specialization of generic functions -----===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Specialize calls to generic functions by substituting static type
// information.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "sil-generic-specializer"
#include "swift/AST/AvailabilityInference.h"
#include "swift/Basic/Assertions.h"
#include "swift/SIL/OptimizationRemark.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/BasicBlockOptUtils.h"
#include "swift/SILOptimizer/Utils/ConstantFolding.h"
#include "swift/SILOptimizer/Utils/Devirtualize.h"
#include "swift/SILOptimizer/Utils/Generics.h"
#include "swift/SILOptimizer/Utils/InstructionDeleter.h"
#include "swift/SILOptimizer/Utils/SILInliner.h"
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
#include "swift/SILOptimizer/Utils/StackNesting.h"
#include "llvm/ADT/SmallVector.h"
using namespace swift;
namespace {
static void transferSpecializeAttributeTargets(SILModule &M,
SILOptFunctionBuilder &builder,
Decl *d) {
auto *vd = cast<AbstractFunctionDecl>(d);
for (auto *A : vd->getAttrs().getAttributes<SpecializeAttr>()) {
auto *SA = cast<SpecializeAttr>(A);
// Filter _spi.
auto spiGroups = SA->getSPIGroups();
auto hasSPIGroup = !spiGroups.empty();
if (hasSPIGroup) {
if (vd->getModuleContext() != M.getSwiftModule() &&
!M.getSwiftModule()->isImportedAsSPI(SA, vd)) {
continue;
}
}
if (auto *targetFunctionDecl = SA->getTargetFunctionDecl(vd)) {
auto kind = SA->getSpecializationKind() ==
SpecializeAttr::SpecializationKind::Full
? SILSpecializeAttr::SpecializationKind::Full
: SILSpecializeAttr::SpecializationKind::Partial;
Identifier spiGroupIdent;
if (hasSPIGroup) {
spiGroupIdent = spiGroups[0];
}
auto availability = AvailabilityInference::annotatedAvailableRangeForAttr(
vd, SA, M.getSwiftModule()->getASTContext());
auto *attr = SILSpecializeAttr::create(
M, SA->getSpecializedSignature(vd), SA->getTypeErasedParams(),
SA->isExported(), kind, nullptr,
spiGroupIdent, vd->getModuleContext(), availability);
auto target = SILDeclRef(targetFunctionDecl);
std::string targetName = target.mangle();
if (SILFunction *targetSILFunction = M.lookUpFunction(targetName)) {
targetSILFunction->addSpecializeAttr(attr);
} else {
M.addPendingSpecializeAttr(targetName, attr);
}
}
}
}
} // end anonymous namespace
bool swift::specializeAppliesInFunction(SILFunction &F,
SILTransform *transform,
bool isMandatory) {
SILOptFunctionBuilder FunctionBuilder(*transform);
DeadInstructionSet DeadApplies;
llvm::SmallSetVector<SILInstruction *, 8> Applies;
OptRemark::Emitter ORE(DEBUG_TYPE, F);
bool Changed = false;
for (auto &BB : F) {
// Collect the applies for this block in reverse order so that we
// can pop them off the end of our vector and process them in
// forward order.
for (auto &I : llvm::reverse(BB)) {
// Skip non-apply instructions, apply instructions with no
// substitutions, apply instructions where we do not statically
// know the called function, and apply instructions where we do
// not have the body of the called function.
ApplySite Apply = ApplySite::isa(&I);
if (!Apply || !Apply.hasSubstitutions())
continue;
auto *Callee = Apply.getReferencedFunctionOrNull();
if (!Callee)
continue;
FunctionBuilder.getModule().performOnceForPrespecializedImportedExtensions(
[&FunctionBuilder](AbstractFunctionDecl *pre) {
transferSpecializeAttributeTargets(FunctionBuilder.getModule(), FunctionBuilder,
pre);
});
if (!Callee->isDefinition() && !Callee->hasPrespecialization()) {
ORE.emit([&]() {
using namespace OptRemark;
return RemarkMissed("NoDef", I)
<< "Unable to specialize generic function "
<< NV("Callee", Callee) << " since definition is not visible";
});
continue;
}
Applies.insert(Apply.getInstruction());
}
// Attempt to specialize each apply we collected, deleting any
// that we do specialize (along with other instructions we clone
// in the process of doing so). We pop from the end of the list to
// avoid tricky iterator invalidation issues.
while (!Applies.empty()) {
auto *I = Applies.pop_back_val();
auto Apply = ApplySite::isa(I);
assert(Apply && "Expected an apply!");
SILFunction *Callee = Apply.getReferencedFunctionOrNull();
assert(Callee && "Expected to have a known callee");
if (!Apply.canOptimize())
continue;
if (!isMandatory && !Callee->shouldOptimize())
continue;
// We have a call that can potentially be specialized, so
// attempt to do so.
llvm::SmallVector<SILFunction *, 2> NewFunctions;
trySpecializeApplyOfGeneric(FunctionBuilder, Apply, DeadApplies,
NewFunctions, ORE, isMandatory);
// Remove all the now-dead applies. We must do this immediately
// rather than defer it in order to avoid problems with cloning
// dead instructions when doing recursive specialization.
while (!DeadApplies.empty()) {
auto *AI = DeadApplies.pop_back_val();
// Remove any applies we are deleting so that we don't attempt
// to specialize them.
Applies.remove(AI);
recursivelyDeleteTriviallyDeadInstructions(AI, true);
Changed = true;
}
if (auto *sft = dyn_cast<SILFunctionTransform>(transform)) {
// If calling the specialization utility resulted in new functions
// (as opposed to returning a previous specialization), we need to notify
// the pass manager so that the new functions get optimized.
for (SILFunction *NewF : reverse(NewFunctions)) {
sft->addFunctionToPassManagerWorklist(NewF, Callee);
}
}
}
}
return Changed;
}
namespace {
/// The generic specializer, used in the optimization pipeline.
class GenericSpecializer : public SILFunctionTransform {
/// The entry point to the transformation.
void run() override {
SILFunction &F = *getFunction();
LLVM_DEBUG(llvm::dbgs() << "***** GenericSpecializer on function:"
<< F.getName() << " *****\n");
if (specializeAppliesInFunction(F, this, /*isMandatory*/ false)) {
invalidateAnalysis(SILAnalysis::InvalidationKind::FunctionBody);
}
}
};
} // end anonymous namespace
SILTransform *swift::createGenericSpecializer() {
return new GenericSpecializer();
}