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 PropertyRef properties = state.getRawProperties();
556
557 if (!properties && info->getOpPropertyByteSize() > 0 && !attributes.empty()) {
558 auto propAlloc = std::make_unique<char[]>(info->getOpPropertyByteSize());
559 properties = PropertyRef(info->getOpPropertiesTypeID(), propAlloc.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
654bool mlirOperationNameHasTrait(MlirStringRef opName, MlirTypeID traitTypeID,
655 MlirContext context) {
656 return OperationName(unwrap(opName), unwrap(context))
657 .hasTrait(unwrap(traitTypeID));
658}
659
660MlirLocation mlirOperationGetLocation(MlirOperation op) {
661 return wrap(unwrap(op)->getLoc());
662}
663
664void mlirOperationSetLocation(MlirOperation op, MlirLocation loc) {
665 unwrap(op)->setLoc(unwrap(loc));
666}
667
668MlirTypeID mlirOperationGetTypeID(MlirOperation op) {
669 if (auto info = unwrap(op)->getRegisteredInfo())
670 return wrap(info->getTypeID());
671 return {nullptr};
672}
673
674MlirIdentifier mlirOperationGetName(MlirOperation op) {
675 return wrap(unwrap(op)->getName().getIdentifier());
676}
677
678MlirBlock mlirOperationGetBlock(MlirOperation op) {
679 return wrap(unwrap(op)->getBlock());
680}
681
682MlirOperation mlirOperationGetParentOperation(MlirOperation op) {
683 return wrap(unwrap(op)->getParentOp());
684}
685
687 return static_cast<intptr_t>(unwrap(op)->getNumRegions());
688}
689
690MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos) {
691 return wrap(&unwrap(op)->getRegion(static_cast<unsigned>(pos)));
692}
693
694MlirRegion mlirOperationGetFirstRegion(MlirOperation op) {
695 Operation *cppOp = unwrap(op);
696 if (cppOp->getNumRegions() == 0)
697 return wrap(static_cast<Region *>(nullptr));
698 return wrap(&cppOp->getRegion(0));
699}
700
701MlirRegion mlirRegionGetNextInOperation(MlirRegion region) {
702 Region *cppRegion = unwrap(region);
703 Operation *parent = cppRegion->getParentOp();
704 intptr_t next = cppRegion->getRegionNumber() + 1;
705 if (parent->getNumRegions() > next)
706 return wrap(&parent->getRegion(next));
707 return wrap(static_cast<Region *>(nullptr));
708}
709
710MlirOperation mlirOperationGetNextInBlock(MlirOperation op) {
711 return wrap(unwrap(op)->getNextNode());
712}
713
715 return static_cast<intptr_t>(unwrap(op)->getNumOperands());
716}
717
718MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos) {
719 return wrap(unwrap(op)->getOperand(static_cast<unsigned>(pos)));
720}
721
722MlirOpOperand mlirOperationGetOpOperand(MlirOperation op, intptr_t pos) {
723 return wrap(&unwrap(op)->getOpOperand(static_cast<unsigned>(pos)));
724}
725
726void mlirOperationSetOperand(MlirOperation op, intptr_t pos,
727 MlirValue newValue) {
728 unwrap(op)->setOperand(static_cast<unsigned>(pos), unwrap(newValue));
729}
730
731void mlirOperationSetOperands(MlirOperation op, intptr_t nOperands,
732 MlirValue const *operands) {
734 unwrap(op)->setOperands(unwrapList(nOperands, operands, ops));
735}
736
738 return static_cast<intptr_t>(unwrap(op)->getNumResults());
739}
740
741MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos) {
742 return wrap(unwrap(op)->getResult(static_cast<unsigned>(pos)));
743}
744
746 return static_cast<intptr_t>(unwrap(op)->getNumSuccessors());
747}
748
749MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos) {
750 return wrap(unwrap(op)->getSuccessor(static_cast<unsigned>(pos)));
751}
752
755 std::optional<Attribute> attr = unwrap(op)->getInherentAttr(unwrap(name));
756 return attr.has_value();
757}
758
759MlirAttribute mlirOperationGetInherentAttributeByName(MlirOperation op,
760 MlirStringRef name) {
761 std::optional<Attribute> attr = unwrap(op)->getInherentAttr(unwrap(name));
762 if (attr.has_value())
763 return wrap(*attr);
764 return {};
765}
766
768 MlirStringRef name,
769 MlirAttribute attr) {
770 unwrap(op)->setInherentAttr(
771 StringAttr::get(unwrap(op)->getContext(), unwrap(name)), unwrap(attr));
772}
773
775 return static_cast<intptr_t>(
776 llvm::range_size(unwrap(op)->getDiscardableAttrs()));
777}
778
780 intptr_t pos) {
781 NamedAttribute attr =
782 *std::next(unwrap(op)->getDiscardableAttrs().begin(), pos);
783 return MlirNamedAttribute{wrap(attr.getName()), wrap(attr.getValue())};
784}
785
786MlirAttribute mlirOperationGetDiscardableAttributeByName(MlirOperation op,
787 MlirStringRef name) {
788 return wrap(unwrap(op)->getDiscardableAttr(unwrap(name)));
789}
790
792 MlirStringRef name,
793 MlirAttribute attr) {
794 unwrap(op)->setDiscardableAttr(unwrap(name), unwrap(attr));
795}
796
798 MlirStringRef name) {
799 return !!unwrap(op)->removeDiscardableAttr(unwrap(name));
800}
801
802void mlirOperationSetSuccessor(MlirOperation op, intptr_t pos,
803 MlirBlock block) {
804 unwrap(op)->setSuccessor(unwrap(block), static_cast<unsigned>(pos));
805}
806
808 return static_cast<intptr_t>(unwrap(op)->getAttrs().size());
809}
810
812 NamedAttribute attr = unwrap(op)->getAttrs()[pos];
813 return MlirNamedAttribute{wrap(attr.getName()), wrap(attr.getValue())};
814}
815
816MlirAttribute mlirOperationGetAttributeByName(MlirOperation op,
817 MlirStringRef name) {
818 return wrap(unwrap(op)->getAttr(unwrap(name)));
819}
820
822 MlirAttribute attr) {
823 unwrap(op)->setAttr(unwrap(name), unwrap(attr));
824}
825
827 return !!unwrap(op)->removeAttr(unwrap(name));
828}
829
830void mlirOperationPrint(MlirOperation op, MlirStringCallback callback,
831 void *userData) {
832 detail::CallbackOstream stream(callback, userData);
833 unwrap(op)->print(stream);
834}
835
836void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags,
837 MlirStringCallback callback, void *userData) {
838 detail::CallbackOstream stream(callback, userData);
839 unwrap(op)->print(stream, *unwrap(flags));
840}
841
842void mlirOperationPrintWithState(MlirOperation op, MlirAsmState state,
843 MlirStringCallback callback, void *userData) {
844 detail::CallbackOstream stream(callback, userData);
845 if (state.ptr)
846 unwrap(op)->print(stream, *unwrap(state));
847 else
848 unwrap(op)->print(stream);
849}
850
851void mlirOperationWriteBytecode(MlirOperation op, MlirStringCallback callback,
852 void *userData) {
853 detail::CallbackOstream stream(callback, userData);
854 // As no desired version is set, no failure can occur.
855 (void)writeBytecodeToFile(unwrap(op), stream);
856}
857
859 MlirOperation op, MlirBytecodeWriterConfig config,
860 MlirStringCallback callback, void *userData) {
861 detail::CallbackOstream stream(callback, userData);
862 return wrap(writeBytecodeToFile(unwrap(op), stream, *unwrap(config)));
863}
864
865void mlirOperationDump(MlirOperation op) { return unwrap(op)->dump(); }
866
867bool mlirOperationVerify(MlirOperation op) {
868 return succeeded(verify(unwrap(op)));
869}
870
871void mlirOperationMoveAfter(MlirOperation op, MlirOperation other) {
872 return unwrap(op)->moveAfter(unwrap(other));
873}
874
875void mlirOperationMoveBefore(MlirOperation op, MlirOperation other) {
876 return unwrap(op)->moveBefore(unwrap(other));
877}
878
879bool mlirOperationIsBeforeInBlock(MlirOperation op, MlirOperation other) {
880 return unwrap(op)->isBeforeInBlock(unwrap(other));
881}
882
884 switch (result) {
887
890
892 return mlir::WalkResult::skip();
893 }
894 llvm_unreachable("unknown result in WalkResult::unwrap");
895}
896
897void mlirOperationWalk(MlirOperation op, MlirOperationWalkCallback callback,
898 void *userData, MlirWalkOrder walkOrder) {
899 switch (walkOrder) {
900
901 case MlirWalkPreOrder:
903 [callback, userData](Operation *op) {
904 return unwrap(callback(wrap(op), userData));
905 });
906 break;
909 [callback, userData](Operation *op) {
910 return unwrap(callback(wrap(op), userData));
911 });
912 }
913}
914
915void mlirOperationReplaceUsesOfWith(MlirOperation op, MlirValue oldValue,
916 MlirValue newValue) {
917 unwrap(op)->replaceUsesOfWith(unwrap(oldValue), unwrap(newValue));
918}
919
920//===----------------------------------------------------------------------===//
921// Region API.
922//===----------------------------------------------------------------------===//
923
924MlirRegion mlirRegionCreate() { return wrap(new Region); }
925
926bool mlirRegionEqual(MlirRegion region, MlirRegion other) {
927 return unwrap(region) == unwrap(other);
928}
929
930MlirBlock mlirRegionGetFirstBlock(MlirRegion region) {
931 Region *cppRegion = unwrap(region);
932 if (cppRegion->empty())
933 return wrap(static_cast<Block *>(nullptr));
934 return wrap(&cppRegion->front());
935}
936
937void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block) {
938 unwrap(region)->push_back(unwrap(block));
939}
940
941void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos,
942 MlirBlock block) {
943 auto &blockList = unwrap(region)->getBlocks();
944 blockList.insert(std::next(blockList.begin(), pos), unwrap(block));
945}
946
947void mlirRegionInsertOwnedBlockAfter(MlirRegion region, MlirBlock reference,
948 MlirBlock block) {
949 Region *cppRegion = unwrap(region);
950 if (mlirBlockIsNull(reference)) {
951 cppRegion->getBlocks().insert(cppRegion->begin(), unwrap(block));
952 return;
953 }
954
955 assert(unwrap(reference)->getParent() == unwrap(region) &&
956 "expected reference block to belong to the region");
957 cppRegion->getBlocks().insertAfter(Region::iterator(unwrap(reference)),
958 unwrap(block));
959}
960
961void mlirRegionInsertOwnedBlockBefore(MlirRegion region, MlirBlock reference,
962 MlirBlock block) {
963 if (mlirBlockIsNull(reference))
964 return mlirRegionAppendOwnedBlock(region, block);
965
966 assert(unwrap(reference)->getParent() == unwrap(region) &&
967 "expected reference block to belong to the region");
968 unwrap(region)->getBlocks().insert(Region::iterator(unwrap(reference)),
969 unwrap(block));
970}
971
972void mlirRegionDestroy(MlirRegion region) {
973 delete static_cast<Region *>(region.ptr);
974}
975
976void mlirRegionTakeBody(MlirRegion target, MlirRegion source) {
977 unwrap(target)->takeBody(*unwrap(source));
978}
979
980//===----------------------------------------------------------------------===//
981// Block API.
982//===----------------------------------------------------------------------===//
983
984MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args,
985 MlirLocation const *locs) {
986 Block *b = new Block;
987 for (intptr_t i = 0; i < nArgs; ++i)
988 b->addArgument(unwrap(args[i]), unwrap(locs[i]));
989 return wrap(b);
990}
991
992bool mlirBlockEqual(MlirBlock block, MlirBlock other) {
993 return unwrap(block) == unwrap(other);
994}
995
996MlirOperation mlirBlockGetParentOperation(MlirBlock block) {
997 return wrap(unwrap(block)->getParentOp());
998}
999
1000MlirRegion mlirBlockGetParentRegion(MlirBlock block) {
1001 return wrap(unwrap(block)->getParent());
1002}
1003
1004MlirBlock mlirBlockGetNextInRegion(MlirBlock block) {
1005 return wrap(unwrap(block)->getNextNode());
1006}
1007
1008MlirOperation mlirBlockGetFirstOperation(MlirBlock block) {
1009 Block *cppBlock = unwrap(block);
1010 if (cppBlock->empty())
1011 return wrap(static_cast<Operation *>(nullptr));
1012 return wrap(&cppBlock->front());
1013}
1014
1015MlirOperation mlirBlockGetTerminator(MlirBlock block) {
1016 Block *cppBlock = unwrap(block);
1017 if (cppBlock->empty())
1018 return wrap(static_cast<Operation *>(nullptr));
1019 Operation &back = cppBlock->back();
1020 if (!back.hasTrait<OpTrait::IsTerminator>())
1021 return wrap(static_cast<Operation *>(nullptr));
1022 return wrap(&back);
1023}
1024
1025void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation) {
1026 unwrap(block)->push_back(unwrap(operation));
1027}
1028
1029void mlirBlockInsertOwnedOperation(MlirBlock block, intptr_t pos,
1030 MlirOperation operation) {
1031 auto &opList = unwrap(block)->getOperations();
1032 opList.insert(std::next(opList.begin(), pos), unwrap(operation));
1033}
1034
1036 MlirOperation reference,
1037 MlirOperation operation) {
1038 Block *cppBlock = unwrap(block);
1039 if (mlirOperationIsNull(reference)) {
1040 cppBlock->getOperations().insert(cppBlock->begin(), unwrap(operation));
1041 return;
1042 }
1043
1044 assert(unwrap(reference)->getBlock() == unwrap(block) &&
1045 "expected reference operation to belong to the block");
1046 cppBlock->getOperations().insertAfter(Block::iterator(unwrap(reference)),
1047 unwrap(operation));
1048}
1049
1051 MlirOperation reference,
1052 MlirOperation operation) {
1053 if (mlirOperationIsNull(reference))
1054 return mlirBlockAppendOwnedOperation(block, operation);
1055
1056 assert(unwrap(reference)->getBlock() == unwrap(block) &&
1057 "expected reference operation to belong to the block");
1058 unwrap(block)->getOperations().insert(Block::iterator(unwrap(reference)),
1059 unwrap(operation));
1060}
1061
1062void mlirBlockDestroy(MlirBlock block) { delete unwrap(block); }
1063
1064void mlirBlockDetach(MlirBlock block) {
1065 Block *b = unwrap(block);
1066 b->getParent()->getBlocks().remove(b);
1067}
1068
1070 return static_cast<intptr_t>(unwrap(block)->getNumArguments());
1071}
1072
1073MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type,
1074 MlirLocation loc) {
1075 return wrap(unwrap(block)->addArgument(unwrap(type), unwrap(loc)));
1076}
1077
1078void mlirBlockEraseArgument(MlirBlock block, unsigned index) {
1079 return unwrap(block)->eraseArgument(index);
1080}
1081
1082MlirValue mlirBlockInsertArgument(MlirBlock block, intptr_t pos, MlirType type,
1083 MlirLocation loc) {
1084 return wrap(unwrap(block)->insertArgument(pos, unwrap(type), unwrap(loc)));
1085}
1086
1087MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos) {
1088 return wrap(unwrap(block)->getArgument(static_cast<unsigned>(pos)));
1089}
1090
1091void mlirBlockPrint(MlirBlock block, MlirStringCallback callback,
1092 void *userData) {
1093 detail::CallbackOstream stream(callback, userData);
1094 unwrap(block)->print(stream);
1095}
1096
1098 return static_cast<intptr_t>(unwrap(block)->getNumSuccessors());
1099}
1100
1101MlirBlock mlirBlockGetSuccessor(MlirBlock block, intptr_t pos) {
1102 return wrap(unwrap(block)->getSuccessor(static_cast<unsigned>(pos)));
1103}
1104
1106 Block *b = unwrap(block);
1107 return static_cast<intptr_t>(std::distance(b->pred_begin(), b->pred_end()));
1108}
1109
1110MlirBlock mlirBlockGetPredecessor(MlirBlock block, intptr_t pos) {
1111 Block *b = unwrap(block);
1112 Block::pred_iterator it = b->pred_begin();
1113 std::advance(it, pos);
1114 return wrap(*it);
1115}
1116
1117//===----------------------------------------------------------------------===//
1118// Value API.
1119//===----------------------------------------------------------------------===//
1120
1121bool mlirValueEqual(MlirValue value1, MlirValue value2) {
1122 return unwrap(value1) == unwrap(value2);
1123}
1124
1125bool mlirValueIsABlockArgument(MlirValue value) {
1126 return llvm::isa<BlockArgument>(unwrap(value));
1127}
1128
1129bool mlirValueIsAOpResult(MlirValue value) {
1130 return llvm::isa<OpResult>(unwrap(value));
1131}
1132
1133MlirBlock mlirBlockArgumentGetOwner(MlirValue value) {
1134 return wrap(llvm::dyn_cast<BlockArgument>(unwrap(value)).getOwner());
1135}
1136
1138 return static_cast<intptr_t>(
1139 llvm::dyn_cast<BlockArgument>(unwrap(value)).getArgNumber());
1140}
1141
1142void mlirBlockArgumentSetType(MlirValue value, MlirType type) {
1143 if (auto blockArg = llvm::dyn_cast<BlockArgument>(unwrap(value)))
1144 blockArg.setType(unwrap(type));
1145}
1146
1147void mlirBlockArgumentSetLocation(MlirValue value, MlirLocation loc) {
1148 if (auto blockArg = llvm::dyn_cast<BlockArgument>(unwrap(value)))
1149 blockArg.setLoc(unwrap(loc));
1150}
1151
1152MlirOperation mlirOpResultGetOwner(MlirValue value) {
1153 return wrap(llvm::dyn_cast<OpResult>(unwrap(value)).getOwner());
1154}
1155
1157 return static_cast<intptr_t>(
1158 llvm::dyn_cast<OpResult>(unwrap(value)).getResultNumber());
1159}
1160
1161MlirType mlirValueGetType(MlirValue value) {
1162 return wrap(unwrap(value).getType());
1163}
1164
1165void mlirValueSetType(MlirValue value, MlirType type) {
1166 unwrap(value).setType(unwrap(type));
1167}
1168
1169void mlirValueDump(MlirValue value) { unwrap(value).dump(); }
1170
1171void mlirValuePrint(MlirValue value, MlirStringCallback callback,
1172 void *userData) {
1173 detail::CallbackOstream stream(callback, userData);
1174 unwrap(value).print(stream);
1175}
1176
1177void mlirValuePrintAsOperand(MlirValue value, MlirAsmState state,
1178 MlirStringCallback callback, void *userData) {
1179 detail::CallbackOstream stream(callback, userData);
1180 Value cppValue = unwrap(value);
1181 cppValue.printAsOperand(stream, *unwrap(state));
1182}
1183
1184MlirOpOperand mlirValueGetFirstUse(MlirValue value) {
1185 Value cppValue = unwrap(value);
1186 if (cppValue.use_empty())
1187 return {};
1188
1189 OpOperand *opOperand = cppValue.use_begin().getOperand();
1190
1191 return wrap(opOperand);
1192}
1193
1194void mlirValueReplaceAllUsesOfWith(MlirValue oldValue, MlirValue newValue) {
1195 unwrap(oldValue).replaceAllUsesWith(unwrap(newValue));
1196}
1197
1198void mlirValueReplaceAllUsesExcept(MlirValue oldValue, MlirValue newValue,
1199 intptr_t numExceptions,
1200 MlirOperation *exceptions) {
1201 Value oldValueCpp = unwrap(oldValue);
1202 Value newValueCpp = unwrap(newValue);
1203
1205 for (intptr_t i = 0; i < numExceptions; ++i) {
1206 exceptionSet.insert(unwrap(exceptions[i]));
1207 }
1208
1209 oldValueCpp.replaceAllUsesExcept(newValueCpp, exceptionSet);
1210}
1211
1212MlirLocation mlirValueGetLocation(MlirValue v) {
1213 return wrap(unwrap(v).getLoc());
1214}
1215
1216MlirContext mlirValueGetContext(MlirValue v) {
1217 return wrap(unwrap(v).getContext());
1218}
1219
1220//===----------------------------------------------------------------------===//
1221// OpOperand API.
1222//===----------------------------------------------------------------------===//
1223
1224bool mlirOpOperandIsNull(MlirOpOperand opOperand) { return !opOperand.ptr; }
1225
1226MlirOperation mlirOpOperandGetOwner(MlirOpOperand opOperand) {
1227 return wrap(unwrap(opOperand)->getOwner());
1228}
1229
1230MlirValue mlirOpOperandGetValue(MlirOpOperand opOperand) {
1231 return wrap(unwrap(opOperand)->get());
1232}
1233
1234unsigned mlirOpOperandGetOperandNumber(MlirOpOperand opOperand) {
1235 return unwrap(opOperand)->getOperandNumber();
1236}
1237
1238MlirOpOperand mlirOpOperandGetNextUse(MlirOpOperand opOperand) {
1239 if (mlirOpOperandIsNull(opOperand))
1240 return {};
1241
1242 OpOperand *nextOpOperand = static_cast<OpOperand *>(
1243 unwrap(opOperand)->getNextOperandUsingThisValue());
1244
1245 if (!nextOpOperand)
1246 return {};
1247
1248 return wrap(nextOpOperand);
1249}
1250
1251//===----------------------------------------------------------------------===//
1252// Type API.
1253//===----------------------------------------------------------------------===//
1254
1255MlirType mlirTypeParseGet(MlirContext context, MlirStringRef type) {
1256 return wrap(mlir::parseType(unwrap(type), unwrap(context)));
1257}
1258
1259MlirContext mlirTypeGetContext(MlirType type) {
1260 return wrap(unwrap(type).getContext());
1261}
1262
1263MlirTypeID mlirTypeGetTypeID(MlirType type) {
1264 return wrap(unwrap(type).getTypeID());
1265}
1266
1267MlirDialect mlirTypeGetDialect(MlirType type) {
1268 return wrap(&unwrap(type).getDialect());
1269}
1270
1271bool mlirTypeEqual(MlirType t1, MlirType t2) {
1272 return unwrap(t1) == unwrap(t2);
1273}
1274
1275void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData) {
1276 detail::CallbackOstream stream(callback, userData);
1277 unwrap(type).print(stream);
1278}
1279
1280void mlirTypeDump(MlirType type) { unwrap(type).dump(); }
1281
1282//===----------------------------------------------------------------------===//
1283// Attribute API.
1284//===----------------------------------------------------------------------===//
1285
1286MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr) {
1287 return wrap(mlir::parseAttribute(unwrap(attr), unwrap(context)));
1288}
1289
1290MlirContext mlirAttributeGetContext(MlirAttribute attribute) {
1291 return wrap(unwrap(attribute).getContext());
1292}
1293
1294MlirType mlirAttributeGetType(MlirAttribute attribute) {
1295 Attribute attr = unwrap(attribute);
1296 if (auto typedAttr = llvm::dyn_cast<TypedAttr>(attr))
1297 return wrap(typedAttr.getType());
1298 return wrap(NoneType::get(attr.getContext()));
1299}
1300
1301MlirTypeID mlirAttributeGetTypeID(MlirAttribute attr) {
1302 return wrap(unwrap(attr).getTypeID());
1303}
1304
1305MlirDialect mlirAttributeGetDialect(MlirAttribute attr) {
1306 return wrap(&unwrap(attr).getDialect());
1307}
1308
1309bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2) {
1310 return unwrap(a1) == unwrap(a2);
1311}
1312
1313void mlirAttributePrint(MlirAttribute attr, MlirStringCallback callback,
1314 void *userData) {
1315 detail::CallbackOstream stream(callback, userData);
1316 unwrap(attr).print(stream);
1317}
1318
1319void mlirAttributeDump(MlirAttribute attr) { unwrap(attr).dump(); }
1320
1322 MlirAttribute attr) {
1323 return MlirNamedAttribute{name, attr};
1324}
1325
1326//===----------------------------------------------------------------------===//
1327// Identifier API.
1328//===----------------------------------------------------------------------===//
1329
1330MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str) {
1331 return wrap(StringAttr::get(unwrap(context), unwrap(str)));
1332}
1333
1334MlirContext mlirIdentifierGetContext(MlirIdentifier ident) {
1335 return wrap(unwrap(ident).getContext());
1336}
1337
1338bool mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other) {
1339 return unwrap(ident) == unwrap(other);
1340}
1341
1342MlirStringRef mlirIdentifierStr(MlirIdentifier ident) {
1343 return wrap(unwrap(ident).strref());
1344}
1345
1346//===----------------------------------------------------------------------===//
1347// Symbol and SymbolTable API.
1348//===----------------------------------------------------------------------===//
1349
1353
1357
1358MlirSymbolTable mlirSymbolTableCreate(MlirOperation operation) {
1359 if (!unwrap(operation)->hasTrait<OpTrait::SymbolTable>())
1360 return wrap(static_cast<SymbolTable *>(nullptr));
1361 return wrap(new SymbolTable(unwrap(operation)));
1362}
1363
1364void mlirSymbolTableDestroy(MlirSymbolTable symbolTable) {
1365 delete unwrap(symbolTable);
1366}
1367
1368MlirOperation mlirSymbolTableLookup(MlirSymbolTable symbolTable,
1369 MlirStringRef name) {
1370 return wrap(unwrap(symbolTable)->lookup(StringRef(name.data, name.length)));
1371}
1372
1373MlirAttribute mlirSymbolTableInsert(MlirSymbolTable symbolTable,
1374 MlirOperation operation) {
1375 return wrap((Attribute)unwrap(symbolTable)->insert(unwrap(operation)));
1376}
1377
1378void mlirSymbolTableErase(MlirSymbolTable symbolTable,
1379 MlirOperation operation) {
1380 unwrap(symbolTable)->erase(unwrap(operation));
1381}
1382
1384 MlirStringRef newSymbol,
1385 MlirOperation from) {
1386 auto *cppFrom = unwrap(from);
1387 auto *context = cppFrom->getContext();
1388 auto oldSymbolAttr = StringAttr::get(context, unwrap(oldSymbol));
1389 auto newSymbolAttr = StringAttr::get(context, unwrap(newSymbol));
1390 return wrap(SymbolTable::replaceAllSymbolUses(oldSymbolAttr, newSymbolAttr,
1391 unwrap(from)));
1392}
1393
1394void mlirSymbolTableWalkSymbolTables(MlirOperation from, bool allSymUsesVisible,
1395 void (*callback)(MlirOperation, bool,
1396 void *userData),
1397 void *userData) {
1398 SymbolTable::walkSymbolTables(unwrap(from), allSymUsesVisible,
1399 [&](Operation *foundOpCpp, bool isVisible) {
1400 callback(wrap(foundOpCpp), isVisible,
1401 userData);
1402 });
1403}
return success()
lhs
unsigned mlirOpOperandGetOperandNumber(MlirOpOperand opOperand)
Returns the operand number of an op operand.
Definition IR.cpp:1234
MlirLocation mlirValueGetLocation(MlirValue v)
Gets the location of the value.
Definition IR.cpp:1212
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:865
MlirAttribute mlirOperationGetDiscardableAttributeByName(MlirOperation op, MlirStringRef name)
Definition IR.cpp:786
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:1368
MlirRegion mlirRegionCreate()
Creates a new empty region and transfers ownership to the caller.
Definition IR.cpp:924
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:830
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:1171
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:1105
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:1226
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:701
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:858
MlirIdentifier mlirOperationGetName(MlirOperation op)
Definition IR.cpp:674
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:1338
void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags, MlirStringCallback callback, void *userData)
Same as mlirOperationPrint but accepts flags controlling the printing behavior.
Definition IR.cpp:836
bool mlirValueIsABlockArgument(MlirValue value)
Definition IR.cpp:1125
MlirTypeID mlirTypeGetTypeID(MlirType type)
Gets the type ID of the type.
Definition IR.cpp:1263
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:1194
MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type, MlirLocation loc)
Appends an argument of the specified type to the block.
Definition IR.cpp:1073
intptr_t mlirOperationGetNumRegions(MlirOperation op)
Definition IR.cpp:686
MlirBlock mlirOperationGetBlock(MlirOperation op)
Definition IR.cpp:678
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:1330
void mlirBlockArgumentSetType(MlirValue value, MlirType type)
Definition IR.cpp:1142
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:1321
MlirStringRef mlirSymbolTableGetVisibilityAttributeName()
Returns the name of the attribute used to store symbol visibility.
Definition IR.cpp:1354
void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n, MlirNamedAttribute const *attributes)
Definition IR.cpp:520
MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos)
Definition IR.cpp:741
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:1394
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
bool mlirOperationNameHasTrait(MlirStringRef opName, MlirTypeID traitTypeID, MlirContext context)
Definition IR.cpp:654
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:1313
MlirBlock mlirRegionGetFirstBlock(MlirRegion region)
Gets the first block in the region.
Definition IR.cpp:930
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:1383
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:737
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:1184
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:947
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:1198
void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation)
Takes an operation owned by the caller and appends it to the block.
Definition IR.cpp:1025
MlirRegion mlirOperationGetFirstRegion(MlirOperation op)
Returns first region attached to the operation.
Definition IR.cpp:694
MlirAttribute mlirOperationGetInherentAttributeByName(MlirOperation op, MlirStringRef name)
Definition IR.cpp:759
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:1290
MlirStringRef mlirSymbolTableGetSymbolAttributeName()
Returns the name of the attribute used to store symbol names compatible with symbol tables.
Definition IR.cpp:1350
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:1029
MlirRegion mlirBlockGetParentRegion(MlirBlock block)
Returns the region that contains this block.
Definition IR.cpp:1000
MlirType mlirValueGetType(MlirValue value)
Definition IR.cpp:1161
void mlirOperationMoveAfter(MlirOperation op, MlirOperation other)
Moves the given operation immediately after the other operation in its parent block.
Definition IR.cpp:871
void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block)
Takes a block owned by the caller and appends it to the given region.
Definition IR.cpp:937
void mlirBlockPrint(MlirBlock block, MlirStringCallback callback, void *userData)
Definition IR.cpp:1091
void mlirOperationSetDiscardableAttributeByName(MlirOperation op, MlirStringRef name, MlirAttribute attr)
Definition IR.cpp:791
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:1004
void mlirOperationSetSuccessor(MlirOperation op, intptr_t pos, MlirBlock block)
Definition IR.cpp:802
MlirValue mlirOpOperandGetValue(MlirOpOperand opOperand)
Returns the value of an op operand.
Definition IR.cpp:1230
#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:710
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:883
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:961
MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos)
Returns pos-th argument of the block.
Definition IR.cpp:1087
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:668
intptr_t mlirBlockArgumentGetArgNumber(MlirValue value)
Definition IR.cpp:1137
void mlirOperationWalk(MlirOperation op, MlirOperationWalkCallback callback, void *userData, MlirWalkOrder walkOrder)
Walks operation op in walkOrder and calls callback on that operation.
Definition IR.cpp:897
MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos)
Definition IR.cpp:749
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:1050
bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2)
Definition IR.cpp:1309
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:767
void mlirOpPrintingFlagsAssumeVerified(MlirOpPrintingFlags flags)
Definition IR.cpp:236
bool mlirValueEqual(MlirValue value1, MlirValue value2)
Definition IR.cpp:1121
MlirContext mlirIdentifierGetContext(MlirIdentifier ident)
Returns the context associated with this identifier.
Definition IR.cpp:1334
void mlirBytecodeWriterConfigDestroy(MlirBytecodeWriterConfig config)
Definition IR.cpp:251
MlirBlock mlirBlockGetSuccessor(MlirBlock block, intptr_t pos)
Definition IR.cpp:1101
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:682
void mlirRegionTakeBody(MlirRegion target, MlirRegion source)
Moves the entire content of the source region to the target region.
Definition IR.cpp:976
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:1082
void mlirValueSetType(MlirValue value, MlirType type)
Definition IR.cpp:1165
intptr_t mlirOperationGetNumSuccessors(MlirOperation op)
Definition IR.cpp:745
MlirDialect mlirAttributeGetDialect(MlirAttribute attr)
Definition IR.cpp:1305
void mlirLocationPrint(MlirLocation location, MlirStringCallback callback, void *userData)
Definition IR.cpp:414
void mlirOperationSetAttributeByName(MlirOperation op, MlirStringRef name, MlirAttribute attr)
Definition IR.cpp:821
void mlirBlockDestroy(MlirBlock block)
Takes a block owned by the caller and destroys it.
Definition IR.cpp:1062
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:941
void mlirOperationSetOperand(MlirOperation op, intptr_t pos, MlirValue newValue)
Definition IR.cpp:726
intptr_t mlirBlockGetNumArguments(MlirBlock block)
Returns the number of arguments of the block.
Definition IR.cpp:1069
MlirTypeID mlirLocationFileLineColRangeGetTypeID()
TypeID Getter for FileLineColRange.
Definition IR.cpp:316
MlirOperation mlirOpResultGetOwner(MlirValue value)
Definition IR.cpp:1152
void mlirBlockEraseArgument(MlirBlock block, unsigned index)
Erase the argument at 'index' and remove it from the argument list.
Definition IR.cpp:1078
bool mlirOpOperandIsNull(MlirOpOperand opOperand)
Returns whether the op operand is null.
Definition IR.cpp:1224
MlirDialect mlirTypeGetDialect(MlirType type)
Gets the dialect a type belongs to.
Definition IR.cpp:1267
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:875
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:1177
MlirBlock mlirBlockArgumentGetOwner(MlirValue value)
Definition IR.cpp:1133
void mlirBlockArgumentSetLocation(MlirValue value, MlirLocation loc)
Definition IR.cpp:1147
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:718
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:722
MlirLocation mlirOperationGetLocation(MlirOperation op)
Definition IR.cpp:660
bool mlirTypeEqual(MlirType t1, MlirType t2)
Checks if two types are equal.
Definition IR.cpp:1271
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:816
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:879
MlirTypeID mlirAttributeGetTypeID(MlirAttribute attr)
Definition IR.cpp:1301
MlirAttribute mlirSymbolTableInsert(MlirSymbolTable symbolTable, MlirOperation operation)
Inserts the given operation into the given symbol table.
Definition IR.cpp:1373
bool mlirLocationIsAFileLineColRange(MlirLocation location)
Checks whether the given location is an FileLineColRange.
Definition IR.cpp:320
intptr_t mlirOperationGetNumDiscardableAttributes(MlirOperation op)
Definition IR.cpp:774
MlirSymbolTable mlirSymbolTableCreate(MlirOperation operation)
Creates a symbol table for the given operation.
Definition IR.cpp:1358
void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n, MlirRegion const *regions)
Definition IR.cpp:512
void mlirOperationSetLocation(MlirOperation op, MlirLocation loc)
Definition IR.cpp:664
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:1008
MlirType mlirAttributeGetType(MlirAttribute attribute)
Definition IR.cpp:1294
bool mlirOperationRemoveDiscardableAttributeByName(MlirOperation op, MlirStringRef name)
Definition IR.cpp:797
void mlirRegionDestroy(MlirRegion region)
Takes a region owned by the caller and destroys it.
Definition IR.cpp:972
bool mlirOperationRemoveAttributeByName(MlirOperation op, MlirStringRef name)
Definition IR.cpp:826
bool mlirValueIsAOpResult(MlirValue value)
Definition IR.cpp:1129
MLIR_CAPI_EXPORTED bool mlirOperationHasInherentAttributeByName(MlirOperation op, MlirStringRef name)
Definition IR.cpp:754
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:1110
MlirOperation mlirBlockGetTerminator(MlirBlock block)
Returns the terminator operation in the block or null if no terminator.
Definition IR.cpp:1015
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:690
void mlirOperationReplaceUsesOfWith(MlirOperation op, MlirValue oldValue, MlirValue newValue)
Replace uses of 'of' value with the 'with' value inside the 'op' operation.
Definition IR.cpp:915
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:1286
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:1364
intptr_t mlirBlockGetNumSuccessors(MlirBlock block)
Definition IR.cpp:1097
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:842
bool mlirOperationVerify(MlirOperation op)
Verify the operation and return true if it passes, false if it fails.
Definition IR.cpp:867
void mlirBlockDetach(MlirBlock block)
Detach a block from the owning region and assume ownership.
Definition IR.cpp:1064
int mlirLocationFileLineColRangeGetEndLine(MlirLocation location)
Getter for end_line of FileLineColRange.
Definition IR.cpp:304
MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos)
Definition IR.cpp:811
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:851
void mlirValueDump(MlirValue value)
Definition IR.cpp:1169
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:1035
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:731
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:1259
void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData)
Definition IR.cpp:1275
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:926
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:1156
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:992
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:1378
MlirLocation mlirLocationNameGetChildLoc(MlirLocation location)
Getter for childLoc of Name.
Definition IR.cpp:391
MlirNamedAttribute mlirOperationGetDiscardableAttribute(MlirOperation op, intptr_t pos)
Definition IR.cpp:779
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:1319
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:1342
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:996
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:1238
intptr_t mlirOperationGetNumOperands(MlirOperation op)
Definition IR.cpp:714
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:1255
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:984
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:1280
intptr_t mlirOperationGetNumAttributes(MlirOperation op)
Definition IR.cpp:807
MlirContext mlirValueGetContext(MlirValue v)
Gets the context that a value was created with.
Definition IR.cpp:1216
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:860
MlirWalkOrder
Traversal order for operation walk.
Definition IR.h:853
@ MlirWalkPreOrder
Definition IR.h:854
@ MlirWalkPostOrder
Definition IR.h:855
MlirWalkResult
Operation walk result.
Definition IR.h:846
@ MlirWalkResultInterrupt
Definition IR.h:848
@ MlirWalkResultSkip
Definition IR.h:849
@ MlirWalkResultAdvance
Definition IR.h:847
static bool mlirBlockIsNull(MlirBlock block)
Checks whether a block is null.
Definition IR.h:946
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: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.