Skip to content

Commit

Permalink
finalize blocks properly in relooper (#600)
Browse files Browse the repository at this point in the history
  • Loading branch information
kripken authored Jun 24, 2016
1 parent ed4e614 commit a88f8f2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/cfg/Relooper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ wasm::Expression* Branch::Render(RelooperBuilder& Builder, Block *Target, bool S
Ret->list.push_back(Builder.makeContinue(Ancestor->Id));
}
}
Ret->finalize();
return Ret;
}

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

if (!ProcessedBranchesOut.size()) return Ret;
if (!ProcessedBranchesOut.size()) {
Ret->finalize();
return Ret;
}

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

Ret->finalize();

return Ret;
}

Expand Down
11 changes: 11 additions & 0 deletions test/example/c-api-kitchen-sink.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,17 @@ void test_relooper() {
BinaryenFunctionRef sinker = BinaryenAddFunction(module, "nontrivial-loop-plus-phi-to-head", v, localTypes, 1, body);
}

BinaryenFunctionTypeRef i = BinaryenAddFunctionType(module, "i", BinaryenInt32(), NULL, 0);

{ // return in a block
RelooperRef relooper = RelooperCreate();
BinaryenExpressionRef listList[] = { makeInt32(module, 42), BinaryenReturn(module, makeInt32(module, 1337)) };
BinaryenExpressionRef list = BinaryenBlock(module, "the-list", listList, 2);
RelooperBlockRef block = RelooperAddBlock(relooper, list);
BinaryenExpressionRef body = RelooperRenderAndDispose(relooper, block, 0, module);
BinaryenFunctionRef sinker = BinaryenAddFunction(module, "return", i, localTypes, 1, body);
}

assert(BinaryenModuleValidate(module));

printf("raw:\n");
Expand Down
14 changes: 14 additions & 0 deletions test/example/c-api-kitchen-sink.txt
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ raw:
(module
(memory 0)
(type $v (func))
(type $i (func (result i32)))
(func $just-one-block (type $v)
(local $0 i32)
(i32.const 1337)
Expand Down Expand Up @@ -630,14 +631,27 @@ raw:
)
)
)
(func $return (type $i) (result i32)
(local $0 i32)
(block $the-list
(i32.const 42)
(return
(i32.const 1337)
)
)
)
)
optimized:
(module
(memory 0)
(type $v (func))
(type $i (func (result i32)))
(func $just-one-block (type $v)
(nop)
)
(func $return (type $i) (result i32)
(i32.const 1337)
)
)
module loaded from binary form:
(module
Expand Down

0 comments on commit a88f8f2

Please sign in to comment.