[llvm-branch-commits] [llvm] 67d0736 - Revert "[DebugInfo][LoopStrengthReduction] SCEV-based salvaging for LSR"

2021-07-28 Thread Chris Jackson via llvm-branch-commits

Author: Chris Jackson
Date: 2021-07-29T00:04:50+01:00
New Revision: 67d0736b14c7888e63af16dcc71129ce07383010

URL: 
https://github.com/llvm/llvm-project/commit/67d0736b14c7888e63af16dcc71129ce07383010
DIFF: 
https://github.com/llvm/llvm-project/commit/67d0736b14c7888e63af16dcc71129ce07383010.diff

LOG: Revert "[DebugInfo][LoopStrengthReduction] SCEV-based salvaging for LSR"
This was reverted due to a reported crash.
This reverts commit 796b84d26f4d461fb50e7b4e84e15a10eaca88fc.

Added: 


Modified: 
llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
llvm/test/Transforms/LoopStrengthReduce/dbg-preserve-0.ll
llvm/test/Transforms/LoopStrengthReduce/dbg-preserve-2.ll

Removed: 
llvm/test/Transforms/LoopStrengthReduce/debuginfo-scev-salvage-0.ll
llvm/test/Transforms/LoopStrengthReduce/debuginfo-scev-salvage-1.ll
llvm/test/Transforms/LoopStrengthReduce/debuginfo-scev-salvage-2.ll
llvm/test/Transforms/LoopStrengthReduce/debuginfo-scev-salvage-3.ll
llvm/test/Transforms/LoopStrengthReduce/debuginfo-scev-salvage-4.ll



diff  --git a/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h 
b/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
index 59bf3a342caa..8662dbf385dc 100644
--- a/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
+++ b/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
@@ -83,9 +83,6 @@ class SCEVExpander : public SCEVVisitor {
   /// InsertedValues/InsertedPostIncValues.
   SmallPtrSet ReusedValues;
 
-  // The induction variables generated.
-  SmallVector InsertedIVs;
-
   /// A memoization of the "relevant" loop for a given SCEV.
   DenseMap RelevantLoops;
 
@@ -202,11 +199,9 @@ class SCEVExpander : public SCEVVisitor {
 InsertedPostIncValues.clear();
 ReusedValues.clear();
 ChainedPhis.clear();
-InsertedIVs.clear();
   }
 
   ScalarEvolution *getSE() { return &SE; }
-  const SmallVectorImpl &getInsertedIVs() const { return InsertedIVs; }
 
   /// Return a vector containing all instructions inserted during expansion.
   SmallVector getAllInsertedInstructions() const {

diff  --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp 
b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
index 5f210380ae5a..b585818af595 100644
--- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -1981,9 +1981,6 @@ class LSRInstance {
   /// IV users that belong to profitable IVChains.
   SmallPtrSet IVIncSet;
 
-  /// Induction variables that were generated and inserted by the SCEV 
Expander.
-  SmallVector ScalarEvolutionIVs;
-
   void OptimizeShadowIV();
   bool FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse);
   ICmpInst *OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse);
@@ -2088,9 +2085,6 @@ class LSRInstance {
   TargetLibraryInfo &TLI, MemorySSAUpdater *MSSAU);
 
   bool getChanged() const { return Changed; }
-  const SmallVectorImpl &getScalarEvolutionIVs() const {
-return ScalarEvolutionIVs;
-  }
 
   void print_factors_and_types(raw_ostream &OS) const;
   void print_fixups(raw_ostream &OS) const;
@@ -5595,11 +5589,6 @@ void LSRInstance::ImplementSolution(
 GenerateIVChain(Chain, Rewriter, DeadInsts);
 Changed = true;
   }
-
-  for (const WeakVH &IV : Rewriter.getInsertedIVs())
-if (IV && dyn_cast(&*IV)->getParent())
-  ScalarEvolutionIVs.push_back(IV);
-
   // Clean up after ourselves. This must be done before deleting any
   // instructions.
   Rewriter.clear();
@@ -5870,389 +5859,87 @@ void 
LoopStrengthReduce::getAnalysisUsage(AnalysisUsage &AU) const {
   AU.addPreserved();
 }
 
-struct SCEVDbgValueBuilder {
-  SCEVDbgValueBuilder() = default;
-  SCEVDbgValueBuilder(const SCEVDbgValueBuilder &Base) {
-Values = Base.Values;
-Expr = Base.Expr;
-  }
-
-  /// The DIExpression as we translate the SCEV.
-  SmallVector Expr;
-  /// The location ops of the DIExpression.
-  SmallVector Values;
-
-  void pushOperator(uint64_t Op) { Expr.push_back(Op); }
-  void pushUInt(uint64_t Operand) { Expr.push_back(Operand); }
-
-  /// Add a DW_OP_LLVM_arg to the expression, followed by the index of the 
value
-  /// in the set of values referenced by the expression.
-  void pushValue(llvm::Value *V) {
-Expr.push_back(llvm::dwarf::DW_OP_LLVM_arg);
-auto *It =
-std::find(Values.begin(), Values.end(), llvm::ValueAsMetadata::get(V));
-unsigned ArgIndex = 0;
-if (It != Values.end()) {
-  ArgIndex = std::distance(Values.begin(), It);
-} else {
-  ArgIndex = Values.size();
-  Values.push_back(llvm::ValueAsMetadata::get(V));
-}
-Expr.push_back(ArgIndex);
-  }
-
-  void pushValue(const SCEVUnknown *U) {
-llvm::Value *V = cast(U)->getValue();
-pushValue(V);
-  }
-
-  void pushC

[llvm-branch-commits] [llvm] dd60b80 - [DebugInfo][LoopStrengthReduction] SCEV-based salvaging for LSR

2021-08-05 Thread Chris Jackson via llvm-branch-commits

Author: Chris Jackson
Date: 2021-08-05T10:34:33+01:00
New Revision: dd60b80561ce637f8b97d709f38bc53c8d0c0510

URL: 
https://github.com/llvm/llvm-project/commit/dd60b80561ce637f8b97d709f38bc53c8d0c0510
DIFF: 
https://github.com/llvm/llvm-project/commit/dd60b80561ce637f8b97d709f38bc53c8d0c0510.diff

LOG: [DebugInfo][LoopStrengthReduction] SCEV-based salvaging for LSR

Reapply commit d675b594f4f1e1f6a195fb9a4fd02cf3de92292d that was
reverted due to buildbot failures. A simple fix has been applied to
remove an assertion.

Differential Revision: https://reviews.llvm.org/D105207

(cherry picked from commit 0ba8595287ea2203ef2250e2b0b41f284a055518)

Added: 
llvm/test/Transforms/LoopStrengthReduce/debuginfo-scev-salvage-0.ll
llvm/test/Transforms/LoopStrengthReduce/debuginfo-scev-salvage-1.ll
llvm/test/Transforms/LoopStrengthReduce/debuginfo-scev-salvage-2.ll
llvm/test/Transforms/LoopStrengthReduce/debuginfo-scev-salvage-3.ll
llvm/test/Transforms/LoopStrengthReduce/debuginfo-scev-salvage-4.ll

Modified: 
llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
llvm/test/Transforms/LoopStrengthReduce/dbg-preserve-0.ll
llvm/test/Transforms/LoopStrengthReduce/dbg-preserve-2.ll

Removed: 




diff  --git a/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h 
b/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
index 8662dbf385dc9..59bf3a342caa3 100644
--- a/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
+++ b/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
@@ -83,6 +83,9 @@ class SCEVExpander : public SCEVVisitor {
   /// InsertedValues/InsertedPostIncValues.
   SmallPtrSet ReusedValues;
 
+  // The induction variables generated.
+  SmallVector InsertedIVs;
+
   /// A memoization of the "relevant" loop for a given SCEV.
   DenseMap RelevantLoops;
 
@@ -199,9 +202,11 @@ class SCEVExpander : public SCEVVisitor {
 InsertedPostIncValues.clear();
 ReusedValues.clear();
 ChainedPhis.clear();
+InsertedIVs.clear();
   }
 
   ScalarEvolution *getSE() { return &SE; }
+  const SmallVectorImpl &getInsertedIVs() const { return InsertedIVs; }
 
   /// Return a vector containing all instructions inserted during expansion.
   SmallVector getAllInsertedInstructions() const {

diff  --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp 
b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
index b585818af5952..a56a4d736f68a 100644
--- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -1981,6 +1981,9 @@ class LSRInstance {
   /// IV users that belong to profitable IVChains.
   SmallPtrSet IVIncSet;
 
+  /// Induction variables that were generated and inserted by the SCEV 
Expander.
+  SmallVector ScalarEvolutionIVs;
+
   void OptimizeShadowIV();
   bool FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse);
   ICmpInst *OptimizeMax(ICmpInst *Cond, IVStrideUse* &CondUse);
@@ -2085,6 +2088,9 @@ class LSRInstance {
   TargetLibraryInfo &TLI, MemorySSAUpdater *MSSAU);
 
   bool getChanged() const { return Changed; }
+  const SmallVectorImpl &getScalarEvolutionIVs() const {
+return ScalarEvolutionIVs;
+  }
 
   void print_factors_and_types(raw_ostream &OS) const;
   void print_fixups(raw_ostream &OS) const;
@@ -5589,6 +5595,11 @@ void LSRInstance::ImplementSolution(
 GenerateIVChain(Chain, Rewriter, DeadInsts);
 Changed = true;
   }
+
+  for (const WeakVH &IV : Rewriter.getInsertedIVs())
+if (IV && dyn_cast(&*IV)->getParent())
+  ScalarEvolutionIVs.push_back(IV);
+
   // Clean up after ourselves. This must be done before deleting any
   // instructions.
   Rewriter.clear();
@@ -5859,87 +5870,391 @@ void 
LoopStrengthReduce::getAnalysisUsage(AnalysisUsage &AU) const {
   AU.addPreserved();
 }
 
-using EqualValues = SmallVector, 4>;
-using EqualValuesMap =
-DenseMap>>;
-using LocationMap =
-DenseMap>;
+struct SCEVDbgValueBuilder {
+  SCEVDbgValueBuilder() = default;
+  SCEVDbgValueBuilder(const SCEVDbgValueBuilder &Base) {
+Values = Base.Values;
+Expr = Base.Expr;
+  }
+
+  /// The DIExpression as we translate the SCEV.
+  SmallVector Expr;
+  /// The location ops of the DIExpression.
+  SmallVector Values;
+
+  void pushOperator(uint64_t Op) { Expr.push_back(Op); }
+  void pushUInt(uint64_t Operand) { Expr.push_back(Operand); }
+
+  /// Add a DW_OP_LLVM_arg to the expression, followed by the index of the 
value
+  /// in the set of values referenced by the expression.
+  void pushValue(llvm::Value *V) {
+Expr.push_back(llvm::dwarf::DW_OP_LLVM_arg);
+auto *It =
+std::find(Values.begin(), Values.end(), llvm::ValueAsMetadata::get(V));
+unsigned ArgIndex = 0;
+if (It != Values.end()) {
+  ArgIndex = std::distance(Value

[llvm-branch-commits] [llvm] 692f875 - Follow-up to D105207, only salvage affine SCEVs to avoid a crash

2021-08-05 Thread Chris Jackson via llvm-branch-commits

Author: Jeremy Morse
Date: 2021-08-05T10:35:08+01:00
New Revision: 692f875535db1d79e5cb9c3862a6cad0228d04e7

URL: 
https://github.com/llvm/llvm-project/commit/692f875535db1d79e5cb9c3862a6cad0228d04e7
DIFF: 
https://github.com/llvm/llvm-project/commit/692f875535db1d79e5cb9c3862a6cad0228d04e7.diff

LOG: Follow-up to D105207, only salvage affine SCEVs to avoid a crash

SCEVToIterCountExpr only expects to be fed affine expressions, but
DbgRewriteSalvageableDVIs is feeding it non-affine induction variables.
Following this up with an obvious fix, will add test coverage too if
this avoids D105207 being reverted.

(cherry picked from commit 2537120c870c04893636f171f553024f378c2de8)

Added: 


Modified: 
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp 
b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
index a56a4d736f68..1ea4e116bd2f 100644
--- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -6162,6 +6162,9 @@ DbgRewriteSalvageableDVIs(llvm::Loop *L, ScalarEvolution 
&SE,
   bool Changed = false;
   if (const SCEVAddRecExpr *IVAddRec =
   dyn_cast(SCEVInductionVar)) {
+if (!IVAddRec->isAffine())
+  return false;
+
 SCEVDbgValueBuilder IterCountExpr;
 IterCountExpr.pushValue(LSRInductionVar);
 if (!IterCountExpr.SCEVToIterCountExpr(*IVAddRec, SE))



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 8988ce3 - [DebugInfo][LSR] Avoid crashes on large integer inputs

2021-08-05 Thread Chris Jackson via llvm-branch-commits

Author: Chris Jackson
Date: 2021-08-05T10:38:19+01:00
New Revision: 8988ce302864ef3216e252af7d491482e71f79c7

URL: 
https://github.com/llvm/llvm-project/commit/8988ce302864ef3216e252af7d491482e71f79c7
DIFF: 
https://github.com/llvm/llvm-project/commit/8988ce302864ef3216e252af7d491482e71f79c7.diff

LOG: [DebugInfo][LSR] Avoid crashes on large integer inputs

SCEV-based salvaging in LSR translates SCEVs to DIExpressions. SCEVs may
contain very large integers but the translation does not support
integers greater than 64 bits. This patch adds checks to ensure
conversions of these large integers is not attempted. A regression test
is added to ensure no such translation is attempted.

Reviewed by: StephenTozer

PR: https://bugs.llvm.org/show_bug.cgi?id=51329

Differential Revision: https://reviews.llvm.org/D107438

(cherry picked from commit 21ee38e24f9801a567306b2a88defacf6e589a8b)

Added: 
llvm/test/Transforms/LoopStrengthReduce/pr51329.ll

Modified: 
llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Removed: 




diff  --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp 
b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
index 1ea4e116bd2fc..404852f1dd4dd 100644
--- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -5906,9 +5906,12 @@ struct SCEVDbgValueBuilder {
 pushValue(V);
   }
 
-  void pushConst(const SCEVConstant *C) {
+  bool pushConst(const SCEVConstant *C) {
+if (C->getAPInt().getMinSignedBits() > 64)
+  return false;
 Expr.push_back(llvm::dwarf::DW_OP_consts);
 Expr.push_back(C->getAPInt().getSExtValue());
+return true;
   }
 
   /// Several SCEV types are sequences of the same arithmetic operator applied
@@ -5947,7 +5950,7 @@ struct SCEVDbgValueBuilder {
   bool pushSCEV(const llvm::SCEV *S) {
 bool Success = true;
 if (const SCEVConstant *StartInt = dyn_cast(S)) {
-  pushConst(StartInt);
+  Success &= pushConst(StartInt);
 
 } else if (const SCEVUnknown *U = dyn_cast(S)) {
   if (!U->getValue())
@@ -6033,6 +6036,8 @@ struct SCEVDbgValueBuilder {
   /// SCEV constant value is an identity function.
   bool isIdentityFunction(uint64_t Op, const SCEV *S) {
 if (const SCEVConstant *C = dyn_cast(S)) {
+  if (C->getAPInt().getMinSignedBits() > 64)
+return false;
   int64_t I = C->getAPInt().getSExtValue();
   switch (Op) {
   case llvm::dwarf::DW_OP_plus:

diff  --git a/llvm/test/Transforms/LoopStrengthReduce/pr51329.ll 
b/llvm/test/Transforms/LoopStrengthReduce/pr51329.ll
new file mode 100644
index 0..f8f1c48eeaa86
--- /dev/null
+++ b/llvm/test/Transforms/LoopStrengthReduce/pr51329.ll
@@ -0,0 +1,50 @@
+; RUN: opt -S -loop-reduce %s | FileCheck %s
+;
+; Test that LSR SCEV-based salvaging does not crash when translating SCEVs
+; that contain integers with binary representations greater than 64-bits. 
+;
+; CHECK: call void @llvm.dbg.value(metadata i64 %var2, metadata !{{[0-9]+}}, 
metadata !DIExpression(DW_OP_plus_uconst, 228, DW_OP_stack_value))
+
+
+target datalayout = 
"e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
+declare void @llvm.dbg.value(metadata, metadata, metadata)
+
+; Function Attrs: nounwind
+define hidden void @reproducer() local_unnamed_addr !dbg !5 {
+init:
+  %0 = lshr i128 undef, 64
+  %var1 = trunc i128 %0 to i64
+  %1 = add nuw i64 undef, %var1
+  %var2 = lshr i64 %1, 12
+  br label %Label_d0
+
+Label_d0: ; preds = %Label_d0, %init
+  %var3 = phi i64 [ %var2, %init ], [ %var4, %Label_d0 ]
+  call void @llvm.dbg.value(metadata i64 %var2, metadata !11, metadata 
!DIExpression(DW_OP_plus_uconst, 228, DW_OP_stack_value)), !dbg !12
+  %var4 = add i64 %var3, -1
+  %var5 = icmp eq i64 %var4, 0
+  br i1 %var5, label %Label_1bc, label %Label_d0
+
+Label_1bc:; preds = %Label_d0
+  ret void
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3, !4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, 
producer: "frontend", isOptimized: true, runtimeVersion: 0, emissionKind: 
FullDebug, enums: !2)
+!1 = !DIFile(filename: "source", directory: "")
+!2 = !{}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = !{i32 1, !"wchar_size", i32 4}
+!5 = distinct !DISubprogram(name: "reproducer", scope: !1, file: !1, line: 
904320, type: !6, scopeLine: 904320, spFlags: DISPFlagDefinition, unit: !0, 
retainedNodes: !10)
+!6 = !DISubroutineType(types: !7)
+!7 = !{null, !8, !9, !9, !9, !9, !9, !9}
+!8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
+!9 = !DIBasicType(name: "my_type", size: 64, encoding: DW_ATE_unsigned)
+!10 = !{!11}
+!11 = !DILocalVariable(name: "my_var", arg: 1, scope: !5, file: !1,