From 24d71aa0146a6dacfce5d84a11c88195e66eee0a Mon Sep 17 00:00:00 2001 From: Heejin Ahn Date: Thu, 20 May 2021 19:54:46 -0700 Subject: [EH] Change Walker::TaskFunc back to function pointer (#3899) `Walker::TaskFunc` has changed from a function pointer to `std::function` in #3494, mainly to make the EH support for `CFGWalker` easier. We didn't notice much performance difference then, but it was recently reported that it creased binaryen.js code size and performance. This changes `Walker::TaskFunc` back to a function pointer and does a little more work to manage catch index in `CFGWalker` side. Hopefully fixes #3857. --- src/cfg/cfg-traversal.h | 28 +++++++++++++++------------- src/wasm-traversal.h | 2 +- 2 files changed, 16 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/cfg/cfg-traversal.h b/src/cfg/cfg-traversal.h index 39f71dd6c..3f2eecb88 100644 --- a/src/cfg/cfg-traversal.h +++ b/src/cfg/cfg-traversal.h @@ -101,6 +101,10 @@ struct CFGWalker : public ControlFlowWalker { // but their usage does not overlap in time, and this is more efficient. std::vector> processCatchStack; + // Stack to store the catch indices within catch bodies. To be used in + // doStartCatch and doEndCatch. + std::vector catchIndexStack; + BasicBlock* startBasicBlock() { currBasicBlock = ((SubType*)this)->makeBasicBlock(); basicBlocks.push_back(std::unique_ptr(currBasicBlock)); @@ -304,16 +308,20 @@ struct CFGWalker : public ControlFlowWalker { self->processCatchStack.push_back(self->unwindCatchStack.back()); self->unwindCatchStack.pop_back(); self->unwindExprStack.pop_back(); + self->catchIndexStack.push_back(0); } - static void doStartCatch(SubType* self, Expression** currp, Index i) { + static void doStartCatch(SubType* self, Expression** currp) { // Get the block that starts this catch - self->currBasicBlock = self->processCatchStack.back()[i]; + self->currBasicBlock = + self->processCatchStack.back()[self->catchIndexStack.back()]; } - static void doEndCatch(SubType* self, Expression** currp, Index i) { + static void doEndCatch(SubType* self, Expression** currp) { // We are done with this catch; set the block that ends it - self->processCatchStack.back()[i] = self->currBasicBlock; + self->processCatchStack.back()[self->catchIndexStack.back()] = + self->currBasicBlock; + self->catchIndexStack.back()++; } static void doEndTry(SubType* self, Expression** currp) { @@ -326,6 +334,7 @@ struct CFGWalker : public ControlFlowWalker { self->link(self->tryStack.back(), self->currBasicBlock); self->tryStack.pop_back(); self->processCatchStack.pop_back(); + self->catchIndexStack.pop_back(); } static void doEndThrow(SubType* self, Expression** currp) { @@ -381,17 +390,10 @@ struct CFGWalker : public ControlFlowWalker { case Expression::Id::TryId: { self->pushTask(SubType::doEndTry, currp); auto& catchBodies = curr->cast()->catchBodies; - using namespace std::placeholders; for (Index i = 0; i < catchBodies.size(); i++) { - auto doEndCatchI = [i](SubType* self, Expression** currp) { - doEndCatch(self, currp, i); - }; - self->pushTask(doEndCatchI, currp); + self->pushTask(doEndCatch, currp); self->pushTask(SubType::scan, &catchBodies[i]); - auto doStartCatchI = [i](SubType* self, Expression** currp) { - doStartCatch(self, currp, i); - }; - self->pushTask(doStartCatchI, currp); + self->pushTask(doStartCatch, currp); } self->pushTask(SubType::doStartCatches, currp); self->pushTask(SubType::scan, &curr->cast()->body); diff --git a/src/wasm-traversal.h b/src/wasm-traversal.h index 388584fd6..eb91184e5 100644 --- a/src/wasm-traversal.h +++ b/src/wasm-traversal.h @@ -282,7 +282,7 @@ struct Walker : public VisitorType { // nested. // Tasks receive the this pointer and a pointer to the pointer to operate on - using TaskFunc = std::function; + typedef void (*TaskFunc)(SubType*, Expression**); struct Task { TaskFunc func; -- cgit v1.2.3