Skip to content

Commit

Permalink
Fix generate-dyncalls and directize passed under table64 (#6604)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed May 18, 2024
1 parent 4e9f495 commit 369cddf
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/passes/Directize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ struct FunctionDirectizer : public WalkerPass<PostWalker<FunctionDirectizer>> {
return CallUtils::Unknown{};
}

Index index = c->value.geti32();
Index index = c->value.getInteger();

// Check if index is invalid, or the type is wrong.
auto& flatTable = *table.flatTable;
Expand Down
25 changes: 13 additions & 12 deletions src/passes/GenerateDynCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,17 @@ void GenerateDynCalls::generateDynCallThunk(HeapType funcType) {
}
std::vector<NameType> namedParams;
std::vector<Type> params;
namedParams.emplace_back("fptr", Type::i32); // function pointer param
params.push_back(Type::i32);
if (wasm->tables.empty()) {
// Add an imported table in exactly the same manner as the LLVM wasm backend
// would add one.
auto* table = wasm->addTable(Builder::makeTable(Name::fromInt(0)));
table->module = ENV;
table->base = "__indirect_function_table";
table->indexType = wasm->memories[0]->indexType;
}
auto& table = wasm->tables[0];
namedParams.emplace_back("fptr", table->indexType); // function pointer param
params.push_back(table->indexType);
int p = 0;
for (const auto& param : sig.params) {
namedParams.emplace_back(std::to_string(p++), param);
Expand All @@ -150,21 +159,13 @@ void GenerateDynCalls::generateDynCallThunk(HeapType funcType) {
auto f = builder.makeFunction(
name, std::move(namedParams), Signature(Type(params), sig.results), {});
f->hasExplicitName = true;
Expression* fptr = builder.makeLocalGet(0, Type::i32);
Expression* fptr = builder.makeLocalGet(0, table->indexType);
std::vector<Expression*> args;
Index i = 0;
for (const auto& param : sig.params) {
args.push_back(builder.makeLocalGet(++i, param));
}
if (wasm->tables.empty()) {
// Add an imported table in exactly the same manner as the LLVM wasm backend
// would add one.
auto* table = wasm->addTable(Builder::makeTable(Name::fromInt(0)));
table->module = ENV;
table->base = "__indirect_function_table";
}
f->body =
builder.makeCallIndirect(wasm->tables[0]->name, fptr, args, funcType);
f->body = builder.makeCallIndirect(table->name, fptr, args, funcType);

wasm->addFunction(std::move(f));
exportFunction(*wasm, name, true);
Expand Down
37 changes: 37 additions & 0 deletions test/lit/passes/directize-wasm64.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
;; NOTE: This test was ported using port_passes_tests_to_lit.py and could be cleaned up.

;; RUN: wasm-opt %s --directize --enable-memory64 -S -o - | filecheck %s

(module
;; CHECK: (type $ii (func (param i32 i32)))
(type $ii (func (param i32 i32)))

;; CHECK: (table $0 i64 5 5 funcref)
(table $0 i64 5 5 funcref)

;; CHECK: (elem $elem (i64.const 1) $foo)
(elem $elem (i64.const 1) $foo)

;; CHECK: (func $foo (param $0 i32) (param $1 i32)
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
(func $foo (param i32) (param i32)
;; helper function
(unreachable)
)

;; CHECK: (func $bar (param $x i32) (param $y i32)
;; CHECK-NEXT: (call $foo
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $bar (param $x i32) (param $y i32)
(call_indirect (type $ii)
(local.get $x)
(local.get $y)
(i64.const 1)
)
)
)
48 changes: 48 additions & 0 deletions test/lit/passes/generate-dyncalls-wasm64.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
;; RUN: wasm-opt %s --generate-dyncalls --enable-memory64 -S -o - | filecheck %s

(module
;; CHECK: (type $0 (func (result i32)))

;; CHECK: (type $1 (func (param i32) (result i64)))

;; CHECK: (type $2 (func (param i64) (result i32)))

;; CHECK: (type $3 (func (param i64 i32) (result i64)))

;; CHECK: (memory $m i64 1)
(memory $m i64 1)
;; CHECK: (table $t i64 2 2 funcref)
(table $t i64 2 2 funcref)
;; CHECK: (elem $elem (i64.const 0) $f1 $f2)
(elem $elem (i64.const 0) $f1 $f2)

;; CHECK: (export "dynCall_i" (func $dynCall_i))

;; CHECK: (export "dynCall_ji" (func $dynCall_ji))

;; CHECK: (func $f1 (result i32)
;; CHECK-NEXT: (i32.const 1024)
;; CHECK-NEXT: )
(func $f1 (result i32)
(i32.const 1024)
)
;; CHECK: (func $f2 (param $0 i32) (result i64)
;; CHECK-NEXT: (i64.const 42)
;; CHECK-NEXT: )
(func $f2 (param i32) (result i64)
(i64.const 42)
)
)
;; CHECK: (func $dynCall_i (param $fptr i64) (result i32)
;; CHECK-NEXT: (call_indirect (type $0)
;; CHECK-NEXT: (local.get $fptr)
;; CHECK-NEXT: )
;; CHECK-NEXT: )

;; CHECK: (func $dynCall_ji (param $fptr i64) (param $0 i32) (result i64)
;; CHECK-NEXT: (call_indirect (type $1)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (local.get $fptr)
;; CHECK-NEXT: )
;; CHECK-NEXT: )

0 comments on commit 369cddf

Please sign in to comment.