improve meta.unroll

This commit is contained in:
Judah Caruso 2025-05-27 16:09:05 -06:00
parent 22016ad488
commit 5f7b1107d1

View file

@ -140,51 +140,19 @@ for_expansion :: (v: *Named_Block, code: Code, _: For_Flags) #expand {
} }
for_expansion :: (loop: *Unrolled_Loop, body: Code, flags: For_Flags, loc := #caller_location) #expand { for_expansion :: (loop: *Unrolled_Loop, body: Code, flags: For_Flags, loc := #caller_location) #expand {
#assert flags & .REVERSE == 0 "reverse iteration not supported with loop unrolling (for now)";
#assert flags & .POINTER == 0 "pointer iteration not supported with loop unrolling (for now)";
// runtime unroll // runtime unroll
#if loop.N == -1 { #if loop.N == -1 {
for i: 0..loop.array.count - 1 { for #v2 <=(flags & .REVERSE == .REVERSE) i: 0..loop.array.count - 1 {
`it := #no_abc loop.array[i]; #if flags & .POINTER {
`it := *(#no_abc loop.array[i]);
}
else {
`it := #no_abc loop.array[i];
}
`it_index := i; `it_index := i;
#insert,scope(body) body; #insert,scope(body) body;
} }
// @todo(judah): below doesn't properly handle counts not divisible by 4,
// so we end up going over the bounds of the array.
// UNROLL_AMOUNT :: 4; // @todo(judah): make this configurable?
// unrolled_loops := loop.array.count / UNROLL_AMOUNT;
// remainder_loops := loop.array.count % UNROLL_AMOUNT;
// `it: loop.T;
// for i: 0..unrolled_loops #no_abc {
// basic.print("I: %\n", i);
// index := i * UNROLL_AMOUNT;
// it_index = index + 0;
// it = loop.array[it_index];
// #insert,scope(body) body;
// it_index = index + 1;
// it = loop.array[it_index];
// #insert,scope(body) body;
// it_index = index + 2;
// it = loop.array[it_index];
// #insert,scope(body) body;
// it_index = index + 3;
// it = loop.array[it_index];
// #insert,scope(body) body;
// }
// after_big_loop := it_index;
// for i: 0..remainder_loops #no_abc {
// it_index = after_big_loop + i;
// it = loop.array[it_index];
// #insert,scope(body) body;
// }
} }
// compile-time unroll // compile-time unroll
else { else {
@ -201,14 +169,19 @@ for_expansion :: (loop: *Unrolled_Loop, body: Code, flags: For_Flags, loc := #ca
basic.append(*b, "`it: loop.T = ---;\n"); basic.append(*b, "`it: loop.T = ---;\n");
} }
for 0..loop.N - 1 { for #v2 <=(flags & .REVERSE == .REVERSE) 0..loop.N - 1 {
basic.append(*b, "{\n"); basic.append(*b, "{\n");
if loop.T == void { if loop.T == void {
basic.print_to_builder(*b, "\tit = %;\n", it); basic.print_to_builder(*b, "\tit = %;\n", it);
} }
else { else {
basic.print_to_builder(*b, "\tit = #no_abc loop.array[%];\n", it); #if flags & .POINTER {
basic.print_to_builder(*b, "\tit = *(#no_abc loop.array[%]);\n", it);
}
else {
basic.print_to_builder(*b, "\tit = #no_abc loop.array[%];\n", it);
}
} }
basic.print_to_builder(*b, "\tit_index = %;\n", it); basic.print_to_builder(*b, "\tit_index = %;\n", it);
@ -221,9 +194,6 @@ for_expansion :: (loop: *Unrolled_Loop, body: Code, flags: For_Flags, loc := #ca
} }
} }
#scope_file;
Default_Name :: "block"; Default_Name :: "block";
Named_Block :: struct(NAME: string) {} Named_Block :: struct(NAME: string) {}
@ -242,6 +212,9 @@ Unrolled_Loop :: struct(N: int, T: Type = void) {
} }
} }
#scope_file;
basic :: #import "Basic"; // @future basic :: #import "Basic"; // @future
pp :: #import "Program_Print"; // @future pp :: #import "Program_Print"; // @future
compiler :: #import "Compiler"; // @future compiler :: #import "Compiler"; // @future