Skip to content

feature(sierra-gen): Added support for injecting constant values externally.#10159

Draft
orizi wants to merge 1 commit into
mainfrom
orizi/06-24-feature_sierra-gen_added_support_for_injecting_constant_values_externally
Draft

feature(sierra-gen): Added support for injecting constant values externally.#10159
orizi wants to merge 1 commit into
mainfrom
orizi/06-24-feature_sierra-gen_added_support_for_injecting_constant_values_externally

Conversation

@orizi

@orizi orizi commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

Summary

Introduces a reserved extern function __externally_provided_const__ that, when called in Cairo code, is intercepted during Sierra generation and replaced with a const_as_immediate instruction whose value is supplied by a caller-installed ExternalConstProvider callback. The provider receives the full path of the extern function and its declared return type, and returns a ConstValueId to substitute. The mechanism is wired into both the code generator (block_generator.rs) and the local-variables analysis (local_variables.rs), where calls to this extern are treated as constants with known AP change rather than resolved as real libfuncs.

A new Salsa input (SierraGenGroupInput) stores the optional provider on the database, exposed via set_external_const_provider / external_const_provider methods on SierraGenGroup. A test provider resolving every call to the constant 7777 is installed in the test harness, and a new test file (externally_provided_const) verifies that the generated Sierra contains const_as_immediate<Const<felt252, 7777>>.


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?

There was no mechanism for build drivers or tooling to inject a compile-time constant into a Cairo program without modifying source code. This is needed when the value of a constant is only known at build time (e.g. a contract address, a configuration value, or a version hash) and must be embedded into the compiled Sierra output without requiring the Cairo source to hard-code it.


What was the behavior or documentation before?

There was no way to externally supply a constant value during Sierra generation. Any constant had to be written directly in Cairo source.


What is the behavior or documentation after?

A Cairo program may declare extern fn __externally_provided_const__() -> T nopanic; and call it. During Sierra generation, if an ExternalConstProvider is installed on the database, the call is replaced with a const_as_immediate of the value the provider returns for that function's full path and return type. If no provider is installed and the extern is called, Sierra generation panics.


Related issue or discussion (if any)


Additional context

The provider's returned ConstValueId type is validated against the declared return type of the extern; a mismatch panics rather than producing a type-incorrect Sierra program. The extern function is recognized solely by its name and never appears in the final Sierra output.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator Author

@orizi orizi force-pushed the orizi/06-24-feature_sierra-gen_added_support_for_injecting_constant_values_externally branch from 4d2e354 to 7ff65e3 Compare June 24, 2026 11:48
@orizi orizi mentioned this pull request Jun 24, 2026
5 tasks
}
value
}
None => ConstValueId::from_int(db, return_type, &BigInt::ZERO),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This should emit warnings imo

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

impossible to emit diagnotics at this stage - so at most - emit a log warning.

let value = match db.external_const_provider() {
Some(provider) => {
let (extern_id, _) = statement.function.get_extern(db).unwrap();
let value = provider(db, &extern_id.full_path(db), return_type)?;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I presume extern_id.full_path will yield the externs' call site?
Don't we want more flexibility here and add some parameters to the method?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

it would emit the full path of the extern - which is basically the same path as the contract (+ a bit)

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@orizi made 2 comments.
Reviewable status: 0 of 8 files reviewed, 2 unresolved discussions (waiting on Arcticae).

let value = match db.external_const_provider() {
Some(provider) => {
let (extern_id, _) = statement.function.get_extern(db).unwrap();
let value = provider(db, &extern_id.full_path(db), return_type)?;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

it would emit the full path of the extern - which is basically the same path as the contract (+ a bit)

}
value
}
None => ConstValueId::from_int(db, return_type, &BigInt::ZERO),

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

impossible to emit diagnotics at this stage - so at most - emit a log warning.

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.

3 participants