1//===- FuncOps.cpp - Func Dialect Operations ------------------------------===//
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#include "mlir/Dialect/Func/IR/FuncOps.h"
10
11#include "mlir/Bytecode/Encoding.h"
12#include "mlir/Conversion/ConvertToEmitC/ToEmitCInterface.h"
13#include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
14#include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h"
15#include "mlir/IR/BuiltinTypes.h"
16#include "mlir/IR/IRMapping.h"
17#include "mlir/IR/Matchers.h"
18#include "mlir/IR/OpImplementation.h"
19#include "mlir/IR/PatternMatch.h"
20#include "mlir/IR/TypeUtilities.h"
21#include "mlir/IR/Value.h"
22#include "mlir/Interfaces/FunctionImplementation.h"
23#include "mlir/Transforms/InliningUtils.h"
24#include "llvm/ADT/APFloat.h"
25#include "llvm/ADT/MapVector.h"
26#include "llvm/ADT/STLExtras.h"
27#include "llvm/ADT/SmallVectorExtras.h"
28
29#include "mlir/Dialect/Func/IR/FuncOpsDialect.cpp.inc"
30
31using namespace mlir;
32using namespace mlir::func;
33
34namespace {
35struct FuncDialectBytecodeInterface : BytecodeDialectInterface {
36 using BytecodeDialectInterface::BytecodeDialectInterface;
37
38 void writeVersion(DialectBytecodeWriter &writer) const override {
39 writer.writeVarInt(value: 1);
40 }
41
42 std::unique_ptr<DialectVersion>
43 readVersion(DialectBytecodeReader &reader) const override {
44 uint64_t version;
45 if (failed(Result: reader.readVarInt(result&: version)))
46 return nullptr;
47 if (version != 1) {
48 reader.emitError(msg: "unsupported func dialect bytecode version");
49 return nullptr;
50 }
51 return std::make_unique<DialectVersion>();
52 }
53};
54} // namespace
55
56//===----------------------------------------------------------------------===//
57// FuncDialect
58//===----------------------------------------------------------------------===//
59
60void FuncDialect::initialize() {
61 addInterfaces<FuncDialectBytecodeInterface>();
62 addOperations<
63#define GET_OP_LIST
64#include "mlir/Dialect/Func/IR/FuncOps.cpp.inc"
65 >();
66 declarePromisedInterface<ConvertToEmitCPatternInterface, FuncDialect>();
67 declarePromisedInterface<DialectInlinerInterface, FuncDialect>();
68 declarePromisedInterface<ConvertToLLVMPatternInterface, FuncDialect>();
69 declarePromisedInterfaces<bufferization::BufferizableOpInterface, CallOp,
70 FuncOp, ReturnOp>();
71}
72
73/// Materialize a single constant operation from a given attribute value with
74/// the desired resultant type.
75Operation *FuncDialect::materializeConstant(OpBuilder &builder, Attribute value,
76 Type type, Location loc) {
77 if (ConstantOp::isBuildableWith(value, type))
78 return ConstantOp::create(builder, location: loc, resultType0: type,
79 value: llvm::cast<FlatSymbolRefAttr>(Val&: value));
80 return nullptr;
81}
82
83//===----------------------------------------------------------------------===//
84// CallOp
85//===----------------------------------------------------------------------===//
86
87LogicalResult CallOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
88 // Check that the callee attribute was specified.
89 auto fnAttr = getCalleeAttr();
90 if (!fnAttr)
91 return emitOpError(message: "requires a 'callee' symbol reference attribute");
92 FuncOp fn = symbolTable.lookupNearestSymbolFrom<FuncOp>(from: *this, symbol: fnAttr);
93 if (!fn)
94 return emitOpError() << "'" << fnAttr.getValue()
95 << "' does not reference a valid function";
96
97 // Verify that the operand and result types match the callee.
98 return call_interface_impl::verifyCallOpInterface(call: *this, callable: fn);
99}
100
101FunctionType CallOp::getCalleeType() {
102 return FunctionType::get(context: getContext(), inputs: getOperandTypes(), results: getResultTypes());
103}
104
105//===----------------------------------------------------------------------===//
106// CallIndirectOp
107//===----------------------------------------------------------------------===//
108
109/// Fold indirect calls that have a constant function as the callee operand.
110LogicalResult CallIndirectOp::canonicalize(CallIndirectOp indirectCall,
111 PatternRewriter &rewriter) {
112 // Check that the callee is a constant callee.
113 FlatSymbolRefAttr calledFn;
114 if (!matchPattern(value: indirectCall.getCallee(), pattern: m_Constant(bind_value: &calledFn)))
115 return failure();
116
117 // Replace with a direct call, preserving the call-site attributes.
118 auto directCall = CallOp::create(
119 builder&: rewriter, location: indirectCall.getLoc(), resultType0: indirectCall.getResultTypes(), callee: calledFn,
120 operands: indirectCall.getArgOperands(), arg_attrs: indirectCall.getArgAttrsAttr(),
121 res_attrs: indirectCall.getResAttrsAttr());
122 directCall->setDiscardableAttrs(indirectCall->getDiscardableAttrDictionary());
123 rewriter.replaceOp(op: indirectCall, newValues: directCall.getResults());
124 return success();
125}
126
127//===----------------------------------------------------------------------===//
128// ConstantOp
129//===----------------------------------------------------------------------===//
130
131LogicalResult ConstantOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
132 StringRef fnName = getValue();
133 Type type = getType();
134
135 // Try to find the referenced function.
136 auto fn = symbolTable.lookupNearestSymbolFrom<FuncOp>(
137 from: this->getOperation(), symbol: StringAttr::get(context: getContext(), bytes: fnName));
138 if (!fn)
139 return emitOpError() << "reference to undefined function '" << fnName
140 << "'";
141
142 // Check that the referenced function has the correct type.
143 if (fn.getFunctionType() != type)
144 return emitOpError(message: "reference to function with mismatched type");
145
146 return success();
147}
148
149OpFoldResult ConstantOp::fold(FoldAdaptor adaptor) {
150 return getValueAttr();
151}
152
153void ConstantOp::getAsmResultNames(
154 function_ref<void(Value, StringRef)> setNameFn) {
155 setNameFn(getResult(), "f");
156}
157
158bool ConstantOp::isBuildableWith(Attribute value, Type type) {
159 return llvm::isa<FlatSymbolRefAttr>(Val: value) && llvm::isa<FunctionType>(Val: type);
160}
161
162//===----------------------------------------------------------------------===//
163// FuncOp
164//===----------------------------------------------------------------------===//
165
166LogicalResult FuncOp::readProperties(DialectBytecodeReader &reader,
167 OperationState &state) {
168 auto &properties = state.getOrAddProperties<Properties>();
169 // Preserve the pre-DLTI field order for unversioned bytecode.
170 if (failed(Result: reader.readOptionalAttribute(result&: properties.arg_attrs)) ||
171 failed(Result: reader.readAttribute(result&: properties.function_type)) ||
172 failed(Result: reader.readOptionalAttribute(result&: properties.no_inline)) ||
173 failed(Result: reader.readOptionalAttribute(result&: properties.res_attrs)) ||
174 failed(Result: reader.readAttribute(result&: properties.sym_name)) ||
175 failed(Result: reader.readOptionalAttribute(result&: properties.sym_visibility)))
176 return failure();
177
178 if (failed(Result: reader.getDialectVersion<FuncDialect>()))
179 return success();
180 return reader.readOptionalAttribute(attr&: properties.dlti);
181}
182
183void FuncOp::writeProperties(DialectBytecodeWriter &writer) {
184 Properties &properties = getProperties();
185 writer.writeOptionalAttribute(attr: properties.arg_attrs);
186 writer.writeAttribute(attr: properties.function_type);
187 writer.writeOptionalAttribute(attr: properties.no_inline);
188 writer.writeOptionalAttribute(attr: properties.res_attrs);
189 writer.writeAttribute(attr: properties.sym_name);
190 writer.writeOptionalAttribute(attr: properties.sym_visibility);
191 if (writer.getBytecodeVersion() >= bytecode::kDialectVersioning)
192 writer.writeOptionalAttribute(attr: properties.dlti);
193}
194
195FuncOp FuncOp::create(Location location, StringRef name, FunctionType type,
196 ArrayRef<NamedAttribute> attrs) {
197 OpBuilder builder(location->getContext());
198 OperationState state(location, getOperationName());
199 FuncOp::build(odsBuilder&: builder, odsState&: state, name, type, attrs);
200 return cast<FuncOp>(Val: Operation::create(state));
201}
202FuncOp FuncOp::create(Location location, StringRef name, FunctionType type,
203 Operation::dialect_attr_range attrs) {
204 SmallVector<NamedAttribute, 8> attrRef(attrs);
205 return create(location, name, type, attrs: llvm::ArrayRef(attrRef));
206}
207FuncOp FuncOp::create(Location location, StringRef name, FunctionType type,
208 ArrayRef<NamedAttribute> attrs,
209 ArrayRef<DictionaryAttr> argAttrs) {
210 FuncOp func = create(location, name, type, attrs);
211 func.setAllArgAttrs(argAttrs);
212 return func;
213}
214
215void FuncOp::build(OpBuilder &builder, OperationState &state, StringRef name,
216 FunctionType type, ArrayRef<NamedAttribute> attrs,
217 ArrayRef<DictionaryAttr> argAttrs) {
218 state.getOrAddProperties<Properties>().sym_name = builder.getStringAttr(bytes: name);
219 state.addAttribute(name: getFunctionTypeAttrName(name: state.name), attr: TypeAttr::get(type));
220 state.attributes.append(inStart: attrs.begin(), inEnd: attrs.end());
221 state.addRegion();
222
223 if (argAttrs.empty())
224 return;
225 assert(type.getNumInputs() == argAttrs.size());
226 call_interface_impl::addArgAndResultAttrs(
227 builder, result&: state, argAttrs, /*resultAttrs=*/{},
228 argAttrsName: getArgAttrsAttrName(name: state.name), resAttrsName: getResAttrsAttrName(name: state.name));
229}
230
231ParseResult FuncOp::parse(OpAsmParser &parser, OperationState &result) {
232 auto buildFuncType =
233 [](Builder &builder, ArrayRef<Type> argTypes, ArrayRef<Type> results,
234 function_interface_impl::VariadicFlag,
235 std::string &) { return builder.getFunctionType(inputs: argTypes, results); };
236
237 return function_interface_impl::parseFunctionOp(
238 parser, result, /*allowVariadic=*/false,
239 typeAttrName: getFunctionTypeAttrName(name: result.name), funcTypeBuilder: buildFuncType,
240 argAttrsName: getArgAttrsAttrName(name: result.name), resAttrsName: getResAttrsAttrName(name: result.name));
241}
242
243void FuncOp::print(OpAsmPrinter &p) {
244 function_interface_impl::printFunctionOp(
245 p, op: *this, /*isVariadic=*/false, typeAttrName: getFunctionTypeAttrName(),
246 argAttrsName: getArgAttrsAttrName(), resAttrsName: getResAttrsAttrName());
247}
248
249/// Clone the internal blocks from this function into dest and all attributes
250/// from this function to dest.
251void FuncOp::cloneInto(FuncOp dest, IRMapping &mapper) {
252 // Add the attributes of this function to dest.
253 llvm::MapVector<StringAttr, Attribute> newAttrMap;
254 for (const auto &attr : dest->getDiscardableAttrDictionary().getValue())
255 newAttrMap.insert(KV: {attr.getName(), attr.getValue()});
256 for (const auto &attr : (*this)->getDiscardableAttrDictionary().getValue())
257 newAttrMap.insert(KV: {attr.getName(), attr.getValue()});
258
259 auto newAttrs = llvm::map_to_vector(
260 C&: newAttrMap, F: [](std::pair<StringAttr, Attribute> attrPair) {
261 return NamedAttribute(attrPair.first, attrPair.second);
262 });
263 dest->setDiscardableAttrs(DictionaryAttr::get(context: getContext(), value: newAttrs));
264
265 // Clone the body.
266 getBody().cloneInto(dest: &dest.getBody(), mapper);
267}
268
269/// Create a deep copy of this function and all of its blocks, remapping
270/// any operands that use values outside of the function using the map that is
271/// provided (leaving them alone if no entry is present). Replaces references
272/// to cloned sub-values with the corresponding value that is copied, and adds
273/// those mappings to the mapper.
274FuncOp FuncOp::clone(IRMapping &mapper) {
275 // Create the new function.
276 FuncOp newFunc = cast<FuncOp>(Val: getOperation()->cloneWithoutRegions());
277
278 // If the function has a body, then the user might be deleting arguments to
279 // the function by specifying them in the mapper. If so, we don't add the
280 // argument to the input type vector.
281 if (!isExternal()) {
282 FunctionType oldType = getFunctionType();
283
284 unsigned oldNumArgs = oldType.getNumInputs();
285 SmallVector<Type, 4> newInputs;
286 newInputs.reserve(N: oldNumArgs);
287 for (unsigned i = 0; i != oldNumArgs; ++i)
288 if (!mapper.contains(from: getArgument(idx: i)))
289 newInputs.push_back(Elt: oldType.getInput(i));
290
291 /// If any of the arguments were dropped, update the type and drop any
292 /// necessary argument attributes.
293 if (newInputs.size() != oldNumArgs) {
294 newFunc.setType(FunctionType::get(context: oldType.getContext(), inputs: newInputs,
295 results: oldType.getResults()));
296
297 if (ArrayAttr argAttrs = getAllArgAttrs()) {
298 SmallVector<Attribute> newArgAttrs;
299 newArgAttrs.reserve(N: newInputs.size());
300 for (unsigned i = 0; i != oldNumArgs; ++i)
301 if (!mapper.contains(from: getArgument(idx: i)))
302 newArgAttrs.push_back(Elt: argAttrs[i]);
303 newFunc.setAllArgAttrs(newArgAttrs);
304 }
305 }
306 }
307
308 /// Clone the current function into the new one and return it.
309 cloneInto(dest: newFunc, mapper);
310 return newFunc;
311}
312FuncOp FuncOp::clone() {
313 IRMapping mapper;
314 return clone(mapper);
315}
316
317//===----------------------------------------------------------------------===//
318// ReturnOp
319//===----------------------------------------------------------------------===//
320
321LogicalResult FuncOp::verifyRegions() {
322 // External declarations have no body to check.
323 if (isDeclaration())
324 return success();
325 // Hoist the result types once; they are the same for every return site.
326 auto resultTypes = getFunctionType().getResults();
327 for (Block &block : getBody()) {
328 if (block.empty())
329 continue;
330 // Check func.return or other return-like terminators ops (e.g.
331 // llvm.return, test.return).
332 auto returnOp = dyn_cast<RegionBranchTerminatorOpInterface>(Val: &block.back());
333 if (!returnOp)
334 continue;
335 auto operands =
336 returnOp.getMutableSuccessorOperands(point: RegionSuccessor(getOperation()));
337 if (operands.size() != resultTypes.size())
338 return returnOp->emitOpError(message: "has ")
339 << operands.size() << " operands, but enclosing function (@"
340 << getName() << ") returns " << resultTypes.size();
341
342 for (auto [i, opType] : llvm::enumerate(First: llvm::zip(t&: operands, u&: resultTypes))) {
343 auto [operand, resTy] = opType;
344 if (operand.get().getType() != resTy)
345 return returnOp->emitError() << "type of return operand " << i << " ("
346 << operand.get().getType()
347 << ") doesn't match function result type ("
348 << resTy << ") in function @" << getName();
349 }
350 }
351
352 return success();
353}
354
355//===----------------------------------------------------------------------===//
356// TableGen'd op method definitions
357//===----------------------------------------------------------------------===//
358
359#define GET_OP_CLASSES
360#include "mlir/Dialect/Func/IR/FuncOps.cpp.inc"
361