Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] bun should prefer bun-baseline #879

Open
1 task done
coolaj86 opened this issue Aug 21, 2024 · 3 comments
Open
1 task done

[Bug] bun should prefer bun-baseline #879

coolaj86 opened this issue Aug 21, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@coolaj86
Copy link
Member

coolaj86 commented Aug 21, 2024

  • linux (vm, container, NOT desktop)
  • NOT macos
  • NOT windows

Containers often play to the lowest common denominator for CPU virtualization. That leads to this:

Bun v1.1.25 (fe62a614) Linux x64
fish: Job 1, 'bun' terminated by signal SIGILL (Illegal instruction)

We either need to either figure out what specific CPU features pertain to "profile" vs "" vs "baseline" and detect them, or just serve baseline, for Linux at least.

Update here's how we find out:

cat /proc/cpuinfo | grep avx2

More: oven-sh/bun#7607 (comment)

@coolaj86 coolaj86 added the bug Something isn't working label Aug 21, 2024
@Jarred-Sumner
Copy link

Jarred-Sumner commented Aug 23, 2024

It should auto-detect the CPU target instead of always choosing baseline. We have a bunch of SIMD optimizations that are disabled on the baseline build

@coolaj86
Copy link
Member Author

@Jarred-Sumner We don't yet have a standard method for detecting cpu-level features, but I have some ideas for how to implement it when I have time.

@coolaj86
Copy link
Member Author

coolaj86 commented Sep 13, 2024

Probably not 100% accurate, but here's a starter:
(however, I'm not sure how far down this rabbit hole I really want to go - it could turn out to be a lot of complex bloat)

To detect specific CPU features that can help identify AMD processor generations (such as AMDv2, AMDv3, and AMDv4), you can use various commands and utilities depending on the operating system. Below is a matrix for Linux, Windows, macOS, FreeBSD, OpenBSD, and NetBSD.

Operating System Command for AMDv2 (AVX) Command for AMDv3 (AVX2) Command for AMDv4 (AVX-512)
Linux cat /proc/cpuinfo | grep avx cat /proc/cpuinfo | grep avx2 cat /proc/cpuinfo | grep avx512
Windows wmic cpu get name, Caption | findstr /C:"AVX" wmic cpu get name, Caption | findstr /C:"AVX2" wmic cpu get name, Caption | findstr /C:"AVX-512"
macOS sysctl -a | grep machdep.cpu.features | grep AVX1.0 sysctl -a | grep machdep.cpu.features | grep AVX2.0 sysctl -a | grep machdep.cpu.features | grep AVX512F
FreeBSD dmesg | grep AVX dmesg | grep AVX2 dmesg | grep AVX512
OpenBSD sysctl -a | grep hw.features | grep AVX sysctl -a | grep hw.features | grep AVX2 sysctl -a | grep hw.features | grep AVX512
NetBSD cpuctl identify 0 | grep AVX cpuctl identify 0 | grep AVX2 cpuctl identify 0 | grep AVX512

Explanation of CPU Feature Detection:

  • AVX (Advanced Vector Extensions): Generally indicates AMDv2.
  • AVX2: Introduced with AMDv3.
  • AVX-512: AMDv4 includes support for AVX-512.

Additional Notes:

  • On Linux, lscpu can also give a comprehensive overview of CPU features.
  • On macOS, sysctl -a is used to retrieve detailed CPU features.
  • On Windows, using wmic is a quick way to retrieve CPU details.
  • On BSD systems, dmesg or sysctl combined with grep is effective, but for NetBSD, cpuctl is a more specific tool to get CPU features.

Drafts Scripts

Perhaps we could template out OS-specific functions for the webi script post-boostrap.

#!/bin/sh
set -e
set -u

# Detect the OS
os="$(uname)"

detect_linux() {
    if grep -q avx512 /proc/cpuinfo; then
        echo "Detected AMDv4 (AVX-512)"
    elif grep -q avx2 /proc/cpuinfo; then
        echo "Detected AMDv3 (AVX2)"
    elif grep -q avx /proc/cpuinfo; then
        echo "Detected AMDv2 (AVX)"
    else
        echo "Unknown CPU features or unsupported CPU"
    fi
}

detect_macos() {
    features=$(sysctl -a 2>/dev/null | grep machdep.cpu.features)
    if echo "$features" | grep -q AVX512F; then
        echo "Detected AMDv4 (AVX-512)"
    elif echo "$features" | grep -q AVX2.0; then
        echo "Detected AMDv3 (AVX2)"
    elif echo "$features" | grep -q AVX1.0; then
        echo "Detected AMDv2 (AVX)"
    else
        echo "Unknown CPU features or unsupported CPU"
    fi
}

detect_freebsd() {
    features=$(dmesg | grep -i avx)
    if echo "$features" | grep -q AVX512; then
        echo "Detected AMDv4 (AVX-512)"
    elif echo "$features" | grep -q AVX2; then
        echo "Detected AMDv3 (AVX2)"
    elif echo "$features" | grep -q AVX; then
        echo "Detected AMDv2 (AVX)"
    else
        echo "Unknown CPU features or unsupported CPU"
    fi
}

detect_openbsd() {
    features=$(sysctl -a 2>/dev/null | grep hw.features)
    if echo "$features" | grep -q AVX512; then
        echo "Detected AMDv4 (AVX-512)"
    elif echo "$features" | grep -q AVX2; then
        echo "Detected AMDv3 (AVX2)"
    elif echo "$features" | grep -q AVX; then
        echo "Detected AMDv2 (AVX)"
    else
        echo "Unknown CPU features or unsupported CPU"
    fi
}

detect_netbsd() {
    features=$(cpuctl identify 0)
    if echo "$features" | grep -q AVX512; then
        echo "Detected AMDv4 (AVX-512)"
    elif echo "$features" | grep -q AVX2; then
        echo "Detected AMDv3 (AVX2)"
    elif echo "$features" | grep -q AVX; then
        echo "Detected AMDv2 (AVX)"
    else
        echo "Unknown CPU features or unsupported CPU"
    fi
}

# Call the appropriate function based on OS
case "$os" in
    Linux)
        detect_linux
        ;;
    Darwin)
        detect_macos
        ;;
    FreeBSD)
        detect_freebsd
        ;;
    OpenBSD)
        detect_openbsd
        ;;
    NetBSD)
        detect_netbsd
        ;;
    *)
        echo "Unsupported OS: $os"
        exit 1
        ;;
esac
# Detect CPU features on Windows: AVX, AVX2, AVX-512

# Run WMIC command to get CPU capabilities
$cpuInfo = wmic cpu get Caption

# Check for AVX, AVX2, AVX-512 support
$avx = $cpuInfo -match "AVX"
$avx2 = $cpuInfo -match "AVX2"
$avx512 = $cpuInfo -match "AVX-512"

# Determine which AMDv version is supported
if ($avx512) {
    Write-Host "Detected AMDv4 (AVX-512)"
} elseif ($avx2) {
    Write-Host "Detected AMDv3 (AVX2)"
} elseif ($avx) {
    Write-Host "Detected AMDv2 (AVX)"
} else {
    Write-Host "Unknown CPU features or unsupported CPU"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants