diff options
| author | Dmitry Stogov <dmitrystogov@gmail.com> | 2025-03-24 18:56:05 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-24 08:56:05 -0700 |
| commit | ac3855c4313d6cab8e2bb778b5bfa679415cb519 (patch) | |
| tree | 65e133808c318951a802b6b156676939bcc530d9 /chapter21/src | |
| parent | 3db126e018388facfab368ecb2c8f8bc566afe1a (diff) | |
| download | Simple-ac3855c4313d6cab8e2bb778b5bfa679415cb519.tar.gz Simple-ac3855c4313d6cab8e2bb778b5bfa679415cb519.zip | |
Driver related fixes and improvements (#175)
* Fix simple.sh (pass arguments)
* Consistently replace "_" with "-" in command-line options
* Add "--dump-time" option to print compilation statistics
* Add "--dump-size" option to print generated code size
Diffstat (limited to 'chapter21/src')
| -rw-r--r-- | chapter21/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java | 10 | ||||
| -rw-r--r-- | chapter21/src/test/java/com/seaofnodes/simple/Simple.java | 49 |
2 files changed, 50 insertions, 9 deletions
diff --git a/chapter21/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java b/chapter21/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java index 1a148910..d1276114 100644 --- a/chapter21/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java +++ b/chapter21/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java @@ -156,7 +156,7 @@ public class CodeGen { // --------------------------- // Last check for bad programs - int _tTypeCheck; + public int _tTypeCheck; public CodeGen typeCheck() { // Demand phase Opto for cleaning up dead control flow at least, // required for the following GCM. @@ -249,7 +249,7 @@ public class CodeGen { // --------------------------- // Control Flow Graph in RPO order. - int _tGCM; + public int _tGCM; public Ary<CFGNode> _cfg = new Ary<>(CFGNode.class); // Global schedule (code motion) nodes @@ -270,7 +270,7 @@ public class CodeGen { // --------------------------- // Local (basic block) scheduler phase, a classic list scheduler - int _tLocal; + public int _tLocal; public CodeGen localSched() { assert _phase == Phase.Schedule; _phase = Phase.LocalSched; @@ -283,7 +283,7 @@ public class CodeGen { // --------------------------- // Register Allocation - int _tRegAlloc; + public int _tRegAlloc; public RegAlloc _regAlloc; public CodeGen regAlloc() { assert _phase == Phase.LocalSched; @@ -305,7 +305,7 @@ public class CodeGen { // --------------------------- // Encoding - int _tEncode; + public int _tEncode; public Encoding _encoding; public CodeGen encode() { assert _phase == Phase.RegAlloc; diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Simple.java b/chapter21/src/test/java/com/seaofnodes/simple/Simple.java index 673b8b7c..56761e7a 100644 --- a/chapter21/src/test/java/com/seaofnodes/simple/Simple.java +++ b/chapter21/src/test/java/com/seaofnodes/simple/Simple.java @@ -43,16 +43,18 @@ Options: --dump-after-parse - dump intermediate representation after parse --dump-after-opto - dump intermediate representation after opto pass --dump-after-type-check - dump intermediate representation after type check pass - --dump-after-instr_select - dump intermediate representation after instrution selection pass + --dump-after-instr-select - dump intermediate representation after instrution selection pass --dump-after-gcm - dump intermediate representation after GCM pass --dump-after-local-sched - dump intermediate representation after local scheduling pass - --dump-after-reg_alloc - dump intermediate representation after register allocation pass + --dump-after-reg-alloc - dump intermediate representation after register allocation pass --dump-after-encode - dump intermediate representation after encoding pass --dump-after-all - dump intermediate representation after all passes --dot - dump grapical representation of intermediate code into *.dot file(s) -S - dump generated assembler code --eval ... - evaluate the compiled code in emulator --run ... - run the compiled code natively + --dump-size - print the size of generated code + --dump-time - print compilation and execution times --cpu <cpu-name> - use specific CPU (x86_64_v2, riscv, arm) --abi <abi-name> - use speific ABI variant (SystemV) --target - print native CPU and ABI @@ -123,6 +125,28 @@ Options: } } + static void print_compilation_times(CodeGen code) { + double t, total = 0; + + total += t = code._tParse / 1e3; + System.out.println(String.format("Parsing Time: %.3f sec", t)); + total += t = code._tOpto / 1e3; + System.out.println(String.format("Optimization Time: %.3f sec", t)); + total += t = code._tTypeCheck / 1e3; + System.out.println(String.format("Type Checking Time: %.3f sec", t)); + total += t = code._tInsSel / 1e3; + System.out.println(String.format("Instruction Selection Time: %.3f sec", t)); + total += t = code._tGCM / 1e3; + System.out.println(String.format("GCM Time: %.3f sec", t)); + total += t = code._tLocal / 1e3; + System.out.println(String.format("Local Scheduling Time: %.3f sec", t)); + total += t = code._tRegAlloc / 1e3; + System.out.println(String.format("Register Allocation Time: %.3f sec", t)); + total += t = code._tEncode / 1e3; + System.out.println(String.format("Instruction Encoding Time: %.3f sec", t)); + System.out.println(String.format("TOTAL COMPILATION TIME: %.3f sec", total)); + } + static String getInput() { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); return reader.lines().collect(Collectors.joining("\n")); @@ -134,6 +158,8 @@ Options: boolean do_eval = false; boolean do_run = false; boolean do_codegen = false; + boolean do_print_size = false; + boolean do_print_time = false; int dump = 0; int first_arg = 0; String src = null; @@ -157,13 +183,15 @@ loop: for (int i = 0; i < args.length; i++) { case "--dump-after-instr-select": dump |= DUMP_AFTER_INSTR_SELECT; break; case "--dump-after-gcm": dump |= DUMP_AFTER_GCM; break; case "--dump-after-local-sched": dump |= DUMP_AFTER_LOCAL_SCHED; break; - case "--dump-after-reg_alloc": dump |= DUMP_AFTER_REG_ALLOC; break; + case "--dump-after-reg-alloc": dump |= DUMP_AFTER_REG_ALLOC; break; case "--dump-after-encode": dump |= DUMP_AFTER_ENCODE; break; case "--dump-after-all": dump |= DUMP_AFTER_ALL; break; case "--dot": dump |= DUMP_DOT; break; case "-S": dump |= DUMP_DISASSEMBLE; break; case "--eval": do_eval = true; first_arg = i + 1; break loop; case "--run": do_run = true; first_arg = i + 1; break loop; + case "--dump-size": do_print_size = true; break; + case "--dump-time": do_print_time = true; break; case "--cpu": if (cpu != null || i + 1 >= args.length || args[i + 1].charAt(0) == '-') bad_usage(); @@ -192,7 +220,7 @@ loop: for (int i = 0; i < args.length; i++) { System.exit(1); } - if (do_run || (dump & DUMP_DISASSEMBLE) != 0) { + if (do_run || (dump & DUMP_DISASSEMBLE) != 0 || do_print_size) { if (cpu == null) cpu = system_cpu; if (abi == null) abi = system_abi; if (cpu == null || abi == null) { @@ -247,10 +275,23 @@ loop: for (int i = 0; i < args.length; i++) { System.out.println(code.asm()); } + if (do_print_size) { + System.out.println(String.format("Code Size: %d", code._encoding._bits.size())); + } + + if (do_print_time) { + print_compilation_times(code); + } + if (do_eval) { // TODO: Support for evaluation of functions with different argument numbers and types + long t = System.currentTimeMillis(); long arg = (first_arg < args.length) ? Integer.valueOf(args[first_arg]) : 0; System.out.println(Eval2.eval(code, arg, 100000)); + if (do_print_time) { + System.out.println(String.format("EXECUTION TIME: %.3f sec", + (System.currentTimeMillis() - t) / 1e3)); + } } else if (do_run) { if (cpu != system_cpu || abi != system_abi) { System.err.println("ERROR: cannot run code on not native target"); |
