xref: /llvm-project-15.0.7/mlir/lib/CAPI/IR/IR.cpp (revision ef2cdfe3)
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"
22 #include "mlir/Interfaces/InferTypeOpInterface.h"
23 #include "mlir/Parser.h"
24 
25 #include "llvm/Support/Debug.h"
26 
27 using namespace mlir;
28 
29 //===----------------------------------------------------------------------===//
30 // Context API.
31 //===----------------------------------------------------------------------===//
32 
33 MlirContext mlirContextCreate() {
34   auto *context = new MLIRContext;
35   return wrap(context);
36 }
37 
38 bool mlirContextEqual(MlirContext ctx1, MlirContext ctx2) {
39   return unwrap(ctx1) == unwrap(ctx2);
40 }
41 
42 void mlirContextDestroy(MlirContext context) { delete unwrap(context); }
43 
44 void mlirContextSetAllowUnregisteredDialects(MlirContext context, bool allow) {
45   unwrap(context)->allowUnregisteredDialects(allow);
46 }
47 
48 bool mlirContextGetAllowUnregisteredDialects(MlirContext context) {
49   return unwrap(context)->allowsUnregisteredDialects();
50 }
51 intptr_t mlirContextGetNumRegisteredDialects(MlirContext context) {
52   return static_cast<intptr_t>(unwrap(context)->getAvailableDialects().size());
53 }
54 
55 // TODO: expose a cheaper way than constructing + sorting a vector only to take
56 // its size.
57 intptr_t mlirContextGetNumLoadedDialects(MlirContext context) {
58   return static_cast<intptr_t>(unwrap(context)->getLoadedDialects().size());
59 }
60 
61 MlirDialect mlirContextGetOrLoadDialect(MlirContext context,
62                                         MlirStringRef name) {
63   return wrap(unwrap(context)->getOrLoadDialect(unwrap(name)));
64 }
65 
66 bool mlirContextIsRegisteredOperation(MlirContext context, MlirStringRef name) {
67   return unwrap(context)->isOperationRegistered(unwrap(name));
68 }
69 
70 void mlirContextEnableMultithreading(MlirContext context, bool enable) {
71   return unwrap(context)->enableMultithreading(enable);
72 }
73 
74 //===----------------------------------------------------------------------===//
75 // Dialect API.
76 //===----------------------------------------------------------------------===//
77 
78 MlirContext mlirDialectGetContext(MlirDialect dialect) {
79   return wrap(unwrap(dialect)->getContext());
80 }
81 
82 bool mlirDialectEqual(MlirDialect dialect1, MlirDialect dialect2) {
83   return unwrap(dialect1) == unwrap(dialect2);
84 }
85 
86 MlirStringRef mlirDialectGetNamespace(MlirDialect dialect) {
87   return wrap(unwrap(dialect)->getNamespace());
88 }
89 
90 //===----------------------------------------------------------------------===//
91 // Printing flags API.
92 //===----------------------------------------------------------------------===//
93 
94 MlirOpPrintingFlags mlirOpPrintingFlagsCreate() {
95   return wrap(new OpPrintingFlags());
96 }
97 
98 void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags) {
99   delete unwrap(flags);
100 }
101 
102 void mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags,
103                                                 intptr_t largeElementLimit) {
104   unwrap(flags)->elideLargeElementsAttrs(largeElementLimit);
105 }
106 
107 void mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags,
108                                         bool prettyForm) {
109   unwrap(flags)->enableDebugInfo(/*prettyForm=*/prettyForm);
110 }
111 
112 void mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags) {
113   unwrap(flags)->printGenericOpForm();
114 }
115 
116 void mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags) {
117   unwrap(flags)->useLocalScope();
118 }
119 
120 //===----------------------------------------------------------------------===//
121 // Location API.
122 //===----------------------------------------------------------------------===//
123 
124 MlirLocation mlirLocationFileLineColGet(MlirContext context,
125                                         MlirStringRef filename, unsigned line,
126                                         unsigned col) {
127   return wrap(Location(
128       FileLineColLoc::get(unwrap(context), unwrap(filename), line, col)));
129 }
130 
131 MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller) {
132   return wrap(Location(CallSiteLoc::get(unwrap(callee), unwrap(caller))));
133 }
134 
135 MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name,
136                                  MlirLocation childLoc) {
137   if (mlirLocationIsNull(childLoc))
138     return wrap(
139         Location(NameLoc::get(Identifier::get(unwrap(name), unwrap(context)))));
140   return wrap(Location(NameLoc::get(
141       Identifier::get(unwrap(name), unwrap(context)), unwrap(childLoc))));
142 }
143 
144 MlirLocation mlirLocationUnknownGet(MlirContext context) {
145   return wrap(Location(UnknownLoc::get(unwrap(context))));
146 }
147 
148 bool mlirLocationEqual(MlirLocation l1, MlirLocation l2) {
149   return unwrap(l1) == unwrap(l2);
150 }
151 
152 MlirContext mlirLocationGetContext(MlirLocation location) {
153   return wrap(unwrap(location).getContext());
154 }
155 
156 void mlirLocationPrint(MlirLocation location, MlirStringCallback callback,
157                        void *userData) {
158   detail::CallbackOstream stream(callback, userData);
159   unwrap(location).print(stream);
160 }
161 
162 //===----------------------------------------------------------------------===//
163 // Module API.
164 //===----------------------------------------------------------------------===//
165 
166 MlirModule mlirModuleCreateEmpty(MlirLocation location) {
167   return wrap(ModuleOp::create(unwrap(location)));
168 }
169 
170 MlirModule mlirModuleCreateParse(MlirContext context, MlirStringRef module) {
171   OwningModuleRef owning = parseSourceString(unwrap(module), unwrap(context));
172   if (!owning)
173     return MlirModule{nullptr};
174   return MlirModule{owning.release().getOperation()};
175 }
176 
177 MlirContext mlirModuleGetContext(MlirModule module) {
178   return wrap(unwrap(module).getContext());
179 }
180 
181 MlirBlock mlirModuleGetBody(MlirModule module) {
182   return wrap(unwrap(module).getBody());
183 }
184 
185 void mlirModuleDestroy(MlirModule module) {
186   // Transfer ownership to an OwningModuleRef so that its destructor is called.
187   OwningModuleRef(unwrap(module));
188 }
189 
190 MlirOperation mlirModuleGetOperation(MlirModule module) {
191   return wrap(unwrap(module).getOperation());
192 }
193 
194 MlirModule mlirModuleFromOperation(MlirOperation op) {
195   return wrap(dyn_cast<ModuleOp>(unwrap(op)));
196 }
197 
198 //===----------------------------------------------------------------------===//
199 // Operation state API.
200 //===----------------------------------------------------------------------===//
201 
202 MlirOperationState mlirOperationStateGet(MlirStringRef name, MlirLocation loc) {
203   MlirOperationState state;
204   state.name = name;
205   state.location = loc;
206   state.nResults = 0;
207   state.results = nullptr;
208   state.nOperands = 0;
209   state.operands = nullptr;
210   state.nRegions = 0;
211   state.regions = nullptr;
212   state.nSuccessors = 0;
213   state.successors = nullptr;
214   state.nAttributes = 0;
215   state.attributes = nullptr;
216   state.enableResultTypeInference = false;
217   return state;
218 }
219 
220 #define APPEND_ELEMS(type, sizeName, elemName)                                 \
221   state->elemName =                                                            \
222       (type *)realloc(state->elemName, (state->sizeName + n) * sizeof(type));  \
223   memcpy(state->elemName + state->sizeName, elemName, n * sizeof(type));       \
224   state->sizeName += n;
225 
226 void mlirOperationStateAddResults(MlirOperationState *state, intptr_t n,
227                                   MlirType const *results) {
228   APPEND_ELEMS(MlirType, nResults, results);
229 }
230 
231 void mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n,
232                                    MlirValue const *operands) {
233   APPEND_ELEMS(MlirValue, nOperands, operands);
234 }
235 void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n,
236                                        MlirRegion const *regions) {
237   APPEND_ELEMS(MlirRegion, nRegions, regions);
238 }
239 void mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n,
240                                      MlirBlock const *successors) {
241   APPEND_ELEMS(MlirBlock, nSuccessors, successors);
242 }
243 void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n,
244                                      MlirNamedAttribute const *attributes) {
245   APPEND_ELEMS(MlirNamedAttribute, nAttributes, attributes);
246 }
247 
248 void mlirOperationStateEnableResultTypeInference(MlirOperationState *state) {
249   state->enableResultTypeInference = true;
250 }
251 
252 //===----------------------------------------------------------------------===//
253 // Operation API.
254 //===----------------------------------------------------------------------===//
255 
256 static LogicalResult inferOperationTypes(OperationState &state) {
257   MLIRContext *context = state.getContext();
258   const AbstractOperation *abstractOp =
259       AbstractOperation::lookup(state.name.getStringRef(), context);
260   if (!abstractOp) {
261     emitError(state.location)
262         << "type inference was requested for the operation " << state.name
263         << ", but the operation was not registered. Ensure that the dialect "
264            "containing the operation is linked into MLIR and registered with "
265            "the context";
266     return failure();
267   }
268 
269   // Fallback to inference via an op interface.
270   auto *inferInterface = abstractOp->getInterface<InferTypeOpInterface>();
271   if (!inferInterface) {
272     emitError(state.location)
273         << "type inference was requested for the operation " << state.name
274         << ", but the operation does not support type inference. Result "
275            "types must be specified explicitly.";
276     return failure();
277   }
278 
279   if (succeeded(inferInterface->inferReturnTypes(
280           context, state.location, state.operands,
281           state.attributes.getDictionary(context), state.regions, state.types)))
282     return success();
283 
284   // Diagnostic emitted by interface.
285   return failure();
286 }
287 
288 MlirOperation mlirOperationCreate(MlirOperationState *state) {
289   assert(state);
290   OperationState cppState(unwrap(state->location), unwrap(state->name));
291   SmallVector<Type, 4> resultStorage;
292   SmallVector<Value, 8> operandStorage;
293   SmallVector<Block *, 2> successorStorage;
294   cppState.addTypes(unwrapList(state->nResults, state->results, resultStorage));
295   cppState.addOperands(
296       unwrapList(state->nOperands, state->operands, operandStorage));
297   cppState.addSuccessors(
298       unwrapList(state->nSuccessors, state->successors, successorStorage));
299 
300   cppState.attributes.reserve(state->nAttributes);
301   for (intptr_t i = 0; i < state->nAttributes; ++i)
302     cppState.addAttribute(unwrap(state->attributes[i].name),
303                           unwrap(state->attributes[i].attribute));
304 
305   for (intptr_t i = 0; i < state->nRegions; ++i)
306     cppState.addRegion(std::unique_ptr<Region>(unwrap(state->regions[i])));
307 
308   free(state->results);
309   free(state->operands);
310   free(state->successors);
311   free(state->regions);
312   free(state->attributes);
313 
314   // Infer result types.
315   if (state->enableResultTypeInference) {
316     assert(cppState.types.empty() &&
317            "result type inference enabled and result types provided");
318     if (failed(inferOperationTypes(cppState)))
319       return {nullptr};
320   }
321 
322   MlirOperation result = wrap(Operation::create(cppState));
323   return result;
324 }
325 
326 MlirOperation mlirOperationClone(MlirOperation op) {
327   return wrap(unwrap(op)->clone());
328 }
329 
330 void mlirOperationDestroy(MlirOperation op) { unwrap(op)->erase(); }
331 
332 bool mlirOperationEqual(MlirOperation op, MlirOperation other) {
333   return unwrap(op) == unwrap(other);
334 }
335 
336 MlirContext mlirOperationGetContext(MlirOperation op) {
337   return wrap(unwrap(op)->getContext());
338 }
339 
340 MlirIdentifier mlirOperationGetName(MlirOperation op) {
341   return wrap(unwrap(op)->getName().getIdentifier());
342 }
343 
344 MlirBlock mlirOperationGetBlock(MlirOperation op) {
345   return wrap(unwrap(op)->getBlock());
346 }
347 
348 MlirOperation mlirOperationGetParentOperation(MlirOperation op) {
349   return wrap(unwrap(op)->getParentOp());
350 }
351 
352 intptr_t mlirOperationGetNumRegions(MlirOperation op) {
353   return static_cast<intptr_t>(unwrap(op)->getNumRegions());
354 }
355 
356 MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos) {
357   return wrap(&unwrap(op)->getRegion(static_cast<unsigned>(pos)));
358 }
359 
360 MlirOperation mlirOperationGetNextInBlock(MlirOperation op) {
361   return wrap(unwrap(op)->getNextNode());
362 }
363 
364 intptr_t mlirOperationGetNumOperands(MlirOperation op) {
365   return static_cast<intptr_t>(unwrap(op)->getNumOperands());
366 }
367 
368 MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos) {
369   return wrap(unwrap(op)->getOperand(static_cast<unsigned>(pos)));
370 }
371 
372 void mlirOperationSetOperand(MlirOperation op, intptr_t pos,
373                              MlirValue newValue) {
374   unwrap(op)->setOperand(static_cast<unsigned>(pos), unwrap(newValue));
375 }
376 
377 intptr_t mlirOperationGetNumResults(MlirOperation op) {
378   return static_cast<intptr_t>(unwrap(op)->getNumResults());
379 }
380 
381 MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos) {
382   return wrap(unwrap(op)->getResult(static_cast<unsigned>(pos)));
383 }
384 
385 intptr_t mlirOperationGetNumSuccessors(MlirOperation op) {
386   return static_cast<intptr_t>(unwrap(op)->getNumSuccessors());
387 }
388 
389 MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos) {
390   return wrap(unwrap(op)->getSuccessor(static_cast<unsigned>(pos)));
391 }
392 
393 intptr_t mlirOperationGetNumAttributes(MlirOperation op) {
394   return static_cast<intptr_t>(unwrap(op)->getAttrs().size());
395 }
396 
397 MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos) {
398   NamedAttribute attr = unwrap(op)->getAttrs()[pos];
399   return MlirNamedAttribute{wrap(attr.first), wrap(attr.second)};
400 }
401 
402 MlirAttribute mlirOperationGetAttributeByName(MlirOperation op,
403                                               MlirStringRef name) {
404   return wrap(unwrap(op)->getAttr(unwrap(name)));
405 }
406 
407 void mlirOperationSetAttributeByName(MlirOperation op, MlirStringRef name,
408                                      MlirAttribute attr) {
409   unwrap(op)->setAttr(unwrap(name), unwrap(attr));
410 }
411 
412 bool mlirOperationRemoveAttributeByName(MlirOperation op, MlirStringRef name) {
413   return !!unwrap(op)->removeAttr(unwrap(name));
414 }
415 
416 void mlirOperationPrint(MlirOperation op, MlirStringCallback callback,
417                         void *userData) {
418   detail::CallbackOstream stream(callback, userData);
419   unwrap(op)->print(stream);
420 }
421 
422 void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags,
423                                  MlirStringCallback callback, void *userData) {
424   detail::CallbackOstream stream(callback, userData);
425   unwrap(op)->print(stream, *unwrap(flags));
426 }
427 
428 void mlirOperationDump(MlirOperation op) { return unwrap(op)->dump(); }
429 
430 bool mlirOperationVerify(MlirOperation op) {
431   return succeeded(verify(unwrap(op)));
432 }
433 
434 //===----------------------------------------------------------------------===//
435 // Region API.
436 //===----------------------------------------------------------------------===//
437 
438 MlirRegion mlirRegionCreate() { return wrap(new Region); }
439 
440 bool mlirRegionEqual(MlirRegion region, MlirRegion other) {
441   return unwrap(region) == unwrap(other);
442 }
443 
444 MlirBlock mlirRegionGetFirstBlock(MlirRegion region) {
445   Region *cppRegion = unwrap(region);
446   if (cppRegion->empty())
447     return wrap(static_cast<Block *>(nullptr));
448   return wrap(&cppRegion->front());
449 }
450 
451 void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block) {
452   unwrap(region)->push_back(unwrap(block));
453 }
454 
455 void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos,
456                                 MlirBlock block) {
457   auto &blockList = unwrap(region)->getBlocks();
458   blockList.insert(std::next(blockList.begin(), pos), unwrap(block));
459 }
460 
461 void mlirRegionInsertOwnedBlockAfter(MlirRegion region, MlirBlock reference,
462                                      MlirBlock block) {
463   Region *cppRegion = unwrap(region);
464   if (mlirBlockIsNull(reference)) {
465     cppRegion->getBlocks().insert(cppRegion->begin(), unwrap(block));
466     return;
467   }
468 
469   assert(unwrap(reference)->getParent() == unwrap(region) &&
470          "expected reference block to belong to the region");
471   cppRegion->getBlocks().insertAfter(Region::iterator(unwrap(reference)),
472                                      unwrap(block));
473 }
474 
475 void mlirRegionInsertOwnedBlockBefore(MlirRegion region, MlirBlock reference,
476                                       MlirBlock block) {
477   if (mlirBlockIsNull(reference))
478     return mlirRegionAppendOwnedBlock(region, block);
479 
480   assert(unwrap(reference)->getParent() == unwrap(region) &&
481          "expected reference block to belong to the region");
482   unwrap(region)->getBlocks().insert(Region::iterator(unwrap(reference)),
483                                      unwrap(block));
484 }
485 
486 void mlirRegionDestroy(MlirRegion region) {
487   delete static_cast<Region *>(region.ptr);
488 }
489 
490 //===----------------------------------------------------------------------===//
491 // Block API.
492 //===----------------------------------------------------------------------===//
493 
494 MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args) {
495   Block *b = new Block;
496   for (intptr_t i = 0; i < nArgs; ++i)
497     b->addArgument(unwrap(args[i]));
498   return wrap(b);
499 }
500 
501 bool mlirBlockEqual(MlirBlock block, MlirBlock other) {
502   return unwrap(block) == unwrap(other);
503 }
504 
505 MlirOperation mlirBlockGetParentOperation(MlirBlock block) {
506   return wrap(unwrap(block)->getParentOp());
507 }
508 
509 MlirRegion mlirBlockGetParentRegion(MlirBlock block) {
510   return wrap(unwrap(block)->getParent());
511 }
512 
513 MlirBlock mlirBlockGetNextInRegion(MlirBlock block) {
514   return wrap(unwrap(block)->getNextNode());
515 }
516 
517 MlirOperation mlirBlockGetFirstOperation(MlirBlock block) {
518   Block *cppBlock = unwrap(block);
519   if (cppBlock->empty())
520     return wrap(static_cast<Operation *>(nullptr));
521   return wrap(&cppBlock->front());
522 }
523 
524 MlirOperation mlirBlockGetTerminator(MlirBlock block) {
525   Block *cppBlock = unwrap(block);
526   if (cppBlock->empty())
527     return wrap(static_cast<Operation *>(nullptr));
528   Operation &back = cppBlock->back();
529   if (!back.hasTrait<OpTrait::IsTerminator>())
530     return wrap(static_cast<Operation *>(nullptr));
531   return wrap(&back);
532 }
533 
534 void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation) {
535   unwrap(block)->push_back(unwrap(operation));
536 }
537 
538 void mlirBlockInsertOwnedOperation(MlirBlock block, intptr_t pos,
539                                    MlirOperation operation) {
540   auto &opList = unwrap(block)->getOperations();
541   opList.insert(std::next(opList.begin(), pos), unwrap(operation));
542 }
543 
544 void mlirBlockInsertOwnedOperationAfter(MlirBlock block,
545                                         MlirOperation reference,
546                                         MlirOperation operation) {
547   Block *cppBlock = unwrap(block);
548   if (mlirOperationIsNull(reference)) {
549     cppBlock->getOperations().insert(cppBlock->begin(), unwrap(operation));
550     return;
551   }
552 
553   assert(unwrap(reference)->getBlock() == unwrap(block) &&
554          "expected reference operation to belong to the block");
555   cppBlock->getOperations().insertAfter(Block::iterator(unwrap(reference)),
556                                         unwrap(operation));
557 }
558 
559 void mlirBlockInsertOwnedOperationBefore(MlirBlock block,
560                                          MlirOperation reference,
561                                          MlirOperation operation) {
562   if (mlirOperationIsNull(reference))
563     return mlirBlockAppendOwnedOperation(block, operation);
564 
565   assert(unwrap(reference)->getBlock() == unwrap(block) &&
566          "expected reference operation to belong to the block");
567   unwrap(block)->getOperations().insert(Block::iterator(unwrap(reference)),
568                                         unwrap(operation));
569 }
570 
571 void mlirBlockDestroy(MlirBlock block) { delete unwrap(block); }
572 
573 intptr_t mlirBlockGetNumArguments(MlirBlock block) {
574   return static_cast<intptr_t>(unwrap(block)->getNumArguments());
575 }
576 
577 MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type) {
578   return wrap(unwrap(block)->addArgument(unwrap(type)));
579 }
580 
581 MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos) {
582   return wrap(unwrap(block)->getArgument(static_cast<unsigned>(pos)));
583 }
584 
585 void mlirBlockPrint(MlirBlock block, MlirStringCallback callback,
586                     void *userData) {
587   detail::CallbackOstream stream(callback, userData);
588   unwrap(block)->print(stream);
589 }
590 
591 //===----------------------------------------------------------------------===//
592 // Value API.
593 //===----------------------------------------------------------------------===//
594 
595 bool mlirValueEqual(MlirValue value1, MlirValue value2) {
596   return unwrap(value1) == unwrap(value2);
597 }
598 
599 bool mlirValueIsABlockArgument(MlirValue value) {
600   return unwrap(value).isa<BlockArgument>();
601 }
602 
603 bool mlirValueIsAOpResult(MlirValue value) {
604   return unwrap(value).isa<OpResult>();
605 }
606 
607 MlirBlock mlirBlockArgumentGetOwner(MlirValue value) {
608   return wrap(unwrap(value).cast<BlockArgument>().getOwner());
609 }
610 
611 intptr_t mlirBlockArgumentGetArgNumber(MlirValue value) {
612   return static_cast<intptr_t>(
613       unwrap(value).cast<BlockArgument>().getArgNumber());
614 }
615 
616 void mlirBlockArgumentSetType(MlirValue value, MlirType type) {
617   unwrap(value).cast<BlockArgument>().setType(unwrap(type));
618 }
619 
620 MlirOperation mlirOpResultGetOwner(MlirValue value) {
621   return wrap(unwrap(value).cast<OpResult>().getOwner());
622 }
623 
624 intptr_t mlirOpResultGetResultNumber(MlirValue value) {
625   return static_cast<intptr_t>(
626       unwrap(value).cast<OpResult>().getResultNumber());
627 }
628 
629 MlirType mlirValueGetType(MlirValue value) {
630   return wrap(unwrap(value).getType());
631 }
632 
633 void mlirValueDump(MlirValue value) { unwrap(value).dump(); }
634 
635 void mlirValuePrint(MlirValue value, MlirStringCallback callback,
636                     void *userData) {
637   detail::CallbackOstream stream(callback, userData);
638   unwrap(value).print(stream);
639 }
640 
641 //===----------------------------------------------------------------------===//
642 // Type API.
643 //===----------------------------------------------------------------------===//
644 
645 MlirType mlirTypeParseGet(MlirContext context, MlirStringRef type) {
646   return wrap(mlir::parseType(unwrap(type), unwrap(context)));
647 }
648 
649 MlirContext mlirTypeGetContext(MlirType type) {
650   return wrap(unwrap(type).getContext());
651 }
652 
653 bool mlirTypeEqual(MlirType t1, MlirType t2) {
654   return unwrap(t1) == unwrap(t2);
655 }
656 
657 void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData) {
658   detail::CallbackOstream stream(callback, userData);
659   unwrap(type).print(stream);
660 }
661 
662 void mlirTypeDump(MlirType type) { unwrap(type).dump(); }
663 
664 //===----------------------------------------------------------------------===//
665 // Attribute API.
666 //===----------------------------------------------------------------------===//
667 
668 MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr) {
669   return wrap(mlir::parseAttribute(unwrap(attr), unwrap(context)));
670 }
671 
672 MlirContext mlirAttributeGetContext(MlirAttribute attribute) {
673   return wrap(unwrap(attribute).getContext());
674 }
675 
676 MlirType mlirAttributeGetType(MlirAttribute attribute) {
677   return wrap(unwrap(attribute).getType());
678 }
679 
680 bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2) {
681   return unwrap(a1) == unwrap(a2);
682 }
683 
684 void mlirAttributePrint(MlirAttribute attr, MlirStringCallback callback,
685                         void *userData) {
686   detail::CallbackOstream stream(callback, userData);
687   unwrap(attr).print(stream);
688 }
689 
690 void mlirAttributeDump(MlirAttribute attr) { unwrap(attr).dump(); }
691 
692 MlirNamedAttribute mlirNamedAttributeGet(MlirIdentifier name,
693                                          MlirAttribute attr) {
694   return MlirNamedAttribute{name, attr};
695 }
696 
697 //===----------------------------------------------------------------------===//
698 // Identifier API.
699 //===----------------------------------------------------------------------===//
700 
701 MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str) {
702   return wrap(Identifier::get(unwrap(str), unwrap(context)));
703 }
704 
705 MlirContext mlirIdentifierGetContext(MlirIdentifier ident) {
706   return wrap(unwrap(ident).getContext());
707 }
708 
709 bool mlirIdentifierEqual(MlirIdentifier ident, MlirIdentifier other) {
710   return unwrap(ident) == unwrap(other);
711 }
712 
713 MlirStringRef mlirIdentifierStr(MlirIdentifier ident) {
714   return wrap(unwrap(ident).strref());
715 }
716