Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/passes/LegalizeJSInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "ir/element-utils.h"
#include "ir/import-utils.h"
#include "ir/literal-utils.h"
#include "ir/names.h"
#include "ir/utils.h"
#include "pass.h"
#include "shared-constants.h"
Expand Down Expand Up @@ -269,12 +270,14 @@ struct LegalizeJSInterface : public Pass {
Function* makeLegalStubForCalledImport(Function* im, Module* module) {
Builder builder(*module);
auto legalIm = std::make_unique<Function>();
legalIm->name = Name(std::string("legalimport$") + im->name.toString());
legalIm->name = Names::getValidFunctionName(
*module, Name(std::string("legalimport$") + im->name.toString()));
legalIm->module = im->module;
legalIm->base = im->base;
legalIm->hasExplicitName = true;
auto stub = std::make_unique<Function>();
stub->name = Name(std::string("legalfunc$") + im->name.toString());
stub->name = Names::getValidFunctionName(
*module, Name(std::string("legalfunc$") + im->name.toString()));
stub->type = im->type.with(Exact);
stub->hasExplicitName = true;

Expand Down Expand Up @@ -308,13 +311,8 @@ struct LegalizeJSInterface : public Pass {
legalIm->type =
Type(Signature(Type(params), call->type), NonNullable, Inexact);

auto* stubPtr = stub.get();
if (!module->getFunctionOrNull(stub->name)) {
module->addFunction(std::move(stub));
}
if (!module->getFunctionOrNull(legalIm->name)) {
module->addFunction(std::move(legalIm));
}
auto* stubPtr = module->addFunction(std::move(stub));
module->addFunction(std::move(legalIm));
return stubPtr;
}

Expand Down
55 changes: 55 additions & 0 deletions test/lit/passes/legalize-js-interface-name-collision.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
;; RUN: wasm-opt %s --legalize-js-interface -S -o - | filecheck %s

;; The module already has functions named like the ones the pass would create
;; for the illegal import. The pass must pick fresh names instead of reusing
;; (or failing to add) the existing ones.

(module
(import "env" "imp" (func $imp (param i64) (result i32)))
;; CHECK: (type $0 (func (param i64) (result i32)))

;; CHECK: (type $1 (func (result f32)))

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

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

;; CHECK: (import "env" "imp" (func $legalimport$imp_4 (param i32 i32) (result i32)))

;; CHECK: (func $legalfunc$imp (param $0 i64) (result i32)
;; CHECK-NEXT: (i32.const 7)
;; CHECK-NEXT: )
(func $legalfunc$imp (param i64) (result i32)
(i32.const 7)
)
;; CHECK: (func $legalimport$imp (result f32)
;; CHECK-NEXT: (f32.const 8)
;; CHECK-NEXT: )
(func $legalimport$imp (result f32)
(f32.const 8)
)
;; CHECK: (func $f (result i32)
;; CHECK-NEXT: (call $legalfunc$imp_4
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $f (result i32)
(call $imp
(i64.const 1)
)
)
)
;; CHECK: (func $legalfunc$imp_4 (param $0 i64) (result i32)
;; CHECK-NEXT: (call $legalimport$imp_4
;; CHECK-NEXT: (i32.wrap_i64
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.wrap_i64
;; CHECK-NEXT: (i64.shr_u
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (i64.const 32)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
Loading