MLIR 23.0.0git
DialectLLVM.cpp
Go to the documentation of this file.
1//===- DialectLLVM.cpp - Pybind module for LLVM dialect API support -------===//
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 <string>
10
11#include "mlir-c/Dialect/LLVM.h"
12#include "mlir-c/IR.h"
13#include "mlir-c/Support.h"
19
20namespace nb = nanobind;
21
22using namespace nanobind::literals;
23using namespace mlir;
25
26namespace mlir {
27namespace python {
29namespace llvm {
30//===--------------------------------------------------------------------===//
31// StructType
32//===--------------------------------------------------------------------===//
33
34struct StructType : PyConcreteType<StructType> {
38 static constexpr const char *pyClassName = "StructType";
40 using Base::Base;
41
42 static void bindDerived(ClassTy &c) {
43 c.def_static(
44 "get_literal",
45 [](const std::vector<PyType> &elements, bool packed,
49 std::vector<MlirType> elements_(elements.size());
50 std::copy(elements.begin(), elements.end(), elements_.begin());
51
53 loc, elements.size(), elements_.data(), packed);
54 if (mlirTypeIsNull(type)) {
55 throw nb::value_error(scope.takeMessage().c_str());
56 }
57 return StructType(context->getRef(), type);
58 },
59 "elements"_a, nb::kw_only(), "packed"_a = false, "loc"_a = nb::none(),
60 "context"_a = nb::none());
61
62 c.def_static(
63 "get_literal_unchecked",
64 [](const std::vector<PyType> &elements, bool packed,
66 python::CollectDiagnosticsToStringScope scope(context.get()->get());
67
68 std::vector<MlirType> elements_(elements.size());
69 std::copy(elements.begin(), elements.end(), elements_.begin());
70
71 MlirType type = mlirLLVMStructTypeLiteralGet(
72 context.get()->get(), elements.size(), elements_.data(), packed);
73 if (mlirTypeIsNull(type)) {
74 throw nb::value_error(scope.takeMessage().c_str());
75 }
76 return StructType(context->getRef(), type);
77 },
78 "elements"_a, nb::kw_only(), "packed"_a = false,
79 "context"_a = nb::none());
80
81 c.def_static(
82 "get_identified",
83 [](const std::string &name, DefaultingPyMlirContext context) {
84 return StructType(context->getRef(),
86 context.get()->get(),
87 mlirStringRefCreate(name.data(), name.size())));
88 },
89 "name"_a, nb::kw_only(), "context"_a = nb::none());
90
91 c.def_static(
92 "get_opaque",
93 [](const std::string &name, DefaultingPyMlirContext context) {
94 return StructType(context->getRef(),
96 context.get()->get(),
97 mlirStringRefCreate(name.data(), name.size())));
98 },
99 "name"_a, "context"_a = nb::none());
100
101 c.def(
102 "set_body",
103 [](const StructType &self, const std::vector<PyType> &elements,
104 bool packed) {
105 std::vector<MlirType> elements_(elements.size());
106 std::copy(elements.begin(), elements.end(), elements_.begin());
108 self, elements.size(), elements_.data(), packed);
110 throw nb::value_error(
111 "Struct body already set to different content.");
112 }
113 },
114 "elements"_a, nb::kw_only(), "packed"_a = false);
115
116 c.def_static(
117 "new_identified",
118 [](const std::string &name, const std::vector<PyType> &elements,
119 bool packed, DefaultingPyMlirContext context) {
120 std::vector<MlirType> elements_(elements.size());
121 std::copy(elements.begin(), elements.end(), elements_.begin());
122 return StructType(context->getRef(),
124 context.get()->get(),
125 mlirStringRefCreate(name.data(), name.length()),
126 elements.size(), elements_.data(), packed));
127 },
128 "name"_a, "elements"_a, nb::kw_only(), "packed"_a = false,
129 "context"_a = nb::none());
130
131 c.def_prop_ro(
132 "name", [](const StructType &type) -> std::optional<std::string> {
134 return std::nullopt;
135
137 return std::string(stringRef.data, stringRef.length);
138 });
139
140 c.def_prop_ro("body", [](const StructType &type) -> nb::object {
141 // Don't crash in absence of a body.
143 return nb::none();
144
145 nb::list body;
147 i < e; ++i) {
148 body.append(mlirLLVMStructTypeGetElementType(type, i));
149 }
150 return body;
151 });
152
153 c.def_prop_ro("packed", [](const StructType &type) {
154 return mlirLLVMStructTypeIsPacked(type);
155 });
156
157 c.def_prop_ro("opaque", [](const StructType &type) {
158 return mlirLLVMStructTypeIsOpaque(type);
159 });
160 }
161};
162
163//===--------------------------------------------------------------------===//
164// PointerType
165//===--------------------------------------------------------------------===//
166
167struct PointerType : PyConcreteType<PointerType> {
171 static constexpr const char *pyClassName = "PointerType";
173 using Base::Base;
174
175 static void bindDerived(ClassTy &c) {
176 c.def_static(
177 "get",
178 [](std::optional<unsigned> addressSpace,
179 DefaultingPyMlirContext context) {
180 python::CollectDiagnosticsToStringScope scope(context.get()->get());
181 MlirType type = mlirLLVMPointerTypeGet(
182 context.get()->get(),
183 addressSpace.has_value() ? *addressSpace : 0);
184 if (mlirTypeIsNull(type)) {
185 throw nb::value_error(scope.takeMessage().c_str());
186 }
187 return PointerType(context->getRef(), type);
188 },
189 "address_space"_a = nb::none(), nb::kw_only(),
190 "context"_a = nb::none());
191 c.def_prop_ro("address_space", [](const PointerType &type) {
193 });
194 }
195};
196
197static void populateDialectLLVMSubmodule(nanobind::module_ &m) {
200
201 m.def(
202 "translate_module_to_llvmir",
203 [](const PyOperation &module) {
205 },
206 "module"_a, nb::rv_policy::take_ownership);
207}
208} // namespace llvm
209} // namespace MLIR_BINDINGS_PYTHON_DOMAIN
210} // namespace python
211} // namespace mlir
212
213NB_MODULE(_mlirDialectsLLVM, m) {
214 m.doc() = "MLIR LLVM Dialect";
215
217 m);
218}
NB_MODULE(_mlirDialectsLLVM, m)
MLIR_CAPI_EXPORTED char * mlirTranslateModuleToLLVMIRToString(MlirOperation module)
Definition LLVMIR.cpp:37
RAII scope intercepting all diagnostics into a string.
Definition Diagnostics.h:25
ReferrentTy * get() const
Used in function arguments when None should resolve to the current context manager set instance.
Definition IRCore.h:525
Used in function arguments when None should resolve to the current context manager set instance.
Definition IRCore.h:279
MLIR_CAPI_EXPORTED 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:153
MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeIdentifiedGet(MlirContext ctx, MlirStringRef name)
Creates an LLVM identified struct type with no body.
Definition LLVM.cpp:149
MLIR_CAPI_EXPORTED bool mlirTypeIsALLVMPointerType(MlirType type)
Returns true if the type is an LLVM dialect pointer type.
Definition LLVM.cpp:38
MLIR_CAPI_EXPORTED 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:163
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMStructTypeGetName(void)
Definition LLVM.cpp:98
MLIR_CAPI_EXPORTED MlirType mlirLLVMPointerTypeGet(MlirContext ctx, unsigned addressSpace)
Creates an llvm.ptr type.
Definition LLVM.cpp:26
MLIR_CAPI_EXPORTED bool mlirLLVMStructTypeIsLiteral(MlirType type)
Returns true if the type is a literal (unnamed) LLVM struct type.
Definition LLVM.cpp:102
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMStructTypeGetIdentifier(MlirType type)
Returns the identifier of the identified struct.
Definition LLVM.cpp:118
MLIR_CAPI_EXPORTED bool mlirLLVMStructTypeIsOpaque(MlirType type)
Returns true is the struct is explicitly opaque (will not have a body) or uninitialized (will eventua...
Definition LLVM.cpp:122
MLIR_CAPI_EXPORTED bool mlirLLVMStructTypeIsPacked(MlirType type)
Returns true if the struct is packed.
Definition LLVM.cpp:114
MLIR_CAPI_EXPORTED intptr_t mlirLLVMStructTypeGetNumElementTypes(MlirType type)
Returns the number of fields in the struct.
Definition LLVM.cpp:106
MLIR_CAPI_EXPORTED MlirTypeID mlirLLVMPointerTypeGetTypeID(void)
Definition LLVM.cpp:34
MLIR_CAPI_EXPORTED MlirTypeID mlirLLVMStructTypeGetTypeID(void)
Definition LLVM.cpp:94
MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeGetElementType(MlirType type, intptr_t position)
Returns the positions-th field of the struct.
Definition LLVM.cpp:110
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMPointerTypeGetName(void)
Definition LLVM.cpp:30
MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeOpaqueGet(MlirContext ctx, MlirStringRef name)
Definition LLVM.cpp:145
MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeLiteralGet(MlirContext ctx, intptr_t nFieldTypes, MlirType const *fieldTypes, bool isPacked)
Creates an LLVM literal (unnamed) struct type.
Definition LLVM.cpp:126
MLIR_CAPI_EXPORTED bool mlirTypeIsALLVMStructType(MlirType type)
Returns true if the type is an LLVM dialect struct type.
Definition LLVM.cpp:90
MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeLiteralGetChecked(MlirLocation loc, intptr_t nFieldTypes, MlirType const *fieldTypes, bool isPacked)
Creates an LLVM literal (unnamed) struct type if possible.
Definition LLVM.cpp:135
MLIR_CAPI_EXPORTED unsigned mlirLLVMPointerTypeGetAddressSpace(MlirType pointerType)
Returns address space of llvm.ptr.
Definition LLVM.cpp:42
static bool mlirTypeIsNull(MlirType type)
Checks whether a type is null.
Definition IR.h:1160
MLIR_CAPI_EXPORTED MlirContext mlirLocationGetContext(MlirLocation location)
Gets the context that a location was created with.
Definition IR.cpp:410
static MlirStringRef mlirStringRefCreate(const char *str, size_t length)
Constructs a string reference from the pointer and length.
Definition Support.h:87
static bool mlirLogicalResultIsSuccess(MlirLogicalResult res)
Checks if the given logical result represents a success.
Definition Support.h:127
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
static void populateDialectLLVMSubmodule(nanobind::module_ &m)
Include the generated interface declarations.
A logical result value, essentially a boolean with named states.
Definition Support.h:121
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:78
const char * data
Pointer to the first symbol.
Definition Support.h:79
size_t length
Length of the fragment.
Definition Support.h:80
static constexpr GetTypeIDFunctionTy getTypeIdFunction