Rollup merge of #138619 - yotamofek:pr/codegen_ssa/flatten-ifs, r=lcnr

Flatten `if`s in `rustc_codegen_ssa`

Best viewed [while ignoring whitespace](https://github.com/rust-lang/rust/pull/138619/files?diff=unified&w=1)
This commit is contained in:
Matthias Krüger 2025-03-17 22:49:08 +01:00 committed by GitHub
commit 597500db8b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 81 additions and 98 deletions

View file

@ -389,11 +389,10 @@ 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)?
{
archive_path = new_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) {

View file

@ -151,17 +151,17 @@ 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() {
// Record size for self-profiling
let file_size = std::fs::metadata(&out_filename).map(|m| m.len()).unwrap_or(0);
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);
sess.prof.artifact_size(
"linked_artifact",
artifact_name.to_string_lossy(),
file_size,
);
}
sess.prof.artifact_size(
"linked_artifact",
artifact_name.to_string_lossy(),
file_size,
);
}
if output.is_stdout() {
@ -186,16 +186,12 @@ 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 {
ensure_removed(sess.dcx(), obj);
}
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 {
ensure_removed(sess.dcx(), dwo_obj);
}
if !preserve_dwarf_objects && let Some(ref dwo_obj) = module.dwarf_object {
ensure_removed(sess.dcx(), dwo_obj);
}
};
@ -2116,11 +2112,11 @@ 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())
{
cmd.add_object(obj);
}
if matches!(crate_type, CrateType::Dylib | CrateType::ProcMacro)
&& let Some(m) = &codegen_results.metadata_module
&& let Some(obj) = &m.object
{
cmd.add_object(obj);
}
}
@ -2540,10 +2536,11 @@ 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 {
cmd.subsystem(s);
}
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

View file

@ -111,24 +111,22 @@ 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 {
cmd.args(tool.args());
for (k, v) in tool.env() {
if k == "PATH" {
new_path.extend(env::split_paths(v));
msvc_changed_path = true;
} else {
cmd.env(k, v);
}
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" {
new_path.extend(env::split_paths(v));
msvc_changed_path = true;
} else {
cmd.env(k, v);
}
}
}
if !msvc_changed_path {
if let Some(path) = env::var_os("PATH") {
new_path.extend(env::split_paths(&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());

View file

@ -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) {
sess.dcx().emit_err(errors::CopyPath::new(from, to.as_path(), e));
}
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) {
sess.dcx().emit_err(errors::CopyPath::new(from, path, e));
}
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| {
@ -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 !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 {
if let Some(ref path) = module.dwarf_object {
ensure_removed(sess.dcx(), path);
}
}
@ -704,12 +699,11 @@ 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 {
ensure_removed(sess.dcx(), path);
}
}
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);
}
}

View file

@ -555,11 +555,9 @@ 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 {
push_item_name(tcx, DefId { krate: def_id.krate, index: parent }, true, output);
output.push_str("::");
}
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);

View file

@ -163,25 +163,25 @@ 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 destination.is_some() {
let caller_def = fx.instance.def_id();
let e = CompilerBuiltinsCannotCall {
span: tcx.def_span(caller_def),
caller: with_no_trimmed_paths!(tcx.def_path_str(caller_def)),
callee: with_no_trimmed_paths!(tcx.def_path_str(instance.def_id())),
};
tcx.dcx().emit_err(e);
} else {
info!(
"compiler_builtins call to diverging function {:?} replaced with abort",
instance.def_id()
);
bx.abort();
bx.unreachable();
return MergingSucc::False;
}
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 {
span: tcx.def_span(caller_def),
caller: with_no_trimmed_paths!(tcx.def_path_str(caller_def)),
callee: with_no_trimmed_paths!(tcx.def_path_str(instance.def_id())),
};
tcx.dcx().emit_err(e);
} else {
info!(
"compiler_builtins call to diverging function {:?} replaced with abort",
instance.def_id()
);
bx.abort();
bx.unreachable();
return MergingSucc::False;
}
}

View file

@ -837,15 +837,12 @@ 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");
return bx.cx().const_usize(n);
}
}
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());