Skip to content

LATX: optimize AVX and IR2 instruction generation - #438

Open
luzeng87 wants to merge 17 commits into
lat-opensource:masterfrom
luzeng87:gb603-avx-patterns
Open

LATX: optimize AVX and IR2 instruction generation#438
luzeng87 wants to merge 17 commits into
lat-opensource:masterfrom
luzeng87:gb603-avx-patterns

Conversation

@luzeng87

@luzeng87 luzeng87 commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Summary

  • defer redundant VEX.128 upper-half clears while materializing them before any observation or TB exit
  • restore full-width YMM writeback for mask and blend instructions and preserve source/destination alias cases
  • keep mixed XMM/YMM blocks out of SHBR when the optimization cannot preserve the full vector state
  • reduce temporary vectors in conversions, narrowing, multiply-add, shuffle, shift, extract, insert, broadcast, and blend lowering
  • combine guarded scalar/AVX sequence rewrites with explicit register-use and flag checks
  • fold exact integer bit-normalization, unsigned-byte clamp, conditional-select, and YMM horizontal-reduction sequences
  • remove redundant LASX immediate loads and fold safe JIT RIP-relative displacements into loads and stores

Correctness coverage

  • YMM mask/blend destination writeback, including dest == src1 and dest == src2
  • VINSERTPS dest == src1 == src2
  • VEX.128 YMM-high zeroing across normal execution, signals, JIT, cold AOT, and hot AOT
  • integer folds reject unsafe register aliases and require exact immediates
  • conversion aliases, all MXCSR rounding modes, NaN, overflow, signed saturation, and deterministic multiply inputs
  • shuffle lane selection, scalar FMA data flow, VCOMIS ordered/unordered flags, and RIP-relative reads and writes

Validation

This PR consolidates and supersedes #437, #439, #442, #445, #446, #448, #449, #450, #451, #452, #453, #454, #456, and #457. It remains a draft pending re-review of the current head.

@xiezyang xiezyang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VINSERTPS 快路径未处理 dest == src1 == src2 的别名情况。
例如 vinsertps xmm4, xmm4, xmm4, 0x9c 会在读取 src1 前覆盖 dest,导致原始 lane 0 丢失。
建议该快路径排除 dest_is_src1,或先保存 src1 后再写目标寄存器。

@xiezyang xiezyang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本 PR 包含的 b245da0 存在 3 处 YMM 结果未写回问题:

  • VMASKMOVPS/PD
  • VPBLENDVB
  • VPBLENDW(立即数不是 0 或 0xff 时)
    这些路径均将结果计算到 temp,但只在 XMM 形式下写回 dest。因此 YMM 目标寄存器低 128 位保留旧值,高 128 位被错误清零。
    建议无条件将 temp 写回 dest,并且仅在 XMM 目标时调用 set_high128_xreg_to_zero(dest)。

@luzeng87

luzeng87 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

This stack inherits the reproducible mixed XMM/YMM failure from #437 and also has the VINSERTPS destination/source alias issue identified in review. The independent changes need to be submitted and verified by function, without this shared stack. Closing this PR.

@luzeng87 luzeng87 closed this Sep 3, 2026
@luzeng87 luzeng87 reopened this Sep 3, 2026
@luzeng87
luzeng87 marked this pull request as draft September 3, 2026 03:10
@luzeng87

luzeng87 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

Synced the YMM mask/blend writeback correction as commit dccd7ee83d. This branch had embedded the earlier deferred-zeroing commit, so it required the same full-width destination fix and regression test. The cumulative branch containing this patch passed the focused JIT/cold-AOT/hot-AOT test and lat-pr-fast 24/24 on LoongArch 3A6000. This PR remains a draft.

@luzeng87 luzeng87 changed the title LATX: optimize AVX scalar physics patterns LATX: optimize AVX and scalar instruction sequences Sep 3, 2026
@luzeng87

luzeng87 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

Reorganized this branch on top of the current #437 head. All sequence changes are now represented by one squashed commit, e4403b032b; the former cumulative audio and pixel-update PRs are superseded. The VINSERTPS dest == src1 == src2 review case is covered and passes in JIT, cold AOT, and hot AOT modes. Test names now describe instructions and transformations only.

Track VEX.128 destinations whose architectural YMM high halves are known to be zero, and materialize those clears only when a 256-bit operation can observe them or before leaving the TB. This removes repeated LASX clear instructions while preserving signal, JIT, TU, and AOT-visible state.

Add a standalone JIT, cold-AOT, and hot-AOT semantic test for the deferred state.

Signed-off-by: Lu Zeng <luzeng87@gmail.com>
Signed-off-by: Lu Zeng <luzeng87@gmail.com>
Signed-off-by: Lu Zeng <luzeng87@gmail.com>
Signed-off-by: Lu Zeng <luzeng87@gmail.com>
Signed-off-by: Lu Zeng <luzeng87@gmail.com>
Combine independently tested AVX lowering, scalar FMA forwarding, VCOMIS flag handling, pixel updates, and VROUNDPS truncation. Preserve aliased VINSERTPS source values and use instruction-based test names.

Signed-off-by: Lu Zeng <luzeng87@gmail.com>
Signed-off-by: Lu Zeng <luzeng87@gmail.com>
Signed-off-by: Lu Zeng <luzeng87@gmail.com>
Signed-off-by: Lu Zeng <luzeng87@gmail.com>
Signed-off-by: Lu Zeng <luzeng87@gmail.com>
Signed-off-by: Lu Zeng <luzeng87@gmail.com>
Signed-off-by: Lu Zeng <luzeng87@gmail.com>
Signed-off-by: Lu Zeng <luzeng87@gmail.com>
Signed-off-by: Lu Zeng <luzeng87@gmail.com>
Signed-off-by: Lu Zeng <luzeng87@gmail.com>
@luzeng87 luzeng87 changed the title LATX: optimize AVX and scalar instruction sequences LATX: optimize AVX and IR2 instruction generation Sep 3, 2026
@luzeng87

luzeng87 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

Rebased and consolidated on current master. The reviewed YMM writeback and VINSERTPS alias defects are fixed and retained. New focused coverage includes YMM mask/blend aliases, VINSERTPS dest==src1==src2, VEX.128 signal-state preservation, conversion rounding/saturation, and JIT/cold-AOT/hot-AOT execution. A clean 3A6000-25G integrated build passed lat-pr-fast 25/25 and all 21 instruction fixtures in JIT and AOT modes. Please re-review the updated draft head.

Signed-off-by: Lu Zeng <luzeng87@gmail.com>
Signed-off-by: Lu Zeng <luzeng87@gmail.com>
@luzeng87

luzeng87 commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

Added commit b382b0fa19 to restore four exact, generally applicable lowering patterns that had been omitted during PR regrouping. The two hottest affected scalar blocks now return from 45 to 36 and from 29 to 22 generated host instructions. Focused integer/YMM fixtures pass on native x86, JIT, cold AOT, and hot AOT; the integrated 3A6000-25G lat-pr-fast run passes 25/25. The draft now records every superseded PR absorbed by this branch.

@luzeng87
luzeng87 marked this pull request as ready for review September 4, 2026 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants