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