Skip to content

Commit 677808a

Browse files
authored
Fix AvoidReinterprets on reinterpreted loads of fewer than the full size (#2123)
* fix * fix style
1 parent d6a6188 commit 677808a

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/passes/AvoidReinterprets.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727

2828
namespace wasm {
2929

30+
static bool canReplaceWithReinterpret(Load* load) {
31+
// We can replace a full-size load with a valid pointer with
32+
// a reinterpret of the same address. A partial load would see
33+
// more bytes and possibly invalid data, and an unreachable
34+
// pointer is just not interesting to handle.
35+
return load->type != unreachable && load->bytes == getTypeSize(load->type);
36+
}
37+
3038
static Load* getSingleLoad(LocalGraph* localGraph, GetLocal* get) {
3139
std::set<GetLocal*> seen;
3240
seen.insert(get);
@@ -104,7 +112,7 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {
104112
for (auto& pair : infos) {
105113
auto* load = pair.first;
106114
auto& info = pair.second;
107-
if (info.reinterpreted && load->type != unreachable) {
115+
if (info.reinterpreted && canReplaceWithReinterpret(load)) {
108116
// We should use another load here, to avoid reinterprets.
109117
info.ptrLocal = Builder::addVar(func, i32);
110118
info.reinterpretedLocal =
@@ -131,8 +139,10 @@ struct AvoidReinterprets : public WalkerPass<PostWalker<AvoidReinterprets>> {
131139
if (isReinterpret(curr)) {
132140
auto* value = Properties::getFallthrough(curr->value);
133141
if (auto* load = value->dynCast<Load>()) {
134-
// A reinterpret of a load - flip it right here.
135-
replaceCurrent(makeReinterpretedLoad(load, load->ptr));
142+
// A reinterpret of a load - flip it right here if we can.
143+
if (canReplaceWithReinterpret(load)) {
144+
replaceCurrent(makeReinterpretedLoad(load, load->ptr));
145+
}
136146
} else if (auto* get = value->dynCast<GetLocal>()) {
137147
if (auto* load = getSingleLoad(localGraph, get)) {
138148
auto iter = infos.find(load);

test/passes/avoid-reinterprets.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(module
22
(type $0 (func))
3+
(type $1 (func (result f32)))
34
(memory $0 1)
45
(func $simple (; 0 ;) (type $0)
56
(drop
@@ -148,4 +149,18 @@
148149
(local.get $3)
149150
)
150151
)
152+
(func $partial1 (; 6 ;) (type $1) (result f32)
153+
(f32.reinterpret_i32
154+
(i32.load16_u
155+
(i32.const 3)
156+
)
157+
)
158+
)
159+
(func $partial2 (; 7 ;) (type $1) (result f32)
160+
(f32.reinterpret_i32
161+
(i32.load8_u
162+
(i32.const 3)
163+
)
164+
)
165+
)
151166
)

test/passes/avoid-reinterprets.wast

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,18 @@
3535
(local.set $y (local.get $x))
3636
(drop (f32.reinterpret_i32 (local.get $y)))
3737
)
38+
(func $partial1 (result f32)
39+
(f32.reinterpret_i32
40+
(i32.load16_u
41+
(i32.const 3)
42+
)
43+
)
44+
)
45+
(func $partial2 (result f32)
46+
(f32.reinterpret_i32
47+
(i32.load8_u
48+
(i32.const 3)
49+
)
50+
)
51+
)
3852
)

0 commit comments

Comments
 (0)