MLIR  19.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 "mlir-c/Dialect/LLVM.h"
10 #include "mlir-c/IR.h"
11 #include "mlir-c/Support.h"
13 #include <string>
14 
15 namespace py = pybind11;
16 using namespace llvm;
17 using namespace mlir;
18 using namespace mlir::python;
19 using namespace mlir::python::adaptors;
20 
21 void populateDialectLLVMSubmodule(const pybind11::module &m) {
22 
23  //===--------------------------------------------------------------------===//
24  // StructType
25  //===--------------------------------------------------------------------===//
26 
27  auto llvmStructType =
29 
30  llvmStructType.def_classmethod(
31  "get_literal",
32  [](py::object cls, const std::vector<MlirType> &elements, bool packed,
33  MlirLocation loc) {
35 
37  loc, elements.size(), elements.data(), packed);
38  if (mlirTypeIsNull(type)) {
39  throw py::value_error(scope.takeMessage());
40  }
41  return cls(type);
42  },
43  "cls"_a, "elements"_a, py::kw_only(), "packed"_a = false,
44  "loc"_a = py::none());
45 
46  llvmStructType.def_classmethod(
47  "get_identified",
48  [](py::object cls, const std::string &name, MlirContext context) {
50  context, mlirStringRefCreate(name.data(), name.size())));
51  },
52  "cls"_a, "name"_a, py::kw_only(), "context"_a = py::none());
53 
54  llvmStructType.def_classmethod(
55  "get_opaque",
56  [](py::object cls, const std::string &name, MlirContext context) {
57  return cls(mlirLLVMStructTypeOpaqueGet(
58  context, mlirStringRefCreate(name.data(), name.size())));
59  },
60  "cls"_a, "name"_a, "context"_a = py::none());
61 
62  llvmStructType.def(
63  "set_body",
64  [](MlirType self, const std::vector<MlirType> &elements, bool packed) {
66  self, elements.size(), elements.data(), packed);
67  if (!mlirLogicalResultIsSuccess(result)) {
68  throw py::value_error(
69  "Struct body already set to different content.");
70  }
71  },
72  "elements"_a, py::kw_only(), "packed"_a = false);
73 
74  llvmStructType.def_classmethod(
75  "new_identified",
76  [](py::object cls, const std::string &name,
77  const std::vector<MlirType> &elements, bool packed, MlirContext ctx) {
79  ctx, mlirStringRefCreate(name.data(), name.length()),
80  elements.size(), elements.data(), packed));
81  },
82  "cls"_a, "name"_a, "elements"_a, py::kw_only(), "packed"_a = false,
83  "context"_a = py::none());
84 
85  llvmStructType.def_property_readonly(
86  "name", [](MlirType type) -> std::optional<std::string> {
88  return std::nullopt;
89 
91  return StringRef(stringRef.data, stringRef.length).str();
92  });
93 
94  llvmStructType.def_property_readonly("body", [](MlirType type) -> py::object {
95  // Don't crash in absence of a body.
97  return py::none();
98 
99  py::list body;
100  for (intptr_t i = 0, e = mlirLLVMStructTypeGetNumElementTypes(type); i < e;
101  ++i) {
102  body.append(mlirLLVMStructTypeGetElementType(type, i));
103  }
104  return body;
105  });
106 
107  llvmStructType.def_property_readonly(
108  "packed", [](MlirType type) { return mlirLLVMStructTypeIsPacked(type); });
109 
110  llvmStructType.def_property_readonly(
111  "opaque", [](MlirType type) { return mlirLLVMStructTypeIsOpaque(type); });
112 
113  //===--------------------------------------------------------------------===//
114  // PointerType
115  //===--------------------------------------------------------------------===//
116 
119  "get",
120  [](py::object cls, std::optional<unsigned> addressSpace,
121  MlirContext context) {
122  CollectDiagnosticsToStringScope scope(context);
123  MlirType type = mlirLLVMPointerTypeGet(
124  context, addressSpace.has_value() ? *addressSpace : 0);
125  if (mlirTypeIsNull(type)) {
126  throw py::value_error(scope.takeMessage());
127  }
128  return cls(type);
129  },
130  "cls"_a, "address_space"_a = py::none(), py::kw_only(),
131  "context"_a = py::none())
132  .def_property_readonly("address_space", [](MlirType type) {
134  });
135 }
136 
137 PYBIND11_MODULE(_mlirDialectsLLVM, m) {
138  m.doc() = "MLIR LLVM Dialect";
139 
141 }
void populateDialectLLVMSubmodule(const pybind11::module &m)
Definition: DialectLLVM.cpp:21
PYBIND11_MODULE(_mlirDialectsLLVM, m)
RAII scope intercepting all diagnostics into a string.
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:109
MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeIdentifiedGet(MlirContext ctx, MlirStringRef name)
Creates an LLVM identified struct type with no body.
Definition: LLVM.cpp:105
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:119
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:58
MLIR_CAPI_EXPORTED MlirStringRef mlirLLVMStructTypeGetIdentifier(MlirType type)
Returns the identifier of the identified struct.
Definition: LLVM.cpp:74
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:78
MLIR_CAPI_EXPORTED bool mlirLLVMStructTypeIsPacked(MlirType type)
Returns true if the struct is packed.
Definition: LLVM.cpp:70
MLIR_CAPI_EXPORTED intptr_t mlirLLVMStructTypeGetNumElementTypes(MlirType type)
Returns the number of fields in the struct.
Definition: LLVM.cpp:62
MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeGetElementType(MlirType type, intptr_t position)
Returns the positions-th field of the struct.
Definition: LLVM.cpp:66
MLIR_CAPI_EXPORTED MlirType mlirLLVMStructTypeOpaqueGet(MlirContext ctx, MlirStringRef name)
Definition: LLVM.cpp:101
MLIR_CAPI_EXPORTED bool mlirTypeIsALLVMStructType(MlirType type)
Returns true if the type is an LLVM dialect struct type.
Definition: LLVM.cpp:54
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:91
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:984
MLIR_CAPI_EXPORTED MlirContext mlirLocationGetContext(MlirLocation location)
Gets the context that a location was created with.
Definition: IR.cpp:287
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
Include the generated interface declarations.
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