2019-11-29 03:50:47 +08:00
|
|
|
//===- FunctionImplementation.cpp - Utilities for function-like ops -------===//
|
2019-07-26 05:26:41 +08:00
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-07-26 05:26:41 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-07-26 05:26:41 +08:00
|
|
|
|
2019-11-29 03:50:47 +08:00
|
|
|
#include "mlir/IR/FunctionImplementation.h"
|
2019-07-26 05:26:41 +08:00
|
|
|
#include "mlir/IR/Builders.h"
|
2019-11-29 03:50:47 +08:00
|
|
|
#include "mlir/IR/FunctionSupport.h"
|
2019-10-22 00:58:22 +08:00
|
|
|
#include "mlir/IR/SymbolTable.h"
|
2019-07-26 05:26:41 +08:00
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
2021-05-08 10:30:25 +08:00
|
|
|
ParseResult mlir::function_like_impl::parseFunctionArgumentList(
|
2020-10-21 17:46:32 +08:00
|
|
|
OpAsmParser &parser, bool allowAttributes, bool allowVariadic,
|
|
|
|
SmallVectorImpl<OpAsmParser::OperandType> &argNames,
|
|
|
|
SmallVectorImpl<Type> &argTypes, SmallVectorImpl<NamedAttrList> &argAttrs,
|
|
|
|
bool &isVariadic) {
|
2019-09-21 02:36:49 +08:00
|
|
|
if (parser.parseLParen())
|
2019-07-26 05:26:41 +08:00
|
|
|
return failure();
|
|
|
|
|
|
|
|
// The argument list either has to consistently have ssa-id's followed by
|
|
|
|
// types, or just be a type list. It isn't ok to sometimes have SSA ID's and
|
|
|
|
// sometimes not.
|
|
|
|
auto parseArgument = [&]() -> ParseResult {
|
2019-09-21 02:36:49 +08:00
|
|
|
llvm::SMLoc loc = parser.getCurrentLocation();
|
2019-07-26 05:26:41 +08:00
|
|
|
|
|
|
|
// Parse argument name if present.
|
|
|
|
OpAsmParser::OperandType argument;
|
|
|
|
Type argumentType;
|
2019-09-21 02:36:49 +08:00
|
|
|
if (succeeded(parser.parseOptionalRegionArgument(argument)) &&
|
2019-07-26 05:26:41 +08:00
|
|
|
!argument.name.empty()) {
|
|
|
|
// Reject this if the preceding argument was missing a name.
|
|
|
|
if (argNames.empty() && !argTypes.empty())
|
2019-09-21 02:36:49 +08:00
|
|
|
return parser.emitError(loc, "expected type instead of SSA identifier");
|
2019-07-26 05:26:41 +08:00
|
|
|
argNames.push_back(argument);
|
|
|
|
|
2019-09-21 02:36:49 +08:00
|
|
|
if (parser.parseColonType(argumentType))
|
2019-07-26 05:26:41 +08:00
|
|
|
return failure();
|
2019-09-21 02:36:49 +08:00
|
|
|
} else if (allowVariadic && succeeded(parser.parseOptionalEllipsis())) {
|
2019-08-09 00:41:48 +08:00
|
|
|
isVariadic = true;
|
|
|
|
return success();
|
2019-07-26 05:26:41 +08:00
|
|
|
} else if (!argNames.empty()) {
|
|
|
|
// Reject this if the preceding argument had a name.
|
2019-09-21 02:36:49 +08:00
|
|
|
return parser.emitError(loc, "expected SSA identifier");
|
|
|
|
} else if (parser.parseType(argumentType)) {
|
2019-07-26 05:26:41 +08:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the argument type.
|
|
|
|
argTypes.push_back(argumentType);
|
|
|
|
|
|
|
|
// Parse any argument attributes.
|
2020-05-07 04:48:36 +08:00
|
|
|
NamedAttrList attrs;
|
2019-11-06 05:32:07 +08:00
|
|
|
if (parser.parseOptionalAttrDict(attrs))
|
2019-07-26 05:26:41 +08:00
|
|
|
return failure();
|
2020-10-21 17:46:32 +08:00
|
|
|
if (!allowAttributes && !attrs.empty())
|
|
|
|
return parser.emitError(loc, "expected arguments without attributes");
|
2019-07-26 05:26:41 +08:00
|
|
|
argAttrs.push_back(attrs);
|
2021-05-24 05:08:31 +08:00
|
|
|
|
|
|
|
// Parse a location if specified. TODO: Don't drop it on the floor.
|
|
|
|
Optional<Location> explicitLoc;
|
|
|
|
if (!argument.name.empty() &&
|
|
|
|
parser.parseOptionalLocationSpecifier(explicitLoc))
|
|
|
|
return failure();
|
|
|
|
|
2019-07-26 05:26:41 +08:00
|
|
|
return success();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Parse the function arguments.
|
2019-12-03 16:26:13 +08:00
|
|
|
isVariadic = false;
|
|
|
|
if (failed(parser.parseOptionalRParen())) {
|
2019-07-26 05:26:41 +08:00
|
|
|
do {
|
2019-08-09 00:41:48 +08:00
|
|
|
unsigned numTypedArguments = argTypes.size();
|
2019-07-26 05:26:41 +08:00
|
|
|
if (parseArgument())
|
|
|
|
return failure();
|
2019-08-09 00:41:48 +08:00
|
|
|
|
2019-09-21 02:36:49 +08:00
|
|
|
llvm::SMLoc loc = parser.getCurrentLocation();
|
2019-08-09 00:41:48 +08:00
|
|
|
if (argTypes.size() == numTypedArguments &&
|
2019-09-21 02:36:49 +08:00
|
|
|
succeeded(parser.parseOptionalComma()))
|
|
|
|
return parser.emitError(
|
2019-08-09 00:41:48 +08:00
|
|
|
loc, "variadic arguments must be in the end of the argument list");
|
2019-09-21 02:36:49 +08:00
|
|
|
} while (succeeded(parser.parseOptionalComma()));
|
|
|
|
parser.parseRParen();
|
2019-07-26 05:26:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-10-19 07:02:56 +08:00
|
|
|
/// Parse a function result list.
|
|
|
|
///
|
|
|
|
/// function-result-list ::= function-result-list-parens
|
|
|
|
/// | non-function-type
|
|
|
|
/// function-result-list-parens ::= `(` `)`
|
|
|
|
/// | `(` function-result-list-no-parens `)`
|
|
|
|
/// function-result-list-no-parens ::= function-result (`,` function-result)*
|
|
|
|
/// function-result ::= type attribute-dict?
|
|
|
|
///
|
2020-05-07 04:48:36 +08:00
|
|
|
static ParseResult
|
|
|
|
parseFunctionResultList(OpAsmParser &parser, SmallVectorImpl<Type> &resultTypes,
|
|
|
|
SmallVectorImpl<NamedAttrList> &resultAttrs) {
|
2019-10-19 07:02:56 +08:00
|
|
|
if (failed(parser.parseOptionalLParen())) {
|
|
|
|
// We already know that there is no `(`, so parse a type.
|
|
|
|
// Because there is no `(`, it cannot be a function type.
|
|
|
|
Type ty;
|
|
|
|
if (parser.parseType(ty))
|
|
|
|
return failure();
|
|
|
|
resultTypes.push_back(ty);
|
|
|
|
resultAttrs.emplace_back();
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special case for an empty set of parens.
|
|
|
|
if (succeeded(parser.parseOptionalRParen()))
|
|
|
|
return success();
|
|
|
|
|
|
|
|
// Parse individual function results.
|
|
|
|
do {
|
|
|
|
resultTypes.emplace_back();
|
|
|
|
resultAttrs.emplace_back();
|
|
|
|
if (parser.parseType(resultTypes.back()) ||
|
2019-11-06 05:32:07 +08:00
|
|
|
parser.parseOptionalAttrDict(resultAttrs.back())) {
|
2019-10-19 07:02:56 +08:00
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
} while (succeeded(parser.parseOptionalComma()));
|
|
|
|
return parser.parseRParen();
|
|
|
|
}
|
|
|
|
|
2019-11-25 23:59:52 +08:00
|
|
|
/// Parses a function signature using `parser`. The `allowVariadic` argument
|
|
|
|
/// indicates whether functions with variadic arguments are supported. The
|
|
|
|
/// trailing arguments are populated by this function with names, types and
|
|
|
|
/// attributes of the arguments and those of the results.
|
2021-05-08 10:30:25 +08:00
|
|
|
ParseResult mlir::function_like_impl::parseFunctionSignature(
|
2019-09-21 02:36:49 +08:00
|
|
|
OpAsmParser &parser, bool allowVariadic,
|
2019-08-09 00:41:48 +08:00
|
|
|
SmallVectorImpl<OpAsmParser::OperandType> &argNames,
|
2020-05-07 04:48:36 +08:00
|
|
|
SmallVectorImpl<Type> &argTypes, SmallVectorImpl<NamedAttrList> &argAttrs,
|
|
|
|
bool &isVariadic, SmallVectorImpl<Type> &resultTypes,
|
|
|
|
SmallVectorImpl<NamedAttrList> &resultAttrs) {
|
2020-10-21 17:46:32 +08:00
|
|
|
bool allowArgAttrs = true;
|
|
|
|
if (parseFunctionArgumentList(parser, allowArgAttrs, allowVariadic, argNames,
|
|
|
|
argTypes, argAttrs, isVariadic))
|
2019-07-26 05:26:41 +08:00
|
|
|
return failure();
|
2019-10-19 07:02:56 +08:00
|
|
|
if (succeeded(parser.parseOptionalArrow()))
|
|
|
|
return parseFunctionResultList(parser, resultTypes, resultAttrs);
|
|
|
|
return success();
|
2019-07-26 05:26:41 +08:00
|
|
|
}
|
|
|
|
|
2021-05-08 10:30:25 +08:00
|
|
|
/// Implementation of `addArgAndResultAttrs` that is attribute list type
|
|
|
|
/// agnostic.
|
|
|
|
template <typename AttrListT, typename AttrArrayBuildFnT>
|
|
|
|
static void addArgAndResultAttrsImpl(Builder &builder, OperationState &result,
|
|
|
|
ArrayRef<AttrListT> argAttrs,
|
|
|
|
ArrayRef<AttrListT> resultAttrs,
|
|
|
|
AttrArrayBuildFnT &&buildAttrArrayFn) {
|
|
|
|
auto nonEmptyAttrsFn = [](const AttrListT &attrs) { return !attrs.empty(); };
|
2019-11-25 23:59:52 +08:00
|
|
|
|
2021-05-08 10:30:25 +08:00
|
|
|
// Add the attributes to the function arguments.
|
|
|
|
if (!argAttrs.empty() && llvm::any_of(argAttrs, nonEmptyAttrsFn)) {
|
|
|
|
ArrayAttr attrDicts = builder.getArrayAttr(buildAttrArrayFn(argAttrs));
|
|
|
|
result.addAttribute(function_like_impl::getArgDictAttrName(), attrDicts);
|
|
|
|
}
|
2019-11-25 23:59:52 +08:00
|
|
|
// Add the attributes to the function results.
|
2021-05-08 10:30:25 +08:00
|
|
|
if (!resultAttrs.empty() && llvm::any_of(resultAttrs, nonEmptyAttrsFn)) {
|
|
|
|
ArrayAttr attrDicts = builder.getArrayAttr(buildAttrArrayFn(resultAttrs));
|
|
|
|
result.addAttribute(function_like_impl::getResultDictAttrName(), attrDicts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mlir::function_like_impl::addArgAndResultAttrs(
|
|
|
|
Builder &builder, OperationState &result, ArrayRef<DictionaryAttr> argAttrs,
|
|
|
|
ArrayRef<DictionaryAttr> resultAttrs) {
|
|
|
|
auto buildFn = [](ArrayRef<DictionaryAttr> attrs) {
|
|
|
|
return ArrayRef<Attribute>(attrs.data(), attrs.size());
|
|
|
|
};
|
|
|
|
addArgAndResultAttrsImpl(builder, result, argAttrs, resultAttrs, buildFn);
|
|
|
|
}
|
|
|
|
void mlir::function_like_impl::addArgAndResultAttrs(
|
|
|
|
Builder &builder, OperationState &result, ArrayRef<NamedAttrList> argAttrs,
|
|
|
|
ArrayRef<NamedAttrList> resultAttrs) {
|
|
|
|
MLIRContext *context = builder.getContext();
|
|
|
|
auto buildFn = [=](ArrayRef<NamedAttrList> attrs) {
|
|
|
|
return llvm::to_vector<8>(
|
|
|
|
llvm::map_range(attrs, [=](const NamedAttrList &attrList) -> Attribute {
|
|
|
|
return attrList.getDictionary(context);
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
addArgAndResultAttrsImpl(builder, result, argAttrs, resultAttrs, buildFn);
|
2019-11-25 23:59:52 +08:00
|
|
|
}
|
|
|
|
|
2019-07-26 05:26:41 +08:00
|
|
|
/// Parser implementation for function-like operations. Uses `funcTypeBuilder`
|
|
|
|
/// to construct the custom function type given lists of input and output types.
|
2021-05-08 10:30:25 +08:00
|
|
|
ParseResult mlir::function_like_impl::parseFunctionLikeOp(
|
|
|
|
OpAsmParser &parser, OperationState &result, bool allowVariadic,
|
|
|
|
FuncTypeBuilder funcTypeBuilder) {
|
2019-07-26 05:26:41 +08:00
|
|
|
SmallVector<OpAsmParser::OperandType, 4> entryArgs;
|
2020-05-07 04:48:36 +08:00
|
|
|
SmallVector<NamedAttrList, 4> argAttrs;
|
|
|
|
SmallVector<NamedAttrList, 4> resultAttrs;
|
2019-07-26 05:26:41 +08:00
|
|
|
SmallVector<Type, 4> argTypes;
|
2019-10-19 07:02:56 +08:00
|
|
|
SmallVector<Type, 4> resultTypes;
|
2019-09-21 02:36:49 +08:00
|
|
|
auto &builder = parser.getBuilder();
|
2019-07-26 05:26:41 +08:00
|
|
|
|
2020-11-13 02:46:44 +08:00
|
|
|
// Parse visibility.
|
|
|
|
impl::parseOptionalVisibilityKeyword(parser, result.attributes);
|
2020-11-10 00:23:55 +08:00
|
|
|
|
2019-11-25 23:59:52 +08:00
|
|
|
// Parse the name as a symbol.
|
2019-11-14 01:31:45 +08:00
|
|
|
StringAttr nameAttr;
|
2020-11-10 00:23:55 +08:00
|
|
|
if (parser.parseSymbolName(nameAttr, SymbolTable::getSymbolAttrName(),
|
2019-11-14 01:31:45 +08:00
|
|
|
result.attributes))
|
2019-07-26 05:26:41 +08:00
|
|
|
return failure();
|
|
|
|
|
|
|
|
// Parse the function signature.
|
2021-01-07 08:32:59 +08:00
|
|
|
llvm::SMLoc signatureLocation = parser.getCurrentLocation();
|
2019-08-09 00:41:48 +08:00
|
|
|
bool isVariadic = false;
|
|
|
|
if (parseFunctionSignature(parser, allowVariadic, entryArgs, argTypes,
|
2019-10-19 07:02:56 +08:00
|
|
|
argAttrs, isVariadic, resultTypes, resultAttrs))
|
2019-07-26 05:26:41 +08:00
|
|
|
return failure();
|
|
|
|
|
2019-08-05 16:57:27 +08:00
|
|
|
std::string errorMessage;
|
2021-05-08 10:30:25 +08:00
|
|
|
Type type = funcTypeBuilder(builder, argTypes, resultTypes,
|
|
|
|
VariadicFlag(isVariadic), errorMessage);
|
|
|
|
if (!type) {
|
2019-09-21 02:36:49 +08:00
|
|
|
return parser.emitError(signatureLocation)
|
2019-08-05 16:57:27 +08:00
|
|
|
<< "failed to construct function type"
|
|
|
|
<< (errorMessage.empty() ? "" : ": ") << errorMessage;
|
2021-05-08 10:30:25 +08:00
|
|
|
}
|
|
|
|
result.addAttribute(getTypeAttrName(), TypeAttr::get(type));
|
2019-07-26 05:26:41 +08:00
|
|
|
|
|
|
|
// If function attributes are present, parse them.
|
2021-01-07 08:32:59 +08:00
|
|
|
NamedAttrList parsedAttributes;
|
|
|
|
llvm::SMLoc attributeDictLocation = parser.getCurrentLocation();
|
|
|
|
if (parser.parseOptionalAttrDictWithKeyword(parsedAttributes))
|
2019-11-06 09:58:16 +08:00
|
|
|
return failure();
|
2019-07-26 05:26:41 +08:00
|
|
|
|
2021-01-07 08:32:59 +08:00
|
|
|
// Disallow attributes that are inferred from elsewhere in the attribute
|
|
|
|
// dictionary.
|
|
|
|
for (StringRef disallowed :
|
|
|
|
{SymbolTable::getVisibilityAttrName(), SymbolTable::getSymbolAttrName(),
|
|
|
|
getTypeAttrName()}) {
|
|
|
|
if (parsedAttributes.get(disallowed))
|
|
|
|
return parser.emitError(attributeDictLocation, "'")
|
|
|
|
<< disallowed
|
|
|
|
<< "' is an inferred attribute and should not be specified in the "
|
|
|
|
"explicit attribute dictionary";
|
|
|
|
}
|
|
|
|
result.attributes.append(parsedAttributes);
|
|
|
|
|
2019-07-26 05:26:41 +08:00
|
|
|
// Add the attributes to the function arguments.
|
2019-11-25 23:59:52 +08:00
|
|
|
assert(argAttrs.size() == argTypes.size());
|
|
|
|
assert(resultAttrs.size() == resultTypes.size());
|
|
|
|
addArgAndResultAttrs(builder, result, argAttrs, resultAttrs);
|
2019-10-19 07:02:56 +08:00
|
|
|
|
2020-12-03 08:49:47 +08:00
|
|
|
// Parse the optional function body. The printer will not print the body if
|
|
|
|
// its empty, so disallow parsing of empty body in the parser.
|
2019-09-21 10:47:05 +08:00
|
|
|
auto *body = result.addRegion();
|
2020-12-03 08:49:47 +08:00
|
|
|
llvm::SMLoc loc = parser.getCurrentLocation();
|
|
|
|
OptionalParseResult parseResult = parser.parseOptionalRegion(
|
|
|
|
*body, entryArgs, entryArgs.empty() ? ArrayRef<Type>() : argTypes,
|
|
|
|
/*enableNameShadowing=*/false);
|
|
|
|
if (parseResult.hasValue()) {
|
|
|
|
if (failed(*parseResult))
|
|
|
|
return failure();
|
|
|
|
// Function body was parsed, make sure its not empty.
|
|
|
|
if (body->empty())
|
|
|
|
return parser.emitError(loc, "expected non-empty function body");
|
|
|
|
}
|
|
|
|
return success();
|
2019-07-26 05:26:41 +08:00
|
|
|
}
|
|
|
|
|
2021-05-08 10:30:25 +08:00
|
|
|
/// Print a function result list. The provided `attrs` must either be null, or
|
|
|
|
/// contain a set of DictionaryAttrs of the same arity as `types`.
|
2019-10-19 07:02:56 +08:00
|
|
|
static void printFunctionResultList(OpAsmPrinter &p, ArrayRef<Type> types,
|
2021-05-08 10:30:25 +08:00
|
|
|
ArrayAttr attrs) {
|
2019-10-19 07:02:56 +08:00
|
|
|
assert(!types.empty() && "Should not be called for empty result list.");
|
2021-05-08 10:30:25 +08:00
|
|
|
assert((!attrs || attrs.size() == types.size()) &&
|
|
|
|
"Invalid number of attributes.");
|
|
|
|
|
2019-10-19 07:02:56 +08:00
|
|
|
auto &os = p.getStream();
|
2021-05-08 10:30:25 +08:00
|
|
|
bool needsParens = types.size() > 1 || types[0].isa<FunctionType>() ||
|
|
|
|
(attrs && !attrs[0].cast<DictionaryAttr>().empty());
|
2019-10-19 07:02:56 +08:00
|
|
|
if (needsParens)
|
|
|
|
os << '(';
|
2021-05-08 10:30:25 +08:00
|
|
|
llvm::interleaveComma(llvm::seq<size_t>(0, types.size()), os, [&](size_t i) {
|
|
|
|
p.printType(types[i]);
|
|
|
|
if (attrs)
|
|
|
|
p.printOptionalAttrDict(attrs[i].cast<DictionaryAttr>().getValue());
|
|
|
|
});
|
2019-10-19 07:02:56 +08:00
|
|
|
if (needsParens)
|
|
|
|
os << ')';
|
|
|
|
}
|
|
|
|
|
2019-07-26 05:26:41 +08:00
|
|
|
/// Print the signature of the function-like operation `op`. Assumes `op` has
|
|
|
|
/// the FunctionLike trait and passed the verification.
|
2021-05-08 10:30:25 +08:00
|
|
|
void mlir::function_like_impl::printFunctionSignature(
|
|
|
|
OpAsmPrinter &p, Operation *op, ArrayRef<Type> argTypes, bool isVariadic,
|
|
|
|
ArrayRef<Type> resultTypes) {
|
2019-07-26 05:26:41 +08:00
|
|
|
Region &body = op->getRegion(0);
|
|
|
|
bool isExternal = body.empty();
|
|
|
|
|
2019-09-21 11:43:02 +08:00
|
|
|
p << '(';
|
2021-05-08 10:30:25 +08:00
|
|
|
ArrayAttr argAttrs = op->getAttrOfType<ArrayAttr>(getArgDictAttrName());
|
2019-07-26 05:26:41 +08:00
|
|
|
for (unsigned i = 0, e = argTypes.size(); i < e; ++i) {
|
|
|
|
if (i > 0)
|
2019-09-21 11:43:02 +08:00
|
|
|
p << ", ";
|
2019-07-26 05:26:41 +08:00
|
|
|
|
|
|
|
if (!isExternal) {
|
2021-05-24 05:08:31 +08:00
|
|
|
ArrayRef<NamedAttribute> attrs;
|
|
|
|
if (argAttrs)
|
|
|
|
attrs = argAttrs[i].cast<DictionaryAttr>().getValue();
|
|
|
|
p.printRegionArgument(body.getArgument(i), attrs);
|
|
|
|
} else {
|
|
|
|
p.printType(argTypes[i]);
|
|
|
|
if (argAttrs)
|
|
|
|
p.printOptionalAttrDict(argAttrs[i].cast<DictionaryAttr>().getValue());
|
2019-07-26 05:26:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 00:41:48 +08:00
|
|
|
if (isVariadic) {
|
|
|
|
if (!argTypes.empty())
|
2019-09-21 11:43:02 +08:00
|
|
|
p << ", ";
|
|
|
|
p << "...";
|
2019-08-09 00:41:48 +08:00
|
|
|
}
|
|
|
|
|
2019-09-21 11:43:02 +08:00
|
|
|
p << ')';
|
2019-10-19 07:02:56 +08:00
|
|
|
|
|
|
|
if (!resultTypes.empty()) {
|
|
|
|
p.getStream() << " -> ";
|
2021-05-08 10:30:25 +08:00
|
|
|
auto resultAttrs = op->getAttrOfType<ArrayAttr>(getResultDictAttrName());
|
2019-10-19 07:02:56 +08:00
|
|
|
printFunctionResultList(p, resultTypes, resultAttrs);
|
|
|
|
}
|
2019-07-26 05:26:41 +08:00
|
|
|
}
|
|
|
|
|
2019-11-25 23:59:52 +08:00
|
|
|
/// Prints the list of function prefixed with the "attributes" keyword. The
|
|
|
|
/// attributes with names listed in "elided" as well as those used by the
|
|
|
|
/// function-like operation internally are not printed. Nothing is printed
|
|
|
|
/// if all attributes are elided. Assumes `op` has the `FunctionLike` trait and
|
|
|
|
/// passed the verification.
|
2021-05-08 10:30:25 +08:00
|
|
|
void mlir::function_like_impl::printFunctionAttributes(
|
|
|
|
OpAsmPrinter &p, Operation *op, unsigned numInputs, unsigned numResults,
|
|
|
|
ArrayRef<StringRef> elided) {
|
2019-07-26 05:26:41 +08:00
|
|
|
// Print out function attributes, if present.
|
|
|
|
SmallVector<StringRef, 2> ignoredAttrs = {
|
2021-05-08 10:30:25 +08:00
|
|
|
::mlir::SymbolTable::getSymbolAttrName(), getTypeAttrName(),
|
|
|
|
getArgDictAttrName(), getResultDictAttrName()};
|
2019-11-25 23:59:52 +08:00
|
|
|
ignoredAttrs.append(elided.begin(), elided.end());
|
2019-07-26 05:26:41 +08:00
|
|
|
|
2019-11-21 20:24:52 +08:00
|
|
|
p.printOptionalAttrDictWithKeyword(op->getAttrs(), ignoredAttrs);
|
2019-11-25 23:59:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Printer implementation for function-like operations. Accepts lists of
|
|
|
|
/// argument and result types to use while printing.
|
2021-05-08 10:30:25 +08:00
|
|
|
void mlir::function_like_impl::printFunctionLikeOp(OpAsmPrinter &p,
|
|
|
|
Operation *op,
|
|
|
|
ArrayRef<Type> argTypes,
|
|
|
|
bool isVariadic,
|
|
|
|
ArrayRef<Type> resultTypes) {
|
2019-11-25 23:59:52 +08:00
|
|
|
// Print the operation and the function name.
|
|
|
|
auto funcName =
|
2020-11-10 00:23:55 +08:00
|
|
|
op->getAttrOfType<StringAttr>(SymbolTable::getSymbolAttrName())
|
2019-11-25 23:59:52 +08:00
|
|
|
.getValue();
|
2021-08-28 11:03:15 +08:00
|
|
|
p << ' ';
|
2020-11-13 02:46:44 +08:00
|
|
|
|
|
|
|
StringRef visibilityAttrName = SymbolTable::getVisibilityAttrName();
|
|
|
|
if (auto visibility = op->getAttrOfType<StringAttr>(visibilityAttrName))
|
|
|
|
p << visibility.getValue() << ' ';
|
2019-11-25 23:59:52 +08:00
|
|
|
p.printSymbolName(funcName);
|
|
|
|
|
|
|
|
printFunctionSignature(p, op, argTypes, isVariadic, resultTypes);
|
2020-11-10 00:23:55 +08:00
|
|
|
printFunctionAttributes(p, op, argTypes.size(), resultTypes.size(),
|
2020-11-13 02:46:44 +08:00
|
|
|
{visibilityAttrName});
|
2019-07-26 05:26:41 +08:00
|
|
|
// Print the body if this is not an external function.
|
|
|
|
Region &body = op->getRegion(0);
|
|
|
|
if (!body.empty())
|
2019-09-21 11:43:02 +08:00
|
|
|
p.printRegion(body, /*printEntryBlockArgs=*/false,
|
|
|
|
/*printBlockTerminators=*/true);
|
2019-07-26 05:26:41 +08:00
|
|
|
}
|