-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
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
Conversation
My worry is that conditionally setting target-cpu via command line could start overwriting build caches (if homebrew uses them). This should be fine if there are no caches (:Fr:), or different cache folders based on version. Otherwise rust-lang/cargo#7773 is a problem. |
Any idea how to test? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work so far!
# Override | ||
# Mirrors version-dependent logic of oldest_cpu | ||
sig { params(version: T.nilable(Version)).returns(String) } | ||
def self.rustflags_target_cpu(version = nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When would version
be passed through here rather than just use the current MacOS.version
?
MacOS.version | ||
end | ||
if Hardware::CPU.intel? && version >= :mojave | ||
"'-Ctarget-cpu=nehalem'" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"'-Ctarget-cpu=nehalem'" | |
"--codegen target-cpu=nehalem" |
- Let callers handle the quoting, if it's needed.
- Use the long version of the flag.
- I'd still rather use
oldest_cpu
here instead of hard-codingnehalem
.
Library/Homebrew/formula.rb
Outdated
@@ -1543,7 +1543,8 @@ def std_configure_args | |||
# Standard parameters for cargo builds. | |||
sig { params(root: T.any(String, Pathname), path: String).returns(T::Array[T.any(String, Pathname)]) } | |||
def std_cargo_args(root: prefix, path: ".") | |||
["--locked", "--root", root, "--path", path] | |||
["--locked", "--root", root, "--path", path, "--config", | |||
"build.rustflags=[#{Hardware.rustflags_target_cpu}]"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this interact with RUSTFLAGS
set in the environment? Does it override them? Append to them?
I think we may prefer to set RUSTFLAGS
in the environment as well as/instead of doing this, since not all rustc
invocations happen through a cargo
call that uses std_cargo_args
. See, for example, gstreamer
, or the many formulae that have a cryptography
resource.
OK — new, prefered method: set RUSTFLAGS through a environment variable. I think I'll add it to std env for now. I'm currently sanitising and overriding RUSTFLAGS.
Current review suggestions:
|
Yes.
Do the same thing as the other similar flags (e.g.
You can try
Maybe, but
Because having the same constants in multiple places (even when it they're not far away from each other) just isn't a good idea. The hypothetical situations you're worried about haven't arisen yet, and I don't think they will. If they do, then we can handle it then. |
I now use oldest_cpu. I still fallback to rust defaults where I can. |
Library/Homebrew/hardware.rb
Outdated
sig { returns(String) } | ||
def rustflags_target_cpu | ||
CPU.rust_optimisation_flags.fetch(oldest_cpu, "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Library/Homebrew/hardware.rb
Outdated
native: "-Ctarget-cpu=native", | ||
nehalem: "-Ctarget-cpu=nehalem", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Library/Homebrew/hardware.rb
Outdated
# 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good so far!
Co-authored-by: Mike McQuaid <mike@mikemcquaid.com>
Looks like the CI failure is due to https://http://sequelpro.com/ being down. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me:
- if it works for you in local testing
- once @carlocab is also ✅
|
I'm currently neck-deep in Homebrew/homebrew-core#134251, so I haven't had a chance to take a proper look here. Will come back to it next week. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks; I'm happy with how this is looking currently.
Bonus points for the following (but you can save it for a future PR):
- Support for
brew install --build-bottle --bottle-arch=<custom-arch>
- An
ENV.rustflags
accessor, just like what we have forENV.cflags
,ENV.ldflags
, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much for your first contribution (hopefully of many)! Without people like you submitting PRs we couldn't run this project. You rock, @Tokarak!
Hi, all. According to The Cargo Book, setting the This may cause some packages to fail to compile if they're using https://github.com/risingwavelabs/risingwave/blob/f03dbc62c4e07fd16164a6b81219d3ffd5f86fa9/.cargo/config.toml#L32-L36 |
I suggest doing ENV.delete "RUSTFLAGS" in the |
brew style
with your changes locally?brew typecheck
with your changes locally?brew tests
with your changes locally?Closes: #15530