Skip to content

Commit 3aaaeb2

Browse files
authored
Revert "Remove bulk memory instructions refering to active segments (#2235)"
This reverts commit 72c52ea.
1 parent 72c52ea commit 3aaaeb2

File tree

5 files changed

+13
-106
lines changed

5 files changed

+13
-106
lines changed

src/ir/manipulation.h

-5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ template<typename InputType> inline Nop* nop(InputType* target) {
3838
return convert<InputType, Nop>(target);
3939
}
4040

41-
template<typename InputType>
42-
inline Unreachable* unreachable(InputType* target) {
43-
return convert<InputType, Unreachable>(target);
44-
}
45-
4641
// Convert a node that allocates
4742
template<typename InputType, typename OutputType>
4843
inline OutputType* convert(InputType* input, MixedArena& allocator) {

src/passes/MemoryPacking.cpp

+4-40
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include "ir/manipulation.h"
18-
#include "ir/utils.h"
1917
#include "pass.h"
2018
#include "wasm-binary.h"
2119
#include "wasm-builder.h"
@@ -34,13 +32,11 @@ struct MemoryPacking : public Pass {
3432
return;
3533
}
3634

35+
// Conservatively refuse to change segments if any are passive to avoid
36+
// invalidating segment indices or segment contents referenced from
37+
// memory.init instructions.
38+
// TODO: optimize in the presence of memory.init instructions
3739
if (module->features.hasBulkMemory()) {
38-
// Remove any references to active segments that might be invalidated.
39-
optimizeTrappingBulkMemoryOps(runner, module);
40-
// Conservatively refuse to change segments if any are passive to avoid
41-
// invalidating segment indices or segment contents referenced from
42-
// memory.init and data.drop instructions.
43-
// TODO: optimize in the presence of memory.init and data.drop
4440
for (auto segment : module->memory.segments) {
4541
if (segment.isPassive) {
4642
return;
@@ -124,38 +120,6 @@ struct MemoryPacking : public Pass {
124120
}
125121
module->memory.segments.swap(packed);
126122
}
127-
128-
void optimizeTrappingBulkMemoryOps(PassRunner* runner, Module* module) {
129-
struct Trapper : WalkerPass<PostWalker<Trapper>> {
130-
bool changed;
131-
void visitMemoryInit(MemoryInit* curr) {
132-
if (!getModule()->memory.segments[curr->segment].isPassive) {
133-
Builder builder(*getModule());
134-
replaceCurrent(builder.blockify(builder.makeDrop(curr->dest),
135-
builder.makeDrop(curr->offset),
136-
builder.makeDrop(curr->size),
137-
builder.makeUnreachable()));
138-
changed = true;
139-
}
140-
}
141-
void visitDataDrop(DataDrop* curr) {
142-
if (!getModule()->memory.segments[curr->segment].isPassive) {
143-
ExpressionManipulator::unreachable(curr);
144-
changed = true;
145-
}
146-
}
147-
void walkFunction(Function* func) {
148-
changed = false;
149-
PostWalker<Trapper>::walkFunction(func);
150-
if (changed) {
151-
ReFinalize().walkFunctionInModule(func, getModule());
152-
}
153-
}
154-
bool isFunctionParallel() override { return true; }
155-
Pass* create() override { return new Trapper; }
156-
} trapper;
157-
trapper.run(runner, module);
158-
}
159123
};
160124

161125
Pass* createMemoryPackingPass() { return new MemoryPacking(); }

src/wasm/wasm-validator.cpp

+8-14
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,8 @@ void FunctionValidator::visitSIMDShift(SIMDShift* curr) {
970970
}
971971

972972
void FunctionValidator::visitMemoryInit(MemoryInit* curr) {
973+
shouldBeTrue(
974+
getModule()->memory.exists, curr, "Memory operations require a memory");
973975
shouldBeTrue(getModule()->features.hasBulkMemory(),
974976
curr,
975977
"Bulk memory operation (bulk memory is disabled)");
@@ -981,33 +983,27 @@ void FunctionValidator::visitMemoryInit(MemoryInit* curr) {
981983
curr->offset->type, i32, curr, "memory.init offset must be an i32");
982984
shouldBeEqualOrFirstIsUnreachable(
983985
curr->size->type, i32, curr, "memory.init size must be an i32");
984-
if (!shouldBeTrue(getModule()->memory.exists,
985-
curr,
986-
"Memory operations require a memory")) {
987-
return;
988-
}
989986
shouldBeTrue(curr->segment < getModule()->memory.segments.size(),
990987
curr,
991988
"memory.init segment index out of bounds");
992989
}
993990

994991
void FunctionValidator::visitDataDrop(DataDrop* curr) {
992+
shouldBeTrue(
993+
getModule()->memory.exists, curr, "Memory operations require a memory");
995994
shouldBeTrue(getModule()->features.hasBulkMemory(),
996995
curr,
997996
"Bulk memory operation (bulk memory is disabled)");
998997
shouldBeEqualOrFirstIsUnreachable(
999998
curr->type, none, curr, "data.drop must have type none");
1000-
if (!shouldBeTrue(getModule()->memory.exists,
1001-
curr,
1002-
"Memory operations require a memory")) {
1003-
return;
1004-
}
1005999
shouldBeTrue(curr->segment < getModule()->memory.segments.size(),
10061000
curr,
10071001
"data.drop segment index out of bounds");
10081002
}
10091003

10101004
void FunctionValidator::visitMemoryCopy(MemoryCopy* curr) {
1005+
shouldBeTrue(
1006+
getModule()->memory.exists, curr, "Memory operations require a memory");
10111007
shouldBeTrue(getModule()->features.hasBulkMemory(),
10121008
curr,
10131009
"Bulk memory operation (bulk memory is disabled)");
@@ -1019,11 +1015,11 @@ void FunctionValidator::visitMemoryCopy(MemoryCopy* curr) {
10191015
curr->source->type, i32, curr, "memory.copy source must be an i32");
10201016
shouldBeEqualOrFirstIsUnreachable(
10211017
curr->size->type, i32, curr, "memory.copy size must be an i32");
1022-
shouldBeTrue(
1023-
getModule()->memory.exists, curr, "Memory operations require a memory");
10241018
}
10251019

10261020
void FunctionValidator::visitMemoryFill(MemoryFill* curr) {
1021+
shouldBeTrue(
1022+
getModule()->memory.exists, curr, "Memory operations require a memory");
10271023
shouldBeTrue(getModule()->features.hasBulkMemory(),
10281024
curr,
10291025
"Bulk memory operation (bulk memory is disabled)");
@@ -1035,8 +1031,6 @@ void FunctionValidator::visitMemoryFill(MemoryFill* curr) {
10351031
curr->value->type, i32, curr, "memory.fill value must be an i32");
10361032
shouldBeEqualOrFirstIsUnreachable(
10371033
curr->size->type, i32, curr, "memory.fill size must be an i32");
1038-
shouldBeTrue(
1039-
getModule()->memory.exists, curr, "Memory operations require a memory");
10401034
}
10411035

10421036
void FunctionValidator::validateMemBytes(uint8_t bytes,

test/passes/memory-packing_all-features.txt renamed to test/passes/memory-packing.txt

-27
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,3 @@
1818
(import "env" "memory" (memory $0 2048 2048))
1919
(import "env" "memoryBase" (global $memoryBase i32))
2020
)
21-
(module
22-
(type $FUNCSIG$v (func))
23-
(memory $0 1 1)
24-
(func $foo (; 0 ;) (type $FUNCSIG$v)
25-
(block
26-
(drop
27-
(i32.const 0)
28-
)
29-
(drop
30-
(i32.const 0)
31-
)
32-
(drop
33-
(i32.const 0)
34-
)
35-
(unreachable)
36-
)
37-
(unreachable)
38-
)
39-
(func $bar (; 1 ;) (type $FUNCSIG$v)
40-
(drop
41-
(loop $loop-in (result i32)
42-
(unreachable)
43-
(i32.const 42)
44-
)
45-
)
46-
)
47-
)

test/passes/memory-packing_all-features.wast renamed to test/passes/memory-packing.wast

+1-20
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,4 @@
1717
(import "env" "memoryBase" (global $memoryBase i32))
1818
(data (i32.const 4066) "") ;; empty
1919
)
20-
(module
21-
(memory $0 1 1)
22-
(data (i32.const 0) "")
23-
(func $foo
24-
(memory.init 0
25-
(i32.const 0)
26-
(i32.const 0)
27-
(i32.const 0)
28-
)
29-
(data.drop 0)
30-
)
31-
(func $bar
32-
(drop
33-
(loop (result i32)
34-
(data.drop 0)
35-
(i32.const 42)
36-
)
37-
)
38-
)
39-
)
20+

0 commit comments

Comments
 (0)