Skip to content

Fuel const expr operator cost - #14215

Open
arcusbuilds wants to merge 3 commits into
bytecodealliance:mainfrom
arcusbuilds:fuel-const-expr-operator-cost
Open

Fuel const expr operator cost#14215
arcusbuilds wants to merge 3 commits into
bytecodealliance:mainfrom
arcusbuilds:fuel-const-expr-operator-cost

Conversation

@arcusbuilds

@arcusbuilds arcusbuilds commented Aug 26, 2026

Copy link
Copy Markdown

Closes #14204.

Const-expr operators and the synthesized start call now use the configured operator costs instead of a hardcoded fuel charge of 1. Adds regression tests and a release note.

The issue also reports that variable per-element costs are missing from const-exprs, but those costs are already applied by the shared array.new translation path used by both const-exprs and function bodies. Existing fuel tests confirm this behavior: tests/all/fuel.wast:289 .

Operators inside constant expressions - global initializers, element and
data segment offsets, element segment expressions - were each charged a
hardcoded 1 fuel unit, so `Config::operator_cost` had no effect on any
instantiation-time work.

Constant expressions are stored as `ConstOp` rather than
`wasmparser::Operator`, so add a `const_op_cost` lookup alongside the
existing `cost` lookup and use it in `translate_const_expr`. Default costs
are unchanged: every `ConstOp` still costs 1 with the default table.
`module_start` synthesizes the call to a module's `(start ...)` function
and manually replicates the fuel accounting that `fuel_before_op` performs
for `Operator::Call`. That replica used a hardcoded 1 rather than the
configured cost, so the `Call` entry of `Config::operator_cost` did not
apply to the start call. Look the cost up from the table instead.
`Config::operator_cost` shipped in 48.0.0, so applying it to const-expr
operators and the synthesized start call changes the observable behavior
of released public API; note it under 49.0.0's `Changed` section.

Also pin a second cost-table entry in the const-expr test so a mis-wired
`ConstOp` match arm cannot pass, and document where the two flat
per-function entry charges in the start-call test come from.
@arcusbuilds
arcusbuilds requested review from a team as code owners August 26, 2026 19:49
@arcusbuilds
arcusbuilds requested review from alexcrichton and cfallin and removed request for a team August 26, 2026 19:49
@cfallin

cfallin commented Aug 26, 2026

Copy link
Copy Markdown
Member

@arcusbuilds can you please confirm that you have followed our AI tools policy, including the point about not ever using LLM output directly in a PR description or any other communications on GitHub?

(I ask because most folks would not write a paragraph about how they wrote a test that adds 7 and 100. If this is a false positive, my apologies.)

Once you have confirmed this and/or made adjustments to conform to our policy, I will then review the PR -- thanks.

@github-actions github-actions Bot added wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:config Issues related to the configuration of Wasmtime wasmtime:docs Issues related to Wasmtime's documentation labels Aug 26, 2026
@github-actions

Copy link
Copy Markdown

Label Messager: wasmtime:config

It looks like you are changing Wasmtime's configuration options. Make sure to
complete this check list:

  • If you added a new Config method, you wrote extensive documentation for
    it.

    Details

    Our documentation should be of the following form:

    Short, simple summary sentence.
    
    More details. These details can be multiple paragraphs. There should be
    information about not just the method, but its parameters and results as
    well.
    
    Is this method fallible? If so, when can it return an error?
    
    Can this method panic? If so, when does it panic?
    
    # Example
    
    Optional example here.
    
  • If you added a new Config method, or modified an existing one, you
    ensured that this configuration is exercised by the fuzz targets.

    Details

    For example, if you expose a new strategy for allocating the next instance
    slot inside the pooling allocator, you should ensure that at least one of our
    fuzz targets exercises that new strategy.

    Often, all that is required of you is to ensure that there is a knob for this
    configuration option in wasmtime_fuzzing::Config (or one
    of its nested structs).

    Rarely, this may require authoring a new fuzz target to specifically test this
    configuration. See our docs on fuzzing for more details.

  • If you are enabling a configuration option by default, make sure that it
    has been fuzzed for at least two weeks before turning it on by default.


Details

To modify this label's message, edit the .github/label-messager/wasmtime-config.md file.

To add new label messages or remove existing label messages, edit the
.github/label-messager.json configuration file.

Learn more.

@arcusbuilds

Copy link
Copy Markdown
Author

@cfallin, I have done the adjustment as per the policy.

///
/// Constant expressions are stored as [`ConstOp`] rather than
/// `wasmparser::Operator`, so they need their own lookup, but the costs
/// come from the same table as [`OperatorCostStrategy::cost`].

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Rather than doing this, could we translate to Operator and then use the existing cost machinery?

Comment thread tests/all/fuel.rs
/// | `$start` function entry | 1 |
/// | total | 117 |
///
/// With the default table every op costs 1, so the same module totals 6.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No need for the excessively verbose (and LLM-produced) comment here -- anyone wanting to know what the test does can look at it.

Comment thread tests/all/fuel.rs
///
/// The two entry rows are the flat per-function entry charge
/// (`FuncEnvironment::new`'s `fuel_consumed: 1`, flushed by `fuel_check` on
/// function entry), not derived from any operator's cost.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Likewise here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:config Issues related to the configuration of Wasmtime wasmtime:docs Issues related to Wasmtime's documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Const-expr operators are charged a hardcoded 1 fuel unit, ignoring Config::operator_cost

2 participants