https://github.com/aobolensk updated https://github.com/llvm/llvm-project/pull/240548
>From 909a7d08cd2f8428fdab07f03dd8a578b39ce45e Mon Sep 17 00:00:00 2001 From: Arseniy Obolenskiy <[email protected]> Date: Mon, 9 Nov 2026 22:14:26 +0100 Subject: [PATCH 1/3] [CIR] Use the global address space for the VTT parameter Match classic CodeGen, which types the VTT as a pointer into the target global address space Let cir.vtt.address_point carry that address space --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 3 ++- clang/lib/CIR/CodeGen/CIRGenClass.cpp | 4 +++- clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp | 20 ++++++++++--------- clang/lib/CIR/CodeGen/CIRGenModule.h | 20 +++++++++---------- clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 14 +++++++++++-- 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 93c91a48bd0c7b..f54be6b69cf327 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -3868,7 +3868,8 @@ def CIR_VTTAddrPointOp : CIR_Op<"vtt.address_point", [ The second parameter `offset` provides a virtual step to adjust to the actual address point of the vtable. - The return type is always a `!cir.ptr<!cir.ptr<void>>`. + The return type is always a `!cir.ptr<!cir.ptr<void>>`. Either pointer + level may carry an address space. Example: ``` diff --git a/clang/lib/CIR/CodeGen/CIRGenClass.cpp b/clang/lib/CIR/CodeGen/CIRGenClass.cpp index 4a703262577ad0..fd93328c3c96a4 100644 --- a/clang/lib/CIR/CodeGen/CIRGenClass.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenClass.cpp @@ -1203,7 +1203,9 @@ mlir::Value CIRGenFunction::getVTTParameter(GlobalDecl gd, bool forVirtualBase, // We're the complete constructor, so get the VTT by name. cir::GlobalOp vtt = cgm.getVTables().getAddrOfVTT(rd); return builder.createVTTAddrPoint( - loc, builder.getPointerTo(cgm.voidPtrTy), + loc, + builder.getPointerTo(convertType(getContext().VoidPtrTy), + vtt.getAddrSpaceAttr()), mlir::FlatSymbolRefAttr::get(vtt.getSymNameAttr()), subVTTIndex); } } diff --git a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp index f70e6e7cd2da09..47205b9c64265d 100644 --- a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp @@ -282,14 +282,14 @@ CIRGenItaniumCXXABI::buildStructorSignature( // All parameters are already in place except VTT, which goes after 'this'. // These are clang types, so we don't need to worry about sret yet. - // Check if we need to add a VTT parameter (which has type void **). + // Check if we need to add a VTT parameter (which has type global void **). if ((isa<CXXConstructorDecl>(gd.getDecl()) ? gd.getCtorType() == Ctor_Base : gd.getDtorType() == Dtor_Base) && cast<CXXMethodDecl>(gd.getDecl())->getParent()->getNumVBases() != 0) { - assert(!cir::MissingFeatures::addressSpace()); + LangAS as = cgm.getGlobalVarAddressSpace(nullptr); + QualType q = astContext.getAddrSpaceQualType(astContext.VoidPtrTy, as); argTys.insert(argTys.begin() + 1, - astContext.getPointerType( - CanQualType::CreateUnsafe(astContext.VoidPtrTy))); + astContext.getPointerType(CanQualType::CreateUnsafe(q))); return AddedStructorArgCounts::withPrefix(1); } @@ -418,8 +418,9 @@ void CIRGenItaniumCXXABI::addImplicitStructorParams(CIRGenFunction &cgf, ASTContext &astContext = cgm.getASTContext(); // FIXME: avoid the fake decl - assert(!cir::MissingFeatures::addressSpace()); - QualType t = astContext.getPointerType(astContext.VoidPtrTy); + LangAS as = cgm.getGlobalVarAddressSpace(nullptr); + QualType q = astContext.getAddrSpaceQualType(astContext.VoidPtrTy, as); + QualType t = astContext.getPointerType(q); auto *vttDecl = ImplicitParamDecl::Create( astContext, /*DC=*/nullptr, md->getLocation(), &astContext.Idents.get("vtt"), t, ImplicitParamKind::CXXVTT); @@ -471,9 +472,10 @@ CIRGenCXXABI::AddedStructorArgs CIRGenItaniumCXXABI::getImplicitConstructorArgs( // some targets. mlir::Value vtt = cgf.getVTTParameter(GlobalDecl(d, type), forVirtualBase, delegating); - QualType vttTy = - cgm.getASTContext().getPointerType(cgm.getASTContext().VoidPtrTy); - assert(!cir::MissingFeatures::addressSpace()); + ASTContext &astContext = cgm.getASTContext(); + LangAS as = cgm.getGlobalVarAddressSpace(nullptr); + QualType q = astContext.getAddrSpaceQualType(astContext.VoidPtrTy, as); + QualType vttTy = astContext.getPointerType(q); return AddedStructorArgs::withPrefix({{vtt, vttTy}}); } diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.h b/clang/lib/CIR/CodeGen/CIRGenModule.h index 7933c3356a4f69..2d606eb977a5a5 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.h +++ b/clang/lib/CIR/CodeGen/CIRGenModule.h @@ -524,6 +524,16 @@ class CIRGenModule : public CIRGenTypeCache { /// space, which may lead to type mismatches in other parts of the IR. LangAS getLangTempAllocaAddressSpace() const; + /// Return the AST address space of the underlying global variable for D, as + /// determined by its declaration. Normally this is the same as the address + /// space of D's type, but in CUDA, address spaces are associated with + /// declarations, not types. If D is nullptr, return the default address + /// space for global variable. + /// + /// For languages without explicit address spaces, if D has default address + /// space, target-specific global or constant address space may be returned. + LangAS getGlobalVarAddressSpace(const VarDecl *decl); + /// Return the CIR address space attribute for a global in \p as, honoring /// the target address space map where the LangAS fold would lose it. mlir::ptr::MemorySpaceAttrInterface getGlobalCIRAddressSpace(LangAS as); @@ -1049,16 +1059,6 @@ class CIRGenModule : public CIRGenTypeCache { /// Create cir::AnnotationAttr for a single AnnotateAttr on a global. cir::AnnotationAttr emitAnnotateAttr(const clang::AnnotateAttr *aa); - - /// Return the AST address space of the underlying global variable for D, as - /// determined by its declaration. Normally this is the same as the address - /// space of D's type, but in CUDA, address spaces are associated with - /// declarations, not types. If D is nullptr, return the default address - /// space for global variable. - /// - /// For languages without explicit address spaces, if D has default address - /// space, target-specific global or constant address space may be returned. - LangAS getGlobalVarAddressSpace(const VarDecl *decl); }; } // namespace CIRGen diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index 35f390afffe8ba..76c7a4ef8828e7 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -2855,6 +2855,12 @@ cir::VTTAddrPointOp::verifySymbolUses(SymbolTableCollection &symbolTable) { if (!op) return emitOpError("'") << name << "' does not reference a valid cir.global"; + auto resultType = mlir::cast<cir::PointerType>(getAddr().getType()); + if (op.getAddrSpaceAttr() != resultType.getAddrSpace()) + return emitOpError() + << "result type address space does not match the address " + "space of the global @" + << name; std::optional<mlir::Attribute> init = op.getInitialValue(); if (!init) return success(); @@ -2874,9 +2880,13 @@ LogicalResult cir::VTTAddrPointOp::verify() { if (getSymAddr()) return success(); - mlir::Type resultType = getAddr().getType(); + // Both pointer levels may carry an address space. + auto resultType = mlir::cast<cir::PointerType>(getAddr().getType()); + auto eltTy = mlir::dyn_cast<cir::PointerType>(resultType.getPointee()); mlir::Type resTy = cir::PointerType::get( - cir::PointerType::get(cir::VoidType::get(getContext()))); + cir::PointerType::get(cir::VoidType::get(getContext()), + eltTy ? eltTy.getAddrSpace() : nullptr), + resultType.getAddrSpace()); if (resultType != resTy) return emitOpError("result type must be ") >From c37c09e418e640dc18e7dbd9ff827bc9ad42313d Mon Sep 17 00:00:00 2001 From: Arseniy Obolenskiy <[email protected]> Date: Mon, 9 Nov 2026 22:36:37 +0100 Subject: [PATCH 2/3] more tests --- clang/test/CIR/CodeGenCUDA/vtt-addrspace.cu | 49 +++++++++++++++++++++ clang/test/CIR/IR/invalid-vtable.cir | 11 +++++ 2 files changed, 60 insertions(+) create mode 100644 clang/test/CIR/CodeGenCUDA/vtt-addrspace.cu diff --git a/clang/test/CIR/CodeGenCUDA/vtt-addrspace.cu b/clang/test/CIR/CodeGenCUDA/vtt-addrspace.cu new file mode 100644 index 00000000000000..7eae03c2e8cb7e --- /dev/null +++ b/clang/test/CIR/CodeGenCUDA/vtt-addrspace.cu @@ -0,0 +1,49 @@ +#include "Inputs/cuda.h" + +// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -x cuda -fcuda-is-device \ +// RUN: -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s +// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -x cuda -fcuda-is-device \ +// RUN: -fclangir -emit-llvm %s -o %t-cir.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s +// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -x cuda -fcuda-is-device \ +// RUN: -emit-llvm %s -o %t.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s + +// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -x hip -fcuda-is-device \ +// RUN: -fclangir -emit-cir %s -o %t-amdgcn.cir +// RUN: FileCheck --check-prefix=CIR --input-file=%t-amdgcn.cir %s + +// RUN: %clang_cc1 -triple spirv64-amd-amdhsa -x hip -fcuda-is-device \ +// RUN: -fclangir -emit-cir %s -o %t-spirv.cir +// RUN: FileCheck --check-prefix=CIR-SPV --input-file=%t-spirv.cir %s + +// The VTT parameter points into the globals address space. The VTT address +// point keeps the address space of the VTT global and is cast at the call. + +struct A { int a; }; +struct B : virtual A { __device__ B() {} }; +struct C : B { __device__ C() {} }; + +__device__ void f() { C c; } + +// CIR-LABEL: cir.func {{.*}} @_ZN1CC1Ev( +// CIR: %[[VTT:.*]] = cir.vtt.address_point @_ZTT1C, offset = 1 -> !cir.ptr<!cir.ptr<!void>> +// CIR: %[[CAST:.*]] = cir.cast address_space %[[VTT]] : !cir.ptr<!cir.ptr<!void>> -> !cir.ptr<!cir.ptr<!void>, target_address_space(1)> +// CIR: cir.call @_ZN1BC2Ev(%{{.*}}, %[[CAST]]) + +// CIR-LABEL: cir.func {{.*}} @_ZN1BC2Ev({{.*}}, %arg1: !cir.ptr<!cir.ptr<!void>, target_address_space(1)> +// CIR: cir.vtt.address_point %{{.*}} : !cir.ptr<!cir.ptr<!void>, target_address_space(1)>, offset = 0 -> !cir.ptr<!cir.ptr<!void>, target_address_space(1)> + +// LLVM-LABEL: define {{.*}}void @_ZN1CC1Ev( +// LLVM: call void @_ZN1BC2Ev(ptr {{.*}}, ptr addrspace(1) noundef addrspacecast (ptr getelementptr inbounds nuw (i8, ptr @_ZTT1C, i64 8) to ptr addrspace(1))) + +// LLVM-LABEL: define linkonce_odr void @_ZN1BC2Ev(ptr {{.*}}, ptr addrspace(1) noundef %{{.*}}) + +// CIR-SPV-LABEL: cir.func {{.*}} @_ZN1CC1Ev( +// CIR-SPV: %[[VTT:.*]] = cir.vtt.address_point @_ZTT1C, offset = 1 -> !cir.ptr<!cir.ptr<!void, target_address_space(4)>> +// CIR-SPV: %[[CAST:.*]] = cir.cast address_space %[[VTT]] : !cir.ptr<!cir.ptr<!void, target_address_space(4)>> -> !cir.ptr<!cir.ptr<!void, target_address_space(4)>, target_address_space(1)> +// CIR-SPV: cir.call @_ZN1BC2Ev(%{{.*}}, %[[CAST]]) + +// CIR-SPV-LABEL: cir.func {{.*}} @_ZN1BC2Ev({{.*}}, %arg1: !cir.ptr<!cir.ptr<!void, target_address_space(4)>, target_address_space(1)> +// CIR-SPV: cir.vtt.address_point %{{.*}} : !cir.ptr<!cir.ptr<!void, target_address_space(4)>, target_address_space(1)>, offset = 0 -> !cir.ptr<!cir.ptr<!void, target_address_space(4)>, target_address_space(1)> diff --git a/clang/test/CIR/IR/invalid-vtable.cir b/clang/test/CIR/IR/invalid-vtable.cir index f444a0ee5b539d..59b2c4faa22655 100644 --- a/clang/test/CIR/IR/invalid-vtable.cir +++ b/clang/test/CIR/IR/invalid-vtable.cir @@ -132,3 +132,14 @@ cir.func @bad_return_type_for_vtt_addrpoint() { %0 = cir.vtt.address_point @_ZTT1C, offset = 1 -> !cir.ptr<!u8i> cir.return } + +// ----- + +!u8i = !cir.int<u, 8> +!void = !cir.void +cir.global linkonce_odr @_ZTT1C = #cir.const_array<[#cir.global_view<@_ZTV1C, [0 : i32, 3 : i32]> : !cir.ptr<!u8i>, #cir.global_view<@_ZTC1C0_1B, [0 : i32, 3 : i32]> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 2> align(8) +cir.func @vtt_addrpoint_addrspace_mismatch() { + // expected-error @below {{'cir.vtt.address_point' op result type address space does not match the address space of the global @_ZTT1C}} + %0 = cir.vtt.address_point @_ZTT1C, offset = 1 -> !cir.ptr<!cir.ptr<!void>, target_address_space(1)> + cir.return +} >From 9b512e6279f830a2621d9dc13694bd1660304eb2 Mon Sep 17 00:00:00 2001 From: Arseniy Obolenskiy <[email protected]> Date: Tue, 10 Nov 2026 12:04:46 +0100 Subject: [PATCH 3/3] comment --- clang/lib/CIR/CodeGen/CIRGenClass.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenClass.cpp b/clang/lib/CIR/CodeGen/CIRGenClass.cpp index fd93328c3c96a4..be173f9299e949 100644 --- a/clang/lib/CIR/CodeGen/CIRGenClass.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenClass.cpp @@ -1202,11 +1202,11 @@ mlir::Value CIRGenFunction::getVTTParameter(GlobalDecl gd, bool forVirtualBase, } else { // We're the complete constructor, so get the VTT by name. cir::GlobalOp vtt = cgm.getVTables().getAddrOfVTT(rd); - return builder.createVTTAddrPoint( - loc, - builder.getPointerTo(convertType(getContext().VoidPtrTy), - vtt.getAddrSpaceAttr()), - mlir::FlatSymbolRefAttr::get(vtt.getSymNameAttr()), subVTTIndex); + mlir::Type voidPtrTy = convertType(getContext().VoidPtrTy); + cir::PointerType vttTy = + builder.getPointerTo(voidPtrTy, vtt.getAddrSpaceAttr()); + auto vttSym = mlir::FlatSymbolRefAttr::get(vtt.getSymNameAttr()); + return builder.createVTTAddrPoint(loc, vttTy, vttSym, subVTTIndex); } } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
