[Mlir-commits] [mlir] [mlir][EmitC] Remove unreachable code and fix Windows build warning (PR #80677)

Simon Camphausen llvmlistbot at llvm.org
Mon Feb 5 07:29:33 PST 2024


https://github.com/simon-camp updated https://github.com/llvm/llvm-project/pull/80677

>From aa77e800a26f2d7c4499d5a1afd322dd2b187523 Mon Sep 17 00:00:00 2001
From: Simon Camphausen <simon.camphausen at iml.fraunhofer.de>
Date: Mon, 5 Feb 2024 13:01:09 +0000
Subject: [PATCH] [mlir][EmitC] Remove unreachable code and fix Windows build
 warning

---
 mlir/lib/Target/Cpp/TranslateToCpp.cpp | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index c0c6105409f8d8..7e797f6fffcf7b 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -68,12 +68,12 @@ inline LogicalResult interleaveCommaWithError(const Container &c,
 
 /// Return the precedence of a operator as an integer, higher values
 /// imply higher precedence.
-static int getOperatorPrecedence(Operation *operation) {
-  return llvm::TypeSwitch<Operation *, int>(operation)
+static FailureOr<int> getOperatorPrecedence(Operation *operation) {
+  return llvm::TypeSwitch<Operation *, FailureOr<int>>(operation)
       .Case<emitc::AddOp>([&](auto op) { return 11; })
       .Case<emitc::ApplyOp>([&](auto op) { return 13; })
       .Case<emitc::CastOp>([&](auto op) { return 13; })
-      .Case<emitc::CmpOp>([&](auto op) {
+      .Case<emitc::CmpOp>([&](auto op) -> FailureOr<int> {
         switch (op.getPredicate()) {
         case emitc::CmpPredicate::eq:
         case emitc::CmpPredicate::ne:
@@ -86,13 +86,14 @@ static int getOperatorPrecedence(Operation *operation) {
         case emitc::CmpPredicate::three_way:
           return 10;
         }
+        return failure();
       })
       .Case<emitc::DivOp>([&](auto op) { return 12; })
       .Case<emitc::MulOp>([&](auto op) { return 12; })
       .Case<emitc::RemOp>([&](auto op) { return 12; })
       .Case<emitc::SubOp>([&](auto op) { return 11; })
-      .Case<emitc::CallOpaqueOp>([&](auto op) { return 14; });
-  llvm_unreachable("Unsupported operator");
+      .Case<emitc::CallOpaqueOp>([&](auto op) { return 14; })
+      .Default([](auto op) { return failure(); });
 }
 
 namespace {
@@ -1072,7 +1073,10 @@ LogicalResult CppEmitter::emitExpression(ExpressionOp expressionOp) {
   Operation *rootOp = expressionOp.getRootOp();
 
   emittedExpression = expressionOp;
-  pushExpressionPrecedence(getOperatorPrecedence(rootOp));
+  FailureOr<int> precedence = getOperatorPrecedence(rootOp);
+  if (failed(precedence))
+    return failure();
+  pushExpressionPrecedence(precedence.value());
 
   if (failed(emitOperation(*rootOp, /*trailingSemicolon=*/false)))
     return failure();
@@ -1089,13 +1093,15 @@ LogicalResult CppEmitter::emitOperand(Value value) {
   if (isPartOfCurrentExpression(value)) {
     Operation *def = value.getDefiningOp();
     assert(def && "Expected operand to be defined by an operation");
-    int precedence = getOperatorPrecedence(def);
-    bool encloseInParenthesis = precedence < getExpressionPrecedence();
+    FailureOr<int> precedence = getOperatorPrecedence(def);
+    if (failed(precedence))
+      return failure();
+    bool encloseInParenthesis = precedence.value() < getExpressionPrecedence();
     if (encloseInParenthesis) {
       os << "(";
       pushExpressionPrecedence(lowestPrecedence());
     } else
-      pushExpressionPrecedence(precedence);
+      pushExpressionPrecedence(precedence.value());
 
     if (failed(emitOperation(*def, /*trailingSemicolon=*/false)))
       return failure();



More information about the Mlir-commits mailing list