MLIR 24.0.0git
Utils.cpp
Go to the documentation of this file.
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
14
19
20using namespace mlir;
21
23 ModuleOp module, const OffloadModuleOpts &opts) {
24 if (auto offloadMod = llvm::dyn_cast<OffloadModuleInterface>(*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(opts.targetDebugKind, opts.assumeTeamsOversubscription,
34 opts.openMPDeviceVersion, opts.noGPULib);
35 if (opts.isTargetDevice && !opts.hostIRFile.empty())
36 offloadMod.setHostIRFilePath(opts.hostIRFile);
37
38 auto strTriples = llvm::to_vector(
39 llvm::map_range(opts.targetTriples, [](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 StringAttr::get(module.getContext(), llvm::Twine{"omp.version"}),
49 VersionAttr::get(module.getContext(), version));
50}
51
52void mlir::omp::setOpenMPIntegerWrapAround(ModuleOp module, bool value) {
53 module->setAttr(StringAttr::get(module.getContext(),
54 llvm::Twine{"omp.integer_wrap_around"}),
55 IntegerWrapAroundAttr::get(module.getContext(), value));
56}
57
59 int64_t fallback) {
60 if (Attribute verAttr = module->getDiscardableAttr("omp.version"))
61 return llvm::cast<VersionAttr>(verAttr).getVersion();
62 return fallback;
63}
64
65bool mlir::omp::isOpenMPModule(ModuleOp module) {
66 return module->hasDiscardableAttr("omp.version");
67}
68
69static bool allocaUseRequiresSharedMem(const OpOperand &use) {
70 Operation *owner = use.getOwner();
71 if (auto parallelOp = dyn_cast<omp::ParallelOp>(owner)) {
72 if (llvm::is_contained(parallelOp.getReductionVars(), use.get()))
73 return true;
74 } else if (auto callOp = dyn_cast<CallOpInterface>(owner)) {
75 if (llvm::is_contained(callOp.getArgOperands(), 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>(owner)) {
83 OperandRange privateVars = argIface.getPrivateVars();
84 auto it = llvm::find(privateVars, use.get());
85 if (it != privateVars.end()) {
86 ArrayAttr privateSyms = *argIface.getPrivateSyms();
87 size_t idx = std::distance(privateVars.begin(), it);
88 auto privateOp =
90 owner, cast<SymbolRefAttr>(privateSyms[idx]));
91 return privateOp.getDataSharingType() !=
92 omp::DataSharingClauseType::Private;
93 }
94 }
95 return true;
96 }
97 return false;
98}
99
101 for (const OpOperand &use : alloc.getUses()) {
102 Operation *owner = use.getOwner();
103 if (isa<LLVM::AddrSpaceCastOp, LLVM::GEPOp>(owner)) {
104 if (llvm::any_of(owner->getResults(), [&](Value result) {
105 return allocaUsesRequireSharedMem(result);
106 }))
107 return true;
108 } else if (allocaUseRequiresSharedMem(use)) {
109 return true;
110 }
111 }
112 return false;
113}
114
116 if (isa<omp::ParallelOp>(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(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>(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}
static bool allocaUseRequiresSharedMem(const OpOperand &use)
Definition Utils.cpp:69
ArrayAttr()
Attributes are known-constant values of operations.
Definition Attributes.h:25
IRValueT get() const
Return the current value being used by this operand.
This class represents an operand of an operation.
Definition Value.h:256
This class implements the operand iterators for the Operation class.
Definition ValueRange.h:44
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
Definition Operation.h:255
result_range getResults()
Definition Operation.h:442
static Operation * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
use_range getUses() const
Returns a range of all uses, which is useful for iterating over all uses.
Definition Value.h:188
Operation * getOwner() const
Return the owner of this operand.
Definition UseDefLists.h:38
void setOffloadModuleInterfaceAttributes(ModuleOp module, const OffloadModuleOpts &opts)
Sets OpenMP offload module interface attributes on a ModuleOp, shared between Flang and Clang (CIR) f...
Definition Utils.cpp:22
bool isOpenMPModule(ModuleOp module)
Checks whether this is an OpenMP-enabled module.
Definition Utils.cpp:65
int64_t getOpenMPVersionAttribute(ModuleOp module, int64_t fallback=-1)
Returns the value of the omp.version attribute, if present, or the fallback.
Definition Utils.cpp:58
bool opInSharedDeviceContext(Operation &op)
Check whether the given operation is located in a context where an allocation to be used by multiple ...
Definition Utils.cpp:115
void setOpenMPVersionAttribute(ModuleOp module, int64_t version)
Adds or updates the omp.version attribute.
Definition Utils.cpp:46
void setOpenMPIntegerWrapAround(ModuleOp module, bool value)
Add the omp.integer_wrap_around attribute.
Definition Utils.cpp:52
bool allocaUsesRequireSharedMem(Value alloc)
Check whether the value representing an allocation, assumed to have been defined in a shared device c...
Definition Utils.cpp:100
Include the generated interface declarations.
Offload-specific OpenMP module attributes, associated to the OffloadModuleInterface.
Definition Utils.h:29
std::vector< llvm::Triple > targetTriples
Definition Utils.h:58