Debian Codegraph @

flang/lib/Optimizer/Dialect/CUF/CUFOps.cpp

raw
1 //===-- CUFOps.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 // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "flang/Optimizer/Dialect/CUF/CUFOps.h"
14 #include "flang/Optimizer/Dialect/CUF/Attributes/CUFAttr.h"
15 #include "flang/Optimizer/Dialect/CUF/CUFDialect.h"
16 #include "flang/Optimizer/Dialect/FIRAttr.h"
17 #include "flang/Optimizer/Dialect/FIRType.h"
18 #include "mlir/IR/Attributes.h"
19 #include "mlir/IR/BuiltinAttributes.h"
20 #include "mlir/IR/BuiltinOps.h"
21 #include "mlir/IR/Diagnostics.h"
22 #include "mlir/IR/Matchers.h"
23 #include "mlir/IR/OpDefinition.h"
24 #include "mlir/IR/PatternMatch.h"
25 #include "llvm/ADT/SmallVector.h"
26
27 //===----------------------------------------------------------------------===//
28 // AllocOp
29 //===----------------------------------------------------------------------===//
30
31 static mlir::Type wrapAllocaResultType(mlir::Type intype) {
32 if (mlir::isa<fir::ReferenceType>(intype))
33 return {};
34 return fir::ReferenceType::get(intype);
35 }
36
37 void cuf::AllocOp::build(mlir::OpBuilder &builder, mlir::OperationState &result,
38 mlir::Type inType, llvm::StringRef uniqName,
39 llvm::StringRef bindcName,
40 cuf::DataAttributeAttr cudaAttr,
41 mlir::ValueRange typeparams, mlir::ValueRange shape,
42 llvm::ArrayRef<mlir::NamedAttribute> attributes) {
43 mlir::StringAttr nameAttr =
44 uniqName.empty() ? mlir::StringAttr{} : builder.getStringAttr(uniqName);
45 mlir::StringAttr bindcAttr =
46 bindcName.empty() ? mlir::StringAttr{} : builder.getStringAttr(bindcName);
47 build(builder, result, wrapAllocaResultType(inType),
48 mlir::TypeAttr::get(inType), nameAttr, bindcAttr, typeparams, shape,
49 cudaAttr);
50 result.addAttributes(attributes);
51 }
52
53 template <typename Op>
54 static llvm::LogicalResult checkCudaAttr(Op op) {
55 if (op.getDataAttr() == cuf::DataAttribute::Device ||
56 op.getDataAttr() == cuf::DataAttribute::Managed ||
57 op.getDataAttr() == cuf::DataAttribute::Unified ||
58 op.getDataAttr() == cuf::DataAttribute::Pinned)
59 return mlir::success();
60 return op.emitOpError()
61 << "expect device, managed, pinned or unified cuda attribute";
62 }
63
64 llvm::LogicalResult cuf::AllocOp::verify() { return checkCudaAttr(*this); }
65
66 //===----------------------------------------------------------------------===//
67 // FreeOp
68 //===----------------------------------------------------------------------===//
69
70 llvm::LogicalResult cuf::FreeOp::verify() { return checkCudaAttr(*this); }
71
72 //===----------------------------------------------------------------------===//
73 // AllocateOp
74 //===----------------------------------------------------------------------===//
75
76 llvm::LogicalResult cuf::AllocateOp::verify() {
77 if (getPinned() && getStream())
78 return emitOpError("pinned and stream cannot appears at the same time");
79 if (!mlir::isa<fir::BaseBoxType>(fir::unwrapRefType(getBox().getType())))
80 return emitOpError(
81 "expect box to be a reference to a class or box type value");
82 if (getSource() &&
83 !mlir::isa<fir::BaseBoxType>(fir::unwrapRefType(getSource().getType())))
84 return emitOpError(
85 "expect source to be a reference to/or a class or box type value");
86 if (getErrmsg() &&
87 !mlir::isa<fir::BoxType>(fir::unwrapRefType(getErrmsg().getType())))
88 return emitOpError(
89 "expect errmsg to be a reference to/or a box type value");
90 if (getErrmsg() && !getHasStat())
91 return emitOpError("expect stat attribute when errmsg is provided");
92 return mlir::success();
93 }
94
95 //===----------------------------------------------------------------------===//
96 // DataTransferOp
97 //===----------------------------------------------------------------------===//
98
99 llvm::LogicalResult cuf::DataTransferOp::verify() {
100 mlir::Type srcTy = getSrc().getType();
101 mlir::Type dstTy = getDst().getType();
102 if ((fir::isa_ref_type(srcTy) && fir::isa_ref_type(dstTy)) ||
103 (fir::isa_box_type(srcTy) && fir::isa_box_type(dstTy)) ||
104 (fir::isa_ref_type(srcTy) && fir::isa_box_type(dstTy)) ||
105 (fir::isa_box_type(srcTy) && fir::isa_ref_type(dstTy)))
106 return mlir::success();
107 if (fir::isa_trivial(srcTy) &&
108 matchPattern(getSrc().getDefiningOp(), mlir::m_Constant()))
109 return mlir::success();
110 return emitOpError()
111 << "expect src and dst to be references or descriptors or src to "
112 "be a constant";
113 }
114
115 //===----------------------------------------------------------------------===//
116 // DeallocateOp
117 //===----------------------------------------------------------------------===//
118
119 llvm::LogicalResult cuf::DeallocateOp::verify() {
120 if (!mlir::isa<fir::BaseBoxType>(fir::unwrapRefType(getBox().getType())))
121 return emitOpError(
122 "expect box to be a reference to class or box type value");
123 if (getErrmsg() &&
124 !mlir::isa<fir::BoxType>(fir::unwrapRefType(getErrmsg().getType())))
125 return emitOpError(
126 "expect errmsg to be a reference to/or a box type value");
127 if (getErrmsg() && !getHasStat())
128 return emitOpError("expect stat attribute when errmsg is provided");
129 return mlir::success();
130 }
131
132 //===----------------------------------------------------------------------===//
133 // KernelOp
134 //===----------------------------------------------------------------------===//
135
136 llvm::SmallVector<mlir::Region *> cuf::KernelOp::getLoopRegions() {
137 return {&getRegion()};
138 }
139
140 mlir::ParseResult parseCUFKernelValues(
141 mlir::OpAsmParser &parser,
142 llvm::SmallVectorImpl<mlir::OpAsmParser::UnresolvedOperand> &values,
143 llvm::SmallVectorImpl<mlir::Type> &types) {
144 if (mlir::succeeded(parser.parseOptionalStar()))
145 return mlir::success();
146
147 if (mlir::succeeded(parser.parseOptionalLParen())) {
148 if (mlir::failed(parser.parseCommaSeparatedList(
149 mlir::AsmParser::Delimiter::None, [&]() {
150 if (parser.parseOperand(values.emplace_back()))
151 return mlir::failure();
152 return mlir::success();
153 })))
154 return mlir::failure();
155 auto builder = parser.getBuilder();
156 for (size_t i = 0; i < values.size(); i++) {
157 types.emplace_back(builder.getI32Type());
158 }
159 if (parser.parseRParen())
160 return mlir::failure();
161 } else {
162 if (parser.parseOperand(values.emplace_back()))
163 return mlir::failure();
164 auto builder = parser.getBuilder();
165 types.emplace_back(builder.getI32Type());
166 return mlir::success();
167 }
168 return mlir::success();
169 }
170
171 void printCUFKernelValues(mlir::OpAsmPrinter &p, mlir::Operation *op,
172 mlir::ValueRange values, mlir::TypeRange types) {
173 if (values.empty())
174 p << "*";
175
176 if (values.size() > 1)
177 p << "(";
178 llvm::interleaveComma(values, p, [&p](mlir::Value v) { p << v; });
179 if (values.size() > 1)
180 p << ")";
181 }
182
183 mlir::ParseResult parseCUFKernelLoopControl(
184 mlir::OpAsmParser &parser, mlir::Region &region,
185 llvm::SmallVectorImpl<mlir::OpAsmParser::UnresolvedOperand> &lowerbound,
186 llvm::SmallVectorImpl<mlir::Type> &lowerboundType,
187 llvm::SmallVectorImpl<mlir::OpAsmParser::UnresolvedOperand> &upperbound,
188 llvm::SmallVectorImpl<mlir::Type> &upperboundType,
189 llvm::SmallVectorImpl<mlir::OpAsmParser::UnresolvedOperand> &step,
190 llvm::SmallVectorImpl<mlir::Type> &stepType) {
191
192 llvm::SmallVector<mlir::OpAsmParser::Argument> inductionVars;
193 if (parser.parseLParen() ||
194 parser.parseArgumentList(inductionVars,
195 mlir::OpAsmParser::Delimiter::None,
196 /*allowType=*/true) ||
197 parser.parseRParen() || parser.parseEqual() || parser.parseLParen() ||
198 parser.parseOperandList(lowerbound, inductionVars.size(),
199 mlir::OpAsmParser::Delimiter::None) ||
200 parser.parseColonTypeList(lowerboundType) || parser.parseRParen() ||
201 parser.parseKeyword("to") || parser.parseLParen() ||
202 parser.parseOperandList(upperbound, inductionVars.size(),
203 mlir::OpAsmParser::Delimiter::None) ||
204 parser.parseColonTypeList(upperboundType) || parser.parseRParen() ||
205 parser.parseKeyword("step") || parser.parseLParen() ||
206 parser.parseOperandList(step, inductionVars.size(),
207 mlir::OpAsmParser::Delimiter::None) ||
208 parser.parseColonTypeList(stepType) || parser.parseRParen())
209 return mlir::failure();
210 return parser.parseRegion(region, inductionVars);
211 }
212
213 void printCUFKernelLoopControl(
214 mlir::OpAsmPrinter &p, mlir::Operation *op, mlir::Region &region,
215 mlir::ValueRange lowerbound, mlir::TypeRange lowerboundType,
216 mlir::ValueRange upperbound, mlir::TypeRange upperboundType,
217 mlir::ValueRange steps, mlir::TypeRange stepType) {
218 mlir::ValueRange regionArgs = region.front().getArguments();
219 if (!regionArgs.empty()) {
220 p << "(";
221 llvm::interleaveComma(
222 regionArgs, p, [&p](mlir::Value v) { p << v << " : " << v.getType(); });
223 p << ") = (" << lowerbound << " : " << lowerboundType << ") to ("
224 << upperbound << " : " << upperboundType << ") "
225 << " step (" << steps << " : " << stepType << ") ";
226 }
227 p.printRegion(region, /*printEntryBlockArgs=*/false);
228 }
229
230 llvm::LogicalResult cuf::KernelOp::verify() {
231 if (getLowerbound().size() != getUpperbound().size() ||
232 getLowerbound().size() != getStep().size())
233 return emitOpError(
234 "expect same number of values in lowerbound, upperbound and step");
235 auto reduceAttrs = getReduceAttrs();
236 std::size_t reduceAttrsSize = reduceAttrs ? reduceAttrs->size() : 0;
237 if (getReduceOperands().size() != reduceAttrsSize)
238 return emitOpError("expect same number of values in reduce operands and "
239 "reduce attributes");
240 if (reduceAttrs) {
241 for (const auto &attr : reduceAttrs.value()) {
242 if (!mlir::isa<fir::ReduceAttr>(attr))
243 return emitOpError("expect reduce attributes to be ReduceAttr");
244 }
245 }
246 return mlir::success();
247 }
248
249 // Tablegen operators
250
251 #define GET_OP_CLASSES
252 #include "flang/Optimizer/Dialect/CUF/CUFOps.cpp.inc"