[llvm] [GlobalISel] Micro-optimize getConstantVRegValWithLookThrough (PR #91969)

Pierre van Houtryve via llvm-commits llvm-commits at lists.llvm.org
Mon May 13 09:00:16 PDT 2024


Pierre-vh wrote:

> Do you know how each of these items contributes to the compile time individually:
> 
> * Avoid copying APint unnecessarily, especially returning std::optional can be expensive when an out parameter also works.
> * Avoid indirect call by using templated function pointers instead of function_ref/std::function
> 
> The use of `optional` versus `bool` + `out` parameter is what I think we've been moving towards in the past few years within the LLVM code base and I'm wondering if that means this is generally bad for performance.

I can come back to this and do more thorough testing if you want a precise answer, but from what i remember (take with a big grain of salt): the biggest contributor was removing the indirect call (which enabled inlining as well probably), as the call occurred within a loop.

The removal of optional brought an additional nice improvement (maybe 1/3rd of the indirect call removal?) mostly because it removed APInt copies, and APInt copies aren't trivial.

Finally this patch needs to be put in context: this is a micro-optimization I did for fun and profit (because it was a low-hanging fruit). The gain is a single digit % improvement on the AArch64PreLegalizerCombine match table execution. That part of the compilation process is in itself like 5-7% of the build in my test, so in the best case this saved 0.2% of a full build.

I wouldn't worry about using optional more, but I'd just be careful with it when dealing with non-trivial types (especially large types). It's easy to lose optimizations like copy elision with it. It's also a pitfall where we can end up using optional just because it's convenient when there are better/more efficient ways to write the same code.

https://github.com/llvm/llvm-project/pull/91969


More information about the llvm-commits mailing list