MLIR  22.0.0git
OpenACCSupport.h
Go to the documentation of this file.
1 //===- OpenACCSupport.h - OpenACC Support Interface -------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the OpenACCSupport analysis interface, which provides
10 // extensible support for OpenACC passes. Custom implementations
11 // can be registered to provide pipeline and dialect-specific information
12 // that cannot be adequately expressed through type or operation interfaces
13 // alone.
14 //
15 // Usage Pattern:
16 // ==============
17 //
18 // A pass that needs this functionality should call
19 // getAnalysis<OpenACCSupport>(), which will provide either:
20 // - A cached version if previously initialized, OR
21 // - A default implementation if not previously initialized
22 //
23 // This analysis is never invalidated (isInvalidated returns false), so it only
24 // needs to be initialized once and will persist throughout the pass pipeline.
25 //
26 // Registering a Custom Implementation:
27 // =====================================
28 //
29 // If a custom implementation is needed, create a pass that runs BEFORE the pass
30 // that needs the analysis. In this setup pass, use
31 // getAnalysis<OpenACCSupport>() followed by setImplementation() to register
32 // your custom implementation. The custom implementation will need to provide
33 // implementation for all methods defined in the `OpenACCSupportTraits::Concept`
34 // class.
35 //
36 // Example:
37 // void MySetupPass::runOnOperation() {
38 // OpenACCSupport &support = getAnalysis<OpenACCSupport>();
39 // support.setImplementation(MyCustomImpl());
40 // }
41 //
42 // void MyAnalysisConsumerPass::runOnOperation() {
43 // OpenACCSupport &support = getAnalysis<OpenACCSupport>();
44 // std::string name = support.getVariableName(someValue);
45 // // ... use the analysis results
46 // }
47 //
48 //===----------------------------------------------------------------------===//
49 
50 #ifndef MLIR_DIALECT_OPENACC_ANALYSIS_OPENACCSUPPORT_H
51 #define MLIR_DIALECT_OPENACC_ANALYSIS_OPENACCSUPPORT_H
52 
53 #include "mlir/IR/Value.h"
55 #include <memory>
56 #include <string>
57 
58 namespace mlir {
59 namespace acc {
60 
61 // Forward declaration for RecipeKind enum
62 enum class RecipeKind : uint32_t;
63 
64 namespace detail {
65 /// This class contains internal trait classes used by OpenACCSupport.
66 /// It follows the Concept-Model pattern used throughout MLIR (e.g., in
67 /// AliasAnalysis and interface definitions).
69  class Concept {
70  public:
71  virtual ~Concept() = default;
72 
73  /// Get the variable name for a given MLIR value.
74  virtual std::string getVariableName(Value v) = 0;
75 
76  /// Get the recipe name for a given kind, type and value.
77  virtual std::string getRecipeName(RecipeKind kind, Type type,
78  Value var) = 0;
79 
80  // Used to report a case that is not supported by the implementation.
81  virtual InFlightDiagnostic emitNYI(Location loc, const Twine &message) = 0;
82  };
83 
84  /// This class wraps a concrete OpenACCSupport implementation and forwards
85  /// interface calls to it. This provides type erasure, allowing different
86  /// implementation types to be used interchangeably without inheritance.
87  template <typename ImplT>
88  class Model final : public Concept {
89  public:
90  explicit Model(ImplT &&impl) : impl(std::forward<ImplT>(impl)) {}
91  ~Model() override = default;
92 
93  std::string getVariableName(Value v) final {
94  return impl.getVariableName(v);
95  }
96 
97  std::string getRecipeName(RecipeKind kind, Type type, Value var) final {
98  return impl.getRecipeName(kind, type, var);
99  }
100 
101  InFlightDiagnostic emitNYI(Location loc, const Twine &message) final {
102  return impl.emitNYI(loc, message);
103  }
104 
105  private:
106  ImplT impl;
107  };
108 };
109 } // namespace detail
110 
111 //===----------------------------------------------------------------------===//
112 // OpenACCSupport
113 //===----------------------------------------------------------------------===//
114 
117  template <typename ImplT>
119 
120 public:
121  OpenACCSupport() = default;
123 
124  /// Register a custom OpenACCSupport implementation. Only one implementation
125  /// can be registered at a time; calling this replaces any existing
126  /// implementation.
127  template <typename AnalysisT>
128  void setImplementation(AnalysisT &&analysis) {
129  impl =
130  std::make_unique<Model<AnalysisT>>(std::forward<AnalysisT>(analysis));
131  }
132 
133  /// Get the variable name for a given value.
134  ///
135  /// \param v The MLIR value to get the variable name for.
136  /// \return The variable name, or an empty string if unavailable.
137  std::string getVariableName(Value v);
138 
139  /// Get the recipe name for a given type and value.
140  ///
141  /// \param kind The kind of recipe to get the name for.
142  /// \param type The type to get the recipe name for. Can be null if the
143  /// var is provided instead.
144  /// \param var The MLIR value to get the recipe name for. Can be null if
145  /// the type is provided instead.
146  /// \return The recipe name, or an empty string if not available.
147  std::string getRecipeName(RecipeKind kind, Type type, Value var);
148 
149  /// Report a case that is not yet supported by the implementation.
150  ///
151  /// \param loc The location to report the unsupported case at.
152  /// \param message The message to report.
153  /// \return An in-flight diagnostic object that can be used to report the
154  /// unsupported case.
155  InFlightDiagnostic emitNYI(Location loc, const Twine &message);
156 
157  /// Signal that this analysis should always be preserved so that
158  /// underlying implementation registration is not lost.
160  return false;
161  }
162 
163 private:
164  /// The registered custom implementation (if any).
165  std::unique_ptr<Concept> impl;
166 };
167 
168 } // namespace acc
169 } // namespace mlir
170 
171 #endif // MLIR_DIALECT_OPENACC_ANALYSIS_OPENACCSUPPORT_H
union mlir::linalg::@1257::ArityGroupAndKind::Kind kind
This class represents a diagnostic that is inflight and set to be reported.
Definition: Diagnostics.h:316
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:76
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
void setImplementation(AnalysisT &&analysis)
Register a custom OpenACCSupport implementation.
InFlightDiagnostic emitNYI(Location loc, const Twine &message)
Report a case that is not yet supported by the implementation.
OpenACCSupport(Operation *op)
bool isInvalidated(const AnalysisManager::PreservedAnalyses &pa)
Signal that this analysis should always be preserved so that underlying implementation registration i...
std::string getVariableName(Value v)
Get the variable name for a given value.
std::string getRecipeName(RecipeKind kind, Type type, Value var)
Get the recipe name for a given type and value.
virtual std::string getRecipeName(RecipeKind kind, Type type, Value var)=0
Get the recipe name for a given kind, type and value.
virtual std::string getVariableName(Value v)=0
Get the variable name for a given MLIR value.
virtual InFlightDiagnostic emitNYI(Location loc, const Twine &message)=0
This class wraps a concrete OpenACCSupport implementation and forwards interface calls to it.
InFlightDiagnostic emitNYI(Location loc, const Twine &message) final
std::string getRecipeName(RecipeKind kind, Type type, Value var) final
Get the recipe name for a given kind, type and value.
std::string getVariableName(Value v) final
Get the variable name for a given MLIR value.
A utility class to represent the analyses that are known to be preserved.
detail::InFlightRemark analysis(Location loc, RemarkOpts opts)
Report an optimization analysis remark.
Definition: Remarks.h:567
Include the generated interface declarations.
This class contains internal trait classes used by OpenACCSupport.