MLIR 23.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
17
18using namespace mlir;
19
20static bool allocaUseRequiresSharedMem(const OpOperand &use) {
21 Operation *owner = use.getOwner();
22 if (auto parallelOp = dyn_cast<omp::ParallelOp>(owner)) {
23 if (llvm::is_contained(parallelOp.getReductionVars(), use.get()))
24 return true;
25 } else if (auto callOp = dyn_cast<CallOpInterface>(owner)) {
26 if (llvm::is_contained(callOp.getArgOperands(), use.get()))
27 return true;
28 }
29
30 // If it is used directly inside of a parallel region, it has to be replaced
31 // unless the use is a private clause.
32 if (owner->getParentOfType<omp::ParallelOp>()) {
33 if (auto argIface = dyn_cast<omp::BlockArgOpenMPOpInterface>(owner)) {
34 OperandRange privateVars = argIface.getPrivateVars();
35 auto it = llvm::find(privateVars, use.get());
36 if (it != privateVars.end()) {
37 auto privateSyms = owner->getAttrOfType<ArrayAttr>("private_syms");
38 size_t idx = std::distance(privateVars.begin(), it);
39 auto privateOp =
41 owner, cast<SymbolRefAttr>(privateSyms[idx]));
42 return privateOp.getDataSharingType() !=
43 omp::DataSharingClauseType::Private;
44 }
45 }
46 return true;
47 }
48 return false;
49}
50
52 for (const OpOperand &use : alloc.getUses()) {
53 Operation *owner = use.getOwner();
54 if (isa<LLVM::AddrSpaceCastOp, LLVM::GEPOp>(owner)) {
55 if (llvm::any_of(owner->getResults(), [&](Value result) {
56 return allocaUsesRequireSharedMem(result);
57 }))
58 return true;
59 } else if (allocaUseRequiresSharedMem(use)) {
60 return true;
61 }
62 }
63 return false;
64}
65
67 if (isa<omp::ParallelOp>(op))
68 return false;
69
70 auto offloadIface = op.getParentOfType<omp::OffloadModuleInterface>();
71 if (!offloadIface || !offloadIface.getIsTargetDevice())
72 return false;
73
74 auto targetOp = op.getParentOfType<omp::TargetOp>();
75
76 // It must be inside of a generic omp.target or in a target device function,
77 // and not inside of omp.parallel.
78 if (auto parallelOp = op.getParentOfType<omp::ParallelOp>()) {
79 if (!targetOp || targetOp->isProperAncestor(parallelOp))
80 return false;
81 }
82
83 // The omp.target operation itself is considered in a shared device context in
84 // order to properly process its own allocation-defining entry block
85 // arguments.
86 if (!targetOp)
87 targetOp = dyn_cast<omp::TargetOp>(op);
88
89 if (targetOp) {
90 if (targetOp.getKernelExecFlags(targetOp.getInnermostCapturedOmpOp()) !=
91 omp::TargetExecMode::generic)
92 return false;
93 } else {
94 auto declTargetIface = op.getParentOfType<omp::DeclareTargetInterface>();
95 if (!declTargetIface || !declTargetIface.isDeclareTarget() ||
96 declTargetIface.getDeclareTargetDeviceType() ==
97 omp::DeclareTargetDeviceType::host)
98 return false;
99 }
100 return true;
101}
static bool allocaUseRequiresSharedMem(const OpOperand &use)
Definition Utils.cpp:20
ArrayAttr()
IRValueT get() const
Return the current value being used by this operand.
This class represents an operand of an operation.
Definition Value.h:254
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:88
AttrClass getAttrOfType(StringAttr name)
Definition Operation.h:576
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
Definition Operation.h:256
result_range getResults()
Definition Operation.h:441
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
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:66
bool allocaUsesRequireSharedMem(Value alloc)
Check whether the value representing an allocation, assumed to have been defined in a shared device c...
Definition Utils.cpp:51
Include the generated interface declarations.