Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1 | //===--------- ScopInfo.cpp - Create Scops from LLVM IR ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Create a polyhedral description for a static control flow region. |
| 11 | // |
| 12 | // The pass creates a polyhedral description of the Scops detected by the Scop |
| 13 | // detection derived from their LLVM-IR code. |
| 14 | // |
Tobias Grosser | a5605d3 | 2014-10-29 19:58:28 +0000 | [diff] [blame] | 15 | // This representation is shared among several tools in the polyhedral |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 16 | // community, which are e.g. Cloog, Pluto, Loopo, Graphite. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 20 | #include "polly/LinkAllPasses.h" |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 21 | #include "polly/Options.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 22 | #include "polly/ScopInfo.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 23 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 24 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 25 | #include "polly/Support/ScopHelper.h" |
Sebastian Pop | 27c10c6 | 2013-03-22 22:07:43 +0000 | [diff] [blame] | 26 | #include "polly/TempScopInfo.h" |
Tobias Grosser | f4c24b2 | 2015-04-05 13:11:54 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/MapVector.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SetVector.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Statistic.h" |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/STLExtras.h" |
Hongbin Zheng | 86a3774 | 2012-04-25 08:01:38 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringExtras.h" |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/AliasAnalysis.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/LoopInfo.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/RegionIterator.h" |
| 35 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Debug.h" |
Tobias Grosser | 33ba62ad | 2011-08-18 06:31:50 +0000 | [diff] [blame] | 37 | #include "isl/aff.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 38 | #include "isl/constraint.h" |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 39 | #include "isl/local_space.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 40 | #include "isl/map.h" |
Tobias Grosser | 4a8e356 | 2011-12-07 07:42:51 +0000 | [diff] [blame] | 41 | #include "isl/options.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 42 | #include "isl/printer.h" |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 43 | #include "isl/schedule.h" |
| 44 | #include "isl/schedule_node.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 45 | #include "isl/set.h" |
| 46 | #include "isl/union_map.h" |
Tobias Grosser | cd524dc | 2015-05-09 09:36:38 +0000 | [diff] [blame] | 47 | #include "isl/union_set.h" |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 48 | #include "isl/val.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 49 | #include <sstream> |
| 50 | #include <string> |
| 51 | #include <vector> |
| 52 | |
| 53 | using namespace llvm; |
| 54 | using namespace polly; |
| 55 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 56 | #define DEBUG_TYPE "polly-scops" |
| 57 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 58 | STATISTIC(ScopFound, "Number of valid Scops"); |
| 59 | STATISTIC(RichScopFound, "Number of Scops containing a loop"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 60 | |
Johannes Doerfert | 9e7b17b | 2014-08-18 00:40:13 +0000 | [diff] [blame] | 61 | // Multiplicative reductions can be disabled separately as these kind of |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 62 | // operations can overflow easily. Additive reductions and bit operations |
| 63 | // are in contrast pretty stable. |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 64 | static cl::opt<bool> DisableMultiplicativeReductions( |
| 65 | "polly-disable-multiplicative-reductions", |
| 66 | cl::desc("Disable multiplicative reductions"), cl::Hidden, cl::ZeroOrMore, |
| 67 | cl::init(false), cl::cat(PollyCategory)); |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 68 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 69 | static cl::opt<unsigned> RunTimeChecksMaxParameters( |
| 70 | "polly-rtc-max-parameters", |
| 71 | cl::desc("The maximal number of parameters allowed in RTCs."), cl::Hidden, |
| 72 | cl::ZeroOrMore, cl::init(8), cl::cat(PollyCategory)); |
| 73 | |
Tobias Grosser | 7150072 | 2015-03-28 15:11:14 +0000 | [diff] [blame] | 74 | static cl::opt<unsigned> RunTimeChecksMaxArraysPerGroup( |
| 75 | "polly-rtc-max-arrays-per-group", |
| 76 | cl::desc("The maximal number of arrays to compare in each alias group."), |
| 77 | cl::Hidden, cl::ZeroOrMore, cl::init(20), cl::cat(PollyCategory)); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 78 | static cl::opt<std::string> UserContextStr( |
| 79 | "polly-context", cl::value_desc("isl parameter set"), |
| 80 | cl::desc("Provide additional constraints on the context parameters"), |
| 81 | cl::init(""), cl::cat(PollyCategory)); |
Tobias Grosser | 7150072 | 2015-03-28 15:11:14 +0000 | [diff] [blame] | 82 | |
Tobias Grosser | d83b8a8 | 2015-08-20 19:08:11 +0000 | [diff] [blame^] | 83 | static cl::opt<bool> DetectReductions("polly-detect-reductions", |
| 84 | cl::desc("Detect and exploit reductions"), |
| 85 | cl::Hidden, cl::ZeroOrMore, |
| 86 | cl::init(true), cl::cat(PollyCategory)); |
| 87 | |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 88 | // Create a sequence of two schedules. Either argument may be null and is |
| 89 | // interpreted as the empty schedule. Can also return null if both schedules are |
| 90 | // empty. |
| 91 | static __isl_give isl_schedule * |
| 92 | combineInSequence(__isl_take isl_schedule *Prev, |
| 93 | __isl_take isl_schedule *Succ) { |
| 94 | if (!Prev) |
| 95 | return Succ; |
| 96 | if (!Succ) |
| 97 | return Prev; |
| 98 | |
| 99 | return isl_schedule_sequence(Prev, Succ); |
| 100 | } |
| 101 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 102 | static __isl_give isl_set *addRangeBoundsToSet(__isl_take isl_set *S, |
| 103 | const ConstantRange &Range, |
| 104 | int dim, |
| 105 | enum isl_dim_type type) { |
| 106 | isl_val *V; |
| 107 | isl_ctx *ctx = isl_set_get_ctx(S); |
| 108 | |
Johannes Doerfert | 8f8af43 | 2015-04-26 20:07:21 +0000 | [diff] [blame] | 109 | bool useLowerUpperBound = Range.isSignWrappedSet() && !Range.isFullSet(); |
| 110 | const auto LB = useLowerUpperBound ? Range.getLower() : Range.getSignedMin(); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 111 | V = isl_valFromAPInt(ctx, LB, true); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 112 | isl_set *SLB = isl_set_lower_bound_val(isl_set_copy(S), type, dim, V); |
| 113 | |
Johannes Doerfert | 8f8af43 | 2015-04-26 20:07:21 +0000 | [diff] [blame] | 114 | const auto UB = useLowerUpperBound ? Range.getUpper() : Range.getSignedMax(); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 115 | V = isl_valFromAPInt(ctx, UB, true); |
Johannes Doerfert | 8f8af43 | 2015-04-26 20:07:21 +0000 | [diff] [blame] | 116 | if (useLowerUpperBound) |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 117 | V = isl_val_sub_ui(V, 1); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 118 | isl_set *SUB = isl_set_upper_bound_val(S, type, dim, V); |
| 119 | |
Johannes Doerfert | 8f8af43 | 2015-04-26 20:07:21 +0000 | [diff] [blame] | 120 | if (useLowerUpperBound) |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 121 | return isl_set_union(SLB, SUB); |
| 122 | else |
| 123 | return isl_set_intersect(SLB, SUB); |
| 124 | } |
| 125 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 126 | static const ScopArrayInfo *identifyBasePtrOriginSAI(Scop *S, Value *BasePtr) { |
| 127 | LoadInst *BasePtrLI = dyn_cast<LoadInst>(BasePtr); |
| 128 | if (!BasePtrLI) |
| 129 | return nullptr; |
| 130 | |
| 131 | if (!S->getRegion().contains(BasePtrLI)) |
| 132 | return nullptr; |
| 133 | |
| 134 | ScalarEvolution &SE = *S->getSE(); |
| 135 | |
| 136 | auto *OriginBaseSCEV = |
| 137 | SE.getPointerBase(SE.getSCEV(BasePtrLI->getPointerOperand())); |
| 138 | if (!OriginBaseSCEV) |
| 139 | return nullptr; |
| 140 | |
| 141 | auto *OriginBaseSCEVUnknown = dyn_cast<SCEVUnknown>(OriginBaseSCEV); |
| 142 | if (!OriginBaseSCEVUnknown) |
| 143 | return nullptr; |
| 144 | |
| 145 | return S->getScopArrayInfo(OriginBaseSCEVUnknown->getValue()); |
| 146 | } |
| 147 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 148 | ScopArrayInfo::ScopArrayInfo(Value *BasePtr, Type *ElementType, isl_ctx *Ctx, |
Tobias Grosser | 9224522 | 2015-07-28 14:53:44 +0000 | [diff] [blame] | 149 | const SmallVector<const SCEV *, 4> &DimensionSizes, |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 150 | bool IsPHI, Scop *S) |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 151 | : BasePtr(BasePtr), ElementType(ElementType), |
Tobias Grosser | 9224522 | 2015-07-28 14:53:44 +0000 | [diff] [blame] | 152 | DimensionSizes(DimensionSizes), IsPHI(IsPHI) { |
| 153 | std::string BasePtrName = |
| 154 | getIslCompatibleName("MemRef_", BasePtr, IsPHI ? "__phi" : ""); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 155 | Id = isl_id_alloc(Ctx, BasePtrName.c_str(), this); |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 156 | for (const SCEV *Expr : DimensionSizes) { |
| 157 | isl_pw_aff *Size = S->getPwAff(Expr); |
| 158 | DimensionSizesPw.push_back(Size); |
| 159 | } |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 160 | |
| 161 | BasePtrOriginSAI = identifyBasePtrOriginSAI(S, BasePtr); |
| 162 | if (BasePtrOriginSAI) |
| 163 | const_cast<ScopArrayInfo *>(BasePtrOriginSAI)->addDerivedSAI(this); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 166 | ScopArrayInfo::~ScopArrayInfo() { |
| 167 | isl_id_free(Id); |
| 168 | for (isl_pw_aff *Size : DimensionSizesPw) |
| 169 | isl_pw_aff_free(Size); |
| 170 | } |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 171 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 172 | std::string ScopArrayInfo::getName() const { return isl_id_get_name(Id); } |
| 173 | |
| 174 | int ScopArrayInfo::getElemSizeInBytes() const { |
| 175 | return ElementType->getPrimitiveSizeInBits() / 8; |
| 176 | } |
| 177 | |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 178 | isl_id *ScopArrayInfo::getBasePtrId() const { return isl_id_copy(Id); } |
| 179 | |
| 180 | void ScopArrayInfo::dump() const { print(errs()); } |
| 181 | |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 182 | void ScopArrayInfo::print(raw_ostream &OS, bool SizeAsPwAff) const { |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 183 | OS.indent(8) << *getElementType() << " " << getName() << "[*]"; |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 184 | for (unsigned u = 0; u < getNumberOfDimensions(); u++) { |
| 185 | OS << "["; |
| 186 | |
| 187 | if (SizeAsPwAff) |
| 188 | OS << " " << DimensionSizesPw[u] << " "; |
| 189 | else |
| 190 | OS << *DimensionSizes[u]; |
| 191 | |
| 192 | OS << "]"; |
| 193 | } |
| 194 | |
Johannes Doerfert | 4eed5be | 2015-08-20 18:04:22 +0000 | [diff] [blame] | 195 | if (BasePtrOriginSAI) |
| 196 | OS << " [BasePtrOrigin: " << BasePtrOriginSAI->getName() << "]"; |
| 197 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 198 | OS << " // Element size " << getElemSizeInBytes() << "\n"; |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | const ScopArrayInfo * |
| 202 | ScopArrayInfo::getFromAccessFunction(__isl_keep isl_pw_multi_aff *PMA) { |
| 203 | isl_id *Id = isl_pw_multi_aff_get_tuple_id(PMA, isl_dim_out); |
| 204 | assert(Id && "Output dimension didn't have an ID"); |
| 205 | return getFromId(Id); |
| 206 | } |
| 207 | |
| 208 | const ScopArrayInfo *ScopArrayInfo::getFromId(isl_id *Id) { |
| 209 | void *User = isl_id_get_user(Id); |
| 210 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
| 211 | isl_id_free(Id); |
| 212 | return SAI; |
| 213 | } |
| 214 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 215 | const std::string |
| 216 | MemoryAccess::getReductionOperatorStr(MemoryAccess::ReductionType RT) { |
| 217 | switch (RT) { |
| 218 | case MemoryAccess::RT_NONE: |
| 219 | llvm_unreachable("Requested a reduction operator string for a memory " |
| 220 | "access which isn't a reduction"); |
| 221 | case MemoryAccess::RT_ADD: |
| 222 | return "+"; |
| 223 | case MemoryAccess::RT_MUL: |
| 224 | return "*"; |
| 225 | case MemoryAccess::RT_BOR: |
| 226 | return "|"; |
| 227 | case MemoryAccess::RT_BXOR: |
| 228 | return "^"; |
| 229 | case MemoryAccess::RT_BAND: |
| 230 | return "&"; |
| 231 | } |
| 232 | llvm_unreachable("Unknown reduction type"); |
| 233 | return ""; |
| 234 | } |
| 235 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 236 | /// @brief Return the reduction type for a given binary operator |
| 237 | static MemoryAccess::ReductionType getReductionType(const BinaryOperator *BinOp, |
| 238 | const Instruction *Load) { |
| 239 | if (!BinOp) |
| 240 | return MemoryAccess::RT_NONE; |
| 241 | switch (BinOp->getOpcode()) { |
| 242 | case Instruction::FAdd: |
| 243 | if (!BinOp->hasUnsafeAlgebra()) |
| 244 | return MemoryAccess::RT_NONE; |
| 245 | // Fall through |
| 246 | case Instruction::Add: |
| 247 | return MemoryAccess::RT_ADD; |
| 248 | case Instruction::Or: |
| 249 | return MemoryAccess::RT_BOR; |
| 250 | case Instruction::Xor: |
| 251 | return MemoryAccess::RT_BXOR; |
| 252 | case Instruction::And: |
| 253 | return MemoryAccess::RT_BAND; |
| 254 | case Instruction::FMul: |
| 255 | if (!BinOp->hasUnsafeAlgebra()) |
| 256 | return MemoryAccess::RT_NONE; |
| 257 | // Fall through |
| 258 | case Instruction::Mul: |
| 259 | if (DisableMultiplicativeReductions) |
| 260 | return MemoryAccess::RT_NONE; |
| 261 | return MemoryAccess::RT_MUL; |
| 262 | default: |
| 263 | return MemoryAccess::RT_NONE; |
| 264 | } |
| 265 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 266 | //===----------------------------------------------------------------------===// |
| 267 | |
| 268 | MemoryAccess::~MemoryAccess() { |
Tobias Grosser | 6f48e0f | 2015-05-15 09:58:32 +0000 | [diff] [blame] | 269 | isl_id_free(Id); |
Tobias Grosser | 54a86e6 | 2011-08-18 06:31:46 +0000 | [diff] [blame] | 270 | isl_map_free(AccessRelation); |
Raghesh Aloor | 129e867 | 2011-08-15 02:33:39 +0000 | [diff] [blame] | 271 | isl_map_free(newAccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Johannes Doerfert | 8f7124c | 2014-09-12 11:00:49 +0000 | [diff] [blame] | 274 | static MemoryAccess::AccessType getMemoryAccessType(const IRAccess &Access) { |
| 275 | switch (Access.getType()) { |
| 276 | case IRAccess::READ: |
| 277 | return MemoryAccess::READ; |
| 278 | case IRAccess::MUST_WRITE: |
| 279 | return MemoryAccess::MUST_WRITE; |
| 280 | case IRAccess::MAY_WRITE: |
| 281 | return MemoryAccess::MAY_WRITE; |
| 282 | } |
| 283 | llvm_unreachable("Unknown IRAccess type!"); |
| 284 | } |
| 285 | |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 286 | const ScopArrayInfo *MemoryAccess::getScopArrayInfo() const { |
| 287 | isl_id *ArrayId = getArrayId(); |
| 288 | void *User = isl_id_get_user(ArrayId); |
| 289 | const ScopArrayInfo *SAI = static_cast<ScopArrayInfo *>(User); |
| 290 | isl_id_free(ArrayId); |
| 291 | return SAI; |
| 292 | } |
| 293 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 294 | __isl_give isl_id *MemoryAccess::getArrayId() const { |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 295 | return isl_map_get_tuple_id(AccessRelation, isl_dim_out); |
| 296 | } |
| 297 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 298 | __isl_give isl_pw_multi_aff *MemoryAccess::applyScheduleToAccessRelation( |
| 299 | __isl_take isl_union_map *USchedule) const { |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 300 | isl_map *Schedule, *ScheduledAccRel; |
| 301 | isl_union_set *UDomain; |
| 302 | |
| 303 | UDomain = isl_union_set_from_set(getStatement()->getDomain()); |
| 304 | USchedule = isl_union_map_intersect_domain(USchedule, UDomain); |
| 305 | Schedule = isl_map_from_union_map(USchedule); |
| 306 | ScheduledAccRel = isl_map_apply_domain(getAccessRelation(), Schedule); |
| 307 | return isl_pw_multi_aff_from_map(ScheduledAccRel); |
| 308 | } |
| 309 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 310 | __isl_give isl_map *MemoryAccess::getOriginalAccessRelation() const { |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 311 | return isl_map_copy(AccessRelation); |
| 312 | } |
| 313 | |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 314 | std::string MemoryAccess::getOriginalAccessRelationStr() const { |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 315 | return stringFromIslObj(AccessRelation); |
| 316 | } |
| 317 | |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 318 | __isl_give isl_space *MemoryAccess::getOriginalAccessRelationSpace() const { |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 319 | return isl_map_get_space(AccessRelation); |
| 320 | } |
| 321 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 322 | __isl_give isl_map *MemoryAccess::getNewAccessRelation() const { |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 323 | return isl_map_copy(newAccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 326 | __isl_give isl_basic_map * |
| 327 | MemoryAccess::createBasicAccessMap(ScopStmt *Statement) { |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 328 | isl_space *Space = isl_space_set_alloc(Statement->getIslCtx(), 0, 1); |
Tobias Grosser | ed29566 | 2012-09-11 13:50:21 +0000 | [diff] [blame] | 329 | Space = isl_space_align_params(Space, Statement->getDomainSpace()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 330 | |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 331 | return isl_basic_map_from_domain_and_range( |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 332 | isl_basic_set_universe(Statement->getDomainSpace()), |
| 333 | isl_basic_set_universe(Space)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 336 | // Formalize no out-of-bound access assumption |
| 337 | // |
| 338 | // When delinearizing array accesses we optimistically assume that the |
| 339 | // delinearized accesses do not access out of bound locations (the subscript |
| 340 | // expression of each array evaluates for each statement instance that is |
| 341 | // executed to a value that is larger than zero and strictly smaller than the |
| 342 | // size of the corresponding dimension). The only exception is the outermost |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 343 | // dimension for which we do not need to assume any upper bound. At this point |
| 344 | // we formalize this assumption to ensure that at code generation time the |
| 345 | // relevant run-time checks can be generated. |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 346 | // |
| 347 | // To find the set of constraints necessary to avoid out of bound accesses, we |
| 348 | // first build the set of data locations that are not within array bounds. We |
| 349 | // then apply the reverse access relation to obtain the set of iterations that |
| 350 | // may contain invalid accesses and reduce this set of iterations to the ones |
| 351 | // that are actually executed by intersecting them with the domain of the |
| 352 | // statement. If we now project out all loop dimensions, we obtain a set of |
| 353 | // parameters that may cause statement instances to be executed that may |
| 354 | // possibly yield out of bound memory accesses. The complement of these |
| 355 | // constraints is the set of constraints that needs to be assumed to ensure such |
| 356 | // statement instances are never executed. |
| 357 | void MemoryAccess::assumeNoOutOfBound(const IRAccess &Access) { |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 358 | isl_space *Space = isl_space_range(getOriginalAccessRelationSpace()); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 359 | isl_set *Outside = isl_set_empty(isl_space_copy(Space)); |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 360 | for (int i = 1, Size = Access.Subscripts.size(); i < Size; ++i) { |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 361 | isl_local_space *LS = isl_local_space_from_space(isl_space_copy(Space)); |
| 362 | isl_pw_aff *Var = |
| 363 | isl_pw_aff_var_on_domain(isl_local_space_copy(LS), isl_dim_set, i); |
| 364 | isl_pw_aff *Zero = isl_pw_aff_zero_on_domain(LS); |
| 365 | |
| 366 | isl_set *DimOutside; |
| 367 | |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 368 | DimOutside = isl_pw_aff_lt_set(isl_pw_aff_copy(Var), Zero); |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 369 | isl_pw_aff *SizeE = Statement->getPwAff(Access.Sizes[i - 1]); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 370 | |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 371 | SizeE = isl_pw_aff_drop_dims(SizeE, isl_dim_in, 0, |
| 372 | Statement->getNumIterators()); |
| 373 | SizeE = isl_pw_aff_add_dims(SizeE, isl_dim_in, |
| 374 | isl_space_dim(Space, isl_dim_set)); |
| 375 | SizeE = isl_pw_aff_set_tuple_id(SizeE, isl_dim_in, |
| 376 | isl_space_get_tuple_id(Space, isl_dim_set)); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 377 | |
Tobias Grosser | f57d63f | 2014-08-03 21:07:30 +0000 | [diff] [blame] | 378 | DimOutside = isl_set_union(DimOutside, isl_pw_aff_le_set(SizeE, Var)); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 379 | |
| 380 | Outside = isl_set_union(Outside, DimOutside); |
| 381 | } |
| 382 | |
| 383 | Outside = isl_set_apply(Outside, isl_map_reverse(getAccessRelation())); |
| 384 | Outside = isl_set_intersect(Outside, Statement->getDomain()); |
| 385 | Outside = isl_set_params(Outside); |
Tobias Grosser | f54bb77 | 2015-06-26 12:09:28 +0000 | [diff] [blame] | 386 | |
| 387 | // Remove divs to avoid the construction of overly complicated assumptions. |
| 388 | // Doing so increases the set of parameter combinations that are assumed to |
| 389 | // not appear. This is always save, but may make the resulting run-time check |
| 390 | // bail out more often than strictly necessary. |
| 391 | Outside = isl_set_remove_divs(Outside); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 392 | Outside = isl_set_complement(Outside); |
| 393 | Statement->getParent()->addAssumption(Outside); |
| 394 | isl_space_free(Space); |
| 395 | } |
| 396 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 397 | void MemoryAccess::computeBoundsOnAccessRelation(unsigned ElementSize) { |
| 398 | ScalarEvolution *SE = Statement->getParent()->getSE(); |
| 399 | |
| 400 | Value *Ptr = getPointerOperand(*getAccessInstruction()); |
| 401 | if (!Ptr || !SE->isSCEVable(Ptr->getType())) |
| 402 | return; |
| 403 | |
| 404 | auto *PtrSCEV = SE->getSCEV(Ptr); |
| 405 | if (isa<SCEVCouldNotCompute>(PtrSCEV)) |
| 406 | return; |
| 407 | |
| 408 | auto *BasePtrSCEV = SE->getPointerBase(PtrSCEV); |
| 409 | if (BasePtrSCEV && !isa<SCEVCouldNotCompute>(BasePtrSCEV)) |
| 410 | PtrSCEV = SE->getMinusSCEV(PtrSCEV, BasePtrSCEV); |
| 411 | |
| 412 | const ConstantRange &Range = SE->getSignedRange(PtrSCEV); |
| 413 | if (Range.isFullSet()) |
| 414 | return; |
| 415 | |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 416 | bool isWrapping = Range.isSignWrappedSet(); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 417 | unsigned BW = Range.getBitWidth(); |
Johannes Doerfert | e4bd53b | 2015-03-08 19:49:50 +0000 | [diff] [blame] | 418 | const auto LB = isWrapping ? Range.getLower() : Range.getSignedMin(); |
| 419 | const auto UB = isWrapping ? Range.getUpper() : Range.getSignedMax(); |
| 420 | |
| 421 | auto Min = LB.sdiv(APInt(BW, ElementSize)); |
| 422 | auto Max = (UB - APInt(BW, 1)).sdiv(APInt(BW, ElementSize)); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 423 | |
| 424 | isl_set *AccessRange = isl_map_range(isl_map_copy(AccessRelation)); |
| 425 | AccessRange = |
| 426 | addRangeBoundsToSet(AccessRange, ConstantRange(Min, Max), 0, isl_dim_set); |
| 427 | AccessRelation = isl_map_intersect_range(AccessRelation, AccessRange); |
| 428 | } |
| 429 | |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 430 | __isl_give isl_map *MemoryAccess::foldAccess(const IRAccess &Access, |
| 431 | __isl_take isl_map *AccessRelation, |
| 432 | ScopStmt *Statement) { |
| 433 | int Size = Access.Subscripts.size(); |
| 434 | |
| 435 | for (int i = Size - 2; i >= 0; --i) { |
| 436 | isl_space *Space; |
| 437 | isl_map *MapOne, *MapTwo; |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 438 | isl_pw_aff *DimSize = Statement->getPwAff(Access.Sizes[i]); |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 439 | |
| 440 | isl_space *SpaceSize = isl_pw_aff_get_space(DimSize); |
| 441 | isl_pw_aff_free(DimSize); |
| 442 | isl_id *ParamId = isl_space_get_dim_id(SpaceSize, isl_dim_param, 0); |
| 443 | |
| 444 | Space = isl_map_get_space(AccessRelation); |
| 445 | Space = isl_space_map_from_set(isl_space_range(Space)); |
| 446 | Space = isl_space_align_params(Space, SpaceSize); |
| 447 | |
| 448 | int ParamLocation = isl_space_find_dim_by_id(Space, isl_dim_param, ParamId); |
| 449 | isl_id_free(ParamId); |
| 450 | |
| 451 | MapOne = isl_map_universe(isl_space_copy(Space)); |
| 452 | for (int j = 0; j < Size; ++j) |
| 453 | MapOne = isl_map_equate(MapOne, isl_dim_in, j, isl_dim_out, j); |
| 454 | MapOne = isl_map_lower_bound_si(MapOne, isl_dim_in, i + 1, 0); |
| 455 | |
| 456 | MapTwo = isl_map_universe(isl_space_copy(Space)); |
| 457 | for (int j = 0; j < Size; ++j) |
| 458 | if (j < i || j > i + 1) |
| 459 | MapTwo = isl_map_equate(MapTwo, isl_dim_in, j, isl_dim_out, j); |
| 460 | |
| 461 | isl_local_space *LS = isl_local_space_from_space(Space); |
| 462 | isl_constraint *C; |
| 463 | C = isl_equality_alloc(isl_local_space_copy(LS)); |
| 464 | C = isl_constraint_set_constant_si(C, -1); |
| 465 | C = isl_constraint_set_coefficient_si(C, isl_dim_in, i, 1); |
| 466 | C = isl_constraint_set_coefficient_si(C, isl_dim_out, i, -1); |
| 467 | MapTwo = isl_map_add_constraint(MapTwo, C); |
| 468 | C = isl_equality_alloc(LS); |
| 469 | C = isl_constraint_set_coefficient_si(C, isl_dim_in, i + 1, 1); |
| 470 | C = isl_constraint_set_coefficient_si(C, isl_dim_out, i + 1, -1); |
| 471 | C = isl_constraint_set_coefficient_si(C, isl_dim_param, ParamLocation, 1); |
| 472 | MapTwo = isl_map_add_constraint(MapTwo, C); |
| 473 | MapTwo = isl_map_upper_bound_si(MapTwo, isl_dim_in, i + 1, -1); |
| 474 | |
| 475 | MapOne = isl_map_union(MapOne, MapTwo); |
| 476 | AccessRelation = isl_map_apply_range(AccessRelation, MapOne); |
| 477 | } |
| 478 | return AccessRelation; |
| 479 | } |
| 480 | |
Johannes Doerfert | 13c8cf2 | 2014-08-10 08:09:38 +0000 | [diff] [blame] | 481 | MemoryAccess::MemoryAccess(const IRAccess &Access, Instruction *AccInst, |
Tobias Grosser | 6f48e0f | 2015-05-15 09:58:32 +0000 | [diff] [blame] | 482 | ScopStmt *Statement, const ScopArrayInfo *SAI, |
| 483 | int Identifier) |
Johannes Doerfert | d86f215 | 2015-08-17 10:58:17 +0000 | [diff] [blame] | 484 | : AccType(getMemoryAccessType(Access)), Statement(Statement), |
| 485 | AccessInstruction(AccInst), AccessValue(Access.getAccessValue()), |
Johannes Doerfert | 8f7124c | 2014-09-12 11:00:49 +0000 | [diff] [blame] | 486 | newAccessRelation(nullptr) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 487 | |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 488 | isl_ctx *Ctx = Statement->getIslCtx(); |
Tobias Grosser | 9759f85 | 2011-11-10 12:44:55 +0000 | [diff] [blame] | 489 | BaseAddr = Access.getBase(); |
Johannes Doerfert | 79fc23f | 2014-07-24 23:48:02 +0000 | [diff] [blame] | 490 | BaseName = getIslCompatibleName("MemRef_", getBaseAddr(), ""); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 491 | |
| 492 | isl_id *BaseAddrId = SAI->getBasePtrId(); |
Tobias Grosser | 5683df4 | 2011-11-09 22:34:34 +0000 | [diff] [blame] | 493 | |
Tobias Grosser | ac3a95f | 2015-08-03 17:53:21 +0000 | [diff] [blame] | 494 | auto IdName = "__polly_array_ref_" + std::to_string(Identifier); |
Tobias Grosser | e29d31c | 2015-05-15 12:24:09 +0000 | [diff] [blame] | 495 | Id = isl_id_alloc(Ctx, IdName.c_str(), nullptr); |
Tobias Grosser | 6f48e0f | 2015-05-15 09:58:32 +0000 | [diff] [blame] | 496 | |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 497 | if (!Access.isAffine()) { |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 498 | // We overapproximate non-affine accesses with a possible access to the |
| 499 | // whole array. For read accesses it does not make a difference, if an |
| 500 | // access must or may happen. However, for write accesses it is important to |
| 501 | // differentiate between writes that must happen and writes that may happen. |
Tobias Grosser | 04d6ae6 | 2013-06-23 06:04:54 +0000 | [diff] [blame] | 502 | AccessRelation = isl_map_from_basic_map(createBasicAccessMap(Statement)); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 503 | AccessRelation = |
| 504 | isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId); |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 505 | |
| 506 | computeBoundsOnAccessRelation(Access.getElemSizeInBytes()); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 507 | return; |
| 508 | } |
| 509 | |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 510 | isl_space *Space = isl_space_alloc(Ctx, 0, Statement->getNumIterators(), 0); |
Tobias Grosser | 79baa21 | 2014-04-10 08:38:02 +0000 | [diff] [blame] | 511 | AccessRelation = isl_map_universe(Space); |
Tobias Grosser | a187964 | 2011-12-20 10:43:14 +0000 | [diff] [blame] | 512 | |
Tobias Grosser | 79baa21 | 2014-04-10 08:38:02 +0000 | [diff] [blame] | 513 | for (int i = 0, Size = Access.Subscripts.size(); i < Size; ++i) { |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 514 | isl_pw_aff *Affine = Statement->getPwAff(Access.Subscripts[i]); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 515 | |
Sebastian Pop | 422e33f | 2014-06-03 18:16:31 +0000 | [diff] [blame] | 516 | if (Size == 1) { |
| 517 | // For the non delinearized arrays, divide the access function of the last |
| 518 | // subscript by the size of the elements in the array. |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 519 | // |
| 520 | // A stride one array access in C expressed as A[i] is expressed in |
| 521 | // LLVM-IR as something like A[i * elementsize]. This hides the fact that |
| 522 | // two subsequent values of 'i' index two values that are stored next to |
| 523 | // each other in memory. By this division we make this characteristic |
| 524 | // obvious again. |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 525 | isl_val *v = isl_val_int_from_si(Ctx, Access.getElemSizeInBytes()); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 526 | Affine = isl_pw_aff_scale_down_val(Affine, v); |
| 527 | } |
| 528 | |
| 529 | isl_map *SubscriptMap = isl_map_from_pw_aff(Affine); |
| 530 | |
Tobias Grosser | 79baa21 | 2014-04-10 08:38:02 +0000 | [diff] [blame] | 531 | AccessRelation = isl_map_flat_range_product(AccessRelation, SubscriptMap); |
Sebastian Pop | 1801668 | 2014-04-08 21:20:44 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Tobias Grosser | 619190d | 2015-03-30 17:22:28 +0000 | [diff] [blame] | 534 | AccessRelation = foldAccess(Access, AccessRelation, Statement); |
| 535 | |
Tobias Grosser | 79baa21 | 2014-04-10 08:38:02 +0000 | [diff] [blame] | 536 | Space = Statement->getDomainSpace(); |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 537 | AccessRelation = isl_map_set_tuple_id( |
| 538 | AccessRelation, isl_dim_in, isl_space_get_tuple_id(Space, isl_dim_set)); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 539 | AccessRelation = |
| 540 | isl_map_set_tuple_id(AccessRelation, isl_dim_out, BaseAddrId); |
| 541 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 542 | assumeNoOutOfBound(Access); |
Tobias Grosser | aa660a9 | 2015-03-30 00:07:50 +0000 | [diff] [blame] | 543 | AccessRelation = isl_map_gist_domain(AccessRelation, Statement->getDomain()); |
Johannes Doerfert | 5d83f09 | 2014-07-29 08:37:55 +0000 | [diff] [blame] | 544 | isl_space_free(Space); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 545 | } |
Tobias Grosser | 30b8a09 | 2011-08-18 07:51:37 +0000 | [diff] [blame] | 546 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 547 | void MemoryAccess::realignParams() { |
Tobias Grosser | 6defb5b | 2014-04-10 08:37:44 +0000 | [diff] [blame] | 548 | isl_space *ParamSpace = Statement->getParent()->getParamSpace(); |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 549 | AccessRelation = isl_map_align_params(AccessRelation, ParamSpace); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 552 | const std::string MemoryAccess::getReductionOperatorStr() const { |
| 553 | return MemoryAccess::getReductionOperatorStr(getReductionType()); |
| 554 | } |
| 555 | |
Tobias Grosser | 6f48e0f | 2015-05-15 09:58:32 +0000 | [diff] [blame] | 556 | __isl_give isl_id *MemoryAccess::getId() const { return isl_id_copy(Id); } |
| 557 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 558 | raw_ostream &polly::operator<<(raw_ostream &OS, |
| 559 | MemoryAccess::ReductionType RT) { |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 560 | if (RT == MemoryAccess::RT_NONE) |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 561 | OS << "NONE"; |
Johannes Doerfert | 32868bf | 2014-08-01 08:13:25 +0000 | [diff] [blame] | 562 | else |
| 563 | OS << MemoryAccess::getReductionOperatorStr(RT); |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 564 | return OS; |
| 565 | } |
| 566 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 567 | void MemoryAccess::print(raw_ostream &OS) const { |
Johannes Doerfert | 4c7ce47 | 2014-10-08 10:11:33 +0000 | [diff] [blame] | 568 | switch (AccType) { |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 569 | case READ: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 570 | OS.indent(12) << "ReadAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 571 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 572 | case MUST_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 573 | OS.indent(12) << "MustWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 574 | break; |
Tobias Grosser | b58f6a4 | 2013-07-13 20:41:24 +0000 | [diff] [blame] | 575 | case MAY_WRITE: |
Johannes Doerfert | 6780bc3 | 2014-06-26 18:47:03 +0000 | [diff] [blame] | 576 | OS.indent(12) << "MayWriteAccess :=\t"; |
Tobias Grosser | 4f96749 | 2013-06-23 05:21:18 +0000 | [diff] [blame] | 577 | break; |
| 578 | } |
Johannes Doerfert | 0ff23ec | 2015-02-06 20:13:15 +0000 | [diff] [blame] | 579 | OS << "[Reduction Type: " << getReductionType() << "] "; |
| 580 | OS << "[Scalar: " << isScalar() << "]\n"; |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 581 | OS.indent(16) << getOriginalAccessRelationStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 584 | void MemoryAccess::dump() const { print(errs()); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 585 | |
| 586 | // Create a map in the size of the provided set domain, that maps from the |
| 587 | // one element of the provided set domain to another element of the provided |
| 588 | // set domain. |
| 589 | // The mapping is limited to all points that are equal in all but the last |
| 590 | // dimension and for which the last dimension of the input is strict smaller |
| 591 | // than the last dimension of the output. |
| 592 | // |
| 593 | // getEqualAndLarger(set[i0, i1, ..., iX]): |
| 594 | // |
| 595 | // set[i0, i1, ..., iX] -> set[o0, o1, ..., oX] |
| 596 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1), iX < oX |
| 597 | // |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 598 | static isl_map *getEqualAndLarger(isl_space *setDomain) { |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 599 | isl_space *Space = isl_space_map_from_set(setDomain); |
Tobias Grosser | 1b6ea57 | 2015-05-21 19:02:44 +0000 | [diff] [blame] | 600 | isl_map *Map = isl_map_universe(Space); |
Sebastian Pop | 4040876 | 2013-10-04 17:14:53 +0000 | [diff] [blame] | 601 | unsigned lastDimension = isl_map_dim(Map, isl_dim_in) - 1; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 602 | |
| 603 | // Set all but the last dimension to be equal for the input and output |
| 604 | // |
| 605 | // input[i0, i1, ..., iX] -> output[o0, o1, ..., oX] |
| 606 | // : i0 = o0, i1 = o1, ..., i(X-1) = o(X-1) |
Sebastian Pop | 4040876 | 2013-10-04 17:14:53 +0000 | [diff] [blame] | 607 | for (unsigned i = 0; i < lastDimension; ++i) |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 608 | Map = isl_map_equate(Map, isl_dim_in, i, isl_dim_out, i); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 609 | |
| 610 | // Set the last dimension of the input to be strict smaller than the |
| 611 | // last dimension of the output. |
| 612 | // |
| 613 | // input[?,?,?,...,iX] -> output[?,?,?,...,oX] : iX < oX |
Tobias Grosser | 1b6ea57 | 2015-05-21 19:02:44 +0000 | [diff] [blame] | 614 | Map = isl_map_order_lt(Map, isl_dim_in, lastDimension, isl_dim_out, |
| 615 | lastDimension); |
Tobias Grosser | c327932c | 2012-02-01 14:23:36 +0000 | [diff] [blame] | 616 | return Map; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 619 | __isl_give isl_set * |
| 620 | MemoryAccess::getStride(__isl_take const isl_map *Schedule) const { |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 621 | isl_map *S = const_cast<isl_map *>(Schedule); |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 622 | isl_map *AccessRelation = getAccessRelation(); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 623 | isl_space *Space = isl_space_range(isl_map_get_space(S)); |
| 624 | isl_map *NextScatt = getEqualAndLarger(Space); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 625 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 626 | S = isl_map_reverse(S); |
| 627 | NextScatt = isl_map_lexmin(NextScatt); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 628 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 629 | NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(S)); |
| 630 | NextScatt = isl_map_apply_range(NextScatt, isl_map_copy(AccessRelation)); |
| 631 | NextScatt = isl_map_apply_domain(NextScatt, S); |
| 632 | NextScatt = isl_map_apply_domain(NextScatt, AccessRelation); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 633 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 634 | isl_set *Deltas = isl_map_deltas(NextScatt); |
| 635 | return Deltas; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 638 | bool MemoryAccess::isStrideX(__isl_take const isl_map *Schedule, |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 639 | int StrideWidth) const { |
| 640 | isl_set *Stride, *StrideX; |
| 641 | bool IsStrideX; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 642 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 643 | Stride = getStride(Schedule); |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 644 | StrideX = isl_set_universe(isl_set_get_space(Stride)); |
| 645 | StrideX = isl_set_fix_si(StrideX, isl_dim_set, 0, StrideWidth); |
Roman Gareev | f2bd72e | 2015-08-18 16:12:05 +0000 | [diff] [blame] | 646 | IsStrideX = isl_set_is_subset(Stride, StrideX); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 647 | |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 648 | isl_set_free(StrideX); |
Tobias Grosser | dea9823 | 2012-01-17 20:34:27 +0000 | [diff] [blame] | 649 | isl_set_free(Stride); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 650 | |
Tobias Grosser | 28dd486 | 2012-01-24 16:42:16 +0000 | [diff] [blame] | 651 | return IsStrideX; |
| 652 | } |
| 653 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 654 | bool MemoryAccess::isStrideZero(const isl_map *Schedule) const { |
| 655 | return isStrideX(Schedule, 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Tobias Grosser | 79baa21 | 2014-04-10 08:38:02 +0000 | [diff] [blame] | 658 | bool MemoryAccess::isScalar() const { |
| 659 | return isl_map_n_out(AccessRelation) == 0; |
| 660 | } |
| 661 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 662 | bool MemoryAccess::isStrideOne(const isl_map *Schedule) const { |
| 663 | return isStrideX(Schedule, 1); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Tobias Grosser | 5d45381 | 2011-10-06 00:04:11 +0000 | [diff] [blame] | 666 | void MemoryAccess::setNewAccessRelation(isl_map *newAccess) { |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 667 | isl_map_free(newAccessRelation); |
Raghesh Aloor | 7a04f4f | 2011-08-03 13:47:59 +0000 | [diff] [blame] | 668 | newAccessRelation = newAccess; |
Raghesh Aloor | 3cb6628 | 2011-07-12 17:14:03 +0000 | [diff] [blame] | 669 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 670 | |
| 671 | //===----------------------------------------------------------------------===// |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 672 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 673 | isl_map *ScopStmt::getSchedule() const { |
| 674 | isl_set *Domain = getDomain(); |
| 675 | if (isl_set_is_empty(Domain)) { |
| 676 | isl_set_free(Domain); |
| 677 | return isl_map_from_aff( |
| 678 | isl_aff_zero_on_domain(isl_local_space_from_space(getDomainSpace()))); |
| 679 | } |
| 680 | auto *Schedule = getParent()->getSchedule(); |
| 681 | Schedule = isl_union_map_intersect_domain( |
| 682 | Schedule, isl_union_set_from_set(isl_set_copy(Domain))); |
| 683 | if (isl_union_map_is_empty(Schedule)) { |
| 684 | isl_set_free(Domain); |
| 685 | isl_union_map_free(Schedule); |
| 686 | return isl_map_from_aff( |
| 687 | isl_aff_zero_on_domain(isl_local_space_from_space(getDomainSpace()))); |
| 688 | } |
| 689 | auto *M = isl_map_from_union_map(Schedule); |
| 690 | M = isl_map_coalesce(M); |
| 691 | M = isl_map_gist_domain(M, Domain); |
| 692 | M = isl_map_coalesce(M); |
| 693 | return M; |
| 694 | } |
Tobias Grosser | cf3942d | 2011-10-06 00:04:05 +0000 | [diff] [blame] | 695 | |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 696 | __isl_give isl_pw_aff *ScopStmt::getPwAff(const SCEV *E) { |
| 697 | return getParent()->getPwAff(E, this); |
| 698 | } |
| 699 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 700 | void ScopStmt::restrictDomain(__isl_take isl_set *NewDomain) { |
| 701 | assert(isl_set_is_subset(NewDomain, Domain) && |
| 702 | "New domain is not a subset of old domain!"); |
| 703 | isl_set_free(Domain); |
| 704 | Domain = NewDomain; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 707 | void ScopStmt::buildAccesses(TempScop &tempScop, BasicBlock *Block, |
| 708 | bool isApproximated) { |
| 709 | AccFuncSetType *AFS = tempScop.getAccessFunctions(Block); |
| 710 | if (!AFS) |
| 711 | return; |
| 712 | |
| 713 | for (auto &AccessPair : *AFS) { |
| 714 | IRAccess &Access = AccessPair.first; |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 715 | Instruction *AccessInst = AccessPair.second; |
Johannes Doerfert | d86f215 | 2015-08-17 10:58:17 +0000 | [diff] [blame] | 716 | Type *ElementType = Access.getAccessValue()->getType(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 717 | |
Johannes Doerfert | 80ef110 | 2014-11-07 08:31:31 +0000 | [diff] [blame] | 718 | const ScopArrayInfo *SAI = getParent()->getOrCreateScopArrayInfo( |
Tobias Grosser | 9224522 | 2015-07-28 14:53:44 +0000 | [diff] [blame] | 719 | Access.getBase(), ElementType, Access.Sizes, Access.isPHI()); |
Johannes Doerfert | 80ef110 | 2014-11-07 08:31:31 +0000 | [diff] [blame] | 720 | |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 721 | if (isApproximated && Access.isWrite()) |
| 722 | Access.setMayWrite(); |
| 723 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 724 | MemoryAccessList *&MAL = InstructionToAccess[AccessInst]; |
| 725 | if (!MAL) |
| 726 | MAL = new MemoryAccessList(); |
| 727 | MAL->emplace_front(Access, AccessInst, this, SAI, MemAccs.size()); |
| 728 | MemAccs.push_back(&MAL->front()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 732 | void ScopStmt::realignParams() { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 733 | for (MemoryAccess *MA : *this) |
| 734 | MA->realignParams(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 735 | |
| 736 | Domain = isl_set_align_params(Domain, Parent.getParamSpace()); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Tobias Grosser | 65b0058 | 2011-11-08 15:41:19 +0000 | [diff] [blame] | 739 | __isl_give isl_set *ScopStmt::buildConditionSet(const Comparison &Comp) { |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 740 | isl_pw_aff *L = getPwAff(Comp.getLHS()); |
| 741 | isl_pw_aff *R = getPwAff(Comp.getRHS()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 742 | |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 743 | switch (Comp.getPred()) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 744 | case ICmpInst::ICMP_EQ: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 745 | return isl_pw_aff_eq_set(L, R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 746 | case ICmpInst::ICMP_NE: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 747 | return isl_pw_aff_ne_set(L, R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 748 | case ICmpInst::ICMP_SLT: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 749 | return isl_pw_aff_lt_set(L, R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 750 | case ICmpInst::ICMP_SLE: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 751 | return isl_pw_aff_le_set(L, R); |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 752 | case ICmpInst::ICMP_SGT: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 753 | return isl_pw_aff_gt_set(L, R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 754 | case ICmpInst::ICMP_SGE: |
Tobias Grosser | 048c879 | 2011-10-23 20:59:20 +0000 | [diff] [blame] | 755 | return isl_pw_aff_ge_set(L, R); |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 756 | case ICmpInst::ICMP_ULT: |
Tobias Grosser | bfbc369 | 2015-01-09 00:01:33 +0000 | [diff] [blame] | 757 | return isl_pw_aff_lt_set(L, R); |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 758 | case ICmpInst::ICMP_UGT: |
Tobias Grosser | bfbc369 | 2015-01-09 00:01:33 +0000 | [diff] [blame] | 759 | return isl_pw_aff_gt_set(L, R); |
Tobias Grosser | d2795d0 | 2011-08-18 07:51:40 +0000 | [diff] [blame] | 760 | case ICmpInst::ICMP_ULE: |
Tobias Grosser | bfbc369 | 2015-01-09 00:01:33 +0000 | [diff] [blame] | 761 | return isl_pw_aff_le_set(L, R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 762 | case ICmpInst::ICMP_UGE: |
Tobias Grosser | bfbc369 | 2015-01-09 00:01:33 +0000 | [diff] [blame] | 763 | return isl_pw_aff_ge_set(L, R); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 764 | default: |
| 765 | llvm_unreachable("Non integer predicate not supported"); |
| 766 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Johannes Doerfert | 45545ff | 2015-08-16 14:36:01 +0000 | [diff] [blame] | 769 | void ScopStmt::addLoopBoundsToDomain(TempScop &tempScop) { |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 770 | isl_space *Space; |
| 771 | isl_local_space *LocalSpace; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 772 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 773 | Space = isl_set_get_space(Domain); |
| 774 | LocalSpace = isl_local_space_from_space(Space); |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 775 | |
Johannes Doerfert | 5ad8a6a | 2014-11-01 01:14:56 +0000 | [diff] [blame] | 776 | ScalarEvolution *SE = getParent()->getSE(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 777 | for (int i = 0, e = getNumIterators(); i != e; ++i) { |
Tobias Grosser | 9b13d3d | 2011-10-06 22:32:58 +0000 | [diff] [blame] | 778 | isl_aff *Zero = isl_aff_zero_on_domain(isl_local_space_copy(LocalSpace)); |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 779 | isl_pw_aff *IV = |
| 780 | isl_pw_aff_from_aff(isl_aff_set_coefficient_si(Zero, isl_dim_in, i, 1)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 781 | |
Tobias Grosser | 9b13d3d | 2011-10-06 22:32:58 +0000 | [diff] [blame] | 782 | // 0 <= IV. |
| 783 | isl_set *LowerBound = isl_pw_aff_nonneg_set(isl_pw_aff_copy(IV)); |
| 784 | Domain = isl_set_intersect(Domain, LowerBound); |
| 785 | |
| 786 | // IV <= LatchExecutions. |
Hongbin Zheng | 27f3afb | 2011-04-30 03:26:51 +0000 | [diff] [blame] | 787 | const Loop *L = getLoopForDimension(i); |
Johannes Doerfert | 5ad8a6a | 2014-11-01 01:14:56 +0000 | [diff] [blame] | 788 | const SCEV *LatchExecutions = SE->getBackedgeTakenCount(L); |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 789 | isl_pw_aff *UpperBound = getPwAff(LatchExecutions); |
Tobias Grosser | 9b13d3d | 2011-10-06 22:32:58 +0000 | [diff] [blame] | 790 | isl_set *UpperBoundSet = isl_pw_aff_le_set(IV, UpperBound); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 791 | Domain = isl_set_intersect(Domain, UpperBoundSet); |
| 792 | } |
| 793 | |
Tobias Grosser | f533880 | 2011-10-06 00:03:35 +0000 | [diff] [blame] | 794 | isl_local_space_free(LocalSpace); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Johannes Doerfert | 45545ff | 2015-08-16 14:36:01 +0000 | [diff] [blame] | 797 | void ScopStmt::addConditionsToDomain(TempScop &tempScop, |
| 798 | const Region &CurRegion) { |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 799 | const Region *TopRegion = tempScop.getMaxRegion().getParent(), |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 800 | *CurrentRegion = &CurRegion; |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 801 | const BasicBlock *BranchingBB = BB ? BB : R->getEntry(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 802 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 803 | do { |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 804 | if (BranchingBB != CurrentRegion->getEntry()) { |
| 805 | if (const BBCond *Condition = tempScop.getBBCond(BranchingBB)) |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 806 | for (const auto &C : *Condition) { |
| 807 | isl_set *ConditionSet = buildConditionSet(C); |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 808 | Domain = isl_set_intersect(Domain, ConditionSet); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 811 | BranchingBB = CurrentRegion->getEntry(); |
| 812 | CurrentRegion = CurrentRegion->getParent(); |
| 813 | } while (TopRegion != CurrentRegion); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Johannes Doerfert | 45545ff | 2015-08-16 14:36:01 +0000 | [diff] [blame] | 816 | void ScopStmt::buildDomain(TempScop &tempScop, const Region &CurRegion) { |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 817 | isl_space *Space; |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 818 | isl_id *Id; |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 819 | |
| 820 | Space = isl_space_set_alloc(getIslCtx(), 0, getNumIterators()); |
| 821 | |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 822 | Id = isl_id_alloc(getIslCtx(), getBaseName(), this); |
| 823 | |
Tobias Grosser | e19661e | 2011-10-07 08:46:57 +0000 | [diff] [blame] | 824 | Domain = isl_set_universe(Space); |
Johannes Doerfert | 45545ff | 2015-08-16 14:36:01 +0000 | [diff] [blame] | 825 | addLoopBoundsToDomain(tempScop); |
| 826 | addConditionsToDomain(tempScop, CurRegion); |
Tobias Grosser | 084d8f7 | 2012-05-29 09:29:44 +0000 | [diff] [blame] | 827 | Domain = isl_set_set_tuple_id(Domain, Id); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Tobias Grosser | 7b50bee | 2014-11-25 10:51:12 +0000 | [diff] [blame] | 830 | void ScopStmt::deriveAssumptionsFromGEP(GetElementPtrInst *GEP) { |
| 831 | int Dimension = 0; |
| 832 | isl_ctx *Ctx = Parent.getIslCtx(); |
| 833 | isl_local_space *LSpace = isl_local_space_from_space(getDomainSpace()); |
| 834 | Type *Ty = GEP->getPointerOperandType(); |
| 835 | ScalarEvolution &SE = *Parent.getSE(); |
| 836 | |
| 837 | if (auto *PtrTy = dyn_cast<PointerType>(Ty)) { |
| 838 | Dimension = 1; |
| 839 | Ty = PtrTy->getElementType(); |
| 840 | } |
| 841 | |
| 842 | while (auto ArrayTy = dyn_cast<ArrayType>(Ty)) { |
| 843 | unsigned int Operand = 1 + Dimension; |
| 844 | |
| 845 | if (GEP->getNumOperands() <= Operand) |
| 846 | break; |
| 847 | |
| 848 | const SCEV *Expr = SE.getSCEV(GEP->getOperand(Operand)); |
| 849 | |
| 850 | if (isAffineExpr(&Parent.getRegion(), Expr, SE)) { |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 851 | isl_pw_aff *AccessOffset = getPwAff(Expr); |
Tobias Grosser | 7b50bee | 2014-11-25 10:51:12 +0000 | [diff] [blame] | 852 | AccessOffset = |
| 853 | isl_pw_aff_set_tuple_id(AccessOffset, isl_dim_in, getDomainId()); |
| 854 | |
| 855 | isl_pw_aff *DimSize = isl_pw_aff_from_aff(isl_aff_val_on_domain( |
| 856 | isl_local_space_copy(LSpace), |
| 857 | isl_val_int_from_si(Ctx, ArrayTy->getNumElements()))); |
| 858 | |
| 859 | isl_set *OutOfBound = isl_pw_aff_ge_set(AccessOffset, DimSize); |
| 860 | OutOfBound = isl_set_intersect(getDomain(), OutOfBound); |
| 861 | OutOfBound = isl_set_params(OutOfBound); |
| 862 | isl_set *InBound = isl_set_complement(OutOfBound); |
| 863 | isl_set *Executed = isl_set_params(getDomain()); |
| 864 | |
| 865 | // A => B == !A or B |
| 866 | isl_set *InBoundIfExecuted = |
| 867 | isl_set_union(isl_set_complement(Executed), InBound); |
| 868 | |
| 869 | Parent.addAssumption(InBoundIfExecuted); |
| 870 | } |
| 871 | |
| 872 | Dimension += 1; |
| 873 | Ty = ArrayTy->getElementType(); |
| 874 | } |
| 875 | |
| 876 | isl_local_space_free(LSpace); |
| 877 | } |
| 878 | |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 879 | void ScopStmt::deriveAssumptions(BasicBlock *Block) { |
| 880 | for (Instruction &Inst : *Block) |
Tobias Grosser | 7b50bee | 2014-11-25 10:51:12 +0000 | [diff] [blame] | 881 | if (auto *GEP = dyn_cast<GetElementPtrInst>(&Inst)) |
| 882 | deriveAssumptionsFromGEP(GEP); |
| 883 | } |
| 884 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 885 | ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion, |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 886 | Region &R, SmallVectorImpl<Loop *> &Nest) |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 887 | : Parent(parent), BB(nullptr), R(&R), Build(nullptr), |
| 888 | NestLoops(Nest.size()) { |
| 889 | // Setup the induction variables. |
| 890 | for (unsigned i = 0, e = Nest.size(); i < e; ++i) |
| 891 | NestLoops[i] = Nest[i]; |
| 892 | |
Tobias Grosser | 16c4403 | 2015-07-09 07:31:45 +0000 | [diff] [blame] | 893 | BaseName = getIslCompatibleName("Stmt_", R.getNameStr(), ""); |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 894 | |
Johannes Doerfert | 45545ff | 2015-08-16 14:36:01 +0000 | [diff] [blame] | 895 | buildDomain(tempScop, CurRegion); |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 896 | |
| 897 | BasicBlock *EntryBB = R.getEntry(); |
| 898 | for (BasicBlock *Block : R.blocks()) { |
| 899 | buildAccesses(tempScop, Block, Block != EntryBB); |
| 900 | deriveAssumptions(Block); |
| 901 | } |
Tobias Grosser | d83b8a8 | 2015-08-20 19:08:11 +0000 | [diff] [blame^] | 902 | if (DetectReductions) |
| 903 | checkForReductions(); |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | ScopStmt::ScopStmt(Scop &parent, TempScop &tempScop, const Region &CurRegion, |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 907 | BasicBlock &bb, SmallVectorImpl<Loop *> &Nest) |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 908 | : Parent(parent), BB(&bb), R(nullptr), Build(nullptr), |
| 909 | NestLoops(Nest.size()) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 910 | // Setup the induction variables. |
Tobias Grosser | 683b8e4 | 2014-11-30 14:33:31 +0000 | [diff] [blame] | 911 | for (unsigned i = 0, e = Nest.size(); i < e; ++i) |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 912 | NestLoops[i] = Nest[i]; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 913 | |
Johannes Doerfert | 79fc23f | 2014-07-24 23:48:02 +0000 | [diff] [blame] | 914 | BaseName = getIslCompatibleName("Stmt_", &bb, ""); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 915 | |
Johannes Doerfert | 45545ff | 2015-08-16 14:36:01 +0000 | [diff] [blame] | 916 | buildDomain(tempScop, CurRegion); |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 917 | buildAccesses(tempScop, BB); |
| 918 | deriveAssumptions(BB); |
Tobias Grosser | d83b8a8 | 2015-08-20 19:08:11 +0000 | [diff] [blame^] | 919 | if (DetectReductions) |
| 920 | checkForReductions(); |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 923 | /// @brief Collect loads which might form a reduction chain with @p StoreMA |
| 924 | /// |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 925 | /// Check if the stored value for @p StoreMA is a binary operator with one or |
| 926 | /// two loads as operands. If the binary operand is commutative & associative, |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 927 | /// used only once (by @p StoreMA) and its load operands are also used only |
| 928 | /// once, we have found a possible reduction chain. It starts at an operand |
| 929 | /// load and includes the binary operator and @p StoreMA. |
| 930 | /// |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 931 | /// Note: We allow only one use to ensure the load and binary operator cannot |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 932 | /// escape this block or into any other store except @p StoreMA. |
| 933 | void ScopStmt::collectCandiateReductionLoads( |
| 934 | MemoryAccess *StoreMA, SmallVectorImpl<MemoryAccess *> &Loads) { |
| 935 | auto *Store = dyn_cast<StoreInst>(StoreMA->getAccessInstruction()); |
| 936 | if (!Store) |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 937 | return; |
| 938 | |
| 939 | // Skip if there is not one binary operator between the load and the store |
| 940 | auto *BinOp = dyn_cast<BinaryOperator>(Store->getValueOperand()); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 941 | if (!BinOp) |
| 942 | return; |
| 943 | |
| 944 | // Skip if the binary operators has multiple uses |
| 945 | if (BinOp->getNumUses() != 1) |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 946 | return; |
| 947 | |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 948 | // Skip if the opcode of the binary operator is not commutative/associative |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 949 | if (!BinOp->isCommutative() || !BinOp->isAssociative()) |
| 950 | return; |
| 951 | |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 952 | // Skip if the binary operator is outside the current SCoP |
| 953 | if (BinOp->getParent() != Store->getParent()) |
| 954 | return; |
| 955 | |
Johannes Doerfert | 0ee1f21 | 2014-06-17 17:31:36 +0000 | [diff] [blame] | 956 | // Skip if it is a multiplicative reduction and we disabled them |
| 957 | if (DisableMultiplicativeReductions && |
| 958 | (BinOp->getOpcode() == Instruction::Mul || |
| 959 | BinOp->getOpcode() == Instruction::FMul)) |
| 960 | return; |
| 961 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 962 | // Check the binary operator operands for a candidate load |
| 963 | auto *PossibleLoad0 = dyn_cast<LoadInst>(BinOp->getOperand(0)); |
| 964 | auto *PossibleLoad1 = dyn_cast<LoadInst>(BinOp->getOperand(1)); |
| 965 | if (!PossibleLoad0 && !PossibleLoad1) |
| 966 | return; |
| 967 | |
| 968 | // A load is only a candidate if it cannot escape (thus has only this use) |
| 969 | if (PossibleLoad0 && PossibleLoad0->getNumUses() == 1) |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 970 | if (PossibleLoad0->getParent() == Store->getParent()) |
| 971 | Loads.push_back(lookupAccessFor(PossibleLoad0)); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 972 | if (PossibleLoad1 && PossibleLoad1->getNumUses() == 1) |
Johannes Doerfert | 9890a05 | 2014-07-01 00:32:29 +0000 | [diff] [blame] | 973 | if (PossibleLoad1->getParent() == Store->getParent()) |
| 974 | Loads.push_back(lookupAccessFor(PossibleLoad1)); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | /// @brief Check for reductions in this ScopStmt |
| 978 | /// |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 979 | /// Iterate over all store memory accesses and check for valid binary reduction |
| 980 | /// like chains. For all candidates we check if they have the same base address |
| 981 | /// and there are no other accesses which overlap with them. The base address |
| 982 | /// check rules out impossible reductions candidates early. The overlap check, |
| 983 | /// together with the "only one user" check in collectCandiateReductionLoads, |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 984 | /// guarantees that none of the intermediate results will escape during |
| 985 | /// execution of the loop nest. We basically check here that no other memory |
| 986 | /// access can access the same memory as the potential reduction. |
| 987 | void ScopStmt::checkForReductions() { |
| 988 | SmallVector<MemoryAccess *, 2> Loads; |
| 989 | SmallVector<std::pair<MemoryAccess *, MemoryAccess *>, 4> Candidates; |
| 990 | |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 991 | // First collect candidate load-store reduction chains by iterating over all |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 992 | // stores and collecting possible reduction loads. |
| 993 | for (MemoryAccess *StoreMA : MemAccs) { |
| 994 | if (StoreMA->isRead()) |
| 995 | continue; |
| 996 | |
| 997 | Loads.clear(); |
| 998 | collectCandiateReductionLoads(StoreMA, Loads); |
| 999 | for (MemoryAccess *LoadMA : Loads) |
| 1000 | Candidates.push_back(std::make_pair(LoadMA, StoreMA)); |
| 1001 | } |
| 1002 | |
| 1003 | // Then check each possible candidate pair. |
| 1004 | for (const auto &CandidatePair : Candidates) { |
| 1005 | bool Valid = true; |
| 1006 | isl_map *LoadAccs = CandidatePair.first->getAccessRelation(); |
| 1007 | isl_map *StoreAccs = CandidatePair.second->getAccessRelation(); |
| 1008 | |
| 1009 | // Skip those with obviously unequal base addresses. |
| 1010 | if (!isl_map_has_equal_space(LoadAccs, StoreAccs)) { |
| 1011 | isl_map_free(LoadAccs); |
| 1012 | isl_map_free(StoreAccs); |
| 1013 | continue; |
| 1014 | } |
| 1015 | |
| 1016 | // And check if the remaining for overlap with other memory accesses. |
| 1017 | isl_map *AllAccsRel = isl_map_union(LoadAccs, StoreAccs); |
| 1018 | AllAccsRel = isl_map_intersect_domain(AllAccsRel, getDomain()); |
| 1019 | isl_set *AllAccs = isl_map_range(AllAccsRel); |
| 1020 | |
| 1021 | for (MemoryAccess *MA : MemAccs) { |
| 1022 | if (MA == CandidatePair.first || MA == CandidatePair.second) |
| 1023 | continue; |
| 1024 | |
| 1025 | isl_map *AccRel = |
| 1026 | isl_map_intersect_domain(MA->getAccessRelation(), getDomain()); |
| 1027 | isl_set *Accs = isl_map_range(AccRel); |
| 1028 | |
| 1029 | if (isl_set_has_equal_space(AllAccs, Accs) || isl_set_free(Accs)) { |
| 1030 | isl_set *OverlapAccs = isl_set_intersect(Accs, isl_set_copy(AllAccs)); |
| 1031 | Valid = Valid && isl_set_is_empty(OverlapAccs); |
| 1032 | isl_set_free(OverlapAccs); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | isl_set_free(AllAccs); |
| 1037 | if (!Valid) |
| 1038 | continue; |
| 1039 | |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1040 | const LoadInst *Load = |
| 1041 | dyn_cast<const LoadInst>(CandidatePair.first->getAccessInstruction()); |
| 1042 | MemoryAccess::ReductionType RT = |
| 1043 | getReductionType(dyn_cast<BinaryOperator>(Load->user_back()), Load); |
| 1044 | |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1045 | // If no overlapping access was found we mark the load and store as |
| 1046 | // reduction like. |
Johannes Doerfert | f618339 | 2014-07-01 20:52:51 +0000 | [diff] [blame] | 1047 | CandidatePair.first->markAsReductionLike(RT); |
| 1048 | CandidatePair.second->markAsReductionLike(RT); |
Johannes Doerfert | e58a012 | 2014-06-27 20:31:28 +0000 | [diff] [blame] | 1049 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1052 | std::string ScopStmt::getDomainStr() const { return stringFromIslObj(Domain); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1053 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1054 | std::string ScopStmt::getScheduleStr() const { |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1055 | auto *S = getSchedule(); |
| 1056 | auto Str = stringFromIslObj(S); |
| 1057 | isl_map_free(S); |
| 1058 | return Str; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1061 | unsigned ScopStmt::getNumParams() const { return Parent.getNumParams(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1062 | |
Tobias Grosser | f567e1a | 2015-02-19 22:16:12 +0000 | [diff] [blame] | 1063 | unsigned ScopStmt::getNumIterators() const { return NestLoops.size(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1064 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1065 | const char *ScopStmt::getBaseName() const { return BaseName.c_str(); } |
| 1066 | |
Hongbin Zheng | 27f3afb | 2011-04-30 03:26:51 +0000 | [diff] [blame] | 1067 | const Loop *ScopStmt::getLoopForDimension(unsigned Dimension) const { |
Sebastian Pop | 860e021 | 2013-02-15 21:26:44 +0000 | [diff] [blame] | 1068 | return NestLoops[Dimension]; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1071 | isl_ctx *ScopStmt::getIslCtx() const { return Parent.getIslCtx(); } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1072 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 1073 | __isl_give isl_set *ScopStmt::getDomain() const { return isl_set_copy(Domain); } |
Tobias Grosser | d5a7bfc | 2011-05-06 19:52:19 +0000 | [diff] [blame] | 1074 | |
Tobias Grosser | 6e6c7e0 | 2015-03-30 12:22:39 +0000 | [diff] [blame] | 1075 | __isl_give isl_space *ScopStmt::getDomainSpace() const { |
Tobias Grosser | 78d8a3d | 2012-01-17 20:34:23 +0000 | [diff] [blame] | 1076 | return isl_set_get_space(Domain); |
| 1077 | } |
| 1078 | |
Tobias Grosser | 4f663aa | 2015-03-30 11:52:59 +0000 | [diff] [blame] | 1079 | __isl_give isl_id *ScopStmt::getDomainId() const { |
| 1080 | return isl_set_get_tuple_id(Domain); |
| 1081 | } |
Tobias Grosser | cd95b77 | 2012-08-30 11:49:38 +0000 | [diff] [blame] | 1082 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1083 | ScopStmt::~ScopStmt() { |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1084 | DeleteContainerSeconds(InstructionToAccess); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1085 | isl_set_free(Domain); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | void ScopStmt::print(raw_ostream &OS) const { |
| 1089 | OS << "\t" << getBaseName() << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1090 | OS.indent(12) << "Domain :=\n"; |
| 1091 | |
| 1092 | if (Domain) { |
| 1093 | OS.indent(16) << getDomainStr() << ";\n"; |
| 1094 | } else |
| 1095 | OS.indent(16) << "n/a\n"; |
| 1096 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1097 | OS.indent(12) << "Schedule :=\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1098 | |
| 1099 | if (Domain) { |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1100 | OS.indent(16) << getScheduleStr() << ";\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1101 | } else |
| 1102 | OS.indent(16) << "n/a\n"; |
| 1103 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1104 | for (MemoryAccess *Access : MemAccs) |
| 1105 | Access->print(OS); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | void ScopStmt::dump() const { print(dbgs()); } |
| 1109 | |
| 1110 | //===----------------------------------------------------------------------===// |
| 1111 | /// Scop class implement |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1112 | |
Tobias Grosser | 7ffe4e8 | 2011-11-17 12:56:10 +0000 | [diff] [blame] | 1113 | void Scop::setContext(__isl_take isl_set *NewContext) { |
Tobias Grosser | ff9b54d | 2011-11-15 11:38:44 +0000 | [diff] [blame] | 1114 | NewContext = isl_set_align_params(NewContext, isl_set_get_space(Context)); |
| 1115 | isl_set_free(Context); |
| 1116 | Context = NewContext; |
| 1117 | } |
| 1118 | |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 1119 | void Scop::addParams(std::vector<const SCEV *> NewParameters) { |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1120 | for (const SCEV *Parameter : NewParameters) { |
Johannes Doerfert | be40996 | 2015-03-29 20:45:09 +0000 | [diff] [blame] | 1121 | Parameter = extractConstantFactor(Parameter, *SE).second; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1122 | if (ParameterIds.find(Parameter) != ParameterIds.end()) |
| 1123 | continue; |
| 1124 | |
| 1125 | int dimension = Parameters.size(); |
| 1126 | |
| 1127 | Parameters.push_back(Parameter); |
| 1128 | ParameterIds[Parameter] = dimension; |
| 1129 | } |
| 1130 | } |
| 1131 | |
Tobias Grosser | 9a38ab8 | 2011-11-08 15:41:03 +0000 | [diff] [blame] | 1132 | __isl_give isl_id *Scop::getIdForParam(const SCEV *Parameter) const { |
| 1133 | ParamIdType::const_iterator IdIter = ParameterIds.find(Parameter); |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 1134 | |
Tobias Grosser | 9a38ab8 | 2011-11-08 15:41:03 +0000 | [diff] [blame] | 1135 | if (IdIter == ParameterIds.end()) |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 1136 | return nullptr; |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 1137 | |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1138 | std::string ParameterName; |
| 1139 | |
| 1140 | if (const SCEVUnknown *ValueParameter = dyn_cast<SCEVUnknown>(Parameter)) { |
| 1141 | Value *Val = ValueParameter->getValue(); |
Tobias Grosser | 29ee0b1 | 2011-11-17 14:52:36 +0000 | [diff] [blame] | 1142 | ParameterName = Val->getName(); |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | if (ParameterName == "" || ParameterName.substr(0, 2) == "p_") |
Hongbin Zheng | 86a3774 | 2012-04-25 08:01:38 +0000 | [diff] [blame] | 1146 | ParameterName = "p_" + utostr_32(IdIter->second); |
Tobias Grosser | 8f99c16 | 2011-11-15 11:38:55 +0000 | [diff] [blame] | 1147 | |
Tobias Grosser | 20532b8 | 2014-04-11 17:56:49 +0000 | [diff] [blame] | 1148 | return isl_id_alloc(getIslCtx(), ParameterName.c_str(), |
| 1149 | const_cast<void *>((const void *)Parameter)); |
Tobias Grosser | 76c2e32 | 2011-11-07 12:58:59 +0000 | [diff] [blame] | 1150 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1151 | |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 1152 | isl_set *Scop::addNonEmptyDomainConstraints(isl_set *C) const { |
| 1153 | isl_set *DomainContext = isl_union_set_params(getDomains()); |
| 1154 | return isl_set_intersect_params(C, DomainContext); |
| 1155 | } |
| 1156 | |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 1157 | void Scop::addUserContext() { |
| 1158 | if (UserContextStr.empty()) |
| 1159 | return; |
| 1160 | |
| 1161 | isl_set *UserContext = isl_set_read_from_str(IslCtx, UserContextStr.c_str()); |
| 1162 | isl_space *Space = getParamSpace(); |
| 1163 | if (isl_space_dim(Space, isl_dim_param) != |
| 1164 | isl_set_dim(UserContext, isl_dim_param)) { |
| 1165 | auto SpaceStr = isl_space_to_str(Space); |
| 1166 | errs() << "Error: the context provided in -polly-context has not the same " |
| 1167 | << "number of dimensions than the computed context. Due to this " |
| 1168 | << "mismatch, the -polly-context option is ignored. Please provide " |
| 1169 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
| 1170 | free(SpaceStr); |
| 1171 | isl_set_free(UserContext); |
| 1172 | isl_space_free(Space); |
| 1173 | return; |
| 1174 | } |
| 1175 | |
| 1176 | for (unsigned i = 0; i < isl_space_dim(Space, isl_dim_param); i++) { |
| 1177 | auto NameContext = isl_set_get_dim_name(Context, isl_dim_param, i); |
| 1178 | auto NameUserContext = isl_set_get_dim_name(UserContext, isl_dim_param, i); |
| 1179 | |
| 1180 | if (strcmp(NameContext, NameUserContext) != 0) { |
| 1181 | auto SpaceStr = isl_space_to_str(Space); |
| 1182 | errs() << "Error: the name of dimension " << i |
| 1183 | << " provided in -polly-context " |
| 1184 | << "is '" << NameUserContext << "', but the name in the computed " |
| 1185 | << "context is '" << NameContext |
| 1186 | << "'. Due to this name mismatch, " |
| 1187 | << "the -polly-context option is ignored. Please provide " |
| 1188 | << "the context in the parameter space: " << SpaceStr << ".\n"; |
| 1189 | free(SpaceStr); |
| 1190 | isl_set_free(UserContext); |
| 1191 | isl_space_free(Space); |
| 1192 | return; |
| 1193 | } |
| 1194 | |
| 1195 | UserContext = |
| 1196 | isl_set_set_dim_id(UserContext, isl_dim_param, i, |
| 1197 | isl_space_get_dim_id(Space, isl_dim_param, i)); |
| 1198 | } |
| 1199 | |
| 1200 | Context = isl_set_intersect(Context, UserContext); |
| 1201 | isl_space_free(Space); |
| 1202 | } |
| 1203 | |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1204 | void Scop::buildContext() { |
| 1205 | isl_space *Space = isl_space_params_alloc(IslCtx, 0); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 1206 | Context = isl_set_universe(isl_space_copy(Space)); |
| 1207 | AssumedContext = isl_set_universe(Space); |
Tobias Grosser | 0e27e24 | 2011-10-06 00:03:48 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1210 | void Scop::addParameterBounds() { |
Johannes Doerfert | 4f8ac3d | 2015-02-23 16:15:51 +0000 | [diff] [blame] | 1211 | for (const auto &ParamID : ParameterIds) { |
Johannes Doerfert | 4f8ac3d | 2015-02-23 16:15:51 +0000 | [diff] [blame] | 1212 | int dim = ParamID.second; |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1213 | |
Johannes Doerfert | 4f8ac3d | 2015-02-23 16:15:51 +0000 | [diff] [blame] | 1214 | ConstantRange SRange = SE->getSignedRange(ParamID.first); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1215 | |
Johannes Doerfert | e704494 | 2015-02-24 11:58:30 +0000 | [diff] [blame] | 1216 | Context = addRangeBoundsToSet(Context, SRange, dim, isl_dim_param); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1217 | } |
| 1218 | } |
| 1219 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1220 | void Scop::realignParams() { |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1221 | // Add all parameters into a common model. |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1222 | isl_space *Space = isl_space_params_alloc(IslCtx, ParameterIds.size()); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1223 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1224 | for (const auto &ParamID : ParameterIds) { |
| 1225 | const SCEV *Parameter = ParamID.first; |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1226 | isl_id *id = getIdForParam(Parameter); |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1227 | Space = isl_space_set_dim_id(Space, isl_dim_param, ParamID.second, id); |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | // Align the parameters of all data structures to the model. |
| 1231 | Context = isl_set_align_params(Context, Space); |
| 1232 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1233 | for (ScopStmt &Stmt : *this) |
| 1234 | Stmt.realignParams(); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1237 | void Scop::simplifyAssumedContext() { |
| 1238 | // The parameter constraints of the iteration domains give us a set of |
| 1239 | // constraints that need to hold for all cases where at least a single |
| 1240 | // statement iteration is executed in the whole scop. We now simplify the |
| 1241 | // assumed context under the assumption that such constraints hold and at |
| 1242 | // least a single statement iteration is executed. For cases where no |
| 1243 | // statement instances are executed, the assumptions we have taken about |
| 1244 | // the executed code do not matter and can be changed. |
| 1245 | // |
| 1246 | // WARNING: This only holds if the assumptions we have taken do not reduce |
| 1247 | // the set of statement instances that are executed. Otherwise we |
| 1248 | // may run into a case where the iteration domains suggest that |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1249 | // for a certain set of parameter constraints no code is executed, |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1250 | // but in the original program some computation would have been |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1251 | // performed. In such a case, modifying the run-time conditions and |
| 1252 | // possibly influencing the run-time check may cause certain scops |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1253 | // to not be executed. |
| 1254 | // |
| 1255 | // Example: |
| 1256 | // |
| 1257 | // When delinearizing the following code: |
| 1258 | // |
| 1259 | // for (long i = 0; i < 100; i++) |
| 1260 | // for (long j = 0; j < m; j++) |
| 1261 | // A[i+p][j] = 1.0; |
| 1262 | // |
| 1263 | // we assume that the condition m <= 0 or (m >= 1 and p >= 0) holds as |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1264 | // otherwise we would access out of bound data. Now, knowing that code is |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1265 | // only executed for the case m >= 0, it is sufficient to assume p >= 0. |
| 1266 | AssumedContext = |
| 1267 | isl_set_gist_params(AssumedContext, isl_union_set_params(getDomains())); |
Johannes Doerfert | 4f8ac3d | 2015-02-23 16:15:51 +0000 | [diff] [blame] | 1268 | AssumedContext = isl_set_gist_params(AssumedContext, getContext()); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1271 | /// @brief Add the minimal/maximal access in @p Set to @p User. |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 1272 | static isl_stat buildMinMaxAccess(__isl_take isl_set *Set, void *User) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1273 | Scop::MinMaxVectorTy *MinMaxAccesses = (Scop::MinMaxVectorTy *)User; |
| 1274 | isl_pw_multi_aff *MinPMA, *MaxPMA; |
| 1275 | isl_pw_aff *LastDimAff; |
| 1276 | isl_aff *OneAff; |
| 1277 | unsigned Pos; |
| 1278 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1279 | // Restrict the number of parameters involved in the access as the lexmin/ |
| 1280 | // lexmax computation will take too long if this number is high. |
| 1281 | // |
| 1282 | // Experiments with a simple test case using an i7 4800MQ: |
| 1283 | // |
| 1284 | // #Parameters involved | Time (in sec) |
| 1285 | // 6 | 0.01 |
| 1286 | // 7 | 0.04 |
| 1287 | // 8 | 0.12 |
| 1288 | // 9 | 0.40 |
| 1289 | // 10 | 1.54 |
| 1290 | // 11 | 6.78 |
| 1291 | // 12 | 30.38 |
| 1292 | // |
| 1293 | if (isl_set_n_param(Set) > RunTimeChecksMaxParameters) { |
| 1294 | unsigned InvolvedParams = 0; |
| 1295 | for (unsigned u = 0, e = isl_set_n_param(Set); u < e; u++) |
| 1296 | if (isl_set_involves_dims(Set, isl_dim_param, u, 1)) |
| 1297 | InvolvedParams++; |
| 1298 | |
| 1299 | if (InvolvedParams > RunTimeChecksMaxParameters) { |
| 1300 | isl_set_free(Set); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 1301 | return isl_stat_error; |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1302 | } |
| 1303 | } |
| 1304 | |
Johannes Doerfert | b6755bb | 2015-02-14 12:00:06 +0000 | [diff] [blame] | 1305 | Set = isl_set_remove_divs(Set); |
| 1306 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1307 | MinPMA = isl_set_lexmin_pw_multi_aff(isl_set_copy(Set)); |
| 1308 | MaxPMA = isl_set_lexmax_pw_multi_aff(isl_set_copy(Set)); |
| 1309 | |
Johannes Doerfert | 219b20e | 2014-10-07 14:37:59 +0000 | [diff] [blame] | 1310 | MinPMA = isl_pw_multi_aff_coalesce(MinPMA); |
| 1311 | MaxPMA = isl_pw_multi_aff_coalesce(MaxPMA); |
| 1312 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1313 | // Adjust the last dimension of the maximal access by one as we want to |
| 1314 | // enclose the accessed memory region by MinPMA and MaxPMA. The pointer |
| 1315 | // we test during code generation might now point after the end of the |
| 1316 | // allocated array but we will never dereference it anyway. |
| 1317 | assert(isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) && |
| 1318 | "Assumed at least one output dimension"); |
| 1319 | Pos = isl_pw_multi_aff_dim(MaxPMA, isl_dim_out) - 1; |
| 1320 | LastDimAff = isl_pw_multi_aff_get_pw_aff(MaxPMA, Pos); |
| 1321 | OneAff = isl_aff_zero_on_domain( |
| 1322 | isl_local_space_from_space(isl_pw_aff_get_domain_space(LastDimAff))); |
| 1323 | OneAff = isl_aff_add_constant_si(OneAff, 1); |
| 1324 | LastDimAff = isl_pw_aff_add(LastDimAff, isl_pw_aff_from_aff(OneAff)); |
| 1325 | MaxPMA = isl_pw_multi_aff_set_pw_aff(MaxPMA, Pos, LastDimAff); |
| 1326 | |
| 1327 | MinMaxAccesses->push_back(std::make_pair(MinPMA, MaxPMA)); |
| 1328 | |
| 1329 | isl_set_free(Set); |
Tobias Grosser | b2f3992 | 2015-05-28 13:32:11 +0000 | [diff] [blame] | 1330 | return isl_stat_ok; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 1333 | static __isl_give isl_set *getAccessDomain(MemoryAccess *MA) { |
| 1334 | isl_set *Domain = MA->getStatement()->getDomain(); |
| 1335 | Domain = isl_set_project_out(Domain, isl_dim_set, 0, isl_set_n_dim(Domain)); |
| 1336 | return isl_set_reset_tuple_id(Domain); |
| 1337 | } |
| 1338 | |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1339 | /// @brief Wrapper function to calculate minimal/maximal accesses to each array. |
| 1340 | static bool calculateMinMaxAccess(__isl_take isl_union_map *Accesses, |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 1341 | __isl_take isl_union_set *Domains, |
| 1342 | __isl_take isl_set *AssumedContext, |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 1343 | Scop::MinMaxVectorTy &MinMaxAccesses) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1344 | |
| 1345 | Accesses = isl_union_map_intersect_domain(Accesses, Domains); |
| 1346 | isl_union_set *Locations = isl_union_map_range(Accesses); |
| 1347 | Locations = isl_union_set_intersect_params(Locations, AssumedContext); |
| 1348 | Locations = isl_union_set_coalesce(Locations); |
| 1349 | Locations = isl_union_set_detect_equalities(Locations); |
| 1350 | bool Valid = (0 == isl_union_set_foreach_set(Locations, buildMinMaxAccess, |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 1351 | &MinMaxAccesses)); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1352 | isl_union_set_free(Locations); |
| 1353 | return Valid; |
| 1354 | } |
| 1355 | |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 1356 | void Scop::buildAliasChecks(AliasAnalysis &AA) { |
| 1357 | if (!PollyUseRuntimeAliasChecks) |
| 1358 | return; |
| 1359 | |
| 1360 | if (buildAliasGroups(AA)) |
| 1361 | return; |
| 1362 | |
| 1363 | // If a problem occurs while building the alias groups we need to delete |
| 1364 | // this SCoP and pretend it wasn't valid in the first place. To this end |
| 1365 | // we make the assumed context infeasible. |
| 1366 | addAssumption(isl_set_empty(getParamSpace())); |
| 1367 | |
| 1368 | DEBUG(dbgs() << "\n\nNOTE: Run time checks for " << getNameStr() |
| 1369 | << " could not be created as the number of parameters involved " |
| 1370 | "is too high. The SCoP will be " |
| 1371 | "dismissed.\nUse:\n\t--polly-rtc-max-parameters=X\nto adjust " |
| 1372 | "the maximal number of parameters but be advised that the " |
| 1373 | "compile time might increase exponentially.\n\n"); |
| 1374 | } |
| 1375 | |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1376 | bool Scop::buildAliasGroups(AliasAnalysis &AA) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1377 | // To create sound alias checks we perform the following steps: |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1378 | // o) Use the alias analysis and an alias set tracker to build alias sets |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1379 | // for all memory accesses inside the SCoP. |
| 1380 | // o) For each alias set we then map the aliasing pointers back to the |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1381 | // memory accesses we know, thus obtain groups of memory accesses which |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1382 | // might alias. |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 1383 | // o) We divide each group based on the domains of the minimal/maximal |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1384 | // accesses. That means two minimal/maximal accesses are only in a group |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 1385 | // if their access domains intersect, otherwise they are in different |
| 1386 | // ones. |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1387 | // o) We partition each group into read only and non read only accesses. |
Johannes Doerfert | 6cad9c4 | 2015-02-24 16:00:29 +0000 | [diff] [blame] | 1388 | // o) For each group with more than one base pointer we then compute minimal |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1389 | // and maximal accesses to each array of a group in read only and non |
| 1390 | // read only partitions separately. |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1391 | using AliasGroupTy = SmallVector<MemoryAccess *, 4>; |
| 1392 | |
| 1393 | AliasSetTracker AST(AA); |
| 1394 | |
| 1395 | DenseMap<Value *, MemoryAccess *> PtrToAcc; |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 1396 | DenseSet<Value *> HasWriteAccess; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1397 | for (ScopStmt &Stmt : *this) { |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 1398 | |
| 1399 | // Skip statements with an empty domain as they will never be executed. |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1400 | isl_set *StmtDomain = Stmt.getDomain(); |
Johannes Doerfert | f1ee262 | 2014-10-06 17:43:00 +0000 | [diff] [blame] | 1401 | bool StmtDomainEmpty = isl_set_is_empty(StmtDomain); |
| 1402 | isl_set_free(StmtDomain); |
| 1403 | if (StmtDomainEmpty) |
| 1404 | continue; |
| 1405 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1406 | for (MemoryAccess *MA : Stmt) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1407 | if (MA->isScalar()) |
| 1408 | continue; |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 1409 | if (!MA->isRead()) |
| 1410 | HasWriteAccess.insert(MA->getBaseAddr()); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1411 | Instruction *Acc = MA->getAccessInstruction(); |
| 1412 | PtrToAcc[getPointerOperand(*Acc)] = MA; |
| 1413 | AST.add(Acc); |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | SmallVector<AliasGroupTy, 4> AliasGroups; |
| 1418 | for (AliasSet &AS : AST) { |
Johannes Doerfert | 74f6869 | 2014-10-08 02:23:48 +0000 | [diff] [blame] | 1419 | if (AS.isMustAlias() || AS.isForwardingAliasSet()) |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1420 | continue; |
| 1421 | AliasGroupTy AG; |
| 1422 | for (auto PR : AS) |
| 1423 | AG.push_back(PtrToAcc[PR.getValue()]); |
| 1424 | assert(AG.size() > 1 && |
| 1425 | "Alias groups should contain at least two accesses"); |
| 1426 | AliasGroups.push_back(std::move(AG)); |
| 1427 | } |
| 1428 | |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 1429 | // Split the alias groups based on their domain. |
| 1430 | for (unsigned u = 0; u < AliasGroups.size(); u++) { |
| 1431 | AliasGroupTy NewAG; |
| 1432 | AliasGroupTy &AG = AliasGroups[u]; |
| 1433 | AliasGroupTy::iterator AGI = AG.begin(); |
| 1434 | isl_set *AGDomain = getAccessDomain(*AGI); |
| 1435 | while (AGI != AG.end()) { |
| 1436 | MemoryAccess *MA = *AGI; |
| 1437 | isl_set *MADomain = getAccessDomain(MA); |
| 1438 | if (isl_set_is_disjoint(AGDomain, MADomain)) { |
| 1439 | NewAG.push_back(MA); |
| 1440 | AGI = AG.erase(AGI); |
| 1441 | isl_set_free(MADomain); |
| 1442 | } else { |
| 1443 | AGDomain = isl_set_union(AGDomain, MADomain); |
| 1444 | AGI++; |
| 1445 | } |
| 1446 | } |
| 1447 | if (NewAG.size() > 1) |
| 1448 | AliasGroups.push_back(std::move(NewAG)); |
| 1449 | isl_set_free(AGDomain); |
| 1450 | } |
| 1451 | |
Tobias Grosser | f4c24b2 | 2015-04-05 13:11:54 +0000 | [diff] [blame] | 1452 | MapVector<const Value *, SmallPtrSet<MemoryAccess *, 8>> ReadOnlyPairs; |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 1453 | SmallPtrSet<const Value *, 4> NonReadOnlyBaseValues; |
| 1454 | for (AliasGroupTy &AG : AliasGroups) { |
| 1455 | NonReadOnlyBaseValues.clear(); |
| 1456 | ReadOnlyPairs.clear(); |
| 1457 | |
Johannes Doerfert | eeab05a | 2014-10-01 12:42:37 +0000 | [diff] [blame] | 1458 | if (AG.size() < 2) { |
| 1459 | AG.clear(); |
| 1460 | continue; |
| 1461 | } |
| 1462 | |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 1463 | for (auto II = AG.begin(); II != AG.end();) { |
| 1464 | Value *BaseAddr = (*II)->getBaseAddr(); |
| 1465 | if (HasWriteAccess.count(BaseAddr)) { |
| 1466 | NonReadOnlyBaseValues.insert(BaseAddr); |
| 1467 | II++; |
| 1468 | } else { |
| 1469 | ReadOnlyPairs[BaseAddr].insert(*II); |
| 1470 | II = AG.erase(II); |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | // If we don't have read only pointers check if there are at least two |
| 1475 | // non read only pointers, otherwise clear the alias group. |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 1476 | if (ReadOnlyPairs.empty() && NonReadOnlyBaseValues.size() <= 1) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1477 | AG.clear(); |
Johannes Doerfert | 1377173 | 2014-10-01 12:40:46 +0000 | [diff] [blame] | 1478 | continue; |
| 1479 | } |
| 1480 | |
| 1481 | // If we don't have non read only pointers clear the alias group. |
| 1482 | if (NonReadOnlyBaseValues.empty()) { |
| 1483 | AG.clear(); |
| 1484 | continue; |
| 1485 | } |
| 1486 | |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1487 | // Calculate minimal and maximal accesses for non read only accesses. |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 1488 | MinMaxAliasGroups.emplace_back(); |
| 1489 | MinMaxVectorPairTy &pair = MinMaxAliasGroups.back(); |
| 1490 | MinMaxVectorTy &MinMaxAccessesNonReadOnly = pair.first; |
| 1491 | MinMaxVectorTy &MinMaxAccessesReadOnly = pair.second; |
| 1492 | MinMaxAccessesNonReadOnly.reserve(AG.size()); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1493 | |
| 1494 | isl_union_map *Accesses = isl_union_map_empty(getParamSpace()); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1495 | |
| 1496 | // AG contains only non read only accesses. |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1497 | for (MemoryAccess *MA : AG) |
| 1498 | Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation()); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1499 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 1500 | bool Valid = calculateMinMaxAccess( |
| 1501 | Accesses, getDomains(), getAssumedContext(), MinMaxAccessesNonReadOnly); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1502 | |
| 1503 | // Bail out if the number of values we need to compare is too large. |
| 1504 | // This is important as the number of comparisions grows quadratically with |
| 1505 | // the number of values we need to compare. |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 1506 | if (!Valid || (MinMaxAccessesNonReadOnly.size() + !ReadOnlyPairs.empty() > |
| 1507 | RunTimeChecksMaxArraysPerGroup)) |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1508 | return false; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1509 | |
| 1510 | // Calculate minimal and maximal accesses for read only accesses. |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 1511 | MinMaxAccessesReadOnly.reserve(ReadOnlyPairs.size()); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1512 | Accesses = isl_union_map_empty(getParamSpace()); |
| 1513 | |
| 1514 | for (const auto &ReadOnlyPair : ReadOnlyPairs) |
| 1515 | for (MemoryAccess *MA : ReadOnlyPair.second) |
| 1516 | Accesses = isl_union_map_add_map(Accesses, MA->getAccessRelation()); |
| 1517 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 1518 | Valid = calculateMinMaxAccess(Accesses, getDomains(), getAssumedContext(), |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1519 | MinMaxAccessesReadOnly); |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1520 | |
| 1521 | if (!Valid) |
Tobias Grosser | 50d4e2e | 2015-03-28 14:50:32 +0000 | [diff] [blame] | 1522 | return false; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1523 | } |
Johannes Doerfert | 9143d67 | 2014-09-27 11:02:39 +0000 | [diff] [blame] | 1524 | |
Tobias Grosser | 50d4e2e | 2015-03-28 14:50:32 +0000 | [diff] [blame] | 1525 | return true; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Johannes Doerfert | f8206cf | 2015-04-12 22:58:40 +0000 | [diff] [blame] | 1528 | static unsigned getMaxLoopDepthInRegion(const Region &R, LoopInfo &LI, |
| 1529 | ScopDetection &SD) { |
| 1530 | |
| 1531 | const ScopDetection::BoxedLoopsSetTy *BoxedLoops = SD.getBoxedLoops(&R); |
| 1532 | |
Johannes Doerfert | e3da05a | 2014-11-01 00:12:13 +0000 | [diff] [blame] | 1533 | unsigned MinLD = INT_MAX, MaxLD = 0; |
| 1534 | for (BasicBlock *BB : R.blocks()) { |
| 1535 | if (Loop *L = LI.getLoopFor(BB)) { |
David Peixotto | dc0a11c | 2015-01-13 18:31:55 +0000 | [diff] [blame] | 1536 | if (!R.contains(L)) |
| 1537 | continue; |
Johannes Doerfert | f8206cf | 2015-04-12 22:58:40 +0000 | [diff] [blame] | 1538 | if (BoxedLoops && BoxedLoops->count(L)) |
| 1539 | continue; |
Johannes Doerfert | e3da05a | 2014-11-01 00:12:13 +0000 | [diff] [blame] | 1540 | unsigned LD = L->getLoopDepth(); |
| 1541 | MinLD = std::min(MinLD, LD); |
| 1542 | MaxLD = std::max(MaxLD, LD); |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | // Handle the case that there is no loop in the SCoP first. |
| 1547 | if (MaxLD == 0) |
| 1548 | return 1; |
| 1549 | |
| 1550 | assert(MinLD >= 1 && "Minimal loop depth should be at least one"); |
| 1551 | assert(MaxLD >= MinLD && |
| 1552 | "Maximal loop depth was smaller than mininaml loop depth?"); |
| 1553 | return MaxLD - MinLD + 1; |
| 1554 | } |
| 1555 | |
Michael Kruse | 471a5e3 | 2015-07-30 19:27:04 +0000 | [diff] [blame] | 1556 | Scop::Scop(Region &R, ScalarEvolution &ScalarEvolution, isl_ctx *Context, |
| 1557 | unsigned MaxLoopDepth) |
| 1558 | : SE(&ScalarEvolution), R(R), IsOptimized(false), |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 1559 | MaxLoopDepth(MaxLoopDepth), IslCtx(Context), Affinator(this) {} |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 1560 | |
Tobias Grosser | 4098501 | 2015-08-20 19:08:05 +0000 | [diff] [blame] | 1561 | void Scop::initFromTempScop(TempScop &TempScop, LoopInfo &LI, ScopDetection &SD, |
| 1562 | AliasAnalysis &AA) { |
Tobias Grosser | 6be480c | 2011-11-08 15:41:13 +0000 | [diff] [blame] | 1563 | buildContext(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1564 | |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 1565 | SmallVector<Loop *, 8> NestLoops; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1566 | |
Tobias Grosser | 5483931 | 2015-04-21 11:37:25 +0000 | [diff] [blame] | 1567 | // Build the iteration domain, access functions and schedule functions |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1568 | // traversing the region tree. |
Michael Kruse | 471a5e3 | 2015-07-30 19:27:04 +0000 | [diff] [blame] | 1569 | Schedule = buildScop(TempScop, getRegion(), NestLoops, LI, SD); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1570 | if (!Schedule) |
| 1571 | Schedule = isl_schedule_empty(getParamSpace()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1572 | |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1573 | realignParams(); |
Tobias Grosser | 18daaca | 2012-05-22 10:47:27 +0000 | [diff] [blame] | 1574 | addParameterBounds(); |
Tobias Grosser | 8a9c235 | 2015-08-16 10:19:29 +0000 | [diff] [blame] | 1575 | addUserContext(); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1576 | simplifyAssumedContext(); |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 1577 | buildAliasChecks(AA); |
Tobias Grosser | 8cae72f | 2011-11-08 15:41:08 +0000 | [diff] [blame] | 1578 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1579 | assert(NestLoops.empty() && "NestLoops not empty at top level!"); |
| 1580 | } |
| 1581 | |
Michael Kruse | 471a5e3 | 2015-07-30 19:27:04 +0000 | [diff] [blame] | 1582 | Scop *Scop::createFromTempScop(TempScop &TempScop, LoopInfo &LI, |
| 1583 | ScalarEvolution &SE, ScopDetection &SD, |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 1584 | AliasAnalysis &AA, isl_ctx *ctx) { |
Michael Kruse | 471a5e3 | 2015-07-30 19:27:04 +0000 | [diff] [blame] | 1585 | auto &R = TempScop.getMaxRegion(); |
| 1586 | auto MaxLoopDepth = getMaxLoopDepthInRegion(R, LI, SD); |
| 1587 | auto S = new Scop(R, SE, ctx, MaxLoopDepth); |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 1588 | S->initFromTempScop(TempScop, LI, SD, AA); |
| 1589 | |
Michael Kruse | 471a5e3 | 2015-07-30 19:27:04 +0000 | [diff] [blame] | 1590 | return S; |
| 1591 | } |
| 1592 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1593 | Scop::~Scop() { |
| 1594 | isl_set_free(Context); |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 1595 | isl_set_free(AssumedContext); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1596 | isl_schedule_free(Schedule); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1597 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1598 | // Free the alias groups |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1599 | for (MinMaxVectorPairTy &MinMaxAccessPair : MinMaxAliasGroups) { |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 1600 | for (MinMaxAccessTy &MMA : MinMaxAccessPair.first) { |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1601 | isl_pw_multi_aff_free(MMA.first); |
| 1602 | isl_pw_multi_aff_free(MMA.second); |
| 1603 | } |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 1604 | for (MinMaxAccessTy &MMA : MinMaxAccessPair.second) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1605 | isl_pw_multi_aff_free(MMA.first); |
| 1606 | isl_pw_multi_aff_free(MMA.second); |
| 1607 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1608 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Johannes Doerfert | 80ef110 | 2014-11-07 08:31:31 +0000 | [diff] [blame] | 1611 | const ScopArrayInfo * |
| 1612 | Scop::getOrCreateScopArrayInfo(Value *BasePtr, Type *AccessType, |
Tobias Grosser | 9224522 | 2015-07-28 14:53:44 +0000 | [diff] [blame] | 1613 | const SmallVector<const SCEV *, 4> &Sizes, |
| 1614 | bool IsPHI) { |
| 1615 | auto &SAI = ScopArrayInfoMap[std::make_pair(BasePtr, IsPHI)]; |
Johannes Doerfert | 80ef110 | 2014-11-07 08:31:31 +0000 | [diff] [blame] | 1616 | if (!SAI) |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 1617 | SAI.reset(new ScopArrayInfo(BasePtr, AccessType, getIslCtx(), Sizes, IsPHI, |
| 1618 | this)); |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 1619 | return SAI.get(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
Tobias Grosser | 9224522 | 2015-07-28 14:53:44 +0000 | [diff] [blame] | 1622 | const ScopArrayInfo *Scop::getScopArrayInfo(Value *BasePtr, bool IsPHI) { |
| 1623 | auto *SAI = ScopArrayInfoMap[std::make_pair(BasePtr, IsPHI)].get(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 +0000 | [diff] [blame] | 1624 | assert(SAI && "No ScopArrayInfo available for this base pointer"); |
| 1625 | return SAI; |
| 1626 | } |
| 1627 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1628 | std::string Scop::getContextStr() const { return stringFromIslObj(Context); } |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1629 | std::string Scop::getAssumedContextStr() const { |
| 1630 | return stringFromIslObj(AssumedContext); |
| 1631 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1632 | |
| 1633 | std::string Scop::getNameStr() const { |
| 1634 | std::string ExitName, EntryName; |
| 1635 | raw_string_ostream ExitStr(ExitName); |
| 1636 | raw_string_ostream EntryStr(EntryName); |
| 1637 | |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 1638 | R.getEntry()->printAsOperand(EntryStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1639 | EntryStr.str(); |
| 1640 | |
| 1641 | if (R.getExit()) { |
Tobias Grosser | f240b48 | 2014-01-09 10:42:15 +0000 | [diff] [blame] | 1642 | R.getExit()->printAsOperand(ExitStr, false); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1643 | ExitStr.str(); |
| 1644 | } else |
| 1645 | ExitName = "FunctionExit"; |
| 1646 | |
| 1647 | return EntryName + "---" + ExitName; |
| 1648 | } |
| 1649 | |
Tobias Grosser | 74394f0 | 2013-01-14 22:40:23 +0000 | [diff] [blame] | 1650 | __isl_give isl_set *Scop::getContext() const { return isl_set_copy(Context); } |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 1651 | __isl_give isl_space *Scop::getParamSpace() const { |
Tobias Grosser | eeb9f3c | 2015-05-26 21:37:31 +0000 | [diff] [blame] | 1652 | return isl_set_get_space(Context); |
Tobias Grosser | 3748705 | 2011-10-06 00:03:42 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Tobias Grosser | e86109f | 2013-10-29 21:05:49 +0000 | [diff] [blame] | 1655 | __isl_give isl_set *Scop::getAssumedContext() const { |
| 1656 | return isl_set_copy(AssumedContext); |
| 1657 | } |
| 1658 | |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 1659 | __isl_give isl_set *Scop::getRuntimeCheckContext() const { |
| 1660 | isl_set *RuntimeCheckContext = getAssumedContext(); |
| 1661 | return RuntimeCheckContext; |
| 1662 | } |
| 1663 | |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 1664 | bool Scop::hasFeasibleRuntimeContext() const { |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 1665 | isl_set *RuntimeCheckContext = getRuntimeCheckContext(); |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 1666 | RuntimeCheckContext = addNonEmptyDomainConstraints(RuntimeCheckContext); |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 1667 | bool IsFeasible = !isl_set_is_empty(RuntimeCheckContext); |
| 1668 | isl_set_free(RuntimeCheckContext); |
| 1669 | return IsFeasible; |
| 1670 | } |
| 1671 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1672 | void Scop::addAssumption(__isl_take isl_set *Set) { |
| 1673 | AssumedContext = isl_set_intersect(AssumedContext, Set); |
Tobias Grosser | 7b50bee | 2014-11-25 10:51:12 +0000 | [diff] [blame] | 1674 | AssumedContext = isl_set_coalesce(AssumedContext); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1677 | void Scop::printContext(raw_ostream &OS) const { |
| 1678 | OS << "Context:\n"; |
| 1679 | |
| 1680 | if (!Context) { |
| 1681 | OS.indent(4) << "n/a\n\n"; |
| 1682 | return; |
| 1683 | } |
| 1684 | |
| 1685 | OS.indent(4) << getContextStr() << "\n"; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1686 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1687 | OS.indent(4) << "Assumed Context:\n"; |
| 1688 | if (!AssumedContext) { |
| 1689 | OS.indent(4) << "n/a\n\n"; |
| 1690 | return; |
| 1691 | } |
| 1692 | |
| 1693 | OS.indent(4) << getAssumedContextStr() << "\n"; |
| 1694 | |
Tobias Grosser | 083d3d3 | 2014-06-28 08:59:45 +0000 | [diff] [blame] | 1695 | for (const SCEV *Parameter : Parameters) { |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1696 | int Dim = ParameterIds.find(Parameter)->second; |
Tobias Grosser | 60b54f1 | 2011-11-08 15:41:28 +0000 | [diff] [blame] | 1697 | OS.indent(4) << "p" << Dim << ": " << *Parameter << "\n"; |
| 1698 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1701 | void Scop::printAliasAssumptions(raw_ostream &OS) const { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 1702 | int noOfGroups = 0; |
| 1703 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 1704 | if (Pair.second.size() == 0) |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1705 | noOfGroups += 1; |
| 1706 | else |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 1707 | noOfGroups += Pair.second.size(); |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 1710 | OS.indent(4) << "Alias Groups (" << noOfGroups << "):\n"; |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1711 | if (MinMaxAliasGroups.empty()) { |
| 1712 | OS.indent(8) << "n/a\n"; |
| 1713 | return; |
| 1714 | } |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1715 | |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 1716 | for (const MinMaxVectorPairTy &Pair : MinMaxAliasGroups) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1717 | |
| 1718 | // If the group has no read only accesses print the write accesses. |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 1719 | if (Pair.second.empty()) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1720 | OS.indent(8) << "[["; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 1721 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 1722 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 1723 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1724 | } |
| 1725 | OS << " ]]\n"; |
| 1726 | } |
| 1727 | |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 1728 | for (const MinMaxAccessTy &MMAReadOnly : Pair.second) { |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1729 | OS.indent(8) << "[["; |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 1730 | OS << " <" << MMAReadOnly.first << ", " << MMAReadOnly.second << ">"; |
Johannes Doerfert | 210b09a | 2015-07-26 13:14:38 +0000 | [diff] [blame] | 1731 | for (const MinMaxAccessTy &MMANonReadOnly : Pair.first) { |
Tobias Grosser | bb853c2 | 2015-07-25 12:31:03 +0000 | [diff] [blame] | 1732 | OS << " <" << MMANonReadOnly.first << ", " << MMANonReadOnly.second |
| 1733 | << ">"; |
Johannes Doerfert | 338b42c | 2015-07-23 17:04:54 +0000 | [diff] [blame] | 1734 | } |
| 1735 | OS << " ]]\n"; |
| 1736 | } |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1737 | } |
| 1738 | } |
| 1739 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1740 | void Scop::printStatements(raw_ostream &OS) const { |
| 1741 | OS << "Statements {\n"; |
| 1742 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1743 | for (const ScopStmt &Stmt : *this) |
| 1744 | OS.indent(4) << Stmt; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1745 | |
| 1746 | OS.indent(4) << "}\n"; |
| 1747 | } |
| 1748 | |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 1749 | void Scop::printArrayInfo(raw_ostream &OS) const { |
| 1750 | OS << "Arrays {\n"; |
| 1751 | |
Tobias Grosser | ab67144 | 2015-05-23 05:58:27 +0000 | [diff] [blame] | 1752 | for (auto &Array : arrays()) |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 1753 | Array.second->print(OS); |
| 1754 | |
| 1755 | OS.indent(4) << "}\n"; |
Tobias Grosser | d46fd5e | 2015-08-12 15:27:16 +0000 | [diff] [blame] | 1756 | |
| 1757 | OS.indent(4) << "Arrays (Bounds as pw_affs) {\n"; |
| 1758 | |
| 1759 | for (auto &Array : arrays()) |
| 1760 | Array.second->print(OS, /* SizeAsPwAff */ true); |
| 1761 | |
| 1762 | OS.indent(4) << "}\n"; |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1765 | void Scop::print(raw_ostream &OS) const { |
Tobias Grosser | 4eb7ddb | 2014-03-18 18:51:11 +0000 | [diff] [blame] | 1766 | OS.indent(4) << "Function: " << getRegion().getEntry()->getParent()->getName() |
| 1767 | << "\n"; |
Tobias Grosser | 483fdd4 | 2014-03-18 18:05:38 +0000 | [diff] [blame] | 1768 | OS.indent(4) << "Region: " << getNameStr() << "\n"; |
David Peixotto | dc0a11c | 2015-01-13 18:31:55 +0000 | [diff] [blame] | 1769 | OS.indent(4) << "Max Loop Depth: " << getMaxLoopDepth() << "\n"; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1770 | printContext(OS.indent(4)); |
Tobias Grosser | 49ad36c | 2015-05-20 08:05:31 +0000 | [diff] [blame] | 1771 | printArrayInfo(OS.indent(4)); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 1772 | printAliasAssumptions(OS); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1773 | printStatements(OS.indent(4)); |
| 1774 | } |
| 1775 | |
| 1776 | void Scop::dump() const { print(dbgs()); } |
| 1777 | |
Tobias Grosser | 9a38ab8 | 2011-11-08 15:41:03 +0000 | [diff] [blame] | 1778 | isl_ctx *Scop::getIslCtx() const { return IslCtx; } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1779 | |
Johannes Doerfert | 574182d | 2015-08-12 10:19:50 +0000 | [diff] [blame] | 1780 | __isl_give isl_pw_aff *Scop::getPwAff(const SCEV *E, ScopStmt *Stmt) { |
| 1781 | return Affinator.getPwAff(E, Stmt); |
| 1782 | } |
| 1783 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1784 | __isl_give isl_union_set *Scop::getDomains() const { |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1785 | isl_union_set *Domain = isl_union_set_empty(getParamSpace()); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 1786 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1787 | for (const ScopStmt &Stmt : *this) |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1788 | Domain = isl_union_set_add_set(Domain, Stmt.getDomain()); |
Tobias Grosser | 5f9a762 | 2012-02-14 14:02:40 +0000 | [diff] [blame] | 1789 | |
| 1790 | return Domain; |
| 1791 | } |
| 1792 | |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 1793 | __isl_give isl_union_map *Scop::getMustWrites() { |
Tobias Grosser | eeb9f3c | 2015-05-26 21:37:31 +0000 | [diff] [blame] | 1794 | isl_union_map *Write = isl_union_map_empty(getParamSpace()); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 1795 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1796 | for (ScopStmt &Stmt : *this) { |
| 1797 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 1798 | if (!MA->isMustWrite()) |
| 1799 | continue; |
| 1800 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1801 | isl_set *Domain = Stmt.getDomain(); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 1802 | isl_map *AccessDomain = MA->getAccessRelation(); |
| 1803 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
| 1804 | Write = isl_union_map_add_map(Write, AccessDomain); |
| 1805 | } |
| 1806 | } |
| 1807 | return isl_union_map_coalesce(Write); |
| 1808 | } |
| 1809 | |
| 1810 | __isl_give isl_union_map *Scop::getMayWrites() { |
Tobias Grosser | eeb9f3c | 2015-05-26 21:37:31 +0000 | [diff] [blame] | 1811 | isl_union_map *Write = isl_union_map_empty(getParamSpace()); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 1812 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1813 | for (ScopStmt &Stmt : *this) { |
| 1814 | for (MemoryAccess *MA : Stmt) { |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 1815 | if (!MA->isMayWrite()) |
| 1816 | continue; |
| 1817 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1818 | isl_set *Domain = Stmt.getDomain(); |
Tobias Grosser | 780ce0f | 2014-07-11 07:12:10 +0000 | [diff] [blame] | 1819 | isl_map *AccessDomain = MA->getAccessRelation(); |
| 1820 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
| 1821 | Write = isl_union_map_add_map(Write, AccessDomain); |
| 1822 | } |
| 1823 | } |
| 1824 | return isl_union_map_coalesce(Write); |
| 1825 | } |
| 1826 | |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1827 | __isl_give isl_union_map *Scop::getWrites() { |
Tobias Grosser | eeb9f3c | 2015-05-26 21:37:31 +0000 | [diff] [blame] | 1828 | isl_union_map *Write = isl_union_map_empty(getParamSpace()); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1829 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1830 | for (ScopStmt &Stmt : *this) { |
| 1831 | for (MemoryAccess *MA : Stmt) { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1832 | if (!MA->isWrite()) |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1833 | continue; |
| 1834 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1835 | isl_set *Domain = Stmt.getDomain(); |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1836 | isl_map *AccessDomain = MA->getAccessRelation(); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1837 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
| 1838 | Write = isl_union_map_add_map(Write, AccessDomain); |
| 1839 | } |
| 1840 | } |
| 1841 | return isl_union_map_coalesce(Write); |
| 1842 | } |
| 1843 | |
| 1844 | __isl_give isl_union_map *Scop::getReads() { |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1845 | isl_union_map *Read = isl_union_map_empty(getParamSpace()); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1846 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1847 | for (ScopStmt &Stmt : *this) { |
| 1848 | for (MemoryAccess *MA : Stmt) { |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1849 | if (!MA->isRead()) |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1850 | continue; |
| 1851 | |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1852 | isl_set *Domain = Stmt.getDomain(); |
Johannes Doerfert | f675289 | 2014-06-13 18:01:45 +0000 | [diff] [blame] | 1853 | isl_map *AccessDomain = MA->getAccessRelation(); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1854 | |
| 1855 | AccessDomain = isl_map_intersect_domain(AccessDomain, Domain); |
| 1856 | Read = isl_union_map_add_map(Read, AccessDomain); |
| 1857 | } |
| 1858 | } |
| 1859 | return isl_union_map_coalesce(Read); |
| 1860 | } |
| 1861 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1862 | __isl_give isl_union_map *Scop::getSchedule() const { |
| 1863 | auto Tree = getScheduleTree(); |
| 1864 | auto S = isl_schedule_get_map(Tree); |
| 1865 | isl_schedule_free(Tree); |
| 1866 | return S; |
| 1867 | } |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1868 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1869 | __isl_give isl_schedule *Scop::getScheduleTree() const { |
| 1870 | return isl_schedule_intersect_domain(isl_schedule_copy(Schedule), |
| 1871 | getDomains()); |
| 1872 | } |
Tobias Grosser | bc4ef90 | 2014-06-28 08:59:38 +0000 | [diff] [blame] | 1873 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1874 | void Scop::setSchedule(__isl_take isl_union_map *NewSchedule) { |
| 1875 | auto *S = isl_schedule_from_domain(getDomains()); |
| 1876 | S = isl_schedule_insert_partial_schedule( |
| 1877 | S, isl_multi_union_pw_aff_from_union_map(NewSchedule)); |
| 1878 | isl_schedule_free(Schedule); |
| 1879 | Schedule = S; |
| 1880 | } |
| 1881 | |
| 1882 | void Scop::setScheduleTree(__isl_take isl_schedule *NewSchedule) { |
| 1883 | isl_schedule_free(Schedule); |
| 1884 | Schedule = NewSchedule; |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | bool Scop::restrictDomains(__isl_take isl_union_set *Domain) { |
| 1888 | bool Changed = false; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1889 | for (ScopStmt &Stmt : *this) { |
| 1890 | isl_union_set *StmtDomain = isl_union_set_from_set(Stmt.getDomain()); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1891 | isl_union_set *NewStmtDomain = isl_union_set_intersect( |
| 1892 | isl_union_set_copy(StmtDomain), isl_union_set_copy(Domain)); |
| 1893 | |
| 1894 | if (isl_union_set_is_subset(StmtDomain, NewStmtDomain)) { |
| 1895 | isl_union_set_free(StmtDomain); |
| 1896 | isl_union_set_free(NewStmtDomain); |
| 1897 | continue; |
| 1898 | } |
| 1899 | |
| 1900 | Changed = true; |
| 1901 | |
| 1902 | isl_union_set_free(StmtDomain); |
| 1903 | NewStmtDomain = isl_union_set_coalesce(NewStmtDomain); |
| 1904 | |
| 1905 | if (isl_union_set_is_empty(NewStmtDomain)) { |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1906 | Stmt.restrictDomain(isl_set_empty(Stmt.getDomainSpace())); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1907 | isl_union_set_free(NewStmtDomain); |
| 1908 | } else |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 1909 | Stmt.restrictDomain(isl_set_from_union_set(NewStmtDomain)); |
Tobias Grosser | 37eb422 | 2014-02-20 21:43:54 +0000 | [diff] [blame] | 1910 | } |
| 1911 | isl_union_set_free(Domain); |
| 1912 | return Changed; |
| 1913 | } |
| 1914 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1915 | ScalarEvolution *Scop::getSE() const { return SE; } |
| 1916 | |
| 1917 | bool Scop::isTrivialBB(BasicBlock *BB, TempScop &tempScop) { |
| 1918 | if (tempScop.getAccessFunctions(BB)) |
| 1919 | return false; |
| 1920 | |
| 1921 | return true; |
| 1922 | } |
| 1923 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1924 | struct MapToDimensionDataTy { |
| 1925 | int N; |
| 1926 | isl_union_pw_multi_aff *Res; |
| 1927 | }; |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 1928 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1929 | // @brief Create a function that maps the elements of 'Set' to its N-th |
| 1930 | // dimension. |
| 1931 | // |
| 1932 | // The result is added to 'User->Res'. |
| 1933 | // |
| 1934 | // @param Set The input set. |
| 1935 | // @param N The dimension to map to. |
| 1936 | // |
| 1937 | // @returns Zero if no error occurred, non-zero otherwise. |
| 1938 | static isl_stat mapToDimension_AddSet(__isl_take isl_set *Set, void *User) { |
| 1939 | struct MapToDimensionDataTy *Data = (struct MapToDimensionDataTy *)User; |
| 1940 | int Dim; |
| 1941 | isl_space *Space; |
| 1942 | isl_pw_multi_aff *PMA; |
| 1943 | |
| 1944 | Dim = isl_set_dim(Set, isl_dim_set); |
| 1945 | Space = isl_set_get_space(Set); |
| 1946 | PMA = isl_pw_multi_aff_project_out_map(Space, isl_dim_set, Data->N, |
| 1947 | Dim - Data->N); |
| 1948 | if (Data->N > 1) |
| 1949 | PMA = isl_pw_multi_aff_drop_dims(PMA, isl_dim_out, 0, Data->N - 1); |
| 1950 | Data->Res = isl_union_pw_multi_aff_add_pw_multi_aff(Data->Res, PMA); |
| 1951 | |
| 1952 | isl_set_free(Set); |
| 1953 | |
| 1954 | return isl_stat_ok; |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 1957 | // @brief Create a function that maps the elements of Domain to their Nth |
| 1958 | // dimension. |
| 1959 | // |
| 1960 | // @param Domain The set of elements to map. |
| 1961 | // @param N The dimension to map to. |
| 1962 | static __isl_give isl_multi_union_pw_aff * |
| 1963 | mapToDimension(__isl_take isl_union_set *Domain, int N) { |
| 1964 | struct MapToDimensionDataTy Data; |
| 1965 | isl_space *Space; |
| 1966 | |
| 1967 | Space = isl_union_set_get_space(Domain); |
| 1968 | Data.N = N; |
| 1969 | Data.Res = isl_union_pw_multi_aff_empty(Space); |
| 1970 | if (isl_union_set_foreach_set(Domain, &mapToDimension_AddSet, &Data) < 0) |
| 1971 | Data.Res = isl_union_pw_multi_aff_free(Data.Res); |
| 1972 | |
| 1973 | isl_union_set_free(Domain); |
| 1974 | return isl_multi_union_pw_aff_from_union_pw_multi_aff(Data.Res); |
| 1975 | } |
| 1976 | |
| 1977 | ScopStmt *Scop::addScopStmt(BasicBlock *BB, Region *R, TempScop &tempScop, |
| 1978 | const Region &CurRegion, |
| 1979 | SmallVectorImpl<Loop *> &NestLoops) { |
| 1980 | ScopStmt *Stmt; |
| 1981 | if (BB) { |
| 1982 | Stmts.emplace_back(*this, tempScop, CurRegion, *BB, NestLoops); |
| 1983 | Stmt = &Stmts.back(); |
| 1984 | StmtMap[BB] = Stmt; |
| 1985 | } else { |
| 1986 | assert(R && "Either basic block or a region expected."); |
| 1987 | Stmts.emplace_back(*this, tempScop, CurRegion, *R, NestLoops); |
| 1988 | Stmt = &Stmts.back(); |
| 1989 | for (BasicBlock *BB : R->blocks()) |
| 1990 | StmtMap[BB] = Stmt; |
| 1991 | } |
| 1992 | return Stmt; |
| 1993 | } |
| 1994 | |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 1995 | __isl_give isl_schedule * |
| 1996 | Scop::buildBBScopStmt(BasicBlock *BB, TempScop &tempScop, |
| 1997 | const Region &CurRegion, |
| 1998 | SmallVectorImpl<Loop *> &NestLoops) { |
| 1999 | if (isTrivialBB(BB, tempScop)) |
| 2000 | return nullptr; |
| 2001 | |
| 2002 | auto *Stmt = addScopStmt(BB, nullptr, tempScop, CurRegion, NestLoops); |
| 2003 | auto *Domain = Stmt->getDomain(); |
| 2004 | return isl_schedule_from_domain(isl_union_set_from_set(Domain)); |
| 2005 | } |
| 2006 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2007 | __isl_give isl_schedule *Scop::buildScop(TempScop &tempScop, |
| 2008 | const Region &CurRegion, |
| 2009 | SmallVectorImpl<Loop *> &NestLoops, |
| 2010 | LoopInfo &LI, ScopDetection &SD) { |
| 2011 | if (SD.isNonAffineSubRegion(&CurRegion, &getRegion())) { |
| 2012 | auto *Stmt = addScopStmt(nullptr, const_cast<Region *>(&CurRegion), |
| 2013 | tempScop, CurRegion, NestLoops); |
| 2014 | auto *Domain = Stmt->getDomain(); |
| 2015 | return isl_schedule_from_domain(isl_union_set_from_set(Domain)); |
| 2016 | } |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 2017 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2018 | Loop *L = castToLoop(CurRegion, LI); |
| 2019 | |
| 2020 | if (L) |
| 2021 | NestLoops.push_back(L); |
| 2022 | |
| 2023 | unsigned loopDepth = NestLoops.size(); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2024 | isl_schedule *Schedule = nullptr; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2025 | |
| 2026 | for (Region::const_element_iterator I = CurRegion.element_begin(), |
Tobias Grosser | abfbe63 | 2013-02-05 12:09:06 +0000 | [diff] [blame] | 2027 | E = CurRegion.element_end(); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2028 | I != E; ++I) { |
| 2029 | isl_schedule *StmtSchedule = nullptr; |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 2030 | if (I->isSubRegion()) { |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2031 | StmtSchedule = |
| 2032 | buildScop(tempScop, *I->getNodeAs<Region>(), NestLoops, LI, SD); |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 2033 | } else { |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 2034 | StmtSchedule = buildBBScopStmt(I->getNodeAs<BasicBlock>(), tempScop, |
| 2035 | CurRegion, NestLoops); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2036 | } |
Michael Kruse | 046dde4 | 2015-08-10 13:01:57 +0000 | [diff] [blame] | 2037 | Schedule = combineInSequence(Schedule, StmtSchedule); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2038 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2039 | |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2040 | if (!L) |
| 2041 | return Schedule; |
| 2042 | |
| 2043 | auto *Domain = isl_schedule_get_domain(Schedule); |
| 2044 | if (!isl_union_set_is_empty(Domain)) { |
| 2045 | auto *MUPA = mapToDimension(isl_union_set_copy(Domain), loopDepth); |
| 2046 | Schedule = isl_schedule_insert_partial_schedule(Schedule, MUPA); |
| 2047 | } |
| 2048 | isl_union_set_free(Domain); |
| 2049 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2050 | NestLoops.pop_back(); |
Tobias Grosser | 808cd69 | 2015-07-14 09:33:13 +0000 | [diff] [blame] | 2051 | return Schedule; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
Johannes Doerfert | 7c49421 | 2014-10-31 23:13:39 +0000 | [diff] [blame] | 2054 | ScopStmt *Scop::getStmtForBasicBlock(BasicBlock *BB) const { |
Tobias Grosser | 57411e3 | 2015-05-27 06:51:34 +0000 | [diff] [blame] | 2055 | auto StmtMapIt = StmtMap.find(BB); |
Johannes Doerfert | 7c49421 | 2014-10-31 23:13:39 +0000 | [diff] [blame] | 2056 | if (StmtMapIt == StmtMap.end()) |
| 2057 | return nullptr; |
| 2058 | return StmtMapIt->second; |
| 2059 | } |
| 2060 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2061 | //===----------------------------------------------------------------------===// |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 2062 | ScopInfo::ScopInfo() : RegionPass(ID), scop(0) { |
| 2063 | ctx = isl_ctx_alloc(); |
Tobias Grosser | 4a8e356 | 2011-12-07 07:42:51 +0000 | [diff] [blame] | 2064 | isl_options_set_on_error(ctx, ISL_ON_ERROR_ABORT); |
Tobias Grosser | b76f3853 | 2011-08-20 11:11:25 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | ScopInfo::~ScopInfo() { |
| 2068 | clear(); |
| 2069 | isl_ctx_free(ctx); |
| 2070 | } |
| 2071 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2072 | void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 2073 | AU.addRequired<LoopInfoWrapperPass>(); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 2074 | AU.addRequired<RegionInfoPass>(); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 2075 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 2076 | AU.addRequired<ScopDetection>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2077 | AU.addRequired<TempScopInfo>(); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2078 | AU.addRequired<AliasAnalysis>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2079 | AU.setPreservesAll(); |
| 2080 | } |
| 2081 | |
| 2082 | bool ScopInfo::runOnRegion(Region *R, RGPassManager &RGM) { |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 2083 | LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2084 | AliasAnalysis &AA = getAnalysis<AliasAnalysis>(); |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 2085 | ScopDetection &SD = getAnalysis<ScopDetection>(); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 2086 | ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2087 | |
Michael Kruse | 82a1c7d | 2015-08-14 20:10:27 +0000 | [diff] [blame] | 2088 | TempScop *tempScop = getAnalysis<TempScopInfo>().getTempScop(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2089 | |
| 2090 | // This region is no Scop. |
| 2091 | if (!tempScop) { |
Tobias Grosser | c98a8fc | 2014-11-14 11:12:31 +0000 | [diff] [blame] | 2092 | scop = nullptr; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2093 | return false; |
| 2094 | } |
| 2095 | |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 2096 | scop = Scop::createFromTempScop(*tempScop, LI, SE, SD, AA, ctx); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2097 | |
Tobias Grosser | d6a50b3 | 2015-05-30 06:26:21 +0000 | [diff] [blame] | 2098 | DEBUG(scop->print(dbgs())); |
| 2099 | |
Johannes Doerfert | 5d5b306 | 2015-08-20 18:06:30 +0000 | [diff] [blame] | 2100 | if (!scop->hasFeasibleRuntimeContext()) { |
Johannes Doerfert | 43788c5 | 2015-08-20 05:58:56 +0000 | [diff] [blame] | 2101 | delete scop; |
| 2102 | scop = nullptr; |
| 2103 | return false; |
| 2104 | } |
| 2105 | |
Johannes Doerfert | 120de4b | 2015-08-20 18:30:08 +0000 | [diff] [blame] | 2106 | // Statistics. |
| 2107 | ++ScopFound; |
| 2108 | if (scop->getMaxLoopDepth() > 0) |
| 2109 | ++RichScopFound; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 2110 | return false; |
| 2111 | } |
| 2112 | |
| 2113 | char ScopInfo::ID = 0; |
| 2114 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 2115 | Pass *polly::createScopInfoPass() { return new ScopInfo(); } |
| 2116 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 2117 | INITIALIZE_PASS_BEGIN(ScopInfo, "polly-scops", |
| 2118 | "Polly - Create polyhedral description of Scops", false, |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 2119 | false); |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 +0000 | [diff] [blame] | 2120 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis); |
Chandler Carruth | f557987 | 2015-01-17 14:16:56 +0000 | [diff] [blame] | 2121 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame] | 2122 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | c5bcf24 | 2015-08-17 10:57:08 +0000 | [diff] [blame] | 2123 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); |
Johannes Doerfert | ff9d198 | 2015-02-24 12:00:50 +0000 | [diff] [blame] | 2124 | INITIALIZE_PASS_DEPENDENCY(ScopDetection); |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 2125 | INITIALIZE_PASS_DEPENDENCY(TempScopInfo); |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 2126 | INITIALIZE_PASS_END(ScopInfo, "polly-scops", |
| 2127 | "Polly - Create polyhedral description of Scops", false, |
| 2128 | false) |