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

Detect x86_64 microarchitecture variant automatically #9788

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14,110 changes: 7,055 additions & 7,055 deletions crates/uv-python/download-metadata.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions crates/uv-python/fetch-download-metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ def __str__(self) -> str:
return (self.family + "_" + self.variant) if self.variant else self.family

def __gt__(self, other) -> bool:
return (self.family, self.variant or "") > (other.family, other.variant or "")
# Note this is inverted to prefer newer variants
return (self.family, self.variant or "") < (other.family, other.variant or "")

def __lt__(self, other) -> bool:
return (self.family, self.variant or "") < (other.family, other.variant or "")
# Note this is inverted to prefer newer variants
return (self.family, self.variant or "") > (other.family, other.variant or "")


type PlatformTripleKey = tuple[str, str, str]
Expand Down
6,800 changes: 3,400 additions & 3,400 deletions crates/uv-python/src/downloads.inc

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions crates/uv-python/src/downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,26 +251,30 @@ impl PythonDownloadRequest {

/// Whether this request is satisfied by the key of an existing installation.
pub fn satisfied_by_key(&self, key: &PythonInstallationKey) -> bool {
if let Some(arch) = &self.arch {
if key.arch != *arch {
if let Some(os) = &self.os {
if key.os != *os {
return false;
}
}
if let Some(os) = &self.os {
if key.os != *os {

if let Some(arch) = &self.arch {
if !arch.is_compatible(key.arch) {
return false;
}
}

if let Some(libc) = &self.libc {
if key.libc != *libc {
return false;
}
}

if let Some(implementation) = &self.implementation {
if key.implementation != LenientImplementationName::from(*implementation) {
return false;
}
}

// If we don't allow pre-releases, don't match a key with a pre-release tag
if !self.allows_prereleases() && key.prerelease.is_some() {
return false;
Expand Down
19 changes: 5 additions & 14 deletions crates/uv-python/src/managed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,15 @@ impl ManagedPythonInstallations {
pub fn find_matching_current_platform(
&self,
) -> Result<impl DoubleEndedIterator<Item = ManagedPythonInstallation>, Error> {
let platform_key = platform_key_from_env()?;
let os = Os::from_env();
let arch = Arch::from_env();
let libc = Libc::from_env()?;

let iter = ManagedPythonInstallations::from_settings(None)?
.find_all()?
.filter(move |installation| {
installation
.path
.file_name()
.map(OsStr::to_string_lossy)
.is_some_and(|filename| filename.ends_with(&platform_key))
let key = installation.key();
key.os == os && arch.is_compatible(key.arch) && key.libc == libc
});

Ok(iter)
Expand Down Expand Up @@ -585,14 +584,6 @@ impl ManagedPythonInstallation {
}
}

/// Generate a platform portion of a key from the environment.
fn platform_key_from_env() -> Result<String, Error> {
let os = Os::from_env();
let arch = Arch::from_env();
let libc = Libc::from_env()?;
Ok(format!("{os}-{arch}-{libc}").to_lowercase())
}

impl fmt::Display for ManagedPythonInstallation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
Expand Down
82 changes: 81 additions & 1 deletion crates/uv-python/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ pub enum Error {
UnsupportedVariant(String, String),
}

/// Architecture variants, e.g., with support for different instruction sets
/// Architecture variants supplementing the [`target_lexicon::Architecture`].
///
/// Currently these are only used for `x86_64` microarchitecture levels.
///
/// See the specification at <https://gitlab.com/x86-psABIs/x86-64-ABI>
#[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)]
pub enum ArchVariant {
/// Targets 64-bit Intel/AMD CPUs newer than Nehalem (2008).
Expand Down Expand Up @@ -99,11 +103,87 @@ impl Arch {
pub fn from_env() -> Self {
Self {
family: target_lexicon::HOST.architecture,
variant: ArchVariant::from_env(),
}
}

/// Is the current architecture compatible with `other`?
///
/// `other` can be a compatible microarchitecture with worse performance characteristics.
pub(crate) fn is_compatible(self, other: Self) -> bool {
self.family == other.family
&& ((self.variant.is_none() && other.variant.is_none())
|| self
.variant
.is_some_and(|variant| variant.is_compatible(other.variant)))
}

#[must_use]
pub fn without_variant(self) -> Self {
Self {
family: self.family,
variant: None,
}
}
}

impl ArchVariant {
/// Only Linux `x86_64` variants are published upstream at this time.
#[cfg(all(unix, target_arch = "x86_64"))]
pub fn from_env() -> Option<Self> {
if is_x86_feature_detected!("avx512f")
&& is_x86_feature_detected!("avx512bw")
&& is_x86_feature_detected!("avx512cd")
&& is_x86_feature_detected!("avx512dq")
&& is_x86_feature_detected!("avx512vl")
Comment on lines +134 to +138
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh no clue if we should check for all of these or not

{
Some(Self::V4)
} else if is_x86_feature_detected!("avx")
&& is_x86_feature_detected!("avx2")
&& is_x86_feature_detected!("bmi1")
&& is_x86_feature_detected!("bmi2")
&& is_x86_feature_detected!("f16c")
&& is_x86_feature_detected!("fma")
&& is_x86_feature_detected!("lzcnt")
&& is_x86_feature_detected!("movbe")
{
Some(Self::V3)
} else if is_x86_feature_detected!("cmpxchg16b")
&& is_x86_feature_detected!("popcnt")
&& is_x86_feature_detected!("sse3")
&& is_x86_feature_detected!("sse4.1")
&& is_x86_feature_detected!("sse4.2")
&& is_x86_feature_detected!("ssse3")
{
Some(Self::V2)
} else {
None
}
}

#[cfg(not(all(unix, target_arch = "x86_64")))]
pub fn from_env() -> Option<Self> {
None
}

/// Is the current variant compatible with `other`?
///
/// `other` can be a compatible variant with lower priority, however, it must be a subset of
/// `self`.
pub(crate) fn is_compatible(self, other: Option<Self>) -> bool {
let Some(other) = other else {
// `None` can be used on any architecture
return true;
};
match self {
Self::V2 => matches!(other, Self::V2),
Self::V3 => matches!(other, Self::V2 | Self::V3),
// If on V4, any variant can be used.
Self::V4 => true,
}
}
}

impl Display for Libc {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/tests/it/python_find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn python_find() {

// Request CPython 3.12 for the current platform
let os = Os::from_env();
let arch = Arch::from_env();
let arch = Arch::from_env().without_variant();

uv_snapshot!(context.filters(), context.python_find()
.arg(format!("cpython-3.12-{os}-{arch}"))
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/tests/it/python_pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ fn python_pin_resolve() {

// Request CPython 3.13 for the current platform
let os = Os::from_env();
let arch = Arch::from_env();
let arch = Arch::from_env().without_variant();

uv_snapshot!(context.filters(), context.python_pin().arg("--resolved")
.arg(format!("cpython-3.13-{os}-{arch}"))
Expand Down
Loading