Optimistic assume required invariant loads to be invariant

So far we bailed if a required invariant load was potentially overwritten in
  the SCoP. From now on we will optimistically assume it is actually invariant
  and, to this end, restrict the valid parameter space.

llvm-svn: 270060
This commit is contained in:
Johannes Doerfert 2016-05-19 13:24:10 +00:00
parent 0de7226236
commit cb77542d1c
5 changed files with 107 additions and 12 deletions

View File

@ -2036,6 +2036,9 @@ public:
const InvariantLoadsSetTy &getRequiredInvariantLoads() const {
return DC.RequiredILS;
}
bool isRequiredInvariantLoad(LoadInst *LI) const {
return getRequiredInvariantLoads().count(LI);
}
const BoxedLoopsSetTy &getBoxedLoops() const { return DC.BoxedLoopsSet; }
bool isNonAffineSubRegion(const Region *R) {
return DC.NonAffineSubRegionSet.count(R);

View File

@ -3418,7 +3418,8 @@ bool Scop::isHoistableAccess(MemoryAccess *Access,
// Skip accesses in non-affine subregions as they might not be executed
// under the same condition as the entry of the non-affine subregion.
if (BB != Access->getAccessInstruction()->getParent())
auto *LI = dyn_cast<LoadInst>(Access->getAccessInstruction());
if (!LI || BB != LI->getParent())
return false;
isl_map *AccessRelation = Access->getAccessRelation();
@ -3436,12 +3437,17 @@ bool Scop::isHoistableAccess(MemoryAccess *Access,
isl_union_map *Written = isl_union_map_intersect_range(
isl_union_map_copy(Writes), isl_union_set_from_set(AccessRange));
bool IsWritten = !isl_union_map_is_empty(Written);
isl_union_map_free(Written);
if (IsWritten)
return false;
if (IsWritten && isRequiredInvariantLoad(LI)) {
auto *WrittenCtx = isl_union_map_params(Written);
WrittenCtx = isl_set_remove_divs(WrittenCtx);
addAssumption(INVARIANTLOAD, WrittenCtx, LI->getDebugLoc(), AS_RESTRICTION);
IsWritten = false;
} else {
isl_union_map_free(Written);
}
return true;
return !IsWritten;
}
void Scop::verifyInvariantLoads() {

View File

@ -9,8 +9,7 @@
; Negative test for INNERMOST.
; At the moment we will optimistically assume A[i] in the conditional before the inner
; loop might be invariant and expand the SCoP from the loop to include the conditional. However,
; during SCoP generation we will realize that A[i] is in fact not invariant (in this region = the body
; of the outer loop) and bail.
; during SCoP generation we will realize that A[i] is in not always invariant.
;
; Possible solutions could be:
; - Do not optimistically assume it to be invariant (as before this commit), however we would loose
@ -18,7 +17,51 @@
; - Reduce the size of the SCoP if an assumed invariant access is in fact not invariant instead of
; rejecting the whole region.
;
; INNERMOST-NOT: Function: f
; INNERMOST: Function: f
; INNERMOST-NEXT: Region: %bb4---%bb3
; INNERMOST-NEXT: Max Loop Depth: 1
; INNERMOST-NEXT: Invariant Accesses: {
; INNERMOST-NEXT: ReadAccess := [Reduction Type: NONE] [Scalar: 0]
; INNERMOST-NEXT: [tmp6, N, p_2] -> { Stmt_bb4[] -> MemRef_A[p_2] };
; INNERMOST-NEXT: Execution Context: [tmp6, N, p_2] -> { : }
; INNERMOST-NEXT: }
; INNERMOST-NEXT: Context:
; INNERMOST-NEXT: [tmp6, N, p_2] -> { : -2147483648 <= tmp6 <= 2147483647 and -2147483648 <= N <= 2147483647 and 0 <= p_2 <= 1024 }
; INNERMOST-NEXT: Assumed Context:
; INNERMOST-NEXT: [tmp6, N, p_2] -> { : }
; INNERMOST-NEXT: Invalid Context:
; INNERMOST-NEXT: [tmp6, N, p_2] -> { : p_2 < N and (tmp6 < 0 or tmp6 > 0) }
; INNERMOST-NEXT: p0: %tmp6
; INNERMOST-NEXT: p1: %N
; INNERMOST-NEXT: p2: {0,+,1}<nuw><nsw><%bb3>
; INNERMOST-NEXT: Arrays {
; INNERMOST-NEXT: i32 MemRef_A[*]; // Element size 4
; INNERMOST-NEXT: i64 MemRef_indvars_iv_next2; // Element size 8
; INNERMOST-NEXT: }
; INNERMOST-NEXT: Arrays (Bounds as pw_affs) {
; INNERMOST-NEXT: i32 MemRef_A[*]; // Element size 4
; INNERMOST-NEXT: i64 MemRef_indvars_iv_next2; // Element size 8
; INNERMOST-NEXT: }
; INNERMOST-NEXT: Alias Groups (0):
; INNERMOST-NEXT: n/a
; INNERMOST-NEXT: Statements {
; INNERMOST-NEXT: Stmt_bb11
; INNERMOST-NEXT: Domain :=
; INNERMOST-NEXT: [tmp6, N, p_2] -> { Stmt_bb11[i0] : 0 <= i0 < N and (tmp6 < 0 or tmp6 > 0) };
; INNERMOST-NEXT: Schedule :=
; INNERMOST-NEXT: [tmp6, N, p_2] -> { Stmt_bb11[i0] -> [0, i0] : tmp6 < 0 or tmp6 > 0 };
; INNERMOST-NEXT: ReadAccess := [Reduction Type: +] [Scalar: 0]
; INNERMOST-NEXT: [tmp6, N, p_2] -> { Stmt_bb11[i0] -> MemRef_A[i0] };
; INNERMOST-NEXT: MustWriteAccess := [Reduction Type: +] [Scalar: 0]
; INNERMOST-NEXT: [tmp6, N, p_2] -> { Stmt_bb11[i0] -> MemRef_A[i0] };
; INNERMOST-NEXT: Stmt_bb18
; INNERMOST-NEXT: Domain :=
; INNERMOST-NEXT: [tmp6, N, p_2] -> { Stmt_bb18[] };
; INNERMOST-NEXT: Schedule :=
; INNERMOST-NEXT: [tmp6, N, p_2] -> { Stmt_bb18[] -> [1, 0] };
; INNERMOST-NEXT: MustWriteAccess := [Reduction Type: NONE] [Scalar: 1]
; INNERMOST-NEXT: [tmp6, N, p_2] -> { Stmt_bb18[] -> MemRef_indvars_iv_next2[] };
; INNERMOST-NEXT: }
;
; ALL: Function: f
; ALL-NEXT: Region: %bb3---%bb19

View File

@ -13,8 +13,7 @@
; Negative test for INNERMOST.
; At the moment we will optimistically assume A[i] in the conditional before the inner
; loop might be invariant and expand the SCoP from the loop to include the conditional. However,
; during SCoP generation we will realize that A[i] is in fact not invariant (in this region = the body
; of the outer loop) and bail.
; during SCoP generation we will realize that A[i] is only sometimes invariant.
;
; Possible solutions could be:
; - Do not optimistically assume it to be invariant (as before this commit), however we would loose
@ -22,7 +21,51 @@
; - Reduce the size of the SCoP if an assumed invariant access is in fact not invariant instead of
; rejecting the whole region.
;
; INNERMOST-NOT: Function: f
; INNERMOST: Function: f
; INNERMOST-NEXT: Region: %bb4---%bb3
; INNERMOST-NEXT: Max Loop Depth: 1
; INNERMOST-NEXT: Invariant Accesses: {
; INNERMOST-NEXT: ReadAccess := [Reduction Type: NONE] [Scalar: 0]
; INNERMOST-NEXT: [tmp6, p_1, p_2] -> { Stmt_bb4[] -> MemRef_A[p_2] };
; INNERMOST-NEXT: Execution Context: [tmp6, p_1, p_2] -> { : }
; INNERMOST-NEXT: }
; INNERMOST-NEXT: Context:
; INNERMOST-NEXT: [tmp6, p_1, p_2] -> { : -2147483648 <= tmp6 <= 2147483647 and -2199023255552 <= p_1 <= 2199023254528 and 0 <= p_2 <= 1024 }
; INNERMOST-NEXT: Assumed Context:
; INNERMOST-NEXT: [tmp6, p_1, p_2] -> { : }
; INNERMOST-NEXT: Invalid Context:
; INNERMOST-NEXT: [tmp6, p_1, p_2] -> { : p_2 < p_1 and (tmp6 < 0 or tmp6 > 0) }
; INNERMOST-NEXT: p0: %tmp6
; INNERMOST-NEXT: p1: {0,+,(sext i32 %N to i64)}<%bb3>
; INNERMOST-NEXT: p2: {0,+,1}<nuw><nsw><%bb3>
; INNERMOST-NEXT: Arrays {
; INNERMOST-NEXT: i32 MemRef_A[*]; // Element size 4
; INNERMOST-NEXT: i64 MemRef_indvars_iv_next2; // Element size 8
; INNERMOST-NEXT: }
; INNERMOST-NEXT: Arrays (Bounds as pw_affs) {
; INNERMOST-NEXT: i32 MemRef_A[*]; // Element size 4
; INNERMOST-NEXT: i64 MemRef_indvars_iv_next2; // Element size 8
; INNERMOST-NEXT: }
; INNERMOST-NEXT: Alias Groups (0):
; INNERMOST-NEXT: n/a
; INNERMOST-NEXT: Statements {
; INNERMOST-NEXT: Stmt_bb12
; INNERMOST-NEXT: Domain :=
; INNERMOST-NEXT: [tmp6, p_1, p_2] -> { Stmt_bb12[i0] : 0 <= i0 < p_1 and (tmp6 < 0 or tmp6 > 0) };
; INNERMOST-NEXT: Schedule :=
; INNERMOST-NEXT: [tmp6, p_1, p_2] -> { Stmt_bb12[i0] -> [0, i0] : tmp6 < 0 or tmp6 > 0 };
; INNERMOST-NEXT: ReadAccess := [Reduction Type: +] [Scalar: 0]
; INNERMOST-NEXT: [tmp6, p_1, p_2] -> { Stmt_bb12[i0] -> MemRef_A[i0] };
; INNERMOST-NEXT: MustWriteAccess := [Reduction Type: +] [Scalar: 0]
; INNERMOST-NEXT: [tmp6, p_1, p_2] -> { Stmt_bb12[i0] -> MemRef_A[i0] };
; INNERMOST-NEXT: Stmt_bb19
; INNERMOST-NEXT: Domain :=
; INNERMOST-NEXT: [tmp6, p_1, p_2] -> { Stmt_bb19[] };
; INNERMOST-NEXT: Schedule :=
; INNERMOST-NEXT: [tmp6, p_1, p_2] -> { Stmt_bb19[] -> [1, 0] };
; INNERMOST-NEXT: MustWriteAccess := [Reduction Type: NONE] [Scalar: 1]
; INNERMOST-NEXT: [tmp6, p_1, p_2] -> { Stmt_bb19[] -> MemRef_indvars_iv_next2[] };
; INNERMOST-NEXT: }
;
; ALL: Function: f
; ALL-NEXT: Region: %bb3---%bb20

View File

@ -10,7 +10,7 @@
; CHECK: remark: test/ScopInfo/remarks.c:9:15: Possibly aliasing pointer, use restrict keyword.
; CHECK: remark: test/ScopInfo/remarks.c:14:3: SCoP ends here.
; CHECK: remark: test/ScopInfo/remarks.c:19:3: SCoP begins here.
; CHECK: remark: test/ScopInfo/remarks.c:21:11: Invariant load assumption: [tmp] -> { : 1 = 0 }
; CHECK: remark: test/ScopInfo/remarks.c:21:11: Invariant load restriction: [tmp] -> { : tmp < 0 or tmp > 0 }
; CHECK: remark: test/ScopInfo/remarks.c:22:16: SCoP ends here but was dismissed.
;
; #include <stdio.h>