summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md3
-rw-r--r--src/passes/ReorderFunctions.cpp18
-rw-r--r--src/passes/pass.cpp3
-rw-r--r--src/passes/passes.h1
-rw-r--r--test/lit/help/wasm-opt.test3
-rw-r--r--test/lit/help/wasm2js.test3
-rw-r--r--test/lit/passes/reorder-functions-by-name.wast107
7 files changed, 138 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 57e7272ba..69948d3f9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,9 @@ full changeset diff at the end of each section.
Current Trunk
-------------
+- Add a pass to reorder functions by name, which can be useful for debugging
+ (e.g. comparisons after optimizations), `--reorder-functions-by-name`.
+
v114
----
diff --git a/src/passes/ReorderFunctions.cpp b/src/passes/ReorderFunctions.cpp
index 96ebfc048..cf3c8bb1f 100644
--- a/src/passes/ReorderFunctions.cpp
+++ b/src/passes/ReorderFunctions.cpp
@@ -97,4 +97,22 @@ struct ReorderFunctions : public Pass {
Pass* createReorderFunctionsPass() { return new ReorderFunctions(); }
+struct ReorderFunctionsByName : public Pass {
+ // Only reorders functions, does not change their contents.
+ bool requiresNonNullableLocalFixups() override { return false; }
+
+ void run(Module* module) override {
+ std::sort(module->functions.begin(),
+ module->functions.end(),
+ [](const std::unique_ptr<Function>& a,
+ const std::unique_ptr<Function>& b) -> bool {
+ return a->name < b->name;
+ });
+ }
+};
+
+Pass* createReorderFunctionsByNamePass() {
+ return new ReorderFunctionsByName();
+}
+
} // namespace wasm
diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp
index adaa42e04..2b89371c4 100644
--- a/src/passes/pass.cpp
+++ b/src/passes/pass.cpp
@@ -375,6 +375,9 @@ void PassRegistry::registerPasses() {
registerPass("remove-unused-types",
"remove unused private GC types",
createRemoveUnusedTypesPass);
+ registerPass("reorder-functions-by-name",
+ "sorts functions by name (useful for debugging)",
+ createReorderFunctionsByNamePass);
registerPass("reorder-functions",
"sorts functions by access frequency",
createReorderFunctionsPass);
diff --git a/src/passes/passes.h b/src/passes/passes.h
index d8855d4a8..8cc092f98 100644
--- a/src/passes/passes.h
+++ b/src/passes/passes.h
@@ -118,6 +118,7 @@ Pass* createRemoveUnusedModuleElementsPass();
Pass* createRemoveUnusedNonFunctionModuleElementsPass();
Pass* createRemoveUnusedNamesPass();
Pass* createRemoveUnusedTypesPass();
+Pass* createReorderFunctionsByNamePass();
Pass* createReorderFunctionsPass();
Pass* createReorderGlobalsPass();
Pass* createReorderGlobalsAlwaysPass();
diff --git a/test/lit/help/wasm-opt.test b/test/lit/help/wasm-opt.test
index 30369e62f..d57e5fd1d 100644
--- a/test/lit/help/wasm-opt.test
+++ b/test/lit/help/wasm-opt.test
@@ -373,6 +373,9 @@
;; CHECK-NEXT: --reorder-functions sorts functions by access
;; CHECK-NEXT: frequency
;; CHECK-NEXT:
+;; CHECK-NEXT: --reorder-functions-by-name sorts functions by name (useful
+;; CHECK-NEXT: for debugging)
+;; CHECK-NEXT:
;; CHECK-NEXT: --reorder-globals sorts globals by access
;; CHECK-NEXT: frequency
;; CHECK-NEXT:
diff --git a/test/lit/help/wasm2js.test b/test/lit/help/wasm2js.test
index 916dbbb4d..2994a1f59 100644
--- a/test/lit/help/wasm2js.test
+++ b/test/lit/help/wasm2js.test
@@ -332,6 +332,9 @@
;; CHECK-NEXT: --reorder-functions sorts functions by access
;; CHECK-NEXT: frequency
;; CHECK-NEXT:
+;; CHECK-NEXT: --reorder-functions-by-name sorts functions by name (useful
+;; CHECK-NEXT: for debugging)
+;; CHECK-NEXT:
;; CHECK-NEXT: --reorder-globals sorts globals by access
;; CHECK-NEXT: frequency
;; CHECK-NEXT:
diff --git a/test/lit/passes/reorder-functions-by-name.wast b/test/lit/passes/reorder-functions-by-name.wast
new file mode 100644
index 000000000..ab5b35bc2
--- /dev/null
+++ b/test/lit/passes/reorder-functions-by-name.wast
@@ -0,0 +1,107 @@
+;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
+;; RUN: foreach %s %t wasm-opt -all --reorder-functions-by-name -S -o - | filecheck %s
+
+(module
+ ;; CHECK: (type $none_=>_i32 (func (result i32)))
+
+ ;; CHECK: (func $a (type $none_=>_i32) (result i32)
+ ;; CHECK-NEXT: (i32.const 10)
+ ;; CHECK-NEXT: )
+
+ ;; CHECK: (func $b (type $none_=>_i32) (result i32)
+ ;; CHECK-NEXT: (i32.const 20)
+ ;; CHECK-NEXT: )
+
+ ;; CHECK: (func $c (type $none_=>_i32) (result i32)
+ ;; CHECK-NEXT: (i32.const 30)
+ ;; CHECK-NEXT: )
+ (func $c (result i32)
+ (i32.const 30)
+ )
+
+ (func $b (result i32)
+ (i32.const 20)
+ )
+
+ (func $a (result i32)
+ (i32.const 10)
+ )
+)
+
+(module
+ ;; CHECK: (type $none_=>_i32 (func (result i32)))
+
+ ;; CHECK: (func $a (type $none_=>_i32) (result i32)
+ ;; CHECK-NEXT: (i32.const 10)
+ ;; CHECK-NEXT: )
+ (func $a (result i32)
+ (i32.const 10)
+ )
+
+ ;; CHECK: (func $b (type $none_=>_i32) (result i32)
+ ;; CHECK-NEXT: (i32.const 20)
+ ;; CHECK-NEXT: )
+ (func $b (result i32)
+ (i32.const 20)
+ )
+
+ ;; CHECK: (func $c (type $none_=>_i32) (result i32)
+ ;; CHECK-NEXT: (i32.const 30)
+ ;; CHECK-NEXT: )
+ (func $c (result i32)
+ (i32.const 30)
+ )
+)
+
+(module
+ ;; CHECK: (type $none_=>_i32 (func (result i32)))
+
+ ;; CHECK: (func $a (type $none_=>_i32) (result i32)
+ ;; CHECK-NEXT: (i32.const 10)
+ ;; CHECK-NEXT: )
+
+ ;; CHECK: (func $b (type $none_=>_i32) (result i32)
+ ;; CHECK-NEXT: (i32.const 20)
+ ;; CHECK-NEXT: )
+ (func $b (result i32)
+ (i32.const 20)
+ )
+
+ (func $a (result i32)
+ (i32.const 10)
+ )
+
+ ;; CHECK: (func $c (type $none_=>_i32) (result i32)
+ ;; CHECK-NEXT: (i32.const 30)
+ ;; CHECK-NEXT: )
+ (func $c (result i32)
+ (i32.const 30)
+ )
+)
+
+(module
+ ;; CHECK: (type $none_=>_i32 (func (result i32)))
+
+ ;; CHECK: (func $a (type $none_=>_i32) (result i32)
+ ;; CHECK-NEXT: (i32.const 10)
+ ;; CHECK-NEXT: )
+
+ ;; CHECK: (func $b (type $none_=>_i32) (result i32)
+ ;; CHECK-NEXT: (i32.const 20)
+ ;; CHECK-NEXT: )
+
+ ;; CHECK: (func $c (type $none_=>_i32) (result i32)
+ ;; CHECK-NEXT: (i32.const 30)
+ ;; CHECK-NEXT: )
+ (func $c (result i32)
+ (i32.const 30)
+ )
+
+ (func $a (result i32)
+ (i32.const 10)
+ )
+
+ (func $b (result i32)
+ (i32.const 20)
+ )
+)