Skip to content

Commit

Permalink
Improve code to generate static items
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Apr 2, 2024
1 parent cbd94ff commit 1563a93
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
16 changes: 7 additions & 9 deletions src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(feature = "master")]
use gccjit::{FnAttribute, VarAttribute, Visibility};
use gccjit::{Function, GlobalKind, LValue, RValue, ToRValue};
use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods, DerivedTypeMethods, StaticMethods};
use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods, StaticMethods};
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
use rustc_middle::mir::interpret::{
self, read_target_uint, ConstAllocation, ErrorHandled, Scalar as InterpScalar,
Expand Down Expand Up @@ -66,12 +66,12 @@ impl<'gcc, 'tcx> StaticMethods for CodegenCx<'gcc, 'tcx> {
fn codegen_static(&self, def_id: DefId) {
let attrs = self.tcx.codegen_fn_attrs(def_id);

let value = match codegen_static_initializer(self, def_id) {
Ok((value, _)) => value,
let Ok((value, alloc)) = codegen_static_initializer(self, def_id) else {
// Error has already been reported
Err(_) => return,
return;
};

let alloc = alloc.inner();
let global = self.get_static(def_id);

// boolean SSA values are i1, but they have to be stored in i8 slots,
Expand All @@ -81,18 +81,16 @@ impl<'gcc, 'tcx> StaticMethods for CodegenCx<'gcc, 'tcx> {
unimplemented!();
};

let instance = Instance::mono(self.tcx, def_id);
let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
let gcc_type = self.layout_of(ty).gcc_type(self);
let gcc_type = self.val_ty(global.to_rvalue());

set_global_alignment(self, global, self.align_of(ty));
set_global_alignment(self, global, alloc.align);

let value = self.bitcast_if_needed(value, gcc_type);
global.global_set_initializer_rvalue(value);

// As an optimization, all shared statics which do not have interior
// mutability are placed into read-only memory.
if !self.tcx.static_mutability(def_id).unwrap().is_mut() && self.type_is_freeze(ty) {
if !alloc.mutability.is_not() {
#[cfg(feature = "master")]
global.global_set_readonly();
}
Expand Down
10 changes: 2 additions & 8 deletions src/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
use rustc_target::abi::call::{CastTarget, FnAbi, Reg};
use rustc_target::abi::{
self, Abi, Align, FieldsShape, Int, Integer, PointeeInfo, Pointer, Size, TyAbiInterface,
Variants, F128, F16, F32, F64,
self, Abi, FieldsShape, Int, Integer, PointeeInfo, Pointer, Size, TyAbiInterface, Variants,
F128, F16, F32, F64,
};

use crate::abi::{FnAbiGcc, FnAbiGccExt, GccType};
Expand Down Expand Up @@ -53,12 +53,6 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
}
}

impl<'a, 'tcx> CodegenCx<'a, 'tcx> {
pub fn align_of(&self, ty: Ty<'tcx>) -> Align {
self.layout_of(ty).align.abi
}
}

fn uncached_gcc_type<'gcc, 'tcx>(
cx: &CodegenCx<'gcc, 'tcx>,
layout: TyAndLayout<'tcx>,
Expand Down

0 comments on commit 1563a93

Please sign in to comment.