Skip to content

Commit 8284325

Browse files
authored
Merge pull request #206 from CodaFi/dont-pass-me-by
Redo the Pass Manager
2 parents 3d65498 + 7d977b6 commit 8284325

File tree

6 files changed

+857
-70
lines changed

6 files changed

+857
-70
lines changed

Sources/LLVM/PassManager.swift

Lines changed: 41 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import cllvm
33
#endif
44

55
/// A subset of supported LLVM IR optimizer passes.
6-
public enum FunctionPass {
6+
public enum Pass {
77
/// This pass uses the SSA based Aggressive DCE algorithm. This algorithm
88
/// assumes instructions are dead until proven otherwise, which makes
99
/// it more successful are removing non-obviously dead instructions.
@@ -44,6 +44,20 @@ public enum FunctionPass {
4444
/// %Z = add int 2, %X
4545
/// ```
4646
case instructionCombining
47+
/// Working in conjunction with the linker, iterate through all functions and
48+
/// global values in the module and attempt to change their linkage from
49+
/// external to internal.
50+
///
51+
/// To preserve the linkage of a global value, return `true` from the given
52+
/// callback.
53+
case internalize(mustPreserve: (IRGlobal) -> Bool)
54+
/// Working in conjunction with the linker, iterate through all functions and
55+
/// global values in the module and attempt to change their linkage from
56+
/// external to internal.
57+
///
58+
/// When a function with the name "main" is encountered, if the value of
59+
/// `preserveMain` is `true`, "main" will not be internalized.
60+
case internalizeAll(preserveMain: Bool)
4761
/// Thread control through mult-pred/multi-succ blocks where some preds
4862
/// always go to some succ. Thresholds other than minus one override the
4963
/// internal BB duplication default threshold.
@@ -75,7 +89,7 @@ public enum FunctionPass {
7589
/// This pass converts SwitchInst instructions into a sequence of chained
7690
/// binary branch instructions.
7791
case lowerSwitch
78-
/// This pass is used to promote memory references to
92+
/// This pass is used to promote memory references to
7993
/// be register references. A simple example of the transformation performed
8094
/// by this pass is going from code like this:
8195
///
@@ -92,6 +106,10 @@ public enum FunctionPass {
92106
/// ret i32 42
93107
/// ```
94108
case promoteMemoryToRegister
109+
/// Adds DWARF discriminators to the IR. Discriminators are
110+
/// used to decide what CFG path was taken inside sub-graphs whose instructions
111+
/// share the same line and column number information.
112+
case addDiscriminators
95113
/// This pass reassociates commutative expressions in an order that
96114
/// is designed to promote better constant propagation, GCSE, LICM, PRE, etc.
97115
///
@@ -102,12 +120,6 @@ public enum FunctionPass {
102120
case reassociate
103121
/// Sparse conditional constant propagation.
104122
case sccp
105-
/// Replace aggregates or pieces of aggregates with scalar SSA values.
106-
case scalarReplAggregates
107-
/// Replace aggregates or pieces of aggregates with scalar SSA values.
108-
case scalarReplAggregatesSSA
109-
/// Tries to inline the fast path of library calls such as sqrt.
110-
case simplifyLibCalls
111123
/// This pass eliminates call instructions to the current function which occur
112124
/// immediately before return instructions.
113125
case tailCallElimination
@@ -170,6 +182,8 @@ public enum FunctionPass {
170182
/// Return a new pass object which transforms invoke instructions into calls,
171183
/// if the callee can *not* unwind the stack.
172184
case pruneEH
185+
186+
case scalarReplacementOfAggregates
173187
/// This pass removes any function declarations (prototypes) that are not used.
174188
case stripDeadPrototypes
175189
/// These functions removes symbols from functions and modules without
@@ -181,73 +195,26 @@ public enum FunctionPass {
181195
/// This pass performs a superword-level parallelism pass to combine
182196
/// similar independent instructions into vector instructions.
183197
case slpVectorize
198+
/// An invalid pass that crashes when added to the pass manager.
199+
case invalid(reason: String)
200+
}
201+
202+
extension Pass {
203+
@available(*, deprecated, message: "Pass has been removed")
204+
static let simplifyLibCalls: Pass = .invalid(reason: "Pass has been removed")
205+
@available(*, deprecated, message: "Use the scalarReplacementOfAggregates instead")
206+
static let scalarReplAggregates: Pass = .invalid(reason: "Pass has been renamed to 'scalarReplacementOfAggregates'")
207+
@available(*, deprecated, message: "Use the scalarReplacementOfAggregates instead")
208+
static let scalarReplAggregatesSSA: Pass = .invalid(reason: "Pass has been renamed to 'scalarReplacementOfAggregates'")
184209
}
185210

186211
/// A `FunctionPassManager` is an object that collects a sequence of passes
187212
/// which run over a particular IR construct, and runs each of them in sequence
188213
/// over each such construct.
214+
@available(*, deprecated, message: "Use the PassPipeliner instead")
189215
public class FunctionPassManager {
190216
internal let llvm: LLVMPassManagerRef
191-
192-
private static let passMapping: [FunctionPass: (LLVMPassManagerRef) -> Void] = [
193-
.aggressiveDCE: LLVMAddAggressiveDCEPass,
194-
.bitTrackingDCE: LLVMAddBitTrackingDCEPass,
195-
.alignmentFromAssumptions: LLVMAddAlignmentFromAssumptionsPass,
196-
.cfgSimplification: LLVMAddCFGSimplificationPass,
197-
.deadStoreElimination: LLVMAddDeadStoreEliminationPass,
198-
.scalarizer: LLVMAddScalarizerPass,
199-
.mergedLoadStoreMotion: LLVMAddMergedLoadStoreMotionPass,
200-
.gvn: LLVMAddGVNPass,
201-
.indVarSimplify: LLVMAddIndVarSimplifyPass,
202-
.instructionCombining: LLVMAddInstructionCombiningPass,
203-
.jumpThreading: LLVMAddJumpThreadingPass,
204-
.licm: LLVMAddLICMPass,
205-
.loopDeletion: LLVMAddLoopDeletionPass,
206-
.loopIdiom: LLVMAddLoopIdiomPass,
207-
.loopRotate: LLVMAddLoopRotatePass,
208-
.loopReroll: LLVMAddLoopRerollPass,
209-
.loopUnroll: LLVMAddLoopUnrollPass,
210-
.loopUnrollAndJam: LLVMAddLoopUnrollAndJamPass,
211-
.loopUnswitch: LLVMAddLoopUnswitchPass,
212-
.lowerAtomic: LLVMAddLowerAtomicPass,
213-
.memCpyOpt: LLVMAddMemCpyOptPass,
214-
.partiallyInlineLibCalls: LLVMAddPartiallyInlineLibCallsPass,
215-
.lowerSwitch: LLVMAddLowerSwitchPass,
216-
.promoteMemoryToRegister: LLVMAddPromoteMemoryToRegisterPass,
217-
.reassociate: LLVMAddReassociatePass,
218-
.sccp: LLVMAddSCCPPass,
219-
.scalarReplAggregates: LLVMAddScalarReplAggregatesPass,
220-
.scalarReplAggregatesSSA: LLVMAddScalarReplAggregatesPassSSA,
221-
.simplifyLibCalls: LLVMAddSimplifyLibCallsPass,
222-
.tailCallElimination: LLVMAddTailCallEliminationPass,
223-
.constantPropagation: LLVMAddConstantPropagationPass,
224-
.demoteMemoryToRegister: LLVMAddDemoteMemoryToRegisterPass,
225-
.verifier: LLVMAddVerifierPass,
226-
.correlatedValuePropagation: LLVMAddCorrelatedValuePropagationPass,
227-
.earlyCSE: LLVMAddEarlyCSEPass,
228-
.lowerExpectIntrinsic: LLVMAddLowerExpectIntrinsicPass,
229-
.typeBasedAliasAnalysis: LLVMAddTypeBasedAliasAnalysisPass,
230-
.scopedNoAliasAA: LLVMAddScopedNoAliasAAPass,
231-
.basicAliasAnalysis: LLVMAddBasicAliasAnalysisPass,
232-
.unifyFunctionExitNodes: LLVMAddUnifyFunctionExitNodesPass,
233-
.alwaysInliner: LLVMAddAlwaysInlinerPass,
234-
.argumentPromotion: LLVMAddArgumentPromotionPass,
235-
.constantMerge: LLVMAddConstantMergePass,
236-
.deadArgElimination: LLVMAddDeadArgEliminationPass,
237-
.functionAttrs: LLVMAddFunctionAttrsPass,
238-
.functionInlining: LLVMAddFunctionInliningPass,
239-
.globalDCE: LLVMAddGlobalDCEPass,
240-
.globalOptimizer: LLVMAddGlobalOptimizerPass,
241-
.ipConstantPropagation: LLVMAddIPConstantPropagationPass,
242-
.ipscc: LLVMAddIPSCCPPass,
243-
.pruneEH: LLVMAddPruneEHPass,
244-
.stripDeadPrototypes: LLVMAddStripDeadPrototypesPass,
245-
.stripSymbols: LLVMAddStripSymbolsPass,
246-
.loopVectorize: LLVMAddLoopVectorizePass,
247-
.slpVectorize: LLVMAddSLPVectorizePass,
248-
// .internalize: LLVMAddInternalizePass,
249-
// .sroaWithThreshhold: LLVMAddScalarReplAggregatesPassWithThreshold,
250-
]
217+
var alivePassObjects = [Any]()
251218

252219
/// Creates a `FunctionPassManager` bound to the given module's IR.
253220
public init(module: Module) {
@@ -259,9 +226,9 @@ public class FunctionPassManager {
259226
///
260227
/// - parameter passes: A list of function passes to add to the pass manager's
261228
/// list of passes to run.
262-
public func add(_ passes: FunctionPass...) {
229+
public func add(_ passes: Pass...) {
263230
for pass in passes {
264-
FunctionPassManager.passMapping[pass]!(llvm)
231+
PassPipeliner.configurePass(pass, passManager: llvm, keepalive: &alivePassObjects)
265232
}
266233
}
267234

@@ -272,3 +239,7 @@ public class FunctionPassManager {
272239
LLVMRunFunctionPassManager(llvm, function.asLLVM())
273240
}
274241
}
242+
243+
@available(*, deprecated, renamed: "Pass")
244+
public typealias FunctionPass = Pass
245+

0 commit comments

Comments
 (0)