Skip to content

Commit 5796688

Browse files
committed
Feature(compiler): Optimize by llvm-opt on IR level before llvm-llc
1 parent efcb4b6 commit 5796688

4 files changed

Lines changed: 18 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Put it in `calculate-pi.ts`
3636
return 4.0 * inside / cycles;
3737
}
3838

39-
console_log(calculatePI(100000));
39+
console_log(calculatePI(1000000000));
4040
}
4141
```
4242

src/cli.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import DiagnosticHostInstance from "./diagnostic.host";
1010
import UnsupportedError from "./backend/error/unsupported.error";
1111
import {existsSync, mkdirSync, unlinkSync} from "fs";
1212
import {execFileSync} from "child_process";
13-
import {executeLLCSync} from "./utils";
13+
import {executeLLCSync, executeOptSync} from "./utils";
1414

1515
interface CommandLineArguments {
1616
args: string[];
@@ -74,13 +74,20 @@ try {
7474

7575
const optimizationLevel = `-O${cliOptions.optimizationLevel}`;
7676

77+
executeOptSync([
78+
optimizationLevel,
79+
path.join(outputPath, 'main.bc'),
80+
'-o', path.join(outputPath, 'main.bc')
81+
]);
82+
7783
executeLLCSync([
7884
optimizationLevel,
7985
// Fully relocatable, position independent code
8086
'-relocation-model=pic',
8187
'-filetype=obj', path.join(outputPath, 'main.bc'),
8288
'-o', path.join(outputPath, 'main.o'),
8389
]);
90+
8491
execFileSync("c++", [
8592
optimizationLevel,
8693
path.join(outputPath, 'main.o'),

src/utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ export function executeLLCSync(options: Array<any>) {
1717
path.join(getLLVMBinDirectory(), 'llc'),
1818
options
1919
)
20-
}
20+
}
21+
22+
export function executeOptSync(options: Array<any>) {
23+
child_process.execFileSync(
24+
path.join(getLLVMBinDirectory(), 'opt'),
25+
options
26+
)
27+
}

tests/snapshots/examples/calculate-pi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
return 4.0 * inside / cycles;
1616
}
1717

18-
console_log(calculatePI(1000000));
18+
console_log(calculatePI(1000000000));
1919
}

0 commit comments

Comments
 (0)