summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ir/names.h27
-rw-r--r--src/passes/MemoryPacking.cpp7
2 files changed, 30 insertions, 4 deletions
diff --git a/src/ir/names.h b/src/ir/names.h
index a589571bf..3d49a4a36 100644
--- a/src/ir/names.h
+++ b/src/ir/names.h
@@ -46,6 +46,33 @@ inline void ensureNames(Function* func) {
}
}
+// Given a root of a name, finds a valid name with perhaps a number appended
+// to it, by calling a function to check if a name is valid.
+inline Name
+getValidName(Module& module, Name root, std::function<bool(Name)> check) {
+ if (check(root)) {
+ return root;
+ }
+ auto prefixed = std::string(root.str) + '_';
+ Index num = 0;
+ while (1) {
+ auto name = prefixed + std::to_string(num);
+ if (check(name)) {
+ return name;
+ }
+ num++;
+ }
+}
+
+inline Name getValidGlobalName(Module& module, Name root) {
+ return getValidName(
+ module, root, [&](Name test) { return !module.getGlobalOrNull(test); });
+}
+inline Name getValidFunctionName(Module& module, Name root) {
+ return getValidName(
+ module, root, [&](Name test) { return !module.getFunctionOrNull(test); });
+}
+
} // namespace Names
} // namespace wasm
diff --git a/src/passes/MemoryPacking.cpp b/src/passes/MemoryPacking.cpp
index 6003274e8..d4d75e6d6 100644
--- a/src/passes/MemoryPacking.cpp
+++ b/src/passes/MemoryPacking.cpp
@@ -30,6 +30,7 @@
#include "ir/manipulation.h"
#include "ir/module-utils.h"
+#include "ir/names.h"
#include "ir/utils.h"
#include "pass.h"
#include "support/space.h"
@@ -90,8 +91,6 @@ makeGtShiftedMemorySize(Builder& builder, Module& module, MemoryInit* curr) {
} // anonymous namespace
struct MemoryPacking : public Pass {
- size_t dropStateGlobalCount = 0;
-
// FIXME: Chrome has a bug decoding section indices that prevents it from
// using more than 63. Just use WebLimitations::MaxDataSegments once this is
// fixed. See https://bugs.chromium.org/p/v8/issues/detail?id=10151.
@@ -559,8 +558,8 @@ void MemoryPacking::createReplacements(Module* module,
if (dropStateGlobal != Name()) {
return dropStateGlobal;
}
- dropStateGlobal = Name(std::string("__mem_segment_drop_state_") +
- std::to_string(dropStateGlobalCount++));
+ dropStateGlobal =
+ Names::getValidGlobalName(*module, "__mem_segment_drop_state");
module->addGlobal(builder.makeGlobal(dropStateGlobal,
Type::i32,
builder.makeConst(int32_t(0)),