MLIR  17.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"
18 #include "mlir/IR/BuiltinOps.h"
19 #include "mlir/IR/Dialect.h"
20 #include "mlir/IR/Location.h"
21 #include "mlir/IR/Operation.h"
22 #include "mlir/IR/Types.h"
23 #include "mlir/IR/Verifier.h"
25 #include "mlir/Parser/Parser.h"
26 
27 #include <cstddef>
28 #include <optional>
29 
30 using namespace mlir;
31 
32 //===----------------------------------------------------------------------===//
33 // Context API.
34 //===----------------------------------------------------------------------===//
35 
36 MlirContext mlirContextCreate() {
37  auto *context = new MLIRContext;
38  return wrap(context);
39 }
40 
41 bool mlirContextEqual(MlirContext ctx1, MlirContext ctx2) {
42  return unwrap(ctx1) == unwrap(ctx2);
43 }
44 
45 void mlirContextDestroy(MlirContext context) { delete unwrap(context); }
46 
47 void mlirContextSetAllowUnregisteredDialects(MlirContext context, bool allow) {
48  unwrap(context)->allowUnregisteredDialects(allow);
49 }
50 
51 bool mlirContextGetAllowUnregisteredDialects(MlirContext context) {
52  return unwrap(context)->allowsUnregisteredDialects();
53 }
54 intptr_t mlirContextGetNumRegisteredDialects(MlirContext context) {
55  return static_cast<intptr_t>(unwrap(context)->getAvailableDialects().size());
56 }
57 
58 void mlirContextAppendDialectRegistry(MlirContext ctx,
59  MlirDialectRegistry registry) {
60  unwrap(ctx)->appendDialectRegistry(*unwrap(registry));
61 }
62 
63 // TODO: expose a cheaper way than constructing + sorting a vector only to take
64 // its size.
65 intptr_t mlirContextGetNumLoadedDialects(MlirContext context) {
66  return static_cast<intptr_t>(unwrap(context)->getLoadedDialects().size());
67 }
68 
69 MlirDialect mlirContextGetOrLoadDialect(MlirContext context,
70  MlirStringRef name) {
71  return wrap(unwrap(context)->getOrLoadDialect(unwrap(name)));
72 }
73 
74 bool mlirContextIsRegisteredOperation(MlirContext context, MlirStringRef name) {
75  return unwrap(context)->isOperationRegistered(unwrap(name));
76 }
77 
78 void mlirContextEnableMultithreading(MlirContext context, bool enable) {
79  return unwrap(context)->enableMultithreading(enable);
80 }
81 
82 void mlirContextLoadAllAvailableDialects(MlirContext context) {
83  unwrap(context)->loadAllAvailableDialects();
84 }
85 
86 //===----------------------------------------------------------------------===//
87 // Dialect API.
88 //===----------------------------------------------------------------------===//
89 
90 MlirContext mlirDialectGetContext(MlirDialect dialect) {
91  return wrap(unwrap(dialect)->getContext());
92 }
93 
94 bool mlirDialectEqual(MlirDialect dialect1, MlirDialect dialect2) {
95  return unwrap(dialect1) == unwrap(dialect2);
96 }
97 
98 MlirStringRef mlirDialectGetNamespace(MlirDialect dialect) {
99  return wrap(unwrap(dialect)->getNamespace());
100 }
101 
102 //===----------------------------------------------------------------------===//
103 // DialectRegistry API.
104 //===----------------------------------------------------------------------===//
105 
106 MlirDialectRegistry mlirDialectRegistryCreate() {
107  return wrap(new DialectRegistry());
108 }
109 
110 void mlirDialectRegistryDestroy(MlirDialectRegistry registry) {
111  delete unwrap(registry);
112 }
113 
114 //===----------------------------------------------------------------------===//
115 // Printing flags API.
116 //===----------------------------------------------------------------------===//
117 
118 MlirOpPrintingFlags mlirOpPrintingFlagsCreate() {
119  return wrap(new OpPrintingFlags());
120 }
121 
122 void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags) {
123  delete unwrap(flags);
124 }
125 
126 void mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags,
127  intptr_t largeElementLimit) {
128  unwrap(flags)->elideLargeElementsAttrs(largeElementLimit);
129 }
130 
131 void mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, bool enable,
132  bool prettyForm) {
133  unwrap(flags)->enableDebugInfo(enable, /*prettyForm=*/prettyForm);
134 }
135 
136 void mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags) {
137  unwrap(flags)->printGenericOpForm();
138 }
139 
140 void mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags) {
141  unwrap(flags)->useLocalScope();
142 }
143 
144 void mlirOpPrintingFlagsAssumeVerified(MlirOpPrintingFlags flags) {
145  unwrap(flags)->assumeVerified();
146 }
147 
148 //===----------------------------------------------------------------------===//
149 // Location API.
150 //===----------------------------------------------------------------------===//
151 
152 MlirAttribute mlirLocationGetAttribute(MlirLocation location) {
153  return wrap(LocationAttr(unwrap(location)));
154 }
155 
156 MlirLocation mlirLocationFromAttribute(MlirAttribute attribute) {
157  return wrap(Location(unwrap(attribute).cast<LocationAttr>()));
158 }
159 
160 MlirLocation mlirLocationFileLineColGet(MlirContext context,
161  MlirStringRef filename, unsigned line,
162  unsigned col) {
163  return wrap(Location(
164  FileLineColLoc::get(unwrap(context), unwrap(filename), line, col)));
165 }
166 
167 MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller) {
168  return wrap(Location(CallSiteLoc::get(unwrap(callee), unwrap(caller))));
169 }
170 
171 MlirLocation mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations,
172  MlirLocation const *locations,
173  MlirAttribute metadata) {
175  ArrayRef<Location> unwrappedLocs = unwrapList(nLocations, locations, locs);
176  return wrap(FusedLoc::get(unwrappedLocs, unwrap(metadata), unwrap(ctx)));
177 }
178 
179 MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name,
180  MlirLocation childLoc) {
181  if (mlirLocationIsNull(childLoc))
182  return wrap(
183  Location(NameLoc::get(StringAttr::get(unwrap(context), unwrap(name)))));
184  return wrap(Location(NameLoc::get(
185  StringAttr::get(unwrap(context), unwrap(name)), unwrap(childLoc))));
186 }
187 
188 MlirLocation mlirLocationUnknownGet(MlirContext context) {
189  return wrap(Location(UnknownLoc::get(unwrap(context))));
190 }
191 
192 bool mlirLocationEqual(MlirLocation l1, MlirLocation l2) {
193  return unwrap(l1) == unwrap(l2);
194 }
195 
196 MlirContext mlirLocationGetContext(MlirLocation location) {
197  return wrap(unwrap(location).getContext());
198 }
199 
200 void mlirLocationPrint(MlirLocation location, MlirStringCallback callback,
201  void *userData) {
202  detail::CallbackOstream stream(callback, userData);
203  unwrap(location).print(stream);
204 }
205 
206 //===----------------------------------------------------------------------===//
207 // Module API.
208 //===----------------------------------------------------------------------===//
209 
210 MlirModule mlirModuleCreateEmpty(MlirLocation location) {
211  return wrap(ModuleOp::create(unwrap(location)));
212 }
213 
214 MlirModule mlirModuleCreateParse(MlirContext context, MlirStringRef module) {
215  OwningOpRef<ModuleOp> owning =
216  parseSourceString<ModuleOp>(unwrap(module), unwrap(context));
217  if (!owning)
218  return MlirModule{nullptr};
219  return MlirModule{owning.release().getOperation()};
220 }
221 
222 MlirContext mlirModuleGetContext(MlirModule module) {
223  return wrap(unwrap(module).getContext());
224 }
225 
226 MlirBlock mlirModuleGetBody(MlirModule module) {
227  return wrap(unwrap(module).getBody());
228 }
229 
230 void mlirModuleDestroy(MlirModule module) {
231  // Transfer ownership to an OwningOpRef<ModuleOp> so that its destructor is
232  // called.
233  OwningOpRef<ModuleOp>(unwrap(module));
234 }
235 
236 MlirOperation mlirModuleGetOperation(MlirModule module) {
237  return wrap(unwrap(module).getOperation());
238 }
239 
240 MlirModule mlirModuleFromOperation(MlirOperation op) {
241  return wrap(dyn_cast<ModuleOp>(unwrap(op)));
242 }
243 
244 //===----------------------------------------------------------------------===//
245 // Operation state API.
246 //===----------------------------------------------------------------------===//
247 
249  MlirOperationState state;
250  state.name = name;
251  state.location = loc;
252  state.nResults = 0;
253  state.results = nullptr;
254  state.nOperands = 0;
255  state.operands = nullptr;
256  state.nRegions = 0;
257  state.regions = nullptr;
258  state.nSuccessors = 0;
259  state.successors = nullptr;
260  state.nAttributes = 0;
261  state.attributes = nullptr;
262  state.enableResultTypeInference = false;
263  return state;
264 }
265 
266 #define APPEND_ELEMS(type, sizeName, elemName) \
267  state->elemName = \
268  (type *)realloc(state->elemName, (state->sizeName + n) * sizeof(type)); \
269  memcpy(state->elemName + state->sizeName, elemName, n * sizeof(type)); \
270  state->sizeName += n;
271 
273  MlirType const *results) {
274  APPEND_ELEMS(MlirType, nResults, results);
275 }
276 
278  MlirValue const *operands) {
279  APPEND_ELEMS(MlirValue, nOperands, operands);
280 }
282  MlirRegion const *regions) {
283  APPEND_ELEMS(MlirRegion, nRegions, regions);
284 }
286  MlirBlock const *successors) {
287  APPEND_ELEMS(MlirBlock, nSuccessors, successors);
288 }
290  MlirNamedAttribute const *attributes) {
291  APPEND_ELEMS(MlirNamedAttribute, nAttributes, attributes);
292 }
293 
295  state->enableResultTypeInference = true;
296 }
297 
298 //===----------------------------------------------------------------------===//
299 // Operation API.
300 //===----------------------------------------------------------------------===//
301 
303  MLIRContext *context = state.getContext();
304  std::optional<RegisteredOperationName> info = state.name.getRegisteredInfo();
305  if (!info) {
306  emitError(state.location)
307  << "type inference was requested for the operation " << state.name
308  << ", but the operation was not registered. Ensure that the dialect "
309  "containing the operation is linked into MLIR and registered with "
310  "the context";
311  return failure();
312  }
313 
314  // Fallback to inference via an op interface.
315  auto *inferInterface = info->getInterface<InferTypeOpInterface>();
316  if (!inferInterface) {
317  emitError(state.location)
318  << "type inference was requested for the operation " << state.name
319  << ", but the operation does not support type inference. Result "
320  "types must be specified explicitly.";
321  return failure();
322  }
323 
324  if (succeeded(inferInterface->inferReturnTypes(
325  context, state.location, state.operands,
326  state.attributes.getDictionary(context), state.regions, state.types)))
327  return success();
328 
329  // Diagnostic emitted by interface.
330  return failure();
331 }
332 
334  assert(state);
335  OperationState cppState(unwrap(state->location), unwrap(state->name));
336  SmallVector<Type, 4> resultStorage;
337  SmallVector<Value, 8> operandStorage;
338  SmallVector<Block *, 2> successorStorage;
339  cppState.addTypes(unwrapList(state->nResults, state->results, resultStorage));
340  cppState.addOperands(
341  unwrapList(state->nOperands, state->operands, operandStorage));
342  cppState.addSuccessors(
343  unwrapList(state->nSuccessors, state->successors, successorStorage));
344 
345  cppState.attributes.reserve(state->nAttributes);
346  for (intptr_t i = 0; i < state->nAttributes; ++i)
347  cppState.addAttribute(unwrap(state->attributes[i].name),
348  unwrap(state->attributes[i].attribute));
349 
350  for (intptr_t i = 0; i < state->nRegions; ++i)
351  cppState.addRegion(std::unique_ptr<Region>(unwrap(state->regions[i])));
352 
353  free(state->results);
354  free(state->operands);
355  free(state->successors);
356  free(state->regions);
357  free(state->attributes);
358 
359  // Infer result types.
360  if (state->enableResultTypeInference) {
361  assert(cppState.types.empty() &&
362  "result type inference enabled and result types provided");
363  if (failed(inferOperationTypes(cppState)))
364  return {nullptr};
365  }
366 
367  MlirOperation result = wrap(Operation::create(cppState));
368  return result;
369 }
370 
371 MlirOperation mlirOperationCreateParse(MlirContext context,
372  MlirStringRef sourceStr,
373  MlirStringRef sourceName) {
374 
375  return wrap(
376  parseSourceString(unwrap(sourceStr), unwrap(context), unwrap(sourceName))
377  .release());
378 }
379 
380 MlirOperation mlirOperationClone(MlirOperation op) {
381  return wrap(unwrap(op)->clone());
382 }
383 
384 void mlirOperationDestroy(MlirOperation op) { unwrap(op)->erase(); }
385 
386 void mlirOperationRemoveFromParent(MlirOperation op) { unwrap(op)->remove(); }
387 
388 bool mlirOperationEqual(MlirOperation op, MlirOperation other) {
389  return unwrap(op) == unwrap(other);
390 }
391 
392 MlirContext mlirOperationGetContext(MlirOperation op) {
393  return wrap(unwrap(op)->getContext());
394 }
395 
396 MlirLocation mlirOperationGetLocation(MlirOperation op) {
397  return wrap(unwrap(op)->getLoc());
398 }
399 
400 MlirTypeID mlirOperationGetTypeID(MlirOperation op) {
401  if (auto info = unwrap(op)->getRegisteredInfo())
402  return wrap(info->getTypeID());
403  return {nullptr};
404 }
405 
406 MlirIdentifier mlirOperationGetName(MlirOperation op) {
407  return wrap(unwrap(op)->getName().getIdentifier());
408 }
409 
410 MlirBlock mlirOperationGetBlock(MlirOperation op) {
411  return wrap(unwrap(op)->getBlock());
412 }
413 
414 MlirOperation mlirOperationGetParentOperation(MlirOperation op) {
415  return wrap(unwrap(op)->getParentOp());
416 }
417 
418 intptr_t mlirOperationGetNumRegions(MlirOperation op) {
419  return static_cast<intptr_t>(unwrap(op)->getNumRegions());
420 }
421 
422 MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos) {
423  return wrap(&unwrap(op)->getRegion(static_cast<unsigned>(pos)));
424 }
425 
426 MlirRegion mlirOperationGetFirstRegion(MlirOperation op) {
427  Operation *cppOp = unwrap(op);
428  if (cppOp->getNumRegions() == 0)
429  return wrap(static_cast<Region *>(nullptr));
430  return wrap(&cppOp->getRegion(0));
431 }
432 
433 MlirRegion mlirRegionGetNextInOperation(MlirRegion region) {
434  Region *cppRegion = unwrap(region);
435  Operation *parent = cppRegion->getParentOp();
436  intptr_t next = cppRegion->getRegionNumber() + 1;
437  if (parent->getNumRegions() > next)
438  return wrap(&parent->getRegion(next));
439  return wrap(static_cast<Region *>(nullptr));
440 }
441 
442 MlirOperation mlirOperationGetNextInBlock(MlirOperation op) {
443  return wrap(unwrap(op)->getNextNode());
444 }
445 
446 intptr_t mlirOperationGetNumOperands(MlirOperation op) {
447  return static_cast<intptr_t>(unwrap(op)->getNumOperands());
448 }
449 
450 MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos) {
451  return wrap(unwrap(op)->getOperand(static_cast<unsigned>(pos)));
452 }
453 
454 void mlirOperationSetOperand(MlirOperation op, intptr_t pos,
455  MlirValue newValue) {
456  unwrap(op)->setOperand(static_cast<unsigned>(pos), unwrap(newValue));
457 }
458 
459 intptr_t mlirOperationGetNumResults(MlirOperation op) {
460  return static_cast<intptr_t>(unwrap(op)->getNumResults());
461 }
462 
463 MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos) {
464  return wrap(unwrap(op)->getResult(static_cast<unsigned>(pos)));
465 }
466 
467 intptr_t mlirOperationGetNumSuccessors(MlirOperation op) {
468  return static_cast<intptr_t>(unwrap(op)->getNumSuccessors());
469 }
470 
471 MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos) {
472  return wrap(unwrap(op)->getSuccessor(static_cast<unsigned>(pos)));
473 }
474 
475 intptr_t mlirOperationGetNumAttributes(MlirOperation op) {
476  return static_cast<intptr_t>(unwrap(op)->getAttrs().size());
477 }
478 
479 MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos) {
480  NamedAttribute attr = unwrap(op)->getAttrs()[pos];
481  return MlirNamedAttribute{wrap(attr.getName()), wrap(attr.getValue())};
482 }
483 
484 MlirAttribute mlirOperationGetAttributeByName(MlirOperation op,
485  MlirStringRef name) {
486  return wrap(unwrap(op)->getAttr(unwrap(name)));
487 }
488 
489 void mlirOperationSetAttributeByName(MlirOperation op, MlirStringRef name,
490  MlirAttribute attr) {
491  unwrap(op)->setAttr(unwrap(name), unwrap(attr));
492 }
493 
494 bool mlirOperationRemoveAttributeByName(MlirOperation op, MlirStringRef name) {
495  return !!unwrap(op)->removeAttr(unwrap(name));
496 }
497 
498 void mlirOperationPrint(MlirOperation op, MlirStringCallback callback,
499  void *userData) {
500  detail::CallbackOstream stream(callback, userData);
501  unwrap(op)->print(stream);
502 }
503 
504 void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags,
505  MlirStringCallback callback, void *userData) {
506  detail::CallbackOstream stream(callback, userData);
507  unwrap(op)->print(stream, *unwrap(flags));
508 }
509 
510 void mlirOperationWriteBytecode(MlirOperation op, MlirStringCallback callback,
511  void *userData) {
512  detail::CallbackOstream stream(callback, userData);
513  writeBytecodeToFile(unwrap(op), stream);
514 }
515 
516 void mlirOperationDump(MlirOperation op) { return unwrap(op)->dump(); }
517 
518 bool mlirOperationVerify(MlirOperation op) {
519  return succeeded(verify(unwrap(op)));
520 }
521 
522 void mlirOperationMoveAfter(MlirOperation op, MlirOperation other) {
523  return unwrap(op)->moveAfter(unwrap(other));
524 }
525 
526 void mlirOperationMoveBefore(MlirOperation op, MlirOperation other) {
527  return unwrap(op)->moveBefore(unwrap(other));
528 }
529 
530 //===----------------------------------------------------------------------===//
531 // Region API.
532 //===----------------------------------------------------------------------===//
533 
534 MlirRegion mlirRegionCreate() { return wrap(new Region); }
535 
536 bool mlirRegionEqual(MlirRegion region, MlirRegion other) {
537  return unwrap(region) == unwrap(other);
538 }
539 
540 MlirBlock mlirRegionGetFirstBlock(MlirRegion region) {
541  Region *cppRegion = unwrap(region);
542  if (cppRegion->empty())
543  return wrap(static_cast<Block *>(nullptr));
544  return wrap(&cppRegion->front());
545 }
546 
547 void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block) {
548  unwrap(region)->push_back(unwrap(block));
549 }
550 
551 void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos,
552  MlirBlock block) {
553  auto &blockList = unwrap(region)->getBlocks();
554  blockList.insert(std::next(blockList.begin(), pos), unwrap(block));
555 }
556 
557 void mlirRegionInsertOwnedBlockAfter(MlirRegion region, MlirBlock reference,
558  MlirBlock block) {
559  Region *cppRegion = unwrap(region);
560  if (mlirBlockIsNull(reference)) {
561  cppRegion->getBlocks().insert(cppRegion->begin(), unwrap(block));
562  return;
563  }
564 
565  assert(unwrap(reference)->getParent() == unwrap(region) &&
566  "expected reference block to belong to the region");
567  cppRegion->getBlocks().insertAfter(Region::iterator(unwrap(reference)),
568  unwrap(block));
569 }
570 
571 void mlirRegionInsertOwnedBlockBefore(MlirRegion region, MlirBlock reference,
572  MlirBlock block) {
573  if (mlirBlockIsNull(reference))
574  return mlirRegionAppendOwnedBlock(region, block);
575 
576  assert(unwrap(reference)->getParent() == unwrap(region) &&
577  "expected reference block to belong to the region");
578  unwrap(region)->getBlocks().insert(Region::iterator(unwrap(reference)),
579  unwrap(block));
580 }
581 
582 void mlirRegionDestroy(MlirRegion region) {
583  delete static_cast<Region *>(region.ptr);
584 }
585 
586 //===----------------------------------------------------------------------===//
587 // Block API.
588 //===----------------------------------------------------------------------===//
589 
590 MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args,
591  MlirLocation const *locs) {
592  Block *b = new Block;
593  for (intptr_t i = 0; i < nArgs; ++i)
594  b->addArgument(unwrap(args[i]), unwrap(locs[i]));
595  return wrap(b);
596 }
597 
598 bool mlirBlockEqual(MlirBlock block, MlirBlock other) {
599  return unwrap(block) == unwrap(other);
600 }
601 
602 MlirOperation mlirBlockGetParentOperation(MlirBlock block) {
603  return wrap(unwrap(block)->getParentOp());
604 }
605 
606 MlirRegion mlirBlockGetParentRegion(MlirBlock block) {
607  return wrap(unwrap(block)->getParent());
608 }
609 
610 MlirBlock mlirBlockGetNextInRegion(MlirBlock block) {
611  return wrap(unwrap(block)->getNextNode());
612 }
613 
614 MlirOperation mlirBlockGetFirstOperation(MlirBlock block) {
615  Block *cppBlock = unwrap(block);
616  if (cppBlock->empty())
617  return wrap(static_cast<Operation *>(nullptr));
618  return wrap(&cppBlock->front());
619 }
620 
621 MlirOperation mlirBlockGetTerminator(MlirBlock block) {
622  Block *cppBlock = unwrap(block);
623  if (cppBlock->empty())
624  return wrap(static_cast<Operation *>(nullptr));
625  Operation &back = cppBlock->back();
626  if (!back.hasTrait<OpTrait::IsTerminator>())
627  return wrap(static_cast<Operation *>(nullptr));
628  return wrap(&back);
629 }
630 
631 void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation) {
632  unwrap(block)->push_back(unwrap(operation));
633 }
634 
635 void mlirBlockInsertOwnedOperation(MlirBlock block, intptr_t pos,
636  MlirOperation operation) {
637  auto &opList = unwrap(block)->getOperations();
638  opList.insert(std::next(opList.begin(), pos), unwrap(operation));
639 }
640 
642  MlirOperation reference,
643  MlirOperation operation) {
644  Block *cppBlock = unwrap(block);
645  if (mlirOperationIsNull(reference)) {
646  cppBlock->getOperations().insert(cppBlock->begin(), unwrap(operation));
647  return;
648  }
649 
650  assert(unwrap(reference)->getBlock() == unwrap(block) &&
651  "expected reference operation to belong to the block");
652  cppBlock->getOperations().insertAfter(Block::iterator(unwrap(reference)),
653  unwrap(operation));
654 }
655 
657  MlirOperation reference,
658  MlirOperation operation) {
659  if (mlirOperationIsNull(reference))
660  return mlirBlockAppendOwnedOperation(block, operation);
661 
662  assert(unwrap(reference)->getBlock() == unwrap(block) &&
663  "expected reference operation to belong to the block");
664  unwrap(block)->getOperations().insert(Block::iterator(unwrap(reference)),
665  unwrap(operation));
666 }
667 
668 void mlirBlockDestroy(MlirBlock block) { delete unwrap(block); }
669 
670 void mlirBlockDetach(MlirBlock block) {
671  Block *b = unwrap(block);
672  b->getParent()->getBlocks().remove(b);
673 }
674 
675 intptr_t mlirBlockGetNumArguments(MlirBlock block) {
676  return static_cast<intptr_t>(unwrap(block)->getNumArguments());
677 }
678 
679 MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type,
680  MlirLocation loc) {
681  return wrap(unwrap(block)->addArgument(unwrap(type), unwrap(loc)));
682 }
683 
684 MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos) {
685  return wrap(unwrap(block)->getArgument(static_cast<unsigned>(pos)));
686 }
687 
688 void mlirBlockPrint(MlirBlock block, MlirStringCallback callback,
689  void *userData) {
690  detail::CallbackOstream stream(callback, userData);
691  unwrap(block)->print(stream);
692 }
693 
694 //===----------------------------------------------------------------------===//
695 // Value API.
696 //===----------------------------------------------------------------------===//
697 
698 bool mlirValueEqual(MlirValue value1, MlirValue value2) {
699  return unwrap(value1) == unwrap(value2);
700 }
701 
702 bool mlirValueIsABlockArgument(MlirValue value) {
703  return unwrap(value).isa<BlockArgument>();
704 }
705 
706 bool mlirValueIsAOpResult(MlirValue value) {
707  return unwrap(value).isa<OpResult>();
708 }
709 
710 MlirBlock mlirBlockArgumentGetOwner(MlirValue value) {
711  return wrap(unwrap(value).cast<BlockArgument>().getOwner());
712 }
713 
714 intptr_t mlirBlockArgumentGetArgNumber(MlirValue value) {
715  return static_cast<intptr_t>(
716  unwrap(value).cast<BlockArgument>().getArgNumber());
717 }
718 
719 void mlirBlockArgumentSetType(MlirValue value, MlirType type) {
720  unwrap(value).cast<BlockArgument>().setType(unwrap(type));
721 }
722 
723 MlirOperation mlirOpResultGetOwner(MlirValue value) {
724  return wrap(unwrap(value).cast<OpResult>().getOwner());
725 }
726 
727 intptr_t mlirOpResultGetResultNumber(MlirValue value) {
728  return static_cast<intptr_t>(
729  unwrap(value).cast<OpResult>().getResultNumber());
730 }
731 
732 MlirType mlirValueGetType(MlirValue value) {
733  return wrap(unwrap(value).getType());
734 }
735 
736 void mlirValueDump(MlirValue value) { unwrap(value).dump(); }
737 
738 void mlirValuePrint(MlirValue value, MlirStringCallback callback,
739  void *userData) {
740  detail::CallbackOstream stream(callback, userData);
741  unwrap(value).print(stream);
742 }
743 
744 MlirOpOperand mlirValueGetFirstUse(MlirValue value) {
745  Value cppValue = unwrap(value);
746  if (cppValue.use_empty())
747  return {};
748 
749  OpOperand *opOperand = cppValue.use_begin().getOperand();
750 
751  return wrap(opOperand);
752 }
753 
754 //===----------------------------------------------------------------------===//
755 // OpOperand API.
756 //===----------------------------------------------------------------------===//
757 
758 bool mlirOpOperandIsNull(MlirOpOperand opOperand) { return !opOperand.ptr; }
759 
760 MlirOperation mlirOpOperandGetOwner(MlirOpOperand opOperand) {
761  return wrap(unwrap(opOperand)->getOwner());
762 }
763 
764 unsigned mlirOpOperandGetOperandNumber(MlirOpOperand opOperand) {
765  return unwrap(opOperand)->getOperandNumber();
766 }
767 
768 MlirOpOperand mlirOpOperandGetNextUse(MlirOpOperand opOperand) {
769  if (mlirOpOperandIsNull(opOperand))
770  return {};
771 
772  OpOperand *nextOpOperand = static_cast<OpOperand *>(
773  unwrap(opOperand)->getNextOperandUsingThisValue());
774 
775  if (!nextOpOperand)
776  return {};
777 
778  return wrap(nextOpOperand);
779 }
780 
781 //===----------------------------------------------------------------------===//
782 // Type API.
783 //===----------------------------------------------------------------------===//
784 
785 MlirType mlirTypeParseGet(MlirContext context, MlirStringRef type) {
786  return wrap(mlir::parseType(unwrap(type), unwrap(context)));
787 }
788 
789 MlirContext mlirTypeGetContext(MlirType type) {
790  return wrap(unwrap(type).getContext());
791 }
792 
793 MlirTypeID mlirTypeGetTypeID(MlirType type) {
794  return wrap(unwrap(type).getTypeID());
795 }
796 
797 bool mlirTypeEqual(MlirType t1, MlirType t2) {
798  return unwrap(t1) == unwrap(t2);
799 }
800 
801 void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData) {
802  detail::CallbackOstream stream(callback, userData);
803  unwrap(type).print(stream);
804 }
805 
806 void mlirTypeDump(MlirType type) { unwrap(type).dump(); }
807 
808 //===----------------------------------------------------------------------===//
809 // Attribute API.
810 //===----------------------------------------------------------------------===//
811 
812 MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr) {
813  return wrap(mlir::parseAttribute(unwrap(attr), unwrap(context)));
814 }
815 
816 MlirContext mlirAttributeGetContext(MlirAttribute attribute) {
817  return wrap(unwrap(attribute).getContext());
818 }
819 
820 MlirType mlirAttributeGetType(MlirAttribute attribute) {
821  Attribute attr = unwrap(attribute);
822  if (auto typedAttr = attr.dyn_cast<TypedAttr>())
823  return wrap(typedAttr.getType());
824  return wrap(NoneType::get(attr.getContext()));
825 }
826 
827 MlirTypeID mlirAttributeGetTypeID(MlirAttribute attr) {
828  return wrap(unwrap(attr).getTypeID());
829 }
830 
831 bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2) {
832  return unwrap(a1) == unwrap(a2);
833 }
834 
835 void mlirAttributePrint(MlirAttribute attr, MlirStringCallback callback,
836  void *userData) {
837  detail::CallbackOstream stream(callback, userData);
838  unwrap(attr).print(stream);
839 }
840 
841 void mlirAttributeDump(MlirAttribute attr) { unwrap(attr).dump(); }
842 
844  MlirAttribute attr) {
845  return MlirNamedAttribute{name, attr};
846 }
847 
848 //===----------------------------------------------------------------------===//
849 // Identifier API.
850 //===----------------------------------------------------------------------===//
851 
852 MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str) {
853  return wrap(StringAttr::get(unwrap(context), unwrap(str)));
854 }
855 
856 MlirContext mlirIdentifierGetContext(MlirIdentifier ident) {
857  return wrap(unwrap(ident).getContext());
858 }
859 
860 bool mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other) {
861  return unwrap(ident) == unwrap(other);
862 }
863 
864 MlirStringRef mlirIdentifierStr(MlirIdentifier ident) {
865  return wrap(unwrap(ident).strref());
866 }
867 
868 //===----------------------------------------------------------------------===//
869 // Symbol and SymbolTable API.
870 //===----------------------------------------------------------------------===//
871 
874 }
875 
878 }
879 
880 MlirSymbolTable mlirSymbolTableCreate(MlirOperation operation) {
881  if (!unwrap(operation)->hasTrait<OpTrait::SymbolTable>())
882  return wrap(static_cast<SymbolTable *>(nullptr));
883  return wrap(new SymbolTable(unwrap(operation)));
884 }
885 
886 void mlirSymbolTableDestroy(MlirSymbolTable symbolTable) {
887  delete unwrap(symbolTable);
888 }
889 
890 MlirOperation mlirSymbolTableLookup(MlirSymbolTable symbolTable,
891  MlirStringRef name) {
892  return wrap(unwrap(symbolTable)->lookup(StringRef(name.data, name.length)));
893 }
894 
895 MlirAttribute mlirSymbolTableInsert(MlirSymbolTable symbolTable,
896  MlirOperation operation) {
897  return wrap((Attribute)unwrap(symbolTable)->insert(unwrap(operation)));
898 }
899 
900 void mlirSymbolTableErase(MlirSymbolTable symbolTable,
901  MlirOperation operation) {
902  unwrap(symbolTable)->erase(unwrap(operation));
903 }
904 
906  MlirStringRef newSymbol,
907  MlirOperation from) {
908  auto *cppFrom = unwrap(from);
909  auto *context = cppFrom->getContext();
910  auto oldSymbolAttr = StringAttr::get(context, unwrap(oldSymbol));
911  auto newSymbolAttr = StringAttr::get(context, unwrap(newSymbol));
912  return wrap(SymbolTable::replaceAllSymbolUses(oldSymbolAttr, newSymbolAttr,
913  unwrap(from)));
914 }
915 
916 void mlirSymbolTableWalkSymbolTables(MlirOperation from, bool allSymUsesVisible,
917  void (*callback)(MlirOperation, bool,
918  void *userData),
919  void *userData) {
920  SymbolTable::walkSymbolTables(unwrap(from), allSymUsesVisible,
921  [&](Operation *foundOpCpp, bool isVisible) {
922  callback(wrap(foundOpCpp), isVisible,
923  userData);
924  });
925 }
unsigned mlirOpOperandGetOperandNumber(MlirOpOperand opOperand)
Returns the operand number of an op operand.
Definition: IR.cpp:764
void mlirContextDestroy(MlirContext context)
Takes an MLIR context owned by the caller and destroys it.
Definition: IR.cpp:45
void mlirOperationDump(MlirOperation op)
Prints an operation to stderr.
Definition: IR.cpp:516
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:890
MlirRegion mlirRegionCreate()
Creates a new empty region and transfers ownership to the caller.
Definition: IR.cpp:534
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:498
MlirContext mlirModuleGetContext(MlirModule module)
Gets the context that a module was created with.
Definition: IR.cpp:222
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:738
MlirContext mlirLocationGetContext(MlirLocation location)
Gets the context that a location was created with.
Definition: IR.cpp:196
MlirOperation mlirOpOperandGetOwner(MlirOpOperand opOperand)
Returns the owner operation of an op operand.
Definition: IR.cpp:760
bool mlirDialectEqual(MlirDialect dialect1, MlirDialect dialect2)
Checks if two dialects that belong to the same context are equal.
Definition: IR.cpp:94
MlirRegion mlirRegionGetNextInOperation(MlirRegion region)
Returns the region immediately following the given region in its parent operation.
Definition: IR.cpp:433
MlirIdentifier mlirOperationGetName(MlirOperation op)
Gets the name of the operation as an identifier.
Definition: IR.cpp:406
void mlirDialectRegistryDestroy(MlirDialectRegistry registry)
Takes a dialect registry owned by the caller and destroys it.
Definition: IR.cpp:110
bool mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other)
Checks whether two identifiers are the same.
Definition: IR.cpp:860
void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags, MlirStringCallback callback, void *userData)
Same as mlirOperationPrint but accepts flags controlling the printing behavior.
Definition: IR.cpp:504
bool mlirValueIsABlockArgument(MlirValue value)
Returns 1 if the value is a block argument, 0 otherwise.
Definition: IR.cpp:702
MlirTypeID mlirTypeGetTypeID(MlirType type)
Gets the type ID of the type.
Definition: IR.cpp:793
MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type, MlirLocation loc)
Appends an argument of the specified type to the block.
Definition: IR.cpp:679
intptr_t mlirOperationGetNumRegions(MlirOperation op)
Returns the number of regions attached to the given operation.
Definition: IR.cpp:418
MlirBlock mlirOperationGetBlock(MlirOperation op)
Gets the block that owns this operation, returning null if the operation is not owned.
Definition: IR.cpp:410
void mlirContextAppendDialectRegistry(MlirContext ctx, MlirDialectRegistry registry)
Append the contents of the given dialect registry to the registry associated with the context.
Definition: IR.cpp:58
MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str)
Gets an identifier with the given string value.
Definition: IR.cpp:852
void mlirBlockArgumentSetType(MlirValue value, MlirType type)
Sets the type of the block argument to the given type.
Definition: IR.cpp:719
MlirLocation mlirLocationFileLineColGet(MlirContext context, MlirStringRef filename, unsigned line, unsigned col)
Creates an File/Line/Column location owned by the given context.
Definition: IR.cpp:160
intptr_t mlirContextGetNumLoadedDialects(MlirContext context)
Returns the number of dialects loaded by the context.
Definition: IR.cpp:65
MlirNamedAttribute mlirNamedAttributeGet(MlirIdentifier name, MlirAttribute attr)
Associates an attribute with the name. Takes ownership of neither.
Definition: IR.cpp:843
MlirStringRef mlirSymbolTableGetVisibilityAttributeName()
Returns the name of the attribute used to store symbol visibility.
Definition: IR.cpp:876
void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n, MlirNamedAttribute const *attributes)
Definition: IR.cpp:289
MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos)
Returns pos-th result of the operation.
Definition: IR.cpp:463
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:916
void mlirContextLoadAllAvailableDialects(MlirContext context)
Eagerly loads all available dialects registered with a context, making them available for use for IR ...
Definition: IR.cpp:82
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:835
MlirBlock mlirRegionGetFirstBlock(MlirRegion region)
Gets the first block in the region.
Definition: IR.cpp:540
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:905
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:171
intptr_t mlirOperationGetNumResults(MlirOperation op)
Returns the number of results of the operation.
Definition: IR.cpp:459
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:744
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:557
void mlirOperationDestroy(MlirOperation op)
Takes an operation owned by the caller and destroys it.
Definition: IR.cpp:384
void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation)
Takes an operation owned by the caller and appends it to the block.
Definition: IR.cpp:631
MlirRegion mlirOperationGetFirstRegion(MlirOperation op)
Returns first region attached to the operation.
Definition: IR.cpp:426
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:69
MlirContext mlirAttributeGetContext(MlirAttribute attribute)
Gets the context that an attribute was created with.
Definition: IR.cpp:816
MlirStringRef mlirSymbolTableGetSymbolAttributeName()
Returns the name of the attribute used to store symbol names compatible with symbol tables.
Definition: IR.cpp:872
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:635
MlirRegion mlirBlockGetParentRegion(MlirBlock block)
Returns the region that contains this block.
Definition: IR.cpp:606
MlirType mlirValueGetType(MlirValue value)
Returns the type of the value.
Definition: IR.cpp:732
void mlirOperationMoveAfter(MlirOperation op, MlirOperation other)
Moves the given operation immediately after the other operation in its parent block.
Definition: IR.cpp:522
void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block)
Takes a block owned by the caller and appends it to the given region.
Definition: IR.cpp:547
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:688
MlirOpPrintingFlags mlirOpPrintingFlagsCreate()
Creates new printing flags with defaults, intended for customization.
Definition: IR.cpp:118
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:126
MlirBlock mlirBlockGetNextInRegion(MlirBlock block)
Returns the block immediately following the given block in its parent region.
Definition: IR.cpp:610
#define APPEND_ELEMS(type, sizeName, elemName)
Definition: IR.cpp:266
bool mlirContextIsRegisteredOperation(MlirContext context, MlirStringRef name)
Returns whether the given fully-qualified operation (i.e.
Definition: IR.cpp:74
MlirOperation mlirOperationGetNextInBlock(MlirOperation op)
Returns an operation immediately following the given operation it its enclosing block.
Definition: IR.cpp:442
void mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, bool enable, bool prettyForm)
Enable or disable printing of debug information (based on enable).
Definition: IR.cpp:131
MlirOperation mlirModuleGetOperation(MlirModule module)
Views the module as a generic operation.
Definition: IR.cpp:236
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:571
MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos)
Returns pos-th argument of the block.
Definition: IR.cpp:684
MlirStringRef mlirDialectGetNamespace(MlirDialect dialect)
Returns the namespace of the given dialect.
Definition: IR.cpp:98
void mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags)
Use local scope when printing the operation.
Definition: IR.cpp:140
MlirTypeID mlirOperationGetTypeID(MlirOperation op)
Gets the type id of the operation.
Definition: IR.cpp:400
intptr_t mlirBlockArgumentGetArgNumber(MlirValue value)
Returns the position of the value in the argument list of its block.
Definition: IR.cpp:714
MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos)
Returns pos-th successor of the operation.
Definition: IR.cpp:471
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:656
bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2)
Checks if two attributes are equal.
Definition: IR.cpp:831
bool mlirOperationEqual(MlirOperation op, MlirOperation other)
Checks whether two operation handles point to the same operation.
Definition: IR.cpp:388
void mlirOpPrintingFlagsAssumeVerified(MlirOpPrintingFlags flags)
Do not verify the operation when using custom operation printers.
Definition: IR.cpp:144
bool mlirValueEqual(MlirValue value1, MlirValue value2)
Returns 1 if two values are equal, 0 otherwise.
Definition: IR.cpp:698
MlirContext mlirIdentifierGetContext(MlirIdentifier ident)
Returns the context associated with this identifier.
Definition: IR.cpp:856
void mlirModuleDestroy(MlirModule module)
Takes a module owned by the caller and deletes it.
Definition: IR.cpp:230
MlirModule mlirModuleCreateEmpty(MlirLocation location)
Creates a new, empty module and transfers ownership to the caller.
Definition: IR.cpp:210
void mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags)
Always print operations in the generic form.
Definition: IR.cpp:136
MlirOperation mlirOperationGetParentOperation(MlirOperation op)
Gets the operation that owns this operation, returning null if the operation is not owned.
Definition: IR.cpp:414
intptr_t mlirOperationGetNumSuccessors(MlirOperation op)
Returns the number of successor blocks of the operation.
Definition: IR.cpp:467
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:200
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:489
void mlirBlockDestroy(MlirBlock block)
Takes a block owned by the caller and destroys it.
Definition: IR.cpp:668
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:551
void mlirOperationSetOperand(MlirOperation op, intptr_t pos, MlirValue newValue)
Sets the pos-th operand of the operation.
Definition: IR.cpp:454
intptr_t mlirBlockGetNumArguments(MlirBlock block)
Returns the number of arguments of the block.
Definition: IR.cpp:675
MlirOperation mlirOpResultGetOwner(MlirValue value)
Returns an operation that produced this value as its result.
Definition: IR.cpp:723
bool mlirOpOperandIsNull(MlirOpOperand opOperand)
Returns whether the op operand is null.
Definition: IR.cpp:758
MlirModule mlirModuleCreateParse(MlirContext context, MlirStringRef module)
Parses a module from the string and transfers ownership to the caller.
Definition: IR.cpp:214
void mlirContextSetAllowUnregisteredDialects(MlirContext context, bool allow)
Sets whether unregistered dialects are allowed in this context.
Definition: IR.cpp:47
void mlirOperationStateAddResults(MlirOperationState *state, intptr_t n, MlirType const *results)
Adds a list of components to the operation state.
Definition: IR.cpp:272
void mlirOperationMoveBefore(MlirOperation op, MlirOperation other)
Moves the given operation immediately before the other operation in its parent block.
Definition: IR.cpp:526
static LogicalResult inferOperationTypes(OperationState &state)
Definition: IR.cpp:302
MlirOperation mlirOperationClone(MlirOperation op)
Creates a deep copy of an operation.
Definition: IR.cpp:380
MlirBlock mlirBlockArgumentGetOwner(MlirValue value)
Returns the block in which this value is defined as an argument.
Definition: IR.cpp:710
MlirDialectRegistry mlirDialectRegistryCreate()
Creates a dialect registry and transfers its ownership to the caller.
Definition: IR.cpp:106
MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos)
Returns pos-th operand of the operation.
Definition: IR.cpp:450
bool mlirContextGetAllowUnregisteredDialects(MlirContext context)
Returns whether the context allows unregistered dialects.
Definition: IR.cpp:51
MlirAttribute mlirLocationGetAttribute(MlirLocation location)
Returns the underlying location attribute of this location.
Definition: IR.cpp:152
MlirModule mlirModuleFromOperation(MlirOperation op)
Views the generic operation as a module.
Definition: IR.cpp:240
MlirLocation mlirOperationGetLocation(MlirOperation op)
Gets the location of the operation.
Definition: IR.cpp:396
bool mlirTypeEqual(MlirType t1, MlirType t2)
Checks if two types are equal.
Definition: IR.cpp:797
bool mlirContextEqual(MlirContext ctx1, MlirContext ctx2)
Checks if two contexts are equal.
Definition: IR.cpp:41
MlirAttribute mlirOperationGetAttributeByName(MlirOperation op, MlirStringRef name)
Returns an attribute attached to the operation given its name.
Definition: IR.cpp:484
MlirTypeID mlirAttributeGetTypeID(MlirAttribute attr)
Gets the type id of the attribute.
Definition: IR.cpp:827
MlirAttribute mlirSymbolTableInsert(MlirSymbolTable symbolTable, MlirOperation operation)
Inserts the given operation into the given symbol table.
Definition: IR.cpp:895
MlirSymbolTable mlirSymbolTableCreate(MlirOperation operation)
Creates a symbol table for the given operation.
Definition: IR.cpp:880
void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n, MlirRegion const *regions)
Definition: IR.cpp:281
MlirLocation mlirLocationUnknownGet(MlirContext context)
Creates a location with unknown position owned by the given context.
Definition: IR.cpp:188
MlirOperation mlirBlockGetFirstOperation(MlirBlock block)
Returns the first operation in the block.
Definition: IR.cpp:614
MlirType mlirAttributeGetType(MlirAttribute attribute)
Gets the type of this attribute.
Definition: IR.cpp:820
void mlirRegionDestroy(MlirRegion region)
Takes a region owned by the caller and destroys it.
Definition: IR.cpp:582
bool mlirOperationRemoveAttributeByName(MlirOperation op, MlirStringRef name)
Removes an attribute by name.
Definition: IR.cpp:494
bool mlirValueIsAOpResult(MlirValue value)
Returns 1 if the value is an operation result, 0 otherwise.
Definition: IR.cpp:706
MlirOperation mlirBlockGetTerminator(MlirBlock block)
Returns the terminator operation in the block or null if no terminator.
Definition: IR.cpp:621
bool mlirLocationEqual(MlirLocation l1, MlirLocation l2)
Checks if two locations are equal.
Definition: IR.cpp:192
MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos)
Returns pos-th region attached to the operation.
Definition: IR.cpp:422
MlirOperation mlirOperationCreate(MlirOperationState *state)
Creates an operation and transfers ownership to the caller.
Definition: IR.cpp:333
MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr)
Parses an attribute. The attribute is owned by the context.
Definition: IR.cpp:812
void mlirOperationRemoveFromParent(MlirOperation op)
Removes the given operation from its parent block.
Definition: IR.cpp:386
void mlirSymbolTableDestroy(MlirSymbolTable symbolTable)
Destroys the symbol table created with mlirSymbolTableCreate.
Definition: IR.cpp:886
MlirContext mlirContextCreate()
Creates an MLIR context and transfers its ownership to the caller.
Definition: IR.cpp:36
bool mlirOperationVerify(MlirOperation op)
Verify the operation and return true if it passes, false if it fails.
Definition: IR.cpp:518
void mlirBlockDetach(MlirBlock block)
Detach a block from the owning region and assume ownership.
Definition: IR.cpp:670
MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos)
Return pos-th attribute of the operation.
Definition: IR.cpp:479
void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags)
Destroys printing flags created with mlirOpPrintingFlagsCreate.
Definition: IR.cpp:122
MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller)
Creates a call site location with a callee and a caller.
Definition: IR.cpp:167
void mlirOperationWriteBytecode(MlirOperation op, MlirStringCallback callback, void *userData)
Same as mlirOperationPrint but writing the bytecode format out.
Definition: IR.cpp:510
void mlirValueDump(MlirValue value)
Prints the value to the standard error stream.
Definition: IR.cpp:736
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:641
intptr_t mlirContextGetNumRegisteredDialects(MlirContext context)
Returns the number of dialects registered with the given context.
Definition: IR.cpp:54
MlirContext mlirTypeGetContext(MlirType type)
Gets the context that a type was created with.
Definition: IR.cpp:789
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:801
MlirBlock mlirModuleGetBody(MlirModule module)
Gets the body of the module, i.e. the only block it contains.
Definition: IR.cpp:226
MlirContext mlirDialectGetContext(MlirDialect dialect)
Returns the context that owns the dialect.
Definition: IR.cpp:90
bool mlirRegionEqual(MlirRegion region, MlirRegion other)
Checks whether two region handles point to the same region.
Definition: IR.cpp:536
MlirOperation mlirOperationCreateParse(MlirContext context, MlirStringRef sourceStr, MlirStringRef sourceName)
Parses an operation, giving ownership to the caller.
Definition: IR.cpp:371
MlirContext mlirOperationGetContext(MlirOperation op)
Gets the context this operation is associated with.
Definition: IR.cpp:392
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:727
MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name, MlirLocation childLoc)
Creates a name location owned by the given context.
Definition: IR.cpp:179
bool mlirBlockEqual(MlirBlock block, MlirBlock other)
Checks whether two blocks handles point to the same block.
Definition: IR.cpp:598
void mlirSymbolTableErase(MlirSymbolTable symbolTable, MlirOperation operation)
Removes the given operation from the symbol table and erases it.
Definition: IR.cpp:900
void mlirOperationStateEnableResultTypeInference(MlirOperationState *state)
Enables result type inference for the operation under construction.
Definition: IR.cpp:294
void mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n, MlirBlock const *successors)
Definition: IR.cpp:285
void mlirAttributeDump(MlirAttribute attr)
Prints the attribute to the standard error stream.
Definition: IR.cpp:841
MlirStringRef mlirIdentifierStr(MlirIdentifier ident)
Gets the string value of the identifier.
Definition: IR.cpp:864
void mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n, MlirValue const *operands)
Definition: IR.cpp:277
MlirOperationState mlirOperationStateGet(MlirStringRef name, MlirLocation loc)
Constructs an operation state from a name and a location.
Definition: IR.cpp:248
MlirLocation mlirLocationFromAttribute(MlirAttribute attribute)
Creates a location from a location attribute.
Definition: IR.cpp:156
MlirOperation mlirBlockGetParentOperation(MlirBlock block)
Returns the closest surrounding operation that contains this block.
Definition: IR.cpp:602
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:768
intptr_t mlirOperationGetNumOperands(MlirOperation op)
Returns the number of operands of the operation.
Definition: IR.cpp:446
MlirType mlirTypeParseGet(MlirContext context, MlirStringRef type)
Parses a type. The type is owned by the context.
Definition: IR.cpp:785
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:590
void mlirContextEnableMultithreading(MlirContext context, bool enable)
Set threading mode (must be set to false to mlir-print-ir-after-all).
Definition: IR.cpp:78
void mlirTypeDump(MlirType type)
Prints the type to the standard error stream.
Definition: IR.cpp:806
intptr_t mlirOperationGetNumAttributes(MlirOperation op)
Returns the number of attributes attached to the operation.
Definition: IR.cpp:475
static llvm::ArrayRef< CppTy > unwrapList(size_t size, CTy *first, llvm::SmallVectorImpl< CppTy > &storage)
Definition: Wrap.h:40
Attributes are known-constant values of operations.
Definition: Attributes.h:25
U dyn_cast() const
Definition: Attributes.h:166
MLIRContext * getContext() const
Return the context this attribute belongs to.
Definition: Attributes.cpp:37
This class represents an argument of a Block.
Definition: Value.h:304
Block represents an ordered list of Operations.
Definition: Block.h:30
OpListType::iterator iterator
Definition: Block.h:129
bool empty()
Definition: Block.h:137
Operation & back()
Definition: Block.h:141
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:141
OpListType & getOperations()
Definition: Block.h:126
Operation & front()
Definition: Block.h:142
iterator begin()
Definition: Block.h:132
void print(raw_ostream &os) const
Outputs this diagnostic to a stream.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
Location objects represent source locations information in MLIR.
Definition: Location.h:31
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
DictionaryAttr getDictionary(MLIRContext *context) const
Return a dictionary attribute for the underlying dictionary.
void reserve(size_type N)
NamedAttribute represents a combination of a name and an Attribute value.
Definition: Attributes.h:189
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:203
This class represents an operand of an operation.
Definition: Value.h:255
Set of flags used to control the behavior of the various IR print methods (e.g.
This is a value defined by a result of an operation.
Definition: Value.h:442
This class provides the API for ops that are known to be terminators.
Definition: OpDefinition.h:703
std::optional< RegisteredOperationName > getRegisteredInfo() const
If this operation is registered, returns the registered information, std::nullopt otherwise.
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:75
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Definition: Operation.h:592
unsigned getNumRegions()
Returns the number of regions held by this operation.
Definition: Operation.h:537
static Operation * create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, NamedAttrList &&attributes, BlockRange successors, unsigned numRegions)
Create a new Operation with the specific fields.
Definition: Operation.cpp:48
Region & getRegion(unsigned index)
Returns the region held by this operation at position 'index'.
Definition: Operation.h:550
OpTy release()
Release the referenced op.
Definition: OwningOpRef.h:59
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:59
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:65
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:261
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:93
bool use_empty() const
Returns true if this value has no uses.
Definition: Value.h:207
use_iterator use_begin() const
Definition: Value.h:197
A simple raw ostream subclass that forwards write_impl calls to the user-supplied callback together w...
Definition: Utils.h:30
static bool mlirBlockIsNull(MlirBlock block)
Checks whether a block is null.
Definition: IR.h:641
static bool mlirLocationIsNull(MlirLocation location)
Checks if the location is null.
Definition: IR.h:263
static bool mlirOperationIsNull(MlirOperation op)
Checks whether the underlying operation is null.
Definition: IR.h:447
void(* MlirStringCallback)(MlirStringRef, void *)
A callback for returning string references.
Definition: Support.h:103
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition: Diagnostics.h:19
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition: Diagnostics.h:24
This header declares functions that assit transformations in the MemRef dialect.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value.
Definition: LogicalResult.h:68
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
Attribute parseAttribute(llvm::StringRef attrStr, MLIRContext *context, Type type={}, size_t *numRead=nullptr, bool isKnownNullTerminated=false)
This parses a single MLIR attribute to an MLIR context if it was valid.
LogicalResult parseSourceString(llvm::StringRef sourceStr, Block *block, const ParserConfig &config, StringRef sourceName="", LocationAttr *sourceFileLoc=nullptr)
This parses the IR string and appends parsed operations to the given block.
Definition: Parser.cpp:90
Operation * clone(OpBuilder &b, Operation *op, TypeRange newResultTypes, ValueRange newOperands)
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 verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...
Definition: Verifier.cpp:374
void writeBytecodeToFile(Operation *op, raw_ostream &os, const BytecodeWriterConfig &config={})
Write the bytecode for the given operation to the provided output stream.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72
A logical result value, essentially a boolean with named states.
Definition: Support.h:114
Named MLIR attribute.
Definition: IR.h:75
MlirAttribute attribute
Definition: IR.h:77
MlirIdentifier name
Definition: IR.h:76
An auxiliary class for constructing operations.
Definition: IR.h:321
MlirBlock * successors
Definition: IR.h:331
intptr_t nAttributes
Definition: IR.h:332
bool enableResultTypeInference
Definition: IR.h:334
MlirLocation location
Definition: IR.h:323
intptr_t nSuccessors
Definition: IR.h:330
MlirStringRef name
Definition: IR.h:322
MlirType * results
Definition: IR.h:325
MlirValue * operands
Definition: IR.h:327
intptr_t nResults
Definition: IR.h:324
intptr_t nOperands
Definition: IR.h:326
MlirNamedAttribute * attributes
Definition: IR.h:333
intptr_t nRegions
Definition: IR.h:328
MlirRegion * regions
Definition: IR.h:329
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition: Support.h:71
const char * data
Pointer to the first symbol.
Definition: Support.h:72
size_t length
Length of the fragment.
Definition: Support.h:73
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
This represents an operation in an abstracted form, suitable for use with the builder APIs.
SmallVector< Value, 4 > operands
void addOperands(ValueRange newOperands)
void addAttribute(StringRef name, Attribute attr)
Add an attribute with the specified name.
void addSuccessors(Block *successor)
void addTypes(ArrayRef< Type > newTypes)
SmallVector< std::unique_ptr< Region >, 1 > regions
Regions that the op will hold.
NamedAttrList attributes
MLIRContext * getContext() const
Get the context held by this operation state.
SmallVector< Type, 4 > types
Types of the results of this operation.
Region * addRegion()
Create a region that should be attached to the operation.