-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Ensure that static initializers are acyclic for NVPTX #150569
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
Merged
rust-bors
merged 1 commit into
rust-lang:main
from
kulst:check_static_initializer_acyclic
Jan 8, 2026
+262
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| //! Checks that need to operate on the entire mono item graph | ||
| use rustc_middle::mir::mono::MonoItem; | ||
| use rustc_middle::ty::TyCtxt; | ||
|
|
||
| use crate::collector::UsageMap; | ||
| use crate::graph_checks::statics::check_static_initializers_are_acyclic; | ||
|
|
||
| mod statics; | ||
|
|
||
| pub(super) fn target_specific_checks<'tcx, 'a, 'b>( | ||
| tcx: TyCtxt<'tcx>, | ||
| mono_items: &'a [MonoItem<'tcx>], | ||
| usage_map: &'b UsageMap<'tcx>, | ||
| ) { | ||
| if tcx.sess.target.options.static_initializer_must_be_acyclic { | ||
| check_static_initializers_are_acyclic(tcx, mono_items, usage_map); | ||
| } | ||
| } | ||
115 changes: 115 additions & 0 deletions
115
compiler/rustc_monomorphize/src/graph_checks/statics.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| use rustc_data_structures::fx::FxIndexSet; | ||
| use rustc_data_structures::graph::scc::Sccs; | ||
| use rustc_data_structures::graph::{DirectedGraph, Successors}; | ||
| use rustc_data_structures::unord::UnordMap; | ||
| use rustc_hir::def_id::DefId; | ||
| use rustc_index::{Idx, IndexVec, newtype_index}; | ||
| use rustc_middle::mir::mono::MonoItem; | ||
| use rustc_middle::ty::TyCtxt; | ||
|
|
||
| use crate::collector::UsageMap; | ||
| use crate::errors; | ||
|
|
||
| #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] | ||
| struct StaticNodeIdx(usize); | ||
|
|
||
| impl Idx for StaticNodeIdx { | ||
| fn new(idx: usize) -> Self { | ||
| Self(idx) | ||
| } | ||
|
|
||
| fn index(self) -> usize { | ||
| self.0 | ||
| } | ||
| } | ||
|
|
||
| impl From<usize> for StaticNodeIdx { | ||
| fn from(value: usize) -> Self { | ||
| StaticNodeIdx(value) | ||
| } | ||
| } | ||
|
|
||
| newtype_index! { | ||
| #[derive(Ord, PartialOrd)] | ||
| struct StaticSccIdx {} | ||
| } | ||
|
|
||
| // Adjacency-list graph for statics using `StaticNodeIdx` as node type. | ||
| // We cannot use `DefId` as the node type directly because each node must be | ||
| // represented by an index in the range `0..num_nodes`. | ||
| struct StaticRefGraph<'a, 'b, 'tcx> { | ||
| // maps from `StaticNodeIdx` to `DefId` and vice versa | ||
| statics: &'a FxIndexSet<DefId>, | ||
| // contains for each `MonoItem` the `MonoItem`s it uses | ||
| used_map: &'b UnordMap<MonoItem<'tcx>, Vec<MonoItem<'tcx>>>, | ||
| } | ||
|
|
||
| impl<'a, 'b, 'tcx> DirectedGraph for StaticRefGraph<'a, 'b, 'tcx> { | ||
| type Node = StaticNodeIdx; | ||
|
|
||
| fn num_nodes(&self) -> usize { | ||
| self.statics.len() | ||
| } | ||
| } | ||
|
|
||
| impl<'a, 'b, 'tcx> Successors for StaticRefGraph<'a, 'b, 'tcx> { | ||
| fn successors(&self, node_idx: StaticNodeIdx) -> impl Iterator<Item = StaticNodeIdx> { | ||
| let def_id = self.statics[node_idx.index()]; | ||
| self.used_map[&MonoItem::Static(def_id)].iter().filter_map(|&mono_item| match mono_item { | ||
| MonoItem::Static(def_id) => self.statics.get_index_of(&def_id).map(|idx| idx.into()), | ||
| _ => None, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| pub(super) fn check_static_initializers_are_acyclic<'tcx, 'a, 'b>( | ||
| tcx: TyCtxt<'tcx>, | ||
| mono_items: &'a [MonoItem<'tcx>], | ||
| usage_map: &'b UsageMap<'tcx>, | ||
| ) { | ||
| // Collect statics | ||
| let statics: FxIndexSet<DefId> = mono_items | ||
| .iter() | ||
| .filter_map(|&mono_item| match mono_item { | ||
| MonoItem::Static(def_id) => Some(def_id), | ||
| _ => None, | ||
| }) | ||
| .collect(); | ||
|
|
||
| // If we don't have any statics the check is not necessary | ||
| if statics.is_empty() { | ||
| return; | ||
| } | ||
| // Create a subgraph from the mono item graph, which only contains statics | ||
| let graph = StaticRefGraph { statics: &statics, used_map: &usage_map.used_map }; | ||
| // Calculate its SCCs | ||
| let sccs: Sccs<StaticNodeIdx, StaticSccIdx> = Sccs::new(&graph); | ||
| // Group statics by SCCs | ||
| let mut nodes_of_sccs: IndexVec<StaticSccIdx, Vec<StaticNodeIdx>> = | ||
| IndexVec::from_elem_n(Vec::new(), sccs.num_sccs()); | ||
| for i in graph.iter_nodes() { | ||
| nodes_of_sccs[sccs.scc(i)].push(i); | ||
| } | ||
| let is_cyclic = |nodes_of_scc: &[StaticNodeIdx]| -> bool { | ||
| match nodes_of_scc.len() { | ||
| 0 => false, | ||
| 1 => graph.successors(nodes_of_scc[0]).any(|x| x == nodes_of_scc[0]), | ||
| 2.. => true, | ||
| } | ||
| }; | ||
| // Emit errors for all cycles | ||
| for nodes in nodes_of_sccs.iter_mut().filter(|nodes| is_cyclic(nodes)) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor adjustment: |
||
| // We sort the nodes by their Span to have consistent error line numbers | ||
| nodes.sort_by_key(|node| tcx.def_span(statics[node.index()])); | ||
|
|
||
| let head_def = statics[nodes[0].index()]; | ||
| let head_span = tcx.def_span(head_def); | ||
|
|
||
| tcx.dcx().emit_err(errors::StaticInitializerCyclic { | ||
| span: head_span, | ||
| labels: nodes.iter().map(|&n| tcx.def_span(statics[n.index()])).collect(), | ||
| head: &tcx.def_path_str(head_def), | ||
| target: &tcx.sess.target.llvm_target, | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
tests/ui/static/static-initializer-acyclic-issue-146787.rs
kulst marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| //@ add-minicore | ||
| //@ needs-llvm-components: nvptx | ||
| //@ compile-flags: --target nvptx64-nvidia-cuda --emit link | ||
| //@ ignore-backends: gcc | ||
| #![crate_type = "rlib"] | ||
| #![feature(no_core)] | ||
| #![no_std] | ||
| #![no_core] | ||
|
|
||
| extern crate minicore; | ||
| use minicore::*; | ||
|
|
||
| struct Foo(&'static Foo); | ||
| impl Sync for Foo {} | ||
|
|
||
| static A: Foo = Foo(&A); //~ ERROR static initializer forms a cycle involving `A` | ||
|
|
||
| static B0: Foo = Foo(&B1); //~ ERROR static initializer forms a cycle involving `B0` | ||
| static B1: Foo = Foo(&B0); | ||
|
|
||
| static C0: Foo = Foo(&C1); //~ ERROR static initializer forms a cycle involving `C0` | ||
| static C1: Foo = Foo(&C2); | ||
| static C2: Foo = Foo(&C0); | ||
|
|
||
| struct Bar(&'static u32); | ||
| impl Sync for Bar {} | ||
|
|
||
| static BAR: Bar = Bar(&INT); | ||
| static INT: u32 = 42u32; |
32 changes: 32 additions & 0 deletions
32
tests/ui/static/static-initializer-acyclic-issue-146787.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| error: static initializer forms a cycle involving `C0` | ||
| --> $DIR/static-initializer-acyclic-issue-146787.rs:21:1 | ||
| | | ||
| LL | static C0: Foo = Foo(&C1); | ||
| | ^^^^^^^^^^^^^^ part of this cycle | ||
| LL | static C1: Foo = Foo(&C2); | ||
| | -------------- part of this cycle | ||
| LL | static C2: Foo = Foo(&C0); | ||
| | -------------- part of this cycle | ||
| | | ||
| = note: cyclic static initializers are not supported for target `nvptx64-nvidia-cuda` | ||
|
|
||
| error: static initializer forms a cycle involving `B0` | ||
| --> $DIR/static-initializer-acyclic-issue-146787.rs:18:1 | ||
| | | ||
| LL | static B0: Foo = Foo(&B1); | ||
| | ^^^^^^^^^^^^^^ part of this cycle | ||
| LL | static B1: Foo = Foo(&B0); | ||
| | -------------- part of this cycle | ||
| | | ||
| = note: cyclic static initializers are not supported for target `nvptx64-nvidia-cuda` | ||
|
|
||
| error: static initializer forms a cycle involving `A` | ||
| --> $DIR/static-initializer-acyclic-issue-146787.rs:16:1 | ||
| | | ||
| LL | static A: Foo = Foo(&A); | ||
| | ^^^^^^^^^^^^^ part of this cycle | ||
| | | ||
| = note: cyclic static initializers are not supported for target `nvptx64-nvidia-cuda` | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
In a way this is sort of a misnomer because target feature checks are also target-specific but they're also kinda not?
I'm not gonna stress over it.