| 1 | //===- BuiltinAttributes.cpp - C Interface to MLIR Builtin Attributes -----===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "mlir-c/BuiltinAttributes.h" |
| 10 | #include "mlir-c/Support.h" |
| 11 | #include "mlir/CAPI/AffineMap.h" |
| 12 | #include "mlir/CAPI/IR.h" |
| 13 | #include "mlir/CAPI/IntegerSet.h" |
| 14 | #include "mlir/CAPI/Support.h" |
| 15 | #include "mlir/IR/AsmState.h" |
| 16 | #include "mlir/IR/Attributes.h" |
| 17 | #include "mlir/IR/BuiltinAttributes.h" |
| 18 | #include "mlir/IR/BuiltinTypes.h" |
| 19 | |
| 20 | using namespace mlir; |
| 21 | |
| 22 | MlirAttribute mlirAttributeGetNull() { return {.ptr: nullptr}; } |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // Location attribute. |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | bool mlirAttributeIsALocation(MlirAttribute attr) { |
| 29 | return llvm::isa<LocationAttr>(Val: unwrap(c: attr)); |
| 30 | } |
| 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | // Affine map attribute. |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | bool mlirAttributeIsAAffineMap(MlirAttribute attr) { |
| 37 | return llvm::isa<AffineMapAttr>(Val: unwrap(c: attr)); |
| 38 | } |
| 39 | |
| 40 | MlirAttribute mlirAffineMapAttrGet(MlirAffineMap map) { |
| 41 | return wrap(cpp: AffineMapAttr::get(value: unwrap(c: map))); |
| 42 | } |
| 43 | |
| 44 | MlirStringRef mlirAffineMapAttrGetName(void) { |
| 45 | return wrap(ref: AffineMapAttr::name); |
| 46 | } |
| 47 | |
| 48 | MlirAffineMap mlirAffineMapAttrGetValue(MlirAttribute attr) { |
| 49 | return wrap(cpp: llvm::cast<AffineMapAttr>(Val: unwrap(c: attr)).getValue()); |
| 50 | } |
| 51 | |
| 52 | MlirTypeID mlirAffineMapAttrGetTypeID(void) { |
| 53 | return wrap(cpp: AffineMapAttr::getTypeID()); |
| 54 | } |
| 55 | |
| 56 | //===----------------------------------------------------------------------===// |
| 57 | // Array attribute. |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | |
| 60 | bool mlirAttributeIsAArray(MlirAttribute attr) { |
| 61 | return llvm::isa<ArrayAttr>(Val: unwrap(c: attr)); |
| 62 | } |
| 63 | |
| 64 | MlirAttribute mlirArrayAttrGet(MlirContext ctx, intptr_t numElements, |
| 65 | MlirAttribute const *elements) { |
| 66 | SmallVector<Attribute, 8> attrs; |
| 67 | return wrap( |
| 68 | cpp: ArrayAttr::get(context: unwrap(c: ctx), value: unwrapList(size: static_cast<size_t>(numElements), |
| 69 | first: elements, storage&: attrs))); |
| 70 | } |
| 71 | |
| 72 | MlirStringRef mlirArrayAttrGetName(void) { return wrap(ref: ArrayAttr::name); } |
| 73 | |
| 74 | intptr_t mlirArrayAttrGetNumElements(MlirAttribute attr) { |
| 75 | return static_cast<intptr_t>(llvm::cast<ArrayAttr>(Val: unwrap(c: attr)).size()); |
| 76 | } |
| 77 | |
| 78 | MlirAttribute mlirArrayAttrGetElement(MlirAttribute attr, intptr_t pos) { |
| 79 | return wrap(cpp: llvm::cast<ArrayAttr>(Val: unwrap(c: attr)).getValue()[pos]); |
| 80 | } |
| 81 | |
| 82 | MlirTypeID mlirArrayAttrGetTypeID(void) { return wrap(cpp: ArrayAttr::getTypeID()); } |
| 83 | |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | // Dictionary attribute. |
| 86 | //===----------------------------------------------------------------------===// |
| 87 | |
| 88 | bool mlirAttributeIsADictionary(MlirAttribute attr) { |
| 89 | return llvm::isa<DictionaryAttr>(Val: unwrap(c: attr)); |
| 90 | } |
| 91 | |
| 92 | MlirAttribute mlirDictionaryAttrGet(MlirContext ctx, intptr_t numElements, |
| 93 | MlirNamedAttribute const *elements) { |
| 94 | SmallVector<NamedAttribute, 8> attributes; |
| 95 | attributes.reserve(N: numElements); |
| 96 | for (intptr_t i = 0; i < numElements; ++i) |
| 97 | attributes.emplace_back(Args: unwrap(c: elements[i].name), |
| 98 | Args: unwrap(c: elements[i].attribute)); |
| 99 | return wrap(cpp: DictionaryAttr::get(context: unwrap(c: ctx), value: attributes)); |
| 100 | } |
| 101 | |
| 102 | MlirStringRef mlirDictionaryAttrGetName(void) { |
| 103 | return wrap(ref: DictionaryAttr::name); |
| 104 | } |
| 105 | |
| 106 | intptr_t mlirDictionaryAttrGetNumElements(MlirAttribute attr) { |
| 107 | return static_cast<intptr_t>(llvm::cast<DictionaryAttr>(Val: unwrap(c: attr)).size()); |
| 108 | } |
| 109 | |
| 110 | MlirNamedAttribute mlirDictionaryAttrGetElement(MlirAttribute attr, |
| 111 | intptr_t pos) { |
| 112 | NamedAttribute attribute = |
| 113 | llvm::cast<DictionaryAttr>(Val: unwrap(c: attr)).getValue()[pos]; |
| 114 | return {.name: wrap(cpp: attribute.getName()), .attribute: wrap(cpp: attribute.getValue())}; |
| 115 | } |
| 116 | |
| 117 | MlirAttribute mlirDictionaryAttrGetElementByName(MlirAttribute attr, |
| 118 | MlirStringRef name) { |
| 119 | return wrap(cpp: llvm::cast<DictionaryAttr>(Val: unwrap(c: attr)).get(name: unwrap(ref: name))); |
| 120 | } |
| 121 | |
| 122 | MlirTypeID mlirDictionaryAttrGetTypeID(void) { |
| 123 | return wrap(cpp: DictionaryAttr::getTypeID()); |
| 124 | } |
| 125 | |
| 126 | //===----------------------------------------------------------------------===// |
| 127 | // Floating point attribute. |
| 128 | //===----------------------------------------------------------------------===// |
| 129 | |
| 130 | bool mlirAttributeIsAFloat(MlirAttribute attr) { |
| 131 | return llvm::isa<FloatAttr>(Val: unwrap(c: attr)); |
| 132 | } |
| 133 | |
| 134 | MlirStringRef mlirFloatAttrGetName(void) { return wrap(ref: FloatAttr::name); } |
| 135 | |
| 136 | MlirAttribute mlirFloatAttrDoubleGet(MlirContext ctx, MlirType type, |
| 137 | double value) { |
| 138 | return wrap(cpp: FloatAttr::get(type: unwrap(c: type), value)); |
| 139 | } |
| 140 | |
| 141 | MlirAttribute mlirFloatAttrDoubleGetChecked(MlirLocation loc, MlirType type, |
| 142 | double value) { |
| 143 | return wrap(cpp: FloatAttr::getChecked(loc: unwrap(c: loc), args: unwrap(c: type), args&: value)); |
| 144 | } |
| 145 | |
| 146 | double mlirFloatAttrGetValueDouble(MlirAttribute attr) { |
| 147 | return llvm::cast<FloatAttr>(Val: unwrap(c: attr)).getValueAsDouble(); |
| 148 | } |
| 149 | |
| 150 | MlirTypeID mlirFloatAttrGetTypeID(void) { return wrap(cpp: FloatAttr::getTypeID()); } |
| 151 | |
| 152 | //===----------------------------------------------------------------------===// |
| 153 | // Integer attribute. |
| 154 | //===----------------------------------------------------------------------===// |
| 155 | |
| 156 | bool mlirAttributeIsAInteger(MlirAttribute attr) { |
| 157 | return llvm::isa<IntegerAttr>(Val: unwrap(c: attr)); |
| 158 | } |
| 159 | |
| 160 | MlirAttribute mlirIntegerAttrGet(MlirType type, int64_t value) { |
| 161 | return wrap(cpp: IntegerAttr::get(type: unwrap(c: type), value)); |
| 162 | } |
| 163 | |
| 164 | MlirStringRef mlirIntegerAttrGetName(void) { return wrap(ref: IntegerAttr::name); } |
| 165 | |
| 166 | int64_t mlirIntegerAttrGetValueInt(MlirAttribute attr) { |
| 167 | return llvm::cast<IntegerAttr>(Val: unwrap(c: attr)).getInt(); |
| 168 | } |
| 169 | |
| 170 | int64_t mlirIntegerAttrGetValueSInt(MlirAttribute attr) { |
| 171 | return llvm::cast<IntegerAttr>(Val: unwrap(c: attr)).getSInt(); |
| 172 | } |
| 173 | |
| 174 | uint64_t mlirIntegerAttrGetValueUInt(MlirAttribute attr) { |
| 175 | return llvm::cast<IntegerAttr>(Val: unwrap(c: attr)).getUInt(); |
| 176 | } |
| 177 | |
| 178 | unsigned mlirIntegerAttrGetValueBitWidth(MlirAttribute attr) { |
| 179 | return llvm::cast<IntegerAttr>(Val: unwrap(c: attr)).getValue().getBitWidth(); |
| 180 | } |
| 181 | |
| 182 | unsigned mlirIntegerAttrGetValueNumWords(MlirAttribute attr) { |
| 183 | return llvm::cast<IntegerAttr>(Val: unwrap(c: attr)).getValue().getNumWords(); |
| 184 | } |
| 185 | |
| 186 | void mlirIntegerAttrGetValueWords(MlirAttribute attr, uint64_t *words) { |
| 187 | const APInt &value = llvm::cast<IntegerAttr>(Val: unwrap(c: attr)).getValue(); |
| 188 | unsigned numWords = value.getNumWords(); |
| 189 | const uint64_t *rawData = value.getRawData(); |
| 190 | std::copy(first: rawData, last: rawData + numWords, result: words); |
| 191 | } |
| 192 | |
| 193 | MlirAttribute mlirIntegerAttrGetFromWords(MlirType type, unsigned numWords, |
| 194 | const uint64_t *words) { |
| 195 | Type mlirType = unwrap(c: type); |
| 196 | unsigned bitWidth = mlirType.getIntOrFloatBitWidth(); |
| 197 | APInt value(bitWidth, ArrayRef<uint64_t>(words, numWords)); |
| 198 | return wrap(cpp: IntegerAttr::get(type: mlirType, value)); |
| 199 | } |
| 200 | |
| 201 | MlirTypeID mlirIntegerAttrGetTypeID(void) { |
| 202 | return wrap(cpp: IntegerAttr::getTypeID()); |
| 203 | } |
| 204 | |
| 205 | //===----------------------------------------------------------------------===// |
| 206 | // Bool attribute. |
| 207 | //===----------------------------------------------------------------------===// |
| 208 | |
| 209 | bool mlirAttributeIsABool(MlirAttribute attr) { |
| 210 | return llvm::isa<BoolAttr>(Val: unwrap(c: attr)); |
| 211 | } |
| 212 | |
| 213 | MlirAttribute mlirBoolAttrGet(MlirContext ctx, int value) { |
| 214 | return wrap(cpp: BoolAttr::get(context: unwrap(c: ctx), value)); |
| 215 | } |
| 216 | |
| 217 | bool mlirBoolAttrGetValue(MlirAttribute attr) { |
| 218 | return llvm::cast<BoolAttr>(Val: unwrap(c: attr)).getValue(); |
| 219 | } |
| 220 | |
| 221 | //===----------------------------------------------------------------------===// |
| 222 | // Integer set attribute. |
| 223 | //===----------------------------------------------------------------------===// |
| 224 | |
| 225 | bool mlirAttributeIsAIntegerSet(MlirAttribute attr) { |
| 226 | return llvm::isa<IntegerSetAttr>(Val: unwrap(c: attr)); |
| 227 | } |
| 228 | |
| 229 | MlirTypeID mlirIntegerSetAttrGetTypeID(void) { |
| 230 | return wrap(cpp: IntegerSetAttr::getTypeID()); |
| 231 | } |
| 232 | |
| 233 | MlirAttribute mlirIntegerSetAttrGet(MlirIntegerSet set) { |
| 234 | return wrap(cpp: IntegerSetAttr::get(value: unwrap(c: set))); |
| 235 | } |
| 236 | |
| 237 | MlirStringRef mlirIntegerSetAttrGetName(void) { |
| 238 | return wrap(ref: IntegerSetAttr::name); |
| 239 | } |
| 240 | |
| 241 | MlirIntegerSet mlirIntegerSetAttrGetValue(MlirAttribute attr) { |
| 242 | return wrap(cpp: llvm::cast<IntegerSetAttr>(Val: unwrap(c: attr)).getValue()); |
| 243 | } |
| 244 | |
| 245 | //===----------------------------------------------------------------------===// |
| 246 | // Opaque attribute. |
| 247 | //===----------------------------------------------------------------------===// |
| 248 | |
| 249 | bool mlirAttributeIsAOpaque(MlirAttribute attr) { |
| 250 | return llvm::isa<OpaqueAttr>(Val: unwrap(c: attr)); |
| 251 | } |
| 252 | |
| 253 | MlirAttribute mlirOpaqueAttrGet(MlirContext ctx, MlirStringRef dialectNamespace, |
| 254 | intptr_t dataLength, const char *data, |
| 255 | MlirType type) { |
| 256 | return wrap( |
| 257 | cpp: OpaqueAttr::get(dialect: StringAttr::get(context: unwrap(c: ctx), bytes: unwrap(ref: dialectNamespace)), |
| 258 | attrData: StringRef(data, dataLength), type: unwrap(c: type))); |
| 259 | } |
| 260 | |
| 261 | MlirStringRef mlirOpaqueAttrGetName(void) { return wrap(ref: OpaqueAttr::name); } |
| 262 | |
| 263 | MlirStringRef mlirOpaqueAttrGetDialectNamespace(MlirAttribute attr) { |
| 264 | return wrap( |
| 265 | ref: llvm::cast<OpaqueAttr>(Val: unwrap(c: attr)).getDialectNamespace().strref()); |
| 266 | } |
| 267 | |
| 268 | MlirStringRef mlirOpaqueAttrGetData(MlirAttribute attr) { |
| 269 | return wrap(ref: llvm::cast<OpaqueAttr>(Val: unwrap(c: attr)).getAttrData()); |
| 270 | } |
| 271 | |
| 272 | MlirTypeID mlirOpaqueAttrGetTypeID(void) { |
| 273 | return wrap(cpp: OpaqueAttr::getTypeID()); |
| 274 | } |
| 275 | |
| 276 | //===----------------------------------------------------------------------===// |
| 277 | // String attribute. |
| 278 | //===----------------------------------------------------------------------===// |
| 279 | |
| 280 | bool mlirAttributeIsAString(MlirAttribute attr) { |
| 281 | return llvm::isa<StringAttr>(Val: unwrap(c: attr)); |
| 282 | } |
| 283 | |
| 284 | MlirAttribute mlirStringAttrGet(MlirContext ctx, MlirStringRef str) { |
| 285 | return wrap(cpp: (Attribute)StringAttr::get(context: unwrap(c: ctx), bytes: unwrap(ref: str))); |
| 286 | } |
| 287 | |
| 288 | MlirStringRef mlirStringAttrGetName(void) { return wrap(ref: StringAttr::name); } |
| 289 | |
| 290 | MlirAttribute mlirStringAttrTypedGet(MlirType type, MlirStringRef str) { |
| 291 | return wrap(cpp: (Attribute)StringAttr::get(bytes: unwrap(ref: str), type: unwrap(c: type))); |
| 292 | } |
| 293 | |
| 294 | MlirStringRef mlirStringAttrGetValue(MlirAttribute attr) { |
| 295 | return wrap(ref: llvm::cast<StringAttr>(Val: unwrap(c: attr)).getValue()); |
| 296 | } |
| 297 | |
| 298 | MlirTypeID mlirStringAttrGetTypeID(void) { |
| 299 | return wrap(cpp: StringAttr::getTypeID()); |
| 300 | } |
| 301 | |
| 302 | //===----------------------------------------------------------------------===// |
| 303 | // SymbolRef attribute. |
| 304 | //===----------------------------------------------------------------------===// |
| 305 | |
| 306 | bool mlirAttributeIsASymbolRef(MlirAttribute attr) { |
| 307 | return llvm::isa<SymbolRefAttr>(Val: unwrap(c: attr)); |
| 308 | } |
| 309 | |
| 310 | MlirAttribute mlirSymbolRefAttrGet(MlirContext ctx, MlirStringRef symbol, |
| 311 | intptr_t numReferences, |
| 312 | MlirAttribute const *references) { |
| 313 | SmallVector<FlatSymbolRefAttr, 4> refs; |
| 314 | refs.reserve(N: numReferences); |
| 315 | for (intptr_t i = 0; i < numReferences; ++i) |
| 316 | refs.push_back(Elt: llvm::cast<FlatSymbolRefAttr>(Val: unwrap(c: references[i]))); |
| 317 | auto symbolAttr = StringAttr::get(context: unwrap(c: ctx), bytes: unwrap(ref: symbol)); |
| 318 | return wrap(cpp: SymbolRefAttr::get(rootReference: symbolAttr, nestedReferences: refs)); |
| 319 | } |
| 320 | |
| 321 | MlirStringRef mlirSymbolRefAttrGetName(void) { |
| 322 | return wrap(ref: SymbolRefAttr::name); |
| 323 | } |
| 324 | |
| 325 | MlirStringRef mlirSymbolRefAttrGetRootReference(MlirAttribute attr) { |
| 326 | return wrap( |
| 327 | ref: llvm::cast<SymbolRefAttr>(Val: unwrap(c: attr)).getRootReference().getValue()); |
| 328 | } |
| 329 | |
| 330 | MlirStringRef mlirSymbolRefAttrGetLeafReference(MlirAttribute attr) { |
| 331 | return wrap( |
| 332 | ref: llvm::cast<SymbolRefAttr>(Val: unwrap(c: attr)).getLeafReference().getValue()); |
| 333 | } |
| 334 | |
| 335 | intptr_t mlirSymbolRefAttrGetNumNestedReferences(MlirAttribute attr) { |
| 336 | return static_cast<intptr_t>( |
| 337 | llvm::cast<SymbolRefAttr>(Val: unwrap(c: attr)).getNestedReferences().size()); |
| 338 | } |
| 339 | |
| 340 | MlirAttribute mlirSymbolRefAttrGetNestedReference(MlirAttribute attr, |
| 341 | intptr_t pos) { |
| 342 | return wrap( |
| 343 | cpp: llvm::cast<SymbolRefAttr>(Val: unwrap(c: attr)).getNestedReferences()[pos]); |
| 344 | } |
| 345 | |
| 346 | MlirTypeID mlirSymbolRefAttrGetTypeID(void) { |
| 347 | return wrap(cpp: SymbolRefAttr::getTypeID()); |
| 348 | } |
| 349 | |
| 350 | MlirAttribute mlirDistinctAttrCreate(MlirAttribute referencedAttr) { |
| 351 | return wrap(cpp: mlir::DistinctAttr::create(referencedAttr: unwrap(c: referencedAttr))); |
| 352 | } |
| 353 | |
| 354 | //===----------------------------------------------------------------------===// |
| 355 | // Flat SymbolRef attribute. |
| 356 | //===----------------------------------------------------------------------===// |
| 357 | |
| 358 | bool mlirAttributeIsAFlatSymbolRef(MlirAttribute attr) { |
| 359 | return llvm::isa<FlatSymbolRefAttr>(Val: unwrap(c: attr)); |
| 360 | } |
| 361 | |
| 362 | MlirAttribute mlirFlatSymbolRefAttrGet(MlirContext ctx, MlirStringRef symbol) { |
| 363 | return wrap(cpp: FlatSymbolRefAttr::get(ctx: unwrap(c: ctx), value: unwrap(ref: symbol))); |
| 364 | } |
| 365 | |
| 366 | MlirStringRef mlirFlatSymbolRefAttrGetName(void) { |
| 367 | return wrap(ref: FlatSymbolRefAttr::name); |
| 368 | } |
| 369 | |
| 370 | MlirStringRef mlirFlatSymbolRefAttrGetValue(MlirAttribute attr) { |
| 371 | return wrap(ref: llvm::cast<FlatSymbolRefAttr>(Val: unwrap(c: attr)).getValue()); |
| 372 | } |
| 373 | |
| 374 | //===----------------------------------------------------------------------===// |
| 375 | // Type attribute. |
| 376 | //===----------------------------------------------------------------------===// |
| 377 | |
| 378 | bool mlirAttributeIsAType(MlirAttribute attr) { |
| 379 | return llvm::isa<TypeAttr>(Val: unwrap(c: attr)); |
| 380 | } |
| 381 | |
| 382 | MlirAttribute mlirTypeAttrGet(MlirType type) { |
| 383 | return wrap(cpp: TypeAttr::get(type: unwrap(c: type))); |
| 384 | } |
| 385 | |
| 386 | MlirStringRef mlirTypeAttrGetName(void) { return wrap(ref: TypeAttr::name); } |
| 387 | |
| 388 | MlirType mlirTypeAttrGetValue(MlirAttribute attr) { |
| 389 | return wrap(cpp: llvm::cast<TypeAttr>(Val: unwrap(c: attr)).getValue()); |
| 390 | } |
| 391 | |
| 392 | MlirTypeID mlirTypeAttrGetTypeID(void) { return wrap(cpp: TypeAttr::getTypeID()); } |
| 393 | |
| 394 | //===----------------------------------------------------------------------===// |
| 395 | // Unit attribute. |
| 396 | //===----------------------------------------------------------------------===// |
| 397 | |
| 398 | bool mlirAttributeIsAUnit(MlirAttribute attr) { |
| 399 | return llvm::isa<UnitAttr>(Val: unwrap(c: attr)); |
| 400 | } |
| 401 | |
| 402 | MlirAttribute mlirUnitAttrGet(MlirContext ctx) { |
| 403 | return wrap(cpp: UnitAttr::get(context: unwrap(c: ctx))); |
| 404 | } |
| 405 | |
| 406 | MlirStringRef mlirUnitAttrGetName(void) { return wrap(ref: UnitAttr::name); } |
| 407 | |
| 408 | MlirTypeID mlirUnitAttrGetTypeID(void) { return wrap(cpp: UnitAttr::getTypeID()); } |
| 409 | |
| 410 | //===----------------------------------------------------------------------===// |
| 411 | // Elements attributes. |
| 412 | //===----------------------------------------------------------------------===// |
| 413 | |
| 414 | bool mlirAttributeIsAElements(MlirAttribute attr) { |
| 415 | return llvm::isa<ElementsAttr>(Val: unwrap(c: attr)); |
| 416 | } |
| 417 | |
| 418 | MlirAttribute mlirElementsAttrGetValue(MlirAttribute attr, intptr_t rank, |
| 419 | uint64_t *idxs) { |
| 420 | return wrap(cpp: llvm::cast<ElementsAttr>(Val: unwrap(c: attr)) |
| 421 | .getValues<Attribute>()[llvm::ArrayRef(idxs, rank)]); |
| 422 | } |
| 423 | |
| 424 | bool mlirElementsAttrIsValidIndex(MlirAttribute attr, intptr_t rank, |
| 425 | uint64_t *idxs) { |
| 426 | return llvm::cast<ElementsAttr>(Val: unwrap(c: attr)) |
| 427 | .isValidIndex(index: llvm::ArrayRef(idxs, rank)); |
| 428 | } |
| 429 | |
| 430 | int64_t mlirElementsAttrGetNumElements(MlirAttribute attr) { |
| 431 | return llvm::cast<ElementsAttr>(Val: unwrap(c: attr)).getNumElements(); |
| 432 | } |
| 433 | |
| 434 | //===----------------------------------------------------------------------===// |
| 435 | // Dense array attribute. |
| 436 | //===----------------------------------------------------------------------===// |
| 437 | |
| 438 | MlirTypeID mlirDenseArrayAttrGetTypeID() { |
| 439 | return wrap(cpp: DenseArrayAttr::getTypeID()); |
| 440 | } |
| 441 | |
| 442 | //===----------------------------------------------------------------------===// |
| 443 | // IsA support. |
| 444 | //===----------------------------------------------------------------------===// |
| 445 | |
| 446 | bool mlirAttributeIsADenseBoolArray(MlirAttribute attr) { |
| 447 | return llvm::isa<DenseBoolArrayAttr>(Val: unwrap(c: attr)); |
| 448 | } |
| 449 | bool mlirAttributeIsADenseI8Array(MlirAttribute attr) { |
| 450 | return llvm::isa<DenseI8ArrayAttr>(Val: unwrap(c: attr)); |
| 451 | } |
| 452 | bool mlirAttributeIsADenseI16Array(MlirAttribute attr) { |
| 453 | return llvm::isa<DenseI16ArrayAttr>(Val: unwrap(c: attr)); |
| 454 | } |
| 455 | bool mlirAttributeIsADenseI32Array(MlirAttribute attr) { |
| 456 | return llvm::isa<DenseI32ArrayAttr>(Val: unwrap(c: attr)); |
| 457 | } |
| 458 | bool mlirAttributeIsADenseI64Array(MlirAttribute attr) { |
| 459 | return llvm::isa<DenseI64ArrayAttr>(Val: unwrap(c: attr)); |
| 460 | } |
| 461 | bool mlirAttributeIsADenseF32Array(MlirAttribute attr) { |
| 462 | return llvm::isa<DenseF32ArrayAttr>(Val: unwrap(c: attr)); |
| 463 | } |
| 464 | bool mlirAttributeIsADenseF64Array(MlirAttribute attr) { |
| 465 | return llvm::isa<DenseF64ArrayAttr>(Val: unwrap(c: attr)); |
| 466 | } |
| 467 | |
| 468 | //===----------------------------------------------------------------------===// |
| 469 | // Constructors. |
| 470 | //===----------------------------------------------------------------------===// |
| 471 | |
| 472 | MlirAttribute mlirDenseBoolArrayGet(MlirContext ctx, intptr_t size, |
| 473 | int const *values) { |
| 474 | SmallVector<bool, 4> elements(values, values + size); |
| 475 | return wrap(cpp: DenseBoolArrayAttr::get(context: unwrap(c: ctx), content: elements)); |
| 476 | } |
| 477 | MlirAttribute mlirDenseI8ArrayGet(MlirContext ctx, intptr_t size, |
| 478 | int8_t const *values) { |
| 479 | return wrap( |
| 480 | cpp: DenseI8ArrayAttr::get(context: unwrap(c: ctx), content: ArrayRef<int8_t>(values, size))); |
| 481 | } |
| 482 | MlirAttribute mlirDenseI16ArrayGet(MlirContext ctx, intptr_t size, |
| 483 | int16_t const *values) { |
| 484 | return wrap( |
| 485 | cpp: DenseI16ArrayAttr::get(context: unwrap(c: ctx), content: ArrayRef<int16_t>(values, size))); |
| 486 | } |
| 487 | MlirAttribute mlirDenseI32ArrayGet(MlirContext ctx, intptr_t size, |
| 488 | int32_t const *values) { |
| 489 | return wrap( |
| 490 | cpp: DenseI32ArrayAttr::get(context: unwrap(c: ctx), content: ArrayRef<int32_t>(values, size))); |
| 491 | } |
| 492 | MlirAttribute mlirDenseI64ArrayGet(MlirContext ctx, intptr_t size, |
| 493 | int64_t const *values) { |
| 494 | return wrap( |
| 495 | cpp: DenseI64ArrayAttr::get(context: unwrap(c: ctx), content: ArrayRef<int64_t>(values, size))); |
| 496 | } |
| 497 | MlirAttribute mlirDenseF32ArrayGet(MlirContext ctx, intptr_t size, |
| 498 | float const *values) { |
| 499 | return wrap( |
| 500 | cpp: DenseF32ArrayAttr::get(context: unwrap(c: ctx), content: ArrayRef<float>(values, size))); |
| 501 | } |
| 502 | MlirAttribute mlirDenseF64ArrayGet(MlirContext ctx, intptr_t size, |
| 503 | double const *values) { |
| 504 | return wrap( |
| 505 | cpp: DenseF64ArrayAttr::get(context: unwrap(c: ctx), content: ArrayRef<double>(values, size))); |
| 506 | } |
| 507 | |
| 508 | //===----------------------------------------------------------------------===// |
| 509 | // Accessors. |
| 510 | //===----------------------------------------------------------------------===// |
| 511 | |
| 512 | intptr_t mlirDenseArrayGetNumElements(MlirAttribute attr) { |
| 513 | return llvm::cast<DenseArrayAttr>(Val: unwrap(c: attr)).size(); |
| 514 | } |
| 515 | |
| 516 | //===----------------------------------------------------------------------===// |
| 517 | // Indexed accessors. |
| 518 | //===----------------------------------------------------------------------===// |
| 519 | |
| 520 | bool mlirDenseBoolArrayGetElement(MlirAttribute attr, intptr_t pos) { |
| 521 | return llvm::cast<DenseBoolArrayAttr>(Val: unwrap(c: attr))[pos]; |
| 522 | } |
| 523 | int8_t mlirDenseI8ArrayGetElement(MlirAttribute attr, intptr_t pos) { |
| 524 | return llvm::cast<DenseI8ArrayAttr>(Val: unwrap(c: attr))[pos]; |
| 525 | } |
| 526 | int16_t mlirDenseI16ArrayGetElement(MlirAttribute attr, intptr_t pos) { |
| 527 | return llvm::cast<DenseI16ArrayAttr>(Val: unwrap(c: attr))[pos]; |
| 528 | } |
| 529 | int32_t mlirDenseI32ArrayGetElement(MlirAttribute attr, intptr_t pos) { |
| 530 | return llvm::cast<DenseI32ArrayAttr>(Val: unwrap(c: attr))[pos]; |
| 531 | } |
| 532 | int64_t mlirDenseI64ArrayGetElement(MlirAttribute attr, intptr_t pos) { |
| 533 | return llvm::cast<DenseI64ArrayAttr>(Val: unwrap(c: attr))[pos]; |
| 534 | } |
| 535 | float mlirDenseF32ArrayGetElement(MlirAttribute attr, intptr_t pos) { |
| 536 | return llvm::cast<DenseF32ArrayAttr>(Val: unwrap(c: attr))[pos]; |
| 537 | } |
| 538 | double mlirDenseF64ArrayGetElement(MlirAttribute attr, intptr_t pos) { |
| 539 | return llvm::cast<DenseF64ArrayAttr>(Val: unwrap(c: attr))[pos]; |
| 540 | } |
| 541 | |
| 542 | //===----------------------------------------------------------------------===// |
| 543 | // Dense elements attribute. |
| 544 | //===----------------------------------------------------------------------===// |
| 545 | |
| 546 | //===----------------------------------------------------------------------===// |
| 547 | // IsA support. |
| 548 | //===----------------------------------------------------------------------===// |
| 549 | |
| 550 | bool mlirAttributeIsADenseElements(MlirAttribute attr) { |
| 551 | return llvm::isa<DenseElementsAttr>(Val: unwrap(c: attr)); |
| 552 | } |
| 553 | |
| 554 | bool mlirAttributeIsADenseIntElements(MlirAttribute attr) { |
| 555 | return llvm::isa<DenseIntElementsAttr>(Val: unwrap(c: attr)); |
| 556 | } |
| 557 | |
| 558 | bool mlirAttributeIsADenseFPElements(MlirAttribute attr) { |
| 559 | return llvm::isa<DenseFPElementsAttr>(Val: unwrap(c: attr)); |
| 560 | } |
| 561 | |
| 562 | MlirTypeID mlirDenseTypedElementsAttrGetTypeID(void) { |
| 563 | return wrap(cpp: DenseTypedElementsAttr::getTypeID()); |
| 564 | } |
| 565 | |
| 566 | // Deprecated API. Will be removed in the future. |
| 567 | MlirTypeID mlirDenseIntOrFPElementsAttrGetTypeID(void) { |
| 568 | return mlirDenseTypedElementsAttrGetTypeID(); |
| 569 | } |
| 570 | |
| 571 | //===----------------------------------------------------------------------===// |
| 572 | // Constructors. |
| 573 | //===----------------------------------------------------------------------===// |
| 574 | |
| 575 | MlirAttribute mlirDenseElementsAttrGet(MlirType shapedType, |
| 576 | intptr_t numElements, |
| 577 | MlirAttribute const *elements) { |
| 578 | SmallVector<Attribute, 8> attributes; |
| 579 | return wrap( |
| 580 | cpp: DenseElementsAttr::get(type: llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), |
| 581 | values: unwrapList(size: numElements, first: elements, storage&: attributes))); |
| 582 | } |
| 583 | |
| 584 | MlirAttribute mlirDenseElementsAttrRawBufferGet(MlirType shapedType, |
| 585 | size_t rawBufferSize, |
| 586 | const void *rawBuffer) { |
| 587 | auto shapedTypeCpp = llvm::cast<ShapedType>(Val: unwrap(c: shapedType)); |
| 588 | ArrayRef<char> rawBufferCpp(static_cast<const char *>(rawBuffer), |
| 589 | rawBufferSize); |
| 590 | if (!DenseElementsAttr::isValidRawBuffer(type: shapedTypeCpp, rawBuffer: rawBufferCpp)) |
| 591 | return mlirAttributeGetNull(); |
| 592 | return wrap(cpp: DenseElementsAttr::getFromRawBuffer(type: shapedTypeCpp, rawBuffer: rawBufferCpp)); |
| 593 | } |
| 594 | |
| 595 | MlirAttribute mlirDenseElementsAttrSplatGet(MlirType shapedType, |
| 596 | MlirAttribute element) { |
| 597 | return wrap(cpp: DenseElementsAttr::get(type: llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), |
| 598 | values: unwrap(c: element))); |
| 599 | } |
| 600 | MlirAttribute mlirDenseElementsAttrBoolSplatGet(MlirType shapedType, |
| 601 | bool element) { |
| 602 | return wrap(cpp: DenseElementsAttr::get(type: llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), |
| 603 | value: element)); |
| 604 | } |
| 605 | MlirAttribute mlirDenseElementsAttrUInt8SplatGet(MlirType shapedType, |
| 606 | uint8_t element) { |
| 607 | return wrap(cpp: DenseElementsAttr::get(type: llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), |
| 608 | value: element)); |
| 609 | } |
| 610 | MlirAttribute mlirDenseElementsAttrInt8SplatGet(MlirType shapedType, |
| 611 | int8_t element) { |
| 612 | return wrap(cpp: DenseElementsAttr::get(type: llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), |
| 613 | value: element)); |
| 614 | } |
| 615 | MlirAttribute mlirDenseElementsAttrUInt32SplatGet(MlirType shapedType, |
| 616 | uint32_t element) { |
| 617 | return wrap(cpp: DenseElementsAttr::get(type: llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), |
| 618 | value: element)); |
| 619 | } |
| 620 | MlirAttribute mlirDenseElementsAttrInt32SplatGet(MlirType shapedType, |
| 621 | int32_t element) { |
| 622 | return wrap(cpp: DenseElementsAttr::get(type: llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), |
| 623 | value: element)); |
| 624 | } |
| 625 | MlirAttribute mlirDenseElementsAttrUInt64SplatGet(MlirType shapedType, |
| 626 | uint64_t element) { |
| 627 | return wrap(cpp: DenseElementsAttr::get(type: llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), |
| 628 | value: element)); |
| 629 | } |
| 630 | MlirAttribute mlirDenseElementsAttrInt64SplatGet(MlirType shapedType, |
| 631 | int64_t element) { |
| 632 | return wrap(cpp: DenseElementsAttr::get(type: llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), |
| 633 | value: element)); |
| 634 | } |
| 635 | MlirAttribute mlirDenseElementsAttrFloatSplatGet(MlirType shapedType, |
| 636 | float element) { |
| 637 | return wrap(cpp: DenseElementsAttr::get(type: llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), |
| 638 | value: element)); |
| 639 | } |
| 640 | MlirAttribute mlirDenseElementsAttrDoubleSplatGet(MlirType shapedType, |
| 641 | double element) { |
| 642 | return wrap(cpp: DenseElementsAttr::get(type: llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), |
| 643 | value: element)); |
| 644 | } |
| 645 | |
| 646 | MlirAttribute mlirDenseElementsAttrBoolGet(MlirType shapedType, |
| 647 | intptr_t numElements, |
| 648 | const int *elements) { |
| 649 | SmallVector<bool, 8> values(elements, elements + numElements); |
| 650 | return wrap(cpp: DenseElementsAttr::get(type: llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), |
| 651 | values)); |
| 652 | } |
| 653 | |
| 654 | /// Creates a dense attribute with elements of the type deduced by templates. |
| 655 | template <typename T> |
| 656 | static MlirAttribute getDenseAttribute(MlirType shapedType, |
| 657 | intptr_t numElements, |
| 658 | const T *elements) { |
| 659 | return wrap(DenseElementsAttr::get(llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), |
| 660 | llvm::ArrayRef(elements, numElements))); |
| 661 | } |
| 662 | |
| 663 | MlirAttribute mlirDenseElementsAttrUInt8Get(MlirType shapedType, |
| 664 | intptr_t numElements, |
| 665 | const uint8_t *elements) { |
| 666 | return getDenseAttribute(shapedType, numElements, elements); |
| 667 | } |
| 668 | MlirAttribute mlirDenseElementsAttrInt8Get(MlirType shapedType, |
| 669 | intptr_t numElements, |
| 670 | const int8_t *elements) { |
| 671 | return getDenseAttribute(shapedType, numElements, elements); |
| 672 | } |
| 673 | MlirAttribute mlirDenseElementsAttrUInt16Get(MlirType shapedType, |
| 674 | intptr_t numElements, |
| 675 | const uint16_t *elements) { |
| 676 | return getDenseAttribute(shapedType, numElements, elements); |
| 677 | } |
| 678 | MlirAttribute mlirDenseElementsAttrInt16Get(MlirType shapedType, |
| 679 | intptr_t numElements, |
| 680 | const int16_t *elements) { |
| 681 | return getDenseAttribute(shapedType, numElements, elements); |
| 682 | } |
| 683 | MlirAttribute mlirDenseElementsAttrUInt32Get(MlirType shapedType, |
| 684 | intptr_t numElements, |
| 685 | const uint32_t *elements) { |
| 686 | return getDenseAttribute(shapedType, numElements, elements); |
| 687 | } |
| 688 | MlirAttribute mlirDenseElementsAttrInt32Get(MlirType shapedType, |
| 689 | intptr_t numElements, |
| 690 | const int32_t *elements) { |
| 691 | return getDenseAttribute(shapedType, numElements, elements); |
| 692 | } |
| 693 | MlirAttribute mlirDenseElementsAttrUInt64Get(MlirType shapedType, |
| 694 | intptr_t numElements, |
| 695 | const uint64_t *elements) { |
| 696 | return getDenseAttribute(shapedType, numElements, elements); |
| 697 | } |
| 698 | MlirAttribute mlirDenseElementsAttrInt64Get(MlirType shapedType, |
| 699 | intptr_t numElements, |
| 700 | const int64_t *elements) { |
| 701 | return getDenseAttribute(shapedType, numElements, elements); |
| 702 | } |
| 703 | MlirAttribute mlirDenseElementsAttrFloatGet(MlirType shapedType, |
| 704 | intptr_t numElements, |
| 705 | const float *elements) { |
| 706 | return getDenseAttribute(shapedType, numElements, elements); |
| 707 | } |
| 708 | MlirAttribute mlirDenseElementsAttrDoubleGet(MlirType shapedType, |
| 709 | intptr_t numElements, |
| 710 | const double *elements) { |
| 711 | return getDenseAttribute(shapedType, numElements, elements); |
| 712 | } |
| 713 | MlirAttribute mlirDenseElementsAttrBFloat16Get(MlirType shapedType, |
| 714 | intptr_t numElements, |
| 715 | const uint16_t *elements) { |
| 716 | size_t bufferSize = numElements * 2; |
| 717 | const void *buffer = static_cast<const void *>(elements); |
| 718 | return mlirDenseElementsAttrRawBufferGet(shapedType, rawBufferSize: bufferSize, rawBuffer: buffer); |
| 719 | } |
| 720 | MlirAttribute mlirDenseElementsAttrFloat16Get(MlirType shapedType, |
| 721 | intptr_t numElements, |
| 722 | const uint16_t *elements) { |
| 723 | size_t bufferSize = numElements * 2; |
| 724 | const void *buffer = static_cast<const void *>(elements); |
| 725 | return mlirDenseElementsAttrRawBufferGet(shapedType, rawBufferSize: bufferSize, rawBuffer: buffer); |
| 726 | } |
| 727 | |
| 728 | MlirAttribute mlirDenseElementsAttrStringGet(MlirType shapedType, |
| 729 | intptr_t numElements, |
| 730 | MlirStringRef *strs) { |
| 731 | SmallVector<StringRef, 8> values; |
| 732 | values.reserve(N: numElements); |
| 733 | for (intptr_t i = 0; i < numElements; ++i) |
| 734 | values.push_back(Elt: unwrap(ref: strs[i])); |
| 735 | |
| 736 | return wrap(cpp: DenseElementsAttr::get(type: llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), |
| 737 | values)); |
| 738 | } |
| 739 | |
| 740 | MlirAttribute mlirDenseElementsAttrReshapeGet(MlirAttribute attr, |
| 741 | MlirType shapedType) { |
| 742 | return wrap(cpp: llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)) |
| 743 | .reshape(newType: llvm::cast<ShapedType>(Val: unwrap(c: shapedType)))); |
| 744 | } |
| 745 | |
| 746 | //===----------------------------------------------------------------------===// |
| 747 | // Splat accessors. |
| 748 | //===----------------------------------------------------------------------===// |
| 749 | |
| 750 | bool mlirDenseElementsAttrIsSplat(MlirAttribute attr) { |
| 751 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).isSplat(); |
| 752 | } |
| 753 | |
| 754 | MlirAttribute mlirDenseElementsAttrGetSplatValue(MlirAttribute attr) { |
| 755 | return wrap( |
| 756 | cpp: llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getSplatValue<Attribute>()); |
| 757 | } |
| 758 | int mlirDenseElementsAttrGetBoolSplatValue(MlirAttribute attr) { |
| 759 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getSplatValue<bool>(); |
| 760 | } |
| 761 | int8_t mlirDenseElementsAttrGetInt8SplatValue(MlirAttribute attr) { |
| 762 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getSplatValue<int8_t>(); |
| 763 | } |
| 764 | uint8_t mlirDenseElementsAttrGetUInt8SplatValue(MlirAttribute attr) { |
| 765 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getSplatValue<uint8_t>(); |
| 766 | } |
| 767 | int32_t mlirDenseElementsAttrGetInt32SplatValue(MlirAttribute attr) { |
| 768 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getSplatValue<int32_t>(); |
| 769 | } |
| 770 | uint32_t mlirDenseElementsAttrGetUInt32SplatValue(MlirAttribute attr) { |
| 771 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getSplatValue<uint32_t>(); |
| 772 | } |
| 773 | int64_t mlirDenseElementsAttrGetInt64SplatValue(MlirAttribute attr) { |
| 774 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getSplatValue<int64_t>(); |
| 775 | } |
| 776 | uint64_t mlirDenseElementsAttrGetUInt64SplatValue(MlirAttribute attr) { |
| 777 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getSplatValue<uint64_t>(); |
| 778 | } |
| 779 | float mlirDenseElementsAttrGetFloatSplatValue(MlirAttribute attr) { |
| 780 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getSplatValue<float>(); |
| 781 | } |
| 782 | double mlirDenseElementsAttrGetDoubleSplatValue(MlirAttribute attr) { |
| 783 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getSplatValue<double>(); |
| 784 | } |
| 785 | MlirStringRef mlirDenseElementsAttrGetStringSplatValue(MlirAttribute attr) { |
| 786 | return wrap( |
| 787 | ref: llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getSplatValue<StringRef>()); |
| 788 | } |
| 789 | |
| 790 | //===----------------------------------------------------------------------===// |
| 791 | // Indexed accessors. |
| 792 | //===----------------------------------------------------------------------===// |
| 793 | |
| 794 | bool mlirDenseElementsAttrGetBoolValue(MlirAttribute attr, intptr_t pos) { |
| 795 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getValues<bool>()[pos]; |
| 796 | } |
| 797 | int8_t mlirDenseElementsAttrGetInt8Value(MlirAttribute attr, intptr_t pos) { |
| 798 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getValues<int8_t>()[pos]; |
| 799 | } |
| 800 | uint8_t mlirDenseElementsAttrGetUInt8Value(MlirAttribute attr, intptr_t pos) { |
| 801 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getValues<uint8_t>()[pos]; |
| 802 | } |
| 803 | int16_t mlirDenseElementsAttrGetInt16Value(MlirAttribute attr, intptr_t pos) { |
| 804 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getValues<int16_t>()[pos]; |
| 805 | } |
| 806 | uint16_t mlirDenseElementsAttrGetUInt16Value(MlirAttribute attr, intptr_t pos) { |
| 807 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getValues<uint16_t>()[pos]; |
| 808 | } |
| 809 | int32_t mlirDenseElementsAttrGetInt32Value(MlirAttribute attr, intptr_t pos) { |
| 810 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getValues<int32_t>()[pos]; |
| 811 | } |
| 812 | uint32_t mlirDenseElementsAttrGetUInt32Value(MlirAttribute attr, intptr_t pos) { |
| 813 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getValues<uint32_t>()[pos]; |
| 814 | } |
| 815 | int64_t mlirDenseElementsAttrGetInt64Value(MlirAttribute attr, intptr_t pos) { |
| 816 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getValues<int64_t>()[pos]; |
| 817 | } |
| 818 | uint64_t mlirDenseElementsAttrGetUInt64Value(MlirAttribute attr, intptr_t pos) { |
| 819 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getValues<uint64_t>()[pos]; |
| 820 | } |
| 821 | uint64_t mlirDenseElementsAttrGetIndexValue(MlirAttribute attr, intptr_t pos) { |
| 822 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getValues<uint64_t>()[pos]; |
| 823 | } |
| 824 | float mlirDenseElementsAttrGetFloatValue(MlirAttribute attr, intptr_t pos) { |
| 825 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getValues<float>()[pos]; |
| 826 | } |
| 827 | double mlirDenseElementsAttrGetDoubleValue(MlirAttribute attr, intptr_t pos) { |
| 828 | return llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getValues<double>()[pos]; |
| 829 | } |
| 830 | MlirStringRef mlirDenseElementsAttrGetStringValue(MlirAttribute attr, |
| 831 | intptr_t pos) { |
| 832 | return wrap( |
| 833 | ref: llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getValues<StringRef>()[pos]); |
| 834 | } |
| 835 | |
| 836 | //===----------------------------------------------------------------------===// |
| 837 | // Raw data accessors. |
| 838 | //===----------------------------------------------------------------------===// |
| 839 | |
| 840 | const void *mlirDenseElementsAttrGetRawData(MlirAttribute attr) { |
| 841 | return static_cast<const void *>( |
| 842 | llvm::cast<DenseElementsAttr>(Val: unwrap(c: attr)).getRawData().data()); |
| 843 | } |
| 844 | |
| 845 | //===----------------------------------------------------------------------===// |
| 846 | // Resource blob attributes. |
| 847 | //===----------------------------------------------------------------------===// |
| 848 | |
| 849 | bool mlirAttributeIsADenseResourceElements(MlirAttribute attr) { |
| 850 | return llvm::isa<DenseResourceElementsAttr>(Val: unwrap(c: attr)); |
| 851 | } |
| 852 | |
| 853 | MlirAttribute mlirUnmanagedDenseResourceElementsAttrGet( |
| 854 | MlirType shapedType, MlirStringRef name, void *data, size_t dataLength, |
| 855 | size_t dataAlignment, bool dataIsMutable, |
| 856 | void (*deleter)(void *userData, const void *data, size_t size, |
| 857 | size_t align), |
| 858 | void *userData) { |
| 859 | AsmResourceBlob::DeleterFn cppDeleter = {}; |
| 860 | if (deleter) { |
| 861 | cppDeleter = [deleter, userData](void *data, size_t size, size_t align) { |
| 862 | deleter(userData, data, size, align); |
| 863 | }; |
| 864 | } |
| 865 | AsmResourceBlob blob( |
| 866 | llvm::ArrayRef(static_cast<const char *>(data), dataLength), |
| 867 | dataAlignment, std::move(cppDeleter), dataIsMutable); |
| 868 | return wrap( |
| 869 | cpp: DenseResourceElementsAttr::get(type: llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), |
| 870 | blobName: unwrap(ref: name), blob: std::move(blob))); |
| 871 | } |
| 872 | |
| 873 | MlirStringRef mlirDenseResourceElementsAttrGetName(void) { |
| 874 | return wrap(ref: DenseResourceElementsAttr::name); |
| 875 | } |
| 876 | |
| 877 | template <typename U, typename T> |
| 878 | static MlirAttribute getDenseResource(MlirType shapedType, MlirStringRef name, |
| 879 | intptr_t numElements, const T *elements) { |
| 880 | return wrap(U::get(llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), unwrap(ref: name), |
| 881 | UnmanagedAsmResourceBlob::allocateInferAlign( |
| 882 | llvm::ArrayRef(elements, numElements)))); |
| 883 | } |
| 884 | |
| 885 | MlirAttribute mlirUnmanagedDenseBoolResourceElementsAttrGet( |
| 886 | MlirType shapedType, MlirStringRef name, intptr_t numElements, |
| 887 | const int *elements) { |
| 888 | return getDenseResource<DenseBoolResourceElementsAttr>(shapedType, name, |
| 889 | numElements, elements); |
| 890 | } |
| 891 | MlirAttribute mlirUnmanagedDenseUInt8ResourceElementsAttrGet( |
| 892 | MlirType shapedType, MlirStringRef name, intptr_t numElements, |
| 893 | const uint8_t *elements) { |
| 894 | return getDenseResource<DenseUI8ResourceElementsAttr>(shapedType, name, |
| 895 | numElements, elements); |
| 896 | } |
| 897 | MlirAttribute mlirUnmanagedDenseUInt16ResourceElementsAttrGet( |
| 898 | MlirType shapedType, MlirStringRef name, intptr_t numElements, |
| 899 | const uint16_t *elements) { |
| 900 | return getDenseResource<DenseUI16ResourceElementsAttr>(shapedType, name, |
| 901 | numElements, elements); |
| 902 | } |
| 903 | MlirAttribute mlirUnmanagedDenseUInt32ResourceElementsAttrGet( |
| 904 | MlirType shapedType, MlirStringRef name, intptr_t numElements, |
| 905 | const uint32_t *elements) { |
| 906 | return getDenseResource<DenseUI32ResourceElementsAttr>(shapedType, name, |
| 907 | numElements, elements); |
| 908 | } |
| 909 | MlirAttribute mlirUnmanagedDenseUInt64ResourceElementsAttrGet( |
| 910 | MlirType shapedType, MlirStringRef name, intptr_t numElements, |
| 911 | const uint64_t *elements) { |
| 912 | return getDenseResource<DenseUI64ResourceElementsAttr>(shapedType, name, |
| 913 | numElements, elements); |
| 914 | } |
| 915 | MlirAttribute mlirUnmanagedDenseInt8ResourceElementsAttrGet( |
| 916 | MlirType shapedType, MlirStringRef name, intptr_t numElements, |
| 917 | const int8_t *elements) { |
| 918 | return getDenseResource<DenseUI8ResourceElementsAttr>(shapedType, name, |
| 919 | numElements, elements); |
| 920 | } |
| 921 | MlirAttribute mlirUnmanagedDenseInt16ResourceElementsAttrGet( |
| 922 | MlirType shapedType, MlirStringRef name, intptr_t numElements, |
| 923 | const int16_t *elements) { |
| 924 | return getDenseResource<DenseUI16ResourceElementsAttr>(shapedType, name, |
| 925 | numElements, elements); |
| 926 | } |
| 927 | MlirAttribute mlirUnmanagedDenseInt32ResourceElementsAttrGet( |
| 928 | MlirType shapedType, MlirStringRef name, intptr_t numElements, |
| 929 | const int32_t *elements) { |
| 930 | return getDenseResource<DenseUI32ResourceElementsAttr>(shapedType, name, |
| 931 | numElements, elements); |
| 932 | } |
| 933 | MlirAttribute mlirUnmanagedDenseInt64ResourceElementsAttrGet( |
| 934 | MlirType shapedType, MlirStringRef name, intptr_t numElements, |
| 935 | const int64_t *elements) { |
| 936 | return getDenseResource<DenseUI64ResourceElementsAttr>(shapedType, name, |
| 937 | numElements, elements); |
| 938 | } |
| 939 | MlirAttribute mlirUnmanagedDenseFloatResourceElementsAttrGet( |
| 940 | MlirType shapedType, MlirStringRef name, intptr_t numElements, |
| 941 | const float *elements) { |
| 942 | return getDenseResource<DenseF32ResourceElementsAttr>(shapedType, name, |
| 943 | numElements, elements); |
| 944 | } |
| 945 | MlirAttribute mlirUnmanagedDenseDoubleResourceElementsAttrGet( |
| 946 | MlirType shapedType, MlirStringRef name, intptr_t numElements, |
| 947 | const double *elements) { |
| 948 | return getDenseResource<DenseF64ResourceElementsAttr>(shapedType, name, |
| 949 | numElements, elements); |
| 950 | } |
| 951 | template <typename U, typename T> |
| 952 | static T getDenseResourceVal(MlirAttribute attr, intptr_t pos) { |
| 953 | return (*llvm::cast<U>(unwrap(c: attr)).tryGetAsArrayRef())[pos]; |
| 954 | } |
| 955 | |
| 956 | bool mlirDenseBoolResourceElementsAttrGetValue(MlirAttribute attr, |
| 957 | intptr_t pos) { |
| 958 | return getDenseResourceVal<DenseBoolResourceElementsAttr, uint8_t>(attr, pos); |
| 959 | } |
| 960 | uint8_t mlirDenseUInt8ResourceElementsAttrGetValue(MlirAttribute attr, |
| 961 | intptr_t pos) { |
| 962 | return getDenseResourceVal<DenseUI8ResourceElementsAttr, uint8_t>(attr, pos); |
| 963 | } |
| 964 | uint16_t mlirDenseUInt16ResourceElementsAttrGetValue(MlirAttribute attr, |
| 965 | intptr_t pos) { |
| 966 | return getDenseResourceVal<DenseUI16ResourceElementsAttr, uint16_t>(attr, |
| 967 | pos); |
| 968 | } |
| 969 | uint32_t mlirDenseUInt32ResourceElementsAttrGetValue(MlirAttribute attr, |
| 970 | intptr_t pos) { |
| 971 | return getDenseResourceVal<DenseUI32ResourceElementsAttr, uint32_t>(attr, |
| 972 | pos); |
| 973 | } |
| 974 | uint64_t mlirDenseUInt64ResourceElementsAttrGetValue(MlirAttribute attr, |
| 975 | intptr_t pos) { |
| 976 | return getDenseResourceVal<DenseUI64ResourceElementsAttr, uint64_t>(attr, |
| 977 | pos); |
| 978 | } |
| 979 | int8_t mlirDenseInt8ResourceElementsAttrGetValue(MlirAttribute attr, |
| 980 | intptr_t pos) { |
| 981 | return getDenseResourceVal<DenseUI8ResourceElementsAttr, int8_t>(attr, pos); |
| 982 | } |
| 983 | int16_t mlirDenseInt16ResourceElementsAttrGetValue(MlirAttribute attr, |
| 984 | intptr_t pos) { |
| 985 | return getDenseResourceVal<DenseUI16ResourceElementsAttr, int16_t>(attr, pos); |
| 986 | } |
| 987 | int32_t mlirDenseInt32ResourceElementsAttrGetValue(MlirAttribute attr, |
| 988 | intptr_t pos) { |
| 989 | return getDenseResourceVal<DenseUI32ResourceElementsAttr, int32_t>(attr, pos); |
| 990 | } |
| 991 | int64_t mlirDenseInt64ResourceElementsAttrGetValue(MlirAttribute attr, |
| 992 | intptr_t pos) { |
| 993 | return getDenseResourceVal<DenseUI64ResourceElementsAttr, int64_t>(attr, pos); |
| 994 | } |
| 995 | float mlirDenseFloatResourceElementsAttrGetValue(MlirAttribute attr, |
| 996 | intptr_t pos) { |
| 997 | return getDenseResourceVal<DenseF32ResourceElementsAttr, float>(attr, pos); |
| 998 | } |
| 999 | double mlirDenseDoubleResourceElementsAttrGetValue(MlirAttribute attr, |
| 1000 | intptr_t pos) { |
| 1001 | return getDenseResourceVal<DenseF64ResourceElementsAttr, double>(attr, pos); |
| 1002 | } |
| 1003 | |
| 1004 | //===----------------------------------------------------------------------===// |
| 1005 | // Sparse elements attribute. |
| 1006 | //===----------------------------------------------------------------------===// |
| 1007 | |
| 1008 | bool mlirAttributeIsASparseElements(MlirAttribute attr) { |
| 1009 | return llvm::isa<SparseElementsAttr>(Val: unwrap(c: attr)); |
| 1010 | } |
| 1011 | |
| 1012 | MlirAttribute mlirSparseElementsAttribute(MlirType shapedType, |
| 1013 | MlirAttribute denseIndices, |
| 1014 | MlirAttribute denseValues) { |
| 1015 | return wrap(cpp: SparseElementsAttr::get( |
| 1016 | type: llvm::cast<ShapedType>(Val: unwrap(c: shapedType)), |
| 1017 | indices: llvm::cast<DenseElementsAttr>(Val: unwrap(c: denseIndices)), |
| 1018 | values: llvm::cast<DenseElementsAttr>(Val: unwrap(c: denseValues)))); |
| 1019 | } |
| 1020 | |
| 1021 | MlirAttribute mlirSparseElementsAttrGetIndices(MlirAttribute attr) { |
| 1022 | return wrap(cpp: llvm::cast<SparseElementsAttr>(Val: unwrap(c: attr)).getIndices()); |
| 1023 | } |
| 1024 | |
| 1025 | MlirAttribute mlirSparseElementsAttrGetValues(MlirAttribute attr) { |
| 1026 | return wrap(cpp: llvm::cast<SparseElementsAttr>(Val: unwrap(c: attr)).getValues()); |
| 1027 | } |
| 1028 | |
| 1029 | MlirTypeID mlirSparseElementsAttrGetTypeID(void) { |
| 1030 | return wrap(cpp: SparseElementsAttr::getTypeID()); |
| 1031 | } |
| 1032 | |
| 1033 | //===----------------------------------------------------------------------===// |
| 1034 | // Strided layout attribute. |
| 1035 | //===----------------------------------------------------------------------===// |
| 1036 | |
| 1037 | bool mlirAttributeIsAStridedLayout(MlirAttribute attr) { |
| 1038 | return llvm::isa<StridedLayoutAttr>(Val: unwrap(c: attr)); |
| 1039 | } |
| 1040 | |
| 1041 | MlirAttribute mlirStridedLayoutAttrGet(MlirContext ctx, int64_t offset, |
| 1042 | intptr_t numStrides, |
| 1043 | const int64_t *strides) { |
| 1044 | return wrap(cpp: StridedLayoutAttr::get(context: unwrap(c: ctx), offset, |
| 1045 | strides: ArrayRef<int64_t>(strides, numStrides))); |
| 1046 | } |
| 1047 | |
| 1048 | MlirStringRef mlirStridedLayoutAttrGetName(void) { |
| 1049 | return wrap(ref: StridedLayoutAttr::name); |
| 1050 | } |
| 1051 | |
| 1052 | int64_t mlirStridedLayoutAttrGetOffset(MlirAttribute attr) { |
| 1053 | return llvm::cast<StridedLayoutAttr>(Val: unwrap(c: attr)).getOffset(); |
| 1054 | } |
| 1055 | |
| 1056 | intptr_t mlirStridedLayoutAttrGetNumStrides(MlirAttribute attr) { |
| 1057 | return static_cast<intptr_t>( |
| 1058 | llvm::cast<StridedLayoutAttr>(Val: unwrap(c: attr)).getStrides().size()); |
| 1059 | } |
| 1060 | |
| 1061 | int64_t mlirStridedLayoutAttrGetStride(MlirAttribute attr, intptr_t pos) { |
| 1062 | return llvm::cast<StridedLayoutAttr>(Val: unwrap(c: attr)).getStrides()[pos]; |
| 1063 | } |
| 1064 | |
| 1065 | MlirTypeID mlirStridedLayoutAttrGetTypeID(void) { |
| 1066 | return wrap(cpp: StridedLayoutAttr::getTypeID()); |
| 1067 | } |
| 1068 | |