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