Skip to content

Commit

Permalink
Make allocopt respect the GC verifier rules with non usual address sp…
Browse files Browse the repository at this point in the history
…aces (#51520)

On AMDGPU, this was generating a `addrspace(10)` pointer to an `alloca`
which is illegal and lead to other issues.

(cherry picked from commit af9a7af)
  • Loading branch information
gbaraldi authored and KristofferC committed Nov 27, 2023
1 parent 1ddd6da commit 99c4ae4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
4 changes: 1 addition & 3 deletions src/llvm-alloc-opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,6 @@ void Optimizer::moveToStack(CallInst *orig_inst, size_t sz, bool has_ref)
}
insertLifetime(ptr, ConstantInt::get(Type::getInt64Ty(prolog_builder.getContext()), sz), orig_inst);
Instruction *new_inst = cast<Instruction>(prolog_builder.CreateBitCast(ptr, JuliaType::get_pjlvalue_ty(prolog_builder.getContext(), buff->getType()->getPointerAddressSpace())));
if (orig_inst->getModule()->getDataLayout().getAllocaAddrSpace() != 0)
new_inst = cast<Instruction>(prolog_builder.CreateAddrSpaceCast(new_inst, JuliaType::get_pjlvalue_ty(prolog_builder.getContext(), orig_inst->getType()->getPointerAddressSpace())));
new_inst->takeName(orig_inst);

auto simple_replace = [&] (Instruction *orig_i, Instruction *new_i) {
Expand Down Expand Up @@ -692,7 +690,7 @@ void Optimizer::moveToStack(CallInst *orig_inst, size_t sz, bool has_ref)
else if (auto call = dyn_cast<CallInst>(user)) {
auto callee = call->getCalledOperand();
if (pass.pointer_from_objref_func == callee) {
call->replaceAllUsesWith(new_i);
call->replaceAllUsesWith(prolog_builder.CreateAddrSpaceCast(new_i, call->getCalledFunction()->getReturnType()));
call->eraseFromParent();
return;
}
Expand Down
11 changes: 6 additions & 5 deletions test/llvmpasses/alloc-opt-gcframe-addrspaces.ll
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ declare {}* @julia.pointer_from_objref({} addrspace(11)*)
; Test that non-0 addrspace allocas are properly emitted and handled

; CHECK-LABEL: @non_zero_addrspace
; CHECK: %1 = alloca i32, align 8, addrspace(5)
; TYPED: %1 = alloca i32, align 8, addrspace(5)

; TYPED: %2 = bitcast i32 addrspace(5)* %1 to i8 addrspace(5)*
; TYPED: %3 = bitcast i8 addrspace(5)* %2 to {} addrspace(5)*
; TYPED: %var1 = addrspacecast {} addrspace(5)* %3 to {} addrspace(10)*
; TYPED: %var1 = bitcast i8 addrspace(5)* %2 to {} addrspace(5)*
; TYPED: %3 = addrspacecast {} addrspace(5)* %var1 to {}*
; TYPED: call void @llvm.lifetime.start.p5i8(i64 4, i8 addrspace(5)* %2)

; OPAQUE: %var1 = addrspacecast ptr addrspace(5) %1 to ptr addrspace(10)
; OPAQUE: call void @llvm.lifetime.start.p5(i64 4, ptr addrspace(5) %1)
; OPAQUE: %var1 = alloca i32, align 8, addrspace(5)
; OPAQUE: %1 = addrspacecast ptr addrspace(5) %var1 to ptr
; OPAQUE: call void @llvm.lifetime.start.p5(i64 4, ptr addrspace(5) %var1)

; CHECK: ret void
define void @non_zero_addrspace() {
Expand Down

0 comments on commit 99c4ae4

Please sign in to comment.