Skip to content

Commit 884a367

Browse files
committed
Merge branch 'master' into tf/full-backprop
* master: chore: Stop cloning `FuncMeta` (#3968) feat: add MVP `nargo export` command (#3870) chore: clippy and general cleanup of debugger (#3957) chore: updating the noirjs tutorial to match stable + some other improvements (#3929) chore: add `insert_range_check` method to `FunctionBuilder` (#3959) chore: remove usage of term "preprocessed" in favour of "artifact" (#3939)
2 parents e0527de + 45f6400 commit 884a367

File tree

38 files changed

+486
-297
lines changed

38 files changed

+486
-297
lines changed

.github/workflows/test-js-packages.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ jobs:
319319
run: yarn workspace @noir-lang/noir_wasm test:browser
320320

321321
test-noir-codegen:
322-
needs: [build-acvm-js, build-noirc-abi]
322+
needs: [build-acvm-js, build-noirc-abi, build-nargo]
323323
name: noir_codegen
324324
runs-on: ubuntu-latest
325325
timeout-minutes: 30
@@ -328,6 +328,12 @@ jobs:
328328
- name: Checkout
329329
uses: actions/checkout@v4
330330

331+
- name: Download nargo binary
332+
uses: actions/download-artifact@v3
333+
with:
334+
name: nargo
335+
path: ./nargo
336+
331337
- name: Download acvm_js package artifact
332338
uses: actions/download-artifact@v3
333339
with:
@@ -339,6 +345,14 @@ jobs:
339345
with:
340346
name: noirc_abi_wasm
341347
path: ./tooling/noirc_abi_wasm
348+
349+
- name: Set nargo on PATH
350+
run: |
351+
nargo_binary="${{ github.workspace }}/nargo/nargo"
352+
chmod +x $nargo_binary
353+
echo "$(dirname $nargo_binary)" >> $GITHUB_PATH
354+
export PATH="$PATH:$(dirname $nargo_binary)"
355+
nargo -V
342356
343357
- name: Install Yarn dependencies
344358
uses: ./.github/actions/setup

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/noirc_driver/src/abi_gen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub(super) fn compute_function_abi(
3333
) -> (Vec<AbiParameter>, Option<AbiType>) {
3434
let func_meta = context.def_interner.function_meta(func_id);
3535

36-
let (parameters, return_type) = func_meta.into_function_signature();
36+
let (parameters, return_type) = func_meta.function_signature();
3737
let parameters = into_abi_params(context, parameters);
3838
let return_type = return_type.map(|typ| AbiType::from_type(context, &typ));
3939
(parameters, return_type)

compiler/noirc_evaluator/src/ssa/function_builder/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,19 @@ impl FunctionBuilder {
255255
self.insert_instruction(Instruction::Constrain(lhs, rhs, assert_message), None);
256256
}
257257

258+
/// Insert a [`Instruction::RangeCheck`] instruction at the end of the current block.
259+
pub(crate) fn insert_range_check(
260+
&mut self,
261+
value: ValueId,
262+
max_bit_size: u32,
263+
assert_message: Option<String>,
264+
) {
265+
self.insert_instruction(
266+
Instruction::RangeCheck { value, max_bit_size, assert_message },
267+
None,
268+
);
269+
}
270+
258271
/// Insert a call instruction at the end of the current block and return
259272
/// the results of the call.
260273
pub(crate) fn insert_call(

compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,11 @@ impl<'a> FunctionContext<'a> {
349349
self.check_shift_overflow(result, rhs, bit_size, location, false)
350350
} else {
351351
let message = format!("attempt to {} with overflow", op_name);
352-
let range_constraint = Instruction::RangeCheck {
353-
value: result,
354-
max_bit_size: bit_size,
355-
assert_message: Some(message),
356-
};
357-
self.builder.set_location(location).insert_instruction(range_constraint, None);
352+
self.builder.set_location(location).insert_range_check(
353+
result,
354+
bit_size,
355+
Some(message),
356+
);
358357
result
359358
}
360359
}
@@ -464,12 +463,11 @@ impl<'a> FunctionContext<'a> {
464463
let product_field = self.builder.insert_binary(lhs_abs, BinaryOp::Mul, rhs_abs);
465464
// It must not already overflow the bit_size
466465
let message = "attempt to multiply with overflow".to_string();
467-
let size_overflow = Instruction::RangeCheck {
468-
value: product_field,
469-
max_bit_size: bit_size,
470-
assert_message: Some(message.clone()),
471-
};
472-
self.builder.set_location(location).insert_instruction(size_overflow, None);
466+
self.builder.set_location(location).insert_range_check(
467+
product_field,
468+
bit_size,
469+
Some(message.clone()),
470+
);
473471
let product = self.builder.insert_cast(product_field, Type::unsigned(bit_size));
474472

475473
// Then we check the signed product fits in a signed integer of bit_size-bits

compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ pub(crate) fn check_methods_signatures(
542542

543543
// TODO: This is not right since it may bind generic return types
544544
trait_method.return_type().unify(&resolved_return_type, &mut typecheck_errors, || {
545+
let impl_method = resolver.interner.function_meta(func_id);
545546
let ret_type_span = impl_method.return_type.get_type().span;
546547
let expr_span = ret_type_span.expect("return type must always have a span");
547548

compiler/noirc_frontend/src/hir/def_map/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,29 @@ impl CrateDefMap {
173173
})
174174
})
175175
}
176+
177+
/// Go through all modules in this crate, and find all functions in
178+
/// each module with the #[export] attribute
179+
pub fn get_all_exported_functions<'a>(
180+
&'a self,
181+
interner: &'a NodeInterner,
182+
) -> impl Iterator<Item = FuncId> + 'a {
183+
self.modules.iter().flat_map(|(_, module)| {
184+
module.value_definitions().filter_map(|id| {
185+
if let Some(func_id) = id.as_function() {
186+
let attributes = interner.function_attributes(&func_id);
187+
if attributes.secondary.contains(&SecondaryAttribute::Export) {
188+
Some(func_id)
189+
} else {
190+
None
191+
}
192+
} else {
193+
None
194+
}
195+
})
196+
})
197+
}
198+
176199
/// Go through all modules in this crate, find all `contract ... { ... }` declarations,
177200
/// and collect them all into a Vec.
178201
pub fn get_all_contracts(&self, interner: &NodeInterner) -> Vec<Contract> {

compiler/noirc_frontend/src/hir/mod.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl Context<'_> {
152152
None
153153
}
154154

155-
pub fn function_meta(&self, func_id: &FuncId) -> FuncMeta {
155+
pub fn function_meta(&self, func_id: &FuncId) -> &FuncMeta {
156156
self.def_interner.function_meta(func_id)
157157
}
158158

@@ -194,6 +194,19 @@ impl Context<'_> {
194194
.collect()
195195
}
196196

197+
pub fn get_all_exported_functions_in_crate(&self, crate_id: &CrateId) -> Vec<(String, FuncId)> {
198+
let interner = &self.def_interner;
199+
let def_map = self.def_map(crate_id).expect("The local crate should be analyzed already");
200+
201+
def_map
202+
.get_all_exported_functions(interner)
203+
.map(|function_id| {
204+
let function_name = self.function_name(&function_id).to_owned();
205+
(function_name, function_id)
206+
})
207+
.collect()
208+
}
209+
197210
/// Return a Vec of all `contract` declarations in the source code and the functions they contain
198211
pub fn get_all_contracts(&self, crate_id: &CrateId) -> Vec<Contract> {
199212
self.def_map(crate_id)

compiler/noirc_frontend/src/hir/type_check/expr.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,11 @@ impl<'interner> TypeChecker<'interner> {
190190
// Automatically add `&mut` if the method expects a mutable reference and
191191
// the object is not already one.
192192
if *func_id != FuncId::dummy_id() {
193-
let func_meta = self.interner.function_meta(func_id);
193+
let function_type =
194+
self.interner.function_meta(func_id).typ.clone();
194195
self.try_add_mutable_reference_to_object(
195196
&mut method_call,
196-
&func_meta.typ,
197+
&function_type,
197198
&mut args,
198199
);
199200
}
@@ -561,7 +562,7 @@ impl<'interner> TypeChecker<'interner> {
561562

562563
let func_meta = self.interner.function_meta(&func_id);
563564
let param_len = func_meta.parameters.len();
564-
(func_meta.typ, param_len)
565+
(func_meta.typ.clone(), param_len)
565566
}
566567
HirMethodReference::TraitMethodId(method) => {
567568
let the_trait = self.interner.get_trait(method.trait_id);
@@ -916,7 +917,7 @@ impl<'interner> TypeChecker<'interner> {
916917
&self.current_function.expect("unexpected method outside a function"),
917918
);
918919

919-
for constraint in func_meta.trait_constraints {
920+
for constraint in &func_meta.trait_constraints {
920921
if *object_type == constraint.typ {
921922
if let Some(the_trait) = self.interner.try_get_trait(constraint.trait_id) {
922923
for (method_index, method) in the_trait.methods.iter().enumerate() {

compiler/noirc_frontend/src/hir/type_check/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,23 @@ pub fn type_check_func(interner: &mut NodeInterner, func_id: FuncId) -> Vec<Type
5050
type_checker.current_function = Some(func_id);
5151

5252
let meta = type_checker.interner.function_meta(&func_id);
53+
let parameters = meta.parameters.clone();
54+
let expected_return_type = meta.return_type.clone();
55+
let expected_trait_constraints = meta.trait_constraints.clone();
56+
let name_span = meta.name.location.span;
57+
5358
let mut errors = Vec::new();
5459

5560
// Temporarily add any impls in this function's `where` clause to scope
56-
for constraint in &meta.trait_constraints {
61+
for constraint in &expected_trait_constraints {
5762
let object = constraint.typ.clone();
5863
let trait_id = constraint.trait_id;
5964

6065
if !type_checker.interner.add_assumed_trait_implementation(object, trait_id) {
6166
if let Some(the_trait) = type_checker.interner.try_get_trait(trait_id) {
6267
let trait_name = the_trait.name.to_string();
6368
let typ = constraint.typ.clone();
64-
let span = meta.name.location.span;
69+
let span = name_span;
6570
errors.push(TypeCheckError::UnneededTraitConstraint { trait_name, typ, span });
6671
}
6772
}
@@ -70,7 +75,7 @@ pub fn type_check_func(interner: &mut NodeInterner, func_id: FuncId) -> Vec<Type
7075
// Bind each parameter to its annotated type.
7176
// This is locally obvious, but it must be bound here so that the
7277
// Definition object of the parameter in the NodeInterner is given the correct type.
73-
for param in meta.parameters.into_iter() {
78+
for param in parameters {
7479
type_checker.bind_pattern(&param.0, param.1);
7580
}
7681

@@ -93,7 +98,7 @@ pub fn type_check_func(interner: &mut NodeInterner, func_id: FuncId) -> Vec<Type
9398
errors.append(&mut type_checker.errors);
9499

95100
// Now remove all the `where` clause constraints we added
96-
for constraint in &meta.trait_constraints {
101+
for constraint in &expected_trait_constraints {
97102
interner.remove_assumed_trait_implementations_for_trait(constraint.trait_id);
98103
}
99104

@@ -107,7 +112,7 @@ pub fn type_check_func(interner: &mut NodeInterner, func_id: FuncId) -> Vec<Type
107112
expected: declared_return_type.clone(),
108113
actual: function_last_type,
109114
span: func_span,
110-
source: Source::Return(meta.return_type, expr_span),
115+
source: Source::Return(expected_return_type, expr_span),
111116
};
112117
errors.push(error);
113118
}
@@ -122,7 +127,7 @@ pub fn type_check_func(interner: &mut NodeInterner, func_id: FuncId) -> Vec<Type
122127
expected: declared_return_type.clone(),
123128
actual: function_last_type.clone(),
124129
span: func_span,
125-
source: Source::Return(meta.return_type, expr_span),
130+
source: Source::Return(expected_return_type, expr_span),
126131
};
127132

128133
if empty_function {

0 commit comments

Comments
 (0)