Skip to content

Fix compiler panic with a large number of threads #132355

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Nov 4, 2024

Conversation

practicalrs
Copy link
Contributor

Hi,

This PR is an attempt to fix the problem described here #117638 using the solution suggested in this comment #117638 (comment)

Best regards,
Michal

@rustbot
Copy link
Collaborator

rustbot commented Oct 30, 2024

r? @jieyouxu

rustbot has assigned @jieyouxu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 30, 2024
@practicalrs
Copy link
Contributor Author

CC @matthiaskrgr @nnethercote

@jieyouxu
Copy link
Member

r? @SparrowLii since this is probably parallel frontend stuff

@rustbot rustbot assigned SparrowLii and unassigned jieyouxu Oct 30, 2024
@SparrowLii
Copy link
Member

That makes sense, thanks!
@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Oct 30, 2024

📌 Commit 818099c has been approved by SparrowLii

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 Oct 30, 2024
@fmease
Copy link
Member

fmease commented Oct 30, 2024

Please squash into one commit, this only touches 3 lines.
@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 30, 2024
@practicalrs
Copy link
Contributor Author

@fmease All good now?

@fmease
Copy link
Member

fmease commented Oct 30, 2024

Yes, thanks! @bors r=SparrowLii

@bors
Copy link
Collaborator

bors commented Oct 30, 2024

📌 Commit ef30dab has been approved by SparrowLii

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 30, 2024
@klensy
Copy link
Contributor

klensy commented Oct 30, 2024

Please no?

This should have some warning when max value actually reached.

@practicalrs
Copy link
Contributor Author

Please no?

This should have some warning when max value actually reached.

Hmm... maybe?

How to emit a warning for such a thing in the compiler?

@klensy
Copy link
Contributor

klensy commented Oct 30, 2024

Probably parsing can be left as it is, and actual max value checked here

pub fn threads(&self) -> usize {
self.opts.unstable_opts.threads
}

and emit_warn here (or early_warn)? Probably someone will suggest correct method.

@fmease
Copy link
Member

fmease commented Oct 30, 2024

@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 30, 2024
@practicalrs
Copy link
Contributor Author

Probably parsing can be left as it is, and actual max value checked here

I would add this warning on parsing.

I think I just need to revert to the first version and add this warning in else.

@fmease @SparrowLii What do you think?

@klensy
Copy link
Contributor

klensy commented Oct 30, 2024

Better place:

if unstable_opts.threads == 0 {
early_dcx.early_fatal("value for threads must be a positive non-zero integer");
}

@practicalrs
Copy link
Contributor Author

Warning will lie for rustc -Zhtreads=0 if pc actually have > 256 cores.

So. Should I use std::thread::available_parallelism() to determinate the proper value?

@klensy
Copy link
Contributor

klensy commented Oct 31, 2024

Maybe apply min value after that match in parse_threads?

@practicalrs
Copy link
Contributor Author

@klensy

Maybe this makes more sense?

@rust-log-analyzer

This comment has been minimized.

@klensy
Copy link
Contributor

klensy commented Nov 1, 2024

More like

pub fn parse_threads(slot: &mut usize, v: Option<&str>) -> bool {
    let ret = match v.and_then(|s| s.parse().ok()) {
        Some(0) => {
            *slot = std::thread::available_parallelism().map_or(1, NonZero::<usize>::get);
            true
        }
        Some(i) => {
            *slot = i;
            true
        }
        None => false,
    };
    // We want to cap the number of threads here to avoid large numbers like 999999 and compiler panics.
    // This solution was suggested here https://github.com/rust-lang/rust/issues/117638#issuecomment-1800925067
    *slot = slot.clone().min(MAX_THREADS_CAP);
    ret
}

and left things in config.rs as in previous try with simple warning.

@practicalrs
Copy link
Contributor Author

@klensy Done.

@klensy
Copy link
Contributor

klensy commented Nov 4, 2024

@rustbot review

for official reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 4, 2024
@SparrowLii
Copy link
Member

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Nov 4, 2024

📌 Commit 7591eb6 has been approved by SparrowLii

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 Nov 4, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 4, 2024
Fix compiler panic with a large number of threads

Hi,

This PR is an attempt to fix the problem described here rust-lang#117638 using the solution suggested in this comment rust-lang#117638 (comment)

Best regards,
Michal
@bjoernager
Copy link
Contributor

Wouldn't we want to be able to use more threads on future machines? I feel having this limit hard-coded could pose a problem, or maybe I'm missing something?

@practicalrs
Copy link
Contributor Author

practicalrs commented Nov 4, 2024

Wouldn't we want to be able to use more threads on future machines? I feel having this limit hard-coded could pose a problem, or maybe I'm missing something?

When you use it like -Zthreads=0, then you should have a value from std::thread::available_parallelism()

In one iteration of the PR I also tried to use it for Some(i) match, but @klensy suggested a different solution.

@klensy
Copy link
Contributor

klensy commented Nov 4, 2024

The way it was implemented, value was taken from available_parallelism() but still warned that it was capped at 256, even if it actually don't.

@practicalrs
Copy link
Contributor Author

The way it was implemented, value was taken from available_parallelism() but still warned that it was capped at 256, even if it actually don't.

I tested it on my machine and it was capped properly to 6.

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 4, 2024
…iaskrgr

Rollup of 4 pull requests

Successful merges:

 - rust-lang#132153 (Stabilise `const_char_encode_utf16`.)
 - rust-lang#132355 (Fix compiler panic with a large number of threads)
 - rust-lang#132486 (No need to instantiate binder in `confirm_async_closure_candidate`)
 - rust-lang#132594 (Subtree update of `rust-analyzer`)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 4, 2024
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#132355 (Fix compiler panic with a large number of threads)
 - rust-lang#132486 (No need to instantiate binder in `confirm_async_closure_candidate`)
 - rust-lang#132544 (Use backticks instead of single quotes for library feature names in diagnostics)
 - rust-lang#132559 (find the generic container rather than simply looking up for the assoc with const arg)
 - rust-lang#132579 (add rustc std workspace crate sources)
 - rust-lang#132583 (Suggest creating unary tuples when types don't match a trait)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 31ad4e4 into rust-lang:master Nov 4, 2024
6 checks passed
@rustbot rustbot added this to the 1.84.0 milestone Nov 4, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Nov 4, 2024
Rollup merge of rust-lang#132355 - practicalrs:fix_117638, r=SparrowLii

Fix compiler panic with a large number of threads

Hi,

This PR is an attempt to fix the problem described here rust-lang#117638 using the solution suggested in this comment rust-lang#117638 (comment)

Best regards,
Michal
@practicalrs
Copy link
Contributor Author

@SparrowLii @matthiaskrgr Can #117638 be closed now?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
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