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

Avoid wrapping constant allocations in packed structs when not necessary #138503

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bjorn3
Copy link
Member

@bjorn3 bjorn3 commented Mar 14, 2025

This way LLVM will set the string merging flag if the alloc is a nul terminated string, reducing binary sizes.

@rustbot
Copy link
Collaborator

rustbot commented Mar 14, 2025

r? @estebank

rustbot has assigned @estebank.
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
Copy link
Collaborator

rustbot commented Mar 14, 2025

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

@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 Mar 14, 2025
@bjorn3
Copy link
Member Author

bjorn3 commented Mar 14, 2025

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 14, 2025
@bors
Copy link
Contributor

bors commented Mar 14, 2025

⌛ Trying commit 1688182 with merge 3da9ce8...

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 14, 2025
Avoid wrapping constant allocations in packed structs when not necessary

This way LLVM will set the string merging flag if the alloc is a nul terminated string, reducing binary sizes.
#[no_mangle]
pub static mut L: u8 = 0;
};

fn x() {
// CHECK: @M = {{(dso_local )?}}local_unnamed_addr constant
// CHECK-DAG: @M = {{(dso_local )?}}local_unnamed_addr constant
Copy link
Member Author

Choose a reason for hiding this comment

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

LLVM decided to put this definition before A. Using CHECK-DAG everywhere allows filecheck to match independent of the order.

bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 14, 2025
…r=<try>

Nul terminate rust string literals

This allows taking advantage of the C string merging functionality of linkers, reducing code size.

Marked as draft to see if this actually has much of an effect. The disadvantage of this is that people may start to rely on string literals getting nul terminated. A potential solution for that would be to put a byte that is not part of a valid UTF-8 character right before the nul terminator.

Builds on rust-lang#138503
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Mar 14, 2025

☀️ Try build successful - checks-actions
Build commit: 3da9ce8 (3da9ce89fe5dc7ed237607149c0cfc7e3f490ce1)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (3da9ce8): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
1.7% [1.7%, 1.7%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.7% [-4.4%, -0.3%] 5
Improvements ✅
(secondary)
-2.3% [-3.3%, -1.9%] 8
All ❌✅ (primary) -1.1% [-4.4%, 1.7%] 6

Max RSS (memory usage)

This benchmark run did not return any relevant results for this metric.

Cycles

Results (primary -2.2%, secondary -3.7%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.2% [-4.2%, -1.3%] 4
Improvements ✅
(secondary)
-3.7% [-4.7%, -3.2%] 8
All ❌✅ (primary) -2.2% [-4.2%, -1.3%] 4

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 775.364s -> 773.305s (-0.27%)
Artifact size: 365.01 MiB -> 365.01 MiB (0.00%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Mar 14, 2025
@bjorn3
Copy link
Member Author

bjorn3 commented Mar 14, 2025

No statistically significant binary size changes, which is not all that surprising as most strings are not nul terminated. Compilation gets a bit faster though.

This way LLVM will set the string merging flag if the alloc is a nul
terminated string, reducing binary sizes.
@bjorn3
Copy link
Member Author

bjorn3 commented Mar 17, 2025

Seems that cg_gcc doesn't handle changing the type of the static to the actual type. Reverted the change for cg_gcc.

@antoyo
Copy link
Contributor

antoyo commented Mar 17, 2025

Seems that cg_gcc doesn't handle changing the type of the static to the actual type. Reverted the change for cg_gcc.

Perhaps this was missing a bitcast?

@bjorn3
Copy link
Member Author

bjorn3 commented Mar 17, 2025

I think it was

#[cfg(feature = "master")]
if global.to_rvalue().get_type() != val_llty {
global.to_rvalue().set_type(val_llty);
}
being configured out.

@antoyo
Copy link
Contributor

antoyo commented Mar 17, 2025

I think it was

#[cfg(feature = "master")]
if global.to_rvalue().get_type() != val_llty {
global.to_rvalue().set_type(val_llty);
}

being configured out.

Oh, I meant an actual call to new_bitcast(), but yeah, maybe that set_type() call should have done it.
I'll check that on my side when this PR is merged.

@tmiasko
Copy link
Contributor

tmiasko commented Mar 25, 2025

Can you add a regression test with C-string literals? An end-to-end test would be nice to ensure that everything works as expected, but LLVM code generation test with an explanation also seems sufficient. Can you also include a reference to https://github.com/rust-lang/llvm-project/blob/acaea3d2bb8f351b740db7ebce7d7a40b9e21488/llvm/lib/Target/TargetLoweringObjectFile.cpp#L249-L280.

r? tmiasko

@rustbot rustbot assigned tmiasko and unassigned estebank Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants