-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add upcast<FromType, ToType> libfunc. (#2118)
- Loading branch information
Showing
14 changed files
with
146 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use cairo_lang_sierra::extensions::casts::CastConcreteLibfunc; | ||
|
||
use super::misc::build_identity; | ||
use super::{CompiledInvocation, CompiledInvocationBuilder, InvocationError}; | ||
|
||
/// Builds instructions for Sierra cast operations. | ||
pub fn build( | ||
libfunc: &CastConcreteLibfunc, | ||
builder: CompiledInvocationBuilder<'_>, | ||
) -> Result<CompiledInvocation, InvocationError> { | ||
match libfunc { | ||
CastConcreteLibfunc::Upcast(_) => build_identity(builder), | ||
} | ||
} |
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,69 @@ | ||
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap; | ||
|
||
use super::uint::Uint16Type; | ||
use crate::define_libfunc_hierarchy; | ||
use crate::extensions::lib_func::{ | ||
LibfuncSignature, OutputVarInfo, SierraApChange, SignatureOnlyGenericLibfunc, | ||
SignatureSpecializationContext, | ||
}; | ||
use crate::extensions::uint::{Uint32Type, Uint64Type, Uint8Type}; | ||
use crate::extensions::uint128::Uint128Type; | ||
use crate::extensions::{ | ||
args_as_two_types, NamedType, OutputVarReferenceInfo, SpecializationError, | ||
}; | ||
use crate::ids::ConcreteTypeId; | ||
use crate::program::GenericArg; | ||
|
||
define_libfunc_hierarchy! { | ||
pub enum CastLibfunc { | ||
Upcast(UpcastLibfunc), | ||
}, CastConcreteLibfunc | ||
} | ||
|
||
/// Libfunc for casting from one type to another where any input value can fit into the destination | ||
/// type. For example, from u8 to u64. | ||
#[derive(Default)] | ||
pub struct UpcastLibfunc {} | ||
impl SignatureOnlyGenericLibfunc for UpcastLibfunc { | ||
const STR_ID: &'static str = "upcast"; | ||
|
||
fn specialize_signature( | ||
&self, | ||
context: &dyn SignatureSpecializationContext, | ||
args: &[GenericArg], | ||
) -> Result<LibfuncSignature, SpecializationError> { | ||
let (from_ty, to_ty) = args_as_two_types(args)?; | ||
|
||
let type_to_n_bits: UnorderedHashMap<ConcreteTypeId, usize> = vec![ | ||
(Uint8Type::ID, 8), | ||
(Uint16Type::ID, 16), | ||
(Uint32Type::ID, 32), | ||
(Uint64Type::ID, 64), | ||
(Uint128Type::ID, 128), | ||
] | ||
.into_iter() | ||
.filter_map(|(generic_type, n_bits)| { | ||
Some((context.get_concrete_type(generic_type, &[]).ok()?, n_bits)) | ||
}) | ||
.collect(); | ||
|
||
let from_nbits = | ||
type_to_n_bits.get(&from_ty).ok_or(SpecializationError::UnsupportedGenericArg)?; | ||
let to_nbits = | ||
type_to_n_bits.get(&to_ty).ok_or(SpecializationError::UnsupportedGenericArg)?; | ||
|
||
let is_valid = from_nbits <= to_nbits; | ||
if !is_valid { | ||
return Err(SpecializationError::UnsupportedGenericArg); | ||
} | ||
|
||
Ok(LibfuncSignature::new_non_branch( | ||
vec![from_ty.clone()], | ||
vec![OutputVarInfo { | ||
ty: to_ty, | ||
ref_info: OutputVarReferenceInfo::SameAsParam { param_idx: 0 }, | ||
}], | ||
SierraApChange::Known { new_vars_only: true }, | ||
)) | ||
} | ||
} |
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,29 @@ | ||
//! > u16 to u64 upcast | ||
|
||
//! > test_function_name | ||
run_small_e2e_test | ||
|
||
//! > cairo | ||
fn foo(a: u16) -> u64 { | ||
upcast(a) | ||
} | ||
|
||
//! > casm | ||
[ap + 0] = [fp + -3], ap++; | ||
ret; | ||
|
||
//! > function_costs | ||
test::foo: OrderedHashMap({Const: 100}) | ||
|
||
//! > sierra_code | ||
type u16 = u16; | ||
type u64 = u64; | ||
|
||
libfunc upcast<u16, u64> = upcast<u16, u64>; | ||
libfunc store_temp<u64> = store_temp<u64>; | ||
|
||
upcast<u16, u64>([0]) -> ([1]); | ||
store_temp<u64>([1]) -> ([2]); | ||
return([2]); | ||
|
||
test::foo@0([0]: u16) -> (u64); |