xref: /llvm-project/mlir/lib/CAPI/Dialect/LLVM.cpp (revision 5656cbca52545e608f6fb8b7c9a778c7c9b4b468)
1 //===- LLVM.cpp - C Interface for LLVM dialect ----------------------------===//
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/Dialect/LLVM.h"
10 #include "mlir-c/IR.h"
11 #include "mlir-c/Support.h"
12 #include "mlir/CAPI/Registration.h"
13 #include "mlir/CAPI/Wrap.h"
14 #include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
15 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
16 #include "mlir/Dialect/LLVMIR/LLVMTypes.h"
17 #include "llvm-c/Core.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/SmallVectorExtras.h"
20 
21 using namespace mlir;
22 using namespace mlir::LLVM;
23 
24 MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(LLVM, llvm, LLVMDialect)
25 
26 MlirType mlirLLVMPointerTypeGet(MlirContext ctx, unsigned addressSpace) {
27   return wrap(LLVMPointerType::get(unwrap(ctx), addressSpace));
28 }
29 
30 bool mlirTypeIsALLVMPointerType(MlirType type) {
31   return isa<LLVM::LLVMPointerType>(unwrap(type));
32 }
33 
34 unsigned mlirLLVMPointerTypeGetAddressSpace(MlirType pointerType) {
35   return cast<LLVM::LLVMPointerType>(unwrap(pointerType)).getAddressSpace();
36 }
37 
38 MlirType mlirLLVMVoidTypeGet(MlirContext ctx) {
39   return wrap(LLVMVoidType::get(unwrap(ctx)));
40 }
41 
42 MlirType mlirLLVMArrayTypeGet(MlirType elementType, unsigned numElements) {
43   return wrap(LLVMArrayType::get(unwrap(elementType), numElements));
44 }
45 
46 MlirType mlirLLVMArrayTypeGetElementType(MlirType type) {
47   return wrap(cast<LLVM::LLVMArrayType>(unwrap(type)).getElementType());
48 }
49 
50 MlirType mlirLLVMFunctionTypeGet(MlirType resultType, intptr_t nArgumentTypes,
51                                  MlirType const *argumentTypes, bool isVarArg) {
52   SmallVector<Type, 2> argumentStorage;
53   return wrap(LLVMFunctionType::get(
54       unwrap(resultType),
55       unwrapList(nArgumentTypes, argumentTypes, argumentStorage), isVarArg));
56 }
57 
58 intptr_t mlirLLVMFunctionTypeGetNumInputs(MlirType type) {
59   return llvm::cast<LLVM::LLVMFunctionType>(unwrap(type)).getNumParams();
60 }
61 
62 MlirType mlirLLVMFunctionTypeGetInput(MlirType type, intptr_t pos) {
63   assert(pos >= 0 && "pos in array must be positive");
64   return wrap(llvm::cast<LLVM::LLVMFunctionType>(unwrap(type))
65                   .getParamType(static_cast<unsigned>(pos)));
66 }
67 
68 bool mlirTypeIsALLVMStructType(MlirType type) {
69   return isa<LLVM::LLVMStructType>(unwrap(type));
70 }
71 
72 bool mlirLLVMStructTypeIsLiteral(MlirType type) {
73   return !cast<LLVM::LLVMStructType>(unwrap(type)).isIdentified();
74 }
75 
76 intptr_t mlirLLVMStructTypeGetNumElementTypes(MlirType type) {
77   return cast<LLVM::LLVMStructType>(unwrap(type)).getBody().size();
78 }
79 
80 MlirType mlirLLVMStructTypeGetElementType(MlirType type, intptr_t position) {
81   return wrap(cast<LLVM::LLVMStructType>(unwrap(type)).getBody()[position]);
82 }
83 
84 bool mlirLLVMStructTypeIsPacked(MlirType type) {
85   return cast<LLVM::LLVMStructType>(unwrap(type)).isPacked();
86 }
87 
88 MlirStringRef mlirLLVMStructTypeGetIdentifier(MlirType type) {
89   return wrap(cast<LLVM::LLVMStructType>(unwrap(type)).getName());
90 }
91 
92 bool mlirLLVMStructTypeIsOpaque(MlirType type) {
93   return cast<LLVM::LLVMStructType>(unwrap(type)).isOpaque();
94 }
95 
96 MlirType mlirLLVMStructTypeLiteralGet(MlirContext ctx, intptr_t nFieldTypes,
97                                       MlirType const *fieldTypes,
98                                       bool isPacked) {
99   SmallVector<Type> fieldStorage;
100   return wrap(LLVMStructType::getLiteral(
101       unwrap(ctx), unwrapList(nFieldTypes, fieldTypes, fieldStorage),
102       isPacked));
103 }
104 
105 MlirType mlirLLVMStructTypeLiteralGetChecked(MlirLocation loc,
106                                              intptr_t nFieldTypes,
107                                              MlirType const *fieldTypes,
108                                              bool isPacked) {
109   SmallVector<Type> fieldStorage;
110   return wrap(LLVMStructType::getLiteralChecked(
111       [loc]() { return emitError(unwrap(loc)); }, unwrap(loc)->getContext(),
112       unwrapList(nFieldTypes, fieldTypes, fieldStorage), isPacked));
113 }
114 
115 MlirType mlirLLVMStructTypeOpaqueGet(MlirContext ctx, MlirStringRef name) {
116   return wrap(LLVMStructType::getOpaque(unwrap(name), unwrap(ctx)));
117 }
118 
119 MlirType mlirLLVMStructTypeIdentifiedGet(MlirContext ctx, MlirStringRef name) {
120   return wrap(LLVMStructType::getIdentified(unwrap(ctx), unwrap(name)));
121 }
122 
123 MlirType mlirLLVMStructTypeIdentifiedNewGet(MlirContext ctx, MlirStringRef name,
124                                             intptr_t nFieldTypes,
125                                             MlirType const *fieldTypes,
126                                             bool isPacked) {
127   SmallVector<Type> fields;
128   return wrap(LLVMStructType::getNewIdentified(
129       unwrap(ctx), unwrap(name), unwrapList(nFieldTypes, fieldTypes, fields),
130       isPacked));
131 }
132 
133 MlirLogicalResult mlirLLVMStructTypeSetBody(MlirType structType,
134                                             intptr_t nFieldTypes,
135                                             MlirType const *fieldTypes,
136                                             bool isPacked) {
137   SmallVector<Type> fields;
138   return wrap(
139       cast<LLVM::LLVMStructType>(unwrap(structType))
140           .setBody(unwrapList(nFieldTypes, fieldTypes, fields), isPacked));
141 }
142 
143 MlirAttribute mlirLLVMDIExpressionElemAttrGet(MlirContext ctx,
144                                               unsigned int opcode,
145                                               intptr_t nArguments,
146                                               uint64_t const *arguments) {
147   auto list = ArrayRef<uint64_t>(arguments, nArguments);
148   return wrap(DIExpressionElemAttr::get(unwrap(ctx), opcode, list));
149 }
150 
151 MlirAttribute mlirLLVMDIExpressionAttrGet(MlirContext ctx, intptr_t nOperations,
152                                           MlirAttribute const *operations) {
153   SmallVector<Attribute> attrStorage;
154   attrStorage.reserve(nOperations);
155 
156   return wrap(DIExpressionAttr::get(
157       unwrap(ctx),
158       llvm::map_to_vector(
159           unwrapList(nOperations, operations, attrStorage),
160           [](Attribute a) { return cast<DIExpressionElemAttr>(a); })));
161 }
162 
163 MlirAttribute mlirLLVMDINullTypeAttrGet(MlirContext ctx) {
164   return wrap(DINullTypeAttr::get(unwrap(ctx)));
165 }
166 
167 MlirAttribute mlirLLVMDIBasicTypeAttrGet(MlirContext ctx, unsigned int tag,
168                                          MlirAttribute name,
169                                          uint64_t sizeInBits,
170                                          MlirLLVMTypeEncoding encoding) {
171 
172   return wrap(DIBasicTypeAttr::get(
173       unwrap(ctx), tag, cast<StringAttr>(unwrap(name)), sizeInBits, encoding));
174 }
175 
176 MlirAttribute mlirLLVMDICompositeTypeAttrGetRecSelf(MlirAttribute recId) {
177   return wrap(
178       DICompositeTypeAttr::getRecSelf(cast<DistinctAttr>(unwrap(recId))));
179 }
180 
181 MlirAttribute mlirLLVMDICompositeTypeAttrGet(
182     MlirContext ctx, MlirAttribute recId, bool isRecSelf, unsigned int tag,
183     MlirAttribute name, MlirAttribute file, uint32_t line, MlirAttribute scope,
184     MlirAttribute baseType, int64_t flags, uint64_t sizeInBits,
185     uint64_t alignInBits, intptr_t nElements, MlirAttribute const *elements,
186     MlirAttribute dataLocation, MlirAttribute rank, MlirAttribute allocated,
187     MlirAttribute associated) {
188   SmallVector<Attribute> elementsStorage;
189   elementsStorage.reserve(nElements);
190 
191   return wrap(DICompositeTypeAttr::get(
192       unwrap(ctx), cast<DistinctAttr>(unwrap(recId)), isRecSelf, tag,
193       cast<StringAttr>(unwrap(name)), cast<DIFileAttr>(unwrap(file)), line,
194       cast<DIScopeAttr>(unwrap(scope)), cast<DITypeAttr>(unwrap(baseType)),
195       DIFlags(flags), sizeInBits, alignInBits,
196       llvm::map_to_vector(unwrapList(nElements, elements, elementsStorage),
197                           [](Attribute a) { return cast<DINodeAttr>(a); }),
198       cast<DIExpressionAttr>(unwrap(dataLocation)),
199       cast<DIExpressionAttr>(unwrap(rank)),
200       cast<DIExpressionAttr>(unwrap(allocated)),
201       cast<DIExpressionAttr>(unwrap(associated))));
202 }
203 
204 MlirAttribute mlirLLVMDIDerivedTypeAttrGet(
205     MlirContext ctx, unsigned int tag, MlirAttribute name,
206     MlirAttribute baseType, uint64_t sizeInBits, uint32_t alignInBits,
207     uint64_t offsetInBits, int64_t dwarfAddressSpace, MlirAttribute extraData) {
208   std::optional<unsigned> addressSpace = std::nullopt;
209   if (dwarfAddressSpace >= 0)
210     addressSpace = (unsigned)dwarfAddressSpace;
211   return wrap(DIDerivedTypeAttr::get(
212       unwrap(ctx), tag, cast<StringAttr>(unwrap(name)),
213       cast<DITypeAttr>(unwrap(baseType)), sizeInBits, alignInBits, offsetInBits,
214       addressSpace, cast<DINodeAttr>(unwrap(extraData))));
215 }
216 
217 MlirAttribute mlirLLVMDIStringTypeAttrGet(
218     MlirContext ctx, unsigned int tag, MlirAttribute name, uint64_t sizeInBits,
219     uint32_t alignInBits, MlirAttribute stringLength,
220     MlirAttribute stringLengthExp, MlirAttribute stringLocationExp,
221     MlirLLVMTypeEncoding encoding) {
222   return wrap(DIStringTypeAttr::get(
223       unwrap(ctx), tag, cast<StringAttr>(unwrap(name)), sizeInBits, alignInBits,
224       cast<DIVariableAttr>(unwrap(stringLength)),
225       cast<DIExpressionAttr>(unwrap(stringLengthExp)),
226       cast<DIExpressionAttr>(unwrap(stringLocationExp)), encoding));
227 }
228 
229 MlirAttribute
230 mlirLLVMDIDerivedTypeAttrGetBaseType(MlirAttribute diDerivedType) {
231   return wrap(cast<DIDerivedTypeAttr>(unwrap(diDerivedType)).getBaseType());
232 }
233 
234 MlirAttribute mlirLLVMCConvAttrGet(MlirContext ctx, MlirLLVMCConv cconv) {
235   return wrap(CConvAttr::get(unwrap(ctx), CConv(cconv)));
236 }
237 
238 MlirAttribute mlirLLVMComdatAttrGet(MlirContext ctx, MlirLLVMComdat comdat) {
239   return wrap(ComdatAttr::get(unwrap(ctx), comdat::Comdat(comdat)));
240 }
241 
242 MlirAttribute mlirLLVMLinkageAttrGet(MlirContext ctx, MlirLLVMLinkage linkage) {
243   return wrap(LinkageAttr::get(unwrap(ctx), linkage::Linkage(linkage)));
244 }
245 
246 MlirAttribute mlirLLVMDIFileAttrGet(MlirContext ctx, MlirAttribute name,
247                                     MlirAttribute directory) {
248   return wrap(DIFileAttr::get(unwrap(ctx), cast<StringAttr>(unwrap(name)),
249                               cast<StringAttr>(unwrap(directory))));
250 }
251 
252 MlirAttribute
253 mlirLLVMDICompileUnitAttrGet(MlirContext ctx, MlirAttribute id,
254                              unsigned int sourceLanguage, MlirAttribute file,
255                              MlirAttribute producer, bool isOptimized,
256                              MlirLLVMDIEmissionKind emissionKind,
257                              MlirLLVMDINameTableKind nameTableKind) {
258   return wrap(DICompileUnitAttr::get(
259       unwrap(ctx), cast<DistinctAttr>(unwrap(id)), sourceLanguage,
260       cast<DIFileAttr>(unwrap(file)), cast<StringAttr>(unwrap(producer)),
261       isOptimized, DIEmissionKind(emissionKind),
262       DINameTableKind(nameTableKind)));
263 }
264 
265 MlirAttribute mlirLLVMDIFlagsAttrGet(MlirContext ctx, uint64_t value) {
266   return wrap(DIFlagsAttr::get(unwrap(ctx), DIFlags(value)));
267 }
268 
269 MlirAttribute mlirLLVMDILexicalBlockAttrGet(MlirContext ctx,
270                                             MlirAttribute scope,
271                                             MlirAttribute file,
272                                             unsigned int line,
273                                             unsigned int column) {
274   return wrap(
275       DILexicalBlockAttr::get(unwrap(ctx), cast<DIScopeAttr>(unwrap(scope)),
276                               cast<DIFileAttr>(unwrap(file)), line, column));
277 }
278 
279 MlirAttribute mlirLLVMDILexicalBlockFileAttrGet(MlirContext ctx,
280                                                 MlirAttribute scope,
281                                                 MlirAttribute file,
282                                                 unsigned int discriminator) {
283   return wrap(DILexicalBlockFileAttr::get(
284       unwrap(ctx), cast<DIScopeAttr>(unwrap(scope)),
285       cast<DIFileAttr>(unwrap(file)), discriminator));
286 }
287 
288 MlirAttribute mlirLLVMDILocalVariableAttrGet(
289     MlirContext ctx, MlirAttribute scope, MlirAttribute name,
290     MlirAttribute diFile, unsigned int line, unsigned int arg,
291     unsigned int alignInBits, MlirAttribute diType, int64_t flags) {
292   return wrap(DILocalVariableAttr::get(
293       unwrap(ctx), cast<DIScopeAttr>(unwrap(scope)),
294       cast<StringAttr>(unwrap(name)), cast<DIFileAttr>(unwrap(diFile)), line,
295       arg, alignInBits, cast<DITypeAttr>(unwrap(diType)), DIFlags(flags)));
296 }
297 
298 MlirAttribute mlirLLVMDISubroutineTypeAttrGet(MlirContext ctx,
299                                               unsigned int callingConvention,
300                                               intptr_t nTypes,
301                                               MlirAttribute const *types) {
302   SmallVector<Attribute> attrStorage;
303   attrStorage.reserve(nTypes);
304 
305   return wrap(DISubroutineTypeAttr::get(
306       unwrap(ctx), callingConvention,
307       llvm::map_to_vector(unwrapList(nTypes, types, attrStorage),
308                           [](Attribute a) { return cast<DITypeAttr>(a); })));
309 }
310 
311 MlirAttribute mlirLLVMDISubprogramAttrGetRecSelf(MlirAttribute recId) {
312   return wrap(DISubprogramAttr::getRecSelf(cast<DistinctAttr>(unwrap(recId))));
313 }
314 
315 MlirAttribute mlirLLVMDISubprogramAttrGet(
316     MlirContext ctx, MlirAttribute recId, bool isRecSelf, MlirAttribute id,
317     MlirAttribute compileUnit, MlirAttribute scope, MlirAttribute name,
318     MlirAttribute linkageName, MlirAttribute file, unsigned int line,
319     unsigned int scopeLine, uint64_t subprogramFlags, MlirAttribute type,
320     intptr_t nRetainedNodes, MlirAttribute const *retainedNodes,
321     intptr_t nAnnotations, MlirAttribute const *annotations) {
322   SmallVector<Attribute> nodesStorage;
323   nodesStorage.reserve(nRetainedNodes);
324 
325   SmallVector<Attribute> annotationsStorage;
326   annotationsStorage.reserve(nAnnotations);
327 
328   return wrap(DISubprogramAttr::get(
329       unwrap(ctx), cast<DistinctAttr>(unwrap(recId)), isRecSelf,
330       cast<DistinctAttr>(unwrap(id)),
331       cast<DICompileUnitAttr>(unwrap(compileUnit)),
332       cast<DIScopeAttr>(unwrap(scope)), cast<StringAttr>(unwrap(name)),
333       cast<StringAttr>(unwrap(linkageName)), cast<DIFileAttr>(unwrap(file)),
334       line, scopeLine, DISubprogramFlags(subprogramFlags),
335       cast<DISubroutineTypeAttr>(unwrap(type)),
336       llvm::map_to_vector(
337           unwrapList(nRetainedNodes, retainedNodes, nodesStorage),
338           [](Attribute a) { return cast<DINodeAttr>(a); }),
339       llvm::map_to_vector(
340           unwrapList(nAnnotations, annotations, annotationsStorage),
341           [](Attribute a) { return cast<DINodeAttr>(a); })));
342 }
343 
344 MlirAttribute mlirLLVMDISubprogramAttrGetScope(MlirAttribute diSubprogram) {
345   return wrap(cast<DISubprogramAttr>(unwrap(diSubprogram)).getScope());
346 }
347 
348 unsigned int mlirLLVMDISubprogramAttrGetLine(MlirAttribute diSubprogram) {
349   return cast<DISubprogramAttr>(unwrap(diSubprogram)).getLine();
350 }
351 
352 unsigned int mlirLLVMDISubprogramAttrGetScopeLine(MlirAttribute diSubprogram) {
353   return cast<DISubprogramAttr>(unwrap(diSubprogram)).getScopeLine();
354 }
355 
356 MlirAttribute
357 mlirLLVMDISubprogramAttrGetCompileUnit(MlirAttribute diSubprogram) {
358   return wrap(cast<DISubprogramAttr>(unwrap(diSubprogram)).getCompileUnit());
359 }
360 
361 MlirAttribute mlirLLVMDISubprogramAttrGetFile(MlirAttribute diSubprogram) {
362   return wrap(cast<DISubprogramAttr>(unwrap(diSubprogram)).getFile());
363 }
364 
365 MlirAttribute mlirLLVMDISubprogramAttrGetType(MlirAttribute diSubprogram) {
366   return wrap(cast<DISubprogramAttr>(unwrap(diSubprogram)).getType());
367 }
368 
369 MlirAttribute mlirLLVMDIModuleAttrGet(MlirContext ctx, MlirAttribute file,
370                                       MlirAttribute scope, MlirAttribute name,
371                                       MlirAttribute configMacros,
372                                       MlirAttribute includePath,
373                                       MlirAttribute apinotes, unsigned int line,
374                                       bool isDecl) {
375   return wrap(DIModuleAttr::get(
376       unwrap(ctx), cast<DIFileAttr>(unwrap(file)),
377       cast<DIScopeAttr>(unwrap(scope)), cast<StringAttr>(unwrap(name)),
378       cast<StringAttr>(unwrap(configMacros)),
379       cast<StringAttr>(unwrap(includePath)), cast<StringAttr>(unwrap(apinotes)),
380       line, isDecl));
381 }
382 
383 MlirAttribute mlirLLVMDIModuleAttrGetScope(MlirAttribute diModule) {
384   return wrap(cast<DIModuleAttr>(unwrap(diModule)).getScope());
385 }
386 
387 MlirAttribute mlirLLVMDIImportedEntityAttrGet(
388     MlirContext ctx, unsigned int tag, MlirAttribute scope,
389     MlirAttribute entity, MlirAttribute file, unsigned int line,
390     MlirAttribute name, intptr_t nElements, MlirAttribute const *elements) {
391   SmallVector<Attribute> elementsStorage;
392   elementsStorage.reserve(nElements);
393   return wrap(DIImportedEntityAttr::get(
394       unwrap(ctx), tag, cast<DIScopeAttr>(unwrap(scope)),
395       cast<DINodeAttr>(unwrap(entity)), cast<DIFileAttr>(unwrap(file)), line,
396       cast<StringAttr>(unwrap(name)),
397       llvm::map_to_vector(unwrapList(nElements, elements, elementsStorage),
398                           [](Attribute a) { return cast<DINodeAttr>(a); })));
399 }
400 
401 MlirAttribute mlirLLVMDIAnnotationAttrGet(MlirContext ctx, MlirAttribute name,
402                                           MlirAttribute value) {
403   return wrap(DIAnnotationAttr::get(unwrap(ctx), cast<StringAttr>(unwrap(name)),
404                                     cast<StringAttr>(unwrap(value))));
405 }
406