Skip to content

Commit d4903f8

Browse files
committed
ir: remove instructions marked as None, fix crash in print
1 parent a37c2f9 commit d4903f8

File tree

6 files changed

+139
-65
lines changed

6 files changed

+139
-65
lines changed

ir/context.c2

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public type Context struct @(opaque) {
5353
RevList revlist;
5454
instr_inserter.InstrInserter inserter;
5555
bit_array.BitArray active_blocks;
56+
CopyList copies;
5657
}
5758

5859
public fn Context* create() {
@@ -69,10 +70,12 @@ public fn Context* create() {
6970
c.collector.create();
7071
c.revlist.create(256);
7172
c.inserter.create();
73+
c.copies.init(1024);
7274
return c;
7375
}
7476

7577
public fn void Context.free(Context* c) {
78+
c.copies.free();
7679
c.inserter.free();
7780
c.revlist.free();
7881
c.collector.free();
@@ -557,15 +560,18 @@ fn void Context.finalizeFunction(Context* c, SymbolId id) {
557560
c.tmp_info.num_slots = cast<u16>(c.slot_idx);
558561

559562
const char* name = c.pool.idx2str(s.name);
563+
bool print = false;
560564

561565
//c.dump_function(c.tmp_info, name);
562-
//c.print_function(id, c.tmp_info);
566+
// causes crash
567+
if (print) c.print_function(id, c.tmp_info, "after generation");
563568

564569
// prune unused and empty blocks, possible convert jmp_if to jmp
565570
c.checkDest(0);
566571

567572
//c.dump_function(c.tmp_info, name);
568-
//c.print_function(id, c.tmp_info);
573+
// causes crash
574+
if (print) c.print_function(id, c.tmp_info, "after check dest");
569575

570576
// move used blocks to new list, drop unused blocks
571577

@@ -679,16 +685,22 @@ fn void Context.finalizeFunction(Context* c, SymbolId id) {
679685

680686
//c.dump_function(fi2, name);
681687
//c.generate_graphviz(fi2, name);
682-
//c.print_function(id, fi2);
688+
if (print) c.print_function(id, fi2, "after reorder");
683689

684-
c.create_ssa(fi2, name);
690+
c.create_ssa(id, fi2, name, print);
685691

686-
if (c.inserter.needsFixup()) c.fixup_function(fi2);
692+
if (print) c.print_function(id, fi2, "after create SSA");
693+
if (c.inserter.needsFixup()) {
694+
c.fixup_function(fi2);
695+
if (print) c.print_function(id, fi2, "after fixup");
696+
}
687697

688-
//c.print_function(id, fi2);
689698
c.eliminateCopies(fi2);
690699
//c.dump_function(fi2, name);
691-
//c.print_function(id, fi2);
700+
if (print) c.print_function(id, fi2, "after eliminate copies");
701+
702+
c.removeNone(fi2);
703+
if (print) c.print_function(id, fi2, "remove none");
692704
}
693705

694706
fn bool empty_block(const Block* b, const InstrList* instructions) {

ir/copy_list.c2

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* Copyright 2022-2025 Bas van den Berg
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
module ir;
17+
18+
import stdlib local;
19+
import string local;
20+
21+
type CopyInfo struct {
22+
u32 instr_idx;
23+
Ref ref;
24+
}
25+
26+
type CopyList struct {
27+
CopyInfo* copies;
28+
u32 count;
29+
u32 capacity;
30+
}
31+
32+
fn void CopyList.init(CopyList* l, u32 capacity) {
33+
l.count = 0;
34+
l.resize(capacity);
35+
}
36+
37+
fn void CopyList.resize(CopyList* l, u32 capacity) {
38+
l.capacity = capacity;
39+
CopyInfo* copies2 = malloc(capacity * sizeof(CopyInfo));
40+
if (l.count) {
41+
memcpy(copies2, l.copies, l.count * sizeof(CopyInfo));
42+
free(l.copies);
43+
}
44+
l.copies = copies2;
45+
}
46+
47+
fn void CopyList.free(CopyList* l) {
48+
free(l.copies);
49+
}
50+
51+
fn void CopyList.clear(CopyList* l) {
52+
l.count = 0;
53+
}
54+
55+
fn void CopyList.add(CopyList* l, u32 idx, Ref ref) {
56+
if (l.count == l.capacity) l.resize(l.capacity * 2);
57+
58+
CopyInfo* info = &l.copies[l.count];
59+
info.instr_idx = idx;
60+
info.ref = ref;
61+
l.count++;
62+
63+
}
64+
65+
fn Ref* CopyList.find(CopyList* l, u32 idx) {
66+
// TODO can do binary search (since instr_idxs are ordered)
67+
for (u32 i=0; i<l.count; i++) {
68+
if (l.copies[i].instr_idx == idx) return &l.copies[i].ref;
69+
}
70+
return nil;
71+
}
72+

ir/print.c2

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,15 @@ fn void PrintHelper.print_func(PrintHelper* ph, const Symbol* s, const FunctionI
174174
ph.collect_temps();
175175

176176
const Block* blks = ph.blocks.get(0);
177-
u32 last = 0;
177+
u32 max = 0;
178178
for (u32 i=0; i<num_blks; i++) {
179179
ph.cur_block = &blks[i];
180180
const Block* b = ph.cur_block;
181181
out.color(color.Yellow);
182182
out.print("%s.%d:", b.getKindName(), i);
183183
if (!b.used) {
184184
out.color(color.Grey);
185-
out.add(" unused");
185+
out.add(" # unused");
186186
}
187187
Index i_idx = b.getInstructions();
188188
#if DebugIr
@@ -202,29 +202,36 @@ fn void PrintHelper.print_func(PrintHelper* ph, const Symbol* s, const FunctionI
202202
for (u32 j=0; j<i_idx.count; j++) {
203203
ph.printInstr(instrs, j, i_idx.start + j);
204204
}
205-
last = i_idx.start + i_idx.count;
205+
u32 last = i_idx.start + i_idx.count;
206+
if (last > max) max = last;
206207
ph.cur_block = nil;
207208
}
208209

209210
// there can be inserted, trailing instructions (outside any block)
210211
u32 instr_count = info.instructions.getCount();
211-
if (last != instr_count) {
212+
if (max != instr_count) {
212213
out.color(col_Fixup);
213-
out.print("Inserted (%d)\n", instr_count - last);
214+
out.print("Inserted (%d)\n", instr_count - max);
214215
const Instr* instrs = info.instructions.get(0);
215-
while (last != instr_count) {
216-
ph.printInstr(instrs, last, last);
217-
last++;
216+
while (max != instr_count) {
217+
ph.printInstr(instrs, max, max);
218+
max++;
218219
}
219220
}
220221

221222
out.color(color.Normal);
222-
out.add("}\n\n");
223+
out.add("}\n");
223224
}
224225

225-
fn void Context.print_function(const Context* c, SymbolId id, const FunctionInfo* info) @(unused) {
226+
fn void Context.print_function(const Context* c,
227+
SymbolId id,
228+
const FunctionInfo* info,
229+
const char* version) @(unused) {
226230
PrintHelper ph.init(c);
227231

232+
ph.out.add("------ ");
233+
ph.out.add(version);
234+
ph.out.add(" ------\n");
228235
ph.print_func(c.symbols.get(id), info);
229236

230237
ph.print();
@@ -241,6 +248,7 @@ public fn void Context.print(const Context* c) {
241248
if (s.is_function) {
242249
if (prev_is_var) out.newline();
243250
ph.print_func(s, s.f.info);
251+
out.newline();
244252
} else {
245253
if (s.is_external) {
246254
out.color(col_Attr);
@@ -458,6 +466,7 @@ fn void PrintHelper.printRef(PrintHelper* ph, Ref r, bool print_type) {
458466
#endif
459467
break;
460468
case JmpDest:
469+
assert(ph.cur_block);
461470
const Block* b = ph.cur_block;
462471
out.color(col_Block);
463472
assert(b.dests[0]);

ir/slot_collector.c2

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
module ir;
1717

1818
import stdlib;
19-
import string;
19+
import string local;
2020
import stdio local;
2121

2222
type ReadSlotAccess struct {
@@ -59,7 +59,7 @@ fn BlockIndex* SlotCollector.getBlock(const SlotCollector* c, BlockId blk_id) {
5959
}
6060

6161
fn void SlotCollector.create(SlotCollector* c) {
62-
string.memset(c, 0, sizeof(SlotCollector));
62+
memset(c, 0, sizeof(SlotCollector));
6363
// TEMP hardcoded max
6464
c.reads = stdlib.malloc(SlotMax * sizeof(ReadSlotAccess));
6565
c.writes = stdlib.malloc(SlotMax * sizeof(WriteSlotAccess));
@@ -209,7 +209,7 @@ fn void SlotCollector.insertWrite(SlotCollector* c, BlockId blk_id, u16 slot, Re
209209
BlockIndex* bi = &c.indexes[blk_id];
210210
assert(c.write_idx + bi.wr_count + 1 < SlotMax);
211211
if (bi.wr_count) {
212-
string.memcpy(&c.writes[c.write_idx], &c.writes[bi.wr_start], bi.wr_count * sizeof(WriteSlotAccess));
212+
memcpy(&c.writes[c.write_idx], &c.writes[bi.wr_start], bi.wr_count * sizeof(WriteSlotAccess));
213213
}
214214
bi.wr_start = cast<u16>(c.write_idx);
215215
c.write_idx += bi.wr_count;
@@ -223,13 +223,14 @@ fn void SlotCollector.insertWrite(SlotCollector* c, BlockId blk_id, u16 slot, Re
223223
if (c.write_idx > c.num_writes + 200) c.defragWrites();
224224
}
225225

226+
// when adding writes, they are copied to tail, so gaps appear. Defrag removes these
226227
fn void SlotCollector.defragWrites(SlotCollector* c) {
227228
u16 out_idx = 0;
228229
for (u32 i=0; i<c.num_blocks; i++) {
229230
BlockIndex* bi = &c.indexes[i];
230231
if (bi.wr_count == 0) continue;
231232

232-
string.memcpy(&c.writes2[out_idx], &c.writes[bi.wr_start], bi.wr_count * sizeof(WriteSlotAccess));
233+
memcpy(&c.writes2[out_idx], &c.writes[bi.wr_start], bi.wr_count * sizeof(WriteSlotAccess));
233234
bi.wr_start = out_idx;
234235
out_idx += bi.wr_count;
235236
}

0 commit comments

Comments
 (0)