Skip to content

Commit edf001f

Browse files
authored
Fix unreachable prefix in instruction printing (#2265)
When a memory instruction's type is unreachable, i.e., one of its child expressions is unreachable, the instruction will be printed like `unreachable.load`, which is invalid text format. This prints unreachable prefix instruction types as `i32` to just make them pass the parser. It is OK because they are not reachable anyway. Also this removes printing of `?` in atomic.rmw instruction printing.
1 parent ccd95f8 commit edf001f

8 files changed

Lines changed: 86 additions & 11 deletions

src/passes/Print.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ static Name printableLocal(Index index, Function* func) {
5555
return name;
5656
}
5757

58+
// Printing "unreachable" as a instruction prefix type is not valid in wasm text
59+
// format. Print something else to make it pass.
60+
static Type forceConcrete(Type type) {
61+
return isConcreteType(type) ? type : i32;
62+
}
63+
5864
// Prints the internal contents of an expression: everything but
5965
// the children.
6066
struct PrintExpressionContents
@@ -141,7 +147,7 @@ struct PrintExpressionContents
141147
printName(curr->name, o);
142148
}
143149
void visitLoad(Load* curr) {
144-
prepareColor(o) << printType(curr->type);
150+
prepareColor(o) << printType(forceConcrete(curr->type));
145151
if (curr->isAtomic) {
146152
o << ".atomic";
147153
}
@@ -167,7 +173,7 @@ struct PrintExpressionContents
167173
}
168174
}
169175
void visitStore(Store* curr) {
170-
prepareColor(o) << printType(curr->valueType);
176+
prepareColor(o) << printType(forceConcrete(curr->valueType));
171177
if (curr->isAtomic) {
172178
o << ".atomic";
173179
}
@@ -192,10 +198,8 @@ struct PrintExpressionContents
192198
}
193199
}
194200
static void printRMWSize(std::ostream& o, Type type, uint8_t bytes) {
195-
prepareColor(o) << printType(type) << ".atomic.rmw";
196-
if (type == unreachable) {
197-
o << '?';
198-
} else if (bytes != getTypeSize(type)) {
201+
prepareColor(o) << printType(forceConcrete(type)) << ".atomic.rmw";
202+
if (type != unreachable && bytes != getTypeSize(type)) {
199203
if (bytes == 1) {
200204
o << '8';
201205
} else if (bytes == 2) {
@@ -253,7 +257,7 @@ struct PrintExpressionContents
253257
}
254258
void visitAtomicWait(AtomicWait* curr) {
255259
prepareColor(o);
256-
o << printType(curr->expectedType) << ".atomic.wait";
260+
o << printType(forceConcrete(curr->expectedType)) << ".atomic.wait";
257261
if (curr->offset) {
258262
o << " offset=" << curr->offset;
259263
}

test/passes/alignment-lowering.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@
437437
)
438438
)
439439
(drop
440-
(unreachable.load offset=100
440+
(i32.load offset=100
441441
(unreachable)
442442
)
443443
)

test/passes/flatten.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@
679679
(drop
680680
(unreachable)
681681
)
682-
(unreachable.load
682+
(i32.load
683683
(unreachable)
684684
)
685685
(unreachable)
@@ -1183,7 +1183,7 @@
11831183
(i32.const 22)
11841184
(block
11851185
(unreachable)
1186-
(unreachable.load
1186+
(i32.load
11871187
(unreachable)
11881188
)
11891189
(drop

test/passes/remove-unused-brs.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@
984984
(i32.const 607395945)
985985
)
986986
(br_if $label$1
987-
(unreachable.load offset=3 align=1
987+
(i32.load offset=3 align=1
988988
(select
989989
(call $untaken-brs-might-prevent-block-removal
990990
(f32.const 1.4904844647389837e-07)

test/unreachable-instr-type.wast

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(module
2+
(memory (shared 1 1))
3+
(func $test
4+
(f32.load (unreachable))
5+
6+
(f32.store
7+
(unreachable)
8+
(f32.const 0)
9+
)
10+
11+
(i64.atomic.rmw.add
12+
(unreachable)
13+
(i64.const 0)
14+
)
15+
16+
(i64.atomic.rmw.cmpxchg
17+
(unreachable)
18+
(i64.const 0)
19+
(i64.const 1)
20+
)
21+
22+
(i64.atomic.wait
23+
(unreachable)
24+
(i64.const 0)
25+
(i64.const 0)
26+
)
27+
)
28+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(module
2+
(type $FUNCSIG$v (func))
3+
(memory $0 (shared 1 1))
4+
(func $test (; 0 ;) (type $FUNCSIG$v)
5+
(i32.load
6+
(unreachable)
7+
)
8+
(f32.store
9+
(unreachable)
10+
(f32.const 0)
11+
)
12+
(i32.atomic.rmw.add
13+
(unreachable)
14+
(i64.const 0)
15+
)
16+
(i32.atomic.rmw.cmpxchg
17+
(unreachable)
18+
(i64.const 0)
19+
(i64.const 1)
20+
)
21+
(i64.atomic.wait
22+
(unreachable)
23+
(i64.const 0)
24+
(i64.const 0)
25+
)
26+
)
27+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(module
2+
(type $0 (func))
3+
(memory $0 (shared 1 1))
4+
(func $test (; 0 ;) (type $0)
5+
(unreachable)
6+
)
7+
)
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(module
2+
(type $0 (func))
3+
(memory $0 (shared 1 1))
4+
(func $0 (; 0 ;) (type $0)
5+
(unreachable)
6+
)
7+
)
8+

0 commit comments

Comments
 (0)