Skip to content

Commit a08949b

Browse files
committed
Emit erros when invalid config
1 parent 82aeffa commit a08949b

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed

compiler/rustc_codegen_llvm/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ codegen_llvm_lto_bitcode_from_rlib = failed to get bitcode from object file for
1717
codegen_llvm_mismatch_data_layout =
1818
data-layout for target `{$rustc_target}`, `{$rustc_layout}`, differs from LLVM target's `{$llvm_target}` default layout, `{$llvm_layout}`
1919
20+
codegen_llvm_offload_without_enable = using the offload feature requires -Z offload=Enable
21+
codegen_llvm_offload_without_fat_lto = using the offload feature requires -C lto=fat
22+
2023
codegen_llvm_parse_bitcode = failed to parse bitcode for LTO module
2124
codegen_llvm_parse_bitcode_with_llvm_err = failed to parse bitcode for LTO module: {$llvm_err}
2225

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> {
3636
#[diag(codegen_llvm_autodiff_without_enable)]
3737
pub(crate) struct AutoDiffWithoutEnable;
3838

39+
#[derive(Diagnostic)]
40+
#[diag(codegen_llvm_offload_without_enable)]
41+
pub(crate) struct OffloadWithoutEnable;
42+
43+
#[derive(Diagnostic)]
44+
#[diag(codegen_llvm_offload_without_fat_lto)]
45+
pub(crate) struct OffloadWithoutFatLTO;
46+
3947
#[derive(Diagnostic)]
4048
#[diag(codegen_llvm_lto_bitcode_from_rlib)]
4149
pub(crate) struct LtoBitcodeFromRlib {

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::builder::Builder;
2727
use crate::builder::autodiff::{adjust_activity_to_abi, generate_enzyme_call};
2828
use crate::builder::gpu_offload::TgtOffloadEntry;
2929
use crate::context::CodegenCx;
30-
use crate::errors::AutoDiffWithoutEnable;
30+
use crate::errors::{AutoDiffWithoutEnable, OffloadWithoutEnable, OffloadWithoutFatLTO};
3131
use crate::llvm::{self, Metadata, Type, Value};
3232
use crate::type_of::LayoutLlvmExt;
3333
use crate::va_arg::emit_va_arg;
@@ -199,7 +199,20 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
199199
return Ok(());
200200
}
201201
sym::offload => {
202-
// TODO(Sa4dUs): emit error when offload is not enabled
202+
if !tcx
203+
.sess
204+
.opts
205+
.unstable_opts
206+
.offload
207+
.contains(&rustc_session::config::Offload::Enable)
208+
{
209+
let _ = tcx.dcx().emit_almost_fatal(OffloadWithoutEnable);
210+
}
211+
212+
if tcx.sess.lto() != rustc_session::config::Lto::Fat {
213+
let _ = tcx.dcx().emit_almost_fatal(OffloadWithoutFatLTO);
214+
}
215+
203216
codegen_offload(self, tcx, instance, args);
204217
return Ok(());
205218
}

compiler/rustc_middle/src/ty/offload_meta.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn get_payload_size<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> u64 {
7474
}
7575

7676
impl MappingFlags {
77-
fn from_ty<'tcx>(_tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self {
77+
fn from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self {
7878
use rustc_ast::Mutability::*;
7979

8080
match ty.kind() {
@@ -109,7 +109,11 @@ impl MappingFlags {
109109
| ty::Bound(_, _)
110110
| ty::Placeholder(_)
111111
| ty::Infer(_)
112-
| ty::Error(_) => MappingFlags::TO, /* TODO(Sa4dUs): emit error */
112+
| ty::Error(_) => {
113+
tcx.dcx()
114+
.span_err(rustc_span::DUMMY_SP, format!("type `{ty:?}` cannot be offloaded"));
115+
MappingFlags::empty()
116+
}
113117
}
114118
}
115119
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
error: using the offload feature requires -Z offload=Enable
2+
3+
error: using the offload feature requires -C lto=fat
4+
5+
error: aborting due to 2 previous errors
6+

tests/ui/offload/check_config.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ revisions: pass fail
2+
//@[pass] build-pass
3+
//@[fail] build-fail
4+
//@[pass] compile-flags: -Zunstable-options -Zoffload=Enable -Clto=fat --emit=metadata
5+
//@[fail] compile-flags: -Clto=thin
6+
7+
//[fail]~? ERROR: using the offload feature requires -Z offload=Enable
8+
//[fail]~? ERROR: using the offload feature requires -C lto=fat
9+
10+
#![feature(core_intrinsics)]
11+
12+
fn main() {
13+
let mut x = [3.0; 256];
14+
kernel_1(&mut x);
15+
}
16+
17+
fn kernel_1(x: &mut [f32; 256]) {
18+
core::intrinsics::offload(_kernel_1, (x,))
19+
}
20+
21+
fn _kernel_1(x: &mut [f32; 256]) {}

0 commit comments

Comments
 (0)