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"
17 
18 namespace nb = nanobind;
19 
20 using namespace nanobind::literals;
21 
22 using namespace llvm;
23 using namespace mlir;
24 using namespace mlir::python;
25 using namespace mlir::python::nanobind_adaptors;
26 
27 static void populateDialectLLVMSubmodule(const nanobind::module_ &m) {
28 
29  //===--------------------------------------------------------------------===//
30  // StructType
31  //===--------------------------------------------------------------------===//
32 
33  auto llvmStructType =
35 
36  llvmStructType
37  .def_classmethod(
38  "get_literal",
39  [](const nb::object &cls, const std::vector<MlirType> &elements,
40  bool packed, MlirLocation loc) {
42 
44  loc, elements.size(), elements.data(), packed);
45  if (mlirTypeIsNull(type)) {
46  throw nb::value_error(scope.takeMessage().c_str());
47  }
48  return cls(type);
49  },
50  "cls"_a, "elements"_a, nb::kw_only(), "packed"_a = false,
51  "loc"_a = nb::none())
52  .def_classmethod(
53  "get_literal_unchecked",
54  [](const nb::object &cls, const std::vector<MlirType> &elements,
55  bool packed, MlirContext context) {
56  CollectDiagnosticsToStringScope scope(context);
57 
58  MlirType type = mlirLLVMStructTypeLiteralGet(
59  context, elements.size(), elements.data(), packed);
60  if (mlirTypeIsNull(type)) {
61  throw nb::value_error(scope.takeMessage().c_str());
62  }
63  return cls(type);
64  },
65  "cls"_a, "elements"_a, nb::kw_only(), "packed"_a = false,
66  "context"_a = nb::none());
67 
68  llvmStructType.def_classmethod(
69  "get_identified",
70  [](const nb::object &cls, const std::string &name, MlirContext context) {
72  context, mlirStringRefCreate(name.data(), name.size())));
73  },
74  "cls"_a, "name"_a, nb::kw_only(), "context"_a = nb::none());
75 
76  llvmStructType.def_classmethod(
77  "get_opaque",
78  [](const nb::object &cls, const std::string &name, MlirContext context) {
79  return cls(mlirLLVMStructTypeOpaqueGet(
80  context, mlirStringRefCreate(name.data(), name.size())));
81  },
82  "cls"_a, "name"_a, "context"_a = nb::none());
83 
84  llvmStructType.def(
85  "set_body",
86  [](MlirType self, const std::vector<MlirType> &elements, bool packed) {
88  self, elements.size(), elements.data(), packed);
89  if (!mlirLogicalResultIsSuccess(result)) {
90  throw nb::value_error(
91  "Struct body already set to different content.");
92  }
93  },
94  "elements"_a, nb::kw_only(), "packed"_a = false);
95 
96  llvmStructType.def_classmethod(
97  "new_identified",
98  [](const nb::object &cls, const std::string &name,
99  const std::vector<MlirType> &elements, bool packed, MlirContext ctx) {
101  ctx, mlirStringRefCreate(name.data(), name.length()),
102  elements.size(), elements.data(), packed));
103  },
104  "cls"_a, "name"_a, "elements"_a, nb::kw_only(), "packed"_a = false,
105  "context"_a = nb::none());
106 
107  llvmStructType.def_property_readonly(
108  "name", [](MlirType type) -> std::optional<std::string> {
109  if (mlirLLVMStructTypeIsLiteral(type))
110  return std::nullopt;
111 
113  return StringRef(stringRef.data, stringRef.length).str();
114  });
115 
116  llvmStructType.def_property_readonly("body", [](MlirType type) -> nb::object {
117  // Don't crash in absence of a body.
118  if (mlirLLVMStructTypeIsOpaque(type))
119  return nb::none();
120 
121  nb::list body;
122  for (intptr_t i = 0, e = mlirLLVMStructTypeGetNumElementTypes(type); i < e;
123  ++i) {
124  body.append(mlirLLVMStructTypeGetElementType(type, i));
125  }
126  return body;
127  });
128 
129  llvmStructType.def_property_readonly(
130  "packed", [](MlirType type) { return mlirLLVMStructTypeIsPacked(type); });
131 
132  llvmStructType.def_property_readonly(
133  "opaque", [](MlirType type) { return mlirLLVMStructTypeIsOpaque(type); });
134 
135  //===--------------------------------------------------------------------===//
136  // PointerType
137  //===--------------------------------------------------------------------===//
138 
141  "get",
142  [](const nb::object &cls, std::optional<unsigned> addressSpace,
143  MlirContext context) {
144  CollectDiagnosticsToStringScope scope(context);
145  MlirType type = mlirLLVMPointerTypeGet(
146  context, addressSpace.has_value() ? *addressSpace : 0);
147  if (mlirTypeIsNull(type)) {
148  throw nb::value_error(scope.takeMessage().c_str());
149  }
150  return cls(type);
151  },
152  "cls"_a, "address_space"_a = nb::none(), nb::kw_only(),
153  "context"_a = nb::none())
154  .def_property_readonly("address_space", [](MlirType type) {
156  });
157 }
158 
159 NB_MODULE(_mlirDialectsLLVM, m) {
160  m.doc() = "MLIR LLVM Dialect";
161 
163 }
static void populateDialectLLVMSubmodule(const nanobind::module_ &m)
Definition: DialectLLVM.cpp:27
NB_MODULE(_mlirDialectsLLVM, m)
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_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