Skip to content

Commit 4053aae

Browse files
committed
.
1 parent 8bf682d commit 4053aae

1 file changed

Lines changed: 14 additions & 34 deletions

File tree

python_bindings/src/halide/halide_/PyGenerator.cpp

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@ using Halide::Parameter;
1717
using Halide::Internal::ArgInfoDirection;
1818
using Halide::Internal::ArgInfoKind;
1919

20-
template<typename T>
21-
std::map<std::string, T> dict_to_map(const py::dict &dict) {
22-
_halide_user_assert(!dict.is(py::none()));
23-
std::map<std::string, T> m;
24-
for (auto it : dict) {
25-
m[it.first.cast<std::string>()] = it.second.cast<T>();
26-
}
27-
return m;
28-
}
29-
3020
class PyGeneratorBase : public AbstractGenerator {
3121
// The name declared in the Python function's decorator
3222
const std::string name_;
@@ -128,29 +118,6 @@ class PyGeneratorFactoryProvider : public GeneratorFactoryProvider {
128118
}
129119
};
130120

131-
// Returns a vector of mutable char * pointers corresponding to each string in `strs`.
132-
// `strs` must outlive the input and the pointers are not stable if the std::strings are mutated.
133-
// Arg (pun intended), this is all because generate_filter_main wants a mutable char **argv.
134-
std::vector<char *> get_mutable_c_strs(const std::vector<std::string> &strs) {
135-
std::vector<char *> c_strs;
136-
c_strs.reserve(strs.size());
137-
for (const auto &s : strs) {
138-
c_strs.push_back(const_cast<char *>(s.c_str()));
139-
}
140-
return c_strs;
141-
}
142-
143-
void generate_filter_main(const std::vector<std::string> &argv) {
144-
std::vector<char *> mutable_argv = get_mutable_c_strs(argv_copy);
145-
const int result = Halide::Internal::generate_filter_main((int)mutable_argv.size(), mutable_argv.data(), PyGeneratorFactoryProvider());
146-
if (result != 0) {
147-
// Some paths in generate_filter_main() will fail with user_error or similar (which throws an exception
148-
// due to how libHalide is built for Python), but some paths just return an error code. For consistency,
149-
// handle both by throwing a C++ exception, which pybind11 turns into a Python exception.
150-
throw std::runtime_error("Generator failed: " + std::to_string(result));
151-
}
152-
}
153-
154121
} // namespace
155122

156123
void define_generator(py::module &m) {
@@ -190,7 +157,20 @@ void define_generator(py::module &m) {
190157
return o.str();
191158
});
192159

193-
m.def("_generate_filter_main", &generate_filter_main, py::arg("argv"));
160+
m.def("_generate_filter_main", [](const std::vector<std::string> &argv) -> void {
161+
std::vector<char *> mutable_argv;
162+
mutable_argv.reserve(argv.size());
163+
for (auto &s : argv) {
164+
mutable_argv.push_back(const_cast<char *>(s.c_str()));
165+
}
166+
int result = Halide::Internal::generate_filter_main((int)mutable_argv.size(), mutable_argv.data(), PyGeneratorFactoryProvider());
167+
if (result != 0) {
168+
// Some paths in generate_filter_main() will fail with user_error or similar (which throws an exception
169+
// due to how libHalide is built for Python), but some paths just return an error code. For consistency,
170+
// handle both by throwing a C++ exception, which PyBind11 turns into a Python exception.
171+
throw std::runtime_error("Generator failed: " + std::to_string(result));
172+
},
173+
&generate_filter_main, py::arg("argv"));
194174

195175
m.def("_unique_name", []() -> std::string {
196176
return ::Halide::Internal::unique_name('p');

0 commit comments

Comments
 (0)