This is an archive of the discontinued LLVM Phabricator instance.

[CodeGenPrepare] Fix for using outdated/corrupt LoopInfo
AbandonedPublic

Authored by chill on May 9 2023, 10:40 AM.

Details

Summary

Some transformation in CodeGenPrepare pass may create and/or delete
basic block, but they don't update the LoopInfo, so the LoopInfo may
end up containing dangling pointers and sometimes reused basic blocks,
which leads to "interesting" non-deterministic behaviour.

This patch makes the pass invalidate the LoopInfo whenever the
DominationTree is invalidated and lazily rebuilds it.

Diff Detail

Event Timeline

chill created this revision.May 9 2023, 10:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 9 2023, 10:40 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
chill requested review of this revision.May 9 2023, 10:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 9 2023, 10:40 AM

It might be worth trying to teach CodeGenPrepare to update the domtree instead of destroying it, at some point... but that shouldn't block this patch.

llvm/lib/CodeGen/CodeGenPrepare.cpp
602

InitialLI is pointless if you're going to immediately throw it away.

chill added inline comments.May 10 2023, 1:30 AM
llvm/lib/CodeGen/CodeGenPrepare.cpp
602

It's used in the construction of BranchProbabilityInfo and BlockGFrequenceInfo.

chill marked an inline comment as done.May 10 2023, 1:30 AM
efriedma added inline comments.May 10 2023, 9:25 AM
llvm/lib/CodeGen/CodeGenPrepare.cpp
602

I guess, but you don't need to store it in the class for that.

Is the BPI also broken by CFG modifications?

chill planned changes to this revision.May 10 2023, 10:25 AM
chill added inline comments.
llvm/lib/CodeGen/CodeGenPrepare.cpp
602

I guess, but you don't need to store it in the class for that.

Ah, indeed.

Is the BPI also broken by CFG modifications?

Looks like it, yes.

chill added inline comments.May 11 2023, 9:48 AM
llvm/lib/CodeGen/CodeGenPrepare.cpp
602

BFI and BPI use CallbackVH for basic blocks, so they are perhaps fine, but they also keep a raw pointer to the LoopInfo.
Fortunately, LoopInfo is not hard to keep updated, so I opted for this approach in a new patch.