MLIR 23.0.0git
IR.cpp
Go to the documentation of this file.
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
14#include "mlir/CAPI/IR.h"
15#include "mlir/CAPI/Support.h"
16#include "mlir/CAPI/Utils.h"
17#include "mlir/IR/Attributes.h"
19#include "mlir/IR/BuiltinOps.h"
20#include "mlir/IR/Diagnostics.h"
21#include "mlir/IR/Dialect.h"
22#include "mlir/IR/Location.h"
23#include "mlir/IR/Operation.h"
25#include "mlir/IR/OwningOpRef.h"
26#include "mlir/IR/Types.h"
27#include "mlir/IR/Value.h"
28#include "mlir/IR/Verifier.h"
29#include "mlir/IR/Visitors.h"
31#include "mlir/Parser/Parser.h"
32#include "llvm/ADT/SmallPtrSet.h"
33
34#include <cstddef>
35#include <memory>
36#include <optional>
37
38using namespace mlir;
39
40//===----------------------------------------------------------------------===//
41// Context API.
42//===----------------------------------------------------------------------===//
43
44MlirContext mlirContextCreate() {
45 auto *context = new MLIRContext;
46 return wrap(context);
47}
48
49static inline MLIRContext::Threading toThreadingEnum(bool threadingEnabled) {
50 return threadingEnabled ? MLIRContext::Threading::ENABLED
52}
53
54MlirContext mlirContextCreateWithThreading(bool threadingEnabled) {
55 auto *context = new MLIRContext(toThreadingEnum(threadingEnabled));
56 return wrap(context);
57}
58
59MlirContext mlirContextCreateWithRegistry(MlirDialectRegistry registry,
60 bool threadingEnabled) {
61 auto *context =
62 new MLIRContext(*unwrap(registry), toThreadingEnum(threadingEnabled));
63 return wrap(context);
64}
65
66bool mlirContextEqual(MlirContext ctx1, MlirContext ctx2) {
67 return unwrap(ctx1) == unwrap(ctx2);
68}
69
70void mlirContextDestroy(MlirContext context) { delete unwrap(context); }
71
72void mlirContextSetAllowUnregisteredDialects(MlirContext context, bool allow) {
73 unwrap(context)->allowUnregisteredDialects(allow);
74}
75
76bool mlirContextGetAllowUnregisteredDialects(MlirContext context) {
77 return unwrap(context)->allowsUnregisteredDialects();
78}
80 return static_cast<intptr_t>(unwrap(context)->getAvailableDialects().size());
81}
82
84 MlirDialectRegistry registry) {
85 unwrap(ctx)->appendDialectRegistry(*unwrap(registry));
86}
87
88// TODO: expose a cheaper way than constructing + sorting a vector only to take
89// its size.
91 return static_cast<intptr_t>(unwrap(context)->getLoadedDialects().size());
92}
93
94MlirDialect mlirContextGetOrLoadDialect(MlirContext context,
95 MlirStringRef name) {
96 return wrap(unwrap(context)->getOrLoadDialect(unwrap(name)));
97}
98
99bool mlirContextIsRegisteredOperation(MlirContext context, MlirStringRef name) {
100 return unwrap(context)->isOperationRegistered(unwrap(name));
101}
102
103void mlirContextEnableMultithreading(MlirContext context, bool enable) {
104 return unwrap(context)->enableMultithreading(enable);
105}
106
107void mlirContextLoadAllAvailableDialects(MlirContext context) {
108 unwrap(context)->loadAllAvailableDialects();
109}
110
111void mlirContextSetThreadPool(MlirContext context,
112 MlirLlvmThreadPool threadPool) {
113 unwrap(context)->setThreadPool(*unwrap(threadPool));
114}
115
116unsigned mlirContextGetNumThreads(MlirContext context) {
117 return unwrap(context)->getNumThreads();
118}
119
120MlirLlvmThreadPool mlirContextGetThreadPool(MlirContext context) {
121 return wrap(&unwrap(context)->getThreadPool());
122}
123
124//===----------------------------------------------------------------------===//
125// Dialect API.
126//===----------------------------------------------------------------------===//
127
128MlirContext mlirDialectGetContext(MlirDialect dialect) {
129 return wrap(unwrap(dialect)->getContext());
130}
131
132bool mlirDialectEqual(MlirDialect dialect1, MlirDialect dialect2) {
133 return unwrap(dialect1) == unwrap(dialect2);
134}
135
137 return wrap(unwrap(dialect)->getNamespace());
138}
139
140//===----------------------------------------------------------------------===//
141// DialectRegistry API.
142//===----------------------------------------------------------------------===//
143
144MlirDialectRegistry mlirDialectRegistryCreate() {
145 return wrap(new DialectRegistry());
146}
147
148void mlirDialectRegistryDestroy(MlirDialectRegistry registry) {
149 delete unwrap(registry);
150}
151
152//===----------------------------------------------------------------------===//
153// AsmState API.
154//===----------------------------------------------------------------------===//
155
156MlirAsmState mlirAsmStateCreateForOperation(MlirOperation op,
157 MlirOpPrintingFlags flags) {
158 return wrap(new AsmState(unwrap(op), *unwrap(flags)));
159}
160
161static Operation *findParent(Operation *op, bool shouldUseLocalScope) {
162 do {
163 // If we are printing local scope, stop at the first operation that is
164 // isolated from above.
165 if (shouldUseLocalScope && op->hasTrait<OpTrait::IsIsolatedFromAbove>())
166 break;
167
168 // Otherwise, traverse up to the next parent.
169 Operation *parentOp = op->getParentOp();
170 if (!parentOp)
171 break;
172 op = parentOp;
173 } while (true);
174 return op;
175}
176
177MlirAsmState mlirAsmStateCreateForValue(MlirValue value,
178 MlirOpPrintingFlags flags) {
179 Operation *op;
180 mlir::Value val = unwrap(value);
181 if (auto result = llvm::dyn_cast<OpResult>(val)) {
182 op = result.getOwner();
183 } else {
184 op = llvm::cast<BlockArgument>(val).getOwner()->getParentOp();
185 if (!op) {
186 emitError(val.getLoc()) << "<<UNKNOWN SSA VALUE>>";
187 return {nullptr};
188 }
189 }
190 op = findParent(op, unwrap(flags)->shouldUseLocalScope());
191 return wrap(new AsmState(op, *unwrap(flags)));
192}
193
194/// Destroys printing flags created with mlirAsmStateCreate.
195void mlirAsmStateDestroy(MlirAsmState state) { delete unwrap(state); }
196
197//===----------------------------------------------------------------------===//
198// Printing flags API.
199//===----------------------------------------------------------------------===//
200
201MlirOpPrintingFlags mlirOpPrintingFlagsCreate() {
202 return wrap(new OpPrintingFlags());
203}
204
205void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags) {
206 delete unwrap(flags);
207}
208
209void mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags,
210 intptr_t largeElementLimit) {
211 unwrap(flags)->elideLargeElementsAttrs(largeElementLimit);
212}
213
214void mlirOpPrintingFlagsElideLargeResourceString(MlirOpPrintingFlags flags,
215 intptr_t largeResourceLimit) {
216 unwrap(flags)->elideLargeResourceString(largeResourceLimit);
217}
218
219void mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, bool enable,
220 bool prettyForm) {
221 unwrap(flags)->enableDebugInfo(enable, /*prettyForm=*/prettyForm);
222}
223
224void mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags) {
225 unwrap(flags)->printGenericOpForm();
226}
227
228void mlirOpPrintingFlagsPrintNameLocAsPrefix(MlirOpPrintingFlags flags) {
229 unwrap(flags)->printNameLocAsPrefix();
230}
231
232void mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags) {
233 unwrap(flags)->useLocalScope();
234}
235
236void mlirOpPrintingFlagsAssumeVerified(MlirOpPrintingFlags flags) {
237 unwrap(flags)->assumeVerified();
238}
239
240void mlirOpPrintingFlagsSkipRegions(MlirOpPrintingFlags flags) {
241 unwrap(flags)->skipRegions();
242}
243//===----------------------------------------------------------------------===//
244// Bytecode printing flags API.
245//===----------------------------------------------------------------------===//
246
247MlirBytecodeWriterConfig mlirBytecodeWriterConfigCreate() {
248 return wrap(new BytecodeWriterConfig());
249}
250
251void mlirBytecodeWriterConfigDestroy(MlirBytecodeWriterConfig config) {
252 delete unwrap(config);
253}
254
255void mlirBytecodeWriterConfigDesiredEmitVersion(MlirBytecodeWriterConfig flags,
256 int64_t version) {
257 unwrap(flags)->setDesiredBytecodeVersion(version);
258}
259
260//===----------------------------------------------------------------------===//
261// Location API.
262//===----------------------------------------------------------------------===//
263
264MlirAttribute mlirLocationGetAttribute(MlirLocation location) {
265 return wrap(LocationAttr(unwrap(location)));
266}
267
268MlirLocation mlirLocationFromAttribute(MlirAttribute attribute) {
269 return wrap(Location(llvm::dyn_cast<LocationAttr>(unwrap(attribute))));
270}
271
272MlirLocation mlirLocationFileLineColGet(MlirContext context,
273 MlirStringRef filename, unsigned line,
274 unsigned col) {
275 return wrap(Location(
276 FileLineColLoc::get(unwrap(context), unwrap(filename), line, col)));
277}
278
279MlirLocation
280mlirLocationFileLineColRangeGet(MlirContext context, MlirStringRef filename,
281 unsigned startLine, unsigned startCol,
282 unsigned endLine, unsigned endCol) {
283 return wrap(
284 Location(FileLineColRange::get(unwrap(context), unwrap(filename),
285 startLine, startCol, endLine, endCol)));
286}
287
288MlirIdentifier mlirLocationFileLineColRangeGetFilename(MlirLocation location) {
289 return wrap(llvm::dyn_cast<FileLineColRange>(unwrap(location)).getFilename());
290}
291
293 if (auto loc = llvm::dyn_cast<FileLineColRange>(unwrap(location)))
294 return loc.getStartLine();
295 return -1;
296}
297
299 if (auto loc = llvm::dyn_cast<FileLineColRange>(unwrap(location)))
300 return loc.getStartColumn();
301 return -1;
302}
303
304int mlirLocationFileLineColRangeGetEndLine(MlirLocation location) {
305 if (auto loc = llvm::dyn_cast<FileLineColRange>(unwrap(location)))
306 return loc.getEndLine();
307 return -1;
308}
309
311 if (auto loc = llvm::dyn_cast<FileLineColRange>(unwrap(location)))
312 return loc.getEndColumn();
313 return -1;
314}
315
317 return wrap(FileLineColRange::getTypeID());
318}
319
320bool mlirLocationIsAFileLineColRange(MlirLocation location) {
321 return isa<FileLineColRange>(unwrap(location));
322}
323
324MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller) {
325 return wrap(Location(CallSiteLoc::get(unwrap(callee), unwrap(caller))));
326}
327
328MlirLocation mlirLocationCallSiteGetCallee(MlirLocation location) {
329 return wrap(
330 Location(llvm::dyn_cast<CallSiteLoc>(unwrap(location)).getCallee()));
331}
332
333MlirLocation mlirLocationCallSiteGetCaller(MlirLocation location) {
334 return wrap(
335 Location(llvm::dyn_cast<CallSiteLoc>(unwrap(location)).getCaller()));
336}
337
339 return wrap(CallSiteLoc::getTypeID());
340}
341
342bool mlirLocationIsACallSite(MlirLocation location) {
343 return isa<CallSiteLoc>(unwrap(location));
344}
345
346MlirLocation mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations,
347 MlirLocation const *locations,
348 MlirAttribute metadata) {
350 ArrayRef<Location> unwrappedLocs = unwrapList(nLocations, locations, locs);
351 return wrap(FusedLoc::get(unwrappedLocs, unwrap(metadata), unwrap(ctx)));
352}
353
354unsigned mlirLocationFusedGetNumLocations(MlirLocation location) {
355 if (auto locationsArrRef = llvm::dyn_cast<FusedLoc>(unwrap(location)))
356 return locationsArrRef.getLocations().size();
357 return 0;
358}
359
360void mlirLocationFusedGetLocations(MlirLocation location,
361 MlirLocation *locationsCPtr) {
362 if (auto locationsArrRef = llvm::dyn_cast<FusedLoc>(unwrap(location))) {
363 for (auto [i, location] : llvm::enumerate(locationsArrRef.getLocations()))
364 locationsCPtr[i] = wrap(location);
365 }
366}
367
368MlirAttribute mlirLocationFusedGetMetadata(MlirLocation location) {
369 return wrap(llvm::dyn_cast<FusedLoc>(unwrap(location)).getMetadata());
370}
371
372MlirTypeID mlirLocationFusedGetTypeID() { return wrap(FusedLoc::getTypeID()); }
373
374bool mlirLocationIsAFused(MlirLocation location) {
375 return isa<FusedLoc>(unwrap(location));
376}
377
378MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name,
379 MlirLocation childLoc) {
380 if (mlirLocationIsNull(childLoc))
381 return wrap(
382 Location(NameLoc::get(StringAttr::get(unwrap(context), unwrap(name)))));
383 return wrap(Location(NameLoc::get(
384 StringAttr::get(unwrap(context), unwrap(name)), unwrap(childLoc))));
385}
386
387MlirIdentifier mlirLocationNameGetName(MlirLocation location) {
388 return wrap((llvm::dyn_cast<NameLoc>(unwrap(location)).getName()));
389}
390
391MlirLocation mlirLocationNameGetChildLoc(MlirLocation location) {
392 return wrap(
393 Location(llvm::dyn_cast<NameLoc>(unwrap(location)).getChildLoc()));
394}
395
396MlirTypeID mlirLocationNameGetTypeID() { return wrap(NameLoc::getTypeID()); }
397
398bool mlirLocationIsAName(MlirLocation location) {
399 return isa<NameLoc>(unwrap(location));
400}
401
402MlirLocation mlirLocationUnknownGet(MlirContext context) {
403 return wrap(Location(UnknownLoc::get(unwrap(context))));
404}
405
406bool mlirLocationEqual(MlirLocation l1, MlirLocation l2) {
407 return unwrap(l1) == unwrap(l2);
408}
409
410MlirContext mlirLocationGetContext(MlirLocation location) {
411 return wrap(unwrap(location).getContext());
412}
413
414void mlirLocationPrint(MlirLocation location, MlirStringCallback callback,
415 void *userData) {
416 detail::CallbackOstream stream(callback, userData);
417 unwrap(location).print(stream);
418}
419
420//===----------------------------------------------------------------------===//
421// Module API.
422//===----------------------------------------------------------------------===//
423
424MlirModule mlirModuleCreateEmpty(MlirLocation location) {
425 return wrap(ModuleOp::create(unwrap(location)));
426}
427
428MlirModule mlirModuleCreateParse(MlirContext context, MlirStringRef module) {
429 OwningOpRef<ModuleOp> owning =
430 parseSourceString<ModuleOp>(unwrap(module), unwrap(context));
431 if (!owning)
432 return MlirModule{nullptr};
433 return MlirModule{owning.release().getOperation()};
434}
435
436MlirModule mlirModuleCreateParseFromFile(MlirContext context,
437 MlirStringRef fileName) {
438 OwningOpRef<ModuleOp> owning =
439 parseSourceFile<ModuleOp>(unwrap(fileName), unwrap(context));
440 if (!owning)
441 return MlirModule{nullptr};
442 return MlirModule{owning.release().getOperation()};
443}
444
445MlirContext mlirModuleGetContext(MlirModule module) {
446 return wrap(unwrap(module).getContext());
447}
448
449MlirBlock mlirModuleGetBody(MlirModule module) {
450 return wrap(unwrap(module).getBody());
451}
452
453void mlirModuleDestroy(MlirModule module) {
454 // Transfer ownership to an OwningOpRef<ModuleOp> so that its destructor is
455 // called.
457}
458
459MlirOperation mlirModuleGetOperation(MlirModule module) {
460 return wrap(unwrap(module).getOperation());
461}
462
463MlirModule mlirModuleFromOperation(MlirOperation op) {
464 return wrap(dyn_cast<ModuleOp>(unwrap(op)));
465}
466
467bool mlirModuleEqual(MlirModule lhs, MlirModule rhs) {
468 return unwrap(lhs) == unwrap(rhs);
469}
470
471size_t mlirModuleHashValue(MlirModule mod) {
472 return OperationEquivalence::computeHash(unwrap(mod).getOperation());
473}
474
475//===----------------------------------------------------------------------===//
476// Operation state API.
477//===----------------------------------------------------------------------===//
478
479MlirOperationState mlirOperationStateGet(MlirStringRef name, MlirLocation loc) {
480 MlirOperationState state;
481 state.name = name;
482 state.location = loc;
483 state.nResults = 0;
484 state.results = nullptr;
485 state.nOperands = 0;
486 state.operands = nullptr;
487 state.nRegions = 0;
488 state.regions = nullptr;
489 state.nSuccessors = 0;
490 state.successors = nullptr;
491 state.nAttributes = 0;
492 state.attributes = nullptr;
493 state.enableResultTypeInference = false;
494 return state;
495}
496
497#define APPEND_ELEMS(type, sizeName, elemName) \
498 state->elemName = \
499 (type *)realloc(state->elemName, (state->sizeName + n) * sizeof(type)); \
500 memcpy(state->elemName + state->sizeName, elemName, n * sizeof(type)); \
501 state->sizeName += n;
502
503void mlirOperationStateAddResults(MlirOperationState *state, intptr_t n,
504 MlirType const *results) {
505 APPEND_ELEMS(MlirType, nResults, results);
506}
507
508void mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n,
509 MlirValue const *operands) {
510 APPEND_ELEMS(MlirValue, nOperands, operands);
511}
512void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n,
513 MlirRegion const *regions) {
514 APPEND_ELEMS(MlirRegion, nRegions, regions);
515}
516void mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n,
517 MlirBlock const *successors) {
518 APPEND_ELEMS(MlirBlock, nSuccessors, successors);
519}
520void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n,
521 MlirNamedAttribute const *attributes) {
522 APPEND_ELEMS(MlirNamedAttribute, nAttributes, attributes);
523}
524
525void mlirOperationStateEnableResultTypeInference(MlirOperationState *state) {
526 state->enableResultTypeInference = true;
527}
528
529//===----------------------------------------------------------------------===//
530// Operation API.
531//===----------------------------------------------------------------------===//
532
533static LogicalResult inferOperationTypes(OperationState &state) {
534 MLIRContext *context = state.getContext();
535 std::optional<RegisteredOperationName> info = state.name.getRegisteredInfo();
536 if (!info) {
537 emitError(state.location)
538 << "type inference was requested for the operation " << state.name
539 << ", but the operation was not registered; ensure that the dialect "
540 "containing the operation is linked into MLIR and registered with "
541 "the context";
542 return failure();
543 }
544
545 auto *inferInterface = info->getInterface<InferTypeOpInterface>();
546 if (!inferInterface) {
547 emitError(state.location)
548 << "type inference was requested for the operation " << state.name
549 << ", but the operation does not support type inference; result "
550 "types must be specified explicitly";
551 return failure();
552 }
553
554 DictionaryAttr attributes = state.attributes.getDictionary(context);
555 OpaqueProperties properties = state.getRawProperties();
556
557 if (!properties && info->getOpPropertyByteSize() > 0 && !attributes.empty()) {
558 auto prop = std::make_unique<char[]>(info->getOpPropertyByteSize());
559 properties = OpaqueProperties(prop.get());
560 if (properties) {
561 auto emitError = [&]() {
562 return mlir::emitError(state.location)
563 << " failed properties conversion while building "
564 << state.name.getStringRef() << " with `" << attributes << "`: ";
565 };
566 if (failed(info->setOpPropertiesFromAttribute(state.name, properties,
567 attributes, emitError)))
568 return failure();
569 }
570 if (succeeded(inferInterface->inferReturnTypes(
571 context, state.location, state.operands, attributes, properties,
572 state.regions, state.types))) {
573 return success();
574 }
575 // Diagnostic emitted by interface.
576 return failure();
577 }
578
579 if (succeeded(inferInterface->inferReturnTypes(
580 context, state.location, state.operands, attributes, properties,
581 state.regions, state.types)))
582 return success();
583
584 // Diagnostic emitted by interface.
585 return failure();
586}
587
588MlirOperation mlirOperationCreate(MlirOperationState *state) {
589 assert(state);
590 OperationState cppState(unwrap(state->location), unwrap(state->name));
591 SmallVector<Type, 4> resultStorage;
592 SmallVector<Value, 8> operandStorage;
593 SmallVector<Block *, 2> successorStorage;
594 cppState.addTypes(unwrapList(state->nResults, state->results, resultStorage));
595 cppState.addOperands(
596 unwrapList(state->nOperands, state->operands, operandStorage));
597 cppState.addSuccessors(
598 unwrapList(state->nSuccessors, state->successors, successorStorage));
599
600 cppState.attributes.reserve(state->nAttributes);
601 for (intptr_t i = 0; i < state->nAttributes; ++i)
602 cppState.addAttribute(unwrap(state->attributes[i].name),
603 unwrap(state->attributes[i].attribute));
604
605 for (intptr_t i = 0; i < state->nRegions; ++i)
606 cppState.addRegion(std::unique_ptr<Region>(unwrap(state->regions[i])));
607
608 free(state->results);
609 free(state->operands);
610 free(state->successors);
611 free(state->regions);
612 free(state->attributes);
613
614 // Infer result types.
615 if (state->enableResultTypeInference) {
616 assert(cppState.types.empty() &&
617 "result type inference enabled and result types provided");
618 if (failed(inferOperationTypes(cppState)))
619 return {nullptr};
620 }
621
622 return wrap(Operation::create(cppState));
623}
624
625MlirOperation mlirOperationCreateParse(MlirContext context,
626 MlirStringRef sourceStr,
627 MlirStringRef sourceName) {
628
629 return wrap(
630 parseSourceString(unwrap(sourceStr), unwrap(context), unwrap(sourceName))
631 .release());
632}
633
634MlirOperation mlirOperationClone(MlirOperation op) {
635 return wrap(unwrap(op)->clone());
636}
637
638void mlirOperationDestroy(MlirOperation op) { unwrap(op)->erase(); }
639
640void mlirOperationRemoveFromParent(MlirOperation op) { unwrap(op)->remove(); }
641
642bool mlirOperationEqual(MlirOperation op, MlirOperation other) {
643 return unwrap(op) == unwrap(other);
644}
645
646size_t mlirOperationHashValue(MlirOperation op) {
648}
649
650MlirContext mlirOperationGetContext(MlirOperation op) {
651 return wrap(unwrap(op)->getContext());
652}
653
654MlirLocation mlirOperationGetLocation(MlirOperation op) {
655 return wrap(unwrap(op)->getLoc());
656}
657
658void mlirOperationSetLocation(MlirOperation op, MlirLocation loc) {
659 unwrap(op)->setLoc(unwrap(loc));
660}
661
662MlirTypeID mlirOperationGetTypeID(MlirOperation op) {
663 if (auto info = unwrap(op)->getRegisteredInfo())
664 return wrap(info->getTypeID());
665 return {nullptr};
666}
667
668MlirIdentifier mlirOperationGetName(MlirOperation op) {
669 return wrap(unwrap(op)->getName().getIdentifier());
670}
671
672MlirBlock mlirOperationGetBlock(MlirOperation op) {
673 return wrap(unwrap(op)->getBlock());
674}
675
676MlirOperation mlirOperationGetParentOperation(MlirOperation op) {
677 return wrap(unwrap(op)->getParentOp());
678}
679
681 return static_cast<intptr_t>(unwrap(op)->getNumRegions());
682}
683
684MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos) {
685 return wrap(&unwrap(op)->getRegion(static_cast<unsigned>(pos)));
686}
687
688MlirRegion mlirOperationGetFirstRegion(MlirOperation op) {
689 Operation *cppOp = unwrap(op);
690 if (cppOp->getNumRegions() == 0)
691 return wrap(static_cast<Region *>(nullptr));
692 return wrap(&cppOp->getRegion(0));
693}
694
695MlirRegion mlirRegionGetNextInOperation(MlirRegion region) {
696 Region *cppRegion = unwrap(region);
697 Operation *parent = cppRegion->getParentOp();
698 intptr_t next = cppRegion->getRegionNumber() + 1;
699 if (parent->getNumRegions() > next)
700 return wrap(&parent->getRegion(next));
701 return wrap(static_cast<Region *>(nullptr));
702}
703
704MlirOperation mlirOperationGetNextInBlock(MlirOperation op) {
705 return wrap(unwrap(op)->getNextNode());
706}
707
709 return static_cast<intptr_t>(unwrap(op)->getNumOperands());
710}
711
712MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos) {
713 return wrap(unwrap(op)->getOperand(static_cast<unsigned>(pos)));
714}
715
716MlirOpOperand mlirOperationGetOpOperand(MlirOperation op, intptr_t pos) {
717 return wrap(&unwrap(op)->getOpOperand(static_cast<unsigned>(pos)));
718}
719
720void mlirOperationSetOperand(MlirOperation op, intptr_t pos,
721 MlirValue newValue) {
722 unwrap(op)->setOperand(static_cast<unsigned>(pos), unwrap(newValue));
723}
724
725void mlirOperationSetOperands(MlirOperation op, intptr_t nOperands,
726 MlirValue const *operands) {
728 unwrap(op)->setOperands(unwrapList(nOperands, operands, ops));
729}
730
732 return static_cast<intptr_t>(unwrap(op)->getNumResults());
733}
734
735MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos) {
736 return wrap(unwrap(op)->getResult(static_cast<unsigned>(pos)));
737}
738
740 return static_cast<intptr_t>(unwrap(op)->getNumSuccessors());
741}
742
743MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos) {
744 return wrap(unwrap(op)->getSuccessor(static_cast<unsigned>(pos)));
745}
746
749 std::optional<Attribute> attr = unwrap(op)->getInherentAttr(unwrap(name));
750 return attr.has_value();
751}
752
753MlirAttribute mlirOperationGetInherentAttributeByName(MlirOperation op,
754 MlirStringRef name) {
755 std::optional<Attribute> attr = unwrap(op)->getInherentAttr(unwrap(name));
756 if (attr.has_value())
757 return wrap(*attr);
758 return {};
759}
760
762 MlirStringRef name,
763 MlirAttribute attr) {
764 unwrap(op)->setInherentAttr(
765 StringAttr::get(unwrap(op)->getContext(), unwrap(name)), unwrap(attr));
766}
767
769 return static_cast<intptr_t>(
770 llvm::range_size(unwrap(op)->getDiscardableAttrs()));
771}
772
774 intptr_t pos) {
775 NamedAttribute attr =
776 *std::next(unwrap(op)->getDiscardableAttrs().begin(), pos);
777 return MlirNamedAttribute{wrap(attr.getName()), wrap(attr.getValue())};
778}
779
780MlirAttribute mlirOperationGetDiscardableAttributeByName(MlirOperation op,
781 MlirStringRef name) {
782 return wrap(unwrap(op)->getDiscardableAttr(unwrap(name)));
783}
784
786 MlirStringRef name,
787 MlirAttribute attr) {
788 unwrap(op)->setDiscardableAttr(unwrap(name), unwrap(attr));
789}
790
792 MlirStringRef name) {
793 return !!unwrap(op)->removeDiscardableAttr(unwrap(name));
794}
795
796void mlirOperationSetSuccessor(MlirOperation op, intptr_t pos,
797 MlirBlock block) {
798 unwrap(op)->setSuccessor(unwrap(block), static_cast<unsigned>(pos));
799}
800
802 return static_cast<intptr_t>(unwrap(op)->getAttrs().size());
803}
804
806 NamedAttribute attr = unwrap(op)->getAttrs()[pos];
807 return MlirNamedAttribute{wrap(attr.getName()), wrap(attr.getValue())};
808}
809
810MlirAttribute mlirOperationGetAttributeByName(MlirOperation op,
811 MlirStringRef name) {
812 return wrap(unwrap(op)->getAttr(unwrap(name)));
813}
814
816 MlirAttribute attr) {
817 unwrap(op)->setAttr(unwrap(name), unwrap(attr));
818}
819
821 return !!unwrap(op)->removeAttr(unwrap(name));
822}
823
824void mlirOperationPrint(MlirOperation op, MlirStringCallback callback,
825 void *userData) {
826 detail::CallbackOstream stream(callback, userData);
827 unwrap(op)->print(stream);
828}
829
830void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags,
831 MlirStringCallback callback, void *userData) {
832 detail::CallbackOstream stream(callback, userData);
833 unwrap(op)->print(stream, *unwrap(flags));
834}
835
836void mlirOperationPrintWithState(MlirOperation op, MlirAsmState state,
837 MlirStringCallback callback, void *userData) {
838 detail::CallbackOstream stream(callback, userData);
839 if (state.ptr)
840 unwrap(op)->print(stream, *unwrap(state));
841 else
842 unwrap(op)->print(stream);
843}
844
845void mlirOperationWriteBytecode(MlirOperation op, MlirStringCallback callback,
846 void *userData) {
847 detail::CallbackOstream stream(callback, userData);
848 // As no desired version is set, no failure can occur.
849 (void)writeBytecodeToFile(unwrap(op), stream);
850}
851
853 MlirOperation op, MlirBytecodeWriterConfig config,
854 MlirStringCallback callback, void *userData) {
855 detail::CallbackOstream stream(callback, userData);
856 return wrap(writeBytecodeToFile(unwrap(op), stream, *unwrap(config)));
857}
858
859void mlirOperationDump(MlirOperation op) { return unwrap(op)->dump(); }
860
861bool mlirOperationVerify(MlirOperation op) {
862 return succeeded(verify(unwrap(op)));
863}
864
865void mlirOperationMoveAfter(MlirOperation op, MlirOperation other) {
866 return unwrap(op)->moveAfter(unwrap(other));
867}
868
869void mlirOperationMoveBefore(MlirOperation op, MlirOperation other) {
870 return unwrap(op)->moveBefore(unwrap(other));
871}
872
873bool mlirOperationIsBeforeInBlock(MlirOperation op, MlirOperation other) {
874 return unwrap(op)->isBeforeInBlock(unwrap(other));
875}
876
878 switch (result) {
881
884
886 return mlir::WalkResult::skip();
887 }
888 llvm_unreachable("unknown result in WalkResult::unwrap");
889}
890
891void mlirOperationWalk(MlirOperation op, MlirOperationWalkCallback callback,
892 void *userData, MlirWalkOrder walkOrder) {
893 switch (walkOrder) {
894
895 case MlirWalkPreOrder:
897 [callback, userData](Operation *op) {
898 return unwrap(callback(wrap(op), userData));
899 });
900 break;
903 [callback, userData](Operation *op) {
904 return unwrap(callback(wrap(op), userData));
905 });
906 }
907}
908
909void mlirOperationReplaceUsesOfWith(MlirOperation op, MlirValue oldValue,
910 MlirValue newValue) {
911 unwrap(op)->replaceUsesOfWith(unwrap(oldValue), unwrap(newValue));
912}
913
914//===----------------------------------------------------------------------===//
915// Region API.
916//===----------------------------------------------------------------------===//
917
918MlirRegion mlirRegionCreate() { return wrap(new Region); }
919
920bool mlirRegionEqual(MlirRegion region, MlirRegion other) {
921 return unwrap(region) == unwrap(other);
922}
923
924MlirBlock mlirRegionGetFirstBlock(MlirRegion region) {
925 Region *cppRegion = unwrap(region);
926 if (cppRegion->empty())
927 return wrap(static_cast<Block *>(nullptr));
928 return wrap(&cppRegion->front());
929}
930
931void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block) {
932 unwrap(region)->push_back(unwrap(block));
933}
934
935void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos,
936 MlirBlock block) {
937 auto &blockList = unwrap(region)->getBlocks();
938 blockList.insert(std::next(blockList.begin(), pos), unwrap(block));
939}
940
941void mlirRegionInsertOwnedBlockAfter(MlirRegion region, MlirBlock reference,
942 MlirBlock block) {
943 Region *cppRegion = unwrap(region);
944 if (mlirBlockIsNull(reference)) {
945 cppRegion->getBlocks().insert(cppRegion->begin(), unwrap(block));
946 return;
947 }
948
949 assert(unwrap(reference)->getParent() == unwrap(region) &&
950 "expected reference block to belong to the region");
951 cppRegion->getBlocks().insertAfter(Region::iterator(unwrap(reference)),
952 unwrap(block));
953}
954
955void mlirRegionInsertOwnedBlockBefore(MlirRegion region, MlirBlock reference,
956 MlirBlock block) {
957 if (mlirBlockIsNull(reference))
958 return mlirRegionAppendOwnedBlock(region, block);
959
960 assert(unwrap(reference)->getParent() == unwrap(region) &&
961 "expected reference block to belong to the region");
962 unwrap(region)->getBlocks().insert(Region::iterator(unwrap(reference)),
963 unwrap(block));
964}
965
966void mlirRegionDestroy(MlirRegion region) {
967 delete static_cast<Region *>(region.ptr);
968}
969
970void mlirRegionTakeBody(MlirRegion target, MlirRegion source) {
971 unwrap(target)->takeBody(*unwrap(source));
972}
973
974//===----------------------------------------------------------------------===//
975// Block API.
976//===----------------------------------------------------------------------===//
977
978MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args,
979 MlirLocation const *locs) {
980 Block *b = new Block;
981 for (intptr_t i = 0; i < nArgs; ++i)
982 b->addArgument(unwrap(args[i]), unwrap(locs[i]));
983 return wrap(b);
984}
985
986bool mlirBlockEqual(MlirBlock block, MlirBlock other) {
987 return unwrap(block) == unwrap(other);
988}
989
990MlirOperation mlirBlockGetParentOperation(MlirBlock block) {
991 return wrap(unwrap(block)->getParentOp());
992}
993
994MlirRegion mlirBlockGetParentRegion(MlirBlock block) {
995 return wrap(unwrap(block)->getParent());
996}
997
998MlirBlock mlirBlockGetNextInRegion(MlirBlock block) {
999 return wrap(unwrap(block)->getNextNode());
1000}
1001
1002MlirOperation mlirBlockGetFirstOperation(MlirBlock block) {
1003 Block *cppBlock = unwrap(block);
1004 if (cppBlock->empty())
1005 return wrap(static_cast<Operation *>(nullptr));
1006 return wrap(&cppBlock->front());
1007}
1008
1009MlirOperation mlirBlockGetTerminator(MlirBlock block) {
1010 Block *cppBlock = unwrap(block);
1011 if (cppBlock->empty())
1012 return wrap(static_cast<Operation *>(nullptr));
1013 Operation &back = cppBlock->back();
1014 if (!back.hasTrait<OpTrait::IsTerminator>())
1015 return wrap(static_cast<Operation *>(nullptr));
1016 return wrap(&back);
1017}
1018
1019void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation) {
1020 unwrap(block)->push_back(unwrap(operation));
1021}
1022
1023void mlirBlockInsertOwnedOperation(MlirBlock block, intptr_t pos,
1024 MlirOperation operation) {
1025 auto &opList = unwrap(block)->getOperations();
1026 opList.insert(std::next(opList.begin(), pos), unwrap(operation));
1027}
1028
1030 MlirOperation reference,
1031 MlirOperation operation) {
1032 Block *cppBlock = unwrap(block);
1033 if (mlirOperationIsNull(reference)) {
1034 cppBlock->getOperations().insert(cppBlock->begin(), unwrap(operation));
1035 return;
1036 }
1037
1038 assert(unwrap(reference)->getBlock() == unwrap(block) &&
1039 "expected reference operation to belong to the block");
1040 cppBlock->getOperations().insertAfter(Block::iterator(unwrap(reference)),
1041 unwrap(operation));
1042}
1043
1045 MlirOperation reference,
1046 MlirOperation operation) {
1047 if (mlirOperationIsNull(reference))
1048 return mlirBlockAppendOwnedOperation(block, operation);
1049
1050 assert(unwrap(reference)->getBlock() == unwrap(block) &&
1051 "expected reference operation to belong to the block");
1052 unwrap(block)->getOperations().insert(Block::iterator(unwrap(reference)),
1053 unwrap(operation));
1054}
1055
1056void mlirBlockDestroy(MlirBlock block) { delete unwrap(block); }
1057
1058void mlirBlockDetach(MlirBlock block) {
1059 Block *b = unwrap(block);
1060 b->getParent()->getBlocks().remove(b);
1061}
1062
1064 return static_cast<intptr_t>(unwrap(block)->getNumArguments());
1065}
1066
1067MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type,
1068 MlirLocation loc) {
1069 return wrap(unwrap(block)->addArgument(unwrap(type), unwrap(loc)));
1070}
1071
1072void mlirBlockEraseArgument(MlirBlock block, unsigned index) {
1073 return unwrap(block)->eraseArgument(index);
1074}
1075
1076MlirValue mlirBlockInsertArgument(MlirBlock block, intptr_t pos, MlirType type,
1077 MlirLocation loc) {
1078 return wrap(unwrap(block)->insertArgument(pos, unwrap(type), unwrap(loc)));
1079}
1080
1081MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos) {
1082 return wrap(unwrap(block)->getArgument(static_cast<unsigned>(pos)));
1083}
1084
1085void mlirBlockPrint(MlirBlock block, MlirStringCallback callback,
1086 void *userData) {
1087 detail::CallbackOstream stream(callback, userData);
1088 unwrap(block)->print(stream);
1089}
1090
1092 return static_cast<intptr_t>(unwrap(block)->getNumSuccessors());
1093}
1094
1095MlirBlock mlirBlockGetSuccessor(MlirBlock block, intptr_t pos) {
1096 return wrap(unwrap(block)->getSuccessor(static_cast<unsigned>(pos)));
1097}
1098
1100 Block *b = unwrap(block);
1101 return static_cast<intptr_t>(std::distance(b->pred_begin(), b->pred_end()));
1102}
1103
1104MlirBlock mlirBlockGetPredecessor(MlirBlock block, intptr_t pos) {
1105 Block *b = unwrap(block);
1106 Block::pred_iterator it = b->pred_begin();
1107 std::advance(it, pos);
1108 return wrap(*it);
1109}
1110
1111//===----------------------------------------------------------------------===//
1112// Value API.
1113//===----------------------------------------------------------------------===//
1114
1115bool mlirValueEqual(MlirValue value1, MlirValue value2) {
1116 return unwrap(value1) == unwrap(value2);
1117}
1118
1119bool mlirValueIsABlockArgument(MlirValue value) {
1120 return llvm::isa<BlockArgument>(unwrap(value));
1121}
1122
1123bool mlirValueIsAOpResult(MlirValue value) {
1124 return llvm::isa<OpResult>(unwrap(value));
1125}
1126
1127MlirBlock mlirBlockArgumentGetOwner(MlirValue value) {
1128 return wrap(llvm::dyn_cast<BlockArgument>(unwrap(value)).getOwner());
1129}
1130
1132 return static_cast<intptr_t>(
1133 llvm::dyn_cast<BlockArgument>(unwrap(value)).getArgNumber());
1134}
1135
1136void mlirBlockArgumentSetType(MlirValue value, MlirType type) {
1137 if (auto blockArg = llvm::dyn_cast<BlockArgument>(unwrap(value)))
1138 blockArg.setType(unwrap(type));
1139}
1140
1141void mlirBlockArgumentSetLocation(MlirValue value, MlirLocation loc) {
1142 if (auto blockArg = llvm::dyn_cast<BlockArgument>(unwrap(value)))
1143 blockArg.setLoc(unwrap(loc));
1144}
1145
1146MlirOperation mlirOpResultGetOwner(MlirValue value) {
1147 return wrap(llvm::dyn_cast<OpResult>(unwrap(value)).getOwner());
1148}
1149
1151 return static_cast<intptr_t>(
1152 llvm::dyn_cast<OpResult>(unwrap(value)).getResultNumber());
1153}
1154
1155MlirType mlirValueGetType(MlirValue value) {
1156 return wrap(unwrap(value).getType());
1157}
1158
1159void mlirValueSetType(MlirValue value, MlirType type) {
1160 unwrap(value).setType(unwrap(type));
1161}
1162
1163void mlirValueDump(MlirValue value) { unwrap(value).dump(); }
1164
1165void mlirValuePrint(MlirValue value, MlirStringCallback callback,
1166 void *userData) {
1167 detail::CallbackOstream stream(callback, userData);
1168 unwrap(value).print(stream);
1169}
1170
1171void mlirValuePrintAsOperand(MlirValue value, MlirAsmState state,
1172 MlirStringCallback callback, void *userData) {
1173 detail::CallbackOstream stream(callback, userData);
1174 Value cppValue = unwrap(value);
1175 cppValue.printAsOperand(stream, *unwrap(state));
1176}
1177
1178MlirOpOperand mlirValueGetFirstUse(MlirValue value) {
1179 Value cppValue = unwrap(value);
1180 if (cppValue.use_empty())
1181 return {};
1182
1183 OpOperand *opOperand = cppValue.use_begin().getOperand();
1184
1185 return wrap(opOperand);
1186}
1187
1188void mlirValueReplaceAllUsesOfWith(MlirValue oldValue, MlirValue newValue) {
1189 unwrap(oldValue).replaceAllUsesWith(unwrap(newValue));
1190}
1191
1192void mlirValueReplaceAllUsesExcept(MlirValue oldValue, MlirValue newValue,
1193 intptr_t numExceptions,
1194 MlirOperation *exceptions) {
1195 Value oldValueCpp = unwrap(oldValue);
1196 Value newValueCpp = unwrap(newValue);
1197
1199 for (intptr_t i = 0; i < numExceptions; ++i) {
1200 exceptionSet.insert(unwrap(exceptions[i]));
1201 }
1202
1203 oldValueCpp.replaceAllUsesExcept(newValueCpp, exceptionSet);
1204}
1205
1206MlirLocation mlirValueGetLocation(MlirValue v) {
1207 return wrap(unwrap(v).getLoc());
1208}
1209
1210MlirContext mlirValueGetContext(MlirValue v) {
1211 return wrap(unwrap(v).getContext());
1212}
1213
1214//===----------------------------------------------------------------------===//
1215// OpOperand API.
1216//===----------------------------------------------------------------------===//
1217
1218bool mlirOpOperandIsNull(MlirOpOperand opOperand) { return !opOperand.ptr; }
1219
1220MlirOperation mlirOpOperandGetOwner(MlirOpOperand opOperand) {
1221 return wrap(unwrap(opOperand)->getOwner());
1222}
1223
1224MlirValue mlirOpOperandGetValue(MlirOpOperand opOperand) {
1225 return wrap(unwrap(opOperand)->get());
1226}
1227
1228unsigned mlirOpOperandGetOperandNumber(MlirOpOperand opOperand) {
1229 return unwrap(opOperand)->getOperandNumber();
1230}
1231
1232MlirOpOperand mlirOpOperandGetNextUse(MlirOpOperand opOperand) {
1233 if (mlirOpOperandIsNull(opOperand))
1234 return {};
1235
1236 OpOperand *nextOpOperand = static_cast<OpOperand *>(
1237 unwrap(opOperand)->getNextOperandUsingThisValue());
1238
1239 if (!nextOpOperand)
1240 return {};
1241
1242 return wrap(nextOpOperand);
1243}
1244
1245//===----------------------------------------------------------------------===//
1246// Type API.
1247//===----------------------------------------------------------------------===//
1248
1249MlirType mlirTypeParseGet(MlirContext context, MlirStringRef type) {
1250 return wrap(mlir::parseType(unwrap(type), unwrap(context)));
1251}
1252
1253MlirContext mlirTypeGetContext(MlirType type) {
1254 return wrap(unwrap(type).getContext());
1255}
1256
1257MlirTypeID mlirTypeGetTypeID(MlirType type) {
1258 return wrap(unwrap(type).getTypeID());
1259}
1260
1261MlirDialect mlirTypeGetDialect(MlirType type) {
1262 return wrap(&unwrap(type).getDialect());
1263}
1264
1265bool mlirTypeEqual(MlirType t1, MlirType t2) {
1266 return unwrap(t1) == unwrap(t2);
1267}
1268
1269void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData) {
1270 detail::CallbackOstream stream(callback, userData);
1271 unwrap(type).print(stream);
1272}
1273
1274void mlirTypeDump(MlirType type) { unwrap(type).dump(); }
1275
1276//===----------------------------------------------------------------------===//
1277// Attribute API.
1278//===----------------------------------------------------------------------===//
1279
1280MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr) {
1281 return wrap(mlir::parseAttribute(unwrap(attr), unwrap(context)));
1282}
1283
1284MlirContext mlirAttributeGetContext(MlirAttribute attribute) {
1285 return wrap(unwrap(attribute).getContext());
1286}
1287
1288MlirType mlirAttributeGetType(MlirAttribute attribute) {
1289 Attribute attr = unwrap(attribute);
1290 if (auto typedAttr = llvm::dyn_cast<TypedAttr>(attr))
1291 return wrap(typedAttr.getType());
1292 return wrap(NoneType::get(attr.getContext()));
1293}
1294
1295MlirTypeID mlirAttributeGetTypeID(MlirAttribute attr) {
1296 return wrap(unwrap(attr).getTypeID());
1297}
1298
1299MlirDialect mlirAttributeGetDialect(MlirAttribute attr) {
1300 return wrap(&unwrap(attr).getDialect());
1301}
1302
1303bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2) {
1304 return unwrap(a1) == unwrap(a2);
1305}
1306
1307void mlirAttributePrint(MlirAttribute attr, MlirStringCallback callback,
1308 void *userData) {
1309 detail::CallbackOstream stream(callback, userData);
1310 unwrap(attr).print(stream);
1311}
1312
1313void mlirAttributeDump(MlirAttribute attr) { unwrap(attr).dump(); }
1314
1316 MlirAttribute attr) {
1317 return MlirNamedAttribute{name, attr};
1318}
1319
1320//===----------------------------------------------------------------------===//
1321// Identifier API.
1322//===----------------------------------------------------------------------===//
1323
1324MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str) {
1325 return wrap(StringAttr::get(unwrap(context), unwrap(str)));
1326}
1327
1328MlirContext mlirIdentifierGetContext(MlirIdentifier ident) {
1329 return wrap(unwrap(ident).getContext());
1330}
1331
1332bool mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other) {
1333 return unwrap(ident) == unwrap(other);
1334}
1335
1336MlirStringRef mlirIdentifierStr(MlirIdentifier ident) {
1337 return wrap(unwrap(ident).strref());
1338}
1339
1340//===----------------------------------------------------------------------===//
1341// Symbol and SymbolTable API.
1342//===----------------------------------------------------------------------===//
1343
1347
1351
1352MlirSymbolTable mlirSymbolTableCreate(MlirOperation operation) {
1353 if (!unwrap(operation)->hasTrait<OpTrait::SymbolTable>())
1354 return wrap(static_cast<SymbolTable *>(nullptr));
1355 return wrap(new SymbolTable(unwrap(operation)));
1356}
1357
1358void mlirSymbolTableDestroy(MlirSymbolTable symbolTable) {
1359 delete unwrap(symbolTable);
1360}
1361
1362MlirOperation mlirSymbolTableLookup(MlirSymbolTable symbolTable,
1363 MlirStringRef name) {
1364 return wrap(unwrap(symbolTable)->lookup(StringRef(name.data, name.length)));
1365}
1366
1367MlirAttribute mlirSymbolTableInsert(MlirSymbolTable symbolTable,
1368 MlirOperation operation) {
1369 return wrap((Attribute)unwrap(symbolTable)->insert(unwrap(operation)));
1370}
1371
1372void mlirSymbolTableErase(MlirSymbolTable symbolTable,
1373 MlirOperation operation) {
1374 unwrap(symbolTable)->erase(unwrap(operation));
1375}
1376
1378 MlirStringRef newSymbol,
1379 MlirOperation from) {
1380 auto *cppFrom = unwrap(from);
1381 auto *context = cppFrom->getContext();
1382 auto oldSymbolAttr = StringAttr::get(context, unwrap(oldSymbol));
1383 auto newSymbolAttr = StringAttr::get(context, unwrap(newSymbol));
1384 return wrap(SymbolTable::replaceAllSymbolUses(oldSymbolAttr, newSymbolAttr,
1385 unwrap(from)));
1386}
1387
1388void mlirSymbolTableWalkSymbolTables(MlirOperation from, bool allSymUsesVisible,
1389 void (*callback)(MlirOperation, bool,
1390 void *userData),
1391 void *userData) {
1392 SymbolTable::walkSymbolTables(unwrap(from), allSymUsesVisible,
1393 [&](Operation *foundOpCpp, bool isVisible) {
1394 callback(wrap(foundOpCpp), isVisible,
1395 userData);
1396 });
1397}
return success()
lhs
unsigned mlirOpOperandGetOperandNumber(MlirOpOperand opOperand)
Returns the operand number of an op operand.
Definition IR.cpp:1228
MlirLocation mlirValueGetLocation(MlirValue v)
Gets the location of the value.
Definition IR.cpp:1206
void mlirContextDestroy(MlirContext context)
Takes an MLIR context owned by the caller and destroys it.
Definition IR.cpp:70
void mlirOperationDump(MlirOperation op)
Prints an operation to stderr.
Definition IR.cpp:859
MlirAttribute mlirOperationGetDiscardableAttributeByName(MlirOperation op, MlirStringRef name)
Definition IR.cpp:780
MlirOperation mlirSymbolTableLookup(MlirSymbolTable symbolTable, MlirStringRef name)
Looks up a symbol with the given name in the given symbol table and returns the operation that corres...
Definition IR.cpp:1362
MlirRegion mlirRegionCreate()
Creates a new empty region and transfers ownership to the caller.
Definition IR.cpp:918
void mlirOperationPrint(MlirOperation op, MlirStringCallback callback, void *userData)
Prints a location by sending chunks of the string representation and forwarding userData to callback`...
Definition IR.cpp:824
MlirContext mlirModuleGetContext(MlirModule module)
Definition IR.cpp:445
void mlirValuePrint(MlirValue value, MlirStringCallback callback, void *userData)
Prints a block by sending chunks of the string representation and forwarding userData to callback`.
Definition IR.cpp:1165
MlirContext mlirLocationGetContext(MlirLocation location)
Gets the context that a location was created with.
Definition IR.cpp:410
size_t mlirModuleHashValue(MlirModule mod)
Definition IR.cpp:471
intptr_t mlirBlockGetNumPredecessors(MlirBlock block)
Definition IR.cpp:1099
bool mlirLocationIsAName(MlirLocation location)
Checks whether the given location is an Name.
Definition IR.cpp:398
MlirOperation mlirOpOperandGetOwner(MlirOpOperand opOperand)
Returns the owner operation of an op operand.
Definition IR.cpp:1220
bool mlirDialectEqual(MlirDialect dialect1, MlirDialect dialect2)
Checks if two dialects that belong to the same context are equal.
Definition IR.cpp:132
MlirRegion mlirRegionGetNextInOperation(MlirRegion region)
Returns the region immediately following the given region in its parent operation.
Definition IR.cpp:695
MlirLogicalResult mlirOperationWriteBytecodeWithConfig(MlirOperation op, MlirBytecodeWriterConfig config, MlirStringCallback callback, void *userData)
Same as mlirOperationWriteBytecode but with writer config and returns failure only if desired bytecod...
Definition IR.cpp:852
MlirIdentifier mlirOperationGetName(MlirOperation op)
Definition IR.cpp:668
void mlirDialectRegistryDestroy(MlirDialectRegistry registry)
Takes a dialect registry owned by the caller and destroys it.
Definition IR.cpp:148
bool mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other)
Checks whether two identifiers are the same.
Definition IR.cpp:1332
void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags, MlirStringCallback callback, void *userData)
Same as mlirOperationPrint but accepts flags controlling the printing behavior.
Definition IR.cpp:830
bool mlirValueIsABlockArgument(MlirValue value)
Definition IR.cpp:1119
MlirTypeID mlirTypeGetTypeID(MlirType type)
Gets the type ID of the type.
Definition IR.cpp:1257
void mlirValueReplaceAllUsesOfWith(MlirValue oldValue, MlirValue newValue)
Replace all uses of 'of' value with the 'with' value, updating anything in the IR that uses 'of' to u...
Definition IR.cpp:1188
MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type, MlirLocation loc)
Appends an argument of the specified type to the block.
Definition IR.cpp:1067
intptr_t mlirOperationGetNumRegions(MlirOperation op)
Definition IR.cpp:680
MlirBlock mlirOperationGetBlock(MlirOperation op)
Definition IR.cpp:672
void mlirContextAppendDialectRegistry(MlirContext ctx, MlirDialectRegistry registry)
Append the contents of the given dialect registry to the registry associated with the context.
Definition IR.cpp:83
MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str)
Gets an identifier with the given string value.
Definition IR.cpp:1324
void mlirBlockArgumentSetType(MlirValue value, MlirType type)
Definition IR.cpp:1136
MlirLocation mlirLocationFileLineColGet(MlirContext context, MlirStringRef filename, unsigned line, unsigned col)
Creates an File/Line/Column location owned by the given context.
Definition IR.cpp:272
intptr_t mlirContextGetNumLoadedDialects(MlirContext context)
Returns the number of dialects loaded by the context.
Definition IR.cpp:90
MlirNamedAttribute mlirNamedAttributeGet(MlirIdentifier name, MlirAttribute attr)
Associates an attribute with the name. Takes ownership of neither.
Definition IR.cpp:1315
MlirStringRef mlirSymbolTableGetVisibilityAttributeName()
Returns the name of the attribute used to store symbol visibility.
Definition IR.cpp:1348
void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n, MlirNamedAttribute const *attributes)
Definition IR.cpp:520
MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos)
Definition IR.cpp:735
bool mlirLocationIsACallSite(MlirLocation location)
Checks whether the given location is an CallSite.
Definition IR.cpp:342
void mlirSymbolTableWalkSymbolTables(MlirOperation from, bool allSymUsesVisible, void(*callback)(MlirOperation, bool, void *userData), void *userData)
Walks all symbol table operations nested within, and including, op.
Definition IR.cpp:1388
void mlirContextLoadAllAvailableDialects(MlirContext context)
Eagerly loads all available dialects registered with a context, making them available for use for IR ...
Definition IR.cpp:107
MlirModule mlirModuleCreateParseFromFile(MlirContext context, MlirStringRef fileName)
Definition IR.cpp:436
void mlirAttributePrint(MlirAttribute attr, MlirStringCallback callback, void *userData)
Prints a location by sending chunks of the string representation and forwarding userData to callback`...
Definition IR.cpp:1307
MlirBlock mlirRegionGetFirstBlock(MlirRegion region)
Gets the first block in the region.
Definition IR.cpp:924
MlirLogicalResult mlirSymbolTableReplaceAllSymbolUses(MlirStringRef oldSymbol, MlirStringRef newSymbol, MlirOperation from)
Attempt to replace all uses that are nested within the given operation of the given symbol 'oldSymbol...
Definition IR.cpp:1377
MlirLocation mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations, MlirLocation const *locations, MlirAttribute metadata)
Creates a fused location with an array of locations and metadata.
Definition IR.cpp:346
MlirAsmState mlirAsmStateCreateForValue(MlirValue value, MlirOpPrintingFlags flags)
Definition IR.cpp:177
intptr_t mlirOperationGetNumResults(MlirOperation op)
Definition IR.cpp:731
MlirOpOperand mlirValueGetFirstUse(MlirValue value)
Returns an op operand representing the first use of the value, or a null op operand if there are no u...
Definition IR.cpp:1178
void mlirRegionInsertOwnedBlockAfter(MlirRegion region, MlirBlock reference, MlirBlock block)
Takes a block owned by the caller and inserts it after the (non-owned) reference block in the given r...
Definition IR.cpp:941
void mlirOperationDestroy(MlirOperation op)
Definition IR.cpp:638
void mlirValueReplaceAllUsesExcept(MlirValue oldValue, MlirValue newValue, intptr_t numExceptions, MlirOperation *exceptions)
Replace all uses of 'of' value with 'with' value, updating anything in the IR that uses 'of' to use '...
Definition IR.cpp:1192
void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation)
Takes an operation owned by the caller and appends it to the block.
Definition IR.cpp:1019
MlirRegion mlirOperationGetFirstRegion(MlirOperation op)
Returns first region attached to the operation.
Definition IR.cpp:688
MlirAttribute mlirOperationGetInherentAttributeByName(MlirOperation op, MlirStringRef name)
Definition IR.cpp:753
MlirDialect mlirContextGetOrLoadDialect(MlirContext context, MlirStringRef name)
Gets the dialect instance owned by the given context using the dialect namespace to identify it,...
Definition IR.cpp:94
MlirContext mlirAttributeGetContext(MlirAttribute attribute)
Definition IR.cpp:1284
MlirStringRef mlirSymbolTableGetSymbolAttributeName()
Returns the name of the attribute used to store symbol names compatible with symbol tables.
Definition IR.cpp:1344
void mlirBlockInsertOwnedOperation(MlirBlock block, intptr_t pos, MlirOperation operation)
Takes an operation owned by the caller and inserts it as pos to the block.
Definition IR.cpp:1023
MlirRegion mlirBlockGetParentRegion(MlirBlock block)
Returns the region that contains this block.
Definition IR.cpp:994
MlirType mlirValueGetType(MlirValue value)
Definition IR.cpp:1155
void mlirOperationMoveAfter(MlirOperation op, MlirOperation other)
Moves the given operation immediately after the other operation in its parent block.
Definition IR.cpp:865
void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block)
Takes a block owned by the caller and appends it to the given region.
Definition IR.cpp:931
void mlirBlockPrint(MlirBlock block, MlirStringCallback callback, void *userData)
Definition IR.cpp:1085
void mlirOperationSetDiscardableAttributeByName(MlirOperation op, MlirStringRef name, MlirAttribute attr)
Definition IR.cpp:785
MlirOpPrintingFlags mlirOpPrintingFlagsCreate()
Definition IR.cpp:201
bool mlirModuleEqual(MlirModule lhs, MlirModule rhs)
Definition IR.cpp:467
void mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags, intptr_t largeElementLimit)
Definition IR.cpp:209
MlirBlock mlirBlockGetNextInRegion(MlirBlock block)
Returns the block immediately following the given block in its parent region.
Definition IR.cpp:998
void mlirOperationSetSuccessor(MlirOperation op, intptr_t pos, MlirBlock block)
Definition IR.cpp:796
MlirValue mlirOpOperandGetValue(MlirOpOperand opOperand)
Returns the value of an op operand.
Definition IR.cpp:1224
#define APPEND_ELEMS(type, sizeName, elemName)
Definition IR.cpp:497
bool mlirContextIsRegisteredOperation(MlirContext context, MlirStringRef name)
Returns whether the given fully-qualified operation (i.e.
Definition IR.cpp:99
MlirOperation mlirOperationGetNextInBlock(MlirOperation op)
Definition IR.cpp:704
void mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, bool enable, bool prettyForm)
Definition IR.cpp:219
MlirOperation mlirModuleGetOperation(MlirModule module)
Definition IR.cpp:459
static mlir::WalkResult unwrap(MlirWalkResult result)
Definition IR.cpp:877
void mlirOpPrintingFlagsElideLargeResourceString(MlirOpPrintingFlags flags, intptr_t largeResourceLimit)
Definition IR.cpp:214
void mlirRegionInsertOwnedBlockBefore(MlirRegion region, MlirBlock reference, MlirBlock block)
Takes a block owned by the caller and inserts it before the (non-owned) reference block in the given ...
Definition IR.cpp:955
MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos)
Returns pos-th argument of the block.
Definition IR.cpp:1081
unsigned mlirLocationFusedGetNumLocations(MlirLocation location)
Getter for number of locations fused together.
Definition IR.cpp:354
MlirStringRef mlirDialectGetNamespace(MlirDialect dialect)
Returns the namespace of the given dialect.
Definition IR.cpp:136
void mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags)
Definition IR.cpp:232
MlirTypeID mlirOperationGetTypeID(MlirOperation op)
Definition IR.cpp:662
intptr_t mlirBlockArgumentGetArgNumber(MlirValue value)
Definition IR.cpp:1131
void mlirOperationWalk(MlirOperation op, MlirOperationWalkCallback callback, void *userData, MlirWalkOrder walkOrder)
Walks operation op in walkOrder and calls callback on that operation.
Definition IR.cpp:891
MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos)
Definition IR.cpp:743
void mlirBlockInsertOwnedOperationBefore(MlirBlock block, MlirOperation reference, MlirOperation operation)
Takes an operation owned by the caller and inserts it before the (non-owned) reference operation in t...
Definition IR.cpp:1044
bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2)
Definition IR.cpp:1303
MlirAsmState mlirAsmStateCreateForOperation(MlirOperation op, MlirOpPrintingFlags flags)
Definition IR.cpp:156
bool mlirOperationEqual(MlirOperation op, MlirOperation other)
Definition IR.cpp:642
void mlirOperationSetInherentAttributeByName(MlirOperation op, MlirStringRef name, MlirAttribute attr)
Definition IR.cpp:761
void mlirOpPrintingFlagsAssumeVerified(MlirOpPrintingFlags flags)
Definition IR.cpp:236
bool mlirValueEqual(MlirValue value1, MlirValue value2)
Definition IR.cpp:1115
MlirContext mlirIdentifierGetContext(MlirIdentifier ident)
Returns the context associated with this identifier.
Definition IR.cpp:1328
void mlirBytecodeWriterConfigDestroy(MlirBytecodeWriterConfig config)
Definition IR.cpp:251
MlirBlock mlirBlockGetSuccessor(MlirBlock block, intptr_t pos)
Definition IR.cpp:1095
void mlirModuleDestroy(MlirModule module)
Definition IR.cpp:453
MlirModule mlirModuleCreateEmpty(MlirLocation location)
Definition IR.cpp:424
void mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags)
Definition IR.cpp:224
MlirOperation mlirOperationGetParentOperation(MlirOperation op)
Definition IR.cpp:676
void mlirRegionTakeBody(MlirRegion target, MlirRegion source)
Moves the entire content of the source region to the target region.
Definition IR.cpp:970
MlirLlvmThreadPool mlirContextGetThreadPool(MlirContext context)
Gets the thread pool of the context when enabled multithreading, otherwise an assertion is raised.
Definition IR.cpp:120
MlirValue mlirBlockInsertArgument(MlirBlock block, intptr_t pos, MlirType type, MlirLocation loc)
Inserts an argument of the specified type at a specified index to the block.
Definition IR.cpp:1076
void mlirValueSetType(MlirValue value, MlirType type)
Definition IR.cpp:1159
intptr_t mlirOperationGetNumSuccessors(MlirOperation op)
Definition IR.cpp:739
MlirDialect mlirAttributeGetDialect(MlirAttribute attr)
Definition IR.cpp:1299
void mlirLocationPrint(MlirLocation location, MlirStringCallback callback, void *userData)
Definition IR.cpp:414
void mlirOperationSetAttributeByName(MlirOperation op, MlirStringRef name, MlirAttribute attr)
Definition IR.cpp:815
void mlirBlockDestroy(MlirBlock block)
Takes a block owned by the caller and destroys it.
Definition IR.cpp:1056
void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos, MlirBlock block)
Takes a block owned by the caller and inserts it at pos to the given region.
Definition IR.cpp:935
void mlirOperationSetOperand(MlirOperation op, intptr_t pos, MlirValue newValue)
Definition IR.cpp:720
intptr_t mlirBlockGetNumArguments(MlirBlock block)
Returns the number of arguments of the block.
Definition IR.cpp:1063
MlirTypeID mlirLocationFileLineColRangeGetTypeID()
TypeID Getter for FileLineColRange.
Definition IR.cpp:316
MlirOperation mlirOpResultGetOwner(MlirValue value)
Definition IR.cpp:1146
void mlirBlockEraseArgument(MlirBlock block, unsigned index)
Erase the argument at 'index' and remove it from the argument list.
Definition IR.cpp:1072
bool mlirOpOperandIsNull(MlirOpOperand opOperand)
Returns whether the op operand is null.
Definition IR.cpp:1218
MlirDialect mlirTypeGetDialect(MlirType type)
Gets the dialect a type belongs to.
Definition IR.cpp:1261
MlirModule mlirModuleCreateParse(MlirContext context, MlirStringRef module)
Definition IR.cpp:428
MlirContext mlirContextCreateWithRegistry(MlirDialectRegistry registry, bool threadingEnabled)
Creates an MLIR context, setting the multithreading setting explicitly and pre-loading the dialects f...
Definition IR.cpp:59
size_t mlirOperationHashValue(MlirOperation op)
Definition IR.cpp:646
void mlirContextSetAllowUnregisteredDialects(MlirContext context, bool allow)
Sets whether unregistered dialects are allowed in this context.
Definition IR.cpp:72
void mlirOperationStateAddResults(MlirOperationState *state, intptr_t n, MlirType const *results)
Definition IR.cpp:503
void mlirOperationMoveBefore(MlirOperation op, MlirOperation other)
Moves the given operation immediately before the other operation in its parent block.
Definition IR.cpp:869
static LogicalResult inferOperationTypes(OperationState &state)
Definition IR.cpp:533
MlirOperation mlirOperationClone(MlirOperation op)
Definition IR.cpp:634
void mlirValuePrintAsOperand(MlirValue value, MlirAsmState state, MlirStringCallback callback, void *userData)
Prints a value as an operand (i.e., the ValueID).
Definition IR.cpp:1171
MlirBlock mlirBlockArgumentGetOwner(MlirValue value)
Definition IR.cpp:1127
void mlirBlockArgumentSetLocation(MlirValue value, MlirLocation loc)
Definition IR.cpp:1141
MlirDialectRegistry mlirDialectRegistryCreate()
Creates a dialect registry and transfers its ownership to the caller.
Definition IR.cpp:144
MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos)
Definition IR.cpp:712
bool mlirContextGetAllowUnregisteredDialects(MlirContext context)
Returns whether the context allows unregistered dialects.
Definition IR.cpp:76
MlirAttribute mlirLocationGetAttribute(MlirLocation location)
Returns the underlying location attribute of this location.
Definition IR.cpp:264
MlirModule mlirModuleFromOperation(MlirOperation op)
Definition IR.cpp:463
MlirTypeID mlirLocationNameGetTypeID()
TypeID Getter for Name.
Definition IR.cpp:396
MlirOpOperand mlirOperationGetOpOperand(MlirOperation op, intptr_t pos)
Definition IR.cpp:716
MlirLocation mlirOperationGetLocation(MlirOperation op)
Definition IR.cpp:654
bool mlirTypeEqual(MlirType t1, MlirType t2)
Checks if two types are equal.
Definition IR.cpp:1265
bool mlirContextEqual(MlirContext ctx1, MlirContext ctx2)
Checks if two contexts are equal.
Definition IR.cpp:66
MlirAttribute mlirOperationGetAttributeByName(MlirOperation op, MlirStringRef name)
Definition IR.cpp:810
bool mlirOperationIsBeforeInBlock(MlirOperation op, MlirOperation other)
Given an operation 'other' that is within the same parent block, return whether the current operation...
Definition IR.cpp:873
MlirTypeID mlirAttributeGetTypeID(MlirAttribute attr)
Definition IR.cpp:1295
MlirAttribute mlirSymbolTableInsert(MlirSymbolTable symbolTable, MlirOperation operation)
Inserts the given operation into the given symbol table.
Definition IR.cpp:1367
bool mlirLocationIsAFileLineColRange(MlirLocation location)
Checks whether the given location is an FileLineColRange.
Definition IR.cpp:320
intptr_t mlirOperationGetNumDiscardableAttributes(MlirOperation op)
Definition IR.cpp:768
MlirSymbolTable mlirSymbolTableCreate(MlirOperation operation)
Creates a symbol table for the given operation.
Definition IR.cpp:1352
void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n, MlirRegion const *regions)
Definition IR.cpp:512
void mlirOperationSetLocation(MlirOperation op, MlirLocation loc)
Definition IR.cpp:658
MlirLocation mlirLocationUnknownGet(MlirContext context)
Creates a location with unknown position owned by the given context.
Definition IR.cpp:402
MlirLocation mlirLocationCallSiteGetCallee(MlirLocation location)
Getter for callee of CallSite.
Definition IR.cpp:328
static Operation * findParent(Operation *op, bool shouldUseLocalScope)
Definition IR.cpp:161
MlirOperation mlirBlockGetFirstOperation(MlirBlock block)
Returns the first operation in the block.
Definition IR.cpp:1002
MlirType mlirAttributeGetType(MlirAttribute attribute)
Definition IR.cpp:1288
bool mlirOperationRemoveDiscardableAttributeByName(MlirOperation op, MlirStringRef name)
Definition IR.cpp:791
void mlirRegionDestroy(MlirRegion region)
Takes a region owned by the caller and destroys it.
Definition IR.cpp:966
bool mlirOperationRemoveAttributeByName(MlirOperation op, MlirStringRef name)
Definition IR.cpp:820
bool mlirValueIsAOpResult(MlirValue value)
Definition IR.cpp:1123
MLIR_CAPI_EXPORTED bool mlirOperationHasInherentAttributeByName(MlirOperation op, MlirStringRef name)
Definition IR.cpp:748
MlirContext mlirContextCreateWithThreading(bool threadingEnabled)
Creates an MLIR context with an explicit setting of the multithreading setting and transfers its owne...
Definition IR.cpp:54
MlirTypeID mlirLocationCallSiteGetTypeID()
TypeID Getter for CallSite.
Definition IR.cpp:338
MlirBlock mlirBlockGetPredecessor(MlirBlock block, intptr_t pos)
Definition IR.cpp:1104
MlirOperation mlirBlockGetTerminator(MlirBlock block)
Returns the terminator operation in the block or null if no terminator.
Definition IR.cpp:1009
bool mlirLocationEqual(MlirLocation l1, MlirLocation l2)
Checks if two locations are equal.
Definition IR.cpp:406
MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos)
Definition IR.cpp:684
void mlirOperationReplaceUsesOfWith(MlirOperation op, MlirValue oldValue, MlirValue newValue)
Replace uses of 'of' value with the 'with' value inside the 'op' operation.
Definition IR.cpp:909
int mlirLocationFileLineColRangeGetEndColumn(MlirLocation location)
Getter for end_column of FileLineColRange.
Definition IR.cpp:310
MlirOperation mlirOperationCreate(MlirOperationState *state)
Definition IR.cpp:588
unsigned mlirContextGetNumThreads(MlirContext context)
Gets the number of threads of the thread pool of the context when multithreading is enabled.
Definition IR.cpp:116
void mlirBytecodeWriterConfigDesiredEmitVersion(MlirBytecodeWriterConfig flags, int64_t version)
Definition IR.cpp:255
int mlirLocationFileLineColRangeGetStartColumn(MlirLocation location)
Getter for start_column of FileLineColRange.
Definition IR.cpp:298
MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr)
Definition IR.cpp:1280
void mlirLocationFusedGetLocations(MlirLocation location, MlirLocation *locationsCPtr)
Getter for locations of Fused.
Definition IR.cpp:360
void mlirOperationRemoveFromParent(MlirOperation op)
Definition IR.cpp:640
void mlirSymbolTableDestroy(MlirSymbolTable symbolTable)
Destroys the symbol table created with mlirSymbolTableCreate.
Definition IR.cpp:1358
intptr_t mlirBlockGetNumSuccessors(MlirBlock block)
Definition IR.cpp:1091
MlirContext mlirContextCreate()
Creates an MLIR context and transfers its ownership to the caller.
Definition IR.cpp:44
void mlirOperationPrintWithState(MlirOperation op, MlirAsmState state, MlirStringCallback callback, void *userData)
Same as mlirOperationPrint but accepts AsmState controlling the printing behavior as well as caching ...
Definition IR.cpp:836
bool mlirOperationVerify(MlirOperation op)
Verify the operation and return true if it passes, false if it fails.
Definition IR.cpp:861
void mlirBlockDetach(MlirBlock block)
Detach a block from the owning region and assume ownership.
Definition IR.cpp:1058
int mlirLocationFileLineColRangeGetEndLine(MlirLocation location)
Getter for end_line of FileLineColRange.
Definition IR.cpp:304
MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos)
Definition IR.cpp:805
void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags)
Definition IR.cpp:205
MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller)
Creates a call site location with a callee and a caller.
Definition IR.cpp:324
void mlirOperationWriteBytecode(MlirOperation op, MlirStringCallback callback, void *userData)
Same as mlirOperationPrint but writing the bytecode format.
Definition IR.cpp:845
void mlirValueDump(MlirValue value)
Definition IR.cpp:1163
void mlirBlockInsertOwnedOperationAfter(MlirBlock block, MlirOperation reference, MlirOperation operation)
Takes an operation owned by the caller and inserts it after the (non-owned) reference operation in th...
Definition IR.cpp:1029
intptr_t mlirContextGetNumRegisteredDialects(MlirContext context)
Returns the number of dialects registered with the given context.
Definition IR.cpp:79
MlirTypeID mlirLocationFusedGetTypeID()
TypeID Getter for Fused.
Definition IR.cpp:372
void mlirOperationSetOperands(MlirOperation op, intptr_t nOperands, MlirValue const *operands)
Definition IR.cpp:725
static MLIRContext::Threading toThreadingEnum(bool threadingEnabled)
Definition IR.cpp:49
MlirIdentifier mlirLocationFileLineColRangeGetFilename(MlirLocation location)
Getter for filename of FileLineColRange.
Definition IR.cpp:288
MlirContext mlirTypeGetContext(MlirType type)
Gets the context that a type was created with.
Definition IR.cpp:1253
void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData)
Definition IR.cpp:1269
MlirBlock mlirModuleGetBody(MlirModule module)
Definition IR.cpp:449
MlirContext mlirDialectGetContext(MlirDialect dialect)
Returns the context that owns the dialect.
Definition IR.cpp:128
bool mlirRegionEqual(MlirRegion region, MlirRegion other)
Checks whether two region handles point to the same region.
Definition IR.cpp:920
int mlirLocationFileLineColRangeGetStartLine(MlirLocation location)
Getter for start_line of FileLineColRange.
Definition IR.cpp:292
MlirOperation mlirOperationCreateParse(MlirContext context, MlirStringRef sourceStr, MlirStringRef sourceName)
Definition IR.cpp:625
MlirLocation mlirLocationCallSiteGetCaller(MlirLocation location)
Getter for caller of CallSite.
Definition IR.cpp:333
void mlirAsmStateDestroy(MlirAsmState state)
Destroys printing flags created with mlirAsmStateCreate.
Definition IR.cpp:195
MlirContext mlirOperationGetContext(MlirOperation op)
Definition IR.cpp:650
intptr_t mlirOpResultGetResultNumber(MlirValue value)
Definition IR.cpp:1150
MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name, MlirLocation childLoc)
Creates a name location owned by the given context.
Definition IR.cpp:378
MlirLocation mlirLocationFileLineColRangeGet(MlirContext context, MlirStringRef filename, unsigned startLine, unsigned startCol, unsigned endLine, unsigned endCol)
Creates an File/Line/Column range location owned by the given context.
Definition IR.cpp:280
bool mlirBlockEqual(MlirBlock block, MlirBlock other)
Checks whether two blocks handles point to the same block.
Definition IR.cpp:986
bool mlirLocationIsAFused(MlirLocation location)
Checks whether the given location is an Fused.
Definition IR.cpp:374
MlirAttribute mlirLocationFusedGetMetadata(MlirLocation location)
Getter for metadata of Fused.
Definition IR.cpp:368
void mlirSymbolTableErase(MlirSymbolTable symbolTable, MlirOperation operation)
Removes the given operation from the symbol table and erases it.
Definition IR.cpp:1372
MlirLocation mlirLocationNameGetChildLoc(MlirLocation location)
Getter for childLoc of Name.
Definition IR.cpp:391
MlirNamedAttribute mlirOperationGetDiscardableAttribute(MlirOperation op, intptr_t pos)
Definition IR.cpp:773
void mlirOperationStateEnableResultTypeInference(MlirOperationState *state)
Definition IR.cpp:525
void mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n, MlirBlock const *successors)
Definition IR.cpp:516
MlirBytecodeWriterConfig mlirBytecodeWriterConfigCreate()
Definition IR.cpp:247
void mlirAttributeDump(MlirAttribute attr)
Prints the attribute to the standard error stream.
Definition IR.cpp:1313
void mlirOpPrintingFlagsPrintNameLocAsPrefix(MlirOpPrintingFlags flags)
Definition IR.cpp:228
MlirIdentifier mlirLocationNameGetName(MlirLocation location)
Getter for name of Name.
Definition IR.cpp:387
MlirStringRef mlirIdentifierStr(MlirIdentifier ident)
Gets the string value of the identifier.
Definition IR.cpp:1336
void mlirOpPrintingFlagsSkipRegions(MlirOpPrintingFlags flags)
Definition IR.cpp:240
void mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n, MlirValue const *operands)
Definition IR.cpp:508
MlirOperationState mlirOperationStateGet(MlirStringRef name, MlirLocation loc)
Definition IR.cpp:479
MlirLocation mlirLocationFromAttribute(MlirAttribute attribute)
Creates a location from a location attribute.
Definition IR.cpp:268
MlirOperation mlirBlockGetParentOperation(MlirBlock block)
Returns the closest surrounding operation that contains this block.
Definition IR.cpp:990
MlirOpOperand mlirOpOperandGetNextUse(MlirOpOperand opOperand)
Returns an op operand representing the next use of the value, or a null op operand if there is no nex...
Definition IR.cpp:1232
intptr_t mlirOperationGetNumOperands(MlirOperation op)
Definition IR.cpp:708
void mlirContextSetThreadPool(MlirContext context, MlirLlvmThreadPool threadPool)
Sets the thread pool of the context explicitly, enabling multithreading in the process.
Definition IR.cpp:111
MlirType mlirTypeParseGet(MlirContext context, MlirStringRef type)
Parses a type. The type is owned by the context.
Definition IR.cpp:1249
MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args, MlirLocation const *locs)
Creates a new empty block with the given argument types and transfers ownership to the caller.
Definition IR.cpp:978
void mlirContextEnableMultithreading(MlirContext context, bool enable)
Set threading mode (must be set to false to mlir-print-ir-after-all).
Definition IR.cpp:103
void mlirTypeDump(MlirType type)
Definition IR.cpp:1274
intptr_t mlirOperationGetNumAttributes(MlirOperation op)
Definition IR.cpp:801
MlirContext mlirValueGetContext(MlirValue v)
Gets the context that a value was created with.
Definition IR.cpp:1210
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
b getContext())
static llvm::ArrayRef< CppTy > unwrapList(size_t size, CTy *first, llvm::SmallVectorImpl< CppTy > &storage)
Definition Wrap.h:40
This class provides management for the lifetime of the state used when printing the IR.
Definition AsmState.h:542
Attributes are known-constant values of operations.
Definition Attributes.h:25
MLIRContext * getContext() const
Return the context this attribute belongs to.
Block represents an ordered list of Operations.
Definition Block.h:33
OpListType::iterator iterator
Definition Block.h:150
bool empty()
Definition Block.h:158
OpListType & getOperations()
Definition Block.h:147
Operation & front()
Definition Block.h:163
Operation & back()
Definition Block.h:162
PredecessorIterator pred_iterator
Definition Block.h:245
iterator begin()
Definition Block.h:153
This class contains the configuration used for the bytecode writer.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
static FileLineColLoc get(StringAttr filename, unsigned line, unsigned column)
Definition Location.cpp:157
Location objects represent source locations information in MLIR.
Definition Location.h:32
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
DictionaryAttr getDictionary(MLIRContext *context) const
Return a dictionary attribute for the underlying dictionary.
void reserve(size_type N)
NamedAttribute represents a combination of a name and an Attribute value.
Definition Attributes.h:164
StringAttr getName() const
Return the name of the attribute.
Attribute getValue() const
Return the value of the attribute.
Definition Attributes.h:179
This class represents an operand of an operation.
Definition Value.h:257
Set of flags used to control the behavior of the various IR print methods (e.g.
This class provides the API for ops that are known to be isolated from above.
This class provides the API for ops that are known to be terminators.
Simple wrapper around a void* in order to express generically how to pass in op properties through AP...
StringRef getStringRef() const
Return the name of this operation. This always succeeds.
std::optional< RegisteredOperationName > getRegisteredInfo() const
If this operation is registered, returns the registered information, std::nullopt otherwise.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
Region & getRegion(unsigned index)
Returns the region held by this operation at position 'index'.
Definition Operation.h:686
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Definition Operation.h:749
unsigned getNumRegions()
Returns the number of regions held by this operation.
Definition Operation.h:674
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
Definition Operation.h:234
static Operation * create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, NamedAttrList &&attributes, OpaqueProperties properties, BlockRange successors, unsigned numRegions)
Create a new Operation with the specific fields.
Definition Operation.cpp:67
This class acts as an owning reference to an op, and will automatically destroy the held op on destru...
Definition OwningOpRef.h:29
OpTy release()
Release the referenced op.
Definition OwningOpRef.h:67
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
Block & front()
Definition Region.h:65
unsigned getRegionNumber()
Return the number of this region in the parent operation.
Definition Region.cpp:62
bool empty()
Definition Region.h:60
iterator begin()
Definition Region.h:55
Operation * getParentOp()
Return the parent operation this region is attached to.
Definition Region.h:200
BlockListType & getBlocks()
Definition Region.h:45
BlockListType::iterator iterator
Definition Region.h:52
This class allows for representing and managing the symbol table used by operations with the 'SymbolT...
Definition SymbolTable.h:24
static StringRef getSymbolAttrName()
Return the name of the attribute used for symbol names.
Definition SymbolTable.h:76
static LogicalResult replaceAllSymbolUses(StringAttr oldSymbol, StringAttr newSymbol, Operation *from)
Attempt to replace all uses of the given symbol 'oldSymbol' with the provided symbol 'newSymbol' that...
static StringRef getVisibilityAttrName()
Return the name of the attribute used for symbol visibility.
Definition SymbolTable.h:82
static void walkSymbolTables(Operation *op, bool allSymUsesVisible, function_ref< void(Operation *, bool)> callback)
Walks all symbol table operations nested within, and including, op.
OperandType * getOperand() const
Returns the current operands.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
bool use_empty() const
Returns true if this value has no uses.
Definition Value.h:208
void replaceAllUsesExcept(Value newValue, const SmallPtrSetImpl< Operation * > &exceptions)
Replace all uses of 'this' value with 'newValue', updating anything in the IR that uses 'this' to use...
Definition Value.cpp:71
void printAsOperand(raw_ostream &os, AsmState &state) const
Print this value as if it were an operand.
Location getLoc() const
Return the location of this value.
Definition Value.cpp:24
use_iterator use_begin() const
Definition Value.h:184
A utility result that is used to signal how to proceed with an ongoing walk:
Definition WalkResult.h:29
static WalkResult skip()
Definition WalkResult.h:48
static WalkResult advance()
Definition WalkResult.h:47
static WalkResult interrupt()
Definition WalkResult.h:46
A simple raw ostream subclass that forwards write_impl calls to the user-supplied callback together w...
Definition Utils.h:30
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition Diagnostics.h:24
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition Diagnostics.h:19
MlirWalkResult(* MlirOperationWalkCallback)(MlirOperation, void *userData)
Operation walker type.
Definition IR.h:855
MlirWalkOrder
Traversal order for operation walk.
Definition IR.h:848
@ MlirWalkPreOrder
Definition IR.h:849
@ MlirWalkPostOrder
Definition IR.h:850
MlirWalkResult
Operation walk result.
Definition IR.h:841
@ MlirWalkResultInterrupt
Definition IR.h:843
@ MlirWalkResultSkip
Definition IR.h:844
@ MlirWalkResultAdvance
Definition IR.h:842
static bool mlirBlockIsNull(MlirBlock block)
Checks whether a block is null.
Definition IR.h:941
static bool mlirLocationIsNull(MlirLocation location)
Checks if the location is null.
Definition IR.h:370
#define MLIR_CAPI_EXPORTED
Definition Support.h:46
void(* MlirStringCallback)(MlirStringRef, void *)
A callback for returning string references.
Definition Support.h:110
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition Utils.cpp:305
const FrozenRewritePatternSet GreedyRewriteConfig config
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
Attribute parseAttribute(llvm::StringRef attrStr, MLIRContext *context, Type type={}, size_t *numRead=nullptr, bool isKnownNullTerminated=false)
This parses a single MLIR attribute to an MLIR context if it was valid.
LogicalResult parseSourceString(llvm::StringRef sourceStr, Block *block, const ParserConfig &config, StringRef sourceName="", LocationAttr *sourceFileLoc=nullptr)
This parses the IR string and appends parsed operations to the given block.
Definition Parser.cpp:108
Operation * clone(OpBuilder &b, Operation *op, TypeRange newResultTypes, ValueRange newOperands)
LogicalResult parseSourceFile(const llvm::SourceMgr &sourceMgr, Block *block, const ParserConfig &config, LocationAttr *sourceFileLoc=nullptr)
This parses the file specified by the indicated SourceMgr and appends parsed operations to the given ...
Definition Parser.cpp:38
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
Type parseType(llvm::StringRef typeStr, MLIRContext *context, size_t *numRead=nullptr, bool isKnownNullTerminated=false)
This parses a single MLIR type to an MLIR context if it was valid.
LogicalResult writeBytecodeToFile(Operation *op, raw_ostream &os, const BytecodeWriterConfig &config={})
Write the bytecode for the given operation to the provided output stream.
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...
Definition Verifier.cpp:423
A logical result value, essentially a boolean with named states.
Definition Support.h:121
Named MLIR attribute.
Definition IR.h:76
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:78
const char * data
Pointer to the first symbol.
Definition Support.h:79
size_t length
Length of the fragment.
Definition Support.h:80
static llvm::hash_code computeHash(Operation *op, function_ref< llvm::hash_code(Value)> hashOperands=[](Value v) { return hash_value(v);}, function_ref< llvm::hash_code(Value)> hashResults=[](Value v) { return hash_value(v);}, Flags flags=Flags::None)
Compute a hash for the given operation.
This represents an operation in an abstracted form, suitable for use with the builder APIs.
OpaqueProperties getRawProperties()
SmallVector< Value, 4 > operands
void addOperands(ValueRange newOperands)
void addAttribute(StringRef name, Attribute attr)
Add an attribute with the specified name.
void addSuccessors(Block *successor)
Adds a successor to the operation sate. successor must not be null.
void addTypes(ArrayRef< Type > newTypes)
MLIRContext * getContext() const
Get the context held by this operation state.
SmallVector< std::unique_ptr< Region >, 1 > regions
Regions that the op will hold.
SmallVector< Type, 4 > types
Types of the results of this operation.
Region * addRegion()
Create a region that should be attached to the operation.