# Wasm module defined \_\_\_ globals which exceeds the maximum number allowed 200

**URL:** <https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834>\
**Category:** Developers\
**Created:** [February 11, 2022, 8:50pm UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834 "2022-02-11T20:50:58Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![icpp](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icpp/32/9310_2.png) [@icpp](https://forum.dfinity.org/u/icpp)\
**Post date:** [October 11, 2023, 3:41pm UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/21 "2023-10-11T15:41:23Z")

</div>

That is awesome news!!

---

<div class="post-metadata">

**Author:** ![icpp](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icpp/32/9310_2.png) [@icpp](https://forum.dfinity.org/u/icpp)\
**Post date:** [October 12, 2023, 12:07am UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/22 "2023-10-12T00:07:48Z")

</div>

@dsarlis ,  
I think we can just use this thread to discuss the possibility to completely remove this limit on the number of globals.

The coming increase to 1,000 will be sufficient for all cases I have seen so far, but we’re only getting started, so you never know if this is sufficiently high.

Would it be possible to get rid of it altogether?

---

<div class="post-metadata">

**Author:** ![dsarlis](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/dsarlis/32/689_2.png) [@dsarlis](https://forum.dfinity.org/u/dsarlis)\
**Post date:** [October 12, 2023, 7:08am UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/23 "2023-10-12T07:08:54Z")

</div>

@icpp I think Diego tagged me out of reflex, this is more in the purview of the runtime team nowadays (I’m in the execution team myself which is very closely related and sometimes leading to these confusions). I’ll let the folks of the runtime team answer on the possibility of getting rid of limits. (Andriy who replied that the limit is being increased to 1000 is part of the runtime team).

---

<div class="post-metadata">

**Author:** ![berestovskyy](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/berestovskyy/32/3989_2.png) [@berestovskyy](https://forum.dfinity.org/u/berestovskyy)\
**Post date:** [October 12, 2023, 12:24pm UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/24 "2023-10-12T12:24:17Z")

</div>

The limits are in place because ATM, on the IC, WASM compilation should happen within a round, i.e., within approximately 1 second.

There are other compilation limits in place as well. So, removing this particular limit for the globals won’t break the IC, but it also won’t help to accommodate much larger programs on the IC, unfortunately.

The fundamental solution is a multi-round compilation, which will be technically possible after the [query stats feature](https://nns.ic0.app/proposal/?u=qoctq-giaaa-aaaaa-aaaea-cai&proposal=123481) is fully implemented.

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [October 20, 2023, 4:13am UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/25 "2023-10-20T04:13:09Z")

</div>

Multi-round compilation ftw!

---

<div class="post-metadata">

**Author:** ![ktimam](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ktimam/32/5964_2.png) [@ktimam](https://forum.dfinity.org/u/ktimam)\
**Post date:** [October 25, 2023, 11:34am UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/26 "2023-10-25T11:34:20Z")

</div>

Hello,

Is the change rolled out yet?

---

<div class="post-metadata">

**Author:** ![berestovskyy](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/berestovskyy/32/3989_2.png) [@berestovskyy](https://forum.dfinity.org/u/berestovskyy)\
**Post date:** [October 26, 2023, 10:28am UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/27 "2023-10-26T10:28:12Z")

</div>

It should be rolled out to all the app subnets and the latest DFX beta should include the change:

```auto
DFX_VERSION=0.15.2-beta.0 sh -ci "$(curl -fsSL https://internetcomputer.org/install.sh)"

```

---

<div class="post-metadata">

**Author:** ![ktimam](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ktimam/32/5964_2.png) [@ktimam](https://forum.dfinity.org/u/ktimam)\
**Post date:** [June 16, 2024, 3:54pm UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/28 "2024-06-16T15:54:22Z")

</div>

Sorry to bring this up again. Been building Deep Learning using Flashlight, and its using 1388 globals which exceeds the maximum number allowed 1000. Could we bring this up again to 2500 (in case i need to bring in yet another library)?

---

<div class="post-metadata">

**Author:** ![ulan](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ulan/32/2705_2.png) [@ulan](https://forum.dfinity.org/u/ulan)\
**Post date:** [June 19, 2024, 9:00am UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/29 "2024-06-19T09:00:51Z")

</div>

@ktimam would you mind sharing the build steps on how to reproduce the Wasm binary? I would like to look into why the library uses so many variables and if that is a regular pattern or something exceptional.

---

<div class="post-metadata">

**Author:** ![ktimam](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ktimam/32/5964_2.png) [@ktimam](https://forum.dfinity.org/u/ktimam)\
**Post date:** [June 19, 2024, 1:16pm UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/30 "2024-06-19T13:16:40Z")

</div>

Thanks @ulan . Can you send me your github id so i can send you an invite to the repo?

---

<div class="post-metadata">

**Author:** ![ulan](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ulan/32/2705_2.png) [@ulan](https://forum.dfinity.org/u/ulan)\
**Post date:** [June 19, 2024, 1:23pm UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/31 "2024-06-19T13:23:20Z")

</div>

My github id is `ulan`

---

<div class="post-metadata">

**Author:** ![ktimam](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ktimam/32/5964_2.png) [@ktimam](https://forum.dfinity.org/u/ktimam)\
**Post date:** [June 19, 2024, 2:12pm UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/32 "2024-06-19T14:12:56Z")

</div>

Sent you access. Please make sure you’re on latest icpp-pro 3.17.0, then run “icpp build-wasm” from project root, then “dfx deploy”

---

<div class="post-metadata">

**Author:** ![ulan](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ulan/32/2705_2.png) [@ulan](https://forum.dfinity.org/u/ulan)\
**Post date:** [June 20, 2024, 7:13am UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/33 "2024-06-20T07:13:36Z")

</div>

Thanks @ktimam! I completed the investigation.

The Wasm binary declares 1387 globals, however only 138 of them are used.

The unused globals are marked as exported as shown below. That is the reason why they are not eliminated by Wasm optimizers.

I think the fix could be implemented on the `icpp-pro` side:

- Remove all `(export global)` declarations by modifying the Wasm binary and run a Wasm optimizer (`wasm-opt` or `ic-wasm`). Note that globals don’t need to be exported on the IC. This should remove the unused globals.
- If that doesn’t help, then it should be possible to implement a script that looks for `(global.get X)` and `(global.set X)` statements in the Wasm binary and removes all globals that are not accessed.
- There might be also a way on the C++ toolchain side to optimize such that globals are not exported in the first place (e.g. using link time optimization (LTO) flags)

```auto
  (global (;1386;) i32 (i32.const 1486192))
  (global (;1387;) i32 (i32.const 1486248))
  (export "memory" (memory 0))
  (export "_ZTVN10 __cxxabiv120__ si_class_type_infoE" (global 139))
  (export "_ZTINSt3 __219__ shared_weak_countE" (global 140))

```

---

<div class="post-metadata">

**Author:** ![ulan](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ulan/32/2705_2.png) [@ulan](https://forum.dfinity.org/u/ulan)\
**Post date:** [June 20, 2024, 9:44am UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/34 "2024-06-20T09:44:02Z")

</div>

@berestovskyy suggested an idea of implementing removal of unused globals in `ic-wasm` when it does the `shrink` pass.

@chenyan: I guess you’re the owner of `ic-wasm`? Wdyt about this suggestion?

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [June 20, 2024, 5:07pm UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/35 "2024-06-20T17:07:42Z")

</div>

Sure. I think the shrink pass already removes the unused globals. It didn’t get removed because they are exported. If we do a pass to remove all exported globals, I think the shrink pass will remove the unused globals for us?

---

<div class="post-metadata">

**Author:** ![ktimam](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ktimam/32/5964_2.png) [@ktimam](https://forum.dfinity.org/u/ktimam)\
**Post date:** [June 21, 2024, 1:16am UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/36 "2024-06-21T01:16:26Z")

</div>

Thanks a lot @ulan for all the info. I tried some manual tinkering, converting wasm to wat, then removing all export lines (except the one with the canister function), then turned back to wasm, ran wasm-opt with -Oz flag, then deployed. It deployed successfully, though not sure if i’m doing this right or how to automate this process!  
The code is also broken so i’m not sure if it’s something related to the code with ICP or if it’s related to those changes. Is there a way to produce a debugging stack when calling “dfx canister --network local call” to determine where the code broke exactly?

---

<div class="post-metadata">

**Author:** ![ulan](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ulan/32/2705_2.png) [@ulan](https://forum.dfinity.org/u/ulan)\
**Post date:** [June 21, 2024, 7:06am UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/37 "2024-06-21T07:06:26Z")

</div>

> [@chenyan](#):
>
> If we do a pass to remove all exported globals, I think the shrink pass will remove the unused globals for us?

Yes, that’s my understanding as well.

> Is there a way to produce a debugging stack when calling “dfx canister --network local call” to determine where the code broke exactly?

@abk is implementing backtrace support. It will take several weeks until the feature is release. If you share the Wasm binary and `call` parameters, `abk` might be able to run it locally in a patched replica and give you the backtrace.

---

<div class="post-metadata">

**Author:** ![ktimam](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ktimam/32/5964_2.png) [@ktimam](https://forum.dfinity.org/u/ktimam)\
**Post date:** [June 21, 2024, 1:04pm UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/38 "2024-06-21T13:04:01Z")

</div>

Thanks a lot, that would be of great help.

Kindly find attached the wasm file with removed exports (the debug info was removed after optimizing though, is there a way to get around with keeping the debug info while removing unneeded globals?):

> **[ICMLPSample.wasm](https://drive.google.com/file/d/1-6sz1VTuhciDNlMk6Cg1gVvaZesx6-Bf/view?usp=sharing)**
>
> Google Drive file.

Command to run after deploying:  
dfx canister --network local call ICMLPSample train

---

<div class="post-metadata">

**Author:** ![icpp](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icpp/32/9310_2.png) [@icpp](https://forum.dfinity.org/u/icpp)\
**Post date:** [June 21, 2024, 9:21pm UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/39 "2024-06-21T21:21:04Z")

</div>

Thank you @ulan , I will have a look at the options to optimize the wasm as part of the build process in icpp-pro.

---

<div class="post-metadata">

**Author:** ![ktimam](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ktimam/32/5964_2.png) [@ktimam](https://forum.dfinity.org/u/ktimam)\
**Post date:** [June 22, 2024, 11:38am UTC](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834/40 "2024-06-22T11:38:51Z")

</div>

After some investigation, i found out that the problem results after using Oz flag during compilation process. I tried removing all optimizations during compilation and used Oz flag to optimize the wasm using wasm-opt after removing exports, and all ran well.

Thanks a lot for your valuable input. Here’s a Multilayer perceptron Flashlight sample running fully on chain:  
dfx canister --network ic call 52uwu-yyaaa-aaaag-aldoa-cai train

[Previous page](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834.md?page=1)

[Next page](https://forum.dfinity.org/t/wasm-module-defined-globals-which-exceeds-the-maximum-number-allowed-200/10834.md?page=3)
