Loading [MathJax]/extensions/tex2jax.js
CIRCT 21.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RTGToolModule.cpp
Go to the documentation of this file.
1//===- RTGToolModule.cpp - RTG Tool API nanobind module -------------------===//
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 "CIRCTModules.h"
10
11#include "circt-c/RtgTool.h"
12
13#include "mlir/Bindings/Python/NanobindAdaptors.h"
14
15#include <nanobind/nanobind.h>
16namespace nb = nanobind;
17
18using namespace circt;
19
20namespace {
21/// Wrapper around an CirctRtgToolOptions.
22class PyRtgToolOptions {
23public:
24 PyRtgToolOptions(unsigned seed, CirctRtgToolOutputFormat outputFormat,
25 bool verifyPasses, bool verbosePassExecution,
26 const std::vector<std::string> &unsupportedInstructions,
27 const std::string &unsupportedInstructionsFile,
28 bool splitOutput, const std::string &outputPath,
29 bool memoriesAsImmediates)
30 : options(circtRtgToolOptionsCreateDefault(seed)) {
31 setOutputFormat(outputFormat);
32 setVerifyPasses(verifyPasses);
33 setVerbosePassExecution(verbosePassExecution);
34 setUnsupportedInstructions(unsupportedInstructions);
35 setUnsupportedInstructionsFile(unsupportedInstructionsFile);
36 setSplitOutput(splitOutput);
37 setOutputPath(outputPath);
38 setMemoriesAsImmediates(memoriesAsImmediates);
39 }
40 ~PyRtgToolOptions() { circtRtgToolOptionsDestroy(options); }
41
42 operator CirctRtgToolOptions() const { return options; }
43 CirctRtgToolOptions get() const { return options; }
44
45 void setOutputFormat(CirctRtgToolOutputFormat format) {
47 }
48
49 void setSeed(unsigned seed) { circtRtgToolOptionsSetSeed(options, seed); }
50
51 void setVerifyPasses(bool enable) {
53 }
54
55 void setVerbosePassExecution(bool enable) {
57 }
58
59 void
60 setUnsupportedInstructions(const std::vector<std::string> &instructions) {
62 options, instructions.size(),
63 reinterpret_cast<const void **>(
64 const_cast<std::string *>(instructions.data())));
65 }
66
67 void addUnsupportedInstruction(const std::string &instruction) {
68 circtRtgToolOptionsAddUnsupportedInstruction(options, instruction.c_str());
69 }
70
71 void setUnsupportedInstructionsFile(const std::string &filename) {
73 filename.c_str());
74 }
75
76 void setSplitOutput(bool enable) {
77 circtRtgToolOptionsSetSplitOutput(options, enable);
78 }
79
80 void setOutputPath(const std::string &path) {
81 circtRtgToolOptionsSetOutputPath(options, path.c_str());
82 }
83
84 void setMemoriesAsImmediates(bool enable) {
86 }
87
88private:
89 CirctRtgToolOptions options;
90};
91} // namespace
92
93/// Populate the rtgtool python module.
95 m.doc() = "RTGTool Python native extension";
96
97 nb::enum_<CirctRtgToolOutputFormat>(m, "OutputFormat")
99 .value("ELABORATED_MLIR", CIRCT_RTGTOOL_OUTPUT_FORMAT_ELABORATED_MLIR)
100 .value("ASM", CIRCT_RTGTOOL_OUTPUT_FORMAT_ASM);
101
102 nb::class_<PyRtgToolOptions>(m, "Options")
103 .def(nb::init<unsigned, CirctRtgToolOutputFormat, bool, bool,
104 const std::vector<std::string> &, const std::string &, bool,
105 const std::string &, bool>(),
106 nb::arg("seed"),
107 nb::arg("output_format") = CIRCT_RTGTOOL_OUTPUT_FORMAT_ASM,
108 nb::arg("verify_passes") = true,
109 nb::arg("verbose_pass_execution") = false,
110 nb::arg("unsupported_instructions") = std::vector<const char *>(),
111 nb::arg("unsupported_instructions_file") = "",
112 nb::arg("split_output") = false, nb::arg("output_path") = "",
113 nb::arg("memories_as_immediates") = true)
114 .def("set_output_format", &PyRtgToolOptions::setOutputFormat,
115 "Specify the output format of the tool", nb::arg("format"))
116 .def("set_seed", &PyRtgToolOptions::setSeed,
117 "Specify the seed for all RNGs used in the tool", nb::arg("seed"))
118 .def("set_verify_passes", &PyRtgToolOptions::setVerifyPasses,
119 "Specify whether the verifiers should be run after each pass",
120 nb::arg("enable"))
121 .def("set_verbose_pass_execution",
122 &PyRtgToolOptions::setVerbosePassExecution,
123 "Specify whether passes should run in verbose mode",
124 nb::arg("enable"))
125 .def("set_unsupported_instructions",
126 &PyRtgToolOptions::setUnsupportedInstructions,
127 "Set the list of of instructions unsupported by the assembler",
128 nb::arg("instructions"))
129 .def("add_unsupported_instruction",
130 &PyRtgToolOptions::addUnsupportedInstruction,
131 "Add the instruction given by name to the list of instructions not "
132 "supported by the assembler",
133 nb::arg("instruction_name"))
134 .def("set_unsupported_instructions_file",
135 &PyRtgToolOptions::setUnsupportedInstructionsFile,
136 "Register a file containing a comma-separated list of instruction "
137 "names which are not supported by the assembler.",
138 nb::arg("filename"))
139 .def("set_split_output", &PyRtgToolOptions::setSplitOutput,
140 "Determines whether each test should be emitted to a separate file.",
141 nb::arg("enable"))
142 .def("output_path", &PyRtgToolOptions::setOutputPath,
143 "The path of a file to be emitted to or a directory if "
144 "'split_output' is enabled.",
145 nb::arg("filename"))
146 .def("set_memories_as_immediates",
147 &PyRtgToolOptions::setMemoriesAsImmediates,
148 "Determines whether memories are lowered to immediates or labels.",
149 nb::arg("enable"));
150
151 m.def("populate_randomizer_pipeline",
152 [](MlirPassManager pm, const PyRtgToolOptions &options) {
153 circtRtgToolRandomizerPipeline(pm, options.get());
154 });
155}
MLIR_CAPI_EXPORTED void circtRtgToolOptionsAddUnsupportedInstruction(CirctRtgToolOptions options, const char *unsupportedInstruction)
Definition RtgTool.cpp:75
@ CIRCT_RTGTOOL_OUTPUT_FORMAT_ASM
Definition RtgTool.h:36
@ CIRCT_RTGTOOL_OUTPUT_FORMAT_ELABORATED_MLIR
Definition RtgTool.h:35
@ CIRCT_RTGTOOL_OUTPUT_FORMAT_MLIR
Definition RtgTool.h:34
MLIR_CAPI_EXPORTED void circtRtgToolOptionsSetVerifyPasses(CirctRtgToolOptions options, bool enable)
Definition RtgTool.cpp:55
MLIR_CAPI_EXPORTED void circtRtgToolOptionsSetOutputFormat(CirctRtgToolOptions options, CirctRtgToolOutputFormat format)
Definition RtgTool.cpp:33
MLIR_CAPI_EXPORTED void circtRtgToolOptionsSetUnsupportedInstructionsFile(CirctRtgToolOptions options, const char *filename)
Definition RtgTool.cpp:81
MLIR_CAPI_EXPORTED void circtRtgToolOptionsSetOutputPath(CirctRtgToolOptions options, const char *path)
Definition RtgTool.cpp:91
MLIR_CAPI_EXPORTED void circtRtgToolOptionsSetSplitOutput(CirctRtgToolOptions options, bool enable)
Definition RtgTool.cpp:86
MLIR_CAPI_EXPORTED void circtRtgToolOptionsDestroy(CirctRtgToolOptions options)
Definition RtgTool.cpp:29
MLIR_CAPI_EXPORTED void circtRtgToolOptionsSetSeed(CirctRtgToolOptions options, unsigned seed)
Definition RtgTool.cpp:51
MLIR_CAPI_EXPORTED void circtRtgToolOptionsSetVerbosePassExecution(CirctRtgToolOptions options, bool enable)
Definition RtgTool.cpp:60
MLIR_CAPI_EXPORTED void circtRtgToolRandomizerPipeline(MlirPassManager pm, CirctRtgToolOptions options)
Definition RtgTool.cpp:105
MLIR_CAPI_EXPORTED CirctRtgToolOptions circtRtgToolOptionsCreateDefault(unsigned seed)
Definition RtgTool.cpp:24
MLIR_CAPI_EXPORTED void circtRtgToolOptionsSetUnsupportedInstructions(CirctRtgToolOptions options, unsigned numInstr, const void **unsupportedInstructions)
Definition RtgTool.cpp:65
enum CiretRtgToolOutputFormat CirctRtgToolOutputFormat
MLIR_CAPI_EXPORTED void circtRtgToolOptionsSetMemoriesAsImmediates(CirctRtgToolOptions options, bool enable)
Definition RtgTool.cpp:96
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition CalyxOps.cpp:55
void populateDialectRTGToolSubmodule(nanobind::module_ &m)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.