MLIR 23.0.0git
OpenACCUtilsGPU.cpp
Go to the documentation of this file.
1//===- OpenACCUtilsGPU.cpp - OpenACC GPU Utilities ------------------------===//
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 implements utility functions for OpenACC that depend on the GPU
10// dialect.
11//
12//===----------------------------------------------------------------------===//
13
15#include "mlir/IR/SymbolTable.h"
16
17namespace mlir {
18namespace acc {
19
20std::optional<gpu::GPUModuleOp> getOrCreateGPUModule(ModuleOp mod, bool create,
21 llvm::StringRef name) {
22 // Use default name if provided name is empty
23 llvm::StringRef moduleName =
24 name.empty() ? llvm::StringRef(kDefaultGPUModuleName) : name;
25
26 // Look for existing GPU module with the specified name
27 SymbolTable symTab(mod);
28 if (auto gpuMod = symTab.lookup<gpu::GPUModuleOp>(moduleName))
29 return gpuMod;
30
31 if (!create)
32 return std::nullopt;
33
34 // Create a new GPU module
35 auto *ctx = mod.getContext();
36 mod->setAttr(gpu::GPUDialect::getContainerModuleAttrName(),
37 UnitAttr::get(ctx));
38
39 OpBuilder builder(ctx);
40 auto gpuMod = gpu::GPUModuleOp::create(builder, mod.getLoc(), moduleName);
41 Block::iterator insertPt(mod.getBodyRegion().front().end());
42 symTab.insert(gpuMod, insertPt);
43 return gpuMod;
44}
45
46} // namespace acc
47} // namespace mlir
OpListType::iterator iterator
Definition Block.h:150
This class helps build Operations.
Definition Builders.h:209
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.
std::optional< gpu::GPUModuleOp > getOrCreateGPUModule(ModuleOp mod, bool create=true, llvm::StringRef name=kDefaultGPUModuleName)
Get or create a GPU module in the given module.
constexpr llvm::StringLiteral kDefaultGPUModuleName
Default GPU module name used by OpenACC.
Include the generated interface declarations.