MLIR  20.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 void populateDialectLLVMSubmodule(const nanobind::module_ &m) {
28 
29  //===--------------------------------------------------------------------===//
30  // StructType
31  //===--------------------------------------------------------------------===//
32 
33  auto llvmStructType =
35 
36  llvmStructType.def_classmethod(
37  "get_literal",
38  [](nb::object cls, const std::vector<MlirType> &elements, bool packed,
39  MlirLocation loc) {
41 
43  loc, elements.size(), elements.data(), packed);
44  if (mlirTypeIsNull(type)) {
45  throw nb::value_error(scope.takeMessage().c_str());
46  }
47  return cls(type);
48  },
49  "cls"_a, "elements"_a, nb::kw_only(), "packed"_a = false,
50  "loc"_a.none() = nb::none());
51 
52  llvmStructType.def_classmethod(
53  "get_identified",
54  [](nb::object cls, const std::string &name, MlirContext context) {
56  context, mlirStringRefCreate(name.data(), name.size())));
57  },
58  "cls"_a, "name"_a, nb::kw_only(), "context"_a.none() = nb::none());
59 
60  llvmStructType.def_classmethod(
61  "get_opaque",
62  [](nb::object cls, const std::string &name, MlirContext context) {
63  return cls(mlirLLVMStructTypeOpaqueGet(
64  context, mlirStringRefCreate(name.data(), name.size())));
65  },
66  "cls"_a, "name"_a, "context"_a.none() = nb::none());
67 
68  llvmStructType.def(
69  "set_body",
70  [](MlirType self, const std::vector<MlirType> &elements, bool packed) {
72  self, elements.size(), elements.data(), packed);
73  if (!mlirLogicalResultIsSuccess(result)) {
74  throw nb::value_error(
75  "Struct body already set to different content.");
76  }
77  },
78  "elements"_a, nb::kw_only(), "packed"_a = false);
79 
80  llvmStructType.def_classmethod(
81  "new_identified",
82  [](nb::object cls, const std::string &name,
83  const std::vector<MlirType> &elements, bool packed, MlirContext ctx) {
85  ctx, mlirStringRefCreate(name.data(), name.length()),
86  elements.size(), elements.data(), packed));
87  },
88  "cls"_a, "name"_a, "elements"_a, nb::kw_only(), "packed"_a = false,
89  "context"_a.none() = nb::none());
90 
91  llvmStructType.def_property_readonly(
92  "name", [](MlirType type) -> std::optional<std::string> {
94  return std::nullopt;
95 
97  return StringRef(stringRef.data, stringRef.length).str();
98  });
99 
100  llvmStructType.def_property_readonly("body", [](MlirType type) -> nb::object {
101  // Don't crash in absence of a body.
102  if (mlirLLVMStructTypeIsOpaque(type))
103  return nb::none();
104 
105  nb::list body;
106  for (intptr_t i = 0, e = mlirLLVMStructTypeGetNumElementTypes(type); i < e;
107  ++i) {
108  body.append(mlirLLVMStructTypeGetElementType(type, i));
109  }
110  return body;
111  });
112 
113  llvmStructType.def_property_readonly(
114  "packed", [](MlirType type) { return mlirLLVMStructTypeIsPacked(type); });
115 
116  llvmStructType.def_property_readonly(
117  "opaque", [](MlirType type) { return mlirLLVMStructTypeIsOpaque(type); });
118 
119  //===--------------------------------------------------------------------===//
120  // PointerType
121  //===--------------------------------------------------------------------===//
122 
125  "get",
126  [](nb::object cls, std::optional<unsigned> addressSpace,
127  MlirContext context) {
128  CollectDiagnosticsToStringScope scope(context);
129  MlirType type = mlirLLVMPointerTypeGet(
130  context, addressSpace.has_value() ? *addressSpace : 0);
131  if (mlirTypeIsNull(type)) {
132  throw nb::value_error(scope.takeMessage().c_str());
133  }
134  return cls(type);
135  },
136  "cls"_a, "address_space"_a.none() = nb::none(), nb::kw_only(),
137  "context"_a.none() = nb::none())
138  .def_property_readonly("address_space", [](MlirType type) {
140  });
141 }
142 
143 NB_MODULE(_mlirDialectsLLVM, m) {
144  m.doc() = "MLIR LLVM Dialect";
145 
147 }
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:24
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:113
MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeIdentifiedGet(MlirContext ctx, MlirStringRef name)
Creates an LLVM identified struct type with no body.
Definition: LLVM.cpp:109
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:123
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:62
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMStructTypeGetIdentifier(MlirType type)
Returns the identifier of the identified struct.
Definition: LLVM.cpp:78
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:82
MLIR_CAPI_EXPORTED bool mlirLLVMStructTypeIsPacked(MlirType type)
Returns true if the struct is packed.
Definition: LLVM.cpp:74
MLIR_CAPI_EXPORTED intptr_t mlirLLVMStructTypeGetNumElementTypes(MlirType type)
Returns the number of fields in the struct.
Definition: LLVM.cpp:66
MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeGetElementType(MlirType type, intptr_t position)
Returns the positions-th field of the struct.
Definition: LLVM.cpp:70
MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeOpaqueGet(MlirContext ctx, MlirStringRef name)
Definition: LLVM.cpp:105
MLIR_CAPI_EXPORTED bool mlirTypeIsALLVMStructType(MlirType type)
Returns true if the type is an LLVM dialect struct type.
Definition: LLVM.cpp:58
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:95
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:1008
MLIR_CAPI_EXPORTED MlirContext mlirLocationGetContext(MlirLocation location)
Gets the context that a location was created with.
Definition: IR.cpp:296
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