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