Skip to content

Commit ead1ba1

Browse files
committed
[CIR] Simplify FuncType printer/parser
1 parent aff6245 commit ead1ba1

File tree

3 files changed

+38
-70
lines changed

3 files changed

+38
-70
lines changed

clang/include/clang/CIR/Dialect/IR/CIRTypes.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,11 @@ def CIR_FuncType : CIR_Type<"Func", "func"> {
387387
}];
388388

389389
let parameters = (ins ArrayRefParameter<"mlir::Type">:$inputs,
390-
"mlir::Type":$optionalReturnType,
390+
OptionalParameter<"mlir::Type">:$optionalReturnType,
391391
"bool":$varArg);
392-
// Use a custom parser to handle the optional return and argument types
392+
// Use a custom parser to handle argument types with variadic elipsis.
393393
let assemblyFormat = [{
394-
`<` custom<FuncType>($optionalReturnType, $inputs, $varArg) `>`
394+
`<` custom<FuncTypeParams>($inputs, $varArg) (`->` $optionalReturnType^)? `>`
395395
}];
396396

397397
let builders = [

clang/lib/CIR/Dialect/IR/CIRTypes.cpp

Lines changed: 27 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ using cir::MissingFeatures;
4242
// CIR Custom Parser/Printer Signatures
4343
//===----------------------------------------------------------------------===//
4444

45-
static mlir::ParseResult parseFuncType(mlir::AsmParser &p,
46-
mlir::Type &optionalReturnTypes,
47-
llvm::SmallVector<mlir::Type> &params,
48-
bool &isVarArg);
45+
static mlir::ParseResult
46+
parseFuncTypeParams(mlir::AsmParser &p, llvm::SmallVector<mlir::Type> &params,
47+
bool &isVarArg);
4948

50-
static void printFuncType(mlir::AsmPrinter &p, mlir::Type optionalReturnTypes,
51-
mlir::ArrayRef<mlir::Type> params, bool isVarArg);
49+
static void printFuncTypeParams(mlir::AsmPrinter &p,
50+
mlir::ArrayRef<mlir::Type> params,
51+
bool isVarArg);
5252
static mlir::ParseResult parsePointerAddrSpace(mlir::AsmParser &p,
5353
mlir::Attribute &addrSpaceAttr);
5454
static void printPointerAddrSpace(mlir::AsmPrinter &p,
@@ -933,54 +933,31 @@ FuncType FuncType::clone(TypeRange inputs, TypeRange results) const {
933933
return get(llvm::to_vector(inputs), results[0], isVarArg());
934934
}
935935

936-
// A special parser is needed for function returning void to handle the missing
937-
// type.
938-
static mlir::ParseResult parseFuncTypeReturn(mlir::AsmParser &p,
939-
mlir::Type &optionalReturnType) {
940-
if (succeeded(p.parseOptionalArrow())) {
941-
// `->` found. It must be followed by the return type.
942-
return p.parseType(optionalReturnType);
943-
}
944-
// Function has `void` return in C++, no return in MLIR.
945-
optionalReturnType = {};
946-
return success();
947-
}
948-
949-
// A special pretty-printer for function returning or not a result.
950-
static void printFuncTypeReturn(mlir::AsmPrinter &p,
951-
mlir::Type optionalReturnType) {
952-
if (optionalReturnType)
953-
p << " -> " << optionalReturnType;
954-
}
955-
936+
// Custom parser that parses function parameters of form `(<type>*, ...)`.
956937
static mlir::ParseResult
957-
parseFuncTypeArgs(mlir::AsmParser &p, llvm::SmallVector<mlir::Type> &params,
958-
bool &isVarArg) {
938+
parseFuncTypeParams(mlir::AsmParser &p, llvm::SmallVector<mlir::Type> &params,
939+
bool &isVarArg) {
959940
isVarArg = false;
960-
if (failed(p.parseLParen()))
961-
return failure();
962-
if (succeeded(p.parseOptionalRParen())) {
963-
// `()` empty argument list
964-
return mlir::success();
965-
}
966-
do {
967-
if (succeeded(p.parseOptionalEllipsis())) {
968-
// `...`, which must be the last thing in the list.
969-
isVarArg = true;
970-
break;
971-
} else {
972-
mlir::Type argType;
973-
if (failed(p.parseType(argType)))
974-
return failure();
975-
params.push_back(argType);
976-
}
977-
} while (succeeded(p.parseOptionalComma()));
978-
return p.parseRParen();
941+
return p.parseCommaSeparatedList(
942+
AsmParser::Delimiter::Paren, [&]() -> mlir::ParseResult {
943+
if (isVarArg)
944+
return p.emitError(p.getCurrentLocation(),
945+
"variadic `...` must be the last parameter");
946+
if (succeeded(p.parseOptionalEllipsis())) {
947+
isVarArg = true;
948+
return success();
949+
}
950+
mlir::Type type;
951+
if (failed(p.parseType(type)))
952+
return failure();
953+
params.push_back(type);
954+
return success();
955+
});
979956
}
980957

981-
static void printFuncTypeArgs(mlir::AsmPrinter &p,
982-
mlir::ArrayRef<mlir::Type> params,
983-
bool isVarArg) {
958+
static void printFuncTypeParams(mlir::AsmPrinter &p,
959+
mlir::ArrayRef<mlir::Type> params,
960+
bool isVarArg) {
984961
p << '(';
985962
llvm::interleaveComma(params, p,
986963
[&p](mlir::Type type) { p.printType(type); });
@@ -992,23 +969,6 @@ static void printFuncTypeArgs(mlir::AsmPrinter &p,
992969
p << ')';
993970
}
994971

995-
// Use a custom parser to handle the optional return and argument types without
996-
// an optional anchor.
997-
static mlir::ParseResult parseFuncType(mlir::AsmParser &p,
998-
mlir::Type &optionalReturnType,
999-
llvm::SmallVector<mlir::Type> &params,
1000-
bool &isVarArg) {
1001-
if (failed(parseFuncTypeArgs(p, params, isVarArg)))
1002-
return failure();
1003-
return parseFuncTypeReturn(p, optionalReturnType);
1004-
}
1005-
1006-
static void printFuncType(mlir::AsmPrinter &p, mlir::Type optionalReturnType,
1007-
mlir::ArrayRef<mlir::Type> params, bool isVarArg) {
1008-
printFuncTypeArgs(p, params, isVarArg);
1009-
printFuncTypeReturn(p, optionalReturnType);
1010-
}
1011-
1012972
/// Get the C-style return type of the function, which is !cir.void if the
1013973
/// function returns nothing and the actual return type otherwise.
1014974
mlir::Type FuncType::getReturnType() const {

clang/test/CIR/IR/invalid.cir

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,3 +1500,11 @@ cir.func @cast0(%arg0: !s32i, %arg1: !s32i) {
15001500
// expected-error @below {{!cir.func cannot have an explicit 'void' return type}}
15011501
// expected-error @below {{failed to parse CIR_PointerType parameter}}
15021502
cir.global external dsolocal @vfp = #cir.ptr<null> : !cir.ptr<!cir.func<(!s32i) -> !cir.void>>
1503+
1504+
// -----
1505+
1506+
// Verify that variadic functions do not allow an ellipsis anywhere except at
1507+
// the end of the parameter list.
1508+
1509+
// expected-error @below {{variadic `...` must be the last parameter}}
1510+
!fty = !cir.func<(..., !s32i)>

0 commit comments

Comments
 (0)