[flang-commits] [flang] [NFC][flang][OpenMP] Split `DataSharing` and `Clause` processors (PR #81973)

Sergio Afonso via flang-commits flang-commits at lists.llvm.org
Wed Feb 21 02:12:52 PST 2024


================
@@ -0,0 +1,305 @@
+//===-- Lower/OpenMP/ClauseProcessor.h --------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
+//
+//===----------------------------------------------------------------------===//
+#ifndef FORTRAN_LOWER_CLAUASEPROCESSOR_H
+#define FORTRAN_LOWER_CLAUASEPROCESSOR_H
+
+#include "DirectivesCommon.h"
+#include "ReductionProcessor.h"
+#include "Utils.h"
+#include "flang/Lower/AbstractConverter.h"
+#include "flang/Lower/Bridge.h"
+#include "flang/Optimizer/Builder/Todo.h"
+#include "flang/Parser/dump-parse-tree.h"
+#include "flang/Parser/parse-tree.h"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
+#include "llvm/Support/CommandLine.h"
+
+extern llvm::cl::opt<bool> treatIndexAsSection;
+
+namespace fir {
+class FirOpBuilder;
+} // namespace fir
+
+namespace Fortran {
+namespace lower {
+namespace omp {
+
+using DeclareTargetCapturePair =
+    std::pair<mlir::omp::DeclareTargetCaptureClause,
+              Fortran::semantics::Symbol>;
+
+mlir::omp::MapInfoOp
+createMapInfoOp(fir::FirOpBuilder &builder, mlir::Location loc,
+                mlir::Value baseAddr, mlir::Value varPtrPtr, std::string name,
+                mlir::SmallVector<mlir::Value> bounds,
+                mlir::SmallVector<mlir::Value> members, uint64_t mapType,
+                mlir::omp::VariableCaptureKind mapCaptureType, mlir::Type retTy,
+                bool isVal = false);
+
+void gatherFuncAndVarSyms(
+    const Fortran::parser::OmpObjectList &objList,
+    mlir::omp::DeclareTargetCaptureClause clause,
+    llvm::SmallVectorImpl<DeclareTargetCapturePair> &symbolAndClause);
+
+void genObjectList(const Fortran::parser::OmpObjectList &objectList,
+                   Fortran::lower::AbstractConverter &converter,
+                   llvm::SmallVectorImpl<mlir::Value> &operands);
+
+/// Class that handles the processing of OpenMP clauses.
+///
+/// Its `process<ClauseName>()` methods perform MLIR code generation for their
+/// corresponding clause if it is present in the clause list. Otherwise, they
+/// will return `false` to signal that the clause was not found.
+///
+/// The intended use is of this class is to move clause processing outside of
+/// construct processing, since the same clauses can appear attached to
+/// different constructs and constructs can be combined, so that code
+/// duplication is minimized.
+///
+/// Each construct-lowering function only calls the `process<ClauseName>()`
+/// methods that relate to clauses that can impact the lowering of that
+/// construct.
+class ClauseProcessor {
+  using ClauseTy = Fortran::parser::OmpClause;
+
+public:
+  ClauseProcessor(Fortran::lower::AbstractConverter &converter,
+                  Fortran::semantics::SemanticsContext &semaCtx,
+                  const Fortran::parser::OmpClauseList &clauses)
+      : converter(converter), semaCtx(semaCtx), clauses(clauses) {}
+
+  // 'Unique' clauses: They can appear at most once in the clause list.
+  bool
+  processCollapse(mlir::Location currentLocation,
+                  Fortran::lower::pft::Evaluation &eval,
+                  llvm::SmallVectorImpl<mlir::Value> &lowerBound,
+                  llvm::SmallVectorImpl<mlir::Value> &upperBound,
+                  llvm::SmallVectorImpl<mlir::Value> &step,
+                  llvm::SmallVectorImpl<const Fortran::semantics::Symbol *> &iv,
+                  std::size_t &loopVarTypeSize) const;
+  bool processDefault() const;
+  bool processDevice(Fortran::lower::StatementContext &stmtCtx,
+                     mlir::Value &result) const;
+  bool processDeviceType(mlir::omp::DeclareTargetDeviceType &result) const;
+  bool processFinal(Fortran::lower::StatementContext &stmtCtx,
+                    mlir::Value &result) const;
+  bool processHint(mlir::IntegerAttr &result) const;
+  bool processMergeable(mlir::UnitAttr &result) const;
+  bool processNowait(mlir::UnitAttr &result) const;
+  bool processNumTeams(Fortran::lower::StatementContext &stmtCtx,
+                       mlir::Value &result) const;
+  bool processNumThreads(Fortran::lower::StatementContext &stmtCtx,
+                         mlir::Value &result) const;
+  bool processOrdered(mlir::IntegerAttr &result) const;
+  bool processPriority(Fortran::lower::StatementContext &stmtCtx,
+                       mlir::Value &result) const;
+  bool processProcBind(mlir::omp::ClauseProcBindKindAttr &result) const;
+  bool processSafelen(mlir::IntegerAttr &result) const;
+  bool processSchedule(mlir::omp::ClauseScheduleKindAttr &valAttr,
+                       mlir::omp::ScheduleModifierAttr &modifierAttr,
+                       mlir::UnitAttr &simdModifierAttr) const;
+  bool processScheduleChunk(Fortran::lower::StatementContext &stmtCtx,
+                            mlir::Value &result) const;
+  bool processSimdlen(mlir::IntegerAttr &result) const;
+  bool processThreadLimit(Fortran::lower::StatementContext &stmtCtx,
+                          mlir::Value &result) const;
+  bool processUntied(mlir::UnitAttr &result) const;
+
+  // 'Repeatable' clauses: They can appear multiple times in the clause list.
+  bool
+  processAllocate(llvm::SmallVectorImpl<mlir::Value> &allocatorOperands,
+                  llvm::SmallVectorImpl<mlir::Value> &allocateOperands) const;
+  bool processCopyin() const;
+  bool processDepend(llvm::SmallVectorImpl<mlir::Attribute> &dependTypeOperands,
+                     llvm::SmallVectorImpl<mlir::Value> &dependOperands) const;
+  bool
+  processEnter(llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const;
+  bool
+  processIf(Fortran::parser::OmpIfClause::DirectiveNameModifier directiveName,
+            mlir::Value &result) const;
+  bool
+  processLink(llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const;
+
+  // This method is used to process a map clause.
+  // The optional parameters - mapSymTypes, mapSymLocs & mapSymbols are used to
+  // store the original type, location and Fortran symbol for the map operands.
+  // They may be used later on to create the block_arguments for some of the
+  // target directives that require it.
+  bool processMap(mlir::Location currentLocation,
+                  const llvm::omp::Directive &directive,
+                  Fortran::lower::StatementContext &stmtCtx,
+                  llvm::SmallVectorImpl<mlir::Value> &mapOperands,
+                  llvm::SmallVectorImpl<mlir::Type> *mapSymTypes = nullptr,
+                  llvm::SmallVectorImpl<mlir::Location> *mapSymLocs = nullptr,
+                  llvm::SmallVectorImpl<const Fortran::semantics::Symbol *>
+                      *mapSymbols = nullptr) const;
+  bool
+  processReduction(mlir::Location currentLocation,
+                   llvm::SmallVectorImpl<mlir::Value> &reductionVars,
+                   llvm::SmallVectorImpl<mlir::Attribute> &reductionDeclSymbols,
+                   llvm::SmallVectorImpl<const Fortran::semantics::Symbol *>
+                       *reductionSymbols = nullptr) const;
+  bool processSectionsReduction(mlir::Location currentLocation) const;
+  bool processTo(llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const;
+  bool
+  processUseDeviceAddr(llvm::SmallVectorImpl<mlir::Value> &operands,
+                       llvm::SmallVectorImpl<mlir::Type> &useDeviceTypes,
+                       llvm::SmallVectorImpl<mlir::Location> &useDeviceLocs,
+                       llvm::SmallVectorImpl<const Fortran::semantics::Symbol *>
+                           &useDeviceSymbols) const;
+  bool
+  processUseDevicePtr(llvm::SmallVectorImpl<mlir::Value> &operands,
+                      llvm::SmallVectorImpl<mlir::Type> &useDeviceTypes,
+                      llvm::SmallVectorImpl<mlir::Location> &useDeviceLocs,
+                      llvm::SmallVectorImpl<const Fortran::semantics::Symbol *>
+                          &useDeviceSymbols) const;
+
+  template <typename T>
+  bool processMotionClauses(Fortran::lower::StatementContext &stmtCtx,
----------------
skatrak wrote:

Nit: To improve readability, could you move template definitions here and in other headers other outside the class body?
```c++
class ClauseProcessor {
  ...
  template <typename T>
  bool processMotionClauses(...);
  ...
};

template <typename T>
bool ClauseProcessor::processMotionClauses(...) {
  ...
}
...
```

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


More information about the flang-commits mailing list