MLIR  21.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 
9 #include "NanobindUtils.h"
10 
11 #include "mlir-c/Dialect/SMT.h"
12 #include "mlir-c/IR.h"
13 #include "mlir-c/Support.h"
18 
19 namespace nb = nanobind;
20 
21 using namespace nanobind::literals;
22 
23 using namespace mlir;
24 using namespace mlir::python;
25 using namespace mlir::python::nanobind_adaptors;
26 
27 void populateDialectSMTSubmodule(nanobind::module_ &m) {
28 
29  auto smtBoolType = mlir_type_subclass(m, "BoolType", mlirSMTTypeIsABool)
31  "get",
32  [](const nb::object &, MlirContext context) {
33  return mlirSMTTypeGetBool(context);
34  },
35  "cls"_a, "context"_a.none() = nb::none());
36  auto smtBitVectorType =
37  mlir_type_subclass(m, "BitVectorType", mlirSMTTypeIsABitVector)
39  "get",
40  [](const nb::object &, int32_t width, MlirContext context) {
41  return mlirSMTTypeGetBitVector(context, width);
42  },
43  "cls"_a, "width"_a, "context"_a.none() = nb::none());
44 
45  auto exportSMTLIB = [](MlirOperation module, bool inlineSingleUseValues,
46  bool indentLetBody) {
48  mlirOperationGetContext(module));
49  PyPrintAccumulator printAccum;
51  module, printAccum.getCallback(), printAccum.getUserData(),
52  inlineSingleUseValues, indentLetBody);
53  if (mlirLogicalResultIsSuccess(result))
54  return printAccum.join();
55  throw nb::value_error(
56  ("Failed to export smtlib.\nDiagnostic message " + scope.takeMessage())
57  .c_str());
58  };
59 
60  m.def(
61  "export_smtlib",
62  [&exportSMTLIB](MlirOperation module, bool inlineSingleUseValues,
63  bool indentLetBody) {
64  return exportSMTLIB(module, inlineSingleUseValues, indentLetBody);
65  },
66  "module"_a, "inline_single_use_values"_a = false,
67  "indent_let_body"_a = false);
68  m.def(
69  "export_smtlib",
70  [&exportSMTLIB](MlirModule module, bool inlineSingleUseValues,
71  bool indentLetBody) {
72  return exportSMTLIB(mlirModuleGetOperation(module),
73  inlineSingleUseValues, indentLetBody);
74  },
75  "module"_a, "inline_single_use_values"_a = false,
76  "indent_let_body"_a = false);
77 }
78 
79 NB_MODULE(_mlirDialectsSMT, m) {
80  m.doc() = "MLIR SMT Dialect";
81 
83 }
NB_MODULE(_mlirDialectsSMT, m)
Definition: DialectSMT.cpp:79
void populateDialectSMTSubmodule(nanobind::module_ &m)
Definition: DialectSMT.cpp:27
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 bool mlirSMTTypeIsABool(MlirType type)
Checks if the given type is a smt::BoolType.
Definition: SMT.cpp:52
MLIR_CAPI_EXPORTED MlirType mlirSMTTypeGetBool(MlirContext ctx)
Creates a smt::BoolType.
Definition: SMT.cpp:54
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
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 MlirContext mlirOperationGetContext(MlirOperation op)
Gets the context this operation is associated with.
Definition: IR.cpp:639
MLIR_CAPI_EXPORTED MlirOperation mlirModuleGetOperation(MlirModule module)
Views the module as a generic operation.
Definition: IR.cpp:460
static bool mlirLogicalResultIsSuccess(MlirLogicalResult res)
Checks if the given logical result represents a success.
Definition: Support.h:122
MLIR_CAPI_EXPORTED MlirLogicalResult mlirTranslateOperationToSMTLIB(MlirOperation, MlirStringCallback, void *userData, bool inlineSingleUseValues, bool indentLetBody)
LogicalResult exportSMTLIB(Operation *module, llvm::raw_ostream &os, const SMTEmissionOptions &options=SMTEmissionOptions())
Run the ExportSMTLIB pass.
Include the generated interface declarations.
A logical result value, essentially a boolean with named states.
Definition: Support.h:116
Accumulates into a python string from a method that accepts an MlirStringCallback.
MlirStringCallback getCallback()