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