|
3 | 3 | use libc::{c_char, c_uint};
|
4 | 4 |
|
5 | 5 | use super::MetadataKindId;
|
6 |
| -use super::ffi::{AttributeKind, BasicBlock, Metadata, Module, Type, Value}; |
| 6 | +use super::ffi::{AttributeKind, BasicBlock, Context, Metadata, Module, Type, Value}; |
7 | 7 | use crate::llvm::{Bool, Builder};
|
8 | 8 |
|
| 9 | +// TypeTree types |
| 10 | +pub(crate) type CTypeTreeRef = *mut EnzymeTypeTree; |
| 11 | + |
| 12 | +#[repr(C)] |
| 13 | +#[derive(Debug, Copy, Clone)] |
| 14 | +pub(crate) struct EnzymeTypeTree { |
| 15 | + _unused: [u8; 0], |
| 16 | +} |
| 17 | + |
| 18 | +#[repr(u32)] |
| 19 | +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] |
| 20 | +#[allow(non_camel_case_types)] |
| 21 | +pub(crate) enum CConcreteType { |
| 22 | + DT_Anything = 0, |
| 23 | + DT_Integer = 1, |
| 24 | + DT_Pointer = 2, |
| 25 | + DT_Half = 3, |
| 26 | + DT_Float = 4, |
| 27 | + DT_Double = 5, |
| 28 | + DT_Unknown = 6, |
| 29 | +} |
| 30 | + |
| 31 | +pub(crate) struct TypeTree { |
| 32 | + pub(crate) inner: CTypeTreeRef, |
| 33 | +} |
| 34 | + |
9 | 35 | #[link(name = "llvm-wrapper", kind = "static")]
|
10 | 36 | unsafe extern "C" {
|
11 | 37 | // Enzyme
|
@@ -68,10 +94,33 @@ pub(crate) mod Enzyme_AD {
|
68 | 94 |
|
69 | 95 | use libc::c_void;
|
70 | 96 |
|
| 97 | + use super::{CConcreteType, CTypeTreeRef, Context}; |
| 98 | + |
71 | 99 | unsafe extern "C" {
|
72 | 100 | pub(crate) fn EnzymeSetCLBool(arg1: *mut ::std::os::raw::c_void, arg2: u8);
|
73 | 101 | pub(crate) fn EnzymeSetCLString(arg1: *mut ::std::os::raw::c_void, arg2: *const c_char);
|
74 | 102 | }
|
| 103 | + |
| 104 | + // TypeTree functions |
| 105 | + unsafe extern "C" { |
| 106 | + pub(crate) fn EnzymeNewTypeTree() -> CTypeTreeRef; |
| 107 | + pub(crate) fn EnzymeNewTypeTreeCT(arg1: CConcreteType, ctx: &Context) -> CTypeTreeRef; |
| 108 | + pub(crate) fn EnzymeNewTypeTreeTR(arg1: CTypeTreeRef) -> CTypeTreeRef; |
| 109 | + pub(crate) fn EnzymeFreeTypeTree(CTT: CTypeTreeRef); |
| 110 | + pub(crate) fn EnzymeMergeTypeTree(arg1: CTypeTreeRef, arg2: CTypeTreeRef) -> bool; |
| 111 | + pub(crate) fn EnzymeTypeTreeOnlyEq(arg1: CTypeTreeRef, pos: i64); |
| 112 | + pub(crate) fn EnzymeTypeTreeData0Eq(arg1: CTypeTreeRef); |
| 113 | + pub(crate) fn EnzymeTypeTreeShiftIndiciesEq( |
| 114 | + arg1: CTypeTreeRef, |
| 115 | + data_layout: *const c_char, |
| 116 | + offset: i64, |
| 117 | + max_size: i64, |
| 118 | + add_offset: u64, |
| 119 | + ); |
| 120 | + pub(crate) fn EnzymeTypeTreeToString(arg1: CTypeTreeRef) -> *const c_char; |
| 121 | + pub(crate) fn EnzymeTypeTreeToStringFree(arg1: *const c_char); |
| 122 | + } |
| 123 | + |
75 | 124 | unsafe extern "C" {
|
76 | 125 | static mut EnzymePrintPerf: c_void;
|
77 | 126 | static mut EnzymePrintActivity: c_void;
|
@@ -141,6 +190,57 @@ pub(crate) use self::Fallback_AD::*;
|
141 | 190 | pub(crate) mod Fallback_AD {
|
142 | 191 | #![allow(unused_variables)]
|
143 | 192 |
|
| 193 | + use libc::c_char; |
| 194 | + |
| 195 | + use super::{CConcreteType, CTypeTreeRef, Context}; |
| 196 | + |
| 197 | + // TypeTree function fallbacks |
| 198 | + pub(crate) unsafe fn EnzymeNewTypeTree() -> CTypeTreeRef { |
| 199 | + unimplemented!() |
| 200 | + } |
| 201 | + |
| 202 | + pub(crate) unsafe fn EnzymeNewTypeTreeCT(arg1: CConcreteType, ctx: &Context) -> CTypeTreeRef { |
| 203 | + unimplemented!() |
| 204 | + } |
| 205 | + |
| 206 | + pub(crate) unsafe fn EnzymeNewTypeTreeTR(arg1: CTypeTreeRef) -> CTypeTreeRef { |
| 207 | + unimplemented!() |
| 208 | + } |
| 209 | + |
| 210 | + pub(crate) unsafe fn EnzymeFreeTypeTree(CTT: CTypeTreeRef) { |
| 211 | + unimplemented!() |
| 212 | + } |
| 213 | + |
| 214 | + pub(crate) unsafe fn EnzymeMergeTypeTree(arg1: CTypeTreeRef, arg2: CTypeTreeRef) -> bool { |
| 215 | + unimplemented!() |
| 216 | + } |
| 217 | + |
| 218 | + pub(crate) unsafe fn EnzymeTypeTreeOnlyEq(arg1: CTypeTreeRef, pos: i64) { |
| 219 | + unimplemented!() |
| 220 | + } |
| 221 | + |
| 222 | + pub(crate) unsafe fn EnzymeTypeTreeData0Eq(arg1: CTypeTreeRef) { |
| 223 | + unimplemented!() |
| 224 | + } |
| 225 | + |
| 226 | + pub(crate) unsafe fn EnzymeTypeTreeShiftIndiciesEq( |
| 227 | + arg1: CTypeTreeRef, |
| 228 | + data_layout: *const c_char, |
| 229 | + offset: i64, |
| 230 | + max_size: i64, |
| 231 | + add_offset: u64, |
| 232 | + ) { |
| 233 | + unimplemented!() |
| 234 | + } |
| 235 | + |
| 236 | + pub(crate) unsafe fn EnzymeTypeTreeToString(arg1: CTypeTreeRef) -> *const c_char { |
| 237 | + unimplemented!() |
| 238 | + } |
| 239 | + |
| 240 | + pub(crate) unsafe fn EnzymeTypeTreeToStringFree(arg1: *const c_char) { |
| 241 | + unimplemented!() |
| 242 | + } |
| 243 | + |
144 | 244 | pub(crate) fn set_inline(val: bool) {
|
145 | 245 | unimplemented!()
|
146 | 246 | }
|
@@ -169,3 +269,83 @@ pub(crate) mod Fallback_AD {
|
169 | 269 | unimplemented!()
|
170 | 270 | }
|
171 | 271 | }
|
| 272 | + |
| 273 | +impl TypeTree { |
| 274 | + pub(crate) fn new() -> TypeTree { |
| 275 | + let inner = unsafe { EnzymeNewTypeTree() }; |
| 276 | + TypeTree { inner } |
| 277 | + } |
| 278 | + |
| 279 | + pub(crate) fn from_type(t: CConcreteType, ctx: &Context) -> TypeTree { |
| 280 | + let inner = unsafe { EnzymeNewTypeTreeCT(t, ctx) }; |
| 281 | + TypeTree { inner } |
| 282 | + } |
| 283 | + |
| 284 | + pub(crate) fn merge(self, other: Self) -> Self { |
| 285 | + unsafe { |
| 286 | + EnzymeMergeTypeTree(self.inner, other.inner); |
| 287 | + } |
| 288 | + drop(other); |
| 289 | + self |
| 290 | + } |
| 291 | + |
| 292 | + #[must_use] |
| 293 | + pub(crate) fn shift( |
| 294 | + self, |
| 295 | + layout: &str, |
| 296 | + offset: isize, |
| 297 | + max_size: isize, |
| 298 | + add_offset: usize, |
| 299 | + ) -> Self { |
| 300 | + let layout = std::ffi::CString::new(layout).unwrap(); |
| 301 | + |
| 302 | + unsafe { |
| 303 | + EnzymeTypeTreeShiftIndiciesEq( |
| 304 | + self.inner, |
| 305 | + layout.as_ptr(), |
| 306 | + offset as i64, |
| 307 | + max_size as i64, |
| 308 | + add_offset as u64, |
| 309 | + ); |
| 310 | + } |
| 311 | + |
| 312 | + self |
| 313 | + } |
| 314 | +} |
| 315 | + |
| 316 | +impl Clone for TypeTree { |
| 317 | + fn clone(&self) -> Self { |
| 318 | + let inner = unsafe { EnzymeNewTypeTreeTR(self.inner) }; |
| 319 | + TypeTree { inner } |
| 320 | + } |
| 321 | +} |
| 322 | + |
| 323 | +impl std::fmt::Display for TypeTree { |
| 324 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 325 | + let ptr = unsafe { EnzymeTypeTreeToString(self.inner) }; |
| 326 | + let cstr = unsafe { std::ffi::CStr::from_ptr(ptr) }; |
| 327 | + match cstr.to_str() { |
| 328 | + Ok(x) => write!(f, "{}", x)?, |
| 329 | + Err(err) => write!(f, "could not parse: {}", err)?, |
| 330 | + } |
| 331 | + |
| 332 | + // delete C string pointer |
| 333 | + unsafe { |
| 334 | + EnzymeTypeTreeToStringFree(ptr); |
| 335 | + } |
| 336 | + |
| 337 | + Ok(()) |
| 338 | + } |
| 339 | +} |
| 340 | + |
| 341 | +impl std::fmt::Debug for TypeTree { |
| 342 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 343 | + <Self as std::fmt::Display>::fmt(self, f) |
| 344 | + } |
| 345 | +} |
| 346 | + |
| 347 | +impl Drop for TypeTree { |
| 348 | + fn drop(&mut self) { |
| 349 | + unsafe { EnzymeFreeTypeTree(self.inner) } |
| 350 | + } |
| 351 | +} |
0 commit comments