-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
frontend: Add support for numeric constants.
- Loading branch information
Showing
7 changed files
with
258 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
use crate::{ | ||
prelude::{ | ||
CubeContext, CubePrimitive, ExpandElementTyped, UInt, BF16, F16, F32, F64, I32, I64, | ||
}, | ||
unexpanded, | ||
}; | ||
|
||
use half::{bf16, f16}; | ||
use paste::paste; | ||
|
||
macro_rules! tt_as_expr { | ||
($t:expr) => { | ||
$t | ||
}; | ||
} | ||
|
||
macro_rules! tt_as_ty { | ||
(( $t:ty )) => { | ||
$t | ||
}; | ||
([ $t:ty ]) => { | ||
$t | ||
}; | ||
({ $t:ty }) => { | ||
$t | ||
}; | ||
($t:ty) => { | ||
$t | ||
}; | ||
} | ||
|
||
macro_rules! impl_op { | ||
($tr:ident { $( $name:ident $(-> { $($ret_type:tt)+ } )? ),* } => { $($type:ty | $expr:tt),* }) => { | ||
paste! { | ||
impl_op!(@default | ||
[], | ||
$tr, | ||
{ $({ $name, [<lim_ $name:lower>], [<__expand_lim_ $name:lower>], $( { $($ret_type)+ } )? })* }, | ||
{ $($type, $expr);* } | ||
); | ||
} | ||
}; | ||
|
||
// Default $ret_type to {Self} | ||
(@default [$($acc:tt)*], $tr:ident, {}, $types:tt) => { | ||
impl_op!(@internal | ||
$tr, | ||
{ $($acc);* }, | ||
$types | ||
); | ||
}; | ||
(@default [$($acc:tt)*], $tr:ident, { { $name:ident, $unexpand_name:ident, $expand_name:ident, } $($tail:tt)* }, $types:tt) => { | ||
impl_op!(@default | ||
[ $($acc)* { $name, $unexpand_name, $expand_name, {Self} } ], | ||
$tr, | ||
{ $($tail)* }, | ||
$types | ||
); | ||
}; | ||
(@default [$($acc:tt)*], $tr:ident, { { $name:ident, $unexpand_name:ident, $expand_name:ident, $ret_type:tt } $($tail:tt)* }, $types:tt) => { | ||
impl_op!(@default | ||
[ $($acc)* { $name, $unexpand_name, $expand_name, $ret_type } ], | ||
$tr, | ||
{ $($tail)* }, | ||
$types | ||
); | ||
}; | ||
|
||
// Generate trait declarations and impl blocks | ||
(@internal $tr:ident, $values:tt, $types:tt) => { | ||
impl_op!(@decls $tr, $values); | ||
impl_op!(@impls $tr, $types, $values); | ||
}; | ||
|
||
// Generate trait declarations | ||
(@decls $tr:ident, { $({ $name:ident, $unexpand_name:ident, $expand_name:ident, { $ret_type:ty } });* }) => { | ||
pub trait $tr: CubePrimitive + Sized { | ||
$( | ||
fn $unexpand_name() -> $ret_type { | ||
unexpanded!() | ||
} | ||
fn $expand_name(_context: &mut CubeContext) -> ExpandElementTyped<$ret_type>; | ||
)* | ||
} | ||
}; | ||
|
||
// Generate impl blocks | ||
(@impls $tr:ident, { $($type:ty, $e:tt);* }, $values:tt) => { | ||
$( | ||
impl $tr for $type { | ||
impl_op!(@impl $e, $values); | ||
} | ||
)* | ||
}; | ||
|
||
// Generate impl block contents | ||
(@impl $e:tt, { $({ $name:ident, $unexpand_name:ident, $expand_name:ident, $ret_type:tt });* }) => { | ||
$( | ||
fn $expand_name(_context: &mut CubeContext) -> ExpandElementTyped<tt_as_ty!($ret_type)> { | ||
impl_op!(@implbody $e, $name, $ret_type) | ||
} | ||
)* | ||
}; | ||
|
||
(@implbody { $({ $($select_type:tt)+ } => $e:tt),+; $default_e:tt }, $name:ident, $ret_type:tt) => { | ||
impl_op!(@implbody $({ { $($select_type)+ }, $e }),+; $default_e, $name, $ret_type) | ||
}; | ||
|
||
(@implbody { $select_type:tt, $e:tt } $(, $acc:tt)*; $default_e:tt, $name:ident, $ret_type:tt) => {{ | ||
macro_rules! __emit_on_match { | ||
($select_type, $select_type) => { impl_op!(@implbody $e, $name, { $ret_type }) }; | ||
($select_type, $ret_type) => { impl_op!(@implbody $($acc),*; $default_e, $name, $ret_type) }; | ||
} | ||
|
||
__emit_on_match!($select_type, $ret_type) | ||
}}; | ||
|
||
(@implbody ; $default_e:tt, $name:ident, $($unused:tt)*) => { | ||
impl_op!(@implbody $default_e, $name,) | ||
}; | ||
|
||
(@implbody ( $($e:tt)* ), $name:ident, $($unused:tt)*) => { | ||
ExpandElementTyped::from_lit(tt_as_expr!(impl_op!(@subst $name, [], $($e)*))) | ||
}; | ||
|
||
// Handle @name substitution | ||
|
||
// No more tokens to process, return the token tree | ||
(@subst $name:ident, [$($acc:tt)*],) => { | ||
$($acc)* | ||
}; | ||
|
||
// Handle replacement | ||
(@subst $name:ident, [$($acc:tt)*], @name $($tail:tt)*) => { | ||
impl_op!(@subst $name, | ||
[ $($acc)* $name ], | ||
$($tail)* | ||
) | ||
}; | ||
|
||
// Handle empty token trees | ||
(@subst $name:ident, [$($acc:tt)*], () $($tail:tt)*) => { | ||
impl_op!(@subst $name, | ||
[ $($acc)* () ], | ||
$($tail)* | ||
) | ||
}; | ||
(@subst $name:ident, [$($acc:tt)*], [] $($tail:tt)*) => { | ||
impl_op!(@subst $name, | ||
[ $($acc)* [] ], | ||
$($tail)* | ||
) | ||
}; | ||
(@subst $name:ident, [$($acc:tt)*], {} $($tail:tt)*) => { | ||
impl_op!(@subst $name, | ||
[ $($acc)* {} ], | ||
$($tail)* | ||
) | ||
}; | ||
|
||
// Handle token trees | ||
(@subst $name:ident, [$($acc:tt)*], ( $($head:tt)* ) $($tail:tt)*) => { | ||
impl_op!(@subst $name, | ||
[ $($acc)* ( impl_op!(@subst $name, [], $($head)*) ) ], | ||
$($tail)* | ||
) | ||
}; | ||
(@subst $name:ident, [$($acc:tt)*], [ $($head:tt)* ] $($tail:tt)*) => { | ||
impl_op!(@subst $name, | ||
[ $($acc)* [ impl_op!(@subst $name, [], $($head)*) ] ], | ||
$($tail)* | ||
) | ||
}; | ||
(@subst $name:ident, [$($acc:tt)*], { $($head:tt)* } $($tail:tt)*) => { | ||
impl_op!(@subst $name, | ||
[ $($acc)* { impl_op!(@subst $name, [], $($head)*) } ], | ||
$($tail)* | ||
) | ||
}; | ||
|
||
// Handle lone tokens | ||
(@subst $name:ident, [$($acc:tt)*], $head:tt $($tail:tt)*) => { | ||
impl_op!(@subst $name, | ||
[ $($acc)* $head ], | ||
$($tail)* | ||
) | ||
}; | ||
} | ||
|
||
impl_op!( | ||
NumConstants { | ||
MAX, | ||
MIN | ||
} => { | ||
BF16 | (bf16::@name.to_f32()), | ||
F16 | (f16::@name.to_f32()), | ||
F32 | (f32::@name), | ||
F64 | (f64::@name), | ||
I32 | (i32::@name), | ||
I64 | (i64::@name), | ||
UInt | (u32::@name) | ||
} | ||
); | ||
|
||
impl_op!( | ||
FloatConstants { | ||
DIGITS -> {UInt}, | ||
EPSILON, | ||
INFINITY, | ||
MANTISSA_DIGITS -> {UInt}, | ||
MAX_10_EXP -> {I32}, | ||
MAX_EXP -> {I32}, | ||
MIN_10_EXP -> {I32}, | ||
MIN_EXP -> {I32}, | ||
MIN_POSITIVE, | ||
NAN, | ||
NEG_INFINITY, | ||
RADIX -> {UInt} | ||
} => { | ||
BF16 | {{Self} => (bf16::@name.to_f32()); (bf16::@name)}, | ||
F16 | {{Self} => (f16::@name.to_f32()); (f16::@name)}, | ||
F32 | (f32::@name), | ||
F64 | (f64::@name) | ||
} | ||
); | ||
|
||
impl_op!( | ||
IntConstants { | ||
BITS -> {UInt} | ||
} => { | ||
I32 | (i32::@name), | ||
I64 | (i64::@name), | ||
UInt | (u32::@name) | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters