Skip to content

Commit e8f4741

Browse files
authored
Merge 4c8c93d into 3239a4a
2 parents 3239a4a + 4c8c93d commit e8f4741

File tree

12 files changed

+495
-91
lines changed

12 files changed

+495
-91
lines changed

compiler/noirc_evaluator/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,20 @@ pub(crate) fn trim_leading_whitespace_from_lines(src: &str) -> String {
2020
while first_line.is_empty() {
2121
first_line = lines.next().unwrap();
2222
}
23+
let first_line_original_length = first_line.len();
2324
let mut result = first_line.trim_start().to_string();
25+
let first_line_trimmed_length = result.len();
26+
27+
// Try to see how many spaces we chopped off the first line
28+
let difference = first_line_original_length - first_line_trimmed_length;
2429
for line in lines {
2530
result.push('\n');
26-
result.push_str(line.trim_start());
31+
// Try to remove just `difference` spaces to preserve indents
32+
if line.len() - line.trim_start().len() >= difference {
33+
result.push_str(&line.chars().skip(difference).collect::<String>());
34+
} else {
35+
result.push_str(line.trim_start());
36+
}
2737
}
2838
result
2939
}

compiler/noirc_evaluator/src/ssa.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ fn optimize_all(builder: SsaBuilder, options: &SsaEvaluatorOptions) -> Result<Ss
211211
.run_pass(Ssa::remove_if_else, "Remove IfElse")
212212
.run_pass(Ssa::purity_analysis, "Purity Analysis (2nd)")
213213
.run_pass(Ssa::fold_constants, "Constant Folding")
214+
.run_pass(
215+
Ssa::inline_constants_into_brillig_functions,
216+
"Inline constants into brillig functions",
217+
)
218+
.run_pass(Ssa::remove_unreachable_functions, "Removing Unreachable Functions (3rd)")
214219
.run_pass(Ssa::flatten_basic_conditionals, "Simplify conditionals for unconstrained")
215220
.run_pass(Ssa::remove_enable_side_effects, "EnableSideEffectsIf removal")
216221
.run_pass(Ssa::fold_constants_using_constraints, "Constraint Folding")
@@ -223,7 +228,7 @@ fn optimize_all(builder: SsaBuilder, options: &SsaEvaluatorOptions) -> Result<Ss
223228
// The used globals map is determined during DIE, so we should duplicate entry points before a DIE pass run.
224229
.run_pass(Ssa::brillig_entry_point_analysis, "Brillig Entry Point Analysis")
225230
// Remove any potentially unnecessary duplication from the Brillig entry point analysis.
226-
.run_pass(Ssa::remove_unreachable_functions, "Removing Unreachable Functions (3rd)")
231+
.run_pass(Ssa::remove_unreachable_functions, "Removing Unreachable Functions (4th)")
227232
// This pass makes transformations specific to Brillig generation.
228233
// It must be the last pass to either alter or add new instructions before Brillig generation,
229234
// as other semantics in the compiler can potentially break (e.g. inserting instructions).

compiler/noirc_evaluator/src/ssa/ir/instruction/call/blackbox.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ mod test {
317317
fn full_constant_folding() {
318318
let src = r#"
319319
acir(inline) fn main f0 {
320-
b0():
320+
b0():
321321
v0 = make_array [Field 2, Field 3, Field 5, Field 5] : [Field; 4]
322322
v1 = make_array [Field 1, Field 17631683881184975370165255887551781615748388533673675138860, Field 0, Field 1, Field 17631683881184975370165255887551781615748388533673675138860, Field 0] : [Field; 6]
323323
v2 = call multi_scalar_mul (v1, v0) -> [Field; 3]
@@ -327,7 +327,7 @@ mod test {
327327

328328
let expected_src = r#"
329329
acir(inline) fn main f0 {
330-
b0():
330+
b0():
331331
v3 = make_array [Field 2, Field 3, Field 5, Field 5] : [Field; 4]
332332
v7 = make_array [Field 1, Field 17631683881184975370165255887551781615748388533673675138860, Field 0, Field 1, Field 17631683881184975370165255887551781615748388533673675138860, Field 0] : [Field; 6]
333333
v10 = make_array [Field 1478523918288173385110236399861791147958001875200066088686689589556927843200, Field 700144278551281040379388961242974992655630750193306467120985766322057145630, Field 0] : [Field; 3]
@@ -342,7 +342,7 @@ mod test {
342342
fn simplify_zero() {
343343
let src = r#"
344344
acir(inline) fn main f0 {
345-
b0(v0: Field, v1: Field):
345+
b0(v0: Field, v1: Field):
346346
v2 = make_array [v0, Field 0, Field 0, Field 0, v0, Field 0] : [Field; 6]
347347
v3 = make_array [
348348
Field 0, Field 0, Field 1, v0, v1, Field 0, Field 1, v0, Field 0] : [Field; 9]
@@ -355,7 +355,7 @@ mod test {
355355
//First point is zero, second scalar is zero, so we should be left with the scalar mul of the last point.
356356
let expected_src = r#"
357357
acir(inline) fn main f0 {
358-
b0(v0: Field, v1: Field):
358+
b0(v0: Field, v1: Field):
359359
v3 = make_array [v0, Field 0, Field 0, Field 0, v0, Field 0] : [Field; 6]
360360
v5 = make_array [Field 0, Field 0, Field 1, v0, v1, Field 0, Field 1, v0, Field 0] : [Field; 9]
361361
v6 = make_array [v0, Field 0] : [Field; 2]
@@ -372,7 +372,7 @@ mod test {
372372
fn partial_constant_folding() {
373373
let src = r#"
374374
acir(inline) fn main f0 {
375-
b0(v0: Field, v1: Field):
375+
b0(v0: Field, v1: Field):
376376
v2 = make_array [Field 1, Field 0, v0, Field 0, Field 2, Field 0] : [Field; 6]
377377
v3 = make_array [
378378
Field 1, Field 17631683881184975370165255887551781615748388533673675138860, Field 0, v0, v1, Field 0, Field 1, Field 17631683881184975370165255887551781615748388533673675138860, Field 0] : [Field; 9]
@@ -383,7 +383,7 @@ mod test {
383383
//First and last scalar/point are constant, so we should be left with the msm of the middle point and the folded constant point
384384
let expected_src = r#"
385385
acir(inline) fn main f0 {
386-
b0(v0: Field, v1: Field):
386+
b0(v0: Field, v1: Field):
387387
v5 = make_array [Field 1, Field 0, v0, Field 0, Field 2, Field 0] : [Field; 6]
388388
v7 = make_array [Field 1, Field 17631683881184975370165255887551781615748388533673675138860, Field 0, v0, v1, Field 0, Field 1, Field 17631683881184975370165255887551781615748388533673675138860, Field 0] : [Field; 9]
389389
v8 = make_array [v0, Field 0, Field 1, Field 0] : [Field; 4]

compiler/noirc_evaluator/src/ssa/ir/instruction/constrain.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ mod tests {
210210
// Regression test for https://github.com/noir-lang/noir/issues/7451
211211
let src = "
212212
acir(inline) predicate_pure fn main f0 {
213-
b0(v0: u8):
213+
b0(v0: u8):
214214
v1 = and u8 255, v0
215215
return v1
216216
}
@@ -220,7 +220,7 @@ mod tests {
220220

221221
let expected = "
222222
acir(inline) fn main f0 {
223-
b0(v0: u8):
223+
b0(v0: u8):
224224
return v0
225225
}
226226
";

compiler/noirc_evaluator/src/ssa/opt/basic_conditional.rs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -394,30 +394,30 @@ mod test {
394394
let src = "
395395
brillig(inline) fn foo f0 {
396396
b0(v0: u32):
397-
v3 = eq v0, u32 0
398-
jmpif v3 then: b2, else: b1
397+
v3 = eq v0, u32 0
398+
jmpif v3 then: b2, else: b1
399399
b1():
400-
jmp b3(u32 5)
400+
jmp b3(u32 5)
401401
b2():
402-
jmp b3(u32 3)
402+
jmp b3(u32 3)
403403
b3(v1: u32):
404-
return v1
404+
return v1
405405
}
406406
";
407407
let ssa = Ssa::from_str(src).unwrap();
408408
assert_eq!(ssa.main().reachable_blocks().len(), 4);
409409

410410
let expected = "
411411
brillig(inline) fn foo f0 {
412-
b0(v0: u32):
413-
v2 = eq v0, u32 0
414-
v3 = not v2
415-
v4 = cast v2 as u32
416-
v5 = cast v3 as u32
417-
v7 = unchecked_mul v4, u32 3
418-
v9 = unchecked_mul v5, u32 5
419-
v10 = unchecked_add v7, v9
420-
return v10
412+
b0(v0: u32):
413+
v2 = eq v0, u32 0
414+
v3 = not v2
415+
v4 = cast v2 as u32
416+
v5 = cast v3 as u32
417+
v7 = unchecked_mul v4, u32 3
418+
v9 = unchecked_mul v5, u32 5
419+
v10 = unchecked_add v7, v9
420+
return v10
421421
}
422422
";
423423

@@ -430,16 +430,16 @@ mod test {
430430
let src = r#"
431431
brillig(inline) fn foo f0 {
432432
b0(v0: u32):
433-
v3 = eq v0, u32 5
434-
jmpif v3 then: b2, else: b1
433+
v3 = eq v0, u32 5
434+
jmpif v3 then: b2, else: b1
435435
b1():
436-
v6 = make_array b"foo"
437-
jmp b3(v6)
436+
v6 = make_array b"foo"
437+
jmp b3(v6)
438438
b2():
439-
v10 = make_array b"bar"
440-
jmp b3(v10)
439+
v10 = make_array b"bar"
440+
jmp b3(v10)
441441
b3(v1: [u8; 3]):
442-
return v1
442+
return v1
443443
}
444444
"#;
445445
let ssa = Ssa::from_str(src).unwrap();
@@ -453,33 +453,33 @@ mod test {
453453
fn nested_jmpifs() {
454454
let src = "
455455
brillig(inline) fn foo f0 {
456-
b0(v0: u32):
456+
b0(v0: u32):
457457
v5 = eq v0, u32 5
458458
v6 = not v5
459459
jmpif v5 then: b5, else: b1
460-
b1():
460+
b1():
461461
v8 = lt v0, u32 3
462462
jmpif v8 then: b3, else: b2
463-
b2():
463+
b2():
464464
v9 = truncate v0 to 2 bits, max_bit_size: 32
465465
jmp b4(v9)
466-
b3():
466+
b3():
467467
v10 = truncate v0 to 1 bits, max_bit_size: 32
468468
jmp b4(v10)
469-
b4(v1: u32):
469+
b4(v1: u32):
470470
jmp b9(v1)
471-
b5():
471+
b5():
472472
v12 = lt u32 2, v0
473473
jmpif v12 then: b7, else: b6
474-
b6():
474+
b6():
475475
v13 = truncate v0 to 3 bits, max_bit_size: 32
476476
jmp b8(v13)
477-
b7():
477+
b7():
478478
v14 = and v0, u32 2
479479
jmp b8(v14)
480-
b8(v2: u32):
480+
b8(v2: u32):
481481
jmp b9(v2)
482-
b9(v3: u32):
482+
b9(v3: u32):
483483
return v3
484484
}
485485
";
@@ -488,11 +488,11 @@ mod test {
488488

489489
let expected = "
490490
brillig(inline) fn foo f0 {
491-
b0(v0: u32):
491+
b0(v0: u32):
492492
v3 = eq v0, u32 5
493493
v4 = not v3
494494
jmpif v3 then: b2, else: b1
495-
b1():
495+
b1():
496496
v6 = lt v0, u32 3
497497
v7 = truncate v0 to 1 bits, max_bit_size: 32
498498
v8 = not v6
@@ -503,7 +503,7 @@ mod test {
503503
v13 = unchecked_mul v11, v9
504504
v14 = unchecked_add v12, v13
505505
jmp b3(v14)
506-
b2():
506+
b2():
507507
v16 = lt u32 2, v0
508508
v17 = and v0, u32 2
509509
v18 = not v16
@@ -514,7 +514,7 @@ mod test {
514514
v23 = unchecked_mul v21, v19
515515
v24 = unchecked_add v22, v23
516516
jmp b3(v24)
517-
b3(v1: u32):
517+
b3(v1: u32):
518518
return v1
519519
}
520520
";

compiler/noirc_evaluator/src/ssa/opt/brillig_entry_points.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! generated for different entry points can conflict.
1414
//!
1515
//! To provide a more concrete example, let's take this program:
16-
//! ```
16+
//! ```noir
1717
//! global ONE: Field = 1;
1818
//! global TWO: Field = 2;
1919
//! global THREE: Field = 3;
@@ -40,7 +40,7 @@
4040
//! }
4141
//! ```
4242
//! The two entry points will have different global allocation maps:
43-
//! ```
43+
//! ```noir
4444
//! GlobalInit(Id(1)):
4545
//! CONST M32835 = 1
4646
//! CONST M32836 = 2

0 commit comments

Comments
 (0)