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

feat(comptime): Implement to_be_bits and to_le_bits in the interpreter #7008

Merged
merged 5 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/acir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2169,7 +2169,7 @@ impl<'a> Context<'a> {

let Type::Array(result_type, array_length) = dfg.type_of_value(result_ids[0])
else {
unreachable!("ICE: ToRadix result must be an array");
unreachable!("ICE: ToBits result must be an array");
};

self.acir_context
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ impl<'context> Elaborator<'context> {
) -> Option<HirMethodReference> {
let func_id = match self.current_item {
Some(DependencyId::Function(id)) => id,
_ => panic!("unexpected method outside a function"),
_ => panic!("unexpected method outside a function: {method_name}"),
};
let func_meta = self.interner.function_meta(&func_id);

Expand Down
22 changes: 22 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ impl<'local, 'context> Interpreter<'local, 'context> {
"struct_def_set_fields" => struct_def_set_fields(interner, arguments, location),
"to_be_radix" => to_be_radix(arguments, return_type, location),
"to_le_radix" => to_le_radix(arguments, return_type, location),
"to_be_bits" => to_be_bits(arguments, return_type, location),
"to_le_bits" => to_le_bits(arguments, return_type, location),
"trait_constraint_eq" => trait_constraint_eq(arguments, location),
"trait_constraint_hash" => trait_constraint_hash(arguments, location),
"trait_def_as_trait_constraint" => {
Expand Down Expand Up @@ -776,6 +778,26 @@ fn quoted_tokens(arguments: Vec<(Value, Location)>, location: Location) -> IResu
))
}

fn to_be_bits(
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
) -> IResult<Value> {
let value = check_one_argument(arguments, location)?;
let radix = (Value::U32(2), value.1);
to_be_radix(vec![value, radix], return_type, location)
}

fn to_le_bits(
arguments: Vec<(Value, Location)>,
return_type: Type,
location: Location,
) -> IResult<Value> {
let value = check_one_argument(arguments, location)?;
let radix = (Value::U32(2), value.1);
to_le_radix(vec![value, radix], return_type, location)
}

fn to_be_radix(
arguments: Vec<(Value, Location)>,
return_type: Type,
Expand Down
2 changes: 2 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use crate::node_interner::FuncId;
use crate::parse_program;

/// Create an interpreter for a code snippet and pass it to a test function.
///
/// The `stdlib` is not made available as a dependency.
jfecher marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) fn with_interpreter<T>(
src: &str,
f: impl FnOnce(&mut Interpreter, FuncId, &[(CompilationError, FileId)]) -> T,
Expand Down
13 changes: 8 additions & 5 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
)
}

/// Compile a program.
///
/// The `stdlib` is not available for these snippets.
jfecher marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) fn get_program_with_maybe_parser_errors(
src: &str,
allow_parser_errors: bool,
Expand Down Expand Up @@ -3185,7 +3188,7 @@
}

#[test]
fn dont_infer_globals_to_u32_from_type_use() {

Check warning on line 3191 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (dont)
let src = r#"
global ARRAY_LEN = 3;
global STR_LEN: _ = 2;
Expand Down Expand Up @@ -3215,7 +3218,7 @@
}

#[test]
fn dont_infer_partial_global_types() {

Check warning on line 3221 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (dont)
let src = r#"
pub global ARRAY: [Field; _] = [0; 3];
pub global NESTED_ARRAY: [[Field; _]; 3] = [[]; 3];
Expand Down Expand Up @@ -3453,7 +3456,7 @@

#[test]
fn unconditional_recursion_fail() {
let srcs = vec![

Check warning on line 3459 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (srcs)
r#"
fn main() {
main()
Expand Down Expand Up @@ -3515,7 +3518,7 @@
"#,
];

for src in srcs {

Check warning on line 3521 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (srcs)
let errors = get_program_errors(src);
assert!(
!errors.is_empty(),
Expand All @@ -3534,7 +3537,7 @@

#[test]
fn unconditional_recursion_pass() {
let srcs = vec![

Check warning on line 3540 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (srcs)
r#"
fn main() {
if false { main(); }
Expand Down Expand Up @@ -3576,7 +3579,7 @@
"#,
];

for src in srcs {

Check warning on line 3582 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (srcs)
assert_no_errors(src);
}
}
Expand Down Expand Up @@ -3894,10 +3897,10 @@
#[test]
fn warns_on_unneeded_unsafe() {
let src = r#"
fn main() {
fn main() {
/// Safety: test
unsafe {
foo()
foo()
}
}

Expand All @@ -3914,12 +3917,12 @@
#[test]
fn warns_on_nested_unsafe() {
let src = r#"
fn main() {
fn main() {
/// Safety: test
unsafe {
unsafe {
/// Safety: test
unsafe {
foo()
foo()
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions test_programs/noir_test_success/global_eval/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "global_eval"
type = "bin"
authors = [""]
compiler_version = ">=0.27.0"

[dependencies]
10 changes: 10 additions & 0 deletions test_programs/noir_test_success/global_eval/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::uint128::U128;

// These definitions require `to_be_bits` and `to_le_bits` to be supported at comptime.
global POW64_A: Field = 2.pow_32(64);
global POW64_B: Field = (U128::one() << 64).to_integer();

#[test]
fn test_pow64_equal() {
assert_eq(POW64_A, POW64_B);
}
aakoshh marked this conversation as resolved.
Show resolved Hide resolved
Loading