Skip to content

Commit

Permalink
J2CLOpts: Add "precompute" and "remove-unused-brs" as additional cleanup
Browse files Browse the repository at this point in the history
This makes the cleanup of bodies of functions that have had constants hoisted from them more effective.
  • Loading branch information
rluble authored Apr 30, 2024
1 parent 497ffe2 commit 049ff7a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/passes/J2CLOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class ConstantHoister : public WalkerPass<PostWalker<ConstantHoister>> {
// TODO: maybe we should not introduce "nop" in the first place and try
// removing instead.
PassRunner runner(getModule());
runner.add("precompute");
runner.add("remove-unused-brs");
runner.add("vacuum");
runner.setIsNested(true);
runner.runOnFunction(curr);
Expand Down
52 changes: 52 additions & 0 deletions test/lit/passes/j2cl.wast
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,55 @@
(global.set $field@Foo (i32.const 1))
)
)


(module
;; CHECK: (type $0 (func (result i32)))

;; CHECK: (global $$var2@Zoo (mut i32) (i32.const 0))

;; CHECK: (global $$var1@Zoo i32 (i32.const 2))
(global $$var1@Zoo (mut i32) (i32.const 0))
(global $$var2@Zoo (mut i32) (i32.const 0))

;; CHECK: (export "getVar1_<once>_@Zoo" (func $getVar1_<once>_@Zoo))

;; CHECK: (func $getVar1_<once>_@Zoo (type $0) (result i32)
;; CHECK-NEXT: (i32.const 2)
;; CHECK-NEXT: )
(func $getVar1_<once>_@Zoo (export "getVar1_<once>_@Zoo") (result i32)
(if (global.get $$var1@Zoo)
(then
(return (global.get $$var1@Zoo))
)
)
(global.set $$var1@Zoo (i32.const 2))
(return (global.get $$var1@Zoo))
)

;; CHECK: (func $getVar2_<once>_@Zoo (type $0) (result i32)
;; CHECK-NEXT: (if
;; CHECK-NEXT: (global.get $$var2@Zoo)
;; CHECK-NEXT: (then
;; CHECK-NEXT: (return
;; CHECK-NEXT: (global.get $$var2@Zoo)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (global.set $$var2@Zoo
;; CHECK-NEXT: (call $getVar1_<once>_@Zoo)
;; CHECK-NEXT: )
;; CHECK-NEXT: (return
;; CHECK-NEXT: (global.get $$var2@Zoo)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $getVar2_<once>_@Zoo (result i32)
(if (global.get $$var2@Zoo)
(then
(return (global.get $$var2@Zoo))
)
)
(global.set $$var2@Zoo (call $getVar1_<once>_@Zoo))
(return (global.get $$var2@Zoo))
)
)

0 comments on commit 049ff7a

Please sign in to comment.