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