Rollup merge of #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`:
f66e4697ae/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?
This commit is contained in:
Mazdak Farrokhzad 2019-02-23 09:25:30 +01:00 committed by GitHub
commit 8ccda2438e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 10 additions and 10 deletions

View File

@ -9,7 +9,7 @@
use crate::ModuleLlvm;
use crate::llvm::{self, False, True};
pub(crate) unsafe fn codegen(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind) {
pub(crate) unsafe fn codegen(tcx: TyCtxt, mods: &mut ModuleLlvm, kind: AllocatorKind) {
let llcx = &*mods.llcx;
let llmod = mods.llmod();
let usize = match &tcx.sess.target.target.target_pointer_width[..] {

View File

@ -46,7 +46,7 @@
pub fn write_metadata<'a, 'gcx>(
tcx: TyCtxt<'a, 'gcx, 'gcx>,
llvm_module: &ModuleLlvm
llvm_module: &mut ModuleLlvm
) -> EncodedMetadata {
use std::io::Write;
use flate2::Compression;

View File

@ -120,11 +120,11 @@ fn new_metadata(&self, tcx: TyCtxt, mod_name: &str) -> ModuleLlvm {
fn write_metadata<'b, 'gcx>(
&self,
tcx: TyCtxt<'b, 'gcx, 'gcx>,
metadata: &ModuleLlvm
metadata: &mut ModuleLlvm
) -> EncodedMetadata {
base::write_metadata(tcx, metadata)
}
fn codegen_allocator(&self, tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind) {
fn codegen_allocator(&self, tcx: TyCtxt, mods: &mut ModuleLlvm, kind: AllocatorKind) {
unsafe { allocator::codegen(tcx, mods, kind) }
}
fn compile_codegen_unit<'a, 'tcx: 'a>(

View File

@ -551,9 +551,9 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
&["crate"],
Some("metadata")).as_str()
.to_string();
let metadata_llvm_module = backend.new_metadata(tcx, &metadata_cgu_name);
let mut metadata_llvm_module = backend.new_metadata(tcx, &metadata_cgu_name);
let metadata = time(tcx.sess, "write metadata", || {
backend.write_metadata(tcx, &metadata_llvm_module)
backend.write_metadata(tcx, &mut metadata_llvm_module)
});
tcx.sess.profiler(|p| p.end_activity(ProfileCategory::Codegen));
@ -636,9 +636,9 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
&["crate"],
Some("allocator")).as_str()
.to_string();
let modules = backend.new_metadata(tcx, &llmod_id);
let mut modules = backend.new_metadata(tcx, &llmod_id);
time(tcx.sess, "write allocator module", || {
backend.codegen_allocator(tcx, &modules, kind)
backend.codegen_allocator(tcx, &mut modules, kind)
});
Some(ModuleCodegen {

View File

@ -36,9 +36,9 @@ pub trait ExtraBackendMethods: CodegenBackend + WriteBackendMethods + Sized + Se
fn write_metadata<'b, 'gcx>(
&self,
tcx: TyCtxt<'b, 'gcx, 'gcx>,
metadata: &Self::Module,
metadata: &mut Self::Module,
) -> EncodedMetadata;
fn codegen_allocator(&self, tcx: TyCtxt, mods: &Self::Module, kind: AllocatorKind);
fn codegen_allocator(&self, tcx: TyCtxt, mods: &mut Self::Module, kind: AllocatorKind);
fn compile_codegen_unit<'a, 'tcx: 'a>(
&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>,