[Mlir-commits] [mlir] [mlir][IR] Add `isFloat()` and clean code (PR #112897)

Longsheng Mou llvmlistbot at llvm.org
Fri Oct 18 05:43:36 PDT 2024


https://github.com/CoTinker updated https://github.com/llvm/llvm-project/pull/112897

>From ee182ff52574c9f8ec502d1d10e3f9ef021eef91 Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Fri, 18 Oct 2024 20:27:46 +0800
Subject: [PATCH] [mlir][IR] Add `isFloat()` and clean code

This PR adds an isFloat() function without checking width and
refactors the code for clarity and maintainability.
---
 mlir/include/mlir/IR/Types.h |  5 +++--
 mlir/lib/IR/Types.cpp        | 22 ++++++++++------------
 2 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/mlir/include/mlir/IR/Types.h b/mlir/include/mlir/IR/Types.h
index acd0f894abbbe6..30e151ece4850c 100644
--- a/mlir/include/mlir/IR/Types.h
+++ b/mlir/include/mlir/IR/Types.h
@@ -125,6 +125,7 @@ class Type {
   // Convenience predicates.  This is only for floating point types,
   // derived types should use isa/dyn_cast.
   bool isIndex() const;
+  bool isFloat() const;
   bool isFloat4E2M1FN() const;
   bool isFloat6E2M3FN() const;
   bool isFloat6E3M2FN() const;
@@ -164,10 +165,10 @@ class Type {
 
   /// Return true if this is a signless integer or index type.
   bool isSignlessIntOrIndex() const;
-  /// Return true if this is a signless integer, index, or float type.
-  bool isSignlessIntOrIndexOrFloat() const;
   /// Return true of this is a signless integer or a float type.
   bool isSignlessIntOrFloat() const;
+  /// Return true if this is a signless integer, index, or float type.
+  bool isSignlessIntOrIndexOrFloat() const;
 
   /// Return true if this is an integer (of any signedness) or an index type.
   bool isIntOrIndex() const;
diff --git a/mlir/lib/IR/Types.cpp b/mlir/lib/IR/Types.cpp
index e190902b2e4898..5914faa327ad23 100644
--- a/mlir/lib/IR/Types.cpp
+++ b/mlir/lib/IR/Types.cpp
@@ -63,6 +63,8 @@ bool Type::isF128() const { return llvm::isa<Float128Type>(*this); }
 
 bool Type::isIndex() const { return llvm::isa<IndexType>(*this); }
 
+bool Type::isFloat() const { return llvm::isa<FloatType>(*this); }
+
 bool Type::isInteger() const { return llvm::isa<IntegerType>(*this); }
 
 /// Return true if this is an integer type with the specified width.
@@ -109,26 +111,22 @@ bool Type::isUnsignedInteger(unsigned width) const {
 }
 
 bool Type::isSignlessIntOrIndex() const {
-  return isSignlessInteger() || llvm::isa<IndexType>(*this);
-}
-
-bool Type::isSignlessIntOrIndexOrFloat() const {
-  return isSignlessInteger() || llvm::isa<IndexType, FloatType>(*this);
+  return isSignlessInteger() || isIndex();
 }
 
 bool Type::isSignlessIntOrFloat() const {
-  return isSignlessInteger() || llvm::isa<FloatType>(*this);
+  return isSignlessInteger() || isFloat();
 }
 
-bool Type::isIntOrIndex() const {
-  return llvm::isa<IntegerType>(*this) || isIndex();
+bool Type::isSignlessIntOrIndexOrFloat() const {
+  return isSignlessIntOrIndex() || isFloat();
 }
 
-bool Type::isIntOrFloat() const {
-  return llvm::isa<IntegerType, FloatType>(*this);
-}
+bool Type::isIntOrIndex() const { return isInteger() || isIndex(); }
+
+bool Type::isIntOrFloat() const { return isInteger() || isFloat(); }
 
-bool Type::isIntOrIndexOrFloat() const { return isIntOrFloat() || isIndex(); }
+bool Type::isIntOrIndexOrFloat() const { return isIntOrIndex() || isFloat(); }
 
 unsigned Type::getIntOrFloatBitWidth() const {
   assert(isIntOrFloat() && "only integers and floats have a bitwidth");



More information about the Mlir-commits mailing list