[Mlir-commits] [mlir] fb5a64f - [mlir-c] Add method to create unmanaged dense resource elements attr

Jacques Pienaar llvmlistbot at llvm.org
Fri Dec 16 13:36:22 PST 2022


Author: Jacques Pienaar
Date: 2022-12-16T13:36:15-08:00
New Revision: fb5a64f0cfaca04828c2c8ca76c1bcc98fbc7a12

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

LOG: [mlir-c] Add method to create unmanaged dense resource elements attr

Following DenseElementsAttr pattern.

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

Added: 
    

Modified: 
    mlir/include/mlir-c/BuiltinAttributes.h
    mlir/lib/CAPI/IR/BuiltinAttributes.cpp
    mlir/test/CAPI/ir.c

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir-c/BuiltinAttributes.h b/mlir/include/mlir-c/BuiltinAttributes.h
index 8887897bcb89b..3b1ac466dd07b 100644
--- a/mlir/include/mlir-c/BuiltinAttributes.h
+++ b/mlir/include/mlir-c/BuiltinAttributes.h
@@ -510,6 +510,77 @@ mlirDenseElementsAttrGetStringValue(MlirAttribute attr, intptr_t pos);
 MLIR_CAPI_EXPORTED const void *
 mlirDenseElementsAttrGetRawData(MlirAttribute attr);
 
+//===----------------------------------------------------------------------===//
+// Resource blob attributes.
+//===----------------------------------------------------------------------===//
+
+MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseBoolResourceElementsAttrGet(
+    MlirType shapedType, MlirStringRef name, intptr_t numElements,
+    const int *elements);
+MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseUInt8ResourceElementsAttrGet(
+    MlirType shapedType, MlirStringRef name, intptr_t numElements,
+    const uint8_t *elements);
+MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseInt8ResourceElementsAttrGet(
+    MlirType shapedType, MlirStringRef name, intptr_t numElements,
+    const int8_t *elements);
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirUnmanagedDenseUInt16ResourceElementsAttrGet(MlirType shapedType,
+                                                MlirStringRef name,
+                                                intptr_t numElements,
+                                                const uint16_t *elements);
+MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseInt16ResourceElementsAttrGet(
+    MlirType shapedType, MlirStringRef name, intptr_t numElements,
+    const int16_t *elements);
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirUnmanagedDenseUInt32ResourceElementsAttrGet(MlirType shapedType,
+                                                MlirStringRef name,
+                                                intptr_t numElements,
+                                                const uint32_t *elements);
+MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseInt32ResourceElementsAttrGet(
+    MlirType shapedType, MlirStringRef name, intptr_t numElements,
+    const int32_t *elements);
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirUnmanagedDenseUInt64ResourceElementsAttrGet(MlirType shapedType,
+                                                MlirStringRef name,
+                                                intptr_t numElements,
+                                                const uint64_t *elements);
+MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseInt64ResourceElementsAttrGet(
+    MlirType shapedType, MlirStringRef name, intptr_t numElements,
+    const int64_t *elements);
+MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseFloatResourceElementsAttrGet(
+    MlirType shapedType, MlirStringRef name, intptr_t numElements,
+    const float *elements);
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirUnmanagedDenseDoubleResourceElementsAttrGet(MlirType shapedType,
+                                                MlirStringRef name,
+                                                intptr_t numElements,
+                                                const double *elements);
+
+/// Returns the pos-th value (flat contiguous indexing) of a specific type
+/// contained by the given dense resource elements attribute.
+MLIR_CAPI_EXPORTED bool
+mlirDenseBoolResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos);
+MLIR_CAPI_EXPORTED int8_t
+mlirDenseInt8ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos);
+MLIR_CAPI_EXPORTED uint8_t
+mlirDenseUInt8ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos);
+MLIR_CAPI_EXPORTED int16_t
+mlirDenseInt16ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos);
+MLIR_CAPI_EXPORTED uint16_t
+mlirDenseUInt16ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos);
+MLIR_CAPI_EXPORTED int32_t
+mlirDenseInt32ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos);
+MLIR_CAPI_EXPORTED uint32_t
+mlirDenseUInt32ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos);
+MLIR_CAPI_EXPORTED int64_t
+mlirDenseInt64ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos);
+MLIR_CAPI_EXPORTED uint64_t
+mlirDenseUInt64ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos);
+MLIR_CAPI_EXPORTED float
+mlirDenseFloatResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos);
+MLIR_CAPI_EXPORTED double
+mlirDenseDoubleResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos);
+
 //===----------------------------------------------------------------------===//
 // Sparse elements attribute.
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/CAPI/IR/BuiltinAttributes.cpp b/mlir/lib/CAPI/IR/BuiltinAttributes.cpp
index 05ecb0fe80792..e392d053b2e2b 100644
--- a/mlir/lib/CAPI/IR/BuiltinAttributes.cpp
+++ b/mlir/lib/CAPI/IR/BuiltinAttributes.cpp
@@ -7,10 +7,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir-c/BuiltinAttributes.h"
+#include "mlir-c/Support.h"
 #include "mlir/CAPI/AffineMap.h"
 #include "mlir/CAPI/IR.h"
 #include "mlir/CAPI/Support.h"
+#include "mlir/IR/AsmState.h"
 #include "mlir/IR/Attributes.h"
+#include "mlir/IR/BuiltinAttributes.h"
 #include "mlir/IR/BuiltinTypes.h"
 
 using namespace mlir;
@@ -698,6 +701,146 @@ const void *mlirDenseElementsAttrGetRawData(MlirAttribute attr) {
       unwrap(attr).cast<DenseElementsAttr>().getRawData().data());
 }
 
+//===----------------------------------------------------------------------===//
+// Resource blob attributes.
+//===----------------------------------------------------------------------===//
+
+template <typename U, typename T>
+static MlirAttribute getDenseResource(MlirType shapedType, MlirStringRef name,
+                                      intptr_t numElements, const T *elements) {
+  return wrap(U::get(unwrap(shapedType).cast<ShapedType>(), unwrap(name),
+                     UnmanagedAsmResourceBlob::allocateInferAlign(
+                         llvm::makeArrayRef(elements, numElements))));
+}
+
+MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseBoolResourceElementsAttrGet(
+    MlirType shapedType, MlirStringRef name, intptr_t numElements,
+    const int *elements) {
+  return getDenseResource<DenseBoolResourceElementsAttr>(shapedType, name,
+                                                         numElements, elements);
+}
+MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseUInt8ResourceElementsAttrGet(
+    MlirType shapedType, MlirStringRef name, intptr_t numElements,
+    const uint8_t *elements) {
+  return getDenseResource<DenseUI8ResourceElementsAttr>(shapedType, name,
+                                                        numElements, elements);
+}
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirUnmanagedDenseUInt16ResourceElementsAttrGet(MlirType shapedType,
+                                                MlirStringRef name,
+                                                intptr_t numElements,
+                                                const uint16_t *elements) {
+  return getDenseResource<DenseUI16ResourceElementsAttr>(shapedType, name,
+                                                         numElements, elements);
+}
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirUnmanagedDenseUInt32ResourceElementsAttrGet(MlirType shapedType,
+                                                MlirStringRef name,
+                                                intptr_t numElements,
+                                                const uint32_t *elements) {
+  return getDenseResource<DenseUI32ResourceElementsAttr>(shapedType, name,
+                                                         numElements, elements);
+}
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirUnmanagedDenseUInt64ResourceElementsAttrGet(MlirType shapedType,
+                                                MlirStringRef name,
+                                                intptr_t numElements,
+                                                const uint64_t *elements) {
+  return getDenseResource<DenseUI64ResourceElementsAttr>(shapedType, name,
+                                                         numElements, elements);
+}
+MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseInt8ResourceElementsAttrGet(
+    MlirType shapedType, MlirStringRef name, intptr_t numElements,
+    const int8_t *elements) {
+  return getDenseResource<DenseUI8ResourceElementsAttr>(shapedType, name,
+                                                        numElements, elements);
+}
+MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseInt16ResourceElementsAttrGet(
+    MlirType shapedType, MlirStringRef name, intptr_t numElements,
+    const int16_t *elements) {
+  return getDenseResource<DenseUI16ResourceElementsAttr>(shapedType, name,
+                                                         numElements, elements);
+}
+MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseInt32ResourceElementsAttrGet(
+    MlirType shapedType, MlirStringRef name, intptr_t numElements,
+    const int32_t *elements) {
+  return getDenseResource<DenseUI32ResourceElementsAttr>(shapedType, name,
+                                                         numElements, elements);
+}
+MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseInt64ResourceElementsAttrGet(
+    MlirType shapedType, MlirStringRef name, intptr_t numElements,
+    const int64_t *elements) {
+  return getDenseResource<DenseUI64ResourceElementsAttr>(shapedType, name,
+                                                         numElements, elements);
+}
+MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseFloatResourceElementsAttrGet(
+    MlirType shapedType, MlirStringRef name, intptr_t numElements,
+    const float *elements) {
+  return getDenseResource<DenseF32ResourceElementsAttr>(shapedType, name,
+                                                        numElements, elements);
+}
+MLIR_CAPI_EXPORTED MlirAttribute
+mlirUnmanagedDenseDoubleResourceElementsAttrGet(MlirType shapedType,
+                                                MlirStringRef name,
+                                                intptr_t numElements,
+                                                const double *elements) {
+  return getDenseResource<DenseF64ResourceElementsAttr>(shapedType, name,
+                                                        numElements, elements);
+}
+
+template <typename U, typename T>
+static T getDenseResourceVal(MlirAttribute attr, intptr_t pos) {
+  return (*unwrap(attr).cast<U>().tryGetAsArrayRef())[pos];
+}
+
+MLIR_CAPI_EXPORTED bool
+mlirDenseBoolResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) {
+  return getDenseResourceVal<DenseBoolResourceElementsAttr, uint8_t>(attr, pos);
+}
+MLIR_CAPI_EXPORTED uint8_t
+mlirDenseUInt8ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) {
+  return getDenseResourceVal<DenseUI8ResourceElementsAttr, uint8_t>(attr, pos);
+}
+MLIR_CAPI_EXPORTED uint16_t
+mlirDenseUInt16ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) {
+  return getDenseResourceVal<DenseUI16ResourceElementsAttr, uint16_t>(attr,
+                                                                      pos);
+}
+MLIR_CAPI_EXPORTED uint32_t
+mlirDenseUInt32ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) {
+  return getDenseResourceVal<DenseUI32ResourceElementsAttr, uint32_t>(attr,
+                                                                      pos);
+}
+MLIR_CAPI_EXPORTED uint64_t
+mlirDenseUInt64ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) {
+  return getDenseResourceVal<DenseUI64ResourceElementsAttr, uint64_t>(attr,
+                                                                      pos);
+}
+MLIR_CAPI_EXPORTED int8_t
+mlirDenseInt8ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) {
+  return getDenseResourceVal<DenseUI8ResourceElementsAttr, int8_t>(attr, pos);
+}
+MLIR_CAPI_EXPORTED int16_t
+mlirDenseInt16ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) {
+  return getDenseResourceVal<DenseUI16ResourceElementsAttr, int16_t>(attr, pos);
+}
+MLIR_CAPI_EXPORTED int32_t
+mlirDenseInt32ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) {
+  return getDenseResourceVal<DenseUI32ResourceElementsAttr, int32_t>(attr, pos);
+}
+MLIR_CAPI_EXPORTED int64_t
+mlirDenseInt64ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) {
+  return getDenseResourceVal<DenseUI64ResourceElementsAttr, int64_t>(attr, pos);
+}
+MLIR_CAPI_EXPORTED float
+mlirDenseFloatResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) {
+  return getDenseResourceVal<DenseF32ResourceElementsAttr, float>(attr, pos);
+}
+MLIR_CAPI_EXPORTED double
+mlirDenseDoubleResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) {
+  return getDenseResourceVal<DenseF64ResourceElementsAttr, double>(attr, pos);
+}
+
 //===----------------------------------------------------------------------===//
 // Sparse elements attribute.
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/test/CAPI/ir.c b/mlir/test/CAPI/ir.c
index d48af140ef841..5b91883a3d8da 100644
--- a/mlir/test/CAPI/ir.c
+++ b/mlir/test/CAPI/ir.c
@@ -1234,6 +1234,77 @@ int printBuiltinAttributes(MlirContext ctx) {
       mlirStridedLayoutAttrGetStride(stridedLayoutAttr, 2) != 13)
     return 22;
 
+  MlirAttribute uint8Blob = mlirUnmanagedDenseUInt8ResourceElementsAttrGet(
+      mlirRankedTensorTypeGet(2, shape, mlirIntegerTypeUnsignedGet(ctx, 8),
+                              encoding),
+      mlirStringRefCreateFromCString("resource_ui8"), 2, uints8);
+  MlirAttribute uint16Blob = mlirUnmanagedDenseUInt16ResourceElementsAttrGet(
+      mlirRankedTensorTypeGet(2, shape, mlirIntegerTypeUnsignedGet(ctx, 16),
+                              encoding),
+      mlirStringRefCreateFromCString("resource_ui16"), 2, uints16);
+  MlirAttribute uint32Blob = mlirUnmanagedDenseUInt32ResourceElementsAttrGet(
+      mlirRankedTensorTypeGet(2, shape, mlirIntegerTypeUnsignedGet(ctx, 32),
+                              encoding),
+      mlirStringRefCreateFromCString("resource_ui32"), 2, uints32);
+  MlirAttribute uint64Blob = mlirUnmanagedDenseUInt64ResourceElementsAttrGet(
+      mlirRankedTensorTypeGet(2, shape, mlirIntegerTypeUnsignedGet(ctx, 64),
+                              encoding),
+      mlirStringRefCreateFromCString("resource_ui64"), 2, uints64);
+  MlirAttribute int8Blob = mlirUnmanagedDenseInt8ResourceElementsAttrGet(
+      mlirRankedTensorTypeGet(2, shape, mlirIntegerTypeGet(ctx, 8), encoding),
+      mlirStringRefCreateFromCString("resource_i8"), 2, ints8);
+  MlirAttribute int16Blob = mlirUnmanagedDenseInt16ResourceElementsAttrGet(
+      mlirRankedTensorTypeGet(2, shape, mlirIntegerTypeGet(ctx, 16), encoding),
+      mlirStringRefCreateFromCString("resource_i16"), 2, ints16);
+  MlirAttribute int32Blob = mlirUnmanagedDenseInt32ResourceElementsAttrGet(
+      mlirRankedTensorTypeGet(2, shape, mlirIntegerTypeGet(ctx, 32), encoding),
+      mlirStringRefCreateFromCString("resource_i32"), 2, ints32);
+  MlirAttribute int64Blob = mlirUnmanagedDenseInt64ResourceElementsAttrGet(
+      mlirRankedTensorTypeGet(2, shape, mlirIntegerTypeGet(ctx, 64), encoding),
+      mlirStringRefCreateFromCString("resource_i64"), 2, ints64);
+  MlirAttribute floatsBlob = mlirUnmanagedDenseFloatResourceElementsAttrGet(
+      mlirRankedTensorTypeGet(2, shape, mlirF32TypeGet(ctx), encoding),
+      mlirStringRefCreateFromCString("resource_f32"), 2, floats);
+  MlirAttribute doublesBlob = mlirUnmanagedDenseDoubleResourceElementsAttrGet(
+      mlirRankedTensorTypeGet(2, shape, mlirF64TypeGet(ctx), encoding),
+      mlirStringRefCreateFromCString("resource_f64"), 2, doubles);
+
+  mlirAttributeDump(uint8Blob);
+  mlirAttributeDump(uint16Blob);
+  mlirAttributeDump(uint32Blob);
+  mlirAttributeDump(uint64Blob);
+  mlirAttributeDump(int8Blob);
+  mlirAttributeDump(int16Blob);
+  mlirAttributeDump(int32Blob);
+  mlirAttributeDump(int64Blob);
+  mlirAttributeDump(floatsBlob);
+  mlirAttributeDump(doublesBlob);
+  // CHECK: dense_resource<resource_ui8> : tensor<1x2xui8>
+  // CHECK: dense_resource<resource_ui16> : tensor<1x2xui16>
+  // CHECK: dense_resource<resource_ui32> : tensor<1x2xui32>
+  // CHECK: dense_resource<resource_ui64> : tensor<1x2xui64>
+  // CHECK: dense_resource<resource_i8> : tensor<1x2xi8>
+  // CHECK: dense_resource<resource_i16> : tensor<1x2xi16>
+  // CHECK: dense_resource<resource_i32> : tensor<1x2xi32>
+  // CHECK: dense_resource<resource_i64> : tensor<1x2xi64>
+  // CHECK: dense_resource<resource_f32> : tensor<1x2xf32>
+  // CHECK: dense_resource<resource_f64> : tensor<1x2xf64>
+
+  if (mlirDenseUInt8ResourceElementsAttrGetValue(uint8Blob, 1) != 1 ||
+      mlirDenseUInt16ResourceElementsAttrGetValue(uint16Blob, 1) != 1 ||
+      mlirDenseUInt32ResourceElementsAttrGetValue(uint32Blob, 1) != 1 ||
+      mlirDenseUInt64ResourceElementsAttrGetValue(uint64Blob, 1) != 1 ||
+      mlirDenseInt8ResourceElementsAttrGetValue(int8Blob, 1) != 1 ||
+      mlirDenseInt16ResourceElementsAttrGetValue(int16Blob, 1) != 1 ||
+      mlirDenseInt32ResourceElementsAttrGetValue(int32Blob, 1) != 1 ||
+      mlirDenseInt64ResourceElementsAttrGetValue(int64Blob, 1) != 1 ||
+      fabsf(mlirDenseF32ArrayGetElement(floatArray, 1) - 1.0f) > 1E-6f ||
+      fabsf(mlirDenseFloatResourceElementsAttrGetValue(floatsBlob, 1) - 1.0f) >
+          1e-6 ||
+      fabs(mlirDenseDoubleResourceElementsAttrGetValue(doublesBlob, 1) - 1.0f) >
+          1e-6)
+    return 23;
+
   return 0;
 }
 


        


More information about the Mlir-commits mailing list