Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 1 | //===------ ZoneAlgo.cpp ----------------------------------------*- C++ -*-===// |
| 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 | // Derive information about array elements between statements ("Zones"). |
| 11 | // |
| 12 | // The algorithms here work on the scatter space - the image space of the |
| 13 | // schedule returned by Scop::getSchedule(). We call an element in that space a |
| 14 | // "timepoint". Timepoints are lexicographically ordered such that we can |
| 15 | // defined ranges in the scatter space. We use two flavors of such ranges: |
| 16 | // Timepoint sets and zones. A timepoint set is simply a subset of the scatter |
| 17 | // space and is directly stored as isl_set. |
| 18 | // |
| 19 | // Zones are used to describe the space between timepoints as open sets, i.e. |
| 20 | // they do not contain the extrema. Using isl rational sets to express these |
| 21 | // would be overkill. We also cannot store them as the integer timepoints they |
| 22 | // contain; the (nonempty) zone between 1 and 2 would be empty and |
| 23 | // indistinguishable from e.g. the zone between 3 and 4. Also, we cannot store |
| 24 | // the integer set including the extrema; the set ]1,2[ + ]3,4[ could be |
| 25 | // coalesced to ]1,3[, although we defined the range [2,3] to be not in the set. |
| 26 | // Instead, we store the "half-open" integer extrema, including the lower bound, |
| 27 | // but excluding the upper bound. Examples: |
| 28 | // |
| 29 | // * The set { [i] : 1 <= i <= 3 } represents the zone ]0,3[ (which contains the |
| 30 | // integer points 1 and 2, but not 0 or 3) |
| 31 | // |
| 32 | // * { [1] } represents the zone ]0,1[ |
| 33 | // |
| 34 | // * { [i] : i = 1 or i = 3 } represents the zone ]0,1[ + ]2,3[ |
| 35 | // |
| 36 | // Therefore, an integer i in the set represents the zone ]i-1,i[, i.e. strictly |
| 37 | // speaking the integer points never belong to the zone. However, depending an |
| 38 | // the interpretation, one might want to include them. Part of the |
| 39 | // interpretation may not be known when the zone is constructed. |
| 40 | // |
| 41 | // Reads are assumed to always take place before writes, hence we can think of |
| 42 | // reads taking place at the beginning of a timepoint and writes at the end. |
| 43 | // |
| 44 | // Let's assume that the zone represents the lifetime of a variable. That is, |
| 45 | // the zone begins with a write that defines the value during its lifetime and |
| 46 | // ends with the last read of that value. In the following we consider whether a |
| 47 | // read/write at the beginning/ending of the lifetime zone should be within the |
| 48 | // zone or outside of it. |
| 49 | // |
| 50 | // * A read at the timepoint that starts the live-range loads the previous |
| 51 | // value. Hence, exclude the timepoint starting the zone. |
| 52 | // |
| 53 | // * A write at the timepoint that starts the live-range is not defined whether |
| 54 | // it occurs before or after the write that starts the lifetime. We do not |
| 55 | // allow this situation to occur. Hence, we include the timepoint starting the |
| 56 | // zone to determine whether they are conflicting. |
| 57 | // |
| 58 | // * A read at the timepoint that ends the live-range reads the same variable. |
| 59 | // We include the timepoint at the end of the zone to include that read into |
| 60 | // the live-range. Doing otherwise would mean that the two reads access |
| 61 | // different values, which would mean that the value they read are both alive |
| 62 | // at the same time but occupy the same variable. |
| 63 | // |
| 64 | // * A write at the timepoint that ends the live-range starts a new live-range. |
| 65 | // It must not be included in the live-range of the previous definition. |
| 66 | // |
| 67 | // All combinations of reads and writes at the endpoints are possible, but most |
| 68 | // of the time only the write->read (for instance, a live-range from definition |
| 69 | // to last use) and read->write (for instance, an unused range from last use to |
| 70 | // overwrite) and combinations are interesting (half-open ranges). write->write |
| 71 | // zones might be useful as well in some context to represent |
| 72 | // output-dependencies. |
| 73 | // |
| 74 | // @see convertZoneToTimepoints |
| 75 | // |
| 76 | // |
| 77 | // The code makes use of maps and sets in many different spaces. To not loose |
| 78 | // track in which space a set or map is expected to be in, variables holding an |
| 79 | // isl reference are usually annotated in the comments. They roughly follow isl |
| 80 | // syntax for spaces, but only the tuples, not the dimensions. The tuples have a |
| 81 | // meaning as follows: |
| 82 | // |
| 83 | // * Space[] - An unspecified tuple. Used for function parameters such that the |
| 84 | // function caller can use it for anything they like. |
| 85 | // |
| 86 | // * Domain[] - A statement instance as returned by ScopStmt::getDomain() |
| 87 | // isl_id_get_name: Stmt_<NameOfBasicBlock> |
| 88 | // isl_id_get_user: Pointer to ScopStmt |
| 89 | // |
| 90 | // * Element[] - An array element as in the range part of |
| 91 | // MemoryAccess::getAccessRelation() |
| 92 | // isl_id_get_name: MemRef_<NameOfArrayVariable> |
| 93 | // isl_id_get_user: Pointer to ScopArrayInfo |
| 94 | // |
| 95 | // * Scatter[] - Scatter space or space of timepoints |
| 96 | // Has no tuple id |
| 97 | // |
| 98 | // * Zone[] - Range between timepoints as described above |
| 99 | // Has no tuple id |
| 100 | // |
| 101 | // * ValInst[] - An llvm::Value as defined at a specific timepoint. |
| 102 | // |
| 103 | // A ValInst[] itself can be structured as one of: |
| 104 | // |
| 105 | // * [] - An unknown value. |
| 106 | // Always zero dimensions |
| 107 | // Has no tuple id |
| 108 | // |
| 109 | // * Value[] - An llvm::Value that is read-only in the SCoP, i.e. its |
| 110 | // runtime content does not depend on the timepoint. |
| 111 | // Always zero dimensions |
| 112 | // isl_id_get_name: Val_<NameOfValue> |
| 113 | // isl_id_get_user: A pointer to an llvm::Value |
| 114 | // |
| 115 | // * SCEV[...] - A synthesizable llvm::SCEV Expression. |
| 116 | // In contrast to a Value[] is has at least one dimension per |
| 117 | // SCEVAddRecExpr in the SCEV. |
| 118 | // |
| 119 | // * [Domain[] -> Value[]] - An llvm::Value that may change during the |
| 120 | // Scop's execution. |
| 121 | // The tuple itself has no id, but it wraps a map space holding a |
| 122 | // statement instance which defines the llvm::Value as the map's domain |
| 123 | // and llvm::Value itself as range. |
| 124 | // |
| 125 | // @see makeValInst() |
| 126 | // |
| 127 | // An annotation "{ Domain[] -> Scatter[] }" therefore means: A map from a |
| 128 | // statement instance to a timepoint, aka a schedule. There is only one scatter |
| 129 | // space, but most of the time multiple statements are processed in one set. |
| 130 | // This is why most of the time isl_union_map has to be used. |
| 131 | // |
| 132 | // The basic algorithm works as follows: |
| 133 | // At first we verify that the SCoP is compatible with this technique. For |
| 134 | // instance, two writes cannot write to the same location at the same statement |
| 135 | // instance because we cannot determine within the polyhedral model which one |
| 136 | // comes first. Once this was verified, we compute zones at which an array |
| 137 | // element is unused. This computation can fail if it takes too long. Then the |
| 138 | // main algorithm is executed. Because every store potentially trails an unused |
| 139 | // zone, we start at stores. We search for a scalar (MemoryKind::Value or |
| 140 | // MemoryKind::PHI) that we can map to the array element overwritten by the |
| 141 | // store, preferably one that is used by the store or at least the ScopStmt. |
| 142 | // When it does not conflict with the lifetime of the values in the array |
| 143 | // element, the map is applied and the unused zone updated as it is now used. We |
| 144 | // continue to try to map scalars to the array element until there are no more |
| 145 | // candidates to map. The algorithm is greedy in the sense that the first scalar |
| 146 | // not conflicting will be mapped. Other scalars processed later that could have |
| 147 | // fit the same unused zone will be rejected. As such the result depends on the |
| 148 | // processing order. |
| 149 | // |
| 150 | //===----------------------------------------------------------------------===// |
| 151 | |
| 152 | #include "polly/ZoneAlgo.h" |
| 153 | #include "polly/ScopInfo.h" |
| 154 | #include "polly/Support/GICHelper.h" |
| 155 | #include "polly/Support/ISLTools.h" |
| 156 | #include "polly/Support/VirtualInstruction.h" |
Michael Kruse | 4728184 | 2017-08-28 20:39:07 +0000 | [diff] [blame] | 157 | #include "llvm/ADT/Statistic.h" |
Zhaoshi Zheng | ceec175 | 2017-11-17 22:05:19 +0000 | [diff] [blame] | 158 | #include "llvm/Support/raw_ostream.h" |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 159 | |
| 160 | #define DEBUG_TYPE "polly-zone" |
| 161 | |
Michael Kruse | 4728184 | 2017-08-28 20:39:07 +0000 | [diff] [blame] | 162 | STATISTIC(NumIncompatibleArrays, "Number of not zone-analyzable arrays"); |
| 163 | STATISTIC(NumCompatibleArrays, "Number of zone-analyzable arrays"); |
Michael Kruse | 68821a8 | 2017-10-31 16:11:46 +0000 | [diff] [blame] | 164 | STATISTIC(NumRecursivePHIs, "Number of recursive PHIs"); |
| 165 | STATISTIC(NumNormalizablePHIs, "Number of normalizable PHIs"); |
| 166 | STATISTIC(NumPHINormialization, "Number of PHI executed normalizations"); |
Michael Kruse | 4728184 | 2017-08-28 20:39:07 +0000 | [diff] [blame] | 167 | |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 168 | using namespace polly; |
| 169 | using namespace llvm; |
| 170 | |
| 171 | static isl::union_map computeReachingDefinition(isl::union_map Schedule, |
| 172 | isl::union_map Writes, |
| 173 | bool InclDef, bool InclRedef) { |
| 174 | return computeReachingWrite(Schedule, Writes, false, InclDef, InclRedef); |
| 175 | } |
| 176 | |
| 177 | /// Compute the reaching definition of a scalar. |
| 178 | /// |
| 179 | /// Compared to computeReachingDefinition, there is just one element which is |
| 180 | /// accessed and therefore only a set if instances that accesses that element is |
| 181 | /// required. |
| 182 | /// |
| 183 | /// @param Schedule { DomainWrite[] -> Scatter[] } |
| 184 | /// @param Writes { DomainWrite[] } |
| 185 | /// @param InclDef Include the timepoint of the definition to the result. |
| 186 | /// @param InclRedef Include the timepoint of the overwrite into the result. |
| 187 | /// |
| 188 | /// @return { Scatter[] -> DomainWrite[] } |
| 189 | static isl::union_map computeScalarReachingDefinition(isl::union_map Schedule, |
| 190 | isl::union_set Writes, |
| 191 | bool InclDef, |
| 192 | bool InclRedef) { |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 193 | // { DomainWrite[] -> Element[] } |
Tobias Grosser | 0dd4251 | 2017-08-21 14:19:40 +0000 | [diff] [blame] | 194 | isl::union_map Defs = isl::union_map::from_domain(Writes); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 195 | |
| 196 | // { [Element[] -> Scatter[]] -> DomainWrite[] } |
| 197 | auto ReachDefs = |
| 198 | computeReachingDefinition(Schedule, Defs, InclDef, InclRedef); |
| 199 | |
| 200 | // { Scatter[] -> DomainWrite[] } |
Tobias Grosser | 0dd4251 | 2017-08-21 14:19:40 +0000 | [diff] [blame] | 201 | return ReachDefs.curry().range().unwrap(); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /// Compute the reaching definition of a scalar. |
| 205 | /// |
| 206 | /// This overload accepts only a single writing statement as an isl_map, |
| 207 | /// consequently the result also is only a single isl_map. |
| 208 | /// |
| 209 | /// @param Schedule { DomainWrite[] -> Scatter[] } |
| 210 | /// @param Writes { DomainWrite[] } |
| 211 | /// @param InclDef Include the timepoint of the definition to the result. |
| 212 | /// @param InclRedef Include the timepoint of the overwrite into the result. |
| 213 | /// |
| 214 | /// @return { Scatter[] -> DomainWrite[] } |
| 215 | static isl::map computeScalarReachingDefinition(isl::union_map Schedule, |
| 216 | isl::set Writes, bool InclDef, |
| 217 | bool InclRedef) { |
Tobias Grosser | 0dd4251 | 2017-08-21 14:19:40 +0000 | [diff] [blame] | 218 | isl::space DomainSpace = Writes.get_space(); |
| 219 | isl::space ScatterSpace = getScatterSpace(Schedule); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 220 | |
| 221 | // { Scatter[] -> DomainWrite[] } |
Tobias Grosser | 0dd4251 | 2017-08-21 14:19:40 +0000 | [diff] [blame] | 222 | isl::union_map UMap = computeScalarReachingDefinition( |
| 223 | Schedule, isl::union_set(Writes), InclDef, InclRedef); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 224 | |
Tobias Grosser | 0dd4251 | 2017-08-21 14:19:40 +0000 | [diff] [blame] | 225 | isl::space ResultSpace = ScatterSpace.map_from_domain_and_range(DomainSpace); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 226 | return singleton(UMap, ResultSpace); |
| 227 | } |
| 228 | |
| 229 | isl::union_map polly::makeUnknownForDomain(isl::union_set Domain) { |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 230 | return isl::union_map::from_domain(Domain); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /// Create a domain-to-unknown value mapping. |
| 234 | /// |
| 235 | /// @see makeUnknownForDomain(isl::union_set) |
| 236 | /// |
| 237 | /// @param Domain { Domain[] } |
| 238 | /// |
| 239 | /// @return { Domain[] -> ValInst[] } |
| 240 | static isl::map makeUnknownForDomain(isl::set Domain) { |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 241 | return isl::map::from_domain(Domain); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 244 | /// Return whether @p Map maps to an unknown value. |
| 245 | /// |
| 246 | /// @param { [] -> ValInst[] } |
| 247 | static bool isMapToUnknown(const isl::map &Map) { |
| 248 | isl::space Space = Map.get_space().range(); |
| 249 | return Space.has_tuple_id(isl::dim::set).is_false() && |
| 250 | Space.is_wrapping().is_false() && Space.dim(isl::dim::set) == 0; |
| 251 | } |
| 252 | |
Michael Kruse | ce67358 | 2017-08-08 17:00:27 +0000 | [diff] [blame] | 253 | isl::union_map polly::filterKnownValInst(const isl::union_map &UMap) { |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 254 | isl::union_map Result = isl::union_map::empty(UMap.get_space()); |
Michael Kruse | 630fc7b | 2017-08-09 11:21:40 +0000 | [diff] [blame] | 255 | isl::stat Success = UMap.foreach_map([=, &Result](isl::map Map) -> isl::stat { |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 256 | if (!isMapToUnknown(Map)) |
| 257 | Result = Result.add_map(Map); |
| 258 | return isl::stat::ok; |
| 259 | }); |
Michael Kruse | 630fc7b | 2017-08-09 11:21:40 +0000 | [diff] [blame] | 260 | if (Success != isl::stat::ok) |
| 261 | return {}; |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 262 | return Result; |
| 263 | } |
| 264 | |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 265 | ZoneAlgorithm::ZoneAlgorithm(const char *PassName, Scop *S, LoopInfo *LI) |
| 266 | : PassName(PassName), IslCtx(S->getSharedIslCtx()), S(S), LI(LI), |
Tobias Grosser | 61bd3a4 | 2017-08-06 21:42:38 +0000 | [diff] [blame] | 267 | Schedule(S->getSchedule()) { |
Tobias Grosser | 31df6f3 | 2017-08-06 21:42:25 +0000 | [diff] [blame] | 268 | auto Domains = S->getDomains(); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 269 | |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 270 | Schedule = Schedule.intersect_domain(Domains); |
| 271 | ParamSpace = Schedule.get_space(); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 272 | ScatterSpace = getScatterSpace(Schedule); |
| 273 | } |
| 274 | |
Tobias Grosser | 2ef3781 | 2017-08-07 22:01:29 +0000 | [diff] [blame] | 275 | /// Check if all stores in @p Stmt store the very same value. |
| 276 | /// |
Michael Kruse | 8756b3f | 2017-08-09 09:29:15 +0000 | [diff] [blame] | 277 | /// This covers a special situation occurring in Polybench's |
| 278 | /// covariance/correlation (which is typical for algorithms that cover symmetric |
| 279 | /// matrices): |
| 280 | /// |
| 281 | /// for (int i = 0; i < n; i += 1) |
| 282 | /// for (int j = 0; j <= i; j += 1) { |
| 283 | /// double x = ...; |
| 284 | /// C[i][j] = x; |
| 285 | /// C[j][i] = x; |
| 286 | /// } |
| 287 | /// |
| 288 | /// For i == j, the same value is written twice to the same element.Double |
| 289 | /// writes to the same element are not allowed in DeLICM because its algorithm |
| 290 | /// does not see which of the writes is effective.But if its the same value |
| 291 | /// anyway, it doesn't matter. |
| 292 | /// |
| 293 | /// LLVM passes, however, cannot simplify this because the write is necessary |
| 294 | /// for i != j (unless it would add a condition for one of the writes to occur |
| 295 | /// only if i != j). |
| 296 | /// |
Tobias Grosser | 2ef3781 | 2017-08-07 22:01:29 +0000 | [diff] [blame] | 297 | /// TODO: In the future we may want to extent this to make the checks |
| 298 | /// specific to different memory locations. |
| 299 | static bool onlySameValueWrites(ScopStmt *Stmt) { |
| 300 | Value *V = nullptr; |
| 301 | |
| 302 | for (auto *MA : *Stmt) { |
| 303 | if (!MA->isLatestArrayKind() || !MA->isMustWrite() || |
| 304 | !MA->isOriginalArrayKind()) |
| 305 | continue; |
| 306 | |
| 307 | if (!V) { |
| 308 | V = MA->getAccessValue(); |
| 309 | continue; |
| 310 | } |
| 311 | |
| 312 | if (V != MA->getAccessValue()) |
| 313 | return false; |
| 314 | } |
| 315 | return true; |
| 316 | } |
| 317 | |
Michael Kruse | 4728184 | 2017-08-28 20:39:07 +0000 | [diff] [blame] | 318 | void ZoneAlgorithm::collectIncompatibleElts(ScopStmt *Stmt, |
| 319 | isl::union_set &IncompatibleElts, |
| 320 | isl::union_set &AllElts) { |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 321 | auto Stores = makeEmptyUnionMap(); |
| 322 | auto Loads = makeEmptyUnionMap(); |
| 323 | |
| 324 | // This assumes that the MemoryKind::Array MemoryAccesses are iterated in |
| 325 | // order. |
| 326 | for (auto *MA : *Stmt) { |
Michael Kruse | ff426d9 | 2017-10-31 12:50:25 +0000 | [diff] [blame] | 327 | if (!MA->isOriginalArrayKind()) |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 328 | continue; |
| 329 | |
Michael Kruse | 4728184 | 2017-08-28 20:39:07 +0000 | [diff] [blame] | 330 | isl::map AccRelMap = getAccessRelationFor(MA); |
| 331 | isl::union_map AccRel = AccRelMap; |
| 332 | |
| 333 | // To avoid solving any ILP problems, always add entire arrays instead of |
| 334 | // just the elements that are accessed. |
| 335 | auto ArrayElts = isl::set::universe(AccRelMap.get_space().range()); |
| 336 | AllElts = AllElts.add_set(ArrayElts); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 337 | |
| 338 | if (MA->isRead()) { |
| 339 | // Reject load after store to same location. |
Tobias Grosser | d3d3d6b | 2018-04-29 00:28:26 +0000 | [diff] [blame] | 340 | if (!Stores.is_disjoint(AccRel)) { |
Nicola Zaghen | 349506a | 2018-05-15 13:37:17 +0000 | [diff] [blame] | 341 | LLVM_DEBUG( |
| 342 | dbgs() << "Load after store of same element in same statement\n"); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 343 | OptimizationRemarkMissed R(PassName, "LoadAfterStore", |
| 344 | MA->getAccessInstruction()); |
| 345 | R << "load after store of same element in same statement"; |
| 346 | R << " (previous stores: " << Stores; |
| 347 | R << ", loading: " << AccRel << ")"; |
| 348 | S->getFunction().getContext().diagnose(R); |
Michael Kruse | 4728184 | 2017-08-28 20:39:07 +0000 | [diff] [blame] | 349 | |
| 350 | IncompatibleElts = IncompatibleElts.add_set(ArrayElts); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 353 | Loads = Loads.unite(AccRel); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 354 | |
| 355 | continue; |
| 356 | } |
| 357 | |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 358 | // In region statements the order is less clear, eg. the load and store |
| 359 | // might be in a boxed loop. |
Tobias Grosser | d3d3d6b | 2018-04-29 00:28:26 +0000 | [diff] [blame] | 360 | if (Stmt->isRegionStmt() && !Loads.is_disjoint(AccRel)) { |
Nicola Zaghen | 349506a | 2018-05-15 13:37:17 +0000 | [diff] [blame] | 361 | LLVM_DEBUG(dbgs() << "WRITE in non-affine subregion not supported\n"); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 362 | OptimizationRemarkMissed R(PassName, "StoreInSubregion", |
| 363 | MA->getAccessInstruction()); |
| 364 | R << "store is in a non-affine subregion"; |
| 365 | S->getFunction().getContext().diagnose(R); |
Michael Kruse | 4728184 | 2017-08-28 20:39:07 +0000 | [diff] [blame] | 366 | |
| 367 | IncompatibleElts = IncompatibleElts.add_set(ArrayElts); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | // Do not allow more than one store to the same location. |
Tobias Grosser | d3d3d6b | 2018-04-29 00:28:26 +0000 | [diff] [blame] | 371 | if (!Stores.is_disjoint(AccRel) && !onlySameValueWrites(Stmt)) { |
Nicola Zaghen | 349506a | 2018-05-15 13:37:17 +0000 | [diff] [blame] | 372 | LLVM_DEBUG(dbgs() << "WRITE after WRITE to same element\n"); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 373 | OptimizationRemarkMissed R(PassName, "StoreAfterStore", |
| 374 | MA->getAccessInstruction()); |
Michael Kruse | a9033aa | 2017-08-09 09:29:09 +0000 | [diff] [blame] | 375 | R << "store after store of same element in same statement"; |
| 376 | R << " (previous stores: " << Stores; |
| 377 | R << ", storing: " << AccRel << ")"; |
| 378 | S->getFunction().getContext().diagnose(R); |
Michael Kruse | 4728184 | 2017-08-28 20:39:07 +0000 | [diff] [blame] | 379 | |
| 380 | IncompatibleElts = IncompatibleElts.add_set(ArrayElts); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 383 | Stores = Stores.unite(AccRel); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 384 | } |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void ZoneAlgorithm::addArrayReadAccess(MemoryAccess *MA) { |
| 388 | assert(MA->isLatestArrayKind()); |
| 389 | assert(MA->isRead()); |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 390 | ScopStmt *Stmt = MA->getStatement(); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 391 | |
| 392 | // { DomainRead[] -> Element[] } |
Michael Kruse | 4728184 | 2017-08-28 20:39:07 +0000 | [diff] [blame] | 393 | auto AccRel = intersectRange(getAccessRelationFor(MA), CompatibleElts); |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 394 | AllReads = AllReads.add_map(AccRel); |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 395 | |
| 396 | if (LoadInst *Load = dyn_cast_or_null<LoadInst>(MA->getAccessInstruction())) { |
| 397 | // { DomainRead[] -> ValInst[] } |
| 398 | isl::map LoadValInst = makeValInst( |
| 399 | Load, Stmt, LI->getLoopFor(Load->getParent()), Stmt->isBlockStmt()); |
| 400 | |
| 401 | // { DomainRead[] -> [Element[] -> DomainRead[]] } |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 402 | isl::map IncludeElement = AccRel.domain_map().curry(); |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 403 | |
| 404 | // { [Element[] -> DomainRead[]] -> ValInst[] } |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 405 | isl::map EltLoadValInst = LoadValInst.apply_domain(IncludeElement); |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 406 | |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 407 | AllReadValInst = AllReadValInst.add_map(EltLoadValInst); |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 408 | } |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Michael Kruse | 68821a8 | 2017-10-31 16:11:46 +0000 | [diff] [blame] | 411 | isl::union_map ZoneAlgorithm::getWrittenValue(MemoryAccess *MA, |
| 412 | isl::map AccRel) { |
Michael Kruse | bd84ce8 | 2017-09-06 12:40:55 +0000 | [diff] [blame] | 413 | if (!MA->isMustWrite()) |
| 414 | return {}; |
| 415 | |
| 416 | Value *AccVal = MA->getAccessValue(); |
| 417 | ScopStmt *Stmt = MA->getStatement(); |
| 418 | Instruction *AccInst = MA->getAccessInstruction(); |
| 419 | |
| 420 | // Write a value to a single element. |
| 421 | auto L = MA->isOriginalArrayKind() ? LI->getLoopFor(AccInst->getParent()) |
| 422 | : Stmt->getSurroundingLoop(); |
| 423 | if (AccVal && |
| 424 | AccVal->getType() == MA->getLatestScopArrayInfo()->getElementType() && |
Michael Kruse | ef8325b | 2017-09-18 17:43:50 +0000 | [diff] [blame] | 425 | AccRel.is_single_valued().is_true()) |
Michael Kruse | 68821a8 | 2017-10-31 16:11:46 +0000 | [diff] [blame] | 426 | return makeNormalizedValInst(AccVal, Stmt, L); |
Michael Kruse | bd84ce8 | 2017-09-06 12:40:55 +0000 | [diff] [blame] | 427 | |
| 428 | // memset(_, '0', ) is equivalent to writing the null value to all touched |
| 429 | // elements. isMustWrite() ensures that all of an element's bytes are |
| 430 | // overwritten. |
| 431 | if (auto *Memset = dyn_cast<MemSetInst>(AccInst)) { |
| 432 | auto *WrittenConstant = dyn_cast<Constant>(Memset->getValue()); |
| 433 | Type *Ty = MA->getLatestScopArrayInfo()->getElementType(); |
| 434 | if (WrittenConstant && WrittenConstant->isZeroValue()) { |
| 435 | Constant *Zero = Constant::getNullValue(Ty); |
Michael Kruse | 68821a8 | 2017-10-31 16:11:46 +0000 | [diff] [blame] | 436 | return makeNormalizedValInst(Zero, Stmt, L); |
Michael Kruse | bd84ce8 | 2017-09-06 12:40:55 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | return {}; |
| 441 | } |
| 442 | |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 443 | void ZoneAlgorithm::addArrayWriteAccess(MemoryAccess *MA) { |
| 444 | assert(MA->isLatestArrayKind()); |
| 445 | assert(MA->isWrite()); |
| 446 | auto *Stmt = MA->getStatement(); |
| 447 | |
| 448 | // { Domain[] -> Element[] } |
Michael Kruse | 983fa9b | 2017-10-24 16:40:34 +0000 | [diff] [blame] | 449 | isl::map AccRel = intersectRange(getAccessRelationFor(MA), CompatibleElts); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 450 | |
| 451 | if (MA->isMustWrite()) |
Michael Kruse | 983fa9b | 2017-10-24 16:40:34 +0000 | [diff] [blame] | 452 | AllMustWrites = AllMustWrites.add_map(AccRel); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 453 | |
| 454 | if (MA->isMayWrite()) |
Michael Kruse | 983fa9b | 2017-10-24 16:40:34 +0000 | [diff] [blame] | 455 | AllMayWrites = AllMayWrites.add_map(AccRel); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 456 | |
| 457 | // { Domain[] -> ValInst[] } |
Michael Kruse | 68821a8 | 2017-10-31 16:11:46 +0000 | [diff] [blame] | 458 | isl::union_map WriteValInstance = getWrittenValue(MA, AccRel); |
Michael Kruse | bd84ce8 | 2017-09-06 12:40:55 +0000 | [diff] [blame] | 459 | if (!WriteValInstance) |
| 460 | WriteValInstance = makeUnknownForDomain(Stmt); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 461 | |
| 462 | // { Domain[] -> [Element[] -> Domain[]] } |
Michael Kruse | 983fa9b | 2017-10-24 16:40:34 +0000 | [diff] [blame] | 463 | isl::map IncludeElement = AccRel.domain_map().curry(); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 464 | |
| 465 | // { [Element[] -> DomainWrite[]] -> ValInst[] } |
Michael Kruse | 68821a8 | 2017-10-31 16:11:46 +0000 | [diff] [blame] | 466 | isl::union_map EltWriteValInst = |
| 467 | WriteValInstance.apply_domain(IncludeElement); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 468 | |
Michael Kruse | 68821a8 | 2017-10-31 16:11:46 +0000 | [diff] [blame] | 469 | AllWriteValInst = AllWriteValInst.unite(EltWriteValInst); |
| 470 | } |
| 471 | |
| 472 | /// Return whether @p PHI refers (also transitively through other PHIs) to |
| 473 | /// itself. |
| 474 | /// |
| 475 | /// loop: |
| 476 | /// %phi1 = phi [0, %preheader], [%phi1, %loop] |
| 477 | /// br i1 %c, label %loop, label %exit |
| 478 | /// |
| 479 | /// exit: |
| 480 | /// %phi2 = phi [%phi1, %bb] |
| 481 | /// |
| 482 | /// In this example, %phi1 is recursive, but %phi2 is not. |
| 483 | static bool isRecursivePHI(const PHINode *PHI) { |
| 484 | SmallVector<const PHINode *, 8> Worklist; |
| 485 | SmallPtrSet<const PHINode *, 8> Visited; |
| 486 | Worklist.push_back(PHI); |
| 487 | |
| 488 | while (!Worklist.empty()) { |
| 489 | const PHINode *Cur = Worklist.pop_back_val(); |
| 490 | |
| 491 | if (Visited.count(Cur)) |
| 492 | continue; |
| 493 | Visited.insert(Cur); |
| 494 | |
| 495 | for (const Use &Incoming : Cur->incoming_values()) { |
| 496 | Value *IncomingVal = Incoming.get(); |
| 497 | auto *IncomingPHI = dyn_cast<PHINode>(IncomingVal); |
| 498 | if (!IncomingPHI) |
| 499 | continue; |
| 500 | |
| 501 | if (IncomingPHI == PHI) |
| 502 | return true; |
| 503 | Worklist.push_back(IncomingPHI); |
| 504 | } |
| 505 | } |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | isl::union_map ZoneAlgorithm::computePerPHI(const ScopArrayInfo *SAI) { |
| 510 | // TODO: If the PHI has an incoming block from before the SCoP, it is not |
| 511 | // represented in any ScopStmt. |
| 512 | |
| 513 | auto *PHI = cast<PHINode>(SAI->getBasePtr()); |
| 514 | auto It = PerPHIMaps.find(PHI); |
| 515 | if (It != PerPHIMaps.end()) |
| 516 | return It->second; |
| 517 | |
| 518 | assert(SAI->isPHIKind()); |
| 519 | |
| 520 | // { DomainPHIWrite[] -> Scatter[] } |
| 521 | isl::union_map PHIWriteScatter = makeEmptyUnionMap(); |
| 522 | |
| 523 | // Collect all incoming block timepoints. |
| 524 | for (MemoryAccess *MA : S->getPHIIncomings(SAI)) { |
| 525 | isl::map Scatter = getScatterFor(MA); |
| 526 | PHIWriteScatter = PHIWriteScatter.add_map(Scatter); |
| 527 | } |
| 528 | |
| 529 | // { DomainPHIRead[] -> Scatter[] } |
| 530 | isl::map PHIReadScatter = getScatterFor(S->getPHIRead(SAI)); |
| 531 | |
| 532 | // { DomainPHIRead[] -> Scatter[] } |
| 533 | isl::map BeforeRead = beforeScatter(PHIReadScatter, true); |
| 534 | |
| 535 | // { Scatter[] } |
| 536 | isl::set WriteTimes = singleton(PHIWriteScatter.range(), ScatterSpace); |
| 537 | |
| 538 | // { DomainPHIRead[] -> Scatter[] } |
| 539 | isl::map PHIWriteTimes = BeforeRead.intersect_range(WriteTimes); |
| 540 | isl::map LastPerPHIWrites = PHIWriteTimes.lexmax(); |
| 541 | |
| 542 | // { DomainPHIRead[] -> DomainPHIWrite[] } |
| 543 | isl::union_map Result = |
| 544 | isl::union_map(LastPerPHIWrites).apply_range(PHIWriteScatter.reverse()); |
| 545 | assert(!Result.is_single_valued().is_false()); |
| 546 | assert(!Result.is_injective().is_false()); |
| 547 | |
| 548 | PerPHIMaps.insert({PHI, Result}); |
| 549 | return Result; |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | isl::union_set ZoneAlgorithm::makeEmptyUnionSet() const { |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 553 | return isl::union_set::empty(ParamSpace); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | isl::union_map ZoneAlgorithm::makeEmptyUnionMap() const { |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 557 | return isl::union_map::empty(ParamSpace); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Michael Kruse | 4728184 | 2017-08-28 20:39:07 +0000 | [diff] [blame] | 560 | void ZoneAlgorithm::collectCompatibleElts() { |
| 561 | // First find all the incompatible elements, then take the complement. |
| 562 | // We compile the list of compatible (rather than incompatible) elements so |
| 563 | // users can intersect with the list, not requiring a subtract operation. It |
| 564 | // also allows us to define a 'universe' of all elements and makes it more |
| 565 | // explicit in which array elements can be used. |
| 566 | isl::union_set AllElts = makeEmptyUnionSet(); |
| 567 | isl::union_set IncompatibleElts = makeEmptyUnionSet(); |
| 568 | |
| 569 | for (auto &Stmt : *S) |
| 570 | collectIncompatibleElts(&Stmt, IncompatibleElts, AllElts); |
| 571 | |
Tobias Grosser | d3d3d6b | 2018-04-29 00:28:26 +0000 | [diff] [blame] | 572 | NumIncompatibleArrays += isl_union_set_n_set(IncompatibleElts.get()); |
Michael Kruse | 4728184 | 2017-08-28 20:39:07 +0000 | [diff] [blame] | 573 | CompatibleElts = AllElts.subtract(IncompatibleElts); |
Tobias Grosser | d3d3d6b | 2018-04-29 00:28:26 +0000 | [diff] [blame] | 574 | NumCompatibleArrays += isl_union_set_n_set(CompatibleElts.get()); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | isl::map ZoneAlgorithm::getScatterFor(ScopStmt *Stmt) const { |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 578 | isl::space ResultSpace = |
| 579 | Stmt->getDomainSpace().map_from_domain_and_range(ScatterSpace); |
| 580 | return Schedule.extract_map(ResultSpace); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | isl::map ZoneAlgorithm::getScatterFor(MemoryAccess *MA) const { |
| 584 | return getScatterFor(MA->getStatement()); |
| 585 | } |
| 586 | |
| 587 | isl::union_map ZoneAlgorithm::getScatterFor(isl::union_set Domain) const { |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 588 | return Schedule.intersect_domain(Domain); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | isl::map ZoneAlgorithm::getScatterFor(isl::set Domain) const { |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 592 | auto ResultSpace = Domain.get_space().map_from_domain_and_range(ScatterSpace); |
| 593 | auto UDomain = isl::union_set(Domain); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 594 | auto UResult = getScatterFor(std::move(UDomain)); |
| 595 | auto Result = singleton(std::move(UResult), std::move(ResultSpace)); |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 596 | assert(!Result || Result.domain().is_equal(Domain) == isl_bool_true); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 597 | return Result; |
| 598 | } |
| 599 | |
| 600 | isl::set ZoneAlgorithm::getDomainFor(ScopStmt *Stmt) const { |
Tobias Grosser | dcf8d69 | 2017-08-06 16:39:52 +0000 | [diff] [blame] | 601 | return Stmt->getDomain().remove_redundancies(); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | isl::set ZoneAlgorithm::getDomainFor(MemoryAccess *MA) const { |
| 605 | return getDomainFor(MA->getStatement()); |
| 606 | } |
| 607 | |
| 608 | isl::map ZoneAlgorithm::getAccessRelationFor(MemoryAccess *MA) const { |
| 609 | auto Domain = getDomainFor(MA); |
| 610 | auto AccRel = MA->getLatestAccessRelation(); |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 611 | return AccRel.intersect_domain(Domain); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | isl::map ZoneAlgorithm::getScalarReachingDefinition(ScopStmt *Stmt) { |
| 615 | auto &Result = ScalarReachDefZone[Stmt]; |
| 616 | if (Result) |
| 617 | return Result; |
| 618 | |
| 619 | auto Domain = getDomainFor(Stmt); |
| 620 | Result = computeScalarReachingDefinition(Schedule, Domain, false, true); |
| 621 | simplify(Result); |
| 622 | |
| 623 | return Result; |
| 624 | } |
| 625 | |
| 626 | isl::map ZoneAlgorithm::getScalarReachingDefinition(isl::set DomainDef) { |
Tobias Grosser | daf68ea | 2018-04-28 22:11:48 +0000 | [diff] [blame] | 627 | auto DomId = DomainDef.get_tuple_id(); |
Tobias Grosser | d3d3d6b | 2018-04-29 00:28:26 +0000 | [diff] [blame] | 628 | auto *Stmt = static_cast<ScopStmt *>(isl_id_get_user(DomId.get())); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 629 | |
| 630 | auto StmtResult = getScalarReachingDefinition(Stmt); |
| 631 | |
Tobias Grosser | daf68ea | 2018-04-28 22:11:48 +0000 | [diff] [blame] | 632 | return StmtResult.intersect_range(DomainDef); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | isl::map ZoneAlgorithm::makeUnknownForDomain(ScopStmt *Stmt) const { |
| 636 | return ::makeUnknownForDomain(getDomainFor(Stmt)); |
| 637 | } |
| 638 | |
| 639 | isl::id ZoneAlgorithm::makeValueId(Value *V) { |
| 640 | if (!V) |
| 641 | return nullptr; |
| 642 | |
| 643 | auto &Id = ValueIds[V]; |
| 644 | if (Id.is_null()) { |
| 645 | auto Name = getIslCompatibleName("Val_", V, ValueIds.size() - 1, |
| 646 | std::string(), UseInstructionNames); |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 647 | Id = isl::id::alloc(IslCtx.get(), Name.c_str(), V); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 648 | } |
| 649 | return Id; |
| 650 | } |
| 651 | |
| 652 | isl::space ZoneAlgorithm::makeValueSpace(Value *V) { |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 653 | auto Result = ParamSpace.set_from_params(); |
| 654 | return Result.set_tuple_id(isl::dim::set, makeValueId(V)); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | isl::set ZoneAlgorithm::makeValueSet(Value *V) { |
| 658 | auto Space = makeValueSpace(V); |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 659 | return isl::set::universe(Space); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | isl::map ZoneAlgorithm::makeValInst(Value *Val, ScopStmt *UserStmt, Loop *Scope, |
| 663 | bool IsCertain) { |
| 664 | // If the definition/write is conditional, the value at the location could |
| 665 | // be either the written value or the old value. Since we cannot know which |
| 666 | // one, consider the value to be unknown. |
| 667 | if (!IsCertain) |
| 668 | return makeUnknownForDomain(UserStmt); |
| 669 | |
| 670 | auto DomainUse = getDomainFor(UserStmt); |
| 671 | auto VUse = VirtualUse::create(S, UserStmt, Scope, Val, true); |
| 672 | switch (VUse.getKind()) { |
| 673 | case VirtualUse::Constant: |
| 674 | case VirtualUse::Block: |
| 675 | case VirtualUse::Hoisted: |
| 676 | case VirtualUse::ReadOnly: { |
| 677 | // The definition does not depend on the statement which uses it. |
| 678 | auto ValSet = makeValueSet(Val); |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 679 | return isl::map::from_domain_and_range(DomainUse, ValSet); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | case VirtualUse::Synthesizable: { |
| 683 | auto *ScevExpr = VUse.getScevExpr(); |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 684 | auto UseDomainSpace = DomainUse.get_space(); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 685 | |
| 686 | // Construct the SCEV space. |
| 687 | // TODO: Add only the induction variables referenced in SCEVAddRecExpr |
| 688 | // expressions, not just all of them. |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 689 | auto ScevId = isl::manage(isl_id_alloc( |
| 690 | UseDomainSpace.get_ctx().get(), nullptr, const_cast<SCEV *>(ScevExpr))); |
| 691 | |
| 692 | auto ScevSpace = UseDomainSpace.drop_dims(isl::dim::set, 0, 0); |
| 693 | ScevSpace = ScevSpace.set_tuple_id(isl::dim::set, ScevId); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 694 | |
| 695 | // { DomainUse[] -> ScevExpr[] } |
Tobias Grosser | 2f549fd | 2018-04-28 21:22:17 +0000 | [diff] [blame] | 696 | auto ValInst = |
| 697 | isl::map::identity(UseDomainSpace.map_from_domain_and_range(ScevSpace)); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 698 | return ValInst; |
| 699 | } |
| 700 | |
| 701 | case VirtualUse::Intra: { |
| 702 | // Definition and use is in the same statement. We do not need to compute |
| 703 | // a reaching definition. |
| 704 | |
| 705 | // { llvm::Value } |
| 706 | auto ValSet = makeValueSet(Val); |
| 707 | |
| 708 | // { UserDomain[] -> llvm::Value } |
Tobias Grosser | daf68ea | 2018-04-28 22:11:48 +0000 | [diff] [blame] | 709 | auto ValInstSet = isl::map::from_domain_and_range(DomainUse, ValSet); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 710 | |
| 711 | // { UserDomain[] -> [UserDomain[] - >llvm::Value] } |
Tobias Grosser | daf68ea | 2018-04-28 22:11:48 +0000 | [diff] [blame] | 712 | auto Result = ValInstSet.domain_map().reverse(); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 713 | simplify(Result); |
| 714 | return Result; |
| 715 | } |
| 716 | |
| 717 | case VirtualUse::Inter: { |
| 718 | // The value is defined in a different statement. |
| 719 | |
| 720 | auto *Inst = cast<Instruction>(Val); |
| 721 | auto *ValStmt = S->getStmtFor(Inst); |
| 722 | |
| 723 | // If the llvm::Value is defined in a removed Stmt, we cannot derive its |
| 724 | // domain. We could use an arbitrary statement, but this could result in |
| 725 | // different ValInst[] for the same llvm::Value. |
| 726 | if (!ValStmt) |
| 727 | return ::makeUnknownForDomain(DomainUse); |
| 728 | |
| 729 | // { DomainDef[] } |
| 730 | auto DomainDef = getDomainFor(ValStmt); |
| 731 | |
| 732 | // { Scatter[] -> DomainDef[] } |
| 733 | auto ReachDef = getScalarReachingDefinition(DomainDef); |
| 734 | |
| 735 | // { DomainUse[] -> Scatter[] } |
| 736 | auto UserSched = getScatterFor(DomainUse); |
| 737 | |
| 738 | // { DomainUse[] -> DomainDef[] } |
Tobias Grosser | daf68ea | 2018-04-28 22:11:48 +0000 | [diff] [blame] | 739 | auto UsedInstance = UserSched.apply_range(ReachDef); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 740 | |
| 741 | // { llvm::Value } |
| 742 | auto ValSet = makeValueSet(Val); |
| 743 | |
| 744 | // { DomainUse[] -> llvm::Value[] } |
Tobias Grosser | daf68ea | 2018-04-28 22:11:48 +0000 | [diff] [blame] | 745 | auto ValInstSet = isl::map::from_domain_and_range(DomainUse, ValSet); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 746 | |
| 747 | // { DomainUse[] -> [DomainDef[] -> llvm::Value] } |
Tobias Grosser | daf68ea | 2018-04-28 22:11:48 +0000 | [diff] [blame] | 748 | auto Result = UsedInstance.range_product(ValInstSet); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 749 | |
| 750 | simplify(Result); |
| 751 | return Result; |
| 752 | } |
| 753 | } |
| 754 | llvm_unreachable("Unhandled use type"); |
| 755 | } |
| 756 | |
Michael Kruse | 68821a8 | 2017-10-31 16:11:46 +0000 | [diff] [blame] | 757 | /// Remove all computed PHIs out of @p Input and replace by their incoming |
| 758 | /// value. |
| 759 | /// |
| 760 | /// @param Input { [] -> ValInst[] } |
| 761 | /// @param ComputedPHIs Set of PHIs that are replaced. Its ValInst must appear |
| 762 | /// on the LHS of @p NormalizeMap. |
| 763 | /// @param NormalizeMap { ValInst[] -> ValInst[] } |
| 764 | static isl::union_map normalizeValInst(isl::union_map Input, |
| 765 | const DenseSet<PHINode *> &ComputedPHIs, |
| 766 | isl::union_map NormalizeMap) { |
| 767 | isl::union_map Result = isl::union_map::empty(Input.get_space()); |
| 768 | Input.foreach_map( |
| 769 | [&Result, &ComputedPHIs, &NormalizeMap](isl::map Map) -> isl::stat { |
| 770 | isl::space Space = Map.get_space(); |
| 771 | isl::space RangeSpace = Space.range(); |
| 772 | |
| 773 | // Instructions within the SCoP are always wrapped. Non-wrapped tuples |
| 774 | // are therefore invariant in the SCoP and don't need normalization. |
| 775 | if (!RangeSpace.is_wrapping()) { |
| 776 | Result = Result.add_map(Map); |
| 777 | return isl::stat::ok; |
| 778 | } |
| 779 | |
| 780 | auto *PHI = dyn_cast<PHINode>(static_cast<Value *>( |
| 781 | RangeSpace.unwrap().get_tuple_id(isl::dim::out).get_user())); |
| 782 | |
| 783 | // If no normalization is necessary, then the ValInst stands for itself. |
| 784 | if (!ComputedPHIs.count(PHI)) { |
| 785 | Result = Result.add_map(Map); |
| 786 | return isl::stat::ok; |
| 787 | } |
| 788 | |
| 789 | // Otherwise, apply the normalization. |
| 790 | isl::union_map Mapped = isl::union_map(Map).apply_range(NormalizeMap); |
| 791 | Result = Result.unite(Mapped); |
| 792 | NumPHINormialization++; |
| 793 | return isl::stat::ok; |
| 794 | }); |
| 795 | return Result; |
| 796 | } |
| 797 | |
| 798 | isl::union_map ZoneAlgorithm::makeNormalizedValInst(llvm::Value *Val, |
| 799 | ScopStmt *UserStmt, |
| 800 | llvm::Loop *Scope, |
| 801 | bool IsCertain) { |
| 802 | isl::map ValInst = makeValInst(Val, UserStmt, Scope, IsCertain); |
| 803 | isl::union_map Normalized = |
| 804 | normalizeValInst(ValInst, ComputedPHIs, NormalizeMap); |
| 805 | return Normalized; |
| 806 | } |
| 807 | |
Michael Kruse | 4728184 | 2017-08-28 20:39:07 +0000 | [diff] [blame] | 808 | bool ZoneAlgorithm::isCompatibleAccess(MemoryAccess *MA) { |
| 809 | if (!MA) |
| 810 | return false; |
| 811 | if (!MA->isLatestArrayKind()) |
| 812 | return false; |
| 813 | Instruction *AccInst = MA->getAccessInstruction(); |
| 814 | return isa<StoreInst>(AccInst) || isa<LoadInst>(AccInst); |
| 815 | } |
| 816 | |
Michael Kruse | 68821a8 | 2017-10-31 16:11:46 +0000 | [diff] [blame] | 817 | bool ZoneAlgorithm::isNormalizable(MemoryAccess *MA) { |
| 818 | assert(MA->isRead()); |
| 819 | |
| 820 | // Exclude ExitPHIs, we are assuming that a normalizable PHI has a READ |
| 821 | // MemoryAccess. |
| 822 | if (!MA->isOriginalPHIKind()) |
| 823 | return false; |
| 824 | |
| 825 | // Exclude recursive PHIs, normalizing them would require a transitive |
| 826 | // closure. |
| 827 | auto *PHI = cast<PHINode>(MA->getAccessInstruction()); |
| 828 | if (RecursivePHIs.count(PHI)) |
| 829 | return false; |
| 830 | |
| 831 | // Ensure that each incoming value can be represented by a ValInst[]. |
| 832 | // We do represent values from statements associated to multiple incoming |
| 833 | // value by the PHI itself, but we do not handle this case yet (especially |
| 834 | // isNormalized()) when normalizing. |
| 835 | const ScopArrayInfo *SAI = MA->getOriginalScopArrayInfo(); |
| 836 | auto Incomings = S->getPHIIncomings(SAI); |
| 837 | for (MemoryAccess *Incoming : Incomings) { |
| 838 | if (Incoming->getIncoming().size() != 1) |
| 839 | return false; |
| 840 | } |
| 841 | |
| 842 | return true; |
| 843 | } |
| 844 | |
| 845 | bool ZoneAlgorithm::isNormalized(isl::map Map) { |
| 846 | isl::space Space = Map.get_space(); |
| 847 | isl::space RangeSpace = Space.range(); |
| 848 | |
| 849 | if (!RangeSpace.is_wrapping()) |
| 850 | return true; |
| 851 | |
| 852 | auto *PHI = dyn_cast<PHINode>(static_cast<Value *>( |
| 853 | RangeSpace.unwrap().get_tuple_id(isl::dim::out).get_user())); |
| 854 | if (!PHI) |
| 855 | return true; |
| 856 | |
| 857 | auto *IncomingStmt = static_cast<ScopStmt *>( |
| 858 | RangeSpace.unwrap().get_tuple_id(isl::dim::in).get_user()); |
| 859 | MemoryAccess *PHIRead = IncomingStmt->lookupPHIReadOf(PHI); |
| 860 | if (!isNormalizable(PHIRead)) |
| 861 | return true; |
| 862 | |
| 863 | return false; |
| 864 | } |
| 865 | |
| 866 | bool ZoneAlgorithm::isNormalized(isl::union_map UMap) { |
| 867 | auto Result = UMap.foreach_map([this](isl::map Map) -> isl::stat { |
| 868 | if (isNormalized(Map)) |
| 869 | return isl::stat::ok; |
| 870 | return isl::stat::error; |
| 871 | }); |
| 872 | return Result == isl::stat::ok; |
| 873 | } |
| 874 | |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 875 | void ZoneAlgorithm::computeCommon() { |
| 876 | AllReads = makeEmptyUnionMap(); |
| 877 | AllMayWrites = makeEmptyUnionMap(); |
| 878 | AllMustWrites = makeEmptyUnionMap(); |
| 879 | AllWriteValInst = makeEmptyUnionMap(); |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 880 | AllReadValInst = makeEmptyUnionMap(); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 881 | |
Michael Kruse | 68821a8 | 2017-10-31 16:11:46 +0000 | [diff] [blame] | 882 | // Default to empty, i.e. no normalization/replacement is taking place. Call |
| 883 | // computeNormalizedPHIs() to initialize. |
| 884 | NormalizeMap = makeEmptyUnionMap(); |
| 885 | ComputedPHIs.clear(); |
| 886 | |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 887 | for (auto &Stmt : *S) { |
| 888 | for (auto *MA : Stmt) { |
| 889 | if (!MA->isLatestArrayKind()) |
| 890 | continue; |
| 891 | |
| 892 | if (MA->isRead()) |
| 893 | addArrayReadAccess(MA); |
| 894 | |
| 895 | if (MA->isWrite()) |
| 896 | addArrayWriteAccess(MA); |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | // { DomainWrite[] -> Element[] } |
Tobias Grosser | daf68ea | 2018-04-28 22:11:48 +0000 | [diff] [blame] | 901 | AllWrites = AllMustWrites.unite(AllMayWrites); |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 902 | |
| 903 | // { [Element[] -> Zone[]] -> DomainWrite[] } |
| 904 | WriteReachDefZone = |
| 905 | computeReachingDefinition(Schedule, AllWrites, false, true); |
| 906 | simplify(WriteReachDefZone); |
| 907 | } |
| 908 | |
Michael Kruse | 68821a8 | 2017-10-31 16:11:46 +0000 | [diff] [blame] | 909 | void ZoneAlgorithm::computeNormalizedPHIs() { |
| 910 | // Determine which PHIs can reference themselves. They are excluded from |
| 911 | // normalization to avoid problems with transitive closures. |
| 912 | for (ScopStmt &Stmt : *S) { |
| 913 | for (MemoryAccess *MA : Stmt) { |
| 914 | if (!MA->isPHIKind()) |
| 915 | continue; |
| 916 | if (!MA->isRead()) |
| 917 | continue; |
| 918 | |
| 919 | // TODO: Can be more efficient since isRecursivePHI can theoretically |
| 920 | // determine recursiveness for multiple values and/or cache results. |
| 921 | auto *PHI = cast<PHINode>(MA->getAccessInstruction()); |
| 922 | if (isRecursivePHI(PHI)) { |
| 923 | NumRecursivePHIs++; |
| 924 | RecursivePHIs.insert(PHI); |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | // { PHIValInst[] -> IncomingValInst[] } |
| 930 | isl::union_map AllPHIMaps = makeEmptyUnionMap(); |
| 931 | |
| 932 | // Discover new PHIs and try to normalize them. |
| 933 | DenseSet<PHINode *> AllPHIs; |
| 934 | for (ScopStmt &Stmt : *S) { |
| 935 | for (MemoryAccess *MA : Stmt) { |
| 936 | if (!MA->isOriginalPHIKind()) |
| 937 | continue; |
| 938 | if (!MA->isRead()) |
| 939 | continue; |
| 940 | if (!isNormalizable(MA)) |
| 941 | continue; |
| 942 | |
| 943 | auto *PHI = cast<PHINode>(MA->getAccessInstruction()); |
| 944 | const ScopArrayInfo *SAI = MA->getOriginalScopArrayInfo(); |
| 945 | |
| 946 | // { PHIDomain[] -> PHIValInst[] } |
| 947 | isl::map PHIValInst = makeValInst(PHI, &Stmt, Stmt.getSurroundingLoop()); |
| 948 | |
| 949 | // { IncomingDomain[] -> IncomingValInst[] } |
| 950 | isl::union_map IncomingValInsts = makeEmptyUnionMap(); |
| 951 | |
| 952 | // Get all incoming values. |
| 953 | for (MemoryAccess *MA : S->getPHIIncomings(SAI)) { |
| 954 | ScopStmt *IncomingStmt = MA->getStatement(); |
| 955 | |
| 956 | auto Incoming = MA->getIncoming(); |
| 957 | assert(Incoming.size() == 1 && "The incoming value must be " |
| 958 | "representable by something else than " |
| 959 | "the PHI itself"); |
| 960 | Value *IncomingVal = Incoming[0].second; |
| 961 | |
| 962 | // { IncomingDomain[] -> IncomingValInst[] } |
| 963 | isl::map IncomingValInst = makeValInst( |
| 964 | IncomingVal, IncomingStmt, IncomingStmt->getSurroundingLoop()); |
| 965 | |
| 966 | IncomingValInsts = IncomingValInsts.add_map(IncomingValInst); |
| 967 | } |
| 968 | |
| 969 | // Determine which instance of the PHI statement corresponds to which |
| 970 | // incoming value. |
| 971 | // { PHIDomain[] -> IncomingDomain[] } |
| 972 | isl::union_map PerPHI = computePerPHI(SAI); |
| 973 | |
| 974 | // { PHIValInst[] -> IncomingValInst[] } |
| 975 | isl::union_map PHIMap = |
| 976 | PerPHI.apply_domain(PHIValInst).apply_range(IncomingValInsts); |
| 977 | assert(!PHIMap.is_single_valued().is_false()); |
| 978 | |
| 979 | // Resolve transitiveness: The incoming value of the newly discovered PHI |
| 980 | // may reference a previously normalized PHI. At the same time, already |
| 981 | // normalized PHIs might be normalized to the new PHI. At the end, none of |
| 982 | // the PHIs may appear on the right-hand-side of the normalization map. |
| 983 | PHIMap = normalizeValInst(PHIMap, AllPHIs, AllPHIMaps); |
| 984 | AllPHIs.insert(PHI); |
| 985 | AllPHIMaps = normalizeValInst(AllPHIMaps, AllPHIs, PHIMap); |
| 986 | |
| 987 | AllPHIMaps = AllPHIMaps.unite(PHIMap); |
| 988 | NumNormalizablePHIs++; |
| 989 | } |
| 990 | } |
| 991 | simplify(AllPHIMaps); |
| 992 | |
| 993 | // Apply the normalization. |
| 994 | ComputedPHIs = AllPHIs; |
| 995 | NormalizeMap = AllPHIMaps; |
| 996 | |
| 997 | assert(!NormalizeMap || isNormalized(NormalizeMap)); |
| 998 | } |
| 999 | |
Michael Kruse | 138a3fb | 2017-08-04 22:51:23 +0000 | [diff] [blame] | 1000 | void ZoneAlgorithm::printAccesses(llvm::raw_ostream &OS, int Indent) const { |
| 1001 | OS.indent(Indent) << "After accesses {\n"; |
| 1002 | for (auto &Stmt : *S) { |
| 1003 | OS.indent(Indent + 4) << Stmt.getBaseName() << "\n"; |
| 1004 | for (auto *MA : Stmt) |
| 1005 | MA->print(OS); |
| 1006 | } |
| 1007 | OS.indent(Indent) << "}\n"; |
| 1008 | } |
Michael Kruse | 70af4f5 | 2017-08-07 18:40:29 +0000 | [diff] [blame] | 1009 | |
| 1010 | isl::union_map ZoneAlgorithm::computeKnownFromMustWrites() const { |
| 1011 | // { [Element[] -> Zone[]] -> [Element[] -> DomainWrite[]] } |
| 1012 | isl::union_map EltReachdDef = distributeDomain(WriteReachDefZone.curry()); |
| 1013 | |
| 1014 | // { [Element[] -> DomainWrite[]] -> ValInst[] } |
| 1015 | isl::union_map AllKnownWriteValInst = filterKnownValInst(AllWriteValInst); |
| 1016 | |
| 1017 | // { [Element[] -> Zone[]] -> ValInst[] } |
| 1018 | return EltReachdDef.apply_range(AllKnownWriteValInst); |
| 1019 | } |
| 1020 | |
| 1021 | isl::union_map ZoneAlgorithm::computeKnownFromLoad() const { |
| 1022 | // { Element[] } |
| 1023 | isl::union_set AllAccessedElts = AllReads.range().unite(AllWrites.range()); |
| 1024 | |
| 1025 | // { Element[] -> Scatter[] } |
| 1026 | isl::union_map EltZoneUniverse = isl::union_map::from_domain_and_range( |
| 1027 | AllAccessedElts, isl::set::universe(ScatterSpace)); |
| 1028 | |
| 1029 | // This assumes there are no "holes" in |
| 1030 | // isl_union_map_domain(WriteReachDefZone); alternatively, compute the zone |
| 1031 | // before the first write or that are not written at all. |
| 1032 | // { Element[] -> Scatter[] } |
| 1033 | isl::union_set NonReachDef = |
| 1034 | EltZoneUniverse.wrap().subtract(WriteReachDefZone.domain()); |
| 1035 | |
| 1036 | // { [Element[] -> Zone[]] -> ReachDefId[] } |
| 1037 | isl::union_map DefZone = |
| 1038 | WriteReachDefZone.unite(isl::union_map::from_domain(NonReachDef)); |
| 1039 | |
| 1040 | // { [Element[] -> Scatter[]] -> Element[] } |
| 1041 | isl::union_map EltZoneElt = EltZoneUniverse.domain_map(); |
| 1042 | |
| 1043 | // { [Element[] -> Zone[]] -> [Element[] -> ReachDefId[]] } |
| 1044 | isl::union_map DefZoneEltDefId = EltZoneElt.range_product(DefZone); |
| 1045 | |
| 1046 | // { Element[] -> [Zone[] -> ReachDefId[]] } |
| 1047 | isl::union_map EltDefZone = DefZone.curry(); |
| 1048 | |
| 1049 | // { [Element[] -> Zone[] -> [Element[] -> ReachDefId[]] } |
| 1050 | isl::union_map EltZoneEltDefid = distributeDomain(EltDefZone); |
| 1051 | |
| 1052 | // { [Element[] -> Scatter[]] -> DomainRead[] } |
| 1053 | isl::union_map Reads = AllReads.range_product(Schedule).reverse(); |
| 1054 | |
| 1055 | // { [Element[] -> Scatter[]] -> [Element[] -> DomainRead[]] } |
| 1056 | isl::union_map ReadsElt = EltZoneElt.range_product(Reads); |
| 1057 | |
| 1058 | // { [Element[] -> Scatter[]] -> ValInst[] } |
| 1059 | isl::union_map ScatterKnown = ReadsElt.apply_range(AllReadValInst); |
| 1060 | |
| 1061 | // { [Element[] -> ReachDefId[]] -> ValInst[] } |
| 1062 | isl::union_map DefidKnown = |
| 1063 | DefZoneEltDefId.apply_domain(ScatterKnown).reverse(); |
| 1064 | |
| 1065 | // { [Element[] -> Zone[]] -> ValInst[] } |
| 1066 | return DefZoneEltDefId.apply_range(DefidKnown); |
| 1067 | } |
| 1068 | |
| 1069 | isl::union_map ZoneAlgorithm::computeKnown(bool FromWrite, |
| 1070 | bool FromRead) const { |
| 1071 | isl::union_map Result = makeEmptyUnionMap(); |
| 1072 | |
| 1073 | if (FromWrite) |
| 1074 | Result = Result.unite(computeKnownFromMustWrites()); |
| 1075 | |
| 1076 | if (FromRead) |
| 1077 | Result = Result.unite(computeKnownFromLoad()); |
| 1078 | |
| 1079 | simplify(Result); |
| 1080 | return Result; |
| 1081 | } |