This is an archive of the discontinued LLVM Phabricator instance.

[ConstantFolding] Accept FoldedOps cache as parameter
AbandonedPublic

Authored by nikic on Mar 4 2020, 12:38 PM.

Details

Summary

InstCombine internally caches constant operands that it folds. ConstantFolding also internally caches folded operands, but does so only on the "second level": The direct operands of an instruction/constant are not cached, but the operands of the operands are. Presumably that's an oversight...

This patch allows passing in the folded operand cache as an extra parameter, and changes the ConstantFoldConstant API to also look up from it / write to it by itself. That way all users handle caching correctly, and the cache can be shared across calls (done only in InstCombine for now).

In particular this will also avoid duplicate constant folding between ConstantFoldInstruction and operand constant folding in InstCombine. I haven't run into this as a perf problem myself, but recently ran across D18155, which did identify this as a performance problem for some types of code.

Diff Detail

Event Timeline

nikic created this revision.Mar 4 2020, 12:38 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 4 2020, 12:38 PM

The point of the cache in ConstantFolding is really to avoid bad behavior for constant expression trees with a "diamond" shape; in that context, it doesn't really matter what level it starts at, since constants have a limited number of operands. But sure, if we have the cache anyway, we might as well use it.

I'd like to see some sort of performance measurement here, given we're complicating the ConstantFoldConstant API for the sake of performance.

lib/Analysis/ConstantFolding.cpp
1192

Do you have any idea why we're checking for ConstantVector specifically, instead of ConstantAggregate?

1195

Might as well use DenseMap::lookup() here.

nikic marked an inline comment as done.Mar 4 2020, 2:30 PM
nikic added inline comments.
lib/Analysis/ConstantFolding.cpp
1192

The ConstantVector handling was added lateron in https://github.com/llvm/llvm-project/commit/d536f2328ededb3aae6563c721c6134c735f1918, so the reason might be just "it hasn't been implemented".

Maybe there is also a compile-time concern here? A ConstantVector will usually have relatively few operands, while a ConstantArray might have thousands.

efriedma added inline comments.Mar 4 2020, 3:57 PM
lib/Analysis/ConstantFolding.cpp
1192

"it hasn't been implemented" makes sense, I guess.

nikic abandoned this revision.Mar 15 2020, 8:18 AM

Going to close this one, as I actually see a 0.2% regression with it (http://llvm-compile-time-tracker.com/compare.php?from=b236b4cb430674a98917ad1b2fa9f7f9838e66f1&to=a0dcbfc3080e6dd91c7b4253dd83e9eea4eefd03&stat=instructions). Presumably, for the cases where we don't have a lot of unfoldable constant expressions (which is the common case), we end up not actually benefiting from the cache, but still paying (a bit) for it.