Skip to content

Commit 4274418

Browse files
committed
fix(compiler): keep provenance information of IR blocks inside the IROptimiser
1 parent ffd6407 commit 4274418

28 files changed

Lines changed: 110 additions & 18 deletions

src/arkreactor/Compiler/IntermediateRepresentation/IROptimizer.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ namespace Ark::internal
249249
for (const auto& two : math_ops)
250250
{
251251
for (const auto& three : math_ops)
252+
// cppcheck-suppress useStlAlgorithm
252253
m_ruleset.emplace_back(Rule { { one, two, three }, fuseMathOps3 });
253254
m_ruleset.emplace_back(Rule { { one, two }, fuseMathOps2 });
254255
}
@@ -265,7 +266,15 @@ namespace Ark::internal
265266

266267
for (const auto& block : pages)
267268
{
268-
m_ir.emplace_back();
269+
m_ir.emplace_back(IR::Block {
270+
.metadata = {
271+
.name = block.metadata.name,
272+
.argument_count = block.metadata.argument_count,
273+
.addr = block.metadata.addr,
274+
.is_closure = block.metadata.is_closure,
275+
.is_recursive = block.metadata.is_recursive,
276+
.is_simple = block.metadata.is_simple },
277+
.data = {} });
269278
std::vector<IR::Entity>& current_block = m_ir.back().data;
270279

271280
std::size_t i = 0;

tests/unittests/Suites/CompilerSuite.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,20 @@ ut::suite<"Compiler"> compiler_suite = [] {
102102
});
103103
};
104104

105-
"IR generation and optimization"_test = [] {
105+
"IR generation and optimisation"_test = [] {
106106
constexpr uint16_t features = featuresNoOpti | Ark::FeatureIROptimiser;
107107

108108
iterTestFiles(
109-
"CompilerSuite/optimized_ir",
109+
"CompilerSuite/optimised_ir",
110110
[](TestData&& data) {
111111
Ark::Welder welder(0, { lib_path }, features);
112112

113-
should("compile without error optimized_ir/" + data.stem) = [&] {
113+
should("compile without error optimised_ir/" + data.stem) = [&] {
114114
expect(mut(welder).computeASTFromFile(data.path));
115115
expect(mut(welder).generateBytecode());
116116
};
117117

118-
should("output expected optimized IR for " + data.stem) = [&] {
118+
should("output expected optimised IR for " + data.stem) = [&] {
119119
std::string ir = welder.textualIR();
120120

121121
Ark::Utils::ltrim(Ark::Utils::rtrim(ir));
@@ -148,7 +148,29 @@ ut::suite<"Compiler"> compiler_suite = [] {
148148
updateExpectedFile(data, ir);
149149
};
150150
});
151+
};
152+
153+
"IR generation, inlining and optimisation"_test = [] {
154+
constexpr uint16_t features = featuresNoOpti | Ark::FeatureIRInliner | Ark::FeatureASTOptimiser;
155+
156+
iterTestFiles(
157+
"CompilerSuite/inlined_optimised_ir",
158+
[](TestData&& data) {
159+
Ark::Welder welder(0, { lib_path }, features);
160+
161+
should("compile without error inlined_optimised_ir/" + data.stem) = [&] {
162+
expect(mut(welder).computeASTFromFile(data.path));
163+
expect(mut(welder).generateBytecode());
164+
};
151165

152-
// todo: ir inliner
166+
should("output expected inlined IR for " + data.stem) = [&] {
167+
std::string ir = welder.textualIR();
168+
169+
Ark::Utils::ltrim(Ark::Utils::rtrim(ir));
170+
expectOrDiff(data.expected, ir);
171+
if (shouldWriteNewDiffsTofile() && data.expected != ir)
172+
updateExpectedFile(data, ir);
173+
};
174+
});
153175
};
154176
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(let h (fun () 1))
2+
((fun () {
3+
(let h (fun () 2))
4+
((fun () (print (h))))
5+
((fun (h) (print (h))) h) }))
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
global
2+
LOAD_CONST 0
3+
STORE 0
4+
PUSH_RETURN_ADDRESS L0
5+
LOAD_CONST 2
6+
CALL 0
7+
.L0:
8+
POP 0
9+
HALT 0
10+
11+
page_1 (h (0 arguments) simple function, 3 instructions)
12+
LOAD_CONST 1
13+
RET 0
14+
HALT 0
15+
16+
page_2 (#anonymous (0 arguments) function, 14 instructions)
17+
LOAD_CONST 3
18+
STORE 0
19+
PUSH_RETURN_ADDRESS L1
20+
LOAD_CONST 5
21+
CALL 0
22+
.L1:
23+
POP 0
24+
PUSH_RETURN_ADDRESS L4
25+
LOAD_CONST 6
26+
LOAD_FAST_BY_INDEX 0
27+
CALL 1
28+
.L4:
29+
RET 0
30+
HALT 0
31+
32+
page_3 (h (0 arguments) simple function, 3 instructions)
33+
LOAD_CONST 4
34+
RET 0
35+
HALT 0
36+
37+
page_4 (#anonymous (0 arguments) function, 8 instructions)
38+
PUSH_RETURN_ADDRESS L2
39+
PUSH_RETURN_ADDRESS L3
40+
CALL_SYMBOL 0, 0
41+
.L3:
42+
CALL_BUILTIN 9, 1
43+
.L2:
44+
RET 0
45+
HALT 0
46+
47+
page_5 (#anonymous (1 argument) function, 9 instructions)
48+
STORE 0
49+
PUSH_RETURN_ADDRESS L5
50+
PUSH_RETURN_ADDRESS L6
51+
CALL_SYMBOL_BY_INDEX 0, 0
52+
.L6:
53+
CALL_BUILTIN 9, 1
54+
.L5:
55+
RET 0
56+
HALT 0

tests/unittests/resources/CompilerSuite/optimized_ir/99bottles.ark renamed to tests/unittests/resources/CompilerSuite/optimised_ir/99bottles.ark

File renamed without changes.

tests/unittests/resources/CompilerSuite/optimized_ir/99bottles.expected renamed to tests/unittests/resources/CompilerSuite/optimised_ir/99bottles.expected

File renamed without changes.

tests/unittests/resources/CompilerSuite/optimized_ir/ackermann.ark renamed to tests/unittests/resources/CompilerSuite/optimised_ir/ackermann.ark

File renamed without changes.

tests/unittests/resources/CompilerSuite/optimized_ir/ackermann.expected renamed to tests/unittests/resources/CompilerSuite/optimised_ir/ackermann.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ global
1111
POP 0
1212
HALT 0
1313

14-
page_1 (#anonymous (0 arguments) function, 25 instructions)
14+
page_1 (ackermann (2 arguments) recursive simple function, 25 instructions)
1515
STORE 1
1616
STORE 2
1717
LOAD_FAST_BY_INDEX 0

tests/unittests/resources/CompilerSuite/optimized_ir/builtins.ark renamed to tests/unittests/resources/CompilerSuite/optimised_ir/builtins.ark

File renamed without changes.

tests/unittests/resources/CompilerSuite/optimized_ir/builtins.expected renamed to tests/unittests/resources/CompilerSuite/optimised_ir/builtins.expected

File renamed without changes.

0 commit comments

Comments
 (0)