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> {
34 static constexpr const char *pyClassName = "BoolType";
35 static inline const MlirStringRef name = mlirSMTBoolTypeGetName();
36 using Base::Base;
37
38 static void bindDerived(ClassTy &c) {
39 c.def_static(
40 "get",
41 [](DefaultingPyMlirContext context) {
42 return BoolType(context->getRef(),
43 mlirSMTTypeGetBool(context.get()->get()));
44 },
45 nb::arg("context").none() = nb::none());
46 }
47};
48
49struct BitVectorType : PyConcreteType<BitVectorType> {
53 static constexpr const char *pyClassName = "BitVectorType";
55 using Base::Base;
56
57 static void bindDerived(ClassTy &c) {
58 c.def_static(
59 "get",
60 [](int32_t width, DefaultingPyMlirContext context) {
61 return BitVectorType(
62 context->getRef(),
63 mlirSMTTypeGetBitVector(context.get()->get(), width));
64 },
65 nb::arg("width"), nb::arg("context").none() = nb::none());
66 }
67};
68
69struct IntType : PyConcreteType<IntType> {
73 static constexpr const char *pyClassName = "IntType";
74 static inline const MlirStringRef name = mlirSMTIntTypeGetName();
75 using Base::Base;
76
77 static void bindDerived(ClassTy &c) {
78 c.def_static(
79 "get",
80 [](DefaultingPyMlirContext context) {
81 return IntType(context->getRef(),
82 mlirSMTTypeGetInt(context.get()->get()));
83 },
84 nb::arg("context").none() = nb::none());
85 }
86};
87
88static void populateDialectSMTSubmodule(nanobind::module_ &m) {
92
93 auto exportSMTLIB = [](MlirOperation module, bool inlineSingleUseValues,
94 bool indentLetBody) {
96 PyPrintAccumulator printAccum;
98 module, printAccum.getCallback(), printAccum.getUserData(),
99 inlineSingleUseValues, indentLetBody);
101 return printAccum.join();
102 throw nb::value_error(
103 ("Failed to export smtlib.\nDiagnostic message " + scope.takeMessage())
104 .c_str());
105 };
106
107 m.def(
108 "export_smtlib",
109 [&exportSMTLIB](const PyOperation &module, bool inlineSingleUseValues,
110 bool indentLetBody) {
111 return exportSMTLIB(module, inlineSingleUseValues, indentLetBody);
112 },
113 "module"_a, "inline_single_use_values"_a = false,
114 "indent_let_body"_a = false);
115 m.def(
116 "export_smtlib",
117 [&exportSMTLIB](PyModule &module, bool inlineSingleUseValues,
118 bool indentLetBody) {
119 return exportSMTLIB(mlirModuleGetOperation(module.get()),
120 inlineSingleUseValues, indentLetBody);
121 },
122 "module"_a, "inline_single_use_values"_a = false,
123 "indent_let_body"_a = false);
124}
125} // namespace smt
126} // namespace MLIR_BINDINGS_PYTHON_DOMAIN
127} // namespace python
128} // namespace mlir
129
130NB_MODULE(_mlirDialectsSMT, m) {
131 m.doc() = "MLIR SMT Dialect";
132
134}
NB_MODULE(_mlirDialectsSMT, m)
MlirOperation mlirModuleGetOperation(MlirModule module)
Definition IR.cpp:459
MlirContext mlirOperationGetContext(MlirOperation op)
Definition IR.cpp:650
MLIR_CAPI_EXPORTED MlirTypeID mlirSMTIntTypeGetTypeID(void)
Definition SMT.cpp:80
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 MlirTypeID mlirSMTBitVectorTypeGetTypeID(void)
Definition SMT.cpp:56
MLIR_CAPI_EXPORTED MlirStringRef mlirSMTBoolTypeGetName(void)
Definition SMT.cpp:66
MLIR_CAPI_EXPORTED MlirType mlirSMTTypeGetInt(MlirContext ctx)
Creates a smt::IntType.
Definition SMT.cpp:74
MLIR_CAPI_EXPORTED bool mlirSMTTypeIsABool(MlirType type)
Checks if the given type is a smt::BoolType.
Definition SMT.cpp:60
MLIR_CAPI_EXPORTED bool mlirSMTTypeIsAInt(MlirType type)
Checks if the given type is a smt::IntType.
Definition SMT.cpp:72
MLIR_CAPI_EXPORTED MlirType mlirSMTTypeGetBool(MlirContext ctx)
Creates a smt::BoolType.
Definition SMT.cpp:62
MLIR_CAPI_EXPORTED MlirTypeID mlirSMTBoolTypeGetTypeID(void)
Definition SMT.cpp:68
MLIR_CAPI_EXPORTED MlirStringRef mlirSMTIntTypeGetName(void)
Definition SMT.cpp:78
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:278
static bool mlirLogicalResultIsSuccess(MlirLogicalResult res)
Checks if the given logical result represents a success.
Definition Support.h:127
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:121
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:78
Accumulates into a python string from a method that accepts an MlirStringCallback.
MlirStringCallback getCallback()
static constexpr GetTypeIDFunctionTy getTypeIdFunction
static constexpr GetTypeIDFunctionTy getTypeIdFunction
static constexpr GetTypeIDFunctionTy getTypeIdFunction