Halide 22.0.0
Halide compiler and libraries
Loading...
Searching...
No Matches
Generator.h
Go to the documentation of this file.
1#ifndef HALIDE_GENERATOR_H_
2#define HALIDE_GENERATOR_H_
3
4/** \file
5 *
6 * Generator is a class used to encapsulate the building of Funcs in user
7 * pipelines. A Generator is agnostic to JIT vs AOT compilation; it can be used for
8 * either purpose, but is especially convenient to use for AOT compilation.
9 *
10 * A Generator explicitly declares the Inputs and Outputs associated for a given
11 * pipeline, and (optionally) separates the code for constructing the outputs from the code from
12 * scheduling them. For instance:
13 *
14 * \code
15 * class Blur : public Generator<Blur> {
16 * public:
17 * Input<Func> input{"input", UInt(16), 2};
18 * Output<Func> output{"output", UInt(16), 2};
19 * void generate() {
20 * blur_x(x, y) = (input(x, y) + input(x+1, y) + input(x+2, y))/3;
21 * blur_y(x, y) = (blur_x(x, y) + blur_x(x, y+1) + blur_x(x, y+2))/3;
22 * output(x, y) = blur(x, y);
23 * }
24 * void schedule() {
25 * blur_y.split(y, y, yi, 8).parallel(y).vectorize(x, 8);
26 * blur_x.store_at(blur_y, y).compute_at(blur_y, yi).vectorize(x, 8);
27 * }
28 * private:
29 * Var x, y, xi, yi;
30 * Func blur_x, blur_y;
31 * };
32 * \endcode
33 *
34 * Halide can compile a Generator into the correct pipeline by introspecting these
35 * values and constructing an appropriate signature based on them.
36 *
37 * A Generator provides implementations of two methods:
38 *
39 * - generate(), which must fill in all Output Func(s); it may optionally also do scheduling
40 * if no schedule() method is present.
41 * - schedule(), which (if present) should contain all scheduling code.
42 *
43 * Inputs can be any C++ scalar type:
44 *
45 * \code
46 * Input<float> radius{"radius"};
47 * Input<int32_t> increment{"increment"};
48 * \endcode
49 *
50 * An Input<Func> is (essentially) like an ImageParam, except that it may (or may
51 * not) not be backed by an actual buffer, and thus has no defined extents.
52 *
53 * \code
54 * Input<Func> input{"input", Float(32), 2};
55 * \endcode
56 *
57 * You can optionally make the type and/or dimensions of Input<Func> unspecified,
58 * in which case the value is simply inferred from the actual Funcs passed to them.
59 * Of course, if you specify an explicit Type or Dimension, we still require the
60 * input Func to match, or a compilation error results.
61 *
62 * \code
63 * Input<Func> input{ "input", 3 }; // require 3-dimensional Func,
64 * // but leave Type unspecified
65 * \endcode
66 *
67 * A Generator must explicitly list the output(s) it produces:
68 *
69 * \code
70 * Output<Func> output{"output", Float(32), 2};
71 * \endcode
72 *
73 * You can specify an output that returns a Tuple by specifying a list of Types:
74 *
75 * \code
76 * class Tupler : Generator<Tupler> {
77 * Input<Func> input{"input", Int(32), 2};
78 * Output<Func> output{"output", {Float(32), UInt(8)}, 2};
79 * void generate() {
80 * Var x, y;
81 * Expr a = cast<float>(input(x, y));
82 * Expr b = cast<uint8_t>(input(x, y));
83 * output(x, y) = Tuple(a, b);
84 * }
85 * };
86 * \endcode
87 *
88 * You can also specify Output<X> for any scalar type (except for Handle types);
89 * this is merely syntactic sugar on top of a zero-dimensional Func, but can be
90 * quite handy, especially when used with multiple outputs:
91 *
92 * \code
93 * Output<float> sum{"sum"}; // equivalent to Output<Func> {"sum", Float(32), 0}
94 * \endcode
95 *
96 * As with Input<Func>, you can optionally make the type and/or dimensions of an
97 * Output<Func> unspecified; any unspecified types must be resolved via an
98 * implicit GeneratorParam in order to use top-level compilation.
99 *
100 * You can also declare an *array* of Input or Output, by using an array type
101 * as the type parameter:
102 *
103 * \code
104 * // Takes exactly 3 images and outputs exactly 3 sums.
105 * class SumRowsAndColumns : Generator<SumRowsAndColumns> {
106 * Input<Func[3]> inputs{"inputs", Float(32), 2};
107 * Input<int32_t[2]> extents{"extents"};
108 * Output<Func[3]> sums{"sums", Float(32), 1};
109 * void generate() {
110 * assert(inputs.size() == sums.size());
111 * // assume all inputs are same extent
112 * Expr width = extent[0];
113 * Expr height = extent[1];
114 * for (size_t i = 0; i < inputs.size(); ++i) {
115 * RDom r(0, width, 0, height);
116 * sums[i]() = 0.f;
117 * sums[i]() += inputs[i](r.x, r.y);
118 * }
119 * }
120 * };
121 * \endcode
122 *
123 * You can also leave array size unspecified, with some caveats:
124 * - For ahead-of-time compilation, Inputs must have a concrete size specified
125 * via a GeneratorParam at build time (e.g., pyramid.size=3)
126 * - For JIT compilation via a Stub, Inputs array sizes will be inferred
127 * from the vector passed.
128 * - For ahead-of-time compilation, Outputs may specify a concrete size
129 * via a GeneratorParam at build time (e.g., pyramid.size=3), or the
130 * size can be specified via a resize() method.
131 *
132 * \code
133 * class Pyramid : public Generator<Pyramid> {
134 * public:
135 * GeneratorParam<int32_t> levels{"levels", 10};
136 * Input<Func> input{ "input", Float(32), 2 };
137 * Output<Func[]> pyramid{ "pyramid", Float(32), 2 };
138 * void generate() {
139 * pyramid.resize(levels);
140 * pyramid[0](x, y) = input(x, y);
141 * for (int i = 1; i < pyramid.size(); i++) {
142 * pyramid[i](x, y) = (pyramid[i-1](2*x, 2*y) +
143 * pyramid[i-1](2*x+1, 2*y) +
144 * pyramid[i-1](2*x, 2*y+1) +
145 * pyramid[i-1](2*x+1, 2*y+1))/4;
146 * }
147 * }
148 * };
149 * \endcode
150 *
151 * A Generator can also be customized via compile-time parameters (GeneratorParams),
152 * which affect code generation.
153 *
154 * GeneratorParams, Inputs, and Outputs are (by convention) always
155 * public and always declared at the top of the Generator class, in the order
156 *
157 * \code
158 * GeneratorParam(s)
159 * Input<Func>(s)
160 * Input<non-Func>(s)
161 * Output<Func>(s)
162 * \endcode
163 *
164 * Note that the Inputs and Outputs will appear in the C function call in the order
165 * they are declared. All Input<Func> and Output<Func> are represented as halide_buffer_t;
166 * all other Input<> are the appropriate C++ scalar type. (GeneratorParams are
167 * always referenced by name, not position, so their order is irrelevant.)
168 *
169 * All Inputs and Outputs must have explicit names, and all such names must match
170 * the regex [A-Za-z][A-Za-z_0-9]* (i.e., essentially a C/C++ variable name, with
171 * some extra restrictions on underscore use). By convention, the name should match
172 * the member-variable name.
173 *
174 * You can dynamically add Inputs and Outputs to your Generator via adding a
175 * configure() method; if present, it will be called before generate(). It can
176 * examine GeneratorParams but it may not examine predeclared Inputs or Outputs;
177 * the only thing it should do is call add_input<>() and/or add_output<>(), or call
178 * set_type()/set_dimensions()/set_array_size() on an Input or Output with an unspecified type.
179 * Added inputs will be appended (in order) after predeclared Inputs but before
180 * any Outputs; added outputs will be appended after predeclared Outputs.
181 *
182 * Note that the pointers returned by add_input() and add_output() are owned
183 * by the Generator and will remain valid for the Generator's lifetime; user code
184 * should not attempt to delete or free them.
185 *
186 * \code
187 * class MultiSum : public Generator<MultiSum> {
188 * public:
189 * GeneratorParam<int32_t> input_count{"input_count", 10};
190 * Output<Func> output{ "output", Float(32), 2 };
191 *
192 * void configure() {
193 * for (int i = 0; i < input_count; ++i) {
194 * extra_inputs.push_back(
195 * add_input<Func>("input_" + std::to_string(i), Float(32), 2);
196 * }
197 * }
198 *
199 * void generate() {
200 * Expr sum = 0.f;
201 * for (int i = 0; i < input_count; ++i) {
202 * sum += (*extra_inputs)[i](x, y);
203 * }
204 * output(x, y) = sum;
205 * }
206 * private:
207 * std::vector<Input<Func>* extra_inputs;
208 * };
209 * \endcode
210 *
211 * All Generators have two GeneratorParams that are implicitly provided
212 * by the base class:
213 *
214 * GeneratorParam<Target> target{"target", Target()};
215 * GeneratorParam<AutoschedulerParams> autoscheduler{"autoscheduler", {}}
216 *
217 * - 'target' is the Halide::Target for which the Generator is producing code.
218 * It is read-only during the Generator's lifetime, and must not be modified;
219 * its value should always be filled in by the calling code: either the Halide
220 * build system (for ahead-of-time compilation), or ordinary C++ code
221 * (for JIT compilation).
222 * - 'autoscheduler' is a string-to-string map that is used to indicates whether
223 * and how an auto-scheduler should be run for this Generator:
224 * - if empty, the Generator should schedule its Funcs as it sees fit; no autoscheduler will be run.
225 * - if the 'name' key is set, it should be one of the known autoschedulers
226 * provided with this release of Halide, which will be used to schedule
227 * the Funcs in the Generator. In this case, the Generator should only
228 * provide estimate()s for its Funcs, and not call any other scheduling methods.
229 * - Other keys may be specified in the params, on a per-autoscheduler
230 * basis, to optimize or enhance the automatically-generated schedule.
231 * See documentation for each autoscheduler for options.
232 *
233 * Generators are added to a global registry to simplify AOT build mechanics; this
234 * is done by simply using the HALIDE_REGISTER_GENERATOR macro at global scope:
235 *
236 * \code
237 * HALIDE_REGISTER_GENERATOR(ExampleGen, jit_example)
238 * \endcode
239 *
240 * The registered name of the Generator is provided must match the same rules as
241 * Input names, above.
242 *
243 * Note that the class name of the generated Stub class will match the registered
244 * name by default; if you want to vary it (typically, to include namespaces),
245 * you can add it as an optional third argument:
246 *
247 * \code
248 * HALIDE_REGISTER_GENERATOR(ExampleGen, jit_example, SomeNamespace::JitExampleStub)
249 * \endcode
250 *
251 * Note that a Generator is always executed with a specific Target assigned to it,
252 * that you can access via the get_target() method. (You should *not* use the
253 * global get_target_from_environment(), etc. methods provided in Target.h)
254 *
255 * (Note that there are older variations of Generator that differ from what's
256 * documented above; these are still supported but not described here. See
257 * https://github.com/halide/Halide/wiki/Old-Generator-Documentation for
258 * more information.)
259 */
260
261#include <algorithm>
262#include <functional>
263#include <iterator>
264#include <limits>
265#include <memory>
266#include <mutex>
267#include <set>
268#include <sstream>
269#include <string>
270#include <type_traits>
271#include <utility>
272#include <vector>
273
274#include "AbstractGenerator.h"
275#include "Func.h"
276#include "ImageParam.h"
278#include "Target.h"
279
280#if !(__cplusplus >= 201703L || _MSVC_LANG >= 201703L)
281#error "Halide requires C++17 or later; please upgrade your compiler."
282#endif
283
284namespace Halide {
285
286class GeneratorContext;
287
288namespace Internal {
289
291
292class GeneratorBase;
293
294std::vector<Expr> parameter_constraints(const Parameter &p);
295
296template<typename T>
297HALIDE_NO_USER_CODE_INLINE std::string enum_to_string(const std::map<std::string, T> &enum_map, const T &t) {
298 for (const auto &key_value : enum_map) {
299 if (t == key_value.second) {
300 return key_value.first;
301 }
302 }
303 user_error << "Enumeration value not found.\n";
304 return "";
305}
306
307template<typename T>
308T enum_from_string(const std::map<std::string, T> &enum_map, const std::string &s) {
309 auto it = enum_map.find(s);
310 user_assert(it != enum_map.end()) << "Enumeration value not found: " << s << "\n";
311 return it->second;
312}
313
314extern const std::map<std::string, Halide::Type> &get_halide_type_enum_map();
315inline std::string halide_type_to_enum_string(const Type &t) {
317}
318
319// Convert a Halide Type into a string representation of its C source.
320// e.g., Int(32) -> "Halide::Int(32)"
321std::string halide_type_to_c_source(const Type &t);
322
323// Convert a Halide Type into a string representation of its C Source.
324// e.g., Int(32) -> "int32_t"
325std::string halide_type_to_c_type(const Type &t);
326
327/** GeneratorFactoryProvider provides a way to customize the Generators
328 * that are visible to generate_filter_main (which otherwise would just
329 * look at the global registry of C++ Generators). */
331public:
333 virtual ~GeneratorFactoryProvider() = default;
334
335 /** Return a list of all registered Generators that are available for use
336 * with the create() method. */
337 virtual std::vector<std::string> enumerate() const = 0;
338
339 /** Create an instance of the Generator that is registered under the given
340 * name. If the name isn't one returned by enumerate(), return nullptr
341 * rather than assert-fail; caller must check for a valid result. */
342 virtual AbstractGeneratorPtr create(const std::string &name,
343 const Halide::GeneratorContext &context) const = 0;
344
349};
350
351/** Return a GeneratorFactoryProvider that knows about all the currently-registered C++ Generators. */
353
354/** generate_filter_main() is a convenient wrapper for GeneratorRegistry::create() +
355 * compile_to_files(); it can be trivially wrapped by a "real" main() to produce a
356 * command-line utility for ahead-of-time filter compilation. */
357int generate_filter_main(int argc, char **argv);
358
359/** This overload of generate_filter_main lets you provide your own provider for how to enumerate and/or create
360 * the generators based on registration name; this is useful if you want to re-use the
361 * 'main' logic but avoid the global Generator registry (e.g. for bindings in languages
362 * other than C++). */
364
365// select_type<> is to std::conditional as switch is to if:
366// it allows a multiway compile-time type definition via the form
367//
368// select_type<cond<condition1, type1>,
369// cond<condition2, type2>,
370// ....
371// cond<conditionN, typeN>>::type
372//
373// Note that the conditions are evaluated in order; the first evaluating to true
374// is chosen.
375//
376// Note that if no conditions evaluate to true, the resulting type is illegal
377// and will produce a compilation error. (You can provide a default by simply
378// using cond<true, SomeType> as the final entry.)
379template<bool B, typename T>
380struct cond {
381 static constexpr bool value = B;
382 using type = T;
383};
384
385template<typename First, typename... Rest>
386struct select_type : std::conditional<First::value, typename First::type, typename select_type<Rest...>::type> {};
387
388template<typename First>
390 using type = std::conditional_t<First::value, typename First::type, void>;
391};
392
393template<typename... Args>
394using select_type_t = typename select_type<Args...>::type;
395
397
399public:
400 explicit GeneratorParamBase(const std::string &name);
402
403 const std::string &name() const {
404 return name_;
405 }
406
407 // overload the set() function to call the right virtual method based on type.
408 // This allows us to attempt to set a GeneratorParam via a
409 // plain C++ type, even if we don't know the specific templated
410 // subclass. Attempting to set the wrong type will assert.
411 // Notice that there is no typed setter for Enums, for obvious reasons;
412 // setting enums in an unknown type must fallback to using set_from_string.
413 //
414 // It's always a bit iffy to use macros for this, but IMHO it clarifies the situation here.
415#define HALIDE_GENERATOR_PARAM_TYPED_SETTER(TYPE) \
416 virtual void set(const TYPE &new_value) = 0;
417
430 HALIDE_GENERATOR_PARAM_TYPED_SETTER(AutoschedulerParams)
433
434#undef HALIDE_GENERATOR_PARAM_TYPED_SETTER
435
436 // Add overloads for string and char*
437 void set(const std::string &new_value) {
439 }
440 void set(const char *new_value) {
441 set_from_string(std::string(new_value));
442 }
443
444protected:
445 friend class GeneratorBase;
446 friend class GeneratorParamInfo;
447 friend class StubEmitter;
448
451
452 // All GeneratorParams are settable from string.
453 virtual void set_from_string(const std::string &value_string) = 0;
454
455 virtual std::string call_to_string(const std::string &v) const = 0;
456 virtual std::string get_c_type() const = 0;
457
458 virtual std::string get_type_decls() const {
459 return "";
460 }
461
462 virtual std::string get_default_value() const = 0;
463
464 virtual bool is_synthetic_param() const {
465 return false;
466 }
467
468 virtual bool is_looplevel_param() const {
469 return false;
470 }
471
472 void fail_wrong_type(const char *type);
473
474private:
475 const std::string name_;
476
477 // Generator which owns this GeneratorParam. Note that this will be null
478 // initially; the GeneratorBase itself will set this field when it initially
479 // builds its info about params. However, since it (generally) isn't
480 // appropriate for GeneratorParam<> to be declared outside of a Generator,
481 // all reasonable non-testing code should expect this to be non-null.
482 GeneratorBase *generator{nullptr};
483
484public:
489};
490
491// This is strictly some syntactic sugar to suppress certain compiler warnings.
492template<typename FROM, typename TO>
493struct Convert {
494 template<typename TO2 = TO, std::enable_if_t<!std::is_same_v<TO2, bool>> * = nullptr>
495 static TO2 value(const FROM &from) {
496 return static_cast<TO2>(from);
497 }
498
499 template<typename TO2 = TO, std::enable_if_t<std::is_same_v<TO2, bool>> * = nullptr>
500 static TO2 value(const FROM &from) {
501 return from != 0;
502 }
503};
504
505template<typename T>
507public:
508 using type = T;
509
510 GeneratorParamImpl(const std::string &name, const T &value)
512 }
513
514 T value() const {
515 this->check_value_readable();
516 return value_;
517 }
518
519 operator T() const {
520 return this->value();
521 }
522
523 operator Expr() const {
524 return make_const(type_of<T>(), this->value());
525 }
526
527#define HALIDE_GENERATOR_PARAM_TYPED_SETTER(TYPE) \
528 void set(const TYPE &new_value) override { \
529 typed_setter_impl<TYPE>(new_value, #TYPE); \
530 }
531
547
548#undef HALIDE_GENERATOR_PARAM_TYPED_SETTER
549
550 // Overload for std::string.
551 void set(const std::string &new_value) {
554 }
555
556protected:
557 virtual void set_impl(const T &new_value) {
560 }
561
562 // Needs to be protected to allow GeneratorParam<LoopLevel>::set() override
564
565private:
566 // If FROM->T is not legal, fail
567 template<typename FROM, std::enable_if_t<!std::is_convertible_v<FROM, T>> * = nullptr>
568 HALIDE_ALWAYS_INLINE void typed_setter_impl(const FROM &, const char *msg) {
569 fail_wrong_type(msg);
570 }
571
572 // If FROM and T are identical, just assign
573 template<typename FROM, std::enable_if_t<std::is_same_v<FROM, T>> * = nullptr>
574 HALIDE_ALWAYS_INLINE void typed_setter_impl(const FROM &value, const char *msg) {
576 value_ = value;
577 }
578
579 // If both FROM->T and T->FROM are legal, ensure it's lossless
580 template<typename FROM, std::enable_if_t<
581 !std::is_same_v<FROM, T> &&
582 std::is_convertible_v<FROM, T> &&
583 std::is_convertible_v<T, FROM>> * = nullptr>
584 HALIDE_ALWAYS_INLINE void typed_setter_impl(const FROM &value, const char *msg) {
586 const T t = Convert<FROM, T>::value(value);
588 if (value2 != value) {
589 fail_wrong_type(msg);
590 }
591 value_ = t;
592 }
593
594 // If FROM->T is legal but T->FROM is not, just assign
595 template<typename FROM, std::enable_if_t<
596 !std::is_same_v<FROM, T> &&
597 std::is_convertible_v<FROM, T> &&
598 !std::is_convertible_v<T, FROM>> * = nullptr>
599 HALIDE_ALWAYS_INLINE void typed_setter_impl(const FROM &value, const char *msg) {
601 value_ = value;
602 }
603};
604
605// Stubs for type-specific implementations of GeneratorParam, to avoid
606// many complex enable_if<> statements that were formerly spread through the
607// implementation. Note that not all of these need to be templated classes,
608// (e.g. for GeneratorParam_Target, T == Target always), but are declared
609// that way for symmetry of declaration.
610template<typename T>
612public:
613 GeneratorParam_Target(const std::string &name, const T &value)
615 }
616
617 void set_from_string(const std::string &new_value_string) override {
619 }
620
621 std::string get_default_value() const override {
622 return this->value().to_string();
623 }
624
625 std::string call_to_string(const std::string &v) const override {
626 std::ostringstream oss;
627 oss << v << ".to_string()";
628 return oss.str();
629 }
630
631 std::string get_c_type() const override {
632 return "Target";
633 }
634};
635
636class GeneratorParam_AutoSchedulerParams : public GeneratorParamImpl<AutoschedulerParams> {
637public:
639
640 void set_from_string(const std::string &new_value_string) override;
641 std::string get_default_value() const override;
642 std::string call_to_string(const std::string &v) const override;
643 std::string get_c_type() const override;
644
645private:
646 friend class GeneratorBase;
647
648 bool try_set(const std::string &key, const std::string &value);
649};
650
652public:
656
658
659 void set(const LoopLevel &value) override {
660 // Don't call check_value_writable(): It's OK to set a LoopLevel after generate().
661 // check_value_writable();
662
663 // This looks odd, but is deliberate:
664
665 // First, mutate the existing contents to match the value passed in,
666 // so that any existing usage of the LoopLevel now uses the newer value.
667 // (Strictly speaking, this is really only necessary if this method
668 // is called after generate(): before generate(), there is no usage
669 // to be concerned with.)
671
672 // Then, reset the value itself so that it points to the same LoopLevelContents
673 // as the value passed in. (Strictly speaking, this is really only
674 // useful if this method is called before generate(): afterwards, it's
675 // too late to alter the code to refer to a different LoopLevelContents.)
676 value_ = value;
677 }
678
679 void set_from_string(const std::string &new_value_string) override {
680 if (new_value_string == "root") {
681 this->set(LoopLevel::root());
682 } else if (new_value_string == "inlined") {
683 this->set(LoopLevel::inlined());
684 } else {
685 user_error << "Unable to parse " << this->name() << ": " << new_value_string;
686 }
687 }
688
689 std::string get_default_value() const override {
690 // This is dodgy but safe in this case: we want to
691 // see what the value of our LoopLevel is *right now*,
692 // so we make a copy and lock the copy so we can inspect it.
693 // (Note that ordinarily this is a bad idea, since LoopLevels
694 // can be mutated later on; however, this method is only
695 // called by the Generator infrastructure, on LoopLevels that
696 // will never be mutated, so this is really just an elaborate way
697 // to avoid runtime assertions.)
698 LoopLevel copy;
699 copy.set(this->value());
700 copy.lock();
701 if (copy.is_inlined()) {
702 return "LoopLevel::inlined()";
703 } else if (copy.is_root()) {
704 return "LoopLevel::root()";
705 } else {
707 return "";
708 }
709 }
710
711 std::string call_to_string(const std::string &v) const override {
713 return std::string();
714 }
715
716 std::string get_c_type() const override {
717 return "LoopLevel";
718 }
719
720 bool is_looplevel_param() const override {
721 return true;
722 }
723};
724
725template<typename T>
727public:
728 GeneratorParam_Arithmetic(const std::string &name,
729 const T &value,
730 const T &min = std::numeric_limits<T>::lowest(),
731 const T &max = std::numeric_limits<T>::max())
732 : GeneratorParamImpl<T>(name, value), min(min), max(max) {
733 // call set() to ensure value is clamped to min/max
734 this->set(value);
735 }
736
737 void set_impl(const T &new_value) override {
738 user_assert(new_value >= min && new_value <= max) << "Value out of range: " << new_value;
740 }
741
742 void set_from_string(const std::string &new_value_string) override {
743 std::istringstream iss(new_value_string);
744 T t;
745 // All one-byte ints int8 and uint8 should be parsed as integers, not chars --
746 // including 'char' itself. (Note that sizeof(bool) is often-but-not-always-1,
747 // so be sure to exclude that case.)
748 if (sizeof(T) == sizeof(char) && !std::is_same_v<T, bool>) {
749 int i;
750 iss >> i;
751 t = (T)i;
752 } else {
753 iss >> t;
754 }
755 user_assert(!iss.fail() && iss.get() == EOF) << "Unable to parse: " << new_value_string;
756 this->set(t);
757 }
758
759 std::string get_default_value() const override {
760 std::ostringstream oss;
761 oss << this->value();
762 if (std::is_same_v<T, float>) {
763 // If the constant has no decimal point ("1")
764 // we must append one before appending "f"
765 if (oss.str().find('.') == std::string::npos) {
766 oss << ".";
767 }
768 oss << "f";
769 }
770 return oss.str();
771 }
772
773 std::string call_to_string(const std::string &v) const override {
774 std::ostringstream oss;
775 oss << "std::to_string(" << v << ")";
776 return oss.str();
777 }
778
779 std::string get_c_type() const override {
780 std::ostringstream oss;
781 if (std::is_same_v<T, float>) {
782 return "float";
783 } else if (std::is_same_v<T, double>) {
784 return "double";
785 } else if (std::is_integral_v<T>) {
786 if (std::is_unsigned_v<T>) {
787 oss << "u";
788 }
789 oss << "int" << (sizeof(T) * 8) << "_t";
790 return oss.str();
791 } else {
792 user_error << "Unknown arithmetic type\n";
793 return "";
794 }
795 }
796
797private:
798 const T min, max;
799};
800
801template<typename T>
803public:
804 GeneratorParam_Bool(const std::string &name, const T &value)
806 }
807
808 void set_from_string(const std::string &new_value_string) override {
809 bool v = false;
810 if (new_value_string == "true" || new_value_string == "True") {
811 v = true;
812 } else if (new_value_string == "false" || new_value_string == "False") {
813 v = false;
814 } else {
815 user_assert(false) << "Unable to parse bool: " << new_value_string;
816 }
817 this->set(v);
818 }
819
820 std::string get_default_value() const override {
821 return this->value() ? "true" : "false";
822 }
823
824 std::string call_to_string(const std::string &v) const override {
825 std::ostringstream oss;
826 oss << "std::string((" << v << ") ? \"true\" : \"false\")";
827 return oss.str();
828 }
829
830 std::string get_c_type() const override {
831 return "bool";
832 }
833};
834
835template<typename T>
837public:
838 GeneratorParam_Enum(const std::string &name, const T &value, const std::map<std::string, T> &enum_map)
839 : GeneratorParamImpl<T>(name, value), enum_map(enum_map) {
840 }
841
842 // define a "set" that takes our specific enum (but don't hide the inherited virtual functions)
844
845 template<typename T2 = T, std::enable_if_t<!std::is_same_v<T2, Type>> * = nullptr>
846 void set(const T &e) {
847 this->set_impl(e);
848 }
849
850 void set_from_string(const std::string &new_value_string) override {
851 auto it = enum_map.find(new_value_string);
852 user_assert(it != enum_map.end()) << "Enumeration value not found: " << new_value_string;
853 this->set_impl(it->second);
854 }
855
856 std::string call_to_string(const std::string &v) const override {
857 return "Enum_" + this->name() + "_map().at(" + v + ")";
858 }
859
860 std::string get_c_type() const override {
861 return "Enum_" + this->name();
862 }
863
864 std::string get_default_value() const override {
865 return "Enum_" + this->name() + "::" + enum_to_string(enum_map, this->value());
866 }
867
868 std::string get_type_decls() const override {
869 std::ostringstream oss;
870 oss << "enum class Enum_" << this->name() << " {\n";
871 for (const auto &key_value : enum_map) {
872 oss << " " << key_value.first << ",\n";
873 }
874 oss << "};\n";
875 oss << "\n";
876
877 // TODO: since we generate the enums, we could probably just use a vector (or array!) rather than a map,
878 // since we can ensure that the enum values are a nice tight range.
879 oss << "inline HALIDE_NO_USER_CODE_INLINE const std::map<Enum_" << this->name() << ", std::string>& Enum_" << this->name() << "_map() {\n";
880 oss << " static const std::map<Enum_" << this->name() << ", std::string> m = {\n";
881 for (const auto &key_value : enum_map) {
882 oss << " { Enum_" << this->name() << "::" << key_value.first << ", \"" << key_value.first << "\"},\n";
883 }
884 oss << " };\n";
885 oss << " return m;\n";
886 oss << "};\n";
887 return oss.str();
888 }
889
890private:
891 const std::map<std::string, T> enum_map;
892};
893
894template<typename T>
896public:
897 GeneratorParam_Type(const std::string &name, const T &value)
899 }
900
901 std::string call_to_string(const std::string &v) const override {
902 return "Halide::Internal::halide_type_to_enum_string(" + v + ")";
903 }
904
905 std::string get_c_type() const override {
906 return "Type";
907 }
908
909 std::string get_default_value() const override {
910 return halide_type_to_c_source(this->value());
911 }
912
913 std::string get_type_decls() const override {
914 return "";
915 }
916};
917
918template<typename T>
920public:
921 GeneratorParam_String(const std::string &name, const std::string &value)
923 }
924 void set_from_string(const std::string &new_value_string) override {
925 this->set(new_value_string);
926 }
927
928 std::string get_default_value() const override {
929 return "\"" + this->value() + "\"";
930 }
931
932 std::string call_to_string(const std::string &v) const override {
933 return v;
934 }
935
936 std::string get_c_type() const override {
937 return "std::string";
938 }
939};
940
941template<typename T>
951
952} // namespace Internal
953
954/** GeneratorParam is a templated class that can be used to modify the behavior
955 * of the Generator at code-generation time. GeneratorParams are commonly
956 * specified in build files (e.g. Makefile) to customize the behavior of
957 * a given Generator, thus they have a very constrained set of types to allow
958 * for efficient specification via command-line flags. A GeneratorParam can be:
959 * - any float or int type.
960 * - bool
961 * - enum
962 * - Halide::Target
963 * - Halide::Type
964 * - std::string
965 * Please don't use std::string unless there's no way to do what you want with some
966 * other type; in particular, don't use this if you can use enum instead.
967 * All GeneratorParams have a default value. Arithmetic types can also
968 * optionally specify min and max. Enum types must specify a string-to-value
969 * map.
970 *
971 * Halide::Type is treated as though it were an enum, with the mappings:
972 *
973 * "int8" Halide::Int(8)
974 * "int16" Halide::Int(16)
975 * "int32" Halide::Int(32)
976 * "int64" Halide::Int(64)
977 * "uint8" Halide::UInt(8)
978 * "uint16" Halide::UInt(16)
979 * "uint32" Halide::UInt(32)
980 * "uint64" Halide::UInt(64)
981 * "float16" Halide::Float(16)
982 * "float32" Halide::Float(32)
983 * "float64" Halide::Float(64)
984 * "bfloat16" Halide::BFloat(16)
985 *
986 * No vector Types are currently supported by this mapping.
987 *
988 */
989template<typename T>
991public:
992 template<typename T2 = T, std::enable_if_t<!std::is_same_v<T2, std::string>> * = nullptr>
993 GeneratorParam(const std::string &name, const T &value)
994 : Internal::GeneratorParamImplBase<T>(name, value) {
995 }
996
997 GeneratorParam(const std::string &name, const T &value, const T &min, const T &max)
998 : Internal::GeneratorParamImplBase<T>(name, value, min, max) {
999 }
1000
1001 GeneratorParam(const std::string &name, const T &value, const std::map<std::string, T> &enum_map)
1002 : Internal::GeneratorParamImplBase<T>(name, value, enum_map) {
1003 }
1004
1005 GeneratorParam(const std::string &name, const std::string &value)
1006 : Internal::GeneratorParamImplBase<T>(name, value) {
1007 }
1008};
1009
1010/** Addition between GeneratorParam<T> and any type that supports operator+ with T.
1011 * Returns type of underlying operator+. */
1012// @{
1013template<typename Other, typename T>
1014auto operator+(const Other &a, const GeneratorParam<T> &b) -> decltype(a + (T)b) {
1015 return a + (T)b;
1016}
1017template<typename Other, typename T>
1018auto operator+(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a + b) {
1019 return (T)a + b;
1020}
1021// @}
1022
1023/** Subtraction between GeneratorParam<T> and any type that supports operator- with T.
1024 * Returns type of underlying operator-. */
1025// @{
1026template<typename Other, typename T>
1027auto operator-(const Other &a, const GeneratorParam<T> &b) -> decltype(a - (T)b) {
1028 return a - (T)b;
1029}
1030template<typename Other, typename T>
1031auto operator-(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a - b) {
1032 return (T)a - b;
1033}
1034// @}
1035
1036/** Multiplication between GeneratorParam<T> and any type that supports operator* with T.
1037 * Returns type of underlying operator*. */
1038// @{
1039template<typename Other, typename T>
1040auto operator*(const Other &a, const GeneratorParam<T> &b) -> decltype(a * (T)b) {
1041 return a * (T)b;
1042}
1043template<typename Other, typename T>
1044auto operator*(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a * b) {
1045 return (T)a * b;
1046}
1047// @}
1048
1049/** Division between GeneratorParam<T> and any type that supports operator/ with T.
1050 * Returns type of underlying operator/. */
1051// @{
1052template<typename Other, typename T>
1053auto operator/(const Other &a, const GeneratorParam<T> &b) -> decltype(a / (T)b) {
1054 return a / (T)b;
1055}
1056template<typename Other, typename T>
1057auto operator/(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a / b) {
1058 return (T)a / b;
1059}
1060// @}
1061
1062/** Modulo between GeneratorParam<T> and any type that supports operator% with T.
1063 * Returns type of underlying operator%. */
1064// @{
1065template<typename Other, typename T>
1066auto operator%(const Other &a, const GeneratorParam<T> &b) -> decltype(a % (T)b) {
1067 return a % (T)b;
1068}
1069template<typename Other, typename T>
1070auto operator%(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a % b) {
1071 return (T)a % b;
1072}
1073// @}
1074
1075/** Greater than comparison between GeneratorParam<T> and any type that supports operator> with T.
1076 * Returns type of underlying operator>. */
1077// @{
1078template<typename Other, typename T>
1079auto operator>(const Other &a, const GeneratorParam<T> &b) -> decltype(a > (T)b) {
1080 return a > (T)b;
1081}
1082template<typename Other, typename T>
1083auto operator>(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a > b) {
1084 return (T)a > b;
1085}
1086// @}
1087
1088/** Less than comparison between GeneratorParam<T> and any type that supports operator< with T.
1089 * Returns type of underlying operator<. */
1090// @{
1091template<typename Other, typename T>
1092auto operator<(const Other &a, const GeneratorParam<T> &b) -> decltype(a < (T)b) {
1093 return a < (T)b;
1094}
1095template<typename Other, typename T>
1096auto operator<(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a < b) {
1097 return (T)a < b;
1098}
1099// @}
1100
1101/** Greater than or equal comparison between GeneratorParam<T> and any type that supports operator>= with T.
1102 * Returns type of underlying operator>=. */
1103// @{
1104template<typename Other, typename T>
1105auto operator>=(const Other &a, const GeneratorParam<T> &b) -> decltype(a >= (T)b) {
1106 return a >= (T)b;
1107}
1108template<typename Other, typename T>
1109auto operator>=(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a >= b) {
1110 return (T)a >= b;
1111}
1112// @}
1113
1114/** Less than or equal comparison between GeneratorParam<T> and any type that supports operator<= with T.
1115 * Returns type of underlying operator<=. */
1116// @{
1117template<typename Other, typename T>
1118auto operator<=(const Other &a, const GeneratorParam<T> &b) -> decltype(a <= (T)b) {
1119 return a <= (T)b;
1120}
1121template<typename Other, typename T>
1122auto operator<=(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a <= b) {
1123 return (T)a <= b;
1124}
1125// @}
1126
1127/** Equality comparison between GeneratorParam<T> and any type that supports operator== with T.
1128 * Returns type of underlying operator==. */
1129// @{
1130template<typename Other, typename T>
1131auto operator==(const Other &a, const GeneratorParam<T> &b) -> decltype(a == (T)b) {
1132 return a == (T)b;
1133}
1134template<typename Other, typename T>
1135auto operator==(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a == b) {
1136 return (T)a == b;
1137}
1138// @}
1139
1140/** Inequality comparison between between GeneratorParam<T> and any type that supports operator!= with T.
1141 * Returns type of underlying operator!=. */
1142// @{
1143template<typename Other, typename T>
1144auto operator!=(const Other &a, const GeneratorParam<T> &b) -> decltype(a != (T)b) {
1145 return a != (T)b;
1146}
1147template<typename Other, typename T>
1148auto operator!=(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a != b) {
1149 return (T)a != b;
1150}
1151// @}
1152
1153/** Logical and between between GeneratorParam<T> and any type that supports operator&& with T.
1154 * Returns type of underlying operator&&. */
1155// @{
1156template<typename Other, typename T>
1157auto operator&&(const Other &a, const GeneratorParam<T> &b) -> decltype(a && (T)b) {
1158 return a && (T)b;
1159}
1160template<typename Other, typename T>
1161auto operator&&(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a && b) {
1162 return (T)a && b;
1163}
1164template<typename T>
1165auto operator&&(const GeneratorParam<T> &a, const GeneratorParam<T> &b) -> decltype((T)a && (T)b) {
1166 return (T)a && (T)b;
1167}
1168// @}
1169
1170/** Logical or between between GeneratorParam<T> and any type that supports operator|| with T.
1171 * Returns type of underlying operator||. */
1172// @{
1173template<typename Other, typename T>
1174auto operator||(const Other &a, const GeneratorParam<T> &b) -> decltype(a || (T)b) {
1175 return a || (T)b;
1176}
1177template<typename Other, typename T>
1178auto operator||(const GeneratorParam<T> &a, const Other &b) -> decltype((T)a || b) {
1179 return (T)a || b;
1180}
1181template<typename T>
1182auto operator||(const GeneratorParam<T> &a, const GeneratorParam<T> &b) -> decltype((T)a || (T)b) {
1183 return (T)a || (T)b;
1184}
1185// @}
1186
1187/* min and max are tricky as the language support for these is in the std
1188 * namespace. In order to make this work, forwarding functions are used that
1189 * are declared in a namespace that has std::min and std::max in scope.
1190 */
1191namespace Internal {
1192namespace GeneratorMinMax {
1193
1194using std::max;
1195using std::min;
1196
1197template<typename Other, typename T>
1198auto min_forward(const Other &a, const GeneratorParam<T> &b) -> decltype(min(a, (T)b)) {
1199 return min(a, (T)b);
1200}
1201template<typename Other, typename T>
1202auto min_forward(const GeneratorParam<T> &a, const Other &b) -> decltype(min((T)a, b)) {
1203 return min((T)a, b);
1204}
1205
1206template<typename Other, typename T>
1207auto max_forward(const Other &a, const GeneratorParam<T> &b) -> decltype(max(a, (T)b)) {
1208 return max(a, (T)b);
1209}
1210template<typename Other, typename T>
1211auto max_forward(const GeneratorParam<T> &a, const Other &b) -> decltype(max((T)a, b)) {
1212 return max((T)a, b);
1213}
1214
1215} // namespace GeneratorMinMax
1216} // namespace Internal
1217
1218/** Compute minimum between GeneratorParam<T> and any type that supports min with T.
1219 * Will automatically import std::min. Returns type of underlying min call. */
1220// @{
1221template<typename Other, typename T>
1222auto min(const Other &a, const GeneratorParam<T> &b) -> decltype(Internal::GeneratorMinMax::min_forward(a, b)) {
1224}
1225template<typename Other, typename T>
1226auto min(const GeneratorParam<T> &a, const Other &b) -> decltype(Internal::GeneratorMinMax::min_forward(a, b)) {
1228}
1229// @}
1230
1231/** Compute the maximum value between GeneratorParam<T> and any type that supports max with T.
1232 * Will automatically import std::max. Returns type of underlying max call. */
1233// @{
1234template<typename Other, typename T>
1235auto max(const Other &a, const GeneratorParam<T> &b) -> decltype(Internal::GeneratorMinMax::max_forward(a, b)) {
1237}
1238template<typename Other, typename T>
1239auto max(const GeneratorParam<T> &a, const Other &b) -> decltype(Internal::GeneratorMinMax::max_forward(a, b)) {
1241}
1242// @}
1243
1244/** Not operator for GeneratorParam */
1245template<typename T>
1246auto operator!(const GeneratorParam<T> &a) -> decltype(!(T)a) {
1247 return !(T)a;
1248}
1249
1250namespace Internal {
1251
1252template<typename T2>
1253class GeneratorInput_Buffer;
1254
1255/**
1256 * StubInputBuffer is the placeholder that a Stub uses when it requires
1257 * a Buffer for an input (rather than merely a Func or Expr). It is constructed
1258 * to allow only two possible sorts of input:
1259 * -- Assignment of an Input<Buffer<>>, with compatible type and dimensions,
1260 * essentially allowing us to pipe a parameter from an enclosing Generator to an internal Stub.
1261 * -- Assignment of a Buffer<>, with compatible type and dimensions,
1262 * causing the Input<Buffer<>> to become a precompiled buffer in the generated code.
1263 */
1266 friend class StubInput;
1267 template<typename T2>
1269 template<typename T2, int D2>
1270 friend class StubInputBuffer;
1271
1272 Parameter parameter_;
1273
1275 : parameter_(p) {
1276 // Create an empty 1-element buffer with the right runtime typing and dimensions,
1277 // which we'll use only to pass to can_convert_from() to verify this
1278 // Parameter is compatible with our constraints.
1279 Buffer<> other(p.type(), nullptr, std::vector<int>(p.dimensions(), 1));
1281 }
1282
1283 template<typename T2, int D2>
1284 HALIDE_NO_USER_CODE_INLINE static Parameter parameter_from_buffer(const Buffer<T2, D2> &b) {
1287 Parameter p(b.type(), true, b.dimensions());
1288 p.set_buffer(b);
1289 return p;
1290 }
1291
1292public:
1293 StubInputBuffer() = default;
1294
1295 // *not* explicit -- this ctor should only be used when you want
1296 // to pass a literal Buffer<> for a Stub Input; this Buffer<> will be
1297 // compiled into the Generator's product, rather than becoming
1298 // a runtime Parameter.
1299 template<typename T2, int D2>
1301 : parameter_(parameter_from_buffer(b)) {
1302 }
1303
1304 template<typename T2>
1305 static std::vector<Parameter> to_parameter_vector(const StubInputBuffer<T2> &t) {
1306 return {t.parameter_};
1307 }
1308
1309 template<typename T2>
1310 static std::vector<Parameter> to_parameter_vector(const std::vector<StubInputBuffer<T2>> &v) {
1311 std::vector<Parameter> r;
1312 r.reserve(v.size());
1313 for (const auto &s : v) {
1314 r.push_back(s.parameter_);
1315 }
1316 return r;
1317 }
1318};
1319
1320class AbstractGenerator;
1321
1323protected:
1325 std::shared_ptr<AbstractGenerator> generator;
1326
1328
1330 explicit StubOutputBufferBase(const Func &f, const std::shared_ptr<AbstractGenerator> &generator);
1331
1332public:
1333 Realization realize(std::vector<int32_t> sizes);
1334
1335 template<typename... Args>
1337 return f.realize(std::forward<Args>(args)..., get_target());
1338 }
1339
1340 template<typename Dst>
1341 void realize(Dst dst) {
1342 f.realize(dst, get_target());
1343 }
1344};
1345
1346/**
1347 * StubOutputBuffer is the placeholder that a Stub uses when it requires
1348 * a Buffer for an output (rather than merely a Func). It is constructed
1349 * to allow only two possible sorts of things:
1350 * -- Assignment to an Output<Buffer<>>, with compatible type and dimensions,
1351 * essentially allowing us to pipe a parameter from the result of a Stub to an
1352 * enclosing Generator
1353 * -- Realization into a Buffer<>; this is useful only in JIT compilation modes
1354 * (and shouldn't be usable otherwise)
1355 *
1356 * It is deliberate that StubOutputBuffer is not (easily) convertible to Func.
1357 */
1358template<typename T = void>
1360 template<typename T2>
1362 explicit StubOutputBuffer(const Func &fn, const std::shared_ptr<AbstractGenerator> &gen)
1363 : StubOutputBufferBase(fn, gen) {
1364 }
1365
1366public:
1367 StubOutputBuffer() = default;
1368
1369 static std::vector<StubOutputBuffer<T>> to_output_buffers(const std::vector<Func> &v,
1370 const std::shared_ptr<AbstractGenerator> &gen) {
1371 std::vector<StubOutputBuffer<T>> result;
1372 for (const Func &f : v) {
1373 result.push_back(StubOutputBuffer<T>(f, gen));
1374 }
1375 return result;
1376 }
1377};
1378
1379// This is a union-like class that allows for convenient initialization of Stub Inputs
1380// via initializer-list syntax; it is only used in situations where the
1381// downstream consumer will be able to explicitly check that each value is
1382// of the expected/required kind.
1384 const ArgInfoKind kind_;
1385 // Exactly one of the following fields should be defined:
1386 const Parameter parameter_;
1387 const Func func_;
1388 const Expr expr_;
1389
1390public:
1391 // *not* explicit.
1392 template<typename T2>
1394 : kind_(ArgInfoKind::Buffer), parameter_(b.parameter_), func_(), expr_() {
1395 }
1397 : kind_(ArgInfoKind::Buffer), parameter_(p), func_(), expr_() {
1398 }
1399 StubInput(const Func &f)
1400 : kind_(ArgInfoKind::Function), parameter_(), func_(f), expr_() {
1401 }
1402 StubInput(const Expr &e)
1403 : kind_(ArgInfoKind::Scalar), parameter_(), func_(), expr_(e) {
1404 }
1405
1407 return kind_;
1408 }
1409
1412 return parameter_;
1413 }
1414
1415 Func func() const {
1417 return func_;
1418 }
1419
1420 Expr expr() const {
1422 return expr_;
1423 }
1424};
1425
1426/** GIOBase is the base class for all GeneratorInput<> and GeneratorOutput<>
1427 * instantiations; it is not part of the public API and should never be
1428 * used directly by user code.
1429 *
1430 * Every GIOBase instance can be either a single value or an array-of-values;
1431 * each of these values can be an Expr or a Func. (Note that for an
1432 * array-of-values, the types/dimensions of all values in the array must match.)
1433 *
1434 * A GIOBase can have multiple Types, in which case it represents a Tuple.
1435 * (Note that Tuples are currently only supported for GeneratorOutput, but
1436 * it is likely that GeneratorInput will be extended to support Tuple as well.)
1437 *
1438 * The array-size, type(s), and dimensions can all be left "unspecified" at
1439 * creation time, in which case they may assume values provided by a Stub.
1440 * (It is important to note that attempting to use a GIOBase with unspecified
1441 * values will assert-fail; you must ensure that all unspecified values are
1442 * filled in prior to use.)
1443 */
1444class GIOBase {
1445public:
1446 virtual ~GIOBase() = default;
1447
1448 // These should only be called from configure() methods.
1449 // TODO: find a way to enforce this. Better yet, find a way to remove these.
1450 void set_type(const Type &type);
1452 void set_array_size(int size);
1453
1454protected:
1456 size_t array_size() const;
1457 virtual bool is_array() const;
1458
1459 const std::string &name() const;
1461
1462 bool gio_types_defined() const;
1463 const std::vector<Type> &gio_types() const;
1465
1466 bool dims_defined() const;
1467 int dims() const;
1468
1469 const std::vector<Func> &funcs() const;
1470 const std::vector<Expr> &exprs() const;
1471
1473 const std::string &name,
1475 const std::vector<Type> &types,
1476 int dims);
1477
1478 friend class GeneratorBase;
1480
1481 mutable int array_size_; // always 1 if is_array() == false.
1482 // -1 if is_array() == true but unspecified.
1483
1484 const std::string name_;
1486 mutable std::vector<Type> types_; // empty if type is unspecified
1487 mutable int dims_; // -1 if dim is unspecified
1488
1489 // Exactly one of these will have nonzero length
1490 std::vector<Func> funcs_;
1491 std::vector<Expr> exprs_;
1492
1493 // Generator which owns this Input or Output. Note that this will be null
1494 // initially; the GeneratorBase itself will set this field when it initially
1495 // builds its info about params. However, since it isn't
1496 // appropriate for Input<> or Output<> to be declared outside of a Generator,
1497 // all reasonable non-testing code should expect this to be non-null.
1499
1500 std::string array_name(size_t i) const;
1501
1502 virtual void verify_internals();
1503
1504 void check_matching_array_size(size_t size) const;
1505 void check_matching_types(const std::vector<Type> &t) const;
1506 void check_matching_dims(int d) const;
1507
1508 template<typename ElemType>
1509 const std::vector<ElemType> &get_values() const;
1510
1511 void check_gio_access() const;
1512
1513 virtual void check_value_writable() const = 0;
1514
1515 virtual const char *input_or_output() const = 0;
1516
1517private:
1518 template<typename T>
1520 friend class GeneratorStub;
1521
1522public:
1523 GIOBase(const GIOBase &) = delete;
1524 GIOBase &operator=(const GIOBase &) = delete;
1525 GIOBase(GIOBase &&) = delete;
1527};
1528
1529template<>
1530inline const std::vector<Expr> &GIOBase::get_values<Expr>() const {
1531 return exprs();
1532}
1533
1534template<>
1535inline const std::vector<Func> &GIOBase::get_values<Func>() const {
1536 return funcs();
1537}
1538
1540protected:
1542 const std::string &name,
1544 const std::vector<Type> &t,
1545 int d);
1546
1547 GeneratorInputBase(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d);
1548
1549 friend class GeneratorBase;
1551
1552 std::vector<Parameter> parameters_;
1553
1555
1557 void set_inputs(const std::vector<StubInput> &inputs);
1558 bool inputs_set = false;
1559
1560 virtual void set_def_min_max();
1561
1562 void verify_internals() override;
1563
1564 friend class StubEmitter;
1565
1566 virtual std::string get_c_type() const = 0;
1567
1568 void check_value_writable() const override;
1569
1570 const char *input_or_output() const override {
1571 return "Input";
1572 }
1573
1574 void set_estimate_impl(const Var &var, const Expr &min, const Expr &extent);
1575 void set_estimates_impl(const Region &estimates);
1576
1577public:
1579};
1580
1581template<typename T, typename ValueType>
1583protected:
1584 using TBase = std::remove_all_extents_t<T>;
1585
1586 bool is_array() const override {
1587 return std::is_array_v<T>;
1588 }
1589
1590 template<typename T2 = T, std::enable_if_t<
1591 // Only allow T2 not-an-array
1592 !std::is_array_v<T2>> * = nullptr>
1593 GeneratorInputImpl(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d)
1594 : GeneratorInputBase(name, kind, t, d) {
1595 }
1596
1597 template<typename T2 = T, std::enable_if_t<
1598 // Only allow T2[kSomeConst]
1599 std::is_array_v<T2> && std::rank_v<T2> == 1 && (std::extent_v<T2, 0> > 0)> * = nullptr>
1600 GeneratorInputImpl(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d)
1601 : GeneratorInputBase(std::extent_v<T2, 0>, name, kind, t, d) {
1602 }
1603
1604 template<typename T2 = T, std::enable_if_t<
1605 // Only allow T2[]
1606 std::is_array_v<T2> && std::rank_v<T2> == 1 && std::extent_v<T2, 0> == 0> * = nullptr>
1607 GeneratorInputImpl(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d)
1608 : GeneratorInputBase(-1, name, kind, t, d) {
1609 }
1610
1611public:
1612 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
1613 size_t size() const {
1614 this->check_gio_access();
1615 return get_values<ValueType>().size();
1616 }
1617
1618 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
1619 const ValueType &operator[](size_t i) const {
1620 this->check_gio_access();
1621 return get_values<ValueType>()[i];
1622 }
1623
1624 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
1625 const ValueType &at(size_t i) const {
1626 this->check_gio_access();
1627 return get_values<ValueType>().at(i);
1628 }
1629
1630 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
1631 typename std::vector<ValueType>::const_iterator begin() const {
1632 this->check_gio_access();
1633 return get_values<ValueType>().begin();
1634 }
1635
1636 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
1637 typename std::vector<ValueType>::const_iterator end() const {
1638 this->check_gio_access();
1639 return get_values<ValueType>().end();
1640 }
1641};
1642
1643// When forwarding methods to ImageParam, Func, etc., we must take
1644// care with the return types: many of the methods return a reference-to-self
1645// (e.g., ImageParam&); since we create temporaries for most of these forwards,
1646// returning a ref will crater because it refers to a now-defunct section of the
1647// stack. Happily, simply removing the reference is solves this, since all of the
1648// types in question satisfy the property of copies referring to the same underlying
1649// structure (returning references is just an optimization). Since this is verbose
1650// and used in several places, we'll use a helper macro:
1651#define HALIDE_FORWARD_METHOD(Class, Method) \
1652 template<typename... Args> \
1653 inline auto Method(Args &&...args) -> std::remove_reference_t<decltype(std::declval<Class>().Method(std::forward<Args>(args)...))> { \
1654 return this->template as<Class>().Method(std::forward<Args>(args)...); \
1655 }
1656
1657#define HALIDE_FORWARD_METHOD_CONST(Class, Method) \
1658 template<typename... Args> \
1659 inline auto Method(Args &&...args) const -> std::remove_reference_t<decltype(std::declval<Class>().Method(std::forward<Args>(args)...))> { \
1660 this->check_gio_access(); \
1661 return this->template as<Class>().Method(std::forward<Args>(args)...); \
1662 }
1663
1664template<typename T>
1666private:
1668
1669protected:
1670 using TBase = typename Super::TBase;
1671
1672 friend class ::Halide::Func;
1673 friend class ::Halide::Stage;
1674
1675 std::string get_c_type() const override {
1676 if (TBase::has_static_halide_type) {
1677 return "Halide::Internal::StubInputBuffer<" +
1678 halide_type_to_c_type(TBase::static_halide_type()) +
1679 ">";
1680 } else {
1681 return "Halide::Internal::StubInputBuffer<>";
1682 }
1683 }
1684
1685 template<typename T2>
1686 T2 as() const {
1687 return (T2) * this;
1688 }
1689
1690public:
1691 explicit GeneratorInput_Buffer(const std::string &name)
1693 TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
1694 TBase::has_static_dimensions ? TBase::static_dimensions() : -1) {
1695 }
1696
1697 GeneratorInput_Buffer(const std::string &name, const Type &t, int d)
1698 : Super(name, ArgInfoKind::Buffer, {t}, d) {
1699 static_assert(!TBase::has_static_halide_type, "You can only specify a Type argument for Input<Buffer<T>> if T is void or omitted.");
1700 static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Input<Buffer<T, D>> if D is -1 or omitted.");
1701 }
1702
1703 GeneratorInput_Buffer(const std::string &name, const Type &t)
1704 : Super(name, ArgInfoKind::Buffer, {t}, -1) {
1705 static_assert(!TBase::has_static_halide_type, "You can only specify a Type argument for Input<Buffer<T>> if T is void or omitted.");
1706 }
1707
1708 GeneratorInput_Buffer(const std::string &name, int d)
1710 TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
1711 d) {
1712 static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Input<Buffer<T, D>> if D is -1 or omitted.");
1713 }
1714
1715 template<typename... Args>
1716 Expr operator()(Args &&...args) const {
1717 this->check_gio_access();
1718 return Func(*this)(std::forward<Args>(args)...);
1719 }
1720
1721 Expr operator()(std::vector<Expr> args) const {
1722 this->check_gio_access();
1723 return Func(*this)(std::move(args));
1724 }
1725
1726 template<typename T2>
1727 operator StubInputBuffer<T2>() const {
1728 user_assert(!this->is_array()) << "Cannot assign an array type to a non-array type for Input " << this->name();
1729 return StubInputBuffer<T2>(this->parameters_.at(0));
1730 }
1731
1732 operator Func() const {
1733 this->check_gio_access();
1734 return this->funcs().at(0);
1735 }
1736
1737 operator ExternFuncArgument() const {
1738 this->check_gio_access();
1739 return ExternFuncArgument(this->parameters_.at(0));
1740 }
1741
1743 this->check_gio_access();
1744 this->set_estimate_impl(var, min, extent);
1745 return *this;
1746 }
1747
1749 this->check_gio_access();
1750 this->set_estimates_impl(estimates);
1751 return *this;
1752 }
1753
1755 this->check_gio_access();
1756 return Func(*this).in();
1757 }
1758
1759 Func in(const Func &other) {
1760 this->check_gio_access();
1761 return Func(*this).in(other);
1762 }
1763
1764 Func in(const std::vector<Func> &others) {
1765 this->check_gio_access();
1766 return Func(*this).in(others);
1767 }
1768
1769 operator ImageParam() const {
1770 this->check_gio_access();
1771 user_assert(!this->is_array()) << "Cannot convert an Input<Buffer<>[]> to an ImageParam; use an explicit subscript operator: " << this->name();
1772 return ImageParam(this->parameters_.at(0), Func(*this));
1773 }
1774
1775 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
1776 size_t size() const {
1777 this->check_gio_access();
1778 return this->parameters_.size();
1779 }
1780
1781 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
1782 ImageParam operator[](size_t i) const {
1783 this->check_gio_access();
1784 return ImageParam(this->parameters_.at(i), this->funcs().at(i));
1785 }
1786
1787 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
1788 ImageParam at(size_t i) const {
1789 this->check_gio_access();
1790 return ImageParam(this->parameters_.at(i), this->funcs().at(i));
1791 }
1792
1793 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
1794 std::vector<ImageParam>::const_iterator begin() const {
1795 user_error << "Input<Buffer<>>::begin() is not supported.";
1796 return {};
1797 }
1798
1799 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
1800 std::vector<ImageParam>::const_iterator end() const {
1801 user_error << "Input<Buffer<>>::end() is not supported.";
1802 return {};
1803 }
1804
1805 /** Forward methods to the ImageParam. */
1806 // @{
1810 HALIDE_FORWARD_METHOD(ImageParam, set_host_alignment)
1823 // }@
1824};
1825
1826template<typename T>
1828private:
1830
1831protected:
1832 using TBase = typename Super::TBase;
1833
1834 std::string get_c_type() const override {
1835 return "Func";
1836 }
1837
1838 template<typename T2>
1839 T2 as() const {
1840 return (T2) * this;
1841 }
1842
1843public:
1844 GeneratorInput_Func(const std::string &name, const Type &t, int d)
1845 : Super(name, ArgInfoKind::Function, {t}, d) {
1846 }
1847
1848 // unspecified type
1849 GeneratorInput_Func(const std::string &name, int d)
1850 : Super(name, ArgInfoKind::Function, {}, d) {
1851 }
1852
1853 // unspecified dimension
1854 GeneratorInput_Func(const std::string &name, const Type &t)
1855 : Super(name, ArgInfoKind::Function, {t}, -1) {
1856 }
1857
1858 // unspecified type & dimension
1859 explicit GeneratorInput_Func(const std::string &name)
1860 : Super(name, ArgInfoKind::Function, {}, -1) {
1861 }
1862
1863 GeneratorInput_Func(size_t array_size, const std::string &name, const Type &t, int d)
1864 : Super(array_size, name, ArgInfoKind::Function, {t}, d) {
1865 }
1866
1867 // unspecified type
1868 GeneratorInput_Func(size_t array_size, const std::string &name, int d)
1870 }
1871
1872 // unspecified dimension
1873 GeneratorInput_Func(size_t array_size, const std::string &name, const Type &t)
1874 : Super(array_size, name, ArgInfoKind::Function, {t}, -1) {
1875 }
1876
1877 // unspecified type & dimension
1878 GeneratorInput_Func(size_t array_size, const std::string &name)
1879 : Super(array_size, name, ArgInfoKind::Function, {}, -1) {
1880 }
1881
1882 template<typename... Args>
1883 Expr operator()(Args &&...args) const {
1884 this->check_gio_access();
1885 return this->funcs().at(0)(std::forward<Args>(args)...);
1886 }
1887
1888 Expr operator()(const std::vector<Expr> &args) const {
1889 this->check_gio_access();
1890 return this->funcs().at(0)(args);
1891 }
1892
1893 operator Func() const {
1894 this->check_gio_access();
1895 return this->funcs().at(0);
1896 }
1897
1898 operator ExternFuncArgument() const {
1899 this->check_gio_access();
1900 return ExternFuncArgument(this->parameters_.at(0));
1901 }
1902
1904 this->check_gio_access();
1905 this->set_estimate_impl(var, min, extent);
1906 return *this;
1907 }
1908
1910 this->check_gio_access();
1911 this->set_estimates_impl(estimates);
1912 return *this;
1913 }
1914
1916 this->check_gio_access();
1917 return Func(*this).in();
1918 }
1919
1920 Func in(const Func &other) {
1921 this->check_gio_access();
1922 return Func(*this).in(other);
1923 }
1924
1925 Func in(const std::vector<Func> &others) {
1926 this->check_gio_access();
1927 return Func(*this).in(others);
1928 }
1929
1930 /** Forward const methods to the underlying Func. (Non-const methods
1931 * aren't available for Input<Func>.) */
1932 // @{
1936 HALIDE_FORWARD_METHOD_CONST(Func, has_update_definition)
1937 HALIDE_FORWARD_METHOD_CONST(Func, num_update_definitions)
1942 HALIDE_FORWARD_METHOD_CONST(Func, update_args)
1943 HALIDE_FORWARD_METHOD_CONST(Func, update_value)
1944 HALIDE_FORWARD_METHOD_CONST(Func, update_values)
1947 // }@
1948};
1949
1950template<typename T>
1952private:
1954
1955 static_assert(std::is_same_v<std::remove_all_extents_t<T>, Expr>, "GeneratorInput_DynamicScalar is only legal to use with T=Expr for now");
1956
1957protected:
1958 std::string get_c_type() const override {
1959 return "Expr";
1960 }
1961
1962public:
1963 explicit GeneratorInput_DynamicScalar(const std::string &name)
1964 : Super(name, ArgInfoKind::Scalar, {}, 0) {
1965 user_assert(!std::is_array_v<T>) << "Input<Expr[]> is not allowed";
1966 }
1967
1968 /** You can use this Input as an expression in a halide
1969 * function definition */
1970 operator Expr() const {
1971 this->check_gio_access();
1972 return this->exprs().at(0);
1973 }
1974
1975 /** Using an Input as the argument to an external stage treats it
1976 * as an Expr */
1977 operator ExternFuncArgument() const {
1978 this->check_gio_access();
1979 return ExternFuncArgument(this->exprs().at(0));
1980 }
1981
1982 void set_estimate(const Expr &value) {
1983 this->check_gio_access();
1984 for (Parameter &p : this->parameters_) {
1985 p.set_estimate(value);
1986 }
1987 }
1988
1989 Type type() const {
1990 return Expr(*this).type();
1991 }
1992};
1993
1994template<typename T>
1996private:
1998
1999protected:
2000 using TBase = typename Super::TBase;
2001
2002 const TBase def_{TBase()};
2004
2005 void set_def_min_max() override {
2006 for (Parameter &p : this->parameters_) {
2007 // No: we want to leave the Parameter unset here.
2008 // p.set_scalar<TBase>(def_);
2010 }
2011 }
2012
2013 std::string get_c_type() const override {
2014 return "Expr";
2015 }
2016
2017 // Expr() doesn't accept a pointer type in its ctor; add a SFINAE adapter
2018 // so that pointer (aka handle) Inputs will get cast to uint64.
2019 template<typename TBase2 = TBase, std::enable_if_t<!std::is_pointer_v<TBase2>> * = nullptr>
2020 static Expr TBaseToExpr(const TBase2 &value) {
2021 return cast<TBase>(Expr(value));
2022 }
2023
2024 template<typename TBase2 = TBase, std::enable_if_t<std::is_pointer_v<TBase2>> * = nullptr>
2025 static Expr TBaseToExpr(const TBase2 &value) {
2026 user_assert(value == 0) << "Zero is the only legal default value for Inputs which are pointer types.\n";
2027 return Expr();
2028 }
2029
2030public:
2031 explicit GeneratorInput_Scalar(const std::string &name)
2032 : Super(name, ArgInfoKind::Scalar, {type_of<TBase>()}, 0), def_(static_cast<TBase>(0)), def_expr_(Expr()) {
2033 }
2034
2035 GeneratorInput_Scalar(const std::string &name, const TBase &def)
2037 }
2038
2040 const std::string &name)
2041 : Super(array_size, name, ArgInfoKind::Scalar, {type_of<TBase>()}, 0), def_(static_cast<TBase>(0)), def_expr_(Expr()) {
2042 }
2043
2045 const std::string &name,
2046 const TBase &def)
2048 }
2049
2050 /** You can use this Input as an expression in a halide
2051 * function definition */
2052 operator Expr() const {
2053 this->check_gio_access();
2054 return this->exprs().at(0);
2055 }
2056
2057 /** Using an Input as the argument to an external stage treats it
2058 * as an Expr */
2059 operator ExternFuncArgument() const {
2060 this->check_gio_access();
2061 return ExternFuncArgument(this->exprs().at(0));
2062 }
2063
2064 template<typename T2 = T, std::enable_if_t<std::is_pointer_v<T2>> * = nullptr>
2065 void set_estimate(const TBase &value) {
2066 this->check_gio_access();
2067 user_assert(value == nullptr) << "nullptr is the only valid estimate for Input<PointerType>";
2069 for (Parameter &p : this->parameters_) {
2070 p.set_estimate(e);
2071 }
2072 }
2073
2074 template<typename T2 = T, std::enable_if_t<!std::is_array_v<T2> && !std::is_pointer_v<T2>> * = nullptr>
2075 void set_estimate(const TBase &value) {
2076 this->check_gio_access();
2077 Expr e = Expr(value);
2078 if (std::is_same_v<T2, bool>) {
2079 e = cast<bool>(e);
2080 }
2081 for (Parameter &p : this->parameters_) {
2082 p.set_estimate(e);
2083 }
2084 }
2085
2086 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
2087 void set_estimate(size_t index, const TBase &value) {
2088 this->check_gio_access();
2089 Expr e = Expr(value);
2090 if (std::is_same_v<T2, bool>) {
2091 e = cast<bool>(e);
2092 }
2093 this->parameters_.at(index).set_estimate(e);
2094 }
2095
2096 Type type() const {
2097 return Expr(*this).type();
2098 }
2099};
2100
2101template<typename T>
2103private:
2105
2106protected:
2107 using TBase = typename Super::TBase;
2108
2110
2111 void set_def_min_max() override {
2113 // Don't set min/max for bool
2114 if (!std::is_same_v<TBase, bool>) {
2115 for (Parameter &p : this->parameters_) {
2116 if (min_.defined()) {
2117 p.set_min_value(min_);
2118 }
2119 if (max_.defined()) {
2120 p.set_max_value(max_);
2121 }
2122 }
2123 }
2124 }
2125
2126public:
2127 explicit GeneratorInput_Arithmetic(const std::string &name)
2128 : Super(name), min_(Expr()), max_(Expr()) {
2129 }
2130
2132 const TBase &def)
2133 : Super(name, def), min_(Expr()), max_(Expr()) {
2134 }
2135
2137 const std::string &name)
2138 : Super(array_size, name), min_(Expr()), max_(Expr()) {
2139 }
2140
2142 const std::string &name,
2143 const TBase &def)
2144 : Super(array_size, name, def), min_(Expr()), max_(Expr()) {
2145 }
2146
2148 const TBase &def,
2149 const TBase &min,
2150 const TBase &max)
2151 : Super(name, def), min_(min), max_(max) {
2152 }
2153
2155 const std::string &name,
2156 const TBase &def,
2157 const TBase &min,
2158 const TBase &max)
2159 : Super(array_size, name, def), min_(min), max_(max) {
2160 }
2161};
2162
2163template<typename>
2164using type_sink_t = void;
2165
2166template<typename T2, typename = void>
2167struct has_static_halide_type_method : std::false_type {};
2168
2169template<typename T2>
2170struct has_static_halide_type_method<T2, type_sink_t<decltype(T2::static_halide_type())>> : std::true_type {};
2171
2172template<typename... Args>
2174
2175template<typename T, typename TBase = std::remove_all_extents_t<T>>
2183
2184} // namespace Internal
2185
2186template<typename T>
2188private:
2190
2191protected:
2192 using TBase = typename Super::TBase;
2193
2194 // Trick to avoid ambiguous ctor between Func-with-dim and int-with-default-value;
2195 // since we can't use std::enable_if on ctors, define the argument to be one that
2196 // can only be properly resolved for TBase=Func.
2197 struct Unused;
2203
2204public:
2205 // Mark all of these explicit (not just single-arg versions) so that
2206 // we disallow copy-list-initialization form (i.e., Input foo{"foo"} is ok,
2207 // but Input foo = {"foo"} is not).
2208 explicit GeneratorInput(const std::string &name)
2209 : Super(name) {
2210 }
2211
2212 explicit GeneratorInput(const std::string &name, const TBase &def)
2213 : Super(name, def) {
2214 }
2215
2216 explicit GeneratorInput(size_t array_size, const std::string &name, const TBase &def)
2217 : Super(array_size, name, def) {
2218 }
2219
2220 explicit GeneratorInput(const std::string &name,
2221 const TBase &def, const TBase &min, const TBase &max)
2222 : Super(name, def, min, max) {
2223 }
2224
2225 explicit GeneratorInput(size_t array_size, const std::string &name,
2226 const TBase &def, const TBase &min, const TBase &max)
2227 : Super(array_size, name, def, min, max) {
2228 }
2229
2230 explicit GeneratorInput(const std::string &name, const Type &t, int d)
2231 : Super(name, t, d) {
2232 }
2233
2234 explicit GeneratorInput(const std::string &name, const Type &t)
2235 : Super(name, t) {
2236 }
2237
2238 // Avoid ambiguity between Func-with-dim and int-with-default
2239 explicit GeneratorInput(const std::string &name, IntIfNonScalar d)
2240 : Super(name, d) {
2241 }
2242
2243 explicit GeneratorInput(size_t array_size, const std::string &name, const Type &t, int d)
2244 : Super(array_size, name, t, d) {
2245 }
2246
2247 explicit GeneratorInput(size_t array_size, const std::string &name, const Type &t)
2248 : Super(array_size, name, t) {
2249 }
2250
2251 // Avoid ambiguity between Func-with-dim and int-with-default
2252 // template <typename T2 = T, std::enable_if_t<std::is_same_v<TBase, Func>> * = nullptr>
2253 explicit GeneratorInput(size_t array_size, const std::string &name, IntIfNonScalar d)
2254 : Super(array_size, name, d) {
2255 }
2256
2257 explicit GeneratorInput(size_t array_size, const std::string &name)
2258 : Super(array_size, name) {
2259 }
2260};
2261
2262namespace Internal {
2263
2265protected:
2266 template<typename T2, std::enable_if_t<std::is_same_v<T2, Func>> * = nullptr>
2268 static_assert(std::is_same_v<T2, Func>, "Only Func allowed here");
2270 internal_assert(exprs_.empty());
2271 user_assert(!funcs_.empty()) << "No funcs_ are defined yet";
2272 user_assert(funcs_.size() == 1) << "Use [] to access individual Funcs in Output<Func[]>";
2273 return funcs_[0];
2274 }
2275
2276public:
2277 /** Forward schedule-related methods to the underlying Func. */
2278 // @{
2279 HALIDE_FORWARD_METHOD(Func, add_trace_tag)
2280 HALIDE_FORWARD_METHOD(Func, align_bounds)
2281 HALIDE_FORWARD_METHOD(Func, align_extent)
2282 HALIDE_FORWARD_METHOD(Func, align_storage)
2283 HALIDE_FORWARD_METHOD(Func, always_partition)
2284 HALIDE_FORWARD_METHOD(Func, always_partition_all)
2287 HALIDE_FORWARD_METHOD(Func, bound_extent)
2288 HALIDE_FORWARD_METHOD(Func, compute_at)
2289 HALIDE_FORWARD_METHOD(Func, compute_inline)
2290 HALIDE_FORWARD_METHOD(Func, compute_root)
2291 HALIDE_FORWARD_METHOD(Func, compute_with)
2292 HALIDE_FORWARD_METHOD(Func, copy_to_device)
2293 HALIDE_FORWARD_METHOD(Func, copy_to_host)
2294 HALIDE_FORWARD_METHOD(Func, define_extern)
2297 HALIDE_FORWARD_METHOD(Func, fold_storage)
2300 HALIDE_FORWARD_METHOD(Func, gpu_blocks)
2301 HALIDE_FORWARD_METHOD(Func, gpu_single_thread)
2302 HALIDE_FORWARD_METHOD(Func, gpu_threads)
2303 HALIDE_FORWARD_METHOD(Func, gpu_tile)
2304 HALIDE_FORWARD_METHOD_CONST(Func, has_update_definition)
2305 HALIDE_FORWARD_METHOD(Func, hexagon)
2307 HALIDE_FORWARD_METHOD(Func, memoize)
2308 HALIDE_FORWARD_METHOD(Func, never_partition)
2309 HALIDE_FORWARD_METHOD(Func, never_partition_all)
2310 HALIDE_FORWARD_METHOD_CONST(Func, num_update_definitions)
2312 HALIDE_FORWARD_METHOD(Func, parallel)
2313 HALIDE_FORWARD_METHOD(Func, partition)
2314 HALIDE_FORWARD_METHOD(Func, prefetch)
2317 HALIDE_FORWARD_METHOD(Func, reorder)
2318 HALIDE_FORWARD_METHOD(Func, reorder_storage)
2321 HALIDE_FORWARD_METHOD(Func, set_estimate)
2322 HALIDE_FORWARD_METHOD(Func, specialize)
2323 HALIDE_FORWARD_METHOD(Func, specialize_fail)
2325 HALIDE_FORWARD_METHOD(Func, store_at)
2326 HALIDE_FORWARD_METHOD(Func, store_root)
2328 HALIDE_FORWARD_METHOD(Func, trace_stores)
2333 HALIDE_FORWARD_METHOD_CONST(Func, update_args)
2334 HALIDE_FORWARD_METHOD_CONST(Func, update_value)
2335 HALIDE_FORWARD_METHOD_CONST(Func, update_values)
2338 HALIDE_FORWARD_METHOD(Func, vectorize)
2339
2340 // }@
2341
2342#undef HALIDE_OUTPUT_FORWARD
2343#undef HALIDE_OUTPUT_FORWARD_CONST
2344
2345 using GIOBase::set_type;
2346
2347 /** Set types dynamically for tuple outputs. */
2348 void set_type(const std::vector<Type> &types);
2349
2350protected:
2352 const std::string &name,
2354 const std::vector<Type> &t,
2355 int d);
2356
2357 GeneratorOutputBase(const std::string &name,
2359 const std::vector<Type> &t,
2360 int d);
2361
2362 friend class GeneratorBase;
2363 friend class StubEmitter;
2364
2366 void resize(size_t size);
2367
2368 virtual std::string get_c_type() const {
2369 return "Func";
2370 }
2371
2372 void check_value_writable() const override;
2373
2374 const char *input_or_output() const override {
2375 return "Output";
2376 }
2377
2378public:
2380};
2381
2382template<typename T>
2384protected:
2385 using TBase = std::remove_all_extents_t<T>;
2387
2388 bool is_array() const override {
2389 return std::is_array_v<T>;
2390 }
2391
2392 template<typename T2 = T, std::enable_if_t<
2393 // Only allow T2 not-an-array
2394 !std::is_array_v<T2>> * = nullptr>
2395 GeneratorOutputImpl(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d)
2396 : GeneratorOutputBase(name, kind, t, d) {
2397 }
2398
2399 template<typename T2 = T, std::enable_if_t<
2400 // Only allow T2[kSomeConst]
2401 std::is_array_v<T2> && std::rank_v<T2> == 1 && (std::extent_v<T2, 0> > 0)> * = nullptr>
2402 GeneratorOutputImpl(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d)
2403 : GeneratorOutputBase(std::extent_v<T2, 0>, name, kind, t, d) {
2404 }
2405
2406 template<typename T2 = T, std::enable_if_t<
2407 // Only allow T2[]
2408 std::is_array_v<T2> && std::rank_v<T2> == 1 && std::extent_v<T2, 0> == 0> * = nullptr>
2409 GeneratorOutputImpl(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d)
2410 : GeneratorOutputBase(-1, name, kind, t, d) {
2411 }
2412
2413public:
2414 template<typename... Args, typename T2 = T, std::enable_if_t<!std::is_array_v<T2>> * = nullptr>
2415 FuncRef operator()(Args &&...args) const {
2416 this->check_gio_access();
2417 return get_values<ValueType>().at(0)(std::forward<Args>(args)...);
2418 }
2419
2420 template<typename ExprOrVar, typename T2 = T, std::enable_if_t<!std::is_array_v<T2>> * = nullptr>
2421 FuncRef operator()(std::vector<ExprOrVar> args) const {
2422 this->check_gio_access();
2423 return get_values<ValueType>().at(0)(std::move(args));
2424 }
2425
2426 template<typename T2 = T, std::enable_if_t<!std::is_array_v<T2>> * = nullptr>
2427 operator Func() const {
2428 this->check_gio_access();
2429 return get_values<ValueType>().at(0);
2430 }
2431
2432 template<typename T2 = T, std::enable_if_t<!std::is_array_v<T2>> * = nullptr>
2433 operator Stage() const {
2434 this->check_gio_access();
2435 return get_values<ValueType>().at(0);
2436 }
2437
2438 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
2439 size_t size() const {
2440 this->check_gio_access();
2441 return get_values<ValueType>().size();
2442 }
2443
2444 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
2445 const ValueType &operator[](size_t i) const {
2446 this->check_gio_access();
2447 return get_values<ValueType>()[i];
2448 }
2449
2450 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
2451 const ValueType &at(size_t i) const {
2452 this->check_gio_access();
2453 return get_values<ValueType>().at(i);
2454 }
2455
2456 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
2457 std::vector<ValueType>::const_iterator begin() const {
2458 this->check_gio_access();
2459 return get_values<ValueType>().begin();
2460 }
2461
2462 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
2463 std::vector<ValueType>::const_iterator end() const {
2464 this->check_gio_access();
2465 return get_values<ValueType>().end();
2466 }
2467
2468 template<typename T2 = T, std::enable_if_t<
2469 // Only allow T2[]
2470 std::is_array_v<T2> && std::rank_v<T2> == 1 && std::extent_v<T2, 0> == 0> * = nullptr>
2471 void resize(size_t size) {
2472 this->check_gio_access();
2474 }
2475};
2476
2477template<typename T>
2479private:
2481
2482 HALIDE_NO_USER_CODE_INLINE void assign_from_func(const Func &f) {
2483 this->check_value_writable();
2484
2486
2487 if (this->gio_types_defined()) {
2488 const auto &my_types = this->gio_types();
2489 user_assert(my_types.size() == f.types().size())
2490 << "Cannot assign Func \"" << f.name()
2491 << "\" to Output \"" << this->name() << "\"\n"
2492 << "Output " << this->name()
2493 << " is declared to have " << my_types.size() << " tuple elements"
2494 << " but Func " << f.name()
2495 << " has " << f.types().size() << " tuple elements.\n";
2496 for (size_t i = 0; i < my_types.size(); i++) {
2497 user_assert(my_types[i] == f.types().at(i))
2498 << "Cannot assign Func \"" << f.name()
2499 << "\" to Output \"" << this->name() << "\"\n"
2500 << (my_types.size() > 1 ? "In tuple element " + std::to_string(i) + ", " : "")
2501 << "Output " << this->name()
2502 << " has declared type " << my_types[i]
2503 << " but Func " << f.name()
2504 << " has type " << f.types().at(i) << "\n";
2505 }
2506 }
2507 if (this->dims_defined()) {
2508 user_assert(f.dimensions() == this->dims())
2509 << "Cannot assign Func \"" << f.name()
2510 << "\" to Output \"" << this->name() << "\"\n"
2511 << "Output " << this->name()
2512 << " has declared dimensionality " << this->dims()
2513 << " but Func " << f.name()
2514 << " has dimensionality " << f.dimensions() << "\n";
2515 }
2516
2517 internal_assert(this->exprs_.empty() && this->funcs_.size() == 1);
2518 user_assert(!this->funcs_.at(0).defined());
2519 this->funcs_[0] = f;
2520 }
2521
2522protected:
2523 using TBase = typename Super::TBase;
2524
2525 explicit GeneratorOutput_Buffer(const std::string &name)
2527 TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
2528 TBase::has_static_dimensions ? TBase::static_dimensions() : -1) {
2529 }
2530
2531 GeneratorOutput_Buffer(const std::string &name, const std::vector<Type> &t, int d)
2532 : Super(name, ArgInfoKind::Buffer, t, d) {
2533 internal_assert(!t.empty());
2534 internal_assert(d != -1);
2535 static_assert(!TBase::has_static_halide_type, "You can only specify a Type argument for Output<Buffer<T, D>> if T is void or omitted.");
2536 static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Output<Buffer<T, D>> if D is -1 or omitted.");
2537 }
2538
2539 GeneratorOutput_Buffer(const std::string &name, const std::vector<Type> &t)
2540 : Super(name, ArgInfoKind::Buffer, t, -1) {
2541 internal_assert(!t.empty());
2542 static_assert(!TBase::has_static_halide_type, "You can only specify a Type argument for Output<Buffer<T, D>> if T is void or omitted.");
2543 }
2544
2545 GeneratorOutput_Buffer(const std::string &name, int d)
2547 TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
2548 d) {
2549 internal_assert(d != -1);
2550 static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Output<Buffer<T, D>> if D is -1 or omitted.");
2551 }
2552
2553 GeneratorOutput_Buffer(size_t array_size, const std::string &name)
2555 TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
2556 TBase::has_static_dimensions ? TBase::static_dimensions() : -1) {
2557 }
2558
2559 GeneratorOutput_Buffer(size_t array_size, const std::string &name, const std::vector<Type> &t, int d)
2560 : Super(array_size, name, ArgInfoKind::Buffer, t, d) {
2561 internal_assert(!t.empty());
2562 internal_assert(d != -1);
2563 static_assert(!TBase::has_static_halide_type, "You can only specify a Type argument for Output<Buffer<T, D>> if T is void or omitted.");
2564 static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Output<Buffer<T, D>> if D is -1 or omitted.");
2565 }
2566
2567 GeneratorOutput_Buffer(size_t array_size, const std::string &name, const std::vector<Type> &t)
2568 : Super(array_size, name, ArgInfoKind::Buffer, t, -1) {
2569 internal_assert(!t.empty());
2570 static_assert(!TBase::has_static_halide_type, "You can only specify a Type argument for Output<Buffer<T, D>> if T is void or omitted.");
2571 }
2572
2573 GeneratorOutput_Buffer(size_t array_size, const std::string &name, int d)
2575 TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
2576 d) {
2577 internal_assert(d != -1);
2578 static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Output<Buffer<T, D>> if D is -1 or omitted.");
2579 }
2580
2581 HALIDE_NO_USER_CODE_INLINE std::string get_c_type() const override {
2582 if (TBase::has_static_halide_type) {
2583 return "Halide::Internal::StubOutputBuffer<" +
2584 halide_type_to_c_type(TBase::static_halide_type()) +
2585 ">";
2586 } else {
2587 return "Halide::Internal::StubOutputBuffer<>";
2588 }
2589 }
2590
2591 template<typename T2, std::enable_if_t<!std::is_same_v<T2, Func>> * = nullptr>
2593 return (T2) * this;
2594 }
2595
2596public:
2597 // Allow assignment from a Buffer<> to an Output<Buffer<>>;
2598 // this allows us to use a statically-compiled buffer inside a Generator
2599 // to assign to an output.
2600 // TODO: This used to take the buffer as a const ref. This no longer works as
2601 // using it in a Pipeline might change the dev field so it is currently
2602 // not considered const. We should consider how this really ought to work.
2603 template<typename T2, int D2>
2605 this->check_gio_access();
2606 this->check_value_writable();
2607
2608 user_assert(T::can_convert_from(buffer))
2609 << "Cannot assign to the Output \"" << this->name()
2610 << "\": the expression is not convertible to the same Buffer type and/or dimensions.\n";
2611
2612 if (this->gio_types_defined()) {
2613 user_assert(Type(buffer.type()) == this->gio_type())
2614 << "Output " << this->name() << " should have type=" << this->gio_type() << " but saw type=" << Type(buffer.type()) << "\n";
2615 }
2616 if (this->dims_defined()) {
2617 user_assert(buffer.dimensions() == this->dims())
2618 << "Output " << this->name() << " should have dim=" << this->dims() << " but saw dim=" << buffer.dimensions() << "\n";
2619 }
2620
2621 internal_assert(this->exprs_.empty() && this->funcs_.size() == 1);
2622 user_assert(!this->funcs_.at(0).defined());
2623 this->funcs_.at(0)(_) = buffer(_);
2624
2625 return *this;
2626 }
2627
2628 // Allow assignment from a StubOutputBuffer to an Output<Buffer>;
2629 // this allows us to pipeline the results of a Stub to the results
2630 // of the enclosing Generator.
2631 template<typename T2>
2633 this->check_gio_access();
2634 assign_from_func(stub_output_buffer.f);
2635 return *this;
2636 }
2637
2638 // Allow assignment from a Func to an Output<Buffer>;
2639 // this allows us to use helper functions that return a plain Func
2640 // to simply set the output(s) without needing a wrapper Func.
2642 this->check_gio_access();
2643 assign_from_func(f);
2644 return *this;
2645 }
2646
2647 operator OutputImageParam() const {
2648 this->check_gio_access();
2649 user_assert(!this->is_array()) << "Cannot convert an Output<Buffer<>[]> to an ImageParam; use an explicit subscript operator: " << this->name();
2650 internal_assert(this->exprs_.empty() && this->funcs_.size() == 1);
2651 return this->funcs_.at(0).output_buffer();
2652 }
2653
2654 // Forward set_estimates() to Func (rather than OutputImageParam) so that it can
2655 // handle Tuple-valued outputs correctly.
2657 user_assert(!this->is_array()) << "Cannot call set_estimates() on an array Output; use an explicit subscript operator: " << this->name();
2658 internal_assert(this->exprs_.empty() && this->funcs_.size() == 1);
2659 this->funcs_.at(0).set_estimates(estimates);
2660 return *this;
2661 }
2662
2663 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
2664 const Func &operator[](size_t i) const {
2665 this->check_gio_access();
2666 return this->template get_values<Func>()[i];
2667 }
2668
2669 // Allow Output<Buffer[]>.compute_root() (or other scheduling directive that requires nonconst)
2670 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
2672 this->check_gio_access();
2673 return this->template get_values<Func>()[i];
2674 }
2675
2676 /** Forward methods to the OutputImageParam. */
2677 // @{
2681 HALIDE_FORWARD_METHOD(OutputImageParam, set_host_alignment)
2691 // }@
2692};
2693
2694template<typename T>
2696private:
2698
2699 HALIDE_NO_USER_CODE_INLINE Func &get_assignable_func_ref(size_t i) {
2700 internal_assert(this->exprs_.empty() && this->funcs_.size() > i);
2701 return this->funcs_.at(i);
2702 }
2703
2704protected:
2705 using TBase = typename Super::TBase;
2706
2707 explicit GeneratorOutput_Func(const std::string &name)
2708 : Super(name, ArgInfoKind::Function, std::vector<Type>{}, -1) {
2709 }
2710
2711 GeneratorOutput_Func(const std::string &name, const std::vector<Type> &t, int d)
2712 : Super(name, ArgInfoKind::Function, t, d) {
2713 }
2714
2715 GeneratorOutput_Func(const std::string &name, const std::vector<Type> &t)
2716 : Super(name, ArgInfoKind::Function, t, -1) {
2717 }
2718
2719 GeneratorOutput_Func(const std::string &name, int d)
2720 : Super(name, ArgInfoKind::Function, {}, d) {
2721 }
2722
2723 GeneratorOutput_Func(size_t array_size, const std::string &name, const std::vector<Type> &t, int d)
2724 : Super(array_size, name, ArgInfoKind::Function, t, d) {
2725 }
2726
2727public:
2728 // Allow Output<Func> = Func
2729 template<typename T2 = T, std::enable_if_t<!std::is_array_v<T2>> * = nullptr>
2731 this->check_gio_access();
2732 this->check_value_writable();
2733
2734 // Don't bother verifying the Func type, dimensions, etc., here:
2735 // That's done later, when we produce the pipeline.
2736 get_assignable_func_ref(0) = f;
2737 return *this;
2738 }
2739
2740 // Allow Output<Func[]> = Func
2741 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
2742 Func &operator[](size_t i) {
2743 this->check_gio_access();
2744 this->check_value_writable();
2745 return get_assignable_func_ref(i);
2746 }
2747
2748 // Allow Func = Output<Func[]>
2749 template<typename T2 = T, std::enable_if_t<std::is_array_v<T2>> * = nullptr>
2750 const Func &operator[](size_t i) const {
2751 this->check_gio_access();
2752 return Super::operator[](i);
2753 }
2754
2755 GeneratorOutput_Func<T> &set_estimate(const Var &var, const Expr &min, const Expr &extent) {
2756 this->check_gio_access();
2757 internal_assert(this->exprs_.empty() && !this->funcs_.empty());
2758 for (Func &f : this->funcs_) {
2759 f.set_estimate(var, min, extent);
2760 }
2761 return *this;
2762 }
2763
2765 this->check_gio_access();
2766 internal_assert(this->exprs_.empty() && !this->funcs_.empty());
2767 for (Func &f : this->funcs_) {
2768 f.set_estimates(estimates);
2769 }
2770 return *this;
2771 }
2772};
2773
2774template<typename T>
2776private:
2778
2779protected:
2780 using TBase = typename Super::TBase;
2781
2782 explicit GeneratorOutput_Arithmetic(const std::string &name)
2783 : Super(name, ArgInfoKind::Function, {type_of<TBase>()}, 0) {
2784 }
2785
2786 GeneratorOutput_Arithmetic(size_t array_size, const std::string &name)
2787 : Super(array_size, name, ArgInfoKind::Function, {type_of<TBase>()}, 0) {
2788 }
2789};
2790
2791template<typename T, typename TBase = std::remove_all_extents_t<T>>
2797
2798} // namespace Internal
2799
2800template<typename T>
2802private:
2804
2805protected:
2806 using TBase = typename Super::TBase;
2807
2808public:
2809 // Mark all of these explicit (not just single-arg versions) so that
2810 // we disallow copy-list-initialization form (i.e., Output foo{"foo"} is ok,
2811 // but Output foo = {"foo"} is not).
2812 explicit GeneratorOutput(const std::string &name)
2813 : Super(name) {
2814 }
2815
2816 explicit GeneratorOutput(const char *name)
2817 : GeneratorOutput(std::string(name)) {
2818 }
2819
2820 explicit GeneratorOutput(size_t array_size, const std::string &name)
2821 : Super(array_size, name) {
2822 }
2823
2824 explicit GeneratorOutput(const std::string &name, int d)
2825 : Super(name, d) {
2826 }
2827
2828 explicit GeneratorOutput(const std::string &name, const Type &t)
2829 : Super(name, {t}) {
2830 }
2831
2832 explicit GeneratorOutput(const std::string &name, const std::vector<Type> &t)
2833 : Super(name, t) {
2834 }
2835
2836 explicit GeneratorOutput(const std::string &name, const Type &t, int d)
2837 : Super(name, {t}, d) {
2838 }
2839
2840 explicit GeneratorOutput(const std::string &name, const std::vector<Type> &t, int d)
2841 : Super(name, t, d) {
2842 }
2843
2844 explicit GeneratorOutput(size_t array_size, const std::string &name, int d)
2845 : Super(array_size, name, d) {
2846 }
2847
2848 explicit GeneratorOutput(size_t array_size, const std::string &name, const Type &t)
2849 : Super(array_size, name, {t}) {
2850 }
2851
2852 explicit GeneratorOutput(size_t array_size, const std::string &name, const std::vector<Type> &t)
2853 : Super(array_size, name, t) {
2854 }
2855
2856 explicit GeneratorOutput(size_t array_size, const std::string &name, const Type &t, int d)
2857 : Super(array_size, name, {t}, d) {
2858 }
2859
2860 explicit GeneratorOutput(size_t array_size, const std::string &name, const std::vector<Type> &t, int d)
2861 : Super(array_size, name, t, d) {
2862 }
2863
2864 // TODO: This used to take the buffer as a const ref. This no longer works as
2865 // using it in a Pipeline might change the dev field so it is currently
2866 // not considered const. We should consider how this really ought to work.
2867 template<typename T2, int D2>
2869 Super::operator=(buffer);
2870 return *this;
2871 }
2872
2873 template<typename T2>
2875 Super::operator=(stub_output_buffer);
2876 return *this;
2877 }
2878
2880 Super::operator=(f);
2881 return *this;
2882 }
2883};
2884
2885namespace Internal {
2886
2887template<typename T>
2888T parse_scalar(const std::string &value) {
2889 std::istringstream iss(value);
2890 T t;
2891 iss >> t;
2892 user_assert(!iss.fail() && iss.get() == EOF) << "Unable to parse: " << value;
2893 return t;
2894}
2895
2896std::vector<Type> parse_halide_type_list(const std::string &types);
2897
2899 Dim,
2900 ArraySize };
2901
2902// This is a type of GeneratorParam used internally to create 'synthetic' params
2903// (e.g. image.type, image.dim); it is not possible for user code to instantiate it.
2904template<typename T>
2906public:
2907 void set_from_string(const std::string &new_value_string) override {
2908 // If error_msg is not empty, this is unsettable:
2909 // display error_msg as a user error.
2910 if (!error_msg.empty()) {
2911 user_error << error_msg;
2912 }
2914 }
2915
2916 std::string get_default_value() const override {
2918 return std::string();
2919 }
2920
2921 std::string call_to_string(const std::string &v) const override {
2923 return std::string();
2924 }
2925
2926 std::string get_c_type() const override {
2928 return std::string();
2929 }
2930
2931 bool is_synthetic_param() const override {
2932 return true;
2933 }
2934
2935private:
2937
2938 static std::unique_ptr<Internal::GeneratorParamBase> make(
2939 GeneratorBase *generator,
2940 const std::string &generator_name,
2941 const std::string &gpname,
2942 GIOBase &gio,
2943 SyntheticParamType which,
2944 bool defined) {
2945 std::string error_msg = defined ? "Cannot set the GeneratorParam " + gpname + " for " + generator_name + " because the value is explicitly specified in the C++ source." : "";
2946 return std::unique_ptr<GeneratorParam_Synthetic<T>>(
2947 new GeneratorParam_Synthetic<T>(gpname, gio, which, error_msg));
2948 }
2949
2950 GeneratorParam_Synthetic(const std::string &name, GIOBase &gio, SyntheticParamType which, const std::string &error_msg = "")
2951 : GeneratorParamImpl<T>(name, T()), gio(gio), which(which), error_msg(error_msg) {
2952 }
2953
2954 template<typename T2 = T, std::enable_if_t<std::is_same_v<T2, ::Halide::Type>> * = nullptr>
2955 void set_from_string_impl(const std::string &new_value_string) {
2958 }
2959
2960 template<typename T2 = T, std::enable_if_t<std::is_integral_v<T2>> * = nullptr>
2961 void set_from_string_impl(const std::string &new_value_string) {
2962 if (which == SyntheticParamType::Dim) {
2964 } else if (which == SyntheticParamType::ArraySize) {
2966 } else {
2968 }
2969 }
2970
2971 GIOBase &gio;
2972 const SyntheticParamType which;
2973 const std::string error_msg;
2974};
2975
2976} // namespace Internal
2977
2978/** GeneratorContext is a class that is used when using Generators (or Stubs) directly;
2979 * it is used to allow the outer context (typically, either a Generator or "top-level" code)
2980 * to specify certain information to the inner context to ensure that inner and outer
2981 * Generators are compiled in a compatible way.
2982 *
2983 * If you are using this at "top level" (e.g. with the JIT), you can construct a GeneratorContext
2984 * with a Target:
2985 * \code
2986 * auto my_stub = MyStub(
2987 * GeneratorContext(get_target_from_environment()),
2988 * // inputs
2989 * { ... },
2990 * // generator params
2991 * { ... }
2992 * );
2993 * \endcode
2994 *
2995 * Note that all Generators embed a GeneratorContext, so if you are using a Stub
2996 * from within a Generator, you can just pass 'context()' for the GeneratorContext:
2997 * \code
2998 * struct SomeGen : Generator<SomeGen> {
2999 * void generate() {
3000 * ...
3001 * auto my_stub = MyStub(
3002 * context(), // GeneratorContext
3003 * // inputs
3004 * { ... },
3005 * // generator params
3006 * { ... }
3007 * );
3008 * ...
3009 * }
3010 * };
3011 * \endcode
3012 */
3014public:
3016
3017 explicit GeneratorContext(const Target &t);
3018 explicit GeneratorContext(const Target &t,
3020
3021 GeneratorContext() = default;
3026
3027 const Target &target() const {
3028 return target_;
3029 }
3031 return autoscheduler_params_;
3032 }
3033
3034 // Return a copy of this GeneratorContext that uses the given Target.
3035 // This method is rarely needed; it's really provided as a convenience
3036 // for use with init_from_context().
3038
3039 template<typename T>
3040 std::unique_ptr<T> create() const {
3041 return T::create(*this);
3042 }
3043 template<typename T, typename... Args>
3044 std::unique_ptr<T> apply(const Args &...args) const {
3045 auto t = this->create<T>();
3046 t->apply(args...);
3047 return t;
3048 }
3049
3050private:
3051 Target target_;
3052 AutoschedulerParams autoscheduler_params_;
3053};
3054
3056 // Names in this class are only intended for use in derived classes.
3057protected:
3058 // Import a consistent list of Halide names that can be used in
3059 // Halide generators without qualification.
3079 template<typename T>
3080 static Expr cast(Expr e) {
3081 return Halide::cast<T>(e);
3082 }
3084 return Halide::cast(t, std::move(e));
3085 }
3086 template<typename T>
3088 template<typename T = void, int D = -1>
3090 template<typename T>
3092 static Type Bool(int lanes = 1) {
3093 return Halide::Bool(lanes);
3094 }
3095 static Type Float(int bits, int lanes = 1) {
3096 return Halide::Float(bits, lanes);
3097 }
3098 static Type Int(int bits, int lanes = 1) {
3099 return Halide::Int(bits, lanes);
3100 }
3101 static Type UInt(int bits, int lanes = 1) {
3102 return Halide::UInt(bits, lanes);
3103 }
3104};
3105
3106namespace Internal {
3107
3108template<typename...>
3109struct NoRealizations : std::false_type {};
3110
3111template<>
3112struct NoRealizations<> : std::true_type {};
3113
3114template<typename T, typename... Args>
3115struct NoRealizations<T, Args...> {
3116 static const bool value = !std::is_convertible_v<T, Realization> && NoRealizations<Args...>::value;
3117};
3118
3119template<typename... Args>
3120inline constexpr bool no_realizations_v = NoRealizations<Args...>::value;
3121
3122// Note that these functions must never return null:
3123// if they cannot return a valid Generator, they must assert-fail.
3124using GeneratorFactory = std::function<AbstractGeneratorPtr(const GeneratorContext &context)>;
3125
3127 // names used across all params, inputs, and outputs.
3128 std::set<std::string> names;
3129
3130 // Ordered-list of non-null ptrs to GeneratorParam<> fields.
3131 std::vector<Internal::GeneratorParamBase *> filter_generator_params;
3132
3133 // Ordered-list of non-null ptrs to Input<> fields.
3134 std::vector<Internal::GeneratorInputBase *> filter_inputs;
3135
3136 // Ordered-list of non-null ptrs to Output<> fields; empty if old-style Generator.
3137 std::vector<Internal::GeneratorOutputBase *> filter_outputs;
3138
3139 // list of synthetic GP's that we dynamically created; this list only exists to simplify
3140 // lifetime management, and shouldn't be accessed directly outside of our ctor/dtor,
3141 // regardless of friend access.
3142 std::vector<std::unique_ptr<Internal::GeneratorParamBase>> owned_synthetic_params;
3143
3144 // list of dynamically-added inputs and outputs, here only for lifetime management.
3145 std::vector<std::unique_ptr<Internal::GIOBase>> owned_extras;
3146
3147public:
3148 friend class GeneratorBase;
3149
3150 GeneratorParamInfo(GeneratorBase *generator, size_t size);
3151
3152 const std::vector<Internal::GeneratorParamBase *> &generator_params() const {
3153 return filter_generator_params;
3154 }
3155 const std::vector<Internal::GeneratorInputBase *> &inputs() const {
3156 return filter_inputs;
3157 }
3158 const std::vector<Internal::GeneratorOutputBase *> &outputs() const {
3159 return filter_outputs;
3160 }
3161};
3162
3164public:
3165 ~GeneratorBase() override;
3166
3167 /** Given a data type, return an estimate of the "natural" vector size
3168 * for that data type when compiling for the current target. */
3170 return get_target().natural_vector_size(t);
3171 }
3172
3173 /** Given a data type, return an estimate of the "natural" vector size
3174 * for that data type when compiling for the current target. */
3175 template<typename data_t>
3178 }
3179
3180 /**
3181 * set_inputs is a variadic wrapper around set_inputs_vector, which makes usage much simpler
3182 * in many cases, as it constructs the relevant entries for the vector for you, which
3183 * is often a bit unintuitive at present. The arguments are passed in Input<>-declaration-order,
3184 * and the types must be compatible. Array inputs are passed as std::vector<> of the relevant type.
3185 *
3186 * Note: at present, scalar input types must match *exactly*, i.e., for Input<uint8_t>, you
3187 * must pass an argument that is actually uint8_t; an argument that is int-that-will-fit-in-uint8
3188 * will assert-fail at Halide compile time.
3189 */
3190 template<typename... Args>
3191 void set_inputs(const Args &...args) {
3192 // set_inputs_vector() checks this too, but checking it here allows build_inputs() to avoid out-of-range checks.
3193 GeneratorParamInfo &pi = this->param_info();
3194 user_assert(sizeof...(args) == pi.inputs().size())
3195 << "Expected exactly " << pi.inputs().size()
3196 << " inputs but got " << sizeof...(args) << "\n";
3197 set_inputs_vector(build_inputs(std::forward_as_tuple<const Args &...>(args...), std::make_index_sequence<sizeof...(Args)>{}));
3198 }
3199
3200 Realization realize(std::vector<int32_t> sizes) {
3201 this->check_scheduled("realize");
3202 return get_pipeline().realize(std::move(sizes), get_target());
3203 }
3204
3205 // Only enable if none of the args are Realization; otherwise we can incorrectly
3206 // select this method instead of the Realization-as-outparam variant
3207 template<typename... Args, std::enable_if_t<no_realizations_v<Args...>> * = nullptr>
3209 this->check_scheduled("realize");
3210 return get_pipeline().realize(std::forward<Args>(args)..., get_target());
3211 }
3212
3214 this->check_scheduled("realize");
3216 }
3217
3218 // Return the Pipeline that has been built by the generate() method.
3219 // This method can only be called from the schedule() method.
3220 // (This may be relaxed in the future to allow calling from generate() as
3221 // long as all Outputs have been defined.)
3223
3224protected:
3225 void claim_name(const std::string &name, const char *param_type) {
3226 user_assert(param_info_ptr->names.count(name) == 0)
3227 << "Cannot add " << param_type << " with name " << name
3228 << ". It is already taken by another input or output parameter.";
3229 param_info_ptr->names.insert(name);
3230 }
3231
3232public:
3233 // Create Input<Func> with dynamic type & dimensions
3234 template<typename T,
3235 std::enable_if_t<std::is_same_v<T, Halide::Func>> * = nullptr>
3236 GeneratorInput<T> *add_input(const std::string &name, const Type &t, int dimensions) {
3238 claim_name(name, "input");
3239 auto *p = new GeneratorInput<T>(name, t, dimensions);
3240 p->generator = this;
3241 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3242 param_info_ptr->filter_inputs.push_back(p);
3243 return p;
3244 }
3245
3246 // Create Input<Buffer> with dynamic type & dimensions
3247 template<typename T,
3248 std::enable_if_t<!std::is_arithmetic_v<T> && !std::is_same_v<T, Halide::Func>> * = nullptr>
3249 GeneratorInput<T> *add_input(const std::string &name, const Type &t, int dimensions) {
3250 static_assert(!T::has_static_halide_type, "You can only call this version of add_input() for a Buffer<T, D> where T is void or omitted .");
3251 static_assert(!T::has_static_dimensions, "You can only call this version of add_input() for a Buffer<T, D> where D is -1 or omitted.");
3253 claim_name(name, "input");
3254 auto *p = new GeneratorInput<T>(name, t, dimensions);
3255 p->generator = this;
3256 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3257 param_info_ptr->filter_inputs.push_back(p);
3258 return p;
3259 }
3260
3261 // Create Input<Buffer> with compile-time type
3262 template<typename T,
3263 std::enable_if_t<!std::is_arithmetic_v<T> && !std::is_same_v<T, Halide::Func>> * = nullptr>
3264 GeneratorInput<T> *add_input(const std::string &name, int dimensions) {
3265 static_assert(T::has_static_halide_type, "You can only call this version of add_input() for a Buffer<T, D> where T is not void.");
3266 static_assert(!T::has_static_dimensions, "You can only call this version of add_input() for a Buffer<T, D> where D is -1 or omitted.");
3268 claim_name(name, "input");
3269 auto *p = new GeneratorInput<T>(name, dimensions);
3270 p->generator = this;
3271 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3272 param_info_ptr->filter_inputs.push_back(p);
3273 return p;
3274 }
3275
3276 // Create Input<Buffer> with compile-time type & dimensions
3277 template<typename T,
3278 std::enable_if_t<!std::is_arithmetic_v<T> && !std::is_same_v<T, Halide::Func>> * = nullptr>
3279 GeneratorInput<T> *add_input(const std::string &name) {
3280 static_assert(T::has_static_halide_type, "You can only call this version of add_input() for a Buffer<T, D> where T is not void.");
3281 static_assert(T::has_static_dimensions, "You can only call this version of add_input() for a Buffer<T, D> where D is not -1.");
3283 claim_name(name, "input");
3284 auto *p = new GeneratorInput<T>(name);
3285 p->generator = this;
3286 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3287 param_info_ptr->filter_inputs.push_back(p);
3288 return p;
3289 }
3290 // Create Input<scalar>
3291 template<typename T,
3292 std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>
3293 GeneratorInput<T> *add_input(const std::string &name) {
3295 claim_name(name, "input");
3296 auto *p = new GeneratorInput<T>(name);
3297 p->generator = this;
3298 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3299 param_info_ptr->filter_inputs.push_back(p);
3300 return p;
3301 }
3302 // Create Input<Expr> with dynamic type
3303 template<typename T,
3304 std::enable_if_t<std::is_same_v<T, Expr>> * = nullptr>
3305 GeneratorInput<T> *add_input(const std::string &name, const Type &type) {
3307 claim_name(name, "input");
3308 auto *p = new GeneratorInput<Expr>(name);
3309 p->generator = this;
3310 p->set_type(type);
3311 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3312 param_info_ptr->filter_inputs.push_back(p);
3313 return p;
3314 }
3315
3316 // Create Output<Func> with dynamic type & dimensions
3317 template<typename T,
3318 std::enable_if_t<std::is_same_v<T, Halide::Func>> * = nullptr>
3319 GeneratorOutput<T> *add_output(const std::string &name, const std::vector<Type> &t, int dimensions) {
3321 claim_name(name, "output");
3322 auto *p = new GeneratorOutput<T>(name, t, dimensions);
3323 p->generator = this;
3324 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3325 param_info_ptr->filter_outputs.push_back(p);
3326 return p;
3327 }
3328
3329 template<typename T,
3330 std::enable_if_t<std::is_same_v<T, Halide::Func>> * = nullptr>
3331 GeneratorOutput<T> *add_output(const std::string &name, const Type &t, int dimensions) {
3332 return add_output<T>(name, std::vector<Type>{t}, dimensions);
3333 }
3334
3335 // Create Output<Buffer> with dynamic type & dimensions
3336 template<typename T,
3337 std::enable_if_t<!std::is_arithmetic_v<T> && !std::is_same_v<T, Halide::Func>> * = nullptr>
3338 GeneratorOutput<T> *add_output(const std::string &name, const std::vector<Type> &t, int dimensions) {
3339 static_assert(!T::has_static_halide_type, "You can only call this version of add_output() for a Buffer<T, D> where T is void or omitted .");
3340 static_assert(!T::has_static_dimensions, "You can only call this version of add_output() for a Buffer<T, D> where D is -1 or omitted.");
3342 claim_name(name, "output");
3343 auto *p = new GeneratorOutput<T>(name, t, dimensions);
3344 p->generator = this;
3345 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3346 param_info_ptr->filter_outputs.push_back(p);
3347 return p;
3348 }
3349
3350 template<typename T,
3351 std::enable_if_t<!std::is_arithmetic_v<T> && !std::is_same_v<T, Halide::Func>> * = nullptr>
3352 GeneratorOutput<T> *add_output(const std::string &name, const Type &t, int dimensions) {
3353 return add_output<T>(name, std::vector<Type>{t}, dimensions);
3354 }
3355
3356 // Create Output<Buffer> with either a compile-time type or a
3357 // to-be-set-later type and dynamic dimensions
3358 template<typename T,
3359 std::enable_if_t<!std::is_arithmetic_v<T> && !std::is_same_v<T, Halide::Func>> * = nullptr>
3360 GeneratorOutput<T> *add_output(const std::string &name, int dimensions) {
3361 static_assert(!T::has_static_dimensions, "You can only call this version of add_output() for a Buffer<T, D> where D is -1 or omitted.");
3363 claim_name(name, "output");
3364 auto *p = new GeneratorOutput<T>(name, dimensions);
3365 p->generator = this;
3366 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3367 param_info_ptr->filter_outputs.push_back(p);
3368 return p;
3369 }
3370
3371 // Create Output<Buffer> with compile-time dimensions and dynamic type
3372 template<typename T,
3373 std::enable_if_t<!std::is_arithmetic_v<T> && !std::is_same_v<T, Halide::Func>> * = nullptr>
3374 GeneratorOutput<T> *add_output(const std::string &name, const std::vector<Type> &t) {
3375 static_assert(!T::has_static_halide_type, "You can only call this version of add_output() for a Buffer<T, D> where T is void or omitted.");
3376 static_assert(T::has_static_dimensions, "You can only call this version of add_output() for a Buffer<void, D> where D is not -1.");
3378 claim_name(name, "output");
3379 auto *p = new GeneratorOutput<T>(name, t);
3380 p->generator = this;
3381 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3382 param_info_ptr->filter_outputs.push_back(p);
3383 return p;
3384 }
3385
3386 template<typename T,
3387 std::enable_if_t<!std::is_arithmetic_v<T> && !std::is_same_v<T, Halide::Func>> * = nullptr>
3388 GeneratorOutput<T> *add_output(const std::string &name, const Type &t) {
3389 return add_output<T>(name, std::vector<Type>{t});
3390 }
3391
3392 // Create Output<Buffer> with compile-time type and dimensions
3393 template<typename T,
3394 std::enable_if_t<!std::is_arithmetic_v<T> && !std::is_same_v<T, Halide::Func>> * = nullptr>
3395 GeneratorOutput<T> *add_output(const std::string &name) {
3396 static_assert(T::has_static_halide_type, "You can only call this version of add_output() for a Buffer<T, D> where T is not void.");
3397 static_assert(T::has_static_dimensions, "You can only call this version of add_output() for a Buffer<T, D> where D is not -1.");
3399 claim_name(name, "output");
3400 auto *p = new GeneratorOutput<T>(name);
3401 p->generator = this;
3402 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3403 param_info_ptr->filter_outputs.push_back(p);
3404 return p;
3405 }
3406
3407 void add_requirement(const Expr &condition, const std::vector<Expr> &error_args);
3408
3409 template<typename... Args,
3410 typename = std::enable_if_t<all_are_printable_args_v<Args...>>>
3411 HALIDE_NO_USER_CODE_INLINE void add_requirement(const Expr &condition, Args &&...error_args) {
3412 std::vector<Expr> collected_args;
3413 Internal::collect_print_args(collected_args, std::forward<Args>(error_args)...);
3414 add_requirement(condition, collected_args);
3415 }
3416
3419 }
3420
3421protected:
3422 GeneratorBase(size_t size);
3423 void set_generator_names(const std::string &registered_name, const std::string &stub_name);
3424
3425 // Note that it is explicitly legal to override init_from_context(), so that you can (say)
3426 // create a modified context with a different Target (eg with features enabled or disabled), but...
3427 //
3428 // *** WARNING ***
3429 //
3430 // Modifying the context here can be fraught with subtle hazards, especially when used
3431 // in conjunction with compiling to multitarget output. Adding or removing Feature
3432 // flags could break your build (if you are lucky), or cause subtle runtime failures (if unlucky)...
3433 //
3434 // e.g. in the latter case, say you decided to enable AVX512_SapphireRapids as an experiment,
3435 // and override init_from_context() to do just that. You'd end up being crashy on pre-AVX512
3436 // hardware, because the code that Halide injects to do runtime CPU feature detection at runtime
3437 // doesn't know it needs to do the runtime detection for this flag.
3438 //
3439 // Even if you are using multitarget output, using this as a 'hook' to enable or disable Features
3440 // can produce hard-to-maintain code in the long term: Halide has dozens of feature flags now,
3441 // many of which are orthogonal to each other and/or specific to a certain architecture
3442 // (or sub-architecture). The interaction between 'orthogonal' flags like this is essentially
3443 // Undefined Behavior (e.g. if I enable the SSE41 Feature on a Target where arch = RISCV, what happens?
3444 // Is it ignored? Does it fail to compile? Something else?). The point here is that adding Features
3445 // here may end up eventually getting added to a Target you didn't anticipate and have adverse consequences.
3446 //
3447 // With all that in mind, here are some guidelines we think will make long-term code maintenance
3448 // less painful for you:
3449 //
3450 // - Override this method *only* for temporary debugging purposes; e.g. if you
3451 // need to add the `profile` feature to a specific Generator, but your build system doesn't easily
3452 // let you specify per-Generator target features, this is the right tool for the job.
3453 //
3454 // - If your build system makes it infeasible to customize the build Target in a reasonable way,
3455 // it may be appropriate to permanently override this method to enable specific Features for
3456 // specific Generators (e.g., enabling `strict_float` is a likely example). In that case,
3457 // we would suggest:
3458 //
3459 // - *NEVER* change the arch/bits/os of the Target.
3460 // - Only add Features; don't remove Features.
3461 // - For Features that are architecture-specific, always check the arch/bits/os
3462 // of the Target to be sure it's what you expect... e.g. if you are enabling
3463 // AVX512, only do so if compiling for an x86-64 Target. Even if your code
3464 // doesn't target any other architecture at the present time, Future You will be
3465 // happier.
3466 // - If you mutate a target conditionally based on the incoming target, try to do so
3467 // so based only on the Target's arch/bits/os, and not at the Features set on the target.
3468 // If examining Features is unavoidable (e.g. enable $FOO only if $BAR is enabled),
3469 // do so as conservatively as possible, and always validate that the rest of the Target
3470 // is sensible for what you are doing.
3471 //
3472 // Furthermore, if you override this, please don't try to directly set the `target` (etc) GeneratorParams
3473 // directly; instead, construct the new GeneratorContext you want and call the superclass
3474 // implementation of init_from_context.
3475 //
3476 // TL;DR: overrides to this method should probably never be checked in to your source control system
3477 // (rather, the override should be temporary and local, for experimentation). If you must check in
3478 // overrides to this method, be paranoid that the Target you get could be something you don't expect.
3479 //
3481
3482 virtual void call_configure() = 0;
3483 virtual void call_generate() = 0;
3484 virtual void call_schedule() = 0;
3485
3494
3495 template<typename T>
3497
3498 template<typename T>
3500
3501 // A Generator's creation and usage must go in a certain phase to ensure correctness;
3502 // the state machine here is advanced and checked at various points to ensure
3503 // this is the case.
3504 enum Phase {
3505 // Generator has just come into being.
3507
3508 // Generator has had its configure() method called. (For Generators without
3509 // a configure() method, this phase will be skipped and will advance
3510 // directly to InputsSet.)
3512
3513 // All Input<>/Param<> fields have been set. (Applicable only in JIT mode;
3514 // in AOT mode, this can be skipped, going Created->GenerateCalled directly.)
3516
3517 // Generator has had its generate() method called.
3519
3520 // Generator has had its schedule() method (if any) called.
3523
3527
3529
3531 return target;
3532 }
3533 bool using_autoscheduler() const {
3534 return !autoscheduler_.value().name.empty();
3535 }
3536
3537 // These must remain here for legacy code that access the fields directly.
3540
3541private:
3542 friend void ::Halide::Internal::generator_test();
3544 friend class GIOBase;
3549
3550 const size_t size;
3551
3552 // Lazily-allocated-and-inited struct with info about our various Params.
3553 // Do not access directly: use the param_info() getter.
3554 std::unique_ptr<GeneratorParamInfo> param_info_ptr;
3555
3556 std::string generator_registered_name, generator_stub_name;
3557 Pipeline pipeline;
3558
3559 struct Requirement {
3560 Expr condition;
3561 std::vector<Expr> error_args;
3562 };
3563 std::vector<Requirement> requirements;
3564
3565 // Return our GeneratorParamInfo.
3566 GeneratorParamInfo &param_info();
3567
3568 template<typename T>
3569 T *find_by_name(const std::string &name, const std::vector<T *> &v) {
3570 for (T *t : v) {
3571 if (t->name() == name) {
3572 return t;
3573 }
3574 }
3575 return nullptr;
3576 }
3577
3578 Internal::GeneratorInputBase *find_input_by_name(const std::string &name);
3579 Internal::GeneratorOutputBase *find_output_by_name(const std::string &name);
3580
3581 void check_scheduled(const char *m) const;
3582
3583 void build_params(bool force = false);
3584
3585 // Provide private, unimplemented, wrong-result-type methods here
3586 // so that Generators don't attempt to call the global methods
3587 // of the same name by accident: use the get_target() method instead.
3588 void get_host_target();
3589 void get_jit_target_from_environment();
3590 void get_target_from_environment();
3591
3592 void set_inputs_vector(const std::vector<std::vector<StubInput>> &inputs);
3593
3594 static void check_input_is_singular(Internal::GeneratorInputBase *in);
3595 static void check_input_is_array(Internal::GeneratorInputBase *in);
3596 static void check_input_kind(Internal::GeneratorInputBase *in, Internal::ArgInfoKind kind);
3597
3598 // Allow Buffer<> if:
3599 // -- we are assigning it to an Input<Buffer<>> (with compatible type and dimensions),
3600 // causing the Input<Buffer<>> to become a precompiled buffer in the generated code.
3601 // -- we are assigningit to an Input<Func>, in which case we just Func-wrap the Buffer<>.
3602 template<typename T, int Dims>
3603 std::vector<StubInput> build_input(size_t i, const Buffer<T, Dims> &arg) {
3604 auto *in = param_info().inputs().at(i);
3605 check_input_is_singular(in);
3606 const auto k = in->kind();
3608 Halide::Buffer<> b = arg;
3610 StubInput si(sib);
3611 return {si};
3612 } else if (k == Internal::ArgInfoKind::Function) {
3613 Halide::Func f(arg.name() + "_im");
3614 f(Halide::_) = arg(Halide::_);
3615 StubInput si(f);
3616 return {si};
3617 } else {
3618 check_input_kind(in, Internal::ArgInfoKind::Buffer); // just to trigger assertion
3619 return {};
3620 }
3621 }
3622
3623 // Allow Input<Buffer<>> if:
3624 // -- we are assigning it to another Input<Buffer<>> (with compatible type and dimensions),
3625 // allowing us to simply pipe a parameter from an enclosing Generator to the Invoker.
3626 // -- we are assigningit to an Input<Func>, in which case we just Func-wrap the Input<Buffer<>>.
3627 template<typename T, int Dims>
3628 std::vector<StubInput> build_input(size_t i, const GeneratorInput<Buffer<T, Dims>> &arg) {
3629 auto *in = param_info().inputs().at(i);
3630 check_input_is_singular(in);
3631 const auto k = in->kind();
3633 StubInputBuffer<> sib = arg;
3634 StubInput si(sib);
3635 return {si};
3636 } else if (k == Internal::ArgInfoKind::Function) {
3637 Halide::Func f = arg.funcs().at(0);
3638 StubInput si(f);
3639 return {si};
3640 } else {
3641 check_input_kind(in, Internal::ArgInfoKind::Buffer); // just to trigger assertion
3642 return {};
3643 }
3644 }
3645
3646 // Allow Func iff we are assigning it to an Input<Func> (with compatible type and dimensions).
3647 std::vector<StubInput> build_input(size_t i, const Func &arg) {
3648 auto *in = param_info().inputs().at(i);
3649 check_input_kind(in, Internal::ArgInfoKind::Function);
3650 check_input_is_singular(in);
3651 const Halide::Func &f = arg;
3652 StubInput si(f);
3653 return {si};
3654 }
3655
3656 // Allow vector<Func> iff we are assigning it to an Input<Func[]> (with compatible type and dimensions).
3657 std::vector<StubInput> build_input(size_t i, const std::vector<Func> &arg) {
3658 auto *in = param_info().inputs().at(i);
3659 check_input_kind(in, Internal::ArgInfoKind::Function);
3660 check_input_is_array(in);
3661 // My kingdom for a list comprehension...
3662 std::vector<StubInput> siv;
3663 siv.reserve(arg.size());
3664 for (const auto &f : arg) {
3665 siv.emplace_back(f);
3666 }
3667 return siv;
3668 }
3669
3670 // Expr must be Input<Scalar>.
3671 std::vector<StubInput> build_input(size_t i, const Expr &arg) {
3672 auto *in = param_info().inputs().at(i);
3673 check_input_kind(in, Internal::ArgInfoKind::Scalar);
3674 check_input_is_singular(in);
3675 StubInput si(arg);
3676 return {si};
3677 }
3678
3679 // (Array form)
3680 std::vector<StubInput> build_input(size_t i, const std::vector<Expr> &arg) {
3681 auto *in = param_info().inputs().at(i);
3682 check_input_kind(in, Internal::ArgInfoKind::Scalar);
3683 check_input_is_array(in);
3684 std::vector<StubInput> siv;
3685 siv.reserve(arg.size());
3686 for (const auto &value : arg) {
3687 siv.emplace_back(value);
3688 }
3689 return siv;
3690 }
3691
3692 // Any other type must be convertible to Expr and must be associated with an Input<Scalar>.
3693 // Use is_arithmetic since some Expr conversions are explicit.
3694 template<typename T,
3695 std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>
3696 std::vector<StubInput> build_input(size_t i, const T &arg) {
3697 auto *in = param_info().inputs().at(i);
3698 check_input_kind(in, Internal::ArgInfoKind::Scalar);
3699 check_input_is_singular(in);
3700 // We must use an explicit Expr() ctor to preserve the type
3701 Expr e(arg);
3702 StubInput si(e);
3703 return {si};
3704 }
3705
3706 // (Array form)
3707 template<typename T, std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>
3708 std::vector<StubInput> build_input(size_t i, const std::vector<T> &arg) {
3709 auto *in = param_info().inputs().at(i);
3710 check_input_kind(in, Internal::ArgInfoKind::Scalar);
3711 check_input_is_array(in);
3712 std::vector<StubInput> siv;
3713 siv.reserve(arg.size());
3714 for (const auto &value : arg) {
3715 // We must use an explicit Expr() ctor to preserve the type;
3716 // otherwise, implicit conversions can downgrade (e.g.) float -> int
3717 Expr e(value);
3718 siv.emplace_back(e);
3719 }
3720 return siv;
3721 }
3722
3723 template<typename... Args, size_t... Indices>
3724 std::vector<std::vector<StubInput>> build_inputs(const std::tuple<const Args &...> &t, std::index_sequence<Indices...>) {
3725 return {build_input(Indices, std::get<Indices>(t))...};
3726 }
3727
3728 // Note that this deliberately ignores inputs/outputs with multiple array values
3729 // (ie, one name per input or output, regardless of array_size())
3730 template<typename T>
3731 static void get_arguments(std::vector<AbstractGenerator::ArgInfo> &args, ArgInfoDirection dir, const T &t) {
3732 for (auto *e : t) {
3733 args.push_back({e->name(),
3734 dir,
3735 e->kind(),
3736 e->gio_types_defined() ? e->gio_types() : std::vector<Type>{},
3737 e->dims_defined() ? e->dims() : 0});
3738 }
3739 }
3740
3741public:
3742 // AbstractGenerator methods
3743 std::string name() override;
3744 GeneratorContext context() const override;
3745 std::vector<ArgInfo> arginfos() override;
3747
3748 void set_generatorparam_value(const std::string &name, const std::string &value) override;
3749 void set_generatorparam_value(const std::string &name, const LoopLevel &loop_level) override;
3750
3751 std::vector<Parameter> input_parameter(const std::string &name) override;
3752 std::vector<Func> output_func(const std::string &name) override;
3753
3754 // This is overridden in the concrete Generator<> subclass.
3755 // Pipeline build_pipeline() override;
3756
3757 void bind_input(const std::string &name, const std::vector<Parameter> &v) override;
3758 void bind_input(const std::string &name, const std::vector<Func> &v) override;
3759 void bind_input(const std::string &name, const std::vector<Expr> &v) override;
3760
3761 bool emit_cpp_stub(const std::string &stub_file_path) override;
3762 bool emit_hlpipe(const std::string &hlpipe_file_path) override;
3763
3764 GeneratorBase(const GeneratorBase &) = delete;
3768};
3769
3771public:
3772 static void register_factory(const std::string &name, GeneratorFactory generator_factory);
3773 static void unregister_factory(const std::string &name);
3774 static std::vector<std::string> enumerate();
3775 // This method returns nullptr if it cannot return a valid Generator;
3776 // the caller is responsible for checking the result.
3777 static AbstractGeneratorPtr create(const std::string &name,
3778 const Halide::GeneratorContext &context);
3779
3780private:
3781 using GeneratorFactoryMap = std::map<const std::string, GeneratorFactory>;
3782
3783 GeneratorFactoryMap factories;
3784 std::mutex mutex;
3785
3786 static GeneratorRegistry &get_registry();
3787
3788 GeneratorRegistry() = default;
3789
3790public:
3795};
3796
3797} // namespace Internal
3798
3799template<class T>
3801protected:
3804 }
3805
3806public:
3807 static std::unique_ptr<T> create(const Halide::GeneratorContext &context) {
3808 // We must have an object of type T (not merely GeneratorBase) to call a protected method,
3809 // because CRTP is a weird beast.
3810 auto g = std::make_unique<T>();
3811 g->init_from_context(context);
3812 return g;
3813 }
3814
3815 // This is public but intended only for use by the HALIDE_REGISTER_GENERATOR() macro.
3816 static std::unique_ptr<T> create(const Halide::GeneratorContext &context,
3817 const std::string &registered_name,
3818 const std::string &stub_name) {
3819 auto g = create(context);
3820 g->set_generator_names(registered_name, stub_name);
3821 return g;
3822 }
3823
3824 template<typename... Args>
3825 void apply(const Args &...args) {
3827 set_inputs(args...);
3828 call_generate();
3829 call_schedule();
3830 }
3831
3832 template<typename T2>
3833 std::unique_ptr<T2> create() const {
3834 return T2::create(context());
3835 }
3836
3837 template<typename T2, typename... Args>
3838 std::unique_ptr<T2> apply(const Args &...args) const {
3839 auto t = this->create<T2>();
3840 t->apply(args...);
3841 return t;
3842 }
3843
3844private:
3845 // std::is_member_function_pointer will fail if there is no member of that name,
3846 // so we use a little SFINAE to detect if there are method-shaped members.
3847 template<typename>
3848 using type_sink_t = void;
3849
3850 template<typename T2, typename = void>
3851 struct has_configure_method : std::false_type {};
3852
3853 template<typename T2>
3854 struct has_configure_method<T2, type_sink_t<decltype(std::declval<T2>().configure())>> : std::true_type {};
3855
3856 template<typename T2>
3857 static constexpr bool has_configure_method_v = has_configure_method<T2>::value;
3858
3859 template<typename T2, typename = void>
3860 struct has_generate_method : std::false_type {};
3861
3862 template<typename T2>
3863 struct has_generate_method<T2, type_sink_t<decltype(std::declval<T2>().generate())>> : std::true_type {};
3864
3865 template<typename T2>
3866 static constexpr bool has_generate_method_v = has_generate_method<T2>::value;
3867
3868 template<typename T2, typename = void>
3869 struct has_schedule_method : std::false_type {};
3870
3871 template<typename T2>
3872 struct has_schedule_method<T2, type_sink_t<decltype(std::declval<T2>().schedule())>> : std::true_type {};
3873
3874 template<typename T2>
3875 static constexpr bool has_schedule_method_v = has_schedule_method<T2>::value;
3876
3877 Pipeline build_pipeline_impl() {
3878 T *t = (T *)this;
3879 // No: configure() must be called prior to this
3880 // (and in fact, prior to calling set_inputs).
3881 //
3882 // t->call_configure_impl();
3883
3884 t->call_generate_impl();
3885 t->call_schedule_impl();
3886 return get_pipeline();
3887 }
3888
3889 void call_configure_impl() {
3890 pre_configure();
3891 if constexpr (has_configure_method_v<T>) {
3892 T *t = (T *)this;
3893 static_assert(std::is_void_v<decltype(t->configure())>, "configure() must return void");
3894 t->configure();
3895 }
3897 }
3898
3899 void call_generate_impl() {
3900 pre_generate();
3901 static_assert(has_generate_method_v<T>, "Expected a generate() method here.");
3902 T *t = (T *)this;
3903 static_assert(std::is_void_v<decltype(t->generate())>, "generate() must return void");
3904 t->generate();
3905 post_generate();
3906 }
3907
3908 void call_schedule_impl() {
3909 pre_schedule();
3910 if constexpr (has_schedule_method_v<T>) {
3911 T *t = (T *)this;
3912 static_assert(std::is_void_v<decltype(t->schedule())>, "schedule() must return void");
3913 t->schedule();
3914 }
3915 post_schedule();
3916 }
3917
3918protected:
3921 return this->build_pipeline_impl();
3922 }
3923
3924 void call_configure() override {
3925 this->call_configure_impl();
3926 }
3927
3928 void call_generate() override {
3929 this->call_generate_impl();
3930 }
3931
3932 void call_schedule() override {
3933 this->call_schedule_impl();
3934 }
3935
3936private:
3937 friend void ::Halide::Internal::generator_test();
3938 friend void ::Halide::Internal::generator_test();
3939 friend class ::Halide::GeneratorContext;
3940
3941public:
3942 Generator(const Generator &) = delete;
3943 Generator &operator=(const Generator &) = delete;
3946};
3947
3948namespace Internal {
3949
3954
3955// -----------------------------
3956
3957/** ExecuteGeneratorArgs is the set of arguments to execute_generator().
3958 */
3960 // Output directory for all files generated. Must not be empty.
3961 std::string output_dir;
3962
3963 // Type(s) of outputs to produce. Must not be empty.
3964 std::set<OutputFileType> output_types;
3965
3966 // Target(s) to use when generating. Must not be empty.
3967 // If list contains multiple entries, a multitarget output will be produced.
3968 std::vector<Target> targets;
3969
3970 // When generating multitarget output, use these as the suffixes for each Target
3971 // specified by the targets field. If empty, the canonical string form of
3972 // each Target will be used. If nonempty, it must be the same length as the
3973 // targets vector.
3974 std::vector<std::string> suffixes;
3975
3976 // Name of the generator to execute (or empty if none, e.g. if generating a runtime)
3977 // Must be one recognized by the specified GeneratorFactoryProvider.
3978 std::string generator_name;
3979
3980 // Name to use for the generated function. May include C++ namespaces,
3981 // e.g. "HalideTest::AnotherNamespace::cxx_mangling". If empty, use `generator_name`.
3982 std::string function_name;
3983
3984 // Base filename for all outputs (differentated by file extension).
3985 // If empty, use `function_name` (ignoring any C++ namespaces).
3986 std::string file_base_name;
3987
3988 // The name of a standalone runtime to generate. Only honors EMIT_OPTIONS 'o'
3989 // and 'static_library'. When multiple targets are specified, it picks a
3990 // runtime that is compatible with all of the targets, or fails if it cannot
3991 // find one. Flags across all of the targets that do not affect runtime code
3992 // generation, such as `no_asserts` and `no_runtime`, are ignored.
3993 std::string runtime_name;
3994
3995 // The mode in which to build the Generator.
3997 // Build it as written.
3999
4000 // Build a version suitable for using for gradient descent calculation.
4003
4004 // The fn that will produce Generator(s) from the name specified.
4005 // (Note that `generator_name` is the only value that will ever be passed
4006 // for name here; it is provided for ease of interoperation with existing code.)
4007 //
4008 // If null, the default global registry of Generators will be used.
4009 using CreateGeneratorFn = std::function<AbstractGeneratorPtr(const std::string &name, const GeneratorContext &context)>;
4011
4012 // Values to substitute for GeneratorParams in the selected Generator.
4013 // Should not contain `target`.
4014 //
4015 // If any of the generator param names specified in this map are unknown
4016 // to the Generator created, an error will occur.
4018
4019 // Compiler Logger to use, for diagnostic work. If null, don't do any logging.
4021
4022 // If true, log the path of all output files to stdout.
4023 bool log_outputs = false;
4024};
4025
4026/**
4027 * Execute a Generator for AOT compilation -- this provides the implementation of
4028 * the command-line Generator interface `generate_filter_main()`, but with a structured
4029 * API that is more suitable for calling directly from code (vs command line).
4030 */
4032
4033// -----------------------------
4034
4035} // namespace Internal
4036
4037/** Create a Generator from the currently-registered Generators, use it to create a Callable.
4038 * Any GeneratorParams specified will be applied to the Generator before compilation.
4039 * If the name isn't registered, assert-fail. */
4040// @{
4042 const std::string &name,
4043 const GeneratorParamsMap &generator_params = {});
4045 const std::string &name,
4046 const GeneratorParamsMap &generator_params = {});
4047// @}
4048
4049} // namespace Halide
4050
4051// Define this namespace at global scope so that anonymous namespaces won't
4052// defeat our static_assert check; define a dummy type inside so we can
4053// check for type aliasing injected by anonymous namespace usage
4055struct halide_global_ns;
4056};
4057
4058#define _HALIDE_REGISTER_GENERATOR_IMPL(GEN_CLASS_NAME, GEN_REGISTRY_NAME, FULLY_QUALIFIED_STUB_NAME) \
4059 namespace halide_register_generator { \
4060 struct halide_global_ns; \
4061 namespace GEN_REGISTRY_NAME##_ns { \
4062 std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context); \
4063 std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context) { \
4064 using GenType = std::remove_pointer_t<decltype(new GEN_CLASS_NAME)>; /* NOLINT(bugprone-macro-parentheses) */ \
4065 return GenType::create(context, #GEN_REGISTRY_NAME, #FULLY_QUALIFIED_STUB_NAME); \
4066 } \
4067 } \
4068 namespace { \
4069 auto reg_##GEN_REGISTRY_NAME = Halide::Internal::RegisterGenerator(#GEN_REGISTRY_NAME, GEN_REGISTRY_NAME##_ns::factory); \
4070 } \
4071 } \
4072 static_assert(std::is_same_v<::halide_register_generator::halide_global_ns, halide_register_generator::halide_global_ns>, \
4073 "HALIDE_REGISTER_GENERATOR must be used at global scope");
4074
4075#define _HALIDE_REGISTER_GENERATOR2(GEN_CLASS_NAME, GEN_REGISTRY_NAME) \
4076 _HALIDE_REGISTER_GENERATOR_IMPL(GEN_CLASS_NAME, GEN_REGISTRY_NAME, GEN_REGISTRY_NAME)
4077
4078#define _HALIDE_REGISTER_GENERATOR3(GEN_CLASS_NAME, GEN_REGISTRY_NAME, FULLY_QUALIFIED_STUB_NAME) \
4079 _HALIDE_REGISTER_GENERATOR_IMPL(GEN_CLASS_NAME, GEN_REGISTRY_NAME, FULLY_QUALIFIED_STUB_NAME)
4080
4081// MSVC has a broken implementation of variadic macros: it expands __VA_ARGS__
4082// as a single token in argument lists (rather than multiple tokens).
4083// Jump through some hoops to work around this.
4084#define __HALIDE_REGISTER_ARGCOUNT_IMPL(_1, _2, _3, COUNT, ...) \
4085 COUNT
4086
4087#define _HALIDE_REGISTER_ARGCOUNT_IMPL(ARGS) \
4088 __HALIDE_REGISTER_ARGCOUNT_IMPL ARGS
4089
4090#define _HALIDE_REGISTER_ARGCOUNT(...) \
4091 _HALIDE_REGISTER_ARGCOUNT_IMPL((__VA_ARGS__, 3, 2, 1, 0))
4092
4093#define ___HALIDE_REGISTER_CHOOSER(COUNT) \
4094 _HALIDE_REGISTER_GENERATOR##COUNT
4095
4096#define __HALIDE_REGISTER_CHOOSER(COUNT) \
4097 ___HALIDE_REGISTER_CHOOSER(COUNT)
4098
4099#define _HALIDE_REGISTER_CHOOSER(COUNT) \
4100 __HALIDE_REGISTER_CHOOSER(COUNT)
4101
4102#define _HALIDE_REGISTER_GENERATOR_PASTE(A, B) \
4103 A B
4104
4105#define HALIDE_REGISTER_GENERATOR(...) \
4106 _HALIDE_REGISTER_GENERATOR_PASTE(_HALIDE_REGISTER_CHOOSER(_HALIDE_REGISTER_ARGCOUNT(__VA_ARGS__)), (__VA_ARGS__))
4107
4108// HALIDE_REGISTER_GENERATOR_ALIAS() can be used to create an an alias-with-a-particular-set-of-param-values
4109// for a given Generator in the build system. Normally, you wouldn't want to do this;
4110// however, some existing Halide clients have build systems that make it challenging to
4111// specify GeneratorParams inside the build system, and this allows a somewhat simpler
4112// customization route for them. It's highly recommended you don't use this for new code.
4113//
4114// The final argument is really an initializer-list of GeneratorParams, in the form
4115// of an initializer-list for map<string, string>:
4116//
4117// { { "gp-name", "gp-value"} [, { "gp2-name", "gp2-value" }] }
4118//
4119// It is specified as a variadic template argument to allow for the fact that the embedded commas
4120// would otherwise confuse the preprocessor; since (in this case) all we're going to do is
4121// pass it thru as-is, this is fine (and even MSVC's 'broken' __VA_ARGS__ should be OK here).
4122#define HALIDE_REGISTER_GENERATOR_ALIAS(GEN_REGISTRY_NAME, ORIGINAL_REGISTRY_NAME, ...) \
4123 namespace halide_register_generator { \
4124 struct halide_global_ns; \
4125 namespace ORIGINAL_REGISTRY_NAME##_ns { \
4126 std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context); \
4127 } \
4128 namespace GEN_REGISTRY_NAME##_ns { \
4129 std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context) { \
4130 auto g = ORIGINAL_REGISTRY_NAME##_ns::factory(context); \
4131 const Halide::GeneratorParamsMap m = __VA_ARGS__; \
4132 g->set_generatorparam_values(m); \
4133 return g; \
4134 } \
4135 } \
4136 namespace { \
4137 auto reg_##GEN_REGISTRY_NAME = Halide::Internal::RegisterGenerator(#GEN_REGISTRY_NAME, GEN_REGISTRY_NAME##_ns::factory); \
4138 } \
4139 } \
4140 static_assert(std::is_same_v<::halide_register_generator::halide_global_ns, halide_register_generator::halide_global_ns>, \
4141 "HALIDE_REGISTER_GENERATOR_ALIAS must be used at global scope");
4142
4143// The HALIDE_GENERATOR_PYSTUB macro is used to produce "PyStubs" -- i.e., CPython wrappers to let a C++ Generator
4144// be called from Python. It shouldn't be necessary to use by anything but the build system in most cases.
4145
4146#define HALIDE_GENERATOR_PYSTUB(GEN_REGISTRY_NAME, MODULE_NAME) \
4147 static_assert(PY_MAJOR_VERSION >= 3, "Python bindings for Halide require Python 3+"); \
4148 extern "C" PyObject *_halide_pystub_impl(const char *module_name, const Halide::Internal::GeneratorFactory &factory); \
4149 namespace halide_register_generator::GEN_REGISTRY_NAME##_ns { \
4150 extern std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context); \
4151 } \
4152 extern "C" HALIDE_EXPORT_SYMBOL PyObject *PyInit_##MODULE_NAME() { \
4153 const auto factory = halide_register_generator::GEN_REGISTRY_NAME##_ns::factory; \
4154 return _halide_pystub_impl(#MODULE_NAME, factory); \
4155 }
4156
4157#endif // HALIDE_GENERATOR_H_
#define internal_error
Definition Error.h:229
#define user_error
Definition Error.h:228
#define internal_assert(c)
Definition Error.h:232
#define user_assert(c)
Definition Error.h:233
Defines Func - the front-end handle on a halide function, and related classes.
#define HALIDE_GENERATOR_PARAM_TYPED_SETTER(TYPE)
Definition Generator.h:415
#define HALIDE_FORWARD_METHOD(Class, Method)
Definition Generator.h:1651
#define HALIDE_FORWARD_METHOD_CONST(Class, Method)
Definition Generator.h:1657
#define HALIDE_ALWAYS_INLINE
Classes for declaring image parameters to halide pipelines.
Provides a single global registry of Generators, GeneratorParams, and Params indexed by this pointer.
Defines the structure that describes a Halide target.
#define HALIDE_NO_USER_CODE_INLINE
Definition Util.h:47
Type type() const
Definition Buffer.h:534
bool defined() const
Check if this Buffer refers to an existing Buffer.
Definition Buffer.h:380
Helper class for identifying purpose of an Expr passed to memoize.
Definition Func.h:691
A halide function.
Definition Func.h:706
bool defined() const
Does this function have at least a pure definition.
int dimensions() const
The dimensionality (number of arguments) of this function.
const std::vector< Type > & types() const
Realization realize(std::vector< int32_t > sizes={}, const Target &target=Target())
Evaluate this function over some rectangular domain and return the resulting buffer or buffers.
const std::string & name() const
The name of this function, either given during construction, or automatically generated.
Func in(const Func &f)
Creates and returns a new identity Func that wraps this Func.
A fragment of front-end syntax of the form f(x, y, z), where x, y, z are Vars or Exprs.
Definition Func.h:494
GeneratorContext is a class that is used when using Generators (or Stubs) directly; it is used to all...
Definition Generator.h:3013
GeneratorContext with_target(const Target &t) const
GeneratorContext(const Target &t)
std::unique_ptr< T > apply(const Args &...args) const
Definition Generator.h:3044
std::unique_ptr< T > create() const
Definition Generator.h:3040
GeneratorContext & operator=(GeneratorContext &&)=default
GeneratorContext & operator=(const GeneratorContext &)=default
const Target & target() const
Definition Generator.h:3027
GeneratorContext(const Target &t, const AutoschedulerParams &autoscheduler_params)
GeneratorContext(const GeneratorContext &)=default
const AutoschedulerParams & autoscheduler_params() const
Definition Generator.h:3030
GeneratorContext(GeneratorContext &&)=default
void call_generate() override
Definition Generator.h:3928
Generator(Generator &&that)=delete
static std::unique_ptr< T > create(const Halide::GeneratorContext &context, const std::string &registered_name, const std::string &stub_name)
Definition Generator.h:3816
void call_schedule() override
Definition Generator.h:3932
std::unique_ptr< T2 > apply(const Args &...args) const
Definition Generator.h:3838
static std::unique_ptr< T > create(const Halide::GeneratorContext &context)
Definition Generator.h:3807
Generator & operator=(Generator &&that)=delete
Generator & operator=(const Generator &)=delete
void apply(const Args &...args)
Definition Generator.h:3825
void call_configure() override
Definition Generator.h:3924
std::unique_ptr< T2 > create() const
Definition Generator.h:3833
Pipeline build_pipeline() override
Build and return the Pipeline for this AbstractGenerator.
Definition Generator.h:3919
Generator(const Generator &)=delete
GeneratorInput(size_t array_size, const std::string &name, const Type &t)
Definition Generator.h:2247
GeneratorInput(const std::string &name, const TBase &def)
Definition Generator.h:2212
GeneratorInput(const std::string &name, const TBase &def, const TBase &min, const TBase &max)
Definition Generator.h:2220
typename Super::TBase TBase
Definition Generator.h:2192
Internal::select_type_t< Internal::cond< Internal::has_static_halide_type_method_v< TBase >, int >, Internal::cond< std::is_same_v< TBase, Func >, int >, Internal::cond< true, Unused > > IntIfNonScalar
Definition Generator.h:2202
GeneratorInput(size_t array_size, const std::string &name, const TBase &def, const TBase &min, const TBase &max)
Definition Generator.h:2225
GeneratorInput(size_t array_size, const std::string &name, IntIfNonScalar d)
Definition Generator.h:2253
GeneratorInput(const std::string &name, const Type &t)
Definition Generator.h:2234
GeneratorInput(size_t array_size, const std::string &name)
Definition Generator.h:2257
GeneratorInput(size_t array_size, const std::string &name, const Type &t, int d)
Definition Generator.h:2243
GeneratorInput(const std::string &name)
Definition Generator.h:2208
GeneratorInput(const std::string &name, const Type &t, int d)
Definition Generator.h:2230
GeneratorInput(size_t array_size, const std::string &name, const TBase &def)
Definition Generator.h:2216
GeneratorInput(const std::string &name, IntIfNonScalar d)
Definition Generator.h:2239
typename Super::TBase TBase
Definition Generator.h:2806
GeneratorOutput(const std::string &name)
Definition Generator.h:2812
GeneratorOutput(const std::string &name, const std::vector< Type > &t, int d)
Definition Generator.h:2840
GeneratorOutput< T > & operator=(const Internal::StubOutputBuffer< T2 > &stub_output_buffer)
Definition Generator.h:2874
GeneratorOutput(const char *name)
Definition Generator.h:2816
GeneratorOutput(const std::string &name, const std::vector< Type > &t)
Definition Generator.h:2832
GeneratorOutput(size_t array_size, const std::string &name, int d)
Definition Generator.h:2844
GeneratorOutput(size_t array_size, const std::string &name, const Type &t, int d)
Definition Generator.h:2856
GeneratorOutput(const std::string &name, const Type &t, int d)
Definition Generator.h:2836
GeneratorOutput< T > & operator=(Buffer< T2, D2 > &buffer)
Definition Generator.h:2868
GeneratorOutput(size_t array_size, const std::string &name, const std::vector< Type > &t, int d)
Definition Generator.h:2860
GeneratorOutput(const std::string &name, int d)
Definition Generator.h:2824
GeneratorOutput(size_t array_size, const std::string &name)
Definition Generator.h:2820
GeneratorOutput(const std::string &name, const Type &t)
Definition Generator.h:2828
GeneratorOutput(size_t array_size, const std::string &name, const std::vector< Type > &t)
Definition Generator.h:2852
GeneratorOutput(size_t array_size, const std::string &name, const Type &t)
Definition Generator.h:2848
GeneratorOutput< T > & operator=(const Func &f)
Definition Generator.h:2879
GeneratorParam is a templated class that can be used to modify the behavior of the Generator at code-...
Definition Generator.h:990
GeneratorParam(const std::string &name, const std::string &value)
Definition Generator.h:1005
GeneratorParam(const std::string &name, const T &value, const T &min, const T &max)
Definition Generator.h:997
GeneratorParam(const std::string &name, const T &value)
Definition Generator.h:993
GeneratorParam(const std::string &name, const T &value, const std::map< std::string, T > &enum_map)
Definition Generator.h:1001
An Image parameter to a halide pipeline.
Definition ImageParam.h:23
AbstractGenerator is an ABC that defines the API a Generator must provide to work with the existing G...
A reference-counted handle to Halide's internal representation of a function.
Definition Function.h:39
GIOBase is the base class for all GeneratorInput<> and GeneratorOutput<> instantiations; it is not pa...
Definition Generator.h:1444
const std::string & name() const
GIOBase & operator=(const GIOBase &)=delete
size_t array_size() const
virtual const char * input_or_output() const =0
GIOBase(size_t array_size, const std::string &name, ArgInfoKind kind, const std::vector< Type > &types, int dims)
void check_matching_dims(int d) const
ArgInfoKind kind() const
bool array_size_defined() const
const std::vector< Type > & gio_types() const
GIOBase & operator=(GIOBase &&)=delete
const std::vector< Func > & funcs() const
std::vector< Type > types_
Definition Generator.h:1486
void check_matching_types(const std::vector< Type > &t) const
std::string array_name(size_t i) const
virtual void check_value_writable() const =0
GIOBase(const GIOBase &)=delete
void check_matching_array_size(size_t size) const
GIOBase(GIOBase &&)=delete
void check_gio_access() const
void set_dimensions(int dims)
void set_array_size(int size)
std::vector< Func > funcs_
Definition Generator.h:1490
const std::string name_
Definition Generator.h:1484
const ArgInfoKind kind_
Definition Generator.h:1485
virtual bool is_array() const
virtual void verify_internals()
virtual ~GIOBase()=default
std::vector< Expr > exprs_
Definition Generator.h:1491
void set_type(const Type &type)
bool gio_types_defined() const
GeneratorBase * generator
Definition Generator.h:1498
const std::vector< Expr > & exprs() const
const std::vector< ElemType > & get_values() const
GeneratorContext context() const override
Return the Target and autoscheduler info that this Generator was created with.
std::string name() override
Return the name of this Generator.
bool allow_out_of_order_inputs_and_outputs() const override
By default, a Generator must declare all Inputs before all Outputs.
GeneratorParam< Target > target
Definition Generator.h:3538
void bind_input(const std::string &name, const std::vector< Parameter > &v) override
Rebind a specified Input to refer to the given piece of IR, replacing the default ImageParam / Param ...
GeneratorInput< T > * add_input(const std::string &name)
Definition Generator.h:3279
std::vector< Func > output_func(const std::string &name) override
Given the name of an output, return the Func(s) for that output.
void claim_name(const std::string &name, const char *param_type)
Definition Generator.h:3225
std::vector< Parameter > input_parameter(const std::string &name) override
Given the name of an input, return the Parameter(s) for that input.
void bind_input(const std::string &name, const std::vector< Func > &v) override
void bind_input(const std::string &name, const std::vector< Expr > &v) override
bool emit_hlpipe(const std::string &hlpipe_file_path) override
Emit a Serialized Halide Pipeline (.hlpipe) file to the given path.
Realization realize(Args &&...args)
Definition Generator.h:3208
GeneratorBase(const GeneratorBase &)=delete
GeneratorOutput< T > * add_output(const std::string &name)
Definition Generator.h:3395
int natural_vector_size() const
Given a data type, return an estimate of the "natural" vector size for that data type when compiling ...
Definition Generator.h:3176
virtual void init_from_context(const Halide::GeneratorContext &context)
GeneratorOutput< T > * add_output(const std::string &name, const Type &t)
Definition Generator.h:3388
void check_exact_phase(Phase expected_phase) const
void set_generatorparam_value(const std::string &name, const LoopLevel &loop_level) override
void check_min_phase(Phase expected_phase) const
void realize(Realization r)
Definition Generator.h:3213
enum Halide::Internal::GeneratorBase::Phase Created
void set_generator_names(const std::string &registered_name, const std::string &stub_name)
Realization realize(std::vector< int32_t > sizes)
Definition Generator.h:3200
GeneratorInput< T > * add_input(const std::string &name, int dimensions)
Definition Generator.h:3264
std::vector< ArgInfo > arginfos() override
Return a list of all the ArgInfos for this generator.
GeneratorInput< T > * add_input(const std::string &name, const Type &type)
Definition Generator.h:3305
void set_generatorparam_value(const std::string &name, const std::string &value) override
Set the value for a specific GeneratorParam for an AbstractGenerator instance.
GeneratorBase(GeneratorBase &&that)=delete
GeneratorOutput< T > * add_output(const std::string &name, int dimensions)
Definition Generator.h:3360
bool emit_cpp_stub(const std::string &stub_file_path) override
Emit a Generator Stub (.stub.h) file to the given path.
GeneratorOutput< T > * add_output(const std::string &name, const std::vector< Type > &t, int dimensions)
Definition Generator.h:3319
GeneratorOutput< T > * add_output(const std::string &name, const std::vector< Type > &t)
Definition Generator.h:3374
GeneratorBase & operator=(const GeneratorBase &)=delete
GeneratorBase & operator=(GeneratorBase &&that)=delete
void set_inputs(const Args &...args)
set_inputs is a variadic wrapper around set_inputs_vector, which makes usage much simpler in many cas...
Definition Generator.h:3191
GeneratorParam_AutoSchedulerParams autoscheduler_
Definition Generator.h:3539
HALIDE_NO_USER_CODE_INLINE void add_requirement(const Expr &condition, Args &&...error_args)
Definition Generator.h:3411
GeneratorInput< T > * add_input(const std::string &name, const Type &t, int dimensions)
Definition Generator.h:3236
void add_requirement(const Expr &condition, const std::vector< Expr > &error_args)
int natural_vector_size(Halide::Type t) const
Given a data type, return an estimate of the "natural" vector size for that data type when compiling ...
Definition Generator.h:3169
void advance_phase(Phase new_phase)
GeneratorOutput< T > * add_output(const std::string &name, const Type &t, int dimensions)
Definition Generator.h:3331
GeneratorFactoryProvider provides a way to customize the Generators that are visible to generate_filt...
Definition Generator.h:330
virtual AbstractGeneratorPtr create(const std::string &name, const Halide::GeneratorContext &context) const =0
Create an instance of the Generator that is registered under the given name.
GeneratorFactoryProvider(const GeneratorFactoryProvider &)=delete
GeneratorFactoryProvider & operator=(GeneratorFactoryProvider &&)=delete
GeneratorFactoryProvider(GeneratorFactoryProvider &&)=delete
GeneratorFactoryProvider & operator=(const GeneratorFactoryProvider &)=delete
virtual std::vector< std::string > enumerate() const =0
Return a list of all registered Generators that are available for use with the create() method.
GeneratorInput_Arithmetic(size_t array_size, const std::string &name)
Definition Generator.h:2136
GeneratorInput_Arithmetic(const std::string &name, const TBase &def, const TBase &min, const TBase &max)
Definition Generator.h:2147
GeneratorInput_Arithmetic(size_t array_size, const std::string &name, const TBase &def)
Definition Generator.h:2141
GeneratorInput_Arithmetic(const std::string &name)
Definition Generator.h:2127
GeneratorInput_Arithmetic(size_t array_size, const std::string &name, const TBase &def, const TBase &min, const TBase &max)
Definition Generator.h:2154
GeneratorInput_Arithmetic(const std::string &name, const TBase &def)
Definition Generator.h:2131
std::string get_c_type() const override
Definition Generator.h:1675
GeneratorInput_Buffer< T > & set_estimate(Var var, Expr min, Expr extent)
Definition Generator.h:1742
GeneratorInput_Buffer(const std::string &name, const Type &t)
Definition Generator.h:1703
GeneratorInput_Buffer(const std::string &name)
Definition Generator.h:1691
GeneratorInput_Buffer(const std::string &name, const Type &t, int d)
Definition Generator.h:1697
std::vector< ImageParam >::const_iterator end() const
Definition Generator.h:1800
Expr operator()(std::vector< Expr > args) const
Definition Generator.h:1721
std::vector< ImageParam >::const_iterator begin() const
Definition Generator.h:1794
Expr operator()(Args &&...args) const
Definition Generator.h:1716
Func in(const std::vector< Func > &others)
Definition Generator.h:1764
GeneratorInput_Buffer< T > & set_estimates(const Region &estimates)
Definition Generator.h:1748
ImageParam operator[](size_t i) const
Definition Generator.h:1782
ImageParam at(size_t i) const
Definition Generator.h:1788
GeneratorInput_Buffer(const std::string &name, int d)
Definition Generator.h:1708
std::string get_c_type() const override
Definition Generator.h:1958
GeneratorInput_DynamicScalar(const std::string &name)
Definition Generator.h:1963
GeneratorInput_Func(size_t array_size, const std::string &name, int d)
Definition Generator.h:1868
Expr operator()(Args &&...args) const
Definition Generator.h:1883
Func in(const std::vector< Func > &others)
Definition Generator.h:1925
GeneratorInput_Func< T > & set_estimates(const Region &estimates)
Definition Generator.h:1909
GeneratorInput_Func< T > & set_estimate(Var var, Expr min, Expr extent)
Definition Generator.h:1903
GeneratorInput_Func(size_t array_size, const std::string &name, const Type &t, int d)
Definition Generator.h:1863
GeneratorInput_Func(const std::string &name, int d)
Definition Generator.h:1849
GeneratorInput_Func(const std::string &name, const Type &t)
Definition Generator.h:1854
GeneratorInput_Func(size_t array_size, const std::string &name, const Type &t)
Definition Generator.h:1873
Expr operator()(const std::vector< Expr > &args) const
Definition Generator.h:1888
std::string get_c_type() const override
Definition Generator.h:1834
GeneratorInput_Func(const std::string &name, const Type &t, int d)
Definition Generator.h:1844
GeneratorInput_Func(const std::string &name)
Definition Generator.h:1859
GeneratorInput_Func(size_t array_size, const std::string &name)
Definition Generator.h:1878
GeneratorInput_Scalar(size_t array_size, const std::string &name)
Definition Generator.h:2039
static Expr TBaseToExpr(const TBase2 &value)
Definition Generator.h:2020
void set_estimate(const TBase &value)
Definition Generator.h:2065
void set_estimate(size_t index, const TBase &value)
Definition Generator.h:2087
GeneratorInput_Scalar(size_t array_size, const std::string &name, const TBase &def)
Definition Generator.h:2044
GeneratorInput_Scalar(const std::string &name)
Definition Generator.h:2031
GeneratorInput_Scalar(const std::string &name, const TBase &def)
Definition Generator.h:2035
std::string get_c_type() const override
Definition Generator.h:2013
void set_inputs(const std::vector< StubInput > &inputs)
virtual std::string get_c_type() const =0
void set_estimate_impl(const Var &var, const Expr &min, const Expr &extent)
std::vector< Parameter > parameters_
Definition Generator.h:1552
const char * input_or_output() const override
Definition Generator.h:1570
GeneratorInputBase(const std::string &name, ArgInfoKind kind, const std::vector< Type > &t, int d)
void set_estimates_impl(const Region &estimates)
void check_value_writable() const override
GeneratorInputBase(size_t array_size, const std::string &name, ArgInfoKind kind, const std::vector< Type > &t, int d)
std::remove_all_extents_t< T > TBase
Definition Generator.h:1584
const ValueType & operator[](size_t i) const
Definition Generator.h:1619
GeneratorInputImpl(const std::string &name, ArgInfoKind kind, const std::vector< Type > &t, int d)
Definition Generator.h:1593
const ValueType & at(size_t i) const
Definition Generator.h:1625
std::vector< ValueType >::const_iterator end() const
Definition Generator.h:1637
std::vector< ValueType >::const_iterator begin() const
Definition Generator.h:1631
GeneratorOutput_Arithmetic(const std::string &name)
Definition Generator.h:2782
GeneratorOutput_Arithmetic(size_t array_size, const std::string &name)
Definition Generator.h:2786
GeneratorOutput_Buffer(const std::string &name, int d)
Definition Generator.h:2545
GeneratorOutput_Buffer(size_t array_size, const std::string &name)
Definition Generator.h:2553
GeneratorOutput_Buffer(size_t array_size, const std::string &name, const std::vector< Type > &t, int d)
Definition Generator.h:2559
GeneratorOutput_Buffer< T > & operator=(const StubOutputBuffer< T2 > &stub_output_buffer)
Definition Generator.h:2632
GeneratorOutput_Buffer(const std::string &name)
Definition Generator.h:2525
HALIDE_NO_USER_CODE_INLINE std::string get_c_type() const override
Definition Generator.h:2581
GeneratorOutput_Buffer< T > & set_estimates(const Region &estimates)
Definition Generator.h:2656
HALIDE_NO_USER_CODE_INLINE T2 as() const
Definition Generator.h:2592
GeneratorOutput_Buffer(const std::string &name, const std::vector< Type > &t)
Definition Generator.h:2539
GeneratorOutput_Buffer(size_t array_size, const std::string &name, int d)
Definition Generator.h:2573
GeneratorOutput_Buffer< T > & operator=(const Func &f)
Definition Generator.h:2641
HALIDE_NO_USER_CODE_INLINE GeneratorOutput_Buffer< T > & operator=(Buffer< T2, D2 > &buffer)
Definition Generator.h:2604
const Func & operator[](size_t i) const
Definition Generator.h:2664
GeneratorOutput_Buffer(size_t array_size, const std::string &name, const std::vector< Type > &t)
Definition Generator.h:2567
GeneratorOutput_Buffer(const std::string &name, const std::vector< Type > &t, int d)
Definition Generator.h:2531
const Func & operator[](size_t i) const
Definition Generator.h:2750
GeneratorOutput_Func(const std::string &name)
Definition Generator.h:2707
GeneratorOutput_Func< T > & operator=(const Func &f)
Definition Generator.h:2730
GeneratorOutput_Func(const std::string &name, const std::vector< Type > &t, int d)
Definition Generator.h:2711
GeneratorOutput_Func(size_t array_size, const std::string &name, const std::vector< Type > &t, int d)
Definition Generator.h:2723
GeneratorOutput_Func(const std::string &name, int d)
Definition Generator.h:2719
GeneratorOutput_Func< T > & set_estimate(const Var &var, const Expr &min, const Expr &extent)
Definition Generator.h:2755
GeneratorOutput_Func(const std::string &name, const std::vector< Type > &t)
Definition Generator.h:2715
GeneratorOutput_Func< T > & set_estimates(const Region &estimates)
Definition Generator.h:2764
void set_type(const std::vector< Type > &types)
Set types dynamically for tuple outputs.
const char * input_or_output() const override
Definition Generator.h:2374
GeneratorOutputBase(const std::string &name, ArgInfoKind kind, const std::vector< Type > &t, int d)
virtual std::string get_c_type() const
Definition Generator.h:2368
HALIDE_NO_USER_CODE_INLINE T2 as() const
Definition Generator.h:2267
void check_value_writable() const override
GeneratorOutputBase(size_t array_size, const std::string &name, ArgInfoKind kind, const std::vector< Type > &t, int d)
std::vector< ValueType >::const_iterator end() const
Definition Generator.h:2463
const ValueType & operator[](size_t i) const
Definition Generator.h:2445
GeneratorOutputImpl(const std::string &name, ArgInfoKind kind, const std::vector< Type > &t, int d)
Definition Generator.h:2395
const ValueType & at(size_t i) const
Definition Generator.h:2451
std::vector< ValueType >::const_iterator begin() const
Definition Generator.h:2457
std::remove_all_extents_t< T > TBase
Definition Generator.h:2385
FuncRef operator()(std::vector< ExprOrVar > args) const
Definition Generator.h:2421
FuncRef operator()(Args &&...args) const
Definition Generator.h:2415
void set_from_string(const std::string &new_value_string) override
Definition Generator.h:742
std::string get_c_type() const override
Definition Generator.h:779
GeneratorParam_Arithmetic(const std::string &name, const T &value, const T &min=std::numeric_limits< T >::lowest(), const T &max=std::numeric_limits< T >::max())
Definition Generator.h:728
std::string get_default_value() const override
Definition Generator.h:759
std::string call_to_string(const std::string &v) const override
Definition Generator.h:773
void set_impl(const T &new_value) override
Definition Generator.h:737
void set_from_string(const std::string &new_value_string) override
std::string call_to_string(const std::string &v) const override
void set_from_string(const std::string &new_value_string) override
Definition Generator.h:808
std::string get_default_value() const override
Definition Generator.h:820
std::string call_to_string(const std::string &v) const override
Definition Generator.h:824
GeneratorParam_Bool(const std::string &name, const T &value)
Definition Generator.h:804
std::string get_c_type() const override
Definition Generator.h:830
std::string call_to_string(const std::string &v) const override
Definition Generator.h:856
std::string get_default_value() const override
Definition Generator.h:864
GeneratorParam_Enum(const std::string &name, const T &value, const std::map< std::string, T > &enum_map)
Definition Generator.h:838
void set_from_string(const std::string &new_value_string) override
Definition Generator.h:850
std::string get_c_type() const override
Definition Generator.h:860
std::string get_type_decls() const override
Definition Generator.h:868
GeneratorParam_LoopLevel(const std::string &name, const LoopLevel &value)
Definition Generator.h:653
std::string get_c_type() const override
Definition Generator.h:716
std::string call_to_string(const std::string &v) const override
Definition Generator.h:711
void set(const LoopLevel &value) override
Definition Generator.h:659
void set_from_string(const std::string &new_value_string) override
Definition Generator.h:679
std::string get_default_value() const override
Definition Generator.h:689
GeneratorParam_String(const std::string &name, const std::string &value)
Definition Generator.h:921
std::string get_c_type() const override
Definition Generator.h:936
void set_from_string(const std::string &new_value_string) override
Definition Generator.h:924
std::string get_default_value() const override
Definition Generator.h:928
std::string call_to_string(const std::string &v) const override
Definition Generator.h:932
std::string call_to_string(const std::string &v) const override
Definition Generator.h:2921
void set_from_string(const std::string &new_value_string) override
Definition Generator.h:2907
std::string get_default_value() const override
Definition Generator.h:2916
std::string get_c_type() const override
Definition Generator.h:2926
void set_from_string(const std::string &new_value_string) override
Definition Generator.h:617
GeneratorParam_Target(const std::string &name, const T &value)
Definition Generator.h:613
std::string get_c_type() const override
Definition Generator.h:631
std::string get_default_value() const override
Definition Generator.h:621
std::string call_to_string(const std::string &v) const override
Definition Generator.h:625
std::string get_type_decls() const override
Definition Generator.h:913
std::string get_c_type() const override
Definition Generator.h:905
std::string call_to_string(const std::string &v) const override
Definition Generator.h:901
std::string get_default_value() const override
Definition Generator.h:909
GeneratorParam_Type(const std::string &name, const T &value)
Definition Generator.h:897
virtual bool is_synthetic_param() const
Definition Generator.h:464
GeneratorParamBase(GeneratorParamBase &&)=delete
virtual std::string call_to_string(const std::string &v) const =0
void fail_wrong_type(const char *type)
virtual std::string get_type_decls() const
Definition Generator.h:458
GeneratorParamBase(const std::string &name)
virtual std::string get_default_value() const =0
void set(const std::string &new_value)
Definition Generator.h:437
GeneratorParamBase(const GeneratorParamBase &)=delete
virtual std::string get_c_type() const =0
virtual bool is_looplevel_param() const
Definition Generator.h:468
virtual void set_from_string(const std::string &value_string)=0
GeneratorParamBase & operator=(GeneratorParamBase &&)=delete
const std::string & name() const
Definition Generator.h:403
GeneratorParamBase & operator=(const GeneratorParamBase &)=delete
void set(const char *new_value)
Definition Generator.h:440
void set(const std::string &new_value)
Definition Generator.h:551
GeneratorParamImpl(const std::string &name, const T &value)
Definition Generator.h:510
virtual void set_impl(const T &new_value)
Definition Generator.h:557
const std::vector< Internal::GeneratorInputBase * > & inputs() const
Definition Generator.h:3155
const std::vector< Internal::GeneratorParamBase * > & generator_params() const
Definition Generator.h:3152
GeneratorParamInfo(GeneratorBase *generator, size_t size)
const std::vector< Internal::GeneratorOutputBase * > & outputs() const
Definition Generator.h:3158
GeneratorRegistry(const GeneratorRegistry &)=delete
static AbstractGeneratorPtr create(const std::string &name, const Halide::GeneratorContext &context)
GeneratorRegistry & operator=(GeneratorRegistry &&that)=delete
GeneratorRegistry(GeneratorRegistry &&that)=delete
GeneratorRegistry & operator=(const GeneratorRegistry &)=delete
static void register_factory(const std::string &name, GeneratorFactory generator_factory)
static std::vector< std::string > enumerate()
static void unregister_factory(const std::string &name)
RegisterGenerator(const char *registered_name, GeneratorFactory generator_factory)
StubInputBuffer is the placeholder that a Stub uses when it requires a Buffer for an input (rather th...
Definition Generator.h:1265
static std::vector< Parameter > to_parameter_vector(const StubInputBuffer< T2 > &t)
Definition Generator.h:1305
StubInputBuffer(const Buffer< T2, D2 > &b)
Definition Generator.h:1300
static std::vector< Parameter > to_parameter_vector(const std::vector< StubInputBuffer< T2 > > &v)
Definition Generator.h:1310
ArgInfoKind kind() const
Definition Generator.h:1406
StubInput(const StubInputBuffer< T2 > &b)
Definition Generator.h:1393
StubInput(const Parameter &p)
Definition Generator.h:1396
Parameter parameter() const
Definition Generator.h:1410
std::shared_ptr< AbstractGenerator > generator
Definition Generator.h:1325
Realization realize(Args &&...args)
Definition Generator.h:1336
StubOutputBufferBase(const Func &f, const std::shared_ptr< AbstractGenerator > &generator)
Realization realize(std::vector< int32_t > sizes)
StubOutputBuffer is the placeholder that a Stub uses when it requires a Buffer for an output (rather ...
Definition Generator.h:1359
static std::vector< StubOutputBuffer< T > > to_output_buffers(const std::vector< Func > &v, const std::shared_ptr< AbstractGenerator > &gen)
Definition Generator.h:1369
A reference to a site in a Halide statement at the top of the body of a particular for loop.
Definition Schedule.h:203
static LoopLevel root()
Construct a special LoopLevel value which represents the location outside of all for loops.
static LoopLevel inlined()
Construct a special LoopLevel value that implies that a function should be inlined away.
void set(const LoopLevel &other)
Mutate our contents to match the contents of 'other'.
bool is_root() const
bool is_inlined() const
LoopLevel & lock()
Halide::Target Target
Definition Generator.h:3075
static Type Bool(int lanes=1)
Definition Generator.h:3092
static Expr cast(Expr e)
Definition Generator.h:3080
static Expr cast(Halide::Type t, Expr e)
Definition Generator.h:3083
static Type UInt(int bits, int lanes=1)
Definition Generator.h:3101
static Type Int(int bits, int lanes=1)
Definition Generator.h:3098
static Type Float(int bits, int lanes=1)
Definition Generator.h:3095
Halide::Pipeline Pipeline
Definition Generator.h:3070
A handle on the output buffer of a pipeline.
A scalar parameter to a halide pipeline.
Definition Param.h:22
A reference-counted handle to a parameter to a halide pipeline.
Definition Parameter.h:40
void set_default_value(const Expr &e)
Get and set the default values for scalar parameters.
void set_estimate(Expr e)
A class representing a Halide pipeline.
Definition Pipeline.h:107
void trace_pipeline()
Generate begin_pipeline and end_pipeline tracing calls for this pipeline.
Realization realize(std::vector< int32_t > sizes={}, const Target &target=Target())
See Func::realize.
A multi-dimensional domain over which to iterate.
Definition RDom.h:193
A reduction variable represents a single dimension of a reduction domain (RDom).
Definition RDom.h:29
A Realization is a vector of references to existing Buffer objects.
Definition Realization.h:19
A single definition of a Func.
Definition Func.h:70
Create a small array of Exprs for defining and calling functions with multiple outputs.
Definition Tuple.h:18
A Halide variable, to be used when defining functions.
Definition Var.h:19
auto max_forward(const Other &a, const GeneratorParam< T > &b) -> decltype(max(a,(T) b))
Definition Generator.h:1207
auto min_forward(const Other &a, const GeneratorParam< T > &b) -> decltype(min(a,(T) b))
Definition Generator.h:1198
std::function< AbstractGeneratorPtr(const GeneratorContext &context)> GeneratorFactory
Definition Generator.h:3124
int generate_filter_main(int argc, char **argv)
generate_filter_main() is a convenient wrapper for GeneratorRegistry::create() + compile_to_files(); ...
std::string halide_type_to_enum_string(const Type &t)
Definition Generator.h:315
constexpr bool has_static_halide_type_method_v
Definition Generator.h:2173
ConstantInterval min(const ConstantInterval &a, const ConstantInterval &b)
std::vector< Expr > parameter_constraints(const Parameter &p)
typename select_type< Args... >::type select_type_t
Definition Generator.h:394
constexpr bool no_realizations_v
Definition Generator.h:3120
Expr make_const(Type t, int64_t val)
Construct an immediate of the given type from any numeric C++ type.
std::string halide_type_to_c_source(const Type &t)
select_type_t< cond< has_static_halide_type_method_v< TBase >, GeneratorOutput_Buffer< T > >, cond< std::is_same_v< TBase, Func >, GeneratorOutput_Func< T > >, cond< std::is_arithmetic_v< TBase >, GeneratorOutput_Arithmetic< T > > > GeneratorOutputImplBase
Definition Generator.h:2796
ConstantInterval max(const ConstantInterval &a, const ConstantInterval &b)
select_type_t< cond< has_static_halide_type_method_v< TBase >, GeneratorInput_Buffer< T > >, cond< std::is_same_v< TBase, Func >, GeneratorInput_Func< T > >, cond< std::is_arithmetic_v< TBase >, GeneratorInput_Arithmetic< T > >, cond< std::is_scalar_v< TBase >, GeneratorInput_Scalar< T > >, cond< std::is_same_v< TBase, Expr >, GeneratorInput_DynamicScalar< T > > > GeneratorInputImplBase
Definition Generator.h:2182
select_type_t< cond< std::is_same_v< T, Target >, GeneratorParam_Target< T > >, cond< std::is_same_v< T, LoopLevel >, GeneratorParam_LoopLevel >, cond< std::is_same_v< T, std::string >, GeneratorParam_String< T > >, cond< std::is_same_v< T, Type >, GeneratorParam_Type< T > >, cond< std::is_same_v< T, bool >, GeneratorParam_Bool< T > >, cond< std::is_arithmetic_v< T >, GeneratorParam_Arithmetic< T > >, cond< std::is_enum_v< T >, GeneratorParam_Enum< T > > > GeneratorParamImplBase
Definition Generator.h:950
HALIDE_NO_USER_CODE_INLINE std::string enum_to_string(const std::map< std::string, T > &enum_map, const T &t)
Definition Generator.h:297
std::vector< Type > parse_halide_type_list(const std::string &types)
std::string halide_type_to_c_type(const Type &t)
std::string print_loop_nest(const std::vector< Function > &output_funcs)
Emit some simple pseudocode that shows the structure of the loop nest specified by this pipeline's sc...
constexpr bool all_are_printable_args_v
Definition IROperator.h:353
std::unique_ptr< AbstractGenerator > AbstractGeneratorPtr
void execute_generator(const ExecuteGeneratorArgs &args)
Execute a Generator for AOT compilation – this provides the implementation of the command-line Genera...
HALIDE_NO_USER_CODE_INLINE void collect_print_args(std::vector< Expr > &args)
Definition IROperator.h:356
const GeneratorFactoryProvider & get_registered_generators()
Return a GeneratorFactoryProvider that knows about all the currently-registered C++ Generators.
T parse_scalar(const std::string &value)
Definition Generator.h:2888
const std::map< std::string, Halide::Type > & get_halide_type_enum_map()
T enum_from_string(const std::map< std::string, T > &enum_map, const std::string &s)
Definition Generator.h:308
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
auto operator>=(const Other &a, const GeneratorParam< T > &b) -> decltype(a >=(T) b)
Greater than or equal comparison between GeneratorParam<T> and any type that supports operator>= with...
Definition Generator.h:1105
Type UInt(int bits, int lanes=1)
Constructing an unsigned integer type.
Definition Type.h:546
Expr reinterpret(Type t, Expr e)
Reinterpret the bits of one value as another type.
Type Float(int bits, int lanes=1)
Construct a floating-point type.
Definition Type.h:551
auto operator==(const Other &a, const GeneratorParam< T > &b) -> decltype(a==(T) b)
Equality comparison between GeneratorParam<T> and any type that supports operator== with T.
Definition Generator.h:1131
@ Internal
Not visible externally, similar to 'static' linkage in C.
auto operator<(const Other &a, const GeneratorParam< T > &b) -> decltype(a<(T) b)
Less than comparison between GeneratorParam<T> and any type that supports operator< with T.
Definition Generator.h:1092
std::map< std::string, std::string > GeneratorParamsMap
auto operator*(const Other &a, const GeneratorParam< T > &b) -> decltype(a *(T) b)
Multiplication between GeneratorParam<T> and any type that supports operator* with T.
Definition Generator.h:1040
auto operator||(const Other &a, const GeneratorParam< T > &b) -> decltype(a||(T) b)
Logical or between between GeneratorParam<T> and any type that supports operator|| with T.
Definition Generator.h:1174
PrefetchBoundStrategy
Different ways to handle accesses outside the original extents in a prefetch.
auto operator-(const Other &a, const GeneratorParam< T > &b) -> decltype(a -(T) b)
Subtraction between GeneratorParam<T> and any type that supports operator- with T.
Definition Generator.h:1027
auto operator!(const GeneratorParam< T > &a) -> decltype(!(T) a)
Not operator for GeneratorParam.
Definition Generator.h:1246
TailStrategy
Different ways to handle a tail case in a split when the factor does not provably divide the extent.
Definition Schedule.h:33
std::function< std::unique_ptr< Internal::CompilerLogger >(const std::string &fn_name, const Target &target)> CompilerLoggerFactory
Definition Module.h:243
Type Int(int bits, int lanes=1)
Constructing a signed integer type.
Definition Type.h:541
auto operator+(const Other &a, const GeneratorParam< T > &b) -> decltype(a+(T) b)
Addition between GeneratorParam<T> and any type that supports operator+ with T.
Definition Generator.h:1014
Callable create_callable_from_generator(const GeneratorContext &context, const std::string &name, const GeneratorParamsMap &generator_params={})
Create a Generator from the currently-registered Generators, use it to create a Callable.
auto operator&&(const Other &a, const GeneratorParam< T > &b) -> decltype(a &&(T) b)
Logical and between between GeneratorParam<T> and any type that supports operator&& with T.
Definition Generator.h:1157
auto operator%(const Other &a, const GeneratorParam< T > &b) -> decltype(a %(T) b)
Modulo between GeneratorParam<T> and any type that supports operator% with T.
Definition Generator.h:1066
NameMangling
An enum to specify calling convention for extern stages.
Definition Function.h:26
auto operator<=(const Other &a, const GeneratorParam< T > &b) -> decltype(a<=(T) b)
Less than or equal comparison between GeneratorParam<T> and any type that supports operator<= with T.
Definition Generator.h:1118
auto operator>(const Other &a, const GeneratorParam< T > &b) -> decltype(a >(T) b)
Greater than comparison between GeneratorParam<T> and any type that supports operator> with T.
Definition Generator.h:1079
auto operator!=(const Other &a, const GeneratorParam< T > &b) -> decltype(a !=(T) b)
Inequality comparison between between GeneratorParam<T> and any type that supports operator!...
Definition Generator.h:1144
Internal::ConstantInterval cast(Type t, const Internal::ConstantInterval &a)
Cast operators for ConstantIntervals.
Type Bool(int lanes=1)
Construct a boolean type.
Definition Type.h:561
std::vector< Range > Region
A multi-dimensional box.
Definition Expr.h:350
auto operator/(const Other &a, const GeneratorParam< T > &b) -> decltype(a/(T) b)
Division between GeneratorParam<T> and any type that supports operator/ with T.
Definition Generator.h:1053
MemoryType
An enum describing different address spaces to be used with Func::store_in.
Definition Expr.h:353
Partition
Different ways to handle loops with a potentially optimizable boundary conditions.
unsigned __INT64_TYPE__ uint64_t
signed __INT64_TYPE__ int64_t
signed __INT32_TYPE__ int32_t
unsigned __INT8_TYPE__ uint8_t
unsigned __INT16_TYPE__ uint16_t
unsigned __INT32_TYPE__ uint32_t
signed __INT16_TYPE__ int16_t
signed __INT8_TYPE__ int8_t
Special the Autoscheduler to be used (if any), along with arbitrary additional arguments specific to ...
Definition Pipeline.h:48
A fragment of Halide syntax.
Definition Expr.h:258
HALIDE_ALWAYS_INLINE Type type() const
Get the type of this expression node.
Definition Expr.h:327
An argument to an extern-defined Func.
static TO2 value(const FROM &from)
Definition Generator.h:495
The Dim struct represents one loop in the schedule's representation of a loop nest.
Definition Schedule.h:439
ExecuteGeneratorArgs is the set of arguments to execute_generator().
Definition Generator.h:3959
CompilerLoggerFactory compiler_logger_factory
Definition Generator.h:4020
enum Halide::Internal::ExecuteGeneratorArgs::BuildMode build_mode
std::set< OutputFileType > output_types
Definition Generator.h:3964
std::vector< std::string > suffixes
Definition Generator.h:3974
std::function< AbstractGeneratorPtr(const std::string &name, const GeneratorContext &context)> CreateGeneratorFn
Definition Generator.h:4009
HALIDE_ALWAYS_INLINE bool defined() const
static constexpr bool value
Definition Generator.h:381
std::conditional_t< First::value, typename First::type, void > type
Definition Generator.h:390
A struct representing a target machine and os to generate code for.
Definition Target.h:19
int natural_vector_size(const Halide::Type &t) const
Given a data type, return an estimate of the "natural" vector size for that data type when compiling ...
Types in the halide type system.
Definition Type.h:283