FlattenSchedule.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //===------ FlattenSchedule.cpp --------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Try to reduce the number of scatter dimension. Useful to make isl_union_map
  10. // schedules more understandable. This is only intended for debugging and
  11. // unittests, not for production use.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "polly/FlattenSchedule.h"
  15. #include "polly/FlattenAlgo.h"
  16. #include "polly/ScopInfo.h"
  17. #include "polly/ScopPass.h"
  18. #include "polly/Support/ISLOStream.h"
  19. #include "polly/Support/ISLTools.h"
  20. #define DEBUG_TYPE "polly-flatten-schedule"
  21. using namespace polly;
  22. using namespace llvm;
  23. namespace {
  24. /// Print a schedule to @p OS.
  25. ///
  26. /// Prints the schedule for each statements on a new line.
  27. void printSchedule(raw_ostream &OS, const isl::union_map &Schedule,
  28. int indent) {
  29. for (isl::map Map : Schedule.get_map_list())
  30. OS.indent(indent) << Map << "\n";
  31. }
  32. /// Flatten the schedule stored in an polly::Scop.
  33. class FlattenSchedule : public ScopPass {
  34. private:
  35. FlattenSchedule(const FlattenSchedule &) = delete;
  36. const FlattenSchedule &operator=(const FlattenSchedule &) = delete;
  37. std::shared_ptr<isl_ctx> IslCtx;
  38. isl::union_map OldSchedule;
  39. public:
  40. static char ID;
  41. explicit FlattenSchedule() : ScopPass(ID) {}
  42. virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
  43. AU.addRequiredTransitive<ScopInfoRegionPass>();
  44. AU.setPreservesAll();
  45. }
  46. virtual bool runOnScop(Scop &S) override {
  47. // Keep a reference to isl_ctx to ensure that it is not freed before we free
  48. // OldSchedule.
  49. IslCtx = S.getSharedIslCtx();
  50. LLVM_DEBUG(dbgs() << "Going to flatten old schedule:\n");
  51. OldSchedule = S.getSchedule();
  52. LLVM_DEBUG(printSchedule(dbgs(), OldSchedule, 2));
  53. auto Domains = S.getDomains();
  54. auto RestrictedOldSchedule = OldSchedule.intersect_domain(Domains);
  55. LLVM_DEBUG(dbgs() << "Old schedule with domains:\n");
  56. LLVM_DEBUG(printSchedule(dbgs(), RestrictedOldSchedule, 2));
  57. auto NewSchedule = flattenSchedule(RestrictedOldSchedule);
  58. LLVM_DEBUG(dbgs() << "Flattened new schedule:\n");
  59. LLVM_DEBUG(printSchedule(dbgs(), NewSchedule, 2));
  60. NewSchedule = NewSchedule.gist_domain(Domains);
  61. LLVM_DEBUG(dbgs() << "Gisted, flattened new schedule:\n");
  62. LLVM_DEBUG(printSchedule(dbgs(), NewSchedule, 2));
  63. S.setSchedule(NewSchedule);
  64. return false;
  65. }
  66. virtual void printScop(raw_ostream &OS, Scop &S) const override {
  67. OS << "Schedule before flattening {\n";
  68. printSchedule(OS, OldSchedule, 4);
  69. OS << "}\n\n";
  70. OS << "Schedule after flattening {\n";
  71. printSchedule(OS, S.getSchedule(), 4);
  72. OS << "}\n";
  73. }
  74. virtual void releaseMemory() override {
  75. OldSchedule = {};
  76. IslCtx.reset();
  77. }
  78. };
  79. char FlattenSchedule::ID;
  80. } // anonymous namespace
  81. Pass *polly::createFlattenSchedulePass() { return new FlattenSchedule(); }
  82. INITIALIZE_PASS_BEGIN(FlattenSchedule, "polly-flatten-schedule",
  83. "Polly - Flatten schedule", false, false)
  84. INITIALIZE_PASS_END(FlattenSchedule, "polly-flatten-schedule",
  85. "Polly - Flatten schedule", false, false)