Skip to content

Commit ea74181

Browse files
committed
deps: V8: cherry-pick 5cbd9b7e4f96
Original commit message: [riscv] Allow vlen=1024 This resolves the remaining crashes observed in Node.js at #64538 allowing execution on systems with vlen=1024 such as the K3's A100 cores including raising kMaxRvvVLEN from 512 to 1024. the code has a refactored helper function to abstract out some of the changes across the SetSimd* functions. The changes to the tests allow them to pass successfully in vlen=1024 environment with functionality for lower vlen's unaffected. Test: Run the code snippet from #64538 (comment) - it will give an illegal exception on a system with a vlen of 1024 without this patch. If someone else can test it on other systems that would be good as I have only verified it on the K3's vlen=256 and vlen=1024 cores (and also by building Node.js with these changes and verifying that npm no longer crashes). The main functional piece of the patch is quite small, but there have been a number of changes to the unit tests required to support this which are included in this CL. The updated tests have been executed on vlen sizes of 256 and 1024 and pass. This patch was assisted by cursor AI. R=yahan@iscas.ac.cn Signed-off-by: Stewart Addison <sxa@ibm.com> Change-Id: I752f9ae23d3e605dbcf5bd8d97d104b0a788e5c6 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/8585179 Reviewed-by: Ji Qiu <qiuji@iscas.ac.cn> Reviewed-by: Yahan Lu (LuYahan) <yahan@iscas.ac.cn> Commit-Queue: Ji Qiu <qiuji@iscas.ac.cn> Cr-Commit-Position: refs/heads/main@{#111579} Refs: v8/v8@5cbd9b7
1 parent 460a740 commit ea74181

8 files changed

Lines changed: 663 additions & 185 deletions

File tree

‎common.gypi‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
# Reset this number to 0 on major V8 upgrades.
5252
# Increment by one for each non-official patch applied to deps/v8.
53-
'v8_embedder_string': '-node.14',
53+
'v8_embedder_string': '-node.15',
5454

5555
##### V8 defaults for Node.js #####
5656

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright 2026 the V8 project authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "src/base/cpu/cpu.h"
6+
7+
#if V8_OS_LINUX
8+
#include <linux/auxvec.h> // AT_HWCAP
9+
#include <sys/auxv.h> // getauxval()
10+
#endif
11+
#if V8_OS_POSIX
12+
#include <unistd.h> // sysconf()
13+
#endif
14+
15+
#include <asm/unistd.h>
16+
#include <ctype.h>
17+
#include <limits.h>
18+
#include <stdio.h>
19+
#include <stdlib.h>
20+
#include <string.h>
21+
22+
#include <algorithm>
23+
24+
#include "src/base/cpu/cpu-helper.h"
25+
#include "src/base/logging.h"
26+
#include "src/base/platform/wrappers.h"
27+
28+
#ifdef V8_HOST_ARCH_RISCV64
29+
#include <asm/hwprobe.h>
30+
#include <riscv_vector.h>
31+
// The __riscv_vlenb intrinsic is only available when compiling with the RVV
32+
// extension enabled. Use the 'target' attribute to tell the compiler to
33+
// compile this function with RVV enabled.
34+
// We must not call this function when RVV is not supported by the CPU.
35+
__attribute__((target("arch=+v"))) static unsigned vlen_intrinsic() {
36+
return static_cast<unsigned>(__riscv_vlenb() * 8);
37+
}
38+
#endif
39+
40+
namespace v8::base {
41+
void CPU::DetectFeatures() {
42+
#ifdef V8_HOST_ARCH_RISCV64
43+
#if V8_OS_LINUX
44+
CPUInfo cpu_info;
45+
riscv_hwprobe pairs[] = {{RISCV_HWPROBE_KEY_IMA_EXT_0, 0}};
46+
if (!syscall(__NR_riscv_hwprobe, &pairs,
47+
sizeof(pairs) / sizeof(riscv_hwprobe), 0, nullptr, 0)) {
48+
if (pairs[0].value & RISCV_HWPROBE_IMA_V) {
49+
has_rvv_ = true;
50+
}
51+
if (pairs[0].value & RISCV_HWPROBE_IMA_FD) {
52+
has_fpu_ = true;
53+
}
54+
if (pairs[0].value & RISCV_HWPROBE_EXT_ZBA) {
55+
has_zba_ = true;
56+
}
57+
if (pairs[0].value & RISCV_HWPROBE_EXT_ZBB) {
58+
has_zbb_ = true;
59+
}
60+
if (pairs[0].value & RISCV_HWPROBE_EXT_ZBS) {
61+
has_zbs_ = true;
62+
}
63+
if (pairs[0].value & RISCV_HWPROBE_EXT_ZFA) {
64+
has_zfa_ = true;
65+
}
66+
if (pairs[0].value & RISCV_HWPROBE_EXT_ZFH) {
67+
has_zfh_ = true;
68+
}
69+
if (pairs[0].value & RISCV_HWPROBE_EXT_ZVFH) {
70+
has_zvfh_ = true;
71+
}
72+
if (pairs[0].value & RISCV_HWPROBE_IMA_C) {
73+
has_rvc_ = true;
74+
}
75+
}
76+
77+
char* mmu = cpu_info.ExtractField("mmu");
78+
if (HasListItem(mmu, "sv48")) {
79+
riscv_mmu_ = RV_MMU_MODE::kRiscvSV48;
80+
}
81+
if (HasListItem(mmu, "sv39")) {
82+
riscv_mmu_ = RV_MMU_MODE::kRiscvSV39;
83+
}
84+
if (HasListItem(mmu, "sv57")) {
85+
riscv_mmu_ = RV_MMU_MODE::kRiscvSV57;
86+
}
87+
#endif
88+
if (has_rvv_) {
89+
vlen_ = vlen_intrinsic();
90+
}
91+
#endif
92+
}
93+
} // namespace v8::base

‎deps/v8/src/codegen/riscv/assembler-riscv.h‎

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -628,30 +628,44 @@ class V8_EXPORT_PRIVATE Assembler : public AssemblerBase,
628628
return (avl_ == expected_avl);
629629
}
630630

631-
void SetSimd128(VSew sew, TailAgnosticType tail = ta) {
632-
Vlmul lmul;
633-
// Just support riscv zve64x now.
634-
// ELEN * LMUL >= SEW
635-
// --> 2^(elen + 3) * 2^(-n) >= 2^(sew + 3)
636-
// --> elen >= sew + n
637-
// mf2 -> n is 1.
638-
// mf4 -> n is 2.
639-
// mf8 -> n is 3.
640-
DCHECK_EQ(kRvvELEN, 64);
641-
switch (CpuFeatures::vlen()) {
642-
case 128:
643-
lmul = m1;
644-
break;
645-
case 256:
646-
lmul = (sew + 1) > E64 ? m1 : mf2;
647-
break;
648-
case 512:
649-
lmul = (sew + 2) > E64 ? m1 : mf4;
650-
break;
651-
default:
652-
static_assert(kMaxRvvVLEN <= 512, "Unsupported VLEN");
653-
UNIMPLEMENTED();
631+
static Vlmul MinimumLmulForTargetBits(int target_bits, VSew sew) {
632+
const int vlen = CpuFeatures::vlen();
633+
DCHECK(vlen >= 128 && vlen <= kMaxRvvVLEN && (vlen % 128) == 0);
634+
const int sew_bits = 8 << static_cast<int>(sew);
635+
// LMUL must satisfy RVV 1.0: LMUL * ELEN >= SEW and VLMAX >= avl, where
636+
// avl = target_bits / SEW. Equivalently, LMUL >= max(SEW / ELEN,
637+
// target_bits / VLEN).
638+
const int64_t req_num_from_sew = sew_bits;
639+
const int64_t req_den_from_sew = kRvvELEN;
640+
const int64_t req_num_from_vlen = target_bits;
641+
const int64_t req_den_from_vlen = vlen;
642+
int64_t req_num;
643+
int64_t req_den;
644+
if (req_num_from_sew * req_den_from_vlen >=
645+
req_num_from_vlen * req_den_from_sew) {
646+
req_num = req_num_from_sew;
647+
req_den = req_den_from_sew;
648+
} else {
649+
req_num = req_num_from_vlen;
650+
req_den = req_den_from_vlen;
654651
}
652+
static constexpr struct {
653+
Vlmul lmul;
654+
int num;
655+
int den;
656+
} kOptions[] = {{mf8, 1, 8}, {mf4, 1, 4}, {mf2, 1, 2}, {m1, 1, 1},
657+
{m2, 2, 1}, {m4, 4, 1}, {m8, 8, 1}};
658+
for (const auto& option : kOptions) {
659+
if (static_cast<int64_t>(option.num) * req_den >=
660+
req_num * option.den) {
661+
return option.lmul;
662+
}
663+
}
664+
UNREACHABLE();
665+
}
666+
667+
void SetSimd128(VSew sew, TailAgnosticType tail = ta) {
668+
Vlmul lmul = MinimumLmulForTargetBits(128, sew);
655669
if (sew == E8) {
656670
set(16, sew, lmul, tail);
657671
} else if (sew == E16) {
@@ -666,22 +680,7 @@ class V8_EXPORT_PRIVATE Assembler : public AssemblerBase,
666680
}
667681

668682
void SetSimd128Half(VSew sew, TailAgnosticType tail = ta) {
669-
Vlmul lmul;
670-
DCHECK_EQ(kRvvELEN, 64);
671-
switch (CpuFeatures::vlen()) {
672-
case 128:
673-
lmul = (sew + 1) > E64 ? m1 : mf2;
674-
break;
675-
case 256:
676-
lmul = (sew + 2) > E64 ? (sew == E32 ? mf2 : m1) : mf4;
677-
break;
678-
case 512:
679-
lmul = (sew + 3) > E64 ? (sew <= E32 ? mf2 : m1) : mf8;
680-
break;
681-
default:
682-
static_assert(kMaxRvvVLEN <= 512, "Unsupported VLEN");
683-
UNIMPLEMENTED();
684-
}
683+
Vlmul lmul = MinimumLmulForTargetBits(64, sew);
685684
if (sew == E8) {
686685
set(8, sew, lmul, tail);
687686
} else if (sew == E16) {
@@ -696,22 +695,7 @@ class V8_EXPORT_PRIVATE Assembler : public AssemblerBase,
696695
}
697696

698697
void SetSimd128x2(VSew sew, TailAgnosticType tail = ta) {
699-
Vlmul lmul;
700-
DCHECK_EQ(kRvvELEN, 64);
701-
switch (CpuFeatures::vlen()) {
702-
case 128:
703-
lmul = m2;
704-
break;
705-
case 256:
706-
lmul = m1;
707-
break;
708-
case 512:
709-
lmul = (sew + 1) > E64 ? m1 : mf2;
710-
break;
711-
default:
712-
static_assert(kMaxRvvVLEN <= 512, "Unsupported VLEN");
713-
UNIMPLEMENTED();
714-
}
698+
Vlmul lmul = MinimumLmulForTargetBits(256, sew);
715699
if (sew == E8) {
716700
set(32, sew, lmul, tail);
717701
} else if (sew == E16) {

‎deps/v8/src/codegen/riscv/base-constants-riscv.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ constexpr int kRvvELEN = 64;
340340
// Maximum supported VLEN in bits.
341341
// Code that could fail with larger VLEN should static_assert against this
342342
// constant.
343-
constexpr int kMaxRvvVLEN = 512;
343+
constexpr int kMaxRvvVLEN = 1024;
344344

345345
#if V8_TARGET_ARCH_RISCV64
346346
constexpr int kRvXLEN = 64;

0 commit comments

Comments
 (0)