Skip to content

Commit ca637b3

Browse files
Rollup merge of #166084 - nnethercote:unnecessary-llvm-passes, r=nikic
Avoid unnecessary LLVM passes when thin LTO buffer is null `LLVMRustOptimize` runs three passes when `NeedThinLTOBufferPasses` is true: `CanonicalizeAliasesPass`, `NameAnonGlobalPass` and `AssignGUIDPass`. These passes are only needed when the module's bitcode or summary will be written. This commit updates how `NeedThinLTOBufferPasses` is initialized in `LLVMRustOptimize`; instead of being initialized as always true, it's now only true if a thin LTO buffer is provided. (Subsequent existing code in the function may change `NeedThinLTOBufferPasses` to false.) Avoiding the unnecessary passes improves compile times a little, e.g. on debug builds (which are `O0` + `PreLinkNoLTO`). That requires a change to the passes added by `LLVMRustModuleSerialize`, because it currently relies on `LLVMRustOptimize` having run `CanonicalizeAliasesPass` and `NameAnonGlobalPass`, and this is no longer true in some cases such as `--emit=llvm-bc`. The passes are idempotent so it doesn't hurt if they are run twice in other cases. Non-LTO `--emit=llvm-ir,asm` output changes in two ways: unnamed globals stay unnamed (or are named `__unnamed` by LLVM) instead of being named `anon` (this requires updating some tests) and `!guid` metadata disappears. r? @nikic
2 parents 5510b38 + ce8a6c8 commit ca637b3

5 files changed

Lines changed: 13 additions & 11 deletions

File tree

‎compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ extern "C" LLVMRustResult LLVMRustOptimize(
997997
};
998998

999999
ModulePassManager MPM;
1000-
bool NeedThinLTOBufferPasses = true;
1000+
bool NeedThinLTOBufferPasses = ThinLTOBufferRef != nullptr;
10011001
auto ThinLTOBuffer = std::make_unique<LLVMRustBuffer>();
10021002
auto ThinLTOSummaryBuffer = std::make_unique<LLVMRustBuffer>();
10031003
raw_string_ostream ThinLTODataOS(ThinLTOBuffer->data);
@@ -1631,6 +1631,8 @@ extern "C" LLVMRustBuffer *LLVMRustModuleSerialize(LLVMModuleRef M,
16311631
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
16321632

16331633
ModulePassManager MPM;
1634+
MPM.addPass(CanonicalizeAliasesPass());
1635+
MPM.addPass(NameAnonGlobalPass());
16341636
#if LLVM_VERSION_GE(23, 0)
16351637
MPM.addPass(AssignGUIDPass());
16361638
#endif

‎tests/assembly-llvm/asm/x86-types.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub unsafe fn sym_static() {
4545

4646
// CHECK-LABEL: const_ptr:
4747
// CHECK: #APP
48-
// CHECK: mov al, byte ptr [{{.*}}anon{{.*}}]
48+
// CHECK: mov al, byte ptr [{{.*}}__unnamed{{.*}}]
4949
// CHECK: #NO_APP
5050
#[no_mangle]
5151
pub unsafe fn const_ptr() {

‎tests/assembly-llvm/cstring-merging.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// MIPS assembler uses the label prefix `$anon.` for local anonymous variables
2-
// other architectures (including ARM and x86-64) use the prefix `.Lanon.`
1+
// MIPS assembler uses the label prefix `$`.
2+
// Other architectures (including ARM and x86-64) use the prefix `.L`.
33
// Hexagon uses `.string` instead of `.asciz` for null-terminated strings
44
//@ only-linux
55
//@ assembly-output: emit-asm
@@ -9,21 +9,21 @@
99
use std::ffi::CStr;
1010

1111
// CHECK: .section .rodata.str1.{{[12]}},"aMS"
12-
// CHECK: {{(\.L|\$)}}anon.{{.+}}:
12+
// CHECK: {{(\.L|\$)}}__unnamed_{{[0-9]+}}:
1313
// CHECK-NEXT: .{{asciz|string}} "foo"
1414
#[unsafe(no_mangle)]
1515
static CSTR: &[u8; 4] = b"foo\0";
1616

1717
// CHECK: .section .rodata.str1.{{[12]}},"aMS"
18-
// CHECK: {{(\.L|\$)}}anon.{{.+}}:
18+
// CHECK: {{(\.L|\$)}}__unnamed_{{[0-9]+}}:
1919
// CHECK-NEXT: .{{asciz|string}} "bar"
2020
#[unsafe(no_mangle)]
2121
pub fn cstr() -> &'static CStr {
2222
c"bar"
2323
}
2424

2525
// CHECK-NOT: .section
26-
// CHECK: {{(\.L|\$)}}anon.{{.+}}:
26+
// CHECK: {{(\.L|\$)}}__unnamed_{{[0-9]+}}:
2727
// CHECK-NEXT: .{{asciz|string}} "baz"
2828
#[unsafe(no_mangle)]
2929
pub fn manual_cstr() -> &'static str {

‎tests/codegen-llvm/call-tmps-lifetime.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use minicore::*;
1515
//
1616
// CHECK-LABEL: define void @const_indirect(
1717
// CHECK-NEXT: start:
18-
// CHECK-NEXT: call void %h(ptr {{.*}}byval([4096 x i8]){{.*}} [[C:@anon.*]])
18+
// CHECK-NEXT: call void %h(ptr {{.*}}byval([4096 x i8]){{.*}} [[C:@[0-9]+]])
1919
// CHECK-NEXT: call void %h(ptr {{.*}}byval([4096 x i8]){{.*}} [[C]])
2020
#[no_mangle]
2121
pub fn const_indirect(h: extern "C" fn([u32; 1024])) {

‎tests/codegen-llvm/indirect-bycopy-bymove-byval.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Copy for Thing {}
3030
// CHECK: call void @llvm.memcpy{{.*}}(ptr{{.*}} [[normal_V1:%.*]], ptr{{.*}} %value,
3131
// CHECK: call void @opaque(ptr{{.*}} [[normal_V1]])
3232
// CHECK: call void @opaque(ptr{{.*}} %value)
33-
// CHECK: call void @llvm.memcpy{{.*}}(ptr{{.*}} [[normal_V3:%.*]], ptr{{.*}} @anon{{.*}},
33+
// CHECK: call void @llvm.memcpy{{.*}}(ptr{{.*}} [[normal_V3:%.*]], ptr{{.*}} @{{[0-9]+}},
3434
// CHECK: call void @opaque(ptr{{.*}} [[normal_V3]])
3535
#[unsafe(no_mangle)]
3636
pub fn normal() {
@@ -54,7 +54,7 @@ pub fn normal() {
5454
// CHECK-NEXT: call void @{{.*}}(ptr {{.*}}, ptr{{.*}} [[untupled_V1]])
5555
// CHECK: call indirect_bycopy_bymove_byval::untupled::{closure#1}
5656
// CHECK-NEXT: call void @{{.*}}(ptr {{.*}}, ptr{{.*}} %value)
57-
// CHECK: call void @llvm.memcpy{{.*}}(ptr{{.*}} [[untupled_V3:%.*]], ptr{{.*}} @anon{{.*}}
57+
// CHECK: call void @llvm.memcpy{{.*}}(ptr{{.*}} [[untupled_V3:%.*]], ptr{{.*}} @{{[0-9]+}}
5858
// CHECK: call indirect_bycopy_bymove_byval::untupled::{closure#2}
5959
// CHECK-NEXT: call void @{{.*}}(ptr {{.*}}, ptr{{.*}} [[untupled_V3]])
6060
#[unsafe(no_mangle)]
@@ -83,7 +83,7 @@ pub fn untupled() {
8383
// CHECK-LABEL: @byval
8484
// CHECK: call void @opaque_byval(ptr{{.*}} byval([24 x i8]){{.*}} %value)
8585
// CHECK: call void @opaque_byval(ptr{{.*}} byval([24 x i8]){{.*}} %value)
86-
// CHECK: call void @opaque_byval(ptr{{.*}} byval([24 x i8]){{.*}} @anon{{.*}})
86+
// CHECK: call void @opaque_byval(ptr{{.*}} byval([24 x i8]){{.*}} @{{[0-9]+}})
8787
#[unsafe(no_mangle)]
8888
pub fn byval() {
8989
#[repr(C)]

0 commit comments

Comments
 (0)