From db2ff9b1064746d4979d07c5887e689d6939edcd Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 23 Nov 2026 12:37:52 -0800 Subject: [PATCH 1/3] [fpcast-emu] Export `fpcast$` thunks for dynamic linking When `--pass-arg=fpcast-emu-export-thunks` is passed to `fpcast-emu`, export an `(i64 x 16) -> i64` thunk named `fpcast$` alongside each original function export by reusing the thunk in `funcThunks`. Skip imports, `dynCall_*` and `fpcast$*` exports, and non-function exports, and unwrap `orig$` suffixes so both direct and `orig$` export pairs share the corresponding `fpcast$` thunk export. This enables dynamic linkers such as Emscripten to resolve uniform function pointer targets across `MAIN_MODULE` and `SIDE_MODULE` exports while preserving original direct-call exports. --- src/passes/FuncCastEmulation.cpp | 45 ++++++++++++++ test/lit/passes/fpcast-emu-export-thunks.wast | 60 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 test/lit/passes/fpcast-emu-export-thunks.wast diff --git a/src/passes/FuncCastEmulation.cpp b/src/passes/FuncCastEmulation.cpp index 4f85c1ddc13..0df237cdf17 100644 --- a/src/passes/FuncCastEmulation.cpp +++ b/src/passes/FuncCastEmulation.cpp @@ -182,6 +182,51 @@ struct FuncCastEmulation : public Pass { } } + if (hasArgument("fpcast-emu-export-thunks")) { + // Export fpcast$ thunks for non-imported function exports (excluding + // dynCall_* and already-prefixed fpcast$ exports) so dynamic linking can + // insert ABI-compatible thunks into the indirect function table. + std::vector> newExports; + Builder builder(*module); + for (auto& ex : module->exports) { + if (ex->kind != ExternalKind::Function) { + continue; + } + Name funcName = *ex->getInternalName(); + if (module->getFunction(funcName)->imported()) { + continue; + } + if (ex->name.startsWith("dynCall_") || ex->name.startsWith("fpcast$")) { + continue; + } + // Under !WASM_BIGINT, LegalizeJSInterface runs earlier and exports + // `orig$foo` for the unlegalized function while `foo` points to a + // legalstub. Wrap `orig$foo` as `fpcast$foo` and skip `foo`. + std::string exportName = ex->name.toString(); + if (ex->name.startsWith("orig$")) { + exportName = exportName.substr(5); + if (Name(exportName).startsWith("dynCall_")) { + continue; + } + } else if (module->getExportOrNull("orig$" + exportName)) { + continue; + } + Name fpcastName = "fpcast$" + exportName; + if (module->getExportOrNull(fpcastName)) { + continue; + } + auto [iter, inserted] = funcThunks.insert({funcName, nullptr}); + if (inserted) { + iter->second = makeThunk(funcName, module, numParams); + } + newExports.push_back(builder.makeExport( + fpcastName, iter->second->name, ExternalKind::Function)); + } + for (auto& ex : newExports) { + module->addExport(std::move(ex)); + } + } + // update call_indirects ParallelFuncCastEmulation(ABIType, numParams).run(getPassRunner(), module); } diff --git a/test/lit/passes/fpcast-emu-export-thunks.wast b/test/lit/passes/fpcast-emu-export-thunks.wast new file mode 100644 index 00000000000..3349bd5f6dd --- /dev/null +++ b/test/lit/passes/fpcast-emu-export-thunks.wast @@ -0,0 +1,60 @@ +;; RUN: foreach %s %t wasm-opt --fpcast-emu --pass-arg=fpcast-emu-export-thunks -S -o - | filecheck %s + +(module + (import "env" "imp" (func $imp (param i32) (result i32))) + (table 2 funcref) + (elem (i32.const 0) $in_table) + (global $g i32 (i32.const 42)) + ;; CHECK: (table $0 2 funcref) + ;; CHECK: (elem $0 (i32.const 0) $byn$fpcast-emu$in_table) + ;; CHECK: (export "add" (func $add)) + ;; CHECK: (export "add_alias" (func $add)) + ;; CHECK: (export "in_table" (func $in_table)) + ;; CHECK: (export "dynCall_ii" (func $dynCall_ii)) + ;; CHECK: (export "reexport_import" (func $imp)) + ;; CHECK: (export "g" (global $g)) + ;; CHECK: (export "legalized" (func $legalstub)) + ;; CHECK: (export "orig$legalized" (func $unlegalized)) + ;; CHECK: (export "fpcast$add" (func $byn$fpcast-emu$add)) + ;; CHECK: (export "fpcast$add_alias" (func $byn$fpcast-emu$add)) + ;; CHECK: (export "fpcast$in_table" (func $byn$fpcast-emu$in_table)) + ;; CHECK: (export "fpcast$legalized" (func $byn$fpcast-emu$unlegalized)) + ;; CHECK-NOT: fpcast$dynCall_ii + ;; CHECK-NOT: fpcast$reexport_import + ;; CHECK-NOT: fpcast$g + ;; CHECK-NOT: fpcast$orig$legalized + (export "add" (func $add)) + (export "add_alias" (func $add)) + (export "in_table" (func $in_table)) + (export "dynCall_ii" (func $dynCall_ii)) + (export "reexport_import" (func $imp)) + (export "g" (global $g)) + (export "legalized" (func $legalstub)) + (export "orig$legalized" (func $unlegalized)) + (func $add (param i32 i32) (result i32) + (i32.add + (local.get 0) + (local.get 1) + ) + ) + (func $in_table (param f32) (result f64) + (f64.promote_f32 + (local.get 0) + ) + ) + (func $dynCall_ii (param i32) (result i32) + (local.get 0) + ) + (func $legalstub (param i32 i32) (result i32) + (i32.wrap_i64 + (call $unlegalized + (i64.extend_i32_u + (local.get 0) + ) + ) + ) + ) + (func $unlegalized (param i64) (result i64) + (local.get 0) + ) +) From 4d9780b8a547db6fec89ab795398b1984aa065c3 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 23 Nov 2026 16:19:40 -0800 Subject: [PATCH 2/3] feedback --- src/passes/FuncCastEmulation.cpp | 18 ++-- test/lit/passes/fpcast-emu-export-thunks.wast | 101 +++++++++++++++--- .../passes/fpcast-emu-max-params-error.wast | 16 +++ 3 files changed, 116 insertions(+), 19 deletions(-) create mode 100644 test/lit/passes/fpcast-emu-max-params-error.wast diff --git a/src/passes/FuncCastEmulation.cpp b/src/passes/FuncCastEmulation.cpp index 0df237cdf17..0032b2b38e5 100644 --- a/src/passes/FuncCastEmulation.cpp +++ b/src/passes/FuncCastEmulation.cpp @@ -196,19 +196,19 @@ struct FuncCastEmulation : public Pass { if (module->getFunction(funcName)->imported()) { continue; } - if (ex->name.startsWith("dynCall_") || ex->name.startsWith("fpcast$")) { - continue; - } // Under !WASM_BIGINT, LegalizeJSInterface runs earlier and exports // `orig$foo` for the unlegalized function while `foo` points to a - // legalstub. Wrap `orig$foo` as `fpcast$foo` and skip `foo`. + // legalstub. Wrap `orig$foo` as `fpcast$foo` and skip the `foo` stub. std::string exportName = ex->name.toString(); if (ex->name.startsWith("orig$")) { exportName = exportName.substr(5); - if (Name(exportName).startsWith("dynCall_")) { - continue; - } } else if (module->getExportOrNull("orig$" + exportName)) { + // `ex` is the legalized stub; its companion `orig$` export provides + // `fpcast$` instead. + continue; + } + if (Name(exportName).startsWith("dynCall_") || + Name(exportName).startsWith("fpcast$")) { continue; } Name fpcastName = "fpcast$" + exportName; @@ -241,6 +241,10 @@ struct FuncCastEmulation : public Pass { } // The item in the table may be a function or a function import. auto* func = module->getFunction(name); + if (func->getNumParams() > numParams) { + Fatal() << "max-func-params needs to be at least " + << func->getNumParams(); + } Type type = func->getResults(); Builder builder(*module); std::vector callOperands; diff --git a/test/lit/passes/fpcast-emu-export-thunks.wast b/test/lit/passes/fpcast-emu-export-thunks.wast index 3349bd5f6dd..eee5b05ca0e 100644 --- a/test/lit/passes/fpcast-emu-export-thunks.wast +++ b/test/lit/passes/fpcast-emu-export-thunks.wast @@ -1,50 +1,94 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. ;; RUN: foreach %s %t wasm-opt --fpcast-emu --pass-arg=fpcast-emu-export-thunks -S -o - | filecheck %s (module + ;; CHECK: (type $0 (func (param i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64) (result i64))) + + ;; CHECK: (type $1 (func (param i32) (result i32))) + + ;; CHECK: (type $2 (func (param i32 i32) (result i32))) + + ;; CHECK: (type $3 (func (param f32) (result f64))) + + ;; CHECK: (type $4 (func (param i64) (result i64))) + + ;; CHECK: (import "env" "imp" (func $imp (param i32) (result i32))) (import "env" "imp" (func $imp (param i32) (result i32))) (table 2 funcref) (elem (i32.const 0) $in_table) + ;; CHECK: (global $g i32 (i32.const 42)) (global $g i32 (i32.const 42)) ;; CHECK: (table $0 2 funcref) + ;; CHECK: (elem $0 (i32.const 0) $byn$fpcast-emu$in_table) + ;; CHECK: (export "add" (func $add)) + (export "add" (func $add)) ;; CHECK: (export "add_alias" (func $add)) + (export "add_alias" (func $add)) + ;; CHECK: (export "already" (func $add)) + (export "already" (func $add)) + ;; CHECK: (export "fpcast$already" (func $add)) + (export "fpcast$already" (func $add)) ;; CHECK: (export "in_table" (func $in_table)) + (export "in_table" (func $in_table)) ;; CHECK: (export "dynCall_ii" (func $dynCall_ii)) + (export "dynCall_ii" (func $dynCall_ii)) + ;; CHECK: (export "orig$dynCall_ii" (func $dynCall_ii)) + (export "orig$dynCall_ii" (func $dynCall_ii)) ;; CHECK: (export "reexport_import" (func $imp)) + (export "reexport_import" (func $imp)) ;; CHECK: (export "g" (global $g)) + (export "g" (global $g)) ;; CHECK: (export "legalized" (func $legalstub)) + (export "legalized" (func $legalstub)) ;; CHECK: (export "orig$legalized" (func $unlegalized)) + (export "orig$legalized" (func $unlegalized)) ;; CHECK: (export "fpcast$add" (func $byn$fpcast-emu$add)) + ;; CHECK: (export "fpcast$add_alias" (func $byn$fpcast-emu$add)) + ;; CHECK: (export "fpcast$in_table" (func $byn$fpcast-emu$in_table)) + ;; CHECK: (export "fpcast$legalized" (func $byn$fpcast-emu$unlegalized)) - ;; CHECK-NOT: fpcast$dynCall_ii - ;; CHECK-NOT: fpcast$reexport_import - ;; CHECK-NOT: fpcast$g - ;; CHECK-NOT: fpcast$orig$legalized - (export "add" (func $add)) - (export "add_alias" (func $add)) - (export "in_table" (func $in_table)) - (export "dynCall_ii" (func $dynCall_ii)) - (export "reexport_import" (func $imp)) - (export "g" (global $g)) - (export "legalized" (func $legalstub)) - (export "orig$legalized" (func $unlegalized)) + + ;; CHECK: (func $add (param $0 i32) (param $1 i32) (result i32) + ;; CHECK-NEXT: (i32.add + ;; CHECK-NEXT: (local.get $0) + ;; CHECK-NEXT: (local.get $1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) (func $add (param i32 i32) (result i32) (i32.add (local.get 0) (local.get 1) ) ) + ;; CHECK: (func $in_table (param $0 f32) (result f64) + ;; CHECK-NEXT: (f64.promote_f32 + ;; CHECK-NEXT: (local.get $0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) (func $in_table (param f32) (result f64) (f64.promote_f32 (local.get 0) ) ) + ;; CHECK: (func $dynCall_ii (param $0 i32) (result i32) + ;; CHECK-NEXT: (local.get $0) + ;; CHECK-NEXT: ) (func $dynCall_ii (param i32) (result i32) (local.get 0) ) + ;; CHECK: (func $legalstub (param $0 i32) (param $1 i32) (result i32) + ;; CHECK-NEXT: (i32.wrap_i64 + ;; CHECK-NEXT: (call $unlegalized + ;; CHECK-NEXT: (i64.extend_i32_u + ;; CHECK-NEXT: (local.get $0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) (func $legalstub (param i32 i32) (result i32) (i32.wrap_i64 (call $unlegalized @@ -54,7 +98,40 @@ ) ) ) + ;; CHECK: (func $unlegalized (param $0 i64) (result i64) + ;; CHECK-NEXT: (local.get $0) + ;; CHECK-NEXT: ) (func $unlegalized (param i64) (result i64) (local.get 0) ) ) +;; CHECK: (func $byn$fpcast-emu$in_table (param $0 i64) (param $1 i64) (param $2 i64) (param $3 i64) (param $4 i64) (param $5 i64) (param $6 i64) (param $7 i64) (param $8 i64) (param $9 i64) (param $10 i64) (param $11 i64) (param $12 i64) (param $13 i64) (param $14 i64) (param $15 i64) (result i64) +;; CHECK-NEXT: (i64.reinterpret_f64 +;; CHECK-NEXT: (call $in_table +;; CHECK-NEXT: (f32.reinterpret_i32 +;; CHECK-NEXT: (i32.wrap_i64 +;; CHECK-NEXT: (local.get $0) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) + +;; CHECK: (func $byn$fpcast-emu$add (param $0 i64) (param $1 i64) (param $2 i64) (param $3 i64) (param $4 i64) (param $5 i64) (param $6 i64) (param $7 i64) (param $8 i64) (param $9 i64) (param $10 i64) (param $11 i64) (param $12 i64) (param $13 i64) (param $14 i64) (param $15 i64) (result i64) +;; CHECK-NEXT: (i64.extend_i32_u +;; CHECK-NEXT: (call $add +;; CHECK-NEXT: (i32.wrap_i64 +;; CHECK-NEXT: (local.get $0) +;; CHECK-NEXT: ) +;; CHECK-NEXT: (i32.wrap_i64 +;; CHECK-NEXT: (local.get $1) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) + +;; CHECK: (func $byn$fpcast-emu$unlegalized (param $0 i64) (param $1 i64) (param $2 i64) (param $3 i64) (param $4 i64) (param $5 i64) (param $6 i64) (param $7 i64) (param $8 i64) (param $9 i64) (param $10 i64) (param $11 i64) (param $12 i64) (param $13 i64) (param $14 i64) (param $15 i64) (result i64) +;; CHECK-NEXT: (call $unlegalized +;; CHECK-NEXT: (local.get $0) +;; CHECK-NEXT: ) +;; CHECK-NEXT: ) diff --git a/test/lit/passes/fpcast-emu-max-params-error.wast b/test/lit/passes/fpcast-emu-max-params-error.wast new file mode 100644 index 00000000000..8e58cfd24a9 --- /dev/null +++ b/test/lit/passes/fpcast-emu-max-params-error.wast @@ -0,0 +1,16 @@ +;; RUN: foreach %s %t not wasm-opt --fpcast-emu --pass-arg=fpcast-emu-export-thunks --pass-arg=max-func-params@1 2>&1 | filecheck %s + +;; Exported function with more parameters than max-func-params. +(module + ;; CHECK: Fatal: max-func-params needs to be at least 2 + (export "too_many_params" (func $f)) + (func $f (param i32 i32)) +) + +;; Function in element segment with more parameters than max-func-params. +(module + ;; CHECK: Fatal: max-func-params needs to be at least 2 + (table 1 funcref) + (elem (i32.const 0) $f) + (func $f (param i32 i32)) +) From 22969e7211dcf60ee309083c0c03786972f49d30 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 23 Nov 2026 16:29:36 -0800 Subject: [PATCH 3/3] Extract `exportThunks` and `getThunk` helpers Move the `--pass-arg=fpcast-emu-export-thunks` export loop out of `FuncCastEmulation::run` into `exportThunks`, and deduplicate the `funcThunks` lookup between table elements and exports via `getThunk`. --- src/passes/FuncCastEmulation.cpp | 106 +++++++++++++++++-------------- 1 file changed, 58 insertions(+), 48 deletions(-) diff --git a/src/passes/FuncCastEmulation.cpp b/src/passes/FuncCastEmulation.cpp index 0032b2b38e5..0c398797824 100644 --- a/src/passes/FuncCastEmulation.cpp +++ b/src/passes/FuncCastEmulation.cpp @@ -172,59 +172,13 @@ struct FuncCastEmulation : public Pass { if (!ref) { continue; } - auto [iter, inserted] = funcThunks.insert({ref->func, nullptr}); - if (inserted) { - iter->second = makeThunk(ref->func, module, numParams); - } - auto* thunk = iter->second; - ref->func = thunk->name; + ref->func = getThunk(ref->func, module, funcThunks, numParams)->name; ref->finalize(*module); } } if (hasArgument("fpcast-emu-export-thunks")) { - // Export fpcast$ thunks for non-imported function exports (excluding - // dynCall_* and already-prefixed fpcast$ exports) so dynamic linking can - // insert ABI-compatible thunks into the indirect function table. - std::vector> newExports; - Builder builder(*module); - for (auto& ex : module->exports) { - if (ex->kind != ExternalKind::Function) { - continue; - } - Name funcName = *ex->getInternalName(); - if (module->getFunction(funcName)->imported()) { - continue; - } - // Under !WASM_BIGINT, LegalizeJSInterface runs earlier and exports - // `orig$foo` for the unlegalized function while `foo` points to a - // legalstub. Wrap `orig$foo` as `fpcast$foo` and skip the `foo` stub. - std::string exportName = ex->name.toString(); - if (ex->name.startsWith("orig$")) { - exportName = exportName.substr(5); - } else if (module->getExportOrNull("orig$" + exportName)) { - // `ex` is the legalized stub; its companion `orig$` export provides - // `fpcast$` instead. - continue; - } - if (Name(exportName).startsWith("dynCall_") || - Name(exportName).startsWith("fpcast$")) { - continue; - } - Name fpcastName = "fpcast$" + exportName; - if (module->getExportOrNull(fpcastName)) { - continue; - } - auto [iter, inserted] = funcThunks.insert({funcName, nullptr}); - if (inserted) { - iter->second = makeThunk(funcName, module, numParams); - } - newExports.push_back(builder.makeExport( - fpcastName, iter->second->name, ExternalKind::Function)); - } - for (auto& ex : newExports) { - module->addExport(std::move(ex)); - } + exportThunks(module, funcThunks, numParams); } // update call_indirects @@ -232,6 +186,62 @@ struct FuncCastEmulation : public Pass { } private: + // Export fpcast$ thunks for all function exports (excluding imported + // functions, dynCall_* and already-prefixed fpcast$ exports) so dynamic + // linking can insert ABI-compatible thunks into the indirect function table. + void exportThunks(Module* module, + std::unordered_map& funcThunks, + Index numParams) { + std::vector> newExports; + Builder builder(*module); + for (auto& ex : module->exports) { + if (ex->kind != ExternalKind::Function) { + continue; + } + Name funcName = *ex->getInternalName(); + if (module->getFunction(funcName)->imported()) { + continue; + } + // Under !WASM_BIGINT, LegalizeJSInterface runs earlier and exports + // `orig$foo` for the unlegalized function while `foo` points to a + // legalstub. Wrap `orig$foo` as `fpcast$foo` and skip the `foo` stub. + std::string exportName = ex->name.toString(); + if (ex->name.startsWith("orig$")) { + exportName = exportName.substr(5); + } else if (module->getExportOrNull("orig$" + exportName)) { + // `ex` is the legalized stub; its companion `orig$` export provides + // `fpcast$` instead. + continue; + } + if (Name(exportName).startsWith("dynCall_") || + Name(exportName).startsWith("fpcast$")) { + continue; + } + Name fpcastName = "fpcast$" + exportName; + if (module->getExportOrNull(fpcastName)) { + continue; + } + auto* thunk = getThunk(funcName, module, funcThunks, numParams); + newExports.push_back( + builder.makeExport(fpcastName, thunk->name, ExternalKind::Function)); + } + for (auto& ex : newExports) { + module->addExport(std::move(ex)); + } + } + + // Returns the thunk for `name`, creating and caching it if needed. + Function* getThunk(Name name, + Module* module, + std::unordered_map& funcThunks, + Index numParams) { + auto [iter, inserted] = funcThunks.insert({name, nullptr}); + if (inserted) { + iter->second = makeThunk(name, module, numParams); + } + return iter->second; + } + // Creates a thunk for a function, casting args and return value as needed. Function* makeThunk(Name name, Module* module, Index numParams) { Name thunk = std::string("byn$fpcast-emu$") + name.toString();