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 MlirModule mlirModuleFromOperation(MlirOperation op) { 185 return wrap(dyn_cast<ModuleOp>(unwrap(op))); 186 } 187 188 //===----------------------------------------------------------------------===// 189 // Operation state API. 190 //===----------------------------------------------------------------------===// 191 192 MlirOperationState mlirOperationStateGet(MlirStringRef name, MlirLocation loc) { 193 MlirOperationState state; 194 state.name = name; 195 state.location = loc; 196 state.nResults = 0; 197 state.results = nullptr; 198 state.nOperands = 0; 199 state.operands = nullptr; 200 state.nRegions = 0; 201 state.regions = nullptr; 202 state.nSuccessors = 0; 203 state.successors = nullptr; 204 state.nAttributes = 0; 205 state.attributes = nullptr; 206 state.enableResultTypeInference = false; 207 return state; 208 } 209 210 #define APPEND_ELEMS(type, sizeName, elemName) \ 211 state->elemName = \ 212 (type *)realloc(state->elemName, (state->sizeName + n) * sizeof(type)); \ 213 memcpy(state->elemName + state->sizeName, elemName, n * sizeof(type)); \ 214 state->sizeName += n; 215 216 void mlirOperationStateAddResults(MlirOperationState *state, intptr_t n, 217 MlirType const *results) { 218 APPEND_ELEMS(MlirType, nResults, results); 219 } 220 221 void mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n, 222 MlirValue const *operands) { 223 APPEND_ELEMS(MlirValue, nOperands, operands); 224 } 225 void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n, 226 MlirRegion const *regions) { 227 APPEND_ELEMS(MlirRegion, nRegions, regions); 228 } 229 void mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n, 230 MlirBlock const *successors) { 231 APPEND_ELEMS(MlirBlock, nSuccessors, successors); 232 } 233 void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n, 234 MlirNamedAttribute const *attributes) { 235 APPEND_ELEMS(MlirNamedAttribute, nAttributes, attributes); 236 } 237 238 void mlirOperationStateEnableResultTypeInference(MlirOperationState *state) { 239 state->enableResultTypeInference = true; 240 } 241 242 //===----------------------------------------------------------------------===// 243 // Operation API. 244 //===----------------------------------------------------------------------===// 245 246 static LogicalResult inferOperationTypes(OperationState &state) { 247 MLIRContext *context = state.getContext(); 248 const AbstractOperation *abstractOp = 249 AbstractOperation::lookup(state.name.getStringRef(), context); 250 if (!abstractOp) { 251 emitError(state.location) 252 << "type inference was requested for the operation " << state.name 253 << ", but the operation was not registered. Ensure that the dialect " 254 "containing the operation is linked into MLIR and registered with " 255 "the context"; 256 return failure(); 257 } 258 259 // Fallback to inference via an op interface. 260 auto *inferInterface = abstractOp->getInterface<InferTypeOpInterface>(); 261 if (!inferInterface) { 262 emitError(state.location) 263 << "type inference was requested for the operation " << state.name 264 << ", but the operation does not support type inference. Result " 265 "types must be specified explicitly."; 266 return failure(); 267 } 268 269 if (succeeded(inferInterface->inferReturnTypes( 270 context, state.location, state.operands, 271 state.attributes.getDictionary(context), state.regions, state.types))) 272 return success(); 273 274 // Diagnostic emitted by interface. 275 return failure(); 276 } 277 278 MlirOperation mlirOperationCreate(MlirOperationState *state) { 279 assert(state); 280 OperationState cppState(unwrap(state->location), unwrap(state->name)); 281 SmallVector<Type, 4> resultStorage; 282 SmallVector<Value, 8> operandStorage; 283 SmallVector<Block *, 2> successorStorage; 284 cppState.addTypes(unwrapList(state->nResults, state->results, resultStorage)); 285 cppState.addOperands( 286 unwrapList(state->nOperands, state->operands, operandStorage)); 287 cppState.addSuccessors( 288 unwrapList(state->nSuccessors, state->successors, successorStorage)); 289 290 cppState.attributes.reserve(state->nAttributes); 291 for (intptr_t i = 0; i < state->nAttributes; ++i) 292 cppState.addAttribute(unwrap(state->attributes[i].name), 293 unwrap(state->attributes[i].attribute)); 294 295 for (intptr_t i = 0; i < state->nRegions; ++i) 296 cppState.addRegion(std::unique_ptr<Region>(unwrap(state->regions[i]))); 297 298 free(state->results); 299 free(state->operands); 300 free(state->successors); 301 free(state->regions); 302 free(state->attributes); 303 304 // Infer result types. 305 if (state->enableResultTypeInference) { 306 assert(cppState.types.empty() && 307 "result type inference enabled and result types provided"); 308 if (failed(inferOperationTypes(cppState))) 309 return {nullptr}; 310 } 311 312 MlirOperation result = wrap(Operation::create(cppState)); 313 return result; 314 } 315 316 void mlirOperationDestroy(MlirOperation op) { unwrap(op)->erase(); } 317 318 bool mlirOperationEqual(MlirOperation op, MlirOperation other) { 319 return unwrap(op) == unwrap(other); 320 } 321 322 MlirContext mlirOperationGetContext(MlirOperation op) { 323 return wrap(unwrap(op)->getContext()); 324 } 325 326 MlirIdentifier mlirOperationGetName(MlirOperation op) { 327 return wrap(unwrap(op)->getName().getIdentifier()); 328 } 329 330 MlirBlock mlirOperationGetBlock(MlirOperation op) { 331 return wrap(unwrap(op)->getBlock()); 332 } 333 334 MlirOperation mlirOperationGetParentOperation(MlirOperation op) { 335 return wrap(unwrap(op)->getParentOp()); 336 } 337 338 intptr_t mlirOperationGetNumRegions(MlirOperation op) { 339 return static_cast<intptr_t>(unwrap(op)->getNumRegions()); 340 } 341 342 MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos) { 343 return wrap(&unwrap(op)->getRegion(static_cast<unsigned>(pos))); 344 } 345 346 MlirOperation mlirOperationGetNextInBlock(MlirOperation op) { 347 return wrap(unwrap(op)->getNextNode()); 348 } 349 350 intptr_t mlirOperationGetNumOperands(MlirOperation op) { 351 return static_cast<intptr_t>(unwrap(op)->getNumOperands()); 352 } 353 354 MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos) { 355 return wrap(unwrap(op)->getOperand(static_cast<unsigned>(pos))); 356 } 357 358 void mlirOperationSetOperand(MlirOperation op, intptr_t pos, 359 MlirValue newValue) { 360 unwrap(op)->setOperand(static_cast<unsigned>(pos), unwrap(newValue)); 361 } 362 363 intptr_t mlirOperationGetNumResults(MlirOperation op) { 364 return static_cast<intptr_t>(unwrap(op)->getNumResults()); 365 } 366 367 MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos) { 368 return wrap(unwrap(op)->getResult(static_cast<unsigned>(pos))); 369 } 370 371 intptr_t mlirOperationGetNumSuccessors(MlirOperation op) { 372 return static_cast<intptr_t>(unwrap(op)->getNumSuccessors()); 373 } 374 375 MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos) { 376 return wrap(unwrap(op)->getSuccessor(static_cast<unsigned>(pos))); 377 } 378 379 intptr_t mlirOperationGetNumAttributes(MlirOperation op) { 380 return static_cast<intptr_t>(unwrap(op)->getAttrs().size()); 381 } 382 383 MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos) { 384 NamedAttribute attr = unwrap(op)->getAttrs()[pos]; 385 return MlirNamedAttribute{wrap(attr.first), wrap(attr.second)}; 386 } 387 388 MlirAttribute mlirOperationGetAttributeByName(MlirOperation op, 389 MlirStringRef name) { 390 return wrap(unwrap(op)->getAttr(unwrap(name))); 391 } 392 393 void mlirOperationSetAttributeByName(MlirOperation op, MlirStringRef name, 394 MlirAttribute attr) { 395 unwrap(op)->setAttr(unwrap(name), unwrap(attr)); 396 } 397 398 bool mlirOperationRemoveAttributeByName(MlirOperation op, MlirStringRef name) { 399 return !!unwrap(op)->removeAttr(unwrap(name)); 400 } 401 402 void mlirOperationPrint(MlirOperation op, MlirStringCallback callback, 403 void *userData) { 404 detail::CallbackOstream stream(callback, userData); 405 unwrap(op)->print(stream); 406 } 407 408 void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags, 409 MlirStringCallback callback, void *userData) { 410 detail::CallbackOstream stream(callback, userData); 411 unwrap(op)->print(stream, *unwrap(flags)); 412 } 413 414 void mlirOperationDump(MlirOperation op) { return unwrap(op)->dump(); } 415 416 bool mlirOperationVerify(MlirOperation op) { 417 return succeeded(verify(unwrap(op))); 418 } 419 420 //===----------------------------------------------------------------------===// 421 // Region API. 422 //===----------------------------------------------------------------------===// 423 424 MlirRegion mlirRegionCreate() { return wrap(new Region); } 425 426 MlirBlock mlirRegionGetFirstBlock(MlirRegion region) { 427 Region *cppRegion = unwrap(region); 428 if (cppRegion->empty()) 429 return wrap(static_cast<Block *>(nullptr)); 430 return wrap(&cppRegion->front()); 431 } 432 433 void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block) { 434 unwrap(region)->push_back(unwrap(block)); 435 } 436 437 void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos, 438 MlirBlock block) { 439 auto &blockList = unwrap(region)->getBlocks(); 440 blockList.insert(std::next(blockList.begin(), pos), unwrap(block)); 441 } 442 443 void mlirRegionInsertOwnedBlockAfter(MlirRegion region, MlirBlock reference, 444 MlirBlock block) { 445 Region *cppRegion = unwrap(region); 446 if (mlirBlockIsNull(reference)) { 447 cppRegion->getBlocks().insert(cppRegion->begin(), unwrap(block)); 448 return; 449 } 450 451 assert(unwrap(reference)->getParent() == unwrap(region) && 452 "expected reference block to belong to the region"); 453 cppRegion->getBlocks().insertAfter(Region::iterator(unwrap(reference)), 454 unwrap(block)); 455 } 456 457 void mlirRegionInsertOwnedBlockBefore(MlirRegion region, MlirBlock reference, 458 MlirBlock block) { 459 if (mlirBlockIsNull(reference)) 460 return mlirRegionAppendOwnedBlock(region, block); 461 462 assert(unwrap(reference)->getParent() == unwrap(region) && 463 "expected reference block to belong to the region"); 464 unwrap(region)->getBlocks().insert(Region::iterator(unwrap(reference)), 465 unwrap(block)); 466 } 467 468 void mlirRegionDestroy(MlirRegion region) { 469 delete static_cast<Region *>(region.ptr); 470 } 471 472 //===----------------------------------------------------------------------===// 473 // Block API. 474 //===----------------------------------------------------------------------===// 475 476 MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args) { 477 Block *b = new Block; 478 for (intptr_t i = 0; i < nArgs; ++i) 479 b->addArgument(unwrap(args[i])); 480 return wrap(b); 481 } 482 483 bool mlirBlockEqual(MlirBlock block, MlirBlock other) { 484 return unwrap(block) == unwrap(other); 485 } 486 487 MlirOperation mlirBlockGetParentOperation(MlirBlock block) { 488 return wrap(unwrap(block)->getParentOp()); 489 } 490 491 MlirBlock mlirBlockGetNextInRegion(MlirBlock block) { 492 return wrap(unwrap(block)->getNextNode()); 493 } 494 495 MlirOperation mlirBlockGetFirstOperation(MlirBlock block) { 496 Block *cppBlock = unwrap(block); 497 if (cppBlock->empty()) 498 return wrap(static_cast<Operation *>(nullptr)); 499 return wrap(&cppBlock->front()); 500 } 501 502 MlirOperation mlirBlockGetTerminator(MlirBlock block) { 503 Block *cppBlock = unwrap(block); 504 if (cppBlock->empty()) 505 return wrap(static_cast<Operation *>(nullptr)); 506 Operation &back = cppBlock->back(); 507 if (!back.hasTrait<OpTrait::IsTerminator>()) 508 return wrap(static_cast<Operation *>(nullptr)); 509 return wrap(&back); 510 } 511 512 void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation) { 513 unwrap(block)->push_back(unwrap(operation)); 514 } 515 516 void mlirBlockInsertOwnedOperation(MlirBlock block, intptr_t pos, 517 MlirOperation operation) { 518 auto &opList = unwrap(block)->getOperations(); 519 opList.insert(std::next(opList.begin(), pos), unwrap(operation)); 520 } 521 522 void mlirBlockInsertOwnedOperationAfter(MlirBlock block, 523 MlirOperation reference, 524 MlirOperation operation) { 525 Block *cppBlock = unwrap(block); 526 if (mlirOperationIsNull(reference)) { 527 cppBlock->getOperations().insert(cppBlock->begin(), unwrap(operation)); 528 return; 529 } 530 531 assert(unwrap(reference)->getBlock() == unwrap(block) && 532 "expected reference operation to belong to the block"); 533 cppBlock->getOperations().insertAfter(Block::iterator(unwrap(reference)), 534 unwrap(operation)); 535 } 536 537 void mlirBlockInsertOwnedOperationBefore(MlirBlock block, 538 MlirOperation reference, 539 MlirOperation operation) { 540 if (mlirOperationIsNull(reference)) 541 return mlirBlockAppendOwnedOperation(block, operation); 542 543 assert(unwrap(reference)->getBlock() == unwrap(block) && 544 "expected reference operation to belong to the block"); 545 unwrap(block)->getOperations().insert(Block::iterator(unwrap(reference)), 546 unwrap(operation)); 547 } 548 549 void mlirBlockDestroy(MlirBlock block) { delete unwrap(block); } 550 551 intptr_t mlirBlockGetNumArguments(MlirBlock block) { 552 return static_cast<intptr_t>(unwrap(block)->getNumArguments()); 553 } 554 555 MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type) { 556 return wrap(unwrap(block)->addArgument(unwrap(type))); 557 } 558 559 MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos) { 560 return wrap(unwrap(block)->getArgument(static_cast<unsigned>(pos))); 561 } 562 563 void mlirBlockPrint(MlirBlock block, MlirStringCallback callback, 564 void *userData) { 565 detail::CallbackOstream stream(callback, userData); 566 unwrap(block)->print(stream); 567 } 568 569 //===----------------------------------------------------------------------===// 570 // Value API. 571 //===----------------------------------------------------------------------===// 572 573 bool mlirValueEqual(MlirValue value1, MlirValue value2) { 574 return unwrap(value1) == unwrap(value2); 575 } 576 577 bool mlirValueIsABlockArgument(MlirValue value) { 578 return unwrap(value).isa<BlockArgument>(); 579 } 580 581 bool mlirValueIsAOpResult(MlirValue value) { 582 return unwrap(value).isa<OpResult>(); 583 } 584 585 MlirBlock mlirBlockArgumentGetOwner(MlirValue value) { 586 return wrap(unwrap(value).cast<BlockArgument>().getOwner()); 587 } 588 589 intptr_t mlirBlockArgumentGetArgNumber(MlirValue value) { 590 return static_cast<intptr_t>( 591 unwrap(value).cast<BlockArgument>().getArgNumber()); 592 } 593 594 void mlirBlockArgumentSetType(MlirValue value, MlirType type) { 595 unwrap(value).cast<BlockArgument>().setType(unwrap(type)); 596 } 597 598 MlirOperation mlirOpResultGetOwner(MlirValue value) { 599 return wrap(unwrap(value).cast<OpResult>().getOwner()); 600 } 601 602 intptr_t mlirOpResultGetResultNumber(MlirValue value) { 603 return static_cast<intptr_t>( 604 unwrap(value).cast<OpResult>().getResultNumber()); 605 } 606 607 MlirType mlirValueGetType(MlirValue value) { 608 return wrap(unwrap(value).getType()); 609 } 610 611 void mlirValueDump(MlirValue value) { unwrap(value).dump(); } 612 613 void mlirValuePrint(MlirValue value, MlirStringCallback callback, 614 void *userData) { 615 detail::CallbackOstream stream(callback, userData); 616 unwrap(value).print(stream); 617 } 618 619 //===----------------------------------------------------------------------===// 620 // Type API. 621 //===----------------------------------------------------------------------===// 622 623 MlirType mlirTypeParseGet(MlirContext context, MlirStringRef type) { 624 return wrap(mlir::parseType(unwrap(type), unwrap(context))); 625 } 626 627 MlirContext mlirTypeGetContext(MlirType type) { 628 return wrap(unwrap(type).getContext()); 629 } 630 631 bool mlirTypeEqual(MlirType t1, MlirType t2) { 632 return unwrap(t1) == unwrap(t2); 633 } 634 635 void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData) { 636 detail::CallbackOstream stream(callback, userData); 637 unwrap(type).print(stream); 638 } 639 640 void mlirTypeDump(MlirType type) { unwrap(type).dump(); } 641 642 //===----------------------------------------------------------------------===// 643 // Attribute API. 644 //===----------------------------------------------------------------------===// 645 646 MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr) { 647 return wrap(mlir::parseAttribute(unwrap(attr), unwrap(context))); 648 } 649 650 MlirContext mlirAttributeGetContext(MlirAttribute attribute) { 651 return wrap(unwrap(attribute).getContext()); 652 } 653 654 MlirType mlirAttributeGetType(MlirAttribute attribute) { 655 return wrap(unwrap(attribute).getType()); 656 } 657 658 bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2) { 659 return unwrap(a1) == unwrap(a2); 660 } 661 662 void mlirAttributePrint(MlirAttribute attr, MlirStringCallback callback, 663 void *userData) { 664 detail::CallbackOstream stream(callback, userData); 665 unwrap(attr).print(stream); 666 } 667 668 void mlirAttributeDump(MlirAttribute attr) { unwrap(attr).dump(); } 669 670 MlirNamedAttribute mlirNamedAttributeGet(MlirIdentifier name, 671 MlirAttribute attr) { 672 return MlirNamedAttribute{name, attr}; 673 } 674 675 //===----------------------------------------------------------------------===// 676 // Identifier API. 677 //===----------------------------------------------------------------------===// 678 679 MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str) { 680 return wrap(Identifier::get(unwrap(str), unwrap(context))); 681 } 682 683 MlirContext mlirIdentifierGetContext(MlirIdentifier ident) { 684 return wrap(unwrap(ident).getContext()); 685 } 686 687 bool mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other) { 688 return unwrap(ident) == unwrap(other); 689 } 690 691 MlirStringRef mlirIdentifierStr(MlirIdentifier ident) { 692 return wrap(unwrap(ident).strref()); 693 } 694