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
407 return wrap(UnknownLoc::getTypeID());
408}
409
410bool mlirLocationIsAUnknown(MlirLocation location) {
411 return isa<UnknownLoc>(unwrap(location));
412}
413
414bool mlirLocationEqual(MlirLocation l1, MlirLocation l2) {
415 return unwrap(l1) == unwrap(l2);
416}
417
418MlirContext mlirLocationGetContext(MlirLocation location) {
419 return wrap(unwrap(location).getContext());
420}
421
422void mlirLocationPrint(MlirLocation location, MlirStringCallback callback,
423 void *userData) {
424 detail::CallbackOstream stream(callback, userData);
425 unwrap(location).print(stream);
426}
427
428//===----------------------------------------------------------------------===//
429// Module API.
430//===----------------------------------------------------------------------===//
431
432MlirModule mlirModuleCreateEmpty(MlirLocation location) {
433 return wrap(ModuleOp::create(unwrap(location)));
434}
435
436MlirModule mlirModuleCreateParse(MlirContext context, MlirStringRef module) {
437 OwningOpRef<ModuleOp> owning =
438 parseSourceString<ModuleOp>(unwrap(module), unwrap(context));
439 if (!owning)
440 return MlirModule{nullptr};
441 return MlirModule{owning.release().getOperation()};
442}
443
444MlirModule mlirModuleCreateParseFromFile(MlirContext context,
445 MlirStringRef fileName) {
446 OwningOpRef<ModuleOp> owning =
447 parseSourceFile<ModuleOp>(unwrap(fileName), unwrap(context));
448 if (!owning)
449 return MlirModule{nullptr};
450 return MlirModule{owning.release().getOperation()};
451}
452
453MlirContext mlirModuleGetContext(MlirModule module) {
454 return wrap(unwrap(module).getContext());
455}
456
457MlirBlock mlirModuleGetBody(MlirModule module) {
458 return wrap(unwrap(module).getBody());
459}
460
461void mlirModuleDestroy(MlirModule module) {
462 // Transfer ownership to an OwningOpRef<ModuleOp> so that its destructor is
463 // called.
465}
466
467MlirOperation mlirModuleGetOperation(MlirModule module) {
468 return wrap(unwrap(module).getOperation());
469}
470
471MlirModule mlirModuleFromOperation(MlirOperation op) {
472 return wrap(dyn_cast<ModuleOp>(unwrap(op)));
473}
474
475bool mlirModuleEqual(MlirModule lhs, MlirModule rhs) {
476 return unwrap(lhs) == unwrap(rhs);
477}
478
479size_t mlirModuleHashValue(MlirModule mod) {
480 return OperationEquivalence::computeHash(unwrap(mod).getOperation());
481}
482
483//===----------------------------------------------------------------------===//
484// Operation state API.
485//===----------------------------------------------------------------------===//
486
487MlirOperationState mlirOperationStateGet(MlirStringRef name, MlirLocation loc) {
488 MlirOperationState state;
489 state.name = name;
490 state.location = loc;
491 state.nResults = 0;
492 state.results = nullptr;
493 state.nOperands = 0;
494 state.operands = nullptr;
495 state.nRegions = 0;
496 state.regions = nullptr;
497 state.nSuccessors = 0;
498 state.successors = nullptr;
499 state.nAttributes = 0;
500 state.attributes = nullptr;
501 state.enableResultTypeInference = false;
502 return state;
503}
504
505#define APPEND_ELEMS(type, sizeName, elemName) \
506 state->elemName = \
507 (type *)realloc(state->elemName, (state->sizeName + n) * sizeof(type)); \
508 memcpy(state->elemName + state->sizeName, elemName, n * sizeof(type)); \
509 state->sizeName += n;
510
511void mlirOperationStateAddResults(MlirOperationState *state, intptr_t n,
512 MlirType const *results) {
513 APPEND_ELEMS(MlirType, nResults, results);
514}
515
516void mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n,
517 MlirValue const *operands) {
518 APPEND_ELEMS(MlirValue, nOperands, operands);
519}
520void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n,
521 MlirRegion const *regions) {
522 APPEND_ELEMS(MlirRegion, nRegions, regions);
523}
524void mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n,
525 MlirBlock const *successors) {
526 APPEND_ELEMS(MlirBlock, nSuccessors, successors);
527}
528void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n,
529 MlirNamedAttribute const *attributes) {
530 APPEND_ELEMS(MlirNamedAttribute, nAttributes, attributes);
531}
532
533void mlirOperationStateEnableResultTypeInference(MlirOperationState *state) {
534 state->enableResultTypeInference = true;
535}
536
537//===----------------------------------------------------------------------===//
538// Operation API.
539//===----------------------------------------------------------------------===//
540
541static LogicalResult inferOperationTypes(OperationState &state) {
542 MLIRContext *context = state.getContext();
543 std::optional<RegisteredOperationName> info = state.name.getRegisteredInfo();
544 if (!info) {
545 emitError(state.location)
546 << "type inference was requested for the operation " << state.name
547 << ", but the operation was not registered; ensure that the dialect "
548 "containing the operation is linked into MLIR and registered with "
549 "the context";
550 return failure();
551 }
552
553 auto *inferInterface = info->getInterface<InferTypeOpInterface>();
554 if (!inferInterface) {
555 emitError(state.location)
556 << "type inference was requested for the operation " << state.name
557 << ", but the operation does not support type inference; result "
558 "types must be specified explicitly";
559 return failure();
560 }
561
562 DictionaryAttr attributes = state.attributes.getDictionary(context);
563 PropertyRef properties = state.getRawProperties();
564
565 if (!properties && info->getOpPropertyByteSize() > 0 && !attributes.empty()) {
566 auto propAlloc = std::make_unique<char[]>(info->getOpPropertyByteSize());
567 properties = PropertyRef(info->getOpPropertiesTypeID(), propAlloc.get());
568 if (properties) {
569 auto emitError = [&]() {
570 return mlir::emitError(state.location)
571 << " failed properties conversion while building "
572 << state.name.getStringRef() << " with `" << attributes << "`: ";
573 };
574 if (failed(info->setOpPropertiesFromAttribute(state.name, properties,
575 attributes, emitError)))
576 return failure();
577 }
578 if (succeeded(inferInterface->inferReturnTypes(
579 context, state.location, state.operands, attributes, properties,
580 state.regions, state.types))) {
581 return success();
582 }
583 // Diagnostic emitted by interface.
584 return failure();
585 }
586
587 if (succeeded(inferInterface->inferReturnTypes(
588 context, state.location, state.operands, attributes, properties,
589 state.regions, state.types)))
590 return success();
591
592 // Diagnostic emitted by interface.
593 return failure();
594}
595
596MlirOperation mlirOperationCreate(MlirOperationState *state) {
597 assert(state);
598 OperationState cppState(unwrap(state->location), unwrap(state->name));
599 SmallVector<Type, 4> resultStorage;
600 SmallVector<Value, 8> operandStorage;
601 SmallVector<Block *, 2> successorStorage;
602 cppState.addTypes(unwrapList(state->nResults, state->results, resultStorage));
603 cppState.addOperands(
604 unwrapList(state->nOperands, state->operands, operandStorage));
605 cppState.addSuccessors(
606 unwrapList(state->nSuccessors, state->successors, successorStorage));
607
608 cppState.attributes.reserve(state->nAttributes);
609 for (intptr_t i = 0; i < state->nAttributes; ++i)
610 cppState.addAttribute(unwrap(state->attributes[i].name),
611 unwrap(state->attributes[i].attribute));
612
613 for (intptr_t i = 0; i < state->nRegions; ++i)
614 cppState.addRegion(std::unique_ptr<Region>(unwrap(state->regions[i])));
615
616 free(state->results);
617 free(state->operands);
618 free(state->successors);
619 free(state->regions);
620 free(state->attributes);
621
622 // Infer result types.
623 if (state->enableResultTypeInference) {
624 assert(cppState.types.empty() &&
625 "result type inference enabled and result types provided");
626 if (failed(inferOperationTypes(cppState)))
627 return {nullptr};
628 }
629
630 return wrap(Operation::create(cppState));
631}
632
633MlirOperation mlirOperationCreateParse(MlirContext context,
634 MlirStringRef sourceStr,
635 MlirStringRef sourceName) {
636
637 return wrap(
638 parseSourceString(unwrap(sourceStr), unwrap(context), unwrap(sourceName))
639 .release());
640}
641
642MlirOperation mlirOperationClone(MlirOperation op) {
643 return wrap(unwrap(op)->clone());
644}
645
646void mlirOperationDestroy(MlirOperation op) { unwrap(op)->erase(); }
647
648void mlirOperationRemoveFromParent(MlirOperation op) { unwrap(op)->remove(); }
649
650bool mlirOperationEqual(MlirOperation op, MlirOperation other) {
651 return unwrap(op) == unwrap(other);
652}
653
654size_t mlirOperationHashValue(MlirOperation op) {
656}
657
658MlirContext mlirOperationGetContext(MlirOperation op) {
659 return wrap(unwrap(op)->getContext());
660}
661
662bool mlirOperationNameHasTrait(MlirStringRef opName, MlirTypeID traitTypeID,
663 MlirContext context) {
664 return OperationName(unwrap(opName), unwrap(context))
665 .hasTrait(unwrap(traitTypeID));
666}
667
668MlirLocation mlirOperationGetLocation(MlirOperation op) {
669 return wrap(unwrap(op)->getLoc());
670}
671
672void mlirOperationSetLocation(MlirOperation op, MlirLocation loc) {
673 unwrap(op)->setLoc(unwrap(loc));
674}
675
676MlirTypeID mlirOperationGetTypeID(MlirOperation op) {
677 if (auto info = unwrap(op)->getRegisteredInfo())
678 return wrap(info->getTypeID());
679 return {nullptr};
680}
681
682MlirIdentifier mlirOperationGetName(MlirOperation op) {
683 return wrap(unwrap(op)->getName().getIdentifier());
684}
685
686MlirBlock mlirOperationGetBlock(MlirOperation op) {
687 return wrap(unwrap(op)->getBlock());
688}
689
690MlirOperation mlirOperationGetParentOperation(MlirOperation op) {
691 return wrap(unwrap(op)->getParentOp());
692}
693
695 return static_cast<intptr_t>(unwrap(op)->getNumRegions());
696}
697
698MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos) {
699 return wrap(&unwrap(op)->getRegion(static_cast<unsigned>(pos)));
700}
701
702MlirRegion mlirOperationGetFirstRegion(MlirOperation op) {
703 Operation *cppOp = unwrap(op);
704 if (cppOp->getNumRegions() == 0)
705 return wrap(static_cast<Region *>(nullptr));
706 return wrap(&cppOp->getRegion(0));
707}
708
709MlirRegion mlirRegionGetNextInOperation(MlirRegion region) {
710 Region *cppRegion = unwrap(region);
711 Operation *parent = cppRegion->getParentOp();
712 intptr_t next = cppRegion->getRegionNumber() + 1;
713 if (parent->getNumRegions() > next)
714 return wrap(&parent->getRegion(next));
715 return wrap(static_cast<Region *>(nullptr));
716}
717
718MlirOperation mlirOperationGetNextInBlock(MlirOperation op) {
719 return wrap(unwrap(op)->getNextNode());
720}
721
723 return static_cast<intptr_t>(unwrap(op)->getNumOperands());
724}
725
726MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos) {
727 return wrap(unwrap(op)->getOperand(static_cast<unsigned>(pos)));
728}
729
730MlirOpOperand mlirOperationGetOpOperand(MlirOperation op, intptr_t pos) {
731 return wrap(&unwrap(op)->getOpOperand(static_cast<unsigned>(pos)));
732}
733
734void mlirOperationSetOperand(MlirOperation op, intptr_t pos,
735 MlirValue newValue) {
736 unwrap(op)->setOperand(static_cast<unsigned>(pos), unwrap(newValue));
737}
738
739void mlirOperationSetOperands(MlirOperation op, intptr_t nOperands,
740 MlirValue const *operands) {
742 unwrap(op)->setOperands(unwrapList(nOperands, operands, ops));
743}
744
746 return static_cast<intptr_t>(unwrap(op)->getNumResults());
747}
748
749MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos) {
750 return wrap(unwrap(op)->getResult(static_cast<unsigned>(pos)));
751}
752
754 return static_cast<intptr_t>(unwrap(op)->getNumSuccessors());
755}
756
757MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos) {
758 return wrap(unwrap(op)->getSuccessor(static_cast<unsigned>(pos)));
759}
760
763 std::optional<Attribute> attr = unwrap(op)->getInherentAttr(unwrap(name));
764 return attr.has_value();
765}
766
767MlirAttribute mlirOperationGetInherentAttributeByName(MlirOperation op,
768 MlirStringRef name) {
769 std::optional<Attribute> attr = unwrap(op)->getInherentAttr(unwrap(name));
770 if (attr.has_value())
771 return wrap(*attr);
772 return {};
773}
774
776 MlirStringRef name,
777 MlirAttribute attr) {
778 unwrap(op)->setInherentAttr(
779 StringAttr::get(unwrap(op)->getContext(), unwrap(name)), unwrap(attr));
780}
781
783 return static_cast<intptr_t>(
784 llvm::range_size(unwrap(op)->getDiscardableAttrs()));
785}
786
788 intptr_t pos) {
789 NamedAttribute attr =
790 *std::next(unwrap(op)->getDiscardableAttrs().begin(), pos);
791 return MlirNamedAttribute{wrap(attr.getName()), wrap(attr.getValue())};
792}
793
794MlirAttribute mlirOperationGetDiscardableAttributeByName(MlirOperation op,
795 MlirStringRef name) {
796 return wrap(unwrap(op)->getDiscardableAttr(unwrap(name)));
797}
798
800 MlirStringRef name,
801 MlirAttribute attr) {
802 unwrap(op)->setDiscardableAttr(unwrap(name), unwrap(attr));
803}
804
806 MlirStringRef name) {
807 return !!unwrap(op)->removeDiscardableAttr(unwrap(name));
808}
809
810void mlirOperationSetSuccessor(MlirOperation op, intptr_t pos,
811 MlirBlock block) {
812 unwrap(op)->setSuccessor(unwrap(block), static_cast<unsigned>(pos));
813}
814
816 return static_cast<intptr_t>(unwrap(op)->getAttrs().size());
817}
818
820 NamedAttribute attr = unwrap(op)->getAttrs()[pos];
821 return MlirNamedAttribute{wrap(attr.getName()), wrap(attr.getValue())};
822}
823
824MlirAttribute mlirOperationGetAttributeByName(MlirOperation op,
825 MlirStringRef name) {
826 return wrap(unwrap(op)->getAttr(unwrap(name)));
827}
828
830 MlirAttribute attr) {
831 unwrap(op)->setAttr(unwrap(name), unwrap(attr));
832}
833
835 return !!unwrap(op)->removeAttr(unwrap(name));
836}
837
838void mlirOperationPrint(MlirOperation op, MlirStringCallback callback,
839 void *userData) {
840 detail::CallbackOstream stream(callback, userData);
841 unwrap(op)->print(stream);
842}
843
844void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags,
845 MlirStringCallback callback, void *userData) {
846 detail::CallbackOstream stream(callback, userData);
847 unwrap(op)->print(stream, *unwrap(flags));
848}
849
850void mlirOperationPrintWithState(MlirOperation op, MlirAsmState state,
851 MlirStringCallback callback, void *userData) {
852 detail::CallbackOstream stream(callback, userData);
853 if (state.ptr)
854 unwrap(op)->print(stream, *unwrap(state));
855 else
856 unwrap(op)->print(stream);
857}
858
859void mlirOperationWriteBytecode(MlirOperation op, MlirStringCallback callback,
860 void *userData) {
861 detail::CallbackOstream stream(callback, userData);
862 // As no desired version is set, no failure can occur.
863 (void)writeBytecodeToFile(unwrap(op), stream);
864}
865
867 MlirOperation op, MlirBytecodeWriterConfig config,
868 MlirStringCallback callback, void *userData) {
869 detail::CallbackOstream stream(callback, userData);
870 return wrap(writeBytecodeToFile(unwrap(op), stream, *unwrap(config)));
871}
872
873void mlirOperationDump(MlirOperation op) { return unwrap(op)->dump(); }
874
875bool mlirOperationVerify(MlirOperation op) {
876 return succeeded(verify(unwrap(op)));
877}
878
879void mlirOperationMoveAfter(MlirOperation op, MlirOperation other) {
880 return unwrap(op)->moveAfter(unwrap(other));
881}
882
883void mlirOperationMoveBefore(MlirOperation op, MlirOperation other) {
884 return unwrap(op)->moveBefore(unwrap(other));
885}
886
887bool mlirOperationIsBeforeInBlock(MlirOperation op, MlirOperation other) {
888 return unwrap(op)->isBeforeInBlock(unwrap(other));
889}
890
892 switch (result) {
895
898
900 return mlir::WalkResult::skip();
901 }
902 llvm_unreachable("unknown result in WalkResult::unwrap");
903}
904
905void mlirOperationWalk(MlirOperation op, MlirOperationWalkCallback callback,
906 void *userData, MlirWalkOrder walkOrder) {
907 switch (walkOrder) {
908
909 case MlirWalkPreOrder:
911 [callback, userData](Operation *op) {
912 return unwrap(callback(wrap(op), userData));
913 });
914 break;
917 [callback, userData](Operation *op) {
918 return unwrap(callback(wrap(op), userData));
919 });
920 }
921}
922
923void mlirOperationReplaceUsesOfWith(MlirOperation op, MlirValue oldValue,
924 MlirValue newValue) {
925 unwrap(op)->replaceUsesOfWith(unwrap(oldValue), unwrap(newValue));
926}
927
928//===----------------------------------------------------------------------===//
929// Region API.
930//===----------------------------------------------------------------------===//
931
932MlirRegion mlirRegionCreate() { return wrap(new Region); }
933
934bool mlirRegionEqual(MlirRegion region, MlirRegion other) {
935 return unwrap(region) == unwrap(other);
936}
937
938MlirBlock mlirRegionGetFirstBlock(MlirRegion region) {
939 Region *cppRegion = unwrap(region);
940 if (cppRegion->empty())
941 return wrap(static_cast<Block *>(nullptr));
942 return wrap(&cppRegion->front());
943}
944
945void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block) {
946 unwrap(region)->push_back(unwrap(block));
947}
948
949void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos,
950 MlirBlock block) {
951 auto &blockList = unwrap(region)->getBlocks();
952 blockList.insert(std::next(blockList.begin(), pos), unwrap(block));
953}
954
955void mlirRegionInsertOwnedBlockAfter(MlirRegion region, MlirBlock reference,
956 MlirBlock block) {
957 Region *cppRegion = unwrap(region);
958 if (mlirBlockIsNull(reference)) {
959 cppRegion->getBlocks().insert(cppRegion->begin(), unwrap(block));
960 return;
961 }
962
963 assert(unwrap(reference)->getParent() == unwrap(region) &&
964 "expected reference block to belong to the region");
965 cppRegion->getBlocks().insertAfter(Region::iterator(unwrap(reference)),
966 unwrap(block));
967}
968
969void mlirRegionInsertOwnedBlockBefore(MlirRegion region, MlirBlock reference,
970 MlirBlock block) {
971 if (mlirBlockIsNull(reference))
972 return mlirRegionAppendOwnedBlock(region, block);
973
974 assert(unwrap(reference)->getParent() == unwrap(region) &&
975 "expected reference block to belong to the region");
976 unwrap(region)->getBlocks().insert(Region::iterator(unwrap(reference)),
977 unwrap(block));
978}
979
980void mlirRegionDestroy(MlirRegion region) {
981 delete static_cast<Region *>(region.ptr);
982}
983
984void mlirRegionTakeBody(MlirRegion target, MlirRegion source) {
985 unwrap(target)->takeBody(*unwrap(source));
986}
987
988//===----------------------------------------------------------------------===//
989// Block API.
990//===----------------------------------------------------------------------===//
991
992MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args,
993 MlirLocation const *locs) {
994 Block *b = new Block;
995 for (intptr_t i = 0; i < nArgs; ++i)
996 b->addArgument(unwrap(args[i]), unwrap(locs[i]));
997 return wrap(b);
998}
999
1000bool mlirBlockEqual(MlirBlock block, MlirBlock other) {
1001 return unwrap(block) == unwrap(other);
1002}
1003
1004MlirOperation mlirBlockGetParentOperation(MlirBlock block) {
1005 return wrap(unwrap(block)->getParentOp());
1006}
1007
1008MlirRegion mlirBlockGetParentRegion(MlirBlock block) {
1009 return wrap(unwrap(block)->getParent());
1010}
1011
1012MlirBlock mlirBlockGetNextInRegion(MlirBlock block) {
1013 return wrap(unwrap(block)->getNextNode());
1014}
1015
1016MlirOperation mlirBlockGetFirstOperation(MlirBlock block) {
1017 Block *cppBlock = unwrap(block);
1018 if (cppBlock->empty())
1019 return wrap(static_cast<Operation *>(nullptr));
1020 return wrap(&cppBlock->front());
1021}
1022
1023MlirOperation mlirBlockGetTerminator(MlirBlock block) {
1024 Block *cppBlock = unwrap(block);
1025 if (cppBlock->empty())
1026 return wrap(static_cast<Operation *>(nullptr));
1027 Operation &back = cppBlock->back();
1028 if (!back.hasTrait<OpTrait::IsTerminator>())
1029 return wrap(static_cast<Operation *>(nullptr));
1030 return wrap(&back);
1031}
1032
1033void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation) {
1034 unwrap(block)->push_back(unwrap(operation));
1035}
1036
1037void mlirBlockInsertOwnedOperation(MlirBlock block, intptr_t pos,
1038 MlirOperation operation) {
1039 auto &opList = unwrap(block)->getOperations();
1040 opList.insert(std::next(opList.begin(), pos), unwrap(operation));
1041}
1042
1044 MlirOperation reference,
1045 MlirOperation operation) {
1046 Block *cppBlock = unwrap(block);
1047 if (mlirOperationIsNull(reference)) {
1048 cppBlock->getOperations().insert(cppBlock->begin(), unwrap(operation));
1049 return;
1050 }
1051
1052 assert(unwrap(reference)->getBlock() == unwrap(block) &&
1053 "expected reference operation to belong to the block");
1054 cppBlock->getOperations().insertAfter(Block::iterator(unwrap(reference)),
1055 unwrap(operation));
1056}
1057
1059 MlirOperation reference,
1060 MlirOperation operation) {
1061 if (mlirOperationIsNull(reference))
1062 return mlirBlockAppendOwnedOperation(block, operation);
1063
1064 assert(unwrap(reference)->getBlock() == unwrap(block) &&
1065 "expected reference operation to belong to the block");
1066 unwrap(block)->getOperations().insert(Block::iterator(unwrap(reference)),
1067 unwrap(operation));
1068}
1069
1070void mlirBlockDestroy(MlirBlock block) { delete unwrap(block); }
1071
1072void mlirBlockDetach(MlirBlock block) {
1073 Block *b = unwrap(block);
1074 b->getParent()->getBlocks().remove(b);
1075}
1076
1078 return static_cast<intptr_t>(unwrap(block)->getNumArguments());
1079}
1080
1081MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type,
1082 MlirLocation loc) {
1083 return wrap(unwrap(block)->addArgument(unwrap(type), unwrap(loc)));
1084}
1085
1086void mlirBlockEraseArgument(MlirBlock block, unsigned index) {
1087 return unwrap(block)->eraseArgument(index);
1088}
1089
1090MlirValue mlirBlockInsertArgument(MlirBlock block, intptr_t pos, MlirType type,
1091 MlirLocation loc) {
1092 return wrap(unwrap(block)->insertArgument(pos, unwrap(type), unwrap(loc)));
1093}
1094
1095MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos) {
1096 return wrap(unwrap(block)->getArgument(static_cast<unsigned>(pos)));
1097}
1098
1099void mlirBlockPrint(MlirBlock block, MlirStringCallback callback,
1100 void *userData) {
1101 detail::CallbackOstream stream(callback, userData);
1102 unwrap(block)->print(stream);
1103}
1104
1106 return static_cast<intptr_t>(unwrap(block)->getNumSuccessors());
1107}
1108
1109MlirBlock mlirBlockGetSuccessor(MlirBlock block, intptr_t pos) {
1110 return wrap(unwrap(block)->getSuccessor(static_cast<unsigned>(pos)));
1111}
1112
1114 Block *b = unwrap(block);
1115 return static_cast<intptr_t>(std::distance(b->pred_begin(), b->pred_end()));
1116}
1117
1118MlirBlock mlirBlockGetPredecessor(MlirBlock block, intptr_t pos) {
1119 Block *b = unwrap(block);
1120 Block::pred_iterator it = b->pred_begin();
1121 std::advance(it, pos);
1122 return wrap(*it);
1123}
1124
1125//===----------------------------------------------------------------------===//
1126// Value API.
1127//===----------------------------------------------------------------------===//
1128
1129bool mlirValueEqual(MlirValue value1, MlirValue value2) {
1130 return unwrap(value1) == unwrap(value2);
1131}
1132
1133bool mlirValueIsABlockArgument(MlirValue value) {
1134 return llvm::isa<BlockArgument>(unwrap(value));
1135}
1136
1137bool mlirValueIsAOpResult(MlirValue value) {
1138 return llvm::isa<OpResult>(unwrap(value));
1139}
1140
1141MlirBlock mlirBlockArgumentGetOwner(MlirValue value) {
1142 return wrap(llvm::dyn_cast<BlockArgument>(unwrap(value)).getOwner());
1143}
1144
1146 return static_cast<intptr_t>(
1147 llvm::dyn_cast<BlockArgument>(unwrap(value)).getArgNumber());
1148}
1149
1150void mlirBlockArgumentSetType(MlirValue value, MlirType type) {
1151 if (auto blockArg = llvm::dyn_cast<BlockArgument>(unwrap(value)))
1152 blockArg.setType(unwrap(type));
1153}
1154
1155void mlirBlockArgumentSetLocation(MlirValue value, MlirLocation loc) {
1156 if (auto blockArg = llvm::dyn_cast<BlockArgument>(unwrap(value)))
1157 blockArg.setLoc(unwrap(loc));
1158}
1159
1160MlirOperation mlirOpResultGetOwner(MlirValue value) {
1161 return wrap(llvm::dyn_cast<OpResult>(unwrap(value)).getOwner());
1162}
1163
1165 return static_cast<intptr_t>(
1166 llvm::dyn_cast<OpResult>(unwrap(value)).getResultNumber());
1167}
1168
1169MlirType mlirValueGetType(MlirValue value) {
1170 return wrap(unwrap(value).getType());
1171}
1172
1173void mlirValueSetType(MlirValue value, MlirType type) {
1174 unwrap(value).setType(unwrap(type));
1175}
1176
1177void mlirValueDump(MlirValue value) { unwrap(value).dump(); }
1178
1179void mlirValuePrint(MlirValue value, MlirStringCallback callback,
1180 void *userData) {
1181 detail::CallbackOstream stream(callback, userData);
1182 unwrap(value).print(stream);
1183}
1184
1185void mlirValuePrintAsOperand(MlirValue value, MlirAsmState state,
1186 MlirStringCallback callback, void *userData) {
1187 detail::CallbackOstream stream(callback, userData);
1188 Value cppValue = unwrap(value);
1189 cppValue.printAsOperand(stream, *unwrap(state));
1190}
1191
1192MlirOpOperand mlirValueGetFirstUse(MlirValue value) {
1193 Value cppValue = unwrap(value);
1194 if (cppValue.use_empty())
1195 return {};
1196
1197 OpOperand *opOperand = cppValue.use_begin().getOperand();
1198
1199 return wrap(opOperand);
1200}
1201
1202void mlirValueReplaceAllUsesOfWith(MlirValue oldValue, MlirValue newValue) {
1203 unwrap(oldValue).replaceAllUsesWith(unwrap(newValue));
1204}
1205
1206void mlirValueReplaceAllUsesExcept(MlirValue oldValue, MlirValue newValue,
1207 intptr_t numExceptions,
1208 MlirOperation *exceptions) {
1209 Value oldValueCpp = unwrap(oldValue);
1210 Value newValueCpp = unwrap(newValue);
1211
1213 for (intptr_t i = 0; i < numExceptions; ++i) {
1214 exceptionSet.insert(unwrap(exceptions[i]));
1215 }
1216
1217 oldValueCpp.replaceAllUsesExcept(newValueCpp, exceptionSet);
1218}
1219
1220MlirLocation mlirValueGetLocation(MlirValue v) {
1221 return wrap(unwrap(v).getLoc());
1222}
1223
1224MlirContext mlirValueGetContext(MlirValue v) {
1225 return wrap(unwrap(v).getContext());
1226}
1227
1228//===----------------------------------------------------------------------===//
1229// OpOperand API.
1230//===----------------------------------------------------------------------===//
1231
1232bool mlirOpOperandIsNull(MlirOpOperand opOperand) { return !opOperand.ptr; }
1233
1234MlirOperation mlirOpOperandGetOwner(MlirOpOperand opOperand) {
1235 return wrap(unwrap(opOperand)->getOwner());
1236}
1237
1238MlirValue mlirOpOperandGetValue(MlirOpOperand opOperand) {
1239 return wrap(unwrap(opOperand)->get());
1240}
1241
1242unsigned mlirOpOperandGetOperandNumber(MlirOpOperand opOperand) {
1243 return unwrap(opOperand)->getOperandNumber();
1244}
1245
1246MlirOpOperand mlirOpOperandGetNextUse(MlirOpOperand opOperand) {
1247 if (mlirOpOperandIsNull(opOperand))
1248 return {};
1249
1250 OpOperand *nextOpOperand = static_cast<OpOperand *>(
1251 unwrap(opOperand)->getNextOperandUsingThisValue());
1252
1253 if (!nextOpOperand)
1254 return {};
1255
1256 return wrap(nextOpOperand);
1257}
1258
1259//===----------------------------------------------------------------------===//
1260// Type API.
1261//===----------------------------------------------------------------------===//
1262
1263MlirType mlirTypeParseGet(MlirContext context, MlirStringRef type) {
1264 return wrap(mlir::parseType(unwrap(type), unwrap(context)));
1265}
1266
1267MlirContext mlirTypeGetContext(MlirType type) {
1268 return wrap(unwrap(type).getContext());
1269}
1270
1271MlirTypeID mlirTypeGetTypeID(MlirType type) {
1272 return wrap(unwrap(type).getTypeID());
1273}
1274
1275MlirDialect mlirTypeGetDialect(MlirType type) {
1276 return wrap(&unwrap(type).getDialect());
1277}
1278
1279bool mlirTypeEqual(MlirType t1, MlirType t2) {
1280 return unwrap(t1) == unwrap(t2);
1281}
1282
1283void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData) {
1284 detail::CallbackOstream stream(callback, userData);
1285 unwrap(type).print(stream);
1286}
1287
1288void mlirTypeDump(MlirType type) { unwrap(type).dump(); }
1289
1290//===----------------------------------------------------------------------===//
1291// Attribute API.
1292//===----------------------------------------------------------------------===//
1293
1294MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr) {
1295 return wrap(mlir::parseAttribute(unwrap(attr), unwrap(context)));
1296}
1297
1298MlirContext mlirAttributeGetContext(MlirAttribute attribute) {
1299 return wrap(unwrap(attribute).getContext());
1300}
1301
1302MlirType mlirAttributeGetType(MlirAttribute attribute) {
1303 Attribute attr = unwrap(attribute);
1304 if (auto typedAttr = llvm::dyn_cast<TypedAttr>(attr))
1305 return wrap(typedAttr.getType());
1306 return wrap(NoneType::get(attr.getContext()));
1307}
1308
1309MlirTypeID mlirAttributeGetTypeID(MlirAttribute attr) {
1310 return wrap(unwrap(attr).getTypeID());
1311}
1312
1313MlirDialect mlirAttributeGetDialect(MlirAttribute attr) {
1314 return wrap(&unwrap(attr).getDialect());
1315}
1316
1317bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2) {
1318 return unwrap(a1) == unwrap(a2);
1319}
1320
1321void mlirAttributePrint(MlirAttribute attr, MlirStringCallback callback,
1322 void *userData) {
1323 detail::CallbackOstream stream(callback, userData);
1324 unwrap(attr).print(stream);
1325}
1326
1327void mlirAttributeDump(MlirAttribute attr) { unwrap(attr).dump(); }
1328
1330 MlirAttribute attr) {
1331 return MlirNamedAttribute{name, attr};
1332}
1333
1334//===----------------------------------------------------------------------===//
1335// Identifier API.
1336//===----------------------------------------------------------------------===//
1337
1338MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str) {
1339 return wrap(StringAttr::get(unwrap(context), unwrap(str)));
1340}
1341
1342MlirContext mlirIdentifierGetContext(MlirIdentifier ident) {
1343 return wrap(unwrap(ident).getContext());
1344}
1345
1346bool mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other) {
1347 return unwrap(ident) == unwrap(other);
1348}
1349
1350MlirStringRef mlirIdentifierStr(MlirIdentifier ident) {
1351 return wrap(unwrap(ident).strref());
1352}
1353
1354//===----------------------------------------------------------------------===//
1355// Symbol and SymbolTable API.
1356//===----------------------------------------------------------------------===//
1357
1361
1365
1366MlirSymbolTable mlirSymbolTableCreate(MlirOperation operation) {
1367 if (!unwrap(operation)->hasTrait<OpTrait::SymbolTable>())
1368 return wrap(static_cast<SymbolTable *>(nullptr));
1369 return wrap(new SymbolTable(unwrap(operation)));
1370}
1371
1372void mlirSymbolTableDestroy(MlirSymbolTable symbolTable) {
1373 delete unwrap(symbolTable);
1374}
1375
1376MlirOperation mlirSymbolTableLookup(MlirSymbolTable symbolTable,
1377 MlirStringRef name) {
1378 return wrap(unwrap(symbolTable)->lookup(StringRef(name.data, name.length)));
1379}
1380
1381MlirAttribute mlirSymbolTableInsert(MlirSymbolTable symbolTable,
1382 MlirOperation operation) {
1383 return wrap((Attribute)unwrap(symbolTable)->insert(unwrap(operation)));
1384}
1385
1386void mlirSymbolTableErase(MlirSymbolTable symbolTable,
1387 MlirOperation operation) {
1388 unwrap(symbolTable)->erase(unwrap(operation));
1389}
1390
1392 MlirStringRef newSymbol,
1393 MlirOperation from) {
1394 auto *cppFrom = unwrap(from);
1395 auto *context = cppFrom->getContext();
1396 auto oldSymbolAttr = StringAttr::get(context, unwrap(oldSymbol));
1397 auto newSymbolAttr = StringAttr::get(context, unwrap(newSymbol));
1398 return wrap(SymbolTable::replaceAllSymbolUses(oldSymbolAttr, newSymbolAttr,
1399 unwrap(from)));
1400}
1401
1402void mlirSymbolTableWalkSymbolTables(MlirOperation from, bool allSymUsesVisible,
1403 void (*callback)(MlirOperation, bool,
1404 void *userData),
1405 void *userData) {
1406 SymbolTable::walkSymbolTables(unwrap(from), allSymUsesVisible,
1407 [&](Operation *foundOpCpp, bool isVisible) {
1408 callback(wrap(foundOpCpp), isVisible,
1409 userData);
1410 });
1411}
return success()
lhs
unsigned mlirOpOperandGetOperandNumber(MlirOpOperand opOperand)
Returns the operand number of an op operand.
Definition IR.cpp:1242
MlirLocation mlirValueGetLocation(MlirValue v)
Gets the location of the value.
Definition IR.cpp:1220
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:873
MlirAttribute mlirOperationGetDiscardableAttributeByName(MlirOperation op, MlirStringRef name)
Definition IR.cpp:794
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:1376
MlirRegion mlirRegionCreate()
Creates a new empty region and transfers ownership to the caller.
Definition IR.cpp:932
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:838
MlirContext mlirModuleGetContext(MlirModule module)
Definition IR.cpp:453
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:1179
MlirContext mlirLocationGetContext(MlirLocation location)
Gets the context that a location was created with.
Definition IR.cpp:418
size_t mlirModuleHashValue(MlirModule mod)
Definition IR.cpp:479
intptr_t mlirBlockGetNumPredecessors(MlirBlock block)
Definition IR.cpp:1113
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:1234
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:709
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:866
MlirIdentifier mlirOperationGetName(MlirOperation op)
Definition IR.cpp:682
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:1346
void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags, MlirStringCallback callback, void *userData)
Same as mlirOperationPrint but accepts flags controlling the printing behavior.
Definition IR.cpp:844
bool mlirValueIsABlockArgument(MlirValue value)
Definition IR.cpp:1133
MlirTypeID mlirTypeGetTypeID(MlirType type)
Gets the type ID of the type.
Definition IR.cpp:1271
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:1202
MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type, MlirLocation loc)
Appends an argument of the specified type to the block.
Definition IR.cpp:1081
intptr_t mlirOperationGetNumRegions(MlirOperation op)
Definition IR.cpp:694
MlirBlock mlirOperationGetBlock(MlirOperation op)
Definition IR.cpp:686
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:1338
void mlirBlockArgumentSetType(MlirValue value, MlirType type)
Definition IR.cpp:1150
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:1329
MlirStringRef mlirSymbolTableGetVisibilityAttributeName()
Returns the name of the attribute used to store symbol visibility.
Definition IR.cpp:1362
void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n, MlirNamedAttribute const *attributes)
Definition IR.cpp:528
MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos)
Definition IR.cpp:749
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:1402
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:444
bool mlirOperationNameHasTrait(MlirStringRef opName, MlirTypeID traitTypeID, MlirContext context)
Definition IR.cpp:662
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:1321
MlirBlock mlirRegionGetFirstBlock(MlirRegion region)
Gets the first block in the region.
Definition IR.cpp:938
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:1391
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:745
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:1192
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:955
void mlirOperationDestroy(MlirOperation op)
Definition IR.cpp:646
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:1206
void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation)
Takes an operation owned by the caller and appends it to the block.
Definition IR.cpp:1033
MlirRegion mlirOperationGetFirstRegion(MlirOperation op)
Returns first region attached to the operation.
Definition IR.cpp:702
MlirAttribute mlirOperationGetInherentAttributeByName(MlirOperation op, MlirStringRef name)
Definition IR.cpp:767
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:1298
MlirStringRef mlirSymbolTableGetSymbolAttributeName()
Returns the name of the attribute used to store symbol names compatible with symbol tables.
Definition IR.cpp:1358
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:1037
MlirRegion mlirBlockGetParentRegion(MlirBlock block)
Returns the region that contains this block.
Definition IR.cpp:1008
MlirType mlirValueGetType(MlirValue value)
Definition IR.cpp:1169
void mlirOperationMoveAfter(MlirOperation op, MlirOperation other)
Moves the given operation immediately after the other operation in its parent block.
Definition IR.cpp:879
void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block)
Takes a block owned by the caller and appends it to the given region.
Definition IR.cpp:945
void mlirBlockPrint(MlirBlock block, MlirStringCallback callback, void *userData)
Definition IR.cpp:1099
void mlirOperationSetDiscardableAttributeByName(MlirOperation op, MlirStringRef name, MlirAttribute attr)
Definition IR.cpp:799
MlirOpPrintingFlags mlirOpPrintingFlagsCreate()
Definition IR.cpp:201
bool mlirModuleEqual(MlirModule lhs, MlirModule rhs)
Definition IR.cpp:475
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:1012
void mlirOperationSetSuccessor(MlirOperation op, intptr_t pos, MlirBlock block)
Definition IR.cpp:810
MlirValue mlirOpOperandGetValue(MlirOpOperand opOperand)
Returns the value of an op operand.
Definition IR.cpp:1238
#define APPEND_ELEMS(type, sizeName, elemName)
Definition IR.cpp:505
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:718
void mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, bool enable, bool prettyForm)
Definition IR.cpp:219
MlirOperation mlirModuleGetOperation(MlirModule module)
Definition IR.cpp:467
static mlir::WalkResult unwrap(MlirWalkResult result)
Definition IR.cpp:891
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:969
MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos)
Returns pos-th argument of the block.
Definition IR.cpp:1095
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:676
intptr_t mlirBlockArgumentGetArgNumber(MlirValue value)
Definition IR.cpp:1145
void mlirOperationWalk(MlirOperation op, MlirOperationWalkCallback callback, void *userData, MlirWalkOrder walkOrder)
Walks operation op in walkOrder and calls callback on that operation.
Definition IR.cpp:905
MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos)
Definition IR.cpp:757
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:1058
bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2)
Definition IR.cpp:1317
MlirAsmState mlirAsmStateCreateForOperation(MlirOperation op, MlirOpPrintingFlags flags)
Definition IR.cpp:156
bool mlirOperationEqual(MlirOperation op, MlirOperation other)
Definition IR.cpp:650
void mlirOperationSetInherentAttributeByName(MlirOperation op, MlirStringRef name, MlirAttribute attr)
Definition IR.cpp:775
void mlirOpPrintingFlagsAssumeVerified(MlirOpPrintingFlags flags)
Definition IR.cpp:236
bool mlirValueEqual(MlirValue value1, MlirValue value2)
Definition IR.cpp:1129
MlirContext mlirIdentifierGetContext(MlirIdentifier ident)
Returns the context associated with this identifier.
Definition IR.cpp:1342
void mlirBytecodeWriterConfigDestroy(MlirBytecodeWriterConfig config)
Definition IR.cpp:251
MlirBlock mlirBlockGetSuccessor(MlirBlock block, intptr_t pos)
Definition IR.cpp:1109
void mlirModuleDestroy(MlirModule module)
Definition IR.cpp:461
MlirModule mlirModuleCreateEmpty(MlirLocation location)
Definition IR.cpp:432
void mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags)
Definition IR.cpp:224
MlirOperation mlirOperationGetParentOperation(MlirOperation op)
Definition IR.cpp:690
void mlirRegionTakeBody(MlirRegion target, MlirRegion source)
Moves the entire content of the source region to the target region.
Definition IR.cpp:984
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:1090
void mlirValueSetType(MlirValue value, MlirType type)
Definition IR.cpp:1173
intptr_t mlirOperationGetNumSuccessors(MlirOperation op)
Definition IR.cpp:753
MlirDialect mlirAttributeGetDialect(MlirAttribute attr)
Definition IR.cpp:1313
void mlirLocationPrint(MlirLocation location, MlirStringCallback callback, void *userData)
Definition IR.cpp:422
void mlirOperationSetAttributeByName(MlirOperation op, MlirStringRef name, MlirAttribute attr)
Definition IR.cpp:829
void mlirBlockDestroy(MlirBlock block)
Takes a block owned by the caller and destroys it.
Definition IR.cpp:1070
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:949
void mlirOperationSetOperand(MlirOperation op, intptr_t pos, MlirValue newValue)
Definition IR.cpp:734
intptr_t mlirBlockGetNumArguments(MlirBlock block)
Returns the number of arguments of the block.
Definition IR.cpp:1077
MlirTypeID mlirLocationFileLineColRangeGetTypeID()
TypeID Getter for FileLineColRange.
Definition IR.cpp:316
MlirOperation mlirOpResultGetOwner(MlirValue value)
Definition IR.cpp:1160
void mlirBlockEraseArgument(MlirBlock block, unsigned index)
Erase the argument at 'index' and remove it from the argument list.
Definition IR.cpp:1086
bool mlirOpOperandIsNull(MlirOpOperand opOperand)
Returns whether the op operand is null.
Definition IR.cpp:1232
MlirDialect mlirTypeGetDialect(MlirType type)
Gets the dialect a type belongs to.
Definition IR.cpp:1275
MlirModule mlirModuleCreateParse(MlirContext context, MlirStringRef module)
Definition IR.cpp:436
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:654
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:511
void mlirOperationMoveBefore(MlirOperation op, MlirOperation other)
Moves the given operation immediately before the other operation in its parent block.
Definition IR.cpp:883
static LogicalResult inferOperationTypes(OperationState &state)
Definition IR.cpp:541
MlirOperation mlirOperationClone(MlirOperation op)
Definition IR.cpp:642
bool mlirLocationIsAUnknown(MlirLocation location)
Checks whether the given location is an Unknown.
Definition IR.cpp:410
void mlirValuePrintAsOperand(MlirValue value, MlirAsmState state, MlirStringCallback callback, void *userData)
Prints a value as an operand (i.e., the ValueID).
Definition IR.cpp:1185
MlirBlock mlirBlockArgumentGetOwner(MlirValue value)
Definition IR.cpp:1141
void mlirBlockArgumentSetLocation(MlirValue value, MlirLocation loc)
Definition IR.cpp:1155
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:726
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:471
MlirTypeID mlirLocationNameGetTypeID()
TypeID Getter for Name.
Definition IR.cpp:396
MlirOpOperand mlirOperationGetOpOperand(MlirOperation op, intptr_t pos)
Definition IR.cpp:730
MlirLocation mlirOperationGetLocation(MlirOperation op)
Definition IR.cpp:668
bool mlirTypeEqual(MlirType t1, MlirType t2)
Checks if two types are equal.
Definition IR.cpp:1279
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:824
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:887
MlirTypeID mlirAttributeGetTypeID(MlirAttribute attr)
Definition IR.cpp:1309
MlirAttribute mlirSymbolTableInsert(MlirSymbolTable symbolTable, MlirOperation operation)
Inserts the given operation into the given symbol table.
Definition IR.cpp:1381
bool mlirLocationIsAFileLineColRange(MlirLocation location)
Checks whether the given location is an FileLineColRange.
Definition IR.cpp:320
intptr_t mlirOperationGetNumDiscardableAttributes(MlirOperation op)
Definition IR.cpp:782
MlirSymbolTable mlirSymbolTableCreate(MlirOperation operation)
Creates a symbol table for the given operation.
Definition IR.cpp:1366
void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n, MlirRegion const *regions)
Definition IR.cpp:520
void mlirOperationSetLocation(MlirOperation op, MlirLocation loc)
Definition IR.cpp:672
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:1016
MlirType mlirAttributeGetType(MlirAttribute attribute)
Definition IR.cpp:1302
bool mlirOperationRemoveDiscardableAttributeByName(MlirOperation op, MlirStringRef name)
Definition IR.cpp:805
void mlirRegionDestroy(MlirRegion region)
Takes a region owned by the caller and destroys it.
Definition IR.cpp:980
bool mlirOperationRemoveAttributeByName(MlirOperation op, MlirStringRef name)
Definition IR.cpp:834
bool mlirValueIsAOpResult(MlirValue value)
Definition IR.cpp:1137
MLIR_CAPI_EXPORTED bool mlirOperationHasInherentAttributeByName(MlirOperation op, MlirStringRef name)
Definition IR.cpp:762
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:1118
MlirOperation mlirBlockGetTerminator(MlirBlock block)
Returns the terminator operation in the block or null if no terminator.
Definition IR.cpp:1023
bool mlirLocationEqual(MlirLocation l1, MlirLocation l2)
Checks if two locations are equal.
Definition IR.cpp:414
MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos)
Definition IR.cpp:698
void mlirOperationReplaceUsesOfWith(MlirOperation op, MlirValue oldValue, MlirValue newValue)
Replace uses of 'of' value with the 'with' value inside the 'op' operation.
Definition IR.cpp:923
int mlirLocationFileLineColRangeGetEndColumn(MlirLocation location)
Getter for end_column of FileLineColRange.
Definition IR.cpp:310
MlirOperation mlirOperationCreate(MlirOperationState *state)
Definition IR.cpp:596
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:1294
void mlirLocationFusedGetLocations(MlirLocation location, MlirLocation *locationsCPtr)
Getter for locations of Fused.
Definition IR.cpp:360
void mlirOperationRemoveFromParent(MlirOperation op)
Definition IR.cpp:648
void mlirSymbolTableDestroy(MlirSymbolTable symbolTable)
Destroys the symbol table created with mlirSymbolTableCreate.
Definition IR.cpp:1372
intptr_t mlirBlockGetNumSuccessors(MlirBlock block)
Definition IR.cpp:1105
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:850
bool mlirOperationVerify(MlirOperation op)
Verify the operation and return true if it passes, false if it fails.
Definition IR.cpp:875
void mlirBlockDetach(MlirBlock block)
Detach a block from the owning region and assume ownership.
Definition IR.cpp:1072
int mlirLocationFileLineColRangeGetEndLine(MlirLocation location)
Getter for end_line of FileLineColRange.
Definition IR.cpp:304
MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos)
Definition IR.cpp:819
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:859
void mlirValueDump(MlirValue value)
Definition IR.cpp:1177
MlirTypeID mlirLocationUnknownGetTypeID()
TypeID Getter for Unknown.
Definition IR.cpp:406
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:1043
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:739
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:1267
void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData)
Definition IR.cpp:1283
MlirBlock mlirModuleGetBody(MlirModule module)
Definition IR.cpp:457
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:934
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:633
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:658
intptr_t mlirOpResultGetResultNumber(MlirValue value)
Definition IR.cpp:1164
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:1000
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:1386
MlirLocation mlirLocationNameGetChildLoc(MlirLocation location)
Getter for childLoc of Name.
Definition IR.cpp:391
MlirNamedAttribute mlirOperationGetDiscardableAttribute(MlirOperation op, intptr_t pos)
Definition IR.cpp:787
void mlirOperationStateEnableResultTypeInference(MlirOperationState *state)
Definition IR.cpp:533
void mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n, MlirBlock const *successors)
Definition IR.cpp:524
MlirBytecodeWriterConfig mlirBytecodeWriterConfigCreate()
Definition IR.cpp:247
void mlirAttributeDump(MlirAttribute attr)
Prints the attribute to the standard error stream.
Definition IR.cpp:1327
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:1350
void mlirOpPrintingFlagsSkipRegions(MlirOpPrintingFlags flags)
Definition IR.cpp:240
void mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n, MlirValue const *operands)
Definition IR.cpp:516
MlirOperationState mlirOperationStateGet(MlirStringRef name, MlirLocation loc)
Definition IR.cpp:487
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:1004
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:1246
intptr_t mlirOperationGetNumOperands(MlirOperation op)
Definition IR.cpp:722
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:1263
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:992
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:1288
intptr_t mlirOperationGetNumAttributes(MlirOperation op)
Definition IR.cpp:815
MlirContext mlirValueGetContext(MlirValue v)
Gets the context that a value was created with.
Definition IR.cpp:1224
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:254
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.
StringRef getStringRef() const
Return the name of this operation. This always succeeds.
bool hasTrait() const
Returns true if the operation was registered with a particular trait, e.g.
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:712
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Definition Operation.h:775
unsigned getNumRegions()
Returns the number of regions held by this operation.
Definition Operation.h:700
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
Definition Operation.h:252
static Operation * create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, NamedAttrList &&attributes, PropertyRef properties, BlockRange successors, unsigned numRegions)
Create a new Operation with the specific fields.
Definition Operation.cpp:66
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
Type-safe wrapper around a void* for passing properties, including the properties structs of operatio...
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:866
MlirWalkOrder
Traversal order for operation walk.
Definition IR.h:859
@ MlirWalkPreOrder
Definition IR.h:860
@ MlirWalkPostOrder
Definition IR.h:861
MlirWalkResult
Operation walk result.
Definition IR.h:852
@ MlirWalkResultInterrupt
Definition IR.h:854
@ MlirWalkResultSkip
Definition IR.h:855
@ MlirWalkResultAdvance
Definition IR.h:853
static bool mlirBlockIsNull(MlirBlock block)
Checks whether a block is null.
Definition IR.h:952
static bool mlirLocationIsNull(MlirLocation location)
Checks if the location is null.
Definition IR.h:376
#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:307
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:480
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.
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.
PropertyRef getRawProperties()
SmallVector< Type, 4 > types
Types of the results of this operation.
Region * addRegion()
Create a region that should be attached to the operation.