emitpy.assign_global (tt::emitpy::AssignGlobalOp)
Assign a value to a global variable
The emitpy.assign_global assigns the value to an existing named global variable.
Example:
emitpy.assign_global @value = %arg0 : !emitpy.opaque<"[ttnn.Tensor]">
Interfaces: SymbolUserOpInterface
Attributes:
| Attribute | MLIR Type | Description |
|---|---|---|
name | ::mlir::FlatSymbolRefAttr | flat symbol reference attribute |
Operands:
| Operand | Description |
|---|---|
value | any type |
emitpy.assign (tt::emitpy::AssignOp)
Assign operation
The emitpy.assign operation represents Python variable assignment.
For subscript assignment (e.g., dict[5] = value), wrap both the subscript
and assign operations inside an emitpy.expression block.
Examples:
// Simple assignment
emitpy.assign %target = %value : (!emitpy.opaque<"int">, !emitpy.opaque<"int">)
...
// Subscript assignment (wrap subscript and assign in expression op)
%dict = emitpy.create_dict "dict" {literal_expr = "{}"} : () -> !emitpy.dict
%key = emitpy.literal "5" : index
%res = emitpy.expression(%dict, %key, %value) : (!emitpy.dict, index, !emitpy.opaque<"[ttnn.Tensor]">) -> !emitpy.opaque<"None"> {
^bb0(%d: !emitpy.dict, %k: index, %v: !emitpy.opaque<"[ttnn.Tensor]">):
%sub = emitpy.subscript %d[%k] : (!emitpy.dict, index) -> !emitpy.opaque<"[ttnn.Tensor]">
emitpy.assign %sub = %v : (!emitpy.opaque<"[ttnn.Tensor]">, !emitpy.opaque<"[ttnn.Tensor]">)
%none = "emitpy.constant"() <{value = #emitpy.opaque<"None">}> : () -> !emitpy.opaque<"None">
emitpy.yield %none : !emitpy.opaque<"None">
}
# Code emitted:
target = value
...
dict[5] = value
Interfaces: MemoryEffectOpInterface
Operands:
| Operand | Description |
|---|---|
target | EmitPy opaque type or EmitPy class type or EmitPy dictionary type or EmitPy string type. |
value | EmitPy opaque type or EmitPy class type or EmitPy dictionary type or EmitPy string type. |
emitpy.call_opaque (tt::emitpy::CallOpaqueOp)
Opaque call operation
Syntax:
operation ::= `emitpy.call_opaque` $callee `(` $operands `)` attr-dict `:` functional-type($operands, results)
The emitpy.call_opaque operation represents a Python function call. The callee
can be an arbitrary non-empty string.
Example:
%2 = emitpy.call_opaque "ttnn.add"(%0, %1) {args = [0 : index, 1 : index, #emitpy.opaque<"ttnn.DataType.BFLOAT16">, #emitpy.opaque<"ttnn.MemoryConfig(ttnn.TensorMemoryLayout.INTERLEAVED, ttnn.BufferType.DRAM, None)">], keyword_args = ["", "", "dtype", "memory_config"]} : (!emitpy.opaque<"ttnn.Tensor">, !emitpy.opaque<"ttnn.Tensor">) -> !emitpy.opaque<"ttnn.Tensor">
Interfaces: PyExpressionInterface
Attributes:
| Attribute | MLIR Type | Description |
|---|---|---|
callee | ::mlir::StringAttr | string attribute |
args | ::mlir::ArrayAttr | array attribute |
keyword_args | ::mlir::ArrayAttr | string array attribute |
Operands:
| Operand | Description |
|---|---|
operands | variadic of any type |
Results:
| Result | Description |
|---|---|
| «unnamed» | variadic of any type |
emitpy.class (tt::emitpy::ClassOp)
Class definition
The emitpy.class operation represents a Python class definition.
It is a symbol and contains a single-region body with class members.
Class verification enforces:
- At most one
__init__method. __init__returns nothing and is an instance method.- Methods can be annotated with
emitpy.method_kindattribute onfunc.funcusing one of:instance,staticmethod,classmethod. - Instance and class methods must have a receiver argument as the first
argument; if named, it must be
self(instance) orcls(classmethod).
Example:
emitpy.class @Model(#emitpy.opaque<"torch.nn.Module">) {
func.func @__init__(%self: !emitpy.class<"Model">) {
%w = emitpy.call_opaque "ttnn.load_weight"() : () -> !emitpy.opaque<"ttnn.Tensor">
emitpy.set_attr %self, "weight", %w : (!emitpy.class<"Model">, !emitpy.opaque<"ttnn.Tensor">)
return
}
}
Traits: IsolatedFromAbove, NoTerminator, SingleBlock, SymbolTable
Interfaces: Symbol
Attributes:
| Attribute | MLIR Type | Description |
|---|---|---|
sym_name | ::mlir::StringAttr | string attribute |
base_classes | ::mlir::ArrayAttr | array attribute |
emitpy.constant (tt::emitpy::ConstantOp)
Constant operation
The emitpy.constant operation produces an SSA value equal to some constant
specified by an attribute. This can be used to form simple integer and
floating point constants, as well as more exotic things like tensor
constants.
Traits: ConstantLike
Attributes:
| Attribute | MLIR Type | Description |
|---|---|---|
value | ::mlir::Attribute | An opaque attribute or TypedAttr instance |
Results:
| Result | Description |
|---|---|
result | any type |
emitpy.create_dict (tt::emitpy::CreateDictOp)
Create dictionary operation
Syntax:
operation ::= `emitpy.create_dict` $dict_name (`(` $items^ `)`)? attr-dict `:` functional-type(operands, results)
Creates a Python dictionary with a specified name.
Can be created in two ways:
- From key-value pairs (items operands) - pairs are alternating key, value
- From a Python literal expression (literal_expr attribute)
When literal_expr is provided, items must be empty. When items are provided, they must be even count (key-value pairs).
Example:
// Empty dict using literal_expr
%dict1 = emitpy.create_dict "my_dict" {literal_expr = "{}"} : () -> !emitpy.dict
// Dict from key-value operands
%dict2 = emitpy.create_dict "cache" (%k0, %v0) : (index, !emitpy.opaque<"None">) -> !emitpy.dict
// Dict from literal expression (most compact for complex dicts)
%dict3 = emitpy.create_dict "_CONST_EVAL_CACHE" {literal_expr = "{i: None for i in range(133)}"} : () -> !emitpy.dict
# Code emitted for the operations above:
my_dict = {}
cache = {0: None}
_CONST_EVAL_CACHE = {i: None for i in range(133)}
Traits: AlwaysSpeculatableImplTrait
Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface)
Effects: MemoryEffects::Effect{}
Attributes:
| Attribute | MLIR Type | Description |
|---|---|---|
dict_name | ::mlir::StringAttr | string attribute |
literal_expr | ::mlir::StringAttr | string attribute |
Operands:
| Operand | Description |
|---|---|
items | variadic of any type |
Results:
| Result | Description |
|---|---|
result | EmitPy dictionary type |
emitpy.expression (tt::emitpy::ExpressionOp)
Expression operation to inline Python code
The emitpy.expression operation returns a single SSA value yielded by its
single-basic-block region. The operations inside the region must form a DAG
(directed acyclic graph) of computations that produces a single result value.
The expression's operands are mapped to block arguments and can be used
within the expression body.
When generating Python code, operations within the expression body are emitted inline without intermediate variable assignments, producing cleaner code.
The optional do_not_inline attribute can be used to force the translator to
emit the expression as separate statements rather than inline.
Example:
%expression = emitpy.expression(%a, %b) : (!emitpy.opaque, !emitpy.opaque) -> !emitpy.opaque {
^bb0(%arg0: !emitpy.opaque, %arg1: !emitpy.opaque):
%0 = emitpy.call_opaque "ttnn.mul"(%arg0, %arg1) : (!emitpy.opaque, !emitpy.opaque) -> !emitpy.opaque
%1 = emitpy.call_opaque "ttnn.add"(%0, %arg1) : (!emitpy.opaque, !emitpy.opaque) -> !emitpy.opaque
emitpy.yield %1 : !emitpy.opaque
}
return %expression : !emitpy.opaque
This would generate:
return ttnn.add(ttnn.mul(a, b), b)
If the do_not_inline attribute is set:
%expression = ttnn.add(ttnn.mul(a, b), b)
return %expression
Traits: HasOnlyGraphRegion, IsolatedFromAbove, SingleBlockImplicitTerminator<YieldOp>, SingleBlock
Attributes:
| Attribute | MLIR Type | Description |
|---|---|---|
do_not_inline | ::mlir::UnitAttr | unit attribute |
Operands:
| Operand | Description |
|---|---|
operands | variadic of any type |
Results:
| Result | Description |
|---|---|
result | any type |
emitpy.file (tt::emitpy::FileOp)
A file container operation
Syntax:
operation ::= `emitpy.file` $id attr-dict-with-keyword $bodyRegion
A file represents a single Python file.
If -emitpy-file-id=id flag is used with ttmlir-translate,
then only the matching emitpy.file op is emitted.
Example:
emitpy.file "main" {
emitpy.func @func_one() {
emitpy.return
}
}
Traits: IsolatedFromAbove, NoRegionArguments, NoTerminator, SymbolTable
Attributes:
| Attribute | MLIR Type | Description |
|---|---|---|
id | ::mlir::StringAttr | An Attribute containing a string{{% markdown %}} Syntax:
{{% /markdown %}} |
emitpy.get_attr (tt::emitpy::GetAttrOp)
Get attribute from an object
Syntax:
operation ::= `emitpy.get_attr` $object `,` $attr_name attr-dict `:` functional-type(operands, results)
The emitpy.get_attr operation represents Python attribute access
via the dot operator.
Example:
%w = emitpy.get_attr %self, "weight" : (!emitpy.class<"Model">) -> !emitpy.opaque<"ttnn.Tensor">
# Code emitted:
w = self.weight
Traits: AlwaysSpeculatableImplTrait
Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface), PyExpressionInterface
Effects: MemoryEffects::Effect{}
Attributes:
| Attribute | MLIR Type | Description |
|---|---|---|
attr_name | ::mlir::StringAttr | string attribute |
Operands:
| Operand | Description |
|---|---|
object | any type |
Results:
| Result | Description |
|---|---|
result | any type |
emitpy.global (tt::emitpy::GlobalOp)
A global variable
Syntax:
operation ::= `emitpy.global` $sym_name custom<EmitPyGlobalOpInitialValue>($initial_value) attr-dict
The emitpy.global operation defines a named global variable.
Example:
emitpy.global @x : #emitpy.opaque<"[]">
Interfaces: Symbol
Attributes:
| Attribute | MLIR Type | Description |
|---|---|---|
sym_name | ::mlir::StringAttr | string attribute |
initial_value | ::mlir::Attribute | An opaque attribute or TypedAttr instance |
emitpy.global_statement (tt::emitpy::GlobalStatementOp)
Marks a variable as global for the current function scope
Syntax:
operation ::= `emitpy.global_statement` $name `:` type($result) attr-dict
The emitpy.global_statement operation represents a Python global
statement.
It marks an identifier, specified by its symbol @name, as
referring to a variable in the global (module) scope, rather
than a new local variable.
Example:
%0 = emitpy.global_statement @x : !emitpy.opaque<"[ttnn.Tensor]">
Traits: AlwaysSpeculatableImplTrait
Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface), SymbolUserOpInterface
Effects: MemoryEffects::Effect{}
Attributes:
| Attribute | MLIR Type | Description |
|---|---|---|
name | ::mlir::FlatSymbolRefAttr | flat symbol reference attribute |
Results:
| Result | Description |
|---|---|
result | any type |
emitpy.if (tt::emitpy::IfOp)
If statement with optional else
Syntax:
operation ::= `emitpy.if` $condition (`args` $cond_args^ `:` `(` type($cond_args) `)`)? $thenRegion
(`else` $elseRegion^)? attr-dict
The emitpy.if operation represents a Python if statement with an
optional else clause. The condition is a format string with {}
placeholders that are replaced by the condition arguments.
An elif chain is represented by nesting an emitpy.if as the sole
operation inside the else region without its own else clause. The Python
emitter detects this pattern and emits elif instead of else: if.
Example:
emitpy.if "not {}" args %dict : (!emitpy.dict) {
// then body
}
emitpy.if "{} > {}" args %a, %b : (index, index) {
// then body
} else {
// else body
}
emitpy.if "{}" args %a : (!emitpy.opaque<"dict">) {
// then body
} else {
emitpy.if "{} > 0" args %b : (!emitpy.opaque<"int">) {
// elif body
}
}
# Code emitted:
if not dict:
# then body
if a > b:
# then body
else:
# else body
if a:
# then body
elif b > 0:
# elif body
Traits: NoTerminator
Attributes:
| Attribute | MLIR Type | Description |
|---|---|---|
condition | ::mlir::StringAttr | string attribute |
Operands:
| Operand | Description |
|---|---|
cond_args | variadic of any type |
emitpy.import (tt::emitpy::ImportOp)
Import operation
The emitpy.import operation allows to define a Python module import
via various forms of the import statement.
Example:
emitpy.import import "ttnn"
Attributes:
| Attribute | MLIR Type | Description |
|---|---|---|
module_name | ::mlir::StringAttr | string attribute |
module_alias | ::mlir::StringAttr | string attribute |
members_to_import | ::mlir::ArrayAttr | string array attribute |
member_aliases | ::mlir::ArrayAttr | string array attribute |
import_all | ::mlir::UnitAttr | unit attribute |
emitpy.literal (tt::emitpy::LiteralOp)
Literal operation
Syntax:
operation ::= `emitpy.literal` $value attr-dict `:` type($result)
The emitpy.literal operation produces an SSA value equal to some constant
specified by an attribute.
Example:
%0 = emitpy.literal "0" : index
Traits: AlwaysSpeculatableImplTrait
Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface), PyExpressionInterface
Effects: MemoryEffects::Effect{}
Attributes:
| Attribute | MLIR Type | Description |
|---|---|---|
value | ::mlir::StringAttr | string attribute |
Results:
| Result | Description |
|---|---|
result | index |
emitpy.set_attr (tt::emitpy::SetAttrOp)
Set attribute on an object
Syntax:
operation ::= `emitpy.set_attr` $object `,` $attr_name `,` $value attr-dict `:` `(` type($object) `,` type($value) `)`
The emitpy.set_attr operation represents Python attribute assignment
via the dot operator.
Example:
emitpy.set_attr %self, "weight", %w : (!emitpy.opaque<"Model">, !emitpy.opaque<"ttnn.Tensor">)
# Code emitted:
self.weight = w
Attributes:
| Attribute | MLIR Type | Description |
|---|---|---|
attr_name | ::mlir::StringAttr | string attribute |
Operands:
| Operand | Description |
|---|---|
object | any type |
value | any type |
emitpy.subscript (tt::emitpy::SubscriptOp)
Subscript operation
Syntax:
operation ::= `emitpy.subscript` $container `[` $index `]` attr-dict `:` functional-type(operands, results)
The emitpy.subscript operation represents Python subscript access.
This is the read counterpart to emitpy.assign.
Supports reading from any subscriptable object (e.g. arrays, dicts) with integer or string keys.
Example:
%0 = emitpy.literal "0" : index
%1 = emitpy.subscript %arg0[%0] : (!emitpy.opaque<"[ttnn.Tensor]">, index) -> !emitpy.opaque<"ttnn.Tensor">
...
%dict = emitpy.global_statement @_CONST_EVAL_CACHE : !emitpy.dict
%key = emitpy.literal "5" : index
%tensors = emitpy.subscript %dict[%key] : (!emitpy.dict, index) -> !emitpy.opaque<"[ttnn.Tensor]">
# Code emitted:
var_0 = subscript_arg[0]
...
tensors = _CONST_EVAL_CACHE[5]
Traits: AlwaysSpeculatableImplTrait
Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface), PyExpressionInterface
Effects: MemoryEffects::Effect{}
Operands:
| Operand | Description |
|---|---|
container | EmitPy opaque type or EmitPy dictionary type |
index | index or EmitPy string type. |
Results:
| Result | Description |
|---|---|
result | EmitPy opaque type or EmitPy class type or EmitPy dictionary type or EmitPy string type. |
emitpy.verbatim (tt::emitpy::VerbatimOp)
Verbatim operation
Syntax:
operation ::= `emitpy.verbatim` $value (`args` $fmtArgs^ `:` type($fmtArgs))? attr-dict
The emitpy.verbatim operation produces no results and the value is emitted as is
followed by a line break ('\n' character) during translation.
This operation can be used in situations where a more suitable operation is not yet implemented in the dialect.
Note: Use with caution. This operation can have arbitrary effects on the semantics of the emitted code. Use semantically more meaningful operations whenever possible. Additionally this op is NOT intended to be used to inject large snippets of code.
Attributes:
| Attribute | MLIR Type | Description |
|---|---|---|
value | ::mlir::StringAttr | string attribute |
Operands:
| Operand | Description |
|---|---|
fmtArgs | variadic of any type |
emitpy.yield (tt::emitpy::YieldOp)
Yield operation for expression termination
Syntax:
operation ::= `emitpy.yield` $result attr-dict `:` type($result)
The emitpy.yield terminates its parent EmitPy operation's region, optionally yielding
an SSA value. The semantics of how the values are yielded is defined by the
parent operation.
Example:
%0 = emitpy.expression(%arg0, %arg1) : (!emitpy.opaque, !emitpy.opaque) -> !emitpy.opaque {
^bb0(%a: !emitpy.opaque, %b: !emitpy.opaque):
%1 = emitpy.call_opaque "foo"(%a, %b) : (!emitpy.opaque, !emitpy.opaque) -> !emitpy.opaque
emitpy.yield %1 : !emitpy.opaque
}
Traits: AlwaysSpeculatableImplTrait, HasParent<ExpressionOp>, ReturnLike, Terminator
Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface), RegionBranchTerminatorOpInterface
Effects: MemoryEffects::Effect{}
Operands:
| Operand | Description |
|---|---|
result | any type |