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

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of mlir/lib/CAPI/Dialect/LLVM.cpp