Skip to content

Commit ec6f622

Browse files
committed
Auto merge of #150079 - jhpratt:rollup-iehzejf, r=jhpratt
Rollup of 4 pull requests Successful merges: - #150000 (Port `#[rustc_legacy_const_generics]` to use attribute parser ) - #150060 (autodiff: emit an error if we fail to find libEnzyme) - #150070 (Partially revert #147888 and print warning if LLVM CMake dir is missing when building Enzyme) - #150072 (Port #[no_link] to use attribute parser) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 2dc3024 + 656d4e8 commit ec6f622

File tree

26 files changed

+448
-382
lines changed

26 files changed

+448
-382
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -237,25 +237,27 @@ impl SpanLowerer {
237237
#[extension(trait ResolverAstLoweringExt)]
238238
impl ResolverAstLowering {
239239
fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>> {
240-
if let ExprKind::Path(None, path) = &expr.kind {
241-
// Don't perform legacy const generics rewriting if the path already
242-
// has generic arguments.
243-
if path.segments.last().unwrap().args.is_some() {
244-
return None;
245-
}
240+
let ExprKind::Path(None, path) = &expr.kind else {
241+
return None;
242+
};
246243

247-
if let Res::Def(DefKind::Fn, def_id) = self.partial_res_map.get(&expr.id)?.full_res()? {
248-
// We only support cross-crate argument rewriting. Uses
249-
// within the same crate should be updated to use the new
250-
// const generics style.
251-
if def_id.is_local() {
252-
return None;
253-
}
244+
// Don't perform legacy const generics rewriting if the path already
245+
// has generic arguments.
246+
if path.segments.last().unwrap().args.is_some() {
247+
return None;
248+
}
254249

255-
if let Some(v) = self.legacy_const_generic_args.get(&def_id) {
256-
return v.clone();
257-
}
258-
}
250+
let def_id = self.partial_res_map.get(&expr.id)?.full_res()?.opt_def_id()?;
251+
252+
// We only support cross-crate argument rewriting. Uses
253+
// within the same crate should be updated to use the new
254+
// const generics style.
255+
if def_id.is_local() {
256+
return None;
257+
}
258+
259+
if let Some(v) = self.legacy_const_generic_args.get(&def_id) {
260+
return v.clone();
259261
}
260262

261263
None

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub(crate) mod loop_match;
4747
pub(crate) mod macro_attrs;
4848
pub(crate) mod must_use;
4949
pub(crate) mod no_implicit_prelude;
50+
pub(crate) mod no_link;
5051
pub(crate) mod non_exhaustive;
5152
pub(crate) mod path;
5253
pub(crate) mod pin_v2;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use super::prelude::*;
2+
3+
pub(crate) struct NoLinkParser;
4+
impl<S: Stage> NoArgsAttributeParser<S> for NoLinkParser {
5+
const PATH: &[Symbol] = &[sym::no_link];
6+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Warn;
7+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
8+
Allow(Target::ExternCrate),
9+
Warn(Target::Field),
10+
Warn(Target::Arm),
11+
Warn(Target::MacroDef),
12+
]);
13+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::NoLink;
14+
}

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use rustc_ast::{LitIntType, LitKind, MetaItemLit};
2+
13
use super::prelude::*;
24
use super::util::parse_single_integer;
35
use crate::session_diagnostics::RustcScalableVectorCountOutOfRange;
@@ -41,6 +43,50 @@ impl<S: Stage> SingleAttributeParser<S> for RustcLayoutScalarValidRangeEndParser
4143
}
4244
}
4345

46+
pub(crate) struct RustcLegacyConstGenericsParser;
47+
48+
impl<S: Stage> SingleAttributeParser<S> for RustcLegacyConstGenericsParser {
49+
const PATH: &[Symbol] = &[sym::rustc_legacy_const_generics];
50+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
51+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
52+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
53+
const TEMPLATE: AttributeTemplate = template!(List: &["N"]);
54+
55+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
56+
let ArgParser::List(meta_items) = args else {
57+
cx.expected_list(cx.attr_span, args);
58+
return None;
59+
};
60+
61+
let mut parsed_indexes = ThinVec::new();
62+
let mut errored = false;
63+
64+
for possible_index in meta_items.mixed() {
65+
if let MetaItemOrLitParser::Lit(MetaItemLit {
66+
kind: LitKind::Int(index, LitIntType::Unsuffixed),
67+
..
68+
}) = possible_index
69+
{
70+
parsed_indexes.push((index.0 as usize, possible_index.span()));
71+
} else {
72+
cx.expected_integer_literal(possible_index.span());
73+
errored = true;
74+
}
75+
}
76+
if errored {
77+
return None;
78+
} else if parsed_indexes.is_empty() {
79+
cx.expected_at_least_one_argument(args.span()?);
80+
return None;
81+
}
82+
83+
Some(AttributeKind::RustcLegacyConstGenerics {
84+
fn_indexes: parsed_indexes,
85+
attr_span: cx.attr_span,
86+
})
87+
}
88+
}
89+
4490
pub(crate) struct RustcObjectLifetimeDefaultParser;
4591

4692
impl<S: Stage> SingleAttributeParser<S> for RustcObjectLifetimeDefaultParser {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use crate::attributes::macro_attrs::{
5050
};
5151
use crate::attributes::must_use::MustUseParser;
5252
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
53+
use crate::attributes::no_link::NoLinkParser;
5354
use crate::attributes::non_exhaustive::NonExhaustiveParser;
5455
use crate::attributes::path::PathParser as PathAttributeParser;
5556
use crate::attributes::pin_v2::PinV2Parser;
@@ -59,9 +60,9 @@ use crate::attributes::proc_macro_attrs::{
5960
use crate::attributes::prototype::CustomMirParser;
6061
use crate::attributes::repr::{AlignParser, AlignStaticParser, ReprParser};
6162
use crate::attributes::rustc_internal::{
62-
RustcLayoutScalarValidRangeEndParser, RustcLayoutScalarValidRangeStartParser, RustcMainParser,
63-
RustcObjectLifetimeDefaultParser, RustcScalableVectorParser,
64-
RustcSimdMonomorphizeLaneLimitParser,
63+
RustcLayoutScalarValidRangeEndParser, RustcLayoutScalarValidRangeStartParser,
64+
RustcLegacyConstGenericsParser, RustcMainParser, RustcObjectLifetimeDefaultParser,
65+
RustcScalableVectorParser, RustcSimdMonomorphizeLaneLimitParser,
6566
};
6667
use crate::attributes::semantics::MayDangleParser;
6768
use crate::attributes::stability::{
@@ -209,6 +210,7 @@ attribute_parsers!(
209210
Single<RustcForceInlineParser>,
210211
Single<RustcLayoutScalarValidRangeEndParser>,
211212
Single<RustcLayoutScalarValidRangeStartParser>,
213+
Single<RustcLegacyConstGenericsParser>,
212214
Single<RustcObjectLifetimeDefaultParser>,
213215
Single<RustcScalableVectorParser>,
214216
Single<RustcSimdMonomorphizeLaneLimitParser>,
@@ -240,6 +242,7 @@ attribute_parsers!(
240242
Single<WithoutArgs<MayDangleParser>>,
241243
Single<WithoutArgs<NoCoreParser>>,
242244
Single<WithoutArgs<NoImplicitPreludeParser>>,
245+
Single<WithoutArgs<NoLinkParser>>,
243246
Single<WithoutArgs<NoMangleParser>>,
244247
Single<WithoutArgs<NoStdParser>>,
245248
Single<WithoutArgs<NonExhaustiveParser>>,

compiler/rustc_codegen_llvm/messages.ftl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
codegen_llvm_autodiff_component_unavailable = failed to load our autodiff backend. Did you install it via rustup?
2+
13
codegen_llvm_autodiff_without_enable = using the autodiff feature requires -Z autodiff=Enable
24
codegen_llvm_autodiff_without_lto = using the autodiff feature requires setting `lto="fat"` in your Cargo.toml
35
46
codegen_llvm_copy_bitcode = failed to copy bitcode to object file: {$err}
57
6-
78
codegen_llvm_fixed_x18_invalid_arch = the `-Zfixed-x18` flag is not supported on the `{$arch}` architecture
89
910
codegen_llvm_from_llvm_diag = {$message}

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for ParseTargetMachineConfig<'_> {
3232
}
3333
}
3434

35+
#[cfg(feature = "llvm_enzyme")]
36+
#[derive(Diagnostic)]
37+
#[diag(codegen_llvm_autodiff_component_unavailable)]
38+
pub(crate) struct AutoDiffComponentUnavailable;
39+
3540
#[derive(Diagnostic)]
3641
#[diag(codegen_llvm_autodiff_without_lto)]
3742
pub(crate) struct AutoDiffWithoutLto;

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![feature(impl_trait_in_assoc_type)]
1414
#![feature(iter_intersperse)]
1515
#![feature(macro_derive)]
16+
#![feature(once_cell_try)]
1617
#![feature(trim_prefix_suffix)]
1718
#![feature(try_blocks)]
1819
// tidy-alphabetical-end
@@ -247,7 +248,9 @@ impl CodegenBackend for LlvmCodegenBackend {
247248

248249
use crate::back::lto::enable_autodiff_settings;
249250
if sess.opts.unstable_opts.autodiff.contains(&AutoDiff::Enable) {
250-
drop(llvm::EnzymeWrapper::get_or_init(&sess.opts.sysroot));
251+
if let Err(_) = llvm::EnzymeWrapper::get_or_init(&sess.opts.sysroot) {
252+
sess.dcx().emit_fatal(crate::errors::AutoDiffComponentUnavailable);
253+
}
251254
enable_autodiff_settings(&sess.opts.unstable_opts.autodiff);
252255
}
253256
}

compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,13 @@ pub(crate) mod Enzyme_AD {
199199
/// Safe to call multiple times - subsequent calls are no-ops due to OnceLock.
200200
pub(crate) fn get_or_init(
201201
sysroot: &rustc_session::config::Sysroot,
202-
) -> MutexGuard<'static, Self> {
203-
ENZYME_INSTANCE
204-
.get_or_init(|| {
205-
Self::call_dynamic(sysroot)
206-
.unwrap_or_else(|e| bug!("failed to load Enzyme: {e}"))
207-
.into()
208-
})
209-
.lock()
210-
.unwrap()
202+
) -> Result<MutexGuard<'static, Self>, Box<dyn std::error::Error>> {
203+
let mtx: &'static Mutex<EnzymeWrapper> = ENZYME_INSTANCE.get_or_try_init(|| {
204+
let w = Self::call_dynamic(sysroot)?;
205+
Ok::<_, Box<dyn std::error::Error>>(Mutex::new(w))
206+
})?;
207+
208+
Ok(mtx.lock().unwrap())
211209
}
212210

213211
/// Get the EnzymeWrapper instance. Panics if not initialized.
@@ -475,7 +473,7 @@ pub(crate) mod Fallback_AD {
475473
impl EnzymeWrapper {
476474
pub(crate) fn get_or_init(
477475
_sysroot: &rustc_session::config::Sysroot,
478-
) -> MutexGuard<'static, Self> {
476+
) -> Result<MutexGuard<'static, Self>, Box<dyn std::error::Error>> {
479477
unimplemented!("Enzyme not available: build with llvm_enzyme feature")
480478
}
481479

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,9 @@ pub enum AttributeKind {
803803
/// Represents `#[no_implicit_prelude]`
804804
NoImplicitPrelude(Span),
805805

806+
/// Represents `#[no_link]`
807+
NoLink,
808+
806809
/// Represents `#[no_mangle]`
807810
NoMangle(Span),
808811

@@ -869,6 +872,9 @@ pub enum AttributeKind {
869872
/// Represents `#[rustc_layout_scalar_valid_range_start]`.
870873
RustcLayoutScalarValidRangeStart(Box<u128>, Span),
871874

875+
/// Represents `#[rustc_legacy_const_generics]`
876+
RustcLegacyConstGenerics { fn_indexes: ThinVec<(usize, Span)>, attr_span: Span },
877+
872878
/// Represents `#[rustc_main]`.
873879
RustcMain,
874880

0 commit comments

Comments
 (0)