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 void mlirOpPrintingFlagsSkipRegions(MlirOpPrintingFlags flags) {
223  unwrap(flags)->skipRegions();
224 }
225 //===----------------------------------------------------------------------===//
226 // Bytecode printing flags API.
227 //===----------------------------------------------------------------------===//
228 
229 MlirBytecodeWriterConfig mlirBytecodeWriterConfigCreate() {
230  return wrap(new BytecodeWriterConfig());
231 }
232 
233 void mlirBytecodeWriterConfigDestroy(MlirBytecodeWriterConfig config) {
234  delete unwrap(config);
235 }
236 
237 void mlirBytecodeWriterConfigDesiredEmitVersion(MlirBytecodeWriterConfig flags,
238  int64_t version) {
239  unwrap(flags)->setDesiredBytecodeVersion(version);
240 }
241 
242 //===----------------------------------------------------------------------===//
243 // Location API.
244 //===----------------------------------------------------------------------===//
245 
246 MlirAttribute mlirLocationGetAttribute(MlirLocation location) {
247  return wrap(LocationAttr(unwrap(location)));
248 }
249 
250 MlirLocation mlirLocationFromAttribute(MlirAttribute attribute) {
251  return wrap(Location(llvm::cast<LocationAttr>(unwrap(attribute))));
252 }
253 
254 MlirLocation mlirLocationFileLineColGet(MlirContext context,
255  MlirStringRef filename, unsigned line,
256  unsigned col) {
257  return wrap(Location(
258  FileLineColLoc::get(unwrap(context), unwrap(filename), line, col)));
259 }
260 
261 MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller) {
262  return wrap(Location(CallSiteLoc::get(unwrap(callee), unwrap(caller))));
263 }
264 
265 MlirLocation mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations,
266  MlirLocation const *locations,
267  MlirAttribute metadata) {
269  ArrayRef<Location> unwrappedLocs = unwrapList(nLocations, locations, locs);
270  return wrap(FusedLoc::get(unwrappedLocs, unwrap(metadata), unwrap(ctx)));
271 }
272 
273 MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name,
274  MlirLocation childLoc) {
275  if (mlirLocationIsNull(childLoc))
276  return wrap(
277  Location(NameLoc::get(StringAttr::get(unwrap(context), unwrap(name)))));
278  return wrap(Location(NameLoc::get(
279  StringAttr::get(unwrap(context), unwrap(name)), unwrap(childLoc))));
280 }
281 
282 MlirLocation mlirLocationUnknownGet(MlirContext context) {
283  return wrap(Location(UnknownLoc::get(unwrap(context))));
284 }
285 
286 bool mlirLocationEqual(MlirLocation l1, MlirLocation l2) {
287  return unwrap(l1) == unwrap(l2);
288 }
289 
290 MlirContext mlirLocationGetContext(MlirLocation location) {
291  return wrap(unwrap(location).getContext());
292 }
293 
294 void mlirLocationPrint(MlirLocation location, MlirStringCallback callback,
295  void *userData) {
296  detail::CallbackOstream stream(callback, userData);
297  unwrap(location).print(stream);
298 }
299 
300 //===----------------------------------------------------------------------===//
301 // Module API.
302 //===----------------------------------------------------------------------===//
303 
304 MlirModule mlirModuleCreateEmpty(MlirLocation location) {
305  return wrap(ModuleOp::create(unwrap(location)));
306 }
307 
308 MlirModule mlirModuleCreateParse(MlirContext context, MlirStringRef module) {
309  OwningOpRef<ModuleOp> owning =
310  parseSourceString<ModuleOp>(unwrap(module), unwrap(context));
311  if (!owning)
312  return MlirModule{nullptr};
313  return MlirModule{owning.release().getOperation()};
314 }
315 
316 MlirContext mlirModuleGetContext(MlirModule module) {
317  return wrap(unwrap(module).getContext());
318 }
319 
320 MlirBlock mlirModuleGetBody(MlirModule module) {
321  return wrap(unwrap(module).getBody());
322 }
323 
324 void mlirModuleDestroy(MlirModule module) {
325  // Transfer ownership to an OwningOpRef<ModuleOp> so that its destructor is
326  // called.
327  OwningOpRef<ModuleOp>(unwrap(module));
328 }
329 
330 MlirOperation mlirModuleGetOperation(MlirModule module) {
331  return wrap(unwrap(module).getOperation());
332 }
333 
334 MlirModule mlirModuleFromOperation(MlirOperation op) {
335  return wrap(dyn_cast<ModuleOp>(unwrap(op)));
336 }
337 
338 //===----------------------------------------------------------------------===//
339 // Operation state API.
340 //===----------------------------------------------------------------------===//
341 
343  MlirOperationState state;
344  state.name = name;
345  state.location = loc;
346  state.nResults = 0;
347  state.results = nullptr;
348  state.nOperands = 0;
349  state.operands = nullptr;
350  state.nRegions = 0;
351  state.regions = nullptr;
352  state.nSuccessors = 0;
353  state.successors = nullptr;
354  state.nAttributes = 0;
355  state.attributes = nullptr;
356  state.enableResultTypeInference = false;
357  return state;
358 }
359 
360 #define APPEND_ELEMS(type, sizeName, elemName) \
361  state->elemName = \
362  (type *)realloc(state->elemName, (state->sizeName + n) * sizeof(type)); \
363  memcpy(state->elemName + state->sizeName, elemName, n * sizeof(type)); \
364  state->sizeName += n;
365 
367  MlirType const *results) {
368  APPEND_ELEMS(MlirType, nResults, results);
369 }
370 
372  MlirValue const *operands) {
373  APPEND_ELEMS(MlirValue, nOperands, operands);
374 }
376  MlirRegion const *regions) {
377  APPEND_ELEMS(MlirRegion, nRegions, regions);
378 }
380  MlirBlock const *successors) {
381  APPEND_ELEMS(MlirBlock, nSuccessors, successors);
382 }
384  MlirNamedAttribute const *attributes) {
385  APPEND_ELEMS(MlirNamedAttribute, nAttributes, attributes);
386 }
387 
389  state->enableResultTypeInference = true;
390 }
391 
392 //===----------------------------------------------------------------------===//
393 // Operation API.
394 //===----------------------------------------------------------------------===//
395 
397  MLIRContext *context = state.getContext();
398  std::optional<RegisteredOperationName> info = state.name.getRegisteredInfo();
399  if (!info) {
400  emitError(state.location)
401  << "type inference was requested for the operation " << state.name
402  << ", but the operation was not registered; ensure that the dialect "
403  "containing the operation is linked into MLIR and registered with "
404  "the context";
405  return failure();
406  }
407 
408  auto *inferInterface = info->getInterface<InferTypeOpInterface>();
409  if (!inferInterface) {
410  emitError(state.location)
411  << "type inference was requested for the operation " << state.name
412  << ", but the operation does not support type inference; result "
413  "types must be specified explicitly";
414  return failure();
415  }
416 
417  DictionaryAttr attributes = state.attributes.getDictionary(context);
418  OpaqueProperties properties = state.getRawProperties();
419 
420  if (!properties && info->getOpPropertyByteSize() > 0 && !attributes.empty()) {
421  auto prop = std::make_unique<char[]>(info->getOpPropertyByteSize());
422  properties = OpaqueProperties(prop.get());
423  if (properties) {
424  auto emitError = [&]() {
425  return mlir::emitError(state.location)
426  << " failed properties conversion while building "
427  << state.name.getStringRef() << " with `" << attributes << "`: ";
428  };
429  if (failed(info->setOpPropertiesFromAttribute(state.name, properties,
430  attributes, emitError)))
431  return failure();
432  }
433  if (succeeded(inferInterface->inferReturnTypes(
434  context, state.location, state.operands, attributes, properties,
435  state.regions, state.types))) {
436  return success();
437  }
438  // Diagnostic emitted by interface.
439  return failure();
440  }
441 
442  if (succeeded(inferInterface->inferReturnTypes(
443  context, state.location, state.operands, attributes, properties,
444  state.regions, state.types)))
445  return success();
446 
447  // Diagnostic emitted by interface.
448  return failure();
449 }
450 
452  assert(state);
453  OperationState cppState(unwrap(state->location), unwrap(state->name));
454  SmallVector<Type, 4> resultStorage;
455  SmallVector<Value, 8> operandStorage;
456  SmallVector<Block *, 2> successorStorage;
457  cppState.addTypes(unwrapList(state->nResults, state->results, resultStorage));
458  cppState.addOperands(
459  unwrapList(state->nOperands, state->operands, operandStorage));
460  cppState.addSuccessors(
461  unwrapList(state->nSuccessors, state->successors, successorStorage));
462 
463  cppState.attributes.reserve(state->nAttributes);
464  for (intptr_t i = 0; i < state->nAttributes; ++i)
465  cppState.addAttribute(unwrap(state->attributes[i].name),
466  unwrap(state->attributes[i].attribute));
467 
468  for (intptr_t i = 0; i < state->nRegions; ++i)
469  cppState.addRegion(std::unique_ptr<Region>(unwrap(state->regions[i])));
470 
471  free(state->results);
472  free(state->operands);
473  free(state->successors);
474  free(state->regions);
475  free(state->attributes);
476 
477  // Infer result types.
478  if (state->enableResultTypeInference) {
479  assert(cppState.types.empty() &&
480  "result type inference enabled and result types provided");
481  if (failed(inferOperationTypes(cppState)))
482  return {nullptr};
483  }
484 
485  return wrap(Operation::create(cppState));
486 }
487 
488 MlirOperation mlirOperationCreateParse(MlirContext context,
489  MlirStringRef sourceStr,
490  MlirStringRef sourceName) {
491 
492  return wrap(
493  parseSourceString(unwrap(sourceStr), unwrap(context), unwrap(sourceName))
494  .release());
495 }
496 
497 MlirOperation mlirOperationClone(MlirOperation op) {
498  return wrap(unwrap(op)->clone());
499 }
500 
501 void mlirOperationDestroy(MlirOperation op) { unwrap(op)->erase(); }
502 
503 void mlirOperationRemoveFromParent(MlirOperation op) { unwrap(op)->remove(); }
504 
505 bool mlirOperationEqual(MlirOperation op, MlirOperation other) {
506  return unwrap(op) == unwrap(other);
507 }
508 
509 MlirContext mlirOperationGetContext(MlirOperation op) {
510  return wrap(unwrap(op)->getContext());
511 }
512 
513 MlirLocation mlirOperationGetLocation(MlirOperation op) {
514  return wrap(unwrap(op)->getLoc());
515 }
516 
517 MlirTypeID mlirOperationGetTypeID(MlirOperation op) {
518  if (auto info = unwrap(op)->getRegisteredInfo())
519  return wrap(info->getTypeID());
520  return {nullptr};
521 }
522 
523 MlirIdentifier mlirOperationGetName(MlirOperation op) {
524  return wrap(unwrap(op)->getName().getIdentifier());
525 }
526 
527 MlirBlock mlirOperationGetBlock(MlirOperation op) {
528  return wrap(unwrap(op)->getBlock());
529 }
530 
531 MlirOperation mlirOperationGetParentOperation(MlirOperation op) {
532  return wrap(unwrap(op)->getParentOp());
533 }
534 
535 intptr_t mlirOperationGetNumRegions(MlirOperation op) {
536  return static_cast<intptr_t>(unwrap(op)->getNumRegions());
537 }
538 
539 MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos) {
540  return wrap(&unwrap(op)->getRegion(static_cast<unsigned>(pos)));
541 }
542 
543 MlirRegion mlirOperationGetFirstRegion(MlirOperation op) {
544  Operation *cppOp = unwrap(op);
545  if (cppOp->getNumRegions() == 0)
546  return wrap(static_cast<Region *>(nullptr));
547  return wrap(&cppOp->getRegion(0));
548 }
549 
550 MlirRegion mlirRegionGetNextInOperation(MlirRegion region) {
551  Region *cppRegion = unwrap(region);
552  Operation *parent = cppRegion->getParentOp();
553  intptr_t next = cppRegion->getRegionNumber() + 1;
554  if (parent->getNumRegions() > next)
555  return wrap(&parent->getRegion(next));
556  return wrap(static_cast<Region *>(nullptr));
557 }
558 
559 MlirOperation mlirOperationGetNextInBlock(MlirOperation op) {
560  return wrap(unwrap(op)->getNextNode());
561 }
562 
563 intptr_t mlirOperationGetNumOperands(MlirOperation op) {
564  return static_cast<intptr_t>(unwrap(op)->getNumOperands());
565 }
566 
567 MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos) {
568  return wrap(unwrap(op)->getOperand(static_cast<unsigned>(pos)));
569 }
570 
571 void mlirOperationSetOperand(MlirOperation op, intptr_t pos,
572  MlirValue newValue) {
573  unwrap(op)->setOperand(static_cast<unsigned>(pos), unwrap(newValue));
574 }
575 
576 void mlirOperationSetOperands(MlirOperation op, intptr_t nOperands,
577  MlirValue const *operands) {
578  SmallVector<Value> ops;
579  unwrap(op)->setOperands(unwrapList(nOperands, operands, ops));
580 }
581 
582 intptr_t mlirOperationGetNumResults(MlirOperation op) {
583  return static_cast<intptr_t>(unwrap(op)->getNumResults());
584 }
585 
586 MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos) {
587  return wrap(unwrap(op)->getResult(static_cast<unsigned>(pos)));
588 }
589 
590 intptr_t mlirOperationGetNumSuccessors(MlirOperation op) {
591  return static_cast<intptr_t>(unwrap(op)->getNumSuccessors());
592 }
593 
594 MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos) {
595  return wrap(unwrap(op)->getSuccessor(static_cast<unsigned>(pos)));
596 }
597 
600  std::optional<Attribute> attr = unwrap(op)->getInherentAttr(unwrap(name));
601  return attr.has_value();
602 }
603 
604 MlirAttribute mlirOperationGetInherentAttributeByName(MlirOperation op,
605  MlirStringRef name) {
606  std::optional<Attribute> attr = unwrap(op)->getInherentAttr(unwrap(name));
607  if (attr.has_value())
608  return wrap(*attr);
609  return {};
610 }
611 
613  MlirStringRef name,
614  MlirAttribute attr) {
615  unwrap(op)->setInherentAttr(
616  StringAttr::get(unwrap(op)->getContext(), unwrap(name)), unwrap(attr));
617 }
618 
619 intptr_t mlirOperationGetNumDiscardableAttributes(MlirOperation op) {
620  return static_cast<intptr_t>(
621  llvm::range_size(unwrap(op)->getDiscardableAttrs()));
622 }
623 
625  intptr_t pos) {
626  NamedAttribute attr =
627  *std::next(unwrap(op)->getDiscardableAttrs().begin(), pos);
628  return MlirNamedAttribute{wrap(attr.getName()), wrap(attr.getValue())};
629 }
630 
631 MlirAttribute mlirOperationGetDiscardableAttributeByName(MlirOperation op,
632  MlirStringRef name) {
633  return wrap(unwrap(op)->getDiscardableAttr(unwrap(name)));
634 }
635 
637  MlirStringRef name,
638  MlirAttribute attr) {
639  unwrap(op)->setDiscardableAttr(unwrap(name), unwrap(attr));
640 }
641 
643  MlirStringRef name) {
644  return !!unwrap(op)->removeDiscardableAttr(unwrap(name));
645 }
646 
647 void mlirOperationSetSuccessor(MlirOperation op, intptr_t pos,
648  MlirBlock block) {
649  unwrap(op)->setSuccessor(unwrap(block), static_cast<unsigned>(pos));
650 }
651 
652 intptr_t mlirOperationGetNumAttributes(MlirOperation op) {
653  return static_cast<intptr_t>(unwrap(op)->getAttrs().size());
654 }
655 
656 MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos) {
657  NamedAttribute attr = unwrap(op)->getAttrs()[pos];
658  return MlirNamedAttribute{wrap(attr.getName()), wrap(attr.getValue())};
659 }
660 
661 MlirAttribute mlirOperationGetAttributeByName(MlirOperation op,
662  MlirStringRef name) {
663  return wrap(unwrap(op)->getAttr(unwrap(name)));
664 }
665 
666 void mlirOperationSetAttributeByName(MlirOperation op, MlirStringRef name,
667  MlirAttribute attr) {
668  unwrap(op)->setAttr(unwrap(name), unwrap(attr));
669 }
670 
671 bool mlirOperationRemoveAttributeByName(MlirOperation op, MlirStringRef name) {
672  return !!unwrap(op)->removeAttr(unwrap(name));
673 }
674 
675 void mlirOperationPrint(MlirOperation op, MlirStringCallback callback,
676  void *userData) {
677  detail::CallbackOstream stream(callback, userData);
678  unwrap(op)->print(stream);
679 }
680 
681 void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags,
682  MlirStringCallback callback, void *userData) {
683  detail::CallbackOstream stream(callback, userData);
684  unwrap(op)->print(stream, *unwrap(flags));
685 }
686 
687 void mlirOperationPrintWithState(MlirOperation op, MlirAsmState state,
688  MlirStringCallback callback, void *userData) {
689  detail::CallbackOstream stream(callback, userData);
690  if (state.ptr)
691  unwrap(op)->print(stream, *unwrap(state));
692  unwrap(op)->print(stream);
693 }
694 
695 void mlirOperationWriteBytecode(MlirOperation op, MlirStringCallback callback,
696  void *userData) {
697  detail::CallbackOstream stream(callback, userData);
698  // As no desired version is set, no failure can occur.
699  (void)writeBytecodeToFile(unwrap(op), stream);
700 }
701 
703  MlirOperation op, MlirBytecodeWriterConfig config,
704  MlirStringCallback callback, void *userData) {
705  detail::CallbackOstream stream(callback, userData);
706  return wrap(writeBytecodeToFile(unwrap(op), stream, *unwrap(config)));
707 }
708 
709 void mlirOperationDump(MlirOperation op) { return unwrap(op)->dump(); }
710 
711 bool mlirOperationVerify(MlirOperation op) {
712  return succeeded(verify(unwrap(op)));
713 }
714 
715 void mlirOperationMoveAfter(MlirOperation op, MlirOperation other) {
716  return unwrap(op)->moveAfter(unwrap(other));
717 }
718 
719 void mlirOperationMoveBefore(MlirOperation op, MlirOperation other) {
720  return unwrap(op)->moveBefore(unwrap(other));
721 }
722 
724  switch (result) {
726  return mlir::WalkResult::advance();
727 
730 
731  case MlirWalkResultSkip:
732  return mlir::WalkResult::skip();
733  }
734 }
735 
736 void mlirOperationWalk(MlirOperation op, MlirOperationWalkCallback callback,
737  void *userData, MlirWalkOrder walkOrder) {
738  switch (walkOrder) {
739 
740  case MlirWalkPreOrder:
742  [callback, userData](Operation *op) {
743  return unwrap(callback(wrap(op), userData));
744  });
745  break;
746  case MlirWalkPostOrder:
748  [callback, userData](Operation *op) {
749  return unwrap(callback(wrap(op), userData));
750  });
751  }
752 }
753 
754 //===----------------------------------------------------------------------===//
755 // Region API.
756 //===----------------------------------------------------------------------===//
757 
758 MlirRegion mlirRegionCreate() { return wrap(new Region); }
759 
760 bool mlirRegionEqual(MlirRegion region, MlirRegion other) {
761  return unwrap(region) == unwrap(other);
762 }
763 
764 MlirBlock mlirRegionGetFirstBlock(MlirRegion region) {
765  Region *cppRegion = unwrap(region);
766  if (cppRegion->empty())
767  return wrap(static_cast<Block *>(nullptr));
768  return wrap(&cppRegion->front());
769 }
770 
771 void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block) {
772  unwrap(region)->push_back(unwrap(block));
773 }
774 
775 void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos,
776  MlirBlock block) {
777  auto &blockList = unwrap(region)->getBlocks();
778  blockList.insert(std::next(blockList.begin(), pos), unwrap(block));
779 }
780 
781 void mlirRegionInsertOwnedBlockAfter(MlirRegion region, MlirBlock reference,
782  MlirBlock block) {
783  Region *cppRegion = unwrap(region);
784  if (mlirBlockIsNull(reference)) {
785  cppRegion->getBlocks().insert(cppRegion->begin(), unwrap(block));
786  return;
787  }
788 
789  assert(unwrap(reference)->getParent() == unwrap(region) &&
790  "expected reference block to belong to the region");
791  cppRegion->getBlocks().insertAfter(Region::iterator(unwrap(reference)),
792  unwrap(block));
793 }
794 
795 void mlirRegionInsertOwnedBlockBefore(MlirRegion region, MlirBlock reference,
796  MlirBlock block) {
797  if (mlirBlockIsNull(reference))
798  return mlirRegionAppendOwnedBlock(region, block);
799 
800  assert(unwrap(reference)->getParent() == unwrap(region) &&
801  "expected reference block to belong to the region");
802  unwrap(region)->getBlocks().insert(Region::iterator(unwrap(reference)),
803  unwrap(block));
804 }
805 
806 void mlirRegionDestroy(MlirRegion region) {
807  delete static_cast<Region *>(region.ptr);
808 }
809 
810 void mlirRegionTakeBody(MlirRegion target, MlirRegion source) {
811  unwrap(target)->takeBody(*unwrap(source));
812 }
813 
814 //===----------------------------------------------------------------------===//
815 // Block API.
816 //===----------------------------------------------------------------------===//
817 
818 MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args,
819  MlirLocation const *locs) {
820  Block *b = new Block;
821  for (intptr_t i = 0; i < nArgs; ++i)
822  b->addArgument(unwrap(args[i]), unwrap(locs[i]));
823  return wrap(b);
824 }
825 
826 bool mlirBlockEqual(MlirBlock block, MlirBlock other) {
827  return unwrap(block) == unwrap(other);
828 }
829 
830 MlirOperation mlirBlockGetParentOperation(MlirBlock block) {
831  return wrap(unwrap(block)->getParentOp());
832 }
833 
834 MlirRegion mlirBlockGetParentRegion(MlirBlock block) {
835  return wrap(unwrap(block)->getParent());
836 }
837 
838 MlirBlock mlirBlockGetNextInRegion(MlirBlock block) {
839  return wrap(unwrap(block)->getNextNode());
840 }
841 
842 MlirOperation mlirBlockGetFirstOperation(MlirBlock block) {
843  Block *cppBlock = unwrap(block);
844  if (cppBlock->empty())
845  return wrap(static_cast<Operation *>(nullptr));
846  return wrap(&cppBlock->front());
847 }
848 
849 MlirOperation mlirBlockGetTerminator(MlirBlock block) {
850  Block *cppBlock = unwrap(block);
851  if (cppBlock->empty())
852  return wrap(static_cast<Operation *>(nullptr));
853  Operation &back = cppBlock->back();
854  if (!back.hasTrait<OpTrait::IsTerminator>())
855  return wrap(static_cast<Operation *>(nullptr));
856  return wrap(&back);
857 }
858 
859 void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation) {
860  unwrap(block)->push_back(unwrap(operation));
861 }
862 
863 void mlirBlockInsertOwnedOperation(MlirBlock block, intptr_t pos,
864  MlirOperation operation) {
865  auto &opList = unwrap(block)->getOperations();
866  opList.insert(std::next(opList.begin(), pos), unwrap(operation));
867 }
868 
870  MlirOperation reference,
871  MlirOperation operation) {
872  Block *cppBlock = unwrap(block);
873  if (mlirOperationIsNull(reference)) {
874  cppBlock->getOperations().insert(cppBlock->begin(), unwrap(operation));
875  return;
876  }
877 
878  assert(unwrap(reference)->getBlock() == unwrap(block) &&
879  "expected reference operation to belong to the block");
880  cppBlock->getOperations().insertAfter(Block::iterator(unwrap(reference)),
881  unwrap(operation));
882 }
883 
885  MlirOperation reference,
886  MlirOperation operation) {
887  if (mlirOperationIsNull(reference))
888  return mlirBlockAppendOwnedOperation(block, operation);
889 
890  assert(unwrap(reference)->getBlock() == unwrap(block) &&
891  "expected reference operation to belong to the block");
892  unwrap(block)->getOperations().insert(Block::iterator(unwrap(reference)),
893  unwrap(operation));
894 }
895 
896 void mlirBlockDestroy(MlirBlock block) { delete unwrap(block); }
897 
898 void mlirBlockDetach(MlirBlock block) {
899  Block *b = unwrap(block);
900  b->getParent()->getBlocks().remove(b);
901 }
902 
903 intptr_t mlirBlockGetNumArguments(MlirBlock block) {
904  return static_cast<intptr_t>(unwrap(block)->getNumArguments());
905 }
906 
907 MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type,
908  MlirLocation loc) {
909  return wrap(unwrap(block)->addArgument(unwrap(type), unwrap(loc)));
910 }
911 
912 void mlirBlockEraseArgument(MlirBlock block, unsigned index) {
913  return unwrap(block)->eraseArgument(index);
914 }
915 
916 MlirValue mlirBlockInsertArgument(MlirBlock block, intptr_t pos, MlirType type,
917  MlirLocation loc) {
918  return wrap(unwrap(block)->insertArgument(pos, unwrap(type), unwrap(loc)));
919 }
920 
921 MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos) {
922  return wrap(unwrap(block)->getArgument(static_cast<unsigned>(pos)));
923 }
924 
925 void mlirBlockPrint(MlirBlock block, MlirStringCallback callback,
926  void *userData) {
927  detail::CallbackOstream stream(callback, userData);
928  unwrap(block)->print(stream);
929 }
930 
931 //===----------------------------------------------------------------------===//
932 // Value API.
933 //===----------------------------------------------------------------------===//
934 
935 bool mlirValueEqual(MlirValue value1, MlirValue value2) {
936  return unwrap(value1) == unwrap(value2);
937 }
938 
939 bool mlirValueIsABlockArgument(MlirValue value) {
940  return llvm::isa<BlockArgument>(unwrap(value));
941 }
942 
943 bool mlirValueIsAOpResult(MlirValue value) {
944  return llvm::isa<OpResult>(unwrap(value));
945 }
946 
947 MlirBlock mlirBlockArgumentGetOwner(MlirValue value) {
948  return wrap(llvm::cast<BlockArgument>(unwrap(value)).getOwner());
949 }
950 
951 intptr_t mlirBlockArgumentGetArgNumber(MlirValue value) {
952  return static_cast<intptr_t>(
953  llvm::cast<BlockArgument>(unwrap(value)).getArgNumber());
954 }
955 
956 void mlirBlockArgumentSetType(MlirValue value, MlirType type) {
957  llvm::cast<BlockArgument>(unwrap(value)).setType(unwrap(type));
958 }
959 
960 MlirOperation mlirOpResultGetOwner(MlirValue value) {
961  return wrap(llvm::cast<OpResult>(unwrap(value)).getOwner());
962 }
963 
964 intptr_t mlirOpResultGetResultNumber(MlirValue value) {
965  return static_cast<intptr_t>(
966  llvm::cast<OpResult>(unwrap(value)).getResultNumber());
967 }
968 
969 MlirType mlirValueGetType(MlirValue value) {
970  return wrap(unwrap(value).getType());
971 }
972 
973 void mlirValueSetType(MlirValue value, MlirType type) {
974  unwrap(value).setType(unwrap(type));
975 }
976 
977 void mlirValueDump(MlirValue value) { unwrap(value).dump(); }
978 
979 void mlirValuePrint(MlirValue value, MlirStringCallback callback,
980  void *userData) {
981  detail::CallbackOstream stream(callback, userData);
982  unwrap(value).print(stream);
983 }
984 
985 void mlirValuePrintAsOperand(MlirValue value, MlirAsmState state,
986  MlirStringCallback callback, void *userData) {
987  detail::CallbackOstream stream(callback, userData);
988  Value cppValue = unwrap(value);
989  cppValue.printAsOperand(stream, *unwrap(state));
990 }
991 
992 MlirOpOperand mlirValueGetFirstUse(MlirValue value) {
993  Value cppValue = unwrap(value);
994  if (cppValue.use_empty())
995  return {};
996 
997  OpOperand *opOperand = cppValue.use_begin().getOperand();
998 
999  return wrap(opOperand);
1000 }
1001 
1002 void mlirValueReplaceAllUsesOfWith(MlirValue oldValue, MlirValue newValue) {
1003  unwrap(oldValue).replaceAllUsesWith(unwrap(newValue));
1004 }
1005 
1006 //===----------------------------------------------------------------------===//
1007 // OpOperand API.
1008 //===----------------------------------------------------------------------===//
1009 
1010 bool mlirOpOperandIsNull(MlirOpOperand opOperand) { return !opOperand.ptr; }
1011 
1012 MlirOperation mlirOpOperandGetOwner(MlirOpOperand opOperand) {
1013  return wrap(unwrap(opOperand)->getOwner());
1014 }
1015 
1016 MlirValue mlirOpOperandGetValue(MlirOpOperand opOperand) {
1017  return wrap(unwrap(opOperand)->get());
1018 }
1019 
1020 unsigned mlirOpOperandGetOperandNumber(MlirOpOperand opOperand) {
1021  return unwrap(opOperand)->getOperandNumber();
1022 }
1023 
1024 MlirOpOperand mlirOpOperandGetNextUse(MlirOpOperand opOperand) {
1025  if (mlirOpOperandIsNull(opOperand))
1026  return {};
1027 
1028  OpOperand *nextOpOperand = static_cast<OpOperand *>(
1029  unwrap(opOperand)->getNextOperandUsingThisValue());
1030 
1031  if (!nextOpOperand)
1032  return {};
1033 
1034  return wrap(nextOpOperand);
1035 }
1036 
1037 //===----------------------------------------------------------------------===//
1038 // Type API.
1039 //===----------------------------------------------------------------------===//
1040 
1041 MlirType mlirTypeParseGet(MlirContext context, MlirStringRef type) {
1042  return wrap(mlir::parseType(unwrap(type), unwrap(context)));
1043 }
1044 
1045 MlirContext mlirTypeGetContext(MlirType type) {
1046  return wrap(unwrap(type).getContext());
1047 }
1048 
1049 MlirTypeID mlirTypeGetTypeID(MlirType type) {
1050  return wrap(unwrap(type).getTypeID());
1051 }
1052 
1053 MlirDialect mlirTypeGetDialect(MlirType type) {
1054  return wrap(&unwrap(type).getDialect());
1055 }
1056 
1057 bool mlirTypeEqual(MlirType t1, MlirType t2) {
1058  return unwrap(t1) == unwrap(t2);
1059 }
1060 
1061 void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData) {
1062  detail::CallbackOstream stream(callback, userData);
1063  unwrap(type).print(stream);
1064 }
1065 
1066 void mlirTypeDump(MlirType type) { unwrap(type).dump(); }
1067 
1068 //===----------------------------------------------------------------------===//
1069 // Attribute API.
1070 //===----------------------------------------------------------------------===//
1071 
1072 MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr) {
1073  return wrap(mlir::parseAttribute(unwrap(attr), unwrap(context)));
1074 }
1075 
1076 MlirContext mlirAttributeGetContext(MlirAttribute attribute) {
1077  return wrap(unwrap(attribute).getContext());
1078 }
1079 
1080 MlirType mlirAttributeGetType(MlirAttribute attribute) {
1081  Attribute attr = unwrap(attribute);
1082  if (auto typedAttr = llvm::dyn_cast<TypedAttr>(attr))
1083  return wrap(typedAttr.getType());
1084  return wrap(NoneType::get(attr.getContext()));
1085 }
1086 
1087 MlirTypeID mlirAttributeGetTypeID(MlirAttribute attr) {
1088  return wrap(unwrap(attr).getTypeID());
1089 }
1090 
1091 MlirDialect mlirAttributeGetDialect(MlirAttribute attr) {
1092  return wrap(&unwrap(attr).getDialect());
1093 }
1094 
1095 bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2) {
1096  return unwrap(a1) == unwrap(a2);
1097 }
1098 
1099 void mlirAttributePrint(MlirAttribute attr, MlirStringCallback callback,
1100  void *userData) {
1101  detail::CallbackOstream stream(callback, userData);
1102  unwrap(attr).print(stream);
1103 }
1104 
1105 void mlirAttributeDump(MlirAttribute attr) { unwrap(attr).dump(); }
1106 
1108  MlirAttribute attr) {
1109  return MlirNamedAttribute{name, attr};
1110 }
1111 
1112 //===----------------------------------------------------------------------===//
1113 // Identifier API.
1114 //===----------------------------------------------------------------------===//
1115 
1116 MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str) {
1117  return wrap(StringAttr::get(unwrap(context), unwrap(str)));
1118 }
1119 
1120 MlirContext mlirIdentifierGetContext(MlirIdentifier ident) {
1121  return wrap(unwrap(ident).getContext());
1122 }
1123 
1124 bool mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other) {
1125  return unwrap(ident) == unwrap(other);
1126 }
1127 
1128 MlirStringRef mlirIdentifierStr(MlirIdentifier ident) {
1129  return wrap(unwrap(ident).strref());
1130 }
1131 
1132 //===----------------------------------------------------------------------===//
1133 // Symbol and SymbolTable API.
1134 //===----------------------------------------------------------------------===//
1135 
1138 }
1139 
1142 }
1143 
1144 MlirSymbolTable mlirSymbolTableCreate(MlirOperation operation) {
1145  if (!unwrap(operation)->hasTrait<OpTrait::SymbolTable>())
1146  return wrap(static_cast<SymbolTable *>(nullptr));
1147  return wrap(new SymbolTable(unwrap(operation)));
1148 }
1149 
1150 void mlirSymbolTableDestroy(MlirSymbolTable symbolTable) {
1151  delete unwrap(symbolTable);
1152 }
1153 
1154 MlirOperation mlirSymbolTableLookup(MlirSymbolTable symbolTable,
1155  MlirStringRef name) {
1156  return wrap(unwrap(symbolTable)->lookup(StringRef(name.data, name.length)));
1157 }
1158 
1159 MlirAttribute mlirSymbolTableInsert(MlirSymbolTable symbolTable,
1160  MlirOperation operation) {
1161  return wrap((Attribute)unwrap(symbolTable)->insert(unwrap(operation)));
1162 }
1163 
1164 void mlirSymbolTableErase(MlirSymbolTable symbolTable,
1165  MlirOperation operation) {
1166  unwrap(symbolTable)->erase(unwrap(operation));
1167 }
1168 
1170  MlirStringRef newSymbol,
1171  MlirOperation from) {
1172  auto *cppFrom = unwrap(from);
1173  auto *context = cppFrom->getContext();
1174  auto oldSymbolAttr = StringAttr::get(context, unwrap(oldSymbol));
1175  auto newSymbolAttr = StringAttr::get(context, unwrap(newSymbol));
1176  return wrap(SymbolTable::replaceAllSymbolUses(oldSymbolAttr, newSymbolAttr,
1177  unwrap(from)));
1178 }
1179 
1180 void mlirSymbolTableWalkSymbolTables(MlirOperation from, bool allSymUsesVisible,
1181  void (*callback)(MlirOperation, bool,
1182  void *userData),
1183  void *userData) {
1184  SymbolTable::walkSymbolTables(unwrap(from), allSymUsesVisible,
1185  [&](Operation *foundOpCpp, bool isVisible) {
1186  callback(wrap(foundOpCpp), isVisible,
1187  userData);
1188  });
1189 }
unsigned mlirOpOperandGetOperandNumber(MlirOpOperand opOperand)
Returns the operand number of an op operand.
Definition: IR.cpp:1020
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:709
MlirAttribute mlirOperationGetDiscardableAttributeByName(MlirOperation op, MlirStringRef name)
Returns a discardable attribute attached to the operation given its name.
Definition: IR.cpp:631
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:1154
MlirRegion mlirRegionCreate()
Creates a new empty region and transfers ownership to the caller.
Definition: IR.cpp:758
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:675
MlirContext mlirModuleGetContext(MlirModule module)
Gets the context that a module was created with.
Definition: IR.cpp:316
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:979
MlirContext mlirLocationGetContext(MlirLocation location)
Gets the context that a location was created with.
Definition: IR.cpp:290
MlirOperation mlirOpOperandGetOwner(MlirOpOperand opOperand)
Returns the owner operation of an op operand.
Definition: IR.cpp:1012
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:550
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:702
MlirIdentifier mlirOperationGetName(MlirOperation op)
Gets the name of the operation as an identifier.
Definition: IR.cpp:523
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:1124
void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags, MlirStringCallback callback, void *userData)
Same as mlirOperationPrint but accepts flags controlling the printing behavior.
Definition: IR.cpp:681
bool mlirValueIsABlockArgument(MlirValue value)
Returns 1 if the value is a block argument, 0 otherwise.
Definition: IR.cpp:939
MlirTypeID mlirTypeGetTypeID(MlirType type)
Gets the type ID of the type.
Definition: IR.cpp:1049
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:1002
MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type, MlirLocation loc)
Appends an argument of the specified type to the block.
Definition: IR.cpp:907
intptr_t mlirOperationGetNumRegions(MlirOperation op)
Returns the number of regions attached to the given operation.
Definition: IR.cpp:535
MlirBlock mlirOperationGetBlock(MlirOperation op)
Gets the block that owns this operation, returning null if the operation is not owned.
Definition: IR.cpp:527
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:1116
void mlirBlockArgumentSetType(MlirValue value, MlirType type)
Sets the type of the block argument to the given type.
Definition: IR.cpp:956
MlirLocation mlirLocationFileLineColGet(MlirContext context, MlirStringRef filename, unsigned line, unsigned col)
Creates an File/Line/Column location owned by the given context.
Definition: IR.cpp:254
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:1107
MlirStringRef mlirSymbolTableGetVisibilityAttributeName()
Returns the name of the attribute used to store symbol visibility.
Definition: IR.cpp:1140
void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n, MlirNamedAttribute const *attributes)
Definition: IR.cpp:383
MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos)
Returns pos-th result of the operation.
Definition: IR.cpp:586
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:1180
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:1099
MlirBlock mlirRegionGetFirstBlock(MlirRegion region)
Gets the first block in the region.
Definition: IR.cpp:764
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:1169
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:265
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:582
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:992
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:781
void mlirOperationDestroy(MlirOperation op)
Takes an operation owned by the caller and destroys it.
Definition: IR.cpp:501
void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation)
Takes an operation owned by the caller and appends it to the block.
Definition: IR.cpp:859
MlirRegion mlirOperationGetFirstRegion(MlirOperation op)
Returns first region attached to the operation.
Definition: IR.cpp:543
MlirAttribute mlirOperationGetInherentAttributeByName(MlirOperation op, MlirStringRef name)
Returns an inherent attribute attached to the operation given its name.
Definition: IR.cpp:604
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:1076
MlirStringRef mlirSymbolTableGetSymbolAttributeName()
Returns the name of the attribute used to store symbol names compatible with symbol tables.
Definition: IR.cpp:1136
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:863
MlirRegion mlirBlockGetParentRegion(MlirBlock block)
Returns the region that contains this block.
Definition: IR.cpp:834
MlirType mlirValueGetType(MlirValue value)
Returns the type of the value.
Definition: IR.cpp:969
void mlirOperationMoveAfter(MlirOperation op, MlirOperation other)
Moves the given operation immediately after the other operation in its parent block.
Definition: IR.cpp:715
void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block)
Takes a block owned by the caller and appends it to the given region.
Definition: IR.cpp:771
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:925
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:636
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:838
void mlirOperationSetSuccessor(MlirOperation op, intptr_t pos, MlirBlock block)
Set pos-th successor of the operation.
Definition: IR.cpp:647
MlirValue mlirOpOperandGetValue(MlirOpOperand opOperand)
Returns the value of an op operand.
Definition: IR.cpp:1016
#define APPEND_ELEMS(type, sizeName, elemName)
Definition: IR.cpp:360
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:559
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:330
static mlir::WalkResult unwrap(MlirWalkResult result)
Definition: IR.cpp:723
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:795
MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos)
Returns pos-th argument of the block.
Definition: IR.cpp:921
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:517
intptr_t mlirBlockArgumentGetArgNumber(MlirValue value)
Returns the position of the value in the argument list of its block.
Definition: IR.cpp:951
void mlirOperationWalk(MlirOperation op, MlirOperationWalkCallback callback, void *userData, MlirWalkOrder walkOrder)
Walks operation op in walkOrder and calls callback on that operation.
Definition: IR.cpp:736
MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos)
Returns pos-th successor of the operation.
Definition: IR.cpp:594
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:884
bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2)
Checks if two attributes are equal.
Definition: IR.cpp:1095
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:505
void mlirOperationSetInherentAttributeByName(MlirOperation op, MlirStringRef name, MlirAttribute attr)
Sets an inherent attribute by name, replacing the existing if it exists.
Definition: IR.cpp:612
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:935
MlirContext mlirIdentifierGetContext(MlirIdentifier ident)
Returns the context associated with this identifier.
Definition: IR.cpp:1120
void mlirBytecodeWriterConfigDestroy(MlirBytecodeWriterConfig config)
Destroys printing flags created with mlirBytecodeWriterConfigCreate.
Definition: IR.cpp:233
void mlirModuleDestroy(MlirModule module)
Takes a module owned by the caller and deletes it.
Definition: IR.cpp:324
MlirModule mlirModuleCreateEmpty(MlirLocation location)
Creates a new, empty module and transfers ownership to the caller.
Definition: IR.cpp:304
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:531
void mlirRegionTakeBody(MlirRegion target, MlirRegion source)
Moves the entire content of the source region to the target region.
Definition: IR.cpp:810
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:916
void mlirValueSetType(MlirValue value, MlirType type)
Set the type of the value.
Definition: IR.cpp:973
intptr_t mlirOperationGetNumSuccessors(MlirOperation op)
Returns the number of successor blocks of the operation.
Definition: IR.cpp:590
MlirDialect mlirAttributeGetDialect(MlirAttribute attr)
Gets the dialect of the attribute.
Definition: IR.cpp:1091
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:294
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:666
void mlirBlockDestroy(MlirBlock block)
Takes a block owned by the caller and destroys it.
Definition: IR.cpp:896
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:775
void mlirOperationSetOperand(MlirOperation op, intptr_t pos, MlirValue newValue)
Sets the pos-th operand of the operation.
Definition: IR.cpp:571
intptr_t mlirBlockGetNumArguments(MlirBlock block)
Returns the number of arguments of the block.
Definition: IR.cpp:903
MlirOperation mlirOpResultGetOwner(MlirValue value)
Returns an operation that produced this value as its result.
Definition: IR.cpp:960
void mlirBlockEraseArgument(MlirBlock block, unsigned index)
Erase the argument at 'index' and remove it from the argument list.
Definition: IR.cpp:912
bool mlirOpOperandIsNull(MlirOpOperand opOperand)
Returns whether the op operand is null.
Definition: IR.cpp:1010
MlirDialect mlirTypeGetDialect(MlirType type)
Gets the dialect a type belongs to.
Definition: IR.cpp:1053
MlirModule mlirModuleCreateParse(MlirContext context, MlirStringRef module)
Parses a module from the string and transfers ownership to the caller.
Definition: IR.cpp:308
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:366
void mlirOperationMoveBefore(MlirOperation op, MlirOperation other)
Moves the given operation immediately before the other operation in its parent block.
Definition: IR.cpp:719
static LogicalResult inferOperationTypes(OperationState &state)
Definition: IR.cpp:396
MlirOperation mlirOperationClone(MlirOperation op)
Creates a deep copy of an operation.
Definition: IR.cpp:497
void mlirValuePrintAsOperand(MlirValue value, MlirAsmState state, MlirStringCallback callback, void *userData)
Prints a value as an operand (i.e., the ValueID).
Definition: IR.cpp:985
MlirBlock mlirBlockArgumentGetOwner(MlirValue value)
Returns the block in which this value is defined as an argument.
Definition: IR.cpp:947
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:567
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:246
MlirModule mlirModuleFromOperation(MlirOperation op)
Views the generic operation as a module.
Definition: IR.cpp:334
MlirLocation mlirOperationGetLocation(MlirOperation op)
Gets the location of the operation.
Definition: IR.cpp:513
bool mlirTypeEqual(MlirType t1, MlirType t2)
Checks if two types are equal.
Definition: IR.cpp:1057
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:661
MlirTypeID mlirAttributeGetTypeID(MlirAttribute attr)
Gets the type id of the attribute.
Definition: IR.cpp:1087
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:1159
intptr_t mlirOperationGetNumDiscardableAttributes(MlirOperation op)
Returns the number of discardable attributes attached to the operation.
Definition: IR.cpp:619
MlirSymbolTable mlirSymbolTableCreate(MlirOperation operation)
Creates a symbol table for the given operation.
Definition: IR.cpp:1144
void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n, MlirRegion const *regions)
Definition: IR.cpp:375
MlirLocation mlirLocationUnknownGet(MlirContext context)
Creates a location with unknown position owned by the given context.
Definition: IR.cpp:282
MlirOperation mlirBlockGetFirstOperation(MlirBlock block)
Returns the first operation in the block.
Definition: IR.cpp:842
MlirType mlirAttributeGetType(MlirAttribute attribute)
Gets the type of this attribute.
Definition: IR.cpp:1080
bool mlirOperationRemoveDiscardableAttributeByName(MlirOperation op, MlirStringRef name)
Removes a discardable attribute by name.
Definition: IR.cpp:642
void mlirRegionDestroy(MlirRegion region)
Takes a region owned by the caller and destroys it.
Definition: IR.cpp:806
bool mlirOperationRemoveAttributeByName(MlirOperation op, MlirStringRef name)
Removes an attribute by name.
Definition: IR.cpp:671
bool mlirValueIsAOpResult(MlirValue value)
Returns 1 if the value is an operation result, 0 otherwise.
Definition: IR.cpp:943
MLIR_CAPI_EXPORTED bool mlirOperationHasInherentAttributeByName(MlirOperation op, MlirStringRef name)
Returns true if this operation defines an inherent attribute with this name.
Definition: IR.cpp:599
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:849
bool mlirLocationEqual(MlirLocation l1, MlirLocation l2)
Checks if two locations are equal.
Definition: IR.cpp:286
MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos)
Returns pos-th region attached to the operation.
Definition: IR.cpp:539
MlirOperation mlirOperationCreate(MlirOperationState *state)
Creates an operation and transfers ownership to the caller.
Definition: IR.cpp:451
void mlirBytecodeWriterConfigDesiredEmitVersion(MlirBytecodeWriterConfig flags, int64_t version)
Sets the version to emit in the writer config.
Definition: IR.cpp:237
MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr)
Parses an attribute. The attribute is owned by the context.
Definition: IR.cpp:1072
void mlirOperationRemoveFromParent(MlirOperation op)
Removes the given operation from its parent block.
Definition: IR.cpp:503
void mlirSymbolTableDestroy(MlirSymbolTable symbolTable)
Destroys the symbol table created with mlirSymbolTableCreate.
Definition: IR.cpp:1150
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:687
bool mlirOperationVerify(MlirOperation op)
Verify the operation and return true if it passes, false if it fails.
Definition: IR.cpp:711
void mlirBlockDetach(MlirBlock block)
Detach a block from the owning region and assume ownership.
Definition: IR.cpp:898
MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos)
Return pos-th attribute of the operation.
Definition: IR.cpp:656
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:261
void mlirOperationWriteBytecode(MlirOperation op, MlirStringCallback callback, void *userData)
Same as mlirOperationPrint but writing the bytecode format.
Definition: IR.cpp:695
void mlirValueDump(MlirValue value)
Prints the value to the standard error stream.
Definition: IR.cpp:977
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:869
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:576
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:1045
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:1061
MlirBlock mlirModuleGetBody(MlirModule module)
Gets the body of the module, i.e. the only block it contains.
Definition: IR.cpp:320
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:760
MlirOperation mlirOperationCreateParse(MlirContext context, MlirStringRef sourceStr, MlirStringRef sourceName)
Parses an operation, giving ownership to the caller.
Definition: IR.cpp:488
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:509
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:964
MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name, MlirLocation childLoc)
Creates a name location owned by the given context.
Definition: IR.cpp:273
bool mlirBlockEqual(MlirBlock block, MlirBlock other)
Checks whether two blocks handles point to the same block.
Definition: IR.cpp:826
void mlirSymbolTableErase(MlirSymbolTable symbolTable, MlirOperation operation)
Removes the given operation from the symbol table and erases it.
Definition: IR.cpp:1164
MlirNamedAttribute mlirOperationGetDiscardableAttribute(MlirOperation op, intptr_t pos)
Return pos-th discardable attribute of the operation.
Definition: IR.cpp:624
void mlirOperationStateEnableResultTypeInference(MlirOperationState *state)
Enables result type inference for the operation under construction.
Definition: IR.cpp:388
void mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n, MlirBlock const *successors)
Definition: IR.cpp:379
MlirBytecodeWriterConfig mlirBytecodeWriterConfigCreate()
Creates new printing flags with defaults, intended for customization.
Definition: IR.cpp:229
void mlirAttributeDump(MlirAttribute attr)
Prints the attribute to the standard error stream.
Definition: IR.cpp:1105
MlirStringRef mlirIdentifierStr(MlirIdentifier ident)
Gets the string value of the identifier.
Definition: IR.cpp:1128
void mlirOpPrintingFlagsSkipRegions(MlirOpPrintingFlags flags)
Skip printing regions.
Definition: IR.cpp:222
void mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n, MlirValue const *operands)
Definition: IR.cpp:371
MlirOperationState mlirOperationStateGet(MlirStringRef name, MlirLocation loc)
Constructs an operation state from a name and a location.
Definition: IR.cpp:342
MlirLocation mlirLocationFromAttribute(MlirAttribute attribute)
Creates a location from a location attribute.
Definition: IR.cpp:250
MlirOperation mlirBlockGetParentOperation(MlirBlock block)
Returns the closest surrounding operation that contains this block.
Definition: IR.cpp:830
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:1024
intptr_t mlirOperationGetNumOperands(MlirOperation op)
Returns the number of operands of the operation.
Definition: IR.cpp:563
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:1041
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:818
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:1066
intptr_t mlirOperationGetNumAttributes(MlirOperation op)
Returns the number of attributes attached to the operation.
Definition: IR.cpp:652
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:31
OpListType::iterator iterator
Definition: Block.h:138
bool empty()
Definition: Block.h:146
Operation & back()
Definition: Block.h:150
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:135
Operation & front()
Definition: Block.h:151
iterator begin()
Definition: Block.h:141
This class contains the configuration used for the bytecode writer.
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:207
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:221
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:764
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: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 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:34
static WalkResult skip()
Definition: Visitors.h:53
static WalkResult advance()
Definition: Visitors.h:52
static WalkResult interrupt()
Definition: Visitors.h:51
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:727
MlirWalkOrder
Traversal order for operation walk.
Definition: IR.h:720
@ MlirWalkPreOrder
Definition: IR.h:721
@ MlirWalkPostOrder
Definition: IR.h:722
MlirWalkResult
Operation walk result.
Definition: IR.h:713
@ MlirWalkResultInterrupt
Definition: IR.h:715
@ MlirWalkResultSkip
Definition: IR.h:716
@ MlirWalkResultAdvance
Definition: IR.h:714
static bool mlirBlockIsNull(MlirBlock block)
Checks whether a block is null.
Definition: IR.h:809
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:511
#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
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition: Utils.cpp:305
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)
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.