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