Skip to content

Commit b0ff5bb

Browse files
committed
Avoid passing addrspacecast to lifetime intrinsics
Since LLVM 22 the alloca must be passed directly. Do this by stripping the addrspacecast if it exists.
1 parent 27076c4 commit b0ff5bb

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,9 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
17731773
}
17741774

17751775
if crate::llvm_util::get_version() >= (22, 0, 0) {
1776+
// LLVM 22 requires the lifetime intrinsic to act directly on the alloca,
1777+
// there can't be an addrspacecast in between.
1778+
let ptr = unsafe { llvm::LLVMRustStripPointerCasts(ptr) };
17761779
self.call_intrinsic(intrinsic, &[self.val_ty(ptr)], &[ptr]);
17771780
} else {
17781781
self.call_intrinsic(intrinsic, &[self.val_ty(ptr)], &[self.cx.const_u64(size), ptr]);

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,7 @@ unsafe extern "C" {
19591959
Metadata: &'a Metadata,
19601960
);
19611961
pub(crate) fn LLVMRustIsNonGVFunctionPointerTy(Val: &Value) -> bool;
1962+
pub(crate) fn LLVMRustStripPointerCasts<'a>(Val: &'a Value) -> &'a Value;
19621963

19631964
// Operations on scalar constants
19641965
pub(crate) fn LLVMRustConstIntGetZExtValue(ConstantVal: &ConstantInt, Value: &mut u64) -> bool;

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,10 @@ extern "C" bool LLVMRustIsNonGVFunctionPointerTy(LLVMValueRef V) {
17451745
return false;
17461746
}
17471747

1748+
extern "C" LLVMValueRef LLVMRustStripPointerCasts(LLVMValueRef V) {
1749+
return wrap(unwrap(V)->stripPointerCasts());
1750+
}
1751+
17481752
extern "C" bool LLVMRustLLVMHasZlibCompression() {
17491753
return llvm::compression::zlib::isAvailable();
17501754
}

tests/codegen-llvm/amdgpu-addrspacecast.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@
33
//@ compile-flags: --crate-type=rlib --target=amdgcn-amd-amdhsa -Ctarget-cpu=gfx900
44
//@ needs-llvm-components: amdgpu
55
//@ add-minicore
6+
//@ revisions: LLVM21 LLVM22
7+
//@ [LLVM21] max-llvm-major-version: 21
8+
//@ [LLVM22] min-llvm-version: 22
69
#![feature(no_core)]
710
#![no_core]
811

912
extern crate minicore;
1013

14+
// Make sure that on LLVM 22, the alloca is passed directly to the lifetime intrinsics,
15+
// not the addrspacecast.
16+
1117
// CHECK-LABEL: @ref_of_local
1218
// CHECK: [[alloca:%[0-9]]] = alloca
1319
// CHECK: %i = addrspacecast ptr addrspace(5) [[alloca]] to ptr
20+
// LLVM22: call void @llvm.lifetime.start.p5(ptr addrspace(5) [[alloca]])
21+
// CHECK: call void %f(ptr{{.*}}%i)
22+
// LLVM22: call void @llvm.lifetime.end.p5(ptr addrspace(5) [[alloca]])
1423
#[no_mangle]
1524
pub fn ref_of_local(f: fn(&i32)) {
1625
let i = 0;

0 commit comments

Comments
 (0)