Skip to content

Commit 45f8fed

Browse files
committed
fix Const finalization and use it properly in br finalization in Precompute
1 parent 90b9b94 commit 45f8fed

5 files changed

Lines changed: 77 additions & 1 deletion

File tree

src/passes/Precompute.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ struct Precompute : public WalkerPass<PostWalker<Precompute, UnifiedExpressionVi
100100
if (ret->value) {
101101
if (auto* value = ret->value->dynCast<Const>()) {
102102
value->value = flow.value;
103+
value->finalize();
103104
return;
104105
}
105106
}
@@ -117,19 +118,21 @@ struct Precompute : public WalkerPass<PostWalker<Precompute, UnifiedExpressionVi
117118
if (auto* br = curr->dynCast<Break>()) {
118119
br->name = flow.breakTo;
119120
br->condition = nullptr;
120-
br->finalize(); // if we removed a condition, the type may change
121121
if (flow.value.type != none) {
122122
// reuse a const value if there is one
123123
if (br->value) {
124124
if (auto* value = br->value->dynCast<Const>()) {
125125
value->value = flow.value;
126+
value->finalize();
127+
br->finalize();
126128
return;
127129
}
128130
}
129131
br->value = Builder(*getModule()).makeConst(flow.value);
130132
} else {
131133
br->value = nullptr;
132134
}
135+
br->finalize();
133136
} else {
134137
Builder builder(*getModule());
135138
replaceCurrent(builder.makeBreak(flow.breakTo, flow.value.type != none ? builder.makeConst(flow.value) : nullptr));

src/wasm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ class Const : public SpecificExpression<Expression::ConstId> {
464464
Literal value;
465465

466466
Const* set(Literal value_);
467+
468+
void finalize();
467469
};
468470

469471
class Unary : public SpecificExpression<Expression::UnaryId> {

src/wasm/wasm.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ Const* Const::set(Literal value_) {
371371
return this;
372372
}
373373

374+
void Const::finalize() {
375+
type = value.type;
376+
}
377+
374378
bool Unary::isRelational() {
375379
return op == EqZInt32 || op == EqZInt64;
376380
}

test/passes/precompute.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(type $0 (func (param i32)))
33
(type $1 (func (result i32)))
44
(type $2 (func))
5+
(type $3 (func (result f64)))
56
(memory $0 0)
67
(func $x (type $0) (param $x i32)
78
(call $x
@@ -129,4 +130,31 @@
129130
)
130131
)
131132
)
133+
(func $reuse-br-value (type $3) (result f64)
134+
(block $label$0 (result f64)
135+
(i32.store8
136+
(i32.const 1919623207)
137+
(if (result i32)
138+
(i32.const 1)
139+
(block $label$2
140+
(drop
141+
(i64.and
142+
(i64.trunc_u/f32
143+
(f32.const 70847791997969805621592064)
144+
)
145+
(i64.const 729618461987467893)
146+
)
147+
)
148+
(br $label$0
149+
(f64.const 6.134856208230095e-154)
150+
)
151+
)
152+
(i32.load offset=3 align=2
153+
(i32.const 169901344)
154+
)
155+
)
156+
)
157+
(f64.const 4776014875438170098655851e156)
158+
)
159+
)
132160
)

test/passes/precompute.wast

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,43 @@
210210
)
211211
)
212212
)
213+
(func $reuse-br-value (result f64)
214+
(block $label$0 (result f64)
215+
(i32.store8
216+
(i32.const 1919623207)
217+
(if (result i32)
218+
(i32.const 1)
219+
(block $label$2 (result i32)
220+
(drop
221+
(i64.and
222+
(i64.trunc_u/f32
223+
(f32.const 70847791997969805621592064)
224+
)
225+
(i64.const 729618461987467893)
226+
)
227+
)
228+
(br_if $label$2
229+
(i32.const 2049535349)
230+
(f32.eq
231+
(f32.demote/f64
232+
(f64.mul
233+
(br_if $label$0 ;; this br is optimized, and br *and* values reused
234+
(f64.const 6.134856208230095e-154)
235+
(i32.const 690910817)
236+
)
237+
(f64.const 1.515470884183969e-152)
238+
)
239+
)
240+
(f32.const 66524025679377434935296)
241+
)
242+
)
243+
)
244+
(i32.load offset=3 align=2
245+
(i32.const 169901344)
246+
)
247+
)
248+
)
249+
(f64.const 4776014875438170098655851e156)
250+
)
251+
)
213252
)

0 commit comments

Comments
 (0)