MLIR 24.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
46static Value getGPUSizeFromLaunch(gpu::LaunchOp launch,
47 gpu::Processor processor) {
48 gpu::KernelDim3 gridSize = launch.getGridSize();
49 gpu::KernelDim3 blockSize = launch.getBlockSize();
50 switch (processor) {
51 case gpu::Processor::ThreadX:
52 return blockSize.x;
53 case gpu::Processor::ThreadY:
54 return blockSize.y;
55 case gpu::Processor::ThreadZ:
56 return blockSize.z;
57 case gpu::Processor::BlockX:
58 return gridSize.x;
59 case gpu::Processor::BlockY:
60 return gridSize.y;
61 case gpu::Processor::BlockZ:
62 return gridSize.z;
63 default:
64 return {};
65 }
66}
67
68Value getGPUSize(gpu::Processor processor, gpu::LaunchOp launch,
69 const llvm::DenseMap<gpu::Processor, Value> &dimensionOps) {
70 if (launch)
71 return getGPUSizeFromLaunch(launch, processor);
72 assert(!dimensionOps.empty() && "dimension map is empty");
73 return dimensionOps.lookup(processor);
74}
75
76static Value getGPUThreadIdFromLaunch(gpu::LaunchOp launch,
77 gpu::Processor processor) {
78 gpu::KernelDim3 blockIds = launch.getBlockIds();
79 gpu::KernelDim3 threadIds = launch.getThreadIds();
80 switch (processor) {
81 case gpu::Processor::ThreadX:
82 return threadIds.x;
83 case gpu::Processor::ThreadY:
84 return threadIds.y;
85 case gpu::Processor::ThreadZ:
86 return threadIds.z;
87 case gpu::Processor::BlockX:
88 return blockIds.x;
89 case gpu::Processor::BlockY:
90 return blockIds.y;
91 case gpu::Processor::BlockZ:
92 return blockIds.z;
93 default:
94 return {};
95 }
96}
97
98Value getGPUThreadId(gpu::Processor processor, gpu::LaunchOp launch,
100 if (launch)
101 return getGPUThreadIdFromLaunch(launch, processor);
102 assert(!indexOps.empty() && "index map is empty");
103 return indexOps.lookup(processor);
104}
105
106} // namespace acc
107} // namespace mlir
OpListType::iterator iterator
Definition Block.h:164
This class helps build Operations.
Definition Builders.h:210
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.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Value getGPUSize(gpu::Processor processor, gpu::LaunchOp launch, const llvm::DenseMap< gpu::Processor, Value > &dimensionOps)
Return the launch dimension for processor from launch, or from dimensionOps when launch is null.
std::optional< gpu::GPUModuleOp > getOrCreateGPUModule(ModuleOp mod, bool create=true, llvm::StringRef name=kDefaultGPUModuleName)
Get or create a GPU module in the given module.
static Value getGPUSizeFromLaunch(gpu::LaunchOp launch, gpu::Processor processor)
Value getGPUThreadId(gpu::Processor processor, gpu::LaunchOp launch, const llvm::DenseMap< gpu::Processor, Value > &indexOps)
Return the thread/block index for processor from launch, or from indexOps when launch is null.
static Value getGPUThreadIdFromLaunch(gpu::LaunchOp launch, gpu::Processor processor)
constexpr llvm::StringLiteral kDefaultGPUModuleName
Default GPU module name used by OpenACC.
Include the generated interface declarations.
Utility class for the GPU dialect to represent triples of Values accessible through ....
Definition GPUDialect.h:39