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

Michael Kruse via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 20 05:06:13 PDT 2025


================
@@ -14189,10 +14196,49 @@ StmtResult SemaOpenMP::ActOnOpenMPTargetTeamsDistributeSimdDirective(
       getASTContext(), StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
 }
 
+/// Overloaded base case function
+template <typename T, typename F> static bool tryHandleAs(T *t, F &&) {
+  return false;
+}
+
+///
+/// Tries to recursively cast `t` to one of the given types and invokes `f` if
+/// successful.
+///
+/// @tparam Class The first type to check.
+/// @tparam Rest The remaining types to check.
+/// @tparam T The base type of `t`.
+/// @tparam F The callable type for the function to invoke upon a successful
+/// cast.
+/// @param t The object to be checked.
+/// @param f The function to invoke if `t` matches `Class`.
+/// @return `true` if `t` matched any type and `f` was called, otherwise
+/// `false`.
+template <typename Class, typename... Rest, typename T, typename F>
+static bool tryHandleAs(T *t, F &&f) {
+  if (Class *c = dyn_cast<Class>(t)) {
+    f(c);
+    return true;
+  }
+  return tryHandleAs<Rest...>(t, std::forward<F>(f));
+}
+
+/// Updates OriginalInits by checking Transform against loop transformation
+/// directives and appending their pre-inits if a match is found.
+static void updatePreInits(OMPLoopBasedDirective *Transform,
+                           SmallVectorImpl<SmallVector<Stmt *>> &PreInits) {
+  if (!tryHandleAs<OMPTileDirective, OMPUnrollDirective, OMPReverseDirective,
+                   OMPInterchangeDirective, OMPFuseDirective>(
+          Transform, [&PreInits](auto *Dir) {
+            appendFlattenedStmtList(PreInits.back(), Dir->getPreInits());
----------------
Meinersbur wrote:

Consider introducing `getPreInits` as a member of `OMPLoopBasedDirective`. It might still call its derived classes function. The the established pattern is a switch case over `getStmtClass()` instead of polymorphic lambdas.

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


More information about the llvm-commits mailing list