heterocl.tvm.ir_builder module

Developer API of IR node builder make function.

class BufferVar(builder, buffer_var, content_type)[source]

Bases: heterocl.tvm._ffi.node_generic.NodeGeneric

Buffer variable with content type, makes load store easily.

Do not create it directly, create use IRBuilder.

Examples

In the follow example, x is BufferVar. x[0] = ... directly emit a store to the IRBuilder, x[10] translates to Load.

# The following code generate IR for x[0] = x[
ib = tvm.ir_builder.create()
x = ib.pointer("float32")
x[0] = x[10] + 1
asnode()[source]

Convert value to node

dtype
class IRBuilder[source]

Bases: object

Auxiliary builder to build IR for testing and dev.

Examples

ib = tvm.ir_builder.create()
n = tvm.var("n")
A = ib.allocate("float32", n, name="A")
with ib.for_range(0, n, name="i") as i:
    with ib.if_scope((i % 2) == 0):
        A[i] = A[i] + 1
# The result stmt.
stmt = ib.get()
allocate(dtype, shape, name='buf', scope=None)[source]

Create a allocate statement.

Parameters
  • dtype (str) – The content data type.

  • shape (tuple of Expr) – The shape of array to be allocated.

  • name (str, optional) – The name of the buffer.

  • scope (str, optional) – The scope of the buffer.

Returns

buffer – The buffer var representing the buffer.

Return type

BufferVar

buffer_ptr(buf)[source]

Create pointer variable corresponds to buffer ptr.

Parameters

buf (Buffer) – The buffer to be extracted.

Returns

ptr – The buffer var representing the buffer.

Return type

BufferVar

else_scope()[source]

Create an else scope.

This can only be used right after an if scope.

Returns

else_scope – The result else scope.

Return type

WithScope

Examples

ib = tvm.ir_builder.create()
i = tvm.var("i")
x = ib.pointer("float32")
with ib.if_scope((i % 2) == 0):
    x[i] = x[i - 1] + 1
with ib.else_scope():
    x[i] = x[i - 1] + 2
emit(stmt)[source]

Emit a statement to the end of current scope.

Parameters

stmt (Stmt or callable.) – The statement to be emitted or callable that build stmt given body.

for_range(begin, end, name='i', dtype='int32', for_type='serial')[source]

Create a for iteration scope.

Parameters
  • begin (Expr) – The min iteration scope.

  • end (Expr) – The end iteration scope

  • name (str, optional) – The name of iteration variable, if no input names, using typical index names i, j, k, then i_nidx

  • dtype (str, optional) – The data type of iteration variable.

  • for_type (str, optional) – The special tag on the for loop.

Returns

loop_scope – The for scope, when enters returns loop_var

Return type

With.Scope of Var

Examples

ib = tvm.ir_builder.create()
x = ib.pointer("float32")
with ib.for_range(1, 10, name="i") as i:
    x[i] = x[i - 1] + 1
get()[source]

Return the builded IR.

Returns

stmt – The result statement.

Return type

Stmt

if_scope(cond)[source]

Create an if scope.

Parameters

cond (Expr) – The condition.

Returns

if_scope – The result if scope.

Return type

WithScope

Examples

ib = tvm.ir_builder.create()
i = tvm.var("i")
x = ib.pointer("float32")
with ib.if_scope((i % 2) == 0):
    x[i] = x[i - 1] + 1
likely(expr)[source]

Add likely tag for expression. :param expr: The expression. Usually a condition expression. :type expr: Expr

Returns

expr – The expression will likely tag.

Return type

Expr

new_scope()[source]

Create new scope,

this is useful to set boundary of attr and allocate.

Returns

new_scope – The result new scope.

Return type

WithScope

pointer(content_type, name='ptr')[source]

Create pointer variable with content type.

Parameters
  • content_type (str) – The content data type.

  • name (str, optional) – The name of the pointer.

Returns

ptr – The buffer var representing the buffer.

Return type

BufferVar

scope_attr(node, attr_key, value)[source]

Create an AttrStmt at current scope.

Parameters
  • attr_key (str) – The key of the attribute type.

  • node (Node) – The attribute node to annottate on.

  • value (Expr) – Attribute value.

Examples

ib = tvm.ir_builder.create()
i = tvm.var("i")
x = ib.pointer("float32")
ib.scope_attr(x, "storage_scope", "global")
x[i] = x[i - 1] + 1
class WithScope(enter_value, exit_cb)[source]

Bases: object

Auxiliary scope with

create()[source]

Create a new IRBuilder

Returns

builder – The created IRBuilder

Return type

IRBuilder