Skip to content

Commit 2e763da

Browse files
PMQ9FractalFir
authored andcommitted
change LLVMBuildPointerCast to LLVMBuildAddrSpaceCast
1 parent 8055b0a commit 2e763da

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

crates/rustc_codegen_nvvm/src/builder.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,15 @@ impl<'ll, 'tcx, 'a> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
479479
let alloca = llvm::LLVMBuildAlloca(bx.llbuilder, ty, UNNAMED);
480480
llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
481481
// Cast to default addrspace if necessary
482-
llvm::LLVMBuildPointerCast(bx.llbuilder, alloca, self.cx().type_ptr(), UNNAMED)
482+
let alloca_ty = llvm::LLVMTypeOf(alloca);
483+
let alloca_addrspace = llvm::LLVMGetPointerAddressSpace(alloca_ty);
484+
let dest_ty = self.cx().type_ptr();
485+
let dest_addrspace = llvm::LLVMGetPointerAddressSpace(dest_ty);
486+
if alloca_addrspace != dest_addrspace {
487+
llvm::LLVMBuildAddrSpaceCast(bx.llbuilder, alloca, dest_ty, UNNAMED)
488+
} else {
489+
alloca
490+
}
483491
}
484492
}
485493

@@ -907,7 +915,19 @@ impl<'ll, 'tcx, 'a> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
907915

908916
fn pointercast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
909917
trace!("Pointercast `{:?}` to ty `{:?}`", val, dest_ty);
910-
unsafe { llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, unnamed()) }
918+
unsafe {
919+
let val_ty = self.val_ty(val);
920+
let val_addrspace = llvm::LLVMGetPointerAddressSpace(val_ty);
921+
let dest_addrspace = llvm::LLVMGetPointerAddressSpace(dest_ty);
922+
923+
if val_addrspace != dest_addrspace {
924+
// Address spaces differ, use addrspacecast
925+
llvm::LLVMBuildAddrSpaceCast(self.llbuilder, val, dest_ty, unnamed())
926+
} else {
927+
// Same address space, use regular pointer cast
928+
llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, unnamed())
929+
}
930+
}
911931
}
912932

913933
/* Comparisons */

0 commit comments

Comments
 (0)