MLIR 23.0.0git
DialectSMT.cpp
Go to the documentation of this file.
1//===- DialectSMT.cpp - Pybind module for SMT 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
10
11#include "mlir-c/Dialect/SMT.h"
12#include "mlir-c/IR.h"
13#include "mlir-c/Support.h"
19
20namespace nb = nanobind;
21
22using namespace nanobind::literals;
23using namespace mlir;
25
26namespace mlir {
27namespace python {
29namespace smt {
30struct BoolType : PyConcreteType<BoolType> {
32 static constexpr const char *pyClassName = "BoolType";
33 static inline const MlirStringRef name = mlirSMTBoolTypeGetName();
34 using Base::Base;
35
36 static void bindDerived(ClassTy &c) {
37 c.def_static(
38 "get",
39 [](DefaultingPyMlirContext context) {
40 return BoolType(context->getRef(),
41 mlirSMTTypeGetBool(context.get()->get()));
42 },
43 nb::arg("context").none() = nb::none());
44 }
45};
46
47struct BitVectorType : PyConcreteType<BitVectorType> {
49 static constexpr const char *pyClassName = "BitVectorType";
51 using Base::Base;
52
53 static void bindDerived(ClassTy &c) {
54 c.def_static(
55 "get",
56 [](int32_t width, DefaultingPyMlirContext context) {
57 return BitVectorType(
58 context->getRef(),
59 mlirSMTTypeGetBitVector(context.get()->get(), width));
60 },
61 nb::arg("width"), nb::arg("context").none() = nb::none());
62 }
63};
64
65struct IntType : PyConcreteType<IntType> {
67 static constexpr const char *pyClassName = "IntType";
68 static inline const MlirStringRef name = mlirSMTIntTypeGetName();
69 using Base::Base;
70
71 static void bindDerived(ClassTy &c) {
72 c.def_static(
73 "get",
74 [](DefaultingPyMlirContext context) {
75 return IntType(context->getRef(),
76 mlirSMTTypeGetInt(context.get()->get()));
77 },
78 nb::arg("context").none() = nb::none());
79 }
80};
81
82static void populateDialectSMTSubmodule(nanobind::module_ &m) {
86
87 auto exportSMTLIB = [](MlirOperation module, bool inlineSingleUseValues,
88 bool indentLetBody) {
90 PyPrintAccumulator printAccum;
92 module, printAccum.getCallback(), printAccum.getUserData(),
93 inlineSingleUseValues, indentLetBody);
95 return printAccum.join();
96 throw nb::value_error(
97 ("Failed to export smtlib.\nDiagnostic message " + scope.takeMessage())
98 .c_str());
99 };
100
101 m.def(
102 "export_smtlib",
103 [&exportSMTLIB](const PyOperation &module, bool inlineSingleUseValues,
104 bool indentLetBody) {
105 return exportSMTLIB(module, inlineSingleUseValues, indentLetBody);
106 },
107 "module"_a, "inline_single_use_values"_a = false,
108 "indent_let_body"_a = false);
109 m.def(
110 "export_smtlib",
111 [&exportSMTLIB](PyModule &module, bool inlineSingleUseValues,
112 bool indentLetBody) {
113 return exportSMTLIB(mlirModuleGetOperation(module.get()),
114 inlineSingleUseValues, indentLetBody);
115 },
116 "module"_a, "inline_single_use_values"_a = false,
117 "indent_let_body"_a = false);
118}
119} // namespace smt
120} // namespace MLIR_BINDINGS_PYTHON_DOMAIN
121} // namespace python
122} // namespace mlir
123
124NB_MODULE(_mlirDialectsSMT, m) {
125 m.doc() = "MLIR SMT Dialect";
126
128}
NB_MODULE(_mlirDialectsSMT, m)
MlirOperation mlirModuleGetOperation(MlirModule module)
Definition IR.cpp:460
MlirContext mlirOperationGetContext(MlirOperation op)
Definition IR.cpp:651
MLIR_CAPI_EXPORTED MlirType mlirSMTTypeGetBitVector(MlirContext ctx, int32_t width)
Creates a smt::BitVectorType with the given width.
Definition SMT.cpp:48
MLIR_CAPI_EXPORTED MlirStringRef mlirSMTBoolTypeGetName(void)
Definition SMT.cpp:62
MLIR_CAPI_EXPORTED MlirType mlirSMTTypeGetInt(MlirContext ctx)
Creates a smt::IntType.
Definition SMT.cpp:66
MLIR_CAPI_EXPORTED bool mlirSMTTypeIsABool(MlirType type)
Checks if the given type is a smt::BoolType.
Definition SMT.cpp:56
MLIR_CAPI_EXPORTED bool mlirSMTTypeIsAInt(MlirType type)
Checks if the given type is a smt::IntType.
Definition SMT.cpp:64
MLIR_CAPI_EXPORTED MlirType mlirSMTTypeGetBool(MlirContext ctx)
Creates a smt::BoolType.
Definition SMT.cpp:58
MLIR_CAPI_EXPORTED MlirStringRef mlirSMTIntTypeGetName(void)
Definition SMT.cpp:70
MLIR_CAPI_EXPORTED MlirStringRef mlirSMTBitVectorTypeGetName(void)
Definition SMT.cpp:52
MLIR_CAPI_EXPORTED bool mlirSMTTypeIsABitVector(MlirType type)
Checks if the given type is a smt::BitVectorType.
Definition SMT.cpp:44
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:280
static bool mlirLogicalResultIsSuccess(MlirLogicalResult res)
Checks if the given logical result represents a success.
Definition Support.h:124
MLIR_CAPI_EXPORTED MlirLogicalResult mlirTranslateOperationToSMTLIB(MlirOperation, MlirStringCallback, void *userData, bool inlineSingleUseValues, bool indentLetBody)
static void populateDialectSMTSubmodule(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
Accumulates into a python string from a method that accepts an MlirStringCallback.
MlirStringCallback getCallback()