Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

cannon: mipsevm: Support mips64r2 for kona #14405

Conversation

pcw109550
Copy link
Contributor

@pcw109550 pcw109550 commented Feb 18, 2025

Description

This attempts cannon's offchain version(mipsevm) to support minimal instruction subset to run kona compiled down to mips64r2 introduced here. Relevant issue: #13969. We can extract all instructions by diassembling kona mips64r2 binary. Onchain fixes are not introduced intentionally. If we decide to merge this PR, further contract impls needed.

After building kona binary using just build-cannon --bin kona --profile release-client-lto at root of kona repo,

$ file target/mips64-unknown-none/release-client-lto/kona
target/mips64-unknown-none/release-client-lto/kona: ELF 64-bit MSB executable, MIPS, MIPS64 rel2 version 1 (SYSV), statically linked, BuildID[sha1]=792eedc498a7888b3e50bfd606fce50be217966a, for GNU/Linux 3.2.0, not stripped

Run

$ llvm-objdump -d --mattr=mips64 target/mips64-unknown-none/release-client-lto/kona | awk '{print $6}' |  sort | uniq -c | sort -nr | awk '{
    printf "%s{\"c\": %s, \"i\": \"%s\"}", (NR==1?"[":" ,"), $1, $2
} END { print "]" }'
[{"c": 108339, "i": "sd"} ,{"c": 99874, "i": "daddiu"} ,{"c": 90576, "i": "ld"} ,{"c": 45429, "i": "daddu"} ,{"c": 35282, "i": "dsll"} ,{"c": 30513, "i": "move"} ,{"c": 27572, "i": "nop"} ,{"c": 22125, "i": "ori"} ,{"c": 19788, "i": "jal"} ,{"c": 18817, "i": "lui"} ,{"c": 14231, "i": "beqz"} ,{"c": 11924, "i": "addiu"} ,{"c": 11650, "i": "j"} ,{"c": 10596, "i": "sltu"} ,{"c": 7239, "i": "lbu"} ,{"c": 7018, "i": "bnez"} ,{"c": 6384, "i": "sb"} ,{"c": 6345, "i": "dext"} ,{"c": 5533, "i": "ldr"} ,{"c": 5533, "i": "ldl"} ,{"c": 5473, "i": "bne"} ,{"c": 5398, "i": ""} ,{"c": 5075, "i": "and"} ,{"c": 4931, "i": "dsubu"} ,{"c": 4649, "i": "or"} ,{"c": 4634, "i": "sdr"} ,{"c": 4634, "i": "sdl"} ,{"c": 4348, "i": "mflo"} ,{"c": 4286, "i": "xor"} ,{"c": 3968, "i": "jr"} ,{"c": 3851, "i": "mfhi"} ,{"c": 3787, "i": "andi"} ,{"c": 3708, "i": "sltiu"} ,{"c": 3527, "i": "lw"} ,{"c": 3140, "i": "sw"} ,{"c": 3056, "i": "jalr"} ,{"c": 2766, "i": "dmultu"} ,{"c": 2612, "i": "dsll32"} ,{"c": 2478, "i": "beq"} ,{"c": 2071, "i": "dsrl32"} ,{"c": 2047, "i": "dsrl"} ,{"c": 1967, "i": "movn"} ,{"c": 1604, "i": "addu"} ,{"c": 1524, "i": "sll"} ,{"c": 1197, "i": "mtlo"} ,{"c": 1197, "i": "mthi"} ,{"c": 1159, "i": "sync"} ,{"c": 1131, "i": "lwr"} ,{"c": 1131, "i": "lwl"} ,{"c": 1032, "i": "movz"} ,{"c": 975, "i": "sh"} ,{"c": 929, "i": "rotr"} ,{"c": 838, "i": "swr"} ,{"c": 838, "i": "swl"} ,{"c": 818, "i": "dshd"} ,{"c": 818, "i": "dsbh"} ,{"c": 769, "i": "dclz"} ,{"c": 721, "i": "dmult"} ,{"c": 699, "i": "lhu"} ,{"c": 618, "i": "xori"} ,{"c": 596, "i": "lb"} ,{"c": 576, "i": "scd"} ,{"c": 576, "i": "lld"} ,{"c": 573, "i": "subu"} ,{"c": 540, "i": "bltz"} ,{"c": 496, "i": "srl"} ,{"c": 447, "i": "dnegu"} ,{"c": 364, "i": "dsllv"} ,{"c": 243, "i": "dsra32"} ,{"c": 197, "i": "not"} ,{"c": 187, "i": "dsrlv"} ,{"c": 156, "i": "break"} ,{"c": 143, "i": "slti"} ,{"c": 140, "i": "bgez"} ,{"c": 109, "i": "slt"} ,{"c": 106, "i": "sllv"} ,{"c": 87, "i": "ins"} ,{"c": 78, "i": "lwu"} ,{"c": 52, "i": "srlv"} ,{"c": 41, "i": "seb"} ,{"c": 41, "i": "multu"} ,{"c": 37, "i": "ext"} ,{"c": 37, "i": "drotr32"} ,{"c": 34, "i": "wsbh"} ,{"c": 30, "i": "mul"} ,{"c": 28, "i": "negu"} ,{"c": 28, "i": "lh"} ,{"c": 26, "i": "drotr"} ,{"c": 25, "i": "seh"} ,{"c": 22, "i": "sc"} ,{"c": 22, "i": "ll"} ,{"c": 20, "i": "nor"} ,{"c": 19, "i": "sra"} ,{"c": 17, "i": "dsra"} ,{"c": 17, "i": "blez"} ,{"c": 16, "i": "ddivu"} ,{"c": 5, "i": "syscall"} ,{"c": 5, "i": "dins"} ,{"c": 5, "i": "clz"} ,{"c": 4, "i": "bal"} ,{"c": 3, "i": "bgtz"} ,{"c": 2, "i": "srav"} ,{"c": 2, "i": "mult"} ,{"c": 2, "i": "divu"} ,{"c": 1, "i": "b"}]

and get all instructions for kona. This PR implements instructions which were not implemented in mipsevm, for running kona mips64r2 binary.

Instruction manual here: https://s3-eu-west-1.amazonaws.com/downloads-mips/documents/MIPS_Architecture_MIPS64_InstructionSet_%20AFP_P_MD00087_06.05.pdf

Tests

Unit tests added per execution of new instructions off chain.

Validated that kona mips64r2 binary runs inside cannon by using preimage https://github.com/op-rs/kona/blob/main/bin/client/testdata/holocene-op-sepolia-22012816-witness.tar.zst, based on op-rs/kona#1052 and kona ci.

just run-client-cannon-offline 22012816 0x42ff78e504c207c3786cb30ecb74fe915984b48649165f95bbf6f9248584be69 0x9084f101b85cd1c7c624946feca169768896d88b3ecf4eea3a7760bfceb9cd73 0x6a34183664b9ad39de024a8d4077c78abf05198148b6dbfc6e39fbe4a70de299 0x02a50d0b5a3226758a6e9b2bdeb5deb5f0779ab55b2b34a52331d0eac48c9389 11155420

Differential testing not implemented because only implemented the offchain version. Also no fuzz tests yet.

Metadata

#13969

@Inphi
Copy link
Contributor

Inphi commented Feb 18, 2025

/ci authorize f7274b2

@pcw109550
Copy link
Contributor Author

pcw109550 commented Feb 18, 2025

I see there are few unit test failures that need to be fixed: https://app.circleci.com/pipelines/github/ethereum-optimism/optimism/81542/workflows/59e428dd-81c1-4965-a4ec-5368becdd4bd/jobs/3264849/tests. All those are related with sign extensions; For example sign extending to 64bit but test hardcoded it to 32bit. Will be fixed.

@pcw109550
Copy link
Contributor Author

Going for minimal diff; DCLZ support is enough for kona by targeting mips64r1.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants