Skip to content

Commit 23a9e69

Browse files
authored
[Compilation Hints] Add all call instructions (#7588)
1 parent e97f13e commit 23a9e69

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

src/parser/contexts.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,7 +2385,9 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx>, AnnotationParserCtx {
23852385
bool isReturn) {
23862386
auto t = getTable(pos, table);
23872387
CHECK_ERR(t);
2388-
return withLoc(pos, irBuilder.makeCallIndirect(*t, type, isReturn));
2388+
auto inline_ = getInlineHint(annotations);
2389+
return withLoc(pos,
2390+
irBuilder.makeCallIndirect(*t, type, isReturn, inline_));
23892391
}
23902392

23912393
// Return the branch hint for a branching instruction, if there is one.
@@ -2561,7 +2563,8 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx>, AnnotationParserCtx {
25612563
const std::vector<Annotation>& annotations,
25622564
HeapType type,
25632565
bool isReturn) {
2564-
return withLoc(pos, irBuilder.makeCallRef(type, isReturn));
2566+
auto inline_ = getInlineHint(annotations);
2567+
return withLoc(pos, irBuilder.makeCallRef(type, isReturn, inline_));
25652568
}
25662569

25672570
Result<> makeRefI31(Index pos,

src/wasm-ir-builder.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ class IRBuilder : public UnifiedExpressionVisitor<IRBuilder, Result<>> {
127127
Result<> makeCall(Name func,
128128
bool isReturn,
129129
std::optional<std::uint8_t> inline_ = std::nullopt);
130-
Result<> makeCallIndirect(Name table, HeapType type, bool isReturn);
130+
Result<> makeCallIndirect(Name table,
131+
HeapType type,
132+
bool isReturn,
133+
std::optional<std::uint8_t> inline_ = std::nullopt);
131134
Result<> makeLocalGet(Index local);
132135
Result<> makeLocalSet(Index local);
133136
Result<> makeLocalTee(Index local);
@@ -201,7 +204,9 @@ class IRBuilder : public UnifiedExpressionVisitor<IRBuilder, Result<>> {
201204
Result<> makeTupleDrop(uint32_t arity);
202205
Result<> makeRefI31(Shareability share);
203206
Result<> makeI31Get(bool signed_);
204-
Result<> makeCallRef(HeapType type, bool isReturn);
207+
Result<> makeCallRef(HeapType type,
208+
bool isReturn,
209+
std::optional<std::uint8_t> inline_ = std::nullopt);
205210
Result<> makeRefTest(Type type);
206211
Result<> makeRefCast(Type type);
207212
Result<> makeBrOn(Index label,

src/wasm/wasm-ir-builder.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,13 +1456,18 @@ Result<> IRBuilder::makeCall(Name func,
14561456
return Ok{};
14571457
}
14581458

1459-
Result<> IRBuilder::makeCallIndirect(Name table, HeapType type, bool isReturn) {
1459+
Result<> IRBuilder::makeCallIndirect(Name table,
1460+
HeapType type,
1461+
bool isReturn,
1462+
std::optional<std::uint8_t> inline_) {
14601463
CallIndirect curr(wasm.allocator);
14611464
curr.heapType = type;
14621465
curr.operands.resize(type.getSignature().params.size());
14631466
CHECK_ERR(visitCallIndirect(&curr));
1464-
push(builder.makeCallIndirect(
1465-
table, curr.target, curr.operands, type, isReturn));
1467+
auto* call =
1468+
builder.makeCallIndirect(table, curr.target, curr.operands, type, isReturn);
1469+
push(call);
1470+
addInlineHint(call, inline_);
14661471
return Ok{};
14671472
}
14681473

@@ -1952,7 +1957,9 @@ Result<> IRBuilder::makeI31Get(bool signed_) {
19521957
return Ok{};
19531958
}
19541959

1955-
Result<> IRBuilder::makeCallRef(HeapType type, bool isReturn) {
1960+
Result<> IRBuilder::makeCallRef(HeapType type,
1961+
bool isReturn,
1962+
std::optional<std::uint8_t> inline_) {
19561963
CallRef curr(wasm.allocator);
19571964
if (!type.isSignature()) {
19581965
return Err{"expected function type"};
@@ -1961,7 +1968,10 @@ Result<> IRBuilder::makeCallRef(HeapType type, bool isReturn) {
19611968
curr.operands.resize(type.getSignature().params.size());
19621969
CHECK_ERR(ChildPopper{*this}.visitCallRef(&curr, type));
19631970
CHECK_ERR(validateTypeAnnotation(type, curr.target));
1964-
push(builder.makeCallRef(curr.target, curr.operands, sig.results, isReturn));
1971+
auto* call =
1972+
builder.makeCallRef(curr.target, curr.operands, sig.results, isReturn);
1973+
push(call);
1974+
addInlineHint(call, inline_);
19651975
return Ok{};
19661976
}
19671977

test/lit/compilation-hints.wast

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55
;; TODO: wasm-opt -all --roundtrip %s -S -o - | filecheck %s --check-prefix=RTRIP
66

77
(module
8-
;; CHECK: (type $0 (func))
8+
;; CHECK: (type $func (func))
9+
(type $func (func))
910

10-
;; CHECK: (func $func (type $0)
11+
;; CHECK: (table $table 10 20 funcref)
12+
(table $table 10 20 funcref)
13+
14+
;; CHECK: (elem declare func $func)
15+
16+
;; CHECK: (func $func (type $func)
1117
;; CHECK-NEXT: (@metadata.code.inline "\00")
1218
;; CHECK-NEXT: (call $func)
1319
;; CHECK-NEXT: (@metadata.code.inline "\01")
@@ -31,6 +37,27 @@
3137
(call $func)
3238
)
3339

40+
;; CHECK: (func $other-calls (type $func)
41+
;; CHECK-NEXT: (@metadata.code.inline "\12")
42+
;; CHECK-NEXT: (call_indirect $table (type $func)
43+
;; CHECK-NEXT: (i32.const 0)
44+
;; CHECK-NEXT: )
45+
;; CHECK-NEXT: (@metadata.code.inline "\34")
46+
;; CHECK-NEXT: (call_ref $func
47+
;; CHECK-NEXT: (ref.func $func)
48+
;; CHECK-NEXT: )
49+
;; CHECK-NEXT: )
50+
(func $other-calls
51+
(@metadata.code.inline "\12")
52+
(call_indirect (type $func)
53+
(i32.const 0)
54+
)
55+
(@metadata.code.inline "\34")
56+
(call_ref $func
57+
(ref.func $func)
58+
)
59+
)
60+
3461
;; TODO: test function annotations, after
3562
;; https://github.com/WebAssembly/tool-conventions/issues/251
3663
)

0 commit comments

Comments
 (0)