[polly] r308876 - [ForwardOpTree] Support read-only value uses.

Michael Kruse via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 24 05:43:27 PDT 2017


Author: meinersbur
Date: Mon Jul 24 05:43:27 2017
New Revision: 308876

URL: http://llvm.org/viewvc/llvm-project?rev=308876&view=rev
Log:
[ForwardOpTree] Support read-only value uses.

Read-only values (values defined before the SCoP) require special
handing with -polly-analyze-read-only-scalars=true (which is the
default). If active, each use of a value requires a read access.
When a copied value uses a read-only value, we must also ensure that
such a MemoryAccess is available or is created.

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

Added:
    polly/trunk/test/ForwardOpTree/forward_readonly.ll
Modified:
    polly/trunk/include/polly/ScopBuilder.h
    polly/trunk/include/polly/ScopInfo.h
    polly/trunk/lib/Analysis/ScopBuilder.cpp
    polly/trunk/lib/Analysis/ScopInfo.cpp
    polly/trunk/lib/Transform/ForwardOpTree.cpp

Modified: polly/trunk/include/polly/ScopBuilder.h
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/include/polly/ScopBuilder.h?rev=308876&r1=308875&r2=308876&view=diff
==============================================================================
--- polly/trunk/include/polly/ScopBuilder.h (original)
+++ polly/trunk/include/polly/ScopBuilder.h Mon Jul 24 05:43:27 2017
@@ -21,6 +21,9 @@
 
 namespace polly {
 
+/// Command line switch whether to model read-only accesses.
+extern bool ModelReadOnlyScalars;
+
 /// Build the Polly IR (Scop and ScopStmt) on a Region.
 class ScopBuilder {
   //===-------------------------------------------------------------------===//

Modified: polly/trunk/include/polly/ScopInfo.h
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/include/polly/ScopInfo.h?rev=308876&r1=308875&r2=308876&view=diff
==============================================================================
--- polly/trunk/include/polly/ScopInfo.h (original)
+++ polly/trunk/include/polly/ScopInfo.h Mon Jul 24 05:43:27 2017
@@ -1628,6 +1628,18 @@ public:
   ///
   void printInstructions(raw_ostream &OS) const;
 
+  /// Check whether there is a value read access for @p V in this statement, and
+  /// if not, create one.
+  ///
+  /// This allows to add MemoryAccesses after the initial creation of the Scop
+  /// by ScopBuilder.
+  ///
+  /// @return The already existing or newly created MemoryKind::Value READ
+  /// MemoryAccess.
+  ///
+  /// @see ScopBuilder::ensureValueRead(Value*,ScopStmt*)
+  MemoryAccess *ensureValueRead(Value *V);
+
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
   /// Print the ScopStmt to stderr.
   void dump() const;

Modified: polly/trunk/lib/Analysis/ScopBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/Analysis/ScopBuilder.cpp?rev=308876&r1=308875&r2=308876&view=diff
==============================================================================
--- polly/trunk/lib/Analysis/ScopBuilder.cpp (original)
+++ polly/trunk/lib/Analysis/ScopBuilder.cpp Mon Jul 24 05:43:27 2017
@@ -32,10 +32,12 @@ STATISTIC(RichScopFound, "Number of Scop
 STATISTIC(InfeasibleScops,
           "Number of SCoPs with statically infeasible context.");
 
-static cl::opt<bool> ModelReadOnlyScalars(
+bool polly::ModelReadOnlyScalars;
+static cl::opt<bool, true> XModelReadOnlyScalars(
     "polly-analyze-read-only-scalars",
     cl::desc("Model read-only scalar values in the scop description"),
-    cl::Hidden, cl::ZeroOrMore, cl::init(true), cl::cat(PollyCategory));
+    cl::location(ModelReadOnlyScalars), cl::Hidden, cl::ZeroOrMore,
+    cl::init(true), cl::cat(PollyCategory));
 
 static cl::opt<bool> UnprofitableScalarAccs(
     "polly-unprofitable-scalar-accs",
@@ -778,6 +780,15 @@ void ScopBuilder::ensureValueWrite(Instr
 }
 
 void ScopBuilder::ensureValueRead(Value *V, ScopStmt *UserStmt) {
+  // TODO: Make ScopStmt::ensureValueRead(Value*) offer the same functionality
+  // to be able to replace this one. Currently, there is a split responsibility.
+  // In a first step, the MemoryAccess is created, but without the
+  // AccessRelation. In the second step by ScopStmt::buildAccessRelations(), the
+  // AccessRelation is created. At least for scalar accesses, there is no new
+  // information available at ScopStmt::buildAccessRelations(), so we could
+  // create the AccessRelation right away. This is what
+  // ScopStmt::ensureValueRead(Value*) does.
+
   auto *Scope = UserStmt->getSurroundingLoop();
   auto VUse = VirtualUse::create(scop.get(), UserStmt, Scope, V, false);
   switch (VUse.getKind()) {

Modified: polly/trunk/lib/Analysis/ScopInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/Analysis/ScopInfo.cpp?rev=308876&r1=308875&r2=308876&view=diff
==============================================================================
--- polly/trunk/lib/Analysis/ScopInfo.cpp (original)
+++ polly/trunk/lib/Analysis/ScopInfo.cpp Mon Jul 24 05:43:27 2017
@@ -2042,6 +2042,22 @@ void ScopStmt::removeSingleMemoryAccess(
   }
 }
 
+MemoryAccess *ScopStmt::ensureValueRead(Value *V) {
+  MemoryAccess *Access = lookupInputAccessOf(V);
+  if (Access)
+    return Access;
+
+  ScopArrayInfo *SAI =
+      Parent.getOrCreateScopArrayInfo(V, V->getType(), {}, MemoryKind::Value);
+  Access = new MemoryAccess(this, nullptr, MemoryAccess::READ, V, V->getType(),
+                            true, {}, {}, V, MemoryKind::Value);
+  Parent.addAccessFunction(Access);
+  Access->buildAccessRelation(SAI);
+  addAccess(Access);
+  Parent.addAccessData(Access);
+  return Access;
+}
+
 raw_ostream &polly::operator<<(raw_ostream &O, const ScopStmt &S) {
   S.print(O, PollyPrintInstructions);
   return O;

Modified: polly/trunk/lib/Transform/ForwardOpTree.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/Transform/ForwardOpTree.cpp?rev=308876&r1=308875&r2=308876&view=diff
==============================================================================
--- polly/trunk/lib/Transform/ForwardOpTree.cpp (original)
+++ polly/trunk/lib/Transform/ForwardOpTree.cpp Mon Jul 24 05:43:27 2017
@@ -13,6 +13,7 @@
 
 #include "polly/ForwardOpTree.h"
 
+#include "polly/ScopBuilder.h"
 #include "polly/ScopInfo.h"
 #include "polly/ScopPass.h"
 #include "polly/Support/GICHelper.h"
@@ -25,6 +26,7 @@ using namespace polly;
 using namespace llvm;
 
 STATISTIC(TotalInstructionsCopied, "Number of copied instructions");
+STATISTIC(TotalReadOnlyCopied, "Number of copied read-only accesses");
 STATISTIC(TotalForwardedTrees, "Number of forwarded operand trees");
 STATISTIC(TotalModifiedStmts,
           "Number of statements with at least one forwarded tree");
@@ -37,6 +39,7 @@ namespace {
 enum ForwardingDecision {
   FD_CannotForward,
   FD_CanForward,
+  FD_CanForwardTree,
   FD_DidForward,
 };
 
@@ -58,6 +61,9 @@ private:
   /// How many instructions have been copied to other statements.
   int NumInstructionsCopied = 0;
 
+  /// How many read-only accesses have been copied.
+  int NumReadOnlyCopied = 0;
+
   /// How many operand trees have been forwarded.
   int NumForwardedTrees = 0;
 
@@ -71,6 +77,8 @@ private:
     OS.indent(Indent) << "Statistics {\n";
     OS.indent(Indent + 4) << "Instructions copied: " << NumInstructionsCopied
                           << '\n';
+    OS.indent(Indent + 4) << "Read-only accesses copied: " << NumReadOnlyCopied
+                          << '\n';
     OS.indent(Indent + 4) << "Operand trees forwarded: " << NumForwardedTrees
                           << '\n';
     OS.indent(Indent + 4) << "Statements with forwarded operand trees: "
@@ -132,9 +140,16 @@ private:
       return FD_CannotForward;
 
     case VirtualUse::ReadOnly:
-      // Not supported yet.
-      DEBUG(dbgs() << "    Cannot forward read-only val: " << *UseVal << "\n");
-      return FD_CannotForward;
+      if (!DoIt)
+        return FD_CanForward;
+
+      // If we model read-only scalars, we need to create a MemoryAccess for it.
+      if (ModelReadOnlyScalars)
+        TargetStmt->ensureValueRead(UseVal);
+
+      NumReadOnlyCopied++;
+      TotalReadOnlyCopied++;
+      return FD_DidForward;
 
     case VirtualUse::Intra:
     case VirtualUse::Inter:
@@ -183,6 +198,7 @@ private:
           return FD_CannotForward;
 
         case FD_CanForward:
+        case FD_CanForwardTree:
           assert(!DoIt);
           break;
 
@@ -194,7 +210,7 @@ private:
 
       if (DoIt)
         return FD_DidForward;
-      return FD_CanForward;
+      return FD_CanForwardTree;
     }
 
     llvm_unreachable("Case unhandled");
@@ -211,7 +227,7 @@ private:
     ForwardingDecision Assessment =
         canForwardTree(Stmt, RA->getAccessValue(), Stmt, InLoop, false);
     assert(Assessment != FD_DidForward);
-    if (Assessment == FD_CannotForward)
+    if (Assessment != FD_CanForwardTree)
       return false;
 
     ForwardingDecision Execution =

Added: polly/trunk/test/ForwardOpTree/forward_readonly.ll
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/test/ForwardOpTree/forward_readonly.ll?rev=308876&view=auto
==============================================================================
--- polly/trunk/test/ForwardOpTree/forward_readonly.ll (added)
+++ polly/trunk/test/ForwardOpTree/forward_readonly.ll Mon Jul 24 05:43:27 2017
@@ -0,0 +1,84 @@
+; RUN: opt %loadPolly -polly-analyze-read-only-scalars=true  -polly-optree -analyze < %s | FileCheck %s -match-full-lines -check-prefixes=STATS,MODEL
+; RUN: opt %loadPolly -polly-analyze-read-only-scalars=false -polly-optree -analyze < %s | FileCheck %s -match-full-lines -check-prefixes=STATS,NOMODEL
+;
+; Move %val to %bodyB, so %bodyA can be removed (by -polly-simplify)
+;
+; for (int j = 0; j < n; j += 1) {
+; bodyA:
+;   double val = arg + 21.0;
+;
+; bodyB:
+;   A[0] = val;
+; }
+;
+define void @func(i32 %n, double* noalias nonnull %A, double %arg) {
+entry:
+  br label %for
+
+for:
+  %j = phi i32 [0, %entry], [%j.inc, %inc]
+  %j.cmp = icmp slt i32 %j, %n
+  br i1 %j.cmp, label %bodyA, label %exit
+
+    bodyA:
+      %val = fadd double %arg, 21.0
+      br label %bodyB
+
+    bodyB:
+      store double %val, double* %A
+      br label %inc
+
+inc:
+  %j.inc = add nuw nsw i32 %j, 1
+  br label %for
+
+exit:
+  br label %return
+
+return:
+  ret void
+}
+
+
+; STATS: Statistics {
+; STATS:     Instructions copied: 1
+; STATS:     Read-only accesses copied: 1
+; STATS:     Operand trees forwarded: 1
+; STATS:     Statements with forwarded operand trees: 1
+; STATS: }
+
+; MODEL:      After statements {
+; MODEL-NEXT:     Stmt_bodyA
+; MODEL-NEXT:             ReadAccess :=       [Reduction Type: NONE] [Scalar: 1]
+; MODEL-NEXT:                 [n] -> { Stmt_bodyA[i0] -> MemRef_arg[] };
+; MODEL-NEXT:             MustWriteAccess :=  [Reduction Type: NONE] [Scalar: 1]
+; MODEL-NEXT:                 [n] -> { Stmt_bodyA[i0] -> MemRef_val[] };
+; MODEL-NEXT:             Instructions {
+; MODEL-NEXT:                   %val = fadd double %arg, 2.100000e+01
+; MODEL-NEXT:                 }
+; MODEL-NEXT:     Stmt_bodyB
+; MODEL-NEXT:             MustWriteAccess :=  [Reduction Type: NONE] [Scalar: 0]
+; MODEL-NEXT:                 [n] -> { Stmt_bodyB[i0] -> MemRef_A[0] };
+; MODEL-NEXT:             ReadAccess :=       [Reduction Type: NONE] [Scalar: 1]
+; MODEL-NEXT:                 [n] -> { Stmt_bodyB[i0] -> MemRef_arg[] };
+; MODEL-NEXT:             Instructions {
+; MODEL-NEXT:                   %val = fadd double %arg, 2.100000e+01
+; MODEL-NEXT:                   store double %val, double* %A
+; MODEL-NEXT:                 }
+; MODEL-NEXT: }
+
+; NOMODEL:      After statements {
+; NOMODEL-NEXT:     Stmt_bodyA
+; NOMODEL-NEXT:             MustWriteAccess :=  [Reduction Type: NONE] [Scalar: 1]
+; NOMODEL-NEXT:                 [n] -> { Stmt_bodyA[i0] -> MemRef_val[] };
+; NOMODEL-NEXT:             Instructions {
+; NOMODEL-NEXT:                   %val = fadd double %arg, 2.100000e+01
+; NOMODEL-NEXT:                 }
+; NOMODEL-NEXT:     Stmt_bodyB
+; NOMODEL-NEXT:             MustWriteAccess :=  [Reduction Type: NONE] [Scalar: 0]
+; NOMODEL-NEXT:                 [n] -> { Stmt_bodyB[i0] -> MemRef_A[0] };
+; NOMODEL-NEXT:             Instructions {
+; NOMODEL-NEXT:                   %val = fadd double %arg, 2.100000e+01
+; NOMODEL-NEXT:                   store double %val, double* %A
+; NOMODEL-NEXT:                 }
+; NOMODEL-NEXT: }




More information about the llvm-commits mailing list