Skip to content

[wasm-split] Split globals' transitive global dependencies#8442

Open
aheejin wants to merge 1 commit intowasm_split_globalfrom
wasm_split_global_transitive
Open

[wasm-split] Split globals' transitive global dependencies#8442
aheejin wants to merge 1 commit intowasm_split_globalfrom
wasm_split_global_transitive

Conversation

@aheejin
Copy link
Member

@aheejin aheejin commented Mar 10, 2026

When a global is exclusively used by a secondary module, we can move it to the secondary module. If its initializer contains a global.get of another global, we exported it from the primary module to the secondary module, even if it may not be used anywhere else.

When we split a global out to a secondary module, this PR computes the transitive dependency of the split global, and if those globals in the dependency are not used anywhere else in other modules, we move them to the secondary module as well.

#8441 and this PR combined reduce the size of the primary module of acx_gallery by 42.6%.

The running time of wasm-split hasn't really changed with this PR, compared to #8441.


wasm-objdump -h result:

     Type start=0x0000000c end=0x00035d44 (size=0x00035d38) count: 11185
   Import start=0x00035d49 end=0x003faf6f (size=0x003c5226) count: 56805
 Function start=0x003faf73 end=0x0040de1f (size=0x00012eac) count: 62890
    Table start=0x0040de22 end=0x0041195d (size=0x00003b3b) count: 2921
      Tag start=0x0041195f end=0x00411963 (size=0x00000004) count: 1
   Global start=0x00411967 end=0x005541c5 (size=0x0014285e) count: 47771
   Export start=0x005541c9 end=0x005dfc2c (size=0x0008ba63) count: 59077
    Start start=0x005dfc2e end=0x005dfc30 (size=0x00000002) start: 828
     Elem start=0x005dfc34 end=0x00649a77 (size=0x00069e43) count: 12303
DataCount start=0x00649a79 end=0x00649a7a (size=0x00000001) count: 1
     Code start=0x00649a7f end=0x00879385 (size=0x0022f906) count: 62890
     Data start=0x00879389 end=0x00898f16 (size=0x0001fb8d) count: 1
  • After (This PR)
     Type start=0x0000000c end=0x00035d44 (size=0x00035d38) count: 11185
   Import start=0x00035d48 end=0x00132efc (size=0x000fd1b4) count: 32642
 Function start=0x00132f00 end=0x00145dac (size=0x00012eac) count: 62890
    Table start=0x00145daf end=0x001498ea (size=0x00003b3b) count: 2921
      Tag start=0x001498ec end=0x001498f0 (size=0x00000004) count: 1
   Global start=0x001498f4 end=0x00289e60 (size=0x0014056c) count: 47728
   Export start=0x00289e64 end=0x002e99c1 (size=0x0005fb5d) count: 35861
    Start start=0x002e99c3 end=0x002e99c5 (size=0x00000002) start: 828
     Elem start=0x002e99c9 end=0x0035380c (size=0x00069e43) count: 12303
DataCount start=0x0035380e end=0x0035380f (size=0x00000001) count: 1
     Code start=0x00353814 end=0x005830e5 (size=0x0022f8d1) count: 62890
     Data start=0x005830e9 end=0x005a2c76 (size=0x0001fb8d) count: 1

Note that while the decrease in the global section is small, we have a significant size decrease in the import and the export sections, because we used to import globals and export them just to relay those globals to the secondary modules.

Follow-up: #8443

When a global is exclusively used by a secondary module, we can move it
to the secondary module. If its initializer contains a `global.get` of
another global, we exported it from the primary module to the secondary
module, even if it may not be used anywhere else.

When we split a global out to a secondary module, this PR computes the
transitive dependency of the split global, and if those globals in the
dependency are not used anywhere else in other modules, we move them to
the secondary module as well.

 #8441 and this PR combined reduce the size of the primary module by
29%.

The running time of `wasm-split` hasn't really changed with this PR,
compared to #8441.

---

`wasm-objdump -h` result:

- Before (#8441)
```
     Type start=0x0000000c end=0x00035d44 (size=0x00035d38) count: 11185
   Import start=0x00035d49 end=0x003faf6f (size=0x003c5226) count: 56805
 Function start=0x003faf73 end=0x0040de1f (size=0x00012eac) count: 62890
    Table start=0x0040de22 end=0x0041195d (size=0x00003b3b) count: 2921
      Tag start=0x0041195f end=0x00411963 (size=0x00000004) count: 1
   Global start=0x00411967 end=0x005541c5 (size=0x0014285e) count: 47771
   Export start=0x005541ca end=0x007c0a00 (size=0x0026c836) count: 59077
    Start start=0x007c0a02 end=0x007c0a04 (size=0x00000002) start: 828
     Elem start=0x007c0a08 end=0x0082a84b (size=0x00069e43) count: 12303
DataCount start=0x0082a84d end=0x0082a84e (size=0x00000001) count: 1
     Code start=0x0082a853 end=0x00a5a159 (size=0x0022f906) count: 62890
     Data start=0x00a5a15d end=0x00a79cea (size=0x0001fb8d) count: 1
```

- After (This PR)
```
     Type start=0x0000000c end=0x00035d44 (size=0x00035d38) count: 11185
   Import start=0x00035d48 end=0x00132efc (size=0x000fd1b4) count: 32642
 Function start=0x00132f00 end=0x00145dac (size=0x00012eac) count: 62890
    Table start=0x00145daf end=0x001498ea (size=0x00003b3b) count: 2921
      Tag start=0x001498ec end=0x001498f0 (size=0x00000004) count: 1
   Global start=0x001498f4 end=0x00289e60 (size=0x0014056c) count: 47728
   Export start=0x00289e65 end=0x004977fe (size=0x0020d999) count: 35861
    Start start=0x00497800 end=0x00497802 (size=0x00000002) start: 828
     Elem start=0x00497806 end=0x00501649 (size=0x00069e43) count: 12303
DataCount start=0x0050164b end=0x0050164c (size=0x00000001) count: 1
     Code start=0x00501651 end=0x00730f22 (size=0x0022f8d1) count: 62890
     Data start=0x00730f26 end=0x00750ab3 (size=0x0001fb8d) count: 1
```

Note that while the decrease in the global section is small, we have a
significant size decrease in the import and the export sections, because
we used to import globals and export them just to relay those globals to
the secondary modules.
@aheejin aheejin requested a review from tlively March 10, 2026 09:35
aheejin added a commit that referenced this pull request Mar 10, 2026
When a global is exclusively used by a secondary module and thus moved
to that module, and its initializer has a `(ref.func $func)`, we used to
create a trampoline and export it from the primary module in all cases,
even in the case that the function is in the same secondary module. This
now moves those functions referred to by `ref.func`s to the secondary
module, as long as they don't have uses anywhere else.

To do this, we now skip scanning global initializers in
`indirectReferencesToSecondaryFunctions`, and selectively create
trampolines only when needed in `shareImportableItems`.

The running time of `wasm-split` hasn't really changed with this PR,
compared to the previous PR #8442 (~25s range in acx_gallery).

 #8441, #8442, and this PR combined reduce the size of the primary
module by 46.6%.

---

`wasm-objdump -h` result:

- Before (#8442)
```
     Type start=0x0000000c end=0x00035d44 (size=0x00035d38) count: 11185
   Import start=0x00035d48 end=0x00132efc (size=0x000fd1b4) count: 32642
 Function start=0x00132f00 end=0x00145dac (size=0x00012eac) count: 62890
    Table start=0x00145daf end=0x001498ea (size=0x00003b3b) count: 2921
      Tag start=0x001498ec end=0x001498f0 (size=0x00000004) count: 1
   Global start=0x001498f4 end=0x00289e60 (size=0x0014056c) count: 47728
   Export start=0x00289e65 end=0x004977fe (size=0x0020d999) count: 35861
    Start start=0x00497800 end=0x00497802 (size=0x00000002) start: 828
     Elem start=0x00497806 end=0x00501649 (size=0x00069e43) count: 12303
DataCount start=0x0050164b end=0x0050164c (size=0x00000001) count: 1
     Code start=0x00501651 end=0x00730f22 (size=0x0022f8d1) count: 62890
     Data start=0x00730f26 end=0x00750ab3 (size=0x0001fb8d) count: 1
```

- After (This PR)
```
     Type start=0x0000000c end=0x00035d38 (size=0x00035d2c) count: 11185
   Import start=0x00035d3c end=0x00132ef0 (size=0x000fd1b4) count: 32642
 Function start=0x00132ef4 end=0x001436cc (size=0x000107d8) count: 53001
    Table start=0x001436cf end=0x0014720a (size=0x00003b3b) count: 2921
      Tag start=0x0014720c end=0x00147210 (size=0x00000004) count: 1
   Global start=0x00147214 end=0x00287b75 (size=0x00140961) count: 47728
   Export start=0x00287b79 end=0x002e703f (size=0x0005f4c6) count: 25972
    Start start=0x002e7041 end=0x002e7043 (size=0x00000002) start: 828
     Elem start=0x002e7047 end=0x00349aa7 (size=0x00062a60) count: 12303
DataCount start=0x00349aa9 end=0x00349aaa (size=0x00000001) count: 1
     Code start=0x00349aaf end=0x00550a4e (size=0x00206f9f) count: 53001
     Data start=0x00550a52 end=0x005705df (size=0x0001fb8d) count: 1
```

We can see while the size of the function and the code sections have
decreased, the big gains come from the decrease of the export section,
which can contain long function names.
@aheejin
Copy link
Member Author

aheejin commented Mar 10, 2026

cc @biggs0125

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant