Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions llvm/lib/Transforms/Vectorize/VPlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,12 +1005,14 @@ static void replaceVPBBWithIRVPBB(VPBasicBlock *VPBB, BasicBlock *IRBB) {
R.moveBefore(*IRVPBB, IRVPBB->end());
}
VPBlockBase *PredVPBB = VPBB->getSinglePredecessor();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert of having a single predecessor is now lost, yet still documented. The function traverses multiple predecessors, but probably works and tested for single predecessor only, as VPBB is still expected to be free of phi recipes? Better be clear, assert and test what is supported.

VPBlockUtils::disconnectBlocks(PredVPBB, VPBB);
VPBlockUtils::connectBlocks(PredVPBB, IRVPBB);
for (auto *Succ : to_vector(VPBB->getSuccessors())) {
VPBlockUtils::connectBlocks(IRVPBB, Succ);
VPBlockUtils::disconnectBlocks(VPBB, Succ);
}
PredVPBB->replaceSuccessor(VPBB, IRVPBB);
IRVPBB->setPredecessors({PredVPBB});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you be sure that for all future uses of this function IRVPBB's predecessors and successors can be replaced? Would it be safer to append them as was done previously?

From a structural point of view VPBlockUtils looks to be the place for managing VPBB linkage (I see a range of insert methods along with the (dis)connect method you're replacing) so in keeping with this perhaps add VPBlockUtils::reassociateBlock(Keep, Old, New) that calls onto replaceSuccessor and replacePredecessor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion - I've added a new VPBlockUtils::reassociateBlock function and do the replacements in there!

for (auto *Succ : to_vector(VPBB->getSuccessors()))
Succ->replacePredecessor(VPBB, IRVPBB);
IRVPBB->setSuccessors(VPBB->getSuccessors());

VPBB->clearSuccessors();
VPBB->clearPredecessors();
delete VPBB;
}

Expand Down
20 changes: 20 additions & 0 deletions llvm/lib/Transforms/Vectorize/VPlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,26 @@ class VPBlockBase {
return getEnclosingBlockWithPredecessors()->getSinglePredecessor();
}

/// This function replaces one predecessor with another, useful when
/// trying to replace an old block in the CFG with a new one.
void replacePredecessor(VPBlockBase *Old, VPBlockBase *New) {
auto I = find(Predecessors, Old);
assert(I != Predecessors.end());
assert(Old->getParent() == New->getParent() &&
"replaced predecessor must have the same parent");
*I = New;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this may leaves Old in an invalid state, as we remove it from the list of predecessors here, but don't remove this from the Successors of Old. Can we also remove it from there? Otherwise the callers of the API must either remove the node itself or completely delete the block

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, but if we go down that route then surely we should also care about setting the successor of New to this as well so that we leave it in a valid state? I guess if this function was private then it wouldn't be part of the interface so it wouldn't matter as much. If we do the extra work here as you suggest, then this patch becomes less about efficiency and more about retaining ordering, which is fine but then I probably ought to change the title!

Copy link
Contributor Author

@david-arm david-arm Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, removePredecessor does not leave the Predecessor argument in a valid state either because this will still be a successor of Predecessor . How about I make the functions private and then we can behave like removePredecessor, unless you see value in these new interfaces being public?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, let me take a look tomorrow

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, no problem. I just added a new commit as I realised the the public reassociateBlocks interface was leaving Old and New in an invalid state. Hopefully, this now addresses your concern!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks this was what I was about to suggest, as this ensures that reassociateBlocks leaves the CFG in a valid state.

}

/// This function replaces one successor with another, useful when
/// trying to replace an old block in the CFG with a new one.
void replaceSuccessor(VPBlockBase *Old, VPBlockBase *New) {
auto I = find(Successors, Old);
assert(I != Successors.end());
assert(Old->getParent() == New->getParent() &&
"replaced successor must have the same parent");
*I = New;
}

/// Set a given VPBlockBase \p Successor as the single successor of this
/// VPBlockBase. This VPBlockBase is not added as predecessor of \p Successor.
/// This VPBlockBase must have no successors.
Expand Down
Loading