summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2021-07-12 17:15:50 -0700
committerGitHub <noreply@github.com>2021-07-12 17:15:50 -0700
commit58ce30422a24f44dcac6f5d1b6950d900582f0f2 (patch)
treeb3f75d30ada94bed39a2f73d80665df4502551cb
parente8137784824bb2578b8ecbe2f76e09e20a1e5442 (diff)
downloadbinaryen-58ce30422a24f44dcac6f5d1b6950d900582f0f2.tar.gz
binaryen-58ce30422a24f44dcac6f5d1b6950d900582f0f2.tar.bz2
binaryen-58ce30422a24f44dcac6f5d1b6950d900582f0f2.zip
Improve NameTypes to keep reasonable names (#3977)
We only set a name now if there was no name, or the existing name was really really long.
-rw-r--r--src/passes/NameTypes.cpp14
-rw-r--r--test/lit/passes/name-types.wast16
2 files changed, 23 insertions, 7 deletions
diff --git a/src/passes/NameTypes.cpp b/src/passes/NameTypes.cpp
index 2939f9ad5..a2c5016bc 100644
--- a/src/passes/NameTypes.cpp
+++ b/src/passes/NameTypes.cpp
@@ -21,14 +21,14 @@
//
// Ensures each type has a name. This can be useful for debugging.
//
-// TODO: keep existing useful (short-enough) names, and just replace ones that
-// are bothersome
-//
using namespace std;
namespace wasm {
+// An arbitrary limit, above which we rename types.
+static const size_t NameLenLimit = 20;
+
struct NameTypes : public Pass {
void run(PassRunner* runner, Module* module) override {
// Find all the types.
@@ -36,10 +36,14 @@ struct NameTypes : public Pass {
std::unordered_map<HeapType, Index> typeIndices;
ModuleUtils::collectHeapTypes(*module, types, typeIndices);
- // Ensure simple names.
+ // Ensure simple names. If a name already exists, and is short enough, keep
+ // it.
size_t i = 0;
for (auto& type : types) {
- module->typeNames[type].name = "type$" + std::to_string(i++);
+ if (module->typeNames.count(type) == 0 ||
+ module->typeNames[type].name.size() >= NameLenLimit) {
+ module->typeNames[type].name = "type$" + std::to_string(i++);
+ }
}
}
};
diff --git a/test/lit/passes/name-types.wast b/test/lit/passes/name-types.wast
index 03b775f20..197a55226 100644
--- a/test/lit/passes/name-types.wast
+++ b/test/lit/passes/name-types.wast
@@ -1,12 +1,24 @@
-;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
+;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
;; RUN: wasm-opt %s -all --name-types -S -o - | filecheck %s
(module
+ ;; An obnoxious name that will be renamed.
(type $obnoxious-super-long-type-name_____________________________1 (struct))
- ;; CHECK: (func $foo (param $x (ref $type$1))
+
+ ;; A reasonable name that will be kept.
+ ;; CHECK: (type $type$0 (func (param (ref $type$1) (ref $reasonable-name))))
+
+ ;; CHECK: (type $type$1 (struct ))
+
+ ;; CHECK: (type $reasonable-name (struct (field i32)))
+ (type $reasonable-name (struct (field i32)))
+
+ ;; CHECK: (func $foo (param $x (ref $type$1)) (param $y (ref $reasonable-name))
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
(func $foo
+ ;; Use the types to keep them alive.
(param $x (ref $obnoxious-super-long-type-name_____________________________1))
+ (param $y (ref $reasonable-name))
)
)