diff --git a/dev/src/wasm/local_lifetimes.cpp b/dev/src/wasm/local_lifetimes.cpp index a6c757df44..300d453984 100644 --- a/dev/src/wasm/local_lifetimes.cpp +++ b/dev/src/wasm/local_lifetimes.cpp @@ -30,6 +30,31 @@ struct Graph std::vector block, first, predecessor_first, predecessors; }; +struct Handlers +{ + std::vector first, targets; + + void enter(const Function& function, const Instruction& inst, const std::vector& labels) + { + first.push_back(static_cast(targets.size())); + uint32_t at = inst.operands.first + 2; + for (uint64_t i = 0; i < function.operands[inst.operands.first + 1]; ++i) + { + const CatchKind kind = static_cast(function.operands[at++]); + if (kind == CatchKind::Tag || kind == CatchKind::TagRef) + ++at; + const uint64_t depth = function.operands[at++]; + targets.push_back(labels[labels.size() - 1 - depth]); + } + } + + void leave() + { + targets.resize(first.back()); + first.pop_back(); + } +}; + // Branch operands name enclosing labels, including the function's label. // Resolve those typed labels before partitioning the selected instruction stream. bool control_edges(const Function& function, std::vector& edges) @@ -37,16 +62,19 @@ bool control_edges(const Function& function, std::vector& edges) const uint32_t size = static_cast(function.instructions.size()); std::vector open(1, size), end(size + 1, size), other(size, none); std::vector branches; + Handlers handlers; for (uint32_t i = 0; i < size; ++i) { const Instruction& inst = function.instructions[i]; if (inst.op == Op::TryTable) - return false; // Landing edges need exceptional liveness too. - if (inst.op == Op::Block || inst.op == Op::Loop || inst.op == Op::If) + handlers.enter(function, inst, open); // Catch depths exclude the new try label. + if (inst.op == Op::Block || inst.op == Op::Loop || inst.op == Op::If || inst.op == Op::TryTable) open.push_back(i); else if (inst.op == Op::End && open.size() > 1) { end[open.back()] = i + 1; + if (function.instructions[open.back()].op == Op::TryTable) + handlers.leave(); open.pop_back(); } else if (inst.op == Op::Else) @@ -57,6 +85,12 @@ bool control_edges(const Function& function, std::vector& edges) else if (inst.op == Op::Br || inst.op == Op::BrIf || inst.op == Op::BrTable) for (uint32_t k = 0; k < inst.operands.count; ++k) branches.push_back(Edge{ i, open[open.size() - 1 - function.operands[inst.operands.first + k]] }); + // Split at throwing instructions: a result store after a call must not + // kill a value that its landing path still needs. Keeping outer handlers + // too is conservative when an inner catch handles the same exception. + if (!handlers.targets.empty() && (effects(inst.op) & machine_model::MayThrow)) + for (uint32_t target : handlers.targets) + branches.push_back(Edge{ i, target }); } for (const Edge& edge : branches) edges.push_back(Edge{ diff --git a/pa37/tests/optimization/600-exception-local-lifetimes.ref.exit_status b/pa37/tests/optimization/600-exception-local-lifetimes.ref.exit_status new file mode 100644 index 0000000000..573541ac97 --- /dev/null +++ b/pa37/tests/optimization/600-exception-local-lifetimes.ref.exit_status @@ -0,0 +1 @@ +0 diff --git a/pa37/tests/optimization/600-exception-local-lifetimes.ref.stdout b/pa37/tests/optimization/600-exception-local-lifetimes.ref.stdout new file mode 100644 index 0000000000..d748bc1575 --- /dev/null +++ b/pa37/tests/optimization/600-exception-local-lifetimes.ref.stdout @@ -0,0 +1 @@ +Wasm behavior: PASS diff --git a/pa37/tests/optimization/600-exception-local-lifetimes.t b/pa37/tests/optimization/600-exception-local-lifetimes.t new file mode 100644 index 0000000000..ce024908e4 --- /dev/null +++ b/pa37/tests/optimization/600-exception-local-lifetimes.t @@ -0,0 +1,42 @@ +static unsigned tally; +struct Mark { + unsigned value; + ~Mark() { tally += value; } +}; +static __attribute__((noinline)) unsigned risky(unsigned value) { + if (value & 4u) throw value; + return value * 3u + 1u; +} + +// Every landing path reads values defined before its throwing calls. Mutually +// exclusive cases can share storage, but a successful-call store must not kill +// a value needed when that call throws. +extern "C" __attribute__((noinline)) unsigned eh_cases(unsigned value, unsigned mode) { + unsigned kept = value * 7u + 3u; + volatile unsigned stamp = value ^ 0x1357u; + switch (value & 15u) { +#define CASE(n) case n: { unsigned left = kept + n; Mark guard = {left}; \ + try { unsigned first = risky(value + n); unsigned live = first ^ kept; \ + unsigned second = risky(first + mode); return live + second + left + stamp; } \ + catch (unsigned error) { return error + left + stamp + kept; } } + CASE(0) CASE(1) CASE(2) CASE(3) CASE(4) CASE(5) CASE(6) CASE(7) + CASE(8) CASE(9) CASE(10) CASE(11) CASE(12) CASE(13) CASE(14) CASE(15) +#undef CASE + } + return 0; +} + +extern "C" unsigned observe(unsigned value, unsigned mode) { + unsigned kept = value * 7u + 3u, left = kept + (value & 15u); + unsigned stamp = value ^ 0x1357u, first = value + (value & 15u), expected; + if (first & 4u) expected = first + left + stamp + kept; + else { + first = first * 3u + 1u; + unsigned second = first + mode; + expected = second & 4u ? second + left + stamp + kept + : (first ^ kept) + second * 3u + 1u + left + stamp; + } + tally = 0; + unsigned actual = eh_cases(value, mode); + return actual == expected && tally == left ? 17 : 1; +} diff --git a/pa37/tests/optimization/600-exception-local-lifetimes.wasm-expect b/pa37/tests/optimization/600-exception-local-lifetimes.wasm-expect new file mode 100644 index 0000000000..822820fd86 --- /dev/null +++ b/pa37/tests/optimization/600-exception-local-lifetimes.wasm-expect @@ -0,0 +1,24 @@ +{ + "source": true, + "runtime": true, + "libraries": ["c++abi", "c++", "unwind"], + "wasi": true, + "target": "wasm32-wasip1", + "link_flags": ["-nostartfiles"], + "reference_flags": ["-fexceptions", "-fwasm-exceptions", "-mllvm", "-wasm-use-legacy-eh=false"], + "reference_link_flags": ["-fwasm-exceptions", "-Wl,--no-entry"], + "reference_initialize": "__wasm_call_ctors", + "llvm_interop": true, + "exports": ["eh_cases"], + "calls": [ + {"export": "observe", "args": ["0", "0"], "result": "17"}, + {"export": "observe", "args": ["1", "0"], "result": "17"}, + {"export": "observe", "args": ["2", "0"], "result": "17"}, + {"export": "observe", "args": ["3", "0"], "result": "17"}, + {"export": "observe", "args": ["8", "4"], "result": "17"}, + {"export": "observe", "args": ["11", "19"], "result": "17"}, + {"export": "observe", "args": ["15", "7"], "result": "17"}, + {"export": "observe", "args": ["-1", "-1"], "result": "17"} + ], + "quality": {"levels": [1, 2, 3], "functions": {"eh_cases": {"locals": 80}}} +}