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