Halide
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

Constructor & Destructor Documentation

◆ Var() [1/2]

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

Construct a Var with the given name.

◆ 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

◆ 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 38 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;

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.

TODO: This is almost guaranteed to incorrectly fire on user declared variables at some point. We should likely prevent user Var declarations from making names of this form.

◆ is_implicit() [2/2]

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

Definition at line 129 of file Var.h.

References name().

Referenced by implicit_index().

◆ implicit_index() [1/2]

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

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 139 of file Var.h.

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

◆ implicit_index() [2/2]

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

Definition at line 142 of file Var.h.

References name().

◆ is_placeholder() [1/2]

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

Test if a var is the placeholder variable _.

Definition at line 149 of file Var.h.

References name().

◆ is_placeholder() [2/2]

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

Definition at line 152 of file Var.h.

References name().

◆ 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 158 of file Var.h.

◆ outermost()

static Var Halide::Var::outermost ( )
inlinestatic

A Var that represents the location outside the outermost loop.

Definition at line 163 of file Var.h.

References Var().


The documentation for this class was generated from the following file:
Halide::Var::Var
Var()
Construct a Var with an automatically-generated unique name.