[Mlir-commits] [mlir] 47d1857 - [mlir] Provide LLVMType::getPrimitiveSizeInBits

Alex Zinenko llvmlistbot at llvm.org
Mon Aug 17 09:01:50 PDT 2020


Author: Alex Zinenko
Date: 2020-08-17T18:01:42+02:00
New Revision: 47d185784d1b82da66da01fe59c05975d455f222

URL: https://github.com/llvm/llvm-project/commit/47d185784d1b82da66da01fe59c05975d455f222
DIFF: https://github.com/llvm/llvm-project/commit/47d185784d1b82da66da01fe59c05975d455f222.diff

LOG: [mlir] Provide LLVMType::getPrimitiveSizeInBits

This function is available on llvm::Type and has been used by some clients of
the LLVM dialect before the transition. Implement the MLIR counterpart.

Reviewed By: schweitz

Differential Revision: https://reviews.llvm.org/D85847

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/LLVMIR/LLVMTypes.h
    mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMTypes.h b/mlir/include/mlir/Dialect/LLVMIR/LLVMTypes.h
index 38a70d510e50..b71964b5d0f8 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/LLVMTypes.h
+++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMTypes.h
@@ -18,6 +18,7 @@
 
 namespace llvm {
 class ElementCount;
+class TypeSize;
 } // namespace llvm
 
 namespace mlir {
@@ -105,6 +106,12 @@ class LLVMType : public Type {
 
   LLVMDialect &getDialect();
 
+  /// Returns the size of a primitive type (including vectors) in bits, for
+  /// example, the size of !llvm.i16 is 16 and the size of !llvm.vec<4 x i16>
+  /// is 64. Returns 0 for non-primitive (aggregates such as struct) or types
+  /// that don't have a size (such as void).
+  llvm::TypeSize getPrimitiveSizeInBits();
+
   /// Floating-point type utilities.
   bool isBFloatTy() { return isa<LLVMBFloatType>(); }
   bool isHalfTy() { return isa<LLVMHalfType>(); }

diff  --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp
index 8ff7fc56edda..f4a278dfbbb0 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp
@@ -18,6 +18,7 @@
 #include "mlir/IR/DialectImplementation.h"
 #include "mlir/IR/TypeSupport.h"
 
+#include "llvm/ADT/TypeSwitch.h"
 #include "llvm/Support/TypeSize.h"
 
 using namespace mlir;
@@ -35,6 +36,40 @@ LLVMDialect &LLVMType::getDialect() {
   return static_cast<LLVMDialect &>(Type::getDialect());
 }
 
+//----------------------------------------------------------------------------//
+// Misc type utilities.
+
+llvm::TypeSize LLVMType::getPrimitiveSizeInBits() {
+  return llvm::TypeSwitch<LLVMType, llvm::TypeSize>(*this)
+      .Case<LLVMHalfType, LLVMBFloatType>(
+          [](LLVMType) { return llvm::TypeSize::Fixed(16); })
+      .Case<LLVMFloatType>([](LLVMType) { return llvm::TypeSize::Fixed(32); })
+      .Case<LLVMDoubleType, LLVMX86MMXType>(
+          [](LLVMType) { return llvm::TypeSize::Fixed(64); })
+      .Case<LLVMIntegerType>([](LLVMIntegerType intTy) {
+        return llvm::TypeSize::Fixed(intTy.getBitWidth());
+      })
+      .Case<LLVMX86FP80Type>([](LLVMType) { return llvm::TypeSize::Fixed(80); })
+      .Case<LLVMPPCFP128Type, LLVMFP128Type>(
+          [](LLVMType) { return llvm::TypeSize::Fixed(128); })
+      .Case<LLVMVectorType>([](LLVMVectorType t) {
+        llvm::TypeSize elementSize =
+            t.getElementType().getPrimitiveSizeInBits();
+        llvm::ElementCount elementCount = t.getElementCount();
+        assert(!elementSize.isScalable() &&
+               "vector type should have fixed-width elements");
+        return llvm::TypeSize(elementSize.getFixedSize() * elementCount.Min,
+                              elementCount.Scalable);
+      })
+      .Default([](LLVMType ty) {
+        assert((ty.isa<LLVMVoidType, LLVMLabelType, LLVMMetadataType,
+                       LLVMTokenType, LLVMStructType, LLVMArrayType,
+                       LLVMPointerType, LLVMFunctionType>()) &&
+               "unexpected missing support for primitive type");
+        return llvm::TypeSize::Fixed(0);
+      });
+}
+
 //----------------------------------------------------------------------------//
 // Integer type utilities.
 


        


More information about the Mlir-commits mailing list