Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several changes to the codegen backend organization #65340

Merged
merged 10 commits into from
Oct 15, 2019
1 change: 1 addition & 0 deletions src/librustc_codegen_llvm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const UNNAMED: *const c_char = EMPTY_C_STR.as_ptr();

impl BackendTypes for Builder<'_, 'll, 'tcx> {
type Value = <CodegenCx<'ll, 'tcx> as BackendTypes>::Value;
type Function = <CodegenCx<'ll, 'tcx> as BackendTypes>::Function;
type BasicBlock = <CodegenCx<'ll, 'tcx> as BackendTypes>::BasicBlock;
type Type = <CodegenCx<'ll, 'tcx> as BackendTypes>::Type;
type Funclet = <CodegenCx<'ll, 'tcx> as BackendTypes>::Funclet;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn get_fn(
assert!(!instance.substs.has_param_types());

let sig = instance.fn_sig(cx.tcx());
if let Some(&llfn) = cx.instances().borrow().get(&instance) {
if let Some(&llfn) = cx.instances.borrow().get(&instance) {
return llfn;
}

Expand Down
42 changes: 20 additions & 22 deletions src/librustc_codegen_llvm/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! Code that is useful in various codegen modules.

use crate::llvm::{self, True, False, Bool, BasicBlock, OperandBundleDef};
use crate::llvm::{self, True, False, Bool, BasicBlock, OperandBundleDef, ConstantInt};
use crate::abi;
use crate::consts;
use crate::type_::Type;
Expand Down Expand Up @@ -86,6 +86,8 @@ impl Funclet<'ll> {

impl BackendTypes for CodegenCx<'ll, 'tcx> {
type Value = &'ll Value;
type Function = &'ll Value;

type BasicBlock = &'ll BasicBlock;
type Type = &'ll Type;
type Funclet = Funclet<'ll>;
Expand Down Expand Up @@ -243,33 +245,23 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
struct_in_context(self.llcx, elts, packed)
}

fn const_to_uint(&self, v: &'ll Value) -> u64 {
unsafe {
fn const_to_opt_uint(&self, v: &'ll Value) -> Option<u64> {
try_as_const_integral(v).map(|v| unsafe {
llvm::LLVMConstIntGetZExtValue(v)
}
}

fn is_const_integral(&self, v: &'ll Value) -> bool {
unsafe {
llvm::LLVMIsAConstantInt(v).is_some()
}
})
}

fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {
unsafe {
if self.is_const_integral(v) {
let (mut lo, mut hi) = (0u64, 0u64);
let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
&mut hi, &mut lo);
if success {
Some(hi_lo_to_u128(lo, hi))
} else {
None
}
try_as_const_integral(v).and_then(|v| unsafe {
let (mut lo, mut hi) = (0u64, 0u64);
let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
&mut hi, &mut lo);
if success {
Some(hi_lo_to_u128(lo, hi))
} else {
None
}
}
})
}

fn scalar_to_backend(
Expand Down Expand Up @@ -305,7 +297,7 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
}
}
Some(GlobalAlloc::Function(fn_instance)) => {
self.get_fn(fn_instance)
self.get_fn_addr(fn_instance)
}
Some(GlobalAlloc::Static(def_id)) => {
assert!(self.tcx.is_static(def_id));
Expand Down Expand Up @@ -386,3 +378,9 @@ pub fn struct_in_context(
fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
((hi as u128) << 64) | (lo as u128)
}

fn try_as_const_integral(v: &'ll Value) -> Option<&'ll ConstantInt> {
unsafe {
llvm::LLVMIsAConstantInt(v)
}
}
25 changes: 19 additions & 6 deletions src/librustc_codegen_llvm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use rustc::ty::layout::{
use rustc::ty::{self, Ty, TyCtxt, Instance};
use rustc::util::nodemap::FxHashMap;
use rustc_target::spec::{HasTargetSpec, Target};
use rustc_codegen_ssa::callee::resolve_and_get_fn;
use rustc_codegen_ssa::base::wants_msvc_seh;
use crate::callee::get_fn;

Expand Down Expand Up @@ -327,11 +326,11 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
&self.vtables
}

fn instances(&self) -> &RefCell<FxHashMap<Instance<'tcx>, &'ll Value>> {
&self.instances
fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
get_fn(self, instance)
}

fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
fn get_fn_addr(&self, instance: Instance<'tcx>) -> &'ll Value {
get_fn(self, instance)
}

Expand Down Expand Up @@ -362,7 +361,14 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
let tcx = self.tcx;
let llfn = match tcx.lang_items().eh_personality() {
Some(def_id) if !wants_msvc_seh(self.sess()) => {
resolve_and_get_fn(self, def_id, tcx.intern_substs(&[]))
self.get_fn_addr(
ty::Instance::resolve(
tcx,
ty::ParamEnv::reveal_all(),
def_id,
tcx.intern_substs(&[]),
).unwrap()
)
}
_ => {
let name = if wants_msvc_seh(self.sess()) {
Expand Down Expand Up @@ -390,7 +396,14 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
let tcx = self.tcx;
assert!(self.sess().target.target.options.custom_unwind_resume);
if let Some(def_id) = tcx.lang_items().eh_unwind_resume() {
let llfn = resolve_and_get_fn(self, def_id, tcx.intern_substs(&[]));
let llfn = self.get_fn_addr(
ty::Instance::resolve(
tcx,
ty::ParamEnv::reveal_all(),
def_id,
tcx.intern_substs(&[]),
).unwrap()
);
unwresume.set(Some(llfn));
return llfn;
}
Expand Down
38 changes: 0 additions & 38 deletions src/librustc_codegen_llvm/error_codes.rs

This file was deleted.

6 changes: 1 addition & 5 deletions src/librustc_codegen_llvm/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
use rustc::hir;
use syntax::ast::{self, FloatTy};

use rustc_codegen_ssa::common::span_invalid_monomorphization_error;
use rustc_codegen_ssa::traits::*;

use rustc::session::Session;
use syntax_pos::Span;

use std::cmp::Ordering;
Expand Down Expand Up @@ -1026,10 +1026,6 @@ fn get_rust_try_fn<'ll, 'tcx>(
rust_try
}

fn span_invalid_monomorphization_error(a: &Session, b: Span, c: &str) {
span_err!(a, b, E0511, "{}", c);
}

fn generic_simd_intrinsic(
bx: &mut Builder<'a, 'll, 'tcx>,
name: &str,
Expand Down
13 changes: 1 addition & 12 deletions src/librustc_codegen_llvm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extern crate rustc_fs_util;
extern crate rustc_driver as _;

#[macro_use] extern crate log;
#[macro_use] extern crate syntax;
extern crate syntax;
extern crate syntax_pos;
extern crate rustc_errors as errors;

Expand All @@ -64,8 +64,6 @@ use rustc::util::common::ErrorReported;
use rustc_codegen_ssa::ModuleCodegen;
use rustc_codegen_utils::codegen_backend::CodegenBackend;

mod error_codes;

mod back {
pub mod archive;
pub mod bytecode;
Expand Down Expand Up @@ -258,10 +256,6 @@ impl CodegenBackend for LlvmCodegenBackend {
llvm_util::print_version();
}

fn diagnostics(&self) -> &[(&'static str, &'static str)] {
&error_codes::DIAGNOSTICS
}

fn target_features(&self, sess: &Session) -> Vec<Symbol> {
target_features(sess)
}
Expand All @@ -271,15 +265,10 @@ impl CodegenBackend for LlvmCodegenBackend {
}

fn provide(&self, providers: &mut ty::query::Providers<'_>) {
rustc_codegen_utils::symbol_names::provide(providers);
rustc_codegen_ssa::back::symbol_export::provide(providers);
rustc_codegen_ssa::base::provide_both(providers);
attributes::provide(providers);
}

fn provide_extern(&self, providers: &mut ty::query::Providers<'_>) {
rustc_codegen_ssa::back::symbol_export::provide_extern(providers);
rustc_codegen_ssa::base::provide_both(providers);
attributes::provide_extern(providers);
}

Expand Down
7 changes: 4 additions & 3 deletions src/librustc_codegen_llvm/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ extern { pub type Module; }
extern { pub type Context; }
extern { pub type Type; }
extern { pub type Value; }
extern { pub type ConstantInt; }
extern { pub type Metadata; }
extern { pub type BasicBlock; }
#[repr(C)]
Expand Down Expand Up @@ -719,8 +720,8 @@ extern "C" {
pub fn LLVMConstInt(IntTy: &Type, N: c_ulonglong, SignExtend: Bool) -> &Value;
pub fn LLVMConstIntOfArbitraryPrecision(IntTy: &Type, Wn: c_uint, Ws: *const u64) -> &Value;
pub fn LLVMConstReal(RealTy: &Type, N: f64) -> &Value;
pub fn LLVMConstIntGetZExtValue(ConstantVal: &Value) -> c_ulonglong;
pub fn LLVMRustConstInt128Get(ConstantVal: &Value, SExt: bool,
pub fn LLVMConstIntGetZExtValue(ConstantVal: &ConstantInt) -> c_ulonglong;
pub fn LLVMRustConstInt128Get(ConstantVal: &ConstantInt, SExt: bool,
high: &mut u64, low: &mut u64) -> bool;


Expand Down Expand Up @@ -1666,7 +1667,7 @@ extern "C" {
#[allow(improper_ctypes)]
pub fn LLVMRustWriteValueToString(value_ref: &Value, s: &RustString);

pub fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&Value>;
pub fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&ConstantInt>;

pub fn LLVMRustPassKind(Pass: &Pass) -> PassKind;
pub fn LLVMRustFindAndCreatePass(Pass: *const c_char) -> Option<&'static mut Pass>;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub trait BuilderMethods<'a, 'tcx>:
{
fn new_block<'b>(
cx: &'a Self::CodegenCx,
llfn: Self::Value,
llfn: Self::Function,
name: &'b str
) -> Self;
/* ... */
Expand Down
17 changes: 9 additions & 8 deletions src/librustc_codegen_ssa/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use crate::mir::place::PlaceRef;
use crate::back::write::{OngoingCodegen, start_async_codegen, submit_pre_lto_module_to_llvm,
submit_post_lto_module_to_llvm};
use crate::{MemFlags, CrateInfo};
use crate::callee;
use crate::common::{RealPredicate, TypeKind, IntPredicate};
use crate::meth;
use crate::mir;
Expand Down Expand Up @@ -377,8 +376,7 @@ pub fn codegen_instance<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
let sig = instance.fn_sig(cx.tcx());
let sig = cx.tcx().normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);

let lldecl = cx.instances().borrow().get(&instance).cloned().unwrap_or_else(||
bug!("Instance `{:?}` not already declared", instance));
let lldecl = cx.get_fn(instance);

let mir = cx.tcx().instance_mir(instance.def);
mir::codegen_mir::<Bx>(cx, lldecl, &mir, instance, sig);
Expand All @@ -400,7 +398,7 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(cx: &'
return;
}

let main_llfn = cx.get_fn(instance);
let main_llfn = cx.get_fn_addr(instance);

let et = cx.tcx().entry_fn(LOCAL_CRATE).map(|e| e.1);
match et {
Expand Down Expand Up @@ -455,10 +453,13 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(cx: &'

let (start_fn, args) = if use_start_lang_item {
let start_def_id = cx.tcx().require_lang_item(StartFnLangItem, None);
let start_fn = callee::resolve_and_get_fn(
cx,
start_def_id,
cx.tcx().intern_substs(&[main_ret_ty.into()]),
let start_fn = cx.get_fn_addr(
ty::Instance::resolve(
cx.tcx(),
ty::ParamEnv::reveal_all(),
start_def_id,
cx.tcx().intern_substs(&[main_ret_ty.into()]),
).unwrap()
);
(start_fn, vec![bx.pointercast(rust_main, cx.type_ptr_to(cx.type_i8p())),
arg_argc, arg_argv])
Expand Down
53 changes: 0 additions & 53 deletions src/librustc_codegen_ssa/callee.rs

This file was deleted.

Loading