1
Fork 0

Rollup merge of #136881 - dpaoliello:cleanllvm3, r=Zalathar

cg_llvm: Reduce visibility of all functions in the llvm module

Next part of #135502

This reduces the visibility of all functions in the `llvm` module to `pub(crate)` and marks the `enzyme_ffi` modules with `#![expect(dead_code)]` (as previously discussed: <https://github.com/rust-lang/rust/pull/135502#discussion_r1915608085>).

r? ``@Zalathar``
This commit is contained in:
Jacob Pratt 2025-02-13 03:53:31 -05:00 committed by GitHub
commit f7d5285062
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 478 additions and 420 deletions

View file

@ -26,7 +26,7 @@ impl ArchiveRO {
/// ///
/// If this archive is used with a mutable method, then an error will be /// If this archive is used with a mutable method, then an error will be
/// raised. /// raised.
pub fn open(dst: &Path) -> Result<ArchiveRO, String> { pub(crate) fn open(dst: &Path) -> Result<ArchiveRO, String> {
unsafe { unsafe {
let s = path_to_c_string(dst); let s = path_to_c_string(dst);
let ar = super::LLVMRustOpenArchive(s.as_ptr()).ok_or_else(|| { let ar = super::LLVMRustOpenArchive(s.as_ptr()).ok_or_else(|| {
@ -36,7 +36,7 @@ impl ArchiveRO {
} }
} }
pub fn iter(&self) -> Iter<'_> { pub(crate) fn iter(&self) -> Iter<'_> {
unsafe { Iter { raw: super::LLVMRustArchiveIteratorNew(self.raw) } } unsafe { Iter { raw: super::LLVMRustArchiveIteratorNew(self.raw) } }
} }
} }
@ -71,7 +71,7 @@ impl<'a> Drop for Iter<'a> {
} }
impl<'a> Child<'a> { impl<'a> Child<'a> {
pub fn name(&self) -> Option<&'a str> { pub(crate) fn name(&self) -> Option<&'a str> {
unsafe { unsafe {
let mut name_len = 0; let mut name_len = 0;
let name_ptr = super::LLVMRustArchiveChildName(self.raw, &mut name_len); let name_ptr = super::LLVMRustArchiveChildName(self.raw, &mut name_len);

View file

@ -1,4 +1,5 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
#![expect(dead_code)]
use libc::{c_char, c_uint}; use libc::{c_char, c_uint};
@ -8,23 +9,23 @@ use crate::llvm::Bool;
#[link(name = "llvm-wrapper", kind = "static")] #[link(name = "llvm-wrapper", kind = "static")]
extern "C" { extern "C" {
// Enzyme // Enzyme
pub fn LLVMRustHasMetadata(I: &Value, KindID: c_uint) -> bool; pub(crate) fn LLVMRustHasMetadata(I: &Value, KindID: c_uint) -> bool;
pub fn LLVMRustEraseInstUntilInclusive(BB: &BasicBlock, I: &Value); pub(crate) fn LLVMRustEraseInstUntilInclusive(BB: &BasicBlock, I: &Value);
pub fn LLVMRustGetLastInstruction<'a>(BB: &BasicBlock) -> Option<&'a Value>; pub(crate) fn LLVMRustGetLastInstruction<'a>(BB: &BasicBlock) -> Option<&'a Value>;
pub fn LLVMRustDIGetInstMetadata(I: &Value) -> Option<&Metadata>; pub(crate) fn LLVMRustDIGetInstMetadata(I: &Value) -> Option<&Metadata>;
pub fn LLVMRustEraseInstFromParent(V: &Value); pub(crate) fn LLVMRustEraseInstFromParent(V: &Value);
pub fn LLVMRustGetTerminator<'a>(B: &BasicBlock) -> &'a Value; pub(crate) fn LLVMRustGetTerminator<'a>(B: &BasicBlock) -> &'a Value;
pub fn LLVMRustVerifyFunction(V: &Value, action: LLVMRustVerifierFailureAction) -> Bool; pub(crate) fn LLVMRustVerifyFunction(V: &Value, action: LLVMRustVerifierFailureAction) -> Bool;
} }
extern "C" { extern "C" {
// Enzyme // Enzyme
pub fn LLVMDumpModule(M: &Module); pub(crate) fn LLVMDumpModule(M: &Module);
pub fn LLVMDumpValue(V: &Value); pub(crate) fn LLVMDumpValue(V: &Value);
pub fn LLVMGetFunctionCallConv(F: &Value) -> c_uint; pub(crate) fn LLVMGetFunctionCallConv(F: &Value) -> c_uint;
pub fn LLVMGetReturnType(T: &Type) -> &Type; pub(crate) fn LLVMGetReturnType(T: &Type) -> &Type;
pub fn LLVMGetParams(Fnc: &Value, parms: *mut &Value); pub(crate) fn LLVMGetParams(Fnc: &Value, parms: *mut &Value);
pub fn LLVMGetNamedFunction(M: &Module, Name: *const c_char) -> Option<&Value>; pub(crate) fn LLVMGetNamedFunction(M: &Module, Name: *const c_char) -> Option<&Value>;
} }
#[repr(C)] #[repr(C)]

File diff suppressed because it is too large Load diff

View file

@ -24,7 +24,7 @@ mod ffi;
pub use self::enzyme_ffi::*; pub use self::enzyme_ffi::*;
impl LLVMRustResult { impl LLVMRustResult {
pub fn into_result(self) -> Result<(), ()> { pub(crate) fn into_result(self) -> Result<(), ()> {
match self { match self {
LLVMRustResult::Success => Ok(()), LLVMRustResult::Success => Ok(()),
LLVMRustResult::Failure => Err(()), LLVMRustResult::Failure => Err(()),
@ -32,13 +32,17 @@ impl LLVMRustResult {
} }
} }
pub fn AddFunctionAttributes<'ll>(llfn: &'ll Value, idx: AttributePlace, attrs: &[&'ll Attribute]) { pub(crate) fn AddFunctionAttributes<'ll>(
llfn: &'ll Value,
idx: AttributePlace,
attrs: &[&'ll Attribute],
) {
unsafe { unsafe {
LLVMRustAddFunctionAttributes(llfn, idx.as_uint(), attrs.as_ptr(), attrs.len()); LLVMRustAddFunctionAttributes(llfn, idx.as_uint(), attrs.as_ptr(), attrs.len());
} }
} }
pub fn AddCallSiteAttributes<'ll>( pub(crate) fn AddCallSiteAttributes<'ll>(
callsite: &'ll Value, callsite: &'ll Value,
idx: AttributePlace, idx: AttributePlace,
attrs: &[&'ll Attribute], attrs: &[&'ll Attribute],
@ -48,7 +52,11 @@ pub fn AddCallSiteAttributes<'ll>(
} }
} }
pub fn CreateAttrStringValue<'ll>(llcx: &'ll Context, attr: &str, value: &str) -> &'ll Attribute { pub(crate) fn CreateAttrStringValue<'ll>(
llcx: &'ll Context,
attr: &str,
value: &str,
) -> &'ll Attribute {
unsafe { unsafe {
LLVMCreateStringAttribute( LLVMCreateStringAttribute(
llcx, llcx,
@ -60,7 +68,7 @@ pub fn CreateAttrStringValue<'ll>(llcx: &'ll Context, attr: &str, value: &str) -
} }
} }
pub fn CreateAttrString<'ll>(llcx: &'ll Context, attr: &str) -> &'ll Attribute { pub(crate) fn CreateAttrString<'ll>(llcx: &'ll Context, attr: &str) -> &'ll Attribute {
unsafe { unsafe {
LLVMCreateStringAttribute( LLVMCreateStringAttribute(
llcx, llcx,
@ -72,39 +80,39 @@ pub fn CreateAttrString<'ll>(llcx: &'ll Context, attr: &str) -> &'ll Attribute {
} }
} }
pub fn CreateAlignmentAttr(llcx: &Context, bytes: u64) -> &Attribute { pub(crate) fn CreateAlignmentAttr(llcx: &Context, bytes: u64) -> &Attribute {
unsafe { LLVMRustCreateAlignmentAttr(llcx, bytes) } unsafe { LLVMRustCreateAlignmentAttr(llcx, bytes) }
} }
pub fn CreateDereferenceableAttr(llcx: &Context, bytes: u64) -> &Attribute { pub(crate) fn CreateDereferenceableAttr(llcx: &Context, bytes: u64) -> &Attribute {
unsafe { LLVMRustCreateDereferenceableAttr(llcx, bytes) } unsafe { LLVMRustCreateDereferenceableAttr(llcx, bytes) }
} }
pub fn CreateDereferenceableOrNullAttr(llcx: &Context, bytes: u64) -> &Attribute { pub(crate) fn CreateDereferenceableOrNullAttr(llcx: &Context, bytes: u64) -> &Attribute {
unsafe { LLVMRustCreateDereferenceableOrNullAttr(llcx, bytes) } unsafe { LLVMRustCreateDereferenceableOrNullAttr(llcx, bytes) }
} }
pub fn CreateByValAttr<'ll>(llcx: &'ll Context, ty: &'ll Type) -> &'ll Attribute { pub(crate) fn CreateByValAttr<'ll>(llcx: &'ll Context, ty: &'ll Type) -> &'ll Attribute {
unsafe { LLVMRustCreateByValAttr(llcx, ty) } unsafe { LLVMRustCreateByValAttr(llcx, ty) }
} }
pub fn CreateStructRetAttr<'ll>(llcx: &'ll Context, ty: &'ll Type) -> &'ll Attribute { pub(crate) fn CreateStructRetAttr<'ll>(llcx: &'ll Context, ty: &'ll Type) -> &'ll Attribute {
unsafe { LLVMRustCreateStructRetAttr(llcx, ty) } unsafe { LLVMRustCreateStructRetAttr(llcx, ty) }
} }
pub fn CreateUWTableAttr(llcx: &Context, async_: bool) -> &Attribute { pub(crate) fn CreateUWTableAttr(llcx: &Context, async_: bool) -> &Attribute {
unsafe { LLVMRustCreateUWTableAttr(llcx, async_) } unsafe { LLVMRustCreateUWTableAttr(llcx, async_) }
} }
pub fn CreateAllocSizeAttr(llcx: &Context, size_arg: u32) -> &Attribute { pub(crate) fn CreateAllocSizeAttr(llcx: &Context, size_arg: u32) -> &Attribute {
unsafe { LLVMRustCreateAllocSizeAttr(llcx, size_arg) } unsafe { LLVMRustCreateAllocSizeAttr(llcx, size_arg) }
} }
pub fn CreateAllocKindAttr(llcx: &Context, kind_arg: AllocKindFlags) -> &Attribute { pub(crate) fn CreateAllocKindAttr(llcx: &Context, kind_arg: AllocKindFlags) -> &Attribute {
unsafe { LLVMRustCreateAllocKindAttr(llcx, kind_arg.bits()) } unsafe { LLVMRustCreateAllocKindAttr(llcx, kind_arg.bits()) }
} }
pub fn CreateRangeAttr(llcx: &Context, size: Size, range: WrappingRange) -> &Attribute { pub(crate) fn CreateRangeAttr(llcx: &Context, size: Size, range: WrappingRange) -> &Attribute {
let lower = range.start; let lower = range.start;
let upper = range.end.wrapping_add(1); let upper = range.end.wrapping_add(1);
let lower_words = [lower as u64, (lower >> 64) as u64]; let lower_words = [lower as u64, (lower >> 64) as u64];
@ -127,7 +135,7 @@ pub enum AttributePlace {
} }
impl AttributePlace { impl AttributePlace {
pub fn as_uint(self) -> c_uint { pub(crate) fn as_uint(self) -> c_uint {
match self { match self {
AttributePlace::ReturnValue => 0, AttributePlace::ReturnValue => 0,
AttributePlace::Argument(i) => 1 + i, AttributePlace::Argument(i) => 1 + i,
@ -159,12 +167,12 @@ impl FromStr for ArchiveKind {
} }
} }
pub fn SetInstructionCallConv(instr: &Value, cc: CallConv) { pub(crate) fn SetInstructionCallConv(instr: &Value, cc: CallConv) {
unsafe { unsafe {
LLVMSetInstructionCallConv(instr, cc as c_uint); LLVMSetInstructionCallConv(instr, cc as c_uint);
} }
} }
pub fn SetFunctionCallConv(fn_: &Value, cc: CallConv) { pub(crate) fn SetFunctionCallConv(fn_: &Value, cc: CallConv) {
unsafe { unsafe {
LLVMSetFunctionCallConv(fn_, cc as c_uint); LLVMSetFunctionCallConv(fn_, cc as c_uint);
} }
@ -176,20 +184,20 @@ pub fn SetFunctionCallConv(fn_: &Value, cc: CallConv) {
// value's name as the comdat value to make sure that it is in a 1-to-1 relationship to the // value's name as the comdat value to make sure that it is in a 1-to-1 relationship to the
// function. // function.
// For more details on COMDAT sections see e.g., https://www.airs.com/blog/archives/52 // For more details on COMDAT sections see e.g., https://www.airs.com/blog/archives/52
pub fn SetUniqueComdat(llmod: &Module, val: &Value) { pub(crate) fn SetUniqueComdat(llmod: &Module, val: &Value) {
let name_buf = get_value_name(val).to_vec(); let name_buf = get_value_name(val).to_vec();
let name = let name =
CString::from_vec_with_nul(name_buf).or_else(|buf| CString::new(buf.into_bytes())).unwrap(); CString::from_vec_with_nul(name_buf).or_else(|buf| CString::new(buf.into_bytes())).unwrap();
set_comdat(llmod, val, &name); set_comdat(llmod, val, &name);
} }
pub fn SetUnnamedAddress(global: &Value, unnamed: UnnamedAddr) { pub(crate) fn SetUnnamedAddress(global: &Value, unnamed: UnnamedAddr) {
unsafe { unsafe {
LLVMSetUnnamedAddress(global, unnamed); LLVMSetUnnamedAddress(global, unnamed);
} }
} }
pub fn set_thread_local_mode(global: &Value, mode: ThreadLocalMode) { pub(crate) fn set_thread_local_mode(global: &Value, mode: ThreadLocalMode) {
unsafe { unsafe {
LLVMSetThreadLocalMode(global, mode); LLVMSetThreadLocalMode(global, mode);
} }
@ -197,65 +205,65 @@ pub fn set_thread_local_mode(global: &Value, mode: ThreadLocalMode) {
impl AttributeKind { impl AttributeKind {
/// Create an LLVM Attribute with no associated value. /// Create an LLVM Attribute with no associated value.
pub fn create_attr(self, llcx: &Context) -> &Attribute { pub(crate) fn create_attr(self, llcx: &Context) -> &Attribute {
unsafe { LLVMRustCreateAttrNoValue(llcx, self) } unsafe { LLVMRustCreateAttrNoValue(llcx, self) }
} }
} }
impl MemoryEffects { impl MemoryEffects {
/// Create an LLVM Attribute with these memory effects. /// Create an LLVM Attribute with these memory effects.
pub fn create_attr(self, llcx: &Context) -> &Attribute { pub(crate) fn create_attr(self, llcx: &Context) -> &Attribute {
unsafe { LLVMRustCreateMemoryEffectsAttr(llcx, self) } unsafe { LLVMRustCreateMemoryEffectsAttr(llcx, self) }
} }
} }
pub fn set_section(llglobal: &Value, section_name: &CStr) { pub(crate) fn set_section(llglobal: &Value, section_name: &CStr) {
unsafe { unsafe {
LLVMSetSection(llglobal, section_name.as_ptr()); LLVMSetSection(llglobal, section_name.as_ptr());
} }
} }
pub fn add_global<'a>(llmod: &'a Module, ty: &'a Type, name_cstr: &CStr) -> &'a Value { pub(crate) fn add_global<'a>(llmod: &'a Module, ty: &'a Type, name_cstr: &CStr) -> &'a Value {
unsafe { LLVMAddGlobal(llmod, ty, name_cstr.as_ptr()) } unsafe { LLVMAddGlobal(llmod, ty, name_cstr.as_ptr()) }
} }
pub fn set_initializer(llglobal: &Value, constant_val: &Value) { pub(crate) fn set_initializer(llglobal: &Value, constant_val: &Value) {
unsafe { unsafe {
LLVMSetInitializer(llglobal, constant_val); LLVMSetInitializer(llglobal, constant_val);
} }
} }
pub fn set_global_constant(llglobal: &Value, is_constant: bool) { pub(crate) fn set_global_constant(llglobal: &Value, is_constant: bool) {
unsafe { unsafe {
LLVMSetGlobalConstant(llglobal, if is_constant { ffi::True } else { ffi::False }); LLVMSetGlobalConstant(llglobal, if is_constant { ffi::True } else { ffi::False });
} }
} }
pub fn get_linkage(llglobal: &Value) -> Linkage { pub(crate) fn get_linkage(llglobal: &Value) -> Linkage {
unsafe { LLVMGetLinkage(llglobal) }.to_rust() unsafe { LLVMGetLinkage(llglobal) }.to_rust()
} }
pub fn set_linkage(llglobal: &Value, linkage: Linkage) { pub(crate) fn set_linkage(llglobal: &Value, linkage: Linkage) {
unsafe { unsafe {
LLVMSetLinkage(llglobal, linkage); LLVMSetLinkage(llglobal, linkage);
} }
} }
pub fn is_declaration(llglobal: &Value) -> bool { pub(crate) fn is_declaration(llglobal: &Value) -> bool {
unsafe { LLVMIsDeclaration(llglobal) == ffi::True } unsafe { LLVMIsDeclaration(llglobal) == ffi::True }
} }
pub fn get_visibility(llglobal: &Value) -> Visibility { pub(crate) fn get_visibility(llglobal: &Value) -> Visibility {
unsafe { LLVMGetVisibility(llglobal) }.to_rust() unsafe { LLVMGetVisibility(llglobal) }.to_rust()
} }
pub fn set_visibility(llglobal: &Value, visibility: Visibility) { pub(crate) fn set_visibility(llglobal: &Value, visibility: Visibility) {
unsafe { unsafe {
LLVMSetVisibility(llglobal, visibility); LLVMSetVisibility(llglobal, visibility);
} }
} }
pub fn set_alignment(llglobal: &Value, align: Align) { pub(crate) fn set_alignment(llglobal: &Value, align: Align) {
unsafe { unsafe {
ffi::LLVMSetAlignment(llglobal, align.bytes() as c_uint); ffi::LLVMSetAlignment(llglobal, align.bytes() as c_uint);
} }
@ -265,7 +273,7 @@ pub fn set_alignment(llglobal: &Value, align: Align) {
/// ///
/// Inserts the comdat into `llmod` if it does not exist. /// Inserts the comdat into `llmod` if it does not exist.
/// It is an error to call this if the target does not support comdat. /// It is an error to call this if the target does not support comdat.
pub fn set_comdat(llmod: &Module, llglobal: &Value, name: &CStr) { pub(crate) fn set_comdat(llmod: &Module, llglobal: &Value, name: &CStr) {
unsafe { unsafe {
let comdat = LLVMGetOrInsertComdat(llmod, name.as_ptr()); let comdat = LLVMGetOrInsertComdat(llmod, name.as_ptr());
LLVMSetComdat(llglobal, comdat); LLVMSetComdat(llglobal, comdat);
@ -273,7 +281,7 @@ pub fn set_comdat(llmod: &Module, llglobal: &Value, name: &CStr) {
} }
/// Safe wrapper around `LLVMGetParam`, because segfaults are no fun. /// Safe wrapper around `LLVMGetParam`, because segfaults are no fun.
pub fn get_param(llfn: &Value, index: c_uint) -> &Value { pub(crate) fn get_param(llfn: &Value, index: c_uint) -> &Value {
unsafe { unsafe {
assert!( assert!(
index < LLVMCountParams(llfn), index < LLVMCountParams(llfn),
@ -286,7 +294,7 @@ pub fn get_param(llfn: &Value, index: c_uint) -> &Value {
} }
/// Safe wrapper for `LLVMGetValueName2` into a byte slice /// Safe wrapper for `LLVMGetValueName2` into a byte slice
pub fn get_value_name(value: &Value) -> &[u8] { pub(crate) fn get_value_name(value: &Value) -> &[u8] {
unsafe { unsafe {
let mut len = 0; let mut len = 0;
let data = LLVMGetValueName2(value, &mut len); let data = LLVMGetValueName2(value, &mut len);
@ -295,28 +303,28 @@ pub fn get_value_name(value: &Value) -> &[u8] {
} }
/// Safe wrapper for `LLVMSetValueName2` from a byte slice /// Safe wrapper for `LLVMSetValueName2` from a byte slice
pub fn set_value_name(value: &Value, name: &[u8]) { pub(crate) fn set_value_name(value: &Value, name: &[u8]) {
unsafe { unsafe {
let data = name.as_c_char_ptr(); let data = name.as_c_char_ptr();
LLVMSetValueName2(value, data, name.len()); LLVMSetValueName2(value, data, name.len());
} }
} }
pub fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> { pub(crate) fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
String::from_utf8(RustString::build_byte_buffer(f)) String::from_utf8(RustString::build_byte_buffer(f))
} }
pub fn build_byte_buffer(f: impl FnOnce(&RustString)) -> Vec<u8> { pub(crate) fn build_byte_buffer(f: impl FnOnce(&RustString)) -> Vec<u8> {
RustString::build_byte_buffer(f) RustString::build_byte_buffer(f)
} }
pub fn twine_to_string(tr: &Twine) -> String { pub(crate) fn twine_to_string(tr: &Twine) -> String {
unsafe { unsafe {
build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM") build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
} }
} }
pub fn last_error() -> Option<String> { pub(crate) fn last_error() -> Option<String> {
unsafe { unsafe {
let cstr = LLVMRustGetLastError(); let cstr = LLVMRustGetLastError();
if cstr.is_null() { if cstr.is_null() {