This is an archive of the discontinued LLVM Phabricator instance.

[SCEVExpander] Fix incorrect reuse of more poisonous instructions (PR63763)
ClosedPublic

Authored by nikic on Aug 17 2023, 6:27 AM.

Details

Summary

SCEVExpander tries to reuse existing instruction with the same SCEV expression. However, doing this replacement blindly is not safe, because the instruction might be more poisonous.

What we were already doing is to drop poison-generating flags on the reused instruction. But this is not the only way that more poison can be introduced. The poison-generating flag might not be directly on the reused instruction, or the poison contribution might come from something like 0 * %var, which folds to 0 but can still introduce poison.

This patch fixes the issue in a principled way, by determining which values can contribute poison to the SCEV expression, and then checking whether any additional values can contribute poison to the instruction being reused. Poison-generating flags are dropped if doing that enables reuse.

This is a pretty big hammer and does cause some regressions in tests, but less than I would have expected. I wasn't able to come up with a less intrusive fix that still satisfies the correctness requirements.

Fixes https://github.com/llvm/llvm-project/issues/63763.
Fixes https://github.com/llvm/llvm-project/issues/63926.
Fixes https://github.com/llvm/llvm-project/issues/64333.
Fixes https://github.com/llvm/llvm-project/issues/63727.

Diff Detail

Event Timeline

nikic created this revision.Aug 17 2023, 6:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 17 2023, 6:27 AM
nikic requested review of this revision.Aug 17 2023, 6:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 17 2023, 6:27 AM
nikic edited the summary of this revision. (Show Details)Aug 17 2023, 6:30 AM
nikic edited the summary of this revision. (Show Details)Aug 17 2023, 6:34 AM
fhahn added inline comments.Aug 18 2023, 6:54 AM
llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
442

nit: should be -> must be?

llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
1503

nit: this comment might be better placed after the check if, we handles the opposite case described.

1832

It's safe to ignore DropPoisonGeneratingInsts here, as the function is only used in isHighCostExpansionHelper, right?

It might be good to lock down the API to return a bool for getRelatedExistingExpansion (hasRelatedExistingExpansion?) to make sure the return value isn't used elsewhere in the future without dropping the flags?

nikic updated this revision to Diff 551949.Aug 21 2023, 2:56 AM
nikic marked 3 inline comments as done.

Address review comments.

llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
1832

Good idea! I've renamed the method and changed the return value to bool.

fhahn accepted this revision.Aug 21 2023, 8:00 AM

LGTM, thanks!

This revision is now accepted and ready to land.Aug 21 2023, 8:00 AM