Coverage Report

Created: 2017-11-23 03:11

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/CodeGen/IslAst.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- IslAst.cpp - isl code generator interface --------------------------===//
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
// The isl code generator interface takes a Scop and generates an isl_ast. This
11
// ist_ast can either be returned directly or it can be pretty printed to
12
// stdout.
13
//
14
// A typical isl_ast output looks like this:
15
//
16
// for (c2 = max(0, ceild(n + m, 2); c2 <= min(511, floord(5 * n, 3)); c2++) {
17
//   bb2(c2);
18
// }
19
//
20
// An in-depth discussion of our AST generation approach can be found in:
21
//
22
// Polyhedral AST generation is more than scanning polyhedra
23
// Tobias Grosser, Sven Verdoolaege, Albert Cohen
24
// ACM Transactions on Programming Languages and Systems (TOPLAS),
25
// 37(4), July 2015
26
// http://www.grosser.es/#pub-polyhedral-AST-generation
27
//
28
//===----------------------------------------------------------------------===//
29
30
#include "polly/CodeGen/IslAst.h"
31
#include "polly/CodeGen/CodeGeneration.h"
32
#include "polly/DependenceInfo.h"
33
#include "polly/LinkAllPasses.h"
34
#include "polly/Options.h"
35
#include "polly/ScopDetection.h"
36
#include "polly/ScopInfo.h"
37
#include "polly/ScopPass.h"
38
#include "polly/Support/GICHelper.h"
39
#include "llvm/ADT/Statistic.h"
40
#include "llvm/IR/Function.h"
41
#include "llvm/Pass.h"
42
#include "llvm/Support/CommandLine.h"
43
#include "llvm/Support/Debug.h"
44
#include "llvm/Support/raw_ostream.h"
45
#include "isl/aff.h"
46
#include "isl/ast.h"
47
#include "isl/ast_build.h"
48
#include "isl/id.h"
49
#include "isl/isl-noexceptions.h"
50
#include "isl/map.h"
51
#include "isl/printer.h"
52
#include "isl/schedule.h"
53
#include "isl/set.h"
54
#include "isl/union_map.h"
55
#include "isl/val.h"
56
#include <cassert>
57
#include <cstdlib>
58
#include <cstring>
59
#include <map>
60
#include <string>
61
#include <utility>
62
63
#define DEBUG_TYPE "polly-ast"
64
65
using namespace llvm;
66
using namespace polly;
67
68
using IslAstUserPayload = IslAstInfo::IslAstUserPayload;
69
70
static cl::opt<bool>
71
    PollyParallel("polly-parallel",
72
                  cl::desc("Generate thread parallel code (isl codegen only)"),
73
                  cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
74
75
static cl::opt<bool> PrintAccesses("polly-ast-print-accesses",
76
                                   cl::desc("Print memory access functions"),
77
                                   cl::init(false), cl::ZeroOrMore,
78
                                   cl::cat(PollyCategory));
79
80
static cl::opt<bool> PollyParallelForce(
81
    "polly-parallel-force",
82
    cl::desc(
83
        "Force generation of thread parallel code ignoring any cost model"),
84
    cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
85
86
static cl::opt<bool> UseContext("polly-ast-use-context",
87
                                cl::desc("Use context"), cl::Hidden,
88
                                cl::init(true), cl::ZeroOrMore,
89
                                cl::cat(PollyCategory));
90
91
static cl::opt<bool> DetectParallel("polly-ast-detect-parallel",
92
                                    cl::desc("Detect parallelism"), cl::Hidden,
93
                                    cl::init(false), cl::ZeroOrMore,
94
                                    cl::cat(PollyCategory));
95
96
STATISTIC(ScopsProcessed, "Number of SCoPs processed");
97
STATISTIC(ScopsBeneficial, "Number of beneficial SCoPs");
98
STATISTIC(BeneficialAffineLoops, "Number of beneficial affine loops");
99
STATISTIC(BeneficialBoxedLoops, "Number of beneficial boxed loops");
100
101
STATISTIC(NumForLoops, "Number of for-loops");
102
STATISTIC(NumParallel, "Number of parallel for-loops");
103
STATISTIC(NumInnermostParallel, "Number of innermost parallel for-loops");
104
STATISTIC(NumOutermostParallel, "Number of outermost parallel for-loops");
105
STATISTIC(NumReductionParallel, "Number of reduction-parallel for-loops");
106
STATISTIC(NumExecutedInParallel, "Number of for-loops executed in parallel");
107
STATISTIC(NumIfConditions, "Number of if-conditions");
108
109
namespace polly {
110
111
/// Temporary information used when building the ast.
112
struct AstBuildUserInfo {
113
  /// Construct and initialize the helper struct for AST creation.
114
443
  AstBuildUserInfo() = default;
115
116
  /// The dependence information used for the parallelism check.
117
  const Dependences *Deps = nullptr;
118
119
  /// Flag to indicate that we are inside a parallel for node.
120
  bool InParallelFor = false;
121
122
  /// The last iterator id created for the current SCoP.
123
  isl_id *LastForNodeId = nullptr;
124
};
125
126
} // namespace polly
127
128
/// Free an IslAstUserPayload object pointed to by @p Ptr.
129
2.31k
static void freeIslAstUserPayload(void *Ptr) {
130
2.31k
  delete ((IslAstInfo::IslAstUserPayload *)Ptr);
131
2.31k
}
132
133
2.31k
IslAstInfo::IslAstUserPayload::~IslAstUserPayload() {
134
2.31k
  isl_ast_build_free(Build);
135
2.31k
  isl_pw_aff_free(MinimalDependenceDistance);
136
2.31k
}
137
138
/// Print a string @p str in a single line using @p Printer.
139
static isl_printer *printLine(__isl_take isl_printer *Printer,
140
                              const std::string &str,
141
116
                              __isl_keep isl_pw_aff *PWA = nullptr) {
142
116
  Printer = isl_printer_start_line(Printer);
143
116
  Printer = isl_printer_print_str(Printer, str.c_str());
144
116
  if (PWA)
145
21
    Printer = isl_printer_print_pw_aff(Printer, PWA);
146
116
  return isl_printer_end_line(Printer);
147
116
}
148
149
/// Return all broken reductions as a string of clauses (OpenMP style).
150
390
static const std::string getBrokenReductionsStr(__isl_keep isl_ast_node *Node) {
151
390
  IslAstInfo::MemoryAccessSet *BrokenReductions;
152
390
  std::string str;
153
390
154
390
  BrokenReductions = IslAstInfo::getBrokenReductions(Node);
155
390
  if (!BrokenReductions || 
BrokenReductions->empty()152
)
156
368
    return "";
157
22
158
22
  // Map each type of reduction to a comma separated list of the base addresses.
159
22
  std::map<MemoryAccess::ReductionType, std::string> Clauses;
160
22
  for (MemoryAccess *MA : *BrokenReductions)
161
52
    if (MA->isWrite())
162
26
      Clauses[MA->getReductionType()] +=
163
26
          ", " + MA->getScopArrayInfo()->getName();
164
22
165
22
  // Now print the reductions sorted by type. Each type will cause a clause
166
22
  // like:  reduction (+ : sum0, sum1, sum2)
167
25
  for (const auto &ReductionClause : Clauses) {
168
25
    str += " reduction (";
169
25
    str += MemoryAccess::getReductionOperatorStr(ReductionClause.first);
170
25
    // Remove the first two symbols (", ") to make the output look pretty.
171
25
    str += " : " + ReductionClause.second.substr(2) + ")";
172
25
  }
173
390
174
390
  return str;
175
390
}
176
177
/// Callback executed for each for node in the ast in order to print it.
178
static isl_printer *cbPrintFor(__isl_take isl_printer *Printer,
179
                               __isl_take isl_ast_print_options *Options,
180
390
                               __isl_keep isl_ast_node *Node, void *) {
181
390
  isl_pw_aff *DD = IslAstInfo::getMinimalDependenceDistance(Node);
182
390
  const std::string BrokenReductionsStr = getBrokenReductionsStr(Node);
183
390
  const std::string KnownParallelStr = "#pragma known-parallel";
184
390
  const std::string DepDisPragmaStr = "#pragma minimal dependence distance: ";
185
390
  const std::string SimdPragmaStr = "#pragma simd";
186
390
  const std::string OmpPragmaStr = "#pragma omp parallel for";
187
390
188
390
  if (DD)
189
21
    Printer = printLine(Printer, DepDisPragmaStr, DD);
190
390
191
390
  if (IslAstInfo::isInnermostParallel(Node))
192
44
    Printer = printLine(Printer, SimdPragmaStr + BrokenReductionsStr);
193
390
194
390
  if (IslAstInfo::isExecutedInParallel(Node))
195
18
    Printer = printLine(Printer, OmpPragmaStr);
196
372
  else if (IslAstInfo::isOutermostParallel(Node))
197
33
    Printer = printLine(Printer, KnownParallelStr + BrokenReductionsStr);
198
390
199
390
  isl_pw_aff_free(DD);
200
390
  return isl_ast_node_for_print(Node, Printer, Options);
201
390
}
202
203
/// Check if the current scheduling dimension is parallel.
204
///
205
/// In case the dimension is parallel we also check if any reduction
206
/// dependences is broken when we exploit this parallelism. If so,
207
/// @p IsReductionParallel will be set to true. The reduction dependences we use
208
/// to check are actually the union of the transitive closure of the initial
209
/// reduction dependences together with their reversal. Even though these
210
/// dependences connect all iterations with each other (thus they are cyclic)
211
/// we can perform the parallelism check as we are only interested in a zero
212
/// (or non-zero) dependence distance on the dimension in question.
213
static bool astScheduleDimIsParallel(__isl_keep isl_ast_build *Build,
214
                                     const Dependences *D,
215
168
                                     IslAstUserPayload *NodeInfo) {
216
168
  if (!D->hasValidDependences())
217
1
    return false;
218
167
219
167
  isl_union_map *Schedule = isl_ast_build_get_schedule(Build);
220
167
  isl_union_map *Deps = D->getDependences(
221
167
      Dependences::TYPE_RAW | Dependences::TYPE_WAW | Dependences::TYPE_WAR);
222
167
223
167
  if (!D->isParallel(Schedule, Deps, &NodeInfo->MinimalDependenceDistance) &&
224
167
      
!isl_union_map_free(Schedule)31
)
225
31
    return false;
226
136
227
136
  isl_union_map *RedDeps = D->getDependences(Dependences::TYPE_TC_RED);
228
136
  if (!D->isParallel(Schedule, RedDeps))
229
22
    NodeInfo->IsReductionParallel = true;
230
136
231
136
  if (!NodeInfo->IsReductionParallel && 
!isl_union_map_free(Schedule)114
)
232
114
    return true;
233
22
234
22
  // Annotate reduction parallel nodes with the memory accesses which caused the
235
22
  // reduction dependences parallel execution of the node conflicts with.
236
52
  
for (const auto &MaRedPair : D->getReductionDependences()) 22
{
237
52
    if (!MaRedPair.second)
238
0
      continue;
239
52
    RedDeps = isl_union_map_from_map(isl_map_copy(MaRedPair.second));
240
52
    if (!D->isParallel(Schedule, RedDeps))
241
52
      NodeInfo->BrokenReductions.insert(MaRedPair.first);
242
52
  }
243
168
244
168
  isl_union_map_free(Schedule);
245
168
  return true;
246
168
}
247
248
// This method is executed before the construction of a for node. It creates
249
// an isl_id that is used to annotate the subsequently generated ast for nodes.
250
//
251
// In this function we also run the following analyses:
252
//
253
// - Detection of openmp parallel loops
254
//
255
static __isl_give isl_id *astBuildBeforeFor(__isl_keep isl_ast_build *Build,
256
230
                                            void *User) {
257
230
  AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User;
258
230
  IslAstUserPayload *Payload = new IslAstUserPayload();
259
230
  isl_id *Id = isl_id_alloc(isl_ast_build_get_ctx(Build), "", Payload);
260
230
  Id = isl_id_set_free_user(Id, freeIslAstUserPayload);
261
230
  BuildInfo->LastForNodeId = Id;
262
230
263
230
  // Test for parallelism only if we are not already inside a parallel loop
264
230
  if (!BuildInfo->InParallelFor)
265
131
    BuildInfo->InParallelFor = Payload->IsOutermostParallel =
266
131
        astScheduleDimIsParallel(Build, BuildInfo->Deps, Payload);
267
230
268
230
  return Id;
269
230
}
270
271
// This method is executed after the construction of a for node.
272
//
273
// It performs the following actions:
274
//
275
// - Reset the 'InParallelFor' flag, as soon as we leave a for node,
276
//   that is marked as openmp parallel.
277
//
278
static __isl_give isl_ast_node *
279
astBuildAfterFor(__isl_take isl_ast_node *Node, __isl_keep isl_ast_build *Build,
280
230
                 void *User) {
281
230
  isl_id *Id = isl_ast_node_get_annotation(Node);
282
230
  assert(Id && "Post order visit assumes annotated for nodes");
283
230
  IslAstUserPayload *Payload = (IslAstUserPayload *)isl_id_get_user(Id);
284
230
  assert(Payload && "Post order visit assumes annotated for nodes");
285
230
286
230
  AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User;
287
230
  assert(!Payload->Build && "Build environment already set");
288
230
  Payload->Build = isl_ast_build_copy(Build);
289
230
  Payload->IsInnermost = (Id == BuildInfo->LastForNodeId);
290
230
291
230
  // Innermost loops that are surrounded by parallel loops have not yet been
292
230
  // tested for parallelism. Test them here to ensure we check all innermost
293
230
  // loops for parallelism.
294
230
  if (Payload->IsInnermost && 
BuildInfo->InParallelFor126
) {
295
113
    if (Payload->IsOutermostParallel) {
296
62
      Payload->IsInnermostParallel = true;
297
62
    } else {
298
51
      if (PollyVectorizerChoice == VECTORIZER_NONE)
299
37
        Payload->IsInnermostParallel =
300
37
            astScheduleDimIsParallel(Build, BuildInfo->Deps, Payload);
301
51
    }
302
113
  }
303
230
  if (Payload->IsOutermostParallel)
304
106
    BuildInfo->InParallelFor = false;
305
230
306
230
  isl_id_free(Id);
307
230
  return Node;
308
230
}
309
310
static isl_stat astBuildBeforeMark(__isl_keep isl_id *MarkId,
311
                                   __isl_keep isl_ast_build *Build,
312
40
                                   void *User) {
313
40
  if (!MarkId)
314
0
    return isl_stat_error;
315
40
316
40
  AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User;
317
40
  if (strcmp(isl_id_get_name(MarkId), "SIMD") == 0)
318
14
    BuildInfo->InParallelFor = true;
319
40
320
40
  return isl_stat_ok;
321
40
}
322
323
static __isl_give isl_ast_node *
324
astBuildAfterMark(__isl_take isl_ast_node *Node,
325
40
                  __isl_keep isl_ast_build *Build, void *User) {
326
40
  assert(isl_ast_node_get_type(Node) == isl_ast_node_mark);
327
40
  AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User;
328
40
  auto *Id = isl_ast_node_mark_get_id(Node);
329
40
  if (strcmp(isl_id_get_name(Id), "SIMD") == 0)
330
14
    BuildInfo->InParallelFor = false;
331
40
  isl_id_free(Id);
332
40
  return Node;
333
40
}
334
335
static __isl_give isl_ast_node *AtEachDomain(__isl_take isl_ast_node *Node,
336
                                             __isl_keep isl_ast_build *Build,
337
2.08k
                                             void *User) {
338
2.08k
  assert(!isl_ast_node_get_annotation(Node) && "Node already annotated");
339
2.08k
340
2.08k
  IslAstUserPayload *Payload = new IslAstUserPayload();
341
2.08k
  isl_id *Id = isl_id_alloc(isl_ast_build_get_ctx(Build), "", Payload);
342
2.08k
  Id = isl_id_set_free_user(Id, freeIslAstUserPayload);
343
2.08k
344
2.08k
  Payload->Build = isl_ast_build_copy(Build);
345
2.08k
346
2.08k
  return isl_ast_node_set_annotation(Node, Id);
347
2.08k
}
348
349
// Build alias check condition given a pair of minimal/maximal access.
350
static isl::ast_expr buildCondition(Scop &S, isl::ast_build Build,
351
                                    const Scop::MinMaxAccessTy *It0,
352
171
                                    const Scop::MinMaxAccessTy *It1) {
353
171
354
171
  isl::pw_multi_aff AFirst = It0->first;
355
171
  isl::pw_multi_aff ASecond = It0->second;
356
171
  isl::pw_multi_aff BFirst = It1->first;
357
171
  isl::pw_multi_aff BSecond = It1->second;
358
171
359
171
  isl::id Left = AFirst.get_tuple_id(isl::dim::set);
360
171
  isl::id Right = BFirst.get_tuple_id(isl::dim::set);
361
171
362
171
  isl::ast_expr True =
363
171
      isl::ast_expr::from_val(isl::val::int_from_ui(Build.get_ctx(), 1));
364
171
  isl::ast_expr False =
365
171
      isl::ast_expr::from_val(isl::val::int_from_ui(Build.get_ctx(), 0));
366
171
367
171
  const ScopArrayInfo *BaseLeft =
368
171
      ScopArrayInfo::getFromId(Left)->getBasePtrOriginSAI();
369
171
  const ScopArrayInfo *BaseRight =
370
171
      ScopArrayInfo::getFromId(Right)->getBasePtrOriginSAI();
371
171
  if (BaseLeft && 
BaseLeft == BaseRight24
)
372
2
    return True;
373
169
374
169
  isl::set Params = S.getContext();
375
169
376
169
  isl::ast_expr NonAliasGroup, MinExpr, MaxExpr;
377
169
378
169
  // In the following, we first check if any accesses will be empty under
379
169
  // the execution context of the scop and do not code generate them if this
380
169
  // is the case as isl will fail to derive valid AST expressions for such
381
169
  // accesses.
382
169
383
169
  if (!AFirst.intersect_params(Params).domain().is_empty() &&
384
169
      !BSecond.intersect_params(Params).domain().is_empty()) {
385
167
    MinExpr = Build.access_from(AFirst).address_of();
386
167
    MaxExpr = Build.access_from(BSecond).address_of();
387
167
    NonAliasGroup = MaxExpr.le(MinExpr);
388
167
  }
389
169
390
169
  if (!BFirst.intersect_params(Params).domain().is_empty() &&
391
169
      
!ASecond.intersect_params(Params).domain().is_empty()167
) {
392
167
    MinExpr = Build.access_from(BFirst).address_of();
393
167
    MaxExpr = Build.access_from(ASecond).address_of();
394
167
395
167
    isl::ast_expr Result = MaxExpr.le(MinExpr);
396
167
    if (!NonAliasGroup.is_null())
397
167
      NonAliasGroup = isl::manage(
398
167
          isl_ast_expr_or(NonAliasGroup.release(), Result.release()));
399
0
    else
400
0
      NonAliasGroup = Result;
401
167
  }
402
169
403
169
  if (NonAliasGroup.is_null())
404
2
    NonAliasGroup = True;
405
171
406
171
  return NonAliasGroup;
407
171
}
408
409
__isl_give isl_ast_expr *
410
443
IslAst::buildRunCondition(Scop &S, __isl_keep isl_ast_build *Build) {
411
443
  isl_ast_expr *RunCondition;
412
443
413
443
  // The conditions that need to be checked at run-time for this scop are
414
443
  // available as an isl_set in the runtime check context from which we can
415
443
  // directly derive a run-time condition.
416
443
  auto *PosCond =
417
443
      isl_ast_build_expr_from_set(Build, S.getAssumedContext().release());
418
443
  if (S.hasTrivialInvalidContext()) {
419
326
    RunCondition = PosCond;
420
326
  } else {
421
117
    auto *ZeroV = isl_val_zero(isl_ast_build_get_ctx(Build));
422
117
    auto *NegCond =
423
117
        isl_ast_build_expr_from_set(Build, S.getInvalidContext().release());
424
117
    auto *NotNegCond = isl_ast_expr_eq(isl_ast_expr_from_val(ZeroV), NegCond);
425
117
    RunCondition = isl_ast_expr_and(PosCond, NotNegCond);
426
117
  }
427
443
428
443
  // Create the alias checks from the minimal/maximal accesses in each alias
429
443
  // group which consists of read only and non read only (read write) accesses.
430
443
  // This operation is by construction quadratic in the read-write pointers and
431
443
  // linear in the read only pointers in each alias group.
432
443
  for (const Scop::MinMaxVectorPairTy &MinMaxAccessPair : S.getAliasGroups()) {
433
104
    auto &MinMaxReadWrite = MinMaxAccessPair.first;
434
104
    auto &MinMaxReadOnly = MinMaxAccessPair.second;
435
104
    auto RWAccEnd = MinMaxReadWrite.end();
436
104
437
226
    for (auto RWAccIt0 = MinMaxReadWrite.begin(); RWAccIt0 != RWAccEnd;
438
122
         ++RWAccIt0) {
439
142
      for (auto RWAccIt1 = RWAccIt0 + 1; RWAccIt1 != RWAccEnd; 
++RWAccIt120
)
440
122
        RunCondition = isl_ast_expr_and(
441
20
            RunCondition,
442
20
            buildCondition(S, isl::manage(isl_ast_build_copy(Build)), RWAccIt0,
443
20
                           RWAccIt1)
444
20
                .release());
445
122
      for (const Scop::MinMaxAccessTy &ROAccIt : MinMaxReadOnly)
446
151
        RunCondition = isl_ast_expr_and(
447
151
            RunCondition,
448
151
            buildCondition(S, isl::manage(isl_ast_build_copy(Build)), RWAccIt0,
449
151
                           &ROAccIt)
450
151
                .release());
451
122
    }
452
104
  }
453
443
454
443
  return RunCondition;
455
443
}
456
457
/// Simple cost analysis for a given SCoP.
458
///
459
/// TODO: Improve this analysis and extract it to make it usable in other
460
///       places too.
461
///       In order to improve the cost model we could either keep track of
462
///       performed optimizations (e.g., tiling) or compute properties on the
463
///       original as well as optimized SCoP (e.g., #stride-one-accesses).
464
443
static bool benefitsFromPolly(Scop &Scop, bool PerformParallelTest) {
465
443
  if (PollyProcessUnprofitable)
466
443
    return true;
467
0
468
0
  // Check if nothing interesting happened.
469
0
  if (!PerformParallelTest && !Scop.isOptimized() &&
470
0
      Scop.getAliasGroups().empty())
471
0
    return false;
472
0
473
0
  // The default assumption is that Polly improves the code.
474
0
  return true;
475
0
}
476
477
/// Collect statistics for the syntax tree rooted at @p Ast.
478
443
static void walkAstForStatistics(__isl_keep isl_ast_node *Ast) {
479
443
  assert(Ast);
480
443
  isl_ast_node_foreach_descendant_top_down(
481
443
      Ast,
482
3.45k
      [](__isl_keep isl_ast_node *Node, void *User) -> isl_bool {
483
3.45k
        switch (isl_ast_node_get_type(Node)) {
484
3.45k
        case isl_ast_node_for:
485
690
          NumForLoops++;
486
690
          if (IslAstInfo::isParallel(Node))
487
136
            NumParallel++;
488
690
          if (IslAstInfo::isInnermostParallel(Node))
489
92
            NumInnermostParallel++;
490
690
          if (IslAstInfo::isOutermostParallel(Node))
491
106
            NumOutermostParallel++;
492
690
          if (IslAstInfo::isReductionParallel(Node))
493
22
            NumReductionParallel++;
494
690
          if (IslAstInfo::isExecutedInParallel(Node))
495
46
            NumExecutedInParallel++;
496
690
          break;
497
3.45k
498
3.45k
        case isl_ast_node_if:
499
189
          NumIfConditions++;
500
189
          break;
501
3.45k
502
3.45k
        default:
503
2.58k
          break;
504
3.45k
        }
505
3.45k
506
3.45k
        // Continue traversing subtrees.
507
3.45k
        return isl_bool_true;
508
3.45k
      },
509
443
      nullptr);
510
443
}
511
512
443
IslAst::IslAst(Scop &Scop) : S(Scop), Ctx(Scop.getSharedIslCtx()) {}
513
514
IslAst::IslAst(IslAst &&O)
515
0
    : S(O.S), Root(O.Root), RunCondition(O.RunCondition), Ctx(O.Ctx) {
516
0
  O.Root = nullptr;
517
0
  O.RunCondition = nullptr;
518
0
}
519
520
443
IslAst::~IslAst() {
521
443
  isl_ast_node_free(Root);
522
443
  isl_ast_expr_free(RunCondition);
523
443
}
524
525
443
void IslAst::init(const Dependences &D) {
526
443
  bool PerformParallelTest = PollyParallel || 
DetectParallel395
||
527
443
                             
PollyVectorizerChoice != VECTORIZER_NONE363
;
528
443
529
443
  // We can not perform the dependence analysis and, consequently,
530
443
  // the parallel code generation in case the schedule tree contains
531
443
  // extension nodes.
532
443
  auto ScheduleTree = S.getScheduleTree();
533
443
  PerformParallelTest =
534
443
      PerformParallelTest && 
!S.containsExtensionNode(ScheduleTree)110
;
535
443
536
443
  // Skip AST and code generation if there was no benefit achieved.
537
443
  if (!benefitsFromPolly(S, PerformParallelTest))
538
0
    return;
539
443
540
443
  auto ScopStats = S.getStatistics();
541
443
  ScopsBeneficial++;
542
443
  BeneficialAffineLoops += ScopStats.NumAffineLoops;
543
443
  BeneficialBoxedLoops += ScopStats.NumBoxedLoops;
544
443
545
443
  auto Ctx = S.getIslCtx();
546
443
  isl_options_set_ast_build_atomic_upper_bound(Ctx.get(), true);
547
443
  isl_options_set_ast_build_detect_min_max(Ctx.get(), true);
548
443
  isl_ast_build *Build;
549
443
  AstBuildUserInfo BuildInfo;
550
443
551
443
  if (UseContext)
552
443
    Build = isl_ast_build_from_context(S.getContext().release());
553
0
  else
554
0
    Build = isl_ast_build_from_context(
555
0
        isl_set_universe(S.getParamSpace().release()));
556
443
557
443
  Build = isl_ast_build_set_at_each_domain(Build, AtEachDomain, nullptr);
558
443
559
443
  if (PerformParallelTest) {
560
110
    BuildInfo.Deps = &D;
561
110
    BuildInfo.InParallelFor = false;
562
110
563
110
    Build = isl_ast_build_set_before_each_for(Build, &astBuildBeforeFor,
564
110
                                              &BuildInfo);
565
110
    Build =
566
110
        isl_ast_build_set_after_each_for(Build, &astBuildAfterFor, &BuildInfo);
567
110
568
110
    Build = isl_ast_build_set_before_each_mark(Build, &astBuildBeforeMark,
569
110
                                               &BuildInfo);
570
110
571
110
    Build = isl_ast_build_set_after_each_mark(Build, &astBuildAfterMark,
572
110
                                              &BuildInfo);
573
110
  }
574
443
575
443
  RunCondition = buildRunCondition(S, Build);
576
443
577
443
  Root = isl_ast_build_node_from_schedule(Build, S.getScheduleTree().release());
578
443
  walkAstForStatistics(Root);
579
443
580
443
  isl_ast_build_free(Build);
581
443
}
582
583
443
IslAst IslAst::create(Scop &Scop, const Dependences &D) {
584
443
  IslAst Ast{Scop};
585
443
  Ast.init(D);
586
443
  return Ast;
587
443
}
588
589
443
__isl_give isl_ast_node *IslAst::getAst() { return isl_ast_node_copy(Root); }
590
439
__isl_give isl_ast_expr *IslAst::getRunCondition() {
591
439
  return isl_ast_expr_copy(RunCondition);
592
439
}
593
594
0
__isl_give isl_ast_node *IslAstInfo::getAst() { return Ast.getAst(); }
595
286
__isl_give isl_ast_expr *IslAstInfo::getRunCondition() {
596
286
  return Ast.getRunCondition();
597
286
}
598
599
6.23k
IslAstUserPayload *IslAstInfo::getNodePayload(__isl_keep isl_ast_node *Node) {
600
6.23k
  isl_id *Id = isl_ast_node_get_annotation(Node);
601
6.23k
  if (!Id)
602
3.69k
    return nullptr;
603
2.54k
  IslAstUserPayload *Payload = (IslAstUserPayload *)isl_id_get_user(Id);
604
2.54k
  isl_id_free(Id);
605
2.54k
  return Payload;
606
2.54k
}
607
608
68
bool IslAstInfo::isInnermost(__isl_keep isl_ast_node *Node) {
609
68
  IslAstUserPayload *Payload = getNodePayload(Node);
610
68
  return Payload && Payload->IsInnermost;
611
68
}
612
613
940
bool IslAstInfo::isParallel(__isl_keep isl_ast_node *Node) {
614
940
  return IslAstInfo::isInnermostParallel(Node) ||
615
940
         
IslAstInfo::isOutermostParallel(Node)835
;
616
940
}
617
618
2.04k
bool IslAstInfo::isInnermostParallel(__isl_keep isl_ast_node *Node) {
619
2.04k
  IslAstUserPayload *Payload = getNodePayload(Node);
620
2.04k
  return Payload && 
Payload->IsInnermostParallel670
;
621
2.04k
}
622
623
2.01k
bool IslAstInfo::isOutermostParallel(__isl_keep isl_ast_node *Node) {
624
2.01k
  IslAstUserPayload *Payload = getNodePayload(Node);
625
2.01k
  return Payload && 
Payload->IsOutermostParallel636
;
626
2.01k
}
627
628
818
bool IslAstInfo::isReductionParallel(__isl_keep isl_ast_node *Node) {
629
818
  IslAstUserPayload *Payload = getNodePayload(Node);
630
818
  return Payload && 
Payload->IsReductionParallel358
;
631
818
}
632
633
1.35k
bool IslAstInfo::isExecutedInParallel(__isl_keep isl_ast_node *Node) {
634
1.35k
  if (!PollyParallel)
635
1.21k
    return false;
636
148
637
148
  // Do not parallelize innermost loops.
638
148
  //
639
148
  // Parallelizing innermost loops is often not profitable, especially if
640
148
  // they have a low number of iterations.
641
148
  //
642
148
  // TODO: Decide this based on the number of loop iterations that will be
643
148
  //       executed. This can possibly require run-time checks, which again
644
148
  //       raises the question of both run-time check overhead and code size
645
148
  //       costs.
646
148
  if (!PollyParallelForce && 
isInnermost(Node)68
)
647
32
    return false;
648
116
649
116
  return isOutermostParallel(Node) && 
!isReductionParallel(Node)92
;
650
1.35k
}
651
652
__isl_give isl_union_map *
653
47
IslAstInfo::getSchedule(__isl_keep isl_ast_node *Node) {
654
47
  IslAstUserPayload *Payload = getNodePayload(Node);
655
47
  return Payload ? isl_ast_build_get_schedule(Payload->Build) : 
nullptr0
;
656
47
}
657
658
__isl_give isl_pw_aff *
659
390
IslAstInfo::getMinimalDependenceDistance(__isl_keep isl_ast_node *Node) {
660
390
  IslAstUserPayload *Payload = getNodePayload(Node);
661
390
  return Payload ? 
isl_pw_aff_copy(Payload->MinimalDependenceDistance)152
662
390
                 : 
nullptr238
;
663
390
}
664
665
IslAstInfo::MemoryAccessSet *
666
390
IslAstInfo::getBrokenReductions(__isl_keep isl_ast_node *Node) {
667
390
  IslAstUserPayload *Payload = getNodePayload(Node);
668
390
  return Payload ? 
&Payload->BrokenReductions152
:
nullptr238
;
669
390
}
670
671
461
isl_ast_build *IslAstInfo::getBuild(__isl_keep isl_ast_node *Node) {
672
461
  IslAstUserPayload *Payload = getNodePayload(Node);
673
461
  return Payload ? Payload->Build : 
nullptr0
;
674
461
}
675
676
IslAstInfo IslAstAnalysis::run(Scop &S, ScopAnalysisManager &SAM,
677
0
                               ScopStandardAnalysisResults &SAR) {
678
0
  return {S, SAM.getResult<DependenceAnalysis>(S, SAR).getDependences(
679
0
                 Dependences::AL_Statement)};
680
0
}
681
682
static __isl_give isl_printer *cbPrintUser(__isl_take isl_printer *P,
683
                                           __isl_take isl_ast_print_options *O,
684
                                           __isl_keep isl_ast_node *Node,
685
2
                                           void *User) {
686
2
  isl::ast_node AstNode = isl::manage(isl_ast_node_copy(Node));
687
2
  isl::ast_expr NodeExpr = AstNode.user_get_expr();
688
2
  isl::ast_expr CallExpr = NodeExpr.get_op_arg(0);
689
2
  isl::id CallExprId = CallExpr.get_id();
690
2
  ScopStmt *AccessStmt = (ScopStmt *)CallExprId.get_user();
691
2
692
2
  P = isl_printer_start_line(P);
693
2
  P = isl_printer_print_str(P, AccessStmt->getBaseName());
694
2
  P = isl_printer_print_str(P, "(");
695
2
  P = isl_printer_end_line(P);
696
2
  P = isl_printer_indent(P, 2);
697
2
698
3
  for (MemoryAccess *MemAcc : *AccessStmt) {
699
3
    P = isl_printer_start_line(P);
700
3
701
3
    if (MemAcc->isRead())
702
1
      P = isl_printer_print_str(P, "/* read  */ &");
703
2
    else
704
2
      P = isl_printer_print_str(P, "/* write */  ");
705
3
706
3
    isl::ast_build Build =
707
3
        isl::manage(isl_ast_build_copy(IslAstInfo::getBuild(Node)));
708
3
    if (MemAcc->isAffine()) {
709
2
      isl_pw_multi_aff *PwmaPtr =
710
2
          MemAcc->applyScheduleToAccessRelation(Build.get_schedule()).release();
711
2
      isl::pw_multi_aff Pwma = isl::manage(PwmaPtr);
712
2
      isl::ast_expr AccessExpr = Build.access_from(Pwma);
713
2
      P = isl_printer_print_ast_expr(P, AccessExpr.get());
714
2
    } else {
715
1
      P = isl_printer_print_str(
716
1
          P, MemAcc->getLatestScopArrayInfo()->getName().c_str());
717
1
      P = isl_printer_print_str(P, "[*]");
718
1
    }
719
3
    P = isl_printer_end_line(P);
720
3
  }
721
2
722
2
  P = isl_printer_indent(P, -2);
723
2
  P = isl_printer_start_line(P);
724
2
  P = isl_printer_print_str(P, ");");
725
2
  P = isl_printer_end_line(P);
726
2
727
2
  isl_ast_print_options_free(O);
728
2
  return P;
729
2
}
730
731
153
void IslAstInfo::print(raw_ostream &OS) {
732
153
  isl_ast_print_options *Options;
733
153
  isl_ast_node *RootNode = Ast.getAst();
734
153
  Function &F = S.getFunction();
735
153
736
153
  OS << ":: isl ast :: " << F.getName() << " :: " << S.getNameStr() << "\n";
737
153
738
153
  if (!RootNode) {
739
0
    OS << ":: isl ast generation and code generation was skipped!\n\n";
740
0
    OS << ":: This is either because no useful optimizations could be applied "
741
0
          "(use -polly-process-unprofitable to enforce code generation) or "
742
0
          "because earlier passes such as dependence analysis timed out (use "
743
0
          "-polly-dependences-computeout=0 to set dependence analysis timeout "
744
0
          "to infinity)\n\n";
745
0
    return;
746
0
  }
747
153
748
153
  isl_ast_expr *RunCondition = Ast.getRunCondition();
749
153
  char *RtCStr, *AstStr;
750
153
751
153
  Options = isl_ast_print_options_alloc(S.getIslCtx().get());
752
153
753
153
  if (PrintAccesses)
754
2
    Options =
755
2
        isl_ast_print_options_set_print_user(Options, cbPrintUser, nullptr);
756
153
  Options = isl_ast_print_options_set_print_for(Options, cbPrintFor, nullptr);
757
153
758
153
  isl_printer *P = isl_printer_to_str(S.getIslCtx().get());
759
153
  P = isl_printer_set_output_format(P, ISL_FORMAT_C);
760
153
  P = isl_printer_print_ast_expr(P, RunCondition);
761
153
  RtCStr = isl_printer_get_str(P);
762
153
  P = isl_printer_flush(P);
763
153
  P = isl_printer_indent(P, 4);
764
153
  P = isl_ast_node_print(RootNode, P, Options);
765
153
  AstStr = isl_printer_get_str(P);
766
153
767
153
  auto *Schedule = S.getScheduleTree().release();
768
153
769
153
  DEBUG({
770
153
    dbgs() << S.getContextStr() << "\n";
771
153
    dbgs() << stringFromIslObj(Schedule);
772
153
  });
773
153
  OS << "\nif (" << RtCStr << ")\n\n";
774
153
  OS << AstStr << "\n";
775
153
  OS << "else\n";
776
153
  OS << "    {  /* original code */ }\n\n";
777
153
778
153
  free(RtCStr);
779
153
  free(AstStr);
780
153
781
153
  isl_ast_expr_free(RunCondition);
782
153
  isl_schedule_free(Schedule);
783
153
  isl_ast_node_free(RootNode);
784
153
  isl_printer_free(P);
785
153
}
786
787
AnalysisKey IslAstAnalysis::Key;
788
PreservedAnalyses IslAstPrinterPass::run(Scop &S, ScopAnalysisManager &SAM,
789
                                         ScopStandardAnalysisResults &SAR,
790
0
                                         SPMUpdater &U) {
791
0
  auto &Ast = SAM.getResult<IslAstAnalysis>(S, SAR);
792
0
  Ast.print(OS);
793
0
  return PreservedAnalyses::all();
794
0
}
795
796
1.74k
void IslAstInfoWrapperPass::releaseMemory() { Ast.reset(); }
797
798
443
bool IslAstInfoWrapperPass::runOnScop(Scop &Scop) {
799
443
  // Skip SCoPs in case they're already handled by PPCGCodeGeneration.
800
443
  if (Scop.isToBeSkipped())
801
0
    return false;
802
443
803
443
  ScopsProcessed++;
804
443
805
443
  const Dependences &D =
806
443
      getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement);
807
443
808
443
  if (D.getSharedIslCtx() != Scop.getSharedIslCtx()) {
809
0
    DEBUG(dbgs() << "Got dependence analysis for different SCoP/isl_ctx\n");
810
0
    Ast.reset();
811
0
    return false;
812
0
  }
813
443
814
443
  Ast.reset(new IslAstInfo(Scop, D));
815
443
816
443
  DEBUG(printScop(dbgs(), Scop));
817
443
  return false;
818
443
}
819
820
481
void IslAstInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
821
481
  // Get the Common analysis usage of ScopPasses.
822
481
  ScopPass::getAnalysisUsage(AU);
823
481
  AU.addRequiredTransitive<ScopInfoRegionPass>();
824
481
  AU.addRequired<DependenceInfo>();
825
481
826
481
  AU.addPreserved<DependenceInfo>();
827
481
}
828
829
153
void IslAstInfoWrapperPass::printScop(raw_ostream &OS, Scop &S) const {
830
153
  if (Ast)
831
153
    Ast->print(OS);
832
153
}
833
834
char IslAstInfoWrapperPass::ID = 0;
835
836
0
Pass *polly::createIslAstInfoWrapperPassPass() {
837
0
  return new IslAstInfoWrapperPass();
838
0
}
839
840
37.3k
INITIALIZE_PASS_BEGIN(IslAstInfoWrapperPass, "polly-ast",
841
37.3k
                      "Polly - Generate an AST of the SCoP (isl)", false,
842
37.3k
                      false);
843
37.3k
INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass);
844
37.3k
INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
845
37.3k
INITIALIZE_PASS_END(IslAstInfoWrapperPass, "polly-ast",
846
                    "Polly - Generate an AST from the SCoP (isl)", false, false)