Halide 17.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"
277#include "Introspection.h"
279#include "Target.h"
280
281#if !(__cplusplus >= 201703L || _MSVC_LANG >= 201703L)
282#error "Halide requires C++17 or later; please upgrade your compiler."
283#endif
284
285namespace Halide {
286
287class GeneratorContext;
288
289namespace Internal {
290
292
293class GeneratorBase;
294
295std::vector<Expr> parameter_constraints(const Parameter &p);
296
297template<typename T>
298HALIDE_NO_USER_CODE_INLINE std::string enum_to_string(const std::map<std::string, T> &enum_map, const T &t) {
299 for (const auto &key_value : enum_map) {
300 if (t == key_value.second) {
301 return key_value.first;
302 }
303 }
304 user_error << "Enumeration value not found.\n";
305 return "";
306}
307
308template<typename T>
309T enum_from_string(const std::map<std::string, T> &enum_map, const std::string &s) {
310 auto it = enum_map.find(s);
311 user_assert(it != enum_map.end()) << "Enumeration value not found: " << s << "\n";
312 return it->second;
313}
314
315extern const std::map<std::string, Halide::Type> &get_halide_type_enum_map();
316inline std::string halide_type_to_enum_string(const Type &t) {
318}
319
320// Convert a Halide Type into a string representation of its C source.
321// e.g., Int(32) -> "Halide::Int(32)"
322std::string halide_type_to_c_source(const Type &t);
323
324// Convert a Halide Type into a string representation of its C Source.
325// e.g., Int(32) -> "int32_t"
326std::string halide_type_to_c_type(const Type &t);
327
328/** GeneratorFactoryProvider provides a way to customize the Generators
329 * that are visible to generate_filter_main (which otherwise would just
330 * look at the global registry of C++ Generators). */
332public:
334 virtual ~GeneratorFactoryProvider() = default;
335
336 /** Return a list of all registered Generators that are available for use
337 * with the create() method. */
338 virtual std::vector<std::string> enumerate() const = 0;
339
340 /** Create an instance of the Generator that is registered under the given
341 * name. If the name isn't one returned by enumerate(), return nullptr
342 * rather than assert-fail; caller must check for a valid result. */
343 virtual AbstractGeneratorPtr create(const std::string &name,
344 const Halide::GeneratorContext &context) const = 0;
345
350};
351
352/** Return a GeneratorFactoryProvider that knows about all the currently-registered C++ Generators. */
354
355/** generate_filter_main() is a convenient wrapper for GeneratorRegistry::create() +
356 * compile_to_files(); it can be trivially wrapped by a "real" main() to produce a
357 * command-line utility for ahead-of-time filter compilation. */
358int generate_filter_main(int argc, char **argv);
359
360/** This overload of generate_filter_main lets you provide your own provider for how to enumerate and/or create
361 * the generators based on registration name; this is useful if you want to re-use the
362 * 'main' logic but avoid the global Generator registry (e.g. for bindings in languages
363 * other than C++). */
365
366// select_type<> is to std::conditional as switch is to if:
367// it allows a multiway compile-time type definition via the form
368//
369// select_type<cond<condition1, type1>,
370// cond<condition2, type2>,
371// ....
372// cond<conditionN, typeN>>::type
373//
374// Note that the conditions are evaluated in order; the first evaluating to true
375// is chosen.
376//
377// Note that if no conditions evaluate to true, the resulting type is illegal
378// and will produce a compilation error. (You can provide a default by simply
379// using cond<true, SomeType> as the final entry.)
380template<bool B, typename T>
381struct cond {
382 static constexpr bool value = B;
383 using type = T;
384};
385
386template<typename First, typename... Rest>
387struct select_type : std::conditional<First::value, typename First::type, typename select_type<Rest...>::type> {};
388
389template<typename First>
391 using type = typename std::conditional<First::value, typename First::type, void>::type;
392};
393
395
397public:
398 explicit GeneratorParamBase(const std::string &name);
400
401 inline const std::string &name() const {
402 return name_;
403 }
404
405 // overload the set() function to call the right virtual method based on type.
406 // This allows us to attempt to set a GeneratorParam via a
407 // plain C++ type, even if we don't know the specific templated
408 // subclass. Attempting to set the wrong type will assert.
409 // Notice that there is no typed setter for Enums, for obvious reasons;
410 // setting enums in an unknown type must fallback to using set_from_string.
411 //
412 // It's always a bit iffy to use macros for this, but IMHO it clarifies the situation here.
413#define HALIDE_GENERATOR_PARAM_TYPED_SETTER(TYPE) \
414 virtual void set(const TYPE &new_value) = 0;
415
428 HALIDE_GENERATOR_PARAM_TYPED_SETTER(AutoschedulerParams)
431
432#undef HALIDE_GENERATOR_PARAM_TYPED_SETTER
433
434 // Add overloads for string and char*
435 void set(const std::string &new_value) {
437 }
438 void set(const char *new_value) {
439 set_from_string(std::string(new_value));
440 }
441
442protected:
443 friend class GeneratorBase;
444 friend class GeneratorParamInfo;
445 friend class StubEmitter;
446
449
450 // All GeneratorParams are settable from string.
451 virtual void set_from_string(const std::string &value_string) = 0;
452
453 virtual std::string call_to_string(const std::string &v) const = 0;
454 virtual std::string get_c_type() const = 0;
455
456 virtual std::string get_type_decls() const {
457 return "";
458 }
459
460 virtual std::string get_default_value() const = 0;
461
462 virtual bool is_synthetic_param() const {
463 return false;
464 }
465
466 virtual bool is_looplevel_param() const {
467 return false;
468 }
469
470 void fail_wrong_type(const char *type);
471
472private:
473 const std::string name_;
474
475 // Generator which owns this GeneratorParam. Note that this will be null
476 // initially; the GeneratorBase itself will set this field when it initially
477 // builds its info about params. However, since it (generally) isn't
478 // appropriate for GeneratorParam<> to be declared outside of a Generator,
479 // all reasonable non-testing code should expect this to be non-null.
480 GeneratorBase *generator{nullptr};
481
482public:
487};
488
489// This is strictly some syntactic sugar to suppress certain compiler warnings.
490template<typename FROM, typename TO>
491struct Convert {
493 inline static TO2 value(const FROM &from) {
494 return static_cast<TO2>(from);
495 }
496
498 inline static TO2 value(const FROM &from) {
499 return from != 0;
500 }
501};
502
503template<typename T>
505public:
506 using type = T;
507
508 GeneratorParamImpl(const std::string &name, const T &value)
510 }
511
512 T value() const {
513 this->check_value_readable();
514 return value_;
515 }
516
517 operator T() const {
518 return this->value();
519 }
520
521 operator Expr() const {
522 return make_const(type_of<T>(), this->value());
523 }
524
525#define HALIDE_GENERATOR_PARAM_TYPED_SETTER(TYPE) \
526 void set(const TYPE &new_value) override { \
527 typed_setter_impl<TYPE>(new_value, #TYPE); \
528 }
529
545
546#undef HALIDE_GENERATOR_PARAM_TYPED_SETTER
547
548 // Overload for std::string.
549 void set(const std::string &new_value) {
552 }
553
554protected:
555 virtual void set_impl(const T &new_value) {
558 }
559
560 // Needs to be protected to allow GeneratorParam<LoopLevel>::set() override
562
563private:
564 // If FROM->T is not legal, fail
565 template<typename FROM, typename std::enable_if<
566 !std::is_convertible<FROM, T>::value>::type * = nullptr>
567 HALIDE_ALWAYS_INLINE void typed_setter_impl(const FROM &, const char *msg) {
568 fail_wrong_type(msg);
569 }
570
571 // If FROM and T are identical, just assign
572 template<typename FROM, typename std::enable_if<
573 std::is_same<FROM, T>::value>::type * = 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, typename std::enable_if<
581 !std::is_same<FROM, T>::value &&
582 std::is_convertible<FROM, T>::value &&
583 std::is_convertible<T, FROM>::value>::type * = 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, typename std::enable_if<
596 !std::is_same<FROM, T>::value &&
597 std::is_convertible<FROM, T>::value &&
598 !std::is_convertible<T, FROM>::value>::type * = 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<T, bool>::value) {
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<T, float>::value) {
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<T, float>::value) {
782 return "float";
783 } else if (std::is_same<T, double>::value) {
784 return "double";
785 } else if (std::is_integral<T>::value) {
786 if (std::is_unsigned<T>::value) {
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
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 (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 (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>
943 typename select_type<
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:
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 = typename std::remove_all_extents<T>::type;
1585
1586 bool is_array() const override {
1587 return std::is_array<T>::value;
1588 }
1589
1590 template<typename T2 = T, typename std::enable_if<
1591 // Only allow T2 not-an-array
1592 !std::is_array<T2>::value>::type * = 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, typename std::enable_if<
1598 // Only allow T2[kSomeConst]
1599 std::is_array<T2>::value && std::rank<T2>::value == 1 && (std::extent<T2, 0>::value > 0)>::type * = nullptr>
1600 GeneratorInputImpl(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d)
1601 : GeneratorInputBase(std::extent<T2, 0>::value, name, kind, t, d) {
1602 }
1603
1604 template<typename T2 = T, typename std::enable_if<
1605 // Only allow T2[]
1606 std::is_array<T2>::value && std::rank<T2>::value == 1 && std::extent<T2, 0>::value == 0>::type * = 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:
1613 size_t size() const {
1614 this->check_gio_access();
1615 return get_values<ValueType>().size();
1616 }
1617
1619 const ValueType &operator[](size_t i) const {
1620 this->check_gio_access();
1621 return get_values<ValueType>()[i];
1622 }
1623
1625 const ValueType &at(size_t i) const {
1626 this->check_gio_access();
1627 return get_values<ValueType>().at(i);
1628 }
1629
1631 typename std::vector<ValueType>::const_iterator begin() const {
1632 this->check_gio_access();
1633 return get_values<ValueType>().begin();
1634 }
1635
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)->typename std::remove_reference<decltype(std::declval<Class>().Method(std::forward<Args>(args)...))>::type { \
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-> \
1660 typename std::remove_reference<decltype(std::declval<Class>().Method(std::forward<Args>(args)...))>::type { \
1661 this->check_gio_access(); \
1662 return this->template as<Class>().Method(std::forward<Args>(args)...); \
1663 }
1664
1665template<typename T>
1667private:
1669
1670protected:
1671 using TBase = typename Super::TBase;
1672
1673 friend class ::Halide::Func;
1674 friend class ::Halide::Stage;
1675
1676 std::string get_c_type() const override {
1677 if (TBase::has_static_halide_type) {
1678 return "Halide::Internal::StubInputBuffer<" +
1679 halide_type_to_c_type(TBase::static_halide_type()) +
1680 ">";
1681 } else {
1682 return "Halide::Internal::StubInputBuffer<>";
1683 }
1684 }
1685
1686 template<typename T2>
1687 inline T2 as() const {
1688 return (T2) * this;
1689 }
1690
1691public:
1692 explicit GeneratorInput_Buffer(const std::string &name)
1694 TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
1695 TBase::has_static_dimensions ? TBase::static_dimensions() : -1) {
1696 }
1697
1698 GeneratorInput_Buffer(const std::string &name, const Type &t, int d)
1699 : Super(name, ArgInfoKind::Buffer, {t}, d) {
1700 static_assert(!TBase::has_static_halide_type, "You can only specify a Type argument for Input<Buffer<T>> if T is void or omitted.");
1701 static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Input<Buffer<T, D>> if D is -1 or omitted.");
1702 }
1703
1704 GeneratorInput_Buffer(const std::string &name, const Type &t)
1705 : Super(name, ArgInfoKind::Buffer, {t}, -1) {
1706 static_assert(!TBase::has_static_halide_type, "You can only specify a Type argument for Input<Buffer<T>> if T is void or omitted.");
1707 }
1708
1709 GeneratorInput_Buffer(const std::string &name, int d)
1711 TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
1712 d) {
1713 static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Input<Buffer<T, D>> if D is -1 or omitted.");
1714 }
1715
1716 template<typename... Args>
1717 Expr operator()(Args &&...args) const {
1718 this->check_gio_access();
1719 return Func(*this)(std::forward<Args>(args)...);
1720 }
1721
1722 Expr operator()(std::vector<Expr> args) const {
1723 this->check_gio_access();
1724 return Func(*this)(std::move(args));
1725 }
1726
1727 template<typename T2>
1728 operator StubInputBuffer<T2>() const {
1729 user_assert(!this->is_array()) << "Cannot assign an array type to a non-array type for Input " << this->name();
1730 return StubInputBuffer<T2>(this->parameters_.at(0));
1731 }
1732
1733 operator Func() const {
1734 this->check_gio_access();
1735 return this->funcs().at(0);
1736 }
1737
1738 operator ExternFuncArgument() const {
1739 this->check_gio_access();
1740 return ExternFuncArgument(this->parameters_.at(0));
1741 }
1742
1744 this->check_gio_access();
1745 this->set_estimate_impl(var, min, extent);
1746 return *this;
1747 }
1748
1750 this->check_gio_access();
1751 this->set_estimates_impl(estimates);
1752 return *this;
1753 }
1754
1756 this->check_gio_access();
1757 return Func(*this).in();
1758 }
1759
1760 Func in(const Func &other) {
1761 this->check_gio_access();
1762 return Func(*this).in(other);
1763 }
1764
1765 Func in(const std::vector<Func> &others) {
1766 this->check_gio_access();
1767 return Func(*this).in(others);
1768 }
1769
1770 operator ImageParam() const {
1771 this->check_gio_access();
1772 user_assert(!this->is_array()) << "Cannot convert an Input<Buffer<>[]> to an ImageParam; use an explicit subscript operator: " << this->name();
1773 return ImageParam(this->parameters_.at(0), Func(*this));
1774 }
1775
1777 size_t size() const {
1778 this->check_gio_access();
1779 return this->parameters_.size();
1780 }
1781
1783 ImageParam operator[](size_t i) const {
1784 this->check_gio_access();
1785 return ImageParam(this->parameters_.at(i), this->funcs().at(i));
1786 }
1787
1789 ImageParam at(size_t i) const {
1790 this->check_gio_access();
1791 return ImageParam(this->parameters_.at(i), this->funcs().at(i));
1792 }
1793
1795 typename std::vector<ImageParam>::const_iterator begin() const {
1796 user_error << "Input<Buffer<>>::begin() is not supported.";
1797 return {};
1798 }
1799
1801 typename std::vector<ImageParam>::const_iterator end() const {
1802 user_error << "Input<Buffer<>>::end() is not supported.";
1803 return {};
1804 }
1805
1806 /** Forward methods to the ImageParam. */
1807 // @{
1811 HALIDE_FORWARD_METHOD(ImageParam, set_host_alignment)
1824 // }@
1825};
1826
1827template<typename T>
1829private:
1831
1832protected:
1833 using TBase = typename Super::TBase;
1834
1835 std::string get_c_type() const override {
1836 return "Func";
1837 }
1838
1839 template<typename T2>
1840 inline T2 as() const {
1841 return (T2) * this;
1842 }
1843
1844public:
1845 GeneratorInput_Func(const std::string &name, const Type &t, int d)
1846 : Super(name, ArgInfoKind::Function, {t}, d) {
1847 }
1848
1849 // unspecified type
1850 GeneratorInput_Func(const std::string &name, int d)
1851 : Super(name, ArgInfoKind::Function, {}, d) {
1852 }
1853
1854 // unspecified dimension
1855 GeneratorInput_Func(const std::string &name, const Type &t)
1856 : Super(name, ArgInfoKind::Function, {t}, -1) {
1857 }
1858
1859 // unspecified type & dimension
1860 explicit GeneratorInput_Func(const std::string &name)
1861 : Super(name, ArgInfoKind::Function, {}, -1) {
1862 }
1863
1864 GeneratorInput_Func(size_t array_size, const std::string &name, const Type &t, int d)
1865 : Super(array_size, name, ArgInfoKind::Function, {t}, d) {
1866 }
1867
1868 // unspecified type
1869 GeneratorInput_Func(size_t array_size, const std::string &name, int d)
1871 }
1872
1873 // unspecified dimension
1874 GeneratorInput_Func(size_t array_size, const std::string &name, const Type &t)
1875 : Super(array_size, name, ArgInfoKind::Function, {t}, -1) {
1876 }
1877
1878 // unspecified type & dimension
1879 GeneratorInput_Func(size_t array_size, const std::string &name)
1880 : Super(array_size, name, ArgInfoKind::Function, {}, -1) {
1881 }
1882
1883 template<typename... Args>
1884 Expr operator()(Args &&...args) const {
1885 this->check_gio_access();
1886 return this->funcs().at(0)(std::forward<Args>(args)...);
1887 }
1888
1889 Expr operator()(const std::vector<Expr> &args) const {
1890 this->check_gio_access();
1891 return this->funcs().at(0)(args);
1892 }
1893
1894 operator Func() const {
1895 this->check_gio_access();
1896 return this->funcs().at(0);
1897 }
1898
1899 operator ExternFuncArgument() const {
1900 this->check_gio_access();
1901 return ExternFuncArgument(this->parameters_.at(0));
1902 }
1903
1905 this->check_gio_access();
1906 this->set_estimate_impl(var, min, extent);
1907 return *this;
1908 }
1909
1911 this->check_gio_access();
1912 this->set_estimates_impl(estimates);
1913 return *this;
1914 }
1915
1917 this->check_gio_access();
1918 return Func(*this).in();
1919 }
1920
1921 Func in(const Func &other) {
1922 this->check_gio_access();
1923 return Func(*this).in(other);
1924 }
1925
1926 Func in(const std::vector<Func> &others) {
1927 this->check_gio_access();
1928 return Func(*this).in(others);
1929 }
1930
1931 /** Forward const methods to the underlying Func. (Non-const methods
1932 * aren't available for Input<Func>.) */
1933 // @{
1937 HALIDE_FORWARD_METHOD_CONST(Func, has_update_definition)
1938 HALIDE_FORWARD_METHOD_CONST(Func, num_update_definitions)
1943 HALIDE_FORWARD_METHOD_CONST(Func, update_args)
1944 HALIDE_FORWARD_METHOD_CONST(Func, update_value)
1945 HALIDE_FORWARD_METHOD_CONST(Func, update_values)
1948 // }@
1949};
1950
1951template<typename T>
1953private:
1955
1956 static_assert(std::is_same<typename std::remove_all_extents<T>::type, Expr>::value, "GeneratorInput_DynamicScalar is only legal to use with T=Expr for now");
1957
1958protected:
1959 std::string get_c_type() const override {
1960 return "Expr";
1961 }
1962
1963public:
1964 explicit GeneratorInput_DynamicScalar(const std::string &name)
1965 : Super(name, ArgInfoKind::Scalar, {}, 0) {
1966 user_assert(!std::is_array<T>::value) << "Input<Expr[]> is not allowed";
1967 }
1968
1969 /** You can use this Input as an expression in a halide
1970 * function definition */
1971 operator Expr() const {
1972 this->check_gio_access();
1973 return this->exprs().at(0);
1974 }
1975
1976 /** Using an Input as the argument to an external stage treats it
1977 * as an Expr */
1978 operator ExternFuncArgument() const {
1979 this->check_gio_access();
1980 return ExternFuncArgument(this->exprs().at(0));
1981 }
1982
1983 void set_estimate(const Expr &value) {
1984 this->check_gio_access();
1985 for (Parameter &p : this->parameters_) {
1986 p.set_estimate(value);
1987 }
1988 }
1989
1990 Type type() const {
1991 return Expr(*this).type();
1992 }
1993};
1994
1995template<typename T>
1997private:
1999
2000protected:
2001 using TBase = typename Super::TBase;
2002
2003 const TBase def_{TBase()};
2005
2006 void set_def_min_max() override {
2007 for (Parameter &p : this->parameters_) {
2008 // No: we want to leave the Parameter unset here.
2009 // p.set_scalar<TBase>(def_);
2011 }
2012 }
2013
2014 std::string get_c_type() const override {
2015 return "Expr";
2016 }
2017
2018 // Expr() doesn't accept a pointer type in its ctor; add a SFINAE adapter
2019 // so that pointer (aka handle) Inputs will get cast to uint64.
2021 static Expr TBaseToExpr(const TBase2 &value) {
2022 return cast<TBase>(Expr(value));
2023 }
2024
2026 static Expr TBaseToExpr(const TBase2 &value) {
2027 user_assert(value == 0) << "Zero is the only legal default value for Inputs which are pointer types.\n";
2028 return Expr();
2029 }
2030
2031public:
2032 explicit GeneratorInput_Scalar(const std::string &name)
2033 : Super(name, ArgInfoKind::Scalar, {type_of<TBase>()}, 0), def_(static_cast<TBase>(0)), def_expr_(Expr()) {
2034 }
2035
2036 GeneratorInput_Scalar(const std::string &name, const TBase &def)
2038 }
2039
2041 const std::string &name)
2042 : Super(array_size, name, ArgInfoKind::Scalar, {type_of<TBase>()}, 0), def_(static_cast<TBase>(0)), def_expr_(Expr()) {
2043 }
2044
2046 const std::string &name,
2047 const TBase &def)
2049 }
2050
2051 /** You can use this Input as an expression in a halide
2052 * function definition */
2053 operator Expr() const {
2054 this->check_gio_access();
2055 return this->exprs().at(0);
2056 }
2057
2058 /** Using an Input as the argument to an external stage treats it
2059 * as an Expr */
2060 operator ExternFuncArgument() const {
2061 this->check_gio_access();
2062 return ExternFuncArgument(this->exprs().at(0));
2063 }
2064
2066 void set_estimate(const TBase &value) {
2067 this->check_gio_access();
2068 user_assert(value == nullptr) << "nullptr is the only valid estimate for Input<PointerType>";
2070 for (Parameter &p : this->parameters_) {
2071 p.set_estimate(e);
2072 }
2073 }
2074
2076 void set_estimate(const TBase &value) {
2077 this->check_gio_access();
2078 Expr e = Expr(value);
2079 if (std::is_same<T2, bool>::value) {
2080 e = cast<bool>(e);
2081 }
2082 for (Parameter &p : this->parameters_) {
2083 p.set_estimate(e);
2084 }
2085 }
2086
2088 void set_estimate(size_t index, const TBase &value) {
2089 this->check_gio_access();
2090 Expr e = Expr(value);
2091 if (std::is_same<T2, bool>::value) {
2092 e = cast<bool>(e);
2093 }
2094 this->parameters_.at(index).set_estimate(e);
2095 }
2096
2097 Type type() const {
2098 return Expr(*this).type();
2099 }
2100};
2101
2102template<typename T>
2104private:
2106
2107protected:
2108 using TBase = typename Super::TBase;
2109
2111
2112 void set_def_min_max() override {
2114 // Don't set min/max for bool
2115 if (!std::is_same<TBase, bool>::value) {
2116 for (Parameter &p : this->parameters_) {
2117 if (min_.defined()) {
2118 p.set_min_value(min_);
2119 }
2120 if (max_.defined()) {
2121 p.set_max_value(max_);
2122 }
2123 }
2124 }
2125 }
2126
2127public:
2128 explicit GeneratorInput_Arithmetic(const std::string &name)
2129 : Super(name), min_(Expr()), max_(Expr()) {
2130 }
2131
2133 const TBase &def)
2134 : Super(name, def), min_(Expr()), max_(Expr()) {
2135 }
2136
2138 const std::string &name)
2139 : Super(array_size, name), min_(Expr()), max_(Expr()) {
2140 }
2141
2143 const std::string &name,
2144 const TBase &def)
2145 : Super(array_size, name, def), min_(Expr()), max_(Expr()) {
2146 }
2147
2149 const TBase &def,
2150 const TBase &min,
2151 const TBase &max)
2152 : Super(name, def), min_(min), max_(max) {
2153 }
2154
2156 const std::string &name,
2157 const TBase &def,
2158 const TBase &min,
2159 const TBase &max)
2160 : Super(array_size, name, def), min_(min), max_(max) {
2161 }
2162};
2163
2164template<typename>
2166 typedef void type;
2167};
2168
2169template<typename T2, typename = void>
2170struct has_static_halide_type_method : std::false_type {};
2171
2172template<typename T2>
2173struct has_static_halide_type_method<T2, typename type_sink<decltype(T2::static_halide_type())>::type> : std::true_type {};
2174
2177 typename select_type<
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;
2199 typename Internal::select_type<
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, typename std::enable_if<std::is_same<TBase, Func>::value>::type * = 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:
2268 static_assert(std::is_same<T2, Func>::value, "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)
2285 HALIDE_FORWARD_METHOD(Func, bound_extent)
2286 HALIDE_FORWARD_METHOD(Func, compute_at)
2287 HALIDE_FORWARD_METHOD(Func, compute_inline)
2288 HALIDE_FORWARD_METHOD(Func, compute_root)
2289 HALIDE_FORWARD_METHOD(Func, compute_with)
2290 HALIDE_FORWARD_METHOD(Func, copy_to_device)
2291 HALIDE_FORWARD_METHOD(Func, copy_to_host)
2292 HALIDE_FORWARD_METHOD(Func, define_extern)
2295 HALIDE_FORWARD_METHOD(Func, fold_storage)
2298 HALIDE_FORWARD_METHOD(Func, gpu_blocks)
2299 HALIDE_FORWARD_METHOD(Func, gpu_single_thread)
2300 HALIDE_FORWARD_METHOD(Func, gpu_threads)
2301 HALIDE_FORWARD_METHOD(Func, gpu_tile)
2302 HALIDE_FORWARD_METHOD_CONST(Func, has_update_definition)
2303 HALIDE_FORWARD_METHOD(Func, hexagon)
2305 HALIDE_FORWARD_METHOD(Func, memoize)
2306 HALIDE_FORWARD_METHOD_CONST(Func, num_update_definitions)
2308 HALIDE_FORWARD_METHOD(Func, parallel)
2309 HALIDE_FORWARD_METHOD(Func, prefetch)
2312 HALIDE_FORWARD_METHOD(Func, reorder)
2313 HALIDE_FORWARD_METHOD(Func, reorder_storage)
2316 HALIDE_FORWARD_METHOD(Func, set_estimate)
2317 HALIDE_FORWARD_METHOD(Func, specialize)
2318 HALIDE_FORWARD_METHOD(Func, specialize_fail)
2320 HALIDE_FORWARD_METHOD(Func, store_at)
2321 HALIDE_FORWARD_METHOD(Func, store_root)
2323 HALIDE_FORWARD_METHOD(Func, trace_stores)
2328 HALIDE_FORWARD_METHOD_CONST(Func, update_args)
2329 HALIDE_FORWARD_METHOD_CONST(Func, update_value)
2330 HALIDE_FORWARD_METHOD_CONST(Func, update_values)
2333 HALIDE_FORWARD_METHOD(Func, vectorize)
2334
2335 // }@
2336
2337#undef HALIDE_OUTPUT_FORWARD
2338#undef HALIDE_OUTPUT_FORWARD_CONST
2339
2340protected:
2342 const std::string &name,
2344 const std::vector<Type> &t,
2345 int d);
2346
2347 GeneratorOutputBase(const std::string &name,
2349 const std::vector<Type> &t,
2350 int d);
2351
2352 friend class GeneratorBase;
2353 friend class StubEmitter;
2354
2356 void resize(size_t size);
2357
2358 virtual std::string get_c_type() const {
2359 return "Func";
2360 }
2361
2362 void check_value_writable() const override;
2363
2364 const char *input_or_output() const override {
2365 return "Output";
2366 }
2367
2368public:
2370};
2371
2372template<typename T>
2374protected:
2375 using TBase = typename std::remove_all_extents<T>::type;
2377
2378 bool is_array() const override {
2379 return std::is_array<T>::value;
2380 }
2381
2382 template<typename T2 = T, typename std::enable_if<
2383 // Only allow T2 not-an-array
2384 !std::is_array<T2>::value>::type * = nullptr>
2385 GeneratorOutputImpl(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d)
2386 : GeneratorOutputBase(name, kind, t, d) {
2387 }
2388
2389 template<typename T2 = T, typename std::enable_if<
2390 // Only allow T2[kSomeConst]
2391 std::is_array<T2>::value && std::rank<T2>::value == 1 && (std::extent<T2, 0>::value > 0)>::type * = nullptr>
2392 GeneratorOutputImpl(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d)
2393 : GeneratorOutputBase(std::extent<T2, 0>::value, name, kind, t, d) {
2394 }
2395
2396 template<typename T2 = T, typename std::enable_if<
2397 // Only allow T2[]
2398 std::is_array<T2>::value && std::rank<T2>::value == 1 && std::extent<T2, 0>::value == 0>::type * = nullptr>
2399 GeneratorOutputImpl(const std::string &name, ArgInfoKind kind, const std::vector<Type> &t, int d)
2400 : GeneratorOutputBase(-1, name, kind, t, d) {
2401 }
2402
2403public:
2404 template<typename... Args, typename T2 = T, typename std::enable_if<!std::is_array<T2>::value>::type * = nullptr>
2405 FuncRef operator()(Args &&...args) const {
2406 this->check_gio_access();
2407 return get_values<ValueType>().at(0)(std::forward<Args>(args)...);
2408 }
2409
2411 FuncRef operator()(std::vector<ExprOrVar> args) const {
2412 this->check_gio_access();
2413 return get_values<ValueType>().at(0)(args);
2414 }
2415
2417 operator Func() const {
2418 this->check_gio_access();
2419 return get_values<ValueType>().at(0);
2420 }
2421
2423 operator Stage() const {
2424 this->check_gio_access();
2425 return get_values<ValueType>().at(0);
2426 }
2427
2429 size_t size() const {
2430 this->check_gio_access();
2431 return get_values<ValueType>().size();
2432 }
2433
2435 const ValueType &operator[](size_t i) const {
2436 this->check_gio_access();
2437 return get_values<ValueType>()[i];
2438 }
2439
2441 const ValueType &at(size_t i) const {
2442 this->check_gio_access();
2443 return get_values<ValueType>().at(i);
2444 }
2445
2447 typename std::vector<ValueType>::const_iterator begin() const {
2448 this->check_gio_access();
2449 return get_values<ValueType>().begin();
2450 }
2451
2453 typename std::vector<ValueType>::const_iterator end() const {
2454 this->check_gio_access();
2455 return get_values<ValueType>().end();
2456 }
2457
2458 template<typename T2 = T, typename std::enable_if<
2459 // Only allow T2[]
2460 std::is_array<T2>::value && std::rank<T2>::value == 1 && std::extent<T2, 0>::value == 0>::type * = nullptr>
2461 void resize(size_t size) {
2462 this->check_gio_access();
2464 }
2465};
2466
2467template<typename T>
2469private:
2471
2472 HALIDE_NO_USER_CODE_INLINE void assign_from_func(const Func &f) {
2473 this->check_value_writable();
2474
2476
2477 if (this->gio_types_defined()) {
2478 const auto &my_types = this->gio_types();
2479 user_assert(my_types.size() == f.types().size())
2480 << "Cannot assign Func \"" << f.name()
2481 << "\" to Output \"" << this->name() << "\"\n"
2482 << "Output " << this->name()
2483 << " is declared to have " << my_types.size() << " tuple elements"
2484 << " but Func " << f.name()
2485 << " has " << f.types().size() << " tuple elements.\n";
2486 for (size_t i = 0; i < my_types.size(); i++) {
2487 user_assert(my_types[i] == f.types().at(i))
2488 << "Cannot assign Func \"" << f.name()
2489 << "\" to Output \"" << this->name() << "\"\n"
2490 << (my_types.size() > 1 ? "In tuple element " + std::to_string(i) + ", " : "")
2491 << "Output " << this->name()
2492 << " has declared type " << my_types[i]
2493 << " but Func " << f.name()
2494 << " has type " << f.types().at(i) << "\n";
2495 }
2496 }
2497 if (this->dims_defined()) {
2498 user_assert(f.dimensions() == this->dims())
2499 << "Cannot assign Func \"" << f.name()
2500 << "\" to Output \"" << this->name() << "\"\n"
2501 << "Output " << this->name()
2502 << " has declared dimensionality " << this->dims()
2503 << " but Func " << f.name()
2504 << " has dimensionality " << f.dimensions() << "\n";
2505 }
2506
2507 internal_assert(this->exprs_.empty() && this->funcs_.size() == 1);
2508 user_assert(!this->funcs_.at(0).defined());
2509 this->funcs_[0] = f;
2510 }
2511
2512protected:
2513 using TBase = typename Super::TBase;
2514
2515 explicit GeneratorOutput_Buffer(const std::string &name)
2517 TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
2518 TBase::has_static_dimensions ? TBase::static_dimensions() : -1) {
2519 }
2520
2521 GeneratorOutput_Buffer(const std::string &name, const std::vector<Type> &t, int d)
2522 : Super(name, ArgInfoKind::Buffer, t, d) {
2523 internal_assert(!t.empty());
2524 internal_assert(d != -1);
2525 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.");
2526 static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Output<Buffer<T, D>> if D is -1 or omitted.");
2527 }
2528
2529 GeneratorOutput_Buffer(const std::string &name, const std::vector<Type> &t)
2530 : Super(name, ArgInfoKind::Buffer, t, -1) {
2531 internal_assert(!t.empty());
2532 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.");
2533 }
2534
2535 GeneratorOutput_Buffer(const std::string &name, int d)
2537 TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
2538 d) {
2539 internal_assert(d != -1);
2540 static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Output<Buffer<T, D>> if D is -1 or omitted.");
2541 }
2542
2543 GeneratorOutput_Buffer(size_t array_size, const std::string &name)
2545 TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
2546 TBase::has_static_dimensions ? TBase::static_dimensions() : -1) {
2547 }
2548
2549 GeneratorOutput_Buffer(size_t array_size, const std::string &name, const std::vector<Type> &t, int d)
2550 : Super(array_size, name, ArgInfoKind::Buffer, t, d) {
2551 internal_assert(!t.empty());
2552 internal_assert(d != -1);
2553 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.");
2554 static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Output<Buffer<T, D>> if D is -1 or omitted.");
2555 }
2556
2557 GeneratorOutput_Buffer(size_t array_size, const std::string &name, const std::vector<Type> &t)
2558 : Super(array_size, name, ArgInfoKind::Buffer, t, -1) {
2559 internal_assert(!t.empty());
2560 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.");
2561 }
2562
2563 GeneratorOutput_Buffer(size_t array_size, const std::string &name, int d)
2565 TBase::has_static_halide_type ? std::vector<Type>{TBase::static_halide_type()} : std::vector<Type>{},
2566 d) {
2567 internal_assert(d != -1);
2568 static_assert(!TBase::has_static_dimensions, "You can only specify a dimension argument for Output<Buffer<T, D>> if D is -1 or omitted.");
2569 }
2570
2571 HALIDE_NO_USER_CODE_INLINE std::string get_c_type() const override {
2572 if (TBase::has_static_halide_type) {
2573 return "Halide::Internal::StubOutputBuffer<" +
2574 halide_type_to_c_type(TBase::static_halide_type()) +
2575 ">";
2576 } else {
2577 return "Halide::Internal::StubOutputBuffer<>";
2578 }
2579 }
2580
2583 return (T2) * this;
2584 }
2585
2586public:
2587 // Allow assignment from a Buffer<> to an Output<Buffer<>>;
2588 // this allows us to use a statically-compiled buffer inside a Generator
2589 // to assign to an output.
2590 // TODO: This used to take the buffer as a const ref. This no longer works as
2591 // using it in a Pipeline might change the dev field so it is currently
2592 // not considered const. We should consider how this really ought to work.
2593 template<typename T2, int D2>
2595 this->check_gio_access();
2596 this->check_value_writable();
2597
2598 user_assert(T::can_convert_from(buffer))
2599 << "Cannot assign to the Output \"" << this->name()
2600 << "\": the expression is not convertible to the same Buffer type and/or dimensions.\n";
2601
2602 if (this->gio_types_defined()) {
2603 user_assert(Type(buffer.type()) == this->gio_type())
2604 << "Output " << this->name() << " should have type=" << this->gio_type() << " but saw type=" << Type(buffer.type()) << "\n";
2605 }
2606 if (this->dims_defined()) {
2607 user_assert(buffer.dimensions() == this->dims())
2608 << "Output " << this->name() << " should have dim=" << this->dims() << " but saw dim=" << buffer.dimensions() << "\n";
2609 }
2610
2611 internal_assert(this->exprs_.empty() && this->funcs_.size() == 1);
2612 user_assert(!this->funcs_.at(0).defined());
2613 this->funcs_.at(0)(_) = buffer(_);
2614
2615 return *this;
2616 }
2617
2618 // Allow assignment from a StubOutputBuffer to an Output<Buffer>;
2619 // this allows us to pipeline the results of a Stub to the results
2620 // of the enclosing Generator.
2621 template<typename T2>
2623 this->check_gio_access();
2624 assign_from_func(stub_output_buffer.f);
2625 return *this;
2626 }
2627
2628 // Allow assignment from a Func to an Output<Buffer>;
2629 // this allows us to use helper functions that return a plain Func
2630 // to simply set the output(s) without needing a wrapper Func.
2632 this->check_gio_access();
2633 assign_from_func(f);
2634 return *this;
2635 }
2636
2637 operator OutputImageParam() const {
2638 this->check_gio_access();
2639 user_assert(!this->is_array()) << "Cannot convert an Output<Buffer<>[]> to an ImageParam; use an explicit subscript operator: " << this->name();
2640 internal_assert(this->exprs_.empty() && this->funcs_.size() == 1);
2641 return this->funcs_.at(0).output_buffer();
2642 }
2643
2644 // Forward set_estimates() to Func (rather than OutputImageParam) so that it can
2645 // handle Tuple-valued outputs correctly.
2647 user_assert(!this->is_array()) << "Cannot call set_estimates() on an array Output; use an explicit subscript operator: " << this->name();
2648 internal_assert(this->exprs_.empty() && this->funcs_.size() == 1);
2649 this->funcs_.at(0).set_estimates(estimates);
2650 return *this;
2651 }
2652
2654 const Func &operator[](size_t i) const {
2655 this->check_gio_access();
2656 return this->template get_values<Func>()[i];
2657 }
2658
2659 // Allow Output<Buffer[]>.compute_root() (or other scheduling directive that requires nonconst)
2662 this->check_gio_access();
2663 return this->template get_values<Func>()[i];
2664 }
2665
2666 /** Forward methods to the OutputImageParam. */
2667 // @{
2671 HALIDE_FORWARD_METHOD(OutputImageParam, set_host_alignment)
2681 // }@
2682};
2683
2684template<typename T>
2686private:
2688
2689 HALIDE_NO_USER_CODE_INLINE Func &get_assignable_func_ref(size_t i) {
2690 internal_assert(this->exprs_.empty() && this->funcs_.size() > i);
2691 return this->funcs_.at(i);
2692 }
2693
2694protected:
2695 using TBase = typename Super::TBase;
2696
2697 explicit GeneratorOutput_Func(const std::string &name)
2698 : Super(name, ArgInfoKind::Function, std::vector<Type>{}, -1) {
2699 }
2700
2701 GeneratorOutput_Func(const std::string &name, const std::vector<Type> &t, int d)
2702 : Super(name, ArgInfoKind::Function, t, d) {
2703 }
2704
2705 GeneratorOutput_Func(const std::string &name, const std::vector<Type> &t)
2706 : Super(name, ArgInfoKind::Function, t, -1) {
2707 }
2708
2709 GeneratorOutput_Func(const std::string &name, int d)
2710 : Super(name, ArgInfoKind::Function, {}, d) {
2711 }
2712
2713 GeneratorOutput_Func(size_t array_size, const std::string &name, const std::vector<Type> &t, int d)
2714 : Super(array_size, name, ArgInfoKind::Function, t, d) {
2715 }
2716
2717public:
2718 // Allow Output<Func> = Func
2721 this->check_gio_access();
2722 this->check_value_writable();
2723
2724 // Don't bother verifying the Func type, dimensions, etc., here:
2725 // That's done later, when we produce the pipeline.
2726 get_assignable_func_ref(0) = f;
2727 return *this;
2728 }
2729
2730 // Allow Output<Func[]> = Func
2732 Func &operator[](size_t i) {
2733 this->check_gio_access();
2734 this->check_value_writable();
2735 return get_assignable_func_ref(i);
2736 }
2737
2738 // Allow Func = Output<Func[]>
2740 const Func &operator[](size_t i) const {
2741 this->check_gio_access();
2742 return Super::operator[](i);
2743 }
2744
2745 GeneratorOutput_Func<T> &set_estimate(const Var &var, const Expr &min, const Expr &extent) {
2746 this->check_gio_access();
2747 internal_assert(this->exprs_.empty() && !this->funcs_.empty());
2748 for (Func &f : this->funcs_) {
2749 f.set_estimate(var, min, extent);
2750 }
2751 return *this;
2752 }
2753
2755 this->check_gio_access();
2756 internal_assert(this->exprs_.empty() && !this->funcs_.empty());
2757 for (Func &f : this->funcs_) {
2758 f.set_estimates(estimates);
2759 }
2760 return *this;
2761 }
2762};
2763
2764template<typename T>
2766private:
2768
2769protected:
2770 using TBase = typename Super::TBase;
2771
2772 explicit GeneratorOutput_Arithmetic(const std::string &name)
2774 }
2775
2776 GeneratorOutput_Arithmetic(size_t array_size, const std::string &name)
2778 }
2779};
2780
2783 typename select_type<
2787
2788} // namespace Internal
2789
2790template<typename T>
2792private:
2794
2795protected:
2796 using TBase = typename Super::TBase;
2797
2798public:
2799 // Mark all of these explicit (not just single-arg versions) so that
2800 // we disallow copy-list-initialization form (i.e., Output foo{"foo"} is ok,
2801 // but Output foo = {"foo"} is not).
2802 explicit GeneratorOutput(const std::string &name)
2803 : Super(name) {
2804 }
2805
2806 explicit GeneratorOutput(const char *name)
2807 : GeneratorOutput(std::string(name)) {
2808 }
2809
2810 explicit GeneratorOutput(size_t array_size, const std::string &name)
2811 : Super(array_size, name) {
2812 }
2813
2814 explicit GeneratorOutput(const std::string &name, int d)
2815 : Super(name, d) {
2816 }
2817
2818 explicit GeneratorOutput(const std::string &name, const Type &t)
2819 : Super(name, {t}) {
2820 }
2821
2822 explicit GeneratorOutput(const std::string &name, const std::vector<Type> &t)
2823 : Super(name, t) {
2824 }
2825
2826 explicit GeneratorOutput(const std::string &name, const Type &t, int d)
2827 : Super(name, {t}, d) {
2828 }
2829
2830 explicit GeneratorOutput(const std::string &name, const std::vector<Type> &t, int d)
2831 : Super(name, t, d) {
2832 }
2833
2834 explicit GeneratorOutput(size_t array_size, const std::string &name, int d)
2835 : Super(array_size, name, d) {
2836 }
2837
2838 explicit GeneratorOutput(size_t array_size, const std::string &name, const Type &t)
2839 : Super(array_size, name, {t}) {
2840 }
2841
2842 explicit GeneratorOutput(size_t array_size, const std::string &name, const std::vector<Type> &t)
2843 : Super(array_size, name, t) {
2844 }
2845
2846 explicit GeneratorOutput(size_t array_size, const std::string &name, const Type &t, int d)
2847 : Super(array_size, name, {t}, d) {
2848 }
2849
2850 explicit GeneratorOutput(size_t array_size, const std::string &name, const std::vector<Type> &t, int d)
2851 : Super(array_size, name, t, d) {
2852 }
2853
2854 // TODO: This used to take the buffer as a const ref. This no longer works as
2855 // using it in a Pipeline might change the dev field so it is currently
2856 // not considered const. We should consider how this really ought to work.
2857 template<typename T2, int D2>
2859 Super::operator=(buffer);
2860 return *this;
2861 }
2862
2863 template<typename T2>
2865 Super::operator=(stub_output_buffer);
2866 return *this;
2867 }
2868
2870 Super::operator=(f);
2871 return *this;
2872 }
2873};
2874
2875namespace Internal {
2876
2877template<typename T>
2878T parse_scalar(const std::string &value) {
2879 std::istringstream iss(value);
2880 T t;
2881 iss >> t;
2882 user_assert(!iss.fail() && iss.get() == EOF) << "Unable to parse: " << value;
2883 return t;
2884}
2885
2886std::vector<Type> parse_halide_type_list(const std::string &types);
2887
2889 Dim,
2890 ArraySize };
2891
2892// This is a type of GeneratorParam used internally to create 'synthetic' params
2893// (e.g. image.type, image.dim); it is not possible for user code to instantiate it.
2894template<typename T>
2896public:
2897 void set_from_string(const std::string &new_value_string) override {
2898 // If error_msg is not empty, this is unsettable:
2899 // display error_msg as a user error.
2900 if (!error_msg.empty()) {
2901 user_error << error_msg;
2902 }
2904 }
2905
2906 std::string get_default_value() const override {
2908 return std::string();
2909 }
2910
2911 std::string call_to_string(const std::string &v) const override {
2913 return std::string();
2914 }
2915
2916 std::string get_c_type() const override {
2918 return std::string();
2919 }
2920
2921 bool is_synthetic_param() const override {
2922 return true;
2923 }
2924
2925private:
2927
2928 static std::unique_ptr<Internal::GeneratorParamBase> make(
2929 GeneratorBase *generator,
2930 const std::string &generator_name,
2931 const std::string &gpname,
2932 GIOBase &gio,
2933 SyntheticParamType which,
2934 bool defined) {
2935 std::string error_msg = defined ? "Cannot set the GeneratorParam " + gpname + " for " + generator_name + " because the value is explicitly specified in the C++ source." : "";
2936 return std::unique_ptr<GeneratorParam_Synthetic<T>>(
2937 new GeneratorParam_Synthetic<T>(gpname, gio, which, error_msg));
2938 }
2939
2940 GeneratorParam_Synthetic(const std::string &name, GIOBase &gio, SyntheticParamType which, const std::string &error_msg = "")
2941 : GeneratorParamImpl<T>(name, T()), gio(gio), which(which), error_msg(error_msg) {
2942 }
2943
2945 void set_from_string_impl(const std::string &new_value_string) {
2948 }
2949
2951 void set_from_string_impl(const std::string &new_value_string) {
2952 if (which == SyntheticParamType::Dim) {
2954 } else if (which == SyntheticParamType::ArraySize) {
2956 } else {
2958 }
2959 }
2960
2961 GIOBase &gio;
2962 const SyntheticParamType which;
2963 const std::string error_msg;
2964};
2965
2966} // namespace Internal
2967
2968/** GeneratorContext is a class that is used when using Generators (or Stubs) directly;
2969 * it is used to allow the outer context (typically, either a Generator or "top-level" code)
2970 * to specify certain information to the inner context to ensure that inner and outer
2971 * Generators are compiled in a compatible way.
2972 *
2973 * If you are using this at "top level" (e.g. with the JIT), you can construct a GeneratorContext
2974 * with a Target:
2975 * \code
2976 * auto my_stub = MyStub(
2977 * GeneratorContext(get_target_from_environment()),
2978 * // inputs
2979 * { ... },
2980 * // generator params
2981 * { ... }
2982 * );
2983 * \endcode
2984 *
2985 * Note that all Generators embed a GeneratorContext, so if you are using a Stub
2986 * from within a Generator, you can just pass 'context()' for the GeneratorContext:
2987 * \code
2988 * struct SomeGen : Generator<SomeGen> {
2989 * void generate() {
2990 * ...
2991 * auto my_stub = MyStub(
2992 * context(), // GeneratorContext
2993 * // inputs
2994 * { ... },
2995 * // generator params
2996 * { ... }
2997 * );
2998 * ...
2999 * }
3000 * };
3001 * \endcode
3002 */
3004public:
3006
3007 explicit GeneratorContext(const Target &t);
3008 explicit GeneratorContext(const Target &t,
3010
3011 GeneratorContext() = default;
3016
3017 const Target &target() const {
3018 return target_;
3019 }
3021 return autoscheduler_params_;
3022 }
3023
3024 // Return a copy of this GeneratorContext that uses the given Target.
3025 // This method is rarely needed; it's really provided as a convenience
3026 // for use with init_from_context().
3028
3029 template<typename T>
3030 inline std::unique_ptr<T> create() const {
3031 return T::create(*this);
3032 }
3033 template<typename T, typename... Args>
3034 inline std::unique_ptr<T> apply(const Args &...args) const {
3035 auto t = this->create<T>();
3036 t->apply(args...);
3037 return t;
3038 }
3039
3040private:
3041 Target target_;
3042 AutoschedulerParams autoscheduler_params_;
3043};
3044
3046 // Names in this class are only intended for use in derived classes.
3047protected:
3048 // Import a consistent list of Halide names that can be used in
3049 // Halide generators without qualification.
3069 template<typename T>
3070 static Expr cast(Expr e) {
3071 return Halide::cast<T>(e);
3072 }
3073 static inline Expr cast(Halide::Type t, Expr e) {
3074 return Halide::cast(t, std::move(e));
3075 }
3076 template<typename T>
3078 template<typename T = void, int D = -1>
3080 template<typename T>
3082 static inline Type Bool(int lanes = 1) {
3083 return Halide::Bool(lanes);
3084 }
3085 static inline Type Float(int bits, int lanes = 1) {
3086 return Halide::Float(bits, lanes);
3087 }
3088 static inline Type Int(int bits, int lanes = 1) {
3089 return Halide::Int(bits, lanes);
3090 }
3091 static inline Type UInt(int bits, int lanes = 1) {
3092 return Halide::UInt(bits, lanes);
3093 }
3094};
3095
3096namespace Internal {
3097
3098template<typename... Args>
3099struct NoRealizations : std::false_type {};
3100
3101template<>
3102struct NoRealizations<> : std::true_type {};
3103
3104template<typename T, typename... Args>
3105struct NoRealizations<T, Args...> {
3106 static const bool value = !std::is_convertible<T, Realization>::value && NoRealizations<Args...>::value;
3107};
3108
3109// Note that these functions must never return null:
3110// if they cannot return a valid Generator, they must assert-fail.
3111using GeneratorFactory = std::function<AbstractGeneratorPtr(const GeneratorContext &context)>;
3112
3114 // names used across all params, inputs, and outputs.
3115 std::set<std::string> names;
3116
3117 // Ordered-list of non-null ptrs to GeneratorParam<> fields.
3118 std::vector<Internal::GeneratorParamBase *> filter_generator_params;
3119
3120 // Ordered-list of non-null ptrs to Input<> fields.
3121 std::vector<Internal::GeneratorInputBase *> filter_inputs;
3122
3123 // Ordered-list of non-null ptrs to Output<> fields; empty if old-style Generator.
3124 std::vector<Internal::GeneratorOutputBase *> filter_outputs;
3125
3126 // list of synthetic GP's that we dynamically created; this list only exists to simplify
3127 // lifetime management, and shouldn't be accessed directly outside of our ctor/dtor,
3128 // regardless of friend access.
3129 std::vector<std::unique_ptr<Internal::GeneratorParamBase>> owned_synthetic_params;
3130
3131 // list of dynamically-added inputs and outputs, here only for lifetime management.
3132 std::vector<std::unique_ptr<Internal::GIOBase>> owned_extras;
3133
3134public:
3135 friend class GeneratorBase;
3136
3137 GeneratorParamInfo(GeneratorBase *generator, size_t size);
3138
3139 const std::vector<Internal::GeneratorParamBase *> &generator_params() const {
3140 return filter_generator_params;
3141 }
3142 const std::vector<Internal::GeneratorInputBase *> &inputs() const {
3143 return filter_inputs;
3144 }
3145 const std::vector<Internal::GeneratorOutputBase *> &outputs() const {
3146 return filter_outputs;
3147 }
3148};
3149
3151public:
3152 ~GeneratorBase() override;
3153
3154 /** Given a data type, return an estimate of the "natural" vector size
3155 * for that data type when compiling for the current target. */
3157 return get_target().natural_vector_size(t);
3158 }
3159
3160 /** Given a data type, return an estimate of the "natural" vector size
3161 * for that data type when compiling for the current target. */
3162 template<typename data_t>
3165 }
3166
3167 /**
3168 * set_inputs is a variadic wrapper around set_inputs_vector, which makes usage much simpler
3169 * in many cases, as it constructs the relevant entries for the vector for you, which
3170 * is often a bit unintuitive at present. The arguments are passed in Input<>-declaration-order,
3171 * and the types must be compatible. Array inputs are passed as std::vector<> of the relevant type.
3172 *
3173 * Note: at present, scalar input types must match *exactly*, i.e., for Input<uint8_t>, you
3174 * must pass an argument that is actually uint8_t; an argument that is int-that-will-fit-in-uint8
3175 * will assert-fail at Halide compile time.
3176 */
3177 template<typename... Args>
3178 void set_inputs(const Args &...args) {
3179 // set_inputs_vector() checks this too, but checking it here allows build_inputs() to avoid out-of-range checks.
3180 GeneratorParamInfo &pi = this->param_info();
3181 user_assert(sizeof...(args) == pi.inputs().size())
3182 << "Expected exactly " << pi.inputs().size()
3183 << " inputs but got " << sizeof...(args) << "\n";
3184 set_inputs_vector(build_inputs(std::forward_as_tuple<const Args &...>(args...), std::make_index_sequence<sizeof...(Args)>{}));
3185 }
3186
3187 Realization realize(std::vector<int32_t> sizes) {
3188 this->check_scheduled("realize");
3189 return get_pipeline().realize(std::move(sizes), get_target());
3190 }
3191
3192 // Only enable if none of the args are Realization; otherwise we can incorrectly
3193 // select this method instead of the Realization-as-outparam variant
3194 template<typename... Args, typename std::enable_if<NoRealizations<Args...>::value>::type * = nullptr>
3196 this->check_scheduled("realize");
3197 return get_pipeline().realize(std::forward<Args>(args)..., get_target());
3198 }
3199
3201 this->check_scheduled("realize");
3203 }
3204
3205 // Return the Pipeline that has been built by the generate() method.
3206 // This method can only be called from the schedule() method.
3207 // (This may be relaxed in the future to allow calling from generate() as
3208 // long as all Outputs have been defined.)
3210
3211 // Create Input<Func> with dynamic type & dimensions
3212 template<typename T,
3213 typename std::enable_if<std::is_same<T, Halide::Func>::value>::type * = nullptr>
3214 GeneratorInput<T> *add_input(const std::string &name, const Type &t, int dimensions) {
3216 auto *p = new GeneratorInput<T>(name, t, dimensions);
3217 p->generator = this;
3218 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3219 param_info_ptr->filter_inputs.push_back(p);
3220 return p;
3221 }
3222
3223 // Create Input<Buffer> with dynamic type & dimensions
3224 template<typename T,
3225 typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, Halide::Func>::value>::type * = nullptr>
3226 GeneratorInput<T> *add_input(const std::string &name, const Type &t, int dimensions) {
3227 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 .");
3228 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.");
3230 auto *p = new GeneratorInput<T>(name, t, dimensions);
3231 p->generator = this;
3232 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3233 param_info_ptr->filter_inputs.push_back(p);
3234 return p;
3235 }
3236
3237 // Create Input<Buffer> with compile-time type
3238 template<typename T,
3239 typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, Halide::Func>::value>::type * = nullptr>
3240 GeneratorInput<T> *add_input(const std::string &name, int dimensions) {
3241 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.");
3242 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.");
3244 auto *p = new GeneratorInput<T>(name, dimensions);
3245 p->generator = this;
3246 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3247 param_info_ptr->filter_inputs.push_back(p);
3248 return p;
3249 }
3250
3251 // Create Input<Buffer> with compile-time type & dimensions
3252 template<typename T,
3253 typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, Halide::Func>::value>::type * = nullptr>
3254 GeneratorInput<T> *add_input(const std::string &name) {
3255 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.");
3256 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.");
3258 auto *p = new GeneratorInput<T>(name);
3259 p->generator = this;
3260 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3261 param_info_ptr->filter_inputs.push_back(p);
3262 return p;
3263 }
3264 // Create Input<scalar>
3265 template<typename T,
3266 typename std::enable_if<std::is_arithmetic<T>::value>::type * = nullptr>
3267 GeneratorInput<T> *add_input(const std::string &name) {
3269 auto *p = new GeneratorInput<T>(name);
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 // Create Input<Expr> with dynamic type
3276 template<typename T,
3277 typename std::enable_if<std::is_same<T, Expr>::value>::type * = nullptr>
3278 GeneratorInput<T> *add_input(const std::string &name, const Type &type) {
3280 auto *p = new GeneratorInput<Expr>(name);
3281 p->generator = this;
3282 p->set_type(type);
3283 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3284 param_info_ptr->filter_inputs.push_back(p);
3285 return p;
3286 }
3287
3288 // Create Output<Func> with dynamic type & dimensions
3289 template<typename T,
3290 typename std::enable_if<std::is_same<T, Halide::Func>::value>::type * = nullptr>
3291 GeneratorOutput<T> *add_output(const std::string &name, const Type &t, int dimensions) {
3293 auto *p = new GeneratorOutput<T>(name, t, dimensions);
3294 p->generator = this;
3295 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3296 param_info_ptr->filter_outputs.push_back(p);
3297 return p;
3298 }
3299
3300 // Create Output<Buffer> with dynamic type & dimensions
3301 template<typename T,
3302 typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, Halide::Func>::value>::type * = nullptr>
3303 GeneratorOutput<T> *add_output(const std::string &name, const Type &t, int dimensions) {
3304 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 .");
3305 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.");
3307 auto *p = new GeneratorOutput<T>(name, t, dimensions);
3308 p->generator = this;
3309 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3310 param_info_ptr->filter_outputs.push_back(p);
3311 return p;
3312 }
3313
3314 // Create Output<Buffer> with compile-time type
3315 template<typename T,
3316 typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, Halide::Func>::value>::type * = nullptr>
3317 GeneratorOutput<T> *add_output(const std::string &name, int dimensions) {
3318 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.");
3319 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.");
3321 auto *p = new GeneratorOutput<T>(name, dimensions);
3322 p->generator = this;
3323 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3324 param_info_ptr->filter_outputs.push_back(p);
3325 return p;
3326 }
3327
3328 // Create Output<Buffer> with compile-time type & dimensions
3329 template<typename T,
3330 typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, Halide::Func>::value>::type * = nullptr>
3331 GeneratorOutput<T> *add_output(const std::string &name) {
3332 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.");
3333 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.");
3335 auto *p = new GeneratorOutput<T>(name);
3336 p->generator = this;
3337 param_info_ptr->owned_extras.push_back(std::unique_ptr<Internal::GIOBase>(p));
3338 param_info_ptr->filter_outputs.push_back(p);
3339 return p;
3340 }
3341
3342 void add_requirement(const Expr &condition, const std::vector<Expr> &error_args);
3343
3344 template<typename... Args,
3345 typename = typename std::enable_if<Internal::all_are_printable_args<Args...>::value>::type>
3346 inline HALIDE_NO_USER_CODE_INLINE void add_requirement(const Expr &condition, Args &&...error_args) {
3347 std::vector<Expr> collected_args;
3348 Internal::collect_print_args(collected_args, std::forward<Args>(error_args)...);
3349 add_requirement(condition, collected_args);
3350 }
3351
3354 }
3355
3356protected:
3357 GeneratorBase(size_t size, const void *introspection_helper);
3358 void set_generator_names(const std::string &registered_name, const std::string &stub_name);
3359
3360 // Note that it is explicitly legal to override init_from_context(), so that you can (say)
3361 // create a modified context with a different Target (eg with features enabled or disabled), but...
3362 //
3363 // *** WARNING ***
3364 //
3365 // Modifying the context here can be fraught with subtle hazards, especially when used
3366 // in conjunction with compiling to multitarget output. Adding or removing Feature
3367 // flags could break your build (if you are lucky), or cause subtle runtime failures (if unlucky)...
3368 //
3369 // e.g. in the latter case, say you decided to enable AVX512_SapphireRapids as an experiment,
3370 // and override init_from_context() to do just that. You'd end up being crashy on pre-AVX512
3371 // hardware, because the code that Halide injects to do runtime CPU feature detection at runtime
3372 // doesn't know it needs to do the runtime detection for this flag.
3373 //
3374 // Even if you are using multitarget output, using this as a 'hook' to enable or disable Features
3375 // can produce hard-to-maintain code in the long term: Halide has dozens of feature flags now,
3376 // many of which are orthogonal to each other and/or specific to a certain architecture
3377 // (or sub-architecture). The interaction between 'orthogonal' flags like this is essentially
3378 // Undefined Behavior (e.g. if I enable the SSE41 Feature on a Target where arch = RISCV, what happens?
3379 // Is it ignored? Does it fail to compile? Something else?). The point here is that adding Features
3380 // here may end up eventually getting added to a Target you didn't anticipate and have adverse consequences.
3381 //
3382 // With all that in mind, here are some guidelines we think will make long-term code maintenance
3383 // less painful for you:
3384 //
3385 // - Override this method *only* for temporary debugging purposes; e.g. if you
3386 // need to add the `profile` feature to a specific Generator, but your build system doesn't easily
3387 // let you specify per-Generator target features, this is the right tool for the job.
3388 //
3389 // - If your build system makes it infeasible to customize the build Target in a reasonable way,
3390 // it may be appropriate to permanently override this method to enable specific Features for
3391 // specific Generators (e.g., enabling `strict_float` is a likely example). In that case,
3392 // we would suggest:
3393 //
3394 // - *NEVER* change the arch/bits/os of the Target.
3395 // - Only add Features; don't remove Features.
3396 // - For Features that are architecture-specific, always check the arch/bits/os
3397 // of the Target to be sure it's what you expect... e.g. if you are enabling
3398 // AVX512, only do so if compiling for an x86-64 Target. Even if your code
3399 // doesn't target any other architecture at the present time, Future You will be
3400 // happier.
3401 // - If you mutate a target conditionally based on the incoming target, try to do so
3402 // so based only on the Target's arch/bits/os, and not at the Features set on the target.
3403 // If examining Features is unavoidable (e.g. enable $FOO only if $BAR is enabled),
3404 // do so as conservatively as possible, and always validate that the rest of the Target
3405 // is sensible for what you are doing.
3406 //
3407 // Furthermore, if you override this, please don't try to directly set the `target` (etc) GeneratorParams
3408 // directly; instead, construct the new GeneratorContext you want and call the superclass
3409 // implementation of init_from_context.
3410 //
3411 // TL;DR: overrides to this method should probably never be checked in to your source control system
3412 // (rather, the override should be temporary and local, for experimentation). If you must check in
3413 // overrides to this method, be paranoid that the Target you get could be something you don't expect.
3414 //
3416
3417 virtual void call_configure() = 0;
3418 virtual void call_generate() = 0;
3419 virtual void call_schedule() = 0;
3420
3429
3430 template<typename T>
3432
3433 template<typename T>
3435
3436 // A Generator's creation and usage must go in a certain phase to ensure correctness;
3437 // the state machine here is advanced and checked at various points to ensure
3438 // this is the case.
3439 enum Phase {
3440 // Generator has just come into being.
3442
3443 // Generator has had its configure() method called. (For Generators without
3444 // a configure() method, this phase will be skipped and will advance
3445 // directly to InputsSet.)
3447
3448 // All Input<>/Param<> fields have been set. (Applicable only in JIT mode;
3449 // in AOT mode, this can be skipped, going Created->GenerateCalled directly.)
3451
3452 // Generator has had its generate() method called.
3454
3455 // Generator has had its schedule() method (if any) called.
3458
3462
3464
3466 return target;
3467 }
3468 bool using_autoscheduler() const {
3469 return !autoscheduler_.value().name.empty();
3470 }
3471
3472 // These must remain here for legacy code that access the fields directly.
3475
3476private:
3477 friend void ::Halide::Internal::generator_test();
3479 friend class GIOBase;
3484
3485 const size_t size;
3486
3487 // Lazily-allocated-and-inited struct with info about our various Params.
3488 // Do not access directly: use the param_info() getter.
3489 std::unique_ptr<GeneratorParamInfo> param_info_ptr;
3490
3491 std::string generator_registered_name, generator_stub_name;
3492 Pipeline pipeline;
3493
3494 struct Requirement {
3495 Expr condition;
3496 std::vector<Expr> error_args;
3497 };
3498 std::vector<Requirement> requirements;
3499
3500 // Return our GeneratorParamInfo.
3501 GeneratorParamInfo &param_info();
3502
3503 template<typename T>
3504 T *find_by_name(const std::string &name, const std::vector<T *> &v) {
3505 for (T *t : v) {
3506 if (t->name() == name) {
3507 return t;
3508 }
3509 }
3510 return nullptr;
3511 }
3512
3513 Internal::GeneratorInputBase *find_input_by_name(const std::string &name);
3514 Internal::GeneratorOutputBase *find_output_by_name(const std::string &name);
3515
3516 void check_scheduled(const char *m) const;
3517
3518 void build_params(bool force = false);
3519
3520 // Provide private, unimplemented, wrong-result-type methods here
3521 // so that Generators don't attempt to call the global methods
3522 // of the same name by accident: use the get_target() method instead.
3523 void get_host_target();
3524 void get_jit_target_from_environment();
3525 void get_target_from_environment();
3526
3527 void set_inputs_vector(const std::vector<std::vector<StubInput>> &inputs);
3528
3529 static void check_input_is_singular(Internal::GeneratorInputBase *in);
3530 static void check_input_is_array(Internal::GeneratorInputBase *in);
3531 static void check_input_kind(Internal::GeneratorInputBase *in, Internal::ArgInfoKind kind);
3532
3533 // Allow Buffer<> if:
3534 // -- we are assigning it to an Input<Buffer<>> (with compatible type and dimensions),
3535 // causing the Input<Buffer<>> to become a precompiled buffer in the generated code.
3536 // -- we are assigningit to an Input<Func>, in which case we just Func-wrap the Buffer<>.
3537 template<typename T, int Dims>
3538 std::vector<StubInput> build_input(size_t i, const Buffer<T, Dims> &arg) {
3539 auto *in = param_info().inputs().at(i);
3540 check_input_is_singular(in);
3541 const auto k = in->kind();
3543 Halide::Buffer<> b = arg;
3545 StubInput si(sib);
3546 return {si};
3547 } else if (k == Internal::ArgInfoKind::Function) {
3548 Halide::Func f(arg.name() + "_im");
3549 f(Halide::_) = arg(Halide::_);
3550 StubInput si(f);
3551 return {si};
3552 } else {
3553 check_input_kind(in, Internal::ArgInfoKind::Buffer); // just to trigger assertion
3554 return {};
3555 }
3556 }
3557
3558 // Allow Input<Buffer<>> if:
3559 // -- we are assigning it to another Input<Buffer<>> (with compatible type and dimensions),
3560 // allowing us to simply pipe a parameter from an enclosing Generator to the Invoker.
3561 // -- we are assigningit to an Input<Func>, in which case we just Func-wrap the Input<Buffer<>>.
3562 template<typename T, int Dims>
3563 std::vector<StubInput> build_input(size_t i, const GeneratorInput<Buffer<T, Dims>> &arg) {
3564 auto *in = param_info().inputs().at(i);
3565 check_input_is_singular(in);
3566 const auto k = in->kind();
3568 StubInputBuffer<> sib = arg;
3569 StubInput si(sib);
3570 return {si};
3571 } else if (k == Internal::ArgInfoKind::Function) {
3572 Halide::Func f = arg.funcs().at(0);
3573 StubInput si(f);
3574 return {si};
3575 } else {
3576 check_input_kind(in, Internal::ArgInfoKind::Buffer); // just to trigger assertion
3577 return {};
3578 }
3579 }
3580
3581 // Allow Func iff we are assigning it to an Input<Func> (with compatible type and dimensions).
3582 std::vector<StubInput> build_input(size_t i, const Func &arg) {
3583 auto *in = param_info().inputs().at(i);
3584 check_input_kind(in, Internal::ArgInfoKind::Function);
3585 check_input_is_singular(in);
3586 const Halide::Func &f = arg;
3587 StubInput si(f);
3588 return {si};
3589 }
3590
3591 // Allow vector<Func> iff we are assigning it to an Input<Func[]> (with compatible type and dimensions).
3592 std::vector<StubInput> build_input(size_t i, const std::vector<Func> &arg) {
3593 auto *in = param_info().inputs().at(i);
3594 check_input_kind(in, Internal::ArgInfoKind::Function);
3595 check_input_is_array(in);
3596 // My kingdom for a list comprehension...
3597 std::vector<StubInput> siv;
3598 siv.reserve(arg.size());
3599 for (const auto &f : arg) {
3600 siv.emplace_back(f);
3601 }
3602 return siv;
3603 }
3604
3605 // Expr must be Input<Scalar>.
3606 std::vector<StubInput> build_input(size_t i, const Expr &arg) {
3607 auto *in = param_info().inputs().at(i);
3608 check_input_kind(in, Internal::ArgInfoKind::Scalar);
3609 check_input_is_singular(in);
3610 StubInput si(arg);
3611 return {si};
3612 }
3613
3614 // (Array form)
3615 std::vector<StubInput> build_input(size_t i, const std::vector<Expr> &arg) {
3616 auto *in = param_info().inputs().at(i);
3617 check_input_kind(in, Internal::ArgInfoKind::Scalar);
3618 check_input_is_array(in);
3619 std::vector<StubInput> siv;
3620 siv.reserve(arg.size());
3621 for (const auto &value : arg) {
3622 siv.emplace_back(value);
3623 }
3624 return siv;
3625 }
3626
3627 // Any other type must be convertible to Expr and must be associated with an Input<Scalar>.
3628 // Use is_arithmetic since some Expr conversions are explicit.
3629 template<typename T,
3630 typename std::enable_if<std::is_arithmetic<T>::value>::type * = nullptr>
3631 std::vector<StubInput> build_input(size_t i, const T &arg) {
3632 auto *in = param_info().inputs().at(i);
3633 check_input_kind(in, Internal::ArgInfoKind::Scalar);
3634 check_input_is_singular(in);
3635 // We must use an explicit Expr() ctor to preserve the type
3636 Expr e(arg);
3637 StubInput si(e);
3638 return {si};
3639 }
3640
3641 // (Array form)
3642 template<typename T,
3643 typename std::enable_if<std::is_arithmetic<T>::value>::type * = nullptr>
3644 std::vector<StubInput> build_input(size_t i, const std::vector<T> &arg) {
3645 auto *in = param_info().inputs().at(i);
3646 check_input_kind(in, Internal::ArgInfoKind::Scalar);
3647 check_input_is_array(in);
3648 std::vector<StubInput> siv;
3649 siv.reserve(arg.size());
3650 for (const auto &value : arg) {
3651 // We must use an explicit Expr() ctor to preserve the type;
3652 // otherwise, implicit conversions can downgrade (e.g.) float -> int
3653 Expr e(value);
3654 siv.emplace_back(e);
3655 }
3656 return siv;
3657 }
3658
3659 template<typename... Args, size_t... Indices>
3660 std::vector<std::vector<StubInput>> build_inputs(const std::tuple<const Args &...> &t, std::index_sequence<Indices...>) {
3661 return {build_input(Indices, std::get<Indices>(t))...};
3662 }
3663
3664 // Note that this deliberately ignores inputs/outputs with multiple array values
3665 // (ie, one name per input or output, regardless of array_size())
3666 template<typename T>
3667 static void get_arguments(std::vector<AbstractGenerator::ArgInfo> &args, ArgInfoDirection dir, const T &t) {
3668 for (auto *e : t) {
3669 args.push_back({e->name(),
3670 dir,
3671 e->kind(),
3672 e->gio_types_defined() ? e->gio_types() : std::vector<Type>{},
3673 e->dims_defined() ? e->dims() : 0});
3674 }
3675 }
3676
3677public:
3678 // AbstractGenerator methods
3679 std::string name() override;
3680 GeneratorContext context() const override;
3681 std::vector<ArgInfo> arginfos() override;
3683
3684 void set_generatorparam_value(const std::string &name, const std::string &value) override;
3685 void set_generatorparam_value(const std::string &name, const LoopLevel &loop_level) override;
3686
3687 std::vector<Parameter> input_parameter(const std::string &name) override;
3688 std::vector<Func> output_func(const std::string &name) override;
3689
3690 // This is overridden in the concrete Generator<> subclass.
3691 // Pipeline build_pipeline() override;
3692
3693 void bind_input(const std::string &name, const std::vector<Parameter> &v) override;
3694 void bind_input(const std::string &name, const std::vector<Func> &v) override;
3695 void bind_input(const std::string &name, const std::vector<Expr> &v) override;
3696
3697 bool emit_cpp_stub(const std::string &stub_file_path) override;
3698 bool emit_hlpipe(const std::string &hlpipe_file_path) override;
3699
3700 GeneratorBase(const GeneratorBase &) = delete;
3704};
3705
3707public:
3708 static void register_factory(const std::string &name, GeneratorFactory generator_factory);
3709 static void unregister_factory(const std::string &name);
3710 static std::vector<std::string> enumerate();
3711 // This method returns nullptr if it cannot return a valid Generator;
3712 // the caller is responsible for checking the result.
3713 static AbstractGeneratorPtr create(const std::string &name,
3714 const Halide::GeneratorContext &context);
3715
3716private:
3717 using GeneratorFactoryMap = std::map<const std::string, GeneratorFactory>;
3718
3719 GeneratorFactoryMap factories;
3720 std::mutex mutex;
3721
3722 static GeneratorRegistry &get_registry();
3723
3724 GeneratorRegistry() = default;
3725
3726public:
3731};
3732
3733} // namespace Internal
3734
3735template<class T>
3737protected:
3740 Internal::Introspection::get_introspection_helper<T>()) {
3741 }
3742
3743public:
3744 static std::unique_ptr<T> create(const Halide::GeneratorContext &context) {
3745 // We must have an object of type T (not merely GeneratorBase) to call a protected method,
3746 // because CRTP is a weird beast.
3747 auto g = std::make_unique<T>();
3748 g->init_from_context(context);
3749 return g;
3750 }
3751
3752 // This is public but intended only for use by the HALIDE_REGISTER_GENERATOR() macro.
3753 static std::unique_ptr<T> create(const Halide::GeneratorContext &context,
3754 const std::string &registered_name,
3755 const std::string &stub_name) {
3756 auto g = create(context);
3757 g->set_generator_names(registered_name, stub_name);
3758 return g;
3759 }
3760
3761 template<typename... Args>
3762 void apply(const Args &...args) {
3764 set_inputs(args...);
3765 call_generate();
3766 call_schedule();
3767 }
3768
3769 template<typename T2>
3770 std::unique_ptr<T2> create() const {
3771 return T2::create(context());
3772 }
3773
3774 template<typename T2, typename... Args>
3775 inline std::unique_ptr<T2> apply(const Args &...args) const {
3776 auto t = this->create<T2>();
3777 t->apply(args...);
3778 return t;
3779 }
3780
3781private:
3782 // std::is_member_function_pointer will fail if there is no member of that name,
3783 // so we use a little SFINAE to detect if there are method-shaped members.
3784 template<typename>
3785 struct type_sink {
3786 typedef void type;
3787 };
3788
3789 template<typename T2, typename = void>
3790 struct has_configure_method : std::false_type {};
3791
3792 template<typename T2>
3793 struct has_configure_method<T2, typename type_sink<decltype(std::declval<T2>().configure())>::type> : std::true_type {};
3794
3795 template<typename T2, typename = void>
3796 struct has_generate_method : std::false_type {};
3797
3798 template<typename T2>
3799 struct has_generate_method<T2, typename type_sink<decltype(std::declval<T2>().generate())>::type> : std::true_type {};
3800
3801 template<typename T2, typename = void>
3802 struct has_schedule_method : std::false_type {};
3803
3804 template<typename T2>
3805 struct has_schedule_method<T2, typename type_sink<decltype(std::declval<T2>().schedule())>::type> : std::true_type {};
3806
3807 Pipeline build_pipeline_impl() {
3808 T *t = (T *)this;
3809 // No: configure() must be called prior to this
3810 // (and in fact, prior to calling set_inputs).
3811 //
3812 // t->call_configure_impl();
3813
3814 t->call_generate_impl();
3815 t->call_schedule_impl();
3816 return get_pipeline();
3817 }
3818
3819 void call_configure_impl() {
3820 pre_configure();
3821 if constexpr (has_configure_method<T>::value) {
3822 T *t = (T *)this;
3823 static_assert(std::is_void<decltype(t->configure())>::value, "configure() must return void");
3824 t->configure();
3825 }
3827 }
3828
3829 void call_generate_impl() {
3830 pre_generate();
3831 static_assert(has_generate_method<T>::value, "Expected a generate() method here.");
3832 T *t = (T *)this;
3833 static_assert(std::is_void<decltype(t->generate())>::value, "generate() must return void");
3834 t->generate();
3835 post_generate();
3836 }
3837
3838 void call_schedule_impl() {
3839 pre_schedule();
3840 if constexpr (has_schedule_method<T>::value) {
3841 T *t = (T *)this;
3842 static_assert(std::is_void<decltype(t->schedule())>::value, "schedule() must return void");
3843 t->schedule();
3844 }
3845 post_schedule();
3846 }
3847
3848protected:
3851 return this->build_pipeline_impl();
3852 }
3853
3854 void call_configure() override {
3855 this->call_configure_impl();
3856 }
3857
3858 void call_generate() override {
3859 this->call_generate_impl();
3860 }
3861
3862 void call_schedule() override {
3863 this->call_schedule_impl();
3864 }
3865
3866private:
3867 friend void ::Halide::Internal::generator_test();
3868 friend void ::Halide::Internal::generator_test();
3869 friend class ::Halide::GeneratorContext;
3870
3871public:
3872 Generator(const Generator &) = delete;
3873 Generator &operator=(const Generator &) = delete;
3876};
3877
3878namespace Internal {
3879
3884
3885// -----------------------------
3886
3887/** ExecuteGeneratorArgs is the set of arguments to execute_generator().
3888 */
3890 // Output directory for all files generated. Must not be empty.
3891 std::string output_dir;
3892
3893 // Type(s) of outputs to produce. Must not be empty.
3894 std::set<OutputFileType> output_types;
3895
3896 // Target(s) to use when generating. Must not be empty.
3897 // If list contains multiple entries, a multitarget output will be produced.
3898 std::vector<Target> targets;
3899
3900 // When generating multitarget output, use these as the suffixes for each Target
3901 // specified by the targets field. If empty, the canonical string form of
3902 // each Target will be used. If nonempty, it must be the same length as the
3903 // targets vector.
3904 std::vector<std::string> suffixes;
3905
3906 // Name of the generator to execute (or empty if none, e.g. if generating a runtime)
3907 // Must be one recognized by the specified GeneratorFactoryProvider.
3908 std::string generator_name;
3909
3910 // Name to use for the generated function. May include C++ namespaces,
3911 // e.g. "HalideTest::AnotherNamespace::cxx_mangling". If empty, use `generator_name`.
3912 std::string function_name;
3913
3914 // Base filename for all outputs (differentated by file extension).
3915 // If empty, use `function_name` (ignoring any C++ namespaces).
3916 std::string file_base_name;
3917
3918 // The name of a standalone runtime to generate. Only honors EMIT_OPTIONS 'o'
3919 // and 'static_library'. When multiple targets are specified, it picks a
3920 // runtime that is compatible with all of the targets, or fails if it cannot
3921 // find one. Flags across all of the targets that do not affect runtime code
3922 // generation, such as `no_asserts` and `no_runtime`, are ignored.
3923 std::string runtime_name;
3924
3925 // The mode in which to build the Generator.
3927 // Build it as written.
3929
3930 // Build a version suitable for using for gradient descent calculation.
3933
3934 // The fn that will produce Generator(s) from the name specified.
3935 // (Note that `generator_name` is the only value that will ever be passed
3936 // for name here; it is provided for ease of interoperation with existing code.)
3937 //
3938 // If null, the default global registry of Generators will be used.
3939 using CreateGeneratorFn = std::function<AbstractGeneratorPtr(const std::string &name, const GeneratorContext &context)>;
3941
3942 // Values to substitute for GeneratorParams in the selected Generator.
3943 // Should not contain `target`.
3944 //
3945 // If any of the generator param names specified in this map are unknown
3946 // to the Generator created, an error will occur.
3948
3949 // Compiler Logger to use, for diagnostic work. If null, don't do any logging.
3951
3952 // If true, log the path of all output files to stdout.
3953 bool log_outputs = false;
3954};
3955
3956/**
3957 * Execute a Generator for AOT compilation -- this provides the implementation of
3958 * the command-line Generator interface `generate_filter_main()`, but with a structured
3959 * API that is more suitable for calling directly from code (vs command line).
3960 */
3962
3963// -----------------------------
3964
3965} // namespace Internal
3966
3967/** Create a Generator from the currently-registered Generators, use it to create a Callable.
3968 * Any GeneratorParams specified will be applied to the Generator before compilation.
3969 * If the name isn't registered, assert-fail. */
3970// @{
3972 const std::string &name,
3973 const GeneratorParamsMap &generator_params = {});
3975 const std::string &name,
3976 const GeneratorParamsMap &generator_params = {});
3977// @}
3978
3979} // namespace Halide
3980
3981// Define this namespace at global scope so that anonymous namespaces won't
3982// defeat our static_assert check; define a dummy type inside so we can
3983// check for type aliasing injected by anonymous namespace usage
3985struct halide_global_ns;
3986};
3987
3988#define _HALIDE_REGISTER_GENERATOR_IMPL(GEN_CLASS_NAME, GEN_REGISTRY_NAME, FULLY_QUALIFIED_STUB_NAME) \
3989 namespace halide_register_generator { \
3990 struct halide_global_ns; \
3991 namespace GEN_REGISTRY_NAME##_ns { \
3992 std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context); \
3993 std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context) { \
3994 using GenType = std::remove_pointer<decltype(new GEN_CLASS_NAME)>::type; /* NOLINT(bugprone-macro-parentheses) */ \
3995 return GenType::create(context, #GEN_REGISTRY_NAME, #FULLY_QUALIFIED_STUB_NAME); \
3996 } \
3997 } \
3998 namespace { \
3999 auto reg_##GEN_REGISTRY_NAME = Halide::Internal::RegisterGenerator(#GEN_REGISTRY_NAME, GEN_REGISTRY_NAME##_ns::factory); \
4000 } \
4001 } \
4002 static_assert(std::is_same<::halide_register_generator::halide_global_ns, halide_register_generator::halide_global_ns>::value, \
4003 "HALIDE_REGISTER_GENERATOR must be used at global scope");
4004
4005#define _HALIDE_REGISTER_GENERATOR2(GEN_CLASS_NAME, GEN_REGISTRY_NAME) \
4006 _HALIDE_REGISTER_GENERATOR_IMPL(GEN_CLASS_NAME, GEN_REGISTRY_NAME, GEN_REGISTRY_NAME)
4007
4008#define _HALIDE_REGISTER_GENERATOR3(GEN_CLASS_NAME, GEN_REGISTRY_NAME, FULLY_QUALIFIED_STUB_NAME) \
4009 _HALIDE_REGISTER_GENERATOR_IMPL(GEN_CLASS_NAME, GEN_REGISTRY_NAME, FULLY_QUALIFIED_STUB_NAME)
4010
4011// MSVC has a broken implementation of variadic macros: it expands __VA_ARGS__
4012// as a single token in argument lists (rather than multiple tokens).
4013// Jump through some hoops to work around this.
4014#define __HALIDE_REGISTER_ARGCOUNT_IMPL(_1, _2, _3, COUNT, ...) \
4015 COUNT
4016
4017#define _HALIDE_REGISTER_ARGCOUNT_IMPL(ARGS) \
4018 __HALIDE_REGISTER_ARGCOUNT_IMPL ARGS
4019
4020#define _HALIDE_REGISTER_ARGCOUNT(...) \
4021 _HALIDE_REGISTER_ARGCOUNT_IMPL((__VA_ARGS__, 3, 2, 1, 0))
4022
4023#define ___HALIDE_REGISTER_CHOOSER(COUNT) \
4024 _HALIDE_REGISTER_GENERATOR##COUNT
4025
4026#define __HALIDE_REGISTER_CHOOSER(COUNT) \
4027 ___HALIDE_REGISTER_CHOOSER(COUNT)
4028
4029#define _HALIDE_REGISTER_CHOOSER(COUNT) \
4030 __HALIDE_REGISTER_CHOOSER(COUNT)
4031
4032#define _HALIDE_REGISTER_GENERATOR_PASTE(A, B) \
4033 A B
4034
4035#define HALIDE_REGISTER_GENERATOR(...) \
4036 _HALIDE_REGISTER_GENERATOR_PASTE(_HALIDE_REGISTER_CHOOSER(_HALIDE_REGISTER_ARGCOUNT(__VA_ARGS__)), (__VA_ARGS__))
4037
4038// HALIDE_REGISTER_GENERATOR_ALIAS() can be used to create an an alias-with-a-particular-set-of-param-values
4039// for a given Generator in the build system. Normally, you wouldn't want to do this;
4040// however, some existing Halide clients have build systems that make it challenging to
4041// specify GeneratorParams inside the build system, and this allows a somewhat simpler
4042// customization route for them. It's highly recommended you don't use this for new code.
4043//
4044// The final argument is really an initializer-list of GeneratorParams, in the form
4045// of an initializer-list for map<string, string>:
4046//
4047// { { "gp-name", "gp-value"} [, { "gp2-name", "gp2-value" }] }
4048//
4049// It is specified as a variadic template argument to allow for the fact that the embedded commas
4050// would otherwise confuse the preprocessor; since (in this case) all we're going to do is
4051// pass it thru as-is, this is fine (and even MSVC's 'broken' __VA_ARGS__ should be OK here).
4052#define HALIDE_REGISTER_GENERATOR_ALIAS(GEN_REGISTRY_NAME, ORIGINAL_REGISTRY_NAME, ...) \
4053 namespace halide_register_generator { \
4054 struct halide_global_ns; \
4055 namespace ORIGINAL_REGISTRY_NAME##_ns { \
4056 std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context); \
4057 } \
4058 namespace GEN_REGISTRY_NAME##_ns { \
4059 std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context) { \
4060 auto g = ORIGINAL_REGISTRY_NAME##_ns::factory(context); \
4061 const Halide::GeneratorParamsMap m = __VA_ARGS__; \
4062 g->set_generatorparam_values(m); \
4063 return g; \
4064 } \
4065 } \
4066 namespace { \
4067 auto reg_##GEN_REGISTRY_NAME = Halide::Internal::RegisterGenerator(#GEN_REGISTRY_NAME, GEN_REGISTRY_NAME##_ns::factory); \
4068 } \
4069 } \
4070 static_assert(std::is_same<::halide_register_generator::halide_global_ns, halide_register_generator::halide_global_ns>::value, \
4071 "HALIDE_REGISTER_GENERATOR_ALIAS must be used at global scope");
4072
4073// The HALIDE_GENERATOR_PYSTUB macro is used to produce "PyStubs" -- i.e., CPython wrappers to let a C++ Generator
4074// be called from Python. It shouldn't be necessary to use by anything but the build system in most cases.
4075
4076#define HALIDE_GENERATOR_PYSTUB(GEN_REGISTRY_NAME, MODULE_NAME) \
4077 static_assert(PY_MAJOR_VERSION >= 3, "Python bindings for Halide require Python 3+"); \
4078 extern "C" PyObject *_halide_pystub_impl(const char *module_name, const Halide::Internal::GeneratorFactory &factory); \
4079 namespace halide_register_generator::GEN_REGISTRY_NAME##_ns { \
4080 extern std::unique_ptr<Halide::Internal::AbstractGenerator> factory(const Halide::GeneratorContext &context); \
4081 } \
4082 extern "C" HALIDE_EXPORT_SYMBOL PyObject *PyInit_##MODULE_NAME() { \
4083 const auto factory = halide_register_generator::GEN_REGISTRY_NAME##_ns::factory; \
4084 return _halide_pystub_impl(#MODULE_NAME, factory); \
4085 }
4086
4087#endif // HALIDE_GENERATOR_H_
#define internal_error
Definition Errors.h:23
#define user_error
Definition Errors.h:7
#define internal_assert(c)
Definition Errors.h:19
Defines Func - the front-end handle on a halide function, and related classes.
#define HALIDE_GENERATOR_PARAM_TYPED_SETTER(TYPE)
Definition Generator.h:413
#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.
Defines methods for introspecting in C++.
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:46
Type type() const
Definition Buffer.h:533
bool defined() const
Check if this Buffer refers to an existing Buffer.
Definition Buffer.h:381
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:497
GeneratorContext is a class that is used when using Generators (or Stubs) directly; it is used to all...
Definition Generator.h:3003
GeneratorContext with_target(const Target &t) const
GeneratorContext(const Target &t)
std::unique_ptr< T > apply(const Args &...args) const
Definition Generator.h:3034
std::unique_ptr< T > create() const
Definition Generator.h:3030
GeneratorContext & operator=(GeneratorContext &&)=default
GeneratorContext & operator=(const GeneratorContext &)=default
const Target & target() const
Definition Generator.h:3017
GeneratorContext(const Target &t, const AutoschedulerParams &autoscheduler_params)
GeneratorContext(const GeneratorContext &)=default
const AutoschedulerParams & autoscheduler_params() const
Definition Generator.h:3020
GeneratorContext(GeneratorContext &&)=default
void call_generate() override
Definition Generator.h:3858
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:3753
void call_schedule() override
Definition Generator.h:3862
std::unique_ptr< T2 > apply(const Args &...args) const
Definition Generator.h:3775
static std::unique_ptr< T > create(const Halide::GeneratorContext &context)
Definition Generator.h:3744
Generator & operator=(Generator &&that)=delete
Generator & operator=(const Generator &)=delete
void apply(const Args &...args)
Definition Generator.h:3762
void call_configure() override
Definition Generator.h:3854
std::unique_ptr< T2 > create() const
Definition Generator.h:3770
Pipeline build_pipeline() override
Build and return the Pipeline for this AbstractGenerator.
Definition Generator.h:3849
Generator(const Generator &)=delete
typename Internal::select_type< Internal::cond< Internal::has_static_halide_type_method< TBase >::value, int >, Internal::cond< std::is_same< TBase, Func >::value, int >, Internal::cond< true, Unused > >::type IntIfNonScalar
Definition Generator.h:2202
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
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:2796
GeneratorOutput(const std::string &name)
Definition Generator.h:2802
GeneratorOutput(const std::string &name, const std::vector< Type > &t, int d)
Definition Generator.h:2830
GeneratorOutput< T > & operator=(const Internal::StubOutputBuffer< T2 > &stub_output_buffer)
Definition Generator.h:2864
GeneratorOutput(const char *name)
Definition Generator.h:2806
GeneratorOutput(const std::string &name, const std::vector< Type > &t)
Definition Generator.h:2822
GeneratorOutput(size_t array_size, const std::string &name, int d)
Definition Generator.h:2834
GeneratorOutput(size_t array_size, const std::string &name, const Type &t, int d)
Definition Generator.h:2846
GeneratorOutput(const std::string &name, const Type &t, int d)
Definition Generator.h:2826
GeneratorOutput< T > & operator=(Buffer< T2, D2 > &buffer)
Definition Generator.h:2858
GeneratorOutput(size_t array_size, const std::string &name, const std::vector< Type > &t, int d)
Definition Generator.h:2850
GeneratorOutput(const std::string &name, int d)
Definition Generator.h:2814
GeneratorOutput(size_t array_size, const std::string &name)
Definition Generator.h:2810
GeneratorOutput(const std::string &name, const Type &t)
Definition Generator.h:2818
GeneratorOutput(size_t array_size, const std::string &name, const std::vector< Type > &t)
Definition Generator.h:2842
GeneratorOutput(size_t array_size, const std::string &name, const Type &t)
Definition Generator.h:2838
GeneratorOutput< T > & operator=(const Func &f)
Definition Generator.h:2869
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:38
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:3473
GeneratorBase(size_t size, const void *introspection_helper)
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:3254
std::vector< Func > output_func(const std::string &name) override
Given the name of an output, return the Func(s) for that output.
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:3195
GeneratorBase(const GeneratorBase &)=delete
GeneratorOutput< T > * add_output(const std::string &name)
Definition Generator.h:3331
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:3163
virtual void init_from_context(const Halide::GeneratorContext &context)
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:3200
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:3187
GeneratorInput< T > * add_input(const std::string &name, int dimensions)
Definition Generator.h:3240
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:3278
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:3317
bool emit_cpp_stub(const std::string &stub_file_path) override
Emit a Generator Stub (.stub.h) file to the given path.
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:3178
GeneratorParam_AutoSchedulerParams autoscheduler_
Definition Generator.h:3474
HALIDE_NO_USER_CODE_INLINE void add_requirement(const Expr &condition, Args &&...error_args)
Definition Generator.h:3346
GeneratorInput< T > * add_input(const std::string &name, const Type &t, int dimensions)
Definition Generator.h:3214
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:3156
void advance_phase(Phase new_phase)
GeneratorOutput< T > * add_output(const std::string &name, const Type &t, int dimensions)
Definition Generator.h:3291
GeneratorFactoryProvider provides a way to customize the Generators that are visible to generate_filt...
Definition Generator.h:331
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:2137
GeneratorInput_Arithmetic(const std::string &name, const TBase &def, const TBase &min, const TBase &max)
Definition Generator.h:2148
GeneratorInput_Arithmetic(size_t array_size, const std::string &name, const TBase &def)
Definition Generator.h:2142
GeneratorInput_Arithmetic(const std::string &name)
Definition Generator.h:2128
GeneratorInput_Arithmetic(size_t array_size, const std::string &name, const TBase &def, const TBase &min, const TBase &max)
Definition Generator.h:2155
GeneratorInput_Arithmetic(const std::string &name, const TBase &def)
Definition Generator.h:2132
std::string get_c_type() const override
Definition Generator.h:1676
GeneratorInput_Buffer< T > & set_estimate(Var var, Expr min, Expr extent)
Definition Generator.h:1743
GeneratorInput_Buffer(const std::string &name, const Type &t)
Definition Generator.h:1704
GeneratorInput_Buffer(const std::string &name)
Definition Generator.h:1692
GeneratorInput_Buffer(const std::string &name, const Type &t, int d)
Definition Generator.h:1698
std::vector< ImageParam >::const_iterator end() const
Definition Generator.h:1801
Expr operator()(std::vector< Expr > args) const
Definition Generator.h:1722
std::vector< ImageParam >::const_iterator begin() const
Definition Generator.h:1795
Expr operator()(Args &&...args) const
Definition Generator.h:1717
Func in(const std::vector< Func > &others)
Definition Generator.h:1765
GeneratorInput_Buffer< T > & set_estimates(const Region &estimates)
Definition Generator.h:1749
ImageParam operator[](size_t i) const
Definition Generator.h:1783
ImageParam at(size_t i) const
Definition Generator.h:1789
GeneratorInput_Buffer(const std::string &name, int d)
Definition Generator.h:1709
std::string get_c_type() const override
Definition Generator.h:1959
GeneratorInput_DynamicScalar(const std::string &name)
Definition Generator.h:1964
GeneratorInput_Func(size_t array_size, const std::string &name, int d)
Definition Generator.h:1869
Expr operator()(Args &&...args) const
Definition Generator.h:1884
Func in(const std::vector< Func > &others)
Definition Generator.h:1926
GeneratorInput_Func< T > & set_estimates(const Region &estimates)
Definition Generator.h:1910
GeneratorInput_Func< T > & set_estimate(Var var, Expr min, Expr extent)
Definition Generator.h:1904
GeneratorInput_Func(size_t array_size, const std::string &name, const Type &t, int d)
Definition Generator.h:1864
GeneratorInput_Func(const std::string &name, int d)
Definition Generator.h:1850
GeneratorInput_Func(const std::string &name, const Type &t)
Definition Generator.h:1855
GeneratorInput_Func(size_t array_size, const std::string &name, const Type &t)
Definition Generator.h:1874
Expr operator()(const std::vector< Expr > &args) const
Definition Generator.h:1889
std::string get_c_type() const override
Definition Generator.h:1835
GeneratorInput_Func(const std::string &name, const Type &t, int d)
Definition Generator.h:1845
GeneratorInput_Func(const std::string &name)
Definition Generator.h:1860
GeneratorInput_Func(size_t array_size, const std::string &name)
Definition Generator.h:1879
GeneratorInput_Scalar(size_t array_size, const std::string &name)
Definition Generator.h:2040
static Expr TBaseToExpr(const TBase2 &value)
Definition Generator.h:2021
void set_estimate(const TBase &value)
Definition Generator.h:2066
void set_estimate(size_t index, const TBase &value)
Definition Generator.h:2088
GeneratorInput_Scalar(size_t array_size, const std::string &name, const TBase &def)
Definition Generator.h:2045
GeneratorInput_Scalar(const std::string &name)
Definition Generator.h:2032
GeneratorInput_Scalar(const std::string &name, const TBase &def)
Definition Generator.h:2036
std::string get_c_type() const override
Definition Generator.h:2014
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)
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
typename std::remove_all_extents< T >::type TBase
Definition Generator.h:1584
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:2772
GeneratorOutput_Arithmetic(size_t array_size, const std::string &name)
Definition Generator.h:2776
GeneratorOutput_Buffer(const std::string &name, int d)
Definition Generator.h:2535
GeneratorOutput_Buffer(size_t array_size, const std::string &name)
Definition Generator.h:2543
GeneratorOutput_Buffer(size_t array_size, const std::string &name, const std::vector< Type > &t, int d)
Definition Generator.h:2549
GeneratorOutput_Buffer< T > & operator=(const StubOutputBuffer< T2 > &stub_output_buffer)
Definition Generator.h:2622
GeneratorOutput_Buffer(const std::string &name)
Definition Generator.h:2515
HALIDE_NO_USER_CODE_INLINE std::string get_c_type() const override
Definition Generator.h:2571
GeneratorOutput_Buffer< T > & set_estimates(const Region &estimates)
Definition Generator.h:2646
HALIDE_NO_USER_CODE_INLINE T2 as() const
Definition Generator.h:2582
GeneratorOutput_Buffer(const std::string &name, const std::vector< Type > &t)
Definition Generator.h:2529
GeneratorOutput_Buffer(size_t array_size, const std::string &name, int d)
Definition Generator.h:2563
GeneratorOutput_Buffer< T > & operator=(const Func &f)
Definition Generator.h:2631
HALIDE_NO_USER_CODE_INLINE GeneratorOutput_Buffer< T > & operator=(Buffer< T2, D2 > &buffer)
Definition Generator.h:2594
const Func & operator[](size_t i) const
Definition Generator.h:2654
GeneratorOutput_Buffer(size_t array_size, const std::string &name, const std::vector< Type > &t)
Definition Generator.h:2557
GeneratorOutput_Buffer(const std::string &name, const std::vector< Type > &t, int d)
Definition Generator.h:2521
const Func & operator[](size_t i) const
Definition Generator.h:2740
GeneratorOutput_Func(const std::string &name)
Definition Generator.h:2697
GeneratorOutput_Func< T > & operator=(const Func &f)
Definition Generator.h:2720
GeneratorOutput_Func(const std::string &name, const std::vector< Type > &t, int d)
Definition Generator.h:2701
GeneratorOutput_Func(size_t array_size, const std::string &name, const std::vector< Type > &t, int d)
Definition Generator.h:2713
GeneratorOutput_Func(const std::string &name, int d)
Definition Generator.h:2709
GeneratorOutput_Func< T > & set_estimate(const Var &var, const Expr &min, const Expr &extent)
Definition Generator.h:2745
GeneratorOutput_Func(const std::string &name, const std::vector< Type > &t)
Definition Generator.h:2705
GeneratorOutput_Func< T > & set_estimates(const Region &estimates)
Definition Generator.h:2754
const char * input_or_output() const override
Definition Generator.h:2364
GeneratorOutputBase(const std::string &name, ArgInfoKind kind, const std::vector< Type > &t, int d)
virtual std::string get_c_type() const
Definition Generator.h:2358
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)
Forward schedule-related methods to the underlying Func.
std::vector< ValueType >::const_iterator end() const
Definition Generator.h:2453
const ValueType & operator[](size_t i) const
Definition Generator.h:2435
GeneratorOutputImpl(const std::string &name, ArgInfoKind kind, const std::vector< Type > &t, int d)
Definition Generator.h:2385
const ValueType & at(size_t i) const
Definition Generator.h:2441
std::vector< ValueType >::const_iterator begin() const
Definition Generator.h:2447
FuncRef operator()(std::vector< ExprOrVar > args) const
Definition Generator.h:2411
typename std::remove_all_extents< T >::type TBase
Definition Generator.h:2375
FuncRef operator()(Args &&...args) const
Definition Generator.h:2405
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:2911
void set_from_string(const std::string &new_value_string) override
Definition Generator.h:2897
std::string get_default_value() const override
Definition Generator.h:2906
std::string get_c_type() const override
Definition Generator.h:2916
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:462
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:456
GeneratorParamBase(const std::string &name)
virtual std::string get_default_value() const =0
void set(const std::string &new_value)
Definition Generator.h:435
GeneratorParamBase(const GeneratorParamBase &)=delete
virtual std::string get_c_type() const =0
virtual bool is_looplevel_param() const
Definition Generator.h:466
virtual void set_from_string(const std::string &value_string)=0
GeneratorParamBase & operator=(GeneratorParamBase &&)=delete
const std::string & name() const
Definition Generator.h:401
GeneratorParamBase & operator=(const GeneratorParamBase &)=delete
void set(const char *new_value)
Definition Generator.h:438
void set(const std::string &new_value)
Definition Generator.h:549
GeneratorParamImpl(const std::string &name, const T &value)
Definition Generator.h:508
virtual void set_impl(const T &new_value)
Definition Generator.h:555
const std::vector< Internal::GeneratorInputBase * > & inputs() const
Definition Generator.h:3142
const std::vector< Internal::GeneratorParamBase * > & generator_params() const
Definition Generator.h:3139
GeneratorParamInfo(GeneratorBase *generator, size_t size)
const std::vector< Internal::GeneratorOutputBase * > & outputs() const
Definition Generator.h:3145
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:3065
static Type Bool(int lanes=1)
Definition Generator.h:3082
static Expr cast(Expr e)
Definition Generator.h:3070
static Expr cast(Halide::Type t, Expr e)
Definition Generator.h:3073
static Type UInt(int bits, int lanes=1)
Definition Generator.h:3091
static Type Int(int bits, int lanes=1)
Definition Generator.h:3088
static Type Float(int bits, int lanes=1)
Definition Generator.h:3085
Halide::Pipeline Pipeline
Definition Generator.h:3060
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:69
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:3111
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:316
std::vector< Expr > parameter_constraints(const Parameter &p)
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)
HALIDE_NO_USER_CODE_INLINE std::string enum_to_string(const std::map< std::string, T > &enum_map, const T &t)
Definition Generator.h:298
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...
typename select_type< cond< has_static_halide_type_method< TBase >::value, GeneratorOutput_Buffer< T > >, cond< std::is_same< TBase, Func >::value, GeneratorOutput_Func< T > >, cond< std::is_arithmetic< TBase >::value, GeneratorOutput_Arithmetic< T > > >::type GeneratorOutputImplBase
Definition Generator.h:2786
typename select_type< cond< has_static_halide_type_method< TBase >::value, GeneratorInput_Buffer< T > >, cond< std::is_same< TBase, Func >::value, GeneratorInput_Func< T > >, cond< std::is_arithmetic< TBase >::value, GeneratorInput_Arithmetic< T > >, cond< std::is_scalar< TBase >::value, GeneratorInput_Scalar< T > >, cond< std::is_same< TBase, Expr >::value, GeneratorInput_DynamicScalar< T > > >::type GeneratorInputImplBase
Definition Generator.h:2182
typename select_type< cond< std::is_same< T, Target >::value, GeneratorParam_Target< T > >, cond< std::is_same< T, LoopLevel >::value, GeneratorParam_LoopLevel >, cond< std::is_same< T, std::string >::value, GeneratorParam_String< T > >, cond< std::is_same< T, Type >::value, GeneratorParam_Type< T > >, cond< std::is_same< T, bool >::value, GeneratorParam_Bool< T > >, cond< std::is_arithmetic< T >::value, GeneratorParam_Arithmetic< T > >, cond< std::is_enum< T >::value, GeneratorParam_Enum< T > > >::type GeneratorParamImplBase
Definition Generator.h:950
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:335
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:2878
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:309
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:535
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:540
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
Expr cast(Expr a)
Cast an expression to the halide type corresponding to the C++ type T.
Definition IROperator.h:364
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:530
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.
Expr min(const FuncRef &a, const FuncRef &b)
Explicit overloads of min and max for FuncRef.
Definition Func.h:603
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:25
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
Type Bool(int lanes=1)
Construct a boolean type.
Definition Type.h:550
std::vector< Range > Region
A multi-dimensional box.
Definition Expr.h:345
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
Expr max(const FuncRef &a, const FuncRef &b)
Definition Func.h:606
MemoryType
An enum describing different address spaces to be used with Func::store_in.
Definition Expr.h:348
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:322
HALIDE_ALWAYS_INLINE const Internal::BaseExprNode * get() const
Override get() to return a BaseExprNode * instead of an IRNode *.
Definition Expr.h:316
An argument to an extern-defined Func.
static TO2 value(const FROM &from)
Definition Generator.h:493
The Dim struct represents one loop in the schedule's representation of a loop nest.
Definition Schedule.h:457
ExecuteGeneratorArgs is the set of arguments to execute_generator().
Definition Generator.h:3889
CompilerLoggerFactory compiler_logger_factory
Definition Generator.h:3950
enum Halide::Internal::ExecuteGeneratorArgs::BuildMode build_mode
std::set< OutputFileType > output_types
Definition Generator.h:3894
std::vector< std::string > suffixes
Definition Generator.h:3904
std::function< AbstractGeneratorPtr(const std::string &name, const GeneratorContext &context)> CreateGeneratorFn
Definition Generator.h:3939
HALIDE_ALWAYS_INLINE bool defined() const
static constexpr bool value
Definition Generator.h:382
typename std::conditional< First::value, typename First::type, void >::type type
Definition Generator.h:391
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:276
#define user_assert(c)
Definition test.h:10