Skip to content

Commit e01b74f

Browse files
committed
adding proper error handling for offload
1 parent b60ff30 commit e01b74f

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

compiler/rustc_codegen_llvm/messages.ftl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ codegen_llvm_lto_bitcode_from_rlib = failed to get bitcode from object file for
1818
codegen_llvm_mismatch_data_layout =
1919
data-layout for target `{$rustc_target}`, `{$rustc_layout}`, differs from LLVM target's `{$llvm_target}` default layout, `{$llvm_layout}`
2020
21-
codegen_llvm_offload_without_enable = using the offload feature requires -Z offload=Enable
21+
codegen_llvm_offload_without_enable = using the offload feature requires -Z offload=<Device or Host=/absolute/path/to/host.out>
22+
codegen_llvm_offload_no_abs_path = using the `-Z offload=Host=/absolute/path/to/host.out` flag requires an absolute path
23+
codegen_llvm_offload_nonexisting = the given path/file to `host.out` does not exist. Did you forget to run the device compilation first?
24+
codegen_llvm_offload_no_host_out = using the `-Z offload=Host=/absolute/path/to/host.out` flag must point to a `host.out` file
2225
codegen_llvm_offload_without_fat_lto = using the offload feature requires -C lto=fat
26+
codegen_llvm_offload_bundleimages_failed = BundleImages call failed, `host.out` was not created
27+
codegen_llvm_offload_embed_failed = EmbedBufferInModule call failed, `host.o` was not created
2328
2429
codegen_llvm_parse_bitcode = failed to parse bitcode for LTO module
2530
codegen_llvm_parse_bitcode_with_llvm_err = failed to parse bitcode for LTO module: {$llvm_err}

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -783,9 +783,8 @@ pub(crate) unsafe fn llvm_optimize(
783783
module.module_llvm.tm.raw(),
784784
device_out_c.as_ptr(),
785785
);
786-
assert!(ok, "LLVMRustBundleImages (device -> host.out) failed");
787-
if !device_out.exists() {
788-
panic!("BundleImages failed, `host.out` was not created!");
786+
if !ok || !device_out.exists() {
787+
dcx.emit_err(crate::errors::OffloadBundleImagesFailed);
789788
}
790789
}
791790
}
@@ -803,15 +802,16 @@ pub(crate) unsafe fn llvm_optimize(
803802
{
804803
let device_pathbuf = PathBuf::from(device_path);
805804
if device_pathbuf.is_relative() {
806-
panic!("Absolute path is needed");
805+
dcx.emit_err(crate::errors::OffloadWithoutAbsPath);
807806
} else if device_pathbuf
808807
.file_name()
809808
.and_then(|n| n.to_str())
810809
.is_some_and(|n| n != "host.out")
811810
{
812-
panic!("Need path to the host.out file");
811+
dcx.emit_err(crate::errors::OffloadWrongFileName);
812+
} else if !device_pathbuf.exists() {
813+
dcx.emit_err(crate::errors::OffloadNonexistingPath);
813814
}
814-
assert!(device_pathbuf.exists());
815815
let host_path = cgcx.output_filenames.path(OutputType::Object);
816816
let host_dir = host_path.parent().unwrap();
817817
let out_obj = host_dir.join("host.o");
@@ -823,7 +823,9 @@ pub(crate) unsafe fn llvm_optimize(
823823
let llmod2 = llvm::LLVMCloneModule(module.module_llvm.llmod());
824824
let ok =
825825
unsafe { llvm::LLVMRustOffloadEmbedBufferInModule(llmod2, host_out_c.as_ptr()) };
826-
assert!(ok, "LLVMRustOffloadEmbedBufferInModule failed");
826+
if !ok {
827+
dcx.emit_err(crate::errors::OffloadEmbedFailed);
828+
}
827829
write_output_file(
828830
dcx,
829831
module.module_llvm.tm.raw(),
@@ -835,10 +837,6 @@ pub(crate) unsafe fn llvm_optimize(
835837
&cgcx.prof,
836838
true,
837839
);
838-
if !out_obj.exists() {
839-
dbg!("{:?} does not exist!", out_obj);
840-
panic!("FinalizeOffload failed!");
841-
}
842840
// We ignore cgcx.save_temps here and unconditionally always keep our `host.out` artifact.
843841
// Otherwise, recompiling the host code would fail since we deleted that device artifact
844842
// in the previous host compilation, which would be confusing at best.

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ pub(crate) struct OffloadWithoutEnable;
4848
#[diag(codegen_llvm_offload_without_fat_lto)]
4949
pub(crate) struct OffloadWithoutFatLTO;
5050

51+
#[derive(Diagnostic)]
52+
#[diag(codegen_llvm_offload_no_abs_path)]
53+
pub(crate) struct OffloadWithoutAbsPath;
54+
55+
#[derive(Diagnostic)]
56+
#[diag(codegen_llvm_offload_no_host_out)]
57+
pub(crate) struct OffloadWrongFileName;
58+
59+
#[derive(Diagnostic)]
60+
#[diag(codegen_llvm_offload_nonexisting)]
61+
pub(crate) struct OffloadNonexistingPath;
62+
63+
#[derive(Diagnostic)]
64+
#[diag(codegen_llvm_offload_bundleimages_failed)]
65+
pub(crate) struct OffloadBundleImagesFailed;
66+
67+
#[derive(Diagnostic)]
68+
#[diag(codegen_llvm_offload_embed_failed)]
69+
pub(crate) struct OffloadEmbedFailed;
70+
5171
#[derive(Diagnostic)]
5272
#[diag(codegen_llvm_lto_bitcode_from_rlib)]
5373
pub(crate) struct LtoBitcodeFromRlib {

0 commit comments

Comments
 (0)