Skip to content

Commit

Permalink
[unbox] Fix folding of if-cascade (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
titzer authored Feb 27, 2025
1 parent 52bb24a commit 8ada887
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 20 deletions.
4 changes: 2 additions & 2 deletions aeneas/src/mach/MachLowering.v3
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ class MachLowering(mach: MachProgram, compiler: Compiler, config: MachLoweringCo
i_cmp2.facts |= Facts.O_NO_NAN_CHECK; // NaN handled by first check
ifc.addCase(i_cmp2);
ifc.endCase(i_false);
ifc.addElse();
ifc.addElse(); // TODO: suspicious
var i_cmp = genIntTruncFEqual(i_old.source, ft, ett, i_x);
ifc.endCase(i_cmp);
var i_new = ifc.finish();
Expand Down Expand Up @@ -1042,7 +1042,7 @@ class MachLowering(mach: MachProgram, compiler: Compiler, config: MachLoweringCo
var i_gteq = apply(i_old.source, ft.opcache().opGteq, [i_x, i_maxf]);
ifc.addCase(i_gteq);
ifc.endCase(i_false);
ifc.addElse();
ifc.addElse(); // TODO: suspicious addElse
var i_cmp = genIntTruncFEqual(i_old.source, ft, ett, i_x);
ifc.endCase(i_cmp);
var i_new = ifc.finish();
Expand Down
2 changes: 1 addition & 1 deletion aeneas/src/main/Version.v3
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

// Updated by VCS scripts. DO NOT EDIT.
component Version {
def version: string = "III-9.1801";
def version: string = "III-9.1802";
var buildData: string;
}
29 changes: 12 additions & 17 deletions aeneas/src/ssa/SsaBuilder.v3
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,10 @@ class SsaBuilder {
}
// ClassAlloc[new](args)
def opClassAlloc(m: IrSpec, x: Array<SsaInstr>) -> SsaInstr {
if (m.member == null) return add(V3Op.newEmptyClassAlloc(m.receiver), Ssa.NO_INSTRS, Fact.V_NON_ZERO);
return add(V3Op.newClassAlloc(m), x, Fact.V_NON_ZERO);
var facts: Fact.set = Fact.V_NON_ZERO;
//TODO if (V3.isVariant(m.receiver)) facts |= Fact.O_PURE;
if (m.member == null) return add(V3Op.newEmptyClassAlloc(m.receiver), Ssa.NO_INSTRS, facts);
return add(V3Op.newClassAlloc(m), x, facts);
}
// (Component|Class|Variant)GetField[f](x)
def opGetField(f: IrSpec, x: SsaInstr) -> SsaInstr {
Expand Down Expand Up @@ -875,29 +877,22 @@ class SsaIfCascade {
fblock = null;
}
def endCase(val: SsaInstr) {
if (builder.end) return;
if (varType != null) {
if (val == null) builder.context.fail("SsaIfCascade variable must be non-null on this path");
phiInputs.put(val);
if (!builder.end) {
if (varType != null) {
if (val == null) builder.context.fail("SsaIfCascade variable must be non-null on this path");
phiInputs.put(val);
}
builder.addGoto(merge);
}
builder.addGoto(merge);
at(fblock);
}
private def addIf0(cond: SsaInstr, not: bool) {
if (builder.end) return;
var tb = SsaBlock.new(), fb = SsaBlock.new();
if (not) builder.addIf(cond, fb, tb);
else builder.addIf(cond, tb, fb);
if (tb.preds.length == 0) { // true case eliminated
at(fb);
fblock = null;
} else if (fb.preds.length == 0) { // false case eliminated
at(tb);
fblock = null;
} else {
at(tb);
fblock = fb;
}
at(tb);
fblock = fb;
}
def finish() -> SsaInstr {
if (fblock != null) builder.context.fail("SsaIfCascade: ended before else clause");
Expand Down
3 changes: 3 additions & 0 deletions doc/aeneas-issues.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ ______ ______ __ _ ______ ______ ______ _____ ______ ______ _ _ ______ _

-- priority (primarily for Wizard) -----
new type representation
TypeSystem.computeCast
-target=wasmgc
unbox and pack variants
canonicalize equivalent variant records
threads
fix and turn on inlining and -O3
fold using SSA interpreter

-- wasm[gc] issues -----
new type representation for structs, arrays, funcs
Expand Down
7 changes: 7 additions & 0 deletions test/float/query02.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//@execute -1=false; 1=true
var f = 0.1f;
var g = 33f;

def main(a: int) -> bool {
return if(a < 0, int.?(f), int.?(g));
}
7 changes: 7 additions & 0 deletions test/float/query03.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//@execute -1=false; 1=true
var f = 0.1d;
var g = 33d;

def main(a: int) -> bool {
return if(a < 0, int.?(f), int.?(g));
}
11 changes: 11 additions & 0 deletions test/variants/ub_const00.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@execute =13
type A #unboxed {
case X;
case Y(x: int);
}

def main() -> int {
var a = A.Y(13);
return getx(a);
}
def getx = A.Y.x;
11 changes: 11 additions & 0 deletions test/variants/ub_const01.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@execute =13
type A #unboxed {
case X;
case Y(x: int);
}

def main() -> int {
var a = [A.Y(13), A.Y(13)];
return getx(a[0]);
}
def getx = A.Y.x;
16 changes: 16 additions & 0 deletions test/variants/ub_const02.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//@execute =13
type A #unboxed {
case X;
case Y(x: int);
}

def main() -> int {
var a = A.Y(13);
return getx(a);
}
def getx(a: A) -> int {
match (a) {
X => return 11;
Y(x) => return x;
}
}
12 changes: 12 additions & 0 deletions test/variants/ub_const03.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@execute =true
type A {
case X;
case Y(x: int);
}

var x = A.Y(14);
var y = A.Y(14);

def main() -> bool {
return x == y;
}
File renamed without changes.
14 changes: 14 additions & 0 deletions test/variants/ub_eq01c.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@execute =0
type A #unboxed {
case X;
case Y(x: int);
}

def main() -> int {
var x: A = A.X;

x = A.Y(12);
if (x != A.Y(12)) return -3;

return 0;
}
13 changes: 13 additions & 0 deletions test/variants/ub_eq01d.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@execute =0
type A #unboxed {
case X;
case Y(x: int);
}

def main() -> int {
var a: Array<A> = [A.Y(12), A.Y(12)];
var x = a[0];
var y = a[1];
if (x != y) return -3;
return 0;
}
9 changes: 9 additions & 0 deletions test/variants/ub_eq01e.v3
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@execute =true
type A #unboxed {
case X;
case Y(x: int);
}

def main() -> bool {
return A.==(A.Y(12), A.Y(12));
}

0 comments on commit 8ada887

Please sign in to comment.