[llvm] r364062 - [GVNSink] prevent crashing on mismatched instructions (PR42346)

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 21 08:17:24 PDT 2019


Author: spatel
Date: Fri Jun 21 08:17:24 2019
New Revision: 364062

URL: http://llvm.org/viewvc/llvm-project?rev=364062&view=rev
Log:
[GVNSink] prevent crashing on mismatched instructions (PR42346)

Patch based on suggestion by James Molloy (@jmolloy) in:
https://bugs.llvm.org/show_bug.cgi?id=42346

Added:
    llvm/trunk/test/Transforms/GVNSink/operand-mismatch.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/GVNSink.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/GVNSink.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/GVNSink.cpp?rev=364062&r1=364061&r2=364062&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/GVNSink.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/GVNSink.cpp Fri Jun 21 08:17:24 2019
@@ -713,6 +713,15 @@ Optional<SinkingInstructionCandidate> GV
   // FIXME: If any of these fail, we should partition up the candidates to
   // try and continue making progress.
   Instruction *I0 = NewInsts[0];
+
+  // If all instructions that are going to participate don't have the same
+  // number of operands, we can't do any useful PHI analysis for all operands.
+  auto hasDifferentNumOperands = [&I0](Instruction *I) {
+    return I->getNumOperands() != I0->getNumOperands();
+  };
+  if (any_of(NewInsts, hasDifferentNumOperands))
+    return None;
+
   for (unsigned OpNum = 0, E = I0->getNumOperands(); OpNum != E; ++OpNum) {
     ModelledPHI PHI(NewInsts, OpNum, ActivePreds);
     if (PHI.areAllIncomingValuesSame())

Added: llvm/trunk/test/Transforms/GVNSink/operand-mismatch.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GVNSink/operand-mismatch.ll?rev=364062&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/GVNSink/operand-mismatch.ll (added)
+++ llvm/trunk/test/Transforms/GVNSink/operand-mismatch.ll Fri Jun 21 08:17:24 2019
@@ -0,0 +1,34 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -gvn-sink -S < %s | FileCheck %s
+
+; This would assert/crash because the calls have different numbers of operands:
+; https://bugs.llvm.org/show_bug.cgi?id=42346
+
+%vec = type opaque
+%map = type { i32 }
+
+define void @PR42346() {
+; CHECK-LABEL: @PR42346(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[CALL1:%.*]] = call %vec* @bar(%map* undef, %vec* (%map*)* undef)
+; CHECK-NEXT:    br label [[EXIT:%.*]]
+; CHECK:       if:
+; CHECK-NEXT:    [[CALL2:%.*]] = call %vec* @baz(%map* undef)
+; CHECK-NEXT:    br label [[EXIT]]
+; CHECK:       exit:
+; CHECK-NEXT:    ret void
+;
+entry:
+  %call1 = call %vec* @bar(%map* undef, %vec* (%map*)* undef)
+  br label %exit
+
+if:
+  %call2 = call %vec* @baz(%map* undef)
+  br label %exit
+
+exit:
+  ret void
+}
+
+declare %vec* @bar(%map*, %vec* (%map*)*)
+declare %vec* @baz(%map*)




More information about the llvm-commits mailing list