Skip to content

Commit d9ec300

Browse files
authored
Refactor ExpressionRunner (#2804)
Tackles the concerns raised in #2797 directly related to #2702 by reverting merging all of `PrecomputeExpressionRunner` into the base `ExpressionRunner`, instead adding a common base for both the precompute pass and the new C-API to inherit. No functional changes. --- ### Current hierarchy after #2702 is ``` ExpressionRunner ├ [PrecomputeExpressionRunner] ├ [CExpressionRunner] ├ ConstantExpressionRunner └ RuntimeExpressionRunner ``` where `ExpressionRunner` contains functionality not utilized by `ConstantExpressionRunner` and `RuntimeExpressionRunner`. ### New hierarchy will be: ``` ExpressionRunner ├ ConstantExpressionRunner │ ├ [PrecomputeExpressionRunner] │ └ [CExpressionRunner] ├ InitializerExpressionRunner └ RuntimeExpressionRunner ``` with the precompute pass's and the C-API's shared functionality now moved out of `ExpressionRunner` into a new `ConstantExpressionRunner`. Also renames the previous `ConstantExpressionRunner` to `InitializerExpressionRunner` to [better represent its uses](https://webassembly.org/docs/modules/#initializer-expression) and to make its previous name usable for the new intermediate template, where it fits perfectly. Also adds a few comments answering some of the questions that came up recently. ### Old hierarchy before #2702 for comparison: ``` ExpressionRunner ├ [PrecomputeExpressionRunner] ├ ConstantExpressionRunner └ RuntimeExpressionRunner ```
1 parent 1e6435a commit d9ec300

File tree

3 files changed

+178
-138
lines changed

3 files changed

+178
-138
lines changed

src/binaryen-c.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4998,16 +4998,18 @@ BinaryenExpressionRef RelooperRenderAndDispose(RelooperRef relooper,
49984998

49994999
namespace wasm {
50005000

5001-
class CExpressionRunner final : public ExpressionRunner<CExpressionRunner> {
5001+
// Evaluates a suspected constant expression via the C-API. Inherits most of its
5002+
// functionality from ConstantExpressionRunner, which it shares with the
5003+
// precompute pass, but must be `final` so we can `delete` its instances.
5004+
class CExpressionRunner final
5005+
: public ConstantExpressionRunner<CExpressionRunner> {
50025006
public:
50035007
CExpressionRunner(Module* module,
50045008
CExpressionRunner::Flags flags,
50055009
Index maxDepth,
50065010
Index maxLoopIterations)
5007-
: ExpressionRunner<CExpressionRunner>(
5011+
: ConstantExpressionRunner<CExpressionRunner>(
50085012
module, flags, maxDepth, maxLoopIterations) {}
5009-
5010-
void trap(const char* why) override { throw NonconstantException(); }
50115013
};
50125014

50135015
} // namespace wasm

src/passes/Precompute.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ namespace wasm {
4242
typedef std::unordered_map<LocalGet*, Literals> GetValues;
4343

4444
// Precomputes an expression. Errors if we hit anything that can't be
45-
// precomputed.
45+
// precomputed. Inherits most of its functionality from
46+
// ConstantExpressionRunner, which it shares with the C-API, but adds handling
47+
// of GetValues computed during the precompute pass.
4648
class PrecomputingExpressionRunner
47-
: public ExpressionRunner<PrecomputingExpressionRunner> {
49+
: public ConstantExpressionRunner<PrecomputingExpressionRunner> {
4850

4951
// Concrete values of gets computed during the pass, which the runner does not
5052
// know about since it only records values of sets it visits.
@@ -66,7 +68,7 @@ class PrecomputingExpressionRunner
6668
PrecomputingExpressionRunner(Module* module,
6769
GetValues& getValues,
6870
bool replaceExpression)
69-
: ExpressionRunner<PrecomputingExpressionRunner>(
71+
: ConstantExpressionRunner<PrecomputingExpressionRunner>(
7072
module,
7173
replaceExpression ? FlagValues::PRESERVE_SIDEEFFECTS
7274
: FlagValues::DEFAULT,
@@ -82,10 +84,9 @@ class PrecomputingExpressionRunner
8284
return Flow(values);
8385
}
8486
}
85-
return ExpressionRunner<PrecomputingExpressionRunner>::visitLocalGet(curr);
87+
return ConstantExpressionRunner<
88+
PrecomputingExpressionRunner>::visitLocalGet(curr);
8689
}
87-
88-
void trap(const char* why) override { throw NonconstantException(); }
8990
};
9091

9192
struct Precompute

0 commit comments

Comments
 (0)