Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0308df2

Browse files
committedJul 21, 2023
Auto merge of #97550 - ojeda:comment-section, r=bjorn3
[RFC] Support `.comment` section like GCC/Clang (`!llvm.ident`) Both GCC and Clang write by default a `.comment` section with compiler information: ```txt $ gcc -c -xc /dev/null && readelf -p '.comment' null.o String dump of section '.comment': [ 1] GCC: (GNU) 11.2.0 $ clang -c -xc /dev/null && readelf -p '.comment' null.o String dump of section '.comment': [ 1] clang version 14.0.1 (https://github.com/llvm/llvm-project.git c62053979489ccb002efe411c3af059addcb5d7d) ``` They also implement the `-Qn` flag to avoid doing so: ```txt $ gcc -Qn -c -xc /dev/null && readelf -p '.comment' null.o readelf: Warning: Section '.comment' was not dumped because it does not exist! $ clang -Qn -c -xc /dev/null && readelf -p '.comment' null.o readelf: Warning: Section '.comment' was not dumped because it does not exist! ``` So far, `rustc` only does it for WebAssembly targets and only when debug info is enabled: ```txt $ echo 'fn main(){}' | rustc --target=wasm32-unknown-unknown --emit=llvm-ir -Cdebuginfo=2 - && grep llvm.ident rust_out.ll !llvm.ident = !{!27} ``` The RFC part of this PR is about which behavior should `rustc` follow: - Always add it. - Add it by default, i.e. have an opt-out flag (GCC, Clang). - Have an opt-in flag. - Never add it (current). There is also the question of whether debug info being enabled matters for that decision, given the current behavior of WebAssembly targets. For instance, adding it by default gets us closer to other popular compilers, but that may surprise some users with an information leak. The most conservative option is to only do so opt-in, even if debug info is enabled (some users may be stripping debug info and not expecting something else to be leaked elsewhere). Implementation-wise, this covers both `ModuleLlvm::new()` and `ModuleLlvm::new_metadata()` cases by moving the addition to `context::create_module` and adds a few test cases. ThinLTO also sees the `llvm.ident` named metadata duplicated (in temporary outputs), so this deduplicates it like it is done for `wasm.custom_sections`. The tests also check this duplication does not take place.
2 parents d908a5b + 74b8d32 commit 0308df2

File tree

6 files changed

+72
-15
lines changed

6 files changed

+72
-15
lines changed
 

‎compiler/rustc_codegen_llvm/src/context.rs

+18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use rustc_target::abi::{
3333
use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
3434
use smallvec::SmallVec;
3535

36+
use libc::c_uint;
3637
use std::cell::{Cell, RefCell};
3738
use std::ffi::CStr;
3839
use std::str;
@@ -349,6 +350,23 @@ pub unsafe fn create_module<'ll>(
349350
);
350351
}
351352

353+
// Insert `llvm.ident` metadata.
354+
//
355+
// On the wasm targets it will get hooked up to the "producer" sections
356+
// `processed-by` information.
357+
let rustc_producer =
358+
format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
359+
let name_metadata = llvm::LLVMMDStringInContext(
360+
llcx,
361+
rustc_producer.as_ptr().cast(),
362+
rustc_producer.as_bytes().len() as c_uint,
363+
);
364+
llvm::LLVMAddNamedMetadataOperand(
365+
llmod,
366+
cstr!("llvm.ident").as_ptr(),
367+
llvm::LLVMMDNodeInContext(llcx, &name_metadata, 1),
368+
);
369+
352370
llmod
353371
}
354372

‎compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

-15
Original file line numberDiff line numberDiff line change
@@ -888,21 +888,6 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
888888
llvm::LLVMAddNamedMetadataOperand(debug_context.llmod, llvm_gcov_ident.as_ptr(), val);
889889
}
890890

891-
// Insert `llvm.ident` metadata on the wasm targets since that will
892-
// get hooked up to the "producer" sections `processed-by` information.
893-
if tcx.sess.target.is_like_wasm {
894-
let name_metadata = llvm::LLVMMDStringInContext(
895-
debug_context.llcontext,
896-
rustc_producer.as_ptr().cast(),
897-
rustc_producer.as_bytes().len() as c_uint,
898-
);
899-
llvm::LLVMAddNamedMetadataOperand(
900-
debug_context.llmod,
901-
cstr!("llvm.ident").as_ptr(),
902-
llvm::LLVMMDNodeInContext(debug_context.llcontext, &name_metadata, 1),
903-
);
904-
}
905-
906891
return unit_metadata;
907892
};
908893

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

+5
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,11 @@ LLVMRustPrepareThinLTOImport(const LLVMRustThinLTOData *Data, LLVMModuleRef M,
13661366
if (WasmCustomSections)
13671367
WasmCustomSections->eraseFromParent();
13681368

1369+
// `llvm.ident` named metadata also gets duplicated.
1370+
auto *llvmIdent = (*MOrErr)->getNamedMetadata("llvm.ident");
1371+
if (llvmIdent)
1372+
llvmIdent->eraseFromParent();
1373+
13691374
return MOrErr;
13701375
};
13711376
bool ClearDSOLocal = clearDSOLocalOnDeclarations(Mod, Target);

‎tests/codegen/llvm-ident.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Verifies that the `!llvm.ident` named metadata is emitted.
2+
//
3+
// revisions: NONE OPT DEBUG
4+
//
5+
// [OPT] compile-flags: -Copt-level=2
6+
// [DEBUG] compile-flags: -Cdebuginfo=2
7+
8+
// The named metadata should contain a single metadata node (see
9+
// `LLVMRustPrepareThinLTOImport` for details).
10+
// CHECK: !llvm.ident = !{![[ID:[0-9]+]]}
11+
12+
// In addition, check that the metadata node has the expected content.
13+
// CHECK: ![[ID]] = !{!"rustc version 1.{{.*}}"}
14+
15+
fn main() {}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
include ../tools.mk
2+
3+
# only-linux
4+
5+
all:
6+
echo 'fn main(){}' | $(RUSTC) - --emit=link,obj -Csave-temps --target=$(TARGET)
7+
8+
# Check linked output has a `.comment` section with the expected content.
9+
readelf -p '.comment' $(TMPDIR)/rust_out | $(CGREP) -F 'rustc version 1.'
10+
11+
# Check all object files (including temporary outputs) have a `.comment`
12+
# section with the expected content.
13+
set -e; for f in $(TMPDIR)/*.o; do \
14+
readelf -p '.comment' $$f | $(CGREP) -F 'rustc version 1.'; \
15+
done

‎tests/run-make/llvm-ident/Makefile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
include ../tools.mk
2+
3+
# only-linux
4+
5+
all:
6+
# `-Ccodegen-units=16 -Copt-level=2` is used here to trigger thin LTO
7+
# across codegen units to test deduplication of the named metadata
8+
# (see `LLVMRustPrepareThinLTOImport` for details).
9+
echo 'fn main(){}' | $(RUSTC) - --emit=link,obj -Csave-temps -Ccodegen-units=16 -Copt-level=2 --target=$(TARGET)
10+
11+
# `llvm-dis` is used here since `--emit=llvm-ir` does not emit LLVM IR
12+
# for temporary outputs.
13+
"$(LLVM_BIN_DIR)"/llvm-dis $(TMPDIR)/*.bc
14+
15+
# Check LLVM IR files (including temporary outputs) have `!llvm.ident`
16+
# named metadata, reusing the related codegen test.
17+
set -e; for f in $(TMPDIR)/*.ll; do \
18+
$(LLVM_FILECHECK) --input-file $$f ../../codegen/llvm-ident.rs; \
19+
done

0 commit comments

Comments
 (0)
Failed to load comments.