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

[RFC] Support .comment section like GCC/Clang (!llvm.ident) #97550

Merged
merged 1 commit into from
Jul 21, 2023
Merged
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
18 changes: 18 additions & 0 deletions compiler/rustc_codegen_llvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use rustc_target::abi::{
use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
use smallvec::SmallVec;

use libc::c_uint;
use std::cell::{Cell, RefCell};
use std::ffi::CStr;
use std::str;
Expand Down Expand Up @@ -349,6 +350,23 @@ pub unsafe fn create_module<'ll>(
);
}

// Insert `llvm.ident` metadata.
//
// On the wasm targets it will get hooked up to the "producer" sections
// `processed-by` information.
let rustc_producer =
format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
let name_metadata = llvm::LLVMMDStringInContext(
llcx,
rustc_producer.as_ptr().cast(),
rustc_producer.as_bytes().len() as c_uint,
);
llvm::LLVMAddNamedMetadataOperand(
llmod,
cstr!("llvm.ident").as_ptr(),
llvm::LLVMMDNodeInContext(llcx, &name_metadata, 1),
);

llmod
}

Expand Down
15 changes: 0 additions & 15 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,21 +888,6 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
llvm::LLVMAddNamedMetadataOperand(debug_context.llmod, llvm_gcov_ident.as_ptr(), val);
}

// Insert `llvm.ident` metadata on the wasm targets since that will
// get hooked up to the "producer" sections `processed-by` information.
if tcx.sess.target.is_like_wasm {
let name_metadata = llvm::LLVMMDStringInContext(
debug_context.llcontext,
rustc_producer.as_ptr().cast(),
rustc_producer.as_bytes().len() as c_uint,
);
llvm::LLVMAddNamedMetadataOperand(
debug_context.llmod,
cstr!("llvm.ident").as_ptr(),
llvm::LLVMMDNodeInContext(debug_context.llcontext, &name_metadata, 1),
);
}

return unit_metadata;
};

Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,11 @@ LLVMRustPrepareThinLTOImport(const LLVMRustThinLTOData *Data, LLVMModuleRef M,
if (WasmCustomSections)
WasmCustomSections->eraseFromParent();

// `llvm.ident` named metadata also gets duplicated.
auto *llvmIdent = (*MOrErr)->getNamedMetadata("llvm.ident");
if (llvmIdent)
llvmIdent->eraseFromParent();

return MOrErr;
};
bool ClearDSOLocal = clearDSOLocalOnDeclarations(Mod, Target);
Expand Down
15 changes: 15 additions & 0 deletions tests/codegen/llvm-ident.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Verifies that the `!llvm.ident` named metadata is emitted.
//
// revisions: NONE OPT DEBUG
//
// [OPT] compile-flags: -Copt-level=2
// [DEBUG] compile-flags: -Cdebuginfo=2

// The named metadata should contain a single metadata node (see
// `LLVMRustPrepareThinLTOImport` for details).
// CHECK: !llvm.ident = !{![[ID:[0-9]+]]}

// In addition, check that the metadata node has the expected content.
// CHECK: ![[ID]] = !{!"rustc version 1.{{.*}}"}

fn main() {}
15 changes: 15 additions & 0 deletions tests/run-make/comment-section/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
include ../tools.mk

# only-linux

all:
echo 'fn main(){}' | $(RUSTC) - --emit=link,obj -Csave-temps --target=$(TARGET)

# Check linked output has a `.comment` section with the expected content.
readelf -p '.comment' $(TMPDIR)/rust_out | $(CGREP) -F 'rustc version 1.'

# Check all object files (including temporary outputs) have a `.comment`
# section with the expected content.
set -e; for f in $(TMPDIR)/*.o; do \
readelf -p '.comment' $$f | $(CGREP) -F 'rustc version 1.'; \
done
19 changes: 19 additions & 0 deletions tests/run-make/llvm-ident/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
include ../tools.mk

# only-linux

all:
# `-Ccodegen-units=16 -Copt-level=2` is used here to trigger thin LTO
# across codegen units to test deduplication of the named metadata
# (see `LLVMRustPrepareThinLTOImport` for details).
echo 'fn main(){}' | $(RUSTC) - --emit=link,obj -Csave-temps -Ccodegen-units=16 -Copt-level=2 --target=$(TARGET)

# `llvm-dis` is used here since `--emit=llvm-ir` does not emit LLVM IR
# for temporary outputs.
"$(LLVM_BIN_DIR)"/llvm-dis $(TMPDIR)/*.bc

# Check LLVM IR files (including temporary outputs) have `!llvm.ident`
# named metadata, reusing the related codegen test.
set -e; for f in $(TMPDIR)/*.ll; do \
$(LLVM_FILECHECK) --input-file $$f ../../codegen/llvm-ident.rs; \
done