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