Skip to content

Flatten ifs in rustc_codegen_ssa #138619

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

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions compiler/rustc_codegen_ssa/src/back/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,11 @@ impl<'a> ArchiveBuilder for ArArchiveBuilder<'a> {
mut skip: Box<dyn FnMut(&str) -> bool + 'static>,
) -> io::Result<()> {
let mut archive_path = archive_path.to_path_buf();
if self.sess.target.llvm_target.contains("-apple-macosx") {
if let Some(new_archive_path) = try_extract_macho_fat_archive(self.sess, &archive_path)?
if self.sess.target.llvm_target.contains("-apple-macosx")
&& let Some(new_archive_path) = try_extract_macho_fat_archive(self.sess, &archive_path)?
{
archive_path = new_archive_path
}
}

if self.src_archives.iter().any(|archive| archive.0 == archive_path) {
return Ok(());
Expand Down
27 changes: 12 additions & 15 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ pub fn link_binary(
sess.dcx().emit_artifact_notification(&out_filename, "link");
}

if sess.prof.enabled() {
if let Some(artifact_name) = out_filename.file_name() {
if sess.prof.enabled()
&& let Some(artifact_name) = out_filename.file_name()
{
// Record size for self-profiling
let file_size = std::fs::metadata(&out_filename).map(|m| m.len()).unwrap_or(0);

Expand All @@ -162,7 +163,6 @@ pub fn link_binary(
file_size,
);
}
}

if output.is_stdout() {
if output.is_tty() {
Expand All @@ -186,17 +186,13 @@ pub fn link_binary(

let maybe_remove_temps_from_module =
|preserve_objects: bool, preserve_dwarf_objects: bool, module: &CompiledModule| {
if !preserve_objects {
if let Some(ref obj) = module.object {
if !preserve_objects && let Some(ref obj) = module.object {
ensure_removed(sess.dcx(), obj);
}
}

if !preserve_dwarf_objects {
if let Some(ref dwo_obj) = module.dwarf_object {
if !preserve_dwarf_objects && let Some(ref dwo_obj) = module.dwarf_object {
ensure_removed(sess.dcx(), dwo_obj);
}
}
};

let remove_temps_from_module =
Expand Down Expand Up @@ -2116,13 +2112,13 @@ fn add_local_crate_metadata_objects(
// 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
// object file, so we link that in here.
if crate_type == CrateType::Dylib || crate_type == CrateType::ProcMacro {
if let Some(obj) = codegen_results.metadata_module.as_ref().and_then(|m| m.object.as_ref())
if matches!(crate_type, CrateType::Dylib | CrateType::ProcMacro)
&& let Some(m) = &codegen_results.metadata_module
&& let Some(obj) = &m.object
{
cmd.add_object(obj);
}
}
}

/// Add sysroot and other globally set directories to the directory search list.
fn add_library_search_dirs(
Expand Down Expand Up @@ -2540,11 +2536,12 @@ fn add_order_independent_options(

cmd.output_filename(out_filename);

if crate_type == CrateType::Executable && sess.target.is_like_windows {
if let Some(ref s) = codegen_results.crate_info.windows_subsystem {
if crate_type == CrateType::Executable
&& sess.target.is_like_windows
&& let Some(s) = &codegen_results.crate_info.windows_subsystem
{
cmd.subsystem(s);
}
}

// Try to strip as much out of the generated object by removing unused
// sections if possible. See more comments in linker.rs
Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ pub(crate) fn get_linker<'a>(
// PATH for the child.
let mut new_path = sess.get_tools_search_paths(self_contained);
let mut msvc_changed_path = false;
if sess.target.is_like_msvc {
if let Some(ref tool) = msvc_tool {
if sess.target.is_like_msvc
&& let Some(ref tool) = msvc_tool
{
cmd.args(tool.args());
for (k, v) in tool.env() {
if k == "PATH" {
Expand All @@ -123,13 +124,10 @@ pub(crate) fn get_linker<'a>(
}
}
}
}

if !msvc_changed_path {
if let Some(path) = env::var_os("PATH") {
if !msvc_changed_path && let Some(path) = env::var_os("PATH") {
new_path.extend(env::split_paths(&path));
}
}
cmd.env("PATH", env::join_paths(new_path).unwrap());

// FIXME: Move `/LIBPATH` addition for uwp targets from the linker construction
Expand Down
22 changes: 8 additions & 14 deletions compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,16 +566,13 @@ fn produce_final_output_artifacts(

// Produce final compile outputs.
let copy_gracefully = |from: &Path, to: &OutFileName| match to {
OutFileName::Stdout => {
if let Err(e) = copy_to_stdout(from) {
OutFileName::Stdout if let Err(e) = copy_to_stdout(from) => {
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));
}
}
_ => {}
};

let copy_if_one_unit = |output_type: OutputType, keep_numbered: bool| {
Expand Down Expand Up @@ -685,14 +682,12 @@ fn produce_final_output_artifacts(
needs_crate_object || (user_wants_objects && sess.codegen_units().as_usize() > 1);

for module in compiled_modules.modules.iter() {
if let Some(ref path) = module.object {
if !keep_numbered_objects {
if let Some(ref path) = module.object {
ensure_removed(sess.dcx(), path);
}
}

if let Some(ref path) = module.dwarf_object {
if !keep_numbered_objects {
ensure_removed(sess.dcx(), path);
}
}
Expand All @@ -704,14 +699,13 @@ fn produce_final_output_artifacts(
}
}

if !user_wants_bitcode {
if let Some(ref allocator_module) = compiled_modules.allocator_module {
if let Some(ref path) = allocator_module.bytecode {
if !user_wants_bitcode
&& let Some(ref allocator_module) = compiled_modules.allocator_module
&& let Some(ref path) = allocator_module.bytecode
{
ensure_removed(sess.dcx(), path);
}
}
}
}

if sess.opts.json_artifact_notifications {
if let [module] = &compiled_modules.modules[..] {
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
let def_key = tcx.def_key(def_id);
if qualified {
if let Some(parent) = def_key.parent {
if qualified && let Some(parent) = def_key.parent {
push_item_name(tcx, DefId { krate: def_id.krate, index: parent }, true, output);
output.push_str("::");
}
}

push_unqualified_item_name(tcx, def_id, def_key.disambiguated_data, output);
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
mergeable_succ: bool,
) -> MergingSucc {
let tcx = bx.tcx();
if let Some(instance) = instance {
if is_call_from_compiler_builtins_to_upstream_monomorphization(tcx, instance) {
if let Some(instance) = instance
&& is_call_from_compiler_builtins_to_upstream_monomorphization(tcx, instance)
{
if destination.is_some() {
let caller_def = fx.instance.def_id();
let e = CompilerBuiltinsCannotCall {
Expand All @@ -183,7 +184,6 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
return MergingSucc::False;
}
}
}

// If there is a cleanup block and the function we're calling can unwind, then
// do an invoke, otherwise do a call.
Expand Down
13 changes: 5 additions & 8 deletions compiler/rustc_codegen_ssa/src/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
// ZST are passed as operands and require special handling
// because codegen_place() panics if Local is operand.
if let Some(index) = place.as_local() {
if let LocalRef::Operand(op) = self.locals[index] {
if let ty::Array(_, n) = op.layout.ty.kind() {
let n = n
.try_to_target_usize(bx.tcx())
.expect("expected monomorphic const in codegen");
if let Some(index) = place.as_local()
&& let LocalRef::Operand(op) = self.locals[index]
&& let ty::Array(_, n) = op.layout.ty.kind()
{
let n = n.try_to_target_usize(bx.tcx()).expect("expected monomorphic const in codegen");
return bx.cx().const_usize(n);
}
}
}
// use common size calculation for non zero-sized types
let cg_value = self.codegen_place(bx, place.as_ref());
cg_value.len(bx.cx())
Expand Down
Loading