For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).
Mojo function
inlined_assembly
def inlined_assembly[asm: StringSpan[ImmStaticOrigin], result_type: TrivialRegisterPassable, *types: AnyType, *, constraints: StringSpan[ImmStaticOrigin], has_side_effect: Bool = True](*args: *types.values) -> result_type
Generates inline assembly code with the given constraints and arguments.
This function allows embedding raw assembly instructions directly into Mojo code, providing fine-grained control over hardware operations. It uses LLVM-style inline assembly syntax and constraint strings.
The assembly string uses $0, $1, etc. to reference operands. Output
operands (including the return value) are numbered first, followed by input
operands.
Example:
from std.sys import inlined_assembly
# Convert bfloat16 to float32 on NVIDIA GPU using PTX assembly.
# "$0" is the output (float32), "$1" is the input (int16 bitcast of bf16).
var my_bf16_as_int16 = Int16(0x3F80) # Example bf16 bit pattern
var result = inlined_assembly[
"cvt.f32.bf16 $0, $1;",
Float32,
constraints="=f,h",
has_side_effect=False,
](my_bf16_as_int16)
# Execute a no-op sleep instruction on AMD GPU (no return value).
inlined_assembly[
"s_sleep 0",
NoneType,
constraints="",
has_side_effect=True,
]()Parameters:
- asm (
StringSpan[ImmStaticOrigin]): The assembly instruction string. Use$0,$1, etc. to reference operands, where$0is the output (if any) and subsequent numbers are inputs in order. - result_type (
TrivialRegisterPassable): The return type of the assembly operation. UseNoneTypefor assembly that produces no result. - *types (
AnyType): The types of the input arguments. - constraints (
StringSpan[ImmStaticOrigin]): LLVM-style constraint string specifying register allocation and operand placement. The output constraint comes first (prefixed with=), followed by input constraints separated by commas. Available constraints are target-specific; refer to LLVM's inline assembly documentation and your target's backend for valid options. - has_side_effect (
Bool): IfTrue(default), the assembly is treated as having side effects and won't be optimized away or reordered. Set toFalsefor pure computations to enable compiler optimizations.
Args:
- *args (
*types.values): The input arguments to pass to the assembly instruction.
Returns:
result_type: The result of the assembly operation, or None if result_type is
NoneType.