[Mlir-commits] [mlir] 7c2ef38 - [mlir][NFC] Use `llvm::to_underlying` in sparse tensor IR detail

Vlad Serebrennikov llvmlistbot at llvm.org
Mon Oct 30 12:34:57 PDT 2023


Author: Vlad Serebrennikov
Date: 2023-10-30T22:34:50+03:00
New Revision: 7c2ef38c36eda2907cd6a3efff88bb86a1b381a3

URL: https://github.com/llvm/llvm-project/commit/7c2ef38c36eda2907cd6a3efff88bb86a1b381a3
DIFF: https://github.com/llvm/llvm-project/commit/7c2ef38c36eda2907cd6a3efff88bb86a1b381a3.diff

LOG: [mlir][NFC] Use `llvm::to_underlying` in sparse tensor IR detail

Added: 
    

Modified: 
    mlir/lib/Dialect/SparseTensor/IR/Detail/DimLvlMap.h
    mlir/lib/Dialect/SparseTensor/IR/Detail/TemplateExtras.h
    mlir/lib/Dialect/SparseTensor/IR/Detail/Var.h

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/SparseTensor/IR/Detail/DimLvlMap.h b/mlir/lib/Dialect/SparseTensor/IR/Detail/DimLvlMap.h
index 664b49509f070f4..b3200d0983eb790 100644
--- a/mlir/lib/Dialect/SparseTensor/IR/Detail/DimLvlMap.h
+++ b/mlir/lib/Dialect/SparseTensor/IR/Detail/DimLvlMap.h
@@ -12,6 +12,7 @@
 #include "Var.h"
 
 #include "mlir/Dialect/SparseTensor/IR/SparseTensor.h"
+#include "llvm/ADT/STLForwardCompat.h"
 
 namespace mlir {
 namespace sparse_tensor {
@@ -22,7 +23,7 @@ enum class ExprKind : bool { Dimension = false, Level = true };
 
 constexpr VarKind getVarKindAllowedInExpr(ExprKind ek) {
   using VK = std::underlying_type_t<VarKind>;
-  return VarKind{2 * static_cast<VK>(!to_underlying(ek))};
+  return VarKind{2 * static_cast<VK>(!llvm::to_underlying(ek))};
 }
 static_assert(getVarKindAllowedInExpr(ExprKind::Dimension) == VarKind::Level &&
               getVarKindAllowedInExpr(ExprKind::Level) == VarKind::Dimension);

diff  --git a/mlir/lib/Dialect/SparseTensor/IR/Detail/TemplateExtras.h b/mlir/lib/Dialect/SparseTensor/IR/Detail/TemplateExtras.h
index 7f0c1fd8c46c78b..383fbcb8dc52053 100644
--- a/mlir/lib/Dialect/SparseTensor/IR/Detail/TemplateExtras.h
+++ b/mlir/lib/Dialect/SparseTensor/IR/Detail/TemplateExtras.h
@@ -37,13 +37,6 @@ operator<<(llvm::raw_ostream &os, T const &t) {
   return os;
 }
 
-//===----------------------------------------------------------------------===//
-/// Convert an enum to its underlying type.
-template <typename Enum>
-constexpr std::underlying_type_t<Enum> to_underlying(Enum e) noexcept {
-  return static_cast<std::underlying_type_t<Enum>>(e);
-}
-
 //===----------------------------------------------------------------------===//
 template <typename T>
 static constexpr bool IsZeroCostAbstraction =

diff  --git a/mlir/lib/Dialect/SparseTensor/IR/Detail/Var.h b/mlir/lib/Dialect/SparseTensor/IR/Detail/Var.h
index 2606dd399eec81c..81f480187c059e7 100644
--- a/mlir/lib/Dialect/SparseTensor/IR/Detail/Var.h
+++ b/mlir/lib/Dialect/SparseTensor/IR/Detail/Var.h
@@ -13,6 +13,7 @@
 
 #include "mlir/IR/OpImplementation.h"
 #include "llvm/ADT/EnumeratedArray.h"
+#include "llvm/ADT/STLForwardCompat.h"
 #include "llvm/ADT/SmallBitVector.h"
 #include "llvm/ADT/StringMap.h"
 
@@ -31,13 +32,13 @@ namespace ir_detail {
 enum class VarKind { Symbol = 1, Dimension = 0, Level = 2 };
 
 [[nodiscard]] constexpr bool isWF(VarKind vk) {
-  const auto vk_ = to_underlying(vk);
+  const auto vk_ = llvm::to_underlying(vk);
   return 0 <= vk_ && vk_ <= 2;
 }
 
 /// Swaps `Dimension` and `Level`, but leaves `Symbol` the same.
 constexpr VarKind flipVarKind(VarKind vk) {
-  return VarKind{2 - to_underlying(vk)};
+  return VarKind{2 - llvm::to_underlying(vk)};
 }
 static_assert(flipVarKind(VarKind::Symbol) == VarKind::Symbol &&
               flipVarKind(VarKind::Dimension) == VarKind::Level &&
@@ -49,7 +50,7 @@ constexpr char toChar(VarKind vk) {
   // in the range [-44..126] (where that lower bound is under worst-case
   // rearranging of the expression); and `int_fast8_t` is the fastest type
   // which can support that range without over-/underflow.
-  const auto vk_ = static_cast<int_fast8_t>(to_underlying(vk));
+  const auto vk_ = static_cast<int_fast8_t>(llvm::to_underlying(vk));
   return static_cast<char>(100 + vk_ * (26 - vk_ * 11));
 }
 static_assert(toChar(VarKind::Symbol) == 's' &&
@@ -100,7 +101,7 @@ class Var {
   public:
     constexpr Impl(VarKind vk, Num n)
         : data((static_cast<Storage>(n) << 2) |
-               static_cast<Storage>(to_underlying(vk))) {
+               static_cast<Storage>(llvm::to_underlying(vk))) {
       assert(isWF(vk) && "unknown VarKind");
       assert(isWF_Num(n) && "Var::Num is too large");
     }
@@ -215,7 +216,7 @@ class Ranks final {
 
   static constexpr unsigned to_index(VarKind vk) {
     assert(isWF(vk) && "unknown VarKind");
-    return static_cast<unsigned>(to_underlying(vk));
+    return static_cast<unsigned>(llvm::to_underlying(vk));
   }
 
 public:
@@ -349,7 +350,7 @@ class VarEnv final {
   /// to live too long.
   VarInfo const &access(VarInfo::ID id) const {
     // `SmallVector::operator[]` already asserts the index is in-bounds.
-    return vars[to_underlying(id)];
+    return vars[llvm::to_underlying(id)];
   }
   VarInfo const *access(std::optional<VarInfo::ID> oid) const {
     return oid ? &access(*oid) : nullptr;


        


More information about the Mlir-commits mailing list