[PATCH] D112472: [Scalarizer] Do not insert instructions between PHI nodes.

Daniele Vettorel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 25 10:53:51 PDT 2021


vettoreldaniele created this revision.
vettoreldaniele added a reviewer: bjope.
vettoreldaniele added a project: LLVM.
Herald added a subscriber: hiraditya.
vettoreldaniele requested review of this revision.

The scalarizer pass seems to be inserting instructions in-between PHI nodes that end up staying at the end of the pass, resulting in malformed IR.

This patch adds a check to make sure the `extractelement` instructions that it adds are correctly placed after all PHI nodes.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112472

Files:
  llvm/lib/Transforms/Scalar/Scalarizer.cpp
  llvm/test/Transforms/Scalarizer/phi-order.ll


Index: llvm/test/Transforms/Scalarizer/phi-order.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/Scalarizer/phi-order.ll
@@ -0,0 +1,29 @@
+; RUN: opt %s -passes='function(scalarizer)' -S -o - | FileCheck %s
+
+; This input caused the scalarizer to insert non-PHI nodes
+; in between PHI nodes (%1 and %2).
+
+define <3 x float> @func(i32 %inval) {
+.entry:
+  br label %0
+
+0:                                                ; preds = %3, %.entry
+; CHECK: %.i01 = phi float [ 0.000000e+00, %.entry ], [ %.i01, %3 ]
+; CHECK-NEXT: %.i12 = phi float [ 0.000000e+00, %.entry ], [ %.i12, %3 ]
+; CHECK-NEXT: %.i23 = phi float [ 0.000000e+00, %.entry ], [ %.i23, %3 ]
+; CHECK-NEXT: %1 = phi float [ 1.000000e+00, %.entry ], [ 2.000000e+00, %3 ]
+; CHECK-NEXT: %.upto0 = insertelement <3 x float> poison, float %.i01, i32 0
+; CHECK-NEXT: %.upto1 = insertelement <3 x float> %.upto0, float %.i12, i32 1
+; CHECK-NEXT: %2 = insertelement <3 x float> %.upto1, float %.i23, i32 2
+  %1 = phi <3 x float> [ <float 0.0, float 0.0, float 0.0>, %.entry], [ %1, %3 ]
+  %2 = phi float [ 1.0, %.entry], [ 2.0, %3 ]
+  br label %3
+
+3:                                                ; preds = %0
+  %cond = icmp eq i32 %inval, 0
+  br i1 %cond, label %0, label %exit
+
+exit:
+  ret <3 x float>  %1
+}
+
Index: llvm/lib/Transforms/Scalar/Scalarizer.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/Scalarizer.cpp
+++ llvm/lib/Transforms/Scalar/Scalarizer.cpp
@@ -371,10 +371,12 @@
       return Scatterer(Point->getParent(), Point->getIterator(),
                        UndefValue::get(V->getType()));
     // Put the scattered form of an instruction directly after the
-    // instruction.
+    // instruction, unless it is a PHI node.
     BasicBlock *BB = VOp->getParent();
-    return Scatterer(BB, std::next(BasicBlock::iterator(VOp)),
-                     V, &Scattered[V]);
+    auto InsertionPoint = std::next(BasicBlock::iterator(VOp));
+    if (isa<PHINode>(VOp))
+      InsertionPoint = BB->getFirstInsertionPt();
+    return Scatterer(BB, InsertionPoint, V, &Scattered[V]);
   }
   // In the fallback case, just put the scattered before Point and
   // keep the result local to Point.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112472.382051.patch
Type: text/x-patch
Size: 2302 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211025/90f5d29a/attachment.bin>


More information about the llvm-commits mailing list