feature(sierra-gen): Added support for injecting constant values externally.#10159
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
4d2e354 to
7ff65e3
Compare
| } | ||
| value | ||
| } | ||
| None => ConstValueId::from_int(db, return_type, &BigInt::ZERO), |
There was a problem hiding this comment.
This should emit warnings imo
There was a problem hiding this comment.
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)?; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
it would emit the full path of the extern - which is basically the same path as the contract (+ a bit)
orizi
left a comment
There was a problem hiding this comment.
@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)?; |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
impossible to emit diagnotics at this stage - so at most - emit a log warning.

Summary
Introduces a reserved extern function
__externally_provided_const__that, when called in Cairo code, is intercepted during Sierra generation and replaced with aconst_as_immediateinstruction whose value is supplied by a caller-installedExternalConstProvidercallback. The provider receives the full path of the extern function and its declared return type, and returns aConstValueIdto 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 viaset_external_const_provider/external_const_providermethods onSierraGenGroup. A test provider resolving every call to the constant7777is installed in the test harness, and a new test file (externally_provided_const) verifies that the generated Sierra containsconst_as_immediate<Const<felt252, 7777>>.Type of change
Please check one:
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 anExternalConstProvideris installed on the database, the call is replaced with aconst_as_immediateof 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
ConstValueIdtype 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.