-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathPasses.cpp
312 lines (264 loc) · 10.8 KB
/
Passes.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//===--- Passes.cpp - Swift Compiler SIL Pass Entrypoints -----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file provides implementations of a few helper functions
/// which provide abstracted entrypoints to the SILPasses stage.
///
/// \note The actual SIL passes should be implemented in per-pass source files,
/// not in this file.
///
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "sil-optimizer"
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Module.h"
#include "swift/SIL/SILModule.h"
#include "swift/SILOptimizer/Analysis/Analysis.h"
#include "swift/SILOptimizer/OptimizerBridging.h"
#include "swift/SILOptimizer/PassManager/PassManager.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/YAMLParser.h"
using namespace swift;
bool swift::runSILDiagnosticPasses(SILModule &Module) {
auto &opts = Module.getOptions();
// Verify the module, if required.
// OSSA lifetimes are incomplete until the SILGenCleanup pass runs.
if (opts.VerifyAll)
Module.verifyIncompleteOSSA();
// If we parsed a .sil file that is already in canonical form, don't rerun
// the diagnostic passes.
if (Module.getStage() != SILStage::Raw)
return false;
executePassPipelinePlan(&Module,
SILPassPipelinePlan::getSILGenPassPipeline(opts),
/*isMandatory*/ true);
if (opts.VerifyAll && opts.OSSAVerifyComplete)
Module.verifyOwnership();
executePassPipelinePlan(&Module,
SILPassPipelinePlan::getDiagnosticPassPipeline(opts),
/*isMandatory*/ true);
// If we were asked to debug serialization, exit now.
auto &Ctx = Module.getASTContext();
if (opts.DebugSerialization)
return Ctx.hadError();
// Generate diagnostics.
Module.setStage(SILStage::Canonical);
// Verify the module, if required.
if (opts.VerifyAll)
Module.verify();
else {
LLVM_DEBUG(Module.verify());
}
// If errors were produced during SIL analysis, return true.
return Ctx.hadError();
}
bool swift::runSILLowerHopToActorPass(SILModule &Module) {
auto &Ctx = Module.getASTContext();
auto &opts = Module.getOptions();
executePassPipelinePlan(
&Module, SILPassPipelinePlan::getLowerHopToActorPassPipeline(opts));
return Ctx.hadError();
}
bool swift::runSILOwnershipEliminatorPass(SILModule &Module) {
auto &Ctx = Module.getASTContext();
auto &opts = Module.getOptions();
executePassPipelinePlan(
&Module, SILPassPipelinePlan::getOwnershipEliminatorPassPipeline(opts));
return Ctx.hadError();
}
void swift::runSILOptimizationPasses(SILModule &Module) {
auto &opts = Module.getOptions();
// Verify the module, if required.
if (opts.VerifyAll)
Module.verify();
if (opts.DisableSILPerfOptimizations) {
// If we are not supposed to run SIL perf optzns, we may still need to
// serialize. So serialize now.
executePassPipelinePlan(
&Module, SILPassPipelinePlan::getSerializeSILPassPipeline(opts),
/*isMandatory*/ true);
return;
}
executePassPipelinePlan(
&Module, SILPassPipelinePlan::getPerformancePassPipeline(opts));
// Check if we actually serialized our module. If we did not, serialize now.
if (!Module.isSerialized()) {
executePassPipelinePlan(
&Module, SILPassPipelinePlan::getSerializeSILPassPipeline(opts),
/*isMandatory*/ true);
}
// If we were asked to debug serialization, exit now.
if (opts.DebugSerialization)
return;
// Verify the module, if required.
if (opts.VerifyAll)
Module.verify();
else {
LLVM_DEBUG(Module.verify());
}
}
void swift::runSILPassesForOnone(SILModule &Module) {
// Verify the module, if required.
if (Module.getOptions().VerifyAll)
Module.verify();
// We want to run the Onone passes also for function which have an explicit
// Onone attribute.
executePassPipelinePlan(
&Module, SILPassPipelinePlan::getOnonePassPipeline(Module.getOptions()),
/*isMandatory*/ true);
// Verify the module, if required.
if (Module.getOptions().VerifyAll)
Module.verify();
else {
LLVM_DEBUG(Module.verify());
}
}
void swift::runSILOptimizationPassesWithFileSpecification(SILModule &M,
StringRef Filename) {
auto &opts = M.getOptions();
executePassPipelinePlan(
&M, SILPassPipelinePlan::getPassPipelineFromFile(opts, Filename));
}
/// Get the Pass ID enum value from an ID string.
PassKind swift::PassKindFromString(StringRef IDString) {
return llvm::StringSwitch<PassKind>(IDString)
#define PASS(ID, TAG, DESCRIPTION) .Case(#ID, PassKind::ID)
#include "swift/SILOptimizer/PassManager/Passes.def"
.Default(PassKind::invalidPassKind);
}
/// Get an ID string for the given pass Kind.
/// This is useful for tools that identify a pass
/// by its type name.
StringRef swift::PassKindID(PassKind Kind) {
switch (Kind) {
#define PASS(ID, TAG, DESCRIPTION) \
case PassKind::ID: \
return #ID;
#include "swift/SILOptimizer/PassManager/Passes.def"
case PassKind::invalidPassKind:
llvm_unreachable("Invalid pass kind?!");
}
llvm_unreachable("Unhandled PassKind in switch.");
}
/// Get a tag string for the given pass Kind.
/// This format is useful for command line options.
StringRef swift::PassKindTag(PassKind Kind) {
switch (Kind) {
#define PASS(ID, TAG, DESCRIPTION) \
case PassKind::ID: \
return TAG;
#include "swift/SILOptimizer/PassManager/Passes.def"
case PassKind::invalidPassKind:
llvm_unreachable("Invalid pass kind?!");
}
llvm_unreachable("Unhandled PassKind in switch.");
}
// During SIL Lowering, passes may see partially lowered SIL, which is
// inconsistent with the current (canonical) stage. We don't change the SIL
// stage until lowering is complete. Consequently, any pass added to this
// PassManager needs to be able to handle the output of the previous pass. If
// the function pass needs to read SIL from other functions, it may be best to
// convert it to a module pass to ensure that the SIL input is always at the
// same stage of lowering.
void swift::runSILLoweringPasses(SILModule &Module) {
auto &opts = Module.getOptions();
executePassPipelinePlan(&Module,
SILPassPipelinePlan::getLoweringPassPipeline(opts),
/*isMandatory*/ true);
Module.setStage(SILStage::Lowered);
}
/// Registered briged pass run functions.
static llvm::StringMap<BridgedModulePassRunFn> bridgedModulePassRunFunctions;
static llvm::StringMap<BridgedFunctionPassRunFn> bridgedFunctionPassRunFunctions;
static bool passesRegistered = false;
/// Runs a bridged module pass.
///
/// \p runFunction is a cache for the run function, so that it has to be looked
/// up only once in bridgedPassRunFunctions.
static void runBridgedModulePass(BridgedModulePassRunFn &runFunction,
SILPassManager *passManager,
StringRef passName) {
if (!runFunction) {
runFunction = bridgedModulePassRunFunctions[passName];
if (!runFunction) {
if (passesRegistered) {
llvm::errs() << "Swift pass " << passName << " is not registered\n";
abort();
}
return;
}
}
runFunction({passManager->getSwiftPassInvocation()});
}
/// Runs a bridged function pass.
///
/// \p runFunction is a cache for the run function, so that it has to be looked
/// up only once in bridgedPassRunFunctions.
static void runBridgedFunctionPass(BridgedFunctionPassRunFn &runFunction,
SILPassManager *passManager,
SILFunction *f, StringRef passName) {
if (!runFunction) {
runFunction = bridgedFunctionPassRunFunctions[passName];
if (!runFunction) {
if (passesRegistered) {
llvm::errs() << "Swift pass " << passName << " is not registered\n";
abort();
}
return;
}
}
if (!f->isBridged()) {
llvm::errs() << "SILFunction metatype is not registered\n";
abort();
}
runFunction({{f}, {passManager->getSwiftPassInvocation()}});
}
// Called from initializeSwiftModules().
void SILPassManager_registerModulePass(BridgedStringRef name,
BridgedModulePassRunFn runFn) {
bridgedModulePassRunFunctions[name.unbridged()] = runFn;
passesRegistered = true;
}
void SILPassManager_registerFunctionPass(BridgedStringRef name,
BridgedFunctionPassRunFn runFn) {
bridgedFunctionPassRunFunctions[name.unbridged()] = runFn;
passesRegistered = true;
}
#define LEGACY_PASS(ID, TAG, DESCRIPTION)
#define PASS(ID, TAG, DESCRIPTION) \
class ID##Pass : public SILFunctionTransform { \
static BridgedFunctionPassRunFn runFunction; \
void run() override { \
runBridgedFunctionPass(runFunction, PM, getFunction(), TAG); \
} \
}; \
BridgedFunctionPassRunFn ID##Pass::runFunction = nullptr; \
SILTransform *swift::create##ID() { return new ID##Pass(); } \
#define MODULE_PASS(ID, TAG, DESCRIPTION) \
class ID##Pass : public SILModuleTransform { \
static BridgedModulePassRunFn runFunction; \
void run() override { \
runBridgedModulePass(runFunction, PM, TAG); \
} \
}; \
BridgedModulePassRunFn ID##Pass::runFunction = nullptr; \
SILTransform *swift::create##ID() { return new ID##Pass(); } \
#include "swift/SILOptimizer/PassManager/Passes.def"
#undef SWIFT_FUNCTION_PASS_COMMON