[Polly] Fix -polly-opt-isl -analyze

The member LastSchedule was never set, such that printScop would always
print "n/a" instead of the last schedule.

To ensure that the isl_ctx lives as least as long as the stored
schedule, also store a shared_ptr.

Also set the schedule tree output style to ISL_YAML_STYLE_BLOCK to avoid
printing everything on a single line.

`opt -polly-opt-isl -analyze` will be used in the next commit.

GitOrigin-RevId: 32bf46842025b740de870dbcd446e4b0f203c9aa
diff --git a/lib/Transform/ScheduleOptimizer.cpp b/lib/Transform/ScheduleOptimizer.cpp
index d9dffb8..ebb5962 100644
--- a/lib/Transform/ScheduleOptimizer.cpp
+++ b/lib/Transform/ScheduleOptimizer.cpp
@@ -1389,7 +1389,7 @@
 
   explicit IslScheduleOptimizer() : ScopPass(ID) {}
 
-  ~IslScheduleOptimizer() override { isl_schedule_free(LastSchedule); }
+  ~IslScheduleOptimizer() override { releaseMemory(); }
 
   /// Optimize the schedule of the SCoP @p S.
   bool runOnScop(Scop &S) override;
@@ -1404,9 +1404,11 @@
   void releaseMemory() override {
     isl_schedule_free(LastSchedule);
     LastSchedule = nullptr;
+    IslCtx.reset();
   }
 
 private:
+  std::shared_ptr<isl_ctx> IslCtx;
   isl_schedule *LastSchedule = nullptr;
 };
 } // namespace
@@ -1630,6 +1632,8 @@
   ScopsOptimized++;
   NumAffineLoopsOptimized += ScopStats.NumAffineLoops;
   NumBoxedLoopsOptimized += ScopStats.NumBoxedLoops;
+  LastSchedule = NewSchedule.copy();
+  IslCtx = S.getSharedIslCtx();
 
   S.setScheduleTree(NewSchedule);
   S.markAsOptimized();
@@ -1652,6 +1656,7 @@
   }
 
   p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule));
+  p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_BLOCK);
   p = isl_printer_print_schedule(p, LastSchedule);
   ScheduleStr = isl_printer_get_str(p);
   isl_printer_free(p);
diff --git a/test/ScheduleOptimizer/mat_mul_pattern_data_layout_2.ll b/test/ScheduleOptimizer/mat_mul_pattern_data_layout_2.ll
index 7a64536..ef6a9f4 100644
--- a/test/ScheduleOptimizer/mat_mul_pattern_data_layout_2.ll
+++ b/test/ScheduleOptimizer/mat_mul_pattern_data_layout_2.ll
@@ -18,6 +18,7 @@
 ;	     C[i][j] += alpha * A[i][k] * B[k][j];
 ;        }
 ;
+; CHECK-LABEL: Printing analysis 'Polly - Generate an AST from the SCoP (isl)' for region: 'bb8 => bb32' in function 'kernel_gemm':
 ; CHECK:    {
 ; CHECK-NEXT:      // 1st level tiling - Tiles
 ; CHECK-NEXT:      for (int c0 = 0; c0 <= 32; c0 += 1)
diff --git a/test/ScheduleOptimizer/pattern-matching-based-opts_3.ll b/test/ScheduleOptimizer/pattern-matching-based-opts_3.ll
index 76341cb..b73083d 100644
--- a/test/ScheduleOptimizer/pattern-matching-based-opts_3.ll
+++ b/test/ScheduleOptimizer/pattern-matching-based-opts_3.ll
@@ -24,6 +24,7 @@
 ;	     C[i][j] += alpha * A[i][k] * B[k][j];
 ;        }
 ;
+; CHECK-LABEL: Printing analysis 'Polly - Generate an AST from the SCoP (isl)' for region: 'bb8 => bb32' in function 'kernel_gemm':
 ; CHECK:    {
 ; CHECK-NEXT:      // 1st level tiling - Tiles
 ; CHECK-NEXT:      for (int c0 = 0; c0 <= 32; c0 += 1)
@@ -76,6 +77,7 @@
 ; CHECK-NEXT:          }
 ; CHECK-NEXT:    }
 ;
+; EXTRACTION-OF-MACRO-KERNEL-LABEL: Printing analysis 'Polly - Generate an AST from the SCoP (isl)' for region: 'bb8 => bb32' in function 'kernel_gemm':
 ; EXTRACTION-OF-MACRO-KERNEL:    {
 ; EXTRACTION-OF-MACRO-KERNEL-NEXT:      // 1st level tiling - Tiles
 ; EXTRACTION-OF-MACRO-KERNEL-NEXT:      for (int c0 = 0; c0 <= 32; c0 += 1)
diff --git a/test/ScheduleOptimizer/tile_after_fusion.ll b/test/ScheduleOptimizer/tile_after_fusion.ll
index 42833ef..35f20ce 100644
--- a/test/ScheduleOptimizer/tile_after_fusion.ll
+++ b/test/ScheduleOptimizer/tile_after_fusion.ll
@@ -17,6 +17,7 @@
 ; checks whether they are tiled after being fused when polly-opt-fusion equals
 ; "max".
 ;
+; CHECK-LABEL: Printing analysis 'Polly - Generate an AST from the SCoP (isl)' for region: 'for.cond => for.end56' in function 'tf':
 ; CHECK:       1st level tiling - Tiles
 ; CHECK-NEXT:     for (int c0 = 0; c0 <= 7; c0 += 1)
 ; CHECK-NEXT:       for (int c1 = 0; c1 <= 7; c1 += 1)