Optimization in Contiguous Unaligned Scenarios

Optimization Suggestions for Contiguous Unaligned Data Movement

[Positive Example]

1
2
3
4
5
6
7
8
// Initialization for unaligned data move-in is moved out of the for loop. uint32_t is used to manage the offset.
AscendC::Reg::LoadUnAlignPre(ureg0, srcAddr);
for (uint16_t i = 0; i < repeatTimes; ++i) {
     AscendC::Reg::LoadUnAlign(srcReg, ureg0, srcAddr + i * postUpdateStride);
     AscendC::Reg::StoreUnAlign(dstAddr, srcReg, ureg1, postUpdateStride);
}
// Post-processing for unaligned data move-out is moved out of the for loop.
AscendC::Reg::StoreUnAlignPost(dstAddr, ureg1, 0);

[Negative Example]

1
2
3
4
5
6
for (uint16_t i = 0; i < repeatTimes; ++i) {
     AscendC::Reg::LoadUnAlignPre(ureg0, srcAddr + i * postUpdateStride);
     AscendC::Reg::LoadUnAlign(srcReg, ureg0, srcAddr + i * postUpdateStride);
     AscendC::Reg::StoreUnAlign(dstAddr, srcReg, ureg1, postUpdateStride);
     AscendC::Reg::StoreUnAlignPost(dstAddr, ureg1, 0);
}

Introduction to Contiguous Unaligned Data Movement Interfaces

To improve the capability of processing irregular memory addresses, Reg vector computation supports access to non-32-byte-aligned addresses during data movement. This is applicable to scenarios with unaligned data movement from Unified Buffer to RegTensor or from RegTensor to Unified Buffer. To reduce the performance overhead caused by unaligned access, RegBase introduces an unaligned register-based cache mechanism. This mechanism uses the unaligned registers UnalignRegForLoad and UnalignRegForStore as temporary buffers to temporarily store data across alignment boundaries, thereby achieving efficient contiguous unaligned data transmission.

Before reading an unaligned address, perform initialization through LoadUnAlignPre before using LoadUnAlign. When writing an unaligned address, use StoreUnAlign first and then StoreUnAlignPost for post-processing. The move-in and move-out interfaces with offsets being managed using uint32_t and the maskReg data move-out interfaces are as follows:

Principles of Unaligned Registers

This part describes how unaligned registers work during unaligned data movement and explains the internal principles in detail to help developers understand why the preceding performance optimization suggestions can bring performance benefits. This part first introduces how contiguous unaligned data move-in, contiguous unaligned data move-out, and contiguous unaligned data move-out for MaskReg are implemented, and then uses an example to describe these key processes work together.