Builder C++-API

Contents

Builder C++-API#

Common#

Common Enumerations#

arithmetic#

enum class tinytc::arithmetic#

Arithmetic operations.

Values:

enumerator add = tinytc_arithmetic_add#

add

enumerator sub = tinytc_arithmetic_sub#

subtract

enumerator mul = tinytc_arithmetic_mul#

multiply

enumerator div = tinytc_arithmetic_div#

divide

enumerator rem = tinytc_arithmetic_rem#

division remainder

enumerator shl = tinytc_arithmetic_shl#

left shift

enumerator shr = tinytc_arithmetic_shr#

arithmetic right shift

enumerator and_ = tinytc_arithmetic_and#

bitwise and

enumerator or_ = tinytc_arithmetic_or#

bitwise or

enumerator xor_ = tinytc_arithmetic_xor#

bitwise xor

arithmetic_unary#

enum class tinytc::arithmetic_unary#

Arithmetic operations (unary)

Values:

enumerator neg = tinytc_arithmetic_unary_neg#

negation

enumerator not_ = tinytc_arithmetic_unary_not#

bitwise not

cmp_condition#

enum class tinytc::cmp_condition#

Compare operation.

Values:

enumerator eq = tinytc_cmp_condition_eq#

equals

enumerator ne = tinytc_cmp_condition_ne#

not equal

enumerator gt = tinytc_cmp_condition_gt#

greater than

enumerator ge = tinytc_cmp_condition_ge#

greather or equal than

enumerator lt = tinytc_cmp_condition_lt#

less than

enumerator le = tinytc_cmp_condition_le#

less or equal than

scalar_type#

enum class tinytc::scalar_type#

Scalar types.

Values:

enumerator i1 = tinytc_scalar_type_i1#

Signed 1 bit integer (boolean)

enumerator i8 = tinytc_scalar_type_i8#

Signed 8 bit integer.

enumerator i16 = tinytc_scalar_type_i16#

Signed 16 bit integer.

enumerator i32 = tinytc_scalar_type_i32#

Signed 32 bit integer.

enumerator i64 = tinytc_scalar_type_i64#

Signed 64 bit integer.

enumerator index = tinytc_scalar_type_index#

Unsigned Integer type for indices.

enumerator f32 = tinytc_scalar_type_f32#

Single precision floating point (32 bit)

enumerator f64 = tinytc_scalar_type_f64#

Double precision floating point (64 bit)

transpose#

enum class tinytc::transpose#

Transpose.

Values:

enumerator N = tinytc_transpose_N#

no transpose

enumerator T = tinytc_transpose_T#

transpose

Common Functions#

is_dynamic_value#

inline bool tinytc::is_dynamic_value(std::int64_t i)#

Check if mode i is dynamic (‘?’)

to_string(arithmetic)#

inline char const *tinytc::to_string(arithmetic op)#

Convert arithmetic operation type to string.

Parameters:

op – Arithmetic operation type

Returns:

C-string

to_string(arithmetic_unary)#

inline char const *tinytc::to_string(arithmetic_unary op)#

Convert arithmetic operation type to string (unary)

Parameters:

op – Arithmetic operation type

Returns:

C-string

to_string(cmp_condition)#

inline char const *tinytc::to_string(cmp_condition cond)#

Convert cmp condition to string.

Parameters:

cond – Condition

Returns:

C-string

to_string(scalar_type)#

inline char const *tinytc::to_string(scalar_type ty)#

Convert scalar type to string.

to_string(transpose)#

inline char const *tinytc::to_string(transpose t)#

Convert transpose to string.

Parameters:

t – Transpose

Returns:

C-string

size#

inline std::size_t tinytc::size(scalar_type ty)#

Size of scalar type in bytes.

Common Classes#

builder_error#

class builder_error : public std::exception#

Builder exception enhanced with location.

Public Functions

inline builder_error(status code, location const &loc)#

ctor; taking location and status code

inline auto code() const noexcept#

Get status code.

inline auto loc() const noexcept -> location const&#

Get location.

inline char const *what() const noexcept override#

Get explanatory string.

Common Typedefs#

position#

using tinytc::position = ::tinytc_position#

Alias for tinytc_position in namespace tinytc.

location#

using tinytc::location = ::tinytc_location#

Alias for tinytc_location in namespace tinytc.

Common Variables#

dynamic#

static constexpr std::int64_t tinytc::dynamic = TINYTC_DYNAMIC#

Data Type#

Data Type Functions#

make_memref#

inline data_type tinytc::make_memref(scalar_type scalar_ty, std::vector<std::int64_t> const &shape, std::vector<std::int64_t> const &stride = {}, location const &loc = {})#

Make a memref data type.

Cf. tinytc_memref_type_create

Parameters:
  • scalar_ty – Element type

  • shape – Tensor shape

  • stride – Tensor stride

  • loc – Source code location

Returns:

Data type

make_group#

inline data_type tinytc::make_group(data_type const &memref_ty, std::int64_t offset = 0, location const &loc = {})#

Make a group data type.

Parameters:
  • memref_ty – Memref data type

  • offset – Offset parameter

  • loc – Source code location

Returns:

Data type

make_scalar#

inline data_type tinytc::make_scalar(scalar_type scalar_ty, location const &loc = {})#

Make a scalar data type.

Cf. tinytc_scalar_type_create

Parameters:
  • scalar_ty – Scalar type

  • loc – Source code location

Returns:

Data type

Data Type Classes#

data_type#

class data_type : public tinytc::shared_handle<tinytc_data_type_t>#

Reference-counting wrapper for tinytc_data_type_t.

Public Functions

inline shared_handle()#

Create empty (invalid) handle.

inline explicit shared_handle(T obj, bool needs_retain = false)#

Create handle from C handle.

inline shared_handle(shared_handle const &other)#

Copy ctor.

inline shared_handle(shared_handle &&other) noexcept#

Move ctor.

Data Type Structures#

to_scalar_type#

template<typename T>
struct to_scalar_type#

Returns the scalar type corresponding to C++ type T

Specializations exist for bool, (u)int8_t, (u)int16_t, (u)int32_t, (u)int64_t, float, double. The scalar_type is stored in the static constexpr member “value”.

Data Type Variables#

to_scalar_type_v#

template<typename T>
constexpr scalar_type tinytc::to_scalar_type_v = to_scalar_type<T>::value#

Convenience variable for to_scalar_type.

Example:

scalar_type ty = to_scalar_type_v<float>; 

Function#

Function Functions#

make_function#

inline func tinytc::make_function(func const &prototype, region const &body, location const &loc = {})#

Make function.

Parameters:
  • prototype – Function prototype

  • body – Function body

  • loc – Source code location

Returns:

Function

make_function_prototype#

inline func tinytc::make_function_prototype(char const *name, std::vector<value> &arg_list, location const &loc = {})#

Make function prototype.

Parameters:
  • name – Function name

  • arg_list – Argument list

  • loc – Source code location

Returns:

Function

set_work_group_size#

inline void tinytc::set_work_group_size(func &fun, std::int32_t x, std::int32_t y)#

Set work-group size (x,y)

Parameters:
  • fun – Function object; must have been created with “make_function”

  • x – x

  • y – y

set_subgroup_size#

inline void tinytc::set_subgroup_size(func &fun, std::int32_t sgs)#

Set subgroup size.

Parameters:
  • fun – Function object; must have been created with “make_function”

  • sgs – Subgroup size

Function Classes#

func#

class func : public tinytc::shared_handle<tinytc_func_t>#

Reference-counting wrapper for tinytc_func_t.

Public Functions

inline shared_handle()#

Create empty (invalid) handle.

inline explicit shared_handle(T obj, bool needs_retain = false)#

Create handle from C handle.

inline shared_handle(shared_handle const &other)#

Copy ctor.

inline shared_handle(shared_handle &&other) noexcept#

Move ctor.

function_builder#

class function_builder#

Builder for functions.

Public Functions

inline function_builder(std::string name)#

creates function @name

Parameters:

name – Function name

inline func get_product(location const &loc = {})#

Returns built product.

Parameters:

loc – Source code location

Returns:

Function

inline value argument(data_type const &ty, std::string const &name = "", location const &loc = {})#
%name: %ty 
Parameters:
  • ty – Argument type

  • name – Argument name

  • loc – Source code location

Returns:

Value

inline void work_group_size(std::int32_t x, std::int32_t y)#
work_group_size(%x, %y) 
Parameters:
  • x – x

  • y – y

inline void subgroup_size(std::int32_t subgroup_size)#
subgroup_size(%subgroup_size) 
Parameters:

subgroup_size – Subgroup size

template<typename F>
inline void body(F &&f, location const &loc = {})#

Build function body with functor f(region_builder&) -> void.

Template Parameters:

F – Functor type

Parameters:
  • f – Functor

  • loc – Source code location

Instruction#

Instruction Functions#

make_alloca#

inline inst tinytc::make_alloca(data_type const &ty, location const &loc = {})#

Make alloca instruction.

Parameters:
  • ty – Memref type of allocated variable

  • loc – Source code location

Returns:

Instruction

make_axpby#

inline inst tinytc::make_axpby(transpose tA, bool atomic, value const &alpha, value const &A, value const &beta, value const &B, location const &loc = {})#

Make AXPBY instruction.

Parameters:
  • tA – Operation applied on A

  • atomic – true for atomic updates of B

  • alpha\(\alpha\)

  • A – A

  • beta\(\beta\)

  • B – B

  • loc – Source code location

Returns:

Instruction

make_arith(arithmetic,value const&,value const&,location const&)#

inline inst tinytc::make_arith(arithmetic op, value const &a, value const &b, location const &loc = {})#

Make arithmetic instruction (binary)

Parameters:
  • op – Arithmetic operation type

  • a – First operand

  • b – Second operand

  • loc – Source code location

Returns:

Instruction

make_arith(arithmetic_unary,value const&,location const&)#

inline inst tinytc::make_arith(arithmetic_unary op, value const &a, location const &loc = {})#

Make arithmetic instruction (unary)

Parameters:
  • op – Arithmetic operation type

  • a – Operand

  • loc – Source code location

Returns:

Instruction

make_cast#

inline inst tinytc::make_cast(value const &a, scalar_type to_ty, location const &loc = {})#

Make cast instruction.

Parameters:
  • a – Operand

  • to_ty – Target type

  • loc – Source code lcoation

Returns:

Instruction

make_cmp#

inline inst tinytc::make_cmp(cmp_condition cond, value const &a, value const &b, location const &loc = {})#

Make compare instruction.

Parameters:
  • cond – Condition type

  • a – First operand

  • b – Second operand

  • loc – Source code location

Returns:

Instruction

make_expand#

inline inst tinytc::make_expand(value const &a, std::int64_t mode, std::vector<value> const &expand_shape, location const &loc = {})#

Make expand instruction.

Parameters:
  • a – Operand

  • mode – Expanded mode

  • expand_shape – New shape of mode

  • loc – Source code location

Returns:

Instruction

make_for#

inline inst tinytc::make_for(value const &loop_var, value const &from, value const &to, value const &step, region const &body, location const &loc = {})#

Make for loop instruction.

Parameters:
  • loop_var – Loop variable

  • from – Loop variable start

  • to – Loop variable bound

  • step – Loop variable step

  • body – Loop body

  • loc – Source code location

Returns:

Instruction

make_foreach#

inline inst tinytc::make_foreach(value const &loop_var, value const &from, value const &to, region const &body, location const &loc = {})#

Make foreach loop instruction.

Parameters:
  • loop_var – Loop variable

  • from – Loop variable start

  • to – Loop variable bound

  • body – Loop body

  • loc – Source code location

Returns:

Instruction

make_fuse#

inline inst tinytc::make_fuse(value const &a, std::int64_t from, std::int64_t to, location const &loc = {})#

Make fuse instruciton.

Parameters:
  • a – Operand

  • from – First mode to fuse

  • to – Last mode to fuse

  • loc – Source code location

Returns:

Instruction

make_gemm#

inline inst tinytc::make_gemm(transpose tA, transpose tB, bool atomic, value const &alpha, value const &A, value const &B, value const &beta, value const &C, location const &loc = {})#

Make GEMM instruction.

Parameters:
  • tA – Operation applied on A

  • tB – Operation applied on B

  • atomic – true for atomic updates of C

  • alpha\(\alpha\)

  • A – A

  • B – B

  • beta\(\beta\)

  • C – C

  • loc – Source code location

Returns:

Instruction

make_gemv#

inline inst tinytc::make_gemv(transpose tA, bool atomic, value const &alpha, value const &A, value const &B, value const &beta, value const &C, location const &loc = {})#

Make GEMV instruction.

Parameters:
  • tA – Operation applied on A

  • atomic – true for atomic updates of C

  • alpha\(\alpha\)

  • A – A

  • B – B

  • beta\(\beta\)

  • C – C

  • loc – Source code location

Returns:

Instruction

make_ger#

inline inst tinytc::make_ger(bool atomic, value const &alpha, value const &A, value const &B, value const &beta, value const &C, location const &loc = {})#

Make GER instruction.

Parameters:
  • atomic – true for atomic updates of C

  • alpha\(\alpha\)

  • A – A

  • B – B

  • beta\(\beta\)

  • C – C

  • loc – Source code location

Returns:

Instruction

make_group_id#

inline inst tinytc::make_group_id(location const &loc = {})#

Make group id instruction.

Parameters:

loc – Source code location

Returns:

Instruction

make_group_size#

inline inst tinytc::make_group_size(location const &loc = {})#

Make group size instruction.

Parameters:

loc – Source code location

Returns:

Instruction

make_hadamard#

inline inst tinytc::make_hadamard(bool atomic, value const &alpha, value const &A, value const &B, value const &beta, value const &C, location const &loc = {})#

Make hadamard instruction.

Parameters:
  • atomic – true for atomic updates of C

  • alpha\(\alpha\)

  • A – A

  • B – B

  • beta\(\beta\)

  • C – C

  • loc – Source code location

Returns:

Instruction

make_if#

inline inst tinytc::make_if(value const &condition, region const &then, region const &otherwise = region{}, std::vector<scalar_type> const &return_type_list = {}, location const &loc = {})#

Make if condition instruction.

Parameters:
  • condition – Condition value (of type bool)

  • then – Then region

  • otherwise – Else region

  • return_type_list – Types of returned values

  • loc – Source code location

Returns:

Instruction

make_load#

inline inst tinytc::make_load(value const &a, std::vector<value> const &index_list, location const &loc = {})#

Make load instruction.

Parameters:
  • a – Operand

  • index_list – Vector of indices

  • loc – Source code location

Returns:

Instruction

make_size#

inline inst tinytc::make_size(value const &a, std::int64_t mode, location const &loc = {})#

Make size instruction.

Parameters:
  • a – Operand

  • mode – Mode

  • loc – Source code location

Returns:

Instruction

make_store#

inline inst tinytc::make_store(value const &val, value const &a, std::vector<value> const &index_list, location const &loc = {})#

Make store instruction.

Parameters:
  • val – Value that is stored

  • a – Target memref

  • index_list – Vector of indices

  • loc – Source code location

Returns:

Instruction

make_subview#

inline inst tinytc::make_subview(value const &a, std::vector<value> const &offset_list, std::vector<value> const &size_list, location const &loc = {})#

Make subview instruction.

Parameters:
  • a – Operand

  • offset_list – Vector of offsets

  • size_list – Vector of sizes; initialize with empty value if only offset is required

  • loc – Source code location

Returns:

Instruction

make_sum#

inline inst tinytc::make_sum(transpose tA, bool atomic, value const &alpha, value const &A, value const &beta, value const &B, location const &loc = {})#

Make sum instruction.

Parameters:
  • tA – Operation applied on A

  • atomic – true for atomic updates of B

  • alpha\(\alpha\)

  • A – A

  • beta\(\beta\)

  • B – B

  • loc – Source code location

Returns:

Instruction

make_yield#

inline inst tinytc::make_yield(std::vector<value> const &yield_list, location const &loc = {})#

Make yield instruction.

Parameters:
  • yield_list – Yielded values

  • loc – Source code location

Returns:

Instruction

Instruction Classes#

inst#

class inst : public tinytc::shared_handle<tinytc_inst_t>#

Reference-counting wrapper for tinytc_inst_t.

Public Functions

inline auto get_value() const -> value#

Get result value.

Returns:

Value; may be empty

inline auto get_values() const -> std::vector<value>#

Get result values.

Returns:

Vector of values

inline shared_handle()#

Create empty (invalid) handle.

inline explicit shared_handle(T obj, bool needs_retain = false)#

Create handle from C handle.

inline shared_handle(shared_handle const &other)#

Copy ctor.

inline shared_handle(shared_handle &&other) noexcept#

Move ctor.

Program#

Program Functions#

make_program#

inline prog tinytc::make_program(std::vector<func> &fun_list, location const &loc = {})#

Make program.

Parameters:
  • fun_list – Vector of functions

  • loc – Source code location

Returns:

Program

Program Classes#

prog#

class prog : public tinytc::shared_handle<tinytc_prog_t>#

Reference-counting wrapper for tinytc_prog_t.

Public Functions

inline void dump() const#

Dump program to stderr.

inline void print_to_file(char const *filename) const#

Dump program to file.

Parameters:

filename – Path to file

inline auto print_to_string() const -> unique_handle<char*>#

Dump program to string.

Returns:

C-string (unique handle)

inline shared_handle()#

Create empty (invalid) handle.

inline explicit shared_handle(T obj, bool needs_retain = false)#

Create handle from C handle.

inline shared_handle(shared_handle const &other)#

Copy ctor.

inline shared_handle(shared_handle &&other) noexcept#

Move ctor.

program_builder#

class program_builder#

Builder for programs.

Public Functions

template<typename F>
inline void create(std::string name, F &&f, location const &loc = {})#

create function @name with functor f(function_builder&) -> void

Template Parameters:

F – Functor type

Parameters:
  • name – Function name

  • f – Functor

  • loc – Source code location

inline void add(func f)#

Add function.

Parameters:

f – function

inline prog get_product(location const &loc = {})#

Returns built product.

Parameters:

loc – Source code location

Returns:

Program

Region#

Region Functions#

make_region#

inline region tinytc::make_region(std::vector<inst> &instructions, location const &loc = {})#

Make region.

Parameters:
  • instructions – Vector of instructions

  • loc – Source code location

Returns:

Region

Region Classes#

region#

class region : public tinytc::shared_handle<tinytc_region_t>#

Reference-counting wrapper for tinytc_region_t.

Public Functions

inline shared_handle()#

Create empty (invalid) handle.

inline explicit shared_handle(T obj, bool needs_retain = false)#

Create handle from C handle.

inline shared_handle(shared_handle const &other)#

Copy ctor.

inline shared_handle(shared_handle &&other) noexcept#

Move ctor.

region_builder#

class region_builder#

Builder for regions.

Public Functions

inline auto get_product(location const &loc = {}) -> region#

Returns built product.

Parameters:

loc – Source code location

Returns:

Region

inline auto add(inst i, std::string const &name = "") -> value#

Add instruction.

Parameters:
  • i – Instruction

  • name – Result name

Returns:

Value returned by instruction; may be empty

inline auto add_multivalued(inst i, std::string const &name = "") -> std::vector<value>#

Add instruction that returns multiple values.

Parameters:
  • i – Instruction

  • name – Result name

Returns:

Values returned by instruction

template<typename F>
inline void for_loop(scalar_type loop_var_ty, value const &from, value const &to, F &&f, std::string const &name = "", location const &loc = {})#

Build for-loop with functor f(region_builder&) -> void.

Template Parameters:

F – Functor type

Parameters:
  • loop_var_ty – Type of loop variable

  • from – Loop variable start

  • to – Loop variable bound

  • f – Functor

  • name – Loop variable name

  • loc – Source code location

template<typename F>
inline void for_loop(scalar_type loop_var_ty, value const &from, value const &to, value const &step, F &&f, std::string const &name = "", location const &loc = {})#

Build for-loop with functor f(region_builder&) -> void.

Template Parameters:

F – Functor type

Parameters:
  • loop_var_ty – Type of loop variable

  • from – Loop variable start

  • to – Loop variable bound

  • step – Loop variable step

  • f – Functor

  • name – Loop variable name

  • loc – Source code location

template<typename F>
inline void foreach(data_type const &loop_var_ty, value const &from, value const &to, F &&f, std::string const &name = "", location const &loc = {})#

Build foreach-loop with functor f(region_builder&) -> void.

Template Parameters:

F – Functor type

Parameters:
  • loop_var_ty – Type of loop variable

  • from – Loop variable start

  • to – Loop variable bound

  • f – functor

  • name – Loop variable name

  • loc – Source code location

template<typename F>
inline auto if_condition(value const &condition, F &&then, std::vector<scalar_type> const &return_type_list = {}, location const &loc = {}) -> std::vector<value>#

Build if with functor then(region_builder&) -> void.

Template Parameters:

F – Functor type

Parameters:
  • condition – Condition value

  • then – Then region functor

  • return_type_list – Types of returned values

  • loc – Source code location

Returns:

Returned values

template<typename F, typename G>
inline auto ifelse(value const &condition, F &&then, G &&otherwise, std::vector<scalar_type> const &return_type_list = {}, location const &loc = {}) -> std::vector<value>#

Build if/else with functors then(region_builder&) -> void and otherwise(region_builder&) -> void.

Template Parameters:
  • F – “if” functor type

  • G – “else” functor type

Parameters:
  • condition – If condition

  • then – “if” functor

  • otherwise – “else” functor

  • return_type_list – List of types of returned values

  • loc – Source code location

Returns:

Returned values

Value#

Value Functions#

make_dynamic(location const&)#

inline auto tinytc::make_dynamic(location const &loc = {}) -> value#

Make dynamic (‘?’)

Parameters:

loc – Source code location

Returns:

Value

make_imm(float,location const&)#

inline auto tinytc::make_imm(float imm, location const &loc = {}) -> value#

Make immediate value.

Type is f32.

Parameters:
  • imm – Float value

  • loc – Source code location

Returns:

Value

make_imm(double,scalar_type,location const&)#

inline auto tinytc::make_imm(double imm, scalar_type type = scalar_type::f64, location const &loc = {}) -> value#

Make immediate value.

Parameters:
  • imm – Float value

  • type – Type of immediate value

  • loc – Source code location

Returns:

Value

make_imm(std::int8_t,location const&)#

inline auto tinytc::make_imm(std::int8_t imm, location const &loc = {}) -> value#

Make immediate value.

Type is i8.

Parameters:
  • imm – Int value

  • loc – Source code location

Returns:

Value

make_imm(std::int16_t,location const&)#

inline auto tinytc::make_imm(std::int16_t imm, location const &loc = {}) -> value#

Make immediate value.

Type is i16.

Parameters:
  • imm – Int value

  • loc – Source code location

Returns:

Value

make_imm(std::int32_t,location const&)#

inline auto tinytc::make_imm(std::int32_t imm, location const &loc = {}) -> value#

Make immediate value.

Type is i32.

Parameters:
  • imm – Int value

  • loc – Source code location

Returns:

Value

make_imm(std::int64_t,scalar_type,location const&)#

inline auto tinytc::make_imm(std::int64_t imm, scalar_type type = scalar_type::i64, location const &loc = {}) -> value#

Make immediate value.

Parameters:
  • imm – Int value

  • type – Type of immediate value

  • loc – Source code location

Returns:

Value

make_index(std::int32_t,location const&)#

inline auto tinytc::make_index(std::int32_t imm, location const &loc = {}) -> value#

Make immediate index value.

Parameters:
  • imm – index value

  • loc – Source code location

Returns:

Value

make_index(std::int64_t,location const&)#

inline auto tinytc::make_index(std::int64_t imm, location const &loc = {}) -> value#

Make immediate index value.

Parameters:
  • imm – index value

  • loc – Source code location

Returns:

Value

make_value(data_type const&,location const&)#

inline auto tinytc::make_value(data_type const &ty, location const &loc = {}) -> value#

Make value.

Parameters:
  • ty – Data type

  • loc – Source code location

Returns:

Value

make_value(scalar_type,location const&)#

inline auto tinytc::make_value(scalar_type scalar_ty, location const &loc = {}) -> value#

Make value.

Parameters:
  • scalar_ty – Scalar type

  • loc – Source code location

Returns:

Value

Value Classes#

value#

class value : public tinytc::shared_handle<tinytc_value_t>#

Reference-counting wrapper for tinytc_value_t.

Public Functions

inline auto get_name() const -> char const*#

Get name.

Returns:

Name

inline void name(std::string const &name)#

Set name.

Parameters:

name – Name

inline shared_handle()#

Create empty (invalid) handle.

inline explicit shared_handle(T obj, bool needs_retain = false)#

Create handle from C handle.

inline shared_handle(shared_handle const &other)#

Copy ctor.

inline shared_handle(shared_handle &&other) noexcept#

Move ctor.