Skip to content
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

old-master2 #75

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 38 additions & 0 deletions .github/workflows/enzyme-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Rust CI

on:
push:
branches:
- master
pull_request:
branches:
- master
merge_group:

jobs:
build:
name: Rust Integration CI LLVM ${{ matrix.llvm }} ${{ matrix.build }} ${{ matrix.os }}
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
os: [openstack22]

timeout-minutes: 600
steps:
- name: checkout the source code
uses: actions/checkout@v3
with:
fetch-depth: 2
- name: build
run: |
mkdir build
cd build
../configure --enable-llvm-link-shared --enable-llvm-plugins --enable-llvm-enzyme --release-channel=nightly --enable-llvm-assertions --enable-lld --enable-option-checking --enable-ninja --disable-docs
../x.py build --stage 1 library/std library/proc_macro library/test tools/rustdoc
rustup toolchain link enzyme `pwd`/build/`rustup target list --installed`/stage1
rustup toolchain install nightly # enables -Z unstable-options
- name: test
run: |
cargo +enzyme test --examples
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@
path = library/backtrace
url = https://github.com/rust-lang/backtrace-rs.git
shallow = true
[submodule "src/tools/enzyme"]
path = src/tools/enzyme
url = https://github.com/EnzymeAD/Enzyme.git
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4312,6 +4312,7 @@ dependencies = [
"rustc_middle",
"rustc_session",
"rustc_span",
"rustc_symbol_mangling",
"rustc_target",
"serde",
"serde_json",
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ exclude = [
"src/tools/x",
# stdarch has its own Cargo workspace
"library/stdarch",
"library/autodiff",
]

[profile.release.package.compiler_builtins]
Expand Down
63 changes: 61 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,68 @@
# The Rust Programming Language
# The Rust Programming Language +Enzyme (git history)

[![Rust Community](https://img.shields.io/badge/Rust_Community%20-Join_us-brightgreen?style=plastic&logo=rust)](https://www.rust-lang.org/community)

This is the main source code repository for [Rust]. It contains the compiler,
standard library, and documentation.
standard library, and documentation. It is modified to use Enzyme for AutoDiff.

Please configure this fork using the following command:

```
mkdir build
cd build
../configure --enable-llvm-link-shared --enable-llvm-plugins --enable-llvm-enzyme --release-channel=nightly --enable-llvm-assertions --enable-clang --enable-lld --enable-option-checking --enable-ninja --disable-docs
```

Afterwards you can build rustc using:
```
../x.py build --stage 1 library/std library/proc_macro library/test tools/rustdoc
```

Afterwards rustc toolchain link will allow you to use it through cargo:
```
rustup toolchain link enzyme `pwd`/build/`rustup target list --installed`/stage1
rustup toolchain install nightly # enables -Z unstable-options
```

You can then look at examples in the `library/autodiff/examples/*` folder and run them with

```bash
# rosenbrock forward iteration
cargo +enzyme run --example rosenbrock_fwd_iter --release

# or all of them
cargo +enzyme test --examples
```

## Enzyme Config
To help with debugging, Enzyme can be configured using environment variables.
```bash
export ENZYME_PRINT_TA=1
export ENZYME_PRINT_AA=1
export ENZYME_PRINT=1
export ENZYME_PRINT_MOD=1
export ENZYME_PRINT_MOD_AFTER=1
```
The first three will print TypeAnalysis, ActivityAnalysis and the llvm-ir on a function basis, respectively.
The last two variables will print the whole module directly before and after Enzyme differented the functions.

When experimenting with flags please make sure that EnzymeStrictAliasing=0
is not changed, since it is required for Enzyme to handle enums correctly.

## Bug reporting
Bugs are pretty much expected at this point of the development process.
In order to help us please minimize the Rust code as far as possible.
This tool might be a nicer helper: https://github.com/Nilstrieb/cargo-minimize
If you have some knowledge of LLVM-IR we also greatly appreciate it if you could help
us by compiling your minimized Rust code to LLVM-IR and reducing it further.

The only exception to this strategy is error based on "Can not deduce type of X",
where reducing your example will make it harder for us to understand the origin of the bug.
In this case please just try to inline all dependencies into a single crate or even file,
without deleting used code.




[Rust]: https://www.rust-lang.org/

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1592,11 +1592,13 @@ impl MacCall {
}
}

/// Manuel
/// Arguments passed to an attribute macro.
#[derive(Clone, Encodable, Decodable, Debug)]
pub enum AttrArgs {
/// No arguments: `#[attr]`.
Empty,
/// Manuel autodiff
/// Delimited arguments: `#[attr()/[]/{}]`.
Delimited(DelimArgs),
/// Arguments of a key-value attribute: `#[attr = "value"]`.
Expand Down
149 changes: 149 additions & 0 deletions compiler/rustc_ast/src/expand/autodiff_attrs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
use super::typetree::TypeTree;
use std::str::FromStr;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};//, StableOrd};
use crate::HashStableContext;

#[allow(dead_code)]
#[derive(Clone, Copy, Eq, PartialEq, Encodable, Decodable, Debug)]
pub enum DiffMode {
Inactive,
Source,
Forward,
Reverse,
}

#[allow(dead_code)]
#[derive(Clone, Copy, Eq, PartialEq, Encodable, Decodable, Debug)]
pub enum DiffActivity {
None,
Active,
Const,
Duplicated,
DuplicatedNoNeed,
}
fn clause_diffactivity_discriminant(value: &DiffActivity) -> usize {
match value {
DiffActivity::None => 0,
DiffActivity::Active => 1,
DiffActivity::Const => 2,
DiffActivity::Duplicated => 3,
DiffActivity::DuplicatedNoNeed => 4,
}
}
fn clause_diffmode_discriminant(value: &DiffMode) -> usize {
match value {
DiffMode::Inactive => 0,
DiffMode::Source => 1,
DiffMode::Forward => 2,
DiffMode::Reverse => 3,
}
}


impl<CTX: HashStableContext> HashStable<CTX> for DiffMode {
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
clause_diffmode_discriminant(self).hash_stable(hcx, hasher);
}
}

impl<CTX: HashStableContext> HashStable<CTX> for DiffActivity {
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
clause_diffactivity_discriminant(self).hash_stable(hcx, hasher);
}
}


impl FromStr for DiffActivity {
type Err = ();

fn from_str(s: &str) -> Result<DiffActivity, ()> {
match s {
"None" => Ok(DiffActivity::None),
"Active" => Ok(DiffActivity::Active),
"Const" => Ok(DiffActivity::Const),
"Duplicated" => Ok(DiffActivity::Duplicated),
"DuplicatedNoNeed" => Ok(DiffActivity::DuplicatedNoNeed),
_ => Err(()),
}
}
}

#[allow(dead_code)]
#[derive(Clone, Eq, PartialEq, Encodable, Decodable, Debug)]
pub struct AutoDiffAttrs {
pub mode: DiffMode,
pub ret_activity: DiffActivity,
pub input_activity: Vec<DiffActivity>,
}

impl<CTX: HashStableContext> HashStable<CTX> for AutoDiffAttrs {
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
self.mode.hash_stable(hcx, hasher);
self.ret_activity.hash_stable(hcx, hasher);
self.input_activity.hash_stable(hcx, hasher);
}
}

impl AutoDiffAttrs {
pub fn inactive() -> Self {
AutoDiffAttrs {
mode: DiffMode::Inactive,
ret_activity: DiffActivity::None,
input_activity: Vec::new(),
}
}

pub fn is_active(&self) -> bool {
match self.mode {
DiffMode::Inactive => false,
_ => true,
}
}

pub fn is_source(&self) -> bool {
match self.mode {
DiffMode::Source => true,
_ => false,
}
}
pub fn apply_autodiff(&self) -> bool {
match self.mode {
DiffMode::Inactive => false,
DiffMode::Source => false,
_ => true,
}
}

pub fn into_item(
self,
source: String,
target: String,
inputs: Vec<TypeTree>,
output: TypeTree,
) -> AutoDiffItem {
AutoDiffItem { source, target, inputs, output, attrs: self }
}
}

#[derive(Clone, Eq, PartialEq, Encodable, Decodable, Debug)]
pub struct AutoDiffItem {
pub source: String,
pub target: String,
pub attrs: AutoDiffAttrs,
pub inputs: Vec<TypeTree>,
pub output: TypeTree,
}

impl<CTX: HashStableContext> HashStable<CTX> for AutoDiffItem {
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
self.source.hash_stable(hcx, hasher);
self.target.hash_stable(hcx, hasher);
self.attrs.hash_stable(hcx, hasher);
for tt in &self.inputs {
tt.0.hash_stable(hcx, hasher);
}
//self.inputs.hash_stable(hcx, hasher);
self.output.0.hash_stable(hcx, hasher);
}
}

2 changes: 2 additions & 0 deletions compiler/rustc_ast/src/expand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use rustc_span::{def_id::DefId, symbol::Ident};
use crate::MetaItem;

pub mod allocator;
pub mod typetree;
pub mod autodiff_attrs;

#[derive(Debug, Clone, Encodable, Decodable, HashStable_Generic)]
pub struct StrippedCfgItem<ModId = DefId> {
Expand Down
67 changes: 67 additions & 0 deletions compiler/rustc_ast/src/expand/typetree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::fmt;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};//, StableOrd};
use crate::HashStableContext;

#[derive(Clone, Eq, PartialEq, Encodable, Decodable, Debug)]
pub enum Kind {
Anything,
Integer,
Pointer,
Half,
Float,
Double,
Unknown,
}
impl<CTX: HashStableContext> HashStable<CTX> for Kind {
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
clause_kind_discriminant(self).hash_stable(hcx, hasher);
}
}
fn clause_kind_discriminant(value: &Kind) -> usize {
match value {
Kind::Anything => 0,
Kind::Integer => 1,
Kind::Pointer => 2,
Kind::Half => 3,
Kind::Float => 4,
Kind::Double => 5,
Kind::Unknown => 6,
}
}

#[derive(Clone, Eq, PartialEq, Encodable, Decodable, Debug)]
pub struct TypeTree(pub Vec<Type>);

#[derive(Clone, Eq, PartialEq, Encodable, Decodable, Debug)]
pub struct Type {
pub offset: isize,
pub size: usize,
pub kind: Kind,
pub child: TypeTree,
}

impl<CTX: HashStableContext> HashStable<CTX> for Type {
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
self.offset.hash_stable(hcx, hasher);
self.size.hash_stable(hcx, hasher);
self.kind.hash_stable(hcx, hasher);
self.child.0.hash_stable(hcx, hasher);
}
}

impl Type {
pub fn add_offset(self, add: isize) -> Self {
let offset = match self.offset {
-1 => add,
x => add + x,
};

Self { size: self.size, kind: self.kind, child: self.child, offset }
}
}

impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
<Self as fmt::Debug>::fmt(self, f)
}
}
2 changes: 2 additions & 0 deletions compiler/rustc_builtin_macros/messages.ftl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
builtin_macros_alloc_error_must_be_fn = alloc_error_handler must be a function
builtin_macros_alloc_must_statics = allocators must be statics

builtin_macros_autodiff = autodiff must be applied to function

builtin_macros_asm_clobber_abi = clobber_abi
builtin_macros_asm_clobber_no_reg = asm with `clobber_abi` must specify explicit registers for outputs
builtin_macros_asm_clobber_outputs = generic outputs
Expand Down
Loading
Loading