[llvm-branch-commits] [polly] [Polly] Introduce PhaseManager and remove LPM support (PR #125442)

2025-02-02 Thread Karthika Devi C via llvm-branch-commits

kartcq wrote:

[like]  Karthika Devi C reacted to your message:

From: Eli Friedman ***@***.***>
Sent: Monday, February 3, 2025 1:40:45 AM
To: llvm/llvm-project ***@***.***>
Cc: Karthika Devi C (QUIC) ***@***.***>; Review requested ***@***.***>
Subject: Re: [llvm/llvm-project] [Polly] Introduce PhaseManager and remove LPM 
support (PR #125442)


WARNING: This email originated from outside of Qualcomm. Please be wary of any 
links or attachments, and do not enable macros.

@efriedma-quic requested your review on: 
#125442 [Polly] Introduce 
PhaseManager and remove LPM support.

—
Reply to this email directly, view it on 
GitHub, or 
unsubscribe.
You are receiving this because your review was requested.Message ID: 
***@***.***>


https://github.com/llvm/llvm-project/pull/125442
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [polly] [Polly] Introduce PhaseManager and remove LPM support (PR #125442)

2025-04-10 Thread Karthika Devi C via llvm-branch-commits


@@ -16,105 +16,50 @@
 
//===--===//
 
 #include "polly/CodePreparation.h"
-#include "polly/LinkAllPasses.h"
 #include "polly/Support/ScopHelper.h"
 #include "llvm/Analysis/DominanceFrontier.h"
 #include "llvm/Analysis/LoopInfo.h"
 #include "llvm/Analysis/RegionInfo.h"
 #include "llvm/Analysis/ScalarEvolution.h"
-#include "llvm/InitializePasses.h"
 
 using namespace llvm;
 using namespace polly;
 
-namespace {
-
-/// Prepare the IR for the scop detection.
-///
-class CodePreparation final : public FunctionPass {
-  CodePreparation(const CodePreparation &) = delete;
-  const CodePreparation &operator=(const CodePreparation &) = delete;
-
-  LoopInfo *LI;
-  ScalarEvolution *SE;
-
-  void clear();
-
-public:
-  static char ID;
-
-  explicit CodePreparation() : FunctionPass(ID) {}
-  ~CodePreparation();
-
-  /// @name FunctionPass interface.
-  //@{
-  void getAnalysisUsage(AnalysisUsage &AU) const override;
-  void releaseMemory() override;
-  bool runOnFunction(Function &F) override;
-  void print(raw_ostream &OS, const Module *) const override;
-  //@}
-};
-} // namespace
-
-PreservedAnalyses CodePreparationPass::run(Function &F,
-   FunctionAnalysisManager &FAM) {
-
+static bool runCodePreprationImpl(Function &F, DominatorTree *DT, LoopInfo *LI,
+  RegionInfo *RI) {
   // Find first non-alloca instruction. Every basic block has a non-alloca
   // instruction, as every well formed basic block has a terminator.
   auto &EntryBlock = F.getEntryBlock();
   BasicBlock::iterator I = EntryBlock.begin();
   while (isa(I))
 ++I;
 
-  auto &DT = FAM.getResult(F);
-  auto &LI = FAM.getResult(F);
+  // Abort if not necessary to split
+  if (I->isTerminator() && isa(I) &&
+  cast(I)->isUnconditional())
+return false;
 
   // splitBlock updates DT, LI and RI.
-  splitEntryBlockForAlloca(&EntryBlock, &DT, &LI, nullptr);

kartcq wrote:

Can we please move CodePreparation pass changes to separate commit.
This will make the these changes more trackable.

https://github.com/llvm/llvm-project/pull/125442
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [polly] [Polly] Introduce PhaseManager and remove LPM support (PR #125442)

2025-04-08 Thread Karthika Devi C via llvm-branch-commits


@@ -0,0 +1,419 @@
+//===-- PhaseManager.cpp *- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "polly/Pass/PhaseManager.h"
+#include "polly/CodeGen/CodeGeneration.h"
+#include "polly/CodeGen/IslAst.h"
+#include "polly/CodePreparation.h"
+#include "polly/DeLICM.h"
+#include "polly/DeadCodeElimination.h"
+#include "polly/DependenceInfo.h"
+#include "polly/FlattenSchedule.h"
+#include "polly/ForwardOpTree.h"
+#include "polly/JSONExporter.h"
+#include "polly/MaximalStaticExpansion.h"
+#include "polly/PruneUnprofitable.h"
+#include "polly/ScheduleOptimizer.h"
+#include "polly/ScopDetection.h"
+#include "polly/ScopDetectionDiagnostic.h"
+#include "polly/ScopGraphPrinter.h"
+#include "polly/ScopInfo.h"
+#include "polly/Simplify.h"
+#include "llvm/Analysis/AssumptionCache.h"
+#include "llvm/Analysis/OptimizationRemarkEmitter.h"
+#include "llvm/IR/Module.h"
+
+#define DEBUG_TYPE "polly-pass"
+
+using namespace polly;
+using namespace llvm;
+
+namespace {
+
+/// Recurse through all subregions and all regions and add them to RQ.
+static void addRegionIntoQueue(Region &R, SmallVector &RQ) {
+  RQ.push_back(&R);
+  for (const auto &E : R)
+addRegionIntoQueue(*E, RQ);
+}
+
+/// The phase pipeline of Polly to be embedded into another pass manager than
+/// runs passes on functions.
+///
+/// Polly holds state besides LLVM-IR (RegionInfo and ScopInfo) between phases
+/// that LLVM pass managers do not consider when scheduling analyses and 
passes.
+/// That is, the ScopInfo must persist between phases that a pass manager must
+/// not invalidate to recompute later.
+class PhaseManager {
+private:
+  Function &F;
+  FunctionAnalysisManager &FAM;
+  PollyPassOptions Opts;
+
+public:
+  PhaseManager(Function &F, FunctionAnalysisManager &FAM, PollyPassOptions 
Opts)
+  : F(F), FAM(FAM), Opts(std::move(Opts)) {}
+
+  /// Execute Polly's phases as indicated by the options.
+  bool run() {
+// Get analyses from the function pass manager.
+// These must be preserved during all phases so that if processing one SCoP
+// has finished, the next SCoP can still use them. Recomputing is not an
+// option because ScopDetection stores references to the old results.
+// TODO: CodePreparation doesn't actually need these analysis, it just 
keeps
+// them up-to-date. If they are not computed yet, can also compute after 
the
+// prepare phase.
+auto &LI = FAM.getResult(F);
+auto &DT = FAM.getResult(F);
+bool ModifiedIR = false;
+
+// Phase: prepare
+// TODO: Setting ModifiedIR will invalidate any anlysis, even if DT, LI are
+// preserved.
+if (Opts.isPhaseEnabled(PassPhase::Prepare))
+  ModifiedIR |= runCodePreparation(F, &DT, &LI, nullptr);
+
+// Can't do anything without detection
+if (!Opts.isPhaseEnabled(PassPhase::Detection))
+  return false;
+
+auto &AA = FAM.getResult(F);
+auto &SE = FAM.getResult(F);
+auto &ORE = FAM.getResult(F);
+
+// ScopDetection is modifying RegionInfo, do not cache it, nor use a cached
+// version.
+RegionInfo RI = RegionInfoAnalysis().run(F, FAM);
+
+// Phase: detection
+ScopDetection SD(DT, SE, LI, RI, AA, ORE);
+SD.detect(F);
+if (Opts.isPhaseEnabled(PassPhase::PrintDetect)) {
+  outs() << "Detected Scops in Function " << F.getName() << "\n";
+  for (const Region *R : SD.ValidRegions)
+outs() << "Valid Region for Scop: " << R->getNameStr() << '\n';
+  outs() << "\n";
+}
+
+if (Opts.isPhaseEnabled(PassPhase::DotScops))
+  printGraphForFunction(F, &SD, "scops", false);
+if (Opts.isPhaseEnabled(PassPhase::DotScopsOnly))
+  printGraphForFunction(F, &SD, "scopsonly", true);
+
+auto ViewScops = [&](const char *Name, bool IsSimply) {
+  if (Opts.ViewFilter.empty() && !F.getName().count(Opts.ViewFilter))
+return;
+
+  if (Opts.ViewAll || std::distance(SD.begin(), SD.end()) > 0)
+viewGraphForFunction(F, &SD, Name, IsSimply);
+};
+if (Opts.isPhaseEnabled(PassPhase::ViewScops))
+  ViewScops("scops", false);
+if (Opts.isPhaseEnabled(PassPhase::ViewScopsOnly))
+  ViewScops("scopsonly", true);
+
+// Phase: scops
+auto &AC = FAM.getResult(F);
+const DataLayout &DL = F.getParent()->getDataLayout();
+ScopInfo Info(DL, SD, SE, LI, AA, DT, AC, ORE);

kartcq wrote:

This way of computing ScopInfo processes the regions in same order, as opposed 
to reverse order, followed by FunctionToScopPassAdaptor earlier.
To pertain to this, can we either invoke ScopBuilder in reverse order here or 
change the order in ScopInfo::recompute.

https://github.com/llvm/llvm-project/pull/1

[llvm-branch-commits] [polly] [Polly] Introduce PhaseManager and remove LPM support (PR #125442)

2025-04-08 Thread Karthika Devi C via llvm-branch-commits


@@ -0,0 +1,419 @@
+//===-- PhaseManager.cpp *- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "polly/Pass/PhaseManager.h"
+#include "polly/CodeGen/CodeGeneration.h"
+#include "polly/CodeGen/IslAst.h"
+#include "polly/CodePreparation.h"
+#include "polly/DeLICM.h"
+#include "polly/DeadCodeElimination.h"
+#include "polly/DependenceInfo.h"
+#include "polly/FlattenSchedule.h"
+#include "polly/ForwardOpTree.h"
+#include "polly/JSONExporter.h"
+#include "polly/MaximalStaticExpansion.h"
+#include "polly/PruneUnprofitable.h"
+#include "polly/ScheduleOptimizer.h"
+#include "polly/ScopDetection.h"
+#include "polly/ScopDetectionDiagnostic.h"
+#include "polly/ScopGraphPrinter.h"
+#include "polly/ScopInfo.h"
+#include "polly/Simplify.h"
+#include "llvm/Analysis/AssumptionCache.h"
+#include "llvm/Analysis/OptimizationRemarkEmitter.h"
+#include "llvm/IR/Module.h"
+
+#define DEBUG_TYPE "polly-pass"
+
+using namespace polly;
+using namespace llvm;
+
+namespace {
+
+/// Recurse through all subregions and all regions and add them to RQ.
+static void addRegionIntoQueue(Region &R, SmallVector &RQ) {
+  RQ.push_back(&R);
+  for (const auto &E : R)
+addRegionIntoQueue(*E, RQ);
+}
+
+/// The phase pipeline of Polly to be embedded into another pass manager than
+/// runs passes on functions.
+///
+/// Polly holds state besides LLVM-IR (RegionInfo and ScopInfo) between phases
+/// that LLVM pass managers do not consider when scheduling analyses and 
passes.
+/// That is, the ScopInfo must persist between phases that a pass manager must
+/// not invalidate to recompute later.
+class PhaseManager {
+private:
+  Function &F;
+  FunctionAnalysisManager &FAM;
+  PollyPassOptions Opts;
+
+public:
+  PhaseManager(Function &F, FunctionAnalysisManager &FAM, PollyPassOptions 
Opts)
+  : F(F), FAM(FAM), Opts(std::move(Opts)) {}
+
+  /// Execute Polly's phases as indicated by the options.
+  bool run() {
+// Get analyses from the function pass manager.
+// These must be preserved during all phases so that if processing one SCoP
+// has finished, the next SCoP can still use them. Recomputing is not an
+// option because ScopDetection stores references to the old results.
+// TODO: CodePreparation doesn't actually need these analysis, it just 
keeps
+// them up-to-date. If they are not computed yet, can also compute after 
the
+// prepare phase.
+auto &LI = FAM.getResult(F);
+auto &DT = FAM.getResult(F);
+bool ModifiedIR = false;
+
+// Phase: prepare
+// TODO: Setting ModifiedIR will invalidate any anlysis, even if DT, LI are
+// preserved.
+if (Opts.isPhaseEnabled(PassPhase::Prepare))
+  ModifiedIR |= runCodePreparation(F, &DT, &LI, nullptr);
+

kartcq wrote:

Can we invalidate the necessary analysis here after CodePreparation pass.
FAM.invalidate(F, PA); helped me.
I am sure this is not the right way to go about this.
One of my observations is PDT got regenerated and as a result Regions got 
generated correctly again as Region Analysis requires PDT in turn. (Not sure 
how accurate is this again).
But in general I think we have to invalidate analysis as required after such 
transformations.


https://github.com/llvm/llvm-project/pull/125442
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [polly] [Polly] Introduce PhaseManager and remove LPM support (PR #125442)

2025-04-09 Thread Karthika Devi C via llvm-branch-commits

https://github.com/kartcq edited 
https://github.com/llvm/llvm-project/pull/125442
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [polly] [Polly] Introduce PhaseManager and remove LPM support (PR #125442)

2025-04-08 Thread Karthika Devi C via llvm-branch-commits

kartcq wrote:

Thanks for your patience @Meinersbur.
I am posting some of my observations as review comments.



https://github.com/llvm/llvm-project/pull/125442
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [polly] [Polly] Introduce PhaseManager and remove LPM support (PR #125442)

2025-04-10 Thread Karthika Devi C via llvm-branch-commits

https://github.com/kartcq edited 
https://github.com/llvm/llvm-project/pull/125442
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [polly] [Polly] Introduce PhaseManager and remove LPM support (PR #125442)

2025-05-25 Thread Karthika Devi C via llvm-branch-commits


@@ -0,0 +1,419 @@
+//===-- PhaseManager.cpp *- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "polly/Pass/PhaseManager.h"
+#include "polly/CodeGen/CodeGeneration.h"
+#include "polly/CodeGen/IslAst.h"
+#include "polly/CodePreparation.h"
+#include "polly/DeLICM.h"
+#include "polly/DeadCodeElimination.h"
+#include "polly/DependenceInfo.h"
+#include "polly/FlattenSchedule.h"
+#include "polly/ForwardOpTree.h"
+#include "polly/JSONExporter.h"
+#include "polly/MaximalStaticExpansion.h"
+#include "polly/PruneUnprofitable.h"
+#include "polly/ScheduleOptimizer.h"
+#include "polly/ScopDetection.h"
+#include "polly/ScopDetectionDiagnostic.h"
+#include "polly/ScopGraphPrinter.h"
+#include "polly/ScopInfo.h"
+#include "polly/Simplify.h"
+#include "llvm/Analysis/AssumptionCache.h"
+#include "llvm/Analysis/OptimizationRemarkEmitter.h"
+#include "llvm/IR/Module.h"
+
+#define DEBUG_TYPE "polly-pass"
+
+using namespace polly;
+using namespace llvm;
+
+namespace {
+
+/// Recurse through all subregions and all regions and add them to RQ.
+static void addRegionIntoQueue(Region &R, SmallVector &RQ) {
+  RQ.push_back(&R);
+  for (const auto &E : R)
+addRegionIntoQueue(*E, RQ);
+}
+
+/// The phase pipeline of Polly to be embedded into another pass manager than
+/// runs passes on functions.
+///
+/// Polly holds state besides LLVM-IR (RegionInfo and ScopInfo) between phases
+/// that LLVM pass managers do not consider when scheduling analyses and 
passes.
+/// That is, the ScopInfo must persist between phases that a pass manager must
+/// not invalidate to recompute later.
+class PhaseManager {
+private:
+  Function &F;
+  FunctionAnalysisManager &FAM;
+  PollyPassOptions Opts;
+
+public:
+  PhaseManager(Function &F, FunctionAnalysisManager &FAM, PollyPassOptions 
Opts)
+  : F(F), FAM(FAM), Opts(std::move(Opts)) {}
+
+  /// Execute Polly's phases as indicated by the options.
+  bool run() {
+// Get analyses from the function pass manager.
+// These must be preserved during all phases so that if processing one SCoP
+// has finished, the next SCoP can still use them. Recomputing is not an
+// option because ScopDetection stores references to the old results.
+// TODO: CodePreparation doesn't actually need these analysis, it just 
keeps
+// them up-to-date. If they are not computed yet, can also compute after 
the
+// prepare phase.
+auto &LI = FAM.getResult(F);
+auto &DT = FAM.getResult(F);
+bool ModifiedIR = false;
+
+// Phase: prepare
+// TODO: Setting ModifiedIR will invalidate any anlysis, even if DT, LI are
+// preserved.
+if (Opts.isPhaseEnabled(PassPhase::Prepare))
+  ModifiedIR |= runCodePreparation(F, &DT, &LI, nullptr);
+
+// Can't do anything without detection
+if (!Opts.isPhaseEnabled(PassPhase::Detection))
+  return false;
+
+auto &AA = FAM.getResult(F);
+auto &SE = FAM.getResult(F);
+auto &ORE = FAM.getResult(F);
+
+// ScopDetection is modifying RegionInfo, do not cache it, nor use a cached
+// version.
+RegionInfo RI = RegionInfoAnalysis().run(F, FAM);
+
+// Phase: detection
+ScopDetection SD(DT, SE, LI, RI, AA, ORE);
+SD.detect(F);
+if (Opts.isPhaseEnabled(PassPhase::PrintDetect)) {
+  outs() << "Detected Scops in Function " << F.getName() << "\n";
+  for (const Region *R : SD.ValidRegions)
+outs() << "Valid Region for Scop: " << R->getNameStr() << '\n';
+  outs() << "\n";
+}
+
+if (Opts.isPhaseEnabled(PassPhase::DotScops))
+  printGraphForFunction(F, &SD, "scops", false);
+if (Opts.isPhaseEnabled(PassPhase::DotScopsOnly))
+  printGraphForFunction(F, &SD, "scopsonly", true);
+
+auto ViewScops = [&](const char *Name, bool IsSimply) {
+  if (Opts.ViewFilter.empty() && !F.getName().count(Opts.ViewFilter))
+return;
+
+  if (Opts.ViewAll || std::distance(SD.begin(), SD.end()) > 0)
+viewGraphForFunction(F, &SD, Name, IsSimply);
+};
+if (Opts.isPhaseEnabled(PassPhase::ViewScops))
+  ViewScops("scops", false);
+if (Opts.isPhaseEnabled(PassPhase::ViewScopsOnly))
+  ViewScops("scopsonly", true);
+
+// Phase: scops
+auto &AC = FAM.getResult(F);
+const DataLayout &DL = F.getParent()->getDataLayout();
+ScopInfo Info(DL, SD, SE, LI, AA, DT, AC, ORE);

kartcq wrote:

It's in second case for pass pipeline, the order is changed.

https://github.com/llvm/llvm-project/pull/125442
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits