MLIR  19.0.0git
LLVM.cpp
Go to the documentation of this file.
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/CAPI/Registration.h"
13 
14 using namespace mlir;
15 using namespace mlir::LLVM;
16 
18 
19 MlirType mlirLLVMPointerTypeGet(MlirContext ctx, unsigned addressSpace) {
20  return wrap(LLVMPointerType::get(unwrap(ctx), addressSpace));
21 }
22 
23 MlirType mlirLLVMVoidTypeGet(MlirContext ctx) {
24  return wrap(LLVMVoidType::get(unwrap(ctx)));
25 }
26 
27 MlirType mlirLLVMArrayTypeGet(MlirType elementType, unsigned numElements) {
28  return wrap(LLVMArrayType::get(unwrap(elementType), numElements));
29 }
30 
31 MlirType mlirLLVMFunctionTypeGet(MlirType resultType, intptr_t nArgumentTypes,
32  MlirType const *argumentTypes, bool isVarArg) {
33  SmallVector<Type, 2> argumentStorage;
35  unwrap(resultType),
36  unwrapList(nArgumentTypes, argumentTypes, argumentStorage), isVarArg));
37 }
38 
39 bool mlirTypeIsALLVMStructType(MlirType type) {
40  return isa<LLVM::LLVMStructType>(unwrap(type));
41 }
42 
43 bool mlirLLVMStructTypeIsLiteral(MlirType type) {
44  return !cast<LLVM::LLVMStructType>(unwrap(type)).isIdentified();
45 }
46 
47 intptr_t mlirLLVMStructTypeGetNumElementTypes(MlirType type) {
48  return cast<LLVM::LLVMStructType>(unwrap(type)).getBody().size();
49 }
50 
51 MlirType mlirLLVMStructTypeGetElementType(MlirType type, intptr_t position) {
52  return wrap(cast<LLVM::LLVMStructType>(unwrap(type)).getBody()[position]);
53 }
54 
55 bool mlirLLVMStructTypeIsPacked(MlirType type) {
56  return cast<LLVM::LLVMStructType>(unwrap(type)).isPacked();
57 }
58 
60  return wrap(cast<LLVM::LLVMStructType>(unwrap(type)).getName());
61 }
62 
63 bool mlirLLVMStructTypeIsOpaque(MlirType type) {
64  return cast<LLVM::LLVMStructType>(unwrap(type)).isOpaque();
65 }
66 
67 MlirType mlirLLVMStructTypeLiteralGet(MlirContext ctx, intptr_t nFieldTypes,
68  MlirType const *fieldTypes,
69  bool isPacked) {
70  SmallVector<Type> fieldStorage;
72  unwrap(ctx), unwrapList(nFieldTypes, fieldTypes, fieldStorage),
73  isPacked));
74 }
75 
76 MlirType mlirLLVMStructTypeLiteralGetChecked(MlirLocation loc,
77  intptr_t nFieldTypes,
78  MlirType const *fieldTypes,
79  bool isPacked) {
80  SmallVector<Type> fieldStorage;
82  [loc]() { return emitError(unwrap(loc)); }, unwrap(loc)->getContext(),
83  unwrapList(nFieldTypes, fieldTypes, fieldStorage), isPacked));
84 }
85 
86 MlirType mlirLLVMStructTypeOpaqueGet(MlirContext ctx, MlirStringRef name) {
87  return wrap(LLVMStructType::getOpaque(unwrap(name), unwrap(ctx)));
88 }
89 
90 MlirType mlirLLVMStructTypeIdentifiedGet(MlirContext ctx, MlirStringRef name) {
91  return wrap(LLVMStructType::getIdentified(unwrap(ctx), unwrap(name)));
92 }
93 
94 MlirType mlirLLVMStructTypeIdentifiedNewGet(MlirContext ctx, MlirStringRef name,
95  intptr_t nFieldTypes,
96  MlirType const *fieldTypes,
97  bool isPacked) {
98  SmallVector<Type> fields;
100  unwrap(ctx), unwrap(name), unwrapList(nFieldTypes, fieldTypes, fields),
101  isPacked));
102 }
103 
105  intptr_t nFieldTypes,
106  MlirType const *fieldTypes,
107  bool isPacked) {
108  SmallVector<Type> fields;
109  return wrap(
110  cast<LLVM::LLVMStructType>(unwrap(structType))
111  .setBody(unwrapList(nFieldTypes, fieldTypes, fields), isPacked));
112 }
bool mlirLLVMStructTypeIsLiteral(MlirType type)
Returns true if the type is a literal (unnamed) LLVM struct type.
Definition: LLVM.cpp:43
MlirType mlirLLVMFunctionTypeGet(MlirType resultType, intptr_t nArgumentTypes, MlirType const *argumentTypes, bool isVarArg)
Creates an llvm.func type.
Definition: LLVM.cpp:31
intptr_t mlirLLVMStructTypeGetNumElementTypes(MlirType type)
Returns the number of fields in the struct.
Definition: LLVM.cpp:47
MlirLogicalResult mlirLLVMStructTypeSetBody(MlirType structType, intptr_t nFieldTypes, MlirType const *fieldTypes, bool isPacked)
Sets the body of the identified struct if it hasn't been set yet.
Definition: LLVM.cpp:104
bool mlirTypeIsALLVMStructType(MlirType type)
Returns true if the type is an LLVM dialect struct type.
Definition: LLVM.cpp:39
MlirStringRef mlirLLVMStructTypeGetIdentifier(MlirType type)
Returns the identifier of the identified struct.
Definition: LLVM.cpp:59
MlirType mlirLLVMVoidTypeGet(MlirContext ctx)
Creates an llmv.void type.
Definition: LLVM.cpp:23
MlirType mlirLLVMStructTypeIdentifiedNewGet(MlirContext ctx, MlirStringRef name, intptr_t nFieldTypes, MlirType const *fieldTypes, bool isPacked)
Creates an LLVM identified struct type with no body and a name starting with the given prefix.
Definition: LLVM.cpp:94
MlirType mlirLLVMStructTypeGetElementType(MlirType type, intptr_t position)
Returns the positions-th field of the struct.
Definition: LLVM.cpp:51
MlirType mlirLLVMStructTypeOpaqueGet(MlirContext ctx, MlirStringRef name)
Definition: LLVM.cpp:86
MlirType mlirLLVMArrayTypeGet(MlirType elementType, unsigned numElements)
Creates an llvm.array type.
Definition: LLVM.cpp:27
MlirType mlirLLVMStructTypeLiteralGetChecked(MlirLocation loc, intptr_t nFieldTypes, MlirType const *fieldTypes, bool isPacked)
Creates an LLVM literal (unnamed) struct type if possible.
Definition: LLVM.cpp:76
bool mlirLLVMStructTypeIsPacked(MlirType type)
Returns true if the struct is packed.
Definition: LLVM.cpp:55
MlirType mlirLLVMStructTypeIdentifiedGet(MlirContext ctx, MlirStringRef name)
Creates an LLVM identified struct type with no body.
Definition: LLVM.cpp:90
bool mlirLLVMStructTypeIsOpaque(MlirType type)
Returns true is the struct is explicitly opaque (will not have a body) or uninitialized (will eventua...
Definition: LLVM.cpp:63
MlirType mlirLLVMPointerTypeGet(MlirContext ctx, unsigned addressSpace)
Creates an llvm.ptr type.
Definition: LLVM.cpp:19
MlirType mlirLLVMStructTypeLiteralGet(MlirContext ctx, intptr_t nFieldTypes, MlirType const *fieldTypes, bool isPacked)
Creates an LLVM literal (unnamed) struct type.
Definition: LLVM.cpp:67
#define MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Name, Namespace, ClassName)
Definition: Registration.h:36
static llvm::ArrayRef< CppTy > unwrapList(size_t size, CTy *first, llvm::SmallVectorImpl< CppTy > &storage)
Definition: Wrap.h:40
static LLVMStructType getLiteralChecked(function_ref< InFlightDiagnostic()> emitError, MLIRContext *context, ArrayRef< Type > types, bool isPacked=false)
Definition: LLVMTypes.cpp:442
static LLVMStructType getLiteral(MLIRContext *context, ArrayRef< Type > types, bool isPacked=false)
Gets or creates a literal struct with the given body in the provided context.
Definition: LLVMTypes.cpp:436
static LLVMStructType getNewIdentified(MLIRContext *context, StringRef name, ArrayRef< Type > elements, bool isPacked=false)
Gets a new identified struct with the given body.
Definition: LLVMTypes.cpp:419
static LLVMStructType getIdentified(MLIRContext *context, StringRef name)
Gets or creates an identified struct with the given name in the provided context.
Definition: LLVMTypes.cpp:408
static LLVMStructType getOpaque(StringRef name, MLIRContext *context)
Gets or creates an intentionally-opaque identified struct.
Definition: LLVMTypes.cpp:448
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition: Diagnostics.h:19
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition: Diagnostics.h:24
Include the generated interface declarations.
Definition: CallGraph.h:229
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
A logical result value, essentially a boolean with named states.
Definition: Support.h:116
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition: Support.h:73