preventing dangling nodes due to cycles in loop phys

This commit is contained in:
Jakub Doka 2024-11-11 21:55:18 +01:00
parent b2eefa5b83
commit 7def052749
No known key found for this signature in database
GPG key ID: C6E9A89936B8C143
3 changed files with 65 additions and 5 deletions

View file

@ -615,6 +615,51 @@ main := fn(): uint {
### Purely Testing Examples ### Purely Testing Examples
#### very_nested_loops
```hb
$W := 200
$H := 200
$MAX_ITER := 20
$ZOOM := 0.5
main := fn(): int {
mv_x := 0.5
mv_y := 0.0
y := 0
loop if y < H break else {
x := 0
loop if x < W break else {
i := MAX_ITER - 1
c_i := (2.0 * @floatcast(@itf(@as(i32, @intcast(x)))) - @floatcast(@itf(@as(i32, @intcast(W))))) / (W * ZOOM) + mv_x
c_r := (2.0 * @floatcast(@itf(@as(i32, @intcast(y)))) - @floatcast(@itf(@as(i32, @intcast(H))))) / (H * ZOOM) + mv_y
z_i := c_i
z_r := c_r
// iteration
loop if (z_r + z_i) * (z_r + z_i) >= 4 | i == 0 break else {
// z = z * z + c
z_i = z_i * z_i + c_i
z_r = z_r * z_r + c_r
i -= 1
}
// b g r
put_pixel(.(x, y), .(@as(u8, @intcast(i)), @as(u8, @intcast(i)), @as(u8, @intcast(i)), 255))
}
}
return 0
}
Color := struct {r: u8, g: u8, b: u8, a: u8}
Vec := struct {x: uint, y: uint}
put_pixel := fn(pos: Vec, color: Color): void {
}
```
#### generic_type_mishap #### generic_type_mishap
```hb ```hb
opaque := fn($Expr: type, ptr: ^?Expr): void { opaque := fn($Expr: type, ptr: ^?Expr): void {

View file

@ -1298,14 +1298,21 @@ impl Nodes {
} }
} }
K::Loop => { K::Loop => {
if self[target].inputs[0] == NEVER { if self[target].inputs[1] == NEVER || self[target].inputs[0] == NEVER {
return Some(NEVER);
}
if self[target].inputs[1] == NEVER {
self.lock(target); self.lock(target);
for o in self[target].outputs.clone() { for o in self[target].outputs.clone() {
if self[o].kind == Kind::Phi { if self[o].kind == Kind::Phi {
self.remove_node_lookup(target);
let prev = self[o].inputs[2];
self[o].inputs[2] = VOID;
self[VOID].outputs.push(o);
let index = self[prev].outputs.iter().position(|&n| n == o).unwrap();
self[prev].outputs.swap_remove(index);
self.lock(o);
self.remove(prev);
self.unlock(o);
self.replace(o, self[o].inputs[1]); self.replace(o, self[o].inputs[1]);
} }
} }
@ -3398,6 +3405,7 @@ impl<'a> Codegen<'a> {
), ),
&mut self.ci.nodes, &mut self.ci.nodes,
); );
self.ci.loops.push(Loop { self.ci.loops.push(Loop {
node: self.ci.ctrl.get(), node: self.ci.ctrl.get(),
ctrl: [StrongRef::DEFAULT; 2], ctrl: [StrongRef::DEFAULT; 2],
@ -4749,6 +4757,7 @@ mod tests {
fb_driver; fb_driver;
// Purely Testing Examples; // Purely Testing Examples;
very_nested_loops;
generic_type_mishap; generic_type_mishap;
storing_into_nullable_struct; storing_into_nullable_struct;
scheduling_block_did_dirty; scheduling_block_did_dirty;

View file

@ -0,0 +1,6 @@
main:
LI64 r1, 0d
JALA r0, r31, 0a
code size: 29
ret: 0
status: Ok(())