MLIR 24.0.0git
ACCDeclareGPUModuleInsertion.cpp
Go to the documentation of this file.
1//===- ACCDeclareGPUModuleInsertion.cpp -----------------------------------===//
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 pass copies globals marked with the `acc.declare` attribute into the
10// GPU module so that device code (e.g. acc routine, compute regions) can
11// reference them.
12//
13// Overview:
14// ---------
15// Globals that have the `acc.declare` attribute (from the OpenACC declare
16// directive or from the `ACCImplicitDeclare` pass) must be present in the
17// GPU module for device code to use them. This pass inserts copies of those
18// globals into the GPU module, creating the module if it does not yet exist.
19// The host copy of each global remains in the parent module.
20//
21// Example:
22// --------
23//
24// Before:
25// module {
26// memref.global @arr : memref<7xf32> = dense<0.0>
27// {acc.declare = #acc.declare<dataClause = acc_create>}
28// }
29//
30// After:
31// module attributes {gpu.container_module} {
32// memref.global @arr : memref<7xf32> = dense<0.0>
33// {acc.declare = #acc.declare<dataClause = acc_create>}
34// gpu.module @acc_gpu_module {
35// memref.global @arr : memref<7xf32> = dense<0.0>
36// {acc.declare = #acc.declare<dataClause = acc_create>}
37// }
38// }
39//
40// Requirements:
41// -------------
42// The pass uses the `acc::OpenACCSupport` for:
43// - getOrCreateGPUModule: to obtain or create the GPU module.
44// - emitNYI: to report failure when GPU module creation is not supported.
45// If no custom implementation is registered, the default implementation is
46// used (see OpenACCSupport).
47//
48//===----------------------------------------------------------------------===//
49
54#include "mlir/IR/BuiltinOps.h"
55#include "mlir/IR/Operation.h"
57#include "mlir/IR/SymbolTable.h"
58
59namespace mlir {
60namespace acc {
61#define GEN_PASS_DEF_ACCDECLAREGPUMODULEINSERTION
62#include "mlir/Dialect/OpenACC/Transforms/Passes.h.inc"
63} // namespace acc
64} // namespace mlir
65
66#define DEBUG_TYPE "acc-declare-gpu-module-insertion"
67
68using namespace mlir;
69
70namespace {
71
72static bool hasAccDeclareGlobals(ModuleOp mod) {
73 for (Operation &op : mod.getBody()->getOperations())
74 if (op.getDiscardableAttr(acc::getDeclareAttrName()))
75 return true;
76 return false;
77}
78
79static void makeDeviceGlobalDeclaration(Operation &globalOp) {
80 globalOp.setInherentAttr(StringAttr::get(globalOp.getContext(), "initVal"),
81 {});
82 globalOp.setInherentAttr(StringAttr::get(globalOp.getContext(), "linkage"),
83 {});
84 for (Region &region : globalOp.getRegions()) {
85 region.dropAllReferences();
86 region.getBlocks().clear();
87 }
88}
89
90class ACCDeclareGPUModuleInsertion
92 ACCDeclareGPUModuleInsertion> {
93public:
95 ACCDeclareGPUModuleInsertion>::ACCDeclareGPUModuleInsertionBase;
96
97 LogicalResult copyGlobalsToGPUModule(gpu::GPUModuleOp gpuMod, ModuleOp mod,
98 acc::OpenACCSupport &accSupport) const {
99 SymbolTable gpuSymTable(gpuMod);
100
101 for (Operation &globalOp : mod.getBody()->getOperations()) {
103 continue;
104
105 auto symOp = dyn_cast<SymbolOpInterface>(&globalOp);
106 if (!symOp)
107 continue;
108
109 StringAttr name = symOp.getNameAttr();
110 Operation *deviceGlobal = globalOp.clone();
111 auto declareAttr = globalOp.getDiscardableAttrOfType<acc::DeclareAttr>(
113 auto globalVar = dyn_cast<acc::GlobalVariableOpInterface>(&globalOp);
114 bool makeUnifiedDeclaration =
115 cudaUnified &&
116 declareAttr.getDataClause().getValue() !=
117 acc::DataClause::acc_declare_device_resident &&
118 (!globalVar || !globalVar.isConstant() ||
119 globalVar.isCompilerGenerated());
120 if (makeUnifiedDeclaration)
121 makeDeviceGlobalDeclaration(*deviceGlobal);
122
123 if (Operation *existing = gpuSymTable.lookup(name.getValue())) {
124 // Reuse when structurally equivalent ignoring locations and discardable
125 // attrs such as `acc.declare` attributes. Only a different op type or a
126 // true definition mismatch is a conflict.
127 auto isEquivalent = [](Operation *lhs, Operation *rhs) {
128 return lhs->getName() == rhs->getName() &&
131 /*markEquivalent=*/nullptr,
134 };
135 if (!isEquivalent(existing, deviceGlobal)) {
136 // Earlier GPU lowering can create a global in the GPU module before
137 // this pass. In unified memory, convert an equivalent pre-existing
138 // global to the declaration form expected for an OpenACC global.
139 if (makeUnifiedDeclaration) {
140 Operation *normalizedExisting = existing->clone();
141 makeDeviceGlobalDeclaration(*normalizedExisting);
142 bool canReuse = isEquivalent(normalizedExisting, deviceGlobal);
143 normalizedExisting->destroy();
144 if (canReuse)
145 makeDeviceGlobalDeclaration(*existing);
146 }
147 }
148 if (!isEquivalent(existing, deviceGlobal)) {
149 deviceGlobal->destroy();
150 accSupport.emitNYI(globalOp.getLoc(),
151 llvm::Twine("duplicate global symbol '") +
152 name.getValue() + "' in gpu module");
153 return failure();
154 }
155 // Propagate acc.declare onto the GPU copy if it was cloned before the
156 // host global was marked.
157 if (!existing->getDiscardableAttr(acc::getDeclareAttrName()))
158 if (Attribute declareAttr =
160 existing->setDiscardableAttr(acc::getDeclareAttrName(),
161 declareAttr);
162 deviceGlobal->destroy();
163 continue;
164 }
165
166 gpuSymTable.insert(deviceGlobal);
167 }
168 return success();
169 }
170
171 void runOnOperation() override {
172 ModuleOp mod = getOperation();
173
174 // Check for any candidates first - do this to avoid creating the GPU module
175 // if there are no candidates.
176 if (!hasAccDeclareGlobals(mod))
177 return;
178
179 acc::OpenACCSupport &accSupport = getAnalysis<acc::OpenACCSupport>();
180 std::optional<gpu::GPUModuleOp> gpuMod =
181 accSupport.getOrCreateGPUModule(mod);
182 if (!gpuMod) {
183 accSupport.emitNYI(mod.getLoc(), "Failed to create GPU module");
184 return;
185 }
186
187 if (failed(copyGlobalsToGPUModule(*gpuMod, mod, accSupport)))
188 return;
189 }
190};
191
192} // namespace
return success()
Attributes are known-constant values of operations.
Definition Attributes.h:25
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
void setInherentAttr(StringAttr name, Attribute value)
Set an inherent attribute by name.
Attribute getDiscardableAttr(StringRef name)
Access a discardable attribute by name, returns a null Attribute if the discardable attribute does no...
Definition Operation.h:485
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:240
void destroy()
Destroys this operation and its subclass data.
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
Definition Operation.h:729
AttrClass getDiscardableAttrOfType(StringRef name)
Access a discardable attribute by name and cast it to AttrClass.
Definition Operation.h:493
Operation * clone(IRMapping &mapper, const CloneOptions &options=CloneOptions::all())
Create a deep copy of this operation, remapping any operands that use values outside of the operation...
MLIRContext * getContext()
Return the context this operation is associated with.
Definition Operation.h:233
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
This class allows for representing and managing the symbol table used by operations with the 'SymbolT...
Definition SymbolTable.h:24
Operation * lookup(StringRef name) const
Look up a symbol with the specified name, returning null if no such name exists.
StringAttr insert(Operation *symbol, Block::iterator insertPt={})
Insert a new symbol into the table, and rename it as necessary to avoid collisions.
InFlightDiagnostic emitNYI(Location loc, const Twine &message)
Report a case that is not yet supported by the implementation.
std::optional< gpu::GPUModuleOp > getOrCreateGPUModule(ModuleOp mod, bool create=true, llvm::StringRef name="")
Get or optionally create a GPU module in the given module.
static constexpr StringLiteral getDeclareAttrName()
Used to obtain the attribute name for declare.
Definition OpenACC.h:177
Include the generated interface declarations.
static bool isEquivalentTo(Operation *lhs, Operation *rhs, function_ref< LogicalResult(Value, Value)> checkEquivalent, function_ref< void(Value, Value)> markEquivalent=nullptr, Flags flags=Flags::None, function_ref< LogicalResult(ValueRange, ValueRange)> checkCommutativeEquivalent=nullptr)
Compare two operations (including their regions) and return if they are equivalent.
static LogicalResult ignoreValueEquivalence(Value lhs, Value rhs)
Helper that can be used with isEquivalentTo above to consider ops equivalent even if their operands a...