Skip to content

Conversation

@orizi
Copy link
Collaborator

@orizi orizi commented Jan 17, 2026

Summary

Refactored the CASM builder to improve label handling and instruction relocation. The PR replaces the Statement enum with a more direct approach that separates instructions from relocations, making the code more maintainable. It introduces a dedicated LabelRelocation struct and Offset type to track and apply relocations more efficiently, eliminating the need to compute label offsets in a separate pass.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

The previous implementation used a complex approach with a Statement enum that mixed instructions, jumps, and labels. This required a separate pass to compute label offsets and apply relocations. The new implementation is more direct, tracking instructions and relocations separately, which simplifies the code and improves performance by eliminating the need for the extra computation pass.


What was the behavior or documentation before?

The builder used a Statement enum with three variants (Final, Jump, Label) and required a separate pass to compute label offsets before applying relocations to jump instructions.


What is the behavior or documentation after?

The builder now directly stores instructions and tracks relocations separately with a dedicated LabelRelocation struct. Label offsets are tracked in the LabelInfo struct with an Offset type, allowing for more efficient relocation handling without requiring a separate computation pass.


Additional context

This refactoring maintains the same functionality while making the code more maintainable and potentially more efficient by eliminating unnecessary computation steps. The change is internal to the CASM builder and doesn't affect its public API.

Copy link
Collaborator Author

orizi commented Jan 17, 2026

@reviewable-StarkWare
Copy link

This change is Reviewable

@orizi orizi marked this pull request as ready for review January 17, 2026 08:10
Copy link
Contributor

@eytan-starkware eytan-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests checking multiple branches, and something that tests future_label

@eytan-starkware reviewed all commit messages and made 9 comments.
Reviewable status: 0 of 1 files reviewed, 8 unresolved discussions (waiting on @orizi and @TomerStarkware).


crates/cairo-lang-casm/src/builder.rs line 104 at r1 (raw file):

    /// The index of the instruction that needs to be relocated.
    instruction_index: usize,
    /// The code offset if the instruction to be relocated.

Fix comment


crates/cairo-lang-casm/src/builder.rs line 121 at r1 (raw file):

    state: State,
    /// The offset of the label. If not set means not yet reached (and not read-only).
    offset: Offset,

If it might not yet be set shouldnt it just be option?


crates/cairo-lang-casm/src/builder.rs line 128 at r1 (raw file):

impl Offset {
    /// The value for an unset offset.
    const UNSET: Offset = Offset(usize::MAX);

Should be u32, as later you assume it fits i32. Even better to keep it i32


crates/cairo-lang-casm/src/builder.rs line 172 at r1 (raw file):

        let mut branch_relocations: [Vec<usize>; BRANCH_COUNT] =
            core::array::from_fn(|_| Vec::new());
        for r in self.relocations {

relocation

Code quote:

r

crates/cairo-lang-casm/src/builder.rs line 179 at r1 (raw file):

                relocate_instruction(
                    &mut self.instructions[r.instruction_index],
                    label_offset.0 as i32 - r.instruction_offset as i32,

I assume this will never overflow, but how big can we get here?


crates/cairo-lang-casm/src/builder.rs line 442 at r1 (raw file):

        );
        let state = core::mem::take(&mut self.main_state);
        self.set_or_test_label(label, state);

You are taking state and not returning it on purpose?


crates/cairo-lang-casm/src/builder.rs line 742 at r1 (raw file):

/// Relocates an instruction by updating its jump offset.
fn relocate_instruction(instruction: &mut Instruction, updated: i32) {

#[inline(always)]
fn relocate_instruction(

Code quote:

fn relocate_instruction(

crates/cairo-lang-casm/src/builder.rs line 749 at r1 (raw file):

        })
        | InstructionBody::Jump(JumpInstruction {
            target: DerefOrImmediate::Immediate(value),

Does jump target really need to be bigint?

@orizi orizi force-pushed the orizi/01-15-performance_casm_made_casm_builder_build-alg_linear branch from b894b20 to aa71e7f Compare January 18, 2026 17:50
Copy link
Collaborator Author

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi made 8 comments.
Reviewable status: 0 of 1 files reviewed, 8 unresolved discussions (waiting on @eytan-starkware and @TomerStarkware).


crates/cairo-lang-casm/src/builder.rs line 104 at r1 (raw file):

Previously, eytan-starkware wrote…

Fix comment

Done.


crates/cairo-lang-casm/src/builder.rs line 121 at r1 (raw file):

Previously, eytan-starkware wrote…

If it might not yet be set shouldnt it just be option?

the size of the object here seems to have a non-negligible effect.


crates/cairo-lang-casm/src/builder.rs line 128 at r1 (raw file):

Previously, eytan-starkware wrote…

Should be u32, as later you assume it fits i32. Even better to keep it i32

it is created from usize - as it is the sum of usizes - checked on conversion - instead of on every add.


crates/cairo-lang-casm/src/builder.rs line 172 at r1 (raw file):

Previously, eytan-starkware wrote…

relocation

Done.


crates/cairo-lang-casm/src/builder.rs line 179 at r1 (raw file):

Previously, eytan-starkware wrote…

I assume this will never overflow, but how big can we get here?

very very small - this is just for creating a single libfunc code.


crates/cairo-lang-casm/src/builder.rs line 442 at r1 (raw file):

Previously, eytan-starkware wrote…

You are taking state and not returning it on purpose?

it is a jump code - the current state is the default (no vars) and unreachable.


crates/cairo-lang-casm/src/builder.rs line 742 at r1 (raw file):

Previously, eytan-starkware wrote…

#[inline(always)]
fn relocate_instruction(

rather let the compiler do its thing - no reason for me to do that.


crates/cairo-lang-casm/src/builder.rs line 749 at r1 (raw file):

Previously, eytan-starkware wrote…

Does jump target really need to be bigint?

not up to us - especially not in this PR.

SIERRA_UPDATE_PATCH_CHANGE_TAG=No interface changes.
@orizi orizi force-pushed the orizi/01-15-performance_casm_made_casm_builder_build-alg_linear branch from aa71e7f to 99a862b Compare January 19, 2026 06:38
Copy link
Collaborator Author

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are already multiple tests with multiple branches.
added future_label test.

@orizi made 1 comment.
Reviewable status: 0 of 1 files reviewed, 8 unresolved discussions (waiting on @eytan-starkware and @TomerStarkware).

Copy link
Contributor

@eytan-starkware eytan-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

@eytan-starkware reviewed 2 files and all commit messages, made 2 comments, and resolved 7 discussions.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @orizi and @TomerStarkware).


crates/cairo-lang-casm/src/builder.rs line 742 at r1 (raw file):

Previously, orizi wrote…

rather let the compiler do its thing - no reason for me to do that.

I would say if we are sure we could inline it (as this just simplifies code), we should help the compiler out

Copy link
Collaborator Author

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi resolved 1 discussion.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @TomerStarkware).

@orizi orizi added this pull request to the merge queue Jan 19, 2026
Merged via the queue into main with commit ffebf01 Jan 19, 2026
55 checks passed
Copy link
Collaborator

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

@TomerStarkware made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved.

@orizi orizi deleted the orizi/01-15-performance_casm_made_casm_builder_build-alg_linear branch January 20, 2026 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants