| 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/AsmParser/AsmParser.h" |
| 13 | #include "mlir/Bytecode/BytecodeWriter.h" |
| 14 | #include "mlir/CAPI/IR.h" |
| 15 | #include "mlir/CAPI/IRMapping.h" |
| 16 | #include "mlir/CAPI/Support.h" |
| 17 | #include "mlir/CAPI/Utils.h" |
| 18 | #include "mlir/IR/Attributes.h" |
| 19 | #include "mlir/IR/BuiltinAttributes.h" |
| 20 | #include "mlir/IR/BuiltinOps.h" |
| 21 | #include "mlir/IR/Diagnostics.h" |
| 22 | #include "mlir/IR/Dialect.h" |
| 23 | #include "mlir/IR/Location.h" |
| 24 | #include "mlir/IR/Operation.h" |
| 25 | #include "mlir/IR/OperationSupport.h" |
| 26 | #include "mlir/IR/OwningOpRef.h" |
| 27 | #include "mlir/IR/Types.h" |
| 28 | #include "mlir/IR/Value.h" |
| 29 | #include "mlir/IR/Verifier.h" |
| 30 | #include "mlir/IR/Visitors.h" |
| 31 | #include "mlir/Interfaces/InferTypeOpInterface.h" |
| 32 | #include "mlir/Parser/Parser.h" |
| 33 | #include "llvm/ADT/SmallPtrSet.h" |
| 34 | |
| 35 | #include <cstddef> |
| 36 | #include <memory> |
| 37 | #include <optional> |
| 38 | |
| 39 | using namespace mlir; |
| 40 | |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | // Context API. |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
| 45 | MlirContext mlirContextCreate() { |
| 46 | auto *context = new MLIRContext; |
| 47 | return wrap(cpp: context); |
| 48 | } |
| 49 | |
| 50 | static inline MLIRContext::Threading toThreadingEnum(bool threadingEnabled) { |
| 51 | return threadingEnabled ? MLIRContext::Threading::ENABLED |
| 52 | : MLIRContext::Threading::DISABLED; |
| 53 | } |
| 54 | |
| 55 | MlirContext mlirContextCreateWithThreading(bool threadingEnabled) { |
| 56 | auto *context = new MLIRContext(toThreadingEnum(threadingEnabled)); |
| 57 | return wrap(cpp: context); |
| 58 | } |
| 59 | |
| 60 | MlirContext mlirContextCreateWithRegistry(MlirDialectRegistry registry, |
| 61 | bool threadingEnabled) { |
| 62 | auto *context = |
| 63 | new MLIRContext(*unwrap(c: registry), toThreadingEnum(threadingEnabled)); |
| 64 | return wrap(cpp: context); |
| 65 | } |
| 66 | |
| 67 | bool mlirContextEqual(MlirContext ctx1, MlirContext ctx2) { |
| 68 | return unwrap(c: ctx1) == unwrap(c: ctx2); |
| 69 | } |
| 70 | |
| 71 | void mlirContextDestroy(MlirContext context) { delete unwrap(c: context); } |
| 72 | |
| 73 | void mlirContextSetAllowUnregisteredDialects(MlirContext context, bool allow) { |
| 74 | unwrap(c: context)->allowUnregisteredDialects(allow); |
| 75 | } |
| 76 | |
| 77 | bool mlirContextGetAllowUnregisteredDialects(MlirContext context) { |
| 78 | return unwrap(c: context)->allowsUnregisteredDialects(); |
| 79 | } |
| 80 | intptr_t mlirContextGetNumRegisteredDialects(MlirContext context) { |
| 81 | return static_cast<intptr_t>(unwrap(c: context)->getAvailableDialects().size()); |
| 82 | } |
| 83 | |
| 84 | void mlirContextAppendDialectRegistry(MlirContext ctx, |
| 85 | MlirDialectRegistry registry) { |
| 86 | unwrap(c: ctx)->appendDialectRegistry(registry: *unwrap(c: registry)); |
| 87 | } |
| 88 | |
| 89 | // TODO: expose a cheaper way than constructing + sorting a vector only to take |
| 90 | // its size. |
| 91 | intptr_t mlirContextGetNumLoadedDialects(MlirContext context) { |
| 92 | return static_cast<intptr_t>(unwrap(c: context)->getLoadedDialects().size()); |
| 93 | } |
| 94 | |
| 95 | MlirDialect mlirContextGetOrLoadDialect(MlirContext context, |
| 96 | MlirStringRef name) { |
| 97 | return wrap(cpp: unwrap(c: context)->getOrLoadDialect(name: unwrap(ref: name))); |
| 98 | } |
| 99 | |
| 100 | MlirDialect mlirContextGetLoadedDialect(MlirContext context, |
| 101 | MlirStringRef name) { |
| 102 | return wrap(cpp: unwrap(c: context)->getLoadedDialect(name: unwrap(ref: name))); |
| 103 | } |
| 104 | |
| 105 | bool mlirContextIsRegisteredOperation(MlirContext context, MlirStringRef name) { |
| 106 | return unwrap(c: context)->isOperationRegistered(name: unwrap(ref: name)); |
| 107 | } |
| 108 | |
| 109 | void mlirContextEnableMultithreading(MlirContext context, bool enable) { |
| 110 | return unwrap(c: context)->enableMultithreading(enable); |
| 111 | } |
| 112 | |
| 113 | void mlirContextLoadAllAvailableDialects(MlirContext context) { |
| 114 | unwrap(c: context)->loadAllAvailableDialects(); |
| 115 | } |
| 116 | |
| 117 | void mlirContextSetThreadPool(MlirContext context, |
| 118 | MlirLlvmThreadPool threadPool) { |
| 119 | unwrap(c: context)->setThreadPool(*unwrap(c: threadPool)); |
| 120 | } |
| 121 | |
| 122 | unsigned mlirContextGetNumThreads(MlirContext context) { |
| 123 | return unwrap(c: context)->getNumThreads(); |
| 124 | } |
| 125 | |
| 126 | MlirLlvmThreadPool mlirContextGetThreadPool(MlirContext context) { |
| 127 | return wrap(cpp: &unwrap(c: context)->getThreadPool()); |
| 128 | } |
| 129 | |
| 130 | void mlirContextBeginTransientScope(MlirContext context) { |
| 131 | unwrap(c: context)->beginTransientScope(); |
| 132 | } |
| 133 | |
| 134 | void mlirContextEndTransientScope(MlirContext context) { |
| 135 | unwrap(c: context)->endTransientScope(); |
| 136 | } |
| 137 | |
| 138 | bool mlirContextIsInTransientScope(MlirContext context) { |
| 139 | return unwrap(c: context)->isInTransientScope(); |
| 140 | } |
| 141 | |
| 142 | //===----------------------------------------------------------------------===// |
| 143 | // Dialect API. |
| 144 | //===----------------------------------------------------------------------===// |
| 145 | |
| 146 | MlirContext mlirDialectGetContext(MlirDialect dialect) { |
| 147 | return wrap(cpp: unwrap(c: dialect)->getContext()); |
| 148 | } |
| 149 | |
| 150 | bool mlirDialectEqual(MlirDialect dialect1, MlirDialect dialect2) { |
| 151 | return unwrap(c: dialect1) == unwrap(c: dialect2); |
| 152 | } |
| 153 | |
| 154 | MlirStringRef mlirDialectGetNamespace(MlirDialect dialect) { |
| 155 | return wrap(ref: unwrap(c: dialect)->getNamespace()); |
| 156 | } |
| 157 | |
| 158 | //===----------------------------------------------------------------------===// |
| 159 | // DialectRegistry API. |
| 160 | //===----------------------------------------------------------------------===// |
| 161 | |
| 162 | MlirDialectRegistry mlirDialectRegistryCreate() { |
| 163 | return wrap(cpp: new DialectRegistry()); |
| 164 | } |
| 165 | |
| 166 | void mlirDialectRegistryDestroy(MlirDialectRegistry registry) { |
| 167 | delete unwrap(c: registry); |
| 168 | } |
| 169 | |
| 170 | //===----------------------------------------------------------------------===// |
| 171 | // AsmState API. |
| 172 | //===----------------------------------------------------------------------===// |
| 173 | |
| 174 | MlirAsmState mlirAsmStateCreateForOperation(MlirOperation op, |
| 175 | MlirOpPrintingFlags flags) { |
| 176 | return wrap(cpp: new AsmState(unwrap(c: op), *unwrap(c: flags))); |
| 177 | } |
| 178 | |
| 179 | static Operation *findParent(Operation *op, bool shouldUseLocalScope) { |
| 180 | do { |
| 181 | // If we are printing local scope, stop at the first operation that is |
| 182 | // isolated from above. |
| 183 | if (shouldUseLocalScope && op->hasTrait<OpTrait::IsIsolatedFromAbove>()) |
| 184 | break; |
| 185 | |
| 186 | // Otherwise, traverse up to the next parent. |
| 187 | Operation *parentOp = op->getParentOp(); |
| 188 | if (!parentOp) |
| 189 | break; |
| 190 | op = parentOp; |
| 191 | } while (true); |
| 192 | return op; |
| 193 | } |
| 194 | |
| 195 | MlirAsmState mlirAsmStateCreateForValue(MlirValue value, |
| 196 | MlirOpPrintingFlags flags) { |
| 197 | Operation *op; |
| 198 | mlir::Value val = unwrap(c: value); |
| 199 | if (auto result = llvm::dyn_cast<OpResult>(Val&: val)) { |
| 200 | op = result.getOwner(); |
| 201 | } else { |
| 202 | op = llvm::cast<BlockArgument>(Val&: val).getOwner()->getParentOp(); |
| 203 | if (!op) { |
| 204 | emitError(loc: val.getLoc()) << "<<UNKNOWN SSA VALUE>>" ; |
| 205 | return {.ptr: nullptr}; |
| 206 | } |
| 207 | } |
| 208 | op = findParent(op, shouldUseLocalScope: unwrap(c: flags)->shouldUseLocalScope()); |
| 209 | return wrap(cpp: new AsmState(op, *unwrap(c: flags))); |
| 210 | } |
| 211 | |
| 212 | /// Destroys printing flags created with mlirAsmStateCreate. |
| 213 | void mlirAsmStateDestroy(MlirAsmState state) { delete unwrap(c: state); } |
| 214 | |
| 215 | //===----------------------------------------------------------------------===// |
| 216 | // Printing flags API. |
| 217 | //===----------------------------------------------------------------------===// |
| 218 | |
| 219 | MlirOpPrintingFlags mlirOpPrintingFlagsCreate() { |
| 220 | return wrap(cpp: new OpPrintingFlags()); |
| 221 | } |
| 222 | |
| 223 | void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags) { |
| 224 | delete unwrap(c: flags); |
| 225 | } |
| 226 | |
| 227 | void mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags, |
| 228 | intptr_t largeElementLimit) { |
| 229 | unwrap(c: flags)->elideLargeElementsAttrs(largeElementLimit); |
| 230 | } |
| 231 | |
| 232 | void mlirOpPrintingFlagsElideLargeResourceString(MlirOpPrintingFlags flags, |
| 233 | intptr_t largeResourceLimit) { |
| 234 | unwrap(c: flags)->elideLargeResourceString(largeResourceLimit); |
| 235 | } |
| 236 | |
| 237 | void mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, bool enable, |
| 238 | bool prettyForm) { |
| 239 | unwrap(c: flags)->enableDebugInfo(enable, /*prettyForm=*/prettyForm); |
| 240 | } |
| 241 | |
| 242 | void mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags) { |
| 243 | unwrap(c: flags)->printGenericOpForm(); |
| 244 | } |
| 245 | |
| 246 | void mlirOpPrintingFlagsPrintNameLocAsPrefix(MlirOpPrintingFlags flags) { |
| 247 | unwrap(c: flags)->printNameLocAsPrefix(); |
| 248 | } |
| 249 | |
| 250 | void mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags) { |
| 251 | unwrap(c: flags)->useLocalScope(); |
| 252 | } |
| 253 | |
| 254 | void mlirOpPrintingFlagsAssumeVerified(MlirOpPrintingFlags flags) { |
| 255 | unwrap(c: flags)->assumeVerified(); |
| 256 | } |
| 257 | |
| 258 | void mlirOpPrintingFlagsSkipRegions(MlirOpPrintingFlags flags) { |
| 259 | unwrap(c: flags)->skipRegions(); |
| 260 | } |
| 261 | //===----------------------------------------------------------------------===// |
| 262 | // Bytecode printing flags API. |
| 263 | //===----------------------------------------------------------------------===// |
| 264 | |
| 265 | MlirBytecodeWriterConfig mlirBytecodeWriterConfigCreate() { |
| 266 | return wrap(cpp: new BytecodeWriterConfig()); |
| 267 | } |
| 268 | |
| 269 | void mlirBytecodeWriterConfigDestroy(MlirBytecodeWriterConfig config) { |
| 270 | delete unwrap(c: config); |
| 271 | } |
| 272 | |
| 273 | void mlirBytecodeWriterConfigDesiredEmitVersion(MlirBytecodeWriterConfig flags, |
| 274 | int64_t version) { |
| 275 | unwrap(c: flags)->setDesiredBytecodeVersion(version); |
| 276 | } |
| 277 | |
| 278 | //===----------------------------------------------------------------------===// |
| 279 | // Location API. |
| 280 | //===----------------------------------------------------------------------===// |
| 281 | |
| 282 | MlirAttribute mlirLocationGetAttribute(MlirLocation location) { |
| 283 | return wrap(cpp: LocationAttr(unwrap(c: location))); |
| 284 | } |
| 285 | |
| 286 | MlirLocation mlirLocationFromAttribute(MlirAttribute attribute) { |
| 287 | return wrap(cpp: Location(llvm::dyn_cast<LocationAttr>(Val: unwrap(c: attribute)))); |
| 288 | } |
| 289 | |
| 290 | MlirLocation mlirLocationFileLineColGet(MlirContext context, |
| 291 | MlirStringRef filename, unsigned line, |
| 292 | unsigned col) { |
| 293 | return wrap(cpp: Location( |
| 294 | FileLineColLoc::get(context: unwrap(c: context), fileName: unwrap(ref: filename), line, column: col))); |
| 295 | } |
| 296 | |
| 297 | MlirLocation |
| 298 | mlirLocationFileLineColRangeGet(MlirContext context, MlirStringRef filename, |
| 299 | unsigned startLine, unsigned startCol, |
| 300 | unsigned endLine, unsigned endCol) { |
| 301 | return wrap( |
| 302 | cpp: Location(FileLineColRange::get(context: unwrap(c: context), filename: unwrap(ref: filename), |
| 303 | start_line: startLine, start_column: startCol, end_line: endLine, end_column: endCol))); |
| 304 | } |
| 305 | |
| 306 | MlirIdentifier mlirLocationFileLineColRangeGetFilename(MlirLocation location) { |
| 307 | return wrap(cpp: llvm::dyn_cast<FileLineColRange>(Val: unwrap(c: location)).getFilename()); |
| 308 | } |
| 309 | |
| 310 | int mlirLocationFileLineColRangeGetStartLine(MlirLocation location) { |
| 311 | if (auto loc = llvm::dyn_cast<FileLineColRange>(Val: unwrap(c: location))) |
| 312 | return loc.getStartLine(); |
| 313 | return -1; |
| 314 | } |
| 315 | |
| 316 | int mlirLocationFileLineColRangeGetStartColumn(MlirLocation location) { |
| 317 | if (auto loc = llvm::dyn_cast<FileLineColRange>(Val: unwrap(c: location))) |
| 318 | return loc.getStartColumn(); |
| 319 | return -1; |
| 320 | } |
| 321 | |
| 322 | int mlirLocationFileLineColRangeGetEndLine(MlirLocation location) { |
| 323 | if (auto loc = llvm::dyn_cast<FileLineColRange>(Val: unwrap(c: location))) |
| 324 | return loc.getEndLine(); |
| 325 | return -1; |
| 326 | } |
| 327 | |
| 328 | int mlirLocationFileLineColRangeGetEndColumn(MlirLocation location) { |
| 329 | if (auto loc = llvm::dyn_cast<FileLineColRange>(Val: unwrap(c: location))) |
| 330 | return loc.getEndColumn(); |
| 331 | return -1; |
| 332 | } |
| 333 | |
| 334 | MlirTypeID mlirLocationFileLineColRangeGetTypeID() { |
| 335 | return wrap(cpp: FileLineColRange::getTypeID()); |
| 336 | } |
| 337 | |
| 338 | bool mlirLocationIsAFileLineColRange(MlirLocation location) { |
| 339 | return isa<FileLineColRange>(Val: unwrap(c: location)); |
| 340 | } |
| 341 | |
| 342 | MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller) { |
| 343 | return wrap(cpp: Location(CallSiteLoc::get(callee: unwrap(c: callee), caller: unwrap(c: caller)))); |
| 344 | } |
| 345 | |
| 346 | MlirLocation mlirLocationCallSiteGetCallee(MlirLocation location) { |
| 347 | return wrap( |
| 348 | cpp: Location(llvm::dyn_cast<CallSiteLoc>(Val: unwrap(c: location)).getCallee())); |
| 349 | } |
| 350 | |
| 351 | MlirLocation mlirLocationCallSiteGetCaller(MlirLocation location) { |
| 352 | return wrap( |
| 353 | cpp: Location(llvm::dyn_cast<CallSiteLoc>(Val: unwrap(c: location)).getCaller())); |
| 354 | } |
| 355 | |
| 356 | MlirTypeID mlirLocationCallSiteGetTypeID() { |
| 357 | return wrap(cpp: CallSiteLoc::getTypeID()); |
| 358 | } |
| 359 | |
| 360 | bool mlirLocationIsACallSite(MlirLocation location) { |
| 361 | return isa<CallSiteLoc>(Val: unwrap(c: location)); |
| 362 | } |
| 363 | |
| 364 | MlirLocation mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations, |
| 365 | MlirLocation const *locations, |
| 366 | MlirAttribute metadata) { |
| 367 | SmallVector<Location, 4> locs; |
| 368 | ArrayRef<Location> unwrappedLocs = unwrapList(size: nLocations, first: locations, storage&: locs); |
| 369 | return wrap(cpp: FusedLoc::get(locs: unwrappedLocs, metadata: unwrap(c: metadata), context: unwrap(c: ctx))); |
| 370 | } |
| 371 | |
| 372 | unsigned mlirLocationFusedGetNumLocations(MlirLocation location) { |
| 373 | if (auto locationsArrRef = llvm::dyn_cast<FusedLoc>(Val: unwrap(c: location))) |
| 374 | return locationsArrRef.getLocations().size(); |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | void mlirLocationFusedGetLocations(MlirLocation location, |
| 379 | MlirLocation *locationsCPtr) { |
| 380 | if (auto locationsArrRef = llvm::dyn_cast<FusedLoc>(Val: unwrap(c: location))) { |
| 381 | for (auto [i, location] : llvm::enumerate(First: locationsArrRef.getLocations())) |
| 382 | locationsCPtr[i] = wrap(cpp: location); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | MlirAttribute mlirLocationFusedGetMetadata(MlirLocation location) { |
| 387 | return wrap(cpp: llvm::dyn_cast<FusedLoc>(Val: unwrap(c: location)).getMetadata()); |
| 388 | } |
| 389 | |
| 390 | MlirTypeID mlirLocationFusedGetTypeID() { return wrap(cpp: FusedLoc::getTypeID()); } |
| 391 | |
| 392 | bool mlirLocationIsAFused(MlirLocation location) { |
| 393 | return isa<FusedLoc>(Val: unwrap(c: location)); |
| 394 | } |
| 395 | |
| 396 | MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name, |
| 397 | MlirLocation childLoc) { |
| 398 | if (mlirLocationIsNull(location: childLoc)) |
| 399 | return wrap( |
| 400 | cpp: Location(NameLoc::get(name: StringAttr::get(context: unwrap(c: context), bytes: unwrap(ref: name))))); |
| 401 | return wrap(cpp: Location(NameLoc::get( |
| 402 | name: StringAttr::get(context: unwrap(c: context), bytes: unwrap(ref: name)), childLoc: unwrap(c: childLoc)))); |
| 403 | } |
| 404 | |
| 405 | MlirIdentifier mlirLocationNameGetName(MlirLocation location) { |
| 406 | return wrap(cpp: (llvm::dyn_cast<NameLoc>(Val: unwrap(c: location)).getName())); |
| 407 | } |
| 408 | |
| 409 | MlirLocation mlirLocationNameGetChildLoc(MlirLocation location) { |
| 410 | return wrap( |
| 411 | cpp: Location(llvm::dyn_cast<NameLoc>(Val: unwrap(c: location)).getChildLoc())); |
| 412 | } |
| 413 | |
| 414 | MlirTypeID mlirLocationNameGetTypeID() { return wrap(cpp: NameLoc::getTypeID()); } |
| 415 | |
| 416 | bool mlirLocationIsAName(MlirLocation location) { |
| 417 | return isa<NameLoc>(Val: unwrap(c: location)); |
| 418 | } |
| 419 | |
| 420 | MlirLocation mlirLocationUnknownGet(MlirContext context) { |
| 421 | return wrap(cpp: Location(UnknownLoc::get(context: unwrap(c: context)))); |
| 422 | } |
| 423 | |
| 424 | MlirTypeID mlirLocationUnknownGetTypeID() { |
| 425 | return wrap(cpp: UnknownLoc::getTypeID()); |
| 426 | } |
| 427 | |
| 428 | bool mlirLocationIsAUnknown(MlirLocation location) { |
| 429 | return isa<UnknownLoc>(Val: unwrap(c: location)); |
| 430 | } |
| 431 | |
| 432 | bool mlirLocationEqual(MlirLocation l1, MlirLocation l2) { |
| 433 | return unwrap(c: l1) == unwrap(c: l2); |
| 434 | } |
| 435 | |
| 436 | MlirContext mlirLocationGetContext(MlirLocation location) { |
| 437 | return wrap(cpp: unwrap(c: location).getContext()); |
| 438 | } |
| 439 | |
| 440 | void mlirLocationPrint(MlirLocation location, MlirStringCallback callback, |
| 441 | void *userData) { |
| 442 | detail::CallbackOstream stream(callback, userData); |
| 443 | unwrap(c: location).print(os&: stream); |
| 444 | } |
| 445 | |
| 446 | //===----------------------------------------------------------------------===// |
| 447 | // Module API. |
| 448 | //===----------------------------------------------------------------------===// |
| 449 | |
| 450 | MlirModule mlirModuleCreateEmpty(MlirLocation location) { |
| 451 | return wrap(cpp: ModuleOp::create(loc: unwrap(c: location))); |
| 452 | } |
| 453 | |
| 454 | MlirModule mlirModuleCreateParse(MlirContext context, MlirStringRef module) { |
| 455 | OwningOpRef<ModuleOp> owning = |
| 456 | parseSourceString<ModuleOp>(sourceStr: unwrap(ref: module), config: unwrap(c: context)); |
| 457 | if (!owning) |
| 458 | return MlirModule{.ptr: nullptr}; |
| 459 | return MlirModule{.ptr: owning.release().getOperation()}; |
| 460 | } |
| 461 | |
| 462 | MlirModule mlirModuleCreateParseFromFile(MlirContext context, |
| 463 | MlirStringRef fileName) { |
| 464 | OwningOpRef<ModuleOp> owning = |
| 465 | parseSourceFile<ModuleOp>(filename: unwrap(ref: fileName), config: unwrap(c: context)); |
| 466 | if (!owning) |
| 467 | return MlirModule{.ptr: nullptr}; |
| 468 | return MlirModule{.ptr: owning.release().getOperation()}; |
| 469 | } |
| 470 | |
| 471 | MlirContext mlirModuleGetContext(MlirModule module) { |
| 472 | return wrap(cpp: unwrap(c: module).getContext()); |
| 473 | } |
| 474 | |
| 475 | MlirBlock mlirModuleGetBody(MlirModule module) { |
| 476 | return wrap(cpp: unwrap(c: module).getBody()); |
| 477 | } |
| 478 | |
| 479 | void mlirModuleDestroy(MlirModule module) { |
| 480 | // Transfer ownership to an OwningOpRef<ModuleOp> so that its destructor is |
| 481 | // called. |
| 482 | OwningOpRef<ModuleOp>(unwrap(c: module)); |
| 483 | } |
| 484 | |
| 485 | MlirOperation mlirModuleGetOperation(MlirModule module) { |
| 486 | return wrap(cpp: unwrap(c: module).getOperation()); |
| 487 | } |
| 488 | |
| 489 | MlirModule mlirModuleFromOperation(MlirOperation op) { |
| 490 | return wrap(cpp: dyn_cast<ModuleOp>(Val: unwrap(c: op))); |
| 491 | } |
| 492 | |
| 493 | bool mlirModuleEqual(MlirModule lhs, MlirModule rhs) { |
| 494 | return unwrap(c: lhs) == unwrap(c: rhs); |
| 495 | } |
| 496 | |
| 497 | size_t mlirModuleHashValue(MlirModule mod) { |
| 498 | return OperationEquivalence::computeHash(op: unwrap(c: mod).getOperation()); |
| 499 | } |
| 500 | |
| 501 | //===----------------------------------------------------------------------===// |
| 502 | // Operation state API. |
| 503 | //===----------------------------------------------------------------------===// |
| 504 | |
| 505 | MlirOperationState mlirOperationStateGet(MlirStringRef name, MlirLocation loc) { |
| 506 | MlirOperationState state; |
| 507 | state.name = name; |
| 508 | state.location = loc; |
| 509 | state.nResults = 0; |
| 510 | state.results = nullptr; |
| 511 | state.nOperands = 0; |
| 512 | state.operands = nullptr; |
| 513 | state.nRegions = 0; |
| 514 | state.regions = nullptr; |
| 515 | state.nSuccessors = 0; |
| 516 | state.successors = nullptr; |
| 517 | state.nAttributes = 0; |
| 518 | state.attributes = nullptr; |
| 519 | state.enableResultTypeInference = false; |
| 520 | return state; |
| 521 | } |
| 522 | |
| 523 | #define APPEND_ELEMS(type, sizeName, elemName) \ |
| 524 | state->elemName = \ |
| 525 | (type *)realloc(state->elemName, (state->sizeName + n) * sizeof(type)); \ |
| 526 | memcpy(state->elemName + state->sizeName, elemName, n * sizeof(type)); \ |
| 527 | state->sizeName += n; |
| 528 | |
| 529 | void mlirOperationStateAddResults(MlirOperationState *state, intptr_t n, |
| 530 | MlirType const *results) { |
| 531 | APPEND_ELEMS(MlirType, nResults, results); |
| 532 | } |
| 533 | |
| 534 | void mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n, |
| 535 | MlirValue const *operands) { |
| 536 | APPEND_ELEMS(MlirValue, nOperands, operands); |
| 537 | } |
| 538 | void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n, |
| 539 | MlirRegion const *regions) { |
| 540 | APPEND_ELEMS(MlirRegion, nRegions, regions); |
| 541 | } |
| 542 | void mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n, |
| 543 | MlirBlock const *successors) { |
| 544 | APPEND_ELEMS(MlirBlock, nSuccessors, successors); |
| 545 | } |
| 546 | void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n, |
| 547 | MlirNamedAttribute const *attributes) { |
| 548 | APPEND_ELEMS(MlirNamedAttribute, nAttributes, attributes); |
| 549 | } |
| 550 | |
| 551 | void mlirOperationStateEnableResultTypeInference(MlirOperationState *state) { |
| 552 | state->enableResultTypeInference = true; |
| 553 | } |
| 554 | |
| 555 | //===----------------------------------------------------------------------===// |
| 556 | // Operation API. |
| 557 | //===----------------------------------------------------------------------===// |
| 558 | |
| 559 | static LogicalResult inferOperationTypes(OperationState &state) { |
| 560 | MLIRContext *context = state.getContext(); |
| 561 | std::optional<RegisteredOperationName> info = state.name.getRegisteredInfo(); |
| 562 | if (!info) { |
| 563 | emitError(loc: state.location) |
| 564 | << "type inference was requested for the operation " << state.name |
| 565 | << ", but the operation was not registered; ensure that the dialect " |
| 566 | "containing the operation is linked into MLIR and registered with " |
| 567 | "the context" ; |
| 568 | return failure(); |
| 569 | } |
| 570 | |
| 571 | auto *inferInterface = info->getInterface<InferTypeOpInterface>(); |
| 572 | if (!inferInterface) { |
| 573 | emitError(loc: state.location) |
| 574 | << "type inference was requested for the operation " << state.name |
| 575 | << ", but the operation does not support type inference; result " |
| 576 | "types must be specified explicitly" ; |
| 577 | return failure(); |
| 578 | } |
| 579 | |
| 580 | DictionaryAttr attributes = state.attributes.getDictionary(context); |
| 581 | PropertyRef properties = state.getRawProperties(); |
| 582 | |
| 583 | if (!properties && info->getOpPropertyByteSize() > 0 && !attributes.empty()) { |
| 584 | auto propAlloc = std::make_unique<char[]>(num: info->getOpPropertyByteSize()); |
| 585 | properties = PropertyRef(info->getOpPropertiesTypeID(), propAlloc.get()); |
| 586 | if (properties) { |
| 587 | auto emitError = [&]() { |
| 588 | return mlir::emitError(loc: state.location) |
| 589 | << " failed properties conversion while building " |
| 590 | << state.name.getStringRef() << " with `" << attributes << "`: " ; |
| 591 | }; |
| 592 | if (failed(Result: info->setOpPropertiesFromAttribute(opName: state.name, properties, |
| 593 | attr: attributes, emitError))) |
| 594 | return failure(); |
| 595 | } |
| 596 | if (succeeded(Result: inferInterface->inferReturnTypes( |
| 597 | context, state.location, state.operands, attributes, properties, |
| 598 | state.regions, state.types))) { |
| 599 | return success(); |
| 600 | } |
| 601 | // Diagnostic emitted by interface. |
| 602 | return failure(); |
| 603 | } |
| 604 | |
| 605 | if (succeeded(Result: inferInterface->inferReturnTypes( |
| 606 | context, state.location, state.operands, attributes, properties, |
| 607 | state.regions, state.types))) |
| 608 | return success(); |
| 609 | |
| 610 | // Diagnostic emitted by interface. |
| 611 | return failure(); |
| 612 | } |
| 613 | |
| 614 | MlirOperation mlirOperationCreate(MlirOperationState *state) { |
| 615 | assert(state); |
| 616 | OperationState cppState(unwrap(c: state->location), unwrap(ref: state->name)); |
| 617 | SmallVector<Type, 4> resultStorage; |
| 618 | SmallVector<Value, 8> operandStorage; |
| 619 | SmallVector<Block *, 2> successorStorage; |
| 620 | cppState.addTypes(newTypes: unwrapList(size: state->nResults, first: state->results, storage&: resultStorage)); |
| 621 | cppState.addOperands( |
| 622 | newOperands: unwrapList(size: state->nOperands, first: state->operands, storage&: operandStorage)); |
| 623 | cppState.addSuccessors( |
| 624 | newSuccessors: unwrapList(size: state->nSuccessors, first: state->successors, storage&: successorStorage)); |
| 625 | |
| 626 | cppState.attributes.reserve(N: state->nAttributes); |
| 627 | for (intptr_t i = 0; i < state->nAttributes; ++i) |
| 628 | cppState.addAttribute(name: unwrap(c: state->attributes[i].name), |
| 629 | attr: unwrap(c: state->attributes[i].attribute)); |
| 630 | |
| 631 | for (intptr_t i = 0; i < state->nRegions; ++i) |
| 632 | cppState.addRegion(region: std::unique_ptr<Region>(unwrap(c: state->regions[i]))); |
| 633 | |
| 634 | free(ptr: state->results); |
| 635 | free(ptr: state->operands); |
| 636 | free(ptr: state->successors); |
| 637 | free(ptr: state->regions); |
| 638 | free(ptr: state->attributes); |
| 639 | |
| 640 | // Infer result types. |
| 641 | if (state->enableResultTypeInference) { |
| 642 | assert(cppState.types.empty() && |
| 643 | "result type inference enabled and result types provided" ); |
| 644 | if (failed(Result: inferOperationTypes(state&: cppState))) |
| 645 | return {.ptr: nullptr}; |
| 646 | } |
| 647 | |
| 648 | return wrap(cpp: Operation::create(state: cppState)); |
| 649 | } |
| 650 | |
| 651 | MlirOperation mlirOperationCreateParse(MlirContext context, |
| 652 | MlirStringRef sourceStr, |
| 653 | MlirStringRef sourceName) { |
| 654 | |
| 655 | return wrap( |
| 656 | cpp: parseSourceString(sourceStr: unwrap(ref: sourceStr), config: unwrap(c: context), sourceName: unwrap(ref: sourceName)) |
| 657 | .release()); |
| 658 | } |
| 659 | |
| 660 | MlirOperation mlirOperationClone(MlirOperation op) { |
| 661 | return wrap(cpp: unwrap(c: op)->clone()); |
| 662 | } |
| 663 | |
| 664 | void mlirOperationDestroy(MlirOperation op) { unwrap(c: op)->erase(); } |
| 665 | |
| 666 | void mlirOperationRemoveFromParent(MlirOperation op) { unwrap(c: op)->remove(); } |
| 667 | |
| 668 | bool mlirOperationEqual(MlirOperation op, MlirOperation other) { |
| 669 | return unwrap(c: op) == unwrap(c: other); |
| 670 | } |
| 671 | |
| 672 | size_t mlirOperationHashValue(MlirOperation op) { |
| 673 | return OperationEquivalence::computeHash(op: unwrap(c: op)); |
| 674 | } |
| 675 | |
| 676 | /// Translates the C equivalence flags to mlir::OperationEquivalence::Flags. The |
| 677 | /// enumerator values mirror each other. |
| 678 | static OperationEquivalence::Flags unwrapEquivalenceFlags(uint32_t flags) { |
| 679 | return static_cast<OperationEquivalence::Flags>(flags); |
| 680 | } |
| 681 | |
| 682 | bool mlirOperationIsStructurallyEquivalent(MlirOperation lhs, MlirOperation rhs, |
| 683 | uint32_t flags) { |
| 684 | return OperationEquivalence::isEquivalentTo(lhs: unwrap(c: lhs), rhs: unwrap(c: rhs), |
| 685 | flags: unwrapEquivalenceFlags(flags)); |
| 686 | } |
| 687 | |
| 688 | size_t mlirOperationStructuralHashValue(MlirOperation op, uint32_t flags) { |
| 689 | return OperationEquivalence::computeHash( |
| 690 | op: unwrap(c: op), /*hashOperands=*/OperationEquivalence::directHashValue, |
| 691 | /*hashResults=*/OperationEquivalence::ignoreHashValue, |
| 692 | flags: unwrapEquivalenceFlags(flags)); |
| 693 | } |
| 694 | |
| 695 | MlirContext mlirOperationGetContext(MlirOperation op) { |
| 696 | return wrap(cpp: unwrap(c: op)->getContext()); |
| 697 | } |
| 698 | |
| 699 | bool mlirOperationNameHasTrait(MlirStringRef opName, MlirTypeID traitTypeID, |
| 700 | MlirContext context) { |
| 701 | return OperationName(unwrap(ref: opName), unwrap(c: context)) |
| 702 | .hasTrait(traitID: unwrap(c: traitTypeID)); |
| 703 | } |
| 704 | |
| 705 | MlirLocation mlirOperationGetLocation(MlirOperation op) { |
| 706 | return wrap(cpp: unwrap(c: op)->getLoc()); |
| 707 | } |
| 708 | |
| 709 | void mlirOperationSetLocation(MlirOperation op, MlirLocation loc) { |
| 710 | unwrap(c: op)->setLoc(unwrap(c: loc)); |
| 711 | } |
| 712 | |
| 713 | MlirTypeID mlirOperationGetTypeID(MlirOperation op) { |
| 714 | if (auto info = unwrap(c: op)->getRegisteredInfo()) |
| 715 | return wrap(cpp: info->getTypeID()); |
| 716 | return {.ptr: nullptr}; |
| 717 | } |
| 718 | |
| 719 | MlirIdentifier mlirOperationGetName(MlirOperation op) { |
| 720 | return wrap(cpp: unwrap(c: op)->getName().getIdentifier()); |
| 721 | } |
| 722 | |
| 723 | MlirBlock mlirOperationGetBlock(MlirOperation op) { |
| 724 | return wrap(cpp: unwrap(c: op)->getBlock()); |
| 725 | } |
| 726 | |
| 727 | MlirOperation mlirOperationGetParentOperation(MlirOperation op) { |
| 728 | return wrap(cpp: unwrap(c: op)->getParentOp()); |
| 729 | } |
| 730 | |
| 731 | bool mlirOperationIsAncestor(MlirOperation a, MlirOperation b) { |
| 732 | return unwrap(c: a)->isAncestor(other: unwrap(c: b)); |
| 733 | } |
| 734 | |
| 735 | bool mlirOperationIsProperAncestor(MlirOperation a, MlirOperation b) { |
| 736 | return unwrap(c: a)->isProperAncestor(other: unwrap(c: b)); |
| 737 | } |
| 738 | |
| 739 | intptr_t mlirOperationGetNumRegions(MlirOperation op) { |
| 740 | return static_cast<intptr_t>(unwrap(c: op)->getNumRegions()); |
| 741 | } |
| 742 | |
| 743 | MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos) { |
| 744 | return wrap(cpp: &unwrap(c: op)->getRegion(index: static_cast<unsigned>(pos))); |
| 745 | } |
| 746 | |
| 747 | MlirRegion mlirOperationGetFirstRegion(MlirOperation op) { |
| 748 | Operation *cppOp = unwrap(c: op); |
| 749 | if (cppOp->getNumRegions() == 0) |
| 750 | return wrap(cpp: static_cast<Region *>(nullptr)); |
| 751 | return wrap(cpp: &cppOp->getRegion(index: 0)); |
| 752 | } |
| 753 | |
| 754 | MlirRegion mlirRegionGetNextInOperation(MlirRegion region) { |
| 755 | Region *cppRegion = unwrap(c: region); |
| 756 | Operation *parent = cppRegion->getParentOp(); |
| 757 | intptr_t next = cppRegion->getRegionNumber() + 1; |
| 758 | if (parent->getNumRegions() > next) |
| 759 | return wrap(cpp: &parent->getRegion(index: next)); |
| 760 | return wrap(cpp: static_cast<Region *>(nullptr)); |
| 761 | } |
| 762 | |
| 763 | MlirOperation mlirOperationGetNextInBlock(MlirOperation op) { |
| 764 | return wrap(cpp: unwrap(c: op)->getNextNode()); |
| 765 | } |
| 766 | |
| 767 | intptr_t mlirOperationGetNumOperands(MlirOperation op) { |
| 768 | return static_cast<intptr_t>(unwrap(c: op)->getNumOperands()); |
| 769 | } |
| 770 | |
| 771 | MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos) { |
| 772 | return wrap(cpp: unwrap(c: op)->getOperand(idx: static_cast<unsigned>(pos))); |
| 773 | } |
| 774 | |
| 775 | MlirOpOperand mlirOperationGetOpOperand(MlirOperation op, intptr_t pos) { |
| 776 | return wrap(cpp: &unwrap(c: op)->getOpOperand(idx: static_cast<unsigned>(pos))); |
| 777 | } |
| 778 | |
| 779 | void mlirOperationSetOperand(MlirOperation op, intptr_t pos, |
| 780 | MlirValue newValue) { |
| 781 | unwrap(c: op)->setOperand(idx: static_cast<unsigned>(pos), value: unwrap(c: newValue)); |
| 782 | } |
| 783 | |
| 784 | void mlirOperationSetOperands(MlirOperation op, intptr_t nOperands, |
| 785 | MlirValue const *operands) { |
| 786 | SmallVector<Value> ops; |
| 787 | unwrap(c: op)->setOperands(unwrapList(size: nOperands, first: operands, storage&: ops)); |
| 788 | } |
| 789 | |
| 790 | intptr_t mlirOperationGetNumResults(MlirOperation op) { |
| 791 | return static_cast<intptr_t>(unwrap(c: op)->getNumResults()); |
| 792 | } |
| 793 | |
| 794 | MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos) { |
| 795 | return wrap(cpp: unwrap(c: op)->getResult(idx: static_cast<unsigned>(pos))); |
| 796 | } |
| 797 | |
| 798 | intptr_t mlirOperationGetNumSuccessors(MlirOperation op) { |
| 799 | return static_cast<intptr_t>(unwrap(c: op)->getNumSuccessors()); |
| 800 | } |
| 801 | |
| 802 | MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos) { |
| 803 | return wrap(cpp: unwrap(c: op)->getSuccessor(index: static_cast<unsigned>(pos))); |
| 804 | } |
| 805 | |
| 806 | MLIR_CAPI_EXPORTED bool |
| 807 | mlirOperationHasInherentAttributeByName(MlirOperation op, MlirStringRef name) { |
| 808 | std::optional<Attribute> attr = unwrap(c: op)->getInherentAttr(name: unwrap(ref: name)); |
| 809 | return attr.has_value(); |
| 810 | } |
| 811 | |
| 812 | MlirAttribute mlirOperationGetInherentAttributeByName(MlirOperation op, |
| 813 | MlirStringRef name) { |
| 814 | std::optional<Attribute> attr = unwrap(c: op)->getInherentAttr(name: unwrap(ref: name)); |
| 815 | if (attr.has_value()) |
| 816 | return wrap(cpp: *attr); |
| 817 | return {}; |
| 818 | } |
| 819 | |
| 820 | void mlirOperationSetInherentAttributeByName(MlirOperation op, |
| 821 | MlirStringRef name, |
| 822 | MlirAttribute attr) { |
| 823 | unwrap(c: op)->setInherentAttr( |
| 824 | name: StringAttr::get(context: unwrap(c: op)->getContext(), bytes: unwrap(ref: name)), value: unwrap(c: attr)); |
| 825 | } |
| 826 | |
| 827 | intptr_t mlirOperationGetNumDiscardableAttributes(MlirOperation op) { |
| 828 | return static_cast<intptr_t>( |
| 829 | llvm::range_size(Range: unwrap(c: op)->getDiscardableAttrDictionary().getValue())); |
| 830 | } |
| 831 | |
| 832 | MlirNamedAttribute mlirOperationGetDiscardableAttribute(MlirOperation op, |
| 833 | intptr_t pos) { |
| 834 | NamedAttribute attr = *std::next( |
| 835 | x: unwrap(c: op)->getDiscardableAttrDictionary().getValue().begin(), n: pos); |
| 836 | return MlirNamedAttribute{.name: wrap(cpp: attr.getName()), .attribute: wrap(cpp: attr.getValue())}; |
| 837 | } |
| 838 | |
| 839 | MlirAttribute mlirOperationGetDiscardableAttributeByName(MlirOperation op, |
| 840 | MlirStringRef name) { |
| 841 | return wrap(cpp: unwrap(c: op)->getDiscardableAttr(name: unwrap(ref: name))); |
| 842 | } |
| 843 | |
| 844 | void mlirOperationSetDiscardableAttributeByName(MlirOperation op, |
| 845 | MlirStringRef name, |
| 846 | MlirAttribute attr) { |
| 847 | unwrap(c: op)->setDiscardableAttr(name: unwrap(ref: name), value: unwrap(c: attr)); |
| 848 | } |
| 849 | |
| 850 | bool mlirOperationRemoveDiscardableAttributeByName(MlirOperation op, |
| 851 | MlirStringRef name) { |
| 852 | return !!unwrap(c: op)->removeDiscardableAttr(name: unwrap(ref: name)); |
| 853 | } |
| 854 | |
| 855 | void mlirOperationSetSuccessor(MlirOperation op, intptr_t pos, |
| 856 | MlirBlock block) { |
| 857 | unwrap(c: op)->setSuccessor(block: unwrap(c: block), index: static_cast<unsigned>(pos)); |
| 858 | } |
| 859 | |
| 860 | intptr_t mlirOperationGetNumAttributes(MlirOperation op) { |
| 861 | return static_cast<intptr_t>(unwrap(c: op)->getAttrs().size()); |
| 862 | } |
| 863 | |
| 864 | MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos) { |
| 865 | NamedAttribute attr = unwrap(c: op)->getAttrs()[pos]; |
| 866 | return MlirNamedAttribute{.name: wrap(cpp: attr.getName()), .attribute: wrap(cpp: attr.getValue())}; |
| 867 | } |
| 868 | |
| 869 | MlirAttribute mlirOperationGetAttributeByName(MlirOperation op, |
| 870 | MlirStringRef name) { |
| 871 | return wrap(cpp: unwrap(c: op)->getAttr(name: unwrap(ref: name))); |
| 872 | } |
| 873 | |
| 874 | void mlirOperationSetAttributeByName(MlirOperation op, MlirStringRef name, |
| 875 | MlirAttribute attr) { |
| 876 | unwrap(c: op)->setAttr(name: unwrap(ref: name), value: unwrap(c: attr)); |
| 877 | } |
| 878 | |
| 879 | bool mlirOperationRemoveAttributeByName(MlirOperation op, MlirStringRef name) { |
| 880 | return !!unwrap(c: op)->removeAttr(name: unwrap(ref: name)); |
| 881 | } |
| 882 | |
| 883 | void mlirOperationPrint(MlirOperation op, MlirStringCallback callback, |
| 884 | void *userData) { |
| 885 | detail::CallbackOstream stream(callback, userData); |
| 886 | unwrap(c: op)->print(os&: stream); |
| 887 | } |
| 888 | |
| 889 | void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags, |
| 890 | MlirStringCallback callback, void *userData) { |
| 891 | detail::CallbackOstream stream(callback, userData); |
| 892 | unwrap(c: op)->print(os&: stream, flags: *unwrap(c: flags)); |
| 893 | } |
| 894 | |
| 895 | void mlirOperationPrintWithState(MlirOperation op, MlirAsmState state, |
| 896 | MlirStringCallback callback, void *userData) { |
| 897 | detail::CallbackOstream stream(callback, userData); |
| 898 | if (state.ptr) |
| 899 | unwrap(c: op)->print(os&: stream, state&: *unwrap(c: state)); |
| 900 | else |
| 901 | unwrap(c: op)->print(os&: stream); |
| 902 | } |
| 903 | |
| 904 | void mlirOperationWriteBytecode(MlirOperation op, MlirStringCallback callback, |
| 905 | void *userData) { |
| 906 | detail::CallbackOstream stream(callback, userData); |
| 907 | // As no desired version is set, no failure can occur. |
| 908 | (void)writeBytecodeToFile(op: unwrap(c: op), os&: stream); |
| 909 | } |
| 910 | |
| 911 | MlirLogicalResult mlirOperationWriteBytecodeWithConfig( |
| 912 | MlirOperation op, MlirBytecodeWriterConfig config, |
| 913 | MlirStringCallback callback, void *userData) { |
| 914 | detail::CallbackOstream stream(callback, userData); |
| 915 | return wrap(res: writeBytecodeToFile(op: unwrap(c: op), os&: stream, config: *unwrap(c: config))); |
| 916 | } |
| 917 | |
| 918 | void mlirOperationDump(MlirOperation op) { |
| 919 | unwrap(c: op)->print(os&: llvm::errs(), flags: OpPrintingFlags().useLocalScope()); |
| 920 | llvm::errs() << "\n" ; |
| 921 | } |
| 922 | |
| 923 | bool mlirOperationVerify(MlirOperation op) { |
| 924 | return succeeded(Result: verify(op: unwrap(c: op))); |
| 925 | } |
| 926 | |
| 927 | void mlirOperationMoveAfter(MlirOperation op, MlirOperation other) { |
| 928 | return unwrap(c: op)->moveAfter(existingOp: unwrap(c: other)); |
| 929 | } |
| 930 | |
| 931 | void mlirOperationMoveBefore(MlirOperation op, MlirOperation other) { |
| 932 | return unwrap(c: op)->moveBefore(existingOp: unwrap(c: other)); |
| 933 | } |
| 934 | |
| 935 | bool mlirOperationIsBeforeInBlock(MlirOperation op, MlirOperation other) { |
| 936 | return unwrap(c: op)->isBeforeInBlock(other: unwrap(c: other)); |
| 937 | } |
| 938 | |
| 939 | static mlir::WalkResult unwrap(MlirWalkResult result) { |
| 940 | switch (result) { |
| 941 | case MlirWalkResultAdvance: |
| 942 | return mlir::WalkResult::advance(); |
| 943 | |
| 944 | case MlirWalkResultInterrupt: |
| 945 | return mlir::WalkResult::interrupt(); |
| 946 | |
| 947 | case MlirWalkResultSkip: |
| 948 | return mlir::WalkResult::skip(); |
| 949 | } |
| 950 | llvm_unreachable("unknown result in WalkResult::unwrap" ); |
| 951 | } |
| 952 | |
| 953 | void mlirOperationWalk(MlirOperation op, MlirOperationWalkCallback callback, |
| 954 | void *userData, MlirWalkOrder walkOrder) { |
| 955 | switch (walkOrder) { |
| 956 | |
| 957 | case MlirWalkPreOrder: |
| 958 | unwrap(c: op)->walk<mlir::WalkOrder::PreOrder>( |
| 959 | callback: [callback, userData](Operation *op) { |
| 960 | return unwrap(result: callback(wrap(cpp: op), userData)); |
| 961 | }); |
| 962 | break; |
| 963 | case MlirWalkPostOrder: |
| 964 | unwrap(c: op)->walk<mlir::WalkOrder::PostOrder>( |
| 965 | callback: [callback, userData](Operation *op) { |
| 966 | return unwrap(result: callback(wrap(cpp: op), userData)); |
| 967 | }); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | void mlirOperationReplaceUsesOfWith(MlirOperation op, MlirValue oldValue, |
| 972 | MlirValue newValue) { |
| 973 | unwrap(c: op)->replaceUsesOfWith(from: unwrap(c: oldValue), to: unwrap(c: newValue)); |
| 974 | } |
| 975 | |
| 976 | //===----------------------------------------------------------------------===// |
| 977 | // Region API. |
| 978 | //===----------------------------------------------------------------------===// |
| 979 | |
| 980 | MlirRegion mlirRegionCreate() { return wrap(cpp: new Region); } |
| 981 | |
| 982 | bool mlirRegionEqual(MlirRegion region, MlirRegion other) { |
| 983 | return unwrap(c: region) == unwrap(c: other); |
| 984 | } |
| 985 | |
| 986 | MlirBlock mlirRegionGetFirstBlock(MlirRegion region) { |
| 987 | Region *cppRegion = unwrap(c: region); |
| 988 | if (cppRegion->empty()) |
| 989 | return wrap(cpp: static_cast<Block *>(nullptr)); |
| 990 | return wrap(cpp: &cppRegion->front()); |
| 991 | } |
| 992 | |
| 993 | void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block) { |
| 994 | unwrap(c: region)->push_back(block: unwrap(c: block)); |
| 995 | } |
| 996 | |
| 997 | void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos, |
| 998 | MlirBlock block) { |
| 999 | auto &blockList = unwrap(c: region)->getBlocks(); |
| 1000 | blockList.insert(where: std::next(x: blockList.begin(), n: pos), New: unwrap(c: block)); |
| 1001 | } |
| 1002 | |
| 1003 | void mlirRegionInsertOwnedBlockAfter(MlirRegion region, MlirBlock reference, |
| 1004 | MlirBlock block) { |
| 1005 | Region *cppRegion = unwrap(c: region); |
| 1006 | if (mlirBlockIsNull(block: reference)) { |
| 1007 | cppRegion->getBlocks().insert(where: cppRegion->begin(), New: unwrap(c: block)); |
| 1008 | return; |
| 1009 | } |
| 1010 | |
| 1011 | assert(unwrap(reference)->getParent() == unwrap(region) && |
| 1012 | "expected reference block to belong to the region" ); |
| 1013 | cppRegion->getBlocks().insertAfter(where: Region::iterator(unwrap(c: reference)), |
| 1014 | New: unwrap(c: block)); |
| 1015 | } |
| 1016 | |
| 1017 | void mlirRegionInsertOwnedBlockBefore(MlirRegion region, MlirBlock reference, |
| 1018 | MlirBlock block) { |
| 1019 | if (mlirBlockIsNull(block: reference)) |
| 1020 | return mlirRegionAppendOwnedBlock(region, block); |
| 1021 | |
| 1022 | assert(unwrap(reference)->getParent() == unwrap(region) && |
| 1023 | "expected reference block to belong to the region" ); |
| 1024 | unwrap(c: region)->getBlocks().insert(where: Region::iterator(unwrap(c: reference)), |
| 1025 | New: unwrap(c: block)); |
| 1026 | } |
| 1027 | |
| 1028 | void mlirRegionDestroy(MlirRegion region) { |
| 1029 | delete static_cast<Region *>(region.ptr); |
| 1030 | } |
| 1031 | |
| 1032 | void mlirRegionTakeBody(MlirRegion target, MlirRegion source) { |
| 1033 | unwrap(c: target)->takeBody(other&: *unwrap(c: source)); |
| 1034 | } |
| 1035 | |
| 1036 | //===----------------------------------------------------------------------===// |
| 1037 | // Block API. |
| 1038 | //===----------------------------------------------------------------------===// |
| 1039 | |
| 1040 | MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args, |
| 1041 | MlirLocation const *locs) { |
| 1042 | Block *b = new Block; |
| 1043 | for (intptr_t i = 0; i < nArgs; ++i) |
| 1044 | b->addArgument(type: unwrap(c: args[i]), loc: unwrap(c: locs[i])); |
| 1045 | return wrap(cpp: b); |
| 1046 | } |
| 1047 | |
| 1048 | bool mlirBlockEqual(MlirBlock block, MlirBlock other) { |
| 1049 | return unwrap(c: block) == unwrap(c: other); |
| 1050 | } |
| 1051 | |
| 1052 | MlirOperation mlirBlockGetParentOperation(MlirBlock block) { |
| 1053 | return wrap(cpp: unwrap(c: block)->getParentOp()); |
| 1054 | } |
| 1055 | |
| 1056 | MlirRegion mlirBlockGetParentRegion(MlirBlock block) { |
| 1057 | return wrap(cpp: unwrap(c: block)->getParent()); |
| 1058 | } |
| 1059 | |
| 1060 | MlirBlock mlirBlockGetNextInRegion(MlirBlock block) { |
| 1061 | return wrap(cpp: unwrap(c: block)->getNextNode()); |
| 1062 | } |
| 1063 | |
| 1064 | MlirOperation mlirBlockGetFirstOperation(MlirBlock block) { |
| 1065 | Block *cppBlock = unwrap(c: block); |
| 1066 | if (cppBlock->empty()) |
| 1067 | return wrap(cpp: static_cast<Operation *>(nullptr)); |
| 1068 | return wrap(cpp: &cppBlock->front()); |
| 1069 | } |
| 1070 | |
| 1071 | MlirOperation mlirBlockGetTerminator(MlirBlock block) { |
| 1072 | Block *cppBlock = unwrap(c: block); |
| 1073 | if (cppBlock->empty()) |
| 1074 | return wrap(cpp: static_cast<Operation *>(nullptr)); |
| 1075 | Operation &back = cppBlock->back(); |
| 1076 | if (!back.hasTrait<OpTrait::IsTerminator>()) |
| 1077 | return wrap(cpp: static_cast<Operation *>(nullptr)); |
| 1078 | return wrap(cpp: &back); |
| 1079 | } |
| 1080 | |
| 1081 | void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation) { |
| 1082 | unwrap(c: block)->push_back(op: unwrap(c: operation)); |
| 1083 | } |
| 1084 | |
| 1085 | void mlirBlockInsertOwnedOperation(MlirBlock block, intptr_t pos, |
| 1086 | MlirOperation operation) { |
| 1087 | auto &opList = unwrap(c: block)->getOperations(); |
| 1088 | opList.insert(where: std::next(x: opList.begin(), n: pos), New: unwrap(c: operation)); |
| 1089 | } |
| 1090 | |
| 1091 | void mlirBlockInsertOwnedOperationAfter(MlirBlock block, |
| 1092 | MlirOperation reference, |
| 1093 | MlirOperation operation) { |
| 1094 | Block *cppBlock = unwrap(c: block); |
| 1095 | if (mlirOperationIsNull(op: reference)) { |
| 1096 | cppBlock->getOperations().insert(where: cppBlock->begin(), New: unwrap(c: operation)); |
| 1097 | return; |
| 1098 | } |
| 1099 | |
| 1100 | assert(unwrap(reference)->getBlock() == unwrap(block) && |
| 1101 | "expected reference operation to belong to the block" ); |
| 1102 | cppBlock->getOperations().insertAfter(where: Block::iterator(unwrap(c: reference)), |
| 1103 | New: unwrap(c: operation)); |
| 1104 | } |
| 1105 | |
| 1106 | void mlirBlockInsertOwnedOperationBefore(MlirBlock block, |
| 1107 | MlirOperation reference, |
| 1108 | MlirOperation operation) { |
| 1109 | if (mlirOperationIsNull(op: reference)) |
| 1110 | return mlirBlockAppendOwnedOperation(block, operation); |
| 1111 | |
| 1112 | assert(unwrap(reference)->getBlock() == unwrap(block) && |
| 1113 | "expected reference operation to belong to the block" ); |
| 1114 | unwrap(c: block)->getOperations().insert(where: Block::iterator(unwrap(c: reference)), |
| 1115 | New: unwrap(c: operation)); |
| 1116 | } |
| 1117 | |
| 1118 | void mlirBlockDestroy(MlirBlock block) { delete unwrap(c: block); } |
| 1119 | |
| 1120 | void mlirBlockDetach(MlirBlock block) { |
| 1121 | Block *b = unwrap(c: block); |
| 1122 | b->getParent()->getBlocks().remove(IT: b); |
| 1123 | } |
| 1124 | |
| 1125 | intptr_t mlirBlockGetNumArguments(MlirBlock block) { |
| 1126 | return static_cast<intptr_t>(unwrap(c: block)->getNumArguments()); |
| 1127 | } |
| 1128 | |
| 1129 | MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type, |
| 1130 | MlirLocation loc) { |
| 1131 | return wrap(cpp: unwrap(c: block)->addArgument(type: unwrap(c: type), loc: unwrap(c: loc))); |
| 1132 | } |
| 1133 | |
| 1134 | void mlirBlockEraseArgument(MlirBlock block, unsigned index) { |
| 1135 | return unwrap(c: block)->eraseArgument(index); |
| 1136 | } |
| 1137 | |
| 1138 | MlirValue mlirBlockInsertArgument(MlirBlock block, intptr_t pos, MlirType type, |
| 1139 | MlirLocation loc) { |
| 1140 | return wrap(cpp: unwrap(c: block)->insertArgument(index: pos, type: unwrap(c: type), loc: unwrap(c: loc))); |
| 1141 | } |
| 1142 | |
| 1143 | MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos) { |
| 1144 | return wrap(cpp: unwrap(c: block)->getArgument(i: static_cast<unsigned>(pos))); |
| 1145 | } |
| 1146 | |
| 1147 | void mlirBlockPrint(MlirBlock block, MlirStringCallback callback, |
| 1148 | void *userData) { |
| 1149 | detail::CallbackOstream stream(callback, userData); |
| 1150 | unwrap(c: block)->print(os&: stream); |
| 1151 | } |
| 1152 | |
| 1153 | intptr_t mlirBlockGetNumSuccessors(MlirBlock block) { |
| 1154 | return static_cast<intptr_t>(unwrap(c: block)->getNumSuccessors()); |
| 1155 | } |
| 1156 | |
| 1157 | MlirBlock mlirBlockGetSuccessor(MlirBlock block, intptr_t pos) { |
| 1158 | return wrap(cpp: unwrap(c: block)->getSuccessor(i: static_cast<unsigned>(pos))); |
| 1159 | } |
| 1160 | |
| 1161 | intptr_t mlirBlockGetNumPredecessors(MlirBlock block) { |
| 1162 | Block *b = unwrap(c: block); |
| 1163 | return static_cast<intptr_t>(std::distance(first: b->pred_begin(), last: b->pred_end())); |
| 1164 | } |
| 1165 | |
| 1166 | MlirBlock mlirBlockGetPredecessor(MlirBlock block, intptr_t pos) { |
| 1167 | Block *b = unwrap(c: block); |
| 1168 | Block::pred_iterator it = b->pred_begin(); |
| 1169 | std::advance(i&: it, n: pos); |
| 1170 | return wrap(cpp: *it); |
| 1171 | } |
| 1172 | |
| 1173 | //===----------------------------------------------------------------------===// |
| 1174 | // Value API. |
| 1175 | //===----------------------------------------------------------------------===// |
| 1176 | |
| 1177 | bool mlirValueEqual(MlirValue value1, MlirValue value2) { |
| 1178 | return unwrap(c: value1) == unwrap(c: value2); |
| 1179 | } |
| 1180 | |
| 1181 | bool mlirValueIsABlockArgument(MlirValue value) { |
| 1182 | return llvm::isa<BlockArgument>(Val: unwrap(c: value)); |
| 1183 | } |
| 1184 | |
| 1185 | bool mlirValueIsAOpResult(MlirValue value) { |
| 1186 | return llvm::isa<OpResult>(Val: unwrap(c: value)); |
| 1187 | } |
| 1188 | |
| 1189 | MlirBlock mlirBlockArgumentGetOwner(MlirValue value) { |
| 1190 | return wrap(cpp: llvm::dyn_cast<BlockArgument>(Val: unwrap(c: value)).getOwner()); |
| 1191 | } |
| 1192 | |
| 1193 | intptr_t mlirBlockArgumentGetArgNumber(MlirValue value) { |
| 1194 | return static_cast<intptr_t>( |
| 1195 | llvm::dyn_cast<BlockArgument>(Val: unwrap(c: value)).getArgNumber()); |
| 1196 | } |
| 1197 | |
| 1198 | void mlirBlockArgumentSetType(MlirValue value, MlirType type) { |
| 1199 | if (auto blockArg = llvm::dyn_cast<BlockArgument>(Val: unwrap(c: value))) |
| 1200 | blockArg.setType(unwrap(c: type)); |
| 1201 | } |
| 1202 | |
| 1203 | void mlirBlockArgumentSetLocation(MlirValue value, MlirLocation loc) { |
| 1204 | if (auto blockArg = llvm::dyn_cast<BlockArgument>(Val: unwrap(c: value))) |
| 1205 | blockArg.setLoc(unwrap(c: loc)); |
| 1206 | } |
| 1207 | |
| 1208 | MlirOperation mlirOpResultGetOwner(MlirValue value) { |
| 1209 | return wrap(cpp: llvm::dyn_cast<OpResult>(Val: unwrap(c: value)).getOwner()); |
| 1210 | } |
| 1211 | |
| 1212 | intptr_t mlirOpResultGetResultNumber(MlirValue value) { |
| 1213 | return static_cast<intptr_t>( |
| 1214 | llvm::dyn_cast<OpResult>(Val: unwrap(c: value)).getResultNumber()); |
| 1215 | } |
| 1216 | |
| 1217 | MlirType mlirValueGetType(MlirValue value) { |
| 1218 | return wrap(cpp: unwrap(c: value).getType()); |
| 1219 | } |
| 1220 | |
| 1221 | void mlirValueSetType(MlirValue value, MlirType type) { |
| 1222 | unwrap(c: value).setType(unwrap(c: type)); |
| 1223 | } |
| 1224 | |
| 1225 | void mlirValueDump(MlirValue value) { |
| 1226 | unwrap(c: value).print(os&: llvm::errs(), flags: OpPrintingFlags().useLocalScope()); |
| 1227 | llvm::errs() << "\n" ; |
| 1228 | } |
| 1229 | |
| 1230 | void mlirValuePrint(MlirValue value, MlirStringCallback callback, |
| 1231 | void *userData) { |
| 1232 | detail::CallbackOstream stream(callback, userData); |
| 1233 | unwrap(c: value).print(os&: stream); |
| 1234 | } |
| 1235 | |
| 1236 | void mlirValuePrintAsOperand(MlirValue value, MlirAsmState state, |
| 1237 | MlirStringCallback callback, void *userData) { |
| 1238 | detail::CallbackOstream stream(callback, userData); |
| 1239 | Value cppValue = unwrap(c: value); |
| 1240 | cppValue.printAsOperand(os&: stream, state&: *unwrap(c: state)); |
| 1241 | } |
| 1242 | |
| 1243 | MlirOpOperand mlirValueGetFirstUse(MlirValue value) { |
| 1244 | Value cppValue = unwrap(c: value); |
| 1245 | if (cppValue.use_empty()) |
| 1246 | return {}; |
| 1247 | |
| 1248 | OpOperand *opOperand = cppValue.use_begin().getOperand(); |
| 1249 | |
| 1250 | return wrap(cpp: opOperand); |
| 1251 | } |
| 1252 | |
| 1253 | void mlirValueReplaceAllUsesOfWith(MlirValue oldValue, MlirValue newValue) { |
| 1254 | unwrap(c: oldValue).replaceAllUsesWith(newValue: unwrap(c: newValue)); |
| 1255 | } |
| 1256 | |
| 1257 | void mlirValueReplaceAllUsesExcept(MlirValue oldValue, MlirValue newValue, |
| 1258 | intptr_t numExceptions, |
| 1259 | MlirOperation *exceptions) { |
| 1260 | Value oldValueCpp = unwrap(c: oldValue); |
| 1261 | Value newValueCpp = unwrap(c: newValue); |
| 1262 | |
| 1263 | llvm::SmallPtrSet<mlir::Operation *, 4> exceptionSet; |
| 1264 | for (intptr_t i = 0; i < numExceptions; ++i) { |
| 1265 | exceptionSet.insert(Ptr: unwrap(c: exceptions[i])); |
| 1266 | } |
| 1267 | |
| 1268 | oldValueCpp.replaceAllUsesExcept(newValue: newValueCpp, exceptions: exceptionSet); |
| 1269 | } |
| 1270 | |
| 1271 | void mlirValueReplaceUsesWithIf(MlirValue of, MlirValue with, |
| 1272 | MlirOpOperandReplaceFilterCallback filter, |
| 1273 | void *userData) { |
| 1274 | assert(filter && "expected non-null filter callback" ); |
| 1275 | unwrap(c: of).replaceUsesWithIf(newValue: unwrap(c: with), |
| 1276 | shouldReplace: [filter, userData](OpOperand &operand) -> bool { |
| 1277 | return filter(wrap(cpp: &operand), userData); |
| 1278 | }); |
| 1279 | } |
| 1280 | |
| 1281 | MlirLocation mlirValueGetLocation(MlirValue v) { |
| 1282 | return wrap(cpp: unwrap(c: v).getLoc()); |
| 1283 | } |
| 1284 | |
| 1285 | MlirContext mlirValueGetContext(MlirValue v) { |
| 1286 | return wrap(cpp: unwrap(c: v).getContext()); |
| 1287 | } |
| 1288 | |
| 1289 | //===----------------------------------------------------------------------===// |
| 1290 | // OpOperand API. |
| 1291 | //===----------------------------------------------------------------------===// |
| 1292 | |
| 1293 | bool mlirOpOperandIsNull(MlirOpOperand opOperand) { return !opOperand.ptr; } |
| 1294 | |
| 1295 | MlirOperation mlirOpOperandGetOwner(MlirOpOperand opOperand) { |
| 1296 | return wrap(cpp: unwrap(c: opOperand)->getOwner()); |
| 1297 | } |
| 1298 | |
| 1299 | MlirValue mlirOpOperandGetValue(MlirOpOperand opOperand) { |
| 1300 | return wrap(cpp: unwrap(c: opOperand)->get()); |
| 1301 | } |
| 1302 | |
| 1303 | unsigned mlirOpOperandGetOperandNumber(MlirOpOperand opOperand) { |
| 1304 | return unwrap(c: opOperand)->getOperandNumber(); |
| 1305 | } |
| 1306 | |
| 1307 | MlirOpOperand mlirOpOperandGetNextUse(MlirOpOperand opOperand) { |
| 1308 | if (mlirOpOperandIsNull(opOperand)) |
| 1309 | return {}; |
| 1310 | |
| 1311 | OpOperand *nextOpOperand = static_cast<OpOperand *>( |
| 1312 | unwrap(c: opOperand)->getNextOperandUsingThisValue()); |
| 1313 | |
| 1314 | if (!nextOpOperand) |
| 1315 | return {}; |
| 1316 | |
| 1317 | return wrap(cpp: nextOpOperand); |
| 1318 | } |
| 1319 | |
| 1320 | //===----------------------------------------------------------------------===// |
| 1321 | // Type API. |
| 1322 | //===----------------------------------------------------------------------===// |
| 1323 | |
| 1324 | MlirType mlirTypeParseGet(MlirContext context, MlirStringRef type) { |
| 1325 | return wrap(cpp: mlir::parseType(typeStr: unwrap(ref: type), context: unwrap(c: context))); |
| 1326 | } |
| 1327 | |
| 1328 | MlirContext mlirTypeGetContext(MlirType type) { |
| 1329 | return wrap(cpp: unwrap(c: type).getContext()); |
| 1330 | } |
| 1331 | |
| 1332 | MlirTypeID mlirTypeGetTypeID(MlirType type) { |
| 1333 | return wrap(cpp: unwrap(c: type).getTypeID()); |
| 1334 | } |
| 1335 | |
| 1336 | MlirDialect mlirTypeGetDialect(MlirType type) { |
| 1337 | return wrap(cpp: &unwrap(c: type).getDialect()); |
| 1338 | } |
| 1339 | |
| 1340 | bool mlirTypeEqual(MlirType t1, MlirType t2) { |
| 1341 | return unwrap(c: t1) == unwrap(c: t2); |
| 1342 | } |
| 1343 | |
| 1344 | void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData) { |
| 1345 | detail::CallbackOstream stream(callback, userData); |
| 1346 | unwrap(c: type).print(os&: stream); |
| 1347 | } |
| 1348 | |
| 1349 | void mlirTypeDump(MlirType type) { |
| 1350 | unwrap(c: type).print(os&: llvm::errs()); |
| 1351 | llvm::errs() << "\n" ; |
| 1352 | } |
| 1353 | |
| 1354 | //===----------------------------------------------------------------------===// |
| 1355 | // Attribute API. |
| 1356 | //===----------------------------------------------------------------------===// |
| 1357 | |
| 1358 | MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr) { |
| 1359 | return wrap(cpp: mlir::parseAttribute(attrStr: unwrap(ref: attr), context: unwrap(c: context))); |
| 1360 | } |
| 1361 | |
| 1362 | MlirContext mlirAttributeGetContext(MlirAttribute attribute) { |
| 1363 | return wrap(cpp: unwrap(c: attribute).getContext()); |
| 1364 | } |
| 1365 | |
| 1366 | MlirType mlirAttributeGetType(MlirAttribute attribute) { |
| 1367 | Attribute attr = unwrap(c: attribute); |
| 1368 | if (auto typedAttr = llvm::dyn_cast<TypedAttr>(Val&: attr)) |
| 1369 | return wrap(cpp: typedAttr.getType()); |
| 1370 | return wrap(cpp: NoneType::get(context: attr.getContext())); |
| 1371 | } |
| 1372 | |
| 1373 | MlirTypeID mlirAttributeGetTypeID(MlirAttribute attr) { |
| 1374 | return wrap(cpp: unwrap(c: attr).getTypeID()); |
| 1375 | } |
| 1376 | |
| 1377 | MlirDialect mlirAttributeGetDialect(MlirAttribute attr) { |
| 1378 | return wrap(cpp: &unwrap(c: attr).getDialect()); |
| 1379 | } |
| 1380 | |
| 1381 | bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2) { |
| 1382 | return unwrap(c: a1) == unwrap(c: a2); |
| 1383 | } |
| 1384 | |
| 1385 | void mlirAttributePrint(MlirAttribute attr, MlirStringCallback callback, |
| 1386 | void *userData) { |
| 1387 | detail::CallbackOstream stream(callback, userData); |
| 1388 | unwrap(c: attr).print(os&: stream); |
| 1389 | } |
| 1390 | |
| 1391 | void mlirAttributeDump(MlirAttribute attr) { |
| 1392 | unwrap(c: attr).print(os&: llvm::errs()); |
| 1393 | llvm::errs() << "\n" ; |
| 1394 | } |
| 1395 | |
| 1396 | MlirNamedAttribute mlirNamedAttributeGet(MlirIdentifier name, |
| 1397 | MlirAttribute attr) { |
| 1398 | return MlirNamedAttribute{.name: name, .attribute: attr}; |
| 1399 | } |
| 1400 | |
| 1401 | //===----------------------------------------------------------------------===// |
| 1402 | // Identifier API. |
| 1403 | //===----------------------------------------------------------------------===// |
| 1404 | |
| 1405 | MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str) { |
| 1406 | return wrap(cpp: StringAttr::get(context: unwrap(c: context), bytes: unwrap(ref: str))); |
| 1407 | } |
| 1408 | |
| 1409 | MlirContext mlirIdentifierGetContext(MlirIdentifier ident) { |
| 1410 | return wrap(cpp: unwrap(c: ident).getContext()); |
| 1411 | } |
| 1412 | |
| 1413 | bool mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other) { |
| 1414 | return unwrap(c: ident) == unwrap(c: other); |
| 1415 | } |
| 1416 | |
| 1417 | MlirStringRef mlirIdentifierStr(MlirIdentifier ident) { |
| 1418 | return wrap(ref: unwrap(c: ident).strref()); |
| 1419 | } |
| 1420 | |
| 1421 | //===----------------------------------------------------------------------===// |
| 1422 | // Symbol and SymbolTable API. |
| 1423 | //===----------------------------------------------------------------------===// |
| 1424 | |
| 1425 | MlirStringRef mlirSymbolTableGetSymbolAttributeName() { |
| 1426 | return wrap(ref: llvm::StringRef("sym_name" )); |
| 1427 | } |
| 1428 | |
| 1429 | MlirStringRef mlirSymbolTableGetDefaultVisibilityAttributeName() { |
| 1430 | return wrap(ref: SymbolOpInterface::getDefaultVisibilityAttrName()); |
| 1431 | } |
| 1432 | |
| 1433 | MlirSymbolTable mlirSymbolTableCreate(MlirOperation operation) { |
| 1434 | if (!unwrap(c: operation)->hasTrait<OpTrait::SymbolTable>()) |
| 1435 | return wrap(cpp: static_cast<SymbolTable *>(nullptr)); |
| 1436 | return wrap(cpp: new SymbolTable(unwrap(c: operation))); |
| 1437 | } |
| 1438 | |
| 1439 | void mlirSymbolTableDestroy(MlirSymbolTable symbolTable) { |
| 1440 | delete unwrap(c: symbolTable); |
| 1441 | } |
| 1442 | |
| 1443 | MlirOperation mlirSymbolTableLookup(MlirSymbolTable symbolTable, |
| 1444 | MlirStringRef name) { |
| 1445 | return wrap(cpp: unwrap(c: symbolTable)->lookup(name: StringRef(name.data, name.length))); |
| 1446 | } |
| 1447 | |
| 1448 | MlirAttribute mlirSymbolTableInsert(MlirSymbolTable symbolTable, |
| 1449 | MlirOperation operation) { |
| 1450 | return wrap(cpp: (Attribute)unwrap(c: symbolTable)->insert(symbol: unwrap(c: operation))); |
| 1451 | } |
| 1452 | |
| 1453 | void mlirSymbolTableErase(MlirSymbolTable symbolTable, |
| 1454 | MlirOperation operation) { |
| 1455 | unwrap(c: symbolTable)->erase(symbol: unwrap(c: operation)); |
| 1456 | } |
| 1457 | |
| 1458 | MlirLogicalResult mlirSymbolTableReplaceAllSymbolUses(MlirStringRef oldSymbol, |
| 1459 | MlirStringRef newSymbol, |
| 1460 | MlirOperation from) { |
| 1461 | auto *cppFrom = unwrap(c: from); |
| 1462 | auto *context = cppFrom->getContext(); |
| 1463 | auto oldSymbolAttr = StringAttr::get(context, bytes: unwrap(ref: oldSymbol)); |
| 1464 | auto newSymbolAttr = StringAttr::get(context, bytes: unwrap(ref: newSymbol)); |
| 1465 | return wrap(res: SymbolTable::replaceAllSymbolUses(oldSymbol: oldSymbolAttr, newSymbol: newSymbolAttr, |
| 1466 | from: unwrap(c: from))); |
| 1467 | } |
| 1468 | |
| 1469 | void mlirSymbolTableWalkSymbolTables(MlirOperation from, bool allSymUsesVisible, |
| 1470 | void (*callback)(MlirOperation, bool, |
| 1471 | void *userData), |
| 1472 | void *userData) { |
| 1473 | SymbolTable::walkSymbolTables(op: unwrap(c: from), allSymUsesVisible, |
| 1474 | callback: [&](Operation *foundOpCpp, bool isVisible) { |
| 1475 | callback(wrap(cpp: foundOpCpp), isVisible, |
| 1476 | userData); |
| 1477 | }); |
| 1478 | } |
| 1479 | |
| 1480 | //===----------------------------------------------------------------------===// |
| 1481 | // IRMapping API |
| 1482 | //===----------------------------------------------------------------------===// |
| 1483 | |
| 1484 | MlirIRMapping mlirIRMappingCreate(void) { return wrap(cpp: new IRMapping()); } |
| 1485 | |
| 1486 | void mlirIRMappingDestroy(MlirIRMapping mapping) { delete unwrap(c: mapping); } |
| 1487 | |
| 1488 | void mlirIRMappingMapValue(MlirIRMapping mapping, MlirValue from, |
| 1489 | MlirValue to) { |
| 1490 | unwrap(c: mapping)->map(from: unwrap(c: from), to: unwrap(c: to)); |
| 1491 | } |
| 1492 | |
| 1493 | void mlirIRMappingMapBlock(MlirIRMapping mapping, MlirBlock from, |
| 1494 | MlirBlock to) { |
| 1495 | unwrap(c: mapping)->map(from: unwrap(c: from), to: unwrap(c: to)); |
| 1496 | } |
| 1497 | |
| 1498 | void mlirIRMappingMapOperation(MlirIRMapping mapping, MlirOperation from, |
| 1499 | MlirOperation to) { |
| 1500 | unwrap(c: mapping)->map(from: unwrap(c: from), to: unwrap(c: to)); |
| 1501 | } |
| 1502 | |
| 1503 | void mlirIRMappingClear(MlirIRMapping mapping) { unwrap(c: mapping)->clear(); } |
| 1504 | |
| 1505 | MlirValue mlirIRMappingLookupOrDefaultValue(MlirIRMapping mapping, |
| 1506 | MlirValue from) { |
| 1507 | return wrap(cpp: unwrap(c: mapping)->lookupOrDefault(from: unwrap(c: from))); |
| 1508 | } |
| 1509 | |
| 1510 | MlirValue mlirIRMappingLookupOrNullValue(MlirIRMapping mapping, |
| 1511 | MlirValue from) { |
| 1512 | return wrap(cpp: unwrap(c: mapping)->lookupOrNull(from: unwrap(c: from))); |
| 1513 | } |
| 1514 | |
| 1515 | MlirBlock mlirIRMappingLookupOrDefaultBlock(MlirIRMapping mapping, |
| 1516 | MlirBlock from) { |
| 1517 | return wrap(cpp: unwrap(c: mapping)->lookupOrDefault(from: unwrap(c: from))); |
| 1518 | } |
| 1519 | |
| 1520 | MlirBlock mlirIRMappingLookupOrNullBlock(MlirIRMapping mapping, |
| 1521 | MlirBlock from) { |
| 1522 | return wrap(cpp: unwrap(c: mapping)->lookupOrNull(from: unwrap(c: from))); |
| 1523 | } |
| 1524 | |
| 1525 | MlirOperation mlirIRMappingLookupOrDefaultOperation(MlirIRMapping mapping, |
| 1526 | MlirOperation from) { |
| 1527 | return wrap(cpp: unwrap(c: mapping)->lookupOrDefault(from: unwrap(c: from))); |
| 1528 | } |
| 1529 | |
| 1530 | MlirOperation mlirIRMappingLookupOrNullOperation(MlirIRMapping mapping, |
| 1531 | MlirOperation from) { |
| 1532 | return wrap(cpp: unwrap(c: mapping)->lookupOrNull(from: unwrap(c: from))); |
| 1533 | } |
| 1534 | |
| 1535 | bool mlirIRMappingContainsValue(MlirIRMapping mapping, MlirValue value) { |
| 1536 | return unwrap(c: mapping)->contains(from: unwrap(c: value)); |
| 1537 | } |
| 1538 | |
| 1539 | bool mlirIRMappingContainsBlock(MlirIRMapping mapping, MlirBlock block) { |
| 1540 | return unwrap(c: mapping)->contains(from: unwrap(c: block)); |
| 1541 | } |
| 1542 | |
| 1543 | bool mlirIRMappingContainsOperation(MlirIRMapping mapping, MlirOperation op) { |
| 1544 | return unwrap(c: mapping)->contains(from: unwrap(c: op)); |
| 1545 | } |
| 1546 | |
| 1547 | void mlirIRMappingEraseValue(MlirIRMapping mapping, MlirValue value) { |
| 1548 | unwrap(c: mapping)->erase(from: unwrap(c: value)); |
| 1549 | } |
| 1550 | |
| 1551 | void mlirIRMappingEraseBlock(MlirIRMapping mapping, MlirBlock block) { |
| 1552 | unwrap(c: mapping)->erase(from: unwrap(c: block)); |
| 1553 | } |
| 1554 | |
| 1555 | void mlirIRMappingEraseOperation(MlirIRMapping mapping, MlirOperation op) { |
| 1556 | unwrap(c: mapping)->erase(from: unwrap(c: op)); |
| 1557 | } |
| 1558 | |
| 1559 | MlirOperation mlirOperationCloneWithMapping(MlirOperation op, |
| 1560 | MlirIRMapping mapping) { |
| 1561 | return wrap(cpp: unwrap(c: op)->clone(mapper&: *unwrap(c: mapping))); |
| 1562 | } |
| 1563 | |