diff --git a/src/passes/Directize.cpp b/src/passes/Directize.cpp index 78294d59d5b..66b42d3867f 100644 --- a/src/passes/Directize.cpp +++ b/src/passes/Directize.cpp @@ -130,7 +130,7 @@ struct FunctionDirectizer : public WalkerPass> { 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; diff --git a/src/passes/GenerateDynCalls.cpp b/src/passes/GenerateDynCalls.cpp index 818bfa967ea..6e6e0a71781 100644 --- a/src/passes/GenerateDynCalls.cpp +++ b/src/passes/GenerateDynCalls.cpp @@ -140,8 +140,17 @@ void GenerateDynCalls::generateDynCallThunk(HeapType funcType) { } std::vector namedParams; std::vector 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); @@ -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 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); diff --git a/test/lit/passes/directize-wasm64.wast b/test/lit/passes/directize-wasm64.wast new file mode 100644 index 00000000000..8c3a0623f81 --- /dev/null +++ b/test/lit/passes/directize-wasm64.wast @@ -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) + ) + ) +) diff --git a/test/lit/passes/generate-dyncalls-wasm64.wast b/test/lit/passes/generate-dyncalls-wasm64.wast new file mode 100644 index 00000000000..c8dfba32e12 --- /dev/null +++ b/test/lit/passes/generate-dyncalls-wasm64.wast @@ -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: )