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