62#define GEN_PASS_DEF_ACCDECLAREGPUMODULEINSERTION
63#include "mlir/Dialect/OpenACC/Transforms/Passes.h.inc"
67#define DEBUG_TYPE "acc-declare-gpu-module-insertion"
73static bool hasAccDeclareGlobals(ModuleOp mod) {
74 for (
Operation &op : mod.getBody()->getOperations())
80static void makeDeviceGlobalDeclaration(
Operation &globalOp) {
84 region.dropAllReferences();
85 region.getBlocks().clear();
89class ACCDeclareGPUModuleInsertion
90 :
public acc::impl::ACCDeclareGPUModuleInsertionBase<
91 ACCDeclareGPUModuleInsertion> {
93 using acc::impl::ACCDeclareGPUModuleInsertionBase<
94 ACCDeclareGPUModuleInsertion>::ACCDeclareGPUModuleInsertionBase;
96 LogicalResult copyGlobalsToGPUModule(gpu::GPUModuleOp gpuMod, ModuleOp mod,
100 for (
Operation &globalOp : mod.getBody()->getOperations()) {
104 auto symOp = dyn_cast<SymbolOpInterface>(&globalOp);
108 StringAttr name = symOp.getNameAttr();
112 auto globalVar = dyn_cast<acc::GlobalVariableOpInterface>(&globalOp);
113 bool makeUnifiedDeclaration =
115 declareAttr.getDataClause().getValue() !=
116 acc::DataClause::acc_declare_device_resident &&
117 (!globalVar || !globalVar.isConstant());
118 if (makeUnifiedDeclaration)
119 makeDeviceGlobalDeclaration(*deviceGlobal);
126 return lhs->getName() ==
rhs->getName() &&
133 if (!isEquivalent(existing, deviceGlobal)) {
137 if (makeUnifiedDeclaration) {
139 makeDeviceGlobalDeclaration(*normalizedExisting);
140 bool canReuse = isEquivalent(normalizedExisting, deviceGlobal);
143 makeDeviceGlobalDeclaration(*existing);
146 if (!isEquivalent(existing, deviceGlobal)) {
149 llvm::Twine(
"duplicate global symbol '") +
150 name.getValue() +
"' in gpu module");
163 gpuSymTable.
insert(deviceGlobal);
168 void runOnOperation()
override {
169 ModuleOp mod = getOperation();
173 if (!hasAccDeclareGlobals(mod))
177 std::optional<gpu::GPUModuleOp> gpuMod =
180 accSupport.
emitNYI(mod.getLoc(),
"Failed to create GPU module");
184 if (failed(copyGlobalsToGPUModule(*gpuMod, mod, accSupport)))
Attributes are known-constant values of operations.
Operation is the basic unit of execution within MLIR.
AttrClass getAttrOfType(StringAttr name)
Attribute getAttr(StringAttr name)
Return the specified attribute if present, null otherwise.
Location getLoc()
The source location the operation was defined or derived from.
void destroy()
Destroys this operation and its subclass data.
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
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...
Attribute removeAttr(StringAttr name)
Remove the attribute with the specified name if it exists.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
This class allows for representing and managing the symbol table used by operations with the 'SymbolT...
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.
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...