Skip to content

Commit a88f8f2

Browse files
authored
finalize blocks properly in relooper (WebAssembly#600)
1 parent ed4e614 commit a88f8f2

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

src/cfg/Relooper.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ wasm::Expression* Branch::Render(RelooperBuilder& Builder, Block *Target, bool S
5555
Ret->list.push_back(Builder.makeContinue(Ancestor->Id));
5656
}
5757
}
58+
Ret->finalize();
5859
return Ret;
5960
}
6061

@@ -85,7 +86,10 @@ wasm::Expression* Block::Render(RelooperBuilder& Builder, bool InLoop) {
8586
}
8687
if (Code) Ret->list.push_back(Code);
8788

88-
if (!ProcessedBranchesOut.size()) return Ret;
89+
if (!ProcessedBranchesOut.size()) {
90+
Ret->finalize();
91+
return Ret;
92+
}
8993

9094
bool SetLabel = true; // in some cases it is clear we can avoid setting label, see later
9195
bool ForceSetLabel = Shape::IsEmulated(Parent) != nullptr;
@@ -219,6 +223,8 @@ wasm::Expression* Block::Render(RelooperBuilder& Builder, bool InLoop) {
219223
Ret->list.push_back(Root);
220224
}
221225

226+
Ret->finalize();
227+
222228
return Ret;
223229
}
224230

test/example/c-api-kitchen-sink.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,17 @@ void test_relooper() {
378378
BinaryenFunctionRef sinker = BinaryenAddFunction(module, "nontrivial-loop-plus-phi-to-head", v, localTypes, 1, body);
379379
}
380380

381+
BinaryenFunctionTypeRef i = BinaryenAddFunctionType(module, "i", BinaryenInt32(), NULL, 0);
382+
383+
{ // return in a block
384+
RelooperRef relooper = RelooperCreate();
385+
BinaryenExpressionRef listList[] = { makeInt32(module, 42), BinaryenReturn(module, makeInt32(module, 1337)) };
386+
BinaryenExpressionRef list = BinaryenBlock(module, "the-list", listList, 2);
387+
RelooperBlockRef block = RelooperAddBlock(relooper, list);
388+
BinaryenExpressionRef body = RelooperRenderAndDispose(relooper, block, 0, module);
389+
BinaryenFunctionRef sinker = BinaryenAddFunction(module, "return", i, localTypes, 1, body);
390+
}
391+
381392
assert(BinaryenModuleValidate(module));
382393

383394
printf("raw:\n");

test/example/c-api-kitchen-sink.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ raw:
370370
(module
371371
(memory 0)
372372
(type $v (func))
373+
(type $i (func (result i32)))
373374
(func $just-one-block (type $v)
374375
(local $0 i32)
375376
(i32.const 1337)
@@ -630,14 +631,27 @@ raw:
630631
)
631632
)
632633
)
634+
(func $return (type $i) (result i32)
635+
(local $0 i32)
636+
(block $the-list
637+
(i32.const 42)
638+
(return
639+
(i32.const 1337)
640+
)
641+
)
642+
)
633643
)
634644
optimized:
635645
(module
636646
(memory 0)
637647
(type $v (func))
648+
(type $i (func (result i32)))
638649
(func $just-one-block (type $v)
639650
(nop)
640651
)
652+
(func $return (type $i) (result i32)
653+
(i32.const 1337)
654+
)
641655
)
642656
module loaded from binary form:
643657
(module

0 commit comments

Comments
 (0)