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 llvm;
24using namespace mlir;
26
27namespace mlir {
28namespace python {
30namespace llvm {
31//===--------------------------------------------------------------------===//
32// StructType
33//===--------------------------------------------------------------------===//
34
35struct StructType : PyConcreteType<StructType> {
39 static constexpr const char *pyClassName = "StructType";
41 using Base::Base;
42
43 static void bindDerived(ClassTy &c) {
44 c.def_static(
45 "get_literal",
46 [](const std::vector<PyType> &elements, bool packed,
50 std::vector<MlirType> elements_(elements.size());
51 std::copy(elements.begin(), elements.end(), elements_.begin());
52
54 loc, elements.size(), elements_.data(), packed);
55 if (mlirTypeIsNull(type)) {
56 throw nb::value_error(scope.takeMessage().c_str());
57 }
58 return StructType(context->getRef(), type);
59 },
60 "elements"_a, nb::kw_only(), "packed"_a = false, "loc"_a = nb::none(),
61 "context"_a = nb::none());
62
63 c.def_static(
64 "get_literal_unchecked",
65 [](const std::vector<PyType> &elements, bool packed,
67 python::CollectDiagnosticsToStringScope scope(context.get()->get());
68
69 std::vector<MlirType> elements_(elements.size());
70 std::copy(elements.begin(), elements.end(), elements_.begin());
71
72 MlirType type = mlirLLVMStructTypeLiteralGet(
73 context.get()->get(), elements.size(), elements_.data(), packed);
74 if (mlirTypeIsNull(type)) {
75 throw nb::value_error(scope.takeMessage().c_str());
76 }
77 return StructType(context->getRef(), type);
78 },
79 "elements"_a, nb::kw_only(), "packed"_a = false,
80 "context"_a = nb::none());
81
82 c.def_static(
83 "get_identified",
84 [](const std::string &name, DefaultingPyMlirContext context) {
85 return StructType(context->getRef(),
87 context.get()->get(),
88 mlirStringRefCreate(name.data(), name.size())));
89 },
90 "name"_a, nb::kw_only(), "context"_a = nb::none());
91
92 c.def_static(
93 "get_opaque",
94 [](const std::string &name, DefaultingPyMlirContext context) {
95 return StructType(context->getRef(),
97 context.get()->get(),
98 mlirStringRefCreate(name.data(), name.size())));
99 },
100 "name"_a, "context"_a = nb::none());
101
102 c.def(
103 "set_body",
104 [](const StructType &self, const std::vector<PyType> &elements,
105 bool packed) {
106 std::vector<MlirType> elements_(elements.size());
107 std::copy(elements.begin(), elements.end(), elements_.begin());
109 self, elements.size(), elements_.data(), packed);
111 throw nb::value_error(
112 "Struct body already set to different content.");
113 }
114 },
115 "elements"_a, nb::kw_only(), "packed"_a = false);
116
117 c.def_static(
118 "new_identified",
119 [](const std::string &name, const std::vector<PyType> &elements,
120 bool packed, DefaultingPyMlirContext context) {
121 std::vector<MlirType> elements_(elements.size());
122 std::copy(elements.begin(), elements.end(), elements_.begin());
123 return StructType(context->getRef(),
125 context.get()->get(),
126 mlirStringRefCreate(name.data(), name.length()),
127 elements.size(), elements_.data(), packed));
128 },
129 "name"_a, "elements"_a, nb::kw_only(), "packed"_a = false,
130 "context"_a = nb::none());
131
132 c.def_prop_ro(
133 "name", [](const StructType &type) -> std::optional<std::string> {
135 return std::nullopt;
136
138 return StringRef(stringRef.data, stringRef.length).str();
139 });
140
141 c.def_prop_ro("body", [](const StructType &type) -> nb::object {
142 // Don't crash in absence of a body.
144 return nb::none();
145
146 nb::list body;
148 i < e; ++i) {
149 body.append(mlirLLVMStructTypeGetElementType(type, i));
150 }
151 return body;
152 });
153
154 c.def_prop_ro("packed", [](const StructType &type) {
155 return mlirLLVMStructTypeIsPacked(type);
156 });
157
158 c.def_prop_ro("opaque", [](const StructType &type) {
159 return mlirLLVMStructTypeIsOpaque(type);
160 });
161 }
162};
163
164//===--------------------------------------------------------------------===//
165// PointerType
166//===--------------------------------------------------------------------===//
167
168struct PointerType : PyConcreteType<PointerType> {
172 static constexpr const char *pyClassName = "PointerType";
174 using Base::Base;
175
176 static void bindDerived(ClassTy &c) {
177 c.def_static(
178 "get",
179 [](std::optional<unsigned> addressSpace,
180 DefaultingPyMlirContext context) {
181 python::CollectDiagnosticsToStringScope scope(context.get()->get());
182 MlirType type = mlirLLVMPointerTypeGet(
183 context.get()->get(),
184 addressSpace.has_value() ? *addressSpace : 0);
185 if (mlirTypeIsNull(type)) {
186 throw nb::value_error(scope.takeMessage().c_str());
187 }
188 return PointerType(context->getRef(), type);
189 },
190 "address_space"_a = nb::none(), nb::kw_only(),
191 "context"_a = nb::none());
192 c.def_prop_ro("address_space", [](const PointerType &type) {
194 });
195 }
196};
197
198static void populateDialectLLVMSubmodule(nanobind::module_ &m) {
201
202 m.def(
203 "translate_module_to_llvmir",
204 [](const PyOperation &module) {
206 },
207 "module"_a, nb::rv_policy::take_ownership);
208}
209} // namespace llvm
210} // namespace MLIR_BINDINGS_PYTHON_DOMAIN
211} // namespace python
212} // namespace mlir
213
214NB_MODULE(_mlirDialectsLLVM, m) {
215 m.doc() = "MLIR LLVM Dialect";
216
218 m);
219}
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:526
Used in function arguments when None should resolve to the current context manager set instance.
Definition IRCore.h:280
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:1156
MLIR_CAPI_EXPORTED MlirContext mlirLocationGetContext(MlirLocation location)
Gets the context that a location was created with.
Definition IR.cpp:411
static MlirStringRef mlirStringRefCreate(const char *str, size_t length)
Constructs a string reference from the pointer and length.
Definition Support.h:84
static bool mlirLogicalResultIsSuccess(MlirLogicalResult res)
Checks if the given logical result represents a success.
Definition Support.h:124
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:118
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:75
const char * data
Pointer to the first symbol.
Definition Support.h:76
size_t length
Length of the fragment.
Definition Support.h:77
static constexpr GetTypeIDFunctionTy getTypeIdFunction