Skip to content

Commit a20e9ab

Browse files
authored
Merge pull request #255 from maleadt/tb/cfg_simplification_options
Add support for CFG simplification with options.
2 parents 9082d90 + 6821ca5 commit a20e9ab

File tree

7 files changed

+74
-5
lines changed

7 files changed

+74
-5
lines changed

Manifest.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ version = "1.3.0"
3434

3535
[[LLVMExtra_jll]]
3636
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
37-
git-tree-sha1 = "99200ba262364a8502e5a0cca0057cf7c60901d7"
37+
git-tree-sha1 = "a9b1130c4728b0e462a1c28772954650039eb847"
3838
uuid = "dad2f222-ce93-54a1-a47d-0025e8a3acab"
39-
version = "0.0.4+0"
39+
version = "0.0.7+0"
4040

4141
[[LibCURL]]
4242
deps = ["LibCURL_jll", "MozillaCACerts_jll"]

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
1111

1212
[compat]
1313
CEnum = "0.2, 0.3, 0.4"
14-
LLVMExtra_jll = "~0.0.6"
14+
LLVMExtra_jll = "~0.0.7"
1515
julia = "1.6"

deps/LLVMExtra/include/LLVMExtra.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ void LLVMExtraGetNamedMetadataOperands2(LLVMNamedMDNodeRef NMD, LLVMMetadataRef
8484

8585
void LLVMExtraAddNamedMetadataOperand2(LLVMNamedMDNodeRef NMD, LLVMMetadataRef Val);
8686

87+
#if LLVM_VERSION_MAJOR >= 12
88+
void LLVMAddCFGSimplificationPass2(LLVMPassManagerRef PM,
89+
int BonusInstThreshold,
90+
LLVMBool ForwardSwitchCondToPhi,
91+
LLVMBool ConvertSwitchToLookupTable,
92+
LLVMBool NeedCanonicalLoop,
93+
LLVMBool HoistCommonInsts,
94+
LLVMBool SinkCommonInsts,
95+
LLVMBool SimplifyCondBranch,
96+
LLVMBool FoldTwoEntryPHINode);
97+
#endif
98+
8799
// Bug fixes
88100
void LLVMExtraSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal);
89101
void LLVMExtraSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn);

deps/LLVMExtra/lib/llvm-api.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,29 @@ char* LLVMExtraPrintMetadataToString(LLVMMetadataRef MD) {
278278
return strdup(buf.c_str());
279279
}
280280

281+
#if LLVM_VERSION_MAJOR >= 12
282+
void LLVMAddCFGSimplificationPass2(LLVMPassManagerRef PM,
283+
int BonusInstThreshold,
284+
LLVMBool ForwardSwitchCondToPhi,
285+
LLVMBool ConvertSwitchToLookupTable,
286+
LLVMBool NeedCanonicalLoop,
287+
LLVMBool HoistCommonInsts,
288+
LLVMBool SinkCommonInsts,
289+
LLVMBool SimplifyCondBranch,
290+
LLVMBool FoldTwoEntryPHINode)
291+
{
292+
auto simplifyCFGOptions = SimplifyCFGOptions().bonusInstThreshold(BonusInstThreshold)
293+
.forwardSwitchCondToPhi(ForwardSwitchCondToPhi)
294+
.convertSwitchToLookupTable(ConvertSwitchToLookupTable)
295+
.needCanonicalLoops(NeedCanonicalLoop)
296+
.hoistCommonInsts(HoistCommonInsts)
297+
.sinkCommonInsts(SinkCommonInsts)
298+
.setSimplifyCondBranch(SimplifyCondBranch)
299+
.setFoldTwoEntryPHINode(FoldTwoEntryPHINode);
300+
unwrap(PM)->add(createCFGSimplificationPass(simplifyCFGOptions));
301+
}
302+
#endif
303+
281304
// versions of API without MetadataAsValue
282305

283306
const char *LLVMExtraGetMDString2(LLVMMetadataRef MD, unsigned *Length) {

lib/libLLVM_extra.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ function LLVMExtraDIScopeGetName(Scope, Len)
140140
ccall((:LLVMExtraDIScopeGetName, libLLVMExtra), Cstring, (LLVMMetadataRef, Ptr{Cuint}), Scope, Len)
141141
end
142142

143+
function LLVMAddCFGSimplificationPass2(PM, BonusInstThreshold, ForwardSwitchCondToPhi,
144+
ConvertSwitchToLookupTable, NeedCanonicalLoop,
145+
HoistCommonInsts, SinkCommonInsts,
146+
SimplifyCondBranch, FoldTwoEntryPHINode)
147+
ccall((:LLVMAddCFGSimplificationPass2, libLLVMExtra), Cvoid,
148+
(LLVMPassManagerRef, Cint, LLVMBool, LLVMBool, LLVMBool, LLVMBool, LLVMBool, LLVMBool, LLVMBool),
149+
PM, BonusInstThreshold, ForwardSwitchCondToPhi, ConvertSwitchToLookupTable, NeedCanonicalLoop,
150+
HoistCommonInsts, SinkCommonInsts, SimplifyCondBranch, FoldTwoEntryPHINode)
151+
end
152+
143153
# bug fixes
144154

145155
# TODO: upstream

src/transform.jl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ end
9595
## scalar transformations
9696

9797
define_transforms([
98-
:AggressiveDCE, :BitTrackingDCE, :AlignmentFromAssumptions, :CFGSimplification,
98+
:AggressiveDCE, :BitTrackingDCE, :AlignmentFromAssumptions,
9999
:DeadStoreElimination, :Scalarizer, :MergedLoadStoreMotion, :GVN, :IndVarSimplify,
100100
:InstructionCombining, :JumpThreading, :LICM, :LoopDeletion, :LoopIdiom, :LoopRotate,
101101
:LoopReroll, :LoopUnroll, :LoopUnswitch, :MemCpyOpt, :PartiallyInlineLibCalls,
@@ -105,7 +105,7 @@ define_transforms([
105105
:LowerExpectIntrinsic, :TypeBasedAliasAnalysis, :ScopedNoAliasAA, :BasicAliasAnalysis
106106
])
107107

108-
export scalar_repl_aggregates!, scalar_repl_aggregates_ssa!
108+
export scalar_repl_aggregates!, scalar_repl_aggregates_ssa!, cfgsimplification!
109109

110110
scalar_repl_aggregates!(pm::PassManager, threshold::Integer) =
111111
API.LLVMAddScalarReplAggregatesPassWithThreshold(pm, Cint(threshold))
@@ -118,6 +118,27 @@ define_transforms([:DivRemPairs, :LoopDistribute, :LoopFuse, :LoopLoadEliminatio
118118

119119
define_transforms([:InstructionSimplify])
120120

121+
if version() >= v"12"
122+
cfgsimplification!(pm::PassManager;
123+
bonus_inst_threshold=1,
124+
forward_switch_cond_to_phi=false,
125+
convert_switch_to_lookup_table=false,
126+
need_canonical_loop=true,
127+
hoist_common_insts=false,
128+
sink_common_insts=false,
129+
simplify_cond_branch=true,
130+
fold_two_entry_phi_node=true) =
131+
API.LLVMAddCFGSimplificationPass2(pm, bonus_inst_threshold,
132+
forward_switch_cond_to_phi,
133+
convert_switch_to_lookup_table,
134+
need_canonical_loop,
135+
hoist_common_insts,
136+
sink_common_insts,
137+
simplify_cond_branch,
138+
fold_two_entry_phi_node)
139+
else
140+
define_transforms([:CFGSimplification])
141+
end
121142

122143
## vectorization transformations
123144

test/transform.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ ModulePassManager() do pm
3434
bit_tracking_dce!(pm)
3535
alignment_from_assumptions!(pm)
3636
cfgsimplification!(pm)
37+
if LLVM.version() >= v"12"
38+
cfgsimplification!(pm; hoist_common_insts=true)
39+
end
3740
dead_store_elimination!(pm)
3841
scalarizer!(pm)
3942
merged_load_store_motion!(pm)

0 commit comments

Comments
 (0)