https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/141032
>From c3ffda832255d6b7280a7d6f61c134f0dc890f5d Mon Sep 17 00:00:00 2001 From: xlauko <xla...@mail.muni.cz> Date: Thu, 22 May 2025 12:24:14 +0200 Subject: [PATCH] [CIR] Simplify error emission to return failures directly Mirrors incubator changes from https://github.com/llvm/clangir/pull/1634 --- clang/lib/CIR/Dialect/IR/CIRAttrs.cpp | 47 +++++++++++---------------- clang/lib/CIR/Dialect/IR/CIRTypes.cpp | 23 +++++-------- 2 files changed, 28 insertions(+), 42 deletions(-) diff --git a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp index 6f41cd4388ac2..c4fb4942aec75 100644 --- a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp @@ -138,17 +138,13 @@ void IntAttr::print(AsmPrinter &printer) const { LogicalResult IntAttr::verify(function_ref<InFlightDiagnostic()> emitError, Type type, APInt value) { - if (!mlir::isa<IntType>(type)) { - emitError() << "expected 'simple.int' type"; - return failure(); - } + if (!mlir::isa<IntType>(type)) + return emitError() << "expected 'simple.int' type"; auto intType = mlir::cast<IntType>(type); - if (value.getBitWidth() != intType.getWidth()) { - emitError() << "type and value bitwidth mismatch: " << intType.getWidth() - << " != " << value.getBitWidth(); - return failure(); - } + if (value.getBitWidth() != intType.getWidth()) + return emitError() << "type and value bitwidth mismatch: " + << intType.getWidth() << " != " << value.getBitWidth(); return success(); } @@ -182,10 +178,8 @@ FPAttr FPAttr::getZero(Type type) { LogicalResult FPAttr::verify(function_ref<InFlightDiagnostic()> emitError, CIRFPTypeInterface fpType, APFloat value) { if (APFloat::SemanticsToEnum(fpType.getFloatSemantics()) != - APFloat::SemanticsToEnum(value.getSemantics())) { - emitError() << "floating-point semantics mismatch"; - return failure(); - } + APFloat::SemanticsToEnum(value.getSemantics())) + return emitError() << "floating-point semantics mismatch"; return success(); } @@ -195,10 +189,10 @@ LogicalResult FPAttr::verify(function_ref<InFlightDiagnostic()> emitError, //===----------------------------------------------------------------------===// LogicalResult -ConstArrayAttr::verify(function_ref<::mlir::InFlightDiagnostic()> emitError, - Type type, Attribute elts, int trailingZerosNum) { +ConstArrayAttr::verify(function_ref<InFlightDiagnostic()> emitError, Type type, + Attribute elts, int trailingZerosNum) { - if (!(mlir::isa<ArrayAttr>(elts) || mlir::isa<StringAttr>(elts))) + if (!(mlir::isa<ArrayAttr, StringAttr>(elts))) return emitError() << "constant array expects ArrayAttr or StringAttr"; if (auto strAttr = mlir::dyn_cast<StringAttr>(elts)) { @@ -206,11 +200,10 @@ ConstArrayAttr::verify(function_ref<::mlir::InFlightDiagnostic()> emitError, const auto intTy = mlir::dyn_cast<IntType>(arrayTy.getElementType()); // TODO: add CIR type for char. - if (!intTy || intTy.getWidth() != 8) { - emitError() << "constant array element for string literals expects " - "!cir.int<u, 8> element type"; - return failure(); - } + if (!intTy || intTy.getWidth() != 8) + return emitError() + << "constant array element for string literals expects " + "!cir.int<u, 8> element type"; return success(); } @@ -303,22 +296,20 @@ void ConstArrayAttr::print(AsmPrinter &printer) const { // CIR ConstVectorAttr //===----------------------------------------------------------------------===// -LogicalResult cir::ConstVectorAttr::verify( - function_ref<::mlir::InFlightDiagnostic()> emitError, Type type, - ArrayAttr elts) { +LogicalResult +cir::ConstVectorAttr::verify(function_ref<InFlightDiagnostic()> emitError, + Type type, ArrayAttr elts) { - if (!mlir::isa<cir::VectorType>(type)) { + if (!mlir::isa<cir::VectorType>(type)) return emitError() << "type of cir::ConstVectorAttr is not a " "cir::VectorType: " << type; - } const auto vecType = mlir::cast<cir::VectorType>(type); - if (vecType.getSize() != elts.size()) { + if (vecType.getSize() != elts.size()) return emitError() << "number of constant elements should match vector size"; - } // Check if the types of the elements match LogicalResult elementTypeCheck = success(); diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp index 9a44f923ac143..479d249516699 100644 --- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp @@ -205,10 +205,8 @@ RecordType::verify(function_ref<mlir::InFlightDiagnostic()> emitError, llvm::ArrayRef<mlir::Type> members, mlir::StringAttr name, bool incomplete, bool packed, bool padded, RecordType::RecordKind kind) { - if (name && name.getValue().empty()) { - emitError() << "identified records cannot have an empty name"; - return mlir::failure(); - } + if (name && name.getValue().empty()) + return emitError() << "identified records cannot have an empty name"; return mlir::success(); } @@ -421,12 +419,10 @@ uint64_t IntType::getABIAlignment(const mlir::DataLayout &dataLayout, mlir::LogicalResult IntType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError, unsigned width, bool isSigned) { - if (width < IntType::minBitwidth() || width > IntType::maxBitwidth()) { - emitError() << "IntType only supports widths from " - << IntType::minBitwidth() << " up to " - << IntType::maxBitwidth(); - return mlir::failure(); - } + if (width < IntType::minBitwidth() || width > IntType::maxBitwidth()) + return emitError() << "IntType only supports widths from " + << IntType::minBitwidth() << " up to " + << IntType::maxBitwidth(); return mlir::success(); } @@ -631,10 +627,9 @@ mlir::LogicalResult FuncType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError, llvm::ArrayRef<mlir::Type> argTypes, mlir::Type returnType, bool isVarArg) { - if (returnType && mlir::isa<cir::VoidType>(returnType)) { - emitError() << "!cir.func cannot have an explicit 'void' return type"; - return mlir::failure(); - } + if (mlir::isa_and_nonnull<cir::VoidType>(returnType)) + return emitError() + << "!cir.func cannot have an explicit 'void' return type"; return mlir::success(); } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits