Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix(LLVM): Implement the missing relocations on aarch64, fix SIMD operations #5210

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 65 additions & 65 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ jobs:
target: aarch64-apple-darwin
artifact_name: 'wasmer-darwin-arm64'
use_sccache: false
use_llvm: false
use_llvm: true
build_wasm: false
# [todo] xdoardo: Reinstate when the code we generate for aarch64 is working correctly.
llvm_url: 'https://github.com/wasmerio/llvm-custom-builds/releases/download/18.x/llvm-darwin-aarch64.tar.xz'
Expand Down Expand Up @@ -573,70 +573,70 @@ jobs:
if-no-files-found: error
retention-days: 2

# linux_riscv64:
# name: Linux riscv64
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3
# - uses: dtolnay/rust-toolchain@stable
# with:
# target: riscv64gc-unknown-linux-gnu
# - name: Build cross image
# run: |
# docker build -t wasmer/riscv64 ${GITHUB_WORKSPACE}/.github/cross-linux-riscv64/
# env:
# CROSS_DOCKER_IN_DOCKER: true
# - name: Build Wasmer binary
# run: |
# make build-wasmer
# env:
# CARGO_BINARY: docker run -v /var/run/docker.sock:/var/run/docker.sock -v ${GITHUB_WORKSPACE}:/project -w /project wasmer/riscv64:latest cargo
# CROSS_DOCKER_IN_DOCKER: true
# CARGO_TARGET: riscv64gc-unknown-linux-gnu
# PKG_CONFIG_PATH: /usr/lib/riscv64-linux-gnu/pkgconfig
# PKG_CONFIG_ALLOW_CROSS: true
# ENABLE_LLVM: 0
# - name: Build C API headless
# shell: bash
# run: |
# make package-capi-headless
# env:
# CARGO_BINARY: docker run -v /var/run/docker.sock:/var/run/docker.sock -v ${GITHUB_WORKSPACE}:/project -w /project wasmer/riscv64:latest cargo
# CROSS_DOCKER_IN_DOCKER: true
# CARGO_TARGET: riscv64gc-unknown-linux-gnu
# PKG_CONFIG_PATH: /usr/lib/riscv64-linux-gnu/pkgconfig
# PKG_CONFIG_ALLOW_CROSS: true
# ENABLE_LLVM: 0
# TARGET: riscv64gc-unknown-linux-gnu
# TARGET_DIR: target/riscv64gc-unknown-linux-gnu/release
# - name: Build C API
# run: |
# make build-capi
# env:
# CARGO_BINARY: docker run -v /var/run/docker.sock:/var/run/docker.sock -v ${GITHUB_WORKSPACE}:/project -w /project wasmer/riscv64:latest cargo
# CROSS_DOCKER_IN_DOCKER: true
# CARGO_TARGET: riscv64gc-unknown-linux-gnu
# PKG_CONFIG_PATH: /usr/lib/riscv64-linux-gnu/pkgconfig
# PKG_CONFIG_ALLOW_CROSS: true
# ENABLE_LLVM: 0
# - name: Dist
# run: |
# make distribution
# env:
# CARGO_BINARY: docker run -v /var/run/docker.sock:/var/run/docker.sock -v ${GITHUB_WORKSPACE}:/project -w /project wasmer/riscv64:latest cargo
# CROSS_DOCKER_IN_DOCKER: true
# CARGO_TARGET: riscv64gc-unknown-linux-gnu
# PKG_CONFIG_PATH: /usr/lib/riscv64-linux-gnu/pkgconfig
# PKG_CONFIG_ALLOW_CROSS: true
# TARGET: riscv64gc-unknown-linux-gnu
# TARGET_DIR: target/riscv64gc-unknown-linux-gnu/release
# - name: Upload Artifacts
# uses: actions/upload-artifact@v4
# with:
# name: wasmer-linux-riscv64
# path: dist
# if-no-files-found: error
# retention-days: 2
linux_riscv64:
name: Linux riscv64
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
target: riscv64gc-unknown-linux-gnu
- name: Build cross image
run: |
docker build -t wasmer/riscv64 ${GITHUB_WORKSPACE}/.github/cross-linux-riscv64/
env:
CROSS_DOCKER_IN_DOCKER: true
- name: Build Wasmer binary
run: |
make build-wasmer
env:
CARGO_BINARY: docker run -v /var/run/docker.sock:/var/run/docker.sock -v ${GITHUB_WORKSPACE}:/project -w /project wasmer/riscv64:latest cargo
CROSS_DOCKER_IN_DOCKER: true
CARGO_TARGET: riscv64gc-unknown-linux-gnu
PKG_CONFIG_PATH: /usr/lib/riscv64-linux-gnu/pkgconfig
PKG_CONFIG_ALLOW_CROSS: true
ENABLE_LLVM: 0
- name: Build C API headless
shell: bash
run: |
make package-capi-headless
env:
CARGO_BINARY: docker run -v /var/run/docker.sock:/var/run/docker.sock -v ${GITHUB_WORKSPACE}:/project -w /project wasmer/riscv64:latest cargo
CROSS_DOCKER_IN_DOCKER: true
CARGO_TARGET: riscv64gc-unknown-linux-gnu
PKG_CONFIG_PATH: /usr/lib/riscv64-linux-gnu/pkgconfig
PKG_CONFIG_ALLOW_CROSS: true
ENABLE_LLVM: 0
TARGET: riscv64gc-unknown-linux-gnu
TARGET_DIR: target/riscv64gc-unknown-linux-gnu/release
- name: Build C API
run: |
make build-capi
env:
CARGO_BINARY: docker run -v /var/run/docker.sock:/var/run/docker.sock -v ${GITHUB_WORKSPACE}:/project -w /project wasmer/riscv64:latest cargo
CROSS_DOCKER_IN_DOCKER: true
CARGO_TARGET: riscv64gc-unknown-linux-gnu
PKG_CONFIG_PATH: /usr/lib/riscv64-linux-gnu/pkgconfig
PKG_CONFIG_ALLOW_CROSS: true
ENABLE_LLVM: 0
- name: Dist
run: |
make distribution
env:
CARGO_BINARY: docker run -v /var/run/docker.sock:/var/run/docker.sock -v ${GITHUB_WORKSPACE}:/project -w /project wasmer/riscv64:latest cargo
CROSS_DOCKER_IN_DOCKER: true
CARGO_TARGET: riscv64gc-unknown-linux-gnu
PKG_CONFIG_PATH: /usr/lib/riscv64-linux-gnu/pkgconfig
PKG_CONFIG_ALLOW_CROSS: true
TARGET: riscv64gc-unknown-linux-gnu
TARGET_DIR: target/riscv64gc-unknown-linux-gnu/release
- name: Upload Artifacts
uses: actions/upload-artifact@v4
with:
name: wasmer-linux-riscv64
path: dist
if-no-files-found: error
retention-days: 2

release:
# needs: [setup, build, linux_aarch64, windows_gnu, linux_riscv64]
Expand Down
8 changes: 2 additions & 6 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,7 @@ jobs:
os: macos-14,
target: aarch64-apple-darwin,
exe: '',
# [todo] xdoardo: Reinstate when the code we generate for aarch64 is working correctly.
# llvm_url: 'https://github.com/wasmerio/llvm-custom-builds/releases/download/18.x/llvm-darwin-aarch64.tar.xz'
llvm_url: 'https://github.com/wasmerio/llvm-custom-builds/releases/download/18.x/llvm-darwin-aarch64.tar.xz'
},
{
build: windows-x64,
Expand Down Expand Up @@ -744,7 +743,6 @@ jobs:
}
]
metadata: [
# We cannot test on macos-arm since we don't have ARM runners
{
build: linux-x64,
os: ubuntu-22.04,
Expand All @@ -758,15 +756,13 @@ jobs:
target: x86_64-apple-darwin,
exe: '',
llvm_url: 'https://github.com/wasmerio/llvm-custom-builds/releases/download/18.x/llvm-darwin-amd64.tar.xz'

},
{
build: macos-arm,
os: macos-14,
target: aarch64-apple-darwin,
exe: '',
# [todo] xdoardo: Reinstate when the code we generate for aarch64 is working correctly.
# llvm_url: 'https://github.com/wasmerio/llvm-custom-builds/releases/download/18.x/llvm-darwin-aarch64.tar.xz'
llvm_url: 'https://github.com/wasmerio/llvm-custom-builds/releases/download/18.x/llvm-darwin-aarch64.tar.xz'
},
{
build: windows-x64,
Expand Down
23 changes: 22 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ else
endif

build-docs:
$(CARGO_BINARY) doc $(CARGO_TARGET_FLAG) --release $(compiler_features) --document-private-items --no-deps --workspace --exclude wasmer-c-api --locked
$(CARGO_BINARY) doc $(CARGO_TARGET_FLAG) --release $(compiler_features) --document-private-items --no-deps --workspace --exclude wasmer-c-api --exclude wasmer-swift --locked

# The tokio crate was excluded from the docs build because the code (which is not under our control)
# does not currently compile its docs successfully
Expand Down
4 changes: 2 additions & 2 deletions examples/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ fn main() -> anyhow::Result<()> {
// see how we can do that:
println!("Growing memory...");

// 'wasm-c-api' does not support direct calls to memory.grow()
#[cfg(not(feature = "wasm-c-api"))]
// 'wamr' does not support direct calls to memory.grow()
#[cfg(not(feature = "wamr"))]
{
// Here we are requesting two more pages for our memory.
memory.grow(&mut store, 2)?;
Expand Down
1 change: 0 additions & 1 deletion examples/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ fn main() -> anyhow::Result<()> {

// This test is currently failing with:
// not implemented: Native function definitions can't be directly called from the host yet
#[cfg(FALSE)]
#[test]
fn test_table() -> anyhow::Result<()> {
main()
Expand Down
2 changes: 2 additions & 0 deletions lib/compiler-llvm/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,13 @@ impl LLVM {
// but not in the case of Aarch64, there the ABI is slightly different
#[allow(clippy::match_single_binding)]
match target.triple().architecture {
Architecture::Aarch64(_) => wasmer_types::OperatingSystem::Darwin,
_ => wasmer_types::OperatingSystem::Linux,
}
} else {
target.triple().operating_system
};

let binary_format = if self.is_pic {
target.triple().binary_format
} else {
Expand Down
26 changes: 26 additions & 0 deletions lib/compiler-llvm/src/object_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,32 @@ where
object::RelocationKind::Elf(object::elf::R_LARCH_ABS64_LO20),
0,
) => RelocationKind::LArchAbs64Lo20,
(
object::Architecture::Aarch64,
object::RelocationKind::Elf(object::elf::R_AARCH64_ADR_PREL_LO21),
0,
) => RelocationKind::Aarch64AdrPrelLo21,
(
object::Architecture::Aarch64,
object::RelocationKind::Elf(object::elf::R_AARCH64_ADR_PREL_PG_HI21),
0,
) => RelocationKind::Aarch64AdrPrelPgHi21,
(
object::Architecture::Aarch64,
object::RelocationKind::Elf(object::elf::R_AARCH64_LDST128_ABS_LO12_NC),
0,
) => RelocationKind::Aarch64Ldst128AbsLo12Nc,
(
object::Architecture::Aarch64,
object::RelocationKind::Elf(object::elf::R_AARCH64_ADD_ABS_LO12_NC),
0,
) => RelocationKind::Aarch64AddAbsLo12Nc,
(
object::Architecture::Aarch64,
object::RelocationKind::Elf(object::elf::R_AARCH64_LDST64_ABS_LO12_NC),
0,
) => RelocationKind::Aarch64Ldst64AbsLo12Nc,

_ => {
return Err(CompileError::Codegen(format!(
"unknown relocation {:?}",
Expand Down
4 changes: 0 additions & 4 deletions lib/compiler-llvm/src/translator/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,10 +1104,6 @@ impl<'ctx> Intrinsics<'ctx> {
intrinsics
.throw_trap
.add_attribute(AttributeLoc::Function, noreturn);
//intrinsics
// .func_ref
// .add_attribute(AttributeLoc::Function, intrinsics.readonly);

intrinsics
}
}
Expand Down
53 changes: 53 additions & 0 deletions lib/compiler/src/engine/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,59 @@ fn apply_relocation(
| read_unaligned(reloc_address as *mut u32);
write_unaligned(reloc_address as *mut u32, reloc_abs);
},
RelocationKind::Aarch64AdrPrelPgHi21 => unsafe {
let (reloc_address, delta) = r.for_address(body, target_func_address as u64);

let delta = delta as isize;
assert!(
((-1 << 32)..(1 << 32)).contains(&delta),
"can't generate page-relative relocation with ±4GB `adrp` instruction"
);

let op = read_unaligned(reloc_address as *mut u32);
let delta = delta >> 12;
let immlo = ((delta as u32) & 0b11) << 29;
let immhi = (((delta as u32) >> 2) & 0x7ffff) << 5;
let mask = !((0x7ffff << 5) | (0b11 << 29));
let op = (op & mask) | immlo | immhi;

write_unaligned(reloc_address as *mut u32, op);
},
RelocationKind::Aarch64AdrPrelLo21 => unsafe {
let (reloc_address, delta) = r.for_address(body, target_func_address as u64);

let delta = delta as isize;
assert!(
((-1 << 20)..(1 << 20)).contains(&delta),
"can't generate an ADR_PREL_LO21 relocation with an immediate larger than 20 bits"
);

let op = read_unaligned(reloc_address as *mut u32);
let immlo = ((delta as u32) & 0b11) << 29;
let immhi = (((delta as u32) >> 2) & 0x7ffff) << 5;
let mask = !((0x7ffff << 5) | (0b11 << 29));
let op = (op & mask) | immlo | immhi;

write_unaligned(reloc_address as *mut u32, op);
},
RelocationKind::Aarch64AddAbsLo12Nc => unsafe {
let (reloc_address, reloc_delta) = r.for_address(body, target_func_address as u64);
let reloc_delta = (reloc_delta as u32 & 0xfff)
| (read_unaligned(reloc_address as *mut u32) & 0xFFC003FF);
write_unaligned(reloc_address as *mut u32, reloc_delta);
},
RelocationKind::Aarch64Ldst128AbsLo12Nc => unsafe {
let (reloc_address, reloc_delta) = r.for_address(body, target_func_address as u64);
let reloc_delta = ((reloc_delta as u32 & 0xfff) >> 4) << 10
| (read_unaligned(reloc_address as *mut u32) & 0xFFC003FF);
write_unaligned(reloc_address as *mut u32, reloc_delta);
},
RelocationKind::Aarch64Ldst64AbsLo12Nc => unsafe {
let (reloc_address, reloc_delta) = r.for_address(body, target_func_address as u64);
let reloc_delta = ((reloc_delta as u32 & 0xfff) >> 3) << 10
| (read_unaligned(reloc_address as *mut u32) & 0xFFC003FF);
write_unaligned(reloc_address as *mut u32, reloc_delta);
},
kind => panic!(
"Relocation kind unsupported in the current architecture {}",
kind
Expand Down
Loading
Loading