Avoid redundant upcasts in UInt8/UInt16 comparisons#715
Avoid redundant upcasts in UInt8/UInt16 comparisons#715
Conversation
|
Maybe let's add Eurydice to CI and merge this afterwards? |
When both operands of a comparison are atomic (variables, field accesses, function results — not arithmetic expressions), strip the unnecessary Cast(_, UInt32) wrappers and compare at native width. Before: (uint32_t)a == (uint32_t)b (redundant casts) After: a == b (clean, no casts) Arithmetic comparisons are unaffected — they still widen and mask: ((uint32_t)a + (uint32_t)b & 0xFFU) == (uint32_t)c Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
07b6e4b to
aeb42bd
Compare
|
Ok just catching up on this. Thanks. This is an acceptable fix for #716 -- I'll let you decide if you prefer this or my suggestion from #703. I'm talking to @Nadrieril tomorrow morning so we'll see about Eurydice CI then, unless @tahina-pro understands enough to copy/paste the nix build description from Eurydice to here. |
|
Let me take a stab at it, I will add eurydice NIX on this PR. |
|
ok interesting diff -- we'd have to review this carefully |
|
|
I think this looks ok but I'd like a second careful review. |
| test-eurydice: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@master | ||
|
|
||
| - name: Install Nix | ||
| uses: DeterminateSystems/nix-installer-action@main | ||
|
|
||
| - name: Build and test Eurydice against this Karamel | ||
| run: | | ||
| nix build -L --no-link \ | ||
| "github:AeneasVerif/eurydice#checks.x86_64-linux.default" \ | ||
| --override-input karamel "github:FStarLang/karamel/$GITHUB_SHA" |
There was a problem hiding this comment.
Looks good to me! The only issue is that this will take ages because it recompiles Charon and all Eurydice dependencies from scratch every time. You should be able to pull paths directly from our CI system by using the https://hacl.cachix.org cachix path. Maybe this is enough:
- uses: cachix/cachix-action@v15
with:
name: hacl
.github/workflows/ci.yml
Outdated
| test-eurydice: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@master |
There was a problem hiding this comment.
You don't need checkout actually, the flake will fetch it
Builds and tests Eurydice against the current Karamel commit using nix. Uses --override-input to point Eurydice's karamel flake input at the PR branch, catching extraction regressions in downstream projects. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
418e86c to
b0d1b70
Compare
|
Indeed, this is not equivalent when variables are 32-bits. Here is an SMT-LIB2 that shows this. |
|
How do I read the SMT2 file? |
|
Nix part LGTM |
|
If lhs.ptr[i0], rhs.ptr[i0] and mask are u32, there is a counter-example given by running the SMT query. If not, this is sound. I am not sure what their type is from looking at the CI logs. |
This should fix the Eurydice regression from #694 @protz
When both operands of a comparison are atomic (variables, field accesses, function results — not arithmetic expressions), strip the unnecessary Cast(_, UInt32) wrappers and compare at native width.
Before: (uint32_t)a == (uint32_t)b (redundant casts)
After: a == b (clean, no casts)
Arithmetic comparisons are unaffected — they still widen and mask:
((uint32_t)a + (uint32_t)b & 0xFFU) == (uint32_t)c