Halide
Parameter.h
Go to the documentation of this file.
1 #ifndef HALIDE_PARAMETER_H
2 #define HALIDE_PARAMETER_H
3 
4 /** \file
5  * Defines the internal representation of parameters to halide piplines
6  */
7 #include <string>
8 
9 #include "Buffer.h"
10 #include "IntrusivePtr.h"
11 #include "Type.h"
12 #include "Util.h" // for HALIDE_NO_USER_CODE_INLINE
13 #include "runtime/HalideRuntime.h" // for HALIDE_ALWAYS_INLINE
14 
15 namespace Halide {
16 
17 struct ArgumentEstimates;
18 struct Expr;
19 struct Type;
20 enum class MemoryType;
21 
22 namespace Internal {
23 
24 struct ParameterContents;
25 
26 /** A reference-counted handle to a parameter to a halide
27  * pipeline. May be a scalar parameter or a buffer */
28 class Parameter {
29  void check_defined() const;
30  void check_is_buffer() const;
31  void check_is_scalar() const;
32  void check_dim_ok(int dim) const;
33  void check_type(const Type &t) const;
34 
35 protected:
37 
38 public:
39  /** Construct a new undefined handle */
40  Parameter() = default;
41 
42  /** Construct a new parameter of the given type. If the second
43  * argument is true, this is a buffer parameter of the given
44  * dimensionality, otherwise, it is a scalar parameter (and the
45  * dimensionality should be zero). The parameter will be given a
46  * unique auto-generated name. */
47  Parameter(const Type &t, bool is_buffer, int dimensions);
48 
49  /** Construct a new parameter of the given type with name given by
50  * the third argument. If the second argument is true, this is a
51  * buffer parameter, otherwise, it is a scalar parameter. The
52  * third argument gives the dimensionality of the buffer
53  * parameter. It should be zero for scalar parameters. If the
54  * fifth argument is true, the the name being passed in was
55  * explicitly specified (as opposed to autogenerated). */
56  Parameter(const Type &t, bool is_buffer, int dimensions, const std::string &name);
57 
58  Parameter(const Parameter &) = default;
59  Parameter &operator=(const Parameter &) = default;
60  Parameter(Parameter &&) = default;
61  Parameter &operator=(Parameter &&) = default;
62 
63  /** Get the type of this parameter */
64  Type type() const;
65 
66  /** Get the dimensionality of this parameter. Zero for scalars. */
67  int dimensions() const;
68 
69  /** Get the name of this parameter */
70  const std::string &name() const;
71 
72  /** Does this parameter refer to a buffer/image? */
73  bool is_buffer() const;
74 
75  /** If the parameter is a scalar parameter, get its currently
76  * bound value. Only relevant when jitting */
77  template<typename T>
79  check_type(type_of<T>());
80  return *((const T *)(scalar_address()));
81  }
82 
83  /** This returns the current value of scalar<type()>()
84  * as an Expr. */
85  Expr scalar_expr() const;
86 
87  /** If the parameter is a scalar parameter, set its current
88  * value. Only relevant when jitting */
89  template<typename T>
91  check_type(type_of<T>());
92  *((T *)(scalar_address())) = val;
93  }
94 
95  /** If the parameter is a scalar parameter, set its current
96  * value. Only relevant when jitting */
98  check_type(val_type);
99  memcpy(scalar_address(), &val, val_type.bytes());
100  }
101 
102  /** If the parameter is a buffer parameter, get its currently
103  * bound buffer. Only relevant when jitting */
104  Buffer<void> buffer() const;
105 
106  /** Get the raw currently-bound buffer. null if unbound */
107  const halide_buffer_t *raw_buffer() const;
108 
109  /** If the parameter is a buffer parameter, set its current
110  * value. Only relevant when jitting */
111  void set_buffer(const Buffer<void> &b);
112 
113  /** Get the pointer to the current value of the scalar
114  * parameter. For a given parameter, this address will never
115  * change. Only relevant when jitting. */
116  void *scalar_address() const;
117 
118  /** Tests if this handle is the same as another handle */
119  bool same_as(const Parameter &other) const;
120 
121  /** Tests if this handle is non-nullptr */
122  bool defined() const;
123 
124  /** Get and set constraints for the min, extent, stride, and estimates on
125  * the min/extent. */
126  //@{
127  void set_min_constraint(int dim, Expr e);
128  void set_extent_constraint(int dim, Expr e);
129  void set_stride_constraint(int dim, Expr e);
130  void set_min_constraint_estimate(int dim, Expr min);
131  void set_extent_constraint_estimate(int dim, Expr extent);
132  void set_host_alignment(int bytes);
133  Expr min_constraint(int dim) const;
134  Expr extent_constraint(int dim) const;
135  Expr stride_constraint(int dim) const;
136  Expr min_constraint_estimate(int dim) const;
137  Expr extent_constraint_estimate(int dim) const;
138  int host_alignment() const;
139  //@}
140 
141  /** Get and set constraints for scalar parameters. These are used
142  * directly by Param, so they must be exported. */
143  // @{
144  void set_min_value(const Expr &e);
145  Expr min_value() const;
146  void set_max_value(const Expr &e);
147  Expr max_value() const;
148  void set_estimate(Expr e);
149  Expr estimate() const;
150  // @}
151 
152  /** Get and set the default values for scalar parameters. At present, these
153  * are used only to emit the default values in the metadata. There isn't
154  * yet a public API in Param<> for them (this is used internally by the
155  * Generator code). */
156  // @{
157  void set_default_value(const Expr &e);
158  Expr default_value() const;
159  // @}
160 
161  /** Order Parameters by their IntrusivePtr so they can be used
162  * to index maps. */
163  bool operator<(const Parameter &other) const {
164  return contents < other.contents;
165  }
166 
167  /** Get the ArgumentEstimates appropriate for this Parameter. */
169 
171  MemoryType memory_type() const;
172 };
173 
174 /** Validate arguments to a call to a func, image or imageparam. */
175 void check_call_arg_types(const std::string &name, std::vector<Expr> *args, int dims);
176 
177 } // namespace Internal
178 } // namespace Halide
179 
180 #endif
Halide::Internal::Parameter::set_scalar
HALIDE_NO_USER_CODE_INLINE void set_scalar(T val)
If the parameter is a scalar parameter, set its current value.
Definition: Parameter.h:90
Halide::Internal::Parameter::raw_buffer
const halide_buffer_t * raw_buffer() const
Get the raw currently-bound buffer.
Halide::Internal::Parameter::set_max_value
void set_max_value(const Expr &e)
Halide::Internal::Parameter::set_default_value
void set_default_value(const Expr &e)
Get and set the default values for scalar parameters.
Halide::Internal::Parameter::max_value
Expr max_value() const
Halide::Internal::Parameter
A reference-counted handle to a parameter to a halide pipeline.
Definition: Parameter.h:28
Halide::Internal::Parameter::set_extent_constraint_estimate
void set_extent_constraint_estimate(int dim, Expr extent)
Halide::Internal::Parameter::min_value
Expr min_value() const
Halide::Internal::check_call_arg_types
void check_call_arg_types(const std::string &name, std::vector< Expr > *args, int dims)
Validate arguments to a call to a func, image or imageparam.
Halide::Type::bytes
int bytes() const
The number of bytes required to store a single scalar value of this type.
Definition: Type.h:292
Halide::min
Expr min(const FuncRef &a, const FuncRef &b)
Explicit overloads of min and max for FuncRef.
Definition: Func.h:584
Halide::Internal::Parameter::set_min_constraint_estimate
void set_min_constraint_estimate(int dim, Expr min)
Halide::Internal::Parameter::set_host_alignment
void set_host_alignment(int bytes)
Halide::Internal::Parameter::extent_constraint
Expr extent_constraint(int dim) const
Halide::Internal::Parameter::stride_constraint
Expr stride_constraint(int dim) const
Halide::Internal::Parameter::memory_type
MemoryType memory_type() const
Halide::Internal::IntrusivePtr< ParameterContents >
Halide::Type
Types in the halide type system.
Definition: Type.h:276
Halide::Internal::Parameter::default_value
Expr default_value() const
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition: AbstractGenerator.h:19
Halide::Internal::Parameter::type
Type type() const
Get the type of this parameter.
halide_scalar_value_t
halide_scalar_value_t is a simple union able to represent all the well-known scalar values in a filte...
Definition: HalideRuntime.h:1662
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::Internal::Parameter::set_stride_constraint
void set_stride_constraint(int dim, Expr e)
Halide::Buffer< void >
Buffer.h
Halide::Internal::Parameter::set_estimate
void set_estimate(Expr e)
Halide::Internal::Parameter::scalar_address
void * scalar_address() const
Get the pointer to the current value of the scalar parameter.
Halide::Internal::Parameter::set_min_value
void set_min_value(const Expr &e)
Get and set constraints for scalar parameters.
Halide::Internal::Parameter::scalar_expr
Expr scalar_expr() const
This returns the current value of scalar<type()>() as an Expr.
Halide::Internal::Parameter::set_buffer
void set_buffer(const Buffer< void > &b)
If the parameter is a buffer parameter, set its current value.
Halide::Internal::Parameter::min_constraint
Expr min_constraint(int dim) const
Type.h
Halide::Internal::Parameter::operator=
Parameter & operator=(const Parameter &)=default
Halide::Internal::Parameter::store_in
void store_in(MemoryType memory_type)
HALIDE_NO_USER_CODE_INLINE
#define HALIDE_NO_USER_CODE_INLINE
Definition: Util.h:45
Halide::Internal::Parameter::set_scalar
HALIDE_NO_USER_CODE_INLINE void set_scalar(const Type &val_type, halide_scalar_value_t val)
If the parameter is a scalar parameter, set its current value.
Definition: Parameter.h:97
Halide::Internal::Parameter::get_argument_estimates
ArgumentEstimates get_argument_estimates() const
Get the ArgumentEstimates appropriate for this Parameter.
Halide::Internal::Parameter::host_alignment
int host_alignment() const
Halide::Internal::Parameter::operator<
bool operator<(const Parameter &other) const
Order Parameters by their IntrusivePtr so they can be used to index maps.
Definition: Parameter.h:163
Halide::Internal::Parameter::name
const std::string & name() const
Get the name of this parameter.
HalideRuntime.h
memcpy
void * memcpy(void *s1, const void *s2, size_t n)
Halide::Internal::Parameter::same_as
bool same_as(const Parameter &other) const
Tests if this handle is the same as another handle.
Halide::Internal::Parameter::Parameter
Parameter()=default
Construct a new undefined handle.
Halide::Internal::SyntheticParamType::Type
@ Type
halide_buffer_t
The raw representation of an image passed around by generated Halide code.
Definition: HalideRuntime.h:1490
Halide::Internal::Parameter::defined
bool defined() const
Tests if this handle is non-nullptr.
Halide::Internal::Parameter::min_constraint_estimate
Expr min_constraint_estimate(int dim) const
Halide::Internal::Parameter::estimate
Expr estimate() const
Halide::Internal::Parameter::extent_constraint_estimate
Expr extent_constraint_estimate(int dim) const
Halide::Internal::Parameter::contents
IntrusivePtr< ParameterContents > contents
Definition: Parameter.h:36
Halide::Internal::Parameter::scalar
HALIDE_NO_USER_CODE_INLINE T scalar() const
If the parameter is a scalar parameter, get its currently bound value.
Definition: Parameter.h:78
Halide::Expr
A fragment of Halide syntax.
Definition: Expr.h:257
Halide::MemoryType
MemoryType
An enum describing different address spaces to be used with Func::store_in.
Definition: Expr.h:347
Util.h
Halide::Internal::Parameter::buffer
Buffer< void > buffer() const
If the parameter is a buffer parameter, get its currently bound buffer.
Halide::Internal::Parameter::dimensions
int dimensions() const
Get the dimensionality of this parameter.
IntrusivePtr.h
Halide::Internal::Parameter::set_min_constraint
void set_min_constraint(int dim, Expr e)
Get and set constraints for the min, extent, stride, and estimates on the min/extent.
Halide::ArgumentEstimates
Definition: Argument.h:18
Halide::Internal::Parameter::set_extent_constraint
void set_extent_constraint(int dim, Expr e)
Halide::Internal::Parameter::is_buffer
bool is_buffer() const
Does this parameter refer to a buffer/image?