[clang] [flang] [llvm] [openmp] [Clang][OpenMP][LoopTransformations] Add support for "#pragma omp fuse" loop transformation directive and "looprange" clause (PR #139293)

Alexey Bataev via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 1 07:59:35 PDT 2025


================
@@ -955,31 +960,69 @@ class OMPLoopBasedDirective : public OMPExecutableDirective {
   }
 };
 
-/// The base class for all loop transformation directives.
-class OMPLoopTransformationDirective : public OMPLoopBasedDirective {
+/// Common class of data shared between
+/// OMPCanonicalLoopNestTransformationDirective and
+/// OMPCanonicalLoopSequenceTransformationDirective
+class OMPLoopTransformationDirective {
   friend class ASTStmtReader;
 
-  /// Number of loops generated by this loop transformation.
-  unsigned NumGeneratedLoops = 0;
+  /// Number of (top-level) generated loops.
+  /// This value is 1 for most transformations as they only map one loop nest
+  /// into another.
+  /// Some loop transformations (like a non-partial 'unroll') may not generate
+  /// a loop nest, so this would be 0.
+  /// Some loop transformations (like 'fuse' with looprange and 'split') may
+  /// generate more than one loop nest, so the value would be >= 1.
+  unsigned NumGeneratedLoops = 1;
+
+  /// We need this because we cannot easily make OMPLoopTransformationDirective
+  /// a proper Stmt.
+  Stmt *S;
 
 protected:
-  explicit OMPLoopTransformationDirective(StmtClass SC,
-                                          OpenMPDirectiveKind Kind,
-                                          SourceLocation StartLoc,
-                                          SourceLocation EndLoc,
-                                          unsigned NumAssociatedLoops)
-      : OMPLoopBasedDirective(SC, Kind, StartLoc, EndLoc, NumAssociatedLoops) {}
+  void setNumGeneratedLoops(unsigned N) { NumGeneratedLoops = N; }
 
-  /// Set the number of loops generated by this loop transformation.
-  void setNumGeneratedLoops(unsigned Num) { NumGeneratedLoops = Num; }
+  explicit OMPLoopTransformationDirective(Stmt *S) : S(S) {}
+
+public:
+  unsigned getNumGeneratedLoops() const { return NumGeneratedLoops; }
+
+  /// Returns the specific directive related to this loop transformation.
+  Stmt *getDirective() const { return S; }
+
+  /// Get the de-sugared statements after the loop transformation.
+  ///
+  /// Might be nullptr if either the directive generates no loops and is handled
+  /// directly in CodeGen, or resolving a template-dependence context is
+  /// required.
+  Stmt *getTransformedStmt() const;
+
+  /// Return preinits statement.
+  Stmt *getPreInits() const;
+
+  static bool classof(const Stmt *T) {
+    return isa<OMPCanonicalLoopNestTransformationDirective>(T) ||
+           isa<OMPCanonicalLoopSequenceTransformationDirective>(T);
----------------
alexey-bataev wrote:

```suggestion
    return isa<OMPCanonicalLoopNestTransformationDirective, OMPCanonicalLoopSequenceTransformationDirective>(T);
```

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


More information about the cfe-commits mailing list