noir-lang/noir

Flattening and remove ifelse adds instructions with empty call stacks

Closed this issue · 0 comments

Aim

Profiling where all the constraints in the program come from

Expected Behavior

Every ACIR opcode should have a call stack associated with it

Bug

Some ACIR opcodes in the program have no call stack associated. I've added a condition in the SSA printer to figure out wich instructions in SSA do not have any call stack associated.

I've tracked down these instructions to some optimization steps:

  • flatten_cfg.rs: Explicitely uses CallStack::new() for instructions related with the if condition. A solution to this might be to add call_stack to JmpIf in the same way Jmp and Return have call stacks, to associated the emitted instructions with.
  • value_merger.rs::merge_numeric_values: This seems to be emitting some Cast and mul instructions with no call stack associated, because sometimes the if value and the else value have no call stack associated. I guess this can happen when the if else values don't come from previous instructions, such as being block params or constants
  • remove_if_else.rs: Seems to be emitting array_get instructions with no call_stack, presumably from value_merger::merge_array_values that has a explicit CallStack::new() when emitting an ArrayGet

In the case we can't track down the instructions to a specific location instruction, it'd be good to at least use the call stack of the function being processed, allowing users to at least track that the instruction comes from a given function call, even though they might not know which part of the function being called generated it 🤔

To Reproduce

Modify this code in printer.rs

/// Display an arbitrary instruction
pub(crate) fn display_instruction(
    function: &Function,
    instruction: InstructionId,
    f: &mut Formatter,
) -> Result {
    // instructions are always indented within a function
    write!(f, "    ")?;

    let results = function.dfg.instruction_results(instruction);
    if !results.is_empty() {
        write!(f, "{} = ", value_list(function, results))?;
    }

    display_instruction_inner(function, &function.dfg[instruction], f, instruction)
}

fn display_instruction_inner(
    function: &Function,
    instruction: &Instruction,
    f: &mut Formatter,
    instruction_id: InstructionId,
) -> Result {
    let show = |id| value(function, id);

    let call_stack = function.dfg.get_call_stack(instruction_id);

    if call_stack.is_empty() {
        write!(f, "NO CALL STACK ")?;
    }

And compile a large program such as aztec protocol circuits or the token contract transfer function with show ssa enabled.

Project Impact

None

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Installation Method

None

Nargo Version

No response

NoirJS Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response