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

Apply cpu-optimisation to Rust projects #15544

Merged
merged 17 commits into from
Jul 4, 2023
1 change: 1 addition & 0 deletions Library/Homebrew/extend/ENV/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module SharedEnvExtension
CMAKE_PREFIX_PATH CMAKE_INCLUDE_PATH CMAKE_FRAMEWORK_PATH
GOBIN GOPATH GOROOT PERL_MB_OPT PERL_MM_OPT
LIBRARY_PATH LD_LIBRARY_PATH LD_PRELOAD LD_RUN_PATH
RUSTFLAGS
].freeze
private_constant :SANITIZED_VARS

Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/extend/ENV/std.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_a
self["PKG_CONFIG_LIBDIR"] = determine_pkg_config_libdir

self["MAKEFLAGS"] = "-j#{make_jobs}"
self["RUSTFLAGS"] = Hardware.rustflags_target_cpu

if HOMEBREW_PREFIX.to_s != "/usr/local"
# /usr/local is already an -isystem and -L directory so we skip it
Expand Down
1 change: 1 addition & 0 deletions Library/Homebrew/extend/ENV/super.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_a

self["HOMEBREW_ENV"] = "super"
self["MAKEFLAGS"] ||= "-j#{determine_make_jobs}"
self["RUSTFLAGS"] = Hardware.rustflags_target_cpu
self["PATH"] = determine_path
self["PKG_CONFIG_PATH"] = determine_pkg_config_path
self["PKG_CONFIG_LIBDIR"] = determine_pkg_config_libdir
Expand Down
17 changes: 17 additions & 0 deletions Library/Homebrew/hardware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ def optimization_flags
end
alias generic_optimization_flags optimization_flags

# Only give values where it is an improvement over rust cpu defaults
# Rust already defaults to the oldest supported cpu for each target-triple
# Including apple-m1 since Rust 1.71 for aarch64-apple-darwin.
Copy link
Member

Choose a reason for hiding this comment

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

I'd really rather be explicit here rather than bake in assumptions about the behaviour of rustc. None of us track the development of rustc that carefully, so if they decide to bump the minimum of core2 before we do, then this method will no longer behave as expected, and that's likely to go unnoticed for some time.

The reverse (i.e. if we decide to bump the minimum of core2 before Rust does) is also an issue, albeit a smaller one.

def rust_optimisation_flags
@rust_optimisation_flags ||= {
native: "-Ctarget-cpu=native",
nehalem: "-Ctarget-cpu=nehalem",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
native: "-Ctarget-cpu=native",
nehalem: "-Ctarget-cpu=nehalem",
native: "--codegen target-cpu=native",
nehalem: "--codegen target-cpu=nehalem",

Please use the long flags for better readability.

}.freeze
end

sig { returns(Symbol) }
def arch_32_bit
if arm?
Expand Down Expand Up @@ -214,6 +224,13 @@ def oldest_cpu(_version = nil)
end
end
alias generic_oldest_cpu oldest_cpu

# Returns a _full_ rustflag to set target cpu, if necessary;
# Defaults to empty string
sig { returns(String) }
def rustflags_target_cpu
CPU.rust_optimisation_flags.fetch(oldest_cpu, "")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
sig { returns(String) }
def rustflags_target_cpu
CPU.rust_optimisation_flags.fetch(oldest_cpu, "")
sig { returns(T.nilable(String)) }
def rustflags_target_cpu
CPU.rust_optimisation_flags.fetch(oldest_cpu, nil)

Also: we should try to support users doing something like

brew install --build-bottle --bottle-arch=ivybridge

which this doesn't currently do.

end
end
end

Expand Down