Skip to content

Commit

Permalink
PPU/SPU LLVM: Emulate VPERM2B with a 256 bit wide VPERMB
Browse files Browse the repository at this point in the history
- Save 1 uop by using 256 wide VPERMB instead of VPERM2B. (Compiles down to a vinserti128 and vpermb)
  • Loading branch information
Whatcookie authored and Nekotekina committed Oct 13, 2021
1 parent 68fdc49 commit f06c8b2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
64 changes: 64 additions & 0 deletions rpcs3/Emu/CPU/CPUTranslator.h
Original file line number Diff line number Diff line change
Expand Up @@ -3442,6 +3442,70 @@ class cpu_translator
return result;
}

// Emulate the behavior of VPERM2B by using a 256 bit wide VPERMB
template <typename T1, typename T2, typename T3>
value_t<u8[16]> vperm2b256to128(T1 a, T2 b, T3 c)
{
value_t<u8[16]> result;

const auto data0 = a.eval(m_ir);
const auto data1 = b.eval(m_ir);
const auto index = c.eval(m_ir);

// May be slower than non constant path?
if (auto c = llvm::dyn_cast<llvm::Constant>(index))
{
// Convert VPERM2B index back to LLVM vector shuffle mask
v128 mask{};

const auto cv = llvm::dyn_cast<llvm::ConstantDataVector>(c);

if (cv)
{
for (u32 i = 0; i < 16; i++)
{
const u64 b = cv->getElementAsInteger(i);
mask._u8[i] = b & 0x1f;
}
}

if (cv || llvm::isa<llvm::ConstantAggregateZero>(c))
{
result.value = llvm::ConstantDataVector::get(m_context, llvm::makeArrayRef(reinterpret_cast<const u8*>(&mask), 16));
result.value = m_ir->CreateZExt(result.value, get_type<u32[16]>());
result.value = m_ir->CreateShuffleVector(data0, data1, result.value);
return result;
}
}

const auto zeroes = llvm::ConstantAggregateZero::get(get_type<u8[16]>());
const auto zeroes32 = llvm::ConstantAggregateZero::get(get_type<u8[32]>());

value_t<u8[32]> intermediate;
value_t<u8[32]> shuffle;
value_t<u8[32]> shuffleindex;

u8 mask32[32] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
u8 mask16[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

// insert the second source operand into the same vector as the first source operand and expand to 256 bit width
shuffle.value = llvm::ConstantDataVector::get(m_context, llvm::makeArrayRef(reinterpret_cast<const u8*>(&mask32), 32));
shuffle.value = m_ir->CreateZExt(shuffle.value, get_type<u32[32]>());
intermediate.value = m_ir->CreateShuffleVector(data0, data1, shuffle.value);

// expand the shuffle index to 256 bits with zeroes
shuffleindex.value = m_ir->CreateShuffleVector(index, zeroes, shuffle.value);

// permute
intermediate.value = m_ir->CreateCall(get_intrinsic(llvm::Intrinsic::x86_avx512_permvar_qi_256), {intermediate.value, shuffleindex.value});

// convert the 256 bit vector back to 128 bits
result.value = llvm::ConstantDataVector::get(m_context, llvm::makeArrayRef(reinterpret_cast<const u8*>(&mask16), 16));
result.value = m_ir->CreateZExt(result.value, get_type<u32[16]>());
result.value = m_ir->CreateShuffleVector(intermediate.value, zeroes32, result.value);
return result;
}

llvm::Value* load_const(llvm::GlobalVariable* g, llvm::Value* i)
{
return m_ir->CreateLoad(m_ir->CreateGEP(g, {m_ir->getInt64(0), m_ir->CreateZExtOrTrunc(i, get_type<u64>())}));
Expand Down
2 changes: 1 addition & 1 deletion rpcs3/Emu/Cell/PPUTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ void PPUTranslator::VPERM(ppu_opcode_t op)
if (m_use_avx512_icl && op.ra != op.rb)
{
const auto i = eval(~c);
set_vr(op.vd, vperm2b(b, a, i));
set_vr(op.vd, vperm2b256to128(b, a, i));
return;
}

Expand Down
8 changes: 4 additions & 4 deletions rpcs3/Emu/Cell/SPURecompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7645,13 +7645,13 @@ class spu_llvm_recompiler : public spu_recompiler_base, public cpu_translator
{
if (perm_only)
{
set_vr(op.rt4, vperm2b(as, bs, c));
set_vr(op.rt4, vperm2b256to128(as, bs, c));
return;
}

const auto m = gf2p8affineqb(c, build<u8[16]>(0x40, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x40, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20), 0x7f);
const auto mm = select(noncast<s8[16]>(m) >= 0, splat<u8[16]>(0), m);
const auto ab = vperm2b(as, bs, c);
const auto ab = vperm2b256to128(as, bs, c);
set_vr(op.rt4, select(noncast<s8[16]>(c) >= 0, ab, mm));
return;
}
Expand Down Expand Up @@ -7707,14 +7707,14 @@ class spu_llvm_recompiler : public spu_recompiler_base, public cpu_translator
{
if (perm_only)
{
set_vr(op.rt4, vperm2b(b, a, eval(~c)));
set_vr(op.rt4, vperm2b256to128(b, a, eval(~c)));
return;
}

const auto m = gf2p8affineqb(c, build<u8[16]>(0x40, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x40, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20), 0x7f);
const auto mm = select(noncast<s8[16]>(m) >= 0, splat<u8[16]>(0), m);
const auto cr = eval(~c);
const auto ab = vperm2b(b, a, cr);
const auto ab = vperm2b256to128(b, a, cr);
set_vr(op.rt4, select(noncast<s8[16]>(cr) >= 0, mm, ab));
return;
}
Expand Down

0 comments on commit f06c8b2

Please sign in to comment.