Skip to content

Commit 303f231

Browse files
authored
Rollup merge of rust-lang#58609 - gabi-250:mutable-refs, r=oli-obk
Allow Self::Module to be mutated. This only changes `&Self::Module` to `&mut Self::Module` in a couple of places. `codegen_allocator` and `write_metadata` from `ExtraBackendMethods` mutate the underlying LLVM module. As such, it makes sense for these two functions to receive a mutable reference to the module (as opposed to an immutable one). I am trying to implement `codegen_allocator` for my backend, and I need to be able to mutate `Self::Module`: https://github.com/rust-lang/rust/blob/f66e4697ae286985ddefc53c3a047614568458bb/src/librustc_codegen_ssa/traits/backend.rs#L41 Modifying the module in `codegen_allocator`/`write_metadata` is not a problem for the LLVM backend, because [ModuleLlvm](https://github.com/rust-lang/rust/blob/master/src/librustc_codegen_llvm/lib.rs#L357) contains a raw pointer to the underlying LLVM module, so it can easily be mutated through FFI calls. I am trying to avoid interior mutability and `unsafe` as much as I can. What do you think? Does this change make sense, or is there a reason why this should stay the way it is?
2 parents 1775299 + e5d1fa5 commit 303f231

File tree

5 files changed

+10
-10
lines changed

5 files changed

+10
-10
lines changed

src/librustc_codegen_llvm/allocator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_allocator::{ALLOCATOR_METHODS, AllocatorTy};
99
use crate::ModuleLlvm;
1010
use crate::llvm::{self, False, True};
1111

12-
pub(crate) unsafe fn codegen(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind) {
12+
pub(crate) unsafe fn codegen(tcx: TyCtxt, mods: &mut ModuleLlvm, kind: AllocatorKind) {
1313
let llcx = &*mods.llcx;
1414
let llmod = mods.llmod();
1515
let usize = match &tcx.sess.target.target.target_pointer_width[..] {

src/librustc_codegen_llvm/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use crate::value::Value;
4646

4747
pub fn write_metadata<'a, 'gcx>(
4848
tcx: TyCtxt<'a, 'gcx, 'gcx>,
49-
llvm_module: &ModuleLlvm
49+
llvm_module: &mut ModuleLlvm
5050
) -> EncodedMetadata {
5151
use std::io::Write;
5252
use flate2::Compression;

src/librustc_codegen_llvm/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
120120
fn write_metadata<'b, 'gcx>(
121121
&self,
122122
tcx: TyCtxt<'b, 'gcx, 'gcx>,
123-
metadata: &ModuleLlvm
123+
metadata: &mut ModuleLlvm
124124
) -> EncodedMetadata {
125125
base::write_metadata(tcx, metadata)
126126
}
127-
fn codegen_allocator(&self, tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind) {
127+
fn codegen_allocator(&self, tcx: TyCtxt, mods: &mut ModuleLlvm, kind: AllocatorKind) {
128128
unsafe { allocator::codegen(tcx, mods, kind) }
129129
}
130130
fn compile_codegen_unit<'a, 'tcx: 'a>(

src/librustc_codegen_ssa/base.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,9 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
551551
&["crate"],
552552
Some("metadata")).as_str()
553553
.to_string();
554-
let metadata_llvm_module = backend.new_metadata(tcx, &metadata_cgu_name);
554+
let mut metadata_llvm_module = backend.new_metadata(tcx, &metadata_cgu_name);
555555
let metadata = time(tcx.sess, "write metadata", || {
556-
backend.write_metadata(tcx, &metadata_llvm_module)
556+
backend.write_metadata(tcx, &mut metadata_llvm_module)
557557
});
558558
tcx.sess.profiler(|p| p.end_activity(ProfileCategory::Codegen));
559559

@@ -636,9 +636,9 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
636636
&["crate"],
637637
Some("allocator")).as_str()
638638
.to_string();
639-
let modules = backend.new_metadata(tcx, &llmod_id);
639+
let mut modules = backend.new_metadata(tcx, &llmod_id);
640640
time(tcx.sess, "write allocator module", || {
641-
backend.codegen_allocator(tcx, &modules, kind)
641+
backend.codegen_allocator(tcx, &mut modules, kind)
642642
});
643643

644644
Some(ModuleCodegen {

src/librustc_codegen_ssa/traits/backend.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Se
3636
fn write_metadata<'b, 'gcx>(
3737
&self,
3838
tcx: TyCtxt<'b, 'gcx, 'gcx>,
39-
metadata: &Self::Module,
39+
metadata: &mut Self::Module,
4040
) -> EncodedMetadata;
41-
fn codegen_allocator(&self, tcx: TyCtxt, mods: &Self::Module, kind: AllocatorKind);
41+
fn codegen_allocator(&self, tcx: TyCtxt, mods: &mut Self::Module, kind: AllocatorKind);
4242
fn compile_codegen_unit<'a, 'tcx: 'a>(
4343
&self,
4444
tcx: TyCtxt<'a, 'tcx, 'tcx>,

0 commit comments

Comments
 (0)