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

Quentin Colombet via llvm-commits llvm-commits at lists.llvm.org
Tue May 14 05:04:42 PDT 2024


qcolombet 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. (hot take) I'd also be wary of using optional just "out of convenience" and ending up with sub-optimal code when sometimes returning a bool & using an out parameter just makes more sense and avoids a copy or two.

Thanks for the details!

I was afraid the optional pattern was a time sink in general, but like you said here we are looking at a micro optimization.

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


More information about the llvm-commits mailing list