[llvm] 6425877 - [CostModel] Avoid traditional ConstantExpr crashy pitfails

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 26 12:48:55 PDT 2020


Author: Roman Lebedev
Date: 2020-06-26T22:48:10+03:00
New Revision: 64258773ad99b3b6a3eb2a456b79518c1444d9f3

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

LOG: [CostModel] Avoid traditional ConstantExpr crashy pitfails

I'm not sure if this is a regression from D81448 + D81643,
which moved at least the code cast from elsewhere,
or somehow no one triggered that before.
But now we can reach it with a non-instruction..

It is not straight-forward to write cost-model tests for constantexprs,
`-cost-model -analyze -cost-kind=` does not appear to look at them,
or maybe i'm doing it wrong.

I've encountered that via a SimplifyCFG crash,
so reduced (currently-crashing) test is added.
There are likely other instances.

For now, simply restore previous status quo of
not crashing and returning TTI::TCC_Basic.

Added: 
    llvm/test/Transforms/SimplifyCFG/constantexprs.ll

Modified: 
    llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
index 9cae4f506ab7..ca7106ab98aa 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
@@ -918,13 +918,17 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
                                            CostKind, I);
     }
     case Instruction::InsertElement: {
-      auto *IE = cast<InsertElementInst>(U);
+      auto *IE = dyn_cast<InsertElementInst>(U);
+      if (!IE)
+        return TTI::TCC_Basic; // FIXME
       auto *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
       unsigned Idx = CI ? CI->getZExtValue() : -1;
       return TargetTTI->getVectorInstrCost(Opcode, Ty, Idx);
     }
     case Instruction::ShuffleVector: {
-      auto *Shuffle = cast<ShuffleVectorInst>(U);
+      auto *Shuffle = dyn_cast<ShuffleVectorInst>(U);
+      if (!Shuffle)
+        return TTI::TCC_Basic; // FIXME
       auto *VecTy = cast<VectorType>(U->getType());
       auto *VecSrcTy = cast<VectorType>(U->getOperand(0)->getType());
 
@@ -954,7 +958,10 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
     }
     case Instruction::ExtractElement: {
       unsigned Idx = -1;
-      auto *EEI = cast<ExtractElementInst>(U);
+      auto *EEI = dyn_cast<ExtractElementInst>(U);
+      if (!EEI)
+        return TTI::TCC_Basic; // FIXME
+
       auto *CI = dyn_cast<ConstantInt>(EEI->getOperand(1));
       if (CI)
         Idx = CI->getZExtValue();

diff  --git a/llvm/test/Transforms/SimplifyCFG/constantexprs.ll b/llvm/test/Transforms/SimplifyCFG/constantexprs.ll
new file mode 100644
index 000000000000..5cfe2cfceafb
--- /dev/null
+++ b/llvm/test/Transforms/SimplifyCFG/constantexprs.ll
@@ -0,0 +1,31 @@
+; RUN: opt -simplifycfg -S < %s | FileCheck %s
+
+; See that we do not crash when queriying cost model about the cost of constant expression extractelement.
+
+%"struct.(anonymous namespace)::aj" = type { %struct.o }
+%struct.o = type { %struct.j }
+%struct.j = type { %struct.c }
+%struct.c = type { %struct.a }
+%struct.a = type { i8 }
+%struct.e = type { %struct.a* }
+
+$_ZN1eC2EPK1a = comdat any
+
+ at _ZN12_GLOBAL__N_12anE = internal global %"struct.(anonymous namespace)::aj" zeroinitializer, align 1
+
+declare dso_local i32 @_Zeq1eS_(%struct.a*, %struct.a*) local_unnamed_addr #2
+
+define internal fastcc %struct.a* @_ZNK1jIiN12_GLOBAL__N_12ajEE2aeERKi() unnamed_addr #0 align 2 {
+; CHECK-LABEL: @_ZNK1jIiN12_GLOBAL__N_12ajEE2aeERKi
+entry:
+  %call = call i32 @_Zeq1eS_(%struct.a* null, %struct.a* null)
+  %tobool = icmp eq i32 %call, 0
+  br i1 %tobool, label %cond.false, label %cond.end
+
+cond.false:                                       ; preds = %entry
+  br label %cond.end
+
+cond.end:                                         ; preds = %entry, %cond.false
+  %retval.sroa.0.0 = phi %struct.a* [ null, %cond.false ], [ extractelement (<1 x %struct.a*> inttoptr (<1 x i64> bitcast (i64 ptrtoint (%"struct.(anonymous namespace)::aj"* @_ZN12_GLOBAL__N_12anE to i64) to <1 x i64>) to <1 x %struct.a*>), i64 0), %entry ]
+  ret %struct.a*  %retval.sroa.0.0
+}


        


More information about the llvm-commits mailing list