noir-lang / noir

Noir is a domain specific language for zero knowledge proofs
https://noir-lang.org
Apache License 2.0
821 stars 177 forks source link

Flattening and remove ifelse adds instructions with empty call stacks #5322

Open sirasistant opened 5 days ago

sirasistant commented 5 days ago

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:

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 :thinking:

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