[llvm] [GVNSink] Fix non-determinisms by using Depth-First ordering (PR #90995)
via llvm-commits
llvm-commits at lists.llvm.org
Fri May 3 12:34:26 PDT 2024
https://github.com/hiraditya updated https://github.com/llvm/llvm-project/pull/90995
>From 45f8c80633d4911d7943df4efbcfa6acd77d2356 Mon Sep 17 00:00:00 2001
From: AdityaK <hiraditya at msn.com>
Date: Thu, 2 May 2024 16:08:51 -0700
Subject: [PATCH] Fix non-determinisms
GVNSink used to order instructions based on their pointer values and was prone to
non-determinism because of that. Using DFSNumber for ordering all the values
fixes the non-determinism
---
llvm/lib/Transforms/Scalar/GVNSink.cpp | 65 +++++++++++++++----
.../test/Transforms/GVNSink/int_sideeffect.ll | 24 +++++++
2 files changed, 75 insertions(+), 14 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/GVNSink.cpp b/llvm/lib/Transforms/Scalar/GVNSink.cpp
index d4907326eb0a5a..c6613f427d8ece 100644
--- a/llvm/lib/Transforms/Scalar/GVNSink.cpp
+++ b/llvm/lib/Transforms/Scalar/GVNSink.cpp
@@ -222,20 +222,29 @@ raw_ostream &operator<<(raw_ostream &OS, const SinkingInstructionCandidate &C) {
class ModelledPHI {
SmallVector<Value *, 4> Values;
SmallVector<BasicBlock *, 4> Blocks;
+ DominatorTree *DT;
public:
ModelledPHI() = default;
- ModelledPHI(const PHINode *PN) {
+ ModelledPHI(const PHINode *PN, DominatorTree *DT) : DT(DT) {
// BasicBlock comes first so we sort by basic block pointer order, then by value pointer order.
- SmallVector<std::pair<BasicBlock *, Value *>, 4> Ops;
+ using OpsType = std::pair<BasicBlock *, Value *>;
+ SmallVector<OpsType, 4> Ops;
for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I)
Ops.push_back({PN->getIncomingBlock(I), PN->getIncomingValue(I)});
- llvm::sort(Ops);
+
+ auto DFSOrder = [DT](OpsType O1, OpsType O2) {
+ return DT->getNode(O1.first) < DT->getNode(O2.first);
+ };
+ // Sort by DFSNumber to have a deterministic order.
+ llvm::sort(Ops, DFSOrder);
+
for (auto &P : Ops) {
Blocks.push_back(P.first);
Values.push_back(P.second);
}
+ verifyModelledPHI();
}
/// Create a dummy ModelledPHI that will compare unequal to any other ModelledPHI
@@ -247,19 +256,37 @@ class ModelledPHI {
return M;
}
+ void verifyModelledPHI() {
+ assert(Values.size() > 1 && Blocks.size() > 1 &&
+ "Modelling PHI with less than 2 values");
+ auto DFSOrder = [this](const BasicBlock *BB1, const BasicBlock *BB2) {
+ return this->DT->getNode(BB1) < this->DT->getNode(BB2);
+ };
+ assert(llvm::is_sorted(Blocks, DFSOrder));
+ int C = 0;
+ llvm::for_each(Values, [&C, this](const Value *V) {
+ const Instruction *I = cast<Instruction>(V);
+ assert(I->getParent() == this->Blocks[C++]);
+ });
+ }
/// Create a PHI from an array of incoming values and incoming blocks.
- template <typename VArray, typename BArray>
- ModelledPHI(const VArray &V, const BArray &B) {
+ ModelledPHI(SmallVectorImpl<Instruction *> &V,
+ SmallSetVector<BasicBlock *, 4> &B, DominatorTree *DT)
+ : DT(DT) {
+ // The order of Values and Blocks are already as per their DFSNumbers.
llvm::copy(V, std::back_inserter(Values));
llvm::copy(B, std::back_inserter(Blocks));
+ verifyModelledPHI();
}
/// Create a PHI from [I[OpNum] for I in Insts].
- template <typename BArray>
- ModelledPHI(ArrayRef<Instruction *> Insts, unsigned OpNum, const BArray &B) {
+ ModelledPHI(ArrayRef<Instruction *> Insts, unsigned OpNum,
+ SmallSetVector<BasicBlock *, 4> &B, DominatorTree *DT)
+ : DT(DT) {
llvm::copy(B, std::back_inserter(Blocks));
for (auto *I : Insts)
Values.push_back(I->getOperand(OpNum));
+ verifyModelledPHI();
}
/// Restrict the PHI's contents down to only \c NewBlocks.
@@ -297,7 +324,8 @@ class ModelledPHI {
// Hash functor
unsigned hash() const {
- return (unsigned)hash_combine_range(Values.begin(), Values.end());
+ // Is deterministic because Values are saved in DFSOrder.
+ return (unsigned)hash_combine_range(Values.begin(), Values.end());
}
bool operator==(const ModelledPHI &Other) const {
@@ -566,7 +594,7 @@ class ValueTable {
class GVNSink {
public:
- GVNSink() = default;
+ GVNSink(DominatorTree *DT) : DT(DT) {}
bool run(Function &F) {
LLVM_DEBUG(dbgs() << "GVNSink: running on function @" << F.getName()
@@ -583,6 +611,7 @@ class GVNSink {
private:
ValueTable VN;
+ DominatorTree *DT;
bool shouldAvoidSinkingInstruction(Instruction *I) {
// These instructions may change or break semantics if moved.
@@ -603,7 +632,7 @@ class GVNSink {
void analyzeInitialPHIs(BasicBlock *BB, ModelledPHISet &PHIs,
SmallPtrSetImpl<Value *> &PHIContents) {
for (PHINode &PN : BB->phis()) {
- auto MPHI = ModelledPHI(&PN);
+ auto MPHI = ModelledPHI(&PN, DT);
PHIs.insert(MPHI);
for (auto *V : MPHI.getValues())
PHIContents.insert(V);
@@ -691,7 +720,7 @@ GVNSink::analyzeInstructionForSinking(LockstepReverseIterator &LRI,
}
// The sunk instruction's results.
- ModelledPHI NewPHI(NewInsts, ActivePreds);
+ ModelledPHI NewPHI(NewInsts, ActivePreds, DT);
// Does sinking this instruction render previous PHIs redundant?
if (NeededPHIs.erase(NewPHI))
@@ -728,7 +757,7 @@ GVNSink::analyzeInstructionForSinking(LockstepReverseIterator &LRI,
return std::nullopt;
for (unsigned OpNum = 0, E = I0->getNumOperands(); OpNum != E; ++OpNum) {
- ModelledPHI PHI(NewInsts, OpNum, ActivePreds);
+ ModelledPHI PHI(NewInsts, OpNum, ActivePreds, DT);
if (PHI.areAllIncomingValuesSame())
continue;
if (!canReplaceOperandWithVariable(I0, OpNum))
@@ -774,7 +803,11 @@ unsigned GVNSink::sinkBB(BasicBlock *BBEnd) {
}
if (Preds.size() < 2)
return 0;
- llvm::sort(Preds);
+ auto DFSOrder = [this](const BasicBlock *BB1, const BasicBlock *BB2) {
+ return this->DT->getNode(BB1) < this->DT->getNode(BB2);
+ };
+ // Sort by DFSNumber to have a deterministic order.
+ llvm::sort(Preds, DFSOrder);
unsigned NumOrigPreds = Preds.size();
// We can only sink instructions through unconditional branches.
@@ -886,8 +919,12 @@ void GVNSink::sinkLastInstruction(ArrayRef<BasicBlock *> Blocks,
} // end anonymous namespace
PreservedAnalyses GVNSinkPass::run(Function &F, FunctionAnalysisManager &AM) {
- GVNSink G;
+ auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
+ GVNSink G(&DT);
if (!G.run(F))
return PreservedAnalyses::all();
+
+ // PHI nodes get inserted which haven't been added to the Dominator Tree.
+ // FIXME: Update DominatorTree to account for sunk instructions.
return PreservedAnalyses::none();
}
diff --git a/llvm/test/Transforms/GVNSink/int_sideeffect.ll b/llvm/test/Transforms/GVNSink/int_sideeffect.ll
index 3cc54e84f17c1b..f27e7aa823efe3 100644
--- a/llvm/test/Transforms/GVNSink/int_sideeffect.ll
+++ b/llvm/test/Transforms/GVNSink/int_sideeffect.ll
@@ -28,3 +28,27 @@ if.end:
ret float %phi
}
+; CHECK-LABEL: scalarsSinkingReverse
+; CHECK-NOT: fmul
+; CHECK: = phi
+; CHECK: = fmul
+define float @scalarsSinkingReverse(float %d, float %m, float %a, i1 %cmp) {
+entry:
+ br i1 %cmp, label %if.then, label %if.else
+
+if.then:
+ %add = fadd float %m, %a
+ %mul1 = fmul float %add, %d
+ br label %if.end
+
+if.else:
+ call void @llvm.sideeffect()
+ %sub = fsub float %m, %a
+ %mul0 = fmul float %sub, %d
+ br label %if.end
+
+if.end:
+ %phi = phi float [ %mul1, %if.then ], [ %mul0, %if.else ]
+ ret float %phi
+}
+
More information about the llvm-commits
mailing list