Skip to content

Commit

Permalink
Clean llvm wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaoliello committed Jan 16, 2025
1 parent 2ae9916 commit 0acbf65
Show file tree
Hide file tree
Showing 12 changed files with 631 additions and 618 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_llvm/src/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ unsafe impl Send for ModuleBuffer {}
unsafe impl Sync for ModuleBuffer {}

impl ModuleBuffer {
pub fn new(m: &llvm::Module) -> ModuleBuffer {
pub(crate) fn new(m: &llvm::Module) -> ModuleBuffer {
ModuleBuffer(unsafe { llvm::LLVMRustModuleBufferCreate(m) })
}
}
Expand Down Expand Up @@ -664,7 +664,7 @@ unsafe impl Send for ThinBuffer {}
unsafe impl Sync for ThinBuffer {}

impl ThinBuffer {
pub fn new(m: &llvm::Module, is_thin: bool, emit_summary: bool) -> ThinBuffer {
pub(crate) fn new(m: &llvm::Module, is_thin: bool, emit_summary: bool) -> ThinBuffer {
unsafe {
let buffer = llvm::LLVMRustThinLTOBufferCreate(m, is_thin, emit_summary);
ThinBuffer(buffer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct OwnedTargetMachine {
}

impl OwnedTargetMachine {
pub fn new(
pub(crate) fn new(
triple: &CStr,
cpu: &CStr,
features: &CStr,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ unsafe extern "C" fn diagnostic_handler(info: &DiagnosticInfo, user: *mut c_void
.expect("non-UTF8 diagnostic");
dcx.emit_err(FromLlvmDiag { message });
}
llvm::diagnostic::UnknownDiagnostic(..) => {}
llvm::diagnostic::UnknownDiagnostic => {}
}
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_codegen_llvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use std::mem::ManuallyDrop;
use back::owned_target_machine::OwnedTargetMachine;
use back::write::{create_informational_target_machine, create_target_machine};
use errors::{AutoDiffWithoutLTO, ParseTargetMachineConfig};
pub use llvm_util::target_features_cfg;
pub(crate) use llvm_util::target_features_cfg;
use rustc_ast::expand::allocator::AllocatorKind;
use rustc_ast::expand::autodiff_attrs::AutoDiffItem;
use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
Expand Down Expand Up @@ -75,8 +75,8 @@ mod intrinsic;
// The following is a workaround that replaces `pub mod llvm;` and that fixes issue 53912.
#[path = "llvm/mod.rs"]
mod llvm_;
pub mod llvm {
pub use super::llvm_::*;
pub(crate) mod llvm {
pub(crate) use super::llvm_::*;
}

mod llvm_util;
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_codegen_llvm/src/llvm/archive_ro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use std::{slice, str};

use rustc_fs_util::path_to_c_string;

pub struct ArchiveRO {
pub(crate) struct ArchiveRO {
pub raw: &'static mut super::Archive,
}

unsafe impl Send for ArchiveRO {}

pub struct Iter<'a> {
pub(crate) struct Iter<'a> {
raw: &'a mut super::ArchiveIterator<'a>,
}

pub struct Child<'a> {
pub(crate) struct Child<'a> {
pub raw: &'a mut super::ArchiveChild<'a>,
}

Expand All @@ -26,7 +26,7 @@ impl ArchiveRO {
///
/// If this archive is used with a mutable method, then an error will be
/// raised.
pub fn open(dst: &Path) -> Result<ArchiveRO, String> {
pub(crate) fn open(dst: &Path) -> Result<ArchiveRO, String> {
unsafe {
let s = path_to_c_string(dst);
let ar = super::LLVMRustOpenArchive(s.as_ptr()).ok_or_else(|| {
Expand All @@ -36,7 +36,7 @@ impl ArchiveRO {
}
}

pub fn iter(&self) -> Iter<'_> {
pub(crate) fn iter(&self) -> Iter<'_> {
unsafe { Iter { raw: super::LLVMRustArchiveIteratorNew(self.raw) } }
}
}
Expand Down Expand Up @@ -71,7 +71,7 @@ impl<'a> Drop for Iter<'a> {
}

impl<'a> Child<'a> {
pub fn name(&self) -> Option<&'a str> {
pub(crate) fn name(&self) -> Option<&'a str> {
unsafe {
let mut name_len = 0;
let name_ptr = super::LLVMRustArchiveChildName(self.raw, &mut name_len);
Expand Down
31 changes: 14 additions & 17 deletions compiler/rustc_codegen_llvm/src/llvm/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
use libc::c_uint;
use rustc_span::InnerSpan;

pub use self::Diagnostic::*;
pub use self::OptimizationDiagnosticKind::*;
pub(crate) use self::Diagnostic::*;
pub(crate) use self::OptimizationDiagnosticKind::*;
use super::{DiagnosticInfo, SMDiagnostic};
use crate::value::Value;

#[derive(Copy, Clone, Debug)]
pub enum OptimizationDiagnosticKind {
pub(crate) enum OptimizationDiagnosticKind {
OptimizationRemark,
OptimizationMissed,
OptimizationAnalysis,
Expand All @@ -19,18 +18,17 @@ pub enum OptimizationDiagnosticKind {
OptimizationRemarkOther,
}

pub struct OptimizationDiagnostic<'ll> {
pub(crate) struct OptimizationDiagnostic {
pub kind: OptimizationDiagnosticKind,
pub pass_name: String,
pub function: &'ll Value,
pub line: c_uint,
pub column: c_uint,
pub filename: String,
pub message: String,
}

impl<'ll> OptimizationDiagnostic<'ll> {
unsafe fn unpack(kind: OptimizationDiagnosticKind, di: &'ll DiagnosticInfo) -> Self {
impl OptimizationDiagnostic {
unsafe fn unpack(kind: OptimizationDiagnosticKind, di: &DiagnosticInfo) -> Self {
let mut function = None;
let mut line = 0;
let mut column = 0;
Expand Down Expand Up @@ -64,7 +62,6 @@ impl<'ll> OptimizationDiagnostic<'ll> {
OptimizationDiagnostic {
kind,
pass_name: pass_name.expect("got a non-UTF8 pass name from LLVM"),
function: function.unwrap(),
line,
column,
filename,
Expand All @@ -73,14 +70,14 @@ impl<'ll> OptimizationDiagnostic<'ll> {
}
}

pub struct SrcMgrDiagnostic {
pub(crate) struct SrcMgrDiagnostic {
pub level: super::DiagnosticLevel,
pub message: String,
pub source: Option<(String, Vec<InnerSpan>)>,
}

impl SrcMgrDiagnostic {
pub unsafe fn unpack(diag: &SMDiagnostic) -> SrcMgrDiagnostic {
pub(crate) unsafe fn unpack(diag: &SMDiagnostic) -> SrcMgrDiagnostic {
// Recover the post-substitution assembly code from LLVM for better
// diagnostics.
let mut have_source = false;
Expand Down Expand Up @@ -120,7 +117,7 @@ impl SrcMgrDiagnostic {
}

#[derive(Clone)]
pub struct InlineAsmDiagnostic {
pub(crate) struct InlineAsmDiagnostic {
pub level: super::DiagnosticLevel,
pub cookie: u64,
pub message: String,
Expand Down Expand Up @@ -158,19 +155,19 @@ impl InlineAsmDiagnostic {
}
}

pub enum Diagnostic<'ll> {
Optimization(OptimizationDiagnostic<'ll>),
pub(crate) enum Diagnostic<'ll> {
Optimization(OptimizationDiagnostic),
InlineAsm(InlineAsmDiagnostic),
PGO(&'ll DiagnosticInfo),
Linker(&'ll DiagnosticInfo),
Unsupported(&'ll DiagnosticInfo),

/// LLVM has other types that we do not wrap here.
UnknownDiagnostic(&'ll DiagnosticInfo),
UnknownDiagnostic,
}

impl<'ll> Diagnostic<'ll> {
pub unsafe fn unpack(di: &'ll DiagnosticInfo) -> Self {
pub(crate) unsafe fn unpack(di: &'ll DiagnosticInfo) -> Self {
use super::DiagnosticKind as Dk;

unsafe {
Expand Down Expand Up @@ -210,7 +207,7 @@ impl<'ll> Diagnostic<'ll> {

Dk::SrcMgr => InlineAsm(InlineAsmDiagnostic::unpackSrcMgr(di)),

_ => UnknownDiagnostic(di),
_ => UnknownDiagnostic,
}
}
}
Expand Down
25 changes: 13 additions & 12 deletions compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
#![allow(non_camel_case_types)]
#![expect(dead_code)]

use libc::{c_char, c_uint};

use super::ffi::{BasicBlock, Metadata, Module, Type, Value};
use crate::llvm::Bool;
extern "C" {
// Enzyme
pub fn LLVMRustHasMetadata(I: &Value, KindID: c_uint) -> bool;
pub fn LLVMRustEraseInstBefore(BB: &BasicBlock, I: &Value);
pub fn LLVMRustGetLastInstruction<'a>(BB: &BasicBlock) -> Option<&'a Value>;
pub fn LLVMRustDIGetInstMetadata(I: &Value) -> Option<&Metadata>;
pub fn LLVMRustEraseInstFromParent(V: &Value);
pub fn LLVMRustGetTerminator<'a>(B: &BasicBlock) -> &'a Value;
pub fn LLVMRustVerifyFunction(V: &Value, action: LLVMRustVerifierFailureAction) -> Bool;
pub(crate) fn LLVMRustHasMetadata(I: &Value, KindID: c_uint) -> bool;
pub(crate) fn LLVMRustEraseInstBefore(BB: &BasicBlock, I: &Value);
pub(crate) fn LLVMRustGetLastInstruction<'a>(BB: &BasicBlock) -> Option<&'a Value>;
pub(crate) fn LLVMRustDIGetInstMetadata(I: &Value) -> Option<&Metadata>;
pub(crate) fn LLVMRustEraseInstFromParent(V: &Value);
pub(crate) fn LLVMRustGetTerminator<'a>(B: &BasicBlock) -> &'a Value;
pub(crate) fn LLVMRustVerifyFunction(V: &Value, action: LLVMRustVerifierFailureAction) -> Bool;

pub fn LLVMGetFunctionCallConv(F: &Value) -> c_uint;
pub fn LLVMGetReturnType(T: &Type) -> &Type;
pub fn LLVMGetParams(Fnc: &Value, parms: *mut &Value);
pub fn LLVMGetNamedFunction(M: &Module, Name: *const c_char) -> Option<&Value>;
pub(crate) fn LLVMGetFunctionCallConv(F: &Value) -> c_uint;
pub(crate) fn LLVMGetReturnType(T: &Type) -> &Type;
pub(crate) fn LLVMGetParams(Fnc: &Value, parms: *mut &Value);
pub(crate) fn LLVMGetNamedFunction(M: &Module, Name: *const c_char) -> Option<&Value>;
}

#[repr(C)]
#[derive(Copy, Clone, PartialEq)]
pub enum LLVMRustVerifierFailureAction {
pub(crate) enum LLVMRustVerifierFailureAction {
LLVMAbortProcessAction = 0,
LLVMPrintMessageAction = 1,
LLVMReturnStatusAction = 2,
Expand Down
Loading

0 comments on commit 0acbf65

Please sign in to comment.