Skip to content

Conversation

@Saw-mon-and-Natalie
Copy link
Contributor

@Saw-mon-and-Natalie Saw-mon-and-Natalie commented Jul 16, 2025

Fixes #16118.

This PR addresses:

By adding a new YUL builtin function clz for EVM versions greater than or equal to Osaka.

Followed the patterns from #14757 to create these modifications:

Tests

Currently the tests in semanticTests/inlineAssembly/clz.sol fail due to evmone not having implemented CLZ:

https://github.com/ethereum/solidity/blob/779daec70826225a447680d2030f5fdaafca541b/test/EVMHost.cpp#L420

In the above line m_vm.execute(...) returns EVMC_UNDEFINED_INSTRUCTION and the m_vm is:

{
    abi_version: 12,
    name: "evmone",
    version: "0.15.0",
    destroy: ... ,
    execute: ... ,
    get_capabilities: ... ,
    set_option: ...
}

References:

@github-actions
Copy link

Thank you for your contribution to the Solidity compiler! A team member will follow up shortly.

If you haven't read our contributing guidelines and our review checklist before, please do it now, this makes the reviewing process and accepting your contribution smoother.

If you have any questions or need our help, feel free to post them in the PR or talk to us directly on the #solidity-dev channel on Matrix.

{Instruction::SHL, {"SHL", 0, 2, 1, false, Tier::VeryLow}},
{Instruction::SHR, {"SHR", 0, 2, 1, false, Tier::VeryLow}},
{Instruction::SAR, {"SAR", 0, 2, 1, false, Tier::VeryLow}},
{Instruction::CLZ, {"CLZ", 0, 1, 1, false, Tier::Low}},
Copy link
Contributor

Choose a reason for hiding this comment

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

CLZ has the same gas cost as ADD, which is in the Tier::VeryLow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it was updated. Also it is 5 in geth. Plus in the EIP it mentions:

The cost of the opcode is 5, matching MUL (raised from 3 to avoid under-pricing DoS risk).

Copy link
Member

@r0qs r0qs Oct 14, 2025

Choose a reason for hiding this comment

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

The linked EIP still mentions a gas cost similar to ADD, and as @Vectorized pointed out, it is indeed 3, i.e. Tier::VeryLow. Which is also the same used by evmone. Do you have the source for the change to 5, @Saw-mon-and-Natalie?

Copy link
Member

Choose a reason for hiding this comment

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

Which is also the same used by evmone.

Oh, I see that in the PR it was actually indeed changed to 5: https://github.com/ipsilon/evmone/pull/1264/files#diff-d47e4d7f522e531f3080bb99145762aef9857717b9e9218a0e90e09e58fa865eR176

Copy link
Member

Choose a reason for hiding this comment

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

this is fine

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's the execution spec that should be the source of truth for us here. count_leading_zeros() uses GAS_LOW, so that's the right value.

But note that it matters whether it's LOW or independently defined as 5. If it was the latter and it had its own constant, we would have to introduce a distinct tier for it. Technically, a future repricing could change the values assigned to those tiers. The cost of CLZ should change in such a case only if it is explicitly defined as a part of such a tier.

Copy link
Collaborator

@matheusaaguiar matheusaaguiar left a comment

Choose a reason for hiding this comment

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

Hi @Saw-mon-and-Natalie , thanks for the PR!
Looking good.

@Saw-mon-and-Natalie
Copy link
Contributor Author

clz also got added to evmone which should solve the test issues (see PR description) here in this PR. But it has not assigned a new version I think yet. So in this repo, one might need to use the compiled binary from the HEAD of that repo.

@r0qs
Copy link
Member

r0qs commented Oct 14, 2025

Hi @Saw-mon-and-Natalie could you please rebase the PR? Or would you prefer if we take it over?

Copy link
Member

@clonker clonker left a comment

Choose a reason for hiding this comment

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

generally lgtm

{Instruction::SHL, {"SHL", 0, 2, 1, false, Tier::VeryLow}},
{Instruction::SHR, {"SHR", 0, 2, 1, false, Tier::VeryLow}},
{Instruction::SAR, {"SAR", 0, 2, 1, false, Tier::VeryLow}},
{Instruction::CLZ, {"CLZ", 0, 1, 1, false, Tier::Low}},
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's the execution spec that should be the source of truth for us here. count_leading_zeros() uses GAS_LOW, so that's the right value.

But note that it matters whether it's LOW or independently defined as 5. If it was the latter and it had its own constant, we would have to introduce a distinct tier for it. Technically, a future repricing could change the values assigned to those tiers. The cost of CLZ should change in such a case only if it is explicitly defined as a part of such a tier.

@clonker clonker changed the base branch from develop to bump-evmone October 16, 2025 07:01
@clonker clonker added the has dependencies The PR depends on other PRs that must be merged first label Oct 16, 2025
@clonker clonker deleted the branch argotorg:develop October 16, 2025 07:34
@clonker clonker closed this Oct 16, 2025
@clonker clonker reopened this Oct 16, 2025
@clonker clonker changed the base branch from bump-evmone to develop October 16, 2025 07:47
@clonker clonker enabled auto-merge October 16, 2025 09:50
@clonker clonker merged commit 728ecd6 into argotorg:develop Oct 16, 2025
62 of 74 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external contribution ⭐ has dependencies The PR depends on other PRs that must be merged first

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for EIP-7939 CLZ

6 participants