diff --git a/.github/workflows/bench-hyperfine.yml b/.github/workflows/bench-hyperfine.yml index 7d4fca2b0..8f6c2e777 100644 --- a/.github/workflows/bench-hyperfine.yml +++ b/.github/workflows/bench-hyperfine.yml @@ -20,8 +20,8 @@ jobs: runs-on: ubuntu-latest env: CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse - MLIR_SYS_170_PREFIX: /usr/lib/llvm-17/ - TABLEGEN_170_PREFIX: /usr/lib/llvm-17/ + MLIR_SYS_180_PREFIX: /usr/lib/llvm-18/ + TABLEGEN_180_PREFIX: /usr/lib/llvm-18/ steps: - uses: actions/checkout@v4 - name: check and free hdd space left @@ -50,11 +50,11 @@ jobs: - name: add llvm deb repository uses: myci-actions/add-deb-repo@10 with: - repo: deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-17 main + repo: deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main repo-name: llvm-repo keys-asc: https://apt.llvm.org/llvm-snapshot.gpg.key - name: Install LLVM - run: sudo apt-get install llvm-17 llvm-17-dev llvm-17-runtime clang-17 clang-tools-17 lld-17 libpolly-17-dev libmlir-17-dev mlir-17-tools + run: sudo apt-get install llvm-18 llvm-18-dev llvm-18-runtime clang-18 clang-tools-18 lld-18 libpolly-18-dev libmlir-18-dev mlir-18-tools - name: Install hyperfine uses: taiki-e/install-action@v2 with: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 28e44e16e..3fed3041a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,9 +12,9 @@ jobs: name: clippy runs-on: ubuntu-latest env: - MLIR_SYS_170_PREFIX: /usr/lib/llvm-17/ - LLVM_SYS_170_PREFIX: /usr/lib/llvm-17/ - TABLEGEN_170_PREFIX: /usr/lib/llvm-17/ + MLIR_SYS_180_PREFIX: /usr/lib/llvm-18/ + LLVM_SYS_180_PREFIX: /usr/lib/llvm-18/ + TABLEGEN_180_PREFIX: /usr/lib/llvm-18/ steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@1.76 @@ -24,11 +24,11 @@ jobs: - name: add llvm deb repository uses: myci-actions/add-deb-repo@10 with: - repo: deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-17 main + repo: deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main repo-name: llvm-repo keys-asc: https://apt.llvm.org/llvm-snapshot.gpg.key - name: Install LLVM - run: sudo apt-get install llvm-17 llvm-17-dev llvm-17-runtime clang-17 clang-tools-17 lld-17 libpolly-17-dev libmlir-17-dev mlir-17-tools + run: sudo apt-get install llvm-18 llvm-18-dev llvm-18-runtime clang-18 clang-tools-18 lld-18 libpolly-18-dev libmlir-18-dev mlir-18-tools - name: Clippy run: make check @@ -79,9 +79,9 @@ jobs: udeps: runs-on: ubuntu-latest env: - MLIR_SYS_170_PREFIX: /usr/lib/llvm-17/ - LLVM_SYS_170_PREFIX: /usr/lib/llvm-17/ - TABLEGEN_170_PREFIX: /usr/lib/llvm-17/ + MLIR_SYS_180_PREFIX: /usr/lib/llvm-18/ + LLVM_SYS_180_PREFIX: /usr/lib/llvm-18/ + TABLEGEN_180_PREFIX: /usr/lib/llvm-18/ RUSTUP_TOOLCHAIN: nightly-2024-02-01 # udeps needs nightly steps: - uses: actions/checkout@v4 @@ -93,11 +93,11 @@ jobs: - name: add llvm deb repository uses: myci-actions/add-deb-repo@10 with: - repo: deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-17 main + repo: deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main repo-name: llvm-repo keys-asc: https://apt.llvm.org/llvm-snapshot.gpg.key - name: Install LLVM - run: sudo apt-get install llvm-17 llvm-17-dev llvm-17-runtime clang-17 clang-tools-17 lld-17 libpolly-17-dev libmlir-17-dev mlir-17-tools + run: sudo apt-get install llvm-18 llvm-18-dev llvm-18-runtime clang-18 clang-tools-18 lld-18 libpolly-18-dev libmlir-18-dev mlir-18-tools - name: "Download and run cargo-udeps" run: | wget -O - -c https://github.com/est31/cargo-udeps/releases/download/v0.1.45/cargo-udeps-v0.1.45-x86_64-unknown-linux-gnu.tar.gz | tar -xz @@ -107,9 +107,9 @@ jobs: name: test (linux, amd64) runs-on: ubuntu-latest env: - MLIR_SYS_170_PREFIX: /usr/lib/llvm-17/ - LLVM_SYS_170_PREFIX: /usr/lib/llvm-17/ - TABLEGEN_170_PREFIX: /usr/lib/llvm-17/ + MLIR_SYS_180_PREFIX: /usr/lib/llvm-18/ + LLVM_SYS_180_PREFIX: /usr/lib/llvm-18/ + TABLEGEN_180_PREFIX: /usr/lib/llvm-18/ RUST_LOG: debug steps: - uses: actions/checkout@v4 @@ -139,11 +139,11 @@ jobs: - name: add llvm deb repository uses: myci-actions/add-deb-repo@10 with: - repo: deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-17 main + repo: deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main repo-name: llvm-repo keys-asc: https://apt.llvm.org/llvm-snapshot.gpg.key - name: Install LLVM - run: sudo apt-get install llvm-17 llvm-17-dev llvm-17-runtime clang-17 clang-tools-17 lld-17 libpolly-17-dev libmlir-17-dev mlir-17-tools + run: sudo apt-get install llvm-18 llvm-18-dev llvm-18-runtime clang-18 clang-tools-18 lld-18 libpolly-18-dev libmlir-18-dev mlir-18-tools - name: Install deps run: make deps - name: Build cairo-native-runtime library. @@ -158,9 +158,9 @@ jobs: env: CARGO_TERM_COLOR: always LIBRARY_PATH: /opt/homebrew/lib - MLIR_SYS_170_PREFIX: /opt/homebrew/opt/llvm@17 - LLVM_SYS_170_PREFIX: /opt/homebrew/opt/llvm@17 - TABLEGEN_170_PREFIX: /opt/homebrew/opt/llvm@17 + MLIR_SYS_180_PREFIX: /opt/homebrew/opt/llvm@18 + LLVM_SYS_180_PREFIX: /opt/homebrew/opt/llvm@18 + TABLEGEN_180_PREFIX: /opt/homebrew/opt/llvm@18 RUST_LOG: debug steps: - uses: actions/checkout@v4 @@ -187,9 +187,9 @@ jobs: runs-on: ubuntu-latest env: CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse - MLIR_SYS_170_PREFIX: /usr/lib/llvm-17/ - LLVM_SYS_170_PREFIX: /usr/lib/llvm-17/ - TABLEGEN_170_PREFIX: /usr/lib/llvm-17/ + MLIR_SYS_180_PREFIX: /usr/lib/llvm-18/ + LLVM_SYS_180_PREFIX: /usr/lib/llvm-18/ + TABLEGEN_180_PREFIX: /usr/lib/llvm-18/ RUST_LOG: debug steps: - uses: actions/checkout@v4 @@ -219,11 +219,11 @@ jobs: - name: add llvm deb repository uses: myci-actions/add-deb-repo@10 with: - repo: deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-17 main + repo: deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main repo-name: llvm-repo keys-asc: https://apt.llvm.org/llvm-snapshot.gpg.key - name: Install LLVM - run: sudo apt-get install llvm-17 llvm-17-dev llvm-17-runtime clang-17 clang-tools-17 lld-17 libpolly-17-dev libmlir-17-dev mlir-17-tools + run: sudo apt-get install llvm-18 llvm-18-dev llvm-18-runtime clang-18 clang-tools-18 lld-18 libpolly-18-dev libmlir-18-dev mlir-18-tools - name: Install cargo-llvm-cov uses: taiki-e/install-action@cargo-llvm-cov - name: Install scarb diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 262fb9e35..66f7844cc 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -13,9 +13,9 @@ jobs: release: runs-on: ubuntu-latest env: - MLIR_SYS_170_PREFIX: /usr/lib/llvm-17/ - LLVM_SYS_170_PREFIX: /usr/lib/llvm-17/ - TABLEGEN_170_PREFIX: /usr/lib/llvm-17/ + MLIR_SYS_180_PREFIX: /usr/lib/llvm-18/ + LLVM_SYS_180_PREFIX: /usr/lib/llvm-18/ + TABLEGEN_180_PREFIX: /usr/lib/llvm-18/ CRATES_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} steps: - name: Checkout @@ -32,11 +32,11 @@ jobs: - name: add llvm deb repository uses: myci-actions/add-deb-repo@10 with: - repo: deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-17 main + repo: deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main repo-name: llvm-repo keys-asc: https://apt.llvm.org/llvm-snapshot.gpg.key - name: Install LLVM - run: sudo apt-get install llvm-17 llvm-17-dev llvm-17-runtime clang-17 clang-tools-17 lld-17 libpolly-17-dev libmlir-17-dev mlir-17-tools + run: sudo apt-get install llvm-18 llvm-18-dev llvm-18-runtime clang-18 clang-tools-18 lld-18 libpolly-18-dev libmlir-18-dev mlir-18-tools - name: publish the runtime run: cargo publish --token ${CRATES_TOKEN} --all-features -p cairo-native-runtime - name: publish the crate diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8683ada10..75a603594 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -13,9 +13,9 @@ jobs: release: runs-on: ubuntu-latest env: - MLIR_SYS_170_PREFIX: /usr/lib/llvm-17/ - LLVM_SYS_170_PREFIX: /usr/lib/llvm-17/ - TABLEGEN_170_PREFIX: /usr/lib/llvm-17/ + MLIR_SYS_180_PREFIX: /usr/lib/llvm-18/ + LLVM_SYS_180_PREFIX: /usr/lib/llvm-18/ + TABLEGEN_180_PREFIX: /usr/lib/llvm-18/ steps: - name: Checkout uses: actions/checkout@v3 @@ -31,11 +31,11 @@ jobs: - name: add llvm deb repository uses: myci-actions/add-deb-repo@10 with: - repo: deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-17 main + repo: deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main repo-name: llvm-repo keys-asc: https://apt.llvm.org/llvm-snapshot.gpg.key - name: Install LLVM - run: sudo apt-get install llvm-17 llvm-17-dev llvm-17-runtime clang-17 clang-tools-17 lld-17 libpolly-17-dev libmlir-17-dev mlir-17-tools + run: sudo apt-get install llvm-18 llvm-18-dev llvm-18-runtime clang-18 clang-tools-18 lld-18 libpolly-18-dev libmlir-18-dev mlir-18-tools - name: build release run: make build - name: Release diff --git a/.github/workflows/rustdoc.yml b/.github/workflows/rustdoc.yml index d93c23fce..b31b8fdc1 100644 --- a/.github/workflows/rustdoc.yml +++ b/.github/workflows/rustdoc.yml @@ -13,9 +13,9 @@ jobs: name: GitHub Pages runs-on: ubuntu-latest env: - MLIR_SYS_170_PREFIX: /usr/lib/llvm-17/ - LLVM_SYS_170_PREFIX: /usr/lib/llvm-17/ - TABLEGEN_170_PREFIX: /usr/lib/llvm-17/ + MLIR_SYS_180_PREFIX: /usr/lib/llvm-18/ + LLVM_SYS_180_PREFIX: /usr/lib/llvm-18/ + TABLEGEN_180_PREFIX: /usr/lib/llvm-18/ steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@1.76.0 @@ -23,11 +23,11 @@ jobs: - name: add llvm deb repository uses: myci-actions/add-deb-repo@11 with: - repo: deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-17 main + repo: deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main repo-name: llvm-repo keys-asc: https://apt.llvm.org/llvm-snapshot.gpg.key - name: Install LLVM - run: sudo apt-get install llvm-17 llvm-17-dev llvm-17-runtime clang-17 clang-tools-17 lld-17 libpolly-17-dev libmlir-17-dev mlir-17-tools + run: sudo apt-get install llvm-18 llvm-18-dev llvm-18-runtime clang-18 clang-tools-18 lld-18 libpolly-18-dev libmlir-18-dev mlir-18-tools - name: Build docs run: make doc - name: Deploy Documentation diff --git a/Cargo.lock b/Cargo.lock index 64775c2f0..62abb7359 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -289,40 +289,17 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.66.1" +version = "0.69.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2b84e06fc203107bfbad243f4aba2af864eb7db3b1cf46ea0a023b0b433d2a7" -dependencies = [ - "bitflags 2.5.0", - "cexpr", - "clang-sys", - "lazy_static", - "lazycell", - "log", - "peeking_take_while", - "prettyplease", - "proc-macro2", - "quote", - "regex", - "rustc-hash", - "shlex", - "syn 2.0.63", - "which", -] - -[[package]] -name = "bindgen" -version = "0.68.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "726e4313eb6ec35d2730258ad4e15b547ee75d6afaa1361a922e78e59b7d8078" +checksum = "a00dc851838a2120612785d195287475a3ac45514741da670b735818822129a0" dependencies = [ "bitflags 2.5.0", "cexpr", "clang-sys", + "itertools 0.12.1", "lazy_static", "lazycell", "log", - "peeking_take_while", "prettyplease", "proc-macro2", "quote", @@ -1124,13 +1101,14 @@ dependencies = [ [[package]] name = "comrak" -version = "0.22.0" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0436149c9f6a1935b13306206c739b1ba84fa81f551b5eb87fc2ca7a13700af" +checksum = "ab5681b26a36dc46800a908f7d932672a4e82f74759419dd4018bf40658457d5" dependencies = [ "clap", "derive_builder", "entities", + "in-place", "memchr", "once_cell", "regex", @@ -1901,6 +1879,15 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "in-place" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cdb69f3adfd5f493210cece799f4620417bf9965bc1536c22ae0e29ba27a8c0" +dependencies = [ + "tempfile", +] + [[package]] name = "indent" version = "0.1.1" @@ -2151,9 +2138,9 @@ checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" [[package]] name = "llvm-sys" -version = "170.0.1" +version = "180.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eed90f72df5504c0af2e3a08ee7762a4a3e42ec2605811fc19f64879de40c50a" +checksum = "778fa5fa02e32728e718f11eec147e6f134137399ab02fd2c13d32476337affa" dependencies = [ "anyhow", "cc", @@ -2208,9 +2195,9 @@ dependencies = [ [[package]] name = "melior" -version = "0.17.1" +version = "0.18.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e4eab7ad8af758cada048f6c5ca94b5bbe4837b1f998e0fb92b33373841a692" +checksum = "99339d2a34c0b7eba15d4c36085c3fa0414fc331e17c17f1676862f2ebf90eb1" dependencies = [ "dashmap", "melior-macro", @@ -2220,9 +2207,9 @@ dependencies = [ [[package]] name = "melior-macro" -version = "0.10.3" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b9fc9d57809ee6146f482b68555a605df4d5b018c5d5377d08a6aa99006f389" +checksum = "9a8253d8a5059e2d6b8321c3cf5f086d37a3f70a8edd57714dfa20d4d018f4e1" dependencies = [ "comrak", "convert_case", @@ -2231,7 +2218,7 @@ dependencies = [ "quote", "regex", "syn 2.0.63", - "tblgen", + "tblgen-alt", "unindent", ] @@ -2277,11 +2264,11 @@ dependencies = [ [[package]] name = "mlir-sys" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5e19a5391ed2759fd9060f538330b9b89191e7b13503d7499a4f9580af6699a" +checksum = "1b1b5806a63bc959cd5c4e5db8cadd20e40045d41969d325132748db8af11b7f" dependencies = [ - "bindgen 0.68.1", + "bindgen", ] [[package]] @@ -2553,12 +2540,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "17359afc20d7ab31fdb42bb844c8b3bb1dabd7dcf7e68428492da7f16966fcef" -[[package]] -name = "peeking_take_while" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" - [[package]] name = "pem-rfc7468" version = "0.7.0" @@ -3436,12 +3417,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" [[package]] -name = "tblgen" -version = "0.3.0" +name = "tblgen-alt" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d19c09266feb8b16718d1183044d14703a0b4b59e55ce8beb4d6e21dd066b1b" +checksum = "8128e684b18b518f32cd15779659c4a8dbb54ef6a36b532f7e3520f27dd01c95" dependencies = [ - "bindgen 0.66.1", + "bindgen", "cc", "paste", "thiserror", diff --git a/Cargo.toml b/Cargo.toml index 2a55a739e..35ca3a0f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,9 +48,9 @@ id-arena = "2.2" itertools = "0.12" lazy_static = "1.4" libc = "0.2.153" -llvm-sys = "170.0.0" -melior = { version = "0.17.1", features = ["ods-dialects"] } -mlir-sys = "=0.2.1" # 0.2.2 is llvm 18 +llvm-sys = "180.0.0" +melior = { version = "0.18.1", features = ["ods-dialects"] } +mlir-sys = "0.2.2" num-bigint = "0.4.4" num-traits = "0.2" starknet-types-core = { version = "0.1.1", default-features = false, features = [ @@ -69,7 +69,7 @@ cairo-lang-utils = "2.6.3" cairo-lang-starknet-classes = "2.6.3" cairo-native-runtime = { version = "0.1.0", path = "runtime", optional = true } clap = { version = "4.5", features = ["derive"], optional = true } -libloading = "0.8.1" +libloading = "0.8.3" tracing-subscriber = { version = "0.3", features = [ "env-filter", ], optional = true } @@ -99,7 +99,7 @@ walkdir = "2" serde_json = { version = "1.0" } [build-dependencies] -cc = "1.0.95" +cc = "1.0.96" [profile.optimized-dev] inherits = "dev" diff --git a/Dockerfile b/Dockerfile index 544ef8e4a..2401cf3e8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,14 +10,14 @@ RUN apt update -y && apt install -y lsb-release \ libz-dev \ libzstd-dev -# Install LLVM 17 -RUN echo "deb http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-17 main" > /etc/apt/sources.list.d/llvm-17.list -RUN echo "deb-src http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-17 main" >> /etc/apt/sources.list.d/llvm-17.list +# Install LLVM 18 +RUN echo "deb http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-18 main" > /etc/apt/sources.list.d/llvm-18.list +RUN echo "deb-src http://apt.llvm.org/bookworm/ llvm-toolchain-bookworm-18 main" >> /etc/apt/sources.list.d/llvm-18.list RUN wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add - -RUN apt update -y && apt install -y llvm-17 \ - libmlir-17-dev \ - mlir-17-tools \ - libpolly-17-dev +RUN apt update -y && apt install -y llvm-18 \ + libmlir-18-dev \ + mlir-18-tools \ + libpolly-18-dev # Install rust RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | bash -s -- -y @@ -28,8 +28,8 @@ COPY . /cairo_native/ # Compile cairo_native WORKDIR /cairo_native/ -ENV MLIR_SYS_170_PREFIX=/usr/lib/llvm-17 -ENV LLVM_SYS_170_PREFIX=/usr/lib/llvm-17 -ENV TABLEGEN_170_PREFIX=/usr/lib/llvm-17 +ENV MLIR_SYS_180_PREFIX=/usr/lib/llvm-18 +ENV LLVM_SYS_180_PREFIX=/usr/lib/llvm-18 +ENV TABLEGEN_180_PREFIX=/usr/lib/llvm-18 RUN make deps RUN make build diff --git a/Makefile b/Makefile index 2508927fe..d5161c37d 100644 --- a/Makefile +++ b/Makefile @@ -8,13 +8,13 @@ UNAME := $(shell uname) CAIRO_2_VERSION=2.6.3 check-llvm: -ifndef MLIR_SYS_170_PREFIX - $(error Could not find a suitable LLVM 17 toolchain (mlir), please set MLIR_SYS_170_PREFIX env pointing to the LLVM 17 dir) +ifndef MLIR_SYS_180_PREFIX + $(error Could not find a suitable LLVM 18 toolchain (mlir), please set MLIR_SYS_180_PREFIX env pointing to the LLVM 18 dir) endif -ifndef TABLEGEN_170_PREFIX - $(error Could not find a suitable LLVM 17 toolchain (tablegen), please set TABLEGEN_170_PREFIX env pointing to the LLVM 17 dir) +ifndef TABLEGEN_180_PREFIX + $(error Could not find a suitable LLVM 18 toolchain (tablegen), please set TABLEGEN_180_PREFIX env pointing to the LLVM 18 dir) endif - @echo "LLVM is correctly set at $(MLIR_SYS_170_PREFIX)." + @echo "LLVM is correctly set at $(MLIR_SYS_180_PREFIX)." needs-cairo2: ifeq ($(wildcard ./cairo2/.),) @@ -100,7 +100,7 @@ endif -ln -s cairo2/corelib corelib deps-macos: build-cairo-2-compiler-macos install-scarb-macos - -brew install llvm@17 --quiet + -brew install llvm@18 --quiet @echo "You can execute the env-macos.sh script to setup the needed env variables." cairo-repo-2-dir = cairo2 diff --git a/README.md b/README.md index 25e0ccfa8..aac890735 100644 --- a/README.md +++ b/README.md @@ -261,8 +261,8 @@ Footnotes on the libfuncs list: ### Dependencies - Linux or macOS (aarch64 included) only for now -- LLVM 17 with MLIR: On debian you can use [apt.llvm.org](https://apt.llvm.org/), on macOS you can use brew -- Nightly Rust +- LLVM 18 with MLIR: On debian you can use [apt.llvm.org](https://apt.llvm.org/), on macOS you can use brew +- Rust 1.76.0 - Git ### Setup @@ -277,13 +277,13 @@ make deps #### Linux -Since Linux distributions change widely, you need to install LLVM 17 via your package manager, compile it or check if the current release has a Linux binary. +Since Linux distributions change widely, you need to install LLVM 18 via your package manager, compile it or check if the current release has a Linux binary. If you are on Debian/Ubuntu, check out the repository https://apt.llvm.org/ Then you can install with: ```bash -sudo apt-get install llvm-17 llvm-17-dev llvm-17-runtime clang-17 clang-tools-17 lld-17 libpolly-17-dev libmlir-17-dev mlir-17-tools +sudo apt-get install llvm-18 llvm-18-dev llvm-18-runtime clang-18 clang-tools-18 lld-18 libpolly-18-dev libmlir-18-dev mlir-18-tools ``` If you decide to build from source, here are some indications: @@ -292,23 +292,23 @@ If you decide to build from source, here are some indications: ```bash # Go to https://github.com/llvm/llvm-project/releases -# Download the latest LLVM 17 release: -# The blob to download is called llvm-project-17.x.x.src.tar.xz +# Download the latest LLVM 18 release: +# The blob to download is called llvm-project-18.x.x.src.tar.xz # For example -wget https://github.com/llvm/llvm-project/releases/download/llvmorg-17.0.3/llvm-project-17.0.3.src.tar.xz -tar xf llvm-project-17.0.3.src.tar.xz +wget https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.4/llvm-project-18.1.4.src.tar.xz +tar xf llvm-project-18.1.4.src.tar.xz -cd llvm-project-17.0.3.src.tar +cd llvm-project-18.1.4.src.tar mkdir build cd build -# The following cmake command configures the build to be installed to /opt/llvm-17 +# The following cmake command configures the build to be installed to /opt/llvm-18 cmake -G Ninja ../llvm \ -DLLVM_ENABLE_PROJECTS="mlir;clang;clang-tools-extra;lld;polly" \ -DLLVM_BUILD_EXAMPLES=OFF \ -DLLVM_TARGETS_TO_BUILD="Native" \ - -DCMAKE_INSTALL_PREFIX=/opt/llvm-17 \ + -DCMAKE_INSTALL_PREFIX=/opt/llvm-18 \ -DCMAKE_BUILD_TYPE=RelWithDebInfo \ -DLLVM_PARALLEL_LINK_JOBS=4 \ -DLLVM_ENABLE_BINDINGS=OFF \ @@ -320,13 +320,13 @@ ninja install -Setup a environment variable called `MLIR_SYS_170_PREFIX`, `LLVM_SYS_170_PREFIX` and `TABLEGEN_170_PREFIX` pointing to the llvm directory: +Setup a environment variable called `MLIR_SYS_180_PREFIX`, `LLVM_SYS_180_PREFIX` and `TABLEGEN_180_PREFIX` pointing to the llvm directory: ```bash -# For Debian/Ubuntu using the repository, the path will be /usr/lib/llvm-17 -export MLIR_SYS_170_PREFIX=/usr/lib/llvm-17 -export LLVM_SYS_170_PREFIX=/usr/lib/llvm-17 -export TABLEGEN_170_PREFIX=/usr/lib/llvm-17 +# For Debian/Ubuntu using the repository, the path will be /usr/lib/llvm-18 +export MLIR_SYS_180_PREFIX=/usr/lib/llvm-18 +export LLVM_SYS_180_PREFIX=/usr/lib/llvm-18 +export TABLEGEN_180_PREFIX=/usr/lib/llvm-18 ``` Run the deps target to install the other dependencies such as the cairo compiler (for tests, benchmarks). @@ -337,7 +337,7 @@ make deps #### MacOS -The makefile `deps` target (which you should have ran before) installs LLVM 17 with brew for you, afterwards you need to execute the `env-macos.sh` script to setup the +The makefile `deps` target (which you should have ran before) installs LLVM 18 with brew for you, afterwards you need to execute the `env-macos.sh` script to setup the needed environment variables. ```bash @@ -348,7 +348,7 @@ source env-macos.sh Running `make` by itself will list available targets. -- Install the necessary dependencies (on Linux, you need to get LLVM 17 manually): +- Install the necessary dependencies (on Linux, you need to get LLVM 18 manually): ```bash make deps @@ -711,9 +711,9 @@ For more examples, check out the `examples/` directory. You need to setup some environment variables: ```bash -$MLIR_SYS_170_PREFIX=/path/to/llvm17 # Required for non-standard LLVM install locations. -$LLVM_SYS_170_PREFIX=/path/to/llvm17 # Required for non-standard LLVM install locations. -$TABLEGEN_170_PREFIX=/path/to/llvm17 # Required for non-standard LLVM install locations. +$MLIR_SYS_180_PREFIX=/path/to/llvm18 # Required for non-standard LLVM install locations. +$LLVM_SYS_180_PREFIX=/path/to/llvm18 # Required for non-standard LLVM install locations. +$TABLEGEN_180_PREFIX=/path/to/llvm18 # Required for non-standard LLVM install locations. ``` ```bash @@ -733,7 +733,7 @@ If you want the benchmarks to run using a specific build, or the `cairo-run` com sierra2mlir program.sierra -o program.mlir # translate all dialects to the llvm dialect -"$MLIR_SYS_170_PREFIX/bin/mlir-opt" \ +"$MLIR_SYS_180_PREFIX/bin/mlir-opt" \ --canonicalize \ --convert-scf-to-cf \ --canonicalize \ @@ -747,13 +747,13 @@ sierra2mlir program.sierra -o program.mlir -o "program-llvm.mlir" # translate mlir to llvm-ir -"$MLIR_SYS_170_PREFIX"/bin/mlir-translate --mlir-to-llvmir program-llvm.mlir -o program.ll +"$MLIR_SYS_180_PREFIX"/bin/mlir-translate --mlir-to-llvmir program-llvm.mlir -o program.ll # compile natively -"$MLIR_SYS_170_PREFIX"/bin/clang program.ll -Wno-override-module \ - -L "$MLIR_SYS_170_PREFIX"/lib -L"./target/release/" \ +"$MLIR_SYS_180_PREFIX"/bin/clang program.ll -Wno-override-module \ + -L "$MLIR_SYS_180_PREFIX"/lib -L"./target/release/" \ -lsierra2mlir_utils -lmlir_c_runner_utils \ - -Wl,-rpath "$MLIR_SYS_170_PREFIX"/lib \ + -Wl,-rpath "$MLIR_SYS_180_PREFIX"/lib \ -Wl,-rpath ./target/release/ \ -o program diff --git a/build.rs b/build.rs index 3355a392b..8449c8ee8 100644 --- a/build.rs +++ b/build.rs @@ -1,7 +1,7 @@ use std::env::var; fn main() { - let mlir_path = var("MLIR_SYS_170_PREFIX").expect("MLIR path should be set."); + let mlir_path = var("MLIR_SYS_180_PREFIX").expect("MLIR path should be set."); cc::Build::new() .cpp(true) diff --git a/env-macos.sh b/env-macos.sh index 5472764bb..01feda302 100644 --- a/env-macos.sh +++ b/env-macos.sh @@ -3,12 +3,12 @@ # This script is only useful on macOS using brew. # It sets the LLVM environment variables. export LIBRARY_PATH=/opt/homebrew/lib -MLIR_SYS_170_PREFIX="$(brew --prefix llvm@17)" -LLVM_SYS_170_PREFIX="$(brew --prefix llvm@17)" -TABLEGEN_170_PREFIX="$(brew --prefix llvm@17)" +MLIR_SYS_180_PREFIX="$(brew --prefix llvm@18)" +LLVM_SYS_180_PREFIX="$(brew --prefix llvm@18)" +TABLEGEN_180_PREFIX="$(brew --prefix llvm@18)" CAIRO_NATIVE_RUNTIME_LIBDIR="$(pwd)/target/debug" -export MLIR_SYS_170_PREFIX -export LLVM_SYS_170_PREFIX -export TABLEGEN_170_PREFIX +export MLIR_SYS_180_PREFIX +export LLVM_SYS_180_PREFIX +export TABLEGEN_180_PREFIX export CAIRO_NATIVE_RUNTIME_LIBDIR diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 907951458..936613c1f 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] -channel = "1.76.0" +channel = "1.78.0" components = ["rustfmt", "clippy"] profile = "minimal" diff --git a/scripts/bench-hyperfine.sh b/scripts/bench-hyperfine.sh index 5d5789552..24b8c8348 100755 --- a/scripts/bench-hyperfine.sh +++ b/scripts/bench-hyperfine.sh @@ -2,7 +2,7 @@ # Configuration. ROOT_DIR="$(dirname "$(dirname "${0%/*}")")" -MLIR_DIR="$MLIR_SYS_170_PREFIX" +MLIR_DIR="$MLIR_SYS_180_PREFIX" CAIRO_SRCS=$(find \ "$ROOT_DIR/programs/benches" \ @@ -25,7 +25,7 @@ then fi if [[ -z "$MLIR_DIR" ]] then - echo "${bold}MLIR_DIR is empty. Did you forget to set MLIR_SYS_160_PREFIX?${normal}" + echo "${bold}MLIR_DIR is empty. Did you forget to set MLIR_SYS_180_PREFIX?${normal}" exit 1 fi diff --git a/src/block_ext.rs b/src/block_ext.rs index 658941b0b..02a540e86 100644 --- a/src/block_ext.rs +++ b/src/block_ext.rs @@ -1,7 +1,7 @@ //! Trait that extends the melior Block type to aid in codegen and consistency. use melior::{ - dialect::{llvm::r#type::opaque_pointer, ods}, + dialect::{llvm::r#type::pointer, ods}, ir::{ attribute::{DenseI64ArrayAttribute, IntegerAttribute, TypeAttribute}, r#type::IntegerType, @@ -11,7 +11,7 @@ use melior::{ }; use num_bigint::BigInt; -use crate::error::Error; +use crate::{error::Error, utils::get_integer_layout}; pub trait BlockExt<'ctx> { /// Appends the operation and returns the first result. @@ -99,6 +99,14 @@ pub trait BlockExt<'ctx> { align: Option, ) -> Result, Error>; + /// Allocates one integer of the given bit width. + fn alloca_int( + &self, + context: &'ctx Context, + location: Location<'ctx>, + bits: u32, + ) -> Result, Error>; + /// Stores a value at the given addr. fn store( &self, @@ -307,7 +315,13 @@ impl<'ctx> BlockExt<'ctx> for Block<'ctx> { num_elems: Value<'ctx, '_>, align: Option, ) -> Result, Error> { - let mut op = ods::llvm::alloca(context, opaque_pointer(context), num_elems, location); + let mut op = ods::llvm::alloca( + context, + pointer(context, 0), + num_elems, + TypeAttribute::new(elem_type), + location, + ); op.set_elem_type(TypeAttribute::new(elem_type)); @@ -331,4 +345,20 @@ impl<'ctx> BlockExt<'ctx> for Block<'ctx> { let num_elems = self.const_int(context, location, 1, 64)?; self.alloca(context, location, elem_type, num_elems, align) } + + fn alloca_int( + &self, + context: &'ctx Context, + location: Location<'ctx>, + bits: u32, + ) -> Result, Error> { + let num_elems = self.const_int(context, location, 1, 64)?; + self.alloca( + context, + location, + IntegerType::new(context, bits).into(), + num_elems, + Some(get_integer_layout(bits).align()), + ) + } } diff --git a/src/compiler.rs b/src/compiler.rs index e2c954308..2fdd2a728 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -182,7 +182,7 @@ fn compile_func( })) { if type_info.is_memory_allocated(registry) { - *ty = llvm::r#type::opaque_pointer(context); + *ty = llvm::r#type::pointer(context, 0); } } @@ -214,7 +214,7 @@ fn compile_func( assert_eq!(return_types.len(), 1); return_types.remove(0); - arg_types.insert(0, llvm::r#type::opaque_pointer(context)); + arg_types.insert(0, llvm::r#type::pointer(context, 0)); Some(true) } else { diff --git a/src/executor.rs b/src/executor.rs index 34a7b0793..580f71ed9 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -357,24 +357,26 @@ impl<'a> ArgumentMapper<'a> { &self.invoke_data } - #[cfg_attr(target_arch = "x86_64", allow(unused_mut))] pub fn push_aligned(&mut self, align: usize, mut values: &[u64]) { assert!(align.is_power_of_two()); assert!(align <= 16); - // x86_64's max alignment is 8 bytes. #[cfg(target_arch = "x86_64")] - assert!(align <= 8); + const NUM_REGISTER_ARGS: usize = 6; + #[cfg(not(target_arch = "x86_64"))] + const NUM_REGISTER_ARGS: usize = 8; - #[cfg(target_arch = "aarch64")] if align == 16 { // This works because on both aarch64 and x86_64 the stack is already aligned to // 16 bytes when the trampoline starts pushing values. - if self.invoke_data.len() >= 8 { + + // Whenever a value spans across multiple registers, if it's in a position where it would be split between + // registers and the stack it must be padded so that the entire value is stored within the stack. + if self.invoke_data.len() >= NUM_REGISTER_ARGS { if self.invoke_data.len() & 1 != 0 { self.invoke_data.push(0); } - } else if self.invoke_data.len() + 1 >= 8 { + } else if self.invoke_data.len() + 1 >= NUM_REGISTER_ARGS { self.invoke_data.push(0); } else { let new_len = self.invoke_data.len() + values.len(); @@ -619,7 +621,9 @@ fn parse_result( .align_offset(layout.align()); *return_ptr = unsafe { - NonNull::new_unchecked(return_ptr.cast::().as_ptr().add(align_offset)).cast() + NonNull::new(return_ptr.cast::().as_ptr().add(align_offset)) + .expect("nonnull is null") + .cast() }; } diff --git a/src/executor/aot.rs b/src/executor/aot.rs index d5c640aa0..d50115d89 100644 --- a/src/executor/aot.rs +++ b/src/executor/aot.rs @@ -247,7 +247,7 @@ mod tests { tag: 0, value: JitValue::Struct { fields: vec![JitValue::Felt252( - syscall_handler.get_block_hash(0, &mut 0).unwrap(), + syscall_handler.get_block_hash(1, &mut 0).unwrap(), )], debug_name: Some("Tuple".into()), } diff --git a/src/libfuncs.rs b/src/libfuncs.rs index 2d28076f1..a4210e2b3 100644 --- a/src/libfuncs.rs +++ b/src/libfuncs.rs @@ -569,7 +569,7 @@ mod tests { // Push a default block and external operand to the branches vector lib_func_helper .branches - .push((&default_block, vec![BranchArg::External(operand)])); + .push((default_block, vec![BranchArg::External(operand)])); // Push a new vector of result cells to the results vector lib_func_helper.results.push([Cell::new(None)].into()); @@ -645,7 +645,7 @@ mod tests { // Push a default block and a returned operand index to the branches vector lib_func_helper .branches - .push((&default_block, vec![BranchArg::Returned(3)])); + .push((default_block, vec![BranchArg::Returned(3)])); // Push a new vector of result cells to the results vector lib_func_helper.results.push([Cell::new(None)].into()); @@ -728,7 +728,7 @@ mod tests { // Loop to add branches and results to the LibfuncHelper struct for _ in 0..20 { // Push a default block and an empty vector of operands to the branches vector - lib_func_helper.branches.push((&default_block, Vec::new())); + lib_func_helper.branches.push((default_block, Vec::new())); // Push a new vector of result cells to the results vector lib_func_helper.results.push([Cell::new(None)].into()); diff --git a/src/libfuncs/array.rs b/src/libfuncs/array.rs index 904a12725..89dce4676 100644 --- a/src/libfuncs/array.rs +++ b/src/libfuncs/array.rs @@ -24,7 +24,7 @@ use melior::{ dialect::{ arith::{self, CmpiPredicate}, cf, - llvm::{self, r#type::opaque_pointer}, + llvm::{self, r#type::pointer}, ods, }, ir::{ @@ -98,10 +98,7 @@ pub fn build_new<'ctx, 'this>( )?; let ptr = entry - .append_operation(llvm::nullptr( - crate::ffi::get_struct_field_type_at(&array_ty, 0), - location, - )) + .append_operation(ods::llvm::mlir_zero(context, pointer(context, 0), location).into()) .result(0)? .into(); let k0 = entry @@ -282,7 +279,7 @@ pub fn build_append<'ctx, 'this>( dst_ptr, &[start_offset], IntegerType::new(context, 8).into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -419,7 +416,7 @@ pub fn build_append<'ctx, 'this>( ptr, &[offset], IntegerType::new(context, 8).into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -587,7 +584,7 @@ pub fn build_get<'ctx, 'this>( ptr, &[elem_offset], IntegerType::new(context, 8).into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -596,10 +593,7 @@ pub fn build_get<'ctx, 'this>( let elem_size = valid_block.const_int(context, location, elem_layout.size(), 64)?; let target_ptr = valid_block - .append_operation(llvm::nullptr( - llvm::r#type::opaque_pointer(context), - location, - )) + .append_operation(ods::llvm::mlir_zero(context, pointer(context, 0), location).into()) .result(0)? .into(); let target_ptr = valid_block @@ -704,17 +698,14 @@ pub fn build_pop_front<'ctx, 'this>( ptr, &[elem_offset], IntegerType::new(context, 8).into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, )) .result(0)? .into(); let target_ptr = valid_block - .append_operation(llvm::nullptr( - llvm::r#type::opaque_pointer(context), - location, - )) + .append_operation(ods::llvm::mlir_zero(context, pointer(context, 0), location).into()) .result(0)? .into(); let target_ptr = valid_block @@ -857,17 +848,14 @@ pub fn build_snapshot_pop_back<'ctx, 'this>( ptr, &[elem_offset], IntegerType::new(context, 8).into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, )) .result(0)? .into(); let target_ptr = valid_block - .append_operation(llvm::nullptr( - llvm::r#type::opaque_pointer(context), - location, - )) + .append_operation(ods::llvm::mlir_zero(context, pointer(context, 0), location).into()) .result(0)? .into(); let target_ptr = valid_block @@ -997,10 +985,9 @@ pub fn build_slice<'ctx, 'this>( .into(); let dst_size = slice_block.append_op_result(arith::muli(dst_size, elem_size, location))?; - let dst_ptr = slice_block.append_op_result(llvm::nullptr( - llvm::r#type::opaque_pointer(context), - location, - ))?; + let dst_ptr = slice_block.append_op_result( + ods::llvm::mlir_zero(context, pointer(context, 0), location).into(), + )?; let dst_ptr = slice_block.append_op_result(ReallocBindingsMeta::realloc( context, dst_ptr, dst_size, location, ))?; @@ -1020,7 +1007,7 @@ pub fn build_slice<'ctx, 'this>( context, location, entry.argument(1)?.into(), - opaque_pointer(context), + pointer(context, 0), 0, )?; let src_ptr = slice_block.append_op_result(llvm::get_element_ptr_dynamic( @@ -1028,7 +1015,7 @@ pub fn build_slice<'ctx, 'this>( src_ptr, &[src_offset], IntegerType::new(context, 8).into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, ))?; @@ -1108,10 +1095,8 @@ pub fn build_span_from_tuple<'ctx, 'this>( let array_container = entry.insert_value(context, location, array_container, array_len_value, 3)?; - let opaque_ptr_ty = opaque_pointer(context); - let ptr = entry - .append_operation(llvm::nullptr(opaque_ptr_ty, location)) + .append_operation(ods::llvm::mlir_zero(context, pointer(context, 0), location).into()) .result(0)? .into(); @@ -1141,7 +1126,7 @@ pub fn build_span_from_tuple<'ctx, 'this>( ptr, DenseI32ArrayAttribute::new(context, &[i as i32]), field_ty, - opaque_pointer(context), + pointer(context, 0), location, )) .result(0)? diff --git a/src/libfuncs/box.rs b/src/libfuncs/box.rs index f1af6b1f8..b0c86ffab 100644 --- a/src/libfuncs/box.rs +++ b/src/libfuncs/box.rs @@ -22,7 +22,8 @@ use cairo_lang_sierra::{ use melior::{ dialect::{ arith, - llvm::{self, r#type::opaque_pointer, LoadStoreOptions}, + llvm::{self, r#type::pointer, LoadStoreOptions}, + ods, }, ir::{attribute::IntegerAttribute, r#type::IntegerType, Block, Location}, Context, @@ -82,7 +83,7 @@ pub fn build_into_box<'ctx, 'this>( .into(); let ptr = entry - .append_operation(llvm::nullptr(opaque_pointer(context), location)) + .append_operation(ods::llvm::mlir_zero(context, pointer(context, 0), location).into()) .result(0)? .into(); let ptr = entry diff --git a/src/libfuncs/const_libfunc.rs b/src/libfuncs/const_libfunc.rs index 5828605c6..98dfd31c0 100644 --- a/src/libfuncs/const_libfunc.rs +++ b/src/libfuncs/const_libfunc.rs @@ -24,7 +24,7 @@ use cairo_lang_sierra::{ use melior::{ dialect::{ arith, - llvm::{self, r#type::opaque_pointer, LoadStoreOptions}, + llvm::{self, r#type::pointer, LoadStoreOptions}, }, ir::{attribute::IntegerAttribute, r#type::IntegerType, Attribute, Block, Location, Value}, Context, @@ -96,7 +96,7 @@ pub fn build_const_as_box<'ctx, 'this>( .into(); let ptr = entry - .append_operation(llvm::nullptr(opaque_pointer(context), location)) + .append_operation(llvm::zero(pointer(context, 0), location)) .result(0)? .into(); let ptr = entry diff --git a/src/libfuncs/debug.rs b/src/libfuncs/debug.rs index 820f54e15..8401d8642 100644 --- a/src/libfuncs/debug.rs +++ b/src/libfuncs/debug.rs @@ -71,7 +71,7 @@ pub fn build_print<'ctx>( context, entry.argument(0)?.into(), DenseI64ArrayAttribute::new(context, &[0]), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -122,7 +122,7 @@ pub fn build_print<'ctx>( values_ptr, &[values_start], IntegerType::new(context, 252).into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, )) .result(0)? diff --git a/src/libfuncs/ec.rs b/src/libfuncs/ec.rs index 3ee253e4b..6c07d0ad3 100644 --- a/src/libfuncs/ec.rs +++ b/src/libfuncs/ec.rs @@ -24,10 +24,7 @@ use melior::{ arith::{self, CmpiPredicate}, llvm, }, - ir::{ - attribute::IntegerAttribute, operation::OperationBuilder, r#type::IntegerType, Block, - Identifier, Location, - }, + ir::{operation::OperationBuilder, r#type::IntegerType, Block, Location}, Context, }; use num_bigint::{BigInt, ToBigInt}; @@ -192,21 +189,11 @@ pub fn build_point_from_x<'ctx, 'this>( false, ); - let k1 = helper.init_block().const_int(context, location, 1, 64)?; - - let point_ptr = entry.append_op_result( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(ec_point_ty, 0)]) - .build()?, + let point_ptr = helper.init_block().alloca1( + context, + location, + ec_point_ty, + Some(get_integer_layout(252).align()), )?; let point = entry.append_op_result(llvm::undef(ec_point_ty, location))?; @@ -252,44 +239,18 @@ pub fn build_state_add<'ctx, 'this>( ], false, ); - let ec_point_ty = llvm::r#type::r#struct( - context, - &[ - IntegerType::new(context, 252).into(), - IntegerType::new(context, 252).into(), - ], - false, - ); - - let k1 = helper.init_block().const_int(context, location, 1, 64)?; - let state_ptr = entry.append_op_result( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(ec_state_ty, 0)]) - .build()?, + let state_ptr = helper.init_block().alloca1( + context, + location, + ec_state_ty, + Some(get_integer_layout(252).align()), )?; - let point_ptr = entry.append_op_result( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(ec_point_ty, 0)]) - .build()?, + let point_ptr = helper.init_block().alloca1( + context, + location, + ec_state_ty, + Some(get_integer_layout(252).align()), )?; entry.store( @@ -340,49 +301,23 @@ pub fn build_state_add_mul<'ctx, 'this>( ); let ec_point_ty = llvm::r#type::r#struct(context, &[felt252_ty, felt252_ty], false); - let k1 = helper.init_block().const_int(context, location, 1, 64)?; - - let state_ptr = entry.append_op_result( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(ec_state_ty, 0)]) - .build()?, + let state_ptr = helper.init_block().alloca1( + context, + location, + ec_state_ty, + Some(get_integer_layout(252).align()), )?; - let scalar_ptr = entry.append_op_result( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(felt252_ty, 0)]) - .build()?, + let scalar_ptr = helper.init_block().alloca1( + context, + location, + felt252_ty, + Some(get_integer_layout(252).align()), )?; - let point_ptr = entry.append_op_result( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(ec_point_ty, 0)]) - .build()?, + let point_ptr = helper.init_block().alloca1( + context, + location, + ec_point_ty, + Some(get_integer_layout(252).align()), )?; entry.store( @@ -438,35 +373,17 @@ pub fn build_state_finalize<'ctx, 'this>( ); let ec_point_ty = llvm::r#type::r#struct(context, &[felt252_ty, felt252_ty], false); - let k1 = helper.init_block().const_int(context, location, 1, 64)?; - - let point_ptr = entry.append_op_result( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(ec_point_ty, 0)]) - .build()?, + let point_ptr = helper.init_block().alloca1( + context, + location, + ec_point_ty, + Some(get_integer_layout(252).align()), )?; - let state_ptr = entry.append_op_result( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(ec_state_ty, 0)]) - .build()?, + let state_ptr = helper.init_block().alloca1( + context, + location, + ec_state_ty, + Some(get_integer_layout(252).align()), )?; entry.store( @@ -561,21 +478,11 @@ pub fn build_try_new<'ctx, 'this>( false, ); - let k1 = helper.init_block().const_int(context, location, 1, 64)?; - - let point_ptr = entry.append_op_result( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer(ec_point_ty, 0)]) - .build()?, + let point_ptr = helper.init_block().alloca1( + context, + location, + ec_point_ty, + Some(get_integer_layout(252).align()), )?; let point = entry.append_op_result(llvm::undef(ec_point_ty, location))?; diff --git a/src/libfuncs/enum.rs b/src/libfuncs/enum.rs index 528419a30..5a12df0c4 100644 --- a/src/libfuncs/enum.rs +++ b/src/libfuncs/enum.rs @@ -186,7 +186,7 @@ pub fn build_enum_value<'ctx, 'this>( .append_operation(llvm::alloca( context, k1, - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, AllocaOptions::new() .align(Some(IntegerAttribute::new( diff --git a/src/libfuncs/felt252.rs b/src/libfuncs/felt252.rs index 6a77664e2..289456336 100644 --- a/src/libfuncs/felt252.rs +++ b/src/libfuncs/felt252.rs @@ -473,8 +473,15 @@ pub mod test { lazy_static! { static ref FELT252_ADD: (String, Program) = load_cairo! { + use core::debug::PrintTrait; fn run_test(lhs: felt252, rhs: felt252) -> felt252 { - lhs + rhs + lhs.print(); + rhs.print(); + let result = lhs + rhs; + + result.print(); + + result } }; @@ -513,28 +520,6 @@ pub mod test { }; } - #[test] - fn felt252_add_new() { - run_program_assert_output( - &FELT252_ADD, - "run_test", - &[JitValue::felt_str("0"), JitValue::felt_str("0")], - JitValue::felt_str("0"), - ); - run_program_assert_output( - &FELT252_ADD, - "run_test", - &[JitValue::felt_str("0"), JitValue::felt_str("1")], - JitValue::felt_str("1"), - ); - run_program_assert_output( - &FELT252_ADD, - "run_test", - &[JitValue::felt_str("0"), JitValue::felt_str("0")], - JitValue::felt_str("0"), - ); - } - #[test] fn felt252_add() { run_program_assert_output( @@ -546,14 +531,8 @@ pub mod test { run_program_assert_output( &FELT252_ADD, "run_test", - &[JitValue::felt_str("0"), JitValue::felt_str("1")], - JitValue::felt_str("1"), - ); - run_program_assert_output( - &FELT252_ADD, - "run_test", - &[JitValue::felt_str("0"), JitValue::felt_str("0")], - JitValue::felt_str("0"), + &[JitValue::felt_str("1"), JitValue::felt_str("2")], + JitValue::felt_str("3"), ); fn r(lhs: JitValue, rhs: JitValue) -> JitValue { diff --git a/src/libfuncs/felt252_dict_entry.rs b/src/libfuncs/felt252_dict_entry.rs index c99a1eb07..d5b803473 100644 --- a/src/libfuncs/felt252_dict_entry.rs +++ b/src/libfuncs/felt252_dict_entry.rs @@ -23,7 +23,7 @@ use cairo_lang_sierra::{ use melior::{ dialect::{ cf, - llvm::{self, r#type::opaque_pointer}, + llvm::{self}, }, ir::{ attribute::IntegerAttribute, operation::OperationBuilder, r#type::IntegerType, Block, @@ -113,7 +113,7 @@ pub fn build_get<'ctx, 'this>( let result_ptr: Value = op.result(0)?.into(); let null_ptr = entry.append_op_result( - OperationBuilder::new("llvm.mlir.null", location) + OperationBuilder::new("llvm.mlir.zero", location) .add_results(&[result_ptr.r#type()]) .build()?, )?; @@ -133,7 +133,7 @@ pub fn build_get<'ctx, 'this>( let block_is_null = helper.append_block(Block::new(&[])); let block_is_found = helper.append_block(Block::new(&[])); let block_final = helper.append_block(Block::new(&[ - (opaque_pointer(context), location), + (llvm::r#type::pointer(context, 0), location), (value_ty, location), ])); @@ -224,11 +224,21 @@ pub fn build_finalize<'ctx, 'this>( let key_value = entry.extract_value(context, location, entry_value, key_ty, 0)?; - let value_ptr = - entry.extract_value(context, location, entry_value, opaque_pointer(context), 1)?; + let value_ptr = entry.extract_value( + context, + location, + entry_value, + llvm::r#type::pointer(context, 0), + 1, + )?; - let dict_ptr = - entry.extract_value(context, location, entry_value, opaque_pointer(context), 2)?; + let dict_ptr = entry.extract_value( + context, + location, + entry_value, + llvm::r#type::pointer(context, 0), + 2, + )?; entry.store( context, diff --git a/src/libfuncs/function_call.rs b/src/libfuncs/function_call.rs index 644d6c95e..87a01adf9 100644 --- a/src/libfuncs/function_call.rs +++ b/src/libfuncs/function_call.rs @@ -228,7 +228,7 @@ pub fn build<'ctx, 'this>( val, DenseI32ArrayAttribute::new(context, &[offset as i32]), IntegerType::new(context, 8).into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, )) .result(0)? diff --git a/src/libfuncs/nullable.rs b/src/libfuncs/nullable.rs index e8edd72f6..603f3ea24 100644 --- a/src/libfuncs/nullable.rs +++ b/src/libfuncs/nullable.rs @@ -3,7 +3,7 @@ //! Like a Box but it can be null. use super::LibfuncHelper; -use crate::{error::Result, metadata::MetadataStorage, utils::ProgramRegistryExt}; +use crate::{error::Result, metadata::MetadataStorage}; use cairo_lang_sierra::{ extensions::{ core::{CoreLibfunc, CoreType}, @@ -12,15 +12,11 @@ use cairo_lang_sierra::{ SignatureOnlyConcreteLibfunc, }, nullable::NullableConcreteLibfunc, - ConcreteLibfunc, }, program_registry::ProgramRegistry, }; use melior::{ - dialect::{ - cf, - llvm::{self, r#type::opaque_pointer}, - }, + dialect::{cf, llvm::r#type::pointer, ods}, ir::{ attribute::IntegerAttribute, operation::OperationBuilder, r#type::IntegerType, Block, Identifier, Location, @@ -65,7 +61,8 @@ fn build_null<'ctx, 'this>( _metadata: &mut MetadataStorage, _info: &SignatureOnlyConcreteLibfunc, ) -> Result<()> { - let op = entry.append_operation(llvm::nullptr(opaque_pointer(context), location)); + let op = + entry.append_operation(ods::llvm::mlir_zero(context, pointer(context, 0), location).into()); entry.append_operation(helper.br(0, &[op.result(0)?.into()], location)); @@ -92,24 +89,17 @@ fn build_nullable_from_box<'ctx, 'this>( #[allow(clippy::too_many_arguments)] fn build_match_nullable<'ctx, 'this>( context: &'ctx Context, - registry: &ProgramRegistry, + _registry: &ProgramRegistry, entry: &'this Block<'ctx>, location: Location<'ctx>, helper: &LibfuncHelper<'ctx, 'this>, - metadata: &mut MetadataStorage, - info: &SignatureAndTypeConcreteLibfunc, + _metadata: &mut MetadataStorage, + _info: &SignatureAndTypeConcreteLibfunc, ) -> Result<()> { - let param_ty = registry.build_type( - context, - helper, - registry, - metadata, - &info.param_signatures()[0].ty, - )?; - let arg = entry.argument(0)?.into(); - let op = entry.append_operation(llvm::nullptr(param_ty, location)); + let op = + entry.append_operation(ods::llvm::mlir_zero(context, pointer(context, 0), location).into()); let nullptr = op.result(0)?.into(); let op = entry.append_operation( diff --git a/src/libfuncs/pedersen.rs b/src/libfuncs/pedersen.rs index c4bb269ed..67ab9435d 100644 --- a/src/libfuncs/pedersen.rs +++ b/src/libfuncs/pedersen.rs @@ -23,10 +23,7 @@ use melior::{ llvm::{self, LoadStoreOptions}, ods, }, - ir::{ - attribute::IntegerAttribute, operation::OperationBuilder, r#type::IntegerType, Block, - Identifier, Location, - }, + ir::{attribute::IntegerAttribute, r#type::IntegerType, Block, Location}, Context, }; @@ -71,8 +68,6 @@ pub fn build_pedersen<'ctx>( &info.param_signatures()[1].ty, )?; - let i64_ty = IntegerType::new(context, 64).into(); - let i256_ty = IntegerType::new(context, 256).into(); let layout_i256 = get_integer_layout(256); @@ -81,52 +76,18 @@ pub fn build_pedersen<'ctx>( // We must extend to i256 because bswap must be an even number of bytes. - let const_1 = entry.const_int_from_type(context, location, 1, i64_ty)?; - - let lhs_ptr = entry.append_op_result( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - layout_i256.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[const_1]) - .add_results(&[llvm::r#type::pointer(i256_ty, 0)]) - .build()?, - )?; - - let rhs_ptr = entry.append_op_result( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - layout_i256.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[const_1]) - .add_results(&[llvm::r#type::pointer(i256_ty, 0)]) - .build()?, - )?; - - let dst_ptr = entry.append_op_result( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - layout_i256.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[const_1]) - .add_results(&[llvm::r#type::pointer(i256_ty, 0)]) - .build()?, - )?; + let lhs_ptr = + helper + .init_block() + .alloca1(context, location, i256_ty, Some(layout_i256.align()))?; + let rhs_ptr = + helper + .init_block() + .alloca1(context, location, i256_ty, Some(layout_i256.align()))?; + let dst_ptr = + helper + .init_block() + .alloca1(context, location, i256_ty, Some(layout_i256.align()))?; let lhs_i256 = entry.append_op_result(arith::extui(lhs, i256_ty, location))?; let rhs_i256 = entry.append_op_result(arith::extui(rhs, i256_ty, location))?; diff --git a/src/libfuncs/poseidon.rs b/src/libfuncs/poseidon.rs index b4b860a7a..f19da482e 100644 --- a/src/libfuncs/poseidon.rs +++ b/src/libfuncs/poseidon.rs @@ -3,6 +3,7 @@ use super::LibfuncHelper; use crate::{ + block_ext::BlockExt, error::Result, metadata::{runtime_bindings::RuntimeBindingsMeta, MetadataStorage}, utils::{get_integer_layout, ProgramRegistryExt}, @@ -22,10 +23,7 @@ use melior::{ llvm::{self, LoadStoreOptions}, ods, }, - ir::{ - attribute::IntegerAttribute, operation::OperationBuilder, r#type::IntegerType, Block, - Identifier, Location, - }, + ir::{attribute::IntegerAttribute, r#type::IntegerType, Block, Location}, Context, }; @@ -79,66 +77,18 @@ pub fn build_hades_permutation<'ctx>( // We must extend to i256 because bswap must be an even number of bytes. - let const_1 = entry - .append_operation(arith::constant( - context, - IntegerAttribute::new(IntegerType::new(context, 64).into(), 1).into(), - location, - )) - .result(0)? - .into(); - - let op0_ptr = entry - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - layout_i256.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[const_1]) - .add_results(&[llvm::r#type::pointer(i256_ty, 0)]) - .build()?, - ) - .result(0)? - .into(); - let op1_ptr = entry - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - layout_i256.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[const_1]) - .add_results(&[llvm::r#type::pointer(i256_ty, 0)]) - .build()?, - ) - .result(0)? - .into(); - let op2_ptr = entry - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - layout_i256.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[const_1]) - .add_results(&[llvm::r#type::pointer(i256_ty, 0)]) - .build()?, - ) - .result(0)? - .into(); + let op0_ptr = + helper + .init_block() + .alloca1(context, location, i256_ty, Some(layout_i256.align()))?; + let op1_ptr = + helper + .init_block() + .alloca1(context, location, i256_ty, Some(layout_i256.align()))?; + let op2_ptr = + helper + .init_block() + .alloca1(context, location, i256_ty, Some(layout_i256.align()))?; let op0_i256 = entry .append_operation(ods::arith::extui(context, i256_ty, op0, location).into()) diff --git a/src/libfuncs/starknet.rs b/src/libfuncs/starknet.rs index db545004c..eab892da3 100644 --- a/src/libfuncs/starknet.rs +++ b/src/libfuncs/starknet.rs @@ -2,6 +2,7 @@ use super::LibfuncHelper; use crate::{ + block_ext::BlockExt, error::Result, ffi::get_struct_field_type_at, metadata::MetadataStorage, @@ -22,7 +23,7 @@ use cairo_lang_sierra::{ use melior::{ dialect::{ arith::{self, CmpiPredicate}, - llvm::{self, r#type::opaque_pointer, AllocaOptions, LoadStoreOptions}, + llvm::{self, LoadStoreOptions}, }, ir::{ attribute::{ @@ -155,7 +156,7 @@ pub fn build_call_contract<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -214,34 +215,19 @@ pub fn build_call_contract<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -251,25 +237,7 @@ pub fn build_call_contract<'ctx, 'this>( )); // Allocate `address` argument and write the value. - let address_arg_ptr_ty = llvm::r#type::pointer(IntegerType::new(context, 252).into(), 0); - let address_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[address_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + let address_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; entry.append_operation(llvm::store( context, entry.argument(2)?.into(), @@ -279,26 +247,7 @@ pub fn build_call_contract<'ctx, 'this>( )); // Allocate `entry_point_selector` argument and write the value. - let entry_point_selector_arg_ptr_ty = - llvm::r#type::pointer(IntegerType::new(context, 252).into(), 0); - let entry_point_selector_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[entry_point_selector_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + let entry_point_selector_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; entry.append_operation(llvm::store( context, entry.argument(3)?.into(), @@ -308,37 +257,23 @@ pub fn build_call_contract<'ctx, 'this>( )); // Allocate `calldata` argument and write the value. - let calldata_arg_ptr_ty = llvm::r#type::pointer( - llvm::r#type::r#struct( + let calldata_arg_ty = llvm::r#type::r#struct( + context, + &[llvm::r#type::r#struct( context, - &[llvm::r#type::r#struct( - context, - &[ - llvm::r#type::pointer(IntegerType::new(context, 252).into(), 0), - IntegerType::new(context, 32).into(), - IntegerType::new(context, 32).into(), - IntegerType::new(context, 32).into(), - ], - false, - )], + &[ + llvm::r#type::pointer(context, 0), // ptr to felt + IntegerType::new(context, 32).into(), + IntegerType::new(context, 32).into(), + IntegerType::new(context, 32).into(), + ], false, - ), - 0, + )], + false, ); let calldata_arg_ptr = helper .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new(IntegerType::new(context, 64).into(), 8).into(), - )]) - .add_operands(&[k1]) - .add_results(&[calldata_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + .alloca1(context, location, calldata_arg_ty, None)?; entry.append_operation(llvm::store( context, entry.argument(4)?.into(), @@ -348,18 +283,6 @@ pub fn build_call_contract<'ctx, 'this>( )); // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - address_arg_ptr_ty, - entry_point_selector_arg_ptr_ty, - calldata_arg_ptr_ty, - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -368,8 +291,8 @@ pub fn build_call_contract<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::CALL_CONTRACT.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -378,7 +301,7 @@ pub fn build_call_contract<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -449,7 +372,7 @@ pub fn build_call_contract<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -484,7 +407,7 @@ pub fn build_call_contract<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -713,7 +636,7 @@ pub fn build_storage_read<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -772,34 +695,19 @@ pub fn build_storage_read<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -809,45 +717,16 @@ pub fn build_storage_read<'ctx, 'this>( )); // Allocate `address` argument and write the value. - let address_arg_ptr_ty = llvm::r#type::pointer(IntegerType::new(context, 252).into(), 0); - let address_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[address_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); - entry.append_operation(llvm::store( + let address_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; + entry.store( context, - entry.argument(3)?.into(), - address_arg_ptr, location, - LoadStoreOptions::default(), - )); + address_arg_ptr, + entry.argument(3)?.into(), + None, + ); // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - IntegerType::new(context, 32).into(), - address_arg_ptr_ty, - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -856,8 +735,8 @@ pub fn build_storage_read<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::STORAGE_READ.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -866,7 +745,7 @@ pub fn build_storage_read<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -936,7 +815,7 @@ pub fn build_storage_read<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -971,7 +850,7 @@ pub fn build_storage_read<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -1026,7 +905,7 @@ pub fn build_storage_write<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -1089,34 +968,19 @@ pub fn build_storage_write<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -1126,74 +990,25 @@ pub fn build_storage_write<'ctx, 'this>( )); // Allocate `address` argument and write the value. - let address_arg_ptr_ty = llvm::r#type::pointer(IntegerType::new(context, 252).into(), 0); - let address_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[address_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); - entry.append_operation(llvm::store( + let address_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; + entry.store( context, - entry.argument(3)?.into(), - address_arg_ptr, location, - LoadStoreOptions::default(), - )); + address_arg_ptr, + entry.argument(3)?.into(), + None, + ); // Allocate `value` argument and write the value. - let value_arg_ptr_ty = llvm::r#type::pointer(IntegerType::new(context, 252).into(), 0); - let value_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[value_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); - entry.append_operation(llvm::store( + let value_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; + entry.store( context, - entry.argument(4)?.into(), - value_arg_ptr, location, - LoadStoreOptions::default(), - )); - - // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - IntegerType::new(context, 32).into(), - address_arg_ptr_ty, - value_arg_ptr_ty, - ], - false, + value_arg_ptr, + entry.argument(4)?.into(), + None, ); + let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -1202,8 +1017,8 @@ pub fn build_storage_write<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::STORAGE_WRITE.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -1212,7 +1027,7 @@ pub fn build_storage_write<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -1283,7 +1098,7 @@ pub fn build_storage_write<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -1318,7 +1133,7 @@ pub fn build_storage_write<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -1553,7 +1368,7 @@ pub fn build_emit_event<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -1616,34 +1431,19 @@ pub fn build_emit_event<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -1653,13 +1453,15 @@ pub fn build_emit_event<'ctx, 'this>( )); // Allocate `keys` argument and write the value. - let keys_arg_ptr_ty = llvm::r#type::pointer( + let keys_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[llvm::r#type::r#struct( context, &[ - llvm::r#type::pointer(IntegerType::new(context, 252).into(), 0), + llvm::r#type::pointer(context, 0), // ptr to felt IntegerType::new(context, 32).into(), IntegerType::new(context, 32).into(), IntegerType::new(context, 32).into(), @@ -1668,38 +1470,26 @@ pub fn build_emit_event<'ctx, 'this>( )], false, ), - 0, - ); - let keys_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new(IntegerType::new(context, 64).into(), 8).into(), - )]) - .add_operands(&[k1]) - .add_results(&[keys_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); - entry.append_operation(llvm::store( + None, + )?; + entry.store( context, - entry.argument(2)?.into(), - keys_arg_ptr, location, - LoadStoreOptions::default(), - )); + keys_arg_ptr, + entry.argument(2)?.into(), + None, + ); // Allocate `data` argument and write the value. - let data_arg_ptr_ty = llvm::r#type::pointer( + let data_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[llvm::r#type::r#struct( context, &[ - llvm::r#type::pointer(IntegerType::new(context, 252).into(), 0), + llvm::r#type::pointer(context, 0), // ptr to felt IntegerType::new(context, 32).into(), IntegerType::new(context, 32).into(), IntegerType::new(context, 32).into(), @@ -1708,42 +1498,16 @@ pub fn build_emit_event<'ctx, 'this>( )], false, ), - 0, - ); - let data_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new(IntegerType::new(context, 64).into(), 8).into(), - )]) - .add_operands(&[k1]) - .add_results(&[data_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); - entry.append_operation(llvm::store( + None, + )?; + entry.store( context, - entry.argument(3)?.into(), - data_arg_ptr, location, - LoadStoreOptions::default(), - )); - - // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - keys_arg_ptr_ty, - data_arg_ptr_ty, - ], - false, + data_arg_ptr, + entry.argument(3)?.into(), + None, ); + let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -1752,8 +1516,8 @@ pub fn build_emit_event<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::EMIT_EVENT.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -1762,7 +1526,7 @@ pub fn build_emit_event<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -1832,7 +1596,7 @@ pub fn build_emit_event<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -1867,7 +1631,7 @@ pub fn build_emit_event<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -1922,7 +1686,7 @@ pub fn build_get_block_hash<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -1981,34 +1745,19 @@ pub fn build_get_block_hash<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -2018,16 +1767,6 @@ pub fn build_get_block_hash<'ctx, 'this>( )); // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - IntegerType::new(context, 64).into(), - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -2036,8 +1775,8 @@ pub fn build_get_block_hash<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::GET_BLOCK_HASH.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -2046,7 +1785,7 @@ pub fn build_get_block_hash<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -2115,7 +1854,7 @@ pub fn build_get_block_hash<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -2150,7 +1889,7 @@ pub fn build_get_block_hash<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -2205,7 +1944,7 @@ pub fn build_get_execution_info<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -2264,34 +2003,19 @@ pub fn build_get_execution_info<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -2301,15 +2025,6 @@ pub fn build_get_execution_info<'ctx, 'this>( )); // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -2318,8 +2033,8 @@ pub fn build_get_execution_info<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::GET_EXECUTION_INFO.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -2328,7 +2043,7 @@ pub fn build_get_execution_info<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -2391,7 +2106,7 @@ pub fn build_get_execution_info<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -2426,7 +2141,7 @@ pub fn build_get_execution_info<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -2481,7 +2196,7 @@ pub fn build_get_execution_info_v2<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -2540,34 +2255,19 @@ pub fn build_get_execution_info_v2<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -2577,15 +2277,6 @@ pub fn build_get_execution_info_v2<'ctx, 'this>( )); // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -2594,8 +2285,8 @@ pub fn build_get_execution_info_v2<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::GET_EXECUTION_INFOV2.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -2604,7 +2295,7 @@ pub fn build_get_execution_info_v2<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -2667,7 +2358,7 @@ pub fn build_get_execution_info_v2<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -2702,7 +2393,7 @@ pub fn build_get_execution_info_v2<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -2757,7 +2448,7 @@ pub fn build_deploy<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -2859,34 +2550,19 @@ pub fn build_deploy<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(64).align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -2896,70 +2572,35 @@ pub fn build_deploy<'ctx, 'this>( )); // Allocate `class_hash` argument and write the value. - let class_hash_arg_ptr_ty = llvm::r#type::pointer(IntegerType::new(context, 252).into(), 0); - let class_hash_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[class_hash_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); - entry.append_operation(llvm::store( + let class_hash_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; + entry.store( context, - entry.argument(2)?.into(), - class_hash_arg_ptr, location, - LoadStoreOptions::default(), - )); + class_hash_arg_ptr, + entry.argument(2)?.into(), + None, + ); // Allocate `entry_point_selector` argument and write the value. - let contract_address_salt_arg_ptr_ty = - llvm::r#type::pointer(IntegerType::new(context, 252).into(), 0); - let contract_address_salt_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[contract_address_salt_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); - entry.append_operation(llvm::store( + let contract_address_salt_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; + entry.store( context, - entry.argument(3)?.into(), - contract_address_salt_arg_ptr, location, - LoadStoreOptions::default(), - )); + contract_address_salt_arg_ptr, + entry.argument(3)?.into(), + None, + ); // Allocate `calldata` argument and write the value. - let calldata_arg_ptr_ty = llvm::r#type::pointer( + let calldata_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[llvm::r#type::r#struct( context, &[ - llvm::r#type::pointer(IntegerType::new(context, 252).into(), 0), + llvm::r#type::pointer(context, 0), // ptr to felt IntegerType::new(context, 32).into(), IntegerType::new(context, 32).into(), IntegerType::new(context, 32).into(), @@ -2968,44 +2609,16 @@ pub fn build_deploy<'ctx, 'this>( )], false, ), - 0, - ); - let calldata_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new(IntegerType::new(context, 64).into(), 8).into(), - )]) - .add_operands(&[k1]) - .add_results(&[calldata_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); - entry.append_operation(llvm::store( + None, + )?; + entry.store( context, - entry.argument(4)?.into(), - calldata_arg_ptr, location, - LoadStoreOptions::default(), - )); - - // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - class_hash_arg_ptr_ty, - contract_address_salt_arg_ptr_ty, - calldata_arg_ptr_ty, - IntegerType::new(context, 1).into(), - ], - false, + calldata_arg_ptr, + entry.argument(4)?.into(), + None, ); + let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -3014,8 +2627,8 @@ pub fn build_deploy<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::DEPLOY.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -3024,7 +2637,7 @@ pub fn build_deploy<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -3105,7 +2718,7 @@ pub fn build_deploy<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -3140,7 +2753,7 @@ pub fn build_deploy<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -3220,7 +2833,7 @@ pub fn build_keccak<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -3279,34 +2892,19 @@ pub fn build_keccak<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -3316,52 +2914,29 @@ pub fn build_keccak<'ctx, 'this>( )); // Allocate `input` argument and write the value. - let input_arg_ptr_ty = llvm::r#type::pointer( + let input_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[ - llvm::r#type::pointer(IntegerType::new(context, 64).into(), 0), + llvm::r#type::pointer(context, 0), // ptr to u64 IntegerType::new(context, 32).into(), IntegerType::new(context, 32).into(), IntegerType::new(context, 32).into(), ], false, ), - 0, - ); - let input_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new(IntegerType::new(context, 64).into(), 8).into(), - )]) - .add_operands(&[k1]) - .add_results(&[input_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); - entry.append_operation(llvm::store( + None, + )?; + entry.store( context, - entry.argument(2)?.into(), - input_arg_ptr, location, - LoadStoreOptions::default(), - )); - - // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - input_arg_ptr_ty, - ], - false, + input_arg_ptr, + entry.argument(2)?.into(), + None, ); + let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -3370,8 +2945,8 @@ pub fn build_keccak<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::KECCAK.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -3380,7 +2955,7 @@ pub fn build_keccak<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -3443,7 +3018,7 @@ pub fn build_keccak<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -3478,7 +3053,7 @@ pub fn build_keccak<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -3533,7 +3108,7 @@ pub fn build_library_call<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -3592,34 +3167,19 @@ pub fn build_library_call<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -3629,25 +3189,7 @@ pub fn build_library_call<'ctx, 'this>( )); // Allocate `class_hash` argument and write the value. - let class_hash_arg_ptr_ty = llvm::r#type::pointer(IntegerType::new(context, 252).into(), 0); - let class_hash_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[class_hash_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + let class_hash_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; entry.append_operation(llvm::store( context, entry.argument(2)?.into(), @@ -3657,26 +3199,7 @@ pub fn build_library_call<'ctx, 'this>( )); // Allocate `entry_point_selector` argument and write the value. - let function_selector_arg_ptr_ty = - llvm::r#type::pointer(IntegerType::new(context, 252).into(), 0); - let function_selector_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[function_selector_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + let function_selector_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; entry.append_operation(llvm::store( context, entry.argument(3)?.into(), @@ -3686,13 +3209,15 @@ pub fn build_library_call<'ctx, 'this>( )); // Allocate `calldata` argument and write the value. - let calldata_arg_ptr_ty = llvm::r#type::pointer( + let calldata_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[llvm::r#type::r#struct( context, &[ - llvm::r#type::pointer(IntegerType::new(context, 252).into(), 0), + llvm::r#type::pointer(context, 0), // ptr to felt IntegerType::new(context, 32).into(), IntegerType::new(context, 32).into(), IntegerType::new(context, 32).into(), @@ -3701,22 +3226,8 @@ pub fn build_library_call<'ctx, 'this>( )], false, ), - 0, - ); - let calldata_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new(IntegerType::new(context, 64).into(), 8).into(), - )]) - .add_operands(&[k1]) - .add_results(&[calldata_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; entry.append_operation(llvm::store( context, entry.argument(4)?.into(), @@ -3725,19 +3236,6 @@ pub fn build_library_call<'ctx, 'this>( LoadStoreOptions::default(), )); - // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - class_hash_arg_ptr_ty, - function_selector_arg_ptr_ty, - calldata_arg_ptr_ty, - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -3746,8 +3244,8 @@ pub fn build_library_call<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::LIBRARY_CALL.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -3756,7 +3254,7 @@ pub fn build_library_call<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -3827,7 +3325,7 @@ pub fn build_library_call<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -3862,7 +3360,7 @@ pub fn build_library_call<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -3917,7 +3415,7 @@ pub fn build_replace_class<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -3980,34 +3478,19 @@ pub fn build_replace_class<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -4017,25 +3500,7 @@ pub fn build_replace_class<'ctx, 'this>( )); // Allocate `class_hash` argument and write the value. - let class_hash_arg_ptr_ty = llvm::r#type::pointer(IntegerType::new(context, 252).into(), 0); - let class_hash_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(252).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[class_hash_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + let class_hash_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; entry.append_operation(llvm::store( context, entry.argument(2)?.into(), @@ -4044,17 +3509,6 @@ pub fn build_replace_class<'ctx, 'this>( LoadStoreOptions::default(), )); - // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - class_hash_arg_ptr_ty, - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -4063,8 +3517,8 @@ pub fn build_replace_class<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::REPLACE_CLASS.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -4073,7 +3527,7 @@ pub fn build_replace_class<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -4136,7 +3590,7 @@ pub fn build_replace_class<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -4171,7 +3625,7 @@ pub fn build_replace_class<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -4226,7 +3680,7 @@ pub fn build_send_message_to_l1<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -4289,34 +3743,19 @@ pub fn build_send_message_to_l1<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -4326,19 +3765,7 @@ pub fn build_send_message_to_l1<'ctx, 'this>( )); // Allocate `to_address` argument and write the value. - let to_address_arg_ptr = helper - .init_block() - .append_operation(llvm::alloca( - context, - k1, - opaque_pointer(context), - location, - AllocaOptions::new().elem_type(Some(TypeAttribute::new( - IntegerType::new(context, 252).into(), - ))), - )) - .result(0)? - .into(); + let to_address_arg_ptr = helper.init_block().alloca_int(context, location, 252)?; entry.append_operation(llvm::store( context, entry.argument(2)?.into(), @@ -4348,27 +3775,21 @@ pub fn build_send_message_to_l1<'ctx, 'this>( )); // Allocate `payload` argument and write the value. - let payload_arg_ptr = helper - .init_block() - .append_operation(llvm::alloca( + let payload_arg_ptr = helper.init_block().alloca1( + context, + location, + llvm::r#type::r#struct( context, - k1, - opaque_pointer(context), - location, - AllocaOptions::new().elem_type(Some(TypeAttribute::new(llvm::r#type::r#struct( - context, - &[ - opaque_pointer(context), - IntegerType::new(context, 32).into(), - IntegerType::new(context, 32).into(), - IntegerType::new(context, 32).into(), - IntegerType::new(context, 32).into(), - ], - false, - )))), - )) - .result(0)? - .into(); + &[ + llvm::r#type::pointer(context, 0), // ptr to felt + IntegerType::new(context, 32).into(), + IntegerType::new(context, 32).into(), + IntegerType::new(context, 32).into(), + ], + false, + ), + None, + )?; entry.append_operation(llvm::store( context, entry.argument(3)?.into(), @@ -4377,18 +3798,6 @@ pub fn build_send_message_to_l1<'ctx, 'this>( LoadStoreOptions::default(), )); - // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - opaque_pointer(context), // to_address - opaque_pointer(context), // payload - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -4397,8 +3806,8 @@ pub fn build_send_message_to_l1<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::SEND_MESSAGE_TO_L1.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -4407,7 +3816,7 @@ pub fn build_send_message_to_l1<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -4477,7 +3886,7 @@ pub fn build_send_message_to_l1<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -4512,7 +3921,7 @@ pub fn build_send_message_to_l1<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -4611,7 +4020,6 @@ mod test { } #[test] - #[cfg_attr(target_arch = "aarch64", ignore = "LLVM code generation bug")] fn storage_base_address_from_felt252() { run_program_assert_output( &STORAGE_BASE_ADDRESS_FROM_FELT252, diff --git a/src/libfuncs/starknet/secp256.rs b/src/libfuncs/starknet/secp256.rs index 66449ce50..dcc451c43 100644 --- a/src/libfuncs/starknet/secp256.rs +++ b/src/libfuncs/starknet/secp256.rs @@ -94,7 +94,7 @@ pub fn build_k1_new<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -153,34 +153,19 @@ pub fn build_k1_new<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -192,28 +177,12 @@ pub fn build_k1_new<'ctx, 'this>( let i128_ty = IntegerType::new(context, 128).into(); // Allocate `x` argument and write the value. - let x_arg_ptr_ty = llvm::r#type::pointer( + let x_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct(context, &[i128_ty, i128_ty], false), - 0, - ); - let x_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(128).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[x_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; entry.append_operation(llvm::store( context, entry.argument(2)?.into(), @@ -223,28 +192,12 @@ pub fn build_k1_new<'ctx, 'this>( )); // Allocate `y` argument and write the value. - let y_arg_ptr_ty = llvm::r#type::pointer( + let y_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct(context, &[i128_ty, i128_ty], false), - 0, - ); - let y_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(128).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[y_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; entry.append_operation(llvm::store( context, entry.argument(3)?.into(), @@ -253,18 +206,6 @@ pub fn build_k1_new<'ctx, 'this>( LoadStoreOptions::default(), )); - // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - x_arg_ptr_ty, - y_arg_ptr_ty, - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -273,8 +214,8 @@ pub fn build_k1_new<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::SECP256K1_NEW.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -283,7 +224,7 @@ pub fn build_k1_new<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -353,7 +294,7 @@ pub fn build_k1_new<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -385,7 +326,7 @@ pub fn build_k1_new<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -440,7 +381,7 @@ pub fn build_k1_add<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -499,34 +440,19 @@ pub fn build_k1_add<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -536,7 +462,9 @@ pub fn build_k1_add<'ctx, 'this>( )); // Allocate `p0` argument and write the value. - let p0_arg_ptr_ty = llvm::r#type::pointer( + let p0_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[ @@ -559,26 +487,8 @@ pub fn build_k1_add<'ctx, 'this>( ], false, ), - 0, - ); - let p0_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(128).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[p0_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; entry.append_operation(llvm::store( context, entry.argument(2)?.into(), @@ -588,7 +498,9 @@ pub fn build_k1_add<'ctx, 'this>( )); // Allocate `p1` argument and write the value. - let p1_arg_ptr_ty = llvm::r#type::pointer( + let p1_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[ @@ -611,26 +523,9 @@ pub fn build_k1_add<'ctx, 'this>( ], false, ), - 0, - ); - let p1_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(128).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[p0_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; + entry.append_operation(llvm::store( context, entry.argument(3)?.into(), @@ -639,18 +534,6 @@ pub fn build_k1_add<'ctx, 'this>( LoadStoreOptions::default(), )); - // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - p0_arg_ptr_ty, - p1_arg_ptr_ty, - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -659,8 +542,8 @@ pub fn build_k1_add<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::SECP256K1_ADD.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -669,7 +552,7 @@ pub fn build_k1_add<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -739,7 +622,7 @@ pub fn build_k1_add<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -774,7 +657,7 @@ pub fn build_k1_add<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -829,7 +712,7 @@ pub fn build_k1_mul<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -888,34 +771,19 @@ pub fn build_k1_mul<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -925,7 +793,9 @@ pub fn build_k1_mul<'ctx, 'this>( )); // Allocate `p` argument and write the value. - let p_arg_ptr_ty = llvm::r#type::pointer( + let p_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[ @@ -948,26 +818,8 @@ pub fn build_k1_mul<'ctx, 'this>( ], false, ), - 0, - ); - let p_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(128).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[p_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; entry.append_operation(llvm::store( context, entry.argument(2)?.into(), @@ -977,7 +829,9 @@ pub fn build_k1_mul<'ctx, 'this>( )); // Allocate `scalar` argument and write the value. - let scalar_arg_ptr_ty = llvm::r#type::pointer( + let scalar_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[ @@ -986,26 +840,8 @@ pub fn build_k1_mul<'ctx, 'this>( ], false, ), - 0, - ); - let scalar_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(128).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[scalar_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; entry.append_operation(llvm::store( context, entry.argument(3)?.into(), @@ -1014,18 +850,6 @@ pub fn build_k1_mul<'ctx, 'this>( LoadStoreOptions::default(), )); - // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - p_arg_ptr_ty, - scalar_arg_ptr_ty, - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -1034,8 +858,8 @@ pub fn build_k1_mul<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::SECP256K1_MUL.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -1044,7 +868,7 @@ pub fn build_k1_mul<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -1114,7 +938,7 @@ pub fn build_k1_mul<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -1149,7 +973,7 @@ pub fn build_k1_mul<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -1204,7 +1028,7 @@ pub fn build_k1_get_point_from_x<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -1263,34 +1087,19 @@ pub fn build_k1_get_point_from_x<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -1300,7 +1109,9 @@ pub fn build_k1_get_point_from_x<'ctx, 'this>( )); // Allocate `x` argument and write the value. - let x_arg_ptr_ty = llvm::r#type::pointer( + let x_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[ @@ -1309,26 +1120,9 @@ pub fn build_k1_get_point_from_x<'ctx, 'this>( ], false, ), - 0, - ); - let x_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(128).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[x_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; + entry.append_operation(llvm::store( context, entry.argument(2)?.into(), @@ -1338,17 +1132,7 @@ pub fn build_k1_get_point_from_x<'ctx, 'this>( )); // Allocate `y_parity` argument and write the value. - let y_parity_arg_ptr_ty = llvm::r#type::pointer(IntegerType::new(context, 1).into(), 0); - let y_parity_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_operands(&[k1]) - .add_results(&[y_parity_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + let y_parity_arg_ptr = helper.init_block().alloca_int(context, location, 1)?; entry.append_operation(llvm::store( context, entry.argument(3)?.into(), @@ -1357,18 +1141,6 @@ pub fn build_k1_get_point_from_x<'ctx, 'this>( LoadStoreOptions::default(), )); - // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - x_arg_ptr_ty, - y_parity_arg_ptr_ty, - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -1377,8 +1149,8 @@ pub fn build_k1_get_point_from_x<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::SECP256K1_GET_POINT_FROM_X.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -1387,7 +1159,7 @@ pub fn build_k1_get_point_from_x<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -1458,7 +1230,7 @@ pub fn build_k1_get_point_from_x<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -1490,7 +1262,7 @@ pub fn build_k1_get_point_from_x<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -1545,7 +1317,7 @@ pub fn build_k1_get_xy<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -1633,34 +1405,19 @@ pub fn build_k1_get_xy<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -1670,7 +1427,9 @@ pub fn build_k1_get_xy<'ctx, 'this>( )); // Allocate `p` argument and write the value. - let p_arg_ptr_ty = llvm::r#type::pointer( + let p_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[ @@ -1693,26 +1452,8 @@ pub fn build_k1_get_xy<'ctx, 'this>( ], false, ), - 0, - ); - let p_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(128).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[p_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; entry.append_operation(llvm::store( context, entry.argument(2)?.into(), @@ -1721,17 +1462,6 @@ pub fn build_k1_get_xy<'ctx, 'this>( LoadStoreOptions::default(), )); - // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - p_arg_ptr_ty, - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -1740,8 +1470,8 @@ pub fn build_k1_get_xy<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::SECP256K1_GET_XY.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -1750,7 +1480,7 @@ pub fn build_k1_get_xy<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -1813,7 +1543,7 @@ pub fn build_k1_get_xy<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -1885,7 +1615,7 @@ pub fn build_k1_get_xy<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -1945,7 +1675,7 @@ pub fn build_r1_new<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -2004,34 +1734,19 @@ pub fn build_r1_new<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -2043,28 +1758,12 @@ pub fn build_r1_new<'ctx, 'this>( let i128_ty = IntegerType::new(context, 128).into(); // Allocate `x` argument and write the value. - let x_arg_ptr_ty = llvm::r#type::pointer( + let x_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct(context, &[i128_ty, i128_ty], false), - 0, - ); - let x_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(128).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[x_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; entry.append_operation(llvm::store( context, entry.argument(2)?.into(), @@ -2074,28 +1773,12 @@ pub fn build_r1_new<'ctx, 'this>( )); // Allocate `y` argument and write the value. - let y_arg_ptr_ty = llvm::r#type::pointer( + let y_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct(context, &[i128_ty, i128_ty], false), - 0, - ); - let y_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(128).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[y_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; entry.append_operation(llvm::store( context, entry.argument(3)?.into(), @@ -2104,18 +1787,6 @@ pub fn build_r1_new<'ctx, 'this>( LoadStoreOptions::default(), )); - // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - x_arg_ptr_ty, - y_arg_ptr_ty, - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -2124,8 +1795,8 @@ pub fn build_r1_new<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::SECP256R1_NEW.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -2134,7 +1805,7 @@ pub fn build_r1_new<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -2205,7 +1876,7 @@ pub fn build_r1_new<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -2237,7 +1908,7 @@ pub fn build_r1_new<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -2292,7 +1963,7 @@ pub fn build_r1_add<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -2351,34 +2022,19 @@ pub fn build_r1_add<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -2388,7 +2044,9 @@ pub fn build_r1_add<'ctx, 'this>( )); // Allocate `p0` argument and write the value. - let p0_arg_ptr_ty = llvm::r#type::pointer( + let p0_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[ @@ -2411,26 +2069,8 @@ pub fn build_r1_add<'ctx, 'this>( ], false, ), - 0, - ); - let p0_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(128).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[p0_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; entry.append_operation(llvm::store( context, entry.argument(2)?.into(), @@ -2440,7 +2080,9 @@ pub fn build_r1_add<'ctx, 'this>( )); // Allocate `p1` argument and write the value. - let p1_arg_ptr_ty = llvm::r#type::pointer( + let p1_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[ @@ -2463,26 +2105,8 @@ pub fn build_r1_add<'ctx, 'this>( ], false, ), - 0, - ); - let p1_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(128).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[p0_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; entry.append_operation(llvm::store( context, entry.argument(3)?.into(), @@ -2492,17 +2116,6 @@ pub fn build_r1_add<'ctx, 'this>( )); // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - p0_arg_ptr_ty, - p1_arg_ptr_ty, - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -2511,8 +2124,8 @@ pub fn build_r1_add<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::SECP256R1_ADD.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -2521,7 +2134,7 @@ pub fn build_r1_add<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -2591,7 +2204,7 @@ pub fn build_r1_add<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -2626,7 +2239,7 @@ pub fn build_r1_add<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -2681,7 +2294,7 @@ pub fn build_r1_mul<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -2740,34 +2353,19 @@ pub fn build_r1_mul<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -2777,7 +2375,9 @@ pub fn build_r1_mul<'ctx, 'this>( )); // Allocate `p` argument and write the value. - let p_arg_ptr_ty = llvm::r#type::pointer( + let p_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[ @@ -2800,26 +2400,9 @@ pub fn build_r1_mul<'ctx, 'this>( ], false, ), - 0, - ); - let p_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(128).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[p_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; + entry.append_operation(llvm::store( context, entry.argument(2)?.into(), @@ -2829,7 +2412,9 @@ pub fn build_r1_mul<'ctx, 'this>( )); // Allocate `scalar` argument and write the value. - let scalar_arg_ptr_ty = llvm::r#type::pointer( + let scalar_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[ @@ -2838,26 +2423,8 @@ pub fn build_r1_mul<'ctx, 'this>( ], false, ), - 0, - ); - let scalar_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(128).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[scalar_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; entry.append_operation(llvm::store( context, entry.argument(3)?.into(), @@ -2867,17 +2434,6 @@ pub fn build_r1_mul<'ctx, 'this>( )); // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - p_arg_ptr_ty, - scalar_arg_ptr_ty, - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -2886,8 +2442,8 @@ pub fn build_r1_mul<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::SECP256R1_MUL.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -2896,7 +2452,7 @@ pub fn build_r1_mul<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -2966,7 +2522,7 @@ pub fn build_r1_mul<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -3001,7 +2557,7 @@ pub fn build_r1_mul<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -3056,7 +2612,7 @@ pub fn build_r1_get_point_from_x<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -3115,34 +2671,19 @@ pub fn build_r1_get_point_from_x<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -3152,7 +2693,9 @@ pub fn build_r1_get_point_from_x<'ctx, 'this>( )); // Allocate `x` argument and write the value. - let x_arg_ptr_ty = llvm::r#type::pointer( + let x_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[ @@ -3161,26 +2704,8 @@ pub fn build_r1_get_point_from_x<'ctx, 'this>( ], false, ), - 0, - ); - let x_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(128).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[x_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; entry.append_operation(llvm::store( context, entry.argument(2)?.into(), @@ -3190,17 +2715,7 @@ pub fn build_r1_get_point_from_x<'ctx, 'this>( )); // Allocate `y_parity` argument and write the value. - let y_parity_arg_ptr_ty = llvm::r#type::pointer(IntegerType::new(context, 1).into(), 0); - let y_parity_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_operands(&[k1]) - .add_results(&[y_parity_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + let y_parity_arg_ptr = helper.init_block().alloca_int(context, location, 1)?; entry.append_operation(llvm::store( context, entry.argument(3)?.into(), @@ -3210,17 +2725,6 @@ pub fn build_r1_get_point_from_x<'ctx, 'this>( )); // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - x_arg_ptr_ty, - y_parity_arg_ptr_ty, - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -3229,8 +2733,8 @@ pub fn build_r1_get_point_from_x<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::SECP256R1_GET_POINT_FROM_X.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -3239,7 +2743,7 @@ pub fn build_r1_get_point_from_x<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -3309,7 +2813,7 @@ pub fn build_r1_get_point_from_x<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -3341,7 +2845,7 @@ pub fn build_r1_get_point_from_x<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -3396,7 +2900,7 @@ pub fn build_r1_get_xy<'ctx, 'this>( .append_operation(llvm::load( context, entry.argument(1)?.into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -3484,34 +2988,19 @@ pub fn build_r1_get_xy<'ctx, 'this>( ), ]) .add_operands(&[k1]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? .into(); // Allocate space and write the current gas. - let gas_builtin_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - result_layout.align().try_into()?, - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[llvm::r#type::pointer( - IntegerType::new(context, 128).into(), - 0, - )]) - .build()?, - ) - .result(0)? - .into(); + let gas_builtin_ptr = helper.init_block().alloca1( + context, + location, + IntegerType::new(context, 128).into(), + Some(get_integer_layout(128).align()), + )?; entry.append_operation(llvm::store( context, entry.argument(0)?.into(), @@ -3521,7 +3010,9 @@ pub fn build_r1_get_xy<'ctx, 'this>( )); // Allocate `p` argument and write the value. - let p_arg_ptr_ty = llvm::r#type::pointer( + let p_arg_ptr = helper.init_block().alloca1( + context, + location, llvm::r#type::r#struct( context, &[ @@ -3544,26 +3035,9 @@ pub fn build_r1_get_xy<'ctx, 'this>( ], false, ), - 0, - ); - let p_arg_ptr = helper - .init_block() - .append_operation( - OperationBuilder::new("llvm.alloca", location) - .add_attributes(&[( - Identifier::new(context, "alignment"), - IntegerAttribute::new( - IntegerType::new(context, 64).into(), - get_integer_layout(128).align().try_into().unwrap(), - ) - .into(), - )]) - .add_operands(&[k1]) - .add_results(&[p_arg_ptr_ty]) - .build()?, - ) - .result(0)? - .into(); + None, + )?; + entry.append_operation(llvm::store( context, entry.argument(2)?.into(), @@ -3573,16 +3047,6 @@ pub fn build_r1_get_xy<'ctx, 'this>( )); // Extract function pointer. - let fn_ptr_ty = llvm::r#type::function( - llvm::r#type::void(context), - &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::pointer(IntegerType::new(context, 128).into(), 0), - p_arg_ptr_ty, - ], - false, - ); let fn_ptr = entry .append_operation(llvm::get_element_ptr( context, @@ -3591,8 +3055,8 @@ pub fn build_r1_get_xy<'ctx, 'this>( context, &[StarknetSyscallHandlerCallbacks::<()>::SECP256R1_GET_XY.try_into()?], ), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -3601,7 +3065,7 @@ pub fn build_r1_get_xy<'ctx, 'this>( .append_operation(llvm::load( context, fn_ptr, - llvm::r#type::pointer(fn_ptr_ty, 0), + llvm::r#type::pointer(context, 0), location, LoadStoreOptions::default(), )) @@ -3664,7 +3128,7 @@ pub fn build_r1_get_xy<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? @@ -3736,7 +3200,7 @@ pub fn build_r1_get_xy<'ctx, 'this>( ), ]) .add_operands(&[result_ptr]) - .add_results(&[llvm::r#type::opaque_pointer(context)]) + .add_results(&[llvm::r#type::pointer(context, 0)]) .build()?, ) .result(0)? diff --git a/src/metadata/debug_utils.rs b/src/metadata/debug_utils.rs index b45b0625d..f9d1277ed 100644 --- a/src/metadata/debug_utils.rs +++ b/src/metadata/debug_utils.rs @@ -87,7 +87,7 @@ use crate::{block_ext::BlockExt, error::Result}; use melior::{ dialect::{ arith, func, - llvm::{self, r#type::opaque_pointer}, + llvm::{self, r#type::pointer}, ods, }, ir::{ @@ -175,10 +175,7 @@ impl DebugUtils { TypeAttribute::new( FunctionType::new( context, - &[ - opaque_pointer(context), - IntegerType::new(context, 64).into(), - ], + &[pointer(context, 0), IntegerType::new(context, 64).into()], &[], ) .into(), @@ -197,26 +194,7 @@ impl DebugUtils { message.len().try_into().unwrap(), ); - let k1 = block - .append_operation(arith::constant( - context, - IntegerAttribute::new(IntegerType::new(context, 64).into(), 1).into(), - location, - )) - .result(0)? - .into(); - - let ptr = block - .append_operation( - { - let mut op = ods::llvm::alloca(context, opaque_pointer(context), k1, location); - op.set_elem_type(TypeAttribute::new(ty)); - op - } - .into(), - ) - .result(0)? - .into(); + let ptr = block.alloca1(context, location, ty, None)?; let msg = block .append_operation( @@ -285,10 +263,7 @@ impl DebugUtils { module.body().append_operation(func::func( context, StringAttribute::new(context, "__debug__print_pointer"), - TypeAttribute::new( - FunctionType::new(context, &[llvm::r#type::opaque_pointer(context)], &[]) - .into(), - ), + TypeAttribute::new(FunctionType::new(context, &[pointer(context, 0)], &[]).into()), Region::new(), &[( Identifier::new(context, "sym_visibility"), @@ -655,7 +630,7 @@ impl DebugUtils { FunctionType::new( context, &[ - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), IntegerType::new(context, 64).into(), ], &[], diff --git a/src/metadata/realloc_bindings.rs b/src/metadata/realloc_bindings.rs index 9321e1f2c..1bd5793f3 100644 --- a/src/metadata/realloc_bindings.rs +++ b/src/metadata/realloc_bindings.rs @@ -30,10 +30,10 @@ impl ReallocBindingsMeta { FunctionType::new( context, &[ - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), IntegerType::new(context, 64).into(), ], - &[llvm::r#type::opaque_pointer(context)], + &[llvm::r#type::pointer(context, 0)], ) .into(), ), @@ -48,7 +48,7 @@ impl ReallocBindingsMeta { context, StringAttribute::new(context, "free"), TypeAttribute::new( - FunctionType::new(context, &[llvm::r#type::opaque_pointer(context)], &[]).into(), + FunctionType::new(context, &[llvm::r#type::pointer(context, 0)], &[]).into(), ), Region::new(), &[( @@ -74,7 +74,7 @@ impl ReallocBindingsMeta { context, FlatSymbolRefAttribute::new(context, "realloc"), &[ptr, len], - &[llvm::r#type::opaque_pointer(context)], + &[llvm::r#type::pointer(context, 0)], location, ) } diff --git a/src/metadata/runtime_bindings.rs b/src/metadata/runtime_bindings.rs index 2db32a285..032cacc6f 100644 --- a/src/metadata/runtime_bindings.rs +++ b/src/metadata/runtime_bindings.rs @@ -64,7 +64,7 @@ impl RuntimeBindingsMeta { context, &[ IntegerType::new(context, 32).into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), IntegerType::new(context, 32).into(), ], &[IntegerType::new(context, 32).into()], @@ -115,9 +115,9 @@ impl RuntimeBindingsMeta { FunctionType::new( context, &[ - llvm::r#type::pointer(IntegerType::new(context, 256).into(), 0), - llvm::r#type::pointer(IntegerType::new(context, 256).into(), 0), - llvm::r#type::pointer(IntegerType::new(context, 256).into(), 0), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), ], &[], ) @@ -165,9 +165,9 @@ impl RuntimeBindingsMeta { FunctionType::new( context, &[ - llvm::r#type::pointer(IntegerType::new(context, 256).into(), 0), - llvm::r#type::pointer(IntegerType::new(context, 256).into(), 0), - llvm::r#type::pointer(IntegerType::new(context, 256).into(), 0), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), ], &[], ) @@ -203,15 +203,6 @@ impl RuntimeBindingsMeta { where 'c: 'a, { - let ec_point_ty = llvm::r#type::r#struct( - context, - &[ - IntegerType::new(context, 252).into(), - IntegerType::new(context, 252).into(), - ], - false, - ); - if self.active_map.insert(RuntimeBinding::EcPointFromXNz) { module.body().append_operation(func::func( context, @@ -219,7 +210,7 @@ impl RuntimeBindingsMeta { TypeAttribute::new( FunctionType::new( context, - &[llvm::r#type::pointer(ec_point_ty, 0)], + &[llvm::r#type::pointer(context, 0)], &[IntegerType::new(context, 1).into()], ) .into(), @@ -254,15 +245,6 @@ impl RuntimeBindingsMeta { where 'c: 'a, { - let ec_point_ty = llvm::r#type::r#struct( - context, - &[ - IntegerType::new(context, 252).into(), - IntegerType::new(context, 252).into(), - ], - false, - ); - if self.active_map.insert(RuntimeBinding::EcPointTryNewNz) { module.body().append_operation(func::func( context, @@ -270,7 +252,7 @@ impl RuntimeBindingsMeta { TypeAttribute::new( FunctionType::new( context, - &[llvm::r#type::pointer(ec_point_ty, 0)], + &[llvm::r#type::pointer(context, 0)], &[IntegerType::new(context, 1).into()], ) .into(), @@ -306,25 +288,6 @@ impl RuntimeBindingsMeta { where 'c: 'a, { - let ec_state_ty = llvm::r#type::r#struct( - context, - &[ - IntegerType::new(context, 252).into(), - IntegerType::new(context, 252).into(), - IntegerType::new(context, 252).into(), - IntegerType::new(context, 252).into(), - ], - false, - ); - let ec_point_ty = llvm::r#type::r#struct( - context, - &[ - IntegerType::new(context, 252).into(), - IntegerType::new(context, 252).into(), - ], - false, - ); - if self.active_map.insert(RuntimeBinding::EcStateAdd) { module.body().append_operation(func::func( context, @@ -333,8 +296,8 @@ impl RuntimeBindingsMeta { FunctionType::new( context, &[ - llvm::r#type::pointer(ec_state_ty, 0), - llvm::r#type::pointer(ec_point_ty, 0), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), ], &[], ) @@ -373,14 +336,6 @@ impl RuntimeBindingsMeta { where 'c: 'a, { - let felt252_ty = IntegerType::new(context, 252).into(); - let ec_state_ty = llvm::r#type::r#struct( - context, - &[felt252_ty, felt252_ty, felt252_ty, felt252_ty], - false, - ); - let ec_point_ty = llvm::r#type::r#struct(context, &[felt252_ty, felt252_ty], false); - if self.active_map.insert(RuntimeBinding::EcStateAddMul) { module.body().append_operation(func::func( context, @@ -389,9 +344,9 @@ impl RuntimeBindingsMeta { FunctionType::new( context, &[ - llvm::r#type::pointer(ec_state_ty, 0), - llvm::r#type::pointer(felt252_ty, 0), - llvm::r#type::pointer(ec_point_ty, 0), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), ], &[], ) @@ -427,14 +382,6 @@ impl RuntimeBindingsMeta { where 'c: 'a, { - let felt252_ty = IntegerType::new(context, 252).into(); - let ec_state_ty = llvm::r#type::r#struct( - context, - &[felt252_ty, felt252_ty, felt252_ty, felt252_ty], - false, - ); - let ec_point_ty = llvm::r#type::r#struct(context, &[felt252_ty, felt252_ty], false); - if self.active_map.insert(RuntimeBinding::EcStateTryFinalizeNz) { module.body().append_operation(func::func( context, @@ -446,8 +393,8 @@ impl RuntimeBindingsMeta { FunctionType::new( context, &[ - llvm::r#type::pointer(ec_point_ty, 0), - llvm::r#type::pointer(ec_state_ty, 0), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), ], &[IntegerType::new(context, 1).into()], ) @@ -493,8 +440,7 @@ impl RuntimeBindingsMeta { context, StringAttribute::new(context, "cairo_native__alloc_dict"), TypeAttribute::new( - FunctionType::new(context, &[], &[llvm::r#type::opaque_pointer(context)]) - .into(), + FunctionType::new(context, &[], &[llvm::r#type::pointer(context, 0)]).into(), ), Region::new(), &[( @@ -509,7 +455,7 @@ impl RuntimeBindingsMeta { context, FlatSymbolRefAttribute::new(context, "cairo_native__alloc_dict"), &[], - &[llvm::r#type::opaque_pointer(context)], + &[llvm::r#type::pointer(context, 0)], location, ))) } @@ -534,8 +480,7 @@ impl RuntimeBindingsMeta { context, StringAttribute::new(context, "cairo_native__dict_free"), TypeAttribute::new( - FunctionType::new(context, &[llvm::r#type::opaque_pointer(context)], &[]) - .into(), + FunctionType::new(context, &[llvm::r#type::pointer(context, 0)], &[]).into(), ), Region::new(), &[( @@ -581,10 +526,10 @@ impl RuntimeBindingsMeta { FunctionType::new( context, &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), ], - &[llvm::r#type::opaque_pointer(context)], + &[llvm::r#type::pointer(context, 0)], ) .into(), ), @@ -601,7 +546,7 @@ impl RuntimeBindingsMeta { context, FlatSymbolRefAttribute::new(context, "cairo_native__dict_get"), &[dict_ptr, key_ptr], - &[llvm::r#type::opaque_pointer(context)], + &[llvm::r#type::pointer(context, 0)], location, ))) } @@ -633,11 +578,11 @@ impl RuntimeBindingsMeta { FunctionType::new( context, &[ - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), + llvm::r#type::pointer(context, 0), ], - &[llvm::r#type::opaque_pointer(context)], + &[llvm::r#type::pointer(context, 0)], ) .into(), ), @@ -654,7 +599,7 @@ impl RuntimeBindingsMeta { context, FlatSymbolRefAttribute::new(context, "cairo_native__dict_insert"), &[dict_ptr, key_ptr, value_ptr], - &[llvm::r#type::opaque_pointer(context)], + &[llvm::r#type::pointer(context, 0)], location, ))) } @@ -683,7 +628,7 @@ impl RuntimeBindingsMeta { TypeAttribute::new( FunctionType::new( context, - &[llvm::r#type::opaque_pointer(context)], + &[llvm::r#type::pointer(context, 0)], &[IntegerType::new(context, 64).into()], ) .into(), diff --git a/src/starknet.rs b/src/starknet.rs index e629b2822..5e7f0da3e 100644 --- a/src/starknet.rs +++ b/src/starknet.rs @@ -15,14 +15,14 @@ pub struct ArrayAbi { /// Binary representation of a `Felt` (in MLIR). #[derive(Debug, Clone)] -#[cfg_attr(target_arch = "x86_64", repr(C, align(8)))] +#[cfg_attr(target_arch = "x86_64", repr(C, align(16)))] #[cfg_attr(not(target_arch = "x86_64"), repr(C, align(16)))] pub struct Felt252Abi(pub [u8; 32]); /// Binary representation of a `u256` (in MLIR). // TODO: This shouldn't need to be public. #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] #[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))] -#[cfg_attr(target_arch = "x86_64", repr(C, align(8)))] +#[cfg_attr(target_arch = "x86_64", repr(C, align(16)))] #[cfg_attr(not(target_arch = "x86_64"), repr(C, align(16)))] pub struct U256 { pub hi: u128, @@ -1009,6 +1009,8 @@ pub(crate) mod handler { .map(|x| Felt252Abi(x.to_bytes_le())) .collect::>(), ); + tx_info_ptr.as_mut().account_contract_address = + Felt252Abi(x.tx_info.account_contract_address.to_bytes_le()); execution_info_ptr.as_mut().block_info = block_info_ptr; execution_info_ptr.as_mut().tx_info = tx_info_ptr; @@ -1054,7 +1056,10 @@ pub(crate) mod handler { let until_offset = calldata.until as usize; debug_assert!(since_offset <= until_offset); let len = until_offset - since_offset; - std::slice::from_raw_parts(calldata.ptr.add(since_offset), len) + match len { + 0 => &[], + _ => std::slice::from_raw_parts(calldata.ptr.add(since_offset), len), + } } .iter() .map(|x| { @@ -1137,7 +1142,10 @@ pub(crate) mod handler { let until_offset = calldata.until as usize; debug_assert!(since_offset <= until_offset); let len = until_offset - since_offset; - std::slice::from_raw_parts(calldata.ptr.add(since_offset), len) + match len { + 0 => &[], + _ => std::slice::from_raw_parts(calldata.ptr.add(since_offset), len), + } } .iter() .map(|x| { @@ -1190,7 +1198,10 @@ pub(crate) mod handler { let until_offset = calldata.until as usize; debug_assert!(since_offset <= until_offset); let len = until_offset - since_offset; - std::slice::from_raw_parts(calldata.ptr.add(since_offset), len) + match len { + 0 => &[], + _ => std::slice::from_raw_parts(calldata.ptr.add(since_offset), len), + } } .iter() .map(|x| { @@ -1287,7 +1298,10 @@ pub(crate) mod handler { let until_offset = keys.until as usize; debug_assert!(since_offset <= until_offset); let len = until_offset - since_offset; - std::slice::from_raw_parts(keys.ptr.add(since_offset), len) + match len { + 0 => &[], + _ => std::slice::from_raw_parts(keys.ptr.add(since_offset), len), + } } .iter() .map(|x| { @@ -1304,7 +1318,10 @@ pub(crate) mod handler { let until_offset = data.until as usize; debug_assert!(since_offset <= until_offset); let len = until_offset - since_offset; - std::slice::from_raw_parts(data.ptr.add(since_offset), len) + match len { + 0 => &[], + _ => std::slice::from_raw_parts(data.ptr.add(since_offset), len), + } } .iter() .map(|x| { @@ -1346,7 +1363,10 @@ pub(crate) mod handler { let until_offset = payload.until as usize; debug_assert!(since_offset <= until_offset); let len = until_offset - since_offset; - std::slice::from_raw_parts(payload.ptr.add(since_offset), len) + match len { + 0 => &[], + _ => std::slice::from_raw_parts(payload.ptr.add(since_offset), len), + } } .iter() .map(|x| { @@ -1382,7 +1402,10 @@ pub(crate) mod handler { let until_offset = input.until as usize; debug_assert!(since_offset <= until_offset); let len = until_offset - since_offset; - std::slice::from_raw_parts(input.ptr.add(since_offset), len) + match len { + 0 => &[], + _ => std::slice::from_raw_parts(input.ptr.add(since_offset), len), + } }; let result = ptr.keccak(input, gas); diff --git a/src/types.rs b/src/types.rs index 4c7bcf593..7ca49840a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -21,7 +21,11 @@ use cairo_lang_sierra::{ program_registry::ProgramRegistry, }; use melior::{ - dialect::{arith, llvm}, + dialect::{ + arith, + llvm::{self, r#type::pointer}, + ods, + }, ir::{ attribute::{DenseI64ArrayAttribute, IntegerAttribute}, r#type::IntegerType, @@ -864,10 +868,9 @@ impl TypeBuilder for CoreTypeConcrete { .result(0)? .into(), Self::Nullable(_) => entry - .append_operation(llvm::nullptr( - llvm::r#type::opaque_pointer(context), - location, - )) + .append_operation( + ods::llvm::mlir_zero(context, pointer(context, 0), location).into(), + ) .result(0)? .into(), Self::Uint8(_) => entry @@ -926,6 +929,10 @@ impl TypeBuilder for CoreTypeConcrete { ) -> Result<(), Self::Error> { match self { CoreTypeConcrete::Array(_info) => { + if metadata.get::().is_none() { + metadata.insert(ReallocBindingsMeta::new(context, helper)); + } + let array_ty = registry.build_type(context, helper, registry, metadata, self_ty)?; let ptr_ty = crate::ffi::get_struct_field_type_at(&array_ty, 0); diff --git a/src/types/array.rs b/src/types/array.rs index d7e600d34..9e8056198 100644 --- a/src/types/array.rs +++ b/src/types/array.rs @@ -36,7 +36,7 @@ use cairo_lang_sierra::{ use melior::{ dialect::{ arith, cf, - llvm::{self, r#type::opaque_pointer, LoadStoreOptions}, + llvm::{self, r#type::pointer, LoadStoreOptions}, ods, }, ir::{ @@ -68,7 +68,7 @@ pub fn build<'ctx>( }, ); - let ptr_ty = llvm::r#type::opaque_pointer(context); + let ptr_ty = llvm::r#type::pointer(context, 0); let len_ty = IntegerType::new(context, 32).into(); Ok(llvm::r#type::r#struct( @@ -107,7 +107,7 @@ fn snapshot_take<'ctx, 'this>( context, src_value, DenseI64ArrayAttribute::new(context, &[0]), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, )) .result(0)? @@ -173,15 +173,13 @@ fn snapshot_take<'ctx, 'this>( .into(); let null_ptr = entry - .append_operation(llvm::nullptr( - llvm::r#type::opaque_pointer(context), - location, - )) + .append_operation(ods::llvm::mlir_zero(context, pointer(context, 0), location).into()) .result(0)? .into(); let block_realloc = helper.append_block(Block::new(&[])); - let block_finish = helper.append_block(Block::new(&[(opaque_pointer(context), location)])); + let block_finish = + helper.append_block(Block::new(&[(llvm::r#type::pointer(context, 0), location)])); entry.append_operation(cf::cond_br( context, @@ -246,7 +244,7 @@ fn snapshot_take<'ctx, 'this>( src_ptr, &[src_ptr_offset], IntegerType::new(context, 8).into(), - llvm::r#type::opaque_pointer(context), + llvm::r#type::pointer(context, 0), location, )) .result(0)? diff --git a/src/types/box.rs b/src/types/box.rs index 9ddeb7f7a..031c934c1 100644 --- a/src/types/box.rs +++ b/src/types/box.rs @@ -31,7 +31,7 @@ use cairo_lang_sierra::{ }; use melior::{ dialect::{ - llvm::{self, r#type::opaque_pointer}, + llvm::{self, r#type::pointer}, ods, }, ir::{attribute::IntegerAttribute, r#type::IntegerType, Block, Location, Module, Type, Value}, @@ -59,7 +59,7 @@ pub fn build<'ctx>( }, ); - Ok(opaque_pointer(context)) + Ok(llvm::r#type::pointer(context, 0)) } #[allow(clippy::too_many_arguments)] @@ -88,7 +88,7 @@ fn snapshot_take<'ctx, 'this>( let value_len = entry.const_int(context, location, inner_layout.pad_to_align().size(), 64)?; let ptr = entry - .append_operation(llvm::nullptr(opaque_pointer(context), location)) + .append_operation(ods::llvm::mlir_zero(context, pointer(context, 0), location).into()) .result(0)? .into(); let dst_ptr = entry diff --git a/src/types/felt252_dict.rs b/src/types/felt252_dict.rs index e3531cfff..a6d2c201d 100644 --- a/src/types/felt252_dict.rs +++ b/src/types/felt252_dict.rs @@ -31,7 +31,7 @@ pub fn build<'ctx>( _metadata: &mut MetadataStorage, _info: WithSelf, ) -> Result> { - Ok(llvm::r#type::opaque_pointer(context)) + Ok(llvm::r#type::pointer(context, 0)) } #[cfg(test)] diff --git a/src/types/felt252_dict_entry.rs b/src/types/felt252_dict_entry.rs index bc4024f32..23fba74f1 100644 --- a/src/types/felt252_dict_entry.rs +++ b/src/types/felt252_dict_entry.rs @@ -40,8 +40,8 @@ pub fn build<'ctx>( context, &[ IntegerType::new(context, 252).into(), // entry key - llvm::r#type::opaque_pointer(context), // value ptr - llvm::r#type::opaque_pointer(context), // dict ptr + llvm::r#type::pointer(context, 0), // value ptr + llvm::r#type::pointer(context, 0), // dict ptr ], false, )) diff --git a/src/types/nullable.rs b/src/types/nullable.rs index baf84722c..b2f6008be 100644 --- a/src/types/nullable.rs +++ b/src/types/nullable.rs @@ -23,7 +23,8 @@ use cairo_lang_sierra::{ use melior::{ dialect::{ arith::{self, CmpiPredicate}, - llvm, ods, scf, + llvm::{self, r#type::pointer}, + ods, scf, }, ir::{ attribute::IntegerAttribute, operation::OperationBuilder, r#type::IntegerType, Block, @@ -54,7 +55,7 @@ pub fn build<'ctx>( ); // nullable is represented as a pointer, like a box, used to check if its null (when it can be null). - Ok(llvm::r#type::opaque_pointer(context)) + Ok(llvm::r#type::pointer(context, 0)) } #[allow(clippy::too_many_arguments)] @@ -83,10 +84,7 @@ fn snapshot_take<'ctx, 'this>( .result(0)? .into(); let null_ptr = entry - .append_operation(llvm::nullptr( - llvm::r#type::opaque_pointer(context), - location, - )) + .append_operation(ods::llvm::mlir_zero(context, pointer(context, 0), location).into()) .result(0)? .into(); @@ -114,7 +112,7 @@ fn snapshot_take<'ctx, 'this>( let value = entry .append_operation(scf::r#if( is_null, - &[llvm::r#type::opaque_pointer(context)], + &[llvm::r#type::pointer(context, 0)], { let region = Region::new(); let block = region.append_block(Block::new(&[])); diff --git a/src/types/squashed_felt252_dict.rs b/src/types/squashed_felt252_dict.rs index f73ff0391..3b7a05621 100644 --- a/src/types/squashed_felt252_dict.rs +++ b/src/types/squashed_felt252_dict.rs @@ -28,5 +28,5 @@ pub fn build<'ctx>( //let inner = registry.get_type(&info.ty)?; //let layout = inner.layout(registry)?; - Ok(llvm::r#type::opaque_pointer(context)) + Ok(llvm::r#type::pointer(context, 0)) } diff --git a/src/types/starknet.rs b/src/types/starknet.rs index 50ebe5145..5d5ded10e 100644 --- a/src/types/starknet.rs +++ b/src/types/starknet.rs @@ -144,7 +144,7 @@ pub fn build_system<'ctx>( _metadata: &mut MetadataStorage, _info: WithSelf, ) -> Result> { - Ok(llvm::r#type::opaque_pointer(context)) + Ok(llvm::r#type::pointer(context, 0)) } pub fn build_secp256_point<'ctx>( diff --git a/src/utils.rs b/src/utils.rs index ec6f66984..790972882 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -54,17 +54,9 @@ pub fn generate_function_name(function_id: &FunctionId) -> Cow { /// Return the layout for an integer of arbitrary width. /// -/// This assumes the platform's maximum (effective) alignment is 8 bytes, and that every integer +/// This assumes the platform's maximum (effective) alignment is 16 bytes, and that every integer /// with a size in bytes of a power of two has the same alignment as its size. pub fn get_integer_layout(width: u32) -> Layout { - // TODO: Fix integer layouts properly. - if width == 248 || width == 252 || width == 256 { - #[cfg(target_arch = "x86_64")] - return Layout::from_size_align(32, 8).unwrap(); - #[cfg(not(target_arch = "x86_64"))] - return Layout::from_size_align(32, 16).unwrap(); - } - if width == 0 { Layout::new::<()>() } else if width <= 8 { @@ -76,14 +68,17 @@ pub fn get_integer_layout(width: u32) -> Layout { } else if width <= 64 { Layout::new::() } else if width <= 128 { - Layout::new::() - } else { - #[cfg(target_arch = "x86_64")] - let value = Layout::array::(next_multiple_of_u32(width, 64) as usize >> 6).unwrap(); #[cfg(not(target_arch = "x86_64"))] - let value = Layout::array::(next_multiple_of_u32(width, 128) as usize >> 7).unwrap(); - - value + { + Layout::new::() + } + #[cfg(target_arch = "x86_64")] + { + Layout::new::().align_to(16).unwrap() + } + } else { + let width = (width as usize).next_multiple_of(8).next_power_of_two(); + Layout::from_size_align(width >> 3, (width >> 3).min(16)).unwrap() } } @@ -841,15 +836,12 @@ pub mod test { /// Ensures that the host's `u128` is compatible with its compiled counterpart. #[test] - #[ignore] fn test_alignment_compatibility_u128() { - // FIXME: Uncomment once LLVM fixes its u128 alignment issues. assert_eq!(get_integer_layout(128).align(), 16); } /// Ensures that the host's `u256` is compatible with its compiled counterpart. #[test] - #[ignore] fn test_alignment_compatibility_u256() { assert_eq!(get_integer_layout(256).align(), 16); } @@ -857,17 +849,13 @@ pub mod test { /// Ensures that the host's `u512` is compatible with its compiled counterpart. #[test] fn test_alignment_compatibility_u512() { - #[cfg(target_arch = "x86_64")] - assert_eq!(get_integer_layout(512).align(), 8); - #[cfg(not(target_arch = "x86_64"))] assert_eq!(get_integer_layout(512).align(), 16); } /// Ensures that the host's `Felt` is compatible with its compiled counterpart. #[test] - #[ignore] fn test_alignment_compatibility_felt() { - assert_eq!(get_integer_layout(252).align(), 8); + assert_eq!(get_integer_layout(252).align(), 16); } // ============================== @@ -1205,8 +1193,8 @@ pub mod test { pub struct TestSyscallHandler; impl StarknetSyscallHandler for TestSyscallHandler { - fn get_block_hash(&mut self, _block_number: u64, _gas: &mut u128) -> SyscallResult { - Ok(Felt::from_bytes_be_slice(b"get_block_hash ok")) + fn get_block_hash(&mut self, block_number: u64, _gas: &mut u128) -> SyscallResult { + Ok(Felt::from(block_number)) } fn get_execution_info( diff --git a/src/values.rs b/src/values.rs index 0a7895e76..d5e1a262f 100644 --- a/src/values.rs +++ b/src/values.rs @@ -521,26 +521,26 @@ impl JitValue { .cast::() .as_ref(); - let init_data_ptr = *ptr.cast::>().as_ref(); - let data_ptr = NonNull::new_unchecked( - init_data_ptr - .as_ptr() - .byte_add(elem_stride * offset_value as usize), - ); + // this pointer can be null if the array has a size of 0. + let init_data_ptr = *ptr.cast::<*mut ()>().as_ref(); + let data_ptr = init_data_ptr.byte_add(elem_stride * offset_value as usize); assert!(length_value >= offset_value); let num_elems = (length_value - offset_value) as usize; let mut array_value = Vec::with_capacity(num_elems); + for i in 0..num_elems { - let cur_elem_ptr = NonNull::new( - ((data_ptr.as_ptr() as usize) + elem_stride * i) as *mut (), - ) - .unwrap(); + // safe to create a NonNull because if the array has elements, the init_data_ptr can't be null. + let cur_elem_ptr = + NonNull::new(((data_ptr as usize) + elem_stride * i) as *mut ()) + .unwrap(); array_value.push(Self::from_jit(cur_elem_ptr, &info.ty, registry)); } - libc::free(init_data_ptr.as_ptr().cast()); + if !init_data_ptr.is_null() { + libc::free(init_data_ptr.cast()); + } Self::Array(array_value) } @@ -567,7 +567,7 @@ impl JitValue { } CoreTypeConcrete::Felt252(_) => { let data = ptr.cast::<[u8; 32]>().as_ref(); - let data = Felt::from_bytes_le(data); + let data = Felt::from_bytes_le_slice(data); Self::Felt252(data) } CoreTypeConcrete::Uint8(_) => Self::Uint8(*ptr.cast::().as_ref()), diff --git a/tests/tests/boolean.rs b/tests/tests/boolean.rs index 848554ff4..54a801fc1 100644 --- a/tests/tests/boolean.rs +++ b/tests/tests/boolean.rs @@ -108,7 +108,6 @@ lazy_static! { // Since comparing a felt to 1 to create boolean (uses felt252_is_zero and felt sub,add) has a bug, // we'll be using use u8 on other tests until this is fixed. The bug may be in felt subtraction. -#[ignore = "comparing a Felt == 1 will lead to wrong results"] #[test] fn felt252_to_bool_bug() { let program = &FELT252_TO_BOOL; diff --git a/tests/tests/cases.rs b/tests/tests/cases.rs index 4b54b876e..09d00e1e0 100644 --- a/tests/tests/cases.rs +++ b/tests/tests/cases.rs @@ -19,7 +19,6 @@ use test_case::test_case; #[test_case("tests/cases/panic_array.cairo")] #[test_case("tests/cases/generic_fn_loop.cairo")] // enums -// TODO: compare error: Fail(Reason("assertion failed: `(left == right)` \n left: `0`,\n right: `10` at tests/common.rs:453")) #[test_case("tests/cases/enums/enum_init_c_style.cairo")] #[test_case("tests/cases/enums/enum_init_empty.cairo")] #[test_case("tests/cases/enums/enum_init_multiple.cairo")] diff --git a/tests/tests/programs.rs b/tests/tests/programs.rs index 6448e8eb0..0a32caa7e 100644 --- a/tests/tests/programs.rs +++ b/tests/tests/programs.rs @@ -362,7 +362,6 @@ proptest! { } #[test] -#[ignore = "currently failing with `vm size not yet implemented`"] fn self_referencing_struct() { let result_vm = run_vm_program( &SELF_REFERENCING, diff --git a/tests/tests/starknet/programs/syscalls.cairo b/tests/tests/starknet/programs/syscalls.cairo index bd2efc431..805dd3754 100644 --- a/tests/tests/starknet/programs/syscalls.cairo +++ b/tests/tests/starknet/programs/syscalls.cairo @@ -1,27 +1,22 @@ use core::starknet::{ call_contract_syscall, class_hash_const, contract_address_const, ContractAddress, deploy_syscall, emit_event_syscall, ExecutionInfo, get_block_hash_syscall, - get_execution_info_syscall, info::v2::ExecutionInfo as ExecutionInfoV2, keccak_syscall, + keccak_syscall, library_call_syscall, replace_class_syscall, send_message_to_l1_syscall, storage_address_try_from_felt252, storage_read_syscall, storage_write_syscall, SyscallResult, }; - -// extern fn get_execution_info_syscall() -> SyscallResult< -// Box -// > implicits(GasBuiltin, System) nopanic; -extern fn get_execution_info_v2_syscall() -> SyscallResult< - Box -> implicits(GasBuiltin, System) nopanic; +use core::starknet::syscalls::get_execution_info_syscall; +use core::starknet::syscalls::get_execution_info_v2_syscall; fn get_block_hash() -> SyscallResult { get_block_hash_syscall(0) } -// fn get_execution_info() -> SyscallResult> { -// get_execution_info_syscall() -// } +fn get_execution_info() -> SyscallResult> { + get_execution_info_syscall() +} -fn get_execution_info_v2() -> SyscallResult> { +fn get_execution_info_v2() -> SyscallResult> { get_execution_info_v2_syscall() } diff --git a/tests/tests/starknet/syscalls.rs b/tests/tests/starknet/syscalls.rs index f62105a6f..8620519dd 100644 --- a/tests/tests/starknet/syscalls.rs +++ b/tests/tests/starknet/syscalls.rs @@ -383,7 +383,6 @@ fn get_block_hash() { } #[test] -#[ignore = "Cairo program doesn't compile"] fn get_execution_info() { let result = run_native_program( &SYSCALLS_PROGRAM, @@ -401,8 +400,8 @@ fn get_execution_info() { fields: vec![ JitValue::Struct { fields: vec![ - JitValue::Uint128(10057862467973663535), - JitValue::Uint128(13878668747512495966), + JitValue::Uint64(10057862467973663535), + JitValue::Uint64(13878668747512495966), JitValue::Felt252(Felt::from_dec_str( "1126241460712630201003776917997524449163698107789103849210792326381258973685", ) @@ -421,7 +420,12 @@ fn get_execution_info() { ) .unwrap()), JitValue::Uint128(67871905340377755668863509019681938001), - JitValue::Array(Vec::new()), + JitValue::Struct { + fields: vec![ + JitValue::Array(Vec::new()), + ], + debug_name: None + }, JitValue::Felt252(Felt::from_dec_str( "2073267424102447009330753642820908998776456851902897601865334818765025369132", ) @@ -458,7 +462,6 @@ fn get_execution_info() { } #[test] -#[ignore = "failing"] fn get_execution_info_v2() { let result = run_native_program( &SYSCALLS_PROGRAM,