Skip to content

Limit the size of cgu names when using the `-Zhuman-readable-cgu-name… #141558

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#![feature(negative_impls)]
#![feature(never_type)]
#![feature(ptr_alignment_type)]
#![feature(round_char_boundary)]
#![feature(rustc_attrs)]
#![feature(rustdoc_internals)]
#![feature(trusted_len)]
Expand Down
26 changes: 25 additions & 1 deletion compiler/rustc_middle/src/mir/mono.rs
Copy link
Member

@fmease fmease May 25, 2025

Choose a reason for hiding this comment

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

I'm not a Windows dev but wouldn't it make sense to "just" use extended-length paths on Windows (whose length limit is 216/2-1 = 32,767) — prefix is \\?\. Docs

Copy link
Member

Choose a reason for hiding this comment

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

Extended paths seem like the better solution to me as well since that means the logic to name the file will be the same regardless of platform.

Copy link
Contributor Author

@Diggsey Diggsey May 25, 2025

Choose a reason for hiding this comment

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

I think you are confusing paths with filenames. The limit here is a filename limit and is a limitation of the file-system, not just the OS.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it's practical to determine the actual limit, since there are so many factors involved (file-system, OS, etc.) not to mention that when the object files are combined into an archive, the archive file format may have its own limit.

IMO it's much better to impose a simple limit that will work everywhere. Especially given that it only affects users of this niche feature, and only CGUs with abormally long filenames.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::fmt;
use std::hash::Hash;

Expand Down Expand Up @@ -468,6 +469,29 @@ impl<'tcx> CodegenUnit<'tcx> {
hash.as_u128().to_base_fixed_len(CASE_INSENSITIVE)
}

pub fn shorten_name(human_readable_name: &str) -> Cow<'_, str> {
// Set a limit a somewhat below the common platform limits for file names.
const MAX_CGU_NAME_LENGTH: usize = 200;
Copy link
Member

Choose a reason for hiding this comment

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

Is this something we can query from the OS?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This needs to be less than the actual filename limit, since the extension and other bits are added later.

Copy link
Member

Choose a reason for hiding this comment

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

Even that aside, while on Windows we could use GetVolumeInformationByHandleW to query the maximum component length for local filesystems, the answer for both NTFS and ReFs is always 255 so I don't really see it as worth it. And I think it's very unlikely that it'll change in the near future for API reasons (there are some APIs that have a fixed-sized buffer for component names).

Copy link
Member

Choose a reason for hiding this comment

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

Browsing the limits it looks like 255 is by far the most common amongst filesystems https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits

Copy link
Member

Choose a reason for hiding this comment

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

A cgu name of 200 characters will still cause errors on Windows if the path to the directory it is in is 60 or more characters.

Copy link
Member

@ChrisDenton ChrisDenton May 26, 2025

Choose a reason for hiding this comment

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

As per the below conversation, verbatim paths can be used to workaround that. IIRC llvm handles long paths now and rustc does by virtue of using std APIs.

const TRUNCATED_NAME_PREFIX: &str = "-trunc-";
if human_readable_name.len() > MAX_CGU_NAME_LENGTH {
let mangled_name = Self::mangle_name(human_readable_name);
// Determine a safe byte offset to truncate the name to
let truncate_to = human_readable_name.floor_char_boundary(
MAX_CGU_NAME_LENGTH - TRUNCATED_NAME_PREFIX.len() - mangled_name.len(),
);
format!(
"{}{}{}",
&human_readable_name[..truncate_to],
TRUNCATED_NAME_PREFIX,
mangled_name
)
.into()
} else {
// If the name is short enough, we can just return it as is.
human_readable_name.into()
}
}

pub fn compute_size_estimate(&mut self) {
// The size of a codegen unit as the sum of the sizes of the items
// within it.
Expand Down Expand Up @@ -604,7 +628,7 @@ impl<'tcx> CodegenUnitNameBuilder<'tcx> {
let cgu_name = self.build_cgu_name_no_mangle(cnum, components, special_suffix);

if self.tcx.sess.opts.unstable_opts.human_readable_cgu_names {
cgu_name
Symbol::intern(&CodegenUnit::shorten_name(cgu_name.as_str()))
} else {
Symbol::intern(&CodegenUnit::mangle_name(cgu_name.as_str()))
}
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_monomorphize/src/partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,15 +461,15 @@ fn merge_codegen_units<'tcx>(

for cgu in codegen_units.iter_mut() {
if let Some(new_cgu_name) = new_cgu_names.get(&cgu.name()) {
if cx.tcx.sess.opts.unstable_opts.human_readable_cgu_names {
cgu.set_name(Symbol::intern(new_cgu_name));
let new_cgu_name = if cx.tcx.sess.opts.unstable_opts.human_readable_cgu_names {
Symbol::intern(&CodegenUnit::shorten_name(new_cgu_name))
} else {
// If we don't require CGU names to be human-readable,
// we use a fixed length hash of the composite CGU name
// instead.
let new_cgu_name = CodegenUnit::mangle_name(new_cgu_name);
cgu.set_name(Symbol::intern(&new_cgu_name));
}
Symbol::intern(&CodegenUnit::mangle_name(new_cgu_name))
};
cgu.set_name(new_cgu_name);
}
}

Expand Down
Loading