Skip to content

Commit

Permalink
[Executor] Fix extra branch in addfunc (WasmEdge#1587)
Browse files Browse the repository at this point in the history
* check the first item and dispatch it to different loops

Signed-off-by: eat4toast <[email protected]>
  • Loading branch information
eat4toast authored Jun 28, 2022
1 parent 439c3a3 commit e4d92d8
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/executor/instantiate/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ Expect<void> Executor::instantiate(Runtime::Instance::ModuleInstance &ModInst,
auto TypeIdxs = FuncSec.getContent();
auto CodeSegs = CodeSec.getContent();

// Iterate through the code segments to instantiate function instances.
for (uint32_t I = 0; I < CodeSegs.size(); ++I) {
// Create and add the function instance into the module instance.
auto *FuncType = *ModInst.getFuncType(TypeIdxs[I]);
if (auto Symbol = CodeSegs[I].getSymbol()) {
if (CodeSegs.size() == 0) {
return {};
}
// The module will always choose the `for` loop in `else` case under
// interpreter mode. Instead, if we do branch in the `for` loop which might
// cause meaningless branch misses. Therefore we should check the first item
// and dispatch it into different cases to reduce branch misses.
if (CodeSegs[0].getSymbol() != false) {
for (uint32_t I = 0; I < CodeSegs.size(); ++I) {
auto *FuncType = *ModInst.getFuncType(TypeIdxs[I]);
auto Symbol = CodeSegs[I].getSymbol();
ModInst.addFunc(&ModInst, *FuncType, std::move(Symbol));
} else {
}
} else {
// Iterate through the code segments to instantiate function instances.
for (uint32_t I = 0; I < CodeSegs.size(); ++I) {
// Create and add the function instance into the module instance.
auto *FuncType = *ModInst.getFuncType(TypeIdxs[I]);
ModInst.addFunc(&ModInst, *FuncType, CodeSegs[I].getLocals(),
CodeSegs[I].getExpr().getInstrs());
}
Expand Down

0 comments on commit e4d92d8

Please sign in to comment.