12f16bf7aSAlex Zinenko //===- FunctionImplementation.cpp - Utilities for function-like ops -------===//
22f16bf7aSAlex Zinenko //
330857107SMehdi Amini // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
456222a06SMehdi Amini // See https://llvm.org/LICENSE.txt for license information.
556222a06SMehdi Amini // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62f16bf7aSAlex Zinenko //
756222a06SMehdi Amini //===----------------------------------------------------------------------===//
82f16bf7aSAlex Zinenko 
92f16bf7aSAlex Zinenko #include "mlir/IR/FunctionImplementation.h"
102f16bf7aSAlex Zinenko #include "mlir/IR/Builders.h"
117ceffae1SRiver Riddle #include "mlir/IR/FunctionInterfaces.h"
122f16bf7aSAlex Zinenko #include "mlir/IR/SymbolTable.h"
132f16bf7aSAlex Zinenko 
142f16bf7aSAlex Zinenko using namespace mlir;
152f16bf7aSAlex Zinenko 
167ceffae1SRiver Riddle ParseResult mlir::function_interface_impl::parseFunctionArgumentList(
171c1803dbSChristian Sigg     OpAsmParser &parser, bool allowAttributes, bool allowVariadic,
182f16bf7aSAlex Zinenko     SmallVectorImpl<OpAsmParser::OperandType> &argNames,
191c1803dbSChristian Sigg     SmallVectorImpl<Type> &argTypes, SmallVectorImpl<NamedAttrList> &argAttrs,
20*e084679fSRiver Riddle     SmallVectorImpl<Location> &argLocations, bool &isVariadic) {
212f16bf7aSAlex Zinenko   if (parser.parseLParen())
222f16bf7aSAlex Zinenko     return failure();
232f16bf7aSAlex Zinenko 
242f16bf7aSAlex Zinenko   // The argument list either has to consistently have ssa-id's followed by
252f16bf7aSAlex Zinenko   // types, or just be a type list.  It isn't ok to sometimes have SSA ID's and
262f16bf7aSAlex Zinenko   // sometimes not.
272f16bf7aSAlex Zinenko   auto parseArgument = [&]() -> ParseResult {
282f16bf7aSAlex Zinenko     llvm::SMLoc loc = parser.getCurrentLocation();
292f16bf7aSAlex Zinenko 
302f16bf7aSAlex Zinenko     // Parse argument name if present.
312f16bf7aSAlex Zinenko     OpAsmParser::OperandType argument;
322f16bf7aSAlex Zinenko     Type argumentType;
332f16bf7aSAlex Zinenko     if (succeeded(parser.parseOptionalRegionArgument(argument)) &&
342f16bf7aSAlex Zinenko         !argument.name.empty()) {
352f16bf7aSAlex Zinenko       // Reject this if the preceding argument was missing a name.
362f16bf7aSAlex Zinenko       if (argNames.empty() && !argTypes.empty())
372f16bf7aSAlex Zinenko         return parser.emitError(loc, "expected type instead of SSA identifier");
382f16bf7aSAlex Zinenko       argNames.push_back(argument);
392f16bf7aSAlex Zinenko 
402f16bf7aSAlex Zinenko       if (parser.parseColonType(argumentType))
412f16bf7aSAlex Zinenko         return failure();
422f16bf7aSAlex Zinenko     } else if (allowVariadic && succeeded(parser.parseOptionalEllipsis())) {
432f16bf7aSAlex Zinenko       isVariadic = true;
442f16bf7aSAlex Zinenko       return success();
452f16bf7aSAlex Zinenko     } else if (!argNames.empty()) {
462f16bf7aSAlex Zinenko       // Reject this if the preceding argument had a name.
472f16bf7aSAlex Zinenko       return parser.emitError(loc, "expected SSA identifier");
482f16bf7aSAlex Zinenko     } else if (parser.parseType(argumentType)) {
492f16bf7aSAlex Zinenko       return failure();
502f16bf7aSAlex Zinenko     }
512f16bf7aSAlex Zinenko 
522f16bf7aSAlex Zinenko     // Add the argument type.
532f16bf7aSAlex Zinenko     argTypes.push_back(argumentType);
542f16bf7aSAlex Zinenko 
552f16bf7aSAlex Zinenko     // Parse any argument attributes.
565eae715aSJacques Pienaar     NamedAttrList attrs;
572f16bf7aSAlex Zinenko     if (parser.parseOptionalAttrDict(attrs))
582f16bf7aSAlex Zinenko       return failure();
591c1803dbSChristian Sigg     if (!allowAttributes && !attrs.empty())
601c1803dbSChristian Sigg       return parser.emitError(loc, "expected arguments without attributes");
612f16bf7aSAlex Zinenko     argAttrs.push_back(attrs);
6281467f50SChris Lattner 
631e09f0a9SDominik Grewe     // Parse a location if specified.
6481467f50SChris Lattner     Optional<Location> explicitLoc;
6581467f50SChris Lattner     if (!argument.name.empty() &&
6681467f50SChris Lattner         parser.parseOptionalLocationSpecifier(explicitLoc))
6781467f50SChris Lattner       return failure();
68*e084679fSRiver Riddle     if (!explicitLoc)
69*e084679fSRiver Riddle       explicitLoc = parser.getEncodedSourceLoc(loc);
70*e084679fSRiver Riddle     argLocations.push_back(*explicitLoc);
7181467f50SChris Lattner 
722f16bf7aSAlex Zinenko     return success();
732f16bf7aSAlex Zinenko   };
742f16bf7aSAlex Zinenko 
752f16bf7aSAlex Zinenko   // Parse the function arguments.
76fdbb99cdSAlex Zinenko   isVariadic = false;
77fdbb99cdSAlex Zinenko   if (failed(parser.parseOptionalRParen())) {
782f16bf7aSAlex Zinenko     do {
792f16bf7aSAlex Zinenko       unsigned numTypedArguments = argTypes.size();
802f16bf7aSAlex Zinenko       if (parseArgument())
812f16bf7aSAlex Zinenko         return failure();
822f16bf7aSAlex Zinenko 
832f16bf7aSAlex Zinenko       llvm::SMLoc loc = parser.getCurrentLocation();
842f16bf7aSAlex Zinenko       if (argTypes.size() == numTypedArguments &&
852f16bf7aSAlex Zinenko           succeeded(parser.parseOptionalComma()))
862f16bf7aSAlex Zinenko         return parser.emitError(
872f16bf7aSAlex Zinenko             loc, "variadic arguments must be in the end of the argument list");
882f16bf7aSAlex Zinenko     } while (succeeded(parser.parseOptionalComma()));
892f16bf7aSAlex Zinenko     parser.parseRParen();
902f16bf7aSAlex Zinenko   }
912f16bf7aSAlex Zinenko 
922f16bf7aSAlex Zinenko   return success();
932f16bf7aSAlex Zinenko }
942f16bf7aSAlex Zinenko 
952f16bf7aSAlex Zinenko /// Parse a function result list.
962f16bf7aSAlex Zinenko ///
972f16bf7aSAlex Zinenko ///   function-result-list ::= function-result-list-parens
982f16bf7aSAlex Zinenko ///                          | non-function-type
992f16bf7aSAlex Zinenko ///   function-result-list-parens ::= `(` `)`
1002f16bf7aSAlex Zinenko ///                                 | `(` function-result-list-no-parens `)`
1012f16bf7aSAlex Zinenko ///   function-result-list-no-parens ::= function-result (`,` function-result)*
1022f16bf7aSAlex Zinenko ///   function-result ::= type attribute-dict?
1032f16bf7aSAlex Zinenko ///
1045eae715aSJacques Pienaar static ParseResult
1055eae715aSJacques Pienaar parseFunctionResultList(OpAsmParser &parser, SmallVectorImpl<Type> &resultTypes,
1065eae715aSJacques Pienaar                         SmallVectorImpl<NamedAttrList> &resultAttrs) {
1072f16bf7aSAlex Zinenko   if (failed(parser.parseOptionalLParen())) {
1082f16bf7aSAlex Zinenko     // We already know that there is no `(`, so parse a type.
1092f16bf7aSAlex Zinenko     // Because there is no `(`, it cannot be a function type.
1102f16bf7aSAlex Zinenko     Type ty;
1112f16bf7aSAlex Zinenko     if (parser.parseType(ty))
1122f16bf7aSAlex Zinenko       return failure();
1132f16bf7aSAlex Zinenko     resultTypes.push_back(ty);
1142f16bf7aSAlex Zinenko     resultAttrs.emplace_back();
1152f16bf7aSAlex Zinenko     return success();
1162f16bf7aSAlex Zinenko   }
1172f16bf7aSAlex Zinenko 
1182f16bf7aSAlex Zinenko   // Special case for an empty set of parens.
1192f16bf7aSAlex Zinenko   if (succeeded(parser.parseOptionalRParen()))
1202f16bf7aSAlex Zinenko     return success();
1212f16bf7aSAlex Zinenko 
1222f16bf7aSAlex Zinenko   // Parse individual function results.
1232f16bf7aSAlex Zinenko   do {
1242f16bf7aSAlex Zinenko     resultTypes.emplace_back();
1252f16bf7aSAlex Zinenko     resultAttrs.emplace_back();
1262f16bf7aSAlex Zinenko     if (parser.parseType(resultTypes.back()) ||
1272f16bf7aSAlex Zinenko         parser.parseOptionalAttrDict(resultAttrs.back())) {
1282f16bf7aSAlex Zinenko       return failure();
1292f16bf7aSAlex Zinenko     }
1302f16bf7aSAlex Zinenko   } while (succeeded(parser.parseOptionalComma()));
1312f16bf7aSAlex Zinenko   return parser.parseRParen();
1322f16bf7aSAlex Zinenko }
1332f16bf7aSAlex Zinenko 
1347ceffae1SRiver Riddle ParseResult mlir::function_interface_impl::parseFunctionSignature(
1352f16bf7aSAlex Zinenko     OpAsmParser &parser, bool allowVariadic,
1362f16bf7aSAlex Zinenko     SmallVectorImpl<OpAsmParser::OperandType> &argNames,
1375eae715aSJacques Pienaar     SmallVectorImpl<Type> &argTypes, SmallVectorImpl<NamedAttrList> &argAttrs,
138*e084679fSRiver Riddle     SmallVectorImpl<Location> &argLocations, bool &isVariadic,
1391e09f0a9SDominik Grewe     SmallVectorImpl<Type> &resultTypes,
1405eae715aSJacques Pienaar     SmallVectorImpl<NamedAttrList> &resultAttrs) {
1411c1803dbSChristian Sigg   bool allowArgAttrs = true;
1421c1803dbSChristian Sigg   if (parseFunctionArgumentList(parser, allowArgAttrs, allowVariadic, argNames,
1431e09f0a9SDominik Grewe                                 argTypes, argAttrs, argLocations, isVariadic))
1442f16bf7aSAlex Zinenko     return failure();
1452f16bf7aSAlex Zinenko   if (succeeded(parser.parseOptionalArrow()))
1462f16bf7aSAlex Zinenko     return parseFunctionResultList(parser, resultTypes, resultAttrs);
1472f16bf7aSAlex Zinenko   return success();
1482f16bf7aSAlex Zinenko }
1492f16bf7aSAlex Zinenko 
15053b946aaSRiver Riddle /// Implementation of `addArgAndResultAttrs` that is attribute list type
15153b946aaSRiver Riddle /// agnostic.
15253b946aaSRiver Riddle template <typename AttrListT, typename AttrArrayBuildFnT>
15353b946aaSRiver Riddle static void addArgAndResultAttrsImpl(Builder &builder, OperationState &result,
15453b946aaSRiver Riddle                                      ArrayRef<AttrListT> argAttrs,
15553b946aaSRiver Riddle                                      ArrayRef<AttrListT> resultAttrs,
15653b946aaSRiver Riddle                                      AttrArrayBuildFnT &&buildAttrArrayFn) {
15753b946aaSRiver Riddle   auto nonEmptyAttrsFn = [](const AttrListT &attrs) { return !attrs.empty(); };
1582f16bf7aSAlex Zinenko 
15953b946aaSRiver Riddle   // Add the attributes to the function arguments.
16053b946aaSRiver Riddle   if (!argAttrs.empty() && llvm::any_of(argAttrs, nonEmptyAttrsFn)) {
16153b946aaSRiver Riddle     ArrayAttr attrDicts = builder.getArrayAttr(buildAttrArrayFn(argAttrs));
1627ceffae1SRiver Riddle     result.addAttribute(function_interface_impl::getArgDictAttrName(),
1637ceffae1SRiver Riddle                         attrDicts);
16453b946aaSRiver Riddle   }
1652f16bf7aSAlex Zinenko   // Add the attributes to the function results.
16653b946aaSRiver Riddle   if (!resultAttrs.empty() && llvm::any_of(resultAttrs, nonEmptyAttrsFn)) {
16753b946aaSRiver Riddle     ArrayAttr attrDicts = builder.getArrayAttr(buildAttrArrayFn(resultAttrs));
1687ceffae1SRiver Riddle     result.addAttribute(function_interface_impl::getResultDictAttrName(),
1697ceffae1SRiver Riddle                         attrDicts);
17053b946aaSRiver Riddle   }
17153b946aaSRiver Riddle }
17253b946aaSRiver Riddle 
1737ceffae1SRiver Riddle void mlir::function_interface_impl::addArgAndResultAttrs(
17453b946aaSRiver Riddle     Builder &builder, OperationState &result, ArrayRef<DictionaryAttr> argAttrs,
17553b946aaSRiver Riddle     ArrayRef<DictionaryAttr> resultAttrs) {
17653b946aaSRiver Riddle   auto buildFn = [](ArrayRef<DictionaryAttr> attrs) {
17753b946aaSRiver Riddle     return ArrayRef<Attribute>(attrs.data(), attrs.size());
17853b946aaSRiver Riddle   };
17953b946aaSRiver Riddle   addArgAndResultAttrsImpl(builder, result, argAttrs, resultAttrs, buildFn);
18053b946aaSRiver Riddle }
1817ceffae1SRiver Riddle void mlir::function_interface_impl::addArgAndResultAttrs(
18253b946aaSRiver Riddle     Builder &builder, OperationState &result, ArrayRef<NamedAttrList> argAttrs,
18353b946aaSRiver Riddle     ArrayRef<NamedAttrList> resultAttrs) {
18453b946aaSRiver Riddle   MLIRContext *context = builder.getContext();
18553b946aaSRiver Riddle   auto buildFn = [=](ArrayRef<NamedAttrList> attrs) {
18653b946aaSRiver Riddle     return llvm::to_vector<8>(
18753b946aaSRiver Riddle         llvm::map_range(attrs, [=](const NamedAttrList &attrList) -> Attribute {
18853b946aaSRiver Riddle           return attrList.getDictionary(context);
18953b946aaSRiver Riddle         }));
19053b946aaSRiver Riddle   };
19153b946aaSRiver Riddle   addArgAndResultAttrsImpl(builder, result, argAttrs, resultAttrs, buildFn);
1922f16bf7aSAlex Zinenko }
1932f16bf7aSAlex Zinenko 
1947ceffae1SRiver Riddle ParseResult mlir::function_interface_impl::parseFunctionOp(
19553b946aaSRiver Riddle     OpAsmParser &parser, OperationState &result, bool allowVariadic,
19653b946aaSRiver Riddle     FuncTypeBuilder funcTypeBuilder) {
1971e09f0a9SDominik Grewe   SmallVector<OpAsmParser::OperandType> entryArgs;
1981e09f0a9SDominik Grewe   SmallVector<NamedAttrList> argAttrs;
1991e09f0a9SDominik Grewe   SmallVector<NamedAttrList> resultAttrs;
2001e09f0a9SDominik Grewe   SmallVector<Type> argTypes;
2011e09f0a9SDominik Grewe   SmallVector<Type> resultTypes;
202*e084679fSRiver Riddle   SmallVector<Location> argLocations;
2032f16bf7aSAlex Zinenko   auto &builder = parser.getBuilder();
2042f16bf7aSAlex Zinenko 
205dea24b42SRahul Joshi   // Parse visibility.
2068b5a3e46SRahul Joshi   impl::parseOptionalVisibilityKeyword(parser, result.attributes);
2078b5a3e46SRahul Joshi 
2082f16bf7aSAlex Zinenko   // Parse the name as a symbol.
2092f16bf7aSAlex Zinenko   StringAttr nameAttr;
2108b5a3e46SRahul Joshi   if (parser.parseSymbolName(nameAttr, SymbolTable::getSymbolAttrName(),
2112f16bf7aSAlex Zinenko                              result.attributes))
2122f16bf7aSAlex Zinenko     return failure();
2132f16bf7aSAlex Zinenko 
2142f16bf7aSAlex Zinenko   // Parse the function signature.
21567a339e9SRahul Joshi   llvm::SMLoc signatureLocation = parser.getCurrentLocation();
2162f16bf7aSAlex Zinenko   bool isVariadic = false;
2172f16bf7aSAlex Zinenko   if (parseFunctionSignature(parser, allowVariadic, entryArgs, argTypes,
2181e09f0a9SDominik Grewe                              argAttrs, argLocations, isVariadic, resultTypes,
2191e09f0a9SDominik Grewe                              resultAttrs))
2202f16bf7aSAlex Zinenko     return failure();
2212f16bf7aSAlex Zinenko 
2222f16bf7aSAlex Zinenko   std::string errorMessage;
22353b946aaSRiver Riddle   Type type = funcTypeBuilder(builder, argTypes, resultTypes,
22453b946aaSRiver Riddle                               VariadicFlag(isVariadic), errorMessage);
22553b946aaSRiver Riddle   if (!type) {
2262f16bf7aSAlex Zinenko     return parser.emitError(signatureLocation)
2272f16bf7aSAlex Zinenko            << "failed to construct function type"
2282f16bf7aSAlex Zinenko            << (errorMessage.empty() ? "" : ": ") << errorMessage;
22953b946aaSRiver Riddle   }
23053b946aaSRiver Riddle   result.addAttribute(getTypeAttrName(), TypeAttr::get(type));
2312f16bf7aSAlex Zinenko 
2322f16bf7aSAlex Zinenko   // If function attributes are present, parse them.
23367a339e9SRahul Joshi   NamedAttrList parsedAttributes;
23467a339e9SRahul Joshi   llvm::SMLoc attributeDictLocation = parser.getCurrentLocation();
23567a339e9SRahul Joshi   if (parser.parseOptionalAttrDictWithKeyword(parsedAttributes))
2362f16bf7aSAlex Zinenko     return failure();
2372f16bf7aSAlex Zinenko 
23867a339e9SRahul Joshi   // Disallow attributes that are inferred from elsewhere in the attribute
23967a339e9SRahul Joshi   // dictionary.
24067a339e9SRahul Joshi   for (StringRef disallowed :
24167a339e9SRahul Joshi        {SymbolTable::getVisibilityAttrName(), SymbolTable::getSymbolAttrName(),
24267a339e9SRahul Joshi         getTypeAttrName()}) {
24367a339e9SRahul Joshi     if (parsedAttributes.get(disallowed))
24467a339e9SRahul Joshi       return parser.emitError(attributeDictLocation, "'")
24567a339e9SRahul Joshi              << disallowed
24667a339e9SRahul Joshi              << "' is an inferred attribute and should not be specified in the "
24767a339e9SRahul Joshi                 "explicit attribute dictionary";
24867a339e9SRahul Joshi   }
24967a339e9SRahul Joshi   result.attributes.append(parsedAttributes);
25067a339e9SRahul Joshi 
2512f16bf7aSAlex Zinenko   // Add the attributes to the function arguments.
2522f16bf7aSAlex Zinenko   assert(argAttrs.size() == argTypes.size());
2532f16bf7aSAlex Zinenko   assert(resultAttrs.size() == resultTypes.size());
2542f16bf7aSAlex Zinenko   addArgAndResultAttrs(builder, result, argAttrs, resultAttrs);
2552f16bf7aSAlex Zinenko 
256fe7fdcacSRahul Joshi   // Parse the optional function body. The printer will not print the body if
257fe7fdcacSRahul Joshi   // its empty, so disallow parsing of empty body in the parser.
2582f16bf7aSAlex Zinenko   auto *body = result.addRegion();
259fe7fdcacSRahul Joshi   llvm::SMLoc loc = parser.getCurrentLocation();
260fe7fdcacSRahul Joshi   OptionalParseResult parseResult = parser.parseOptionalRegion(
261fe7fdcacSRahul Joshi       *body, entryArgs, entryArgs.empty() ? ArrayRef<Type>() : argTypes,
262*e084679fSRiver Riddle       entryArgs.empty() ? ArrayRef<Location>() : argLocations,
263fe7fdcacSRahul Joshi       /*enableNameShadowing=*/false);
264fe7fdcacSRahul Joshi   if (parseResult.hasValue()) {
265fe7fdcacSRahul Joshi     if (failed(*parseResult))
266fe7fdcacSRahul Joshi       return failure();
267fe7fdcacSRahul Joshi     // Function body was parsed, make sure its not empty.
268fe7fdcacSRahul Joshi     if (body->empty())
269fe7fdcacSRahul Joshi       return parser.emitError(loc, "expected non-empty function body");
270fe7fdcacSRahul Joshi   }
271fe7fdcacSRahul Joshi   return success();
2722f16bf7aSAlex Zinenko }
2732f16bf7aSAlex Zinenko 
27453b946aaSRiver Riddle /// Print a function result list. The provided `attrs` must either be null, or
27553b946aaSRiver Riddle /// contain a set of DictionaryAttrs of the same arity as `types`.
2762f16bf7aSAlex Zinenko static void printFunctionResultList(OpAsmPrinter &p, ArrayRef<Type> types,
27753b946aaSRiver Riddle                                     ArrayAttr attrs) {
2782f16bf7aSAlex Zinenko   assert(!types.empty() && "Should not be called for empty result list.");
27953b946aaSRiver Riddle   assert((!attrs || attrs.size() == types.size()) &&
28053b946aaSRiver Riddle          "Invalid number of attributes.");
28153b946aaSRiver Riddle 
2822f16bf7aSAlex Zinenko   auto &os = p.getStream();
28353b946aaSRiver Riddle   bool needsParens = types.size() > 1 || types[0].isa<FunctionType>() ||
28453b946aaSRiver Riddle                      (attrs && !attrs[0].cast<DictionaryAttr>().empty());
2852f16bf7aSAlex Zinenko   if (needsParens)
2862f16bf7aSAlex Zinenko     os << '(';
28753b946aaSRiver Riddle   llvm::interleaveComma(llvm::seq<size_t>(0, types.size()), os, [&](size_t i) {
28853b946aaSRiver Riddle     p.printType(types[i]);
28953b946aaSRiver Riddle     if (attrs)
29053b946aaSRiver Riddle       p.printOptionalAttrDict(attrs[i].cast<DictionaryAttr>().getValue());
2912f16bf7aSAlex Zinenko   });
2922f16bf7aSAlex Zinenko   if (needsParens)
2932f16bf7aSAlex Zinenko     os << ')';
2942f16bf7aSAlex Zinenko }
2952f16bf7aSAlex Zinenko 
2967ceffae1SRiver Riddle void mlir::function_interface_impl::printFunctionSignature(
29753b946aaSRiver Riddle     OpAsmPrinter &p, Operation *op, ArrayRef<Type> argTypes, bool isVariadic,
2982f16bf7aSAlex Zinenko     ArrayRef<Type> resultTypes) {
2992f16bf7aSAlex Zinenko   Region &body = op->getRegion(0);
3002f16bf7aSAlex Zinenko   bool isExternal = body.empty();
3012f16bf7aSAlex Zinenko 
3022f16bf7aSAlex Zinenko   p << '(';
30353b946aaSRiver Riddle   ArrayAttr argAttrs = op->getAttrOfType<ArrayAttr>(getArgDictAttrName());
3042f16bf7aSAlex Zinenko   for (unsigned i = 0, e = argTypes.size(); i < e; ++i) {
3052f16bf7aSAlex Zinenko     if (i > 0)
3062f16bf7aSAlex Zinenko       p << ", ";
3072f16bf7aSAlex Zinenko 
3082f16bf7aSAlex Zinenko     if (!isExternal) {
30981467f50SChris Lattner       ArrayRef<NamedAttribute> attrs;
31081467f50SChris Lattner       if (argAttrs)
31181467f50SChris Lattner         attrs = argAttrs[i].cast<DictionaryAttr>().getValue();
31281467f50SChris Lattner       p.printRegionArgument(body.getArgument(i), attrs);
31381467f50SChris Lattner     } else {
3142f16bf7aSAlex Zinenko       p.printType(argTypes[i]);
31553b946aaSRiver Riddle       if (argAttrs)
31653b946aaSRiver Riddle         p.printOptionalAttrDict(argAttrs[i].cast<DictionaryAttr>().getValue());
3172f16bf7aSAlex Zinenko     }
31881467f50SChris Lattner   }
3192f16bf7aSAlex Zinenko 
3202f16bf7aSAlex Zinenko   if (isVariadic) {
3212f16bf7aSAlex Zinenko     if (!argTypes.empty())
3222f16bf7aSAlex Zinenko       p << ", ";
3232f16bf7aSAlex Zinenko     p << "...";
3242f16bf7aSAlex Zinenko   }
3252f16bf7aSAlex Zinenko 
3262f16bf7aSAlex Zinenko   p << ')';
3272f16bf7aSAlex Zinenko 
3282f16bf7aSAlex Zinenko   if (!resultTypes.empty()) {
3292f16bf7aSAlex Zinenko     p.getStream() << " -> ";
33053b946aaSRiver Riddle     auto resultAttrs = op->getAttrOfType<ArrayAttr>(getResultDictAttrName());
3312f16bf7aSAlex Zinenko     printFunctionResultList(p, resultTypes, resultAttrs);
3322f16bf7aSAlex Zinenko   }
3332f16bf7aSAlex Zinenko }
3342f16bf7aSAlex Zinenko 
3357ceffae1SRiver Riddle void mlir::function_interface_impl::printFunctionAttributes(
33653b946aaSRiver Riddle     OpAsmPrinter &p, Operation *op, unsigned numInputs, unsigned numResults,
3372f16bf7aSAlex Zinenko     ArrayRef<StringRef> elided) {
3382f16bf7aSAlex Zinenko   // Print out function attributes, if present.
3392f16bf7aSAlex Zinenko   SmallVector<StringRef, 2> ignoredAttrs = {
34053b946aaSRiver Riddle       ::mlir::SymbolTable::getSymbolAttrName(), getTypeAttrName(),
34153b946aaSRiver Riddle       getArgDictAttrName(), getResultDictAttrName()};
3422f16bf7aSAlex Zinenko   ignoredAttrs.append(elided.begin(), elided.end());
3432f16bf7aSAlex Zinenko 
3442f16bf7aSAlex Zinenko   p.printOptionalAttrDictWithKeyword(op->getAttrs(), ignoredAttrs);
3452f16bf7aSAlex Zinenko }
3462f16bf7aSAlex Zinenko 
3477ceffae1SRiver Riddle void mlir::function_interface_impl::printFunctionOp(
3487ceffae1SRiver Riddle     OpAsmPrinter &p, Operation *op, ArrayRef<Type> argTypes, bool isVariadic,
349dea24b42SRahul Joshi     ArrayRef<Type> resultTypes) {
3502f16bf7aSAlex Zinenko   // Print the operation and the function name.
3512f16bf7aSAlex Zinenko   auto funcName =
3528b5a3e46SRahul Joshi       op->getAttrOfType<StringAttr>(SymbolTable::getSymbolAttrName())
3532f16bf7aSAlex Zinenko           .getValue();
354c41b16c2SMehdi Amini   p << ' ';
355dea24b42SRahul Joshi 
356dea24b42SRahul Joshi   StringRef visibilityAttrName = SymbolTable::getVisibilityAttrName();
357dea24b42SRahul Joshi   if (auto visibility = op->getAttrOfType<StringAttr>(visibilityAttrName))
3588b5a3e46SRahul Joshi     p << visibility.getValue() << ' ';
3592f16bf7aSAlex Zinenko   p.printSymbolName(funcName);
3602f16bf7aSAlex Zinenko 
3612f16bf7aSAlex Zinenko   printFunctionSignature(p, op, argTypes, isVariadic, resultTypes);
3628b5a3e46SRahul Joshi   printFunctionAttributes(p, op, argTypes.size(), resultTypes.size(),
363dea24b42SRahul Joshi                           {visibilityAttrName});
3642f16bf7aSAlex Zinenko   // Print the body if this is not an external function.
3652f16bf7aSAlex Zinenko   Region &body = op->getRegion(0);
3665c36ee8dSMogball   if (!body.empty()) {
3675c36ee8dSMogball     p << ' ';
3682f16bf7aSAlex Zinenko     p.printRegion(body, /*printEntryBlockArgs=*/false,
3692f16bf7aSAlex Zinenko                   /*printBlockTerminators=*/true);
3702f16bf7aSAlex Zinenko   }
3715c36ee8dSMogball }
372