rust/library/std/src
bors 785c83015c Auto merge of #129458 - EnzymeAD:enzyme-frontend, r=jieyouxu
Autodiff Upstreaming - enzyme frontend

This is an upstream PR for the `autodiff` rustc_builtin_macro that is part of the autodiff feature.

For the full implementation, see: https://github.com/rust-lang/rust/pull/129175

**Content:**
It contains a new `#[autodiff(<args>)]` rustc_builtin_macro, as well as a `#[rustc_autodiff]` builtin attribute.
The autodiff macro is applied on function `f` and will expand to a second function `df` (name given by user).
It will add a dummy body to `df` to make sure it type-checks. The body will later be replaced by enzyme on llvm-ir level,
we therefore don't really care about the content. Most of the changes (700 from 1.2k) are in `compiler/rustc_builtin_macros/src/autodiff.rs`, which expand the macro. Nothing except expansion is implemented for now.
I have a fallback implementation for relevant functions in case that rustc should be build without autodiff support. The default for now will be off, although we want to flip it later (once everything landed) to on for nightly. For the sake of CI, I have flipped the defaults, I'll revert this before merging.

**Dummy function Body:**
The first line is an `inline_asm` nop to make inlining less likely (I have additional checks to prevent this in the middle end of rustc. If `f` gets inlined too early, we can't pass it to enzyme and thus can't differentiate it.
If `df` gets inlined too early, the call site will just compute this dummy code instead of the derivatives, a correctness issue. The following black_box lines make sure that none of the input arguments is getting optimized away before we replace the body.

**Motivation:**
The user facing autodiff macro can verify the user input. Then I write it as args to the rustc_attribute, so from here on I can know that these values should be sensible. A rustc_attribute also turned out to be quite nice to attach this information to the corresponding function and carry it till the backend.
This is also just an experiment, I expect to adjust the user facing autodiff macro based on user feedback, to improve usability.

As a simple example of what this will do, we can see this expansion:
From:
```
#[autodiff(df, Reverse, Duplicated, Const, Active)]
pub fn f1(x: &[f64], y: f64) -> f64 {
    unimplemented!()
}
```
to
```
#[rustc_autodiff]
#[inline(never)]
pub fn f1(x: &[f64], y: f64) -> f64 {
    ::core::panicking::panic("not implemented")
}
#[rustc_autodiff(Reverse, Duplicated, Const, Active,)]
#[inline(never)]
pub fn df(x: &[f64], dx: &mut [f64], y: f64, dret: f64) -> f64 {
    unsafe { asm!("NOP"); };
    ::core::hint::black_box(f1(x, y));
    ::core::hint::black_box((dx, dret));
    ::core::hint::black_box(f1(x, y))
}
```
I will add a few more tests once I figured out why rustc rebuilds every time I touch a test.

Tracking:

- https://github.com/rust-lang/rust/issues/124509

try-job: dist-x86_64-msvc
2024-10-15 01:30:01 +00:00
..
backtrace remove redundant imports 2023-12-10 10:56:22 +08:00
collections impl Default for Hash{Map,Set} iterators that don't already have it 2024-10-02 23:43:48 -04:00
env Fix std tests for wasm32-wasip2 target 2024-09-29 04:48:13 +02:00
error Reformat use declarations. 2024-07-29 08:26:52 +10:00
f16 std float tests: special-case Miri in feature detection 2024-08-08 12:17:50 +02:00
f32 these tests seem to work fine on i586 these days 2024-09-10 15:57:40 -07:00
f64 these tests seem to work fine on i586 these days 2024-09-10 15:57:40 -07:00
f128 Add core functions for f16 and f128 that require math routines 2024-08-01 15:38:53 -04:00
ffi Use &raw in the standard library 2024-09-25 17:03:20 -07:00
fs Use &raw in the standard library 2024-09-25 17:03:20 -07:00
hash std: implement the random feature 2024-09-23 10:29:51 +02:00
io More clearly document Stdin::read_line 2024-10-10 23:12:03 +07:00
net Rollup merge of #129638 - nickrum:wasip2-net, r=alexcrichton 2024-09-30 19:18:49 -04:00
num removed nonfunctioning benchmark 2024-01-11 11:30:12 -05:00
os Decouple WASIp2 sockets from WasiFd 2024-10-09 14:39:28 +02:00
panic review: fix nits and move panic safety tests to the correct place 2020-09-25 23:10:24 +02:00
path Fix std tests for wasm32-wasip2 target 2024-09-29 04:48:13 +02:00
pipe Cleanup sys module to match house style 2024-07-30 19:22:54 +00:00
prelude Avoid comments that describe multiple use items. 2024-07-17 08:02:46 +10:00
process Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
sync mpmc doctest: make sure main thread waits for child threads 2024-10-02 08:00:17 +02:00
sys Rollup merge of #128967 - devnexen:get_path_fbsd_upd, r=joboet 2024-10-14 06:04:26 +02:00
sys_common std: replace LazyBox with OnceBox 2024-10-01 22:05:35 +02:00
thread std: fix stdout-before-main 2024-10-12 13:01:36 +02:00
time Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
alloc.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
ascii.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
backtrace.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
env.rs Rollup merge of #128535 - mmvanheusden:master, r=workingjubilee 2024-09-17 17:28:31 +02:00
error.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
f16.rs copysign with sign being a NaN is non-portable 2024-08-28 12:06:28 +02:00
f32.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
f64.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
f128.rs copysign with sign being a NaN is non-portable 2024-08-28 12:06:28 +02:00
fs.rs Mention allocation errors for open_buffered 2024-10-09 12:43:23 -07:00
keyword_docs.rs Library: Rename "object safe" to "dyn compatible" 2024-10-09 18:48:29 +02:00
lib.miri.rs add 'x.py miri', and make it work for 'library/{core,alloc,std}' 2024-04-03 20:27:20 +02:00
lib.rs Auto merge of #129458 - EnzymeAD:enzyme-frontend, r=jieyouxu 2024-10-15 01:30:01 +00:00
macros.rs Add math functions for f16 and f128 2024-08-01 15:38:51 -04:00
num.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
panic.rs Auto merge of #128321 - BatmanAoD:catch-unwind-doc-update, r=Mark-Simulacrum 2024-09-29 05:54:47 +00:00
panicking.rs Rollup merge of #130846 - ChrisDenton:revert-break, r=Noratrieb 2024-09-26 22:20:54 -07:00
pat.rs Add pattern types to parser 2024-04-08 11:57:17 +00:00
path.rs Use &raw in the standard library 2024-09-25 17:03:20 -07:00
pipe.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
process.rs Fix std tests for wasm32-wasip2 target 2024-09-29 04:48:13 +02:00
random.rs add link from random() helper fn to extensive DefaultRandomSource docs 2024-09-25 08:25:36 +02:00
rt.rs std: fix stdout-before-main 2024-10-12 13:01:36 +02:00
time.rs [Clippy] Swap instant_subtraction to use diagnostic item instead of path 2024-09-19 13:13:42 +01:00