LowerToAsc passes

-asclower-arith

Lower arith dialect constant, bitcast, and negf operations to ascendc equivalents

Converts selected arithmetic operations on tensor types to Ascend C equivalents:

  • Splat constants: Creates LocalTensorAutoOp + DuplicateL2Op to broadcast scalar to all elements

  • Dense non-splat constants: Creates tensor + series of LocalTensorSetValueOp for each element

  • Bitcast: Converts to LocalTensorReinterpretCastOp for type reinterpretation

  • Negf: Converts to MulsL2Op with -1.0 scalar to negate floating-point tiles

asctile::LocalTensorType is converted to ascendc::LocalTensorType via unrealized conversion casts.

-asclower-arith-binary

Lower binary arith dialect operations on tiles to ascendc L2 vector operations

Converts binary arithmetic operations on tensor types to Ascend C L2-level vector operations. Supports floating-point and integer arithmetic (add, sub, mul, div, max, min variants). For bitwise operations (andi, ori), performs reinterpret casting to i8 elements if needed since the hardware processes bitwise ops on 8-bit granularity.

Each operation creates a destination tensor allocation and the corresponding L2 compute op with calCount.

-asclower-asctile

Lower core asctile dialect operations to ascendc compute and neural network operations

Converts AscTile tensor operations to Ascend C equivalents:

  • TensorOp: Creates GlobalTensorOp with buffer pointer setup

  • SplatOp: Creates DuplicateL2Op to broadcast scalar

  • ReluOp, CastOp: Creates corresponding L2 unary ops

  • MatmulOp: Creates MmadOp with M/N/K params derived from tensor shapes

  • ReshapeOp: Creates LocalTensorReinterpretCastOp

  • BroadcastOp: Creates DuplicateL2Op for 1-element tiles or BroadcastOp for larger tiles

  • SoftmaxOp/RmsNormOp: Creates library ops with tiling/shape info params

  • ReduceAs1dOp/ReduceOp: Creates reduction ops with pattern-based shape decomposition

  • Scalar-tensor ops (adds, subs, muls, divs, mins, maxs, shls, shrs): Creates L2 scalar variants

  • AccumulatorOp/MatmulAccOp: Sets up L0C accumulator for matrix multiplication accumulation

-asclower-asctile-data-transfer

Lower asctile data transfer operations (load, store, copy) to ascendc memory operations

Converts AscTile memory transfer operations to Ascend C equivalents:

  • LoadOp (UB destination): Creates DataCopyPadExtL0Op from GM to UB with optional padding

  • LoadOp (L1 destination): Creates DataCopyL2Op with Nd2Nz params for cube matrix format

  • StoreOp (from UB): Creates DataCopyPadExtL2Op to GM

  • StoreFixpipeOp (from L0C): Creates FixpipeOp with optional relu/quantize flags

  • CopyFixpipeOp (L0C to L1): Creates FixpipeOp with NZ layout for L0C-to-L1 transfer

  • CopyOp (L1 to L0A/L0B): Creates LoadDataG2LOp or LoadDataWithTransposeOp with fractal layout

  • GetValueOp/SetValueOp: Creates global tensor scalar get/set operations

Handles both static and dynamic tensor shapes, computing linear offsets from multi-dimensional indices. Quantization modes (F322F16, DEQF16, QF322B8_PRE, REQ8) are inferred from source/destination type pairs.

-asclower-asctile-i1

Lower asctile comparison and select operations with i1 tiles to ascendc L0 operations

Converts i1 (boolean) tensor operations to Ascend C L0-level operations with packed i8 representation:

  • CmpOp: Creates CompareL0Op with packed i8 destination (8 boolean values per i8 byte)

    • For integer comparisons on non-C310 targets, casts operands to float first

  • CmpSOp: Creates CompareScalarL0Op for tensor-scalar comparisons

  • SelectOp: Creates SelectL0Op with VSEL_TENSOR_TENSOR mode

Boolean tiles (i1 elements) are represented as packed i8 tensors (8 bits per byte), enabling hardware support for 8-way parallel boolean operations. The destination shape is ceil(numElements/8).

-asclower-atomic

Lower atomic read-modify-write operations to ascendc atomic store sequences

Converts asctile.atomic_rmw operations to atomic store sequences:

  1. Sets atomic mode via SetAtomicAddOp, SetAtomicMaxOp, or SetAtomicMinOp

  2. Converts atomic_rmw to asctile.store (which is later lowered by LowerAscTileDataTransfer)

  3. Resets atomic mode via SetAtomicNoneOp

Supported atomic kinds: Add (accumulator), Max (maximum), Min (minimum). The atomic mode affects how subsequent stores modify existing memory values.

-asclower-displace-concat

Transform concat operations by rewriting related tensor allocations to eliminate actual data copies

Rewrites asctile.concat operations to avoid unnecessary data copies by adjusting tensor allocations:

  1. Optimization phase: If all concat operands are from LocalTensorAutoOp with same position, creates a single combined tensor at function entry and rewrites each operand’s allocation as a sub-index view into this combined tensor. The concat itself becomes a no-op (just an unrealized conversion cast).

  2. Fallback phase: If operands cannot be optimized, creates a destination tensor and inserts DataCopyL2Op operations to copy each operand into its corresponding sub-index region.

This pass eliminates redundant UB allocations and copies when concatenating temporaries, reducing memory usage.

-asclower-expand-mask

Expand mask regions by moving nested operations outside and updating mask parameters

Transforms asctile.count_mask and asctile.bitwise_mask regions by:

  1. Moving all operations from inside the mask region to outside (before the mask op)

  2. Lowering L2 operations inside the mask to L0 variants via populateLowerToL0Patterns

  3. Updating each L0 operation’s mask parameter to use the mask region’s count or bitwise mask

  4. If the mask has other value, inserting DuplicateL2Op to fill the destination with that value

After expansion, the mask region op is erased and all operations execute with explicit mask parameters.

-asclower-expand-math

Expand math operations that lack direct hardware support into equivalent sequences

Applies MLIR’s standard math expansion patterns to decompose operations without direct Ascend C support:

  • rsqrt(x)1.0 / sqrt(x)

  • exp2(x)exp(x * ln(2))

These expanded forms use math operations that have direct Ascend C library support.

-asclower-math

Lower math dialect unary operations on tiles to ascendc library and L2 operations

Converts math dialect unary operations on tensor types to Ascend C equivalents. Library operations (log, erf, trigonometric functions, ceil, floor, round) use Ascend C’s high-precision library calls with optional shared temporary buffer. Direct L2 operations (abs, exp, sqrt, rsqrt) map to hardware-accelerated vector instructions.

Each operation creates a destination tensor allocation and the corresponding compute op with calCount.

-asclower-realize-conversion-cast

Convert unrealized conversion casts between tensor types into ascendc reinterpret cast operations

Simplifies and materializes UnrealizedConversionCastOp chains between tensor types:

  1. FoldCastChain: Eliminates redundant cast chains where source and destination types match (e.g., cast(cast(x → y) → x) → x, or three-level chains)

  2. RewriteReinterpretCast: Converts nested casts between compatible tensor types to LocalTensorReinterpretCastOp when memory size is identical

Compatible tensors have same total memory size (numElements × bitWidth), enabling type reinterpretation without data movement. This pass cleans up conversion artifacts from the dialect conversion process.

-asclower-redress-i1-tensor

Convert i1 tensor types to packed i8 representation and adjust constants accordingly

Rewrites i1 (boolean) tensor types to use packed i8 representation before lowering:

  • LocalTensorType conversion: tensor<Nxi1>tensor<ceil(N/8)xi8> (8 boolean values packed per byte)

  • Splat constants: dense<true>dense<-1> (all bits set), dense<false>dense<0>

  • Dense constants: Bit-packed representation where bit index corresponds to original element index

This preprocessing enables hardware support for boolean operations, which operate on 8-bit granularity. After redressing, LowerAscTileI1 can convert comparison/select operations to packed L0 variants.

-asclower-scf

Convert tensor type operands and results in scf control flow operations to local tensor types

Uses MLIR’s populateSCFStructuralTypeConversionsAndLegality to convert scf operations with tensor types:

  • scf.if: Result types converted from asctile::LocalTensorType to ascendc::LocalTensorType

  • scf.for: Iter argument types converted, loop body operations wrapped with conversion casts

  • scf.while: Before/after region argument types converted

This enables scf control flow to work with converted tensor types after AscTile lowering. Uses TensorTypeConverter to map asctile::LocalTensorTypeascendc::LocalTensorType.