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