From 07f93d8c2cc427e851dec710a866c2605ce5f884 Mon Sep 17 00:00:00 2001 From: Zain Jaffal Date: Wed, 15 Feb 2023 15:41:24 +0000 Subject: [PATCH] Recommit "[ConstraintElimination] Change debug output to display variable names." This reverts commit 02ae7e72b3f00969eeb579a2b4346082827f0b35. include Value.h in ConstraintSystem.h --- llvm/include/llvm/Analysis/ConstraintSystem.h | 14 +++++++---- llvm/lib/Analysis/ConstraintSystem.cpp | 27 ++++++++++++++-------- .../Transforms/Scalar/ConstraintElimination.cpp | 26 ++++++++------------- .../reproducer-remarks-debug.ll | 2 +- 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/llvm/include/llvm/Analysis/ConstraintSystem.h b/llvm/include/llvm/Analysis/ConstraintSystem.h index e348b38..14eab74 100644 --- a/llvm/include/llvm/Analysis/ConstraintSystem.h +++ b/llvm/include/llvm/Analysis/ConstraintSystem.h @@ -36,13 +36,17 @@ class ConstraintSystem { // Eliminate constraints from the system using Fourier–Motzkin elimination. bool eliminateUsingFM(); - /// Print the constraints in the system, using x0...xn as variable names. - void dump() const; - /// Returns true if there may be a solution for the constraints in the system. bool mayHaveSolutionImpl(); + /// Get list of variable names from the Value2Index map. + SmallVector getVarNamesList() const; + public: + ConstraintSystem() {} + ConstraintSystem(const DenseMap &Value2Index) + : Value2Index(Value2Index) {} + bool addVariableRow(ArrayRef R) { assert(Constraints.empty() || R.size() == Constraints.back().size()); // If all variable coefficients are 0, the constraint does not provide any @@ -103,8 +107,8 @@ public: /// Returns the number of rows in the constraint system. unsigned size() const { return Constraints.size(); } - /// Print the constraints in the system, using \p Names as variable names. - void dump(ArrayRef Names) const; + /// Print the constraints in the system. + void dump() const; }; } // namespace llvm diff --git a/llvm/lib/Analysis/ConstraintSystem.cpp b/llvm/lib/Analysis/ConstraintSystem.cpp index fd96c61..9a8897e 100644 --- a/llvm/lib/Analysis/ConstraintSystem.cpp +++ b/llvm/lib/Analysis/ConstraintSystem.cpp @@ -10,6 +10,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/Support/MathExtras.h" #include "llvm/ADT/StringExtras.h" +#include "llvm/IR/Value.h" #include "llvm/Support/Debug.h" #include @@ -111,10 +112,23 @@ bool ConstraintSystem::mayHaveSolutionImpl() { return all_of(Constraints, [](auto &R) { return R[0] >= 0; }); } -void ConstraintSystem::dump(ArrayRef Names) const { +SmallVector ConstraintSystem::getVarNamesList() const { + SmallVector Names(Value2Index.size(), ""); + for (auto &[V, Index] : Value2Index) { + std::string OperandName; + if (V->getName().empty()) + OperandName = V->getNameOrAsOperand(); + else + OperandName = std::string("%") + V->getName().str(); + Names[Index - 1] = OperandName; + } + return Names; +} + +void ConstraintSystem::dump() const { if (Constraints.empty()) return; - + SmallVector Names = getVarNamesList(); for (const auto &Row : Constraints) { SmallVector Parts; for (unsigned I = 1, S = Row.size(); I < S; ++I) { @@ -131,15 +145,8 @@ void ConstraintSystem::dump(ArrayRef Names) const { } } -void ConstraintSystem::dump() const { - SmallVector Names; - for (unsigned i = 1; i < Constraints.back().size(); ++i) - Names.push_back("x" + std::to_string(i)); - LLVM_DEBUG(dbgs() << "---\n"); - dump(Names); -} - bool ConstraintSystem::mayHaveSolution() { + LLVM_DEBUG(dbgs() << "---\n"); LLVM_DEBUG(dump()); bool HasSolution = mayHaveSolutionImpl(); LLVM_DEBUG(dbgs() << (HasSolution ? "sat" : "unsat") << "\n"); diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp index 4ec3a58..e5ec355 100644 --- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp +++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp @@ -644,20 +644,12 @@ struct State { } // namespace #ifndef NDEBUG -static void dumpWithNames(const ConstraintSystem &CS, - DenseMap &Value2Index) { - SmallVector Names(Value2Index.size(), ""); - for (auto &KV : Value2Index) { - Names[KV.second - 1] = std::string("%") + KV.first->getName().str(); - } - CS.dump(Names); -} -static void dumpWithNames(ArrayRef C, - DenseMap &Value2Index) { - ConstraintSystem CS; +static void dumpConstraint(ArrayRef C, + const DenseMap &Value2Index) { + ConstraintSystem CS(Value2Index); CS.addVariableRowFill(C); - dumpWithNames(CS, Value2Index); + CS.dump(); } #endif @@ -941,7 +933,7 @@ static bool checkAndReplaceCondition( LLVM_DEBUG({ dbgs() << "Condition " << *Cmp << " implied by dominating constraints\n"; - dumpWithNames(CSToUse, Info.getValue2Index(R.IsSigned)); + CSToUse.dump(); }); generateReproducer(Cmp, ReproducerModule, ReproducerCondStack, Info, DT); Constant *TrueC = @@ -961,7 +953,7 @@ static bool checkAndReplaceCondition( LLVM_DEBUG({ dbgs() << "Condition !" << *Cmp << " implied by dominating constraints\n"; - dumpWithNames(CSToUse, Info.getValue2Index(R.IsSigned)); + CSToUse.dump(); }); generateReproducer(Cmp, ReproducerModule, ReproducerCondStack, Info, DT); Constant *FalseC = @@ -1005,7 +997,7 @@ void ConstraintInfo::addFact(CmpInst::Predicate Pred, Value *A, Value *B, LLVM_DEBUG({ dbgs() << " constraint: "; - dumpWithNames(R.Coefficients, getValue2Index(R.IsSigned)); + dumpConstraint(R.Coefficients, getValue2Index(R.IsSigned)); dbgs() << "\n"; }); @@ -1150,8 +1142,8 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT, break; LLVM_DEBUG({ dbgs() << "Removing "; - dumpWithNames(Info.getCS(E.IsSigned).getLastConstraint(), - Info.getValue2Index(E.IsSigned)); + dumpConstraint(Info.getCS(E.IsSigned).getLastConstraint(), + Info.getValue2Index(E.IsSigned)); dbgs() << "\n"; }); diff --git a/llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll b/llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll index a4cf0dc..3755fa0 100644 --- a/llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll +++ b/llvm/test/Transforms/ConstraintElimination/reproducer-remarks-debug.ll @@ -5,7 +5,7 @@ target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" ; CHECK: Condition %c.2 = icmp eq ptr %a, null implied by dominating constraints -; CHECK-NEXT: %a + -1 * % <= 0 +; CHECK-NEXT: %a + -1 * null <= 0 ; CHECK-NEXT: Creating reproducer for %c.2 = icmp eq ptr %a, null ; CHECK-NEXT: found external input ptr %a ; CHECK-NEXT: Materializing assumption %c.1 = icmp eq ptr %a, null -- 2.7.4