1- use std:: ffi:: { CString , c_char, c_uint } ;
1+ use std:: ffi:: { CString , c_char} ;
22
3- use rustc_ast:: expand:: typetree:: { FncTree , TypeTree as RustTypeTree } ;
3+ use rustc_ast:: expand:: typetree:: { FncTree , Kind , TypeTree as RustTypeTree } ;
4+ use rustc_middle:: ty:: TyCtxt ;
45
56use crate :: attributes;
67use crate :: llvm:: { self , EnzymeWrapper , Value } ;
78
89fn to_enzyme_typetree (
9- rust_typetree : RustTypeTree ,
10+ rust_typetree : & RustTypeTree ,
1011 _data_layout : & str ,
1112 llcx : & llvm:: Context ,
12- ) -> llvm:: TypeTree {
13+ ) -> ( llvm:: TypeTree , Vec < llvm :: TypeTree > ) {
1314 let mut enzyme_tt = llvm:: TypeTree :: new ( ) ;
14- process_typetree_recursive ( & mut enzyme_tt, & rust_typetree, & [ ] , llcx) ;
15- enzyme_tt
15+ let extra_ints = process_typetree_recursive ( & mut enzyme_tt, & rust_typetree, & [ ] , llcx) ;
16+
17+ let mut int_vec = vec ! [ ] ;
18+ for _ in 0 ..extra_ints {
19+ let mut int_tt = llvm:: TypeTree :: new ( ) ;
20+ int_tt. insert ( & [ 0 ] , llvm:: CConcreteType :: DT_Integer , llcx) ;
21+ int_vec. push ( int_tt) ;
22+ }
23+
24+ ( enzyme_tt, int_vec)
1625}
26+
1727fn process_typetree_recursive (
1828 enzyme_tt : & mut llvm:: TypeTree ,
1929 rust_typetree : & RustTypeTree ,
2030 parent_indices : & [ i64 ] ,
2131 llcx : & llvm:: Context ,
22- ) {
32+ ) -> u32 {
33+ let mut extra_ints = 0 ;
2334 for rust_type in & rust_typetree. 0 {
2435 let concrete_type = match rust_type. kind {
25- rustc_ast:: expand:: typetree:: Kind :: Anything => llvm:: CConcreteType :: DT_Anything ,
26- rustc_ast:: expand:: typetree:: Kind :: Integer => llvm:: CConcreteType :: DT_Integer ,
27- rustc_ast:: expand:: typetree:: Kind :: Pointer => llvm:: CConcreteType :: DT_Pointer ,
28- rustc_ast:: expand:: typetree:: Kind :: Half => llvm:: CConcreteType :: DT_Half ,
29- rustc_ast:: expand:: typetree:: Kind :: Float => llvm:: CConcreteType :: DT_Float ,
30- rustc_ast:: expand:: typetree:: Kind :: Double => llvm:: CConcreteType :: DT_Double ,
31- rustc_ast:: expand:: typetree:: Kind :: F128 => llvm:: CConcreteType :: DT_FP128 ,
32- rustc_ast:: expand:: typetree:: Kind :: Unknown => llvm:: CConcreteType :: DT_Unknown ,
36+ Kind :: Anything => llvm:: CConcreteType :: DT_Anything ,
37+ Kind :: Integer => llvm:: CConcreteType :: DT_Integer ,
38+ Kind :: Pointer => llvm:: CConcreteType :: DT_Pointer ,
39+ Kind :: RustSlice => llvm:: CConcreteType :: DT_Pointer ,
40+ Kind :: Half => llvm:: CConcreteType :: DT_Half ,
41+ Kind :: Float => llvm:: CConcreteType :: DT_Float ,
42+ Kind :: Double => llvm:: CConcreteType :: DT_Double ,
43+ Kind :: F128 => llvm:: CConcreteType :: DT_FP128 ,
44+ Kind :: Unknown => llvm:: CConcreteType :: DT_Unknown ,
3345 } ;
3446
3547 let mut indices = parent_indices. to_vec ( ) ;
@@ -43,18 +55,31 @@ fn process_typetree_recursive(
4355
4456 enzyme_tt. insert ( & indices, concrete_type, llcx) ;
4557
46- if rust_type. kind == rustc_ast:: expand:: typetree:: Kind :: Pointer
58+ if matches ! ( rust_type. kind, Kind :: RustSlice ) {
59+ // We lower slices to `ptr,int`, so add the int here.
60+ extra_ints += 1 ;
61+ }
62+
63+ if matches ! ( rust_type. kind, Kind :: Pointer | Kind :: RustSlice )
4764 && !rust_type. child . 0 . is_empty ( )
4865 {
4966 process_typetree_recursive ( enzyme_tt, & rust_type. child , & indices, llcx) ;
5067 }
5168 }
69+ extra_ints
70+ }
71+
72+ // Describes all the locations in which we know how to apply an Enzyme TypeTree.
73+ enum TTLocation {
74+ Definition ,
75+ Callsite ,
5276}
5377
5478#[ cfg_attr( not( feature = "llvm_enzyme" ) , allow( unused) ) ]
55- pub ( crate ) fn add_tt < ' ll > (
79+ pub ( crate ) fn add_tt < ' tcx , ' ll > (
5680 llmod : & ' ll llvm:: Module ,
5781 llcx : & ' ll llvm:: Context ,
82+ tcx : TyCtxt < ' tcx > ,
5883 fn_def : & ' ll Value ,
5984 tt : FncTree ,
6085) {
@@ -66,6 +91,13 @@ pub(crate) fn add_tt<'ll>(
6691 #[ cfg( not( feature = "llvm_enzyme" ) ) ]
6792 return ;
6893
94+ if !tcx. sess . opts . unstable_opts . autodiff . contains ( & rustc_session:: config:: AutoDiff :: Enable ) {
95+ return ;
96+ }
97+ if tcx. sess . opts . unstable_opts . autodiff . contains ( & rustc_session:: config:: AutoDiff :: NoTT ) {
98+ return ;
99+ }
100+
69101 let inputs = tt. args ;
70102 let ret_tt: RustTypeTree = tt. ret ;
71103
@@ -77,41 +109,81 @@ pub(crate) fn add_tt<'ll>(
77109 let attr_name = "enzyme_type" ;
78110 let c_attr_name = CString :: new ( attr_name) . unwrap ( ) ;
79111
112+ let tt_location: TTLocation =
113+ if llvm:: LLVMRustIsCall ( fn_def) { TTLocation :: Callsite } else { TTLocation :: Definition } ;
114+
115+ let mut offset = 0 ;
80116 for ( i, input) in inputs. iter ( ) . enumerate ( ) {
81- unsafe {
82- let enzyme_tt = to_enzyme_typetree ( input. clone ( ) , llvm_data_layout, llcx) ;
117+ let ( enzyme_tt, extra_ints) = to_enzyme_typetree ( & input, llvm_data_layout, llcx) ;
118+
119+ // This scope is just a visual reminder that we *must* drop the enzyme_wrapper before
120+ // we drop any typetrees (mainly enzyme_tt and extra_ints). Drop calls can not accept
121+ // arguments like an enzyme_wrapper, so the typetree drop impl has to call get_instance
122+ // on the static enzyme instance, which is behind a Mutex. Therefore we'd deadlock if we
123+ // hold the enzyme_wrapper while dropping the typetrees.
124+ {
83125 let enzyme_wrapper = EnzymeWrapper :: get_instance ( ) ;
84- let c_str = enzyme_wrapper. tree_to_string ( enzyme_tt. inner ) ;
85- let c_str = std:: ffi:: CStr :: from_ptr ( c_str) ;
86-
87- let attr = llvm:: LLVMCreateStringAttribute (
88- llcx,
89- c_attr_name. as_ptr ( ) ,
90- c_attr_name. as_bytes ( ) . len ( ) as c_uint ,
91- c_str. as_ptr ( ) ,
92- c_str. to_bytes ( ) . len ( ) as c_uint ,
93- ) ;
94-
95- attributes:: apply_to_llfn ( fn_def, llvm:: AttributePlace :: Argument ( i as u32 ) , & [ attr] ) ;
126+ let c_str = enzyme_wrapper. tree_to_cstr ( enzyme_tt. inner ) ;
127+
128+ let attr = llvm:: CreateAttrStringValueFromCStr ( llcx, & c_attr_name, & c_str) ;
129+ let arg_pos = llvm:: AttributePlace :: Argument ( i as u32 + offset) ;
130+ // FIXME(autodiff): We currently know that this is correct for all the cases in which we
131+ // call this function. But we should make it more robust for the future.
132+ match tt_location {
133+ TTLocation :: Definition => {
134+ attributes:: apply_to_llfn ( fn_def, arg_pos, & [ attr] ) ;
135+ }
136+ TTLocation :: Callsite => {
137+ attributes:: apply_to_callsite ( fn_def, arg_pos, & [ attr] ) ;
138+ }
139+ }
96140 enzyme_wrapper. tree_to_string_free ( c_str. as_ptr ( ) ) ;
141+ for v in & extra_ints {
142+ offset += 1 ;
143+ let c_str = enzyme_wrapper. tree_to_cstr ( v. inner ) ;
144+ let int_attr = llvm:: CreateAttrStringValueFromCStr ( llcx, & c_attr_name, & c_str) ;
145+ let arg_pos = llvm:: AttributePlace :: Argument ( i as u32 + offset) ;
146+ match tt_location {
147+ TTLocation :: Definition => {
148+ attributes:: apply_to_llfn ( fn_def, arg_pos, & [ int_attr] ) ;
149+ }
150+ TTLocation :: Callsite => {
151+ attributes:: apply_to_callsite ( fn_def, arg_pos, & [ int_attr] ) ;
152+ }
153+ }
154+ enzyme_wrapper. tree_to_string_free ( c_str. as_ptr ( ) ) ;
155+ }
156+ }
157+ }
158+ // We will only fail this if Rust types got lowered to LLVM in a way that we didn't predict.
159+ // Error, so we can learn from our mistakes.
160+ if matches ! ( tt_location, TTLocation :: Definition ) {
161+ let expected = offset as usize + inputs. len ( ) ;
162+ let actual = llvm:: count_params ( fn_def) as usize ;
163+ if expected != actual {
164+ tcx. dcx ( ) . warn ( format ! (
165+ "autodiff type-tree failure. We expected {expected} LLVM argument(s), \
166+ but the generated LLVM function has {actual} parameter(s)"
167+ ) ) ;
97168 }
98169 }
99170
100- unsafe {
101- let enzyme_tt = to_enzyme_typetree ( ret_tt, llvm_data_layout, llcx) ;
171+ // FIXME(autodiff): We should think more about what it means if a function returns a slice or
172+ // other fat ptrs.
173+ let ( enzyme_tt, _extra_ints) = to_enzyme_typetree ( & ret_tt, llvm_data_layout, llcx) ;
174+ if ret_tt != RustTypeTree :: new ( ) {
102175 let enzyme_wrapper = EnzymeWrapper :: get_instance ( ) ;
103- let c_str = enzyme_wrapper. tree_to_string ( enzyme_tt. inner ) ;
104- let c_str = std:: ffi:: CStr :: from_ptr ( c_str) ;
105-
106- let ret_attr = llvm:: LLVMCreateStringAttribute (
107- llcx,
108- c_attr_name. as_ptr ( ) ,
109- c_attr_name. as_bytes ( ) . len ( ) as c_uint ,
110- c_str. as_ptr ( ) ,
111- c_str. to_bytes ( ) . len ( ) as c_uint ,
112- ) ;
113-
114- attributes:: apply_to_llfn ( fn_def, llvm:: AttributePlace :: ReturnValue , & [ ret_attr] ) ;
176+ let c_str = enzyme_wrapper. tree_to_cstr ( enzyme_tt. inner ) ;
177+ let ret_attr = llvm:: CreateAttrStringValueFromCStr ( llcx, & c_attr_name, & c_str) ;
178+ let arg_pos = llvm:: AttributePlace :: ReturnValue ;
179+ match tt_location {
180+ TTLocation :: Definition => {
181+ attributes:: apply_to_llfn ( fn_def, arg_pos, & [ ret_attr] ) ;
182+ }
183+ TTLocation :: Callsite => {
184+ attributes:: apply_to_callsite ( fn_def, arg_pos, & [ ret_attr] ) ;
185+ }
186+ }
115187 enzyme_wrapper. tree_to_string_free ( c_str. as_ptr ( ) ) ;
116188 }
117189}
0 commit comments