[CIR] Pass the __cxa_throw destructor in the program address space #246091
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
@llvm/pr-subscribers-clangir
Author: Arseniy Obolenskiy (aobolensk)
<details>
<summary>Changes</summary>
Function pointers live in the program address space on targets like spirv64-amd-amdhsa, as in classic codegen
ABI lowering now also keeps that address space when it retypes a function address
---
Full diff: https://github.com/llvm/llvm-project/pull/246091.diff
7 Files Affected:
````````````diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h b/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
index 04c381b4311186..1b6fe58270b0ef 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
+++ b/clang/include/clang/CIR/Dialect/IR/CIRDataLayout.h
@@ -156,6 +156,13 @@ class CIRDataLayout {
return {};
return cir::TargetAddressSpaceAttr::get(ctx, globalsASAttr.getUInt());
}
+
+ mlir::ptr::MemorySpaceAttrInterface
+ getProgramAddrSpace(mlir::MLIRContext *ctx) const {
+ if (programAddrSpace == 0)
+ return {};
+ return cir::TargetAddressSpaceAttr::get(ctx, programAddrSpace);
+ }
};
} // namespace cir
diff --git a/clang/lib/CIR/Dialect/Transforms/EHABILowering.cpp b/clang/lib/CIR/Dialect/Transforms/EHABILowering.cpp
index c40983509864af..41da25110cc20b 100644
--- a/clang/lib/CIR/Dialect/Transforms/EHABILowering.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/EHABILowering.cpp
@@ -126,6 +126,7 @@ class ItaniumEHLowering : public EHABILowering {
cir::VoidType voidType;
cir::PointerType voidPtrType;
+ cir::PointerType programVoidPtrType;
cir::PointerType u8PtrType;
cir::IntType u32Type;
cir::IntType s32Type;
@@ -175,6 +176,8 @@ mlir::LogicalResult ItaniumEHLowering::run() {
// TODO(cir): Move these to the base class if they are also needed for MSVC.
voidType = cir::VoidType::get(ctx);
voidPtrType = cir::PointerType::get(voidType);
+ programVoidPtrType = cir::PointerType::get(
+ voidType, cir::CIRDataLayout(mod).getProgramAddrSpace(ctx));
auto u8Type = cir::IntType::get(ctx, 8, /*isSigned=*/false);
u8PtrType = cir::PointerType::get(u8Type);
u32Type = cir::IntType::get(ctx, 32, /*isSigned=*/false);
@@ -289,11 +292,14 @@ void ItaniumEHLowering::ensureClangCallTerminate(mlir::Location loc) {
/// Ensure the __cxa_throw runtime function is declared in the module.
///
/// void __cxa_throw(void *exception, void *type_info, void *dtor);
+///
+/// The destructor pointer uses the program address space.
void ItaniumEHLowering::ensureCxaThrowDecl(mlir::Location loc) {
if (cxaThrowFunc)
return;
- auto throwFuncTy = cir::FuncType::get({voidPtrType, voidPtrType, voidPtrType},
- voidType, /*isVarArg=*/false);
+ auto throwFuncTy =
+ cir::FuncType::get({voidPtrType, voidPtrType, programVoidPtrType},
+ voidType, /*isVarArg=*/false);
cxaThrowFunc =
getOrCreateRuntimeFuncDecl(mod, loc, "__cxa_throw", throwFuncTy);
}
@@ -1000,16 +1006,18 @@ mlir::LogicalResult ItaniumEHLowering::lowerTryThrow(cir::TryThrowOp op) {
auto dtorFunc = mod.lookupSymbol<cir::FuncOp>(dtorAttr);
if (!dtorFunc)
return op.emitError("dtor symbol not found in module");
- auto dtorPtrTy = cir::PointerType::get(dtorFunc.getFunctionType());
+ auto dtorPtrTy = cir::PointerType::get(dtorFunc.getFunctionType(),
+ programVoidPtrType.getAddrSpace());
dtor =
cir::GetGlobalOp::create(builder, loc, dtorPtrTy, dtorAttr.getValue());
- if (dtor.getType() != voidPtrType)
- dtor = cir::CastOp::create(builder, loc, voidPtrType,
+ if (dtor.getType() != programVoidPtrType)
+ dtor = cir::CastOp::create(builder, loc, programVoidPtrType,
cir::CastKind::bitcast, dtor);
} else {
dtor = cir::ConstantOp::create(
builder, loc,
- cir::ConstPtrAttr::get(voidPtrType, builder.getI64IntegerAttr(0)));
+ cir::ConstPtrAttr::get(programVoidPtrType,
+ builder.getI64IntegerAttr(0)));
}
cir::TryCallOp::create(
diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
index fa5a253abe5d86..f22b2c78990dd1 100644
--- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/CIRABIRewriteContext.cpp
@@ -2533,7 +2533,8 @@ void CIRABIRewriteContext::rewriteFunctionAddress(cir::GetGlobalOp addrOp,
return;
// The verifier requires the retype even when nothing reads the address.
- addrOp.getAddr().setType(cir::PointerType::get(newFuncTy));
+ addrOp.getAddr().setType(
+ cir::PointerType::get(newFuncTy, oldPtrTy.getAddrSpace()));
if (addrOp.getAddr().use_empty())
return;
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index e0dd52a9268739..397b471f836ad5 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -5494,8 +5494,11 @@ mlir::LogicalResult CIRToLLVMThrowOpLowering::matchAndRewrite(
}
auto llvmPtrTy = mlir::LLVM::LLVMPointerType::get(rewriter.getContext());
+ cir::CIRDataLayout cirDataLayout(op->getParentOfType<mlir::ModuleOp>());
+ auto dtorPtrTy = mlir::LLVM::LLVMPointerType::get(
+ rewriter.getContext(), cirDataLayout.getProgramAddressSpace());
auto fnTy = mlir::LLVM::LLVMFunctionType::get(
- voidTy, {llvmPtrTy, llvmPtrTy, llvmPtrTy});
+ voidTy, {llvmPtrTy, llvmPtrTy, dtorPtrTy});
// Get or create `declare void @__cxa_throw(ptr, ptr, ptr)`
const llvm::StringRef fnName = "__cxa_throw";
@@ -5507,10 +5510,10 @@ mlir::LogicalResult CIRToLLVMThrowOpLowering::matchAndRewrite(
mlir::Value dtor;
if (op.getDtor()) {
- dtor = mlir::LLVM::AddressOfOp::create(rewriter, loc, llvmPtrTy,
+ dtor = mlir::LLVM::AddressOfOp::create(rewriter, loc, dtorPtrTy,
adaptor.getDtorAttr());
} else {
- dtor = mlir::LLVM::ZeroOp::create(rewriter, loc, llvmPtrTy);
+ dtor = mlir::LLVM::ZeroOp::create(rewriter, loc, dtorPtrTy);
}
auto cxaThrowCall = mlir::LLVM::CallOp::create(
diff --git a/clang/test/CIR/CodeGen/throw-dtor-addrspace.cpp b/clang/test/CIR/CodeGen/throw-dtor-addrspace.cpp
new file mode 100644
index 00000000000000..a427ed9b019b8c
--- /dev/null
+++ b/clang/test/CIR/CodeGen/throw-dtor-addrspace.cpp
@@ -0,0 +1,37 @@
+// RUN: %clang_cc1 -std=c++17 -triple spirv64-amd-amdhsa -fcxx-exceptions -fexceptions -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s
+// RUN: %clang_cc1 -std=c++17 -triple spirv64-amd-amdhsa -fcxx-exceptions -fexceptions -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s
+
+// The destructor pointer passed to __cxa_throw uses the program address space.
+
+struct X {
+ ~X();
+};
+
+struct Guard {
+ ~Guard();
+};
+
+void throw_int() { throw 1; }
+
+void throw_object() { throw X{}; }
+
+void throw_with_cleanup() {
+ Guard guard;
+ throw 2;
+}
+
+void throw_object_with_cleanup() {
+ Guard guard;
+ throw X{};
+}
+
+// CHECK-LABEL: define{{.*}} void @_Z9throw_intv()
+// CHECK: call {{.*}}void @__cxa_throw(ptr{{.*}} %{{.*}}, ptr {{.*}}@_ZTIi, ptr addrspace(4) null)
+// CHECK-LABEL: define{{.*}} void @_Z12throw_objectv()
+// CHECK: call {{.*}}void @__cxa_throw(ptr{{.*}} %{{.*}}, ptr {{.*}}@_ZTI1X, ptr addrspace(4) @_ZN1XD1Ev)
+// CHECK-LABEL: define{{.*}} void @_Z18throw_with_cleanupv()
+// CHECK: invoke {{.*}}void @__cxa_throw(ptr{{.*}} %{{.*}}, ptr {{.*}}@_ZTIi, ptr addrspace(4) null)
+// CHECK-LABEL: define{{.*}} void @_Z25throw_object_with_cleanupv()
+// CHECK: invoke {{.*}}void @__cxa_throw(ptr{{.*}} %{{.*}}, ptr {{.*}}@_ZTI1X, ptr addrspace(4) @_ZN1XD1Ev)
diff --git a/clang/test/CIR/IR/invalid-addrspace.cir b/clang/test/CIR/IR/invalid-addrspace.cir
index 8562aa7a090ab8..2be076acbf90a1 100644
--- a/clang/test/CIR/IR/invalid-addrspace.cir
+++ b/clang/test/CIR/IR/invalid-addrspace.cir
@@ -70,3 +70,17 @@ cir.func @get_global_unexpected_address_space() {
%0 = cir.get_global @global_default_as : !cir.ptr<!s32i, target_address_space(1)>
cir.return
}
+
+// -----
+
+module attributes {
+ dlti.dl_spec = #dlti.dl_spec<"dlti.program_memory_space" = 4 : ui64>
+} {
+cir.func private @fn_in_program_as()
+cir.func @get_global_function_address_space() {
+ %0 = cir.get_global @fn_in_program_as : !cir.ptr<!cir.func<()>, target_address_space(4)>
+ // expected-error@+1 {{result type address space does not match the address space of the global @fn_in_program_as}}
+ %1 = cir.get_global @fn_in_program_as : !cir.ptr<!cir.func<()>, target_address_space(2)>
+ cir.return
+}
+}
diff --git a/clang/test/CIR/Transforms/abi-lowering/spirv-get-global.cir b/clang/test/CIR/Transforms/abi-lowering/spirv-get-global.cir
new file mode 100644
index 00000000000000..54713a8bd18dfa
--- /dev/null
+++ b/clang/test/CIR/Transforms/abi-lowering/spirv-get-global.cir
@@ -0,0 +1,36 @@
+// RUN: cir-opt %s -cir-call-conv-lowering=target=spirv | FileCheck %s
+
+// A function address retyped for an sret rewrite keeps the program address
+// space.
+
+!s32i = !cir.int<s, 32>
+!rec_NT = !cir.struct<"NT" {data !s32i}>
+
+module attributes {
+ cir.triple = "spirv64-amd-amdhsa",
+ cir.lowering_options = #cir.lowering_options<
+ exceptions = false, threadsafe_statics = true, cuda = false,
+ cuda_is_device = false, hip = false, hlsl = false, sycl_is_device = true,
+ gpu_rdc = false, openmp = false, openmp_is_target_device = false,
+ clang_abi_compat = 17, long_double_size = 0, enable_noundef_attrs = true,
+ no_trapping_math = true, ssp_buffer_size = 8>,
+ cir.record_layouts = {NT = #cir.record_layout<
+ arg_passing_kind = cannot_pass_in_regs, has_trivial_dtor = false,
+ record_align = 4>},
+ dlti.dl_spec = #dlti.dl_spec<
+ #dlti.dl_entry<i32, dense<32>: vector<2xi64>>,
+ #dlti.dl_entry<i64, dense<64>: vector<2xi64>>,
+ #dlti.dl_entry<"dlti.program_memory_space", 4 : ui64>>
+} {
+ cir.func private @make_nt() -> !rec_NT cc(spir_function) requires_call_conv_lowering
+
+ cir.func @addr_sret() -> !cir.ptr<!cir.func<() -> !rec_NT>, target_address_space(4)> cc(spir_function) requires_call_conv_lowering {
+ %0 = cir.get_global @make_nt : !cir.ptr<!cir.func<() -> !rec_NT>, target_address_space(4)>
+ cir.return %0 : !cir.ptr<!cir.func<() -> !rec_NT>, target_address_space(4)>
+ }
+
+ // CHECK-LABEL: cir.func @addr_sret()
+ // CHECK-NEXT: %[[G:.*]] = cir.get_global @make_nt : !cir.ptr<!cir.func<(!cir.ptr<!rec_NT, target_address_space(4)>)>, target_address_space(4)>
+ // CHECK-NEXT: %[[C:.*]] = cir.cast bitcast %[[G]] : !cir.ptr<!cir.func<(!cir.ptr<!rec_NT, target_address_space(4)>)>, target_address_space(4)> -> !cir.ptr<!cir.func<() -> !rec_NT>, target_address_space(4)>
+ // CHECK-NEXT: cir.return %[[C]]
+}
</details>
Is this only relevant to SPIR-V for AMD? If not, could we also check other targets? Either way, should it be placed under a more specific CodeGen sub-folder?
Overall looks reasonable to me.
No, it affects any target with a non-zero program address space
Adjusted
steffenlarsen
aobolensk
Function pointers live in the program address space on targets like spirv64-amd-amdhsa, as in classic codegen
ABI lowering now also keeps that address space when it retypes a function address