asc2.inline
- asc2.inline(code: str, args: Tuple[Any] | None = None, before_function: bool = False) None
Inject raw Ascend C++ code into the generated kernel.
The provided code string is emitted verbatim into the output Ascend C++ source at the current insertion point. Optional arguments can be passed to be substituted into the emitted code as IR values.
- Parameters:
code – The raw C++ code string to inject into the generated kernel source. Use
$0,$1, etc. as placeholders to reference arguments from theargslist by index.args – Optional list of values to pass as arguments to the emitted code. Each value is materialized as an IR value and forwarded to the underlying verbatim operation. In the code string, use
$<index>(e.g.,$0,$1) to reference these arguments by their position in the list.before_function – If
True, emit the code before the current function body instead of at the current insertion point. Default isFalse.
- Returns:
None
Examples
Inject constant declarations into the kernel:
asc.inline(""" constexpr int32_t TOTAL_ROWS = 668; constexpr int32_t ELEMENTS_PER_ROW = 32; """)
Pass kernel arguments to inline code using
$<index>placeholders:@asc2.jit def kernel(x_ptr: asc2.GlobalAddress, y_ptr: asc2.GlobalAddress, size: int): asc.inline(""" auto input_ptr = $0; auto output_ptr = $1; int64_t length = $2; AscendC::GlobalTensor<float> x_gm; x_gm.SetGlobalBuffer(input_ptr); """, [x_ptr, y_ptr, size])