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

Don't distinguish Debuginfo::None and Debuginfo::Explicit(None) #12205

Merged
merged 2 commits into from
May 31, 2023

Conversation

jyn514
Copy link
Member

@jyn514 jyn514 commented May 30, 2023

Previously, Debuginfo::None meant "don't pass -C debuginfo" and Explicit(None) meant "-C debuginfo=0", which occasionally led to caching bugs where cargo would sometimes pass -C debuginfo=0 and sometimes not. There are no such bugs currently that we know of, but representing them the same within cargo avoids the possibility of the bug popping up again in the future.

I tested the with_stderr_does_not_contain tests with this diff to ensure they did not pass:

diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs
index 55ec17182..c186dd00a 100644
--- a/src/cargo/core/compiler/mod.rs
+++ b/src/cargo/core/compiler/mod.rs
@@ -1073,9 +1073,7 @@ fn build_base_args(

     let debuginfo = debuginfo.into_inner();
     // Shorten the number of arguments if possible.
-    if debuginfo != TomlDebugInfo::None {
         cmd.arg("-C").arg(format!("debuginfo={}", debuginfo));
-    }

     cmd.args(unit.pkg.manifest().lint_rustflags());
     if !rustflags.is_empty() {

What does this PR try to resolve?

This reduces the likelihood of a latent caching bug. See #12163 (comment) for details.

How should we test and review this PR?

Run cargo test. To verify the with_stderr_does_not_contain tests are actually doing something, apply the diff in the description above and rerun cargo test. You should see 5 failures in the profiles and profile_targets modules.

Additional information

The only subtle bit of logic this changes is

// For build dependencies, we usually don't need debuginfo, and
// removing it will compile faster. However, that can conflict with
// a unit graph optimization, reusing units that are shared between
// build dependencies and runtime dependencies: when the runtime
// target is the same as the build host, we only need to build a
// dependency once and reuse the results, instead of building twice.
// We defer the choice of the debuginfo level until we can check if
// a unit is shared. If that's the case, we'll use the deferred value
// below so the unit can be reused, otherwise we can avoid emitting
// the unit's debuginfo.
profile.debuginfo = DebugInfo::Deferred(profile.debuginfo.into_inner());
I think this is the same behavior as before: the only time profile.debuginfo.to_option() would be None is when it was set to Debuginfo::None. In that case today, we'll now set it to Deferred(None). The only time that can be modified is in
// If this is a build dependency, and it's not shared with runtime dependencies, we can weaken
// its debuginfo level to optimize build times. We do nothing if it's an artifact dependency,
// as it and its debuginfo may end up embedded in the main program.
if unit_is_for_host
&& to_host.is_some()
&& profile.debuginfo.is_deferred()
&& !unit.artifact.is_true()
{
// We create a "probe" test to see if a unit with the same explicit debuginfo level exists
// in the graph. This is the level we'd expect if it was set manually or the default value
// set by a profile for a runtime dependency: its canonical value.
let canonical_debuginfo = profile.debuginfo.finalize();
let mut canonical_profile = profile.clone();
canonical_profile.debuginfo = canonical_debuginfo;
let unit_probe = interner.intern(
&unit.pkg,
&unit.target,
canonical_profile,
to_host.unwrap(),
unit.mode,
unit.features.clone(),
unit.is_std,
unit.dep_hash,
unit.artifact,
unit.artifact_target_for_features,
);
// We can now turn the deferred value into its actual final value.
profile.debuginfo = if unit_graph.contains_key(&unit_probe) {
// The unit is present in both build time and runtime subgraphs: we canonicalize its
// level to the other unit's, thus ensuring reuse between the two to optimize build times.
canonical_debuginfo
} else {
// The unit is only present in the build time subgraph, we can weaken its debuginfo
// level to optimize build times.
canonical_debuginfo.weaken()
}
}
where it will be canonicalized to Resolved(None) regardless of whether the unit graph has a matching unit in the runtime graph or not.

I think there are existing tests for this behavior but I'm not sure.

r? @weihanglo

@rustbot rustbot added A-build-execution Area: anything dealing with executing the compiler A-profiles Area: profiles S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 30, 2023
Previously, `Debuginfo::None` meant "don't pass -C debuginfo" and `Explicit(None)` meant
"-C debuginfo=0", which occasionally led to caching bugs where cargo would sometimes pass
`-C debuginfo=0` and sometimes not. There are no such bugs currently that we know of, but
representing them the same within cargo avoids the possibility of the bug popping up again in the
future.

I tested the `with_stderr_does_not_contain_tests` with this diff to ensure they did not pass:
```diff
diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs
index 55ec17182..c186dd00a 100644
--- a/src/cargo/core/compiler/mod.rs
+++ b/src/cargo/core/compiler/mod.rs
@@ -1073,9 +1073,7 @@ fn build_base_args(

     let debuginfo = debuginfo.into_inner();
     // Shorten the number of arguments if possible.
-    if debuginfo != TomlDebugInfo::None {
         cmd.arg("-C").arg(format!("debuginfo={}", debuginfo));
-    }

     cmd.args(unit.pkg.manifest().lint_rustflags());
     if !rustflags.is_empty() {
```
@jyn514 jyn514 force-pushed the simplify-debuginfo branch from c35fdbf to e2eacc6 Compare May 30, 2023 22:06
Copy link
Member

@weihanglo weihanglo left a comment

Choose a reason for hiding this comment

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

Beautiful!!! Thank you!
Only one minor doc comment issue left.

BTW I found split-debuginfo is also redundant when debuginfo != TomlDebugInfo::None. Just FYI. No need to change.

if let Some(split) = split_debuginfo {
if cx
.bcx
.target_data
.info(unit.kind)
.supports_debuginfo_split(split)
{
cmd.arg("-C").arg(format!("split-debuginfo={}", split));
}
}

src/cargo/core/profiles.rs Outdated Show resolved Hide resolved
@Mark-Simulacrum
Copy link
Member

Do we test for / does this break setting debug info via RUSTFLAGS? That use case is probably not well supported, but it's sometimes more convenient than alternatives (e.g., because of target-dependent debuginfo which I'm not sure is otherwise file configuration placeable).

@jyn514
Copy link
Member Author

jyn514 commented May 30, 2023

I would be extremely surprised if that worked before this PR; the reason I made #11958 originally is I couldn't figure out how to do it through RUSTFLAGS in bootstrap.

That said, this passes -C debuginfo strictly less than before, so I wouldn't expect it to hurt the situation.

@weihanglo
Copy link
Member

Do we test for / does this break setting debug info via RUSTFLAGS?

Thanks for calling out! RUSTFLAGS, build.rustflags and target.<triple>.rustflags should take precedence over profile settings. I don't think this break anything. Profile setting should have the lowest priority.

profile.debuginfo:

cmd.arg("-C").arg(format!("debuginfo={}", debuginfo));

RUStFLAGS:

rustc.args(cx.bcx.rustflags_args(unit));

@weihanglo
Copy link
Member

Thanks!

@bors r+

@bors
Copy link
Contributor

bors commented May 30, 2023

📌 Commit f74c8df has been approved by weihanglo

It is now in the queue for this repository.

@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 May 30, 2023
@bors
Copy link
Contributor

bors commented May 30, 2023

⌛ Testing commit f74c8df with merge c0f36b8...

@bors
Copy link
Contributor

bors commented May 31, 2023

☀️ Test successful - checks-actions
Approved by: weihanglo
Pushing c0f36b8 to master...

@bors bors merged commit c0f36b8 into rust-lang:master May 31, 2023
@jyn514 jyn514 deleted the simplify-debuginfo branch May 31, 2023 01:05
weihanglo added a commit to weihanglo/cargo that referenced this pull request May 31, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 4, 2023
Update cargo

14 commits in f7b95e31642e09c2b6eabb18ed75007dda6677a0..b0fa79679e717cd077b7fc0fa4166f47107f1ba9
2023-05-30 19:25:02 +0000 to 2023-06-03 14:19:48 +0000
- Emit error when users try to use a toolchain via the `add` or `install` command (rust-lang/cargo#12226)
- Support "default" option for `build.jobs` (rust-lang/cargo#12222)
- Fix typo in changelog (rust-lang/cargo#12227)
- chore: Sort `-Z` flags match statement (rust-lang/cargo#12223)
- Update curl-sys (rust-lang/cargo#12218)
- Bump to 0.73.0; update changelog (rust-lang/cargo#12219)
- refactor: housekeeping for 1.70.0 (rust-lang/cargo#12217)
- nit: fix typo in changelog for 1.70 (rust-lang/cargo#12215)
- Remove a noop `.clone` (rust-lang/cargo#12213)
- refactor: compiler invocations (rust-lang/cargo#12211)
- cargo clean: use `remove_dir_all` (rust-lang/cargo#11442)
- Add a small note about indexes ignoring SemVer build metadata. (rust-lang/cargo#12206)
- Revert "chore: detect the channel a PR wants to merge into" (rust-lang/cargo#12204)
- Don't distinguish `Debuginfo::None` and `Debuginfo::Explicit(None)` (rust-lang/cargo#12205)

r? `@ghost`
RalfJung pushed a commit to RalfJung/miri that referenced this pull request Jun 11, 2023
Update cargo

14 commits in f7b95e31642e09c2b6eabb18ed75007dda6677a0..b0fa79679e717cd077b7fc0fa4166f47107f1ba9
2023-05-30 19:25:02 +0000 to 2023-06-03 14:19:48 +0000
- Emit error when users try to use a toolchain via the `add` or `install` command (rust-lang/cargo#12226)
- Support "default" option for `build.jobs` (rust-lang/cargo#12222)
- Fix typo in changelog (rust-lang/cargo#12227)
- chore: Sort `-Z` flags match statement (rust-lang/cargo#12223)
- Update curl-sys (rust-lang/cargo#12218)
- Bump to 0.73.0; update changelog (rust-lang/cargo#12219)
- refactor: housekeeping for 1.70.0 (rust-lang/cargo#12217)
- nit: fix typo in changelog for 1.70 (rust-lang/cargo#12215)
- Remove a noop `.clone` (rust-lang/cargo#12213)
- refactor: compiler invocations (rust-lang/cargo#12211)
- cargo clean: use `remove_dir_all` (rust-lang/cargo#11442)
- Add a small note about indexes ignoring SemVer build metadata. (rust-lang/cargo#12206)
- Revert "chore: detect the channel a PR wants to merge into" (rust-lang/cargo#12204)
- Don't distinguish `Debuginfo::None` and `Debuginfo::Explicit(None)` (rust-lang/cargo#12205)

r? `@ghost`
@ehuss ehuss added this to the 1.72.0 milestone Jun 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-build-execution Area: anything dealing with executing the compiler A-profiles Area: profiles S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants