[PATCH] D155940: [SimplifyCFG] Transform for redirecting phis between unmergeable BB and SuccBB

Hongyu Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 20 00:39:57 PDT 2023


XChy marked an inline comment as done.
XChy added inline comments.


================
Comment at: llvm/lib/Transforms/Utils/Local.cpp:1128
+      BBKillable ||
+      CanRedirectPredsOfEmptyBBToSucc(BB, Succ, BBPreds, SuccPreds, CommonPred);
+
----------------
nikic wrote:
> XChy wrote:
> > nikic wrote:
> > > Is it intentional that if BBKillable is true we still call CanRedirectPredsOfEmptyBBToSucc? Should this be `!BBKillable &&`?
> > Both `BBKillable || redirect()` and `!BBKillable && redirect()` do not call CanRedirectPredsOfEmptyBBToSucc when BBKillable is true.
> > What is different is , that when BBKillable is true, the former BBPhisMergeable is true while the latter is false.
> > Actually it's logically OK, since when BBKillable is true, whether BBPhisMergeable doesn't have any impact.
> > I write `BBKillable || redirect()` based on semantic meaning : Phis are mergeable if BB can be killed.
> In particular, what worries me here is that we set BBKillable = true, then call CanRedirectPredsOfEmptyBBToSucc() which may set CommonPred (even if the function ultimately returns false) and then various code in the following will do things differently based on CommonPred. It's not clear to me whether this will result in misbehavior or not.
> 
> (With that in mind, I think it may also be cleaner if the BBPhisMergeable flag is removed and only CommonPred left, so that CommonPred != nullptr is equivalent to BBPhisMergeable and we don't leave dangling CommonPred if CanRedirectPredsOfEmptyBBToSucc exits early.)
What you're worried about make sense to me. But as far as I know, the assumption "`BBKillable || redirect()` will call redirect() when BBKillable is true" is wrong。


================
Comment at: llvm/lib/Transforms/Utils/Local.cpp:1179
     // To avoid processing the same predecessor more than once.
-    SmallPtrSet<BasicBlock *, 8> SeenPreds;
-    // All predecessors of BB will be moved to Succ.
----------------
nikic wrote:
> XChy wrote:
> > nikic wrote:
> > > Why is this set no longer needed? Can no predecessors appear multiple times in this code?
> > BBPreds is a SmallPtrSet, on which we would not iterate the same predecessor multiple times.
> Oh, I missed that this does the updates based on SmallPtrSet now. This is not allowed, because it makes the updates non-deterministic (order matters here). You should replace SmallPtrSet with SmallSetVector to keep order deterministic.
Sure, restore to original codem though I don't know why order matters here. Because the algorithm for updating dom-tree?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D155940/new/

https://reviews.llvm.org/D155940



More information about the llvm-commits mailing list