binaryen icon indicating copy to clipboard operation
binaryen copied to clipboard

BranchHintAnalysis pass

Open kripken opened this issue 7 months ago • 2 comments

This pass statically infers branch hint annotations, inspired by LLVM's BranchProbabilityInfo:

https://github.com/llvm/llvm-project/blob/main/llvm/lib/Analysis/BranchProbabilityInfo.cpp

So far this sets low priority for throwing paths and even lower for unreachable ones. It propagates such indications back to calling blocks and even calling functions.

LLVM does a little more, it also has some minor hints for things like a float being NaN (unlikely) and an integer/pointer being 0 (unlikely), etc.

This pass has no benefit I can measure. My best guess is that we mostly end up marking exit paths from functions (error handling, etc.), and VMs can place those blocks and regalloc them efficiently without our hints. The cross-function part can't be done by VMs, and adds a signficant amount of hints (3-50% on real-world binaries) but even that does not help any benchmark I have tried (Emscripten benchmark suite). Opening this PR as draft to share that information, and maybe as a basis for future work.

kripken avatar May 27 '25 20:05 kripken

Is that 3-50% of original code size or number of hints?

tlively avatar May 27 '25 22:05 tlively

Number of hints. So if function analysis gives 100 hints in the entire module, cross-function analysis adds 3-50 on top.

kripken avatar May 27 '25 23:05 kripken