Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions dev/src/wasm/local_lifetimes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,51 @@ struct Graph
std::vector<uint32_t> block, first, predecessor_first, predecessors;
};

struct Handlers
{
std::vector<uint32_t> first, targets;

void enter(const Function& function, const Instruction& inst, const std::vector<uint32_t>& labels)
{
first.push_back(static_cast<uint32_t>(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<CatchKind>(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<Edge>& edges)
{
const uint32_t size = static_cast<uint32_t>(function.instructions.size());
std::vector<uint32_t> open(1, size), end(size + 1, size), other(size, none);
std::vector<Edge> 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)
Expand All @@ -57,6 +85,12 @@ bool control_edges(const Function& function, std::vector<Edge>& 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{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Wasm behavior: PASS
42 changes: 42 additions & 0 deletions pa37/tests/optimization/600-exception-local-lifetimes.t
Original file line number Diff line number Diff line change
@@ -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;
}
24 changes: 24 additions & 0 deletions pa37/tests/optimization/600-exception-local-lifetimes.wasm-expect
Original file line number Diff line number Diff line change
@@ -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}}}
}
Loading