Skip to content

Commit 4c55e49

Browse files
authored
Fix stackAlloc runtime function generation for wasm backend (#1348)
It was returning the top of the allocated space rather than the bottom. Fix taken from @tbfleming in emscripten-core/emscripten#5974
1 parent bbcb50d commit 4c55e49

File tree

88 files changed

+786
-875
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+786
-875
lines changed

src/wasm-emscripten.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,16 @@ void EmscriptenGlueGenerator::generateStackAllocFunction() {
8383
name, std::move(params), i32, { { "1", i32 } }
8484
);
8585
Load* loadStack = generateLoadStackPointer();
86-
SetLocal* setStackLocal = builder.makeSetLocal(1, loadStack);
87-
GetLocal* getStackLocal = builder.makeGetLocal(1, i32);
8886
GetLocal* getSizeArg = builder.makeGetLocal(0, i32);
89-
Binary* sub = builder.makeBinary(SubInt32, getStackLocal, getSizeArg);
87+
Binary* sub = builder.makeBinary(SubInt32, loadStack, getSizeArg);
9088
const static uint32_t bitAlignment = 16;
9189
const static uint32_t bitMask = bitAlignment - 1;
9290
Const* subConst = builder.makeConst(Literal(~bitMask));
9391
Binary* maskedSub = builder.makeBinary(AndInt32, sub, subConst);
94-
Store* storeStack = generateStoreStackPointer(maskedSub);
92+
SetLocal* teeStackLocal = builder.makeTeeLocal(1, maskedSub);
93+
Store* storeStack = generateStoreStackPointer(teeStackLocal);
9594

9695
Block* block = builder.makeBlock();
97-
block->list.push_back(setStackLocal);
9896
block->list.push_back(storeStack);
9997
GetLocal* getStackLocal2 = builder.makeGetLocal(1, i32);
10098
block->list.push_back(getStackLocal2);

test/dot_s/alias.wast

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,18 @@
3838
)
3939
(func $stackAlloc (; 4 ;) (param $0 i32) (result i32)
4040
(local $1 i32)
41-
(set_local $1
42-
(i32.load offset=4
43-
(i32.const 0)
44-
)
45-
)
4641
(i32.store offset=4
4742
(i32.const 0)
48-
(i32.and
49-
(i32.sub
50-
(get_local $1)
51-
(get_local $0)
43+
(tee_local $1
44+
(i32.and
45+
(i32.sub
46+
(i32.load offset=4
47+
(i32.const 0)
48+
)
49+
(get_local $0)
50+
)
51+
(i32.const -16)
5252
)
53-
(i32.const -16)
5453
)
5554
)
5655
(get_local $1)

test/dot_s/alternate-lcomm.wast

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,18 @@
1111
)
1212
(func $stackAlloc (; 1 ;) (param $0 i32) (result i32)
1313
(local $1 i32)
14-
(set_local $1
15-
(i32.load offset=4
16-
(i32.const 0)
17-
)
18-
)
1914
(i32.store offset=4
2015
(i32.const 0)
21-
(i32.and
22-
(i32.sub
23-
(get_local $1)
24-
(get_local $0)
16+
(tee_local $1
17+
(i32.and
18+
(i32.sub
19+
(i32.load offset=4
20+
(i32.const 0)
21+
)
22+
(get_local $0)
23+
)
24+
(i32.const -16)
2525
)
26-
(i32.const -16)
2726
)
2827
)
2928
(get_local $1)

test/dot_s/asm_const.wast

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,18 @@
2323
)
2424
(func $stackAlloc (; 3 ;) (param $0 i32) (result i32)
2525
(local $1 i32)
26-
(set_local $1
27-
(i32.load offset=4
28-
(i32.const 0)
29-
)
30-
)
3126
(i32.store offset=4
3227
(i32.const 0)
33-
(i32.and
34-
(i32.sub
35-
(get_local $1)
36-
(get_local $0)
28+
(tee_local $1
29+
(i32.and
30+
(i32.sub
31+
(i32.load offset=4
32+
(i32.const 0)
33+
)
34+
(get_local $0)
35+
)
36+
(i32.const -16)
3737
)
38-
(i32.const -16)
3938
)
4039
)
4140
(get_local $1)

test/dot_s/basics.wast

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,18 @@
107107
)
108108
(func $stackAlloc (; 4 ;) (param $0 i32) (result i32)
109109
(local $1 i32)
110-
(set_local $1
111-
(i32.load offset=4
112-
(i32.const 0)
113-
)
114-
)
115110
(i32.store offset=4
116111
(i32.const 0)
117-
(i32.and
118-
(i32.sub
119-
(get_local $1)
120-
(get_local $0)
112+
(tee_local $1
113+
(i32.and
114+
(i32.sub
115+
(i32.load offset=4
116+
(i32.const 0)
117+
)
118+
(get_local $0)
119+
)
120+
(i32.const -16)
121121
)
122-
(i32.const -16)
123122
)
124123
)
125124
(get_local $1)

test/dot_s/bcp-1.wast

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -319,19 +319,18 @@
319319
)
320320
(func $stackAlloc (; 22 ;) (param $0 i32) (result i32)
321321
(local $1 i32)
322-
(set_local $1
323-
(i32.load offset=4
324-
(i32.const 0)
325-
)
326-
)
327322
(i32.store offset=4
328323
(i32.const 0)
329-
(i32.and
330-
(i32.sub
331-
(get_local $1)
332-
(get_local $0)
324+
(tee_local $1
325+
(i32.and
326+
(i32.sub
327+
(i32.load offset=4
328+
(i32.const 0)
329+
)
330+
(get_local $0)
331+
)
332+
(i32.const -16)
333333
)
334-
(i32.const -16)
335334
)
336335
)
337336
(get_local $1)

test/dot_s/data-offset-folding.wast

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@
1313
)
1414
(func $stackAlloc (; 1 ;) (param $0 i32) (result i32)
1515
(local $1 i32)
16-
(set_local $1
17-
(i32.load offset=4
18-
(i32.const 0)
19-
)
20-
)
2116
(i32.store offset=4
2217
(i32.const 0)
23-
(i32.and
24-
(i32.sub
25-
(get_local $1)
26-
(get_local $0)
18+
(tee_local $1
19+
(i32.and
20+
(i32.sub
21+
(i32.load offset=4
22+
(i32.const 0)
23+
)
24+
(get_local $0)
25+
)
26+
(i32.const -16)
2727
)
28-
(i32.const -16)
2928
)
3029
)
3130
(get_local $1)

test/dot_s/debug.wast

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,18 @@
6464
)
6565
(func $stackAlloc (; 2 ;) (param $0 i32) (result i32)
6666
(local $1 i32)
67-
(set_local $1
68-
(i32.load offset=4
69-
(i32.const 0)
70-
)
71-
)
7267
(i32.store offset=4
7368
(i32.const 0)
74-
(i32.and
75-
(i32.sub
76-
(get_local $1)
77-
(get_local $0)
69+
(tee_local $1
70+
(i32.and
71+
(i32.sub
72+
(i32.load offset=4
73+
(i32.const 0)
74+
)
75+
(get_local $0)
76+
)
77+
(i32.const -16)
7878
)
79-
(i32.const -16)
8079
)
8180
)
8281
(get_local $1)

test/dot_s/dso_handle.wast

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@
1717
)
1818
(func $stackAlloc (; 2 ;) (param $0 i32) (result i32)
1919
(local $1 i32)
20-
(set_local $1
21-
(i32.load offset=4
22-
(i32.const 0)
23-
)
24-
)
2520
(i32.store offset=4
2621
(i32.const 0)
27-
(i32.and
28-
(i32.sub
29-
(get_local $1)
30-
(get_local $0)
22+
(tee_local $1
23+
(i32.and
24+
(i32.sub
25+
(i32.load offset=4
26+
(i32.const 0)
27+
)
28+
(get_local $0)
29+
)
30+
(i32.const -16)
3131
)
32-
(i32.const -16)
3332
)
3433
)
3534
(get_local $1)

test/dot_s/dyncall.wast

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,18 @@
6060
)
6161
(func $stackAlloc (; 8 ;) (param $0 i32) (result i32)
6262
(local $1 i32)
63-
(set_local $1
64-
(i32.load offset=4
65-
(i32.const 0)
66-
)
67-
)
6863
(i32.store offset=4
6964
(i32.const 0)
70-
(i32.and
71-
(i32.sub
72-
(get_local $1)
73-
(get_local $0)
65+
(tee_local $1
66+
(i32.and
67+
(i32.sub
68+
(i32.load offset=4
69+
(i32.const 0)
70+
)
71+
(get_local $0)
72+
)
73+
(i32.const -16)
7474
)
75-
(i32.const -16)
7675
)
7776
)
7877
(get_local $1)

0 commit comments

Comments
 (0)