[clang] [CIR] Keep the address space in dynamic_cast to void (PR #237245)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 30 07:56:29 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangir
Author: Arseniy Obolenskiy (aobolensk)
<details>
<summary>Changes</summary>
Use the source address space instead of the default one
This fixes CIR verification for HIP on spirv64-amd-amdhsa
---
Full diff: https://github.com/llvm/llvm-project/pull/237245.diff
3 Files Affected:
- (modified) clang/lib/CIR/CodeGen/CIRGenBuilder.h (+2-3)
- (modified) clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp (+4-6)
- (added) clang/test/CIR/CodeGenHIP/dynamic-cast-void-addrspace.hip (+14)
``````````diff
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
index e60e3092b699f3..48e657aa2bfd6d 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
@@ -468,9 +468,8 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
mlir::Value createDynCastToVoid(mlir::Location loc, mlir::Value src,
bool vtableUseRelativeLayout) {
- // TODO(cir): consider address space here.
- assert(!cir::MissingFeatures::addressSpace());
- cir::PointerType destTy = getVoidPtrTy();
+ auto srcPtrTy = mlir::cast<cir::PointerType>(src.getType());
+ cir::PointerType destTy = getVoidPtrTy(srcPtrTy.getAddrSpace());
return cir::DynamicCastOp::create(
*this, loc, destTy, cir::DynamicCastKind::Ptr, src,
cir::DynamicCastInfoAttr{}, vtableUseRelativeLayout);
diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp
index 962cdd3782adad..f5179f38cfc1e4 100644
--- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp
@@ -789,7 +789,7 @@ static mlir::Value buildDynamicCastToVoidAfterNullCheck(
mlir::Location loc = op.getLoc();
bool vtableUsesRelativeLayout = op.getRelativeLayout();
- // TODO(cir): consider address space in this function.
+ // TODO(cir): consider address space of the vtable pointer.
assert(!cir::MissingFeatures::addressSpace());
mlir::Type vtableElemTy;
@@ -837,20 +837,18 @@ static mlir::Value buildDynamicCastToVoidAfterNullCheck(
/*mem_order=*/cir::MemOrderAttr(),
/*invariant=*/false);
- auto voidPtrTy =
- cir::PointerType::get(cir::VoidType::get(builder.getContext()));
-
// Add the offset to the given pointer to get the cast result.
// Cast the input pointer to a uint8_t* to allow pointer arithmetic.
mlir::Type u8PtrTy =
cir::PointerType::get(cir::IntType::get(builder.getContext(), /*width=*/8,
- /*isSigned=*/false));
+ /*isSigned=*/false),
+ op.getSrc().getType().getAddrSpace());
mlir::Value srcBytePtr = cir::CastOp::create(
builder, loc, u8PtrTy, cir::CastKind::bitcast, op.getSrc());
auto dstBytePtr =
cir::PtrStrideOp::create(builder, loc, u8PtrTy, srcBytePtr, offsetToTop);
// Cast the result to a void*.
- return cir::CastOp::create(builder, loc, voidPtrTy, cir::CastKind::bitcast,
+ return cir::CastOp::create(builder, loc, op.getType(), cir::CastKind::bitcast,
dstBytePtr);
}
diff --git a/clang/test/CIR/CodeGenHIP/dynamic-cast-void-addrspace.hip b/clang/test/CIR/CodeGenHIP/dynamic-cast-void-addrspace.hip
new file mode 100644
index 00000000000000..c52719fbd4ab0d
--- /dev/null
+++ b/clang/test/CIR/CodeGenHIP/dynamic-cast-void-addrspace.hip
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple spirv64-amd-amdhsa -x hip -fcuda-is-device -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s
+
+#define __device__ __attribute__((device))
+
+struct A { __device__ virtual void f(); };
+
+// CHECK-LABEL: cir.func {{.*}} @_Z9cast_voidP1A
+// CHECK: %[[SRC:.*]] = cir.load {{.*}}, !cir.ptr<!rec_A, target_address_space(4)>
+// CHECK: %[[RAW:.*]] = cir.cast bitcast %[[SRC]] : !cir.ptr<!rec_A, target_address_space(4)> -> !cir.ptr<!u8i, target_address_space(4)>
+// CHECK: %[[ADJ:.*]] = cir.ptr_stride %[[RAW]], %{{.*}} : (!cir.ptr<!u8i, target_address_space(4)>, !s64i) -> !cir.ptr<!u8i, target_address_space(4)>
+// CHECK: cir.cast bitcast %[[ADJ]] : !cir.ptr<!u8i, target_address_space(4)> -> !cir.ptr<!void, target_address_space(4)>
+// CHECK: }) : (!cir.bool) -> !cir.ptr<!void, target_address_space(4)>
+__device__ void *cast_void(A *p) { return dynamic_cast<void *>(p); }
``````````
</details>
https://github.com/llvm/llvm-project/pull/237245
More information about the cfe-commits
mailing list