Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions clang/include/clang/CIR/Dialect/IR/CIRTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,11 @@ def CIR_FuncType : CIR_Type<"Func", "func"> {
}];

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

let builders = [
Expand Down
94 changes: 27 additions & 67 deletions clang/lib/CIR/Dialect/IR/CIRTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ using cir::MissingFeatures;
// CIR Custom Parser/Printer Signatures
//===----------------------------------------------------------------------===//

static mlir::ParseResult parseFuncType(mlir::AsmParser &p,
mlir::Type &optionalReturnTypes,
llvm::SmallVector<mlir::Type> &params,
bool &isVarArg);
static mlir::ParseResult
parseFuncTypeParams(mlir::AsmParser &p, llvm::SmallVector<mlir::Type> &params,
bool &isVarArg);

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

// A special parser is needed for function returning void to handle the missing
// type.
static mlir::ParseResult parseFuncTypeReturn(mlir::AsmParser &p,
mlir::Type &optionalReturnType) {
if (succeeded(p.parseOptionalArrow())) {
// `->` found. It must be followed by the return type.
return p.parseType(optionalReturnType);
}
// Function has `void` return in C++, no return in MLIR.
optionalReturnType = {};
return success();
}

// A special pretty-printer for function returning or not a result.
static void printFuncTypeReturn(mlir::AsmPrinter &p,
mlir::Type optionalReturnType) {
if (optionalReturnType)
p << " -> " << optionalReturnType;
}

// Custom parser that parses function parameters of form `(<type>*, ...)`.
static mlir::ParseResult
parseFuncTypeArgs(mlir::AsmParser &p, llvm::SmallVector<mlir::Type> &params,
bool &isVarArg) {
parseFuncTypeParams(mlir::AsmParser &p, llvm::SmallVector<mlir::Type> &params,
bool &isVarArg) {
isVarArg = false;
if (failed(p.parseLParen()))
return failure();
if (succeeded(p.parseOptionalRParen())) {
// `()` empty argument list
return mlir::success();
}
do {
if (succeeded(p.parseOptionalEllipsis())) {
// `...`, which must be the last thing in the list.
isVarArg = true;
break;
} else {
mlir::Type argType;
if (failed(p.parseType(argType)))
return failure();
params.push_back(argType);
}
} while (succeeded(p.parseOptionalComma()));
return p.parseRParen();
return p.parseCommaSeparatedList(
AsmParser::Delimiter::Paren, [&]() -> mlir::ParseResult {
if (isVarArg)
return p.emitError(p.getCurrentLocation(),
"variadic `...` must be the last parameter");
if (succeeded(p.parseOptionalEllipsis())) {
isVarArg = true;
return success();
}
mlir::Type type;
if (failed(p.parseType(type)))
return failure();
params.push_back(type);
return success();
});
}

static void printFuncTypeArgs(mlir::AsmPrinter &p,
mlir::ArrayRef<mlir::Type> params,
bool isVarArg) {
static void printFuncTypeParams(mlir::AsmPrinter &p,
mlir::ArrayRef<mlir::Type> params,
bool isVarArg) {
p << '(';
llvm::interleaveComma(params, p,
[&p](mlir::Type type) { p.printType(type); });
Expand All @@ -992,23 +969,6 @@ static void printFuncTypeArgs(mlir::AsmPrinter &p,
p << ')';
}

// Use a custom parser to handle the optional return and argument types without
// an optional anchor.
static mlir::ParseResult parseFuncType(mlir::AsmParser &p,
mlir::Type &optionalReturnType,
llvm::SmallVector<mlir::Type> &params,
bool &isVarArg) {
if (failed(parseFuncTypeArgs(p, params, isVarArg)))
return failure();
return parseFuncTypeReturn(p, optionalReturnType);
}

static void printFuncType(mlir::AsmPrinter &p, mlir::Type optionalReturnType,
mlir::ArrayRef<mlir::Type> params, bool isVarArg) {
printFuncTypeArgs(p, params, isVarArg);
printFuncTypeReturn(p, optionalReturnType);
}

/// Get the C-style return type of the function, which is !cir.void if the
/// function returns nothing and the actual return type otherwise.
mlir::Type FuncType::getReturnType() const {
Expand Down
8 changes: 8 additions & 0 deletions clang/test/CIR/IR/invalid.cir
Original file line number Diff line number Diff line change
Expand Up @@ -1500,3 +1500,11 @@ cir.func @cast0(%arg0: !s32i, %arg1: !s32i) {
// expected-error @below {{!cir.func cannot have an explicit 'void' return type}}
// expected-error @below {{failed to parse CIR_PointerType parameter}}
cir.global external dsolocal @vfp = #cir.ptr<null> : !cir.ptr<!cir.func<(!s32i) -> !cir.void>>

// -----

// Verify that variadic functions do not allow an ellipsis anywhere except at
// the end of the parameter list.

// expected-error @below {{variadic `...` must be the last parameter}}
!fty = !cir.func<(..., !s32i)>
Loading