Compilation errors encountered when upgrading Rust on-chain script to ckb-std v1.1

This guide is intended for Rust on-chain script projects that need to upgrade from an older version of ckb-std to v1.1 or later.
If you are starting a new project (created by ckb-script-templates), or if your project is already using ckb-std >= v1.1, you usually do not need to follow this guide.

Background

The Rust compiler used by the current CKB Rust contract toolchain has been upgraded to rustc 1.95.0. As a result, ckb-std has also been updated to v1.1 to support this toolchain version.

In rustc 1.95, LLVM has been upgraded to LLVM 22. LLVM 22 no longer supports disabling atomic instructions with -a. Instead, atomic instructions should be lowered using passes=lower-atomic.

Error Log

When encountering this Error, there may be logs similar to:

rustc-LLVM ERROR: Cannot select: 0x11e292150: i64,ch = AtomicLoadAdd<(load store release (s64) on %ir.6)> 0x11dfc2150:1, 0x11e292ee0, Constant:i64<-1>
  0x11e292ee0: i64 = add nuw 0x11dfc2150, Constant:i64<16>
    0x11dfc2150: i64,ch = load<(dereferenceable load (s64) from %ir.0)> 0x11e9e0108, 0x11dfc2230, undef:i64
      0x11dfc2230: i64,ch = CopyFromReg 0x11e9e0108, Register:i64 %2
In function: _ZN5bytes5bytes11shared_drop17h582b12e6806ad067E
error: could not compile `bytes` (lib)

Caused by:
  process didn't exit successfully: `/Users/joiihan/.rustup/toolchains/1.95.0-aarch64-apple-darwin/bin/rustc --crate-name bytes --edition=2021 /Users/joiihan/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bytes-1.11.1/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=138 --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no -C codegen-units=1 --warn=unexpected_cfgs --check-cfg 'cfg(loom)' -C overflow-checks=on --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values("default", "extra-platforms", "serde", "std"))' -C metadata=985f180287cf7db9 -C extra-filename=-0f966891aea62c02 --out-dir /Volumes/workspack/code/ckb/ckb-script-ipc/target/riscv64imac-unknown-none-elf/release/deps --target riscv64imac-unknown-none-elf -C strip=symbols -L dependency=/Volumes/workspack/code/ckb/ckb-script-ipc/target/riscv64imac-unknown-none-elf/release/deps -L dependency=/Volumes/workspack/code/ckb/ckb-script-ipc/target/release/deps --cap-lints allow -C target-feature=+zba,+zbb,+zbc,+zbs,-a -C debug_assertions` (exit status: 101)
warning: build failed, waiting for other jobs to finish...
make[1]: *** [build] Error 101
make: *** [build] Error 2

Fix

  1. Upgrade ckb-std to the latest version. Currently, this is v1.1.

  2. If the project root contains a rust-toolchain file, update the Rust compiler version to 1.95.0.

  3. Update the Makefile.

    For example, change:

    FULL_RUSTFLAGS := -C target-feature=+zba,+zbb,+zbc,+zbs,-a $(CUSTOM_RUSTFLAGS)
    

    Remove ,-a , and add -C passes=lower-atomic.

  4. If the project was created using ckb-script-templates, replace find_clang and reproducible_build_docker under the scripts directory with the latest versions.

Tips

  • If the project contains multiple contracts, you can replace ,-a globally with -C passes=lower-atomic. Pay attention to spaces and commas.
  • It is also recommended to upgrade ckb-testtool at the same time.
  • The CI in .github may need to be upgraded simultaneously.
2 Likes