MLIR 23.0.0git
OpenACCUtilsCG.cpp
Go to the documentation of this file.
1//===- OpenACCUtilsCG.cpp - OpenACC Code Generation 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 code generation.
10//
11//===----------------------------------------------------------------------===//
12
16#include "mlir/IR/BuiltinOps.h"
17#include "mlir/IR/IRMapping.h"
18
19namespace mlir {
20namespace acc {
21
22std::optional<DataLayout> getDataLayout(Operation *op, bool allowDefault) {
23 if (!op)
24 return std::nullopt;
25
26 // Walk up the parent chain to find the nearest operation with an explicit
27 // data layout spec. Check ModuleOp explicitly since it does not actually
28 // implement DataLayoutOpInterface as a trait (it just has the same methods).
29 Operation *current = op;
30 while (current) {
31 // Check for ModuleOp with explicit data layout spec
32 if (auto mod = llvm::dyn_cast<ModuleOp>(current)) {
33 if (mod.getDataLayoutSpec())
34 return DataLayout(mod);
35 } else if (auto dataLayoutOp =
36 llvm::dyn_cast<DataLayoutOpInterface>(current)) {
37 // Check other DataLayoutOpInterface implementations
38 if (dataLayoutOp.getDataLayoutSpec())
39 return DataLayout(dataLayoutOp);
40 }
41 current = current->getParentOp();
42 }
43
44 // No explicit data layout found; return default if allowed
45 if (allowDefault) {
46 // Check if op itself is a ModuleOp
47 if (auto mod = llvm::dyn_cast<ModuleOp>(op))
48 return DataLayout(mod);
49 // Otherwise check parents
50 if (auto mod = op->getParentOfType<ModuleOp>())
51 return DataLayout(mod);
52 }
53
54 return std::nullopt;
55}
56
57ComputeRegionOp
59 llvm::StringRef origin, Region &regionToClone,
60 RewriterBase &rewriter, IRMapping &mapping,
61 ValueRange output, FlatSymbolRefAttr kernelFuncName,
62 FlatSymbolRefAttr kernelModuleName, Value stream) {
63 SmallVector<Type> resultTypes;
64 for (auto val : output)
65 resultTypes.push_back(val.getType());
66 auto computeRegion =
67 ComputeRegionOp::create(rewriter, loc, resultTypes, launchArgs, inputArgs,
68 stream, origin, kernelFuncName, kernelModuleName);
69
70 assert(!regionToClone.getBlocks().empty() &&
71 "empty region for acc.compute_region");
72 OpBuilder::InsertionGuard guard(rewriter);
73
74 auto parWidthType = ParWidthType::get(rewriter.getContext());
75 Block *entryBlock = rewriter.createBlock(&computeRegion.getRegion());
76 for (size_t i = 0; i < launchArgs.size(); ++i)
77 entryBlock->addArgument(parWidthType, loc);
78 for (Value input : inputArgs)
79 entryBlock->addArgument(input.getType(), loc);
80 for (size_t i = 0; i < inputArgs.size(); ++i)
81 mapping.map(inputArgs[i], entryBlock->getArgument(launchArgs.size() + i));
82 rewriter.setInsertionPointToStart(entryBlock);
83 if (regionToClone.getBlocks().size() == 1) {
84 for (auto &op : regionToClone.front().getOperations()) {
85 if (op.hasTrait<OpTrait::IsTerminator>())
86 break;
87 rewriter.clone(op, mapping);
88 }
89 } else {
91 regionToClone, mapping, loc, rewriter);
92 if (!exeRegion) {
93 rewriter.eraseOp(computeRegion);
94 return nullptr;
95 }
96 }
97
98 SmallVector<Value> yieldOperands;
99 for (auto val : output)
100 yieldOperands.push_back(mapping.lookup(val));
101 rewriter.setInsertionPointToEnd(entryBlock);
102 YieldOp::create(rewriter, loc, yieldOperands);
103
104 return computeRegion;
105}
106
107} // namespace acc
108} // namespace mlir
Block represents an ordered list of Operations.
Definition Block.h:33
BlockArgument getArgument(unsigned i)
Definition Block.h:139
OpListType & getOperations()
Definition Block.h:147
BlockArgument addArgument(Type type, Location loc)
Add one value to the argument list.
Definition Block.cpp:158
MLIRContext * getContext() const
Definition Builders.h:56
The main mechanism for performing data layout queries.
A symbol reference with a reference path containing a single element.
This is a utility class for mapping one set of IR entities to another.
Definition IRMapping.h:26
auto lookup(T from) const
Lookup a mapped value within the map.
Definition IRMapping.h:72
void map(Value from, Value to)
Inserts a new mapping for 'from' to 'to'.
Definition IRMapping.h:30
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
RAII guard to reset the insertion point of the builder when destroyed.
Definition Builders.h:350
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes={}, ArrayRef< Location > locs={})
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
Definition Builders.cpp:434
Operation * clone(Operation &op, IRMapping &mapper)
Creates a deep copy of the specified operation, remapping any operands that use values outside of the...
Definition Builders.cpp:566
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
Definition Builders.h:433
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
Definition Builders.h:438
This class provides the API for ops that are known to be terminators.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
Definition Operation.h:255
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
Definition Operation.h:259
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
Block & front()
Definition Region.h:65
BlockListType & getBlocks()
Definition Region.h:45
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:387
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
std::optional< DataLayout > getDataLayout(Operation *op, bool allowDefault=true)
Get the data layout for an operation.
scf::ExecuteRegionOp wrapMultiBlockRegionWithSCFExecuteRegion(Region &region, IRMapping &mapping, Location loc, RewriterBase &rewriter, bool convertFuncReturn=false)
Wrap a multi-block region in an scf.execute_region.
ComputeRegionOp buildComputeRegion(Location loc, ValueRange launchArgs, ValueRange inputArgs, llvm::StringRef origin, Region &regionToClone, RewriterBase &rewriter, IRMapping &mapping, ValueRange output={}, FlatSymbolRefAttr kernelFuncName={}, FlatSymbolRefAttr kernelModuleName={}, Value stream={})
Build an acc.compute_region operation by cloning a source region.
Include the generated interface declarations.