1 //===- IR.cpp - C Interface for Core MLIR APIs ----------------------------===// 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/IR.h" 10 #include "mlir-c/Support.h" 11 12 #include "mlir/CAPI/IR.h" 13 #include "mlir/CAPI/Support.h" 14 #include "mlir/CAPI/Utils.h" 15 #include "mlir/IR/Attributes.h" 16 #include "mlir/IR/BuiltinOps.h" 17 #include "mlir/IR/Dialect.h" 18 #include "mlir/IR/Operation.h" 19 #include "mlir/IR/Types.h" 20 #include "mlir/IR/Verifier.h" 21 #include "mlir/Interfaces/InferTypeOpInterface.h" 22 #include "mlir/Parser.h" 23 24 #include "llvm/Support/Debug.h" 25 26 using namespace mlir; 27 28 //===----------------------------------------------------------------------===// 29 // Context API. 30 //===----------------------------------------------------------------------===// 31 32 MlirContext mlirContextCreate() { 33 auto *context = new MLIRContext; 34 return wrap(context); 35 } 36 37 bool mlirContextEqual(MlirContext ctx1, MlirContext ctx2) { 38 return unwrap(ctx1) == unwrap(ctx2); 39 } 40 41 void mlirContextDestroy(MlirContext context) { delete unwrap(context); } 42 43 void mlirContextSetAllowUnregisteredDialects(MlirContext context, bool allow) { 44 unwrap(context)->allowUnregisteredDialects(allow); 45 } 46 47 bool mlirContextGetAllowUnregisteredDialects(MlirContext context) { 48 return unwrap(context)->allowsUnregisteredDialects(); 49 } 50 intptr_t mlirContextGetNumRegisteredDialects(MlirContext context) { 51 return static_cast<intptr_t>(unwrap(context)->getAvailableDialects().size()); 52 } 53 54 // TODO: expose a cheaper way than constructing + sorting a vector only to take 55 // its size. 56 intptr_t mlirContextGetNumLoadedDialects(MlirContext context) { 57 return static_cast<intptr_t>(unwrap(context)->getLoadedDialects().size()); 58 } 59 60 MlirDialect mlirContextGetOrLoadDialect(MlirContext context, 61 MlirStringRef name) { 62 return wrap(unwrap(context)->getOrLoadDialect(unwrap(name))); 63 } 64 65 bool mlirContextIsRegisteredOperation(MlirContext context, MlirStringRef name) { 66 return unwrap(context)->isOperationRegistered(unwrap(name)); 67 } 68 69 void mlirContextEnableMultithreading(MlirContext context, bool enable) { 70 return unwrap(context)->enableMultithreading(enable); 71 } 72 73 //===----------------------------------------------------------------------===// 74 // Dialect API. 75 //===----------------------------------------------------------------------===// 76 77 MlirContext mlirDialectGetContext(MlirDialect dialect) { 78 return wrap(unwrap(dialect)->getContext()); 79 } 80 81 bool mlirDialectEqual(MlirDialect dialect1, MlirDialect dialect2) { 82 return unwrap(dialect1) == unwrap(dialect2); 83 } 84 85 MlirStringRef mlirDialectGetNamespace(MlirDialect dialect) { 86 return wrap(unwrap(dialect)->getNamespace()); 87 } 88 89 //===----------------------------------------------------------------------===// 90 // Printing flags API. 91 //===----------------------------------------------------------------------===// 92 93 MlirOpPrintingFlags mlirOpPrintingFlagsCreate() { 94 return wrap(new OpPrintingFlags()); 95 } 96 97 void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags) { 98 delete unwrap(flags); 99 } 100 101 void mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags, 102 intptr_t largeElementLimit) { 103 unwrap(flags)->elideLargeElementsAttrs(largeElementLimit); 104 } 105 106 void mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, 107 bool prettyForm) { 108 unwrap(flags)->enableDebugInfo(/*prettyForm=*/prettyForm); 109 } 110 111 void mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags) { 112 unwrap(flags)->printGenericOpForm(); 113 } 114 115 void mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags) { 116 unwrap(flags)->useLocalScope(); 117 } 118 119 //===----------------------------------------------------------------------===// 120 // Location API. 121 //===----------------------------------------------------------------------===// 122 123 MlirLocation mlirLocationFileLineColGet(MlirContext context, 124 MlirStringRef filename, unsigned line, 125 unsigned col) { 126 return wrap(Location( 127 FileLineColLoc::get(unwrap(context), unwrap(filename), line, col))); 128 } 129 130 MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller) { 131 return wrap(Location(CallSiteLoc::get(unwrap(callee), unwrap(caller)))); 132 } 133 134 MlirLocation mlirLocationUnknownGet(MlirContext context) { 135 return wrap(Location(UnknownLoc::get(unwrap(context)))); 136 } 137 138 bool mlirLocationEqual(MlirLocation l1, MlirLocation l2) { 139 return unwrap(l1) == unwrap(l2); 140 } 141 142 MlirContext mlirLocationGetContext(MlirLocation location) { 143 return wrap(unwrap(location).getContext()); 144 } 145 146 void mlirLocationPrint(MlirLocation location, MlirStringCallback callback, 147 void *userData) { 148 detail::CallbackOstream stream(callback, userData); 149 unwrap(location).print(stream); 150 } 151 152 //===----------------------------------------------------------------------===// 153 // Module API. 154 //===----------------------------------------------------------------------===// 155 156 MlirModule mlirModuleCreateEmpty(MlirLocation location) { 157 return wrap(ModuleOp::create(unwrap(location))); 158 } 159 160 MlirModule mlirModuleCreateParse(MlirContext context, MlirStringRef module) { 161 OwningModuleRef owning = parseSourceString(unwrap(module), unwrap(context)); 162 if (!owning) 163 return MlirModule{nullptr}; 164 return MlirModule{owning.release().getOperation()}; 165 } 166 167 MlirContext mlirModuleGetContext(MlirModule module) { 168 return wrap(unwrap(module).getContext()); 169 } 170 171 MlirBlock mlirModuleGetBody(MlirModule module) { 172 return wrap(unwrap(module).getBody()); 173 } 174 175 void mlirModuleDestroy(MlirModule module) { 176 // Transfer ownership to an OwningModuleRef so that its destructor is called. 177 OwningModuleRef(unwrap(module)); 178 } 179 180 MlirOperation mlirModuleGetOperation(MlirModule module) { 181 return wrap(unwrap(module).getOperation()); 182 } 183 184 //===----------------------------------------------------------------------===// 185 // Operation state API. 186 //===----------------------------------------------------------------------===// 187 188 MlirOperationState mlirOperationStateGet(MlirStringRef name, MlirLocation loc) { 189 MlirOperationState state; 190 state.name = name; 191 state.location = loc; 192 state.nResults = 0; 193 state.results = nullptr; 194 state.nOperands = 0; 195 state.operands = nullptr; 196 state.nRegions = 0; 197 state.regions = nullptr; 198 state.nSuccessors = 0; 199 state.successors = nullptr; 200 state.nAttributes = 0; 201 state.attributes = nullptr; 202 state.enableResultTypeInference = false; 203 return state; 204 } 205 206 #define APPEND_ELEMS(type, sizeName, elemName) \ 207 state->elemName = \ 208 (type *)realloc(state->elemName, (state->sizeName + n) * sizeof(type)); \ 209 memcpy(state->elemName + state->sizeName, elemName, n * sizeof(type)); \ 210 state->sizeName += n; 211 212 void mlirOperationStateAddResults(MlirOperationState *state, intptr_t n, 213 MlirType const *results) { 214 APPEND_ELEMS(MlirType, nResults, results); 215 } 216 217 void mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n, 218 MlirValue const *operands) { 219 APPEND_ELEMS(MlirValue, nOperands, operands); 220 } 221 void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n, 222 MlirRegion const *regions) { 223 APPEND_ELEMS(MlirRegion, nRegions, regions); 224 } 225 void mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n, 226 MlirBlock const *successors) { 227 APPEND_ELEMS(MlirBlock, nSuccessors, successors); 228 } 229 void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n, 230 MlirNamedAttribute const *attributes) { 231 APPEND_ELEMS(MlirNamedAttribute, nAttributes, attributes); 232 } 233 234 void mlirOperationStateEnableResultTypeInference(MlirOperationState *state) { 235 state->enableResultTypeInference = true; 236 } 237 238 //===----------------------------------------------------------------------===// 239 // Operation API. 240 //===----------------------------------------------------------------------===// 241 242 static LogicalResult inferOperationTypes(OperationState &state) { 243 MLIRContext *context = state.getContext(); 244 const AbstractOperation *abstractOp = 245 AbstractOperation::lookup(state.name.getStringRef(), context); 246 if (!abstractOp) { 247 emitError(state.location) 248 << "type inference was requested for the operation " << state.name 249 << ", but the operation was not registered. Ensure that the dialect " 250 "containing the operation is linked into MLIR and registered with " 251 "the context"; 252 return failure(); 253 } 254 255 // Fallback to inference via an op interface. 256 auto *inferInterface = abstractOp->getInterface<InferTypeOpInterface>(); 257 if (!inferInterface) { 258 emitError(state.location) 259 << "type inference was requested for the operation " << state.name 260 << ", but the operation does not support type inference. Result " 261 "types must be specified explicitly."; 262 return failure(); 263 } 264 265 if (succeeded(inferInterface->inferReturnTypes( 266 context, state.location, state.operands, 267 state.attributes.getDictionary(context), state.regions, state.types))) 268 return success(); 269 270 // Diagnostic emitted by interface. 271 return failure(); 272 } 273 274 MlirOperation mlirOperationCreate(MlirOperationState *state) { 275 assert(state); 276 OperationState cppState(unwrap(state->location), unwrap(state->name)); 277 SmallVector<Type, 4> resultStorage; 278 SmallVector<Value, 8> operandStorage; 279 SmallVector<Block *, 2> successorStorage; 280 cppState.addTypes(unwrapList(state->nResults, state->results, resultStorage)); 281 cppState.addOperands( 282 unwrapList(state->nOperands, state->operands, operandStorage)); 283 cppState.addSuccessors( 284 unwrapList(state->nSuccessors, state->successors, successorStorage)); 285 286 cppState.attributes.reserve(state->nAttributes); 287 for (intptr_t i = 0; i < state->nAttributes; ++i) 288 cppState.addAttribute(unwrap(state->attributes[i].name), 289 unwrap(state->attributes[i].attribute)); 290 291 for (intptr_t i = 0; i < state->nRegions; ++i) 292 cppState.addRegion(std::unique_ptr<Region>(unwrap(state->regions[i]))); 293 294 free(state->results); 295 free(state->operands); 296 free(state->successors); 297 free(state->regions); 298 free(state->attributes); 299 300 // Infer result types. 301 if (state->enableResultTypeInference) { 302 assert(cppState.types.empty() && 303 "result type inference enabled and result types provided"); 304 if (failed(inferOperationTypes(cppState))) 305 return {nullptr}; 306 } 307 308 MlirOperation result = wrap(Operation::create(cppState)); 309 return result; 310 } 311 312 void mlirOperationDestroy(MlirOperation op) { unwrap(op)->erase(); } 313 314 bool mlirOperationEqual(MlirOperation op, MlirOperation other) { 315 return unwrap(op) == unwrap(other); 316 } 317 318 MlirContext mlirOperationGetContext(MlirOperation op) { 319 return wrap(unwrap(op)->getContext()); 320 } 321 322 MlirIdentifier mlirOperationGetName(MlirOperation op) { 323 return wrap(unwrap(op)->getName().getIdentifier()); 324 } 325 326 MlirBlock mlirOperationGetBlock(MlirOperation op) { 327 return wrap(unwrap(op)->getBlock()); 328 } 329 330 MlirOperation mlirOperationGetParentOperation(MlirOperation op) { 331 return wrap(unwrap(op)->getParentOp()); 332 } 333 334 intptr_t mlirOperationGetNumRegions(MlirOperation op) { 335 return static_cast<intptr_t>(unwrap(op)->getNumRegions()); 336 } 337 338 MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos) { 339 return wrap(&unwrap(op)->getRegion(static_cast<unsigned>(pos))); 340 } 341 342 MlirOperation mlirOperationGetNextInBlock(MlirOperation op) { 343 return wrap(unwrap(op)->getNextNode()); 344 } 345 346 intptr_t mlirOperationGetNumOperands(MlirOperation op) { 347 return static_cast<intptr_t>(unwrap(op)->getNumOperands()); 348 } 349 350 MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos) { 351 return wrap(unwrap(op)->getOperand(static_cast<unsigned>(pos))); 352 } 353 354 intptr_t mlirOperationGetNumResults(MlirOperation op) { 355 return static_cast<intptr_t>(unwrap(op)->getNumResults()); 356 } 357 358 MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos) { 359 return wrap(unwrap(op)->getResult(static_cast<unsigned>(pos))); 360 } 361 362 intptr_t mlirOperationGetNumSuccessors(MlirOperation op) { 363 return static_cast<intptr_t>(unwrap(op)->getNumSuccessors()); 364 } 365 366 MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos) { 367 return wrap(unwrap(op)->getSuccessor(static_cast<unsigned>(pos))); 368 } 369 370 intptr_t mlirOperationGetNumAttributes(MlirOperation op) { 371 return static_cast<intptr_t>(unwrap(op)->getAttrs().size()); 372 } 373 374 MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos) { 375 NamedAttribute attr = unwrap(op)->getAttrs()[pos]; 376 return MlirNamedAttribute{wrap(attr.first), wrap(attr.second)}; 377 } 378 379 MlirAttribute mlirOperationGetAttributeByName(MlirOperation op, 380 MlirStringRef name) { 381 return wrap(unwrap(op)->getAttr(unwrap(name))); 382 } 383 384 void mlirOperationSetAttributeByName(MlirOperation op, MlirStringRef name, 385 MlirAttribute attr) { 386 unwrap(op)->setAttr(unwrap(name), unwrap(attr)); 387 } 388 389 bool mlirOperationRemoveAttributeByName(MlirOperation op, MlirStringRef name) { 390 return !!unwrap(op)->removeAttr(unwrap(name)); 391 } 392 393 void mlirOperationPrint(MlirOperation op, MlirStringCallback callback, 394 void *userData) { 395 detail::CallbackOstream stream(callback, userData); 396 unwrap(op)->print(stream); 397 } 398 399 void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags, 400 MlirStringCallback callback, void *userData) { 401 detail::CallbackOstream stream(callback, userData); 402 unwrap(op)->print(stream, *unwrap(flags)); 403 } 404 405 void mlirOperationDump(MlirOperation op) { return unwrap(op)->dump(); } 406 407 bool mlirOperationVerify(MlirOperation op) { 408 return succeeded(verify(unwrap(op))); 409 } 410 411 //===----------------------------------------------------------------------===// 412 // Region API. 413 //===----------------------------------------------------------------------===// 414 415 MlirRegion mlirRegionCreate() { return wrap(new Region); } 416 417 MlirBlock mlirRegionGetFirstBlock(MlirRegion region) { 418 Region *cppRegion = unwrap(region); 419 if (cppRegion->empty()) 420 return wrap(static_cast<Block *>(nullptr)); 421 return wrap(&cppRegion->front()); 422 } 423 424 void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block) { 425 unwrap(region)->push_back(unwrap(block)); 426 } 427 428 void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos, 429 MlirBlock block) { 430 auto &blockList = unwrap(region)->getBlocks(); 431 blockList.insert(std::next(blockList.begin(), pos), unwrap(block)); 432 } 433 434 void mlirRegionInsertOwnedBlockAfter(MlirRegion region, MlirBlock reference, 435 MlirBlock block) { 436 Region *cppRegion = unwrap(region); 437 if (mlirBlockIsNull(reference)) { 438 cppRegion->getBlocks().insert(cppRegion->begin(), unwrap(block)); 439 return; 440 } 441 442 assert(unwrap(reference)->getParent() == unwrap(region) && 443 "expected reference block to belong to the region"); 444 cppRegion->getBlocks().insertAfter(Region::iterator(unwrap(reference)), 445 unwrap(block)); 446 } 447 448 void mlirRegionInsertOwnedBlockBefore(MlirRegion region, MlirBlock reference, 449 MlirBlock block) { 450 if (mlirBlockIsNull(reference)) 451 return mlirRegionAppendOwnedBlock(region, block); 452 453 assert(unwrap(reference)->getParent() == unwrap(region) && 454 "expected reference block to belong to the region"); 455 unwrap(region)->getBlocks().insert(Region::iterator(unwrap(reference)), 456 unwrap(block)); 457 } 458 459 void mlirRegionDestroy(MlirRegion region) { 460 delete static_cast<Region *>(region.ptr); 461 } 462 463 //===----------------------------------------------------------------------===// 464 // Block API. 465 //===----------------------------------------------------------------------===// 466 467 MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args) { 468 Block *b = new Block; 469 for (intptr_t i = 0; i < nArgs; ++i) 470 b->addArgument(unwrap(args[i])); 471 return wrap(b); 472 } 473 474 bool mlirBlockEqual(MlirBlock block, MlirBlock other) { 475 return unwrap(block) == unwrap(other); 476 } 477 478 MlirOperation mlirBlockGetParentOperation(MlirBlock block) { 479 return wrap(unwrap(block)->getParentOp()); 480 } 481 482 MlirBlock mlirBlockGetNextInRegion(MlirBlock block) { 483 return wrap(unwrap(block)->getNextNode()); 484 } 485 486 MlirOperation mlirBlockGetFirstOperation(MlirBlock block) { 487 Block *cppBlock = unwrap(block); 488 if (cppBlock->empty()) 489 return wrap(static_cast<Operation *>(nullptr)); 490 return wrap(&cppBlock->front()); 491 } 492 493 MlirOperation mlirBlockGetTerminator(MlirBlock block) { 494 Block *cppBlock = unwrap(block); 495 if (cppBlock->empty()) 496 return wrap(static_cast<Operation *>(nullptr)); 497 Operation &back = cppBlock->back(); 498 if (!back.hasTrait<OpTrait::IsTerminator>()) 499 return wrap(static_cast<Operation *>(nullptr)); 500 return wrap(&back); 501 } 502 503 void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation) { 504 unwrap(block)->push_back(unwrap(operation)); 505 } 506 507 void mlirBlockInsertOwnedOperation(MlirBlock block, intptr_t pos, 508 MlirOperation operation) { 509 auto &opList = unwrap(block)->getOperations(); 510 opList.insert(std::next(opList.begin(), pos), unwrap(operation)); 511 } 512 513 void mlirBlockInsertOwnedOperationAfter(MlirBlock block, 514 MlirOperation reference, 515 MlirOperation operation) { 516 Block *cppBlock = unwrap(block); 517 if (mlirOperationIsNull(reference)) { 518 cppBlock->getOperations().insert(cppBlock->begin(), unwrap(operation)); 519 return; 520 } 521 522 assert(unwrap(reference)->getBlock() == unwrap(block) && 523 "expected reference operation to belong to the block"); 524 cppBlock->getOperations().insertAfter(Block::iterator(unwrap(reference)), 525 unwrap(operation)); 526 } 527 528 void mlirBlockInsertOwnedOperationBefore(MlirBlock block, 529 MlirOperation reference, 530 MlirOperation operation) { 531 if (mlirOperationIsNull(reference)) 532 return mlirBlockAppendOwnedOperation(block, operation); 533 534 assert(unwrap(reference)->getBlock() == unwrap(block) && 535 "expected reference operation to belong to the block"); 536 unwrap(block)->getOperations().insert(Block::iterator(unwrap(reference)), 537 unwrap(operation)); 538 } 539 540 void mlirBlockDestroy(MlirBlock block) { delete unwrap(block); } 541 542 intptr_t mlirBlockGetNumArguments(MlirBlock block) { 543 return static_cast<intptr_t>(unwrap(block)->getNumArguments()); 544 } 545 546 MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type) { 547 return wrap(unwrap(block)->addArgument(unwrap(type))); 548 } 549 550 MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos) { 551 return wrap(unwrap(block)->getArgument(static_cast<unsigned>(pos))); 552 } 553 554 void mlirBlockPrint(MlirBlock block, MlirStringCallback callback, 555 void *userData) { 556 detail::CallbackOstream stream(callback, userData); 557 unwrap(block)->print(stream); 558 } 559 560 //===----------------------------------------------------------------------===// 561 // Value API. 562 //===----------------------------------------------------------------------===// 563 564 bool mlirValueEqual(MlirValue value1, MlirValue value2) { 565 return unwrap(value1) == unwrap(value2); 566 } 567 568 bool mlirValueIsABlockArgument(MlirValue value) { 569 return unwrap(value).isa<BlockArgument>(); 570 } 571 572 bool mlirValueIsAOpResult(MlirValue value) { 573 return unwrap(value).isa<OpResult>(); 574 } 575 576 MlirBlock mlirBlockArgumentGetOwner(MlirValue value) { 577 return wrap(unwrap(value).cast<BlockArgument>().getOwner()); 578 } 579 580 intptr_t mlirBlockArgumentGetArgNumber(MlirValue value) { 581 return static_cast<intptr_t>( 582 unwrap(value).cast<BlockArgument>().getArgNumber()); 583 } 584 585 void mlirBlockArgumentSetType(MlirValue value, MlirType type) { 586 unwrap(value).cast<BlockArgument>().setType(unwrap(type)); 587 } 588 589 MlirOperation mlirOpResultGetOwner(MlirValue value) { 590 return wrap(unwrap(value).cast<OpResult>().getOwner()); 591 } 592 593 intptr_t mlirOpResultGetResultNumber(MlirValue value) { 594 return static_cast<intptr_t>( 595 unwrap(value).cast<OpResult>().getResultNumber()); 596 } 597 598 MlirType mlirValueGetType(MlirValue value) { 599 return wrap(unwrap(value).getType()); 600 } 601 602 void mlirValueDump(MlirValue value) { unwrap(value).dump(); } 603 604 void mlirValuePrint(MlirValue value, MlirStringCallback callback, 605 void *userData) { 606 detail::CallbackOstream stream(callback, userData); 607 unwrap(value).print(stream); 608 } 609 610 //===----------------------------------------------------------------------===// 611 // Type API. 612 //===----------------------------------------------------------------------===// 613 614 MlirType mlirTypeParseGet(MlirContext context, MlirStringRef type) { 615 return wrap(mlir::parseType(unwrap(type), unwrap(context))); 616 } 617 618 MlirContext mlirTypeGetContext(MlirType type) { 619 return wrap(unwrap(type).getContext()); 620 } 621 622 bool mlirTypeEqual(MlirType t1, MlirType t2) { 623 return unwrap(t1) == unwrap(t2); 624 } 625 626 void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData) { 627 detail::CallbackOstream stream(callback, userData); 628 unwrap(type).print(stream); 629 } 630 631 void mlirTypeDump(MlirType type) { unwrap(type).dump(); } 632 633 //===----------------------------------------------------------------------===// 634 // Attribute API. 635 //===----------------------------------------------------------------------===// 636 637 MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr) { 638 return wrap(mlir::parseAttribute(unwrap(attr), unwrap(context))); 639 } 640 641 MlirContext mlirAttributeGetContext(MlirAttribute attribute) { 642 return wrap(unwrap(attribute).getContext()); 643 } 644 645 MlirType mlirAttributeGetType(MlirAttribute attribute) { 646 return wrap(unwrap(attribute).getType()); 647 } 648 649 bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2) { 650 return unwrap(a1) == unwrap(a2); 651 } 652 653 void mlirAttributePrint(MlirAttribute attr, MlirStringCallback callback, 654 void *userData) { 655 detail::CallbackOstream stream(callback, userData); 656 unwrap(attr).print(stream); 657 } 658 659 void mlirAttributeDump(MlirAttribute attr) { unwrap(attr).dump(); } 660 661 MlirNamedAttribute mlirNamedAttributeGet(MlirIdentifier name, 662 MlirAttribute attr) { 663 return MlirNamedAttribute{name, attr}; 664 } 665 666 //===----------------------------------------------------------------------===// 667 // Identifier API. 668 //===----------------------------------------------------------------------===// 669 670 MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str) { 671 return wrap(Identifier::get(unwrap(str), unwrap(context))); 672 } 673 674 MlirContext mlirIdentifierGetContext(MlirIdentifier ident) { 675 return wrap(unwrap(ident).getContext()); 676 } 677 678 bool mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other) { 679 return unwrap(ident) == unwrap(other); 680 } 681 682 MlirStringRef mlirIdentifierStr(MlirIdentifier ident) { 683 return wrap(unwrap(ident).strref()); 684 } 685