brintos

brintos / llvm-project-archived public Read only

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