https://github.com/erichkeane updated https://github.com/llvm/llvm-project/pull/242079
>From 45743f4386e5f810b0caa6e2ac366a97206f27a3 Mon Sep 17 00:00:00 2001 From: erichkeane <[email protected]> Date: Thu, 12 Nov 2026 14:12:12 -0800 Subject: [PATCH] [CIR] Abstract the 'load' of the type_info into vtable.get_type_info cir.vtable.get_type_info used to return the address of the vtable slot that holds the type_info pointer, and CIRGen loaded the pointer from it. This forced the entry-format into CIRGen, and doesn't work for vtable layouts (like relative layout) where the entries aren't pointers. This returns the pointer to the type_info itself, so that lowering can manage this correctly. The operation is still lowered by the C++ ABI lowering, which knows where the ABI places the type_info (entry -1 for the Itanium ABI). It now lowers to a new operation, cir.vtable.get_entry, which reads a pointer entry of a vtable by its index from the address point, and is lowered by LowerToLLVM. That way only LowerToLLVM needs to know how the entries of a vtable are stored. The LLVM IR for the lookup is now an inbounds GEP, like classic codegen emits, so the FIXME about the missing inbounds in typeid-most-derived.cpp is resolved. --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 56 ++++++++++++++++--- clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp | 17 +----- .../TargetLowering/LowerItaniumCXXABI.cpp | 15 ++--- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 26 +++++++++ .../CIR/CodeGenCXX/typeid-most-derived.cpp | 10 ++-- clang/test/CIR/CodeGenCXX/typeid.cpp | 18 ++---- clang/test/CIR/IR/vtable-get-entry.cir | 17 ++++++ .../CIR/Transforms/pure-ptr-arithmetic.cir | 2 +- 8 files changed, 107 insertions(+), 54 deletions(-) create mode 100644 clang/test/CIR/IR/vtable-get-entry.cir diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index dc3acaa6eeb542..b4ef21b84806bf 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -3842,31 +3842,69 @@ def CIR_VTableGetVirtualFnOp : CIR_Op<"vtable.get_virtual_fn", [ }]; } +//===----------------------------------------------------------------------===// +// VTableGetEntryOp +//===----------------------------------------------------------------------===// + +def CIR_VTableGetEntryOp : CIR_Op<"vtable.get_entry", [ + MemoryEffects<[MemRead]> +]> { + let summary = "Get a pointer entry from a vtable"; + let description = [{ + The `vtable.get_entry` operation reads an entry of an object's vtable that + holds a pointer, other than a virtual function. This is an ABI independent + abstraction of this operation: how the entry is stored and read is decided + when the operation is lowered. + + The `vptr` operand must be a `!cir.vptr` value, which would have been + returned by a previous call to `cir.vtable.get_vptr`. The `index` operand + is the index of the entry in the vtable, counted from the address point. + Entries that precede the address point have a negative index. + + The result is the pointer that the entry holds. + + Example: + ``` + %3 = cir.vtable.get_vptr %2 : !cir.ptr<!rec_A> -> !cir.ptr<!cir.vptr> + %4 = cir.load %3 : !cir.ptr<!cir.vptr>, !cir.vptr + %5 = cir.vtable.get_entry %4[-1] : !cir.vptr + -> !cir.ptr<!rec_std3A3Atype_info> + ``` + }]; + + let arguments = (ins CIR_VPtrType:$vptr, I64Attr:$index); + let results = (outs CIR_PointerType:$result); + + let assemblyFormat = [{ + $vptr `[` $index `]` attr-dict + `:` qualified(type($vptr)) `->` qualified(type($result)) + }]; +} + //===----------------------------------------------------------------------===// // VTableGetTypeInfoOp //===----------------------------------------------------------------------===// def CIR_VTableGetTypeInfoOp : CIR_Op<"vtable.get_type_info", [ - Pure + MemoryEffects<[MemRead]> ]> { - let summary = "Get the address of the type_info from the vtable"; + let summary = "Get the type_info of an object from its vtable"; let description = [{ - The `vtable.get_type_info` operation retreives the address of the dynamic - type_info/rtti object from an object's vtable. This is an ABI independent - abstraction of this operation. + The `vtable.get_type_info` operation retrieves the pointer to the dynamic + type_info/rtti object from an object's vtable (__vptr). This is an ABI + independent abstraction of this operation: where the entry is, and how it + is stored and read, is decided when the operation is lowered. The `vptr` operand must be a `!cir.vptr` value, which would have been returned by a previous call to `cir.vtable.get_vptr`. - The return type is a loadable pointer to a `type_info` struct. + The result is the pointer to the `type_info` struct. Example: ``` %5 = cir.vtable.get_vptr %2 : !cir.ptr<!rec_A> -> !cir.ptr<!cir.vptr> %6 = cir.load align(8) %5 : !cir.ptr<!cir.vptr>, !cir.vptr - %7 = cir.vtable.get_type_info %6 : !cir.vptr -> !cir.ptr<!cir.ptr<!rec_std3A3Atype_info>> - %8 = cir.load align(8) %7 : !cir.ptr<!cir.ptr<!rec_std3A3Atype_info>>, !cir.ptr<!rec_std3A3Atype_info> - + %7 = cir.vtable.get_type_info %6 : !cir.vptr -> !cir.ptr<!rec_std3A3Atype_info> ``` }]; diff --git a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp index cea4bae77be106..ff921abfb7edc3 100644 --- a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp @@ -1392,21 +1392,8 @@ mlir::Value CIRGenItaniumCXXABI::emitTypeid(CIRGenFunction &cgf, QualType srcTy, auto *classDecl = srcTy->castAsCXXRecordDecl(); mlir::Location loc = cgm.getLoc(classDecl->getSourceRange()); mlir::Value vptr = cgf.getVTablePtr(loc, thisPtr, classDecl); - mlir::Value vtbl; - - // TODO(cir): In classic codegen relative layouts cause us to do a - // 'load_relative' of -4 here. We probably don't want to reprensent this in - // CIR at all, but we should have the NYI here since this could be - // meaningful/notable for implementation of relative layout in the future. - if (cgm.getLangOpts().RelativeCXXABIVTables) - cgm.errorNYI("buildVTablePointer: isRelativeLayout"); - else - vtbl = cir::VTableGetTypeInfoOp::create( - cgf.getBuilder(), loc, cgf.getBuilder().getPointerTo(typeInfoPtrTy), - vptr); - - return cgf.getBuilder().createAlignedLoad(loc, typeInfoPtrTy, vtbl, - cgf.getPointerAlign()); + return cir::VTableGetTypeInfoOp::create(cgf.getBuilder(), loc, typeInfoPtrTy, + vptr); } mlir::Attribute CIRGenItaniumCXXABI::getAddrOfRTTIDescriptor(mlir::Location loc, diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp index 690018f9343a2d..9e323e8117dbf7 100644 --- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp +++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp @@ -882,17 +882,10 @@ LowerItaniumCXXABI::lowerDynamicCast(cir::DynamicCastOp op, mlir::Value LowerItaniumCXXABI::lowerVTableGetTypeInfo(cir::VTableGetTypeInfoOp op, mlir::OpBuilder &builder) const { - mlir::Location loc = op->getLoc(); - auto offset = cir::ConstantOp::create( - builder, op->getLoc(), cir::IntAttr::get(getPtrDiffCIRTy(lm), -1)); - - // Cast the vptr to type_info-ptr, so that we can go backwards 1 pointer. - auto vptrCast = cir::CastOp::create(builder, loc, op.getType(), - cir::CastKind::bitcast, op.getVptr()); - - return cir::PtrStrideOp::create(builder, loc, vptrCast.getType(), vptrCast, - offset) - .getResult(); + // The Itanium ABI always places the pointer to the type_info at entry -1 in + // the vtable. + return cir::VTableGetEntryOp::create(builder, op->getLoc(), op.getType(), + op.getVptr(), /*index=*/-1); } clang::CharUnits LowerItaniumCXXABI::getArrayCookieSizeImpl( diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 96a6c516dcff00..bfdf7a26d13cc7 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -5626,6 +5626,32 @@ mlir::LogicalResult CIRToLLVMVTableGetVirtualFnOpLowering::matchAndRewrite( return mlir::success(); } +mlir::LogicalResult CIRToLLVMVTableGetEntryOpLowering::matchAndRewrite( + cir::VTableGetEntryOp op, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const { + assert(!cir::MissingFeatures::vtableRelativeLayout()); + if (usesRelativeVTables(op->getParentOfType<mlir::ModuleOp>())) + return op.emitError( + "NYI: pointer entry lookup in a relative layout vtable"); + + mlir::Type resultType = getTypeConverter()->convertType(op.getType()); + if (!resultType) + return emitTypeConversionError(op, op.getType()); + + // The entry is the pointer itself: load it from its slot. + auto ptrTy = mlir::LLVM::LLVMPointerType::get(rewriter.getContext()); + mlir::Value index = mlir::LLVM::ConstantOp::create( + rewriter, op.getLoc(), rewriter.getI64Type(), + rewriter.getI64IntegerAttr(op.getIndex())); + mlir::Value slot = mlir::LLVM::GEPOp::create( + rewriter, op.getLoc(), ptrTy, ptrTy, adaptor.getVptr(), + llvm::ArrayRef<mlir::LLVM::GEPArg>{index}, + mlir::LLVM::GEPNoWrapFlags::inbounds); + rewriter.replaceOpWithNewOp<mlir::LLVM::LoadOp>( + op, resultType, slot, dataLayout.getTypeABIAlignment(ptrTy)); + return mlir::success(); +} + mlir::LogicalResult CIRToLLVMVTTAddrPointOpLowering::matchAndRewrite( cir::VTTAddrPointOp op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) const { diff --git a/clang/test/CIR/CodeGenCXX/typeid-most-derived.cpp b/clang/test/CIR/CodeGenCXX/typeid-most-derived.cpp index 4ab2379178cb13..a95e04f636df69 100644 --- a/clang/test/CIR/CodeGenCXX/typeid-most-derived.cpp +++ b/clang/test/CIR/CodeGenCXX/typeid-most-derived.cpp @@ -4,8 +4,6 @@ // RUN: %clang_cc1 %s -triple x86_64-linux-gnu -Wno-unused-value -std=c++11 -fclangir -emit-llvm -o - -std=c++11 | FileCheck %s --check-prefixes=CIR-TO-LLVM // RUN: %clang_cc1 %s -triple x86_64-linux-gnu -Wno-unused-value -std=c++11 -emit-llvm -o - -std=c++11 | FileCheck %s --check-prefixes=LLVM -// FIXME: missing `inbounds` in `getelementptr` in CIR-TO-LLVM output. - namespace std { class type_info {}; } @@ -95,7 +93,7 @@ void base_ref(Base &b) { typeid(b); } // LLVM: getelementptr inbounds ptr, ptr // LLVM: ret void // CIR-TO-LLVM-LABEL: define {{.*}}void @_Z8base_refR4Base -// CIR-TO-LLVM: getelementptr ptr, ptr +// CIR-TO-LLVM: getelementptr inbounds ptr, ptr // CIR-TO-LLVM: ret void // Not most derived @@ -107,7 +105,7 @@ void base_deref(Base *b) { ForceLoadingVTable(typeid(*b)); } // LLVM: getelementptr inbounds ptr, ptr // LLVM: ret void // CIR-TO-LLVM-LABEL: define {{.*}}void @_Z10base_derefP4Base -// CIR-TO-LLVM: getelementptr ptr, ptr +// CIR-TO-LLVM: getelementptr inbounds ptr, ptr // CIR-TO-LLVM: ret void // Not most derived @@ -119,7 +117,7 @@ void nonfinal_ref(NonFinal &d) { typeid(d); } // LLVM: getelementptr inbounds ptr, ptr // LLVM: ret void // CIR-TO-LLVM-LABEL: define {{.*}}void @_Z12nonfinal_refR8NonFinal -// CIR-TO-LLVM: getelementptr ptr, ptr +// CIR-TO-LLVM: getelementptr inbounds ptr, ptr // CIR-TO-LLVM: ret void // Not most derived @@ -131,5 +129,5 @@ void nonfinal_deref(NonFinal *d) { ForceLoadingVTable(typeid(*d)); } // LLVM: getelementptr inbounds ptr, ptr // LLVM: ret void // CIR-TO-LLVM-LABEL: define {{.*}}void @_Z14nonfinal_derefP8NonFinal -// CIR-TO-LLVM: getelementptr ptr, ptr +// CIR-TO-LLVM: getelementptr inbounds ptr, ptr // CIR-TO-LLVM: ret void diff --git a/clang/test/CIR/CodeGenCXX/typeid.cpp b/clang/test/CIR/CodeGenCXX/typeid.cpp index aca39540acc8f5..a2dac15cea7ac0 100644 --- a/clang/test/CIR/CodeGenCXX/typeid.cpp +++ b/clang/test/CIR/CodeGenCXX/typeid.cpp @@ -42,14 +42,11 @@ extern A &a; // CIR-NEXT: %[[LOAD_GLOB_A:.*]] = cir.load %[[GET_GLOB_A]] : !cir.ptr<!cir.ptr<!rec_Test13A3AA>>, !cir.ptr<!rec_Test13A3AA> // CIR-NEXT: %[[GET_VPTR:.*]] = cir.vtable.get_vptr %[[LOAD_GLOB_A]] : !cir.ptr<!rec_Test13A3AA> -> !cir.ptr<!cir.vptr> // CIR-NEXT: %[[LOAD_VPTR:.*]] = cir.load align(8) %[[GET_VPTR]] : !cir.ptr<!cir.vptr>, !cir.vptr -// CIR-BEFORE: %[[GET_TYPEINFO:.*]] = cir.vtable.get_type_info %[[LOAD_VPTR]] : !cir.vptr -> !cir.ptr<!cir.ptr<!rec_std3A3Atype_info>> +// CIR-BEFORE: %[[TYPEINFO:.*]] = cir.vtable.get_type_info %[[LOAD_VPTR]] : !cir.vptr -> !cir.ptr<!rec_std3A3Atype_info> // -// CIR-AFTER: %[[NEG_ONE:.*]] = cir.const #cir.int<-1> : !s64i -// CIR-AFTER: %[[VPTR_CAST:.*]] = cir.cast bitcast %[[LOAD_VPTR]] : !cir.vptr -> !cir.ptr<!cir.ptr<!rec_std3A3Atype_info>> -// CIR-AFTER: %[[GET_TYPEINFO:.*]] = cir.ptr_stride %[[VPTR_CAST]], %[[NEG_ONE]] : (!cir.ptr<!cir.ptr<!rec_std3A3Atype_info>>, !s64i) -> !cir.ptr<!cir.ptr<!rec_std3A3Atype_info>> +// CIR-AFTER: %[[TYPEINFO:.*]] = cir.vtable.get_entry %[[LOAD_VPTR]][-1] : !cir.vptr -> !cir.ptr<!rec_std3A3Atype_info> // -// CIR-NEXT: %[[LOAD_TYPEINFO:.*]] = cir.load align(8) %[[GET_TYPEINFO]] : !cir.ptr<!cir.ptr<!rec_std3A3Atype_info>>, !cir.ptr<!rec_std3A3Atype_info> -// CIR-NEXT: cir.store align(8) %[[LOAD_TYPEINFO]], %[[GET_GLOB_ATI]] : !cir.ptr<!rec_std3A3Atype_info>, !cir.ptr<!cir.ptr<!rec_std3A3Atype_info>> +// CIR-NEXT: cir.store align(8) %[[TYPEINFO]], %[[GET_GLOB_ATI]] : !cir.ptr<!rec_std3A3Atype_info>, !cir.ptr<!cir.ptr<!rec_std3A3Atype_info>> // CIR-AFTER: cir.return // CIR-NEXT:} // CIR: cir.global "private" constant external @_ZN5Test11aE : !cir.ptr<!rec_Test13A3AA> @@ -79,14 +76,11 @@ const char *f(void *arg) { // CIR: %[[GETVPTR:.*]] = cir.vtable.get_vptr %[[ARG_CAST]] // CIR-NEXT: %[[LOAD_VPTR:.*]] = cir.load{{.*}} %[[GETVPTR]] : !cir.ptr<!cir.vptr>, !cir.vptr // - // CIR-BEFORE: %[[GET_TYPEINFO:.*]] = cir.vtable.get_type_info %[[LOAD_VPTR]] : !cir.vptr -> !cir.ptr<!cir.ptr<!rec_std3A3Atype_info>> + // CIR-BEFORE: %[[TYPEINFO:.*]] = cir.vtable.get_type_info %[[LOAD_VPTR]] : !cir.vptr -> !cir.ptr<!rec_std3A3Atype_info> // - // CIR-AFTER: %[[NEG_ONE:.*]] = cir.const #cir.int<-1> : !s64i - // CIR-AFTER: %[[VPTR_CAST:.*]] = cir.cast bitcast %[[LOAD_VPTR]] : !cir.vptr -> !cir.ptr<!cir.ptr<!rec_std3A3Atype_info>> - // CIR-AFTER: %[[GET_TYPEINFO:.*]] = cir.ptr_stride %[[VPTR_CAST]], %[[NEG_ONE]] : (!cir.ptr<!cir.ptr<!rec_std3A3Atype_info>>, !s64i) -> !cir.ptr<!cir.ptr<!rec_std3A3Atype_info>> + // CIR-AFTER: %[[TYPEINFO:.*]] = cir.vtable.get_entry %[[LOAD_VPTR]][-1] : !cir.vptr -> !cir.ptr<!rec_std3A3Atype_info> // - // CIR-NEXT: %[[LOAD_TYPEINFO:.*]] = cir.load{{.*}}%[[GET_TYPEINFO]] - // CIR-NEXT: cir.call @_ZNKSt9type_info4nameEv(%[[LOAD_TYPEINFO]]) + // CIR-NEXT: cir.call @_ZNKSt9type_info4nameEv(%[[TYPEINFO]]) // LLVM: br i1 // LLVM: invoke void @__cxa_bad_typeid() diff --git a/clang/test/CIR/IR/vtable-get-entry.cir b/clang/test/CIR/IR/vtable-get-entry.cir new file mode 100644 index 00000000000000..9ae04371d7dcef --- /dev/null +++ b/clang/test/CIR/IR/vtable-get-entry.cir @@ -0,0 +1,17 @@ +// RUN: cir-opt %s --verify-roundtrip | FileCheck %s + +!rec_std3A3Atype_info = !cir.struct<"std::type_info" incomplete> + +// CHECK-LABEL: cir.func @get_entry +cir.func @get_entry(%arg0: !cir.vptr) -> !cir.ptr<!rec_std3A3Atype_info> { + // CHECK: cir.vtable.get_entry %arg0[-1] : !cir.vptr -> !cir.ptr<!rec_std3A3Atype_info> + %0 = cir.vtable.get_entry %arg0[-1] : !cir.vptr -> !cir.ptr<!rec_std3A3Atype_info> + cir.return %0 : !cir.ptr<!rec_std3A3Atype_info> +} + +// CHECK-LABEL: cir.func @get_type_info +cir.func @get_type_info(%arg0: !cir.vptr) -> !cir.ptr<!rec_std3A3Atype_info> { + // CHECK: cir.vtable.get_type_info %arg0 : !cir.vptr -> !cir.ptr<!rec_std3A3Atype_info> + %0 = cir.vtable.get_type_info %arg0 : !cir.vptr -> !cir.ptr<!rec_std3A3Atype_info> + cir.return %0 : !cir.ptr<!rec_std3A3Atype_info> +} diff --git a/clang/test/CIR/Transforms/pure-ptr-arithmetic.cir b/clang/test/CIR/Transforms/pure-ptr-arithmetic.cir index ce22a8aac0c92f..f012071ff3ff33 100644 --- a/clang/test/CIR/Transforms/pure-ptr-arithmetic.cir +++ b/clang/test/CIR/Transforms/pure-ptr-arithmetic.cir @@ -51,7 +51,7 @@ cir.func @dead_vtable_get_virtual_fn(%arg0: !cir.vptr) { // CHECK-LABEL: @dead_vtable_get_type_info // CHECK-NOT: cir.vtable.get_type_info cir.func @dead_vtable_get_type_info(%arg0: !cir.vptr) { - %0 = cir.vtable.get_type_info %arg0 : !cir.vptr -> !cir.ptr<!cir.ptr<!rec_S>> + %0 = cir.vtable.get_type_info %arg0 : !cir.vptr -> !cir.ptr<!rec_S> cir.return } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
