Skip to content

Commit d68c266

Browse files
authored
Omit asserts if condition can be proven statically (AssemblyScript#685)
1 parent 316b2d4 commit d68c266

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+3944
-7593
lines changed

src/builtins.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,52 @@ export function compileCall(
20902090
let type = compiler.currentType;
20912091
compiler.currentType = type.nonNullableType;
20922092

2093+
// if the assertion can be proven statically, omit it
2094+
if (getExpressionId(arg0 = module.precomputeExpression(arg0)) == ExpressionId.Const) {
2095+
switch (getExpressionType(arg0)) {
2096+
case NativeType.I32: {
2097+
if (getConstValueI32(arg0) != 0) {
2098+
if (contextualType == Type.void) {
2099+
compiler.currentType = Type.void;
2100+
return module.nop();
2101+
}
2102+
return arg0;
2103+
}
2104+
break;
2105+
}
2106+
case NativeType.I64: {
2107+
if (getConstValueI64Low(arg0) != 0 || getConstValueI64High(arg0) != 0) {
2108+
if (contextualType == Type.void) {
2109+
compiler.currentType = Type.void;
2110+
return module.nop();
2111+
}
2112+
return arg0;
2113+
}
2114+
break;
2115+
}
2116+
case NativeType.F32: {
2117+
if (getConstValueF32(arg0) != 0) {
2118+
if (contextualType == Type.void) {
2119+
compiler.currentType = Type.void;
2120+
return module.nop();
2121+
}
2122+
return arg0;
2123+
}
2124+
break;
2125+
}
2126+
case NativeType.F64: {
2127+
if (getConstValueF64(arg0) != 0) {
2128+
if (contextualType == Type.void) {
2129+
compiler.currentType = Type.void;
2130+
return module.nop();
2131+
}
2132+
return arg0;
2133+
}
2134+
break;
2135+
}
2136+
}
2137+
}
2138+
20932139
// return ifTrueish if assertions are disabled
20942140
if (compiler.options.noAssert) {
20952141
if (contextualType == Type.void) { // simplify if dropped anyway

tests/compiler/assert.optimized.wat

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
(module
22
(type $FUNCSIG$v (func))
3-
(memory $0 1)
4-
(data (i32.const 8) "\12\00\00\00\01\00\00\00\01\00\00\00\12\00\00\00a\00s\00s\00e\00r\00t\00.\00t\00s")
5-
(data (i32.const 48) "\18\00\00\00\01\00\00\00\01\00\00\00\18\00\00\00m\00u\00s\00t\00 \00b\00e\00 \00t\00r\00u\00e")
3+
(memory $0 0)
64
(export "memory" (memory $0))
7-
(func $start (; 0 ;) (type $FUNCSIG$v)
5+
(func $null (; 0 ;) (type $FUNCSIG$v)
86
nop
97
)
108
)

tests/compiler/assert.untouched.wat

Lines changed: 2 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,9 @@
11
(module
2-
(type $FUNCSIG$viiii (func (param i32 i32 i32 i32)))
32
(type $FUNCSIG$v (func))
4-
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
5-
(memory $0 1)
6-
(data (i32.const 8) "\12\00\00\00\01\00\00\00\01\00\00\00\12\00\00\00a\00s\00s\00e\00r\00t\00.\00t\00s\00")
7-
(data (i32.const 48) "\18\00\00\00\01\00\00\00\01\00\00\00\18\00\00\00m\00u\00s\00t\00 \00b\00e\00 \00t\00r\00u\00e\00")
3+
(memory $0 0)
84
(table $0 1 funcref)
95
(elem (i32.const 0) $null)
106
(export "memory" (memory $0))
11-
(start $start)
12-
(func $start:assert (; 1 ;) (type $FUNCSIG$v)
13-
(local $0 i32)
14-
i32.const 1
15-
i32.eqz
16-
if
17-
i32.const 0
18-
i32.const 24
19-
i32.const 1
20-
i32.const 0
21-
call $~lib/builtins/abort
22-
unreachable
23-
end
24-
i32.const 1
25-
i32.eqz
26-
if
27-
i32.const 0
28-
i32.const 24
29-
i32.const 2
30-
i32.const 0
31-
call $~lib/builtins/abort
32-
unreachable
33-
end
34-
i32.const 1
35-
i32.const 0
36-
i32.gt_u
37-
i32.eqz
38-
if
39-
i32.const 0
40-
i32.const 24
41-
i32.const 3
42-
i32.const 0
43-
call $~lib/builtins/abort
44-
unreachable
45-
end
46-
f64.const 0.5
47-
f64.const 0
48-
f64.eq
49-
if
50-
i32.const 0
51-
i32.const 24
52-
i32.const 4
53-
i32.const 0
54-
call $~lib/builtins/abort
55-
unreachable
56-
end
57-
f64.const 0.5
58-
f64.const 0.4
59-
f64.gt
60-
i32.eqz
61-
if
62-
i32.const 0
63-
i32.const 24
64-
i32.const 5
65-
i32.const 0
66-
call $~lib/builtins/abort
67-
unreachable
68-
end
69-
i64.const 4294967296
70-
i64.eqz
71-
if
72-
i32.const 0
73-
i32.const 24
74-
i32.const 6
75-
i32.const 0
76-
call $~lib/builtins/abort
77-
unreachable
78-
end
79-
i64.const 4294967296
80-
i64.const 1
81-
i64.gt_s
82-
i32.eqz
83-
if
84-
i32.const 0
85-
i32.const 24
86-
i32.const 7
87-
i32.const 0
88-
call $~lib/builtins/abort
89-
unreachable
90-
end
91-
i32.const 1
92-
local.tee $0
93-
if (result i32)
94-
local.get $0
95-
else
96-
i32.const 64
97-
i32.const 24
98-
i32.const 10
99-
i32.const 5
100-
call $~lib/builtins/abort
101-
unreachable
102-
end
103-
i32.eqz
104-
if
105-
unreachable
106-
end
107-
)
108-
(func $start (; 2 ;) (type $FUNCSIG$v)
109-
call $start:assert
110-
)
111-
(func $null (; 3 ;) (type $FUNCSIG$v)
7+
(func $null (; 0 ;) (type $FUNCSIG$v)
1128
)
1139
)

0 commit comments

Comments
 (0)