Skip to content

Commit

Permalink
Remove result list, there is at most one result
Browse files Browse the repository at this point in the history
  • Loading branch information
cferry-AMD committed Sep 6, 2024
1 parent 2137c0f commit 2e595fc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 59 deletions.
91 changes: 33 additions & 58 deletions mlir/lib/Dialect/EmitC/IR/FunctionOpAssembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ parseFunctionArgumentList(OpAsmParser &parser, bool allowVariadic,
if (!arguments.empty() && arguments.back().ssaName.name.empty())
return parser.emitError(argument.ssaName.location,
"expected type instead of SSA identifier");
if (!parser.parseOptionalKeyword("ref")) {
if (succeeded(parser.parseOptionalKeyword("ref"))) {
llvm::ArrayRef<NamedAttribute> origAttrs;
if (!argument.attrs.empty())
origAttrs = argument.attrs.getValue();
Expand All @@ -87,7 +87,7 @@ parseFunctionArgumentList(OpAsmParser &parser, bool allowVariadic,
parser.parseOptionalAttrDict(attrs) ||
parser.parseOptionalLocationSpecifier(argument.sourceLoc))
return failure();
if (!parser.parseOptionalKeyword("ref")) {
if (succeeded(parser.parseOptionalKeyword("ref"))) {
// Add attribute to argument
attrs.push_back(NamedAttribute(
StringAttr::get(parser.getContext(),
Expand All @@ -101,45 +101,38 @@ parseFunctionArgumentList(OpAsmParser &parser, bool allowVariadic,
});
}

/// Parse a function result list.
/// Parse a function result.
///
/// 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?
/// function-result ::= type | `(` type attribute-dict? `)`
///
static ParseResult
parseFunctionResultList(OpAsmParser &parser, SmallVectorImpl<Type> &resultTypes,
SmallVectorImpl<DictionaryAttr> &resultAttrs) {
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();
parseFunctionResult(OpAsmParser &parser, SmallVectorImpl<Type> &resultTypes,
SmallVectorImpl<DictionaryAttr> &resultAttrs) {

bool hasLParen = succeeded(parser.parseOptionalLParen());

if (hasLParen) {
// Special case for an empty set of parens.
if (succeeded(parser.parseOptionalRParen()))
return success();
}

// Special case for an empty set of parens.
if (succeeded(parser.parseOptionalRParen()))
// Parse a single type.
Type ty;
if (parser.parseType(ty))
return failure();
resultTypes.push_back(ty);
resultAttrs.emplace_back();

// There can be no attribute without parentheses (they would be confused with
// the function body)
if (!hasLParen)
return success();

// Parse individual function results.
if (parser.parseCommaSeparatedList([&]() -> ParseResult {
resultTypes.emplace_back();
resultAttrs.emplace_back();
NamedAttrList attrs;
if (parser.parseType(resultTypes.back()) ||
parser.parseOptionalAttrDict(attrs))
return failure();
resultAttrs.back() = attrs.getDictionary(parser.getContext());
return success();
}))
return failure();
// Parse result attributes if any.
NamedAttrList attrs;
if (succeeded(parser.parseOptionalAttrDict(attrs)))
resultAttrs.back() = attrs.getDictionary(parser.getContext());

return parser.parseRParen();
}
Expand All @@ -152,7 +145,7 @@ parseFunctionSignature(OpAsmParser &parser, bool allowVariadic,
if (parseFunctionArgumentList(parser, allowVariadic, arguments, isVariadic))
return failure();
if (succeeded(parser.parseOptionalArrow()))
return parseFunctionResultList(parser, resultTypes, resultAttrs);
return parseFunctionResult(parser, resultTypes, resultAttrs);
return success();
}

Expand Down Expand Up @@ -238,28 +231,6 @@ parseFunctionOp(OpAsmParser &parser, OperationState &result, bool allowVariadic,
return success();
}

/// Print a function result list. The provided `attrs` must either be null, or
/// contain a set of DictionaryAttrs of the same arity as `types`.
static void printFunctionResultList(OpAsmPrinter &p, ArrayRef<Type> types,
ArrayAttr attrs) {
assert(!types.empty() && "Should not be called for empty result list.");
assert((!attrs || attrs.size() == types.size()) &&
"Invalid number of attributes.");

auto &os = p.getStream();
bool needsParens = types.size() > 1 || llvm::isa<FunctionType>(types[0]) ||
(attrs && !llvm::cast<DictionaryAttr>(attrs[0]).empty());
if (needsParens)
os << '(';
llvm::interleaveComma(llvm::seq<size_t>(0, types.size()), os, [&](size_t i) {
p.printType(types[i]);
if (attrs)
p.printOptionalAttrDict(llvm::cast<DictionaryAttr>(attrs[i]).getValue());
});
if (needsParens)
os << ')';
}

void printFunctionSignature(OpAsmPrinter &p, FuncOp op, ArrayRef<Type> argTypes,
bool isVariadic, ArrayRef<Type> resultTypes) {
Region &body = op->getRegion(0);
Expand Down Expand Up @@ -298,9 +269,13 @@ void printFunctionSignature(OpAsmPrinter &p, FuncOp op, ArrayRef<Type> argTypes,
p << ')';

if (!resultTypes.empty()) {
assert(resultTypes.size() == 1);
p.getStream() << " -> ";
auto resultAttrs = op.getResAttrsAttr();
printFunctionResultList(p, resultTypes, resultAttrs);
p.printType(resultTypes[0]);
if (resultAttrs)
p.printOptionalAttrDict(
llvm::cast<DictionaryAttr>(resultAttrs[0]).getValue());
}
}

Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Dialect/EmitC/invalid_ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func.func @test_expression_multiple_results(%arg0: i32) -> i32 {

// -----

// expected-error @+1 {{'emitc.func' op requires zero or exactly one result, but has 2}}
// expected-error @+1 {{expected ')'}}
emitc.func @multiple_results(%0: i32) -> (i32, i32) {
emitc.return %0 : i32
}
Expand Down

0 comments on commit 2e595fc

Please sign in to comment.