Skip to content

Inlining: More accurate function weight calculation #9410

@vezenovm

Description

@vezenovm

Problem

We have differing runtimes that have different costs for a given SSA instruction. In the case of inlining we are mostly concerned with the Brillig runtime as we expect all ACIR functions to be inlined.

However, when computing the weights of different functions we use the number of SSA instructions as our heuristic

weight += func.dfg[block_id].instructions().len() + 1; // We add one for the terminator

This is imperfect as some SSA operations could translate to many different Brillig instructions while others could be closer to a one-to-one translation. This could lead to marking function weights incorrectly and an incorrect cost calculation.

The + 1 for the terminator also needs to be updated to accurately reflect real Brillig opcode costs. We can see how a jmpif terminator in fact requires two Brillig opcodes:

This then grows even more for jmp and return which require Mov instructions to pass along their arguments.

Happy Case

We should have cost estimates for how many Brillig instructions are generated from a given SSA instruction. This cost estimate should be used in computing the weights of functions in Brillig.

We actually do something similar in the basic_conditional optimization:

fn block_cost(block: BasicBlockId, dfg: &DataFlowGraph) -> u32 {

We could have a brillig_cost function on Instruction that returns this estimate and is used across passes that may need this estimate.

Workaround

None

Workaround Description

No response

Additional Context

No response

Project Impact

Nice-to-have

Blocker Context

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

Metadata

Metadata

Assignees

Labels

auditPreparation or execution of security code audits.enhancementNew feature or requestssa

Type

No type

Projects

Status

📋 Backlog

Relationships

None yet

Development

No branches or pull requests

Issue actions