Skip to content
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
6 changes: 4 additions & 2 deletions compiler/rustc_codegen_llvm/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,13 @@ impl<'ll> CodegenCx<'ll, '_> {
let gv = self.define_global(&name, self.val_ty(cv)).unwrap_or_else(|| {
bug!("symbol `{}` is already defined", name);
});
llvm::set_linkage(gv, llvm::Linkage::PrivateLinkage);
gv
}
_ => self.define_private_global(self.val_ty(cv)),
_ => self.define_global("", self.val_ty(cv)).unwrap_or_else(|| {
Copy link
Member

@RalfJung RalfJung Oct 16, 2025

Choose a reason for hiding this comment

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

What on Earth does it mean to give a global the empty string as the name?!? This is not documented anywhere that I could find. (I checked define_global and a few of the functions it calls.)

Copy link
Contributor

Choose a reason for hiding this comment

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

The change done after @nikic's review comment doesn't seem to match what was suggested.

Copy link
Contributor

@nikic nikic Oct 16, 2025

Choose a reason for hiding this comment

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

An empty string name is how you create an anonymous symbol.

This is the same thing the old code did, it was just hidden behind a const Twine &Name = "" default value.

Copy link
Member

@RalfJung RalfJung Oct 16, 2025

Choose a reason for hiding this comment

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

Does "anonymous" mean "guaranteed fresh"?
And get_defined_value("") always returns None?

The bug! below this line makes it sound like "anonymous global symbol is already defined" is an actual error condition, but if it is truly fresh then that adds to my confusion.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. This kind of global is considered "unnamed" and will be rendered as @42 or similar when printed. The bug! here can't fail.

Do I understand correctly that the suggestion here is to replace the call to define_global() with declare_global(), which doesn't check existence of the global first, which also means we don't need this unwrap?

Copy link
Member

Choose a reason for hiding this comment

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

I hadn't even though of that, but that sounds like a good idea -- together with documentation for declare_global that says what happens when the name is empty.

bug!("anonymous global symbol is already defined");
}),
};
llvm::set_linkage(gv, llvm::Linkage::PrivateLinkage);
llvm::set_initializer(gv, cv);
set_global_alignment(self, gv, align);
llvm::set_unnamed_address(gv, llvm::UnnamedAddr::Global);
Expand Down
7 changes: 0 additions & 7 deletions compiler/rustc_codegen_llvm/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,6 @@ impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
}
}

/// Declare a private global
///
/// Use this function when you intend to define a global without a name.
pub(crate) fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
unsafe { llvm::LLVMRustInsertPrivateGlobal(self.llmod(), ty) }
}

/// Gets declared value by name.
pub(crate) fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
debug!("get_declared_value(name={:?})", name);
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1954,7 +1954,6 @@ unsafe extern "C" {
NameLen: size_t,
T: &'a Type,
) -> &'a Value;
pub(crate) fn LLVMRustInsertPrivateGlobal<'a>(M: &'a Module, T: &'a Type) -> &'a Value;
pub(crate) fn LLVMRustGetNamedValue(
M: &Module,
Name: *const c_char,
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,6 @@ extern "C" LLVMValueRef LLVMRustGetOrInsertGlobal(LLVMModuleRef M,
return wrap(GV);
}

extern "C" LLVMValueRef LLVMRustInsertPrivateGlobal(LLVMModuleRef M,
LLVMTypeRef Ty) {
return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
GlobalValue::PrivateLinkage, nullptr));
}

// Must match the layout of `rustc_codegen_llvm::llvm::ffi::AttributeKind`.
enum class LLVMRustAttributeKind {
AlwaysInline = 0,
Expand Down
Loading