Skip to content

Commit b8b4150

Browse files
committed
Auto merge of #54911 - ljedrz:cleanup_codegen_llvm_top, r=michaelwoerister
Cleanup top-level codegen_llvm - improve allocations - improve common patterns - remove explicit returns - fix spelling & grammatical errors - whitespace & formatting improvements
2 parents a534216 + a0fc2e6 commit b8b4150

File tree

12 files changed

+99
-126
lines changed

12 files changed

+99
-126
lines changed

src/librustc_codegen_llvm/abi.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,7 @@ impl ArgTypeExt<'ll, 'tcx> for ArgType<'tcx, Ty<'tcx>> {
259259
}
260260

261261
pub trait FnTypeExt<'tcx> {
262-
fn of_instance(cx: &CodegenCx<'ll, 'tcx>, instance: &ty::Instance<'tcx>)
263-
-> Self;
262+
fn of_instance(cx: &CodegenCx<'ll, 'tcx>, instance: &ty::Instance<'tcx>) -> Self;
264263
fn new(cx: &CodegenCx<'ll, 'tcx>,
265264
sig: ty::FnSig<'tcx>,
266265
extra_args: &[Ty<'tcx>]) -> Self;
@@ -283,8 +282,7 @@ pub trait FnTypeExt<'tcx> {
283282
}
284283

285284
impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
286-
fn of_instance(cx: &CodegenCx<'ll, 'tcx>, instance: &ty::Instance<'tcx>)
287-
-> Self {
285+
fn of_instance(cx: &CodegenCx<'ll, 'tcx>, instance: &ty::Instance<'tcx>) -> Self {
288286
let fn_ty = instance.ty(cx.tcx);
289287
let sig = ty_fn_sig(cx, fn_ty);
290288
let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
@@ -338,7 +336,7 @@ impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
338336
RustIntrinsic | PlatformIntrinsic |
339337
Rust | RustCall => Conv::C,
340338

341-
// It's the ABI's job to select this, not us.
339+
// It's the ABI's job to select this, not ours.
342340
System => bug!("system abi should be selected elsewhere"),
343341

344342
Stdcall => Conv::X86Stdcall,
@@ -697,14 +695,13 @@ impl<'tcx> FnTypeExt<'tcx> for FnType<'tcx, Ty<'tcx>> {
697695
// If the value is a boolean, the range is 0..2 and that ultimately
698696
// become 0..0 when the type becomes i1, which would be rejected
699697
// by the LLVM verifier.
700-
match scalar.value {
701-
layout::Int(..) if !scalar.is_bool() => {
698+
if let layout::Int(..) = scalar.value {
699+
if !scalar.is_bool() {
702700
let range = scalar.valid_range_exclusive(bx.cx);
703701
if range.start != range.end {
704702
bx.range_metadata(callsite, range);
705703
}
706704
}
707-
_ => {}
708705
}
709706
}
710707
for arg in &self.args {

src/librustc_codegen_llvm/allocator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind
3333
let void = llvm::LLVMVoidTypeInContext(llcx);
3434

3535
for method in ALLOCATOR_METHODS {
36-
let mut args = Vec::new();
36+
let mut args = Vec::with_capacity(method.inputs.len());
3737
for ty in method.inputs.iter() {
3838
match *ty {
3939
AllocatorTy::Layout => {

src/librustc_codegen_llvm/attributes.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ pub fn set_probestack(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
9494
// Currently stack probes seem somewhat incompatible with the address
9595
// sanitizer. With asan we're already protected from stack overflow anyway
9696
// so we don't really need stack probes regardless.
97-
match cx.sess().opts.debugging_opts.sanitizer {
98-
Some(Sanitizer::Address) => return,
99-
_ => {}
97+
if let Some(Sanitizer::Address) = cx.sess().opts.debugging_opts.sanitizer {
98+
return
10099
}
101100

102101
// probestack doesn't play nice either with pgo-gen.
@@ -280,12 +279,14 @@ pub fn provide_extern(providers: &mut Providers) {
280279
// `NativeLibrary` internally contains information about
281280
// `#[link(wasm_import_module = "...")]` for example.
282281
let native_libs = tcx.native_libraries(cnum);
283-
let mut def_id_to_native_lib = FxHashMap();
284-
for lib in native_libs.iter() {
282+
283+
let def_id_to_native_lib = native_libs.iter().filter_map(|lib|
285284
if let Some(id) = lib.foreign_module {
286-
def_id_to_native_lib.insert(id, lib);
287-
}
285+
Some((id, lib))
286+
} else {
287+
None
288288
}
289+
).collect::<FxHashMap<_, _>>();
289290

290291
let mut ret = FxHashMap();
291292
for lib in tcx.foreign_modules(cnum).iter() {
@@ -296,10 +297,10 @@ pub fn provide_extern(providers: &mut Providers) {
296297
Some(s) => s,
297298
None => continue,
298299
};
299-
for id in lib.foreign_items.iter() {
300+
ret.extend(lib.foreign_items.iter().map(|id| {
300301
assert_eq!(id.krate, cnum);
301-
ret.insert(*id, module.to_string());
302-
}
302+
(*id, module.to_string())
303+
}));
303304
}
304305

305306
Lrc::new(ret)

src/librustc_codegen_llvm/base.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,8 @@ fn cast_shift_rhs<'ll, F, G>(op: hir::BinOpKind,
363363
if lhs_sz < rhs_sz {
364364
trunc(rhs, lhs_llty)
365365
} else if lhs_sz > rhs_sz {
366-
// FIXME (#1877: If shifting by negative
367-
// values becomes not undefined then this is wrong.
366+
// FIXME (#1877: If in the future shifting by negative
367+
// values is no longer undefined then this is wrong.
368368
zext(rhs, lhs_llty)
369369
} else {
370370
rhs
@@ -495,10 +495,8 @@ pub fn codegen_instance<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>, instance: Instance<'
495495
let sig = common::ty_fn_sig(cx, fn_ty);
496496
let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
497497

498-
let lldecl = match cx.instances.borrow().get(&instance) {
499-
Some(&val) => val,
500-
None => bug!("Instance `{:?}` not already declared", instance)
501-
};
498+
let lldecl = cx.instances.borrow().get(&instance).cloned().unwrap_or_else(||
499+
bug!("Instance `{:?}` not already declared", instance));
502500

503501
cx.stats.borrow_mut().n_closures += 1;
504502

@@ -737,8 +735,8 @@ fn determine_cgu_reuse<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
737735

738736
pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
739737
rx: mpsc::Receiver<Box<dyn Any + Send>>)
740-
-> OngoingCodegen {
741-
738+
-> OngoingCodegen
739+
{
742740
check_for_rustc_errors_attr(tcx);
743741

744742
if let Some(true) = tcx.sess.opts.debugging_opts.thinlto {
@@ -803,8 +801,7 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
803801

804802
// Run the monomorphization collector and partition the collected items into
805803
// codegen units.
806-
let codegen_units =
807-
tcx.collect_and_partition_mono_items(LOCAL_CRATE).1;
804+
let codegen_units = tcx.collect_and_partition_mono_items(LOCAL_CRATE).1;
808805
let codegen_units = (*codegen_units).clone();
809806

810807
// Force all codegen_unit queries so they are already either red or green
@@ -837,12 +834,7 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
837834
.iter()
838835
.any(|(_, list)| {
839836
use rustc::middle::dependency_format::Linkage;
840-
list.iter().any(|linkage| {
841-
match linkage {
842-
Linkage::Dynamic => true,
843-
_ => false,
844-
}
845-
})
837+
list.iter().any(|&linkage| linkage == Linkage::Dynamic)
846838
});
847839
let allocator_module = if any_dynamic_crate {
848840
None
@@ -1123,7 +1115,15 @@ impl CrateInfo {
11231115
info.load_wasm_imports(tcx, LOCAL_CRATE);
11241116
}
11251117

1126-
for &cnum in tcx.crates().iter() {
1118+
let crates = tcx.crates();
1119+
1120+
let n_crates = crates.len();
1121+
info.native_libraries.reserve(n_crates);
1122+
info.crate_name.reserve(n_crates);
1123+
info.used_crate_source.reserve(n_crates);
1124+
info.missing_lang_items.reserve(n_crates);
1125+
1126+
for &cnum in crates.iter() {
11271127
info.native_libraries.insert(cnum, tcx.native_libraries(cnum));
11281128
info.crate_name.insert(cnum, tcx.crate_name(cnum).to_string());
11291129
info.used_crate_source.insert(cnum, tcx.used_crate_source(cnum));
@@ -1165,11 +1165,12 @@ impl CrateInfo {
11651165
}
11661166

11671167
fn load_wasm_imports(&mut self, tcx: TyCtxt, cnum: CrateNum) {
1168-
for (&id, module) in tcx.wasm_import_module_map(cnum).iter() {
1168+
self.wasm_imports.extend(tcx.wasm_import_module_map(cnum).iter().map(|(&id, module)| {
11691169
let instance = Instance::mono(tcx, id);
11701170
let import_name = tcx.symbol_name(instance);
1171-
self.wasm_imports.insert(import_name.to_string(), module.clone());
1172-
}
1171+
1172+
(import_name.to_string(), module.clone())
1173+
}));
11731174
}
11741175
}
11751176

src/librustc_codegen_llvm/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ impl Builder<'a, 'll, 'tcx> {
11941194
})
11951195
.collect();
11961196

1197-
return Cow::Owned(casted_args);
1197+
Cow::Owned(casted_args)
11981198
}
11991199

12001200
pub fn lifetime_start(&self, ptr: &'ll Value, size: Size) {

src/librustc_codegen_llvm/common.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,13 @@ pub fn langcall(tcx: TyCtxt,
336336
msg: &str,
337337
li: LangItem)
338338
-> DefId {
339-
match tcx.lang_items().require(li) {
340-
Ok(id) => id,
341-
Err(s) => {
339+
tcx.lang_items().require(li).unwrap_or_else(|s| {
342340
let msg = format!("{} {}", msg, s);
343341
match span {
344342
Some(span) => tcx.sess.span_fatal(span, &msg[..]),
345343
None => tcx.sess.fatal(&msg[..]),
346344
}
347-
}
348-
}
345+
})
349346
}
350347

351348
// To avoid UB from LLVM, these two functions mask RHS with an

src/librustc_codegen_llvm/consts.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,14 @@ fn check_and_apply_linkage(
249249
// extern "C" fn() from being non-null, so we can't just declare a
250250
// static and call it a day. Some linkages (like weak) will make it such
251251
// that the static actually has a null value.
252-
let llty2 = match ty.sty {
253-
ty::RawPtr(ref mt) => cx.layout_of(mt.ty).llvm_type(cx),
254-
_ => {
255-
if span.is_some() {
256-
cx.sess().span_fatal(span.unwrap(), "must have type `*const T` or `*mut T`")
252+
let llty2 = if let ty::RawPtr(ref mt) = ty.sty {
253+
cx.layout_of(mt.ty).llvm_type(cx)
254+
} else {
255+
if let Some(span) = span {
256+
cx.sess().span_fatal(span, "must have type `*const T` or `*mut T`")
257257
} else {
258258
bug!("must have type `*const T` or `*mut T`")
259259
}
260-
}
261260
};
262261
unsafe {
263262
// Declare a symbol `foo` with the desired linkage.
@@ -273,9 +272,9 @@ fn check_and_apply_linkage(
273272
let mut real_name = "_rust_extern_with_linkage_".to_string();
274273
real_name.push_str(&sym);
275274
let g2 = declare::define_global(cx, &real_name, llty).unwrap_or_else(||{
276-
if span.is_some() {
275+
if let Some(span) = span {
277276
cx.sess().span_fatal(
278-
span.unwrap(),
277+
span,
279278
&format!("symbol `{}` is already defined", &sym)
280279
)
281280
} else {

src/librustc_codegen_llvm/context.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ pub struct CodegenCx<'a, 'tcx: 'a> {
5959
/// Cache instances of monomorphic and polymorphic items
6060
pub instances: RefCell<FxHashMap<Instance<'tcx>, &'a Value>>,
6161
/// Cache generated vtables
62-
pub vtables: RefCell<FxHashMap<(Ty<'tcx>,
63-
Option<ty::PolyExistentialTraitRef<'tcx>>), &'a Value>>,
62+
pub vtables: RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>),
63+
&'a Value>>,
6464
/// Cache of constant strings,
6565
pub const_cstr_cache: RefCell<FxHashMap<LocalInternedString, &'a Value>>,
6666

@@ -318,10 +318,8 @@ impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
318318
if let Some(v) = self.intrinsics.borrow().get(key).cloned() {
319319
return v;
320320
}
321-
match declare_intrinsic(self, key) {
322-
Some(v) => return v,
323-
None => bug!("unknown intrinsic '{}'", key)
324-
}
321+
322+
declare_intrinsic(self, key).unwrap_or_else(|| bug!("unknown intrinsic '{}'", key))
325323
}
326324

327325
/// Generate a new symbol name with the given prefix. This symbol name must
@@ -465,9 +463,10 @@ impl LayoutOf for &'a CodegenCx<'ll, 'tcx> {
465463

466464
fn layout_of(self, ty: Ty<'tcx>) -> Self::TyLayout {
467465
self.tcx.layout_of(ty::ParamEnv::reveal_all().and(ty))
468-
.unwrap_or_else(|e| match e {
469-
LayoutError::SizeOverflow(_) => self.sess().fatal(&e.to_string()),
470-
_ => bug!("failed to get layout for `{}`: {}", ty, e)
466+
.unwrap_or_else(|e| if let LayoutError::SizeOverflow(_) = e {
467+
self.sess().fatal(&e.to_string())
468+
} else {
469+
bug!("failed to get layout for `{}`: {}", ty, e)
471470
})
472471
}
473472
}
@@ -772,5 +771,6 @@ fn declare_intrinsic(cx: &CodegenCx<'ll, '_>, key: &str) -> Option<&'ll Value> {
772771
ifn!("llvm.dbg.declare", fn(Type::metadata(cx), Type::metadata(cx)) -> void);
773772
ifn!("llvm.dbg.value", fn(Type::metadata(cx), t_i64, Type::metadata(cx)) -> void);
774773
}
775-
return None;
774+
775+
None
776776
}

src/librustc_codegen_llvm/glue.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)