From 08b398bbd195a6c73441f56f9656e4b2fdd68ae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 15 Oct 2026 15:17:50 +0200 Subject: [PATCH] LoongArch64 IR JIT: Vec2 packs with LSX, and join two mapped pairs into a quad With LSX, the Vec2 pack and unpack ops went to the interpreter, as they addressed the lanes of a pair one by one. They're now an interleave or a pick of halfwords in the pair's register. That makes pairs, so mapping a quad whose halves are mapped pairs (two vs2i.s feeding a vi2f.q) joins them with VILVL_D rather than storing both and loading the quad back, like the ARM64 JIT's ZIP1 (3d20d51c70). Co-Authored-By: Claude Opus 5.5 (1M context) --- Core/MIPS/LoongArch64/LoongArch64CompVec.cpp | 31 ++++++++-- Core/MIPS/LoongArch64/LoongArch64RegCache.cpp | 62 +++++++++++++++++++ Core/MIPS/LoongArch64/LoongArch64RegCache.h | 1 + 3 files changed, 88 insertions(+), 6 deletions(-) diff --git a/Core/MIPS/LoongArch64/LoongArch64CompVec.cpp b/Core/MIPS/LoongArch64/LoongArch64CompVec.cpp index 967cbba189..c85c2e0318 100644 --- a/Core/MIPS/LoongArch64/LoongArch64CompVec.cpp +++ b/Core/MIPS/LoongArch64/LoongArch64CompVec.cpp @@ -519,10 +519,15 @@ void LoongArch64JitBackend::CompIR_VecPack(IRInst inst) { switch (inst.op) { case IROp::Vec2Unpack16To31: - // With LSX, a lane group can be mapped as one vector reg, and then F() returns the same - // reg for every lane. The Vec2 ops here address lanes one by one, so leave them to the interpreter. + // With LSX, the pair is one vector reg (F() would give the same reg for both lanes). if (cpu_info.LOONGARCH_LSX) { - CompIR_Generic(inst); + if (Overlap(inst.dest, 2, inst.src1, 1)) + DISABLE; + regs_.Map(inst); + // Each halfword to the top of its lane, then down one. + VXOR_V(EncodeRegToV(SCRATCHF1), EncodeRegToV(SCRATCHF1), EncodeRegToV(SCRATCHF1)); + VILVL_H(regs_.V(inst.dest), regs_.V(inst.src1), EncodeRegToV(SCRATCHF1)); + VSRLI_W(regs_.V(inst.dest), regs_.V(inst.dest), 1); break; } // Like Vec2Unpack16To32, shifted down one more. @@ -539,7 +544,13 @@ void LoongArch64JitBackend::CompIR_VecPack(IRInst inst) { case IROp::Vec2Pack31To16: { if (cpu_info.LOONGARCH_LSX) { - CompIR_Generic(inst); + if (Overlap(inst.dest, 1, inst.src1, 2)) + DISABLE; + regs_.Map(inst); + VREPLGR2VR_D(EncodeRegToV(SCRATCHF1), R_ZERO); + VMAX_W(EncodeRegToV(SCRATCHF1), regs_.V(inst.src1), EncodeRegToV(SCRATCHF1)); + VSRLI_W(EncodeRegToV(SCRATCHF1), EncodeRegToV(SCRATCHF1), 15); + VPICKEV_H(regs_.V(inst.dest), EncodeRegToV(SCRATCHF1), EncodeRegToV(SCRATCHF1)); break; } // Bits 30-15 of each lane, with negative lanes clamped to zero. @@ -612,7 +623,11 @@ void LoongArch64JitBackend::CompIR_VecPack(IRInst inst) { case IROp::Vec2Unpack16To32: if (cpu_info.LOONGARCH_LSX) { - CompIR_Generic(inst); + if (Overlap(inst.dest, 2, inst.src1, 1)) + DISABLE; + regs_.Map(inst); + VXOR_V(EncodeRegToV(SCRATCHF1), EncodeRegToV(SCRATCHF1), EncodeRegToV(SCRATCHF1)); + VILVL_H(regs_.V(inst.dest), regs_.V(inst.src1), EncodeRegToV(SCRATCHF1)); break; } // src1 is read before either lane of dest is written. @@ -681,7 +696,11 @@ void LoongArch64JitBackend::CompIR_VecPack(IRInst inst) { case IROp::Vec2Pack32To16: if (cpu_info.LOONGARCH_LSX) { - CompIR_Generic(inst); + if (Overlap(inst.dest, 1, inst.src1, 2)) + DISABLE; + // The top halfword of each lane. + regs_.Map(inst); + VPICKOD_H(regs_.V(inst.dest), regs_.V(inst.src1), regs_.V(inst.src1)); break; } regs_.Map(inst); diff --git a/Core/MIPS/LoongArch64/LoongArch64RegCache.cpp b/Core/MIPS/LoongArch64/LoongArch64RegCache.cpp index 468ad201f9..df4c77d729 100644 --- a/Core/MIPS/LoongArch64/LoongArch64RegCache.cpp +++ b/Core/MIPS/LoongArch64/LoongArch64RegCache.cpp @@ -435,6 +435,8 @@ bool LoongArch64RegCache::TransferNativeReg(IRNativeReg nreg, IRNativeReg dest, return true; if (oldlanes == 1 && Transfer1ToVec(nreg, dest, first, lanes)) return true; + if (oldlanes == 2 && lanes == 4 && Transfer2ToVec4(nreg, dest, first)) + return true; } return IRNativeRegCacheBase::TransferNativeReg(nreg, dest, type, first, lanes, flags); @@ -690,6 +692,66 @@ bool LoongArch64RegCache::Transfer1ToVec(IRNativeReg nreg, IRNativeReg dest, IRR return true; } +// Two pairs into a quad, like after two vs2i.p into the halves of one. Either half may be in memory. +bool LoongArch64RegCache::Transfer2ToVec4(IRNativeReg nreg, IRNativeReg dest, IRReg first) { + if ((first & 3) != 0) + return false; + + // Each half: the native reg holding just that pair, or -1 for in memory. + IRNativeReg half[2]; + for (int h = 0; h < 2; ++h) { + const IRReg m = first + h * 2; + const IRNativeReg r = mr[m].nReg; + if (r == -1) { + if (mr[m].loc != MIPSLoc::MEM || mr[m + 1].loc != MIPSLoc::MEM) + return false; + } else if (nr[r].mipsReg != m || mr[m].lane != 0 || mr[m + 1].nReg != r || mr[m + 1].lane != 1 || (h == 0 && mr[m + 2].nReg == r)) { + return false; + } + // The other half, if this instruction maps it separately. + if (r != nreg && mr[m].spillLockIRIndex >= irIndex_) + return false; + half[h] = r; + } + if (half[0] != nreg && half[1] != nreg) + return false; + + const LoongArch64Reg destReg = EncodeRegToV(FromNativeReg(dest)); + auto V = [&](IRNativeReg r) { return EncodeRegToV(FromNativeReg(r)); }; + if (half[0] != -1 && half[1] != -1) { + emit_->VILVL_D(destReg, V(half[1]), V(half[0])); + } else { + const int inReg = half[0] != -1 ? 0 : 1; + if (dest != half[inReg]) { + emit_->VLD(destReg, CTXREG, GetMipsRegOffset(first)); + emit_->VEXTRINS_D(destReg, V(half[inReg]), inReg << 4); + } else { + emit_->FLD_D(SCRATCHF1, CTXREG, GetMipsRegOffset(first + (1 - inReg) * 2)); + if (inReg == 0) + emit_->VEXTRINS_D(destReg, EncodeRegToV(SCRATCHF1), 1 << 4); + else + emit_->VILVL_D(destReg, destReg, EncodeRegToV(SCRATCHF1)); + } + } + + bool dirty = false; + for (int h = 0; h < 2; ++h) { + if (half[h] == -1) + continue; + dirty = dirty || nr[half[h]].isDirty; + if (half[h] != dest) + DiscardNativeReg(half[h]); + } + for (int i = 0; i < 4; ++i) { + mr[first + i].lane = i; + mr[first + i].loc = MIPSLoc::FREG; + mr[first + i].nReg = dest; + } + nr[dest].mipsReg = first; + nr[dest].isDirty = dirty; + return true; +} + LoongArch64Reg LoongArch64RegCache::R(IRReg mipsReg) { _dbg_assert_(IsValidGPR(mipsReg)); _dbg_assert_(mr[mipsReg].loc == MIPSLoc::REG || mr[mipsReg].loc == MIPSLoc::REG_IMM); diff --git a/Core/MIPS/LoongArch64/LoongArch64RegCache.h b/Core/MIPS/LoongArch64/LoongArch64RegCache.h index 628fbb519e..4261318bc9 100644 --- a/Core/MIPS/LoongArch64/LoongArch64RegCache.h +++ b/Core/MIPS/LoongArch64/LoongArch64RegCache.h @@ -93,6 +93,7 @@ protected: private: bool TransferVecTo1(IRNativeReg nreg, IRNativeReg dest, IRReg first, int oldlanes); bool Transfer1ToVec(IRNativeReg nreg, IRNativeReg dest, IRReg first, int lanes); + bool Transfer2ToVec4(IRNativeReg nreg, IRNativeReg dest, IRReg first); LoongArch64Gen::LoongArch64Reg FromNativeReg(IRNativeReg r) { if (r >= NUM_LAGPR)