Skip to content
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

rustc: add support for split debuginfo (aka fission) #3168

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 30 additions & 9 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,13 @@ def rustc_compile_action(
if experimental_use_cc_common_link:
emit = ["obj"]

use_split_debuginfo = cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "per_object_debug_info") and ctx.fragments.cpp.fission_active_for_current_compilation_mode()
if use_split_debuginfo:
rust_flags = rust_flags + [
"--codegen=split-debuginfo=unpacked",
"--codegen=debuginfo=full",
]

args, env_from_args = construct_arguments(
ctx = ctx,
attr = attr,
Expand Down Expand Up @@ -1340,6 +1347,13 @@ def rustc_compile_action(
elif toolchain.target_os == "darwin":
dsym_folder = ctx.actions.declare_directory(crate_info.output.basename + ".dSYM", sibling = crate_info.output)
action_outputs.append(dsym_folder)
if use_split_debuginfo:
fission_directory = crate_info.name + "_fission"
if output_hash:
fission_directory = fission_directory + output_hash
dwo_outputs = ctx.actions.declare_directory(fission_directory, sibling = crate_info.output)
args.process_wrapper_flags.add("--kludge-move-dwo-to", dwo_outputs.path)
action_outputs.append(dwo_outputs)

if ctx.executable._process_wrapper:
# Run as normal
Expand Down Expand Up @@ -1398,15 +1412,19 @@ def rustc_compile_action(
else:
fail("No process wrapper was defined for {}".format(ctx.label))

cco_args = {}
if experimental_use_cc_common_link:
# Wrap the main `.o` file into a compilation output suitable for
# cc_common.link. The main `.o` file is useful in both PIC and non-PIC
# modes.
compilation_outputs = cc_common.create_compilation_outputs(
objects = depset([output_o]),
pic_objects = depset([output_o]),
)

cco_args["objects"] = depset([output_o])
cco_args["pic_objects"] = depset([output_o])
if use_split_debuginfo:
cco_args["dwo_objects"] = depset([dwo_outputs]) # buildifier: disable=uninitialized
cco_args["pic_dwo_objects"] = depset([dwo_outputs]) # buildifier: disable=uninitialized
compilation_outputs = cc_common.create_compilation_outputs(**cco_args)
debug_context = cc_common.create_debug_context(compilation_outputs)
if experimental_use_cc_common_link:
malloc_library = ctx.attr._custom_malloc or ctx.attr.malloc

# Collect the linking contexts of the standard library and dependencies.
Expand Down Expand Up @@ -1551,7 +1569,7 @@ def rustc_compile_action(
else:
providers.extend([crate_info, dep_info])

providers += establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_configuration, interface_library)
providers += establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_configuration, interface_library, debug_context)

output_group_info = {}

Expand Down Expand Up @@ -1638,7 +1656,7 @@ def _add_codegen_units_flags(toolchain, args):

args.add("-Ccodegen-units={}".format(toolchain._codegen_units))

def establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_configuration, interface_library):
def establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_configuration, interface_library, debug_context = None):
"""If the produced crate is suitable yield a CcInfo to allow for interop with cc rules

Args:
Expand All @@ -1649,7 +1667,7 @@ def establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_co
cc_toolchain (CcToolchainInfo): The current `CcToolchainInfo`
feature_configuration (FeatureConfiguration): Feature configuration to be queried.
interface_library (File): Optional interface library for cdylib crates on Windows.

debug_context (DebugContext): Optional debug context.
Returns:
list: A list containing the CcInfo provider
"""
Expand Down Expand Up @@ -1715,7 +1733,10 @@ def establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_co
)

cc_infos = [
CcInfo(linking_context = linking_context),
CcInfo(
linking_context = linking_context,
debug_context = debug_context,
),
toolchain.stdlib_linkflags,
]

Expand Down
18 changes: 18 additions & 0 deletions util/process_wrapper/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,24 @@ fn main() -> Result<(), ProcessWrapperError> {
))
})?;
}
if let Some(dwp_destination) = opts.kludge_move_dwo_to {
// find all .dwo files in our out_dir (how do we get that? --out-dir?)
// move them all to dwp_destination
let dest = std::path::Path::new(&dwp_destination);
// Unwrap is okay because we know that --kludge-move-dwo-two will be a child of
// rustc's --out-dir.
let out_dir = dest.parent().unwrap();
let paths = std::fs::read_dir(out_dir).unwrap();
for p in paths {
let p = p.unwrap();
let fn_ = p.file_name();
let basename = fn_.to_string_lossy();
if basename.ends_with(".dwo") {
let tgt = dest.join(fn_);
std::fs::rename(p.path(), tgt).map_err(|e| ProcessWrapperError(format!("failed to rename dwo output: {}", e)))?;
}
}
}
}

exit(code)
Expand Down
9 changes: 9 additions & 0 deletions util/process_wrapper/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ pub(crate) struct Options {
pub(crate) rustc_quit_on_rmeta: bool,
// This controls the output format of rustc messages.
pub(crate) rustc_output_format: Option<rustc::ErrorFormat>,
// If set, move any produced .dwo files into the named directory.
pub(crate) kludge_move_dwo_to: Option<String>,
}

pub(crate) fn options() -> Result<Options, OptionError> {
Expand All @@ -67,6 +69,7 @@ pub(crate) fn options() -> Result<Options, OptionError> {
let mut rustc_quit_on_rmeta_raw = None;
let mut rustc_output_format_raw = None;
let mut flags = Flags::new();
let mut kludge_move_dwo_to = None;
flags.define_repeated_flag("--subst", "", &mut subst_mapping_raw);
flags.define_flag("--stable-status-file", "", &mut stable_status_file_raw);
flags.define_flag("--volatile-status-file", "", &mut volatile_status_file_raw);
Expand Down Expand Up @@ -114,6 +117,11 @@ pub(crate) fn options() -> Result<Options, OptionError> {
Default: `rendered`",
&mut rustc_output_format_raw,
);
flags.define_flag(
"--kludge-move-dwo-to",
"If set, move all produced .dwo files to the specified directory.",
&mut kludge_move_dwo_to,
);

let mut child_args = match flags
.parse(env::args().collect())
Expand Down Expand Up @@ -212,6 +220,7 @@ pub(crate) fn options() -> Result<Options, OptionError> {
output_file,
rustc_quit_on_rmeta,
rustc_output_format,
kludge_move_dwo_to,
})
}

Expand Down
Loading