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 noundef attribute to &T, &mut T, Box<T>, bool #93670

Merged
merged 5 commits into from
Feb 13, 2022

Conversation

erikdesjardins
Copy link
Contributor

@erikdesjardins erikdesjardins commented Feb 5, 2022

This doesn't handle char because it's a bit awkward to distinguish it from u32 at this point in codegen.

Note that this does not change whether or not it is UB for &, &mut, or Box to point to undef. It only applies to the pointer itself, not the pointed-to memory.

Fixes (partially) #74378.

r? @nikic cc @RalfJung

This doesn't handle `char` because it's a bit awkward to distinguish it
from u32 at this point in codegen.

Note that for some types (like `&Struct` and `&mut Struct`),
we already apply `dereferenceable`, which implies `noundef`,
so the IR does not change.
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Feb 5, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 5, 2022
@nikic
Copy link
Contributor

nikic commented Feb 10, 2022

@bors r+ rollup=never Extra attributes might have a compile-time impact.

@bors
Copy link
Contributor

bors commented Feb 10, 2022

📌 Commit 75ed7de has been approved by nikic

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 10, 2022
@bors
Copy link
Contributor

bors commented Feb 12, 2022

⌛ Testing commit 75ed7de with merge a78ffefbccd8c5b80e5532af443c28f122a7716b...

@bors
Copy link
Contributor

bors commented Feb 12, 2022

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Feb 12, 2022
@rust-log-analyzer

This comment has been minimized.

…n on any host platform (with the right llvm components)
@nikic
Copy link
Contributor

nikic commented Feb 12, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Feb 12, 2022

📌 Commit 4013077 has been approved by nikic

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 12, 2022
@bors
Copy link
Contributor

bors commented Feb 13, 2022

⌛ Testing commit 4013077 with merge 5c30d65...

@bors
Copy link
Contributor

bors commented Feb 13, 2022

☀️ Test successful - checks-actions
Approved by: nikic
Pushing 5c30d65 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Feb 13, 2022
@bors bors merged commit 5c30d65 into rust-lang:master Feb 13, 2022
@rustbot rustbot added this to the 1.60.0 milestone Feb 13, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (5c30d65): comparison url.

Summary: This benchmark run did not return any relevant results. 54 results were found to be statistically significant but too small to be relevant.

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

@rustbot label: -perf-regression

@erikdesjardins erikdesjardins deleted the noundef branch February 13, 2022 07:54
@bjorn3
Copy link
Member

bjorn3 commented Feb 13, 2022

This should likely have been labeled as perf regression.

@erikdesjardins
Copy link
Contributor Author

Indeed. Cachegrind indicates that it's simply due to the overhead of inserting the attribute itself:

--------------------------------------------------------------------------------
Ir         
--------------------------------------------------------------------------------
18,075,777  PROGRAM TOTALS

--------------------------------------------------------------------------------
Ir          file:function
--------------------------------------------------------------------------------
22,240,315  ???:llvm::AttributeList::addAttribute(llvm::LLVMContext&, unsigned int, llvm::Attribute) const
-4,727,728  ???:llvm::Attribute::hasParentContext(llvm::LLVMContext&) const
 4,724,839  ???:<alloc::vec::drain_filter::DrainFilter<rustc_infer::traits::Obligation<rustc_middle::ty::Predicate>, rustc_trait_selection::traits::project::opt_normalize_projection_type::{closure
-4,400,231  ???:rustc_trait_selection::traits::project::opt_normalize_projection_type
 4,293,104  ???:(anonymous namespace)::Verifier::verifyFunctionAttrs(llvm::FunctionType*, llvm::AttributeList, llvm::Value const*, bool)
 3,962,446  ???:llvm::FoldingSet<llvm::AttributeImpl>::NodeEquals(llvm::FoldingSetBase const*, llvm::FoldingSetBase::Node*, llvm::FoldingSetNodeID const&, unsigned int, llvm::FoldingSetNodeID&)
-2,954,144  ???:(anonymous namespace)::Verifier::visitMDNode(llvm::MDNode const&, (anonymous namespace)::Verifier::AreDebugLocsAllowed)
 2,299,517  ???:llvm::Attribute::get(llvm::LLVMContext&, llvm::Attribute::AttrKind, unsigned long)
-2,273,112  ???:llvm::FPPassManager::runOnFunction(llvm::Function&)
 1,577,122  ./string/../sysdeps/x86_64/multiarch/memcmp-avx2-movbe.S:__memcmp_avx2_movbe
-1,401,896  ./nptl/../nptl/pthread_mutex_trylock.c:pthread_mutex_trylock
-1,216,700  ???:llvm::AnalysisResolver::getAnalysisIfAvailable(void const*) const
  -916,235  ???:llvm::MCAssembler::layout(llvm::MCAsmLayout&)
...

LLVM 14 includes some performance improvements to AttrBuilder and attribute-related code, so after #93577 is merged I'll open a revert PR to see if the impact is still significant.

@Mark-Simulacrum Mark-Simulacrum added the perf-regression Performance regression. label Feb 13, 2022
erikdesjardins added a commit to erikdesjardins/rust that referenced this pull request Feb 17, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 26, 2022
At opt-level=0, apply only ABI-affecting attributes to functions

This should provide a small perf improvement for debug builds,
and should more than cancel out the perf regression from adding noundef (rust-lang#93670 (comment), rust-lang#94106).

r? `@nikic`
@erikdesjardins
Copy link
Contributor Author

Regression after LLVM 14 investigated in #94106, which reverts this PR, giving a potential savings of:

Benchmark & Profile Scenario % Change Significance Factor ?
coercions debug incr-patched: add static arr item -2.79%? 1.40x
cargo debug full -0.65%? 1.17x
webrender debug full -0.61%? 1.05x
regression-31157 debug incr-patched: println -0.57%? 1.00x
regression-31157 debug full -0.54%? 1.16x
cargo debug incr-full -0.53%? 1.34x
webrender debug incr-full -0.49%? 1.26x
piston-image debug full -0.47%? 1.67x
encoding debug full -0.47%? 1.43x
clap-rs debug full -0.47%? 1.17x
piston-image debug incr-full -0.43%? 1.54x
issue-46449 debug incr-patched: static str 6144 -0.42%? 1.03x
regression-31157 debug incr-full -0.41%? 1.03x
regex debug incr-full -0.41%? 1.20x
ripgrep debug full -0.39%? 1.03x
cranelift-codegen debug incr-full -0.38%? 1.58x
tokio-webpush-simple debug incr-full -0.36%? 1.19x
issue-46449 debug full -0.36%? 1.05x
cranelift-codegen debug full -0.35%? 1.19x
style-servo opt incr-patched: b9b3e592dd cherry picked -0.33%? 1.01x
syn debug incr-full -0.29%? 1.24x
webrender-wrench opt incr-patched: println -0.29%? 1.08x
cargo opt incr-full -0.29%? 1.02x
cargo opt incr-patched: println -0.28%? 1.18x


All of that is due to the cost of adding the noundef attribute, and almost entirely in debug builds, so I landed #94127, which makes us stop adding noundef (and related optimization-only attributes) in debug builds, giving us an actual savings of:

Benchmark & Profile Scenario % Change Significance Factor ?
regex debug incr-patched: Job -3.92%? 8.19x
regex debug incr-patched: sparse set -3.63%? 8.48x
regex debug full -3.09%? 5.65x
regex debug incr-full -2.95%? 6.73x
issue-46449 debug incr-patched: static str 6144 -1.48%? 4.64x
cargo debug incr-full -1.36%? 3.56x
cargo debug full -1.30%? 2.54x
regression-31157 debug full -1.24%? 2.03x
issue-46449 debug incr-patched: empty 3072 -1.20%? 2.91x
tokio-webpush-simple debug full -1.16%? 2.80x
webrender debug full -1.11%? 1.77x
clap-rs debug full -1.06%? 2.66x
regression-31157 debug incr-full -1.05%? 2.20x
piston-image debug full -1.05%? 3.14x
issue-46449 debug incr-patched: u8 3072 -1.04%? 2.54x
issue-46449 debug incr-patched: u32 3072 -1.02%? 2.79x
webrender debug incr-full -1.02%? 2.48x
ripgrep debug incr-full -1.02%? 2.97x
piston-image debug incr-full -1.01%? 3.46x
regression-31157 debug incr-patched: println -1.00%? 1.62x
cranelift-codegen debug full -0.98%? 3.26x
tokio-webpush-simple debug incr-full -0.98%? 3.17x
issue-46449 debug full -0.96%? 2.40x
cargo debug incr-patched: println -0.85%? 1.70x
style-servo debug full -0.85%? 1.56x
style-servo debug incr-full -0.83%? 1.95x
webrender-wrench debug full -0.80%? 1.73x
cranelift-codegen debug incr-full -0.79%? 2.76x
issue-46449 debug incr-full -0.78%? 2.10x
syn debug full -0.78%? 1.74x
hyper-2 debug full -0.74%? 2.01x
ripgrep debug full -0.73%? 1.73x
syn debug incr-full -0.71%? 2.68x
issue-46449 debug incr-patched: io error 6144 -0.70%? 1.88x
regex debug incr-patched: println -0.68%? 1.53x
webrender-wrench debug incr-full -0.68%? 1.79x
hyper-2 debug incr-full -0.58%? 1.83x
encoding debug incr-full -0.56%? 2.10x
encoding debug full -0.53%? 1.64x


...which (more than) completely eliminates the regression in debug builds.

The remaining potential savings from reverting this PR are only in opt builds, specifically:

style-servo      opt incr-patched -0.33%
webrender-wrench opt incr-patched -0.29%
cargo            opt incr-full    -0.29%
cargo            opt incr-patched -0.28%

The is just due to the overhead of inserting the noundef attribute itself (which is expected). Given the size and limited number of regressions, I think this is an acceptable cost. I am also working on #94221, which should make our attribute manipulation faster, and erase the last few regressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants