| 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 | |
| 14 | #include "mlir/Dialect/OpenACC/OpenACCUtilsGPU.h" |
| 15 | #include "mlir/IR/SymbolTable.h" |
| 16 | |
| 17 | namespace mlir { |
| 18 | namespace acc { |
| 19 | |
| 20 | std::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>(name: 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->setDiscardableAttr(name: gpu::GPUDialect::getContainerModuleAttrName(), |
| 37 | value: UnitAttr::get(context: ctx)); |
| 38 | |
| 39 | OpBuilder builder(ctx); |
| 40 | auto gpuMod = gpu::GPUModuleOp::create(builder, location: mod.getLoc(), name: moduleName); |
| 41 | Block::iterator insertPt(mod.getBodyRegion().front().end()); |
| 42 | symTab.insert(symbol: gpuMod, insertPt); |
| 43 | return gpuMod; |
| 44 | } |
| 45 | |
| 46 | static 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 | |
| 68 | Value 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(Val: processor); |
| 74 | } |
| 75 | |
| 76 | static 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 | |
| 98 | Value getGPUThreadId(gpu::Processor processor, gpu::LaunchOp launch, |
| 99 | const llvm::DenseMap<gpu::Processor, Value> &indexOps) { |
| 100 | if (launch) |
| 101 | return getGPUThreadIdFromLaunch(launch, processor); |
| 102 | assert(!indexOps.empty() && "index map is empty" ); |
| 103 | return indexOps.lookup(Val: processor); |
| 104 | } |
| 105 | |
| 106 | } // namespace acc |
| 107 | } // namespace mlir |
| 108 | |