MLIR  22.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"
14 #include "mlir-c/Target/LLVMIR.h"
18 
19 namespace nb = nanobind;
20 
21 using namespace nanobind::literals;
22 
23 using namespace llvm;
24 using namespace mlir;
25 using namespace mlir::python;
26 using namespace mlir::python::nanobind_adaptors;
27 
28 static void populateDialectLLVMSubmodule(nanobind::module_ &m) {
29 
30  //===--------------------------------------------------------------------===//
31  // StructType
32  //===--------------------------------------------------------------------===//
33 
34  auto llvmStructType =
36 
37  llvmStructType
38  .def_classmethod(
39  "get_literal",
40  [](const nb::object &cls, const std::vector<MlirType> &elements,
41  bool packed, MlirLocation loc) {
43 
45  loc, elements.size(), elements.data(), packed);
46  if (mlirTypeIsNull(type)) {
47  throw nb::value_error(scope.takeMessage().c_str());
48  }
49  return cls(type);
50  },
51  "cls"_a, "elements"_a, nb::kw_only(), "packed"_a = false,
52  "loc"_a = nb::none())
53  .def_classmethod(
54  "get_literal_unchecked",
55  [](const nb::object &cls, const std::vector<MlirType> &elements,
56  bool packed, MlirContext context) {
57  CollectDiagnosticsToStringScope scope(context);
58 
59  MlirType type = mlirLLVMStructTypeLiteralGet(
60  context, elements.size(), elements.data(), packed);
61  if (mlirTypeIsNull(type)) {
62  throw nb::value_error(scope.takeMessage().c_str());
63  }
64  return cls(type);
65  },
66  "cls"_a, "elements"_a, nb::kw_only(), "packed"_a = false,
67  "context"_a = nb::none());
68 
69  llvmStructType.def_classmethod(
70  "get_identified",
71  [](const nb::object &cls, const std::string &name, MlirContext context) {
73  context, mlirStringRefCreate(name.data(), name.size())));
74  },
75  "cls"_a, "name"_a, nb::kw_only(), "context"_a = nb::none());
76 
77  llvmStructType.def_classmethod(
78  "get_opaque",
79  [](const nb::object &cls, const std::string &name, MlirContext context) {
80  return cls(mlirLLVMStructTypeOpaqueGet(
81  context, mlirStringRefCreate(name.data(), name.size())));
82  },
83  "cls"_a, "name"_a, "context"_a = nb::none());
84 
85  llvmStructType.def(
86  "set_body",
87  [](MlirType self, const std::vector<MlirType> &elements, bool packed) {
89  self, elements.size(), elements.data(), packed);
90  if (!mlirLogicalResultIsSuccess(result)) {
91  throw nb::value_error(
92  "Struct body already set to different content.");
93  }
94  },
95  "elements"_a, nb::kw_only(), "packed"_a = false);
96 
97  llvmStructType.def_classmethod(
98  "new_identified",
99  [](const nb::object &cls, const std::string &name,
100  const std::vector<MlirType> &elements, bool packed, MlirContext ctx) {
102  ctx, mlirStringRefCreate(name.data(), name.length()),
103  elements.size(), elements.data(), packed));
104  },
105  "cls"_a, "name"_a, "elements"_a, nb::kw_only(), "packed"_a = false,
106  "context"_a = nb::none());
107 
108  llvmStructType.def_property_readonly(
109  "name", [](MlirType type) -> std::optional<std::string> {
110  if (mlirLLVMStructTypeIsLiteral(type))
111  return std::nullopt;
112 
114  return StringRef(stringRef.data, stringRef.length).str();
115  });
116 
117  llvmStructType.def_property_readonly("body", [](MlirType type) -> nb::object {
118  // Don't crash in absence of a body.
119  if (mlirLLVMStructTypeIsOpaque(type))
120  return nb::none();
121 
122  nb::list body;
123  for (intptr_t i = 0, e = mlirLLVMStructTypeGetNumElementTypes(type); i < e;
124  ++i) {
125  body.append(mlirLLVMStructTypeGetElementType(type, i));
126  }
127  return body;
128  });
129 
130  llvmStructType.def_property_readonly(
131  "packed", [](MlirType type) { return mlirLLVMStructTypeIsPacked(type); });
132 
133  llvmStructType.def_property_readonly(
134  "opaque", [](MlirType type) { return mlirLLVMStructTypeIsOpaque(type); });
135 
136  //===--------------------------------------------------------------------===//
137  // PointerType
138  //===--------------------------------------------------------------------===//
139 
142  "get",
143  [](const nb::object &cls, std::optional<unsigned> addressSpace,
144  MlirContext context) {
145  CollectDiagnosticsToStringScope scope(context);
146  MlirType type = mlirLLVMPointerTypeGet(
147  context, addressSpace.has_value() ? *addressSpace : 0);
148  if (mlirTypeIsNull(type)) {
149  throw nb::value_error(scope.takeMessage().c_str());
150  }
151  return cls(type);
152  },
153  "cls"_a, "address_space"_a = nb::none(), nb::kw_only(),
154  "context"_a = nb::none())
155  .def_property_readonly("address_space", [](MlirType type) {
157  });
158 
159  m.def(
160  "translate_module_to_llvmir",
161  [](MlirOperation module) {
163  },
164  // clang-format off
165  nb::sig("def translate_module_to_llvmir(module: " MAKE_MLIR_PYTHON_QUALNAME("ir.Operation") ") -> str"),
166  // clang-format on
167  "module"_a, nb::rv_policy::take_ownership);
168 }
169 
170 NB_MODULE(_mlirDialectsLLVM, m) {
171  m.doc() = "MLIR LLVM Dialect";
172 
174 }
static void populateDialectLLVMSubmodule(nanobind::module_ &m)
Definition: DialectLLVM.cpp:28
NB_MODULE(_mlirDialectsLLVM, m)
#define MAKE_MLIR_PYTHON_QUALNAME(local)
Definition: Interop.h:57
MLIR_CAPI_EXPORTED char * mlirTranslateModuleToLLVMIRToString(MlirOperation module)
Definition: LLVMIR.cpp:37
RAII scope intercepting all diagnostics into a string.
Definition: Diagnostics.h:25
Creates a custom subclass of mlir.ir.Type, implementing a casting constructor and type checking metho...
pure_subclass & def(const char *name, Func &&f, const Extra &...extra)
pure_subclass & def_classmethod(const char *name, Func &&f, const Extra &...extra)
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:127
MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeIdentifiedGet(MlirContext ctx, MlirStringRef name)
Creates an LLVM identified struct type with no body.
Definition: LLVM.cpp:123
MLIR_CAPI_EXPORTED bool mlirTypeIsALLVMPointerType(MlirType type)
Returns true if the type is an LLVM dialect pointer type.
Definition: LLVM.cpp:30
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:137
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:76
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMStructTypeGetIdentifier(MlirType type)
Returns the identifier of the identified struct.
Definition: LLVM.cpp:92
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:96
MLIR_CAPI_EXPORTED bool mlirLLVMStructTypeIsPacked(MlirType type)
Returns true if the struct is packed.
Definition: LLVM.cpp:88
MLIR_CAPI_EXPORTED intptr_t mlirLLVMStructTypeGetNumElementTypes(MlirType type)
Returns the number of fields in the struct.
Definition: LLVM.cpp:80
MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeGetElementType(MlirType type, intptr_t position)
Returns the positions-th field of the struct.
Definition: LLVM.cpp:84
MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeOpaqueGet(MlirContext ctx, MlirStringRef name)
Definition: LLVM.cpp:119
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:100
MLIR_CAPI_EXPORTED bool mlirTypeIsALLVMStructType(MlirType type)
Returns true if the type is an LLVM dialect struct type.
Definition: LLVM.cpp:72
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:109
MLIR_CAPI_EXPORTED unsigned mlirLLVMPointerTypeGetAddressSpace(MlirType pointerType)
Returns address space of llvm.ptr.
Definition: LLVM.cpp:34
static bool mlirTypeIsNull(MlirType type)
Checks whether a type is null.
Definition: IR.h:1148
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:82
static bool mlirLogicalResultIsSuccess(MlirLogicalResult res)
Checks if the given logical result represents a success.
Definition: Support.h:122
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition: CallGraph.h:229
Include the generated interface declarations.
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
const char * data
Pointer to the first symbol.
Definition: Support.h:74
size_t length
Length of the fragment.
Definition: Support.h:75