Skip to content

Commit dcdaa5d

Browse files
authored
Fix if copying (#1278)
* fix if copying - we should preserve the forced explicit type if there is one, and not just infer it from the arms. this adds a builder method for makeIf that receives a type to apply to the if, and for blocks a method that makes a block from a list, also with a variant with a provided type
1 parent 27474b7 commit dcdaa5d

File tree

4 files changed

+96
-6
lines changed

4 files changed

+96
-6
lines changed

src/ir/ExpressionManipulator.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,14 @@ Expression* flexibleCopy(Expression* original, Module& wasm, CustomCopier custom
3838
}
3939

4040
Expression* visitBlock(Block *curr) {
41-
auto* ret = builder.makeBlock();
41+
ExpressionList list(wasm.allocator);
4242
for (Index i = 0; i < curr->list.size(); i++) {
43-
ret->list.push_back(copy(curr->list[i]));
43+
list.push_back(copy(curr->list[i]));
4444
}
45-
ret->name = curr->name;
46-
ret->finalize(curr->type);
47-
return ret;
45+
return builder.makeBlock(curr->name, list, curr->type);
4846
}
4947
Expression* visitIf(If *curr) {
50-
return builder.makeIf(copy(curr->condition), copy(curr->ifTrue), copy(curr->ifFalse));
48+
return builder.makeIf(copy(curr->condition), copy(curr->ifTrue), copy(curr->ifFalse), curr->type);
5149
}
5250
Expression* visitLoop(Loop *curr) {
5351
return builder.makeLoop(curr->name, copy(curr->body));

src/wasm-builder.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,44 @@ class Builder {
9090
ret->finalize();
9191
return ret;
9292
}
93+
Block* makeBlock(const ExpressionList& items) {
94+
auto* ret = allocator.alloc<Block>();
95+
ret->list.set(items);
96+
ret->finalize();
97+
return ret;
98+
}
99+
Block* makeBlock(const ExpressionList& items, WasmType type) {
100+
auto* ret = allocator.alloc<Block>();
101+
ret->list.set(items);
102+
ret->finalize(type);
103+
return ret;
104+
}
105+
Block* makeBlock(Name name, const ExpressionList& items) {
106+
auto* ret = allocator.alloc<Block>();
107+
ret->name = name;
108+
ret->list.set(items);
109+
ret->finalize();
110+
return ret;
111+
}
112+
Block* makeBlock(Name name, const ExpressionList& items, WasmType type) {
113+
auto* ret = allocator.alloc<Block>();
114+
ret->name = name;
115+
ret->list.set(items);
116+
ret->finalize(type);
117+
return ret;
118+
}
93119
If* makeIf(Expression* condition, Expression* ifTrue, Expression* ifFalse = nullptr) {
94120
auto* ret = allocator.alloc<If>();
95121
ret->condition = condition; ret->ifTrue = ifTrue; ret->ifFalse = ifFalse;
96122
ret->finalize();
97123
return ret;
98124
}
125+
If* makeIf(Expression* condition, Expression* ifTrue, Expression* ifFalse, WasmType type) {
126+
auto* ret = allocator.alloc<If>();
127+
ret->condition = condition; ret->ifTrue = ifTrue; ret->ifFalse = ifFalse;
128+
ret->finalize(type);
129+
return ret;
130+
}
99131
Loop* makeLoop(Name name, Expression* body) {
100132
auto* ret = allocator.alloc<Loop>();
101133
ret->name = name; ret->body = body;

test/passes/inlining.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,34 @@
218218
)
219219
)
220220
)
221+
(module
222+
(type $T (func (param i32)))
223+
(type $1 (func))
224+
(table 10 anyfunc)
225+
(memory $0 0)
226+
(func $0 (; 0 ;) (type $1)
227+
(block $__inlined_func$1
228+
(call_indirect $T
229+
(if (result i32)
230+
(i32.const 0)
231+
(unreachable)
232+
(unreachable)
233+
)
234+
(i32.const 1)
235+
)
236+
)
237+
)
238+
)
239+
(module
240+
(type $0 (func))
241+
(memory $0 0)
242+
(func $1 (; 0 ;) (type $0)
243+
(block $__inlined_func$0
244+
(block $label$1
245+
(br_table $label$1 $label$1
246+
(i32.const 0)
247+
)
248+
)
249+
)
250+
)
251+
)

test/passes/inlining.wast

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,33 @@
145145
)
146146
)
147147
)
148+
(module
149+
(type $T (func (param i32)))
150+
(table 10 anyfunc)
151+
(func $0
152+
(call $1)
153+
)
154+
(func $1
155+
(call_indirect $T
156+
(if (result i32) ;; if copy must preserve the forced type
157+
(i32.const 0)
158+
(unreachable)
159+
(unreachable)
160+
)
161+
(i32.const 1)
162+
)
163+
)
164+
)
165+
(module
166+
(func $0
167+
(block $label$1 ;; copy this name
168+
(br_table $label$1 $label$1
169+
(i32.const 0)
170+
)
171+
)
172+
)
173+
(func $1
174+
(call $0)
175+
)
176+
)
148177

0 commit comments

Comments
 (0)