Skip to content

Commit 22dc398

Browse files
committed
[JSC] Implement VectorDupElement for all integers in x64
https://bugs.webkit.org/show_bug.cgi?id=329538 rdar://191379267 Reviewed by Vassili Bykov. This patch implements VectorDupElement for all integers in x64 so we can use this in ARM64 and x64. We also tweak Rosetta's CPU features as it is now supporting both AVX and AVX2. 1. i16x8 We use vpshuflw / vpshufhw to spread one lane in each half part, then spread it via vpshufd. 2. i8x16 Use vpunpcklbw to spread one side into interleaving other lane, and then now each i8x2 gets the expected one, so we spread it with i16x8 way. Both use vpbroadcast[bw] if available. Tests: JSTests/wasm/stress/simd-shuffle-dup-element.js Source/JavaScriptCore/assembler/testmasm.cpp * JSTests/wasm/stress/simd-shuffle-dup-element.js: Added. (async test): * Source/JavaScriptCore/assembler/MacroAssemblerX86_64.cpp: (JSC::MacroAssemblerX86_64::collectCPUFeatures): * Source/JavaScriptCore/assembler/MacroAssemblerX86_64.h: (JSC::MacroAssemblerX86_64::vectorDupElement): (JSC::MacroAssemblerX86_64::dupInt16Element): * Source/JavaScriptCore/assembler/X86Assembler.h: (JSC::X86Assembler::vpshufhw_i8rr): * Source/JavaScriptCore/assembler/testmasm.cpp: (JSC::testVectorDupElement): * Source/JavaScriptCore/b3/B3ReduceStrength.cpp: * Source/JavaScriptCore/b3/air/AirOpcode.opcodes: * Source/JavaScriptCore/jit/SIMDShuffle.h: (JSC::SIMDShuffle::isDupElement): * Source/JavaScriptCore/wasm/WasmBBQJIT64.cpp: (JSC::Wasm::BBQJITImpl::BBQJIT::addSIMDShuffle): Canonical link: https://commits.webkit.org/325305@main
1 parent 1568a3d commit 22dc398

9 files changed

Lines changed: 196 additions & 49 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//@ requireOptions("--useWasmSIMD=1")
2+
//@ skip if !$isSIMDPlatform
3+
import { instantiate } from "../wabt-wrapper.js"
4+
import * as assert from "../assert.js"
5+
6+
const patterns = [];
7+
for (const elementSize of [1, 2, 4, 8]) {
8+
for (const base of [0, 16]) {
9+
for (let lane = 0; lane < 16 / elementSize; ++lane) {
10+
const pattern = [];
11+
for (let i = 0; i < 16; ++i)
12+
pattern.push(base + lane * elementSize + i % elementSize);
13+
patterns.push(pattern);
14+
}
15+
}
16+
}
17+
18+
async function test() {
19+
let source = "";
20+
patterns.forEach((pattern, index) => {
21+
source += `
22+
(func (export "dup${index}") (param i32 i32 i32)
23+
(v128.store (local.get 2)
24+
(i8x16.shuffle ${pattern.join(" ")} (v128.load (local.get 0)) (v128.load (local.get 1)))))`;
25+
});
26+
const instance = await instantiate(`(module (memory (export "mem") 1) ${source})`, {}, { simd: true });
27+
const memory = new Uint8Array(instance.exports.mem.buffer);
28+
const input = [];
29+
for (let i = 0; i < 32; ++i) {
30+
input.push((0x81 + i * 7) & 0xff);
31+
memory[i] = input[i];
32+
}
33+
const funcs = patterns.map((_, index) => instance.exports[`dup${index}`]);
34+
const expected = patterns.map(pattern => pattern.map(index => input[index]));
35+
36+
for (let iteration = 0; iteration < wasmTestLoopCount; ++iteration) {
37+
for (let index = 0; index < funcs.length; ++index) {
38+
funcs[index](0, 16, 64);
39+
const bytes = expected[index];
40+
for (let i = 0; i < 16; ++i) {
41+
if (memory[64 + i] !== bytes[i])
42+
throw new Error(`dup${index} byte ${i}: expected ${bytes[i]} but got ${memory[64 + i]}`);
43+
}
44+
}
45+
}
46+
}
47+
48+
await assert.asyncTest(test());

‎Source/JavaScriptCore/assembler/MacroAssemblerX86_64.cpp‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -513,22 +513,38 @@ void MacroAssemblerX86_64::collectCPUFeatures()
513513
s_avxCheckState = (cpuid[2] & (1 << 28)) ? CPUIDCheckState::Set : CPUIDCheckState::Clear;
514514
#endif
515515
}
516+
auto collectLeaf7Features = [] {
517+
if (getCPUID(0x0)[0] < 0x7) {
518+
s_bmi1CheckState = CPUIDCheckState::Clear;
519+
s_avx2CheckState = CPUIDCheckState::Clear;
520+
return;
521+
}
522+
CPUID cpuid = getCPUID(0x7);
523+
s_bmi1CheckState = (cpuid[1] & (1 << 3)) ? CPUIDCheckState::Set : CPUIDCheckState::Clear;
524+
// Hypervisors can mask AVX while still reporting AVX2, and AVX2 code paths emit VEX instructions.
525+
s_avx2CheckState = (s_avxCheckState == CPUIDCheckState::Set && (cpuid[1] & (1 << 5))) ? CPUIDCheckState::Set : CPUIDCheckState::Clear;
526+
};
516527
#if OS(DARWIN)
517528
{
518529
uint32_t val = 0;
519530
size_t valSize = sizeof(val);
520-
int rc = sysctlbyname("hw.optional.bmi1", &val, &valSize, nullptr, 0);
521-
s_bmi1CheckState = (rc >= 0 && val) ? CPUIDCheckState::Set : CPUIDCheckState::Clear;
522-
523-
rc = sysctlbyname("hw.optional.avx2_0", &val, &valSize, nullptr, 0);
524-
s_avx2CheckState = (rc >= 0 && val) ? CPUIDCheckState::Set : CPUIDCheckState::Clear;
531+
// Not WTF::isX86BinaryRunningOnARM(): Rosetta developer builds compile it out but still run translated.
532+
int rc = sysctlbyname("sysctl.proc_translated", &val, &valSize, nullptr, 0);
533+
if (rc >= 0 && val) {
534+
// Rosetta translates AVX2 even when it does not advertise it, the same as AVX. It reports
535+
// BMI1 only through CPUID, and only when the process is launched with ROSETTA_ADVERTISE_AVX=1.
536+
collectLeaf7Features();
537+
s_avx2CheckState = CPUIDCheckState::Set;
538+
} else {
539+
rc = sysctlbyname("hw.optional.bmi1", &val, &valSize, nullptr, 0);
540+
s_bmi1CheckState = (rc >= 0 && val) ? CPUIDCheckState::Set : CPUIDCheckState::Clear;
541+
542+
rc = sysctlbyname("hw.optional.avx2_0", &val, &valSize, nullptr, 0);
543+
s_avx2CheckState = (rc >= 0 && val) ? CPUIDCheckState::Set : CPUIDCheckState::Clear;
544+
}
525545
}
526546
#else
527-
{
528-
CPUID cpuid = getCPUID(0x7);
529-
s_bmi1CheckState = (cpuid[2] & (1 << 3)) ? CPUIDCheckState::Set : CPUIDCheckState::Clear;
530-
s_avx2CheckState = (cpuid[2] & (1 << 5)) ? CPUIDCheckState::Set : CPUIDCheckState::Clear;
531-
}
547+
collectLeaf7Features();
532548
#endif
533549
{
534550
CPUID cpuid = getCPUID(0x80000001);

‎Source/JavaScriptCore/assembler/MacroAssemblerX86_64.h‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7937,6 +7937,29 @@ class MacroAssemblerX86_64 : public AbstractMacroAssembler<Assembler> {
79377937
m_assembler.vpshufd_i8rr(imm, src, dest);
79387938
break;
79397939
}
7940+
case SIMDLane::i16x8:
7941+
if (!lane.m_value && supportsAVX2()) {
7942+
m_assembler.vpbroadcastw_rr(src, dest);
7943+
break;
7944+
}
7945+
dupInt16Element(lane.m_value, src, dest);
7946+
break;
7947+
case SIMDLane::i8x16:
7948+
if (supportsAVX2()) {
7949+
if (lane.m_value) {
7950+
m_assembler.vpalignr_i8rrr(lane.m_value, src, src, dest);
7951+
m_assembler.vpbroadcastb_rr(dest, dest);
7952+
} else
7953+
m_assembler.vpbroadcastb_rr(src, dest);
7954+
break;
7955+
}
7956+
// Interleaving the vector with itself turns each byte into a 16-bit lane holding two copies of it.
7957+
if (lane.m_value < 8)
7958+
m_assembler.vpunpcklbw_rrr(src, src, dest);
7959+
else
7960+
m_assembler.vpunpckhbw_rrr(src, src, dest);
7961+
dupInt16Element(lane.m_value % 8, dest, dest);
7962+
break;
79407963
default:
79417964
RELEASE_ASSERT_NOT_REACHED();
79427965
}
@@ -9973,6 +9996,18 @@ class MacroAssemblerX86_64 : public AbstractMacroAssembler<Assembler> {
99739996
skipNonZeroCase.link(this);
99749997
}
99759998

9999+
void dupInt16Element(unsigned lane, FPRegisterID src, FPRegisterID dest)
10000+
{
10001+
ASSERT(lane < 8);
10002+
uint8_t wordShuffle = (lane % 4) * 0x55;
10003+
if (lane < 4)
10004+
m_assembler.vpshuflw_i8rr(wordShuffle, src, dest);
10005+
else
10006+
m_assembler.vpshufhw_i8rr(wordShuffle, src, dest);
10007+
// Every 32-bit lane of that 64-bit half now holds two copies of the word.
10008+
vectorDupElement(SIMDLane::i32x4, TrustedImm32(lane < 4 ? 0 : 2), dest, dest);
10009+
}
10010+
997610011
friend class LinkBuffer;
997710012

997810013
template<PtrTag tag>

‎Source/JavaScriptCore/assembler/X86Assembler.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4656,6 +4656,15 @@ class X86Assembler {
46564656
m_formatter.immediate8(controlBits);
46574657
}
46584658

4659+
void vpshufhw_i8rr(uint8_t controlBits, XMMRegisterID xmm2, XMMRegisterID xmm1)
4660+
{
4661+
// https://www.felixcloutier.com/x86/pshufhw
4662+
// VEX.128.F3.0F.WIG 70 /r ib VPSHUFHW xmm1, xmm2/m128, imm8
4663+
// A NA ModRM:reg (w) ModRM:r/m (r) imm8 NA
4664+
m_formatter.vexNdsLigWigTwoByteOp(PRE_SSE_F3, OP2_PSHUFHW_VdqWdqIb, (RegisterID)xmm1, (RegisterID)0, (RegisterID)xmm2);
4665+
m_formatter.immediate8(controlBits);
4666+
}
4667+
46594668
void vpshufd_i8rr(uint8_t controlBits, XMMRegisterID xmm2, XMMRegisterID xmm1)
46604669
{
46614670
// https://www.felixcloutier.com/x86/pshufd

‎Source/JavaScriptCore/assembler/testmasm.cpp‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8390,6 +8390,48 @@ static void testMove128ToVectorComprehensive()
83908390
testPattern(v128_t { 0xABCD1234EF005678ULL, 0xABCD1234EF005678ULL }); // Arbitrary pattern repeated
83918391
}
83928392

8393+
static void testVectorDupElement()
8394+
{
8395+
#if CPU(X86_64)
8396+
if (!MacroAssembler::supportsAVX())
8397+
return;
8398+
#endif
8399+
8400+
v128_t input;
8401+
for (unsigned i = 0; i < 16; ++i)
8402+
input.u8x16[i] = 0x81 + i * 7;
8403+
8404+
auto testLane = [&](SIMDLane simdLane, unsigned lane, bool inPlace) {
8405+
FPRReg src = FPRInfo::argumentFPR0;
8406+
FPRReg dest = inPlace ? src : FPRInfo::argumentFPR1;
8407+
auto compilation = compile([&](CCallHelpers& jit) {
8408+
emitFunctionPrologue(jit);
8409+
jit.move128ToVector(input, src);
8410+
jit.vectorDupElement(simdLane, CCallHelpers::TrustedImm32(lane), src, dest);
8411+
jit.vectorExtractLaneInt64(CCallHelpers::TrustedImm32(0), dest, GPRInfo::returnValueGPR);
8412+
jit.vectorExtractLaneInt64(CCallHelpers::TrustedImm32(1), dest, GPRInfo::returnValueGPR2);
8413+
emitFunctionEpilogue(jit);
8414+
jit.ret();
8415+
});
8416+
8417+
v128_t expected;
8418+
unsigned elementSize = elementByteSize(simdLane);
8419+
for (unsigned i = 0; i < 16; ++i)
8420+
expected.u8x16[i] = input.u8x16[lane * elementSize + i % elementSize];
8421+
8422+
auto [low64, high64] = invoke<std::pair<uint64_t, uint64_t>>(compilation);
8423+
CHECK_EQ(low64, expected.u64x2[0]);
8424+
CHECK_EQ(high64, expected.u64x2[1]);
8425+
};
8426+
8427+
for (SIMDLane simdLane : { SIMDLane::i8x16, SIMDLane::i16x8, SIMDLane::i32x4, SIMDLane::i64x2 }) {
8428+
for (unsigned lane = 0; lane < elementCount(simdLane); ++lane) {
8429+
testLane(simdLane, lane, false);
8430+
testLane(simdLane, lane, true);
8431+
}
8432+
}
8433+
}
8434+
83938435
#if CPU(ARM64)
83948436
// Test half-precision FMOV encoding (cmode=0b1110, op=1)
83958437
// Verifies that 16-bit, 32-bit, and 64-bit FMOV use correct encodings
@@ -9115,6 +9157,7 @@ void run(const char* filter) WTF_IGNORES_THREAD_SAFETY_ANALYSIS
91159157
RUN(testMove32ToFloatComprehensive());
91169158
RUN(testMove64ToDoubleComprehensive());
91179159
RUN(testMove128ToVectorComprehensive());
9160+
RUN(testVectorDupElement());
91189161

91199162
RUN(testMove32ToFloatX64());
91209163
RUN(testMove64ToDoubleX64());
@@ -9155,6 +9198,19 @@ void run(const char* filter) WTF_IGNORES_THREAD_SAFETY_ANALYSIS
91559198

91569199
for (auto& thread : threads)
91579200
thread->waitForCompletion();
9201+
9202+
#if CPU(X86_64)
9203+
// CPU feature flags are process-wide, so a fallback path can only be forced once no other test is running.
9204+
if (shouldRun("testVectorDupElementWithoutAVX2") && MacroAssembler::supportsAVX2()) {
9205+
numberOfTests++;
9206+
dataLog("testVectorDupElementWithoutAVX2...\n");
9207+
MacroAssembler::s_avx2CheckState = MacroAssembler::CPUIDCheckState::Clear;
9208+
testVectorDupElement();
9209+
MacroAssembler::s_avx2CheckState = MacroAssembler::CPUIDCheckState::Set;
9210+
dataLog("testVectorDupElementWithoutAVX2: OK!\n");
9211+
}
9212+
#endif
9213+
91589214
crashLock.lock();
91599215
dataLog("Completed ", numberOfTests, " tests\n");
91609216
}

‎Source/JavaScriptCore/b3/B3ReduceStrength.cpp‎

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3780,30 +3780,12 @@ class ReduceStrength {
37803780
}
37813781
}
37823782

3783-
// DupElement: i64x2 and i32x4 on all platforms
3784-
if (auto lane = SIMDShuffle::isI64x2DupElement(pattern)) {
3785-
replaceWithNew<SIMDValue>(m_value->origin(), VectorDupElement, B3::V128, SIMDLane::i64x2, SIMDSignMode::None, lane.value(), inner);
3783+
if (auto dup = SIMDShuffle::isDupElement(pattern)) {
3784+
auto [lane, index] = *dup;
3785+
replaceWithNew<SIMDValue>(m_value->origin(), VectorDupElement, B3::V128, lane, SIMDSignMode::None, index, inner);
37863786
break;
37873787
}
37883788

3789-
if (auto lane = SIMDShuffle::isI32x4DupElement(pattern)) {
3790-
replaceWithNew<SIMDValue>(m_value->origin(), VectorDupElement, B3::V128, SIMDLane::i32x4, SIMDSignMode::None, lane.value(), inner);
3791-
break;
3792-
}
3793-
3794-
// DupElement: i16x8 and i8x16 — ARM64 only
3795-
if constexpr (isARM64()) {
3796-
if (auto lane = SIMDShuffle::isI16x8DupElement(pattern)) {
3797-
replaceWithNew<SIMDValue>(m_value->origin(), VectorDupElement, B3::V128, SIMDLane::i16x8, SIMDSignMode::None, lane.value(), inner);
3798-
break;
3799-
}
3800-
3801-
if (auto lane = SIMDShuffle::isI8x16DupElement(pattern)) {
3802-
replaceWithNew<SIMDValue>(m_value->origin(), VectorDupElement, B3::V128, SIMDLane::i8x16, SIMDSignMode::None, lane.value(), inner);
3803-
break;
3804-
}
3805-
}
3806-
38073789
if (m_pass == ReduceStrengthPass::Final) {
38083790
// Check if this unary pattern matches a canonical instruction
38093791
// (UZP, ZIP, TRN) when treated as both inputs being the same register.

‎Source/JavaScriptCore/b3/air/AirOpcode.opcodes‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,9 +2203,9 @@ arm64: VectorSwizzle2 U:F:128, U:F:128, U:F:128, D:F:128
22032203
64: VectorRelaxedDotI8x16I7x16Add U:F:128, U:F:128, U:F:128, D:F:128, S:F:128, S:F:128
22042204
Tmp, Tmp, Tmp, Tmp, Tmp, Tmp
22052205

2206-
arm64: VectorDupElementInt8 U:G:8, U:F:128, D:F:128
2206+
64: VectorDupElementInt8 U:G:8, U:F:128, D:F:128
22072207
Imm, Tmp, Tmp
2208-
arm64: VectorDupElementInt16 U:G:8, U:F:128, D:F:128
2208+
64: VectorDupElementInt16 U:G:8, U:F:128, D:F:128
22092209
Imm, Tmp, Tmp
22102210
64: VectorDupElementInt32 U:G:8, U:F:128, D:F:128
22112211
Imm, Tmp, Tmp

‎Source/JavaScriptCore/jit/SIMDShuffle.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ class SIMDShuffle {
181181
return std::nullopt;
182182
}
183183

184+
static std::optional<std::tuple<SIMDLane, uint8_t>> isDupElement(v128_t pattern)
185+
{
186+
if (auto lane = isI64x2DupElement(pattern))
187+
return std::tuple { SIMDLane::i64x2, *lane };
188+
if (auto lane = isI32x4DupElement(pattern))
189+
return std::tuple { SIMDLane::i32x4, *lane };
190+
if (auto lane = isI16x8DupElement(pattern))
191+
return std::tuple { SIMDLane::i16x8, *lane };
192+
if (auto lane = isI8x16DupElement(pattern))
193+
return std::tuple { SIMDLane::i8x16, *lane };
194+
return std::nullopt;
195+
}
196+
184197
static bool isI16x8Shuffle(v128_t pattern)
185198
{
186199
return isLargerElementShuffle(pattern, 2);

‎Source/JavaScriptCore/wasm/WasmBBQJIT64.cpp‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3733,22 +3733,10 @@ void LEGACY_NODELETE BBQJIT::notifyFunctionUsesSIMD()
37333733
Location resultLocation = allocate(result);
37343734
LOG_INSTRUCTION("VectorShuffle", a, b, source, sourceLocation, RESULT(result));
37353735

3736-
if constexpr (isARM64()) {
3737-
if (auto lane = SIMDShuffle::isI8x16DupElement(pattern)) {
3738-
m_jit.vectorDupElement(SIMDLane::i8x16, TrustedImm32(*lane), sourceLocation.asFPR(), resultLocation.asFPR());
3739-
return { };
3740-
}
3741-
if (auto lane = SIMDShuffle::isI16x8DupElement(pattern)) {
3742-
m_jit.vectorDupElement(SIMDLane::i16x8, TrustedImm32(*lane), sourceLocation.asFPR(), resultLocation.asFPR());
3743-
return { };
3744-
}
3745-
}
3746-
3747-
if (auto lane = SIMDShuffle::isI32x4DupElement(pattern))
3748-
m_jit.vectorDupElement(SIMDLane::i32x4, TrustedImm32(*lane), sourceLocation.asFPR(), resultLocation.asFPR());
3749-
else if (auto lane = SIMDShuffle::isI64x2DupElement(pattern))
3750-
m_jit.vectorDupElement(SIMDLane::i64x2, TrustedImm32(*lane), sourceLocation.asFPR(), resultLocation.asFPR());
3751-
else {
3736+
if (auto dup = SIMDShuffle::isDupElement(pattern)) {
3737+
auto [lane, index] = *dup;
3738+
m_jit.vectorDupElement(lane, TrustedImm32(index), sourceLocation.asFPR(), resultLocation.asFPR());
3739+
} else {
37523740
materializeVectorConstant(pattern, Location::fromFPR(wasmScratchFPR));
37533741
m_jit.vectorSwizzle(sourceLocation.asFPR(), wasmScratchFPR, resultLocation.asFPR());
37543742
}

0 commit comments

Comments
 (0)