1 //===-- mlir-c/Dialect/LLVM.h - C API for LLVM --------------------*- C -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM 4 // Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef MLIR_C_DIALECT_LLVM_H 11 #define MLIR_C_DIALECT_LLVM_H 12 13 #include "mlir-c/IR.h" 14 #include "mlir-c/Support.h" 15 16 #ifdef __cplusplus 17 extern "C" { 18 #endif 19 20 MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(LLVM, llvm); 21 22 /// Creates an llvm.ptr type. 23 MLIR_CAPI_EXPORTED MlirType mlirLLVMPointerTypeGet(MlirContext ctx, 24 unsigned addressSpace); 25 26 /// Returns `true` if the type is an LLVM dialect pointer type. 27 MLIR_CAPI_EXPORTED bool mlirTypeIsALLVMPointerType(MlirType type); 28 29 /// Returns address space of llvm.ptr 30 MLIR_CAPI_EXPORTED unsigned 31 mlirLLVMPointerTypeGetAddressSpace(MlirType pointerType); 32 33 /// Creates an llmv.void type. 34 MLIR_CAPI_EXPORTED MlirType mlirLLVMVoidTypeGet(MlirContext ctx); 35 36 /// Creates an llvm.array type. 37 MLIR_CAPI_EXPORTED MlirType mlirLLVMArrayTypeGet(MlirType elementType, 38 unsigned numElements); 39 40 /// Returns the element type of the llvm.array type. 41 MLIR_CAPI_EXPORTED MlirType mlirLLVMArrayTypeGetElementType(MlirType type); 42 43 /// Creates an llvm.func type. 44 MLIR_CAPI_EXPORTED MlirType 45 mlirLLVMFunctionTypeGet(MlirType resultType, intptr_t nArgumentTypes, 46 MlirType const *argumentTypes, bool isVarArg); 47 48 /// Returns the number of input types. 49 MLIR_CAPI_EXPORTED intptr_t mlirLLVMFunctionTypeGetNumInputs(MlirType type); 50 51 /// Returns the pos-th input type. 52 MLIR_CAPI_EXPORTED MlirType mlirLLVMFunctionTypeGetInput(MlirType type, 53 intptr_t pos); 54 55 /// Returns `true` if the type is an LLVM dialect struct type. 56 MLIR_CAPI_EXPORTED bool mlirTypeIsALLVMStructType(MlirType type); 57 58 /// Returns `true` if the type is a literal (unnamed) LLVM struct type. 59 MLIR_CAPI_EXPORTED bool mlirLLVMStructTypeIsLiteral(MlirType type); 60 61 /// Returns the number of fields in the struct. Asserts if the struct is opaque 62 /// or not yet initialized. 63 MLIR_CAPI_EXPORTED intptr_t mlirLLVMStructTypeGetNumElementTypes(MlirType type); 64 65 /// Returns the `positions`-th field of the struct. Asserts if the struct is 66 /// opaque, not yet initialized or if the position is out of range. 67 MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeGetElementType(MlirType type, 68 intptr_t position); 69 70 /// Returns `true` if the struct is packed. 71 MLIR_CAPI_EXPORTED bool mlirLLVMStructTypeIsPacked(MlirType type); 72 73 /// Returns the identifier of the identified struct. Asserts that the struct is 74 /// identified, i.e., not literal. 75 MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMStructTypeGetIdentifier(MlirType type); 76 77 /// Returns `true` is the struct is explicitly opaque (will not have a body) or 78 /// uninitialized (will eventually have a body). 79 MLIR_CAPI_EXPORTED bool mlirLLVMStructTypeIsOpaque(MlirType type); 80 81 /// Creates an LLVM literal (unnamed) struct type. This may assert if the fields 82 /// have types not compatible with the LLVM dialect. For a graceful failure, use 83 /// the checked version. 84 MLIR_CAPI_EXPORTED MlirType 85 mlirLLVMStructTypeLiteralGet(MlirContext ctx, intptr_t nFieldTypes, 86 MlirType const *fieldTypes, bool isPacked); 87 88 /// Creates an LLVM literal (unnamed) struct type if possible. Emits a 89 /// diagnostic at the given location and returns null otherwise. 90 MLIR_CAPI_EXPORTED MlirType 91 mlirLLVMStructTypeLiteralGetChecked(MlirLocation loc, intptr_t nFieldTypes, 92 MlirType const *fieldTypes, bool isPacked); 93 94 /// Creates an LLVM identified struct type with no body. If a struct type with 95 /// this name already exists in the context, returns that type. Use 96 /// mlirLLVMStructTypeIdentifiedNewGet to create a fresh struct type, 97 /// potentially renaming it. The body should be set separatelty by calling 98 /// mlirLLVMStructTypeSetBody, if it isn't set already. 99 MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeIdentifiedGet(MlirContext ctx, 100 MlirStringRef name); 101 102 /// Creates an LLVM identified struct type with no body and a name starting with 103 /// the given prefix. If a struct with the exact name as the given prefix 104 /// already exists, appends an unspecified suffix to the name so that the name 105 /// is unique in context. 106 MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeIdentifiedNewGet( 107 MlirContext ctx, MlirStringRef name, intptr_t nFieldTypes, 108 MlirType const *fieldTypes, bool isPacked); 109 110 MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeOpaqueGet(MlirContext ctx, 111 MlirStringRef name); 112 113 /// Sets the body of the identified struct if it hasn't been set yet. Returns 114 /// whether the operation was successful. 115 MLIR_CAPI_EXPORTED MlirLogicalResult 116 mlirLLVMStructTypeSetBody(MlirType structType, intptr_t nFieldTypes, 117 MlirType const *fieldTypes, bool isPacked); 118 119 enum MlirLLVMCConv { 120 MlirLLVMCConvC = 0, 121 MlirLLVMCConvFast = 8, 122 MlirLLVMCConvCold = 9, 123 MlirLLVMCConvGHC = 10, 124 MlirLLVMCConvHiPE = 11, 125 MlirLLVMCConvAnyReg = 13, 126 MlirLLVMCConvPreserveMost = 14, 127 MlirLLVMCConvPreserveAll = 15, 128 MlirLLVMCConvSwift = 16, 129 MlirLLVMCConvCXX_FAST_TLS = 17, 130 MlirLLVMCConvTail = 18, 131 MlirLLVMCConvCFGuard_Check = 19, 132 MlirLLVMCConvSwiftTail = 20, 133 MlirLLVMCConvX86_StdCall = 64, 134 MlirLLVMCConvX86_FastCall = 65, 135 MlirLLVMCConvARM_APCS = 66, 136 MlirLLVMCConvARM_AAPCS = 67, 137 MlirLLVMCConvARM_AAPCS_VFP = 68, 138 MlirLLVMCConvMSP430_INTR = 69, 139 MlirLLVMCConvX86_ThisCall = 70, 140 MlirLLVMCConvPTX_Kernel = 71, 141 MlirLLVMCConvPTX_Device = 72, 142 MlirLLVMCConvSPIR_FUNC = 75, 143 MlirLLVMCConvSPIR_KERNEL = 76, 144 MlirLLVMCConvIntel_OCL_BI = 77, 145 MlirLLVMCConvX86_64_SysV = 78, 146 MlirLLVMCConvWin64 = 79, 147 MlirLLVMCConvX86_VectorCall = 80, 148 MlirLLVMCConvDUMMY_HHVM = 81, 149 MlirLLVMCConvDUMMY_HHVM_C = 82, 150 MlirLLVMCConvX86_INTR = 83, 151 MlirLLVMCConvAVR_INTR = 84, 152 MlirLLVMCConvAVR_BUILTIN = 86, 153 MlirLLVMCConvAMDGPU_VS = 87, 154 MlirLLVMCConvAMDGPU_GS = 88, 155 MlirLLVMCConvAMDGPU_CS = 90, 156 MlirLLVMCConvAMDGPU_KERNEL = 91, 157 MlirLLVMCConvX86_RegCall = 92, 158 MlirLLVMCConvAMDGPU_HS = 93, 159 MlirLLVMCConvMSP430_BUILTIN = 94, 160 MlirLLVMCConvAMDGPU_LS = 95, 161 MlirLLVMCConvAMDGPU_ES = 96, 162 MlirLLVMCConvAArch64_VectorCall = 97, 163 MlirLLVMCConvAArch64_SVE_VectorCall = 98, 164 MlirLLVMCConvWASM_EmscriptenInvoke = 99, 165 MlirLLVMCConvAMDGPU_Gfx = 100, 166 MlirLLVMCConvM68k_INTR = 101, 167 }; 168 typedef enum MlirLLVMCConv MlirLLVMCConv; 169 170 /// Creates a LLVM CConv attribute. 171 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMCConvAttrGet(MlirContext ctx, 172 MlirLLVMCConv cconv); 173 174 enum MlirLLVMComdat { 175 MlirLLVMComdatAny = 0, 176 MlirLLVMComdatExactMatch = 1, 177 MlirLLVMComdatLargest = 2, 178 MlirLLVMComdatNoDeduplicate = 3, 179 MlirLLVMComdatSameSize = 4, 180 }; 181 typedef enum MlirLLVMComdat MlirLLVMComdat; 182 183 /// Creates a LLVM Comdat attribute. 184 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMComdatAttrGet(MlirContext ctx, 185 MlirLLVMComdat comdat); 186 187 enum MlirLLVMLinkage { 188 MlirLLVMLinkageExternal = 0, 189 MlirLLVMLinkageAvailableExternally = 1, 190 MlirLLVMLinkageLinkonce = 2, 191 MlirLLVMLinkageLinkonceODR = 3, 192 MlirLLVMLinkageWeak = 4, 193 MlirLLVMLinkageWeakODR = 5, 194 MlirLLVMLinkageAppending = 6, 195 MlirLLVMLinkageInternal = 7, 196 MlirLLVMLinkagePrivate = 8, 197 MlirLLVMLinkageExternWeak = 9, 198 MlirLLVMLinkageCommon = 10, 199 }; 200 typedef enum MlirLLVMLinkage MlirLLVMLinkage; 201 202 /// Creates a LLVM Linkage attribute. 203 MLIR_CAPI_EXPORTED MlirAttribute 204 mlirLLVMLinkageAttrGet(MlirContext ctx, MlirLLVMLinkage linkage); 205 206 /// Creates a LLVM DINullType attribute. 207 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDINullTypeAttrGet(MlirContext ctx); 208 209 /// Creates a LLVM DIExpressionElem attribute. 210 MLIR_CAPI_EXPORTED MlirAttribute 211 mlirLLVMDIExpressionElemAttrGet(MlirContext ctx, unsigned int opcode, 212 intptr_t nArguments, uint64_t const *arguments); 213 214 /// Creates a LLVM DIExpression attribute. 215 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIExpressionAttrGet( 216 MlirContext ctx, intptr_t nOperations, MlirAttribute const *operations); 217 218 enum MlirLLVMTypeEncoding { 219 MlirLLVMTypeEncodingAddress = 0x1, 220 MlirLLVMTypeEncodingBoolean = 0x2, 221 MlirLLVMTypeEncodingComplexFloat = 0x31, 222 MlirLLVMTypeEncodingFloatT = 0x4, 223 MlirLLVMTypeEncodingSigned = 0x5, 224 MlirLLVMTypeEncodingSignedChar = 0x6, 225 MlirLLVMTypeEncodingUnsigned = 0x7, 226 MlirLLVMTypeEncodingUnsignedChar = 0x08, 227 MlirLLVMTypeEncodingImaginaryFloat = 0x09, 228 MlirLLVMTypeEncodingPackedDecimal = 0x0a, 229 MlirLLVMTypeEncodingNumericString = 0x0b, 230 MlirLLVMTypeEncodingEdited = 0x0c, 231 MlirLLVMTypeEncodingSignedFixed = 0x0d, 232 MlirLLVMTypeEncodingUnsignedFixed = 0x0e, 233 MlirLLVMTypeEncodingDecimalFloat = 0x0f, 234 MlirLLVMTypeEncodingUTF = 0x10, 235 MlirLLVMTypeEncodingUCS = 0x11, 236 MlirLLVMTypeEncodingASCII = 0x12, 237 MlirLLVMTypeEncodingLoUser = 0x80, 238 MlirLLVMTypeEncodingHiUser = 0xff, 239 }; 240 typedef enum MlirLLVMTypeEncoding MlirLLVMTypeEncoding; 241 242 /// Creates a LLVM DIBasicType attribute. 243 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIBasicTypeAttrGet( 244 MlirContext ctx, unsigned int tag, MlirAttribute name, uint64_t sizeInBits, 245 MlirLLVMTypeEncoding encoding); 246 247 /// Creates a self-referencing LLVM DICompositeType attribute. 248 MLIR_CAPI_EXPORTED MlirAttribute 249 mlirLLVMDICompositeTypeAttrGetRecSelf(MlirAttribute recId); 250 251 /// Creates a LLVM DICompositeType attribute. 252 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDICompositeTypeAttrGet( 253 MlirContext ctx, MlirAttribute recId, bool isRecSelf, unsigned int tag, 254 MlirAttribute name, MlirAttribute file, uint32_t line, MlirAttribute scope, 255 MlirAttribute baseType, int64_t flags, uint64_t sizeInBits, 256 uint64_t alignInBits, intptr_t nElements, MlirAttribute const *elements, 257 MlirAttribute dataLocation, MlirAttribute rank, MlirAttribute allocated, 258 MlirAttribute associated); 259 260 /// Creates a LLVM DIDerivedType attribute. Note that `dwarfAddressSpace` is an 261 /// optional field, where `MLIR_CAPI_DWARF_ADDRESS_SPACE_NULL` indicates null 262 /// and non-negative values indicate a value present. 263 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIDerivedTypeAttrGet( 264 MlirContext ctx, unsigned int tag, MlirAttribute name, 265 MlirAttribute baseType, uint64_t sizeInBits, uint32_t alignInBits, 266 uint64_t offsetInBits, int64_t dwarfAddressSpace, MlirAttribute extraData); 267 268 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIStringTypeAttrGet( 269 MlirContext ctx, unsigned int tag, MlirAttribute name, uint64_t sizeInBits, 270 uint32_t alignInBits, MlirAttribute stringLength, 271 MlirAttribute stringLengthExp, MlirAttribute stringLocationExp, 272 MlirLLVMTypeEncoding encoding); 273 274 /// Constant to represent std::nullopt for dwarfAddressSpace to omit the field. 275 #define MLIR_CAPI_DWARF_ADDRESS_SPACE_NULL -1 276 277 /// Gets the base type from a LLVM DIDerivedType attribute. 278 MLIR_CAPI_EXPORTED MlirAttribute 279 mlirLLVMDIDerivedTypeAttrGetBaseType(MlirAttribute diDerivedType); 280 281 /// Creates a LLVM DIFileAttr attribute. 282 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIFileAttrGet(MlirContext ctx, 283 MlirAttribute name, 284 MlirAttribute directory); 285 286 enum MlirLLVMDIEmissionKind { 287 MlirLLVMDIEmissionKindNone = 0, 288 MlirLLVMDIEmissionKindFull = 1, 289 MlirLLVMDIEmissionKindLineTablesOnly = 2, 290 MlirLLVMDIEmissionKindDebugDirectivesOnly = 3, 291 }; 292 typedef enum MlirLLVMDIEmissionKind MlirLLVMDIEmissionKind; 293 294 enum MlirLLVMDINameTableKind { 295 MlirLLVMDINameTableKindDefault = 0, 296 MlirLLVMDINameTableKindGNU = 1, 297 MlirLLVMDINameTableKindNone = 2, 298 MlirLLVMDINameTableKindApple = 3, 299 }; 300 typedef enum MlirLLVMDINameTableKind MlirLLVMDINameTableKind; 301 302 /// Creates a LLVM DICompileUnit attribute. 303 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDICompileUnitAttrGet( 304 MlirContext ctx, MlirAttribute id, unsigned int sourceLanguage, 305 MlirAttribute file, MlirAttribute producer, bool isOptimized, 306 MlirLLVMDIEmissionKind emissionKind, MlirLLVMDINameTableKind nameTableKind); 307 308 /// Creates a LLVM DIFlags attribute. 309 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIFlagsAttrGet(MlirContext ctx, 310 uint64_t value); 311 312 /// Creates a LLVM DILexicalBlock attribute. 313 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDILexicalBlockAttrGet( 314 MlirContext ctx, MlirAttribute scope, MlirAttribute file, unsigned int line, 315 unsigned int column); 316 317 /// Creates a LLVM DILexicalBlockFile attribute. 318 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDILexicalBlockFileAttrGet( 319 MlirContext ctx, MlirAttribute scope, MlirAttribute file, 320 unsigned int discriminator); 321 322 /// Creates a LLVM DILocalVariableAttr attribute. 323 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDILocalVariableAttrGet( 324 MlirContext ctx, MlirAttribute scope, MlirAttribute name, 325 MlirAttribute diFile, unsigned int line, unsigned int arg, 326 unsigned int alignInBits, MlirAttribute diType, int64_t flags); 327 328 /// Creates a self-referencing LLVM DISubprogramAttr attribute. 329 MLIR_CAPI_EXPORTED MlirAttribute 330 mlirLLVMDISubprogramAttrGetRecSelf(MlirAttribute recId); 331 332 /// Creates a LLVM DISubprogramAttr attribute. 333 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDISubprogramAttrGet( 334 MlirContext ctx, MlirAttribute recId, bool isRecSelf, MlirAttribute id, 335 MlirAttribute compileUnit, MlirAttribute scope, MlirAttribute name, 336 MlirAttribute linkageName, MlirAttribute file, unsigned int line, 337 unsigned int scopeLine, uint64_t subprogramFlags, MlirAttribute type, 338 intptr_t nRetainedNodes, MlirAttribute const *retainedNodes, 339 intptr_t nAnnotations, MlirAttribute const *annotations); 340 341 /// Creates a LLVM DIAnnotation attribute. 342 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIAnnotationAttrGet( 343 MlirContext ctx, MlirAttribute name, MlirAttribute value); 344 345 /// Gets the scope from this DISubprogramAttr. 346 MLIR_CAPI_EXPORTED MlirAttribute 347 mlirLLVMDISubprogramAttrGetScope(MlirAttribute diSubprogram); 348 349 /// Gets the line from this DISubprogramAttr. 350 MLIR_CAPI_EXPORTED unsigned int 351 mlirLLVMDISubprogramAttrGetLine(MlirAttribute diSubprogram); 352 353 /// Gets the scope line from this DISubprogram. 354 MLIR_CAPI_EXPORTED unsigned int 355 mlirLLVMDISubprogramAttrGetScopeLine(MlirAttribute diSubprogram); 356 357 /// Gets the compile unit from this DISubprogram. 358 MLIR_CAPI_EXPORTED MlirAttribute 359 mlirLLVMDISubprogramAttrGetCompileUnit(MlirAttribute diSubprogram); 360 361 /// Gets the file from this DISubprogramAttr. 362 MLIR_CAPI_EXPORTED MlirAttribute 363 mlirLLVMDISubprogramAttrGetFile(MlirAttribute diSubprogram); 364 365 /// Gets the type from this DISubprogramAttr. 366 MLIR_CAPI_EXPORTED MlirAttribute 367 mlirLLVMDISubprogramAttrGetType(MlirAttribute diSubprogram); 368 369 /// Creates a LLVM DISubroutineTypeAttr attribute. 370 MLIR_CAPI_EXPORTED MlirAttribute 371 mlirLLVMDISubroutineTypeAttrGet(MlirContext ctx, unsigned int callingConvention, 372 intptr_t nTypes, MlirAttribute const *types); 373 374 /// Creates a LLVM DIModuleAttr attribute. 375 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIModuleAttrGet( 376 MlirContext ctx, MlirAttribute file, MlirAttribute scope, 377 MlirAttribute name, MlirAttribute configMacros, MlirAttribute includePath, 378 MlirAttribute apinotes, unsigned int line, bool isDecl); 379 380 /// Creates a LLVM DIImportedEntityAttr attribute. 381 MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIImportedEntityAttrGet( 382 MlirContext ctx, unsigned int tag, MlirAttribute scope, 383 MlirAttribute entity, MlirAttribute file, unsigned int line, 384 MlirAttribute name, intptr_t nElements, MlirAttribute const *elements); 385 386 /// Gets the scope of this DIModuleAttr. 387 MLIR_CAPI_EXPORTED MlirAttribute 388 mlirLLVMDIModuleAttrGetScope(MlirAttribute diModule); 389 390 #ifdef __cplusplus 391 } 392 #endif 393 394 #endif // MLIR_C_DIALECT_LLVM_H 395