-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: unknown slice lengths coming from as_slice (noir-lang/noir#4725)
chore: remove unused env vars from `Cross.toml` (noir-lang/noir#4717) feat: improve nargo check cli with --override flag and feedback for existing files (noir-lang/noir#4575) feat: Allow slices to brillig entry points (noir-lang/noir#4713) chore: simplify how `acvm_backend.wasm` is embedded (noir-lang/noir#4703) fix(acvm): Mark outputs of Opcode::Call solvable (noir-lang/noir#4708) fix: Field comparisons (noir-lang/noir#4704) feat(acvm_js): Execute program (noir-lang/noir#4694) chore: simplify how blns is loaded into tests (noir-lang/noir#4705) fix(ssa): Do not use get_value_max_num_bits when we want pure type information (noir-lang/noir#4700) chore: remove conditional compilation around `acvm_js` package (noir-lang/noir#4702) feat(docs): Documenting noir codegen (noir-lang/noir#4454) chore: check for references to private functions during path resolution (noir-lang/noir#4622) chore: fix clippy errors (noir-lang/noir#4684) fix: Last use analysis & make it an SSA pass (noir-lang/noir#4686) feat: improve SSA type-awareness in EQ and MUL instructions (noir-lang/noir#4691) feat: improve optimisations on range constraints (noir-lang/noir#4690) chore: remove last traces of nix (noir-lang/noir#4679) chore: Use is_entry_point helper on RuntimeType (noir-lang/noir#4678)
- Loading branch information
Showing
15 changed files
with
289 additions
and
47 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
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
71 changes: 71 additions & 0 deletions
71
noir/noir-repo/compiler/noirc_evaluator/src/ssa/opt/as_slice_length.rs
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,71 @@ | ||
use crate::ssa::{ | ||
ir::{ | ||
function::Function, | ||
instruction::{Instruction, InstructionId, Intrinsic}, | ||
types::Type, | ||
value::Value, | ||
}, | ||
ssa_gen::Ssa, | ||
}; | ||
use fxhash::FxHashMap as HashMap; | ||
|
||
impl Ssa { | ||
/// A simple SSA pass to find any calls to `Intrinsic::AsSlice` and replacing any references to the length of the | ||
/// resulting slice with the length of the array from which it was generated. | ||
/// | ||
/// This allows the length of a slice generated from an array to be used in locations where a constant value is | ||
/// necessary when the value of the array is unknown. | ||
/// | ||
/// Note that this pass must be placed before loop unrolling to be useful. | ||
#[tracing::instrument(level = "trace", skip(self))] | ||
pub(crate) fn as_slice_optimization(mut self) -> Self { | ||
for func in self.functions.values_mut() { | ||
let known_slice_lengths = known_slice_lengths(func); | ||
replace_known_slice_lengths(func, known_slice_lengths); | ||
} | ||
self | ||
} | ||
} | ||
|
||
fn known_slice_lengths(func: &Function) -> HashMap<InstructionId, usize> { | ||
let mut known_slice_lengths = HashMap::default(); | ||
for block_id in func.reachable_blocks() { | ||
let block = &func.dfg[block_id]; | ||
for instruction_id in block.instructions() { | ||
let (target_func, arguments) = match &func.dfg[*instruction_id] { | ||
Instruction::Call { func, arguments } => (func, arguments), | ||
_ => continue, | ||
}; | ||
|
||
match &func.dfg[*target_func] { | ||
Value::Intrinsic(Intrinsic::AsSlice) => { | ||
let array_typ = func.dfg.type_of_value(arguments[0]); | ||
if let Type::Array(_, length) = array_typ { | ||
known_slice_lengths.insert(*instruction_id, length); | ||
} else { | ||
unreachable!("AsSlice called with non-array {}", array_typ); | ||
} | ||
} | ||
_ => continue, | ||
}; | ||
} | ||
} | ||
known_slice_lengths | ||
} | ||
|
||
fn replace_known_slice_lengths( | ||
func: &mut Function, | ||
known_slice_lengths: HashMap<InstructionId, usize>, | ||
) { | ||
known_slice_lengths.into_iter().for_each(|(instruction_id, known_length)| { | ||
let call_returns = func.dfg.instruction_results(instruction_id); | ||
let original_slice_length = call_returns[0]; | ||
|
||
// We won't use the new id for the original unknown length. | ||
// This isn't strictly necessary as a new result will be defined the next time for which the instruction | ||
// is reinserted but this avoids leaving the program in an invalid state. | ||
func.dfg.replace_result(instruction_id, original_slice_length); | ||
let known_length = func.dfg.make_constant(known_length.into(), Type::length_type()); | ||
func.dfg.set_value_from_id(original_slice_length, known_length); | ||
}); | ||
} |
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
7 changes: 7 additions & 0 deletions
7
noir/noir-repo/test_programs/execution_success/array_to_slice_constant_length/Nargo.toml
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,7 @@ | ||
[package] | ||
name = "array_to_slice_constant_length" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.26.0" | ||
|
||
[dependencies] |
1 change: 1 addition & 0 deletions
1
noir/noir-repo/test_programs/execution_success/array_to_slice_constant_length/Prover.toml
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 @@ | ||
val = "42" |
Oops, something went wrong.