Skip to content

Commit 20e7058

Browse files
authored
[Outlining] Add TryTable (#7504)
Supports try_table in the stringify of the module, and filters out try_table outlining sequences.
1 parent d48529d commit 20e7058

File tree

5 files changed

+88
-4
lines changed

5 files changed

+88
-4
lines changed

src/passes/Outlining.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ struct ReconstructStringifyWalker
151151
} else if (reason.getCatchAllStart()) {
152152
ASSERT_OK(existingBuilder.visitCatchAll());
153153
ODBG(desc = "Catch All Start at");
154+
} else if (auto curr = reason.getTryTableStart()) {
155+
ODBG(desc = "Try Table Start at ");
156+
ASSERT_OK(existingBuilder.visitTryTableStart(curr->tryt));
154157
} else if (reason.getEnd()) {
155158
ODBG(desc = "End at ");
156159
ASSERT_OK(existingBuilder.visitEnd());
@@ -346,9 +349,9 @@ struct Outlining : public Pass {
346349
substrings = StringifyProcessor::dedupe(substrings);
347350
// Remove substrings with overlapping indices.
348351
substrings = StringifyProcessor::filterOverlaps(substrings);
349-
// Remove substrings with branch and return instructions until an analysis
350-
// is performed to see if the intended destination of the branch is included
351-
// in the substring to be outlined.
352+
// Remove substrings with branch, return, and try_table instructions until
353+
// an analysis is performed to see if the intended destination of the branch
354+
// is included in the substring to be outlined.
352355
substrings =
353356
StringifyProcessor::filterBranches(substrings, stringify.exprs);
354357
// Remove substrings with local.set instructions until Outlining is extended

src/passes/hash-stringify-walker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ std::vector<SuffixTree::RepeatedSubstring> StringifyProcessor::filterBranches(
269269
const std::vector<Expression*>& exprs) {
270270
return StringifyProcessor::filter(
271271
substrings, exprs, [](const Expression* curr) {
272-
return Properties::isBranch(curr) || curr->is<Return>();
272+
return Properties::isBranch(curr) || curr->is<Return>() ||
273+
curr->is<TryTable>();
273274
});
274275
}
275276

src/passes/stringify-walker-impl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ template<typename SubType> void StringifyWalker<SubType>::dequeueControlFlow() {
118118
addUniqueSymbol(SeparatorReason::makeEnd());
119119
break;
120120
}
121+
case Expression::Id::TryTableId: {
122+
auto* tryt = curr->cast<TryTable>();
123+
addUniqueSymbol(SeparatorReason::makeTryTableStart(tryt));
124+
Super::walk(tryt->body);
125+
addUniqueSymbol(SeparatorReason::makeEnd());
126+
break;
127+
}
121128
default: {
122129
assert(Properties::isControlFlowStructure(curr));
123130
WASM_UNREACHABLE("unexpected expression");

src/passes/stringify-walker.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ struct StringifyWalker
9999

100100
struct CatchAllStart {};
101101

102+
struct TryTableStart {
103+
TryTable* tryt;
104+
};
105+
102106
struct End {
103107
Expression* curr;
104108
};
@@ -110,6 +114,7 @@ struct StringifyWalker
110114
TryStart,
111115
CatchStart,
112116
CatchAllStart,
117+
TryTableStart,
113118
End>;
114119

115120
Separator reason;
@@ -140,6 +145,9 @@ struct StringifyWalker
140145
static SeparatorReason makeCatchAllStart() {
141146
return SeparatorReason(CatchAllStart{});
142147
}
148+
static SeparatorReason makeTryTableStart(TryTable* tryt) {
149+
return SeparatorReason(TryTableStart{tryt});
150+
}
143151
static SeparatorReason makeEnd() { return SeparatorReason(End{}); }
144152
FuncStart* getFuncStart() { return std::get_if<FuncStart>(&reason); }
145153
BlockStart* getBlockStart() { return std::get_if<BlockStart>(&reason); }
@@ -151,6 +159,9 @@ struct StringifyWalker
151159
CatchAllStart* getCatchAllStart() {
152160
return std::get_if<CatchAllStart>(&reason);
153161
}
162+
TryTableStart* getTryTableStart() {
163+
return std::get_if<TryTableStart>(&reason);
164+
}
154165
End* getEnd() { return std::get_if<End>(&reason); }
155166
};
156167

@@ -173,6 +184,8 @@ struct StringifyWalker
173184
return o << "Catch Start";
174185
} else if (reason.getCatchAllStart()) {
175186
return o << "Catch All Start";
187+
} else if (reason.getTryTableStart()) {
188+
return o << "Try Table Start";
176189
} else if (reason.getEnd()) {
177190
return o << "End";
178191
}

test/lit/passes/outlining.wast

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,3 +1244,63 @@
12441244
)
12451245
)
12461246
)
1247+
1248+
;; Tests TryTable instructions are correctly filtered from being outlined.
1249+
;; The (drop (i32.const 0)) instructions were added to form an outlineable
1250+
;; sequence with the block that contains the try_table.
1251+
(module
1252+
;; CHECK: (type $1 (func (result (ref exn))))
1253+
1254+
;; CHECK: (type $0 (func))
1255+
(type $0 (func))
1256+
;; CHECK: (tag $tag$0 (type $0))
1257+
(tag $tag$0 (type $0))
1258+
;; CHECK: (func $a (type $1) (result (ref exn))
1259+
;; CHECK-NEXT: (loop $label1
1260+
;; CHECK-NEXT: (drop
1261+
;; CHECK-NEXT: (i32.const 0)
1262+
;; CHECK-NEXT: )
1263+
;; CHECK-NEXT: (block
1264+
;; CHECK-NEXT: (try_table (catch_all $label1)
1265+
;; CHECK-NEXT: (throw $tag$0)
1266+
;; CHECK-NEXT: )
1267+
;; CHECK-NEXT: )
1268+
;; CHECK-NEXT: )
1269+
;; CHECK-NEXT: )
1270+
(func $a (result (ref exn))
1271+
(loop $label1 (result (ref exn))
1272+
(drop
1273+
(i32.const 0)
1274+
)
1275+
(block (result (ref exn))
1276+
(try_table (catch_all $label1)
1277+
(throw $tag$0)
1278+
)
1279+
)
1280+
)
1281+
)
1282+
;; CHECK: (func $b (type $1) (result (ref exn))
1283+
;; CHECK-NEXT: (loop $label1
1284+
;; CHECK-NEXT: (drop
1285+
;; CHECK-NEXT: (i32.const 0)
1286+
;; CHECK-NEXT: )
1287+
;; CHECK-NEXT: (block
1288+
;; CHECK-NEXT: (try_table (catch_all $label1)
1289+
;; CHECK-NEXT: (throw $tag$0)
1290+
;; CHECK-NEXT: )
1291+
;; CHECK-NEXT: )
1292+
;; CHECK-NEXT: )
1293+
;; CHECK-NEXT: )
1294+
(func $b (result (ref exn))
1295+
(loop $label1 (result (ref exn))
1296+
(drop
1297+
(i32.const 0)
1298+
)
1299+
(block (result (ref exn))
1300+
(try_table (catch_all $label1)
1301+
(throw $tag$0)
1302+
)
1303+
)
1304+
)
1305+
)
1306+
)

0 commit comments

Comments
 (0)