Skip to content

Commit fbb9479

Browse files
Merge pull request #59 from CompilerProgramming/ssaexit
Some fixes, tests and updated README
2 parents f73098f + a1b0af0 commit fbb9479

File tree

8 files changed

+6421
-7
lines changed

8 files changed

+6421
-7
lines changed

optvm/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ A VM / Interpreter is provided that can run the generated code.
4444
SSA Form using Dominator Trees. The input to this transformation is regular IR, output is SSA IR.
4545
* Incremental SSA - This method generate SSA IR directly from the AST, using [Braun's algorithm](https://dl.acm.org/doi/10.1007/978-3-642-37051-9_6) - this is integrated into the
4646
[compiler](src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java) itself and can be enabled using an option.
47-
* [ExitSSA](src/main/java/com/compilerprogramming/ezlang/compiler/ExitSSA.java) - Exits SSA form, using algorithm by Preston Briggs.
47+
* [ExitSSA](src/main/java/com/compilerprogramming/ezlang/compiler/ExitSSA.java) - Exits SSA form - i.e. implements SSA destruction. Two methods are implemented:
48+
* [ExitSSABriggs](src/main/java/com/compilerprogramming/ezlang/compiler/ExitSSABriggs.java) - implements method described by [Preston Briggs](https://dl.acm.org/doi/10.5555/295545.295551).
49+
* [ExitSSABoissinotNoCoalesce](src/main/java/com/compilerprogramming/ezlang/compiler/ExitSSABoissinotNoCoalesce.java) - implements method described by [Benoit Boissinot](https://inria.hal.science/inria-00349925v1/document) -
50+
without the coalescing step. Thus, it is the "naive" approach that resembles Sreedhar's method I.
4851

4952
## Optimizations on SSA Form
5053

51-
* [SparseConditionalConstantPropagation](src/main/java/com/compilerprogramming/ezlang/compiler/SparseConditionalConstantPropagation.java) - Conditional Constant Propagation on SSA form (SCCP)
54+
* [SparseConditionalConstantPropagation](src/main/java/com/compilerprogramming/ezlang/compiler/SparseConditionalConstantPropagation.java) - Conditional Constant Propagation on SSA form (SCCP). This is an implementation of the paper [Constant propagation with conditional branches](https://dl.acm.org/doi/10.1145/103135.103136).
5255
* [ConstantComparisonPropagation](src/main/java/com/compilerprogramming/ezlang/compiler/ConstantComparisonPropagation.java) - Detects equals and not equals against constants within conditionals,
5356
and inserts scoped variables with appropriately specialized type within the dominated blocks, so that a second pass of SCCP can further optimize code.
5457
* [SSAEdges](src/main/java/com/compilerprogramming/ezlang/compiler/SSAEdges.java) - SSAEdges are def-use chains used by SCCP algorithm, and also generated during incremental SSA construction using Braun's method.
@@ -70,7 +73,7 @@ unlimited amount of those.
7073
* [InterferenceGraphBuilder](src/main/java/com/compilerprogramming/ezlang/compiler/InterferenceGraphBuilder.java) - Constructs an InterferenceGraph for a set
7174
of basic bocks, using basic block level liveness information as a starting point for calculating instruction level liveness.
7275
* [ChaitinGraphColoringRegisterAllocator](src/main/java/com/compilerprogramming/ezlang/compiler/ChaitinGraphColoringRegisterAllocator.java) - basic
73-
Chaitin Graph Coloring Register Allocator. Since our target machine here is an abstract machine, we do not really needing spilling support
76+
[Chaitin Graph Coloring Register Allocator](https://web.eecs.umich.edu/~mahlke/courses/583f12/reading/chaitin82.pdf) without spilling. Since our target machine here is an abstract machine, we do not really need spilling support
7477
as we can size each function's stack frame to accommodate the number of registers needed such that each register is really a slot in the stack
7578
frame. But we will eventually simulate an abstract machine with a limited set of registers and a separate stack frame.
7679

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class CompiledFunction {
2727
public boolean hasLiveness;
2828
private final IncrementalSSA issa;
2929

30+
private StringBuilder dumpTarget;
31+
3032
/**
3133
* We essentially do a form of abstract interpretation as we generate
3234
* the bytecode instructions. For this purpose we use a virtual operand stack.
@@ -72,7 +74,9 @@ public CompiledFunction(EZType.EZTypeFunction functionType, TypeDictionary typeD
7274
issa.finish(null);
7375
this.frameSlots = registerPool.numRegisters();
7476
}
75-
77+
public void setDumpTarget(StringBuilder dumpTarget) {
78+
this.dumpTarget = dumpTarget;
79+
}
7680
private void generateArgInstructions(Scope scope) {
7781
if (scope.isFunctionParameterScope) {
7882
for (Symbol symbol: scope.getLocalSymbols()) {
@@ -814,8 +818,14 @@ public StringBuilder toStr(StringBuilder sb, boolean verbose) {
814818
}
815819

816820
public void dumpIR(boolean verbose, String title) {
817-
System.out.println(title);
818-
System.out.println(toStr(new StringBuilder(), verbose));
821+
if (dumpTarget != null) {
822+
dumpTarget.append(title).append("\n");
823+
toStr(dumpTarget,verbose);
824+
}
825+
else {
826+
System.out.println(title);
827+
System.out.println(toStr(new StringBuilder(), verbose));
828+
}
819829
}
820830

821831
public void livenessAnalysis() {

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/ExitSSABoissinotNoCoalesce.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ public ExitSSABoissinotNoCoalesce(CompiledFunction function, EnumSet<Options> op
4141
allBlocks = function.getBlocks();
4242
init();
4343
makeConventionalSSA();
44+
if (options.contains(Options.DUMP_SSA_TO_CSSA)) function.dumpIR(false, "After converting from SSA to CSSA");
4445
removePhis();
46+
if (options.contains(Options.DUMP_CSSA_PHI_REMOVAL)) function.dumpIR(false, "After removing phis from CSSA");
4547
sequenceParallelCopies();
4648
function.isSSA = false;
47-
if (options.contains(Options.DUMP_POST_SSA_IR)) function.dumpIR(false, "After exiting SSA");
49+
if (options.contains(Options.DUMP_POST_SSA_IR)) function.dumpIR(false, "After exiting SSA (Boissinot method)");
4850
}
4951

5052
private void init() {

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/InterferenceGraph.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public boolean interfere(Integer from, Integer to) {
6262
public void rename(Integer source, Integer target) {
6363
// Move all interferences
6464
var fromSet = edges.remove(source);
65+
if (fromSet == null) {
66+
// FIXME figure out why
67+
// Test case testSSA21 / when run using Boissinot SSA Destruction without coalescing
68+
// This is eq() function in mergesort test case
69+
return;
70+
}
6571
var toSet = edges.get(target);
6672
if (toSet == null) {
6773
//throw new RuntimeException("Cannot find edge " + target + " from " + source);

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Options.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ public enum Options {
2020
DUMP_CCP_POSTAPPLY,
2121
DUMP_SSA_LIVENESS,
2222
DUMP_SSA_DOMTREE,
23+
DUMP_SSA_TO_CSSA,
24+
DUMP_CSSA_PHI_REMOVAL,
2325
DUMP_POST_SSA_IR,
2426
DUMP_INTERFERENCE_GRAPH,
2527
DUMP_CHAITIN_COALESCE,
2628
DUMP_POST_CHAITIN_IR;
2729

2830
public static final EnumSet<Options> NONE = EnumSet.noneOf(Options.class);
2931
public static final EnumSet<Options> OPT = EnumSet.of(Options.OPTIMIZE,Options.SCCP,Options.CCP,Options.REGALLOC);
32+
public static final EnumSet<Options> OPT_B = EnumSet.of(Options.OPTIMIZE,Options.SCCP,Options.CCP,Options.REGALLOC,Options.SSA_DESTRUCTION_BOISSINOT_NOCOALESCE);
3033
public static final EnumSet<Options> OPT_ISSA = EnumSet.of(Options.OPTIMIZE,Options.ISSA,Options.SCCP,Options.CCP,Options.REGALLOC);
34+
public static final EnumSet<Options> OPT_ISSA_B = EnumSet.of(Options.OPTIMIZE,Options.ISSA,Options.SCCP,Options.CCP,Options.REGALLOC,Options.SSA_DESTRUCTION_BOISSINOT_NOCOALESCE);
3135
public static final EnumSet<Options> VERBOSE = EnumSet.range(DUMP_INITIAL_IR, DUMP_POST_CHAITIN_IR);
3236
public static final EnumSet<Options> OPT_VERBOSE = EnumSet.range(OPTIMIZE, DUMP_POST_CHAITIN_IR);
3337
}

0 commit comments

Comments
 (0)