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
37 changes: 35 additions & 2 deletions src/passes/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,50 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
// user-provided names and the fallback indexed names.
struct TypePrinter : TypeNameGeneratorBase<TypePrinter> {
PrintSExpression& parent;
IndexedTypeNameGenerator<> fallback;
DefaultTypeNameGenerator fallback;
std::unordered_map<HeapType, TypeNames> fallbackNames;

TypePrinter(PrintSExpression& parent, const std::vector<HeapType>& types)
: parent(parent), fallback(types) {}
: parent(parent) {
if (!parent.currModule) {
return;
}
std::unordered_set<Name> usedNames;
for (auto& [_, names] : parent.currModule->typeNames) {
usedNames.insert(names.name);
}
size_t i = 0;
// Use indices for any remaining type names, skipping any that are already
// used.
for (auto type : types) {
if (parent.currModule->typeNames.count(type)) {
++i;
continue;
}
Name name;
do {
name = std::to_string(i++);
} while (usedNames.count(name));
fallbackNames[type] = {name, {}};
}
}

TypeNames getNames(HeapType type) {
if (parent.currModule) {
if (auto it = parent.currModule->typeNames.find(type);
it != parent.currModule->typeNames.end()) {
return it->second;
}
// In principle we should always have at least a fallback name for every
// type in the module, so this lookup should never fail. In practice,
// though, the `printExpression` variants deliberately avoid walking the
// module to find unnamed types so they can be safely used in a
// function-parallel context. That means we can have a module but not
// have generated the fallback names, so this lookup can fail, in which
// case we generate a name on demand.
if (auto it = fallbackNames.find(type); it != fallbackNames.end()) {
return it->second;
}
}
return fallback.getNames(type);
}
Expand Down
2 changes: 1 addition & 1 deletion test/lit/passes/O4_disable-bulk-memory.wast
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
(type $7 (func (param i32 i32 i32)))
;; CHECK: (type $11 (func (param i32)))

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

;; CHECK: (type $8 (func (result f64)))
(type $8 (func (result f64)))
Expand Down
8 changes: 4 additions & 4 deletions test/lit/passes/coalesce-locals.wast
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

;; CHECK: (type $FUNCSIG$iii (func (param i32 i32) (result i32)))

;; CHECK: (type $4 (func (param f64 i32) (result i64)))
;; CHECK: (type $5 (func (param f64 i32) (result i64)))

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

Expand All @@ -22,11 +22,11 @@
(type $2 (func))
(type $3 (func (param i32 f32)))
(type $4 (func (param i32)))
;; CHECK: (type $7 (func (param i32) (result i32)))
;; CHECK: (type $8 (func (param i32) (result i32)))

;; CHECK: (type $8 (func (param i32 i32)))
;; CHECK: (type $9 (func (param i32 i32)))

;; CHECK: (type $9 (func (result f64)))
;; CHECK: (type $10 (func (result f64)))

;; CHECK: (import "env" "_emscripten_autodebug_i32" (func $_emscripten_autodebug_i32 (param i32 i32) (result i32)))
(import "env" "_emscripten_autodebug_i32" (func $_emscripten_autodebug_i32 (param i32 i32) (result i32)))
Expand Down
6 changes: 4 additions & 2 deletions test/lit/passes/dae-optimizing.wast
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
;; RUN: foreach %s %t wasm-opt --dae-optimizing -S -o - | filecheck %s

(module
;; CHECK: (type $0 (func (result i32)))
(type $0 (func (param f32) (result f32)))
;; CHECK: (type $1 (func (result f32)))
(type $1 (func (param f64 f32 f32 f64 f32 i64 f64) (result i32)))
;; CHECK: (type $3 (func (result i32)))

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

;; CHECK: (type $2 (func (param f64 f32 f32 f64 f32 i32 i32 f64) (result i32)))
(type $2 (func (param f64 f32 f32 f64 f32 i32 i32 f64) (result i32)))
;; CHECK: (global $global$0 (mut i32) (i32.const 10))
Expand Down
14 changes: 7 additions & 7 deletions test/lit/passes/flatten_all-features.wast
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
(type $2 (func (result i32)))
;; CHECK: (type $3 (func (param i32) (result i32)))
(type $3 (func (param i32) (result i32)))
;; CHECK: (type $4 (func (result f32)))
(type $4 (func (param i64 i64) (result i64)))
;; CHECK: (type $4 (func (param i64 i64) (result i64)))
;; CHECK: (type $5 (func (result f32)))

;; CHECK: (type $6 (func (result anyref)))
;; CHECK: (type $4 (func (param i64 i64) (result i64)))
(type $4 (func (param i64 i64) (result i64)))
;; CHECK: (type $7 (func (result anyref)))

;; CHECK: (global $x (mut i32) (i32.const 0))
(global $x (mut i32) (i32.const 0))
Expand Down Expand Up @@ -833,7 +833,7 @@
(i32.const 0)
)
)
;; CHECK: (func $a17 (type $4) (result f32)
;; CHECK: (func $a17 (type $5) (result f32)
;; CHECK-NEXT: (local $var$0 f32)
;; CHECK-NEXT: (local $1 f32)
;; CHECK-NEXT: (local $2 f32)
Expand Down Expand Up @@ -921,7 +921,7 @@
)
)
)
;; CHECK: (func $a19 (type $4) (result f32)
;; CHECK: (func $a19 (type $5) (result f32)
;; CHECK-NEXT: (block $label$0
;; CHECK-NEXT: (block $label$1
;; CHECK-NEXT: (unreachable)
Expand Down Expand Up @@ -3585,7 +3585,7 @@
;; targets an outer branch whose return type is a supertype of the br_if's
;; value type, we need the value to be set into two locals: one with the outer
;; block's type, and one with its value type.
;; CHECK: (func $subtype (type $6) (result anyref)
;; CHECK: (func $subtype (type $7) (result anyref)
;; CHECK-NEXT: (local $0 eqref)
;; CHECK-NEXT: (local $1 anyref)
;; CHECK-NEXT: (local $2 nullref)
Expand Down
4 changes: 2 additions & 2 deletions test/lit/passes/fpcast-emu.wast
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@
(type $0 (func (param i64)))
;; CHECK: (type $1 (func (param f32) (result i64)))
(type $1 (func (param f32) (result i64)))
;; CHECK: (type $1 (func (param i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64) (result i64)))
;; CHECK: (type $2 (func (param i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64) (result i64)))

;; CHECK: (global $global$0 (mut i32) (i32.const 10))
(global $global$0 (mut i32) (i32.const 10))
Expand All @@ -312,7 +312,7 @@
;; CHECK-NEXT: (global.set $global$0
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call_indirect (type $1)
;; CHECK-NEXT: (call_indirect (type $2)
;; CHECK-NEXT: (br $label$1
;; CHECK-NEXT: (i64.const 4294967295)
;; CHECK-NEXT: )
Expand Down
10 changes: 5 additions & 5 deletions test/lit/passes/instrument-memory.wast
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
(memory 256 256)
;; CHECK: (type $1 (func))
(type $1 (func))
;; CHECK: (type $1 (func (param i32 i32 i32 i32) (result i32)))
;; CHECK: (type $2 (func (param i32 i32 i32 i32) (result i32)))

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

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

;; CHECK: (type $4 (func (param i32 f32) (result f32)))
;; CHECK: (type $5 (func (param i32 f32) (result f32)))

;; CHECK: (type $5 (func (param i32 f64) (result f64)))
;; CHECK: (type $6 (func (param i32 f64) (result f64)))

;; CHECK: (import "env" "load_ptr" (func $load_ptr (param i32 i32 i32 i32) (result i32)))

Expand Down
10 changes: 5 additions & 5 deletions test/lit/passes/instrument-memory64.wast
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
(memory i64 256 256)
;; CHECK: (type $1 (func))
(type $1 (func))
;; CHECK: (type $1 (func (param i32 i32 i64 i64) (result i64)))
;; CHECK: (type $2 (func (param i32 i32 i64 i64) (result i64)))

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

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

;; CHECK: (type $4 (func (param i32 f32) (result f32)))
;; CHECK: (type $5 (func (param i32 f32) (result f32)))

;; CHECK: (type $5 (func (param i32 f64) (result f64)))
;; CHECK: (type $6 (func (param i32 f64) (result f64)))

;; CHECK: (import "env" "load_ptr" (func $load_ptr (param i32 i32 i64 i64) (result i64)))

Expand Down
4 changes: 1 addition & 3 deletions test/lit/passes/optimize-instructions-mvp.wast
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
;; RUN: wasm-opt %s --optimize-instructions --mvp-features -S -o - | filecheck %s

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

;; CHECK: (type $0 (func (param i32 i64)))
(type $0 (func (param i32 i64)))

;; CHECK: (import "a" "b" (func $get-f64 (result f64)))
(import "a" "b" (func $get-f64 (result f64)))
Expand Down
16 changes: 8 additions & 8 deletions test/lld/duplicate_imports.wat.out
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(module
(type $0 (func (param i32 f32 f64) (result f32)))
(type $1 (func (param i32 f64 f64) (result f32)))
(type $3 (func (param i32 f32 f64) (result f32)))
(type $4 (func (param i32 f64 f64) (result f32)))
(type $0 (func (param i32) (result i32)))
(type $1 (func (result i32)))
(type $2 (func))
(type $5 (func (param f32 f64) (result f32)))
(type $6 (func (param f64 f64) (result f32)))
(type $7 (func (param i64) (result i32)))
(type $8 (func (param i32 i32) (result i32)))
(type $8 (func (param f32 f64) (result f32)))
(type $9 (func (param f64 f64) (result f32)))
(type $10 (func (param i64) (result i32)))
(type $11 (func (param i32 i32) (result i32)))
(import "env" "puts" (func $puts1 (param i32) (result i32)))
(import "env" "invoke_ffd" (func $invoke_ffd (param i32 f32 f64) (result f32)))
(import "env" "invoke_ffd" (func $invoke_ffd2 (param i32 f64 f64) (result f32)))
Expand Down Expand Up @@ -37,14 +37,14 @@
(nop)
)
(func $dynCall_ffd (param $fptr i32) (param $0 f32) (param $1 f64) (result f32)
(call_indirect (type $5)
(call_indirect (type $8)
(local.get $0)
(local.get $1)
(local.get $fptr)
)
)
(func $dynCall_fdd (param $fptr i32) (param $0 f64) (param $1 f64) (result f32)
(call_indirect (type $6)
(call_indirect (type $9)
(local.get $0)
(local.get $1)
(local.get $fptr)
Expand Down
2 changes: 1 addition & 1 deletion test/passes/func-metrics.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func: ifs
Drop : 6
If : 4
(module
(type $0 (func))
(type $1 (func))
(type $0 (func (param i32)))
(global $glob i32 (i32.const 1337))
(memory $0 256 256)
Expand Down
12 changes: 6 additions & 6 deletions test/passes/post-emscripten.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(module
(type $0 (func (param i32 f32)))
(type $1 (func (param i32 i32 f32)))
(type $2 (func))
(type $1 (func (param i32 f32)))
(type $2 (func (param i32 i32 f32)))
(type $3 (func))
(import "env" "invoke_vif" (func $invoke_vif (param i32 i32 f32)))
(memory $0 256 256)
(table $0 7 7 funcref)
Expand Down Expand Up @@ -68,9 +68,9 @@
)
)
(module
(type $0 (func (param i32 i32 f32)))
(type $1 (func))
(type $2 (func (param i32 f32)))
(type $1 (func (param i32 i32 f32)))
(type $2 (func))
(type $3 (func (param i32 f32)))
(import "env" "glob" (global $glob i32))
(import "env" "invoke_vif" (func $invoke_vif (param i32 i32 f32)))
(memory $0 256 256)
Expand Down
Loading