Binaryen: Inlining constant globals

Created on 17 Jul 2018  路  2Comments  路  Source: WebAssembly/binaryen

Quick question: Is there a pass that would inline the constant (non-mutable) global in the following? Tried several optimization levels but wasn't able to make it happen.

(module
 (type $i (func (result i32)))
 (global $testGlobal i32 (i32.const 2))
 (export "test" (func $test))
 (func $test (; 0 ;) (type $i) (result i32)
  (get_global $testGlobal)
 )
)

For a bit of background: AssemblyScript currently does its own inlining of constant values, but I'd like to remove that in favor of retaining as much source information as possible in the untouched compiler output and leave it up to Binaryen to decide when optimizing. My expectation was that Binaryen would always inline constant globals if size isn't a concern (not sure if this has any performance benefits in current VMs actually), but if size is a concern, would count the number of uses (and possibly take the binary size of the constant value into account compared to a get_global) and decide from that.

Most helpful comment

Implemented by that PR. Leaving open though as it might be interesting to measure the effects here, and see if we want to gate this on particular optimization options, as a future refinement.

All 2 comments

Good suggestion, I think we should do this. Maybe the only question is when - if optimizing for size, we could check if it helps or not, but even if it helps binary size it may affect gzip size in a surprising way, I never found time to investigate what's going on with inlining local constants, see https://github.com/WebAssembly/binaryen/blob/e601522b1e15e5a2a96578244faca1648021fb2d/src/passes/ConstHoisting.cpp#L18

On the other hand, often inlining a constant would help other optimizations later, so maybe it's worth it even then.

Unless someone else wants to implement this, I can get to it later this week.

Implemented by that PR. Leaving open though as it might be interesting to measure the effects here, and see if we want to gate this on particular optimization options, as a future refinement.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jirutka picture jirutka  路  6Comments

Becavalier picture Becavalier  路  4Comments

aheejin picture aheejin  路  3Comments

z2oh picture z2oh  路  4Comments

tlively picture tlively  路  7Comments