diff --git a/mlir/include/mlir-c/BuiltinAttributes.h b/mlir/include/mlir-c/BuiltinAttributes.h --- a/mlir/include/mlir-c/BuiltinAttributes.h +++ b/mlir/include/mlir-c/BuiltinAttributes.h @@ -510,6 +510,77 @@ 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 --- 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 @@ unwrap(attr).cast().getRawData().data()); } +//===----------------------------------------------------------------------===// +// Resource blob attributes. +//===----------------------------------------------------------------------===// + +template +static MlirAttribute getDenseResource(MlirType shapedType, MlirStringRef name, + intptr_t numElements, const T *elements) { + return wrap(U::get(unwrap(shapedType).cast(), 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(shapedType, name, + numElements, elements); +} +MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseUInt8ResourceElementsAttrGet( + MlirType shapedType, MlirStringRef name, intptr_t numElements, + const uint8_t *elements) { + return getDenseResource(shapedType, name, + numElements, elements); +} +MLIR_CAPI_EXPORTED MlirAttribute +mlirUnmanagedDenseUInt16ResourceElementsAttrGet(MlirType shapedType, + MlirStringRef name, + intptr_t numElements, + const uint16_t *elements) { + return getDenseResource(shapedType, name, + numElements, elements); +} +MLIR_CAPI_EXPORTED MlirAttribute +mlirUnmanagedDenseUInt32ResourceElementsAttrGet(MlirType shapedType, + MlirStringRef name, + intptr_t numElements, + const uint32_t *elements) { + return getDenseResource(shapedType, name, + numElements, elements); +} +MLIR_CAPI_EXPORTED MlirAttribute +mlirUnmanagedDenseUInt64ResourceElementsAttrGet(MlirType shapedType, + MlirStringRef name, + intptr_t numElements, + const uint64_t *elements) { + return getDenseResource(shapedType, name, + numElements, elements); +} +MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseInt8ResourceElementsAttrGet( + MlirType shapedType, MlirStringRef name, intptr_t numElements, + const int8_t *elements) { + return getDenseResource(shapedType, name, + numElements, elements); +} +MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseInt16ResourceElementsAttrGet( + MlirType shapedType, MlirStringRef name, intptr_t numElements, + const int16_t *elements) { + return getDenseResource(shapedType, name, + numElements, elements); +} +MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseInt32ResourceElementsAttrGet( + MlirType shapedType, MlirStringRef name, intptr_t numElements, + const int32_t *elements) { + return getDenseResource(shapedType, name, + numElements, elements); +} +MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseInt64ResourceElementsAttrGet( + MlirType shapedType, MlirStringRef name, intptr_t numElements, + const int64_t *elements) { + return getDenseResource(shapedType, name, + numElements, elements); +} +MLIR_CAPI_EXPORTED MlirAttribute mlirUnmanagedDenseFloatResourceElementsAttrGet( + MlirType shapedType, MlirStringRef name, intptr_t numElements, + const float *elements) { + return getDenseResource(shapedType, name, + numElements, elements); +} +MLIR_CAPI_EXPORTED MlirAttribute +mlirUnmanagedDenseDoubleResourceElementsAttrGet(MlirType shapedType, + MlirStringRef name, + intptr_t numElements, + const double *elements) { + return getDenseResource(shapedType, name, + numElements, elements); +} + +template +static T getDenseResourceVal(MlirAttribute attr, intptr_t pos) { + return (*unwrap(attr).cast().tryGetAsArrayRef())[pos]; +} + +MLIR_CAPI_EXPORTED bool +mlirDenseBoolResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) { + return getDenseResourceVal(attr, pos); +} +MLIR_CAPI_EXPORTED uint8_t +mlirDenseUInt8ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) { + return getDenseResourceVal(attr, pos); +} +MLIR_CAPI_EXPORTED uint16_t +mlirDenseUInt16ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) { + return getDenseResourceVal(attr, + pos); +} +MLIR_CAPI_EXPORTED uint32_t +mlirDenseUInt32ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) { + return getDenseResourceVal(attr, + pos); +} +MLIR_CAPI_EXPORTED uint64_t +mlirDenseUInt64ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) { + return getDenseResourceVal(attr, + pos); +} +MLIR_CAPI_EXPORTED int8_t +mlirDenseInt8ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) { + return getDenseResourceVal(attr, pos); +} +MLIR_CAPI_EXPORTED int16_t +mlirDenseInt16ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) { + return getDenseResourceVal(attr, pos); +} +MLIR_CAPI_EXPORTED int32_t +mlirDenseInt32ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) { + return getDenseResourceVal(attr, pos); +} +MLIR_CAPI_EXPORTED int64_t +mlirDenseInt64ResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) { + return getDenseResourceVal(attr, pos); +} +MLIR_CAPI_EXPORTED float +mlirDenseFloatResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) { + return getDenseResourceVal(attr, pos); +} +MLIR_CAPI_EXPORTED double +mlirDenseDoubleResourceElementsAttrGetValue(MlirAttribute attr, intptr_t pos) { + return getDenseResourceVal(attr, pos); +} + //===----------------------------------------------------------------------===// // Sparse elements attribute. //===----------------------------------------------------------------------===// diff --git a/mlir/test/CAPI/ir.c b/mlir/test/CAPI/ir.c --- a/mlir/test/CAPI/ir.c +++ b/mlir/test/CAPI/ir.c @@ -1234,6 +1234,77 @@ 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 : tensor<1x2xui8> + // CHECK: dense_resource : tensor<1x2xui16> + // CHECK: dense_resource : tensor<1x2xui32> + // CHECK: dense_resource : tensor<1x2xui64> + // CHECK: dense_resource : tensor<1x2xi8> + // CHECK: dense_resource : tensor<1x2xi16> + // CHECK: dense_resource : tensor<1x2xi32> + // CHECK: dense_resource : tensor<1x2xi64> + // CHECK: dense_resource : tensor<1x2xf32> + // CHECK: dense_resource : 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; }