Skip to content

Commit

Permalink
Remove deprecated ASTOptimizer (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Jan 1, 2024
1 parent 48402f7 commit c8f3554
Show file tree
Hide file tree
Showing 16 changed files with 31 additions and 294 deletions.
3 changes: 1 addition & 2 deletions docs/docs/cli/build.md
Expand Up @@ -39,5 +39,4 @@ You can apply following options to the `build` subcommand:
| - | `--static` | Produce stand-alone executable by linking statically |
| - | `--no-entry` | Do not require or generate main function (useful for web assembly target) |
| - | `--disable-verifier` | Disable LLVM module and function verification (only recommended for debugging the compiler) |
| - | `--ignore-cache` | Compile always and ignore the compile cache |
| - | `--enable-ast-opt` | Enable AST optimization |
| - | `--ignore-cache` | Compile always and ignore the compile cache |
3 changes: 1 addition & 2 deletions docs/docs/cli/install.md
Expand Up @@ -34,5 +34,4 @@ You can apply following options to the `install` subcommand:
| `-o` | `--output` | Set path for executable output. |
| `-O<n>` | - | Set optimization level. <br> Valid options: `-O0`, `-O1`, `-O2`, `-O3`, `-Os`, `-Oz` |
| `-m` | `--build-mode` | Controls the build mode. Valid values are `debug` and `release` |
| - | `--ignore-cache` | Compile always and ignore the compile cache |
| - | `--enable-ast-opt` | Enable AST optimization |
| - | `--ignore-cache` | Compile always and ignore the compile cache |
3 changes: 1 addition & 2 deletions docs/docs/cli/run.md
Expand Up @@ -35,5 +35,4 @@ You can apply following options to the `run` subcommand:
| `-m` | `--build-mode` | Controls the build mode. Valid values are `debug` and `release` |
| `-g` | `--debug-info` | Generate debug info to debug the executable in GDB, etc. |
| - | `--disable-verifier` | Disable LLVM module and function verification (only recommended for debugging the compiler) |
| - | `--ignore-cache` | Compile always and ignore the compile cache |
| - | `--enable-ast-opt` | Enable AST optimization |
| - | `--ignore-cache` | Compile always and ignore the compile cache |
3 changes: 1 addition & 2 deletions docs/docs/cli/test.md
Expand Up @@ -33,5 +33,4 @@ You can apply following options to the `test` subcommand:
| `-O<x>` | - | Set optimization level. <br> Valid options: `-O0`, `-O1`, `-O2`, `-O3`, `-Os`, `-Oz` |
| `-g` | `--debug-info` | Generate debug info to debug the executable in GDB, etc. |
| - | `--disable-verifier` | Disable LLVM module and function verification (only recommended for debugging the compiler) |
| - | `--ignore-cache` | Compile always and ignore the compile cache |
| - | `--enable-ast-opt` | Enable AST optimization |
| - | `--ignore-cache` | Compile always and ignore the compile cache |
52 changes: 25 additions & 27 deletions media/specs/compile-stages.md
Expand Up @@ -11,7 +11,6 @@ graph TD;
Parser-->
CSTVisualizer-->
ASTBuilder-->
ASTOptimizer-->
ASTVisualizer-->
ImportCollector-->
SymbolTableBuilder-->
Expand Down Expand Up @@ -46,37 +45,33 @@ graph TD;
Input/Output: CST -> AST <br>
Note: Transforms the Concrete Syntax Tree to an Abstract Syntax Tree, enriches it and executes concluding syntax checks.

5. **AST Optimizer** <br>
Input/Output: AST -> AST <br>
Note: Removes obviously unnecessary nodes like empty if-statements

6. **AST Visualizer** <br>
5. **AST Visualizer** <br>
Input/Output: AST -> AST <br>
Note: Prints the AST as JSON or Dot code.

7. **Import Collector** <br>
6. **Import Collector** <br>
Input/Output: AST -> AST <br>
Note: Checks, which other source file are imported by the current one. Registers external symbols. Process module attributes.
[here](./better-imports.md).

8. **Symbol Table Builder** <br>
7. **Symbol Table Builder** <br>
Input/Output: AST -> AST <br>
Note: Creates the symbol table without types and lifetimes

9. **Type Checker** <br>
8. **Type Checker** <br>
Input/Output: AST -> AST <br>
Note: Checks if all types match, performs type inference, fill types in symbol table

10. **IR Generator** <br>
9. **IR Generator** <br>
Input/Output: AST -> IR <br>
Additional used resources: Symbol Table <br>
Note: Uses several helper modules to generate IR from the information of AST and Symbol Table.

11. **IR Optimizer** <br>
10. **IR Optimizer** <br>
Input/Output: IR -> IR <br>
Note: Uses the stated optimization level to call the LLVM optimizer. In case of -O0, the IR Optimizer is not invoked.

12. **Object Emitter** <br>
11. **Object Emitter** <br>
Input/Output: IR -> Object file <br>
Note: Calls LLVM to emit an object file from the generated IR.

Expand Down Expand Up @@ -109,21 +104,24 @@ Source file A imports B and C.
4. AST Builder for C
5. AST Visualizer for C
6. Import Collector for C
7. Type Checker for B (prepare)
8. Type Checker for C (prepare)
9. Type Checker for A (prepare)
10. Type Checker for A (check)
11. Type Checker for B (check)
12. Type Checker for C (check)
13. IR Generator for B
14. IR Optimizer for B
15. Object Emitter for B
16. IR Generator for C
17. IR Optimizer for C
18. Object Emitter for C
19. IR Generator for A
20. IR Optimizer for A
21. Object Emitter for A
7. Symbol Table Builder for B
8. Symbol Table Builder for C
9. Symbol Table Builder for A
10. Type Checker for B (prepare)
11. Type Checker for C (prepare)
12. Type Checker for A (prepare)
13. Type Checker for A (check)
14. Type Checker for B (check)
15. Type Checker for C (check)
16. IR Generator for B
17. IR Optimizer for B
18. Object Emitter for B
19. IR Generator for C
20. IR Optimizer for C
21. Object Emitter for C
22. IR Generator for A
23. IR Optimizer for A
24. Object Emitter for A

## Note for parallelization:

Expand Down
46 changes: 2 additions & 44 deletions media/test-project/test.spice
@@ -1,46 +1,4 @@
f<int> main() {
// Directly
String s1 = String("");
dyn s2 = String("Hello");
dyn s3 = String("Hello!");
dyn s4 = String("Hello World!");

assert s1.isEmpty();
assert !s2.isEmpty();
assert s3.getLength() == 6;
assert s4.getLength() == 12;
assert s3.getCapacity() == 12;
assert s4.getCapacity() == 24;
assert s2.isFull();
assert !s4.isFull();
assert s4.find("ell") == 1;
assert s4.find("Wort") == -1;
assert s4.find("H") == 0;
assert s4.find("!") == 11;
assert s4.find(" ", 12) == -1;
assert s4.rfind("ell") == 1;
assert s4.rfind("o") == 7;
assert s4.rfind("!") == 11;
assert s4.rfind("o", 6) == 4;
assert !s4.contains("abc");
assert s4.contains("Hello");
assert s4.contains("World!");
assert s4.contains("o W");
String sub = s4.getSubstring(0, 5l);
assert sub.getRaw() == "Hello";
sub = s4.getSubstring(4l, 2l);
assert sub.getRaw() == "o ";
sub = s4.getSubstring(6);
assert sub.getRaw() == "World!";
sub = s4.getSubstring(2, 0l);
assert sub.getRaw() == "";
sub = s4.getSubstring(2l, 12l);
assert sub.getRaw() == "llo World!";

printf("All assertions passed!");
}

/*type Visitable interface {
type Visitable interface {
p print();
}

Expand All @@ -56,7 +14,7 @@ f<int> main() {
Test1 t1 = Test1{123};
Visitable* v = &t1;
v.print();
}*/
}

/*import "../../src-bootstrap/lexer/lexer";
import "../../src-bootstrap/parser/parser";
Expand Down
3 changes: 0 additions & 3 deletions src-bootstrap/driver.spice
Expand Up @@ -27,7 +27,6 @@ public type CliOptions struct {
public bool dumpIR = false
public bool dumpAssembly = false
public bool dumpSymbolTable = false
public bool enableAstOpt = false
public short optLevel = 2s // -O0 = 0, -O1 = 1, -O2 = 2, -O3 = 3, -Os = 4, -Oz = 5
public bool generateDebugInfo = false
public bool disableVerifier = false
Expand Down Expand Up @@ -177,8 +176,6 @@ p Driver.addCompileSubcommandOptions(CliSubcommand& subCmd) {
jobsFlag.addAlias("-j");
// --ignore-cache
subCmd.addFlag("--ignore-cache", this.cliOptions.ignoreCache, "Force re-compilation of all source files");
// --enable-ast-opt
subCmd.addFlag("--enable-ast-opt", this.cliOptions.enableAstOpt, "Enable first order optimizations on the AST");

// Opt levels
//subCmd.addFlag("-O0", [&]() { this.cliOptions.optLevel = 0; }, "Disable optimization for the output executable.");
Expand Down
1 change: 0 additions & 1 deletion src-bootstrap/source-file.spice
Expand Up @@ -36,7 +36,6 @@ type TimerOutput struct {
unsigned long parser = 0l
unsigned long cstVisualizer = 0l
unsigned long astBuilder = 0l
unsigned long astOptimizer = 0l
unsigned long astVisualizer = 0l
unsigned long importCollector = 0l
unsigned long symbolTableBuilder = 0l
Expand Down
3 changes: 0 additions & 3 deletions src/CMakeLists.txt
Expand Up @@ -36,9 +36,6 @@ set(SOURCES
# AST visualizer
visualizer/ASTVisualizer.cpp
visualizer/ASTVisualizer.h
# AST optimizer
astoptimizer/ASTOptimizer.cpp
astoptimizer/ASTOptimizer.h
# Import collector
importcollector/ImportCollector.cpp
importcollector/ImportCollector.h
Expand Down
18 changes: 0 additions & 18 deletions src/SourceFile.cpp
Expand Up @@ -3,7 +3,6 @@
#include "SourceFile.h"

#include <ast/ASTBuilder.h>
#include <astoptimizer/ASTOptimizer.h>
#include <exception/AntlrThrowingErrorListener.h>
#include <exception/CompilerError.h>
#include <global/GlobalResourceManager.h>
Expand Down Expand Up @@ -145,22 +144,6 @@ void SourceFile::runASTBuilder() {
printStatusMessage("AST Builder", IO_CST, IO_AST, compilerOutput.times.astBuilder);
}

void SourceFile::runASTOptimizer() {
// Skip if restored from cache or this stage has already been done
if (restoredFromCache || previousStage >= AST_OPTIMIZER)
return;

Timer timer(&compilerOutput.times.astOptimizer);
timer.start();

ASTOptimizer astOptimizer(resourceManager, this);
astOptimizer.visit(ast);

previousStage = AST_OPTIMIZER;
timer.stop();
printStatusMessage("AST Optimizer", IO_AST, IO_AST, compilerOutput.times.astOptimizer);
}

void SourceFile::runASTVisualizer() {
// Only execute if enabled
if (restoredFromCache || (!resourceManager.cliOptions.dumpSettings.dumpAST && !resourceManager.cliOptions.testMode))
Expand Down Expand Up @@ -507,7 +490,6 @@ void SourceFile::runFrontEnd() { // NOLINT(misc-no-recursion)
runParser();
runCSTVisualizer();
runASTBuilder();
runASTOptimizer();
runASTVisualizer();
runImportCollector();
runSymbolTableBuilder();
Expand Down
2 changes: 0 additions & 2 deletions src/SourceFile.h
Expand Up @@ -63,7 +63,6 @@ struct TimerOutput {
uint64_t parser = 0;
uint64_t cstVisualizer = 0;
uint64_t astBuilder = 0;
uint64_t astOptimizer = 0;
uint64_t astVisualizer = 0;
uint64_t importCollector = 0;
uint64_t symbolTableBuilder = 0;
Expand Down Expand Up @@ -108,7 +107,6 @@ class SourceFile {
void runParser();
void runCSTVisualizer();
void runASTBuilder();
void runASTOptimizer();
void runASTVisualizer();
void runImportCollector();
void runSymbolTableBuilder();
Expand Down

0 comments on commit c8f3554

Please sign in to comment.