From 092147724853b63036cf98fc9948903d4db383c4 Mon Sep 17 00:00:00 2001 From: Tobias Grosser Date: Tue, 15 Dec 2015 23:49:53 +0000 Subject: [PATCH] ScopInfo: Look up first (and only) array access When rewriting the access functions of load/store statements, we are only interested in the actual array memory location. The current code just took the very first memory access, which could be a scalar or an array access. As a result, we failed to update access functions even though this was requested via .jscop. llvm-svn: 255713 --- polly/include/polly/ScopInfo.h | 27 +++++++++++ polly/lib/CodeGen/BlockGenerators.cpp | 2 +- .../MemAccess/update_access_functions.ll | 46 ++++++++++++++++++ ...te_access_functions___%loop1---%exit.jscop | 47 +++++++++++++++++++ ...nctions___%loop1---%exit.jscop.transformed | 47 +++++++++++++++++++ 5 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 polly/test/Isl/CodeGen/MemAccess/update_access_functions.ll create mode 100644 polly/test/Isl/CodeGen/MemAccess/update_access_functions___%loop1---%exit.jscop create mode 100644 polly/test/Isl/CodeGen/MemAccess/update_access_functions___%loop1---%exit.jscop.transformed diff --git a/polly/include/polly/ScopInfo.h b/polly/include/polly/ScopInfo.h index caf5247cf10a..130e4360a155 100644 --- a/polly/include/polly/ScopInfo.h +++ b/polly/include/polly/ScopInfo.h @@ -979,6 +979,33 @@ public: return *MA; } + /// @brief Return the only array access for @p Inst. + /// + /// @param Inst The instruction for which to look up the access. + /// @returns The unique array memory access related to Inst. + MemoryAccess &getArrayAccessFor(const Instruction *Inst) const { + auto It = InstructionToAccess.find(Inst); + assert(It != InstructionToAccess.end() && + "No memory accesses found for instruction"); + auto *Accesses = It->getSecond(); + + assert(Accesses && "No memory accesses found for instruction"); + + MemoryAccess *ArrayAccess = nullptr; + + for (auto Access : *Accesses) { + if (!Access->isArrayKind()) + continue; + + assert(!ArrayAccess && "More then one array access for instruction"); + + ArrayAccess = Access; + } + + assert(ArrayAccess && "No array access found for instruction!"); + return *ArrayAccess; + } + /// @brief Return the __first__ (scalar) memory access for @p Inst if any. MemoryAccess *lookupAccessFor(const Instruction *Inst) const { auto It = InstructionToAccess.find(Inst); diff --git a/polly/lib/CodeGen/BlockGenerators.cpp b/polly/lib/CodeGen/BlockGenerators.cpp index 17305da9b597..b4364df7fdaa 100644 --- a/polly/lib/CodeGen/BlockGenerators.cpp +++ b/polly/lib/CodeGen/BlockGenerators.cpp @@ -164,7 +164,7 @@ void BlockGenerator::copyInstScalar(ScopStmt &Stmt, Instruction *Inst, Value *BlockGenerator::generateLocationAccessed( ScopStmt &Stmt, const Instruction *Inst, Value *Pointer, ValueMapT &BBMap, LoopToScevMapT <S, isl_id_to_ast_expr *NewAccesses) { - const MemoryAccess &MA = Stmt.getAccessFor(Inst); + const MemoryAccess &MA = Stmt.getArrayAccessFor(Inst); isl_ast_expr *AccessExpr = isl_id_to_ast_expr_get(NewAccesses, MA.getId()); diff --git a/polly/test/Isl/CodeGen/MemAccess/update_access_functions.ll b/polly/test/Isl/CodeGen/MemAccess/update_access_functions.ll new file mode 100644 index 000000000000..33eba60e8256 --- /dev/null +++ b/polly/test/Isl/CodeGen/MemAccess/update_access_functions.ll @@ -0,0 +1,46 @@ +; RUN: opt %loadPolly -polly-import-jscop -polly-import-jscop-dir=%S \ +; RUN: -polly-import-jscop-postfix=transformed -polly-codegen \ +; RUN: < %s -S | FileCheck %s + +; CHECK: polly.stmt.loop2: +; CHECK-NEXT: %polly.access.A = getelementptr double, double* %A, i64 42 +; CHECK-NEXT: %val_p_scalar_ = load double, double* %polly.access.A + +; CHECK: polly.stmt.loop3: +; CHECK-NEXT: %val.s2a.reload = load double, double* %val.s2a +; CHECK-NEXT: %polly.access.A20 = getelementptr double, double* %A, i64 42 +; CHECK-NEXT: store double %val.s2a.reload, double* %polly.access.A20 + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + +define void @update_access_functions(i64 %arg, double* %A) { +bb3: + br label %loop1 + +loop1: + %indvar = phi i64 [ %indvar.next, %loop1 ], [ 1, %bb3 ] + %ptr1 = getelementptr inbounds double, double* %A, i64 %indvar + store double 42.0, double* %ptr1, align 8 + %indvar.next = add nuw nsw i64 %indvar, 1 + %cmp = icmp ne i64 %indvar.next, %arg + br i1 %cmp, label %loop1, label %loop2 + +loop2: + %indvar.2 = phi i64 [ %indvar.2.next, %loop2 ], [ 1, %loop1 ] + %ptr2 = getelementptr inbounds double, double* %A, i64 %indvar.2 + %val = load double, double* %ptr2, align 8 + %indvar.2.next = add nuw nsw i64 %indvar.2, 1 + %cmp.2 = icmp ne i64 %indvar.2.next, %arg + br i1 %cmp.2, label %loop2, label %loop3 + +loop3: + %indvar.3 = phi i64 [ %indvar.3.next, %loop3 ], [ 1, %loop2 ] + %ptr3 = getelementptr inbounds double, double* %A, i64 %indvar.3 + store double %val, double* %ptr3, align 8 + %indvar.3.next = add nuw nsw i64 %indvar.3, 1 + %cmp.3 = icmp ne i64 %indvar.3.next, %arg + br i1 %cmp.3, label %loop3, label %exit + +exit: + ret void +} diff --git a/polly/test/Isl/CodeGen/MemAccess/update_access_functions___%loop1---%exit.jscop b/polly/test/Isl/CodeGen/MemAccess/update_access_functions___%loop1---%exit.jscop new file mode 100644 index 000000000000..733280104a69 --- /dev/null +++ b/polly/test/Isl/CodeGen/MemAccess/update_access_functions___%loop1---%exit.jscop @@ -0,0 +1,47 @@ +{ + "context" : "[arg] -> { : arg >= -9223372036854775808 and arg <= 9223372036854775807 }", + "name" : "loop1 => exit", + "statements" : [ + { + "accesses" : [ + { + "kind" : "write", + "relation" : "[arg] -> { Stmt_loop1[i0] -> MemRef_A[1 + i0] }" + } + ], + "domain" : "[arg] -> { Stmt_loop1[i0] : i0 >= 0 and i0 <= -2 + arg }", + "name" : "Stmt_loop1", + "schedule" : "[arg] -> { Stmt_loop1[i0] -> [0, i0] }" + }, + { + "accesses" : [ + { + "kind" : "read", + "relation" : "[arg] -> { Stmt_loop2[i0] -> MemRef_A[1 + i0] }" + }, + { + "kind" : "write", + "relation" : "[arg] -> { Stmt_loop2[i0] -> MemRef_val[] }" + } + ], + "domain" : "[arg] -> { Stmt_loop2[i0] : i0 >= 0 and i0 <= -2 + arg }", + "name" : "Stmt_loop2", + "schedule" : "[arg] -> { Stmt_loop2[i0] -> [1, i0] }" + }, + { + "accesses" : [ + { + "kind" : "read", + "relation" : "[arg] -> { Stmt_loop3[i0] -> MemRef_val[] }" + }, + { + "kind" : "write", + "relation" : "[arg] -> { Stmt_loop3[i0] -> MemRef_A[1 + i0] }" + } + ], + "domain" : "[arg] -> { Stmt_loop3[i0] : i0 >= 0 and i0 <= -2 + arg }", + "name" : "Stmt_loop3", + "schedule" : "[arg] -> { Stmt_loop3[i0] -> [2, i0] }" + } + ] +} diff --git a/polly/test/Isl/CodeGen/MemAccess/update_access_functions___%loop1---%exit.jscop.transformed b/polly/test/Isl/CodeGen/MemAccess/update_access_functions___%loop1---%exit.jscop.transformed new file mode 100644 index 000000000000..845af31e7e29 --- /dev/null +++ b/polly/test/Isl/CodeGen/MemAccess/update_access_functions___%loop1---%exit.jscop.transformed @@ -0,0 +1,47 @@ +{ + "context" : "[arg] -> { : arg >= -9223372036854775808 and arg <= 9223372036854775807 }", + "name" : "loop1 => exit", + "statements" : [ + { + "accesses" : [ + { + "kind" : "write", + "relation" : "[arg] -> { Stmt_loop1[i0] -> MemRef_A[1 + i0] }" + } + ], + "domain" : "[arg] -> { Stmt_loop1[i0] : i0 >= 0 and i0 <= -2 + arg }", + "name" : "Stmt_loop1", + "schedule" : "[arg] -> { Stmt_loop1[i0] -> [0, i0] }" + }, + { + "accesses" : [ + { + "kind" : "read", + "relation" : "[arg] -> { Stmt_loop2[i0] -> MemRef_A[42] }" + }, + { + "kind" : "write", + "relation" : "[arg] -> { Stmt_loop2[i0] -> MemRef_val[] }" + } + ], + "domain" : "[arg] -> { Stmt_loop2[i0] : i0 >= 0 and i0 <= -2 + arg }", + "name" : "Stmt_loop2", + "schedule" : "[arg] -> { Stmt_loop2[i0] -> [1, i0] }" + }, + { + "accesses" : [ + { + "kind" : "read", + "relation" : "[arg] -> { Stmt_loop3[i0] -> MemRef_val[] }" + }, + { + "kind" : "write", + "relation" : "[arg] -> { Stmt_loop3[i0] -> MemRef_A[42] }" + } + ], + "domain" : "[arg] -> { Stmt_loop3[i0] : i0 >= 0 and i0 <= -2 + arg }", + "name" : "Stmt_loop3", + "schedule" : "[arg] -> { Stmt_loop3[i0] -> [2, i0] }" + } + ] +}