﻿# Triton inline assembly: purity and packed operand constraints are compiler contracts

Inspect Triton inline assembly side-effect declarations, packed element groups, output types, and LLVM constraints before integrating a target-specific instruction.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-triton-inline-asm-purity-pack-contract/
Language: en

## Treat assembly as an interface

The [inline_asm_elementwise API](https://triton-lang.org/main/python-api/generated/triton.language.inline_asm_elementwise.html) maps target assembly across tensor inputs. The compiler still needs a precise interface: argument constraints, returned types, element packing, and side-effect behavior. Embedding an instruction string does not remove those obligations. A custom kernel can contain syntactically valid assembly while communicating an incorrect contract to optimization or mapping the wrong logical values into its operands.

## Declare actual side effects

The API's `is_pure` field tells the compiler whether the assembly has side effects. This should describe behavior, not a preference for more optimization. If code performs effects not represented by returned tensors, declaring it pure can allow transformations inconsistent with the intended operation. The [LLVM reference](https://llvm.org/docs/LangRef.html#inline-assembler-expressions) explains the underlying inline-assembly interface and constraints. Review the generated operation against those rules rather than assuming the compiler infers all memory effects from the text of an arbitrary assembly fragment.

## Understand packing without inventing adjacency

The Triton documentation states that each invocation processes `pack` elements, while the particular set of inputs grouped into an invocation is unspecified. Smaller elements can be packed into registers. Therefore, a fragment should not depend on an undocumented assumption that its grouped logical elements form consecutive global addresses. Use packing to express the supported elementwise instruction interface, and keep any cross-element dependency within the documented mapping guarantees of the chosen implementation.

## Match constraints and output interpretation

The [language implementation](https://raw.githubusercontent.com/triton-lang/triton/main/python/triton/language/core.py) passes assembly, constraints, operand handles, purity, packing, and result types into the generated operation. A mismatch in result arity or register interpretation is not repaired by matching the outer tensor shape. Test a fragment with varied input bit patterns and compare every returned component against a reference. Include cases that expose packing and type-conversion mistakes instead of relying on uniform inputs that collapse distinct operands to the same observed value.

## Validate before performance attribution

Record target backend, instruction, constraints, returned types, packing policy, and side-effect declaration. Guides to [tensor axes](/journal/tensor-axes-batch-sequence-hidden/) and [GPU acceptance evidence](/journal/gpu-acceptance-evidence/) provide context. Inspect compiler output and test observable results before benchmarking. The useful claim is that a specific fragment preserves a verified interface on a specified target, because valid assembly syntax alone does not establish semantic correctness or portable acceleration across GPU architectures.
