Halide 22.0.0 Halide compiler and libraries

Public Member Functions | Static Public Member Functions | List of all members
Halide::Var Class Reference

A Halide variable, to be used when defining functions. More...

`#include <Var.h>`

## Public Member Functions
| Var (const std::string &n)
| Construct a Var with the given name. More...
| Var ()
| Construct a Var with an automatically-generated unique name. More...
const std::string & | name () const
| Get the name of a Var. More...
bool | same_as (const Var &other) const
| Test if two Vars are the same. More...
bool | is_implicit () const
int | implicit_index () const
bool | is_placeholder () const
| operator const Expr & () const
| A Var can be treated as an Expr of type Int(32) More...

## Static Public Member Functions
static Var | implicit (int n)
| Implicit var constructor. More...
static bool | is_implicit (const std::string &name)
| Return whether a variable name is of the form for an implicit argument. More...
static int | implicit_index (const std::string &name)
| Return the argument index for a placeholder argument given its name. More...
static bool | is_placeholder (const std::string &name)
| Test if a var is the placeholder variable _. More...
static Var | outermost ()
| A Var that represents the location outside the outermost loop. More...

## Detailed Description

A Halide variable, to be used when defining functions.

It is just a name, and can be reused in places where no name conflict will occur. It can be used in the left-hand-side of a function definition, or as an Expr. As an Expr, it always has type Int(32).

Examples
  tutorial/lesson_01_basics.cpp, and tutorial/lesson_02_input_image.cpp.

Definition at line 19 of file Var.h.

## Constructor & Destructor Documentation

## ◆ Var() [1/2]

Halide::Var::Var | ( | const std::string & | n | ) |

Construct a Var with the given name.

Unlike Funcs, this will be treated as the same Var as another other Var with the same name, including implicit Vars.

## ◆ Var() [2/2]

Halide::Var::Var | ( |  | ) |

Construct a Var with an automatically-generated unique name.

Referenced by outermost().

## Member Function Documentation

## ◆ name()

const std::string & Halide::Var::name | ( |  | ) | const

Get the name of a Var.

Referenced by implicit_index(), is_implicit(), is_placeholder(), Halide::VarOrRVar::name(), and same_as().

## ◆ same_as()

bool Halide::Var::same_as | ( | const Var & | other | ) | const
| inline

Test if two Vars are the same.

This simply compares the names.

Definition at line 40 of file Var.h.

References name().

## ◆ implicit()

static Var Halide::Var::implicit | ( | int | n | ) |
| static

Implicit var constructor.

Implicit variables are injected automatically into a function call if the number of arguments to the function are fewer than its dimensionality and a placeholder ("_") appears in its argument list. Defining a function to equal an expression containing implicit variables similarly appends those implicit variables, in the same order, to the left-hand-side of the definition where the placeholder ('_') appears.

For example, consider the definition:

Func f, g;
Var x, y;
f(x, y) = 3;
Halide::Var::Var
Var()
Construct a Var with an automatically-generated unique name.

A call to f with the placeholder symbol _ will have implicit arguments injected automatically, so f(2, _) is equivalent to f(2, _0), where _0 = ImplicitVar<0>(), and f(_) (and indeed f when cast to an Expr) is equivalent to f(_0, _1). The following definitions are all equivalent, differing only in the variable names.

g(_) = f*3;
g(_) = f(_)*3;
g(x, _) = f(x, _)*3;
g(x, y) = f(x, y)*3;

These are expanded internally as follows:

g(_0, _1) = f(_0, _1)*3;
g(_0, _1) = f(_0, _1)*3;
g(x, _0) = f(x, _0)*3;
g(x, y) = f(x, y)*3;

The following, however, defines g as four dimensional:

g(x, y, _) = f*3;

It is equivalent to:

g(x, y, _0, _1) = f(_0, _1)*3;

Expressions requiring differing numbers of implicit variables can be combined. The left-hand-side of a definition injects enough implicit variables to cover all of them:

Func h;
h(x) = x*3;
g(x) = h + (f + f(x)) * f(x, y);

expands to:

Func h;
h(x) = x*3;
g(x, _0, _1) = h(_0) + (f(_0, _1) + f(x, _0)) * f(x, y);

The first ten implicits, _0 through _9, are predeclared in this header and can be used for scheduling. They should never be used as arguments in a declaration or used in a call.

While it is possible to use Var::implicit or the predeclared implicits to create expressions that can be treated as small anonymous functions (e.g. Func(_0 + _1)) this is considered poor style. Instead use lambda.

Referenced by Halide::ImplicitVar< N >::to_var().

## ◆ is_implicit() [1/2]

static bool Halide::Var::is_implicit | ( | const std::string & | name | ) |
| static

Return whether a variable name is of the form for an implicit argument.

## ◆ is_implicit() [2/2]

bool Halide::Var::is_implicit | ( |  | ) | const
| inline

Definition at line 128 of file Var.h.

References is_implicit(), and name().

Referenced by implicit_index(), and is_implicit().

## ◆ implicit_index() [1/2]

static int Halide::Var::implicit_index | ( | const std::string & | name | ) |
| inline static

Return the argument index for a placeholder argument given its name.

Returns 0 for _0, 1 for _1, etc. Returns -1 if the variable is not of implicit form.

Definition at line 138 of file Var.h.

References atoi(), is_implicit(), and name().

## ◆ implicit_index() [2/2]

int Halide::Var::implicit_index | ( |  | ) | const
| inline

Definition at line 141 of file Var.h.

References implicit_index(), and name().

Referenced by implicit_index().

## ◆ is_placeholder() [1/2]

static bool Halide::Var::is_placeholder | ( | const std::string & | name | ) |
| inline static

Test if a var is the placeholder variable _.

Definition at line 148 of file Var.h.

References name().

## ◆ is_placeholder() [2/2]

bool Halide::Var::is_placeholder | ( |  | ) | const
| inline

Definition at line 151 of file Var.h.

References is_placeholder(), and name().

Referenced by is_placeholder().

## ◆ operator const Expr &()

Halide::Var::operator const Expr & | ( |  | ) | const
| inline

A Var can be treated as an Expr of type Int(32)

Definition at line 157 of file Var.h.

## ◆ outermost()

static Var Halide::Var::outermost | ( |  | ) |
| inline static

A Var that represents the location outside the outermost loop.

Definition at line 162 of file Var.h.

References Var().

The documentation for this class was generated from the following file:

- src/Var.h