Skip to content

Commit 6241a4a

Browse files
authored
Rollup merge of #65340 - bjorn3:cg_ssa_refactor4, r=eddyb
Several changes to the codegen backend organization * Split functions from values in cg_ssa `BackendTypes`. * Remove `is_const_integral` function from `ConstMethods`. * Actually register the invalid monomorphization of intrinsic long diagnostic and remove the `diagnostics` method from `CodegenBackends`, as it was unused. * Add cg_ssa and cg_utils provided methods to `default_provide`, so codegen backend don't have to do it themself.
2 parents e38639f + f0e2fc7 commit 6241a4a

29 files changed

+157
-178
lines changed

src/librustc_codegen_llvm/builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const UNNAMED: *const c_char = EMPTY_C_STR.as_ptr();
5252

5353
impl BackendTypes for Builder<'_, 'll, 'tcx> {
5454
type Value = <CodegenCx<'ll, 'tcx> as BackendTypes>::Value;
55+
type Function = <CodegenCx<'ll, 'tcx> as BackendTypes>::Function;
5556
type BasicBlock = <CodegenCx<'ll, 'tcx> as BackendTypes>::BasicBlock;
5657
type Type = <CodegenCx<'ll, 'tcx> as BackendTypes>::Type;
5758
type Funclet = <CodegenCx<'ll, 'tcx> as BackendTypes>::Funclet;

src/librustc_codegen_llvm/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn get_fn(
3333
assert!(!instance.substs.has_param_types());
3434

3535
let sig = instance.fn_sig(cx.tcx());
36-
if let Some(&llfn) = cx.instances().borrow().get(&instance) {
36+
if let Some(&llfn) = cx.instances.borrow().get(&instance) {
3737
return llfn;
3838
}
3939

src/librustc_codegen_llvm/common.rs

+20-22
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
//! Code that is useful in various codegen modules.
44
5-
use crate::llvm::{self, True, False, Bool, BasicBlock, OperandBundleDef};
5+
use crate::llvm::{self, True, False, Bool, BasicBlock, OperandBundleDef, ConstantInt};
66
use crate::abi;
77
use crate::consts;
88
use crate::type_::Type;
@@ -86,6 +86,8 @@ impl Funclet<'ll> {
8686

8787
impl BackendTypes for CodegenCx<'ll, 'tcx> {
8888
type Value = &'ll Value;
89+
type Function = &'ll Value;
90+
8991
type BasicBlock = &'ll BasicBlock;
9092
type Type = &'ll Type;
9193
type Funclet = Funclet<'ll>;
@@ -243,33 +245,23 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
243245
struct_in_context(self.llcx, elts, packed)
244246
}
245247

246-
fn const_to_uint(&self, v: &'ll Value) -> u64 {
247-
unsafe {
248+
fn const_to_opt_uint(&self, v: &'ll Value) -> Option<u64> {
249+
try_as_const_integral(v).map(|v| unsafe {
248250
llvm::LLVMConstIntGetZExtValue(v)
249-
}
250-
}
251-
252-
fn is_const_integral(&self, v: &'ll Value) -> bool {
253-
unsafe {
254-
llvm::LLVMIsAConstantInt(v).is_some()
255-
}
251+
})
256252
}
257253

258254
fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> {
259-
unsafe {
260-
if self.is_const_integral(v) {
261-
let (mut lo, mut hi) = (0u64, 0u64);
262-
let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
263-
&mut hi, &mut lo);
264-
if success {
265-
Some(hi_lo_to_u128(lo, hi))
266-
} else {
267-
None
268-
}
255+
try_as_const_integral(v).and_then(|v| unsafe {
256+
let (mut lo, mut hi) = (0u64, 0u64);
257+
let success = llvm::LLVMRustConstInt128Get(v, sign_ext,
258+
&mut hi, &mut lo);
259+
if success {
260+
Some(hi_lo_to_u128(lo, hi))
269261
} else {
270262
None
271263
}
272-
}
264+
})
273265
}
274266

275267
fn scalar_to_backend(
@@ -305,7 +297,7 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
305297
}
306298
}
307299
Some(GlobalAlloc::Function(fn_instance)) => {
308-
self.get_fn(fn_instance)
300+
self.get_fn_addr(fn_instance)
309301
}
310302
Some(GlobalAlloc::Static(def_id)) => {
311303
assert!(self.tcx.is_static(def_id));
@@ -386,3 +378,9 @@ pub fn struct_in_context(
386378
fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 {
387379
((hi as u128) << 64) | (lo as u128)
388380
}
381+
382+
fn try_as_const_integral(v: &'ll Value) -> Option<&'ll ConstantInt> {
383+
unsafe {
384+
llvm::LLVMIsAConstantInt(v)
385+
}
386+
}

src/librustc_codegen_llvm/context.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use rustc::ty::layout::{
2020
use rustc::ty::{self, Ty, TyCtxt, Instance};
2121
use rustc::util::nodemap::FxHashMap;
2222
use rustc_target::spec::{HasTargetSpec, Target};
23-
use rustc_codegen_ssa::callee::resolve_and_get_fn;
2423
use rustc_codegen_ssa::base::wants_msvc_seh;
2524
use crate::callee::get_fn;
2625

@@ -327,11 +326,11 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
327326
&self.vtables
328327
}
329328

330-
fn instances(&self) -> &RefCell<FxHashMap<Instance<'tcx>, &'ll Value>> {
331-
&self.instances
329+
fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
330+
get_fn(self, instance)
332331
}
333332

334-
fn get_fn(&self, instance: Instance<'tcx>) -> &'ll Value {
333+
fn get_fn_addr(&self, instance: Instance<'tcx>) -> &'ll Value {
335334
get_fn(self, instance)
336335
}
337336

@@ -362,7 +361,14 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
362361
let tcx = self.tcx;
363362
let llfn = match tcx.lang_items().eh_personality() {
364363
Some(def_id) if !wants_msvc_seh(self.sess()) => {
365-
resolve_and_get_fn(self, def_id, tcx.intern_substs(&[]))
364+
self.get_fn_addr(
365+
ty::Instance::resolve(
366+
tcx,
367+
ty::ParamEnv::reveal_all(),
368+
def_id,
369+
tcx.intern_substs(&[]),
370+
).unwrap()
371+
)
366372
}
367373
_ => {
368374
let name = if wants_msvc_seh(self.sess()) {
@@ -390,7 +396,14 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
390396
let tcx = self.tcx;
391397
assert!(self.sess().target.target.options.custom_unwind_resume);
392398
if let Some(def_id) = tcx.lang_items().eh_unwind_resume() {
393-
let llfn = resolve_and_get_fn(self, def_id, tcx.intern_substs(&[]));
399+
let llfn = self.get_fn_addr(
400+
ty::Instance::resolve(
401+
tcx,
402+
ty::ParamEnv::reveal_all(),
403+
def_id,
404+
tcx.intern_substs(&[]),
405+
).unwrap()
406+
);
394407
unwresume.set(Some(llfn));
395408
return llfn;
396409
}

src/librustc_codegen_llvm/error_codes.rs

-38
This file was deleted.

src/librustc_codegen_llvm/intrinsic.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
2020
use rustc::hir;
2121
use syntax::ast::{self, FloatTy};
2222

23+
use rustc_codegen_ssa::common::span_invalid_monomorphization_error;
2324
use rustc_codegen_ssa::traits::*;
2425

25-
use rustc::session::Session;
2626
use syntax_pos::Span;
2727

2828
use std::cmp::Ordering;
@@ -1026,10 +1026,6 @@ fn get_rust_try_fn<'ll, 'tcx>(
10261026
rust_try
10271027
}
10281028

1029-
fn span_invalid_monomorphization_error(a: &Session, b: Span, c: &str) {
1030-
span_err!(a, b, E0511, "{}", c);
1031-
}
1032-
10331029
fn generic_simd_intrinsic(
10341030
bx: &mut Builder<'a, 'll, 'tcx>,
10351031
name: &str,

src/librustc_codegen_llvm/lib.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extern crate rustc_fs_util;
3838
extern crate rustc_driver as _;
3939

4040
#[macro_use] extern crate log;
41-
#[macro_use] extern crate syntax;
41+
extern crate syntax;
4242
extern crate syntax_pos;
4343
extern crate rustc_errors as errors;
4444

@@ -64,8 +64,6 @@ use rustc::util::common::ErrorReported;
6464
use rustc_codegen_ssa::ModuleCodegen;
6565
use rustc_codegen_utils::codegen_backend::CodegenBackend;
6666

67-
mod error_codes;
68-
6967
mod back {
7068
pub mod archive;
7169
pub mod bytecode;
@@ -258,10 +256,6 @@ impl CodegenBackend for LlvmCodegenBackend {
258256
llvm_util::print_version();
259257
}
260258

261-
fn diagnostics(&self) -> &[(&'static str, &'static str)] {
262-
&error_codes::DIAGNOSTICS
263-
}
264-
265259
fn target_features(&self, sess: &Session) -> Vec<Symbol> {
266260
target_features(sess)
267261
}
@@ -271,15 +265,10 @@ impl CodegenBackend for LlvmCodegenBackend {
271265
}
272266

273267
fn provide(&self, providers: &mut ty::query::Providers<'_>) {
274-
rustc_codegen_utils::symbol_names::provide(providers);
275-
rustc_codegen_ssa::back::symbol_export::provide(providers);
276-
rustc_codegen_ssa::base::provide_both(providers);
277268
attributes::provide(providers);
278269
}
279270

280271
fn provide_extern(&self, providers: &mut ty::query::Providers<'_>) {
281-
rustc_codegen_ssa::back::symbol_export::provide_extern(providers);
282-
rustc_codegen_ssa::base::provide_both(providers);
283272
attributes::provide_extern(providers);
284273
}
285274

src/librustc_codegen_llvm/llvm/ffi.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ extern { pub type Module; }
510510
extern { pub type Context; }
511511
extern { pub type Type; }
512512
extern { pub type Value; }
513+
extern { pub type ConstantInt; }
513514
extern { pub type Metadata; }
514515
extern { pub type BasicBlock; }
515516
#[repr(C)]
@@ -719,8 +720,8 @@ extern "C" {
719720
pub fn LLVMConstInt(IntTy: &Type, N: c_ulonglong, SignExtend: Bool) -> &Value;
720721
pub fn LLVMConstIntOfArbitraryPrecision(IntTy: &Type, Wn: c_uint, Ws: *const u64) -> &Value;
721722
pub fn LLVMConstReal(RealTy: &Type, N: f64) -> &Value;
722-
pub fn LLVMConstIntGetZExtValue(ConstantVal: &Value) -> c_ulonglong;
723-
pub fn LLVMRustConstInt128Get(ConstantVal: &Value, SExt: bool,
723+
pub fn LLVMConstIntGetZExtValue(ConstantVal: &ConstantInt) -> c_ulonglong;
724+
pub fn LLVMRustConstInt128Get(ConstantVal: &ConstantInt, SExt: bool,
724725
high: &mut u64, low: &mut u64) -> bool;
725726

726727

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

1669-
pub fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&Value>;
1670+
pub fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&ConstantInt>;
16701671

16711672
pub fn LLVMRustPassKind(Pass: &Pass) -> PassKind;
16721673
pub fn LLVMRustFindAndCreatePass(Pass: *const c_char) -> Option<&'static mut Pass>;

src/librustc_codegen_ssa/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub trait BuilderMethods<'a, 'tcx>:
8484
{
8585
fn new_block<'b>(
8686
cx: &'a Self::CodegenCx,
87-
llfn: Self::Value,
87+
llfn: Self::Function,
8888
name: &'b str
8989
) -> Self;
9090
/* ... */

src/librustc_codegen_ssa/base.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use crate::mir::place::PlaceRef;
3636
use crate::back::write::{OngoingCodegen, start_async_codegen, submit_pre_lto_module_to_llvm,
3737
submit_post_lto_module_to_llvm};
3838
use crate::{MemFlags, CrateInfo};
39-
use crate::callee;
4039
use crate::common::{RealPredicate, TypeKind, IntPredicate};
4140
use crate::meth;
4241
use crate::mir;
@@ -377,8 +376,7 @@ pub fn codegen_instance<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
377376
let sig = instance.fn_sig(cx.tcx());
378377
let sig = cx.tcx().normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
379378

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

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

403-
let main_llfn = cx.get_fn(instance);
401+
let main_llfn = cx.get_fn_addr(instance);
404402

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

456454
let (start_fn, args) = if use_start_lang_item {
457455
let start_def_id = cx.tcx().require_lang_item(StartFnLangItem, None);
458-
let start_fn = callee::resolve_and_get_fn(
459-
cx,
460-
start_def_id,
461-
cx.tcx().intern_substs(&[main_ret_ty.into()]),
456+
let start_fn = cx.get_fn_addr(
457+
ty::Instance::resolve(
458+
cx.tcx(),
459+
ty::ParamEnv::reveal_all(),
460+
start_def_id,
461+
cx.tcx().intern_substs(&[main_ret_ty.into()]),
462+
).unwrap()
462463
);
463464
(start_fn, vec![bx.pointercast(rust_main, cx.type_ptr_to(cx.type_i8p())),
464465
arg_argc, arg_argv])

src/librustc_codegen_ssa/callee.rs

-53
This file was deleted.

0 commit comments

Comments
 (0)