[polly] r246394 - Store scalar dependences from outside the scop into alloca locations

Tobias Grosser via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 30 12:19:34 PDT 2015


Author: grosser
Date: Sun Aug 30 14:19:34 2015
New Revision: 246394

URL: http://llvm.org/viewvc/llvm-project?rev=246394&view=rev
Log:
Store scalar dependences from outside the scop into alloca locations

We already modeled read-only dependences to scalar values defined outside the
scop as memory reads and also generated read accesses from the corresponding
alloca instructions that have been used to pass these scalar values around
during code generation. However, besides for PHI nodes that have already been
handled, we failed to store the orignal read-only scalar values into these
alloc. This commit extends the initialization of scalar values to all read-only
scalar values used within the scop.

Modified:
    polly/trunk/include/polly/CodeGen/BlockGenerators.h
    polly/trunk/lib/CodeGen/BlockGenerators.cpp
    polly/trunk/test/Isl/CodeGen/read-only-scalars.ll

Modified: polly/trunk/include/polly/CodeGen/BlockGenerators.h
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/include/polly/CodeGen/BlockGenerators.h?rev=246394&r1=246393&r2=246394&view=diff
==============================================================================
--- polly/trunk/include/polly/CodeGen/BlockGenerators.h (original)
+++ polly/trunk/include/polly/CodeGen/BlockGenerators.h Sun Aug 30 14:19:34 2015
@@ -147,7 +147,7 @@ public:
   /// This will initialize and finalize the scalar variables we demoted during
   /// the code generation.
   ///
-  /// @see createScalarInitialization(Region &)
+  /// @see createScalarInitialization(Scop &)
   /// @see createScalarFinalization(Region &)
   void finalizeSCoP(Scop &S);
 
@@ -383,11 +383,8 @@ protected:
 
   /// @brief Initialize the memory of demoted scalars.
   ///
-  /// If a PHI node was demoted and one of its predecessor blocks was outside
-  /// the SCoP we need to initialize the memory cell we demoted the PHI into
-  /// with the value corresponding to that predecessor. As a SCoP is a
-  /// __single__ entry region there is at most one such predecessor.
-  void createScalarInitialization(Region &R);
+  /// @param S The scop for which to generate the scalar initializers.
+  void createScalarInitialization(Scop &S);
 
   /// @brief Promote the values of demoted scalars after the SCoP.
   ///

Modified: polly/trunk/lib/CodeGen/BlockGenerators.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/CodeGen/BlockGenerators.cpp?rev=246394&r1=246393&r2=246394&view=diff
==============================================================================
--- polly/trunk/lib/CodeGen/BlockGenerators.cpp (original)
+++ polly/trunk/lib/CodeGen/BlockGenerators.cpp Sun Aug 30 14:19:34 2015
@@ -464,7 +464,8 @@ void BlockGenerator::generateScalarStore
   }
 }
 
-void BlockGenerator::createScalarInitialization(Region &R) {
+void BlockGenerator::createScalarInitialization(Scop &S) {
+  Region &R = S.getRegion();
   // The split block __just before__ the region and optimized region.
   BasicBlock *SplitBB = R.getEnteringBlock();
   BranchInst *SplitBBTerm = cast<BranchInst>(SplitBB->getTerminator());
@@ -475,23 +476,41 @@ void BlockGenerator::createScalarInitial
   if (StartBB == R.getEntry())
     StartBB = SplitBBTerm->getSuccessor(1);
 
-  // For each PHI predecessor outside the region store the incoming operand
-  // value prior to entering the optimized region.
   Builder.SetInsertPoint(StartBB->getTerminator());
 
-  for (const auto &PHIOpMapping : PHIOpMap) {
-    const PHINode *PHI = cast<PHINode>(PHIOpMapping.getFirst());
+  for (auto &Pair : S.arrays()) {
+    auto &Array = Pair.second;
+    if (Array->getNumberOfDimensions() != 0)
+      continue;
+    if (Array->isPHI()) {
+      // For PHI nodes, the only values we need to store are the ones that
+      // reach the PHI node from outside the region. In general there should
+      // only be one such incoming edge and this edge should enter through
+      // 'SplitBB'.
+      auto PHI = cast<PHINode>(Array->getBasePtr());
+
+      for (auto BI = PHI->block_begin(), BE = PHI->block_end(); BI != BE; BI++)
+        if (!R.contains(*BI) && *BI != SplitBB)
+          llvm_unreachable("Incoming edges from outside the scop should always "
+                           "come from SplitBB");
+
+      int Idx = PHI->getBasicBlockIndex(SplitBB);
+      if (Idx < 0)
+        continue;
+
+      Value *ScalarValue = PHI->getIncomingValue(Idx);
 
-    // Check if this PHI has the split block as predecessor (that is the only
-    // possible predecessor outside the SCoP).
-    int idx = PHI->getBasicBlockIndex(SplitBB);
-    if (idx < 0)
+      Builder.CreateStore(ScalarValue, getOrCreatePHIAlloca(PHI));
       continue;
+    }
 
-    Value *ScalarValue = PHI->getIncomingValue(idx);
+    auto *Inst = dyn_cast<Instruction>(Array->getBasePtr());
+
+    if (Inst && R.contains(Inst))
+      continue;
 
-    // If the split block is the predecessor initialize the PHI operator alloca.
-    Builder.CreateStore(ScalarValue, PHIOpMapping.getSecond());
+    Builder.CreateStore(Array->getBasePtr(),
+                        getOrCreateScalarAlloca(Array->getBasePtr()));
   }
 }
 
@@ -540,7 +559,7 @@ void BlockGenerator::createScalarFinaliz
 }
 
 void BlockGenerator::finalizeSCoP(Scop &S) {
-  createScalarInitialization(S.getRegion());
+  createScalarInitialization(S);
   createScalarFinalization(S.getRegion());
 }
 

Modified: polly/trunk/test/Isl/CodeGen/read-only-scalars.ll
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/test/Isl/CodeGen/read-only-scalars.ll?rev=246394&r1=246393&r2=246394&view=diff
==============================================================================
--- polly/trunk/test/Isl/CodeGen/read-only-scalars.ll (original)
+++ polly/trunk/test/Isl/CodeGen/read-only-scalars.ll Sun Aug 30 14:19:34 2015
@@ -10,7 +10,12 @@
 ; SCALAR-LABEL: entry:
 ; SCALAR-NEXT: %scalar.s2a = alloca float
 
-; SCALAR:  %scalar.s2a.reload = load float, float* %scalar.s2a
+; SCALAR-LABEL: polly.start:
+; SCALAR-NEXT:  store float %scalar, float* %scalar.s2a
+
+; SCALAR-LABEL: polly.stmt.stmt1:
+; SCALAR-NEXT:  %val_p_scalar_ = load float, float* %A,
+; SCALAR-NEXT:  %scalar.s2a.reload = load float, float* %scalar.s2a
 ; SCALAR-NEXT:  %p_sum = fadd float %val_p_scalar_, %scalar.s2a.reload
 
 define void @foo(float* noalias %A, float %scalar) {




More information about the llvm-commits mailing list