Skip to content

Rollup of 8 pull requests #140354

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

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
12dd4a1
Stabilise c_str_module
clarfonthey Feb 22, 2025
6be84b1
Somehow these stability attributes were able to be omitted before?
clarfonthey Feb 22, 2025
3536324
Fix detection of `main` function if there are expressions around it
GuillaumeGomez Apr 23, 2025
81438c0
Add regression ui test for #140162 and for #139651
GuillaumeGomez Apr 23, 2025
8cd12bf
check types of const param default
lcnr Apr 10, 2025
f319dd9
add llvm wrappers and corresponding methods in attribute
Shourya742 Apr 20, 2025
9bc0401
add custom enzyme markers to target methods
Shourya742 Apr 20, 2025
48d05aa
remove noinline attribute and add alwaysinline after AD pass
Shourya742 Apr 20, 2025
3ededc1
Improve code
GuillaumeGomez Apr 25, 2025
3ef98a5
If there is a `;` alone, we consider that the doctest needs to be put…
GuillaumeGomez Apr 25, 2025
59b6cf5
uefi: Update r-efi
Ayush1325 Mar 20, 2025
43d8d89
clarified bootstrap optimization agrs
Kivooeo Apr 26, 2025
86969db
session: Cleanup `CanonicalizedPath::new`
petrochenkov Apr 26, 2025
9112c86
Update example to use `CStr::to_string_lossy`
shepmaster Apr 25, 2025
aa69e3a
Fix bad handling of macros if there is already a `main` function
GuillaumeGomez Apr 26, 2025
59eca8a
Rollup merge of #137439 - clarfonthey:c-str-module, r=tgross35
tgross35 Apr 27, 2025
4cf8048
Rollup merge of #138737 - Ayush1325:r-efi-update, r=tgross35
tgross35 Apr 27, 2025
ff4a1fc
Rollup merge of #139308 - Shourya742:2025-03-29-add-autodiff-inline, …
tgross35 Apr 27, 2025
6afbf8a
Rollup merge of #139646 - lcnr:default-is-fully-concrete, r=BoxyUwU
tgross35 Apr 27, 2025
e813782
Rollup merge of #140220 - GuillaumeGomez:doctest-main-wrapping, r=fmease
tgross35 Apr 27, 2025
1387180
Rollup merge of #140297 - shepmaster:cstr-lossy, r=joboet
tgross35 Apr 27, 2025
bec3dad
Rollup merge of #140330 - Kivooeo:new-fix-five, r=clubby789
tgross35 Apr 27, 2025
2ab151b
Rollup merge of #140339 - petrochenkov:capanew, r=lqd
tgross35 Apr 27, 2025
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
2 changes: 1 addition & 1 deletion bootstrap.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@
# building without optimizations takes much longer than optimizing. Further, some platforms
# fail to build without this optimization (c.f. #65352).
# The valid options are:
# true - Enable optimizations.
# true - Enable optimizations (same as 3).
# false - Disable optimizations.
# 0 - Disable optimizations.
# 1 - Basic optimizations.
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_codegen_llvm/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ pub(crate) fn apply_to_callsite(callsite: &Value, idx: AttributePlace, attrs: &[
}
}

pub(crate) fn has_attr(llfn: &Value, idx: AttributePlace, attr: AttributeKind) -> bool {
llvm::HasAttributeAtIndex(llfn, idx, attr)
}

pub(crate) fn has_string_attr(llfn: &Value, name: *const i8) -> bool {
llvm::HasStringAttribute(llfn, name)
}

pub(crate) fn remove_from_llfn(llfn: &Value, place: AttributePlace, kind: AttributeKind) {
llvm::RemoveRustEnumAttributeAtIndex(llfn, place, kind);
}

pub(crate) fn remove_string_attr_from_llfn(llfn: &Value, name: *const i8) {
llvm::RemoveStringAttrFromFn(llfn, name);
}

/// Get LLVM attribute for the provided inline heuristic.
#[inline]
fn inline_attr<'ll>(cx: &CodegenCx<'ll, '_>, inline: InlineAttr) -> Option<&'ll Attribute> {
Expand Down
30 changes: 29 additions & 1 deletion compiler/rustc_codegen_llvm/src/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ use crate::back::write::{
use crate::errors::{
DynamicLinkingWithLTO, LlvmError, LtoBitcodeFromRlib, LtoDisallowed, LtoDylib, LtoProcMacro,
};
use crate::llvm::AttributePlace::Function;
use crate::llvm::{self, build_string};
use crate::{LlvmCodegenBackend, ModuleLlvm};
use crate::{LlvmCodegenBackend, ModuleLlvm, SimpleCx, attributes};

/// We keep track of the computed LTO cache keys from the previous
/// session to determine which CGUs we can reuse.
Expand Down Expand Up @@ -666,6 +667,33 @@ pub(crate) fn run_pass_manager(
}

if cfg!(llvm_enzyme) && enable_ad && !thin {
let cx =
SimpleCx::new(module.module_llvm.llmod(), &module.module_llvm.llcx, cgcx.pointer_size);

for function in cx.get_functions() {
let enzyme_marker = CString::new("enzyme_marker").unwrap();
let marker_ptr = enzyme_marker.as_ptr();

if attributes::has_string_attr(function, marker_ptr) {
// Sanity check: Ensure 'noinline' is present before replacing it.
assert!(
!attributes::has_attr(function, Function, llvm::AttributeKind::NoInline),
"Expected __enzyme function to have 'noinline' before adding 'alwaysinline'"
);

attributes::remove_from_llfn(function, Function, llvm::AttributeKind::NoInline);
attributes::remove_string_attr_from_llfn(function, marker_ptr);

assert!(
!attributes::has_string_attr(function, marker_ptr),
"Expected function to not have 'enzyme_marker'"
);

let always_inline = llvm::AttributeKind::AlwaysInline.create_attr(cx.llcx);
attributes::apply_to_llfn(function, Function, &[always_inline]);
}
}

let opt_stage = llvm::OptStage::FatLTO;
let stage = write::AutodiffStage::PostAD;
if !config.autodiff.contains(&config::AutoDiff::NoPostopt) {
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_codegen_llvm/src/builder/autodiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@ fn generate_enzyme_call<'ll>(
let attr = llvm::AttributeKind::NoInline.create_attr(cx.llcx);
attributes::apply_to_llfn(ad_fn, Function, &[attr]);

// We add a made-up attribute just such that we can recognize it after AD to update
// (no)-inline attributes. We'll then also remove this attribute.
let enzyme_marker_attr = llvm::CreateAttrString(cx.llcx, "enzyme_marker");
attributes::apply_to_llfn(outer_fn, Function, &[enzyme_marker_attr]);

// first, remove all calls from fnc
let entry = llvm::LLVMGetFirstBasicBlock(outer_fn);
let br = llvm::LLVMRustGetTerminator(entry);
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_codegen_llvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,16 @@ impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
llvm::LLVMMDStringInContext2(self.llcx(), name.as_ptr() as *const c_char, name.len())
})
}

pub(crate) fn get_functions(&self) -> Vec<&'ll Value> {
let mut functions = vec![];
let mut func = unsafe { llvm::LLVMGetFirstFunction(self.llmod()) };
while let Some(f) = func {
functions.push(f);
func = unsafe { llvm::LLVMGetNextFunction(f) }
}
functions
}
}

impl<'ll, 'tcx> MiscCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ unsafe extern "C" {
pub(crate) fn LLVMRustVerifyFunction(V: &Value, action: LLVMRustVerifierFailureAction) -> Bool;
pub(crate) fn LLVMRustHasAttributeAtIndex(V: &Value, i: c_uint, Kind: AttributeKind) -> bool;
pub(crate) fn LLVMRustGetArrayNumElements(Ty: &Type) -> u64;
pub(crate) fn LLVMRustHasFnAttribute(F: &Value, Name: *const c_char) -> bool;
pub(crate) fn LLVMRustRemoveFnAttribute(F: &Value, Name: *const c_char);
pub(crate) fn LLVMGetFirstFunction(M: &Module) -> Option<&Value>;
pub(crate) fn LLVMGetNextFunction(Fn: &Value) -> Option<&Value>;
pub(crate) fn LLVMRustRemoveEnumAttributeAtIndex(
Fn: &Value,
index: c_uint,
kind: AttributeKind,
);
}

unsafe extern "C" {
Expand Down
26 changes: 26 additions & 0 deletions compiler/rustc_codegen_llvm/src/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ pub(crate) fn AddFunctionAttributes<'ll>(
}
}

pub(crate) fn HasAttributeAtIndex<'ll>(
llfn: &'ll Value,
idx: AttributePlace,
kind: AttributeKind,
) -> bool {
unsafe { LLVMRustHasAttributeAtIndex(llfn, idx.as_uint(), kind) }
}

pub(crate) fn HasStringAttribute<'ll>(llfn: &'ll Value, name: *const i8) -> bool {
unsafe { LLVMRustHasFnAttribute(llfn, name) }
}

pub(crate) fn RemoveStringAttrFromFn<'ll>(llfn: &'ll Value, name: *const i8) {
unsafe { LLVMRustRemoveFnAttribute(llfn, name) }
}

pub(crate) fn RemoveRustEnumAttributeAtIndex(
llfn: &Value,
place: AttributePlace,
kind: AttributeKind,
) {
unsafe {
LLVMRustRemoveEnumAttributeAtIndex(llfn, place.as_uint(), kind);
}
}

pub(crate) fn AddCallSiteAttributes<'ll>(
callsite: &'ll Value,
idx: AttributePlace,
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_codegen_llvm/src/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
(**self).borrow().llcx
}

pub(crate) fn llmod(&self) -> &'ll llvm::Module {
(**self).borrow().llmod
}

pub(crate) fn isize_ty(&self) -> &'ll Type {
(**self).borrow().isize_ty
}
Expand Down
33 changes: 33 additions & 0 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,39 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
.then(|| WellFormedLoc::Ty(param.def_id.expect_local())),
default.as_term().unwrap(),
);
} else {
// If we've got a generic const parameter we still want to check its
// type is correct in case both it and the param type are fully concrete.
let GenericArgKind::Const(ct) = default.unpack() else {
continue;
};

let ct_ty = match ct.kind() {
ty::ConstKind::Infer(_)
| ty::ConstKind::Placeholder(_)
| ty::ConstKind::Bound(_, _) => unreachable!(),
ty::ConstKind::Error(_) | ty::ConstKind::Expr(_) => continue,
ty::ConstKind::Value(cv) => cv.ty,
ty::ConstKind::Unevaluated(uv) => {
infcx.tcx.type_of(uv.def).instantiate(infcx.tcx, uv.args)
}
ty::ConstKind::Param(param_ct) => param_ct.find_ty_from_env(wfcx.param_env),
};

let param_ty = tcx.type_of(param.def_id).instantiate_identity();
if !ct_ty.has_param() && !param_ty.has_param() {
let cause = traits::ObligationCause::new(
tcx.def_span(param.def_id),
wfcx.body_def_id,
ObligationCauseCode::WellFormed(None),
);
wfcx.register_obligation(Obligation::new(
tcx,
cause,
wfcx.param_env,
ty::ClauseKind::ConstArgHasType(ct, param_ty),
));
}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(rustc::bad_opt_access)]
use std::collections::{BTreeMap, BTreeSet};
use std::collections::BTreeMap;
use std::num::NonZero;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::sync::atomic::AtomicBool;

use rustc_abi::Align;
Expand Down Expand Up @@ -89,8 +89,8 @@ where
S: Into<String>,
I: IntoIterator<Item = S>,
{
let locations: BTreeSet<CanonicalizedPath> =
locations.into_iter().map(|s| CanonicalizedPath::new(Path::new(&s.into()))).collect();
let locations =
locations.into_iter().map(|s| CanonicalizedPath::new(PathBuf::from(s.into()))).collect();

ExternEntry {
location: ExternLocation::ExactPaths(locations),
Expand Down
19 changes: 19 additions & 0 deletions compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,25 @@ extern "C" LLVMMetadataRef LLVMRustDIGetInstMetadata(LLVMValueRef x) {
return nullptr;
}

extern "C" void
LLVMRustRemoveEnumAttributeAtIndex(LLVMValueRef F, size_t index,
LLVMRustAttributeKind RustAttr) {
LLVMRemoveEnumAttributeAtIndex(F, index, fromRust(RustAttr));
}

extern "C" bool LLVMRustHasFnAttribute(LLVMValueRef F, const char *Name) {
if (auto *Fn = dyn_cast<Function>(unwrap<Value>(F))) {
return Fn->hasFnAttribute(Name);
}
return false;
}

extern "C" void LLVMRustRemoveFnAttribute(LLVMValueRef Fn, const char *Name) {
if (auto *F = dyn_cast<Function>(unwrap<Value>(Fn))) {
F->removeFnAttr(Name);
}
}

extern "C" void LLVMRustGlobalAddMetadata(LLVMValueRef Global, unsigned Kind,
LLVMMetadataRef MD) {
unwrap<GlobalObject>(Global)->addMetadata(Kind, *unwrap<MDNode>(MD));
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2323,14 +2323,13 @@ pub fn parse_externs(
let ExternOpt { crate_name: name, path, options } =
split_extern_opt(early_dcx, unstable_opts, &arg).unwrap_or_else(|e| e.emit());

let path = path.map(|p| CanonicalizedPath::new(p.as_path()));

let entry = externs.entry(name.to_owned());

use std::collections::btree_map::Entry;

let entry = if let Some(path) = path {
// --extern prelude_name=some_file.rlib
let path = CanonicalizedPath::new(path);
match entry {
Entry::Vacant(vacant) => {
let files = BTreeSet::from_iter(iter::once(path));
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_session/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::sync::OnceLock;

use rustc_data_structures::profiling::VerboseTimingGuard;
Expand Down Expand Up @@ -104,8 +104,8 @@ pub struct CanonicalizedPath {
}

impl CanonicalizedPath {
pub fn new(path: &Path) -> Self {
Self { original: path.to_owned(), canonicalized: try_canonicalize(path).ok() }
pub fn new(path: PathBuf) -> Self {
Self { canonicalized: try_canonicalize(&path).ok(), original: path }
}

pub fn canonicalized(&self) -> &PathBuf {
Expand Down
8 changes: 4 additions & 4 deletions library/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -257,19 +257,19 @@ dependencies = [

[[package]]
name = "r-efi"
version = "4.5.0"
version = "5.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9e935efc5854715dfc0a4c9ef18dc69dee0ec3bf9cc3ab740db831c0fdd86a3"
checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5"
dependencies = [
"compiler_builtins",
"rustc-std-workspace-core",
]

[[package]]
name = "r-efi-alloc"
version = "1.0.0"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31d6f09fe2b6ad044bc3d2c34ce4979796581afd2f1ebc185837e02421e02fd7"
checksum = "e43c53ff1a01d423d1cb762fd991de07d32965ff0ca2e4f80444ac7804198203"
dependencies = [
"compiler_builtins",
"r-efi",
Expand Down
2 changes: 2 additions & 0 deletions library/alloc/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ impl From<Vec<NonZero<u8>>> for CString {
}
}

#[stable(feature = "c_string_from_str", since = "1.85.0")]
impl FromStr for CString {
type Err = NulError;

Expand All @@ -830,6 +831,7 @@ impl FromStr for CString {
}
}

#[stable(feature = "c_string_from_str", since = "1.85.0")]
impl TryFrom<CString> for String {
type Error = IntoStringError;

Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,5 @@ pub use self::c_str::CString;
#[stable(feature = "alloc_c_string", since = "1.64.0")]
pub use self::c_str::{FromVecWithNulError, IntoStringError, NulError};

#[unstable(feature = "c_str_module", issue = "112134")]
#[stable(feature = "c_str_module", since = "CURRENT_RUSTC_VERSION")]
pub mod c_str;
5 changes: 3 additions & 2 deletions library/core/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ use crate::{fmt, ops, slice, str};
///
/// fn my_string_safe() -> String {
/// let cstr = unsafe { CStr::from_ptr(my_string()) };
/// // Get copy-on-write Cow<'_, str>, then guarantee a freshly-owned String allocation
/// String::from_utf8_lossy(cstr.to_bytes()).to_string()
/// // Get a copy-on-write Cow<'_, str>, then extract the
/// // allocated String (or allocate a fresh one if needed).
/// cstr.to_string_lossy().into_owned()
/// }
///
/// println!("string: {}", my_string_safe());
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use self::c_str::FromBytesUntilNulError;
pub use self::c_str::FromBytesWithNulError;
use crate::fmt;

#[unstable(feature = "c_str_module", issue = "112134")]
#[stable(feature = "c_str_module", since = "CURRENT_RUSTC_VERSION")]
pub mod c_str;

#[unstable(
Expand Down
4 changes: 2 additions & 2 deletions library/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ wasi = { version = "0.11.0", features = [
], default-features = false }

[target.'cfg(target_os = "uefi")'.dependencies]
r-efi = { version = "4.5.0", features = ['rustc-dep-of-std'] }
r-efi-alloc = { version = "1.0.0", features = ['rustc-dep-of-std'] }
r-efi = { version = "5.2.0", features = ['rustc-dep-of-std'] }
r-efi-alloc = { version = "2.0.0", features = ['rustc-dep-of-std'] }

[features]
backtrace = [
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@

#![stable(feature = "rust1", since = "1.0.0")]

#[unstable(feature = "c_str_module", issue = "112134")]
#[stable(feature = "c_str_module", since = "CURRENT_RUSTC_VERSION")]
pub mod c_str;

#[stable(feature = "core_c_void", since = "1.30.0")]
Expand Down
1 change: 0 additions & 1 deletion library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@
#![feature(array_chunks)]
#![feature(bstr)]
#![feature(bstr_internals)]
#![feature(c_str_module)]
#![feature(char_internals)]
#![feature(clone_to_uninit)]
#![feature(core_intrinsics)]
Expand Down
Loading
Loading