Skip to content

[CIR] Simplify error emission to return failures directly #141032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2025

Conversation

xlauko
Copy link
Contributor

@xlauko xlauko commented May 22, 2025

Mirrors incubator changes from llvm/clangir#1634

@xlauko xlauko marked this pull request as ready for review May 22, 2025 10:24
@xlauko
Copy link
Contributor Author

xlauko commented May 22, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@llvmbot llvmbot added clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project labels May 22, 2025
@xlauko xlauko requested review from erichkeane and andykaylor May 22, 2025 10:25
@llvmbot
Copy link
Member

llvmbot commented May 22, 2025

@llvm/pr-subscribers-clangir

@llvm/pr-subscribers-clang

Author: Henrich Lauko (xlauko)

Changes

Mirrors incubator changes from llvm/clangir#1634


Full diff: https://github.com/llvm/llvm-project/pull/141032.diff

2 Files Affected:

  • (modified) clang/lib/CIR/Dialect/IR/CIRAttrs.cpp (+19-28)
  • (modified) clang/lib/CIR/Dialect/IR/CIRTypes.cpp (+10-14)
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..fa8180c890506 100644
--- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
@@ -13,6 +13,7 @@
 #include "clang/CIR/Dialect/IR/CIRTypes.h"
 
 #include "mlir/IR/DialectImplementation.h"
+#include "mlir/Support/LLVM.h"
 #include "clang/CIR/Dialect/IR/CIRDialect.h"
 #include "clang/CIR/Dialect/IR/CIRTypesDetails.h"
 #include "clang/CIR/MissingFeatures.h"
@@ -205,10 +206,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 +420,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 +628,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();
 }
 

Copy link
Contributor

@andykaylor andykaylor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I just have a question about an added include.

@xlauko xlauko force-pushed the users/xlauko/cir-emit-fail-returns branch from f575584 to c3ffda8 Compare May 22, 2025 16:49
@xlauko
Copy link
Contributor Author

xlauko commented May 22, 2025

Merge activity

  • May 22, 4:51 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • May 22, 4:53 PM UTC: @xlauko merged this pull request with Graphite.

@xlauko xlauko merged commit dc68166 into main May 22, 2025
6 of 9 checks passed
@xlauko xlauko deleted the users/xlauko/cir-emit-fail-returns branch May 22, 2025 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants