Skip to content

Commit a404c36

Browse files
committed
[CIR] FuncType changes: respond to feedback
Incorporate llvm/clangir#1413 to simplify the parsing/printing of `FuncType`. Get rid of `FuncOp::verifyType` which did nothing useful.
1 parent 3e396ab commit a404c36

File tree

4 files changed

+33
-93
lines changed

4 files changed

+33
-93
lines changed

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,6 @@ def FuncOp : CIR_Op<"func", [
365365
return getFunctionType().getReturnTypes();
366366
}
367367

368-
/// Hook for OpTrait::FunctionOpInterfaceTrait, called after verifying that
369-
/// the 'type' attribute is present and checks if it holds a function type.
370-
/// Ensures getType, getNumFuncArguments, and getNumFuncResults can be
371-
/// called safely.
372-
llvm::LogicalResult verifyType();
373-
374368
//===------------------------------------------------------------------===//
375369
// SymbolOpInterface Methods
376370
//===------------------------------------------------------------------===//

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,11 @@ def CIR_FuncType : CIR_Type<"Func", "func"> {
302302
}];
303303

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

311312
let builders = [

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -416,21 +416,6 @@ void cir::FuncOp::print(OpAsmPrinter &p) {
416416
}
417417
}
418418

419-
// Hook for OpTrait::FunctionLike, called after verifying that the 'type'
420-
// attribute is present. This can check for preconditions of the
421-
// getNumArguments hook not failing.
422-
LogicalResult cir::FuncOp::verifyType() {
423-
auto type = getFunctionType();
424-
if (!isa<cir::FuncType>(type))
425-
return emitOpError("requires '" + getFunctionTypeAttrName().str() +
426-
"' attribute of function type");
427-
if (auto rt = type.getReturnTypes();
428-
!rt.empty() && mlir::isa<cir::VoidType>(rt.front()))
429-
return emitOpError("The return type for a function returning void should "
430-
"be empty instead of an explicit !cir.void");
431-
return success();
432-
}
433-
434419
// TODO(CIR): The properties of functions that require verification haven't
435420
// been implemented yet.
436421
mlir::LogicalResult cir::FuncOp::verify() { return success(); }

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

Lines changed: 29 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
// CIR Custom Parser/Printer Signatures
2121
//===----------------------------------------------------------------------===//
2222

23-
static mlir::ParseResult parseFuncType(mlir::AsmParser &p,
24-
mlir::Type &optionalReturnTypes,
25-
llvm::SmallVector<mlir::Type> &params,
26-
bool &isVarArg);
27-
static void printFuncType(mlir::AsmPrinter &p, mlir::Type optionalReturnTypes,
28-
mlir::ArrayRef<mlir::Type> params, bool isVarArg);
23+
static mlir::ParseResult
24+
parseFuncTypeParams(mlir::AsmParser &p, llvm::SmallVector<mlir::Type> &params,
25+
bool &isVarArg);
26+
static void printFuncTypeParams(mlir::AsmPrinter &p,
27+
mlir::ArrayRef<mlir::Type> params,
28+
bool isVarArg);
2929

3030
//===----------------------------------------------------------------------===//
3131
// Get autogenerated stuff
@@ -283,54 +283,31 @@ FuncType FuncType::clone(TypeRange inputs, TypeRange results) const {
283283
return get(llvm::to_vector(inputs), results[0], isVarArg());
284284
}
285285

286-
// A special parser is needed for function returning void to handle the missing
287-
// type.
288-
static mlir::ParseResult parseFuncTypeReturn(mlir::AsmParser &p,
289-
mlir::Type &optionalReturnType) {
290-
if (succeeded(p.parseOptionalArrow())) {
291-
// `->` found. It must be followed by the return type.
292-
return p.parseType(optionalReturnType);
293-
}
294-
// Function has `void` return in C++, no return in MLIR.
295-
optionalReturnType = {};
296-
return success();
297-
}
298-
299-
// A special pretty-printer for function returning or not a result.
300-
static void printFuncTypeReturn(mlir::AsmPrinter &p,
301-
mlir::Type optionalReturnType) {
302-
if (optionalReturnType)
303-
p << " -> " << optionalReturnType;
304-
}
305-
286+
// Custom parser that parses function parameters of form `(<type>*, ...)`.
306287
static mlir::ParseResult
307-
parseFuncTypeArgs(mlir::AsmParser &p, llvm::SmallVector<mlir::Type> &params,
308-
bool &isVarArg) {
288+
parseFuncTypeParams(mlir::AsmParser &p, llvm::SmallVector<mlir::Type> &params,
289+
bool &isVarArg) {
309290
isVarArg = false;
310-
if (failed(p.parseLParen()))
311-
return failure();
312-
if (succeeded(p.parseOptionalRParen())) {
313-
// `()` empty argument list
314-
return mlir::success();
315-
}
316-
do {
317-
if (succeeded(p.parseOptionalEllipsis())) {
318-
// `...`, which must be the last thing in the list.
319-
isVarArg = true;
320-
break;
321-
} else {
322-
mlir::Type argType;
323-
if (failed(p.parseType(argType)))
324-
return failure();
325-
params.push_back(argType);
326-
}
327-
} while (succeeded(p.parseOptionalComma()));
328-
return p.parseRParen();
329-
}
330-
331-
static void printFuncTypeArgs(mlir::AsmPrinter &p,
332-
mlir::ArrayRef<mlir::Type> params,
333-
bool isVarArg) {
291+
return p.parseCommaSeparatedList(
292+
AsmParser::Delimiter::Paren, [&]() -> mlir::ParseResult {
293+
if (isVarArg)
294+
return p.emitError(p.getCurrentLocation(),
295+
"variadic `...` must be the last parameter");
296+
if (succeeded(p.parseOptionalEllipsis())) {
297+
isVarArg = true;
298+
return success();
299+
}
300+
mlir::Type type;
301+
if (failed(p.parseType(type)))
302+
return failure();
303+
params.push_back(type);
304+
return success();
305+
});
306+
}
307+
308+
static void printFuncTypeParams(mlir::AsmPrinter &p,
309+
mlir::ArrayRef<mlir::Type> params,
310+
bool isVarArg) {
334311
p << '(';
335312
llvm::interleaveComma(params, p,
336313
[&p](mlir::Type type) { p.printType(type); });
@@ -342,23 +319,6 @@ static void printFuncTypeArgs(mlir::AsmPrinter &p,
342319
p << ')';
343320
}
344321

345-
// Use a custom parser to handle the optional return and argument types without
346-
// an optional anchor.
347-
static mlir::ParseResult parseFuncType(mlir::AsmParser &p,
348-
mlir::Type &optionalReturnType,
349-
llvm::SmallVector<mlir::Type> &params,
350-
bool &isVarArg) {
351-
if (failed(parseFuncTypeArgs(p, params, isVarArg)))
352-
return failure();
353-
return parseFuncTypeReturn(p, optionalReturnType);
354-
}
355-
356-
static void printFuncType(mlir::AsmPrinter &p, mlir::Type optionalReturnType,
357-
mlir::ArrayRef<mlir::Type> params, bool isVarArg) {
358-
printFuncTypeArgs(p, params, isVarArg);
359-
printFuncTypeReturn(p, optionalReturnType);
360-
}
361-
362322
/// Get the C-style return type of the function, which is !cir.void if the
363323
/// function returns nothing and the actual return type otherwise.
364324
mlir::Type FuncType::getReturnType() const {

0 commit comments

Comments
 (0)