summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2021-11-05 16:05:35 -0700
committerGitHub <noreply@github.com>2021-11-05 23:05:35 +0000
commitb604ad935d1adc8498872e8afdb18df2f9b3b572 (patch)
tree773002bf5cd2428e7b97a69b77c98ab2f842aa0d
parent993ebb35dbd499bf36cd37bb79d695cd63767765 (diff)
downloadbinaryen-b604ad935d1adc8498872e8afdb18df2f9b3b572.tar.gz
binaryen-b604ad935d1adc8498872e8afdb18df2f9b3b572.tar.bz2
binaryen-b604ad935d1adc8498872e8afdb18df2f9b3b572.zip
[Wasm GC] Enable GlobalTypeOptimization (#4305)
Now that all known issues with that pass are fixed, enable it by default. This adds it in a place that seems to make sense on j2wasm, but in general multiple cycles of optimization will be needed. This adds a test showing that we run this pass and that it helps ConstantFieldPropagation by running before it.
-rw-r--r--src/passes/pass.cpp7
-rw-r--r--test/lit/passes/gto_and_cfp_in_O.wast44
2 files changed, 50 insertions, 1 deletions
diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp
index fd31dada8..3b0faa6c3 100644
--- a/src/passes/pass.cpp
+++ b/src/passes/pass.cpp
@@ -524,7 +524,12 @@ void PassRunner::addDefaultGlobalOptimizationPrePasses() {
}
if (wasm->features.hasGC() && getTypeSystem() == TypeSystem::Nominal &&
options.optimizeLevel >= 2) {
- // TODO: investigate enabling --gto and --remove-module-elements before cfp
+ // Global type optimization can remove fields that are not needed, which can
+ // remove ref.funcs that were once assigned to vtables but are no longer
+ // needed, which can allow more code to be removed globally. After those,
+ // constant field propagation can be more effective.
+ addIfNoDWARFIssues("gto");
+ addIfNoDWARFIssues("remove-unused-module-elements");
addIfNoDWARFIssues("cfp");
}
}
diff --git a/test/lit/passes/gto_and_cfp_in_O.wast b/test/lit/passes/gto_and_cfp_in_O.wast
new file mode 100644
index 000000000..dcff13a8c
--- /dev/null
+++ b/test/lit/passes/gto_and_cfp_in_O.wast
@@ -0,0 +1,44 @@
+;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
+
+;; RUN: foreach %s %t wasm-opt -O -all --nominal -S -o - | filecheck %s
+
+;; Test that -O, with nominal typing + GC enabled, will run global type
+;; optimization in conjunction with constant field propagation etc.
+
+(module
+ (type $struct (struct_subtype (field (mut funcref)) (field (mut i32)) data))
+
+ (global $glob (ref $struct) (struct.new $struct
+ (ref.func $by-ref)
+ (i32.const 100)
+ ))
+
+ (func $by-ref
+ ;; This function is kept alive by the reference in $glob. After we remove
+ ;; the field that the funcref is written to, we remove the funcref, which
+ ;; means this function can be removed.
+ ;;
+ ;; Once it is removed, this write no longer exists, and does not hamper
+ ;; constant field propagation from inferring the value of the i32 field.
+ (struct.set $struct 1
+ (global.get $glob)
+ (i32.const 200)
+ )
+ )
+
+ ;; CHECK: (type $none_=>_i32 (func_subtype (result i32) func))
+
+ ;; CHECK: (export "main" (func $main))
+
+ ;; CHECK: (func $main (; has Stack IR ;) (result i32)
+ ;; CHECK-NEXT: (i32.const 100)
+ ;; CHECK-NEXT: )
+ (func $main (export "main") (result i32)
+ ;; After all the above optimizations, we can infer that $main should simply
+ ;; return 100.
+ (struct.get $struct 1
+ (global.get $glob)
+ )
+ )
+)
+