From 474a7168abf4237b14537c59541a64efea2dfa68 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 12 Feb 2026 18:01:59 +0000 Subject: [PATCH] Remove explicit EmitThinLTOSummary argument In favor of passing a NULL ThinLTOSummaryBufferRef. And improve type improve type safety on the Rust side. --- compiler/rustc_codegen_llvm/src/back/lto.rs | 7 +------ compiler/rustc_codegen_llvm/src/back/write.rs | 18 +++++++----------- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 5 ++--- .../rustc_llvm/llvm-wrapper/PassWrapper.cpp | 19 ++++++++++++------- 4 files changed, 22 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/lto.rs b/compiler/rustc_codegen_llvm/src/back/lto.rs index 3b1a425f7d91..303f8761fe41 100644 --- a/compiler/rustc_codegen_llvm/src/back/lto.rs +++ b/compiler/rustc_codegen_llvm/src/back/lto.rs @@ -2,7 +2,6 @@ use std::ffi::{CStr, CString}; use std::fs::File; use std::path::{Path, PathBuf}; -use std::ptr::NonNull; use std::sync::Arc; use std::{io, iter, slice}; @@ -660,17 +659,13 @@ pub(crate) fn run_pass_manager( debug!("lto done"); } +#[repr(transparent)] pub(crate) struct Buffer(&'static mut llvm::Buffer); unsafe impl Send for Buffer {} unsafe impl Sync for Buffer {} impl Buffer { - pub(crate) unsafe fn from_raw_ptr(ptr: *mut llvm::Buffer) -> Buffer { - let mut ptr = NonNull::new(ptr).unwrap(); - Buffer(unsafe { ptr.as_mut() }) - } - pub(crate) fn data(&self) -> &[u8] { unsafe { let ptr = llvm::LLVMRustBufferPtr(self.0); diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index 9bb6045084d7..d7ab1356fafe 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -1,7 +1,6 @@ use std::ffi::{CStr, CString}; use std::io::{self, Write}; use std::path::{Path, PathBuf}; -use std::ptr::null_mut; use std::sync::Arc; use std::{fs, slice, str}; @@ -29,7 +28,7 @@ }; use tracing::{debug, trace}; -use crate::back::lto::ModuleBuffer; +use crate::back::lto::{Buffer, ModuleBuffer}; use crate::back::owned_target_machine::OwnedTargetMachine; use crate::back::profiling::{ LlvmSelfProfiler, selfprofile_after_pass_callback, selfprofile_before_pass_callback, @@ -563,8 +562,8 @@ pub(crate) unsafe fn llvm_optimize( prof: &SelfProfilerRef, dcx: DiagCtxtHandle<'_>, module: &ModuleCodegen, - thin_lto_buffer: Option<&mut *mut llvm::Buffer>, - thin_lto_summary_buffer: Option<&mut *mut llvm::Buffer>, + thin_lto_buffer: Option<&mut Option>, + thin_lto_summary_buffer: Option<&mut Option>, config: &ModuleConfig, opt_level: config::OptLevel, opt_stage: llvm::OptStage, @@ -788,7 +787,6 @@ fn handle_offload<'ll>(cx: &'ll SimpleCx<'_>, old_fn: &llvm::Value) { config.lint_llvm_ir, thin_lto_buffer, thin_lto_summary_buffer, - config.emit_thin_lto_summary, merge_functions, unroll_loops, vectorize_slp, @@ -939,7 +937,7 @@ pub(crate) fn optimize( && config.emit_obj == EmitObj::ObjectCode(BitcodeSection::Full)) || config.emit_thin_lto_summary { - (Some(null_mut()), Some(null_mut())) + (Some(None), config.emit_thin_lto_summary.then_some(None)) } else { (None, None) }; @@ -958,19 +956,17 @@ pub(crate) fn optimize( ) }; if let Some(thin_lto_buffer) = thin_lto_buffer { - let thin_lto_buffer = - unsafe { crate::back::lto::Buffer::from_raw_ptr(thin_lto_buffer) }; - let thin_lto_summary_buffer = - unsafe { crate::back::lto::Buffer::from_raw_ptr(thin_lto_summary_buffer.unwrap()) }; + let thin_lto_buffer = thin_lto_buffer.unwrap(); module.thin_lto_buffer = Some(thin_lto_buffer.data().to_vec()); let bc_summary_out = cgcx.output_filenames.temp_path_for_cgu( OutputType::ThinLinkBitcode, &module.name, cgcx.invocation_temp.as_deref(), ); - if config.emit_thin_lto_summary + if let Some(thin_lto_summary_buffer) = thin_lto_summary_buffer && let Some(thin_link_bitcode_filename) = bc_summary_out.file_name() { + let thin_lto_summary_buffer = thin_lto_summary_buffer.unwrap(); let summary_data = thin_lto_summary_buffer.data(); prof.artifact_size( "llvm_bitcode_summary", diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index f9fa2e276996..f9af42494cad 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -2372,9 +2372,8 @@ pub(crate) fn LLVMRustOptimize<'a>( NoPrepopulatePasses: bool, VerifyIR: bool, LintIR: bool, - ThinLTOBuffer: Option<&mut *mut Buffer>, - ThinLTOSummaryBuffer: Option<&mut *mut Buffer>, - EmitThinLTOSummary: bool, + ThinLTOBuffer: Option<&mut Option>, + ThinLTOSummaryBuffer: Option<&mut Option>, MergeFunctions: bool, UnrollLoops: bool, SLPVectorize: bool, diff --git a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp index 1ee67e952ead..568335f7dcb5 100644 --- a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp @@ -554,9 +554,9 @@ extern "C" LLVMRustResult LLVMRustOptimize( LLVMRustPassBuilderOptLevel OptLevelRust, LLVMRustOptStage OptStage, bool IsLinkerPluginLTO, bool NoPrepopulatePasses, bool VerifyIR, bool LintIR, LLVMRustBuffer **ThinLTOBufferRef, - LLVMRustBuffer **ThinLTOSummaryBufferRef, bool EmitThinLTOSummary, - bool MergeFunctions, bool UnrollLoops, bool SLPVectorize, - bool LoopVectorize, bool DisableSimplifyLibCalls, bool EmitLifetimeMarkers, + LLVMRustBuffer **ThinLTOSummaryBufferRef, bool MergeFunctions, + bool UnrollLoops, bool SLPVectorize, bool LoopVectorize, + bool DisableSimplifyLibCalls, bool EmitLifetimeMarkers, registerEnzymeAndPassPipelineFn EnzymePtr, bool PrintBeforeEnzyme, bool PrintAfterEnzyme, bool PrintPasses, LLVMRustSanitizerOptions *SanitizerOptions, const char *PGOGenPath, @@ -825,9 +825,12 @@ extern "C" LLVMRustResult LLVMRustOptimize( // `ThinLTOPreLinkDefaultPipeline`. MPM.addPass(PB.buildThinLTOPreLinkDefaultPipeline(OptLevel)); MPM.addPass(ThinLTOBitcodeWriterPass( - ThinLTODataOS, EmitThinLTOSummary ? &ThinLinkDataOS : nullptr)); + ThinLTODataOS, + ThinLTOSummaryBufferRef ? &ThinLinkDataOS : nullptr)); *ThinLTOBufferRef = ThinLTOBuffer.release(); - *ThinLTOSummaryBufferRef = ThinLTOSummaryBuffer.release(); + if (ThinLTOSummaryBufferRef) { + *ThinLTOSummaryBufferRef = ThinLTOSummaryBuffer.release(); + } MPM.addPass(PB.buildModuleOptimizationPipeline( OptLevel, ThinOrFullLTOPhase::None)); MPM.addPass( @@ -883,12 +886,14 @@ extern "C" LLVMRustResult LLVMRustOptimize( // lto is requested. See PR #136840 for background information. if (OptStage != LLVMRustOptStage::PreLinkFatLTO) { MPM.addPass(ThinLTOBitcodeWriterPass( - ThinLTODataOS, EmitThinLTOSummary ? &ThinLinkDataOS : nullptr)); + ThinLTODataOS, ThinLTOSummaryBufferRef ? &ThinLinkDataOS : nullptr)); } else { MPM.addPass(BitcodeWriterPass(ThinLTODataOS)); } *ThinLTOBufferRef = ThinLTOBuffer.release(); - *ThinLTOSummaryBufferRef = ThinLTOSummaryBuffer.release(); + if (ThinLTOSummaryBufferRef) { + *ThinLTOSummaryBufferRef = ThinLTOSummaryBuffer.release(); + } } // now load "-enzyme" pass: