From f8dded8de55990c800ffe723b0b66eecc0067d86 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 17 Jan 2024 09:18:36 -0800 Subject: [PATCH 1/6] prep --- src/passes/SimplifyGlobals.cpp | 41 ++++++++++--- ...e-unused-module-elements_all-features.wast | 58 +++++++++++++++++++ 2 files changed, 91 insertions(+), 8 deletions(-) diff --git a/src/passes/SimplifyGlobals.cpp b/src/passes/SimplifyGlobals.cpp index 0e66b592657..2b43b72cb01 100644 --- a/src/passes/SimplifyGlobals.cpp +++ b/src/passes/SimplifyGlobals.cpp @@ -646,23 +646,48 @@ struct SimplifyGlobals : public Pass { // since we do know the value during startup, it can't be modified until // code runs. void propagateConstantsToGlobals() { - // Go over the list of globals in order, which is the order of - // initialization as well, tracking their constant values. + Builder builder(*module); + + // We will note constant globals here as we compute them. std::map constantGlobals; + + // Given an init expression (something like the init of a global or a + // segment), see if it is a simple global.get of a constant that we can + // apply. + auto applyGlobals = [&](Expression*& init) { + if (auto* get = init->dynCast()) { + auto iter = constantGlobals.find(get->name); + if (iter != constantGlobals.end()) { + init = builder.makeConstantExpression(iter->second); + } + } + }; + + // Go over the list of globals first, and note their constant values as we + // go, as well as applying them where possible. for (auto& global : module->globals) { if (!global->imported()) { if (Properties::isConstantExpression(global->init)) { constantGlobals[global->name] = getLiteralsFromConstExpression(global->init); - } else if (auto* get = global->init->dynCast()) { - auto iter = constantGlobals.find(get->name); - if (iter != constantGlobals.end()) { - Builder builder(*module); - global->init = builder.makeConstantExpression(iter->second); - } + } else { + applyGlobals(global->init); } } } +#if 0 + // Go over other things with inits and apply globals there. + for (auto& elementSegment : module->elementSegments) { + if (!elementSegment->imported()) { + applyGlobals(elementSegment->offset); + } + } + for (auto& dataSegment : module->dataSegments) { + if (!dataSegment->imported()) { + applyGlobals(dataSegment->offset); + } + } + #endif } // Constant propagation part 2: apply the values of immutable globals diff --git a/test/lit/passes/remove-unused-module-elements_all-features.wast b/test/lit/passes/remove-unused-module-elements_all-features.wast index 23f01878734..7b1caf54703 100644 --- a/test/lit/passes/remove-unused-module-elements_all-features.wast +++ b/test/lit/passes/remove-unused-module-elements_all-features.wast @@ -1026,3 +1026,61 @@ (return_call $import) ) ) + +;; Apply constant globals to segment offsets. +(module + ;; CHECK: (type $0 (func)) + + ;; CHECK: (import "env" "memory" (memory $memory 256)) + (import "env" "memory" (memory $memory 256)) + + ;; CHECK: (import "env" "table" (table $table 0 funcref)) + (import "env" "table" (table $table 0 funcref)) + + ;; CHECK: (import "env" "imported" (global $imported i32)) + (import "env" "imported" (global $imported i32)) + + ;; CHECK: (global $defined i32 (i32.const 42)) + (global $defined i32 (i32.const 42)) + + ;; CHECK: (data $0 (global.get $imported) "hello, world!") + (data $0 (global.get $imported) "hello, world!") + + ;; CHECK: (data $1 (global.get $defined) "hello, world!") + (data $1 (global.get $defined) "hello, world!") + + ;; CHECK: (elem $0 (global.get $imported) $func) + (elem $0 (global.get $imported) $func) + + ;; CHECK: (elem $1 (global.get $defined) $func $func) + (elem $1 (global.get $defined) $func $func) + + ;; CHECK: (export "func" (func $func)) + (export "func" (func $func)) + + ;; CHECK: (func $func (type $0) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.load + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (table.get $table + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $func + ;; Use the table and memory to avoid DCE. + (drop + (i32.load + (i32.const 0) + ) + ) + (drop + (table.get $table + (i32.const 0) + ) + ) + ) +) From 69d101cc2dd80b1003d4deff2b5cbe1b99f66f27 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 17 Jan 2024 09:21:57 -0800 Subject: [PATCH 2/6] work --- src/passes/SimplifyGlobals.cpp | 11 +-- ...e-unused-module-elements_all-features.wast | 57 --------------- test/lit/passes/simplify-globals-offsets.wast | 71 +++++++++++++++++++ 3 files changed, 74 insertions(+), 65 deletions(-) create mode 100644 test/lit/passes/simplify-globals-offsets.wast diff --git a/src/passes/SimplifyGlobals.cpp b/src/passes/SimplifyGlobals.cpp index 2b43b72cb01..3e84539c128 100644 --- a/src/passes/SimplifyGlobals.cpp +++ b/src/passes/SimplifyGlobals.cpp @@ -675,19 +675,14 @@ struct SimplifyGlobals : public Pass { } } } -#if 0 + // Go over other things with inits and apply globals there. for (auto& elementSegment : module->elementSegments) { - if (!elementSegment->imported()) { - applyGlobals(elementSegment->offset); - } + applyGlobals(elementSegment->offset); } for (auto& dataSegment : module->dataSegments) { - if (!dataSegment->imported()) { - applyGlobals(dataSegment->offset); - } + applyGlobals(dataSegment->offset); } - #endif } // Constant propagation part 2: apply the values of immutable globals diff --git a/test/lit/passes/remove-unused-module-elements_all-features.wast b/test/lit/passes/remove-unused-module-elements_all-features.wast index 7b1caf54703..779569915eb 100644 --- a/test/lit/passes/remove-unused-module-elements_all-features.wast +++ b/test/lit/passes/remove-unused-module-elements_all-features.wast @@ -1027,60 +1027,3 @@ ) ) -;; Apply constant globals to segment offsets. -(module - ;; CHECK: (type $0 (func)) - - ;; CHECK: (import "env" "memory" (memory $memory 256)) - (import "env" "memory" (memory $memory 256)) - - ;; CHECK: (import "env" "table" (table $table 0 funcref)) - (import "env" "table" (table $table 0 funcref)) - - ;; CHECK: (import "env" "imported" (global $imported i32)) - (import "env" "imported" (global $imported i32)) - - ;; CHECK: (global $defined i32 (i32.const 42)) - (global $defined i32 (i32.const 42)) - - ;; CHECK: (data $0 (global.get $imported) "hello, world!") - (data $0 (global.get $imported) "hello, world!") - - ;; CHECK: (data $1 (global.get $defined) "hello, world!") - (data $1 (global.get $defined) "hello, world!") - - ;; CHECK: (elem $0 (global.get $imported) $func) - (elem $0 (global.get $imported) $func) - - ;; CHECK: (elem $1 (global.get $defined) $func $func) - (elem $1 (global.get $defined) $func $func) - - ;; CHECK: (export "func" (func $func)) - (export "func" (func $func)) - - ;; CHECK: (func $func (type $0) - ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (i32.load - ;; CHECK-NEXT: (i32.const 0) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (table.get $table - ;; CHECK-NEXT: (i32.const 0) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: ) - (func $func - ;; Use the table and memory to avoid DCE. - (drop - (i32.load - (i32.const 0) - ) - ) - (drop - (table.get $table - (i32.const 0) - ) - ) - ) -) diff --git a/test/lit/passes/simplify-globals-offsets.wast b/test/lit/passes/simplify-globals-offsets.wast new file mode 100644 index 00000000000..f2c768e9301 --- /dev/null +++ b/test/lit/passes/simplify-globals-offsets.wast @@ -0,0 +1,71 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. +;; NOTE: This test was ported using port_passes_tests_to_lit.py and could be cleaned up. + +;; RUN: foreach %s %t wasm-opt --simplify-globals -all -S -o - | filecheck %s + +;; Apply constant globals to segment offsets. +(module + ;; CHECK: (type $0 (func)) + + ;; CHECK: (import "env" "memory" (memory $memory 256)) + (import "env" "memory" (memory $memory 256)) + + ;; CHECK: (import "env" "table" (table $table 0 funcref)) + (import "env" "table" (table $table 0 funcref)) + + ;; CHECK: (import "env" "imported" (global $imported i32)) + (import "env" "imported" (global $imported i32)) + + ;; CHECK: (global $defined i32 (i32.const 42)) + (global $defined i32 (i32.const 42)) + + ;; CHECK: (global $use-defined i32 (i32.const 42)) + (global $use-defined i32 (global.get $defined)) + + ;; CHECK: (data $0 (global.get $imported) "hello, world!") + (data $0 (global.get $imported) "hello, world!") + + ;; CHECK: (data $1 (i32.const 42) "hello, world!") + (data $1 (global.get $defined) "hello, world!") + + ;; CHECK: (elem $0 (global.get $imported) $func) + (elem $0 (global.get $imported) $func) + + ;; CHECK: (elem $1 (i32.const 42) $func $func) + (elem $1 (global.get $defined) $func $func) + + ;; CHECK: (export "func" (func $func)) + (export "func" (func $func)) + + ;; CHECK: (func $func (type $0) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.load + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (table.get $table + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.const 42) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $func + ;; Use things to avoid DCE. + (drop + (i32.load + (i32.const 0) + ) + ) + (drop + (table.get $table + (i32.const 0) + ) + ) + (drop + (global.get $use-defined) + ) + ) +) From 05e191b4729cd71931d4a938ce8f1541f38a8d9e Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 17 Jan 2024 09:22:27 -0800 Subject: [PATCH 3/6] work --- test/lit/passes/remove-unused-module-elements_all-features.wast | 1 - 1 file changed, 1 deletion(-) diff --git a/test/lit/passes/remove-unused-module-elements_all-features.wast b/test/lit/passes/remove-unused-module-elements_all-features.wast index 779569915eb..23f01878734 100644 --- a/test/lit/passes/remove-unused-module-elements_all-features.wast +++ b/test/lit/passes/remove-unused-module-elements_all-features.wast @@ -1026,4 +1026,3 @@ (return_call $import) ) ) - From f1611326ca72e71b7dea3163e0ed66bcede8c312 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 17 Jan 2024 09:25:18 -0800 Subject: [PATCH 4/6] comment --- test/lit/passes/simplify-globals-offsets.wast | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/lit/passes/simplify-globals-offsets.wast b/test/lit/passes/simplify-globals-offsets.wast index f2c768e9301..7546560299d 100644 --- a/test/lit/passes/simplify-globals-offsets.wast +++ b/test/lit/passes/simplify-globals-offsets.wast @@ -3,7 +3,9 @@ ;; RUN: foreach %s %t wasm-opt --simplify-globals -all -S -o - | filecheck %s -;; Apply constant globals to segment offsets. +;; Apply constant globals to segment offsets. The non-imported global.gets will +;; be applied in the segments named $1. + (module ;; CHECK: (type $0 (func)) From 06ccc63c56a89500d6bc48cca9a0b5162675b385 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 17 Jan 2024 09:29:43 -0800 Subject: [PATCH 5/6] finish --- src/passes/SimplifyGlobals.cpp | 4 ++++ test/lit/passes/simplify-globals-offsets.wast | 23 +++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/passes/SimplifyGlobals.cpp b/src/passes/SimplifyGlobals.cpp index 3e84539c128..3baec440931 100644 --- a/src/passes/SimplifyGlobals.cpp +++ b/src/passes/SimplifyGlobals.cpp @@ -655,6 +655,10 @@ struct SimplifyGlobals : public Pass { // segment), see if it is a simple global.get of a constant that we can // apply. auto applyGlobals = [&](Expression*& init) { + if (!init) { + // This is the init of a passive segment, which is null. + return; + } if (auto* get = init->dynCast()) { auto iter = constantGlobals.find(get->name); if (iter != constantGlobals.end()) { diff --git a/test/lit/passes/simplify-globals-offsets.wast b/test/lit/passes/simplify-globals-offsets.wast index 7546560299d..52ba5504010 100644 --- a/test/lit/passes/simplify-globals-offsets.wast +++ b/test/lit/passes/simplify-globals-offsets.wast @@ -4,7 +4,7 @@ ;; RUN: foreach %s %t wasm-opt --simplify-globals -all -S -o - | filecheck %s ;; Apply constant globals to segment offsets. The non-imported global.gets will -;; be applied in the segments named $1. +;; be applied in the segments named $use-defined. (module ;; CHECK: (type $0 (func)) @@ -24,17 +24,20 @@ ;; CHECK: (global $use-defined i32 (i32.const 42)) (global $use-defined i32 (global.get $defined)) - ;; CHECK: (data $0 (global.get $imported) "hello, world!") - (data $0 (global.get $imported) "hello, world!") + ;; CHECK: (data $use-imported (global.get $imported) "hello, world!") + (data $use-imported (global.get $imported) "hello, world!") - ;; CHECK: (data $1 (i32.const 42) "hello, world!") - (data $1 (global.get $defined) "hello, world!") + ;; CHECK: (data $use-defined (i32.const 42) "hello, world!") + (data $use-defined (global.get $defined) "hello, world!") - ;; CHECK: (elem $0 (global.get $imported) $func) - (elem $0 (global.get $imported) $func) + ;; CHECK: (data $dropped "hello, world!") + (data $dropped "hello, world!") - ;; CHECK: (elem $1 (i32.const 42) $func $func) - (elem $1 (global.get $defined) $func $func) + ;; CHECK: (elem $use-imported (global.get $imported) $func) + (elem $use-imported (global.get $imported) $func) + + ;; CHECK: (elem $use-defined (i32.const 42) $func $func) + (elem $use-defined (global.get $defined) $func $func) ;; CHECK: (export "func" (func $func)) (export "func" (func $func)) @@ -53,6 +56,7 @@ ;; CHECK-NEXT: (drop ;; CHECK-NEXT: (i32.const 42) ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (data.drop $dropped) ;; CHECK-NEXT: ) (func $func ;; Use things to avoid DCE. @@ -69,5 +73,6 @@ (drop (global.get $use-defined) ) + (data.drop $dropped) ) ) From 961b83fd3923b30724267a01f9af902d2f22a673 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 18 Jan 2024 10:47:21 -0800 Subject: [PATCH 6/6] feedback --- test/lit/passes/simplify-globals-offsets.wast | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/lit/passes/simplify-globals-offsets.wast b/test/lit/passes/simplify-globals-offsets.wast index 52ba5504010..077c51e734d 100644 --- a/test/lit/passes/simplify-globals-offsets.wast +++ b/test/lit/passes/simplify-globals-offsets.wast @@ -30,6 +30,7 @@ ;; CHECK: (data $use-defined (i32.const 42) "hello, world!") (data $use-defined (global.get $defined) "hello, world!") + ;; A passive segment has no offset to test, which we should not error on. ;; CHECK: (data $dropped "hello, world!") (data $dropped "hello, world!") @@ -54,6 +55,9 @@ ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (global.get $imported) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop ;; CHECK-NEXT: (i32.const 42) ;; CHECK-NEXT: ) ;; CHECK-NEXT: (data.drop $dropped) @@ -70,6 +74,9 @@ (i32.const 0) ) ) + (drop + (global.get $imported) + ) (drop (global.get $use-defined) )