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(StringAttr::get(unwrap(context), unwrap(name))))); 149 return wrap(Location(NameLoc::get( 150 StringAttr::get(unwrap(context), unwrap(name)), 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 Optional<RegisteredOperationName> info = state.name.getRegisteredInfo(); 268 if (!info) { 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 = info->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 void mlirOperationRemoveFromParent(MlirOperation op) { unwrap(op)->remove(); } 341 342 bool mlirOperationEqual(MlirOperation op, MlirOperation other) { 343 return unwrap(op) == unwrap(other); 344 } 345 346 MlirContext mlirOperationGetContext(MlirOperation op) { 347 return wrap(unwrap(op)->getContext()); 348 } 349 350 MlirLocation mlirOperationGetLocation(MlirOperation op) { 351 return wrap(unwrap(op)->getLoc()); 352 } 353 354 MlirTypeID mlirOperationGetTypeID(MlirOperation op) { 355 if (auto info = unwrap(op)->getRegisteredInfo()) 356 return wrap(info->getTypeID()); 357 return {nullptr}; 358 } 359 360 MlirIdentifier mlirOperationGetName(MlirOperation op) { 361 return wrap(unwrap(op)->getName().getIdentifier()); 362 } 363 364 MlirBlock mlirOperationGetBlock(MlirOperation op) { 365 return wrap(unwrap(op)->getBlock()); 366 } 367 368 MlirOperation mlirOperationGetParentOperation(MlirOperation op) { 369 return wrap(unwrap(op)->getParentOp()); 370 } 371 372 intptr_t mlirOperationGetNumRegions(MlirOperation op) { 373 return static_cast<intptr_t>(unwrap(op)->getNumRegions()); 374 } 375 376 MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos) { 377 return wrap(&unwrap(op)->getRegion(static_cast<unsigned>(pos))); 378 } 379 380 MlirRegion mlirOperationGetFirstRegion(MlirOperation op) { 381 Operation *cppOp = unwrap(op); 382 if (cppOp->getNumRegions() == 0) 383 return wrap(static_cast<Region *>(nullptr)); 384 return wrap(&cppOp->getRegion(0)); 385 } 386 387 MlirRegion mlirRegionGetNextInOperation(MlirRegion region) { 388 Region *cppRegion = unwrap(region); 389 Operation *parent = cppRegion->getParentOp(); 390 intptr_t next = cppRegion->getRegionNumber() + 1; 391 if (parent->getNumRegions() > next) 392 return wrap(&parent->getRegion(next)); 393 return wrap(static_cast<Region *>(nullptr)); 394 } 395 396 MlirOperation mlirOperationGetNextInBlock(MlirOperation op) { 397 return wrap(unwrap(op)->getNextNode()); 398 } 399 400 intptr_t mlirOperationGetNumOperands(MlirOperation op) { 401 return static_cast<intptr_t>(unwrap(op)->getNumOperands()); 402 } 403 404 MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos) { 405 return wrap(unwrap(op)->getOperand(static_cast<unsigned>(pos))); 406 } 407 408 void mlirOperationSetOperand(MlirOperation op, intptr_t pos, 409 MlirValue newValue) { 410 unwrap(op)->setOperand(static_cast<unsigned>(pos), unwrap(newValue)); 411 } 412 413 intptr_t mlirOperationGetNumResults(MlirOperation op) { 414 return static_cast<intptr_t>(unwrap(op)->getNumResults()); 415 } 416 417 MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos) { 418 return wrap(unwrap(op)->getResult(static_cast<unsigned>(pos))); 419 } 420 421 intptr_t mlirOperationGetNumSuccessors(MlirOperation op) { 422 return static_cast<intptr_t>(unwrap(op)->getNumSuccessors()); 423 } 424 425 MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos) { 426 return wrap(unwrap(op)->getSuccessor(static_cast<unsigned>(pos))); 427 } 428 429 intptr_t mlirOperationGetNumAttributes(MlirOperation op) { 430 return static_cast<intptr_t>(unwrap(op)->getAttrs().size()); 431 } 432 433 MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos) { 434 NamedAttribute attr = unwrap(op)->getAttrs()[pos]; 435 return MlirNamedAttribute{wrap(attr.getName()), wrap(attr.getValue())}; 436 } 437 438 MlirAttribute mlirOperationGetAttributeByName(MlirOperation op, 439 MlirStringRef name) { 440 return wrap(unwrap(op)->getAttr(unwrap(name))); 441 } 442 443 void mlirOperationSetAttributeByName(MlirOperation op, MlirStringRef name, 444 MlirAttribute attr) { 445 unwrap(op)->setAttr(unwrap(name), unwrap(attr)); 446 } 447 448 bool mlirOperationRemoveAttributeByName(MlirOperation op, MlirStringRef name) { 449 return !!unwrap(op)->removeAttr(unwrap(name)); 450 } 451 452 void mlirOperationPrint(MlirOperation op, MlirStringCallback callback, 453 void *userData) { 454 detail::CallbackOstream stream(callback, userData); 455 unwrap(op)->print(stream); 456 } 457 458 void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags, 459 MlirStringCallback callback, void *userData) { 460 detail::CallbackOstream stream(callback, userData); 461 unwrap(op)->print(stream, *unwrap(flags)); 462 } 463 464 void mlirOperationDump(MlirOperation op) { return unwrap(op)->dump(); } 465 466 bool mlirOperationVerify(MlirOperation op) { 467 return succeeded(verify(unwrap(op))); 468 } 469 470 void mlirOperationMoveAfter(MlirOperation op, MlirOperation other) { 471 return unwrap(op)->moveAfter(unwrap(other)); 472 } 473 474 void mlirOperationMoveBefore(MlirOperation op, MlirOperation other) { 475 return unwrap(op)->moveBefore(unwrap(other)); 476 } 477 478 //===----------------------------------------------------------------------===// 479 // Region API. 480 //===----------------------------------------------------------------------===// 481 482 MlirRegion mlirRegionCreate() { return wrap(new Region); } 483 484 bool mlirRegionEqual(MlirRegion region, MlirRegion other) { 485 return unwrap(region) == unwrap(other); 486 } 487 488 MlirBlock mlirRegionGetFirstBlock(MlirRegion region) { 489 Region *cppRegion = unwrap(region); 490 if (cppRegion->empty()) 491 return wrap(static_cast<Block *>(nullptr)); 492 return wrap(&cppRegion->front()); 493 } 494 495 void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block) { 496 unwrap(region)->push_back(unwrap(block)); 497 } 498 499 void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos, 500 MlirBlock block) { 501 auto &blockList = unwrap(region)->getBlocks(); 502 blockList.insert(std::next(blockList.begin(), pos), unwrap(block)); 503 } 504 505 void mlirRegionInsertOwnedBlockAfter(MlirRegion region, MlirBlock reference, 506 MlirBlock block) { 507 Region *cppRegion = unwrap(region); 508 if (mlirBlockIsNull(reference)) { 509 cppRegion->getBlocks().insert(cppRegion->begin(), unwrap(block)); 510 return; 511 } 512 513 assert(unwrap(reference)->getParent() == unwrap(region) && 514 "expected reference block to belong to the region"); 515 cppRegion->getBlocks().insertAfter(Region::iterator(unwrap(reference)), 516 unwrap(block)); 517 } 518 519 void mlirRegionInsertOwnedBlockBefore(MlirRegion region, MlirBlock reference, 520 MlirBlock block) { 521 if (mlirBlockIsNull(reference)) 522 return mlirRegionAppendOwnedBlock(region, block); 523 524 assert(unwrap(reference)->getParent() == unwrap(region) && 525 "expected reference block to belong to the region"); 526 unwrap(region)->getBlocks().insert(Region::iterator(unwrap(reference)), 527 unwrap(block)); 528 } 529 530 void mlirRegionDestroy(MlirRegion region) { 531 delete static_cast<Region *>(region.ptr); 532 } 533 534 //===----------------------------------------------------------------------===// 535 // Block API. 536 //===----------------------------------------------------------------------===// 537 538 MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args) { 539 Block *b = new Block; 540 for (intptr_t i = 0; i < nArgs; ++i) 541 b->addArgument(unwrap(args[i])); 542 return wrap(b); 543 } 544 545 bool mlirBlockEqual(MlirBlock block, MlirBlock other) { 546 return unwrap(block) == unwrap(other); 547 } 548 549 MlirOperation mlirBlockGetParentOperation(MlirBlock block) { 550 return wrap(unwrap(block)->getParentOp()); 551 } 552 553 MlirRegion mlirBlockGetParentRegion(MlirBlock block) { 554 return wrap(unwrap(block)->getParent()); 555 } 556 557 MlirBlock mlirBlockGetNextInRegion(MlirBlock block) { 558 return wrap(unwrap(block)->getNextNode()); 559 } 560 561 MlirOperation mlirBlockGetFirstOperation(MlirBlock block) { 562 Block *cppBlock = unwrap(block); 563 if (cppBlock->empty()) 564 return wrap(static_cast<Operation *>(nullptr)); 565 return wrap(&cppBlock->front()); 566 } 567 568 MlirOperation mlirBlockGetTerminator(MlirBlock block) { 569 Block *cppBlock = unwrap(block); 570 if (cppBlock->empty()) 571 return wrap(static_cast<Operation *>(nullptr)); 572 Operation &back = cppBlock->back(); 573 if (!back.hasTrait<OpTrait::IsTerminator>()) 574 return wrap(static_cast<Operation *>(nullptr)); 575 return wrap(&back); 576 } 577 578 void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation) { 579 unwrap(block)->push_back(unwrap(operation)); 580 } 581 582 void mlirBlockInsertOwnedOperation(MlirBlock block, intptr_t pos, 583 MlirOperation operation) { 584 auto &opList = unwrap(block)->getOperations(); 585 opList.insert(std::next(opList.begin(), pos), unwrap(operation)); 586 } 587 588 void mlirBlockInsertOwnedOperationAfter(MlirBlock block, 589 MlirOperation reference, 590 MlirOperation operation) { 591 Block *cppBlock = unwrap(block); 592 if (mlirOperationIsNull(reference)) { 593 cppBlock->getOperations().insert(cppBlock->begin(), unwrap(operation)); 594 return; 595 } 596 597 assert(unwrap(reference)->getBlock() == unwrap(block) && 598 "expected reference operation to belong to the block"); 599 cppBlock->getOperations().insertAfter(Block::iterator(unwrap(reference)), 600 unwrap(operation)); 601 } 602 603 void mlirBlockInsertOwnedOperationBefore(MlirBlock block, 604 MlirOperation reference, 605 MlirOperation operation) { 606 if (mlirOperationIsNull(reference)) 607 return mlirBlockAppendOwnedOperation(block, operation); 608 609 assert(unwrap(reference)->getBlock() == unwrap(block) && 610 "expected reference operation to belong to the block"); 611 unwrap(block)->getOperations().insert(Block::iterator(unwrap(reference)), 612 unwrap(operation)); 613 } 614 615 void mlirBlockDestroy(MlirBlock block) { delete unwrap(block); } 616 617 intptr_t mlirBlockGetNumArguments(MlirBlock block) { 618 return static_cast<intptr_t>(unwrap(block)->getNumArguments()); 619 } 620 621 MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type) { 622 return wrap(unwrap(block)->addArgument(unwrap(type))); 623 } 624 625 MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos) { 626 return wrap(unwrap(block)->getArgument(static_cast<unsigned>(pos))); 627 } 628 629 void mlirBlockPrint(MlirBlock block, MlirStringCallback callback, 630 void *userData) { 631 detail::CallbackOstream stream(callback, userData); 632 unwrap(block)->print(stream); 633 } 634 635 //===----------------------------------------------------------------------===// 636 // Value API. 637 //===----------------------------------------------------------------------===// 638 639 bool mlirValueEqual(MlirValue value1, MlirValue value2) { 640 return unwrap(value1) == unwrap(value2); 641 } 642 643 bool mlirValueIsABlockArgument(MlirValue value) { 644 return unwrap(value).isa<BlockArgument>(); 645 } 646 647 bool mlirValueIsAOpResult(MlirValue value) { 648 return unwrap(value).isa<OpResult>(); 649 } 650 651 MlirBlock mlirBlockArgumentGetOwner(MlirValue value) { 652 return wrap(unwrap(value).cast<BlockArgument>().getOwner()); 653 } 654 655 intptr_t mlirBlockArgumentGetArgNumber(MlirValue value) { 656 return static_cast<intptr_t>( 657 unwrap(value).cast<BlockArgument>().getArgNumber()); 658 } 659 660 void mlirBlockArgumentSetType(MlirValue value, MlirType type) { 661 unwrap(value).cast<BlockArgument>().setType(unwrap(type)); 662 } 663 664 MlirOperation mlirOpResultGetOwner(MlirValue value) { 665 return wrap(unwrap(value).cast<OpResult>().getOwner()); 666 } 667 668 intptr_t mlirOpResultGetResultNumber(MlirValue value) { 669 return static_cast<intptr_t>( 670 unwrap(value).cast<OpResult>().getResultNumber()); 671 } 672 673 MlirType mlirValueGetType(MlirValue value) { 674 return wrap(unwrap(value).getType()); 675 } 676 677 void mlirValueDump(MlirValue value) { unwrap(value).dump(); } 678 679 void mlirValuePrint(MlirValue value, MlirStringCallback callback, 680 void *userData) { 681 detail::CallbackOstream stream(callback, userData); 682 unwrap(value).print(stream); 683 } 684 685 //===----------------------------------------------------------------------===// 686 // Type API. 687 //===----------------------------------------------------------------------===// 688 689 MlirType mlirTypeParseGet(MlirContext context, MlirStringRef type) { 690 return wrap(mlir::parseType(unwrap(type), unwrap(context))); 691 } 692 693 MlirContext mlirTypeGetContext(MlirType type) { 694 return wrap(unwrap(type).getContext()); 695 } 696 697 MlirTypeID mlirTypeGetTypeID(MlirType type) { 698 return wrap(unwrap(type).getTypeID()); 699 } 700 701 bool mlirTypeEqual(MlirType t1, MlirType t2) { 702 return unwrap(t1) == unwrap(t2); 703 } 704 705 void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData) { 706 detail::CallbackOstream stream(callback, userData); 707 unwrap(type).print(stream); 708 } 709 710 void mlirTypeDump(MlirType type) { unwrap(type).dump(); } 711 712 //===----------------------------------------------------------------------===// 713 // Attribute API. 714 //===----------------------------------------------------------------------===// 715 716 MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr) { 717 return wrap(mlir::parseAttribute(unwrap(attr), unwrap(context))); 718 } 719 720 MlirContext mlirAttributeGetContext(MlirAttribute attribute) { 721 return wrap(unwrap(attribute).getContext()); 722 } 723 724 MlirType mlirAttributeGetType(MlirAttribute attribute) { 725 return wrap(unwrap(attribute).getType()); 726 } 727 728 MlirTypeID mlirAttributeGetTypeID(MlirAttribute attr) { 729 return wrap(unwrap(attr).getTypeID()); 730 } 731 732 bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2) { 733 return unwrap(a1) == unwrap(a2); 734 } 735 736 void mlirAttributePrint(MlirAttribute attr, MlirStringCallback callback, 737 void *userData) { 738 detail::CallbackOstream stream(callback, userData); 739 unwrap(attr).print(stream); 740 } 741 742 void mlirAttributeDump(MlirAttribute attr) { unwrap(attr).dump(); } 743 744 MlirNamedAttribute mlirNamedAttributeGet(MlirIdentifier name, 745 MlirAttribute attr) { 746 return MlirNamedAttribute{name, attr}; 747 } 748 749 //===----------------------------------------------------------------------===// 750 // Identifier API. 751 //===----------------------------------------------------------------------===// 752 753 MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str) { 754 return wrap(StringAttr::get(unwrap(context), unwrap(str))); 755 } 756 757 MlirContext mlirIdentifierGetContext(MlirIdentifier ident) { 758 return wrap(unwrap(ident).getContext()); 759 } 760 761 bool mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other) { 762 return unwrap(ident) == unwrap(other); 763 } 764 765 MlirStringRef mlirIdentifierStr(MlirIdentifier ident) { 766 return wrap(unwrap(ident).strref()); 767 } 768 769 //===----------------------------------------------------------------------===// 770 // TypeID API. 771 //===----------------------------------------------------------------------===// 772 773 bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2) { 774 return unwrap(typeID1) == unwrap(typeID2); 775 } 776 777 size_t mlirTypeIDHashValue(MlirTypeID typeID) { 778 return hash_value(unwrap(typeID)); 779 } 780 781 //===----------------------------------------------------------------------===// 782 // Symbol and SymbolTable API. 783 //===----------------------------------------------------------------------===// 784 785 MlirStringRef mlirSymbolTableGetSymbolAttributeName() { 786 return wrap(SymbolTable::getSymbolAttrName()); 787 } 788 789 MlirStringRef mlirSymbolTableGetVisibilityAttributeName() { 790 return wrap(SymbolTable::getVisibilityAttrName()); 791 } 792 793 MlirSymbolTable mlirSymbolTableCreate(MlirOperation operation) { 794 if (!unwrap(operation)->hasTrait<OpTrait::SymbolTable>()) 795 return wrap(static_cast<SymbolTable *>(nullptr)); 796 return wrap(new SymbolTable(unwrap(operation))); 797 } 798 799 void mlirSymbolTableDestroy(MlirSymbolTable symbolTable) { 800 delete unwrap(symbolTable); 801 } 802 803 MlirOperation mlirSymbolTableLookup(MlirSymbolTable symbolTable, 804 MlirStringRef name) { 805 return wrap(unwrap(symbolTable)->lookup(StringRef(name.data, name.length))); 806 } 807 808 MlirAttribute mlirSymbolTableInsert(MlirSymbolTable symbolTable, 809 MlirOperation operation) { 810 return wrap((Attribute)unwrap(symbolTable)->insert(unwrap(operation))); 811 } 812 813 void mlirSymbolTableErase(MlirSymbolTable symbolTable, 814 MlirOperation operation) { 815 unwrap(symbolTable)->erase(unwrap(operation)); 816 } 817 818 MlirLogicalResult mlirSymbolTableReplaceAllSymbolUses(MlirStringRef oldSymbol, 819 MlirStringRef newSymbol, 820 MlirOperation from) { 821 auto cppFrom = unwrap(from); 822 auto *context = cppFrom->getContext(); 823 auto oldSymbolAttr = StringAttr::get(unwrap(oldSymbol), context); 824 auto newSymbolAttr = StringAttr::get(unwrap(newSymbol), context); 825 return wrap(SymbolTable::replaceAllSymbolUses(oldSymbolAttr, newSymbolAttr, 826 unwrap(from))); 827 } 828 829 void mlirSymbolTableWalkSymbolTables(MlirOperation from, bool allSymUsesVisible, 830 void (*callback)(MlirOperation, bool, 831 void *userData), 832 void *userData) { 833 SymbolTable::walkSymbolTables(unwrap(from), allSymUsesVisible, 834 [&](Operation *foundOpCpp, bool isVisible) { 835 callback(wrap(foundOpCpp), isVisible, 836 userData); 837 }); 838 } 839