1
Fork 0

Flatten ifs in rustc_codegen_ssa

This commit is contained in:
Yotam Ofek 2025-03-17 18:56:52 +00:00
parent 9c67cecd12
commit 36f6bc5e3d
7 changed files with 81 additions and 98 deletions

View file

@ -389,12 +389,11 @@ impl<'a> ArchiveBuilder for ArArchiveBuilder<'a> {
mut skip: Box<dyn FnMut(&str) -> bool + 'static>, mut skip: Box<dyn FnMut(&str) -> bool + 'static>,
) -> io::Result<()> { ) -> io::Result<()> {
let mut archive_path = archive_path.to_path_buf(); let mut archive_path = archive_path.to_path_buf();
if self.sess.target.llvm_target.contains("-apple-macosx") { if self.sess.target.llvm_target.contains("-apple-macosx")
if let Some(new_archive_path) = try_extract_macho_fat_archive(self.sess, &archive_path)? && let Some(new_archive_path) = try_extract_macho_fat_archive(self.sess, &archive_path)?
{ {
archive_path = new_archive_path archive_path = new_archive_path
} }
}
if self.src_archives.iter().any(|archive| archive.0 == archive_path) { if self.src_archives.iter().any(|archive| archive.0 == archive_path) {
return Ok(()); return Ok(());

View file

@ -151,8 +151,9 @@ pub fn link_binary(
sess.dcx().emit_artifact_notification(&out_filename, "link"); sess.dcx().emit_artifact_notification(&out_filename, "link");
} }
if sess.prof.enabled() { if sess.prof.enabled()
if let Some(artifact_name) = out_filename.file_name() { && let Some(artifact_name) = out_filename.file_name()
{
// Record size for self-profiling // Record size for self-profiling
let file_size = std::fs::metadata(&out_filename).map(|m| m.len()).unwrap_or(0); let file_size = std::fs::metadata(&out_filename).map(|m| m.len()).unwrap_or(0);
@ -162,7 +163,6 @@ pub fn link_binary(
file_size, file_size,
); );
} }
}
if output.is_stdout() { if output.is_stdout() {
if output.is_tty() { if output.is_tty() {
@ -186,17 +186,13 @@ pub fn link_binary(
let maybe_remove_temps_from_module = let maybe_remove_temps_from_module =
|preserve_objects: bool, preserve_dwarf_objects: bool, module: &CompiledModule| { |preserve_objects: bool, preserve_dwarf_objects: bool, module: &CompiledModule| {
if !preserve_objects { if !preserve_objects && let Some(ref obj) = module.object {
if let Some(ref obj) = module.object {
ensure_removed(sess.dcx(), obj); ensure_removed(sess.dcx(), obj);
} }
}
if !preserve_dwarf_objects { if !preserve_dwarf_objects && let Some(ref dwo_obj) = module.dwarf_object {
if let Some(ref dwo_obj) = module.dwarf_object {
ensure_removed(sess.dcx(), dwo_obj); ensure_removed(sess.dcx(), dwo_obj);
} }
}
}; };
let remove_temps_from_module = let remove_temps_from_module =
@ -2116,12 +2112,12 @@ fn add_local_crate_metadata_objects(
// When linking a dynamic library, we put the metadata into a section of the // When linking a dynamic library, we put the metadata into a section of the
// executable. This metadata is in a separate object file from the main // executable. This metadata is in a separate object file from the main
// object file, so we link that in here. // object file, so we link that in here.
if crate_type == CrateType::Dylib || crate_type == CrateType::ProcMacro { if matches!(crate_type, CrateType::Dylib | CrateType::ProcMacro)
if let Some(obj) = codegen_results.metadata_module.as_ref().and_then(|m| m.object.as_ref()) && let Some(m) = &codegen_results.metadata_module
&& let Some(obj) = &m.object
{ {
cmd.add_object(obj); cmd.add_object(obj);
} }
}
} }
/// Add sysroot and other globally set directories to the directory search list. /// Add sysroot and other globally set directories to the directory search list.
@ -2540,11 +2536,12 @@ fn add_order_independent_options(
cmd.output_filename(out_filename); cmd.output_filename(out_filename);
if crate_type == CrateType::Executable && sess.target.is_like_windows { if crate_type == CrateType::Executable
if let Some(ref s) = codegen_results.crate_info.windows_subsystem { && sess.target.is_like_windows
&& let Some(s) = &codegen_results.crate_info.windows_subsystem
{
cmd.subsystem(s); cmd.subsystem(s);
} }
}
// Try to strip as much out of the generated object by removing unused // Try to strip as much out of the generated object by removing unused
// sections if possible. See more comments in linker.rs // sections if possible. See more comments in linker.rs

View file

@ -111,8 +111,9 @@ pub(crate) fn get_linker<'a>(
// PATH for the child. // PATH for the child.
let mut new_path = sess.get_tools_search_paths(self_contained); let mut new_path = sess.get_tools_search_paths(self_contained);
let mut msvc_changed_path = false; let mut msvc_changed_path = false;
if sess.target.is_like_msvc { if sess.target.is_like_msvc
if let Some(ref tool) = msvc_tool { && let Some(ref tool) = msvc_tool
{
cmd.args(tool.args()); cmd.args(tool.args());
for (k, v) in tool.env() { for (k, v) in tool.env() {
if k == "PATH" { if k == "PATH" {
@ -123,13 +124,10 @@ pub(crate) fn get_linker<'a>(
} }
} }
} }
}
if !msvc_changed_path { if !msvc_changed_path && let Some(path) = env::var_os("PATH") {
if let Some(path) = env::var_os("PATH") {
new_path.extend(env::split_paths(&path)); new_path.extend(env::split_paths(&path));
} }
}
cmd.env("PATH", env::join_paths(new_path).unwrap()); cmd.env("PATH", env::join_paths(new_path).unwrap());
// FIXME: Move `/LIBPATH` addition for uwp targets from the linker construction // FIXME: Move `/LIBPATH` addition for uwp targets from the linker construction

View file

@ -566,16 +566,13 @@ fn produce_final_output_artifacts(
// Produce final compile outputs. // Produce final compile outputs.
let copy_gracefully = |from: &Path, to: &OutFileName| match to { let copy_gracefully = |from: &Path, to: &OutFileName| match to {
OutFileName::Stdout => { OutFileName::Stdout if let Err(e) = copy_to_stdout(from) => {
if let Err(e) = copy_to_stdout(from) {
sess.dcx().emit_err(errors::CopyPath::new(from, to.as_path(), e)); sess.dcx().emit_err(errors::CopyPath::new(from, to.as_path(), e));
} }
} OutFileName::Real(path) if let Err(e) = fs::copy(from, path) => {
OutFileName::Real(path) => {
if let Err(e) = fs::copy(from, path) {
sess.dcx().emit_err(errors::CopyPath::new(from, path, e)); sess.dcx().emit_err(errors::CopyPath::new(from, path, e));
} }
} _ => {}
}; };
let copy_if_one_unit = |output_type: OutputType, keep_numbered: bool| { let copy_if_one_unit = |output_type: OutputType, keep_numbered: bool| {
@ -685,14 +682,12 @@ fn produce_final_output_artifacts(
needs_crate_object || (user_wants_objects && sess.codegen_units().as_usize() > 1); needs_crate_object || (user_wants_objects && sess.codegen_units().as_usize() > 1);
for module in compiled_modules.modules.iter() { for module in compiled_modules.modules.iter() {
if let Some(ref path) = module.object {
if !keep_numbered_objects { if !keep_numbered_objects {
if let Some(ref path) = module.object {
ensure_removed(sess.dcx(), path); ensure_removed(sess.dcx(), path);
} }
}
if let Some(ref path) = module.dwarf_object { if let Some(ref path) = module.dwarf_object {
if !keep_numbered_objects {
ensure_removed(sess.dcx(), path); ensure_removed(sess.dcx(), path);
} }
} }
@ -704,14 +699,13 @@ fn produce_final_output_artifacts(
} }
} }
if !user_wants_bitcode { if !user_wants_bitcode
if let Some(ref allocator_module) = compiled_modules.allocator_module { && let Some(ref allocator_module) = compiled_modules.allocator_module
if let Some(ref path) = allocator_module.bytecode { && let Some(ref path) = allocator_module.bytecode
{
ensure_removed(sess.dcx(), path); ensure_removed(sess.dcx(), path);
} }
} }
}
}
if sess.opts.json_artifact_notifications { if sess.opts.json_artifact_notifications {
if let [module] = &compiled_modules.modules[..] { if let [module] = &compiled_modules.modules[..] {

View file

@ -555,12 +555,10 @@ pub fn compute_debuginfo_vtable_name<'tcx>(
pub fn push_item_name(tcx: TyCtxt<'_>, def_id: DefId, qualified: bool, output: &mut String) { pub fn push_item_name(tcx: TyCtxt<'_>, def_id: DefId, qualified: bool, output: &mut String) {
let def_key = tcx.def_key(def_id); let def_key = tcx.def_key(def_id);
if qualified { if qualified && let Some(parent) = def_key.parent {
if let Some(parent) = def_key.parent {
push_item_name(tcx, DefId { krate: def_id.krate, index: parent }, true, output); push_item_name(tcx, DefId { krate: def_id.krate, index: parent }, true, output);
output.push_str("::"); output.push_str("::");
} }
}
push_unqualified_item_name(tcx, def_id, def_key.disambiguated_data, output); push_unqualified_item_name(tcx, def_id, def_key.disambiguated_data, output);
} }

View file

@ -163,8 +163,9 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
mergeable_succ: bool, mergeable_succ: bool,
) -> MergingSucc { ) -> MergingSucc {
let tcx = bx.tcx(); let tcx = bx.tcx();
if let Some(instance) = instance { if let Some(instance) = instance
if is_call_from_compiler_builtins_to_upstream_monomorphization(tcx, instance) { && is_call_from_compiler_builtins_to_upstream_monomorphization(tcx, instance)
{
if destination.is_some() { if destination.is_some() {
let caller_def = fx.instance.def_id(); let caller_def = fx.instance.def_id();
let e = CompilerBuiltinsCannotCall { let e = CompilerBuiltinsCannotCall {
@ -183,7 +184,6 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
return MergingSucc::False; return MergingSucc::False;
} }
} }
}
// If there is a cleanup block and the function we're calling can unwind, then // If there is a cleanup block and the function we're calling can unwind, then
// do an invoke, otherwise do a call. // do an invoke, otherwise do a call.

View file

@ -837,16 +837,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
fn evaluate_array_len(&mut self, bx: &mut Bx, place: mir::Place<'tcx>) -> Bx::Value { fn evaluate_array_len(&mut self, bx: &mut Bx, place: mir::Place<'tcx>) -> Bx::Value {
// ZST are passed as operands and require special handling // ZST are passed as operands and require special handling
// because codegen_place() panics if Local is operand. // because codegen_place() panics if Local is operand.
if let Some(index) = place.as_local() { if let Some(index) = place.as_local()
if let LocalRef::Operand(op) = self.locals[index] { && let LocalRef::Operand(op) = self.locals[index]
if let ty::Array(_, n) = op.layout.ty.kind() { && let ty::Array(_, n) = op.layout.ty.kind()
let n = n {
.try_to_target_usize(bx.tcx()) let n = n.try_to_target_usize(bx.tcx()).expect("expected monomorphic const in codegen");
.expect("expected monomorphic const in codegen");
return bx.cx().const_usize(n); return bx.cx().const_usize(n);
} }
}
}
// use common size calculation for non zero-sized types // use common size calculation for non zero-sized types
let cg_value = self.codegen_place(bx, place.as_ref()); let cg_value = self.codegen_place(bx, place.as_ref());
cg_value.len(bx.cx()) cg_value.len(bx.cx())