68#include "llvm/ADT/STLExtras.h"
69#include "llvm/ADT/SetVector.h"
70#include "llvm/Support/Debug.h"
74#define GEN_PASS_DEF_ACCIFCLAUSELOWERING
75#include "mlir/Dialect/OpenACC/Transforms/Passes.h.inc"
79#define DEBUG_TYPE "acc-if-clause-lowering"
86class ACCIfClauseLowering
87 :
public acc::impl::ACCIfClauseLoweringBase<ACCIfClauseLowering> {
88 using ACCIfClauseLoweringBase<ACCIfClauseLowering>::ACCIfClauseLoweringBase;
95 template <
typename OpTy>
97 lowerIfClauseForComputeConstruct(OpTy computeConstructOp,
102 void runOnOperation()
override;
105void ACCIfClauseLowering::convertHostRegion(
Operation *computeOp,
110 if (isa<acc::OpenACCDialect>(op->getDialect()))
111 hostOps.push_back(op);
121 accSupport->
emitNYI(computeOp->
getLoc(),
"failed to convert host region");
126template <
typename OpTy>
127void ACCIfClauseLowering::lowerIfClauseForComputeConstruct(
130 Value ifCond = computeConstructOp.getIfCond();
136 LLVM_DEBUG(llvm::dbgs() <<
"Converting " << computeConstructOp->getName()
137 <<
" with if condition: " << computeConstructOp
150 auto isExternallyOwned = [&](
Operation *dataOp) {
152 if (!isa<ACC_DATA_EXIT_OPS>(user) && user != computeConstructOp &&
153 !computeConstructOp->isAncestor(user))
159 for (
Value operand : computeConstructOp.getDataClauseOperands())
160 if (
Operation *defOp = operand.getDefiningOp())
161 if (isa<ACC_DATA_ENTRY_OPS>(defOp))
162 dataEntryOps.insert(defOp);
167 for (
Operation *dataEntryOp : llvm::reverse(dataEntryOps))
168 if (!isExternallyOwned(dataEntryOp))
170 if (isa<ACC_DATA_EXIT_OPS>(user))
171 dataExitOps.insert(user);
175 for (
Value operand : operands)
176 if (
Operation *defOp = operand.getDefiningOp())
177 ops.push_back(defOp);
179 collectOps(firstprivateOps, computeConstructOp.getFirstprivateOperands());
180 collectOps(privateOps, computeConstructOp.getPrivateOperands());
181 collectOps(reductionOps, computeConstructOp.getReductionOperands());
184 auto ifOp = scf::IfOp::create(rewriter, computeConstructOp.getLoc(),
187 LLVM_DEBUG(llvm::dbgs() <<
"Cloning " << dataEntryOps.size()
188 <<
" data entry operations for device path\n");
191 Block &thenBlock = ifOp.getThenRegion().
front();
206 operands.push_back(clonedOp->
getResult(0));
207 deviceMapping.
map(op->getResult(0), clonedOp->
getResult(0));
213 if (isExternallyOwned(op))
216 deviceMapping.
map(op->getResult(0), clonedOp->
getResult(0));
220 for (
Value operand : computeConstructOp.getDataClauseOperands())
222 cloneAndMapOps(firstprivateOps, firstprivateOperands);
223 cloneAndMapOps(privateOps, privateOperands);
224 cloneAndMapOps(reductionOps, reductionOperands);
228 OpTy newComputeOp = cast<OpTy>(
229 rewriter.
clone(*computeConstructOp.getOperation(), deviceMapping));
230 newComputeOp.getIfCondMutable().clear();
231 newComputeOp.getDataClauseOperandsMutable().assign(deviceDataOperands);
232 newComputeOp.getFirstprivateOperandsMutable().assign(firstprivateOperands);
233 newComputeOp.getPrivateOperandsMutable().assign(privateOperands);
234 newComputeOp.getReductionOperandsMutable().assign(reductionOperands);
239 rewriter.
clone(*dataOp, deviceMapping);
243 scf::YieldOp::create(rewriter, computeConstructOp.getLoc());
246 Region &hostRegion = computeConstructOp.getRegion();
249 ifOp.getElseRegion().takeBody(hostRegion);
252 Block &elseBlock = ifOp.getElseRegion().
front();
255 scf::YieldOp::create(rewriter, computeConstructOp.getLoc());
257 convertHostRegion(computeConstructOp, ifOp.getElseRegion());
261 Block &elseBlock = ifOp.getElseRegion().
front();
265 hostRegion, hostMapping, computeConstructOp.getLoc(), rewriter);
266 convertHostRegion(computeConstructOp, hostExecuteRegion.getRegion());
270 eraseOps.insert(computeConstructOp);
275 eraseOps.insert(dataOp);
279 Region &elseRegion = ifOp.getElseRegion();
280 auto replaceHostUsesAndScheduleErase = [&](
auto &ops) {
285 condEraseOps.insert(op);
288 replaceHostUsesAndScheduleErase(dataEntryOps);
289 replaceHostUsesAndScheduleErase(firstprivateOps);
290 replaceHostUsesAndScheduleErase(privateOps);
291 replaceHostUsesAndScheduleErase(reductionOps);
294void ACCIfClauseLowering::runOnOperation() {
295 func::FuncOp funcOp = getOperation();
296 accSupport = &getAnalysis<OpenACCSupport>();
301 if (
auto parallelOp = dyn_cast<acc::ParallelOp>(op))
302 lowerIfClauseForComputeConstruct(parallelOp, eraseOps, condEraseOps);
303 else if (
auto kernelsOp = dyn_cast<acc::KernelsOp>(op))
304 lowerIfClauseForComputeConstruct(kernelsOp, eraseOps, condEraseOps);
305 else if (
auto serialOp = dyn_cast<acc::SerialOp>(op))
306 lowerIfClauseForComputeConstruct(serialOp, eraseOps, condEraseOps);
309 for (
Operation *op : llvm::reverse(eraseOps))
318 for (
size_t i = pendingEraseOps.size(); i > 0; --i) {
322 pendingEraseOps.erase(pendingEraseOps.begin() + i - 1);
Block represents an ordered list of Operations.
Operation * getTerminator()
Get the terminator operation of this block.
This class allows control over how the GreedyPatternRewriteDriver works.
GreedyRewriteConfig & setUseTopDownTraversal(bool use=true)
GreedyRewriteConfig & setStrictness(GreedyRewriteStrictness mode)
This is a utility class for mapping one set of IR entities to another.
auto lookupOrDefault(T from) const
Lookup a mapped value within the map.
void map(Value from, Value to)
Inserts a new mapping for 'from' to 'to'.
This class coordinates rewriting a piece of IR outside of a pattern rewrite, providing a way to keep ...
Operation * clone(Operation &op, IRMapping &mapper)
Creates a deep copy of the specified operation, remapping any operands that use values outside of the...
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
This class represents an operand of an operation.
This class implements the operand iterators for the Operation class.
Operation is the basic unit of execution within MLIR.
bool use_empty()
Returns true if this operation has no uses.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Location getLoc()
The source location the operation was defined or derived from.
user_range getUsers()
Returns a range of all users.
Region * getParentRegion()
Returns the region to which the instruction belongs.
MLIRContext * getContext()
Return the context this operation is associated with.
void erase()
Remove this operation from its parent block and delete it.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
bool isAncestor(Region *other)
Return true if this region is ancestor of the other region.
bool hasOneBlock()
Return true if this region has exactly one block.
RetT walk(FnT &&callback)
Walk all nested operations, blocks or regions (including this region), depending on the type of callb...
This class provides an abstraction over the various different ranges of value types.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
void replaceUsesWithIf(Value newValue, function_ref< bool(OpOperand &)> shouldReplace)
Replace all uses of 'this' value with 'newValue' if the given callback returns true.
InFlightDiagnostic emitNYI(Location loc, const Twine &message)
Report a case that is not yet supported by the implementation.
Operation * getOwner() const
Return the owner of this operand.
mlir::Value getAccVar(mlir::Operation *accDataClauseOp)
Used to obtain the accVar from a data clause operation.
mlir::Value getVar(mlir::Operation *accDataClauseOp)
Used to obtain the var from a data clause operation.
scf::ExecuteRegionOp wrapMultiBlockRegionWithSCFExecuteRegion(Region ®ion, IRMapping &mapping, Location loc, RewriterBase &rewriter)
Wrap a multi-block region in an scf.execute_region.
void populateACCHostFallbackPatterns(RewritePatternSet &patterns, OpenACCSupport &accSupport, bool enableLoopConversion=true)
Populates all patterns for host fallback path (when if clause evaluates to false).
Include the generated interface declarations.
LogicalResult applyOpPatternsGreedily(ArrayRef< Operation * > ops, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr, bool *allErased=nullptr)
Rewrite the specified ops by repeatedly applying the highest benefit patterns in a greedy worklist dr...
@ ExistingOps
Only pre-existing ops are processed.