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