1//===- StackToShared.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 file implements various OpenMP dialect utilities.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Dialect/OpenMP/Utils/Utils.h"
14
15#include "mlir/Dialect/LLVMIR/LLVMConversionOps.h"
16#include "mlir/Dialect/LLVMIR/LLVMMemoryOps.h"
17#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
18#include "mlir/Interfaces/CallInterfaces.h"
19
20using namespace mlir;
21
22void mlir::omp::setOffloadModuleInterfaceAttributes(
23 ModuleOp module, const OffloadModuleOpts &opts) {
24 if (auto offloadMod = llvm::dyn_cast<OffloadModuleInterface>(Val&: *module)) {
25 offloadMod.setIsTargetDevice(opts.isTargetDevice);
26 offloadMod.setIsGPU(opts.isGPU);
27 if (opts.forceUSM)
28 offloadMod.setRequires(offloadMod.getRequires() |
29 ClauseRequires::unified_shared_memory);
30 offloadMod.setFlags(debugKind: opts.targetDebugKind, assumeTeamsOversubscription: opts.assumeTeamsOversubscription,
31 assumeThreadsOversubscription: opts.assumeThreadsOversubscription,
32 assumeNoThreadState: opts.assumeNoThreadState,
33 assumeNoNestedParallelism: opts.assumeNoNestedParallelism,
34 openmpDeviceVersion: opts.openMPDeviceVersion, noGPULib: opts.noGPULib);
35 if (opts.isTargetDevice && !opts.hostIRFile.empty())
36 offloadMod.setHostIRFilePath(opts.hostIRFile);
37
38 auto strTriples = llvm::to_vector(
39 Range: llvm::map_range(C: opts.targetTriples, F: [](const llvm::Triple &triple) {
40 return triple.normalize();
41 }));
42 offloadMod.setTargetTriples(strTriples);
43 }
44}
45
46void mlir::omp::setOpenMPVersionAttribute(ModuleOp module, int64_t version) {
47 module->setDiscardableAttr(
48 name: StringAttr::get(context: module.getContext(), bytes: llvm::Twine{"omp.version"}),
49 value: VersionAttr::get(context: module.getContext(), version));
50}
51
52void mlir::omp::setOpenMPIntegerWrapAround(ModuleOp module, bool value) {
53 module->setAttr(name: StringAttr::get(context: module.getContext(),
54 bytes: llvm::Twine{"omp.integer_wrap_around"}),
55 value: IntegerWrapAroundAttr::get(context: module.getContext(), integer_wrap_around: value));
56}
57
58int64_t mlir::omp::getOpenMPVersionAttribute(ModuleOp module,
59 int64_t fallback) {
60 if (Attribute verAttr = module->getDiscardableAttr(name: "omp.version"))
61 return llvm::cast<VersionAttr>(Val&: verAttr).getVersion();
62 return fallback;
63}
64
65bool mlir::omp::isOpenMPModule(ModuleOp module) {
66 return module->hasDiscardableAttr(name: "omp.version");
67}
68
69static bool allocaUseRequiresSharedMem(const OpOperand &use) {
70 Operation *owner = use.getOwner();
71 if (auto parallelOp = dyn_cast<omp::ParallelOp>(Val: owner)) {
72 if (llvm::is_contained(Range: parallelOp.getReductionVars(), Element: use.get()))
73 return true;
74 } else if (auto callOp = dyn_cast<CallOpInterface>(Val: owner)) {
75 if (llvm::is_contained(Range: callOp.getArgOperands(), Element: use.get()))
76 return true;
77 }
78
79 // If it is used directly inside of a parallel region, it has to be replaced
80 // unless the use is a private clause.
81 if (owner->getParentOfType<omp::ParallelOp>()) {
82 if (auto argIface = dyn_cast<omp::BlockArgOpenMPOpInterface>(Val: owner)) {
83 OperandRange privateVars = argIface.getPrivateVars();
84 auto it = llvm::find(Range&: privateVars, Val: use.get());
85 if (it != privateVars.end()) {
86 ArrayAttr privateSyms = *argIface.getPrivateSyms();
87 size_t idx = std::distance(first: privateVars.begin(), last: it);
88 auto privateOp =
89 SymbolTable::lookupNearestSymbolFrom<omp::PrivateClauseOp>(
90 from: owner, symbol: cast<SymbolRefAttr>(Val: privateSyms[idx]));
91 return privateOp.getDataSharingType() !=
92 omp::DataSharingClauseType::Private;
93 }
94 }
95 return true;
96 }
97 return false;
98}
99
100bool mlir::omp::allocaUsesRequireSharedMem(Value alloc) {
101 for (const OpOperand &use : alloc.getUses()) {
102 Operation *owner = use.getOwner();
103 if (isa<LLVM::AddrSpaceCastOp, LLVM::GEPOp>(Val: owner)) {
104 if (llvm::any_of(Range: owner->getResults(), P: [&](Value result) {
105 return allocaUsesRequireSharedMem(alloc: result);
106 }))
107 return true;
108 } else if (allocaUseRequiresSharedMem(use)) {
109 return true;
110 }
111 }
112 return false;
113}
114
115bool mlir::omp::opInSharedDeviceContext(Operation &op) {
116 if (isa<omp::ParallelOp>(Val: op))
117 return false;
118
119 auto offloadIface = op.getParentOfType<omp::OffloadModuleInterface>();
120 if (!offloadIface || !offloadIface.getIsTargetDevice())
121 return false;
122
123 auto targetOp = op.getParentOfType<omp::TargetOp>();
124
125 // It must be inside of a generic omp.target or in a target device function,
126 // and not inside of omp.parallel.
127 if (auto parallelOp = op.getParentOfType<omp::ParallelOp>()) {
128 if (!targetOp || targetOp->isProperAncestor(other: parallelOp))
129 return false;
130 }
131
132 // The omp.target operation itself is considered in a shared device context in
133 // order to properly process its own allocation-defining entry block
134 // arguments.
135 if (!targetOp)
136 targetOp = dyn_cast<omp::TargetOp>(Val&: op);
137
138 if (targetOp) {
139 if (targetOp.getKernelType() != omp::TargetExecMode::generic)
140 return false;
141 } else {
142 auto declTargetIface = op.getParentOfType<omp::DeclareTargetInterface>();
143 omp::DeclareTargetAttr declTargetAttr =
144 declTargetIface ? declTargetIface.getDeclareTarget() : nullptr;
145 if (!declTargetAttr ||
146 declTargetAttr.getDeviceType() == omp::DeclareTargetDeviceType::host)
147 return false;
148 }
149 return true;
150}
151