[clang] [CIR] Introduce cir.vtable.get_offset to handle offset mechanism (PR #242032)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 12 12:13:18 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clangir

Author: Erich Keane (erichkeane)

<details>
<summary>Changes</summary>

In anticipation of the relative-vtable implementation, this patch replaces the vtable pointer offset math with its own operation that replaces all of the offset calculations, and re-introduces them during LowerToLLVM.

Eventually, this will unify the two 'types' (pointer vs diff) that comes out when using relative vtable lowering.

Additionally, this patch converts the dyn-cast operation to remove its relative-flag and just use this get_offset functionality instead.

---

Patch is 31.73 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/242032.diff


15 Files Affected:

- (modified) clang/include/clang/CIR/Dialect/IR/CIRDialect.h (+9) 
- (modified) clang/include/clang/CIR/Dialect/IR/CIROps.td (+40-10) 
- (modified) clang/lib/CIR/CodeGen/CIRGenBuilder.h (+6-7) 
- (modified) clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp (+14-33) 
- (modified) clang/lib/CIR/Dialect/IR/CIRDialect.cpp (+15) 
- (modified) clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp (+15-42) 
- (modified) clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp (+28-7) 
- (modified) clang/test/CIR/CodeGen/delegating-ctor.cpp (+1-5) 
- (modified) clang/test/CIR/CodeGen/dynamic-cast.cpp (+2-5) 
- (modified) clang/test/CIR/CodeGen/vbase.cpp (+1-5) 
- (modified) clang/test/CIR/CodeGen/vtt.cpp (+3-15) 
- (modified) clang/test/CIR/CodeGenCXX/virtual-base-cast.cpp (+2-6) 
- (modified) clang/test/CIR/CodeGenCXX/vtable-virt-thunk-adj.cpp (+2-10) 
- (modified) clang/test/CIR/IR/dynamic-cast.cir (-10) 
- (added) clang/test/CIR/IR/vtable-get-offset.cir (+10) 


``````````diff
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialect.h b/clang/include/clang/CIR/Dialect/IR/CIRDialect.h
index 04abe65453993..c128b79e11c23 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRDialect.h
+++ b/clang/include/clang/CIR/Dialect/IR/CIRDialect.h
@@ -115,6 +115,15 @@ namespace cir {
 /// its allocated type.  Addressing has to keep using \p addr, whose type and
 /// address space may differ from the alloca's own result.
 AllocaOp getUnderlyingAlloca(mlir::Value addr);
+
+/// Whether the C++ vtables of \p module use the relative layout.
+bool usesRelativeVTables(mlir::ModuleOp mod);
+
+/// The size in bytes of an entry of a C++ vtable in \p module. Operations
+/// address vtable entries by index, so this is the one place that relates the
+/// index of an entry to its distance from the address point.
+uint64_t getVTableEntrySize(mlir::ModuleOp mod,
+                            const mlir::DataLayout &dataLayout);
 } // namespace cir
 
 #endif // CLANG_CIR_DIALECT_IR_CIRDIALECT_H
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index dc3acaa6eeb54..66dfcad03077f 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -491,17 +491,11 @@ def CIR_DynamicCastOp : CIR_Op<"dyn_cast"> {
     cast operation. It is an optional `#cir.dyn_cast_info` attribute that is
     only present when the operation models a down-cast or a side-cast.
 
-    The `relative_layout` argument specifies whether the Itanium C++ ABI vtable
-    uses relative layout. It is only meaningful when the operation models a
-    cast-to-complete operation.
-
     Examples:
 
     ```
     %0 = cir.dyn_cast ptr %p : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived>
-    %1 = cir.dyn_cast ptr relative_layout %p : !cir.ptr<!rec_Base>
-              -> !cir.ptr<!rec_Derived>
-    %2 = cir.dyn_cast ref %r : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived>
+    %1 = cir.dyn_cast ref %r : !cir.ptr<!rec_Base> -> !cir.ptr<!rec_Derived>
               #cir.dyn_cast_info<
                 srcRtti = #cir.global_view<@_ZTI4Base> : !cir.ptr<!u8i>,
                 destRtti = #cir.global_view<@_ZTI7Derived> : !cir.ptr<!u8i>,
@@ -515,8 +509,7 @@ def CIR_DynamicCastOp : CIR_Op<"dyn_cast"> {
   let arguments = (ins
     CIR_DynamicCastKindAttr:$kind,
     CIR_PtrToRecordType:$src,
-    OptionalAttr<CIR_DynamicCastInfoAttr>:$info,
-    UnitAttr:$relative_layout
+    OptionalAttr<CIR_DynamicCastInfoAttr>:$info
   );
 
   let results = (outs
@@ -524,7 +517,7 @@ def CIR_DynamicCastOp : CIR_Op<"dyn_cast"> {
   );
 
   let assemblyFormat = [{
-    enum($kind) (`relative_layout` $relative_layout^)? $src
+    enum($kind) $src
     `:` qualified(type($src)) `->` qualified(type($result))
     (qualified($info)^)? attr-dict
   }];
@@ -3842,6 +3835,43 @@ def CIR_VTableGetVirtualFnOp : CIR_Op<"vtable.get_virtual_fn", [
   }];
 }
 
+//===----------------------------------------------------------------------===//
+// VTableGetOffsetOp
+//===----------------------------------------------------------------------===//
+
+def CIR_VTableGetOffsetOp : CIR_Op<"vtable.get_offset", [
+  MemoryEffects<[MemRead]>
+]> {
+  let summary = "Get an offset stored in a vtable";
+  let description = [{
+    The `vtable.get_offset` operation reads an offset that is stored in an
+    object's vtable, such as the offset of a virtual base class or a virtual
+    adjustment for a thunk, in an ABI independent way.
+
+    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 value of the offset, in bytes.
+
+    Example:
+    ```
+    %3 = cir.vtable.get_vptr %2 : !cir.ptr<!rec_D> -> !cir.ptr<!cir.vptr>
+    %4 = cir.load %3 : !cir.ptr<!cir.vptr>, !cir.vptr
+    %5 = cir.vtable.get_offset %4[-4] : !cir.vptr -> !s64i
+    ```
+  }];
+
+  let arguments = (ins CIR_VPtrType:$vptr, I64Attr:$index);
+  let results = (outs CIR_IntType:$result);
+
+  let assemblyFormat = [{
+    $vptr `[` $index `]` attr-dict
+    `:` qualified(type($vptr)) `->` qualified(type($result))
+  }];
+}
+
 //===----------------------------------------------------------------------===//
 // VTableGetTypeInfoOp
 //===----------------------------------------------------------------------===//
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
index 33feccefb8073..e756ac6087e2a 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
@@ -462,17 +462,16 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
                             cir::DynamicCastInfoAttr info) {
     auto castKind =
         isRefCast ? cir::DynamicCastKind::Ref : cir::DynamicCastKind::Ptr;
-    return cir::DynamicCastOp::create(*this, loc, destType, castKind, src, info,
-                                      /*relative_layout=*/false);
+    return cir::DynamicCastOp::create(*this, loc, destType, castKind, src,
+                                      info);
   }
 
-  mlir::Value createDynCastToVoid(mlir::Location loc, mlir::Value src,
-                                  bool vtableUseRelativeLayout) {
+  mlir::Value createDynCastToVoid(mlir::Location loc, mlir::Value src) {
     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);
+    return cir::DynamicCastOp::create(*this, loc, destTy,
+                                      cir::DynamicCastKind::Ptr, src,
+                                      cir::DynamicCastInfoAttr{});
   }
 
   //===--------------------------------------------------------------------===//
diff --git a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
index cea4bae77be10..201e9dbddd95b 100644
--- a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
@@ -1766,31 +1766,24 @@ bool CIRGenItaniumCXXABI::isVirtualOffsetNeededForVTableField(
   return needsVTTParameter(cgf.curGD);
 }
 
+static int64_t getVTableEntryIndex(CIRGenModule &cgm, int64_t byteOffset) {
+  int64_t entrySize =
+      cir::getVTableEntrySize(cgm.getModule(), cgm.getDataLayout().layout);
+  assert(byteOffset % entrySize == 0 && "unaligned vtable entry offset");
+  return byteOffset / entrySize;
+}
+
 mlir::Value CIRGenItaniumCXXABI::getVirtualBaseClassOffset(
     mlir::Location loc, CIRGenFunction &cgf, Address thisAddr,
     const CXXRecordDecl *classDecl, const CXXRecordDecl *baseClassDecl) {
   CIRGenBuilderTy &builder = cgf.getBuilder();
   mlir::Value vtablePtr = cgf.getVTablePtr(loc, thisAddr, classDecl);
-  mlir::Value vtableBytePtr = builder.createBitcast(vtablePtr, cgm.uInt8PtrTy);
   CharUnits vbaseOffsetOffset =
       cgm.getItaniumVTableContext().getVirtualBaseOffsetOffset(classDecl,
                                                                baseClassDecl);
-  mlir::Value offsetVal =
-      builder.getSInt64(vbaseOffsetOffset.getQuantity(), loc);
-  auto vbaseOffsetPtr = cir::PtrStrideOp::create(builder, loc, cgm.uInt8PtrTy,
-                                                 vtableBytePtr, offsetVal);
-
-  mlir::Value vbaseOffset;
-  if (cgm.getLangOpts().RelativeCXXABIVTables) {
-    assert(!cir::MissingFeatures::vtableRelativeLayout());
-    cgm.errorNYI(loc, "getVirtualBaseClassOffset: relative layout");
-  } else {
-    mlir::Value offsetPtr = builder.createBitcast(
-        vbaseOffsetPtr, builder.getPointerTo(cgm.ptrDiffTy));
-    vbaseOffset = builder.createLoad(
-        loc, Address(offsetPtr, cgm.ptrDiffTy, cgf.getPointerAlign()));
-  }
-  return vbaseOffset;
+  return cir::VTableGetOffsetOp::create(
+      builder, loc, cgm.ptrDiffTy, vtablePtr,
+      getVTableEntryIndex(cgm, vbaseOffsetOffset.getQuantity()));
 }
 
 static cir::FuncOp getBadCastFn(CIRGenFunction &cgf) {
@@ -1850,9 +1843,7 @@ static cir::FuncOp getItaniumDynamicCastFn(CIRGenFunction &cgf) {
 
 static Address emitDynamicCastToVoid(CIRGenFunction &cgf, mlir::Location loc,
                                      QualType srcRecordTy, Address src) {
-  bool vtableUsesRelativeLayout = cgf.cgm.getLangOpts().RelativeCXXABIVTables;
-  mlir::Value ptr = cgf.getBuilder().createDynCastToVoid(
-      loc, src.getPointer(), vtableUsesRelativeLayout);
+  mlir::Value ptr = cgf.getBuilder().createDynCastToVoid(loc, src.getPointer());
   return Address{ptr, src.getAlignment()};
 }
 
@@ -2288,7 +2279,6 @@ static mlir::Value performTypeAdjustment(CIRGenFunction &cgf,
 
   CIRGenBuilderTy &builder = cgf.getBuilder();
   mlir::Location loc = builder.getUnknownLoc();
-  cir::PointerType ui8DefaultASPtrTy = builder.getUInt8PtrTy();
   mlir::Value v =
       builder.createPtrBitcast(initialPtr.getPointer(), builder.getUInt8Ty());
   auto i8PtrTy = mlir::cast<cir::PointerType>(v.getType());
@@ -2304,19 +2294,10 @@ static mlir::Value performTypeAdjustment(CIRGenFunction &cgf,
   if (virtualAdjustment) {
     mlir::Value vtablePtr = cgf.getVTablePtr(
         loc, Address(v, clang::CharUnits::One()), unadjustedClass);
-    vtablePtr = builder.createBitcast(vtablePtr, ui8DefaultASPtrTy);
 
-    mlir::Value offset;
-    mlir::Value offsetPtr =
-        cir::PtrStrideOp::create(builder, loc, ui8DefaultASPtrTy, vtablePtr,
-                                 builder.getSInt64(virtualAdjustment, loc));
-    if (cgf.cgm.getLangOpts().RelativeCXXABIVTables) {
-      assert(!cir::MissingFeatures::vtableRelativeLayout());
-      cgf.cgm.errorNYI("virtual adjustment for relative layout vtables");
-    } else {
-      offset = builder.createAlignedLoad(loc, cgf.ptrDiffTy, offsetPtr,
-                                         cgf.getPointerAlign());
-    }
+    mlir::Value offset = cir::VTableGetOffsetOp::create(
+        builder, loc, cgf.ptrDiffTy, vtablePtr,
+        getVTableEntryIndex(cgf.cgm, virtualAdjustment));
 
     resultPtr = cir::PtrStrideOp::create(builder, loc, i8PtrTy, v, offset);
   } else {
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 83d1adb9605a6..78e3d687f44dc 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -563,6 +563,21 @@ void cir::AllocaOp::build(mlir::OpBuilder &odsBuilder,
   odsState.addTypes(addr);
 }
 
+bool cir::usesRelativeVTables(mlir::ModuleOp mod) {
+  auto loweringOpts = mod->getAttrOfType<cir::LoweringOptionsAttr>(
+      cir::CIRDialect::getLoweringOptionsAttrName());
+  return loweringOpts && loweringOpts.getRelativeCxxAbiVtables();
+}
+
+uint64_t cir::getVTableEntrySize(mlir::ModuleOp mod,
+                                 const mlir::DataLayout &dataLayout) {
+  // Relative layout entries are 32-bit offsets.
+  if (usesRelativeVTables(mod))
+    return 4;
+  return dataLayout.getTypeSize(
+      cir::PointerType::get(cir::VoidType::get(mod.getContext())));
+}
+
 cir::AllocaOp cir::getUnderlyingAlloca(mlir::Value addr) {
   mlir::Value ptr = addr;
   while (cir::CastOp castOp = ptr.getDefiningOp<cir::CastOp>()) {
diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp
index 690018f9343a2..04b0feb7ab36d 100644
--- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerItaniumCXXABI.cpp
@@ -782,55 +782,28 @@ static mlir::Value buildDynamicCastAfterNullCheck(cir::DynamicCastOp op,
 static mlir::Value buildDynamicCastToVoidAfterNullCheck(
     cir::DynamicCastOp op, cir::LowerModule &lm, mlir::OpBuilder &builder) {
   mlir::Location loc = op.getLoc();
-  bool vtableUsesRelativeLayout = op.getRelativeLayout();
 
   // TODO(cir): consider address space of the vtable pointer.
   assert(!cir::MissingFeatures::addressSpace());
 
-  mlir::Type vtableElemTy;
-  uint64_t vtableElemAlign;
-  if (vtableUsesRelativeLayout) {
-    vtableElemTy =
-        cir::IntType::get(builder.getContext(), 32, /*isSigned=*/true);
-    vtableElemAlign = 4;
-  } else {
-    vtableElemTy = getPtrDiffCIRTy(lm);
-    vtableElemAlign = llvm::divideCeil(
-        lm.getTarget().getPointerAlign(clang::LangAS::Default), 8);
-  }
-
-  mlir::Type vtableElemPtrTy = cir::PointerType::get(vtableElemTy);
-  mlir::Type i64Ty = cir::IntType::get(builder.getContext(), /*width=*/64,
-                                       /*isSigned=*/true);
-
   // Access vtable to get the offset from the given object to its containing
   // complete object.
-  // TODO: Add a specialized operation to get the object offset?
+  uint64_t vptrAlign = llvm::divideCeil(
+      lm.getTarget().getPointerAlign(clang::LangAS::Default), 8);
   auto vptrPtr = cir::VTableGetVPtrOp::create(builder, loc, op.getSrc());
-  mlir::Value vptr = cir::LoadOp::create(
-      builder, loc, vptrPtr,
-      /*isDeref=*/false,
-      /*is_volatile=*/false,
-      /*isNontemporal=*/false,
-      /*alignment=*/builder.getI64IntegerAttr(vtableElemAlign),
-      /*sync_scope=*/cir::SyncScopeKindAttr(),
-      /*mem_order=*/cir::MemOrderAttr(),
-      /*invariant=*/false);
-  mlir::Value elementPtr = cir::CastOp::create(builder, loc, vtableElemPtrTy,
-                                               cir::CastKind::bitcast, vptr);
-  mlir::Value minusTwo =
-      cir::ConstantOp::create(builder, loc, cir::IntAttr::get(i64Ty, -2));
-  mlir::Value offsetToTopSlotPtr = cir::PtrStrideOp::create(
-      builder, loc, vtableElemPtrTy, elementPtr, minusTwo);
-  mlir::Value offsetToTop = cir::LoadOp::create(
-      builder, loc, offsetToTopSlotPtr,
-      /*isDeref=*/false,
-      /*is_volatile=*/false,
-      /*isNontemporal=*/false,
-      /*alignment=*/builder.getI64IntegerAttr(vtableElemAlign),
-      /*sync_scope=*/cir::SyncScopeKindAttr(),
-      /*mem_order=*/cir::MemOrderAttr(),
-      /*invariant=*/false);
+  mlir::Value vptr =
+      cir::LoadOp::create(builder, loc, vptrPtr,
+                          /*isDeref=*/false,
+                          /*is_volatile=*/false,
+                          /*isNontemporal=*/false,
+                          /*alignment=*/builder.getI64IntegerAttr(vptrAlign),
+                          /*sync_scope=*/cir::SyncScopeKindAttr(),
+                          /*mem_order=*/cir::MemOrderAttr(),
+                          /*invariant=*/false);
+  // The Itanium ABI always places an offset to the complete object at entry -2
+  // in the vtable.
+  mlir::Value offsetToTop = cir::VTableGetOffsetOp::create(
+      builder, loc, getPtrDiffCIRTy(lm), vptr, /*index=*/-2);
 
   // Add the offset to the given pointer to get the cast result.
   // Cast the input pointer to a uint8_t* to allow pointer arithmetic.
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index feeffb56a5d31..7b8dec3aea94d 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -1088,13 +1088,6 @@ mlir::Value CIRAttrToValue::visitCirAttr(cir::ConstVectorAttr attr) {
                                    mlirValues));
 }
 
-/// Whether the C++ vtables of the module use the relative layout.
-static bool usesRelativeVTables(mlir::ModuleOp module) {
-  auto loweringOpts = module->getAttrOfType<cir::LoweringOptionsAttr>(
-      cir::CIRDialect::getLoweringOptionsAttrName());
-  return loweringOpts && loweringOpts.getRelativeCxxAbiVtables();
-}
-
 // GlobalViewAttr / GlobalOffsetAttr visitors.
 CIRAttrToValue::LoweredGlobalSymbol
 CIRAttrToValue::getAddressOfSymbol(mlir::FlatSymbolRefAttr symbol) {
@@ -5623,6 +5616,34 @@ mlir::LogicalResult CIRToLLVMVTableGetVirtualFnOpLowering::matchAndRewrite(
   return mlir::success();
 }
 
+mlir::LogicalResult CIRToLLVMVTableGetOffsetOpLowering::matchAndRewrite(
+    cir::VTableGetOffsetOp op, OpAdaptor adaptor,
+    mlir::ConversionPatternRewriter &rewriter) const {
+  assert(!cir::MissingFeatures::vtableRelativeLayout());
+  if (usesRelativeVTables(op->getParentOfType<mlir::ModuleOp>()))
+    return op.emitError("NYI: offset lookup in a relative layout vtable");
+
+  mlir::Type resultType = getTypeConverter()->convertType(op.getType());
+  if (!resultType)
+    return emitTypeConversionError(op, op.getType());
+
+  // The entry is the offset itself: load it from its slot, addressed by its
+  // byte offset.
+  auto ptrTy = mlir::LLVM::LLVMPointerType::get(rewriter.getContext());
+  int64_t byteOffset =
+      op.getIndex() * cir::getVTableEntrySize(
+                          op->getParentOfType<mlir::ModuleOp>(), dataLayout);
+  mlir::Value byteOffsetVal = mlir::LLVM::ConstantOp::create(
+      rewriter, op.getLoc(), rewriter.getI64Type(),
+      rewriter.getI64IntegerAttr(byteOffset));
+  mlir::Value slot = mlir::LLVM::GEPOp::create(
+      rewriter, op.getLoc(), ptrTy, rewriter.getI8Type(), adaptor.getVptr(),
+      llvm::ArrayRef<mlir::LLVM::GEPArg>{byteOffsetVal});
+  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/CodeGen/delegating-ctor.cpp b/clang/test/CIR/CodeGen/delegating-ctor.cpp
index 96ecb5815a0f6..4bf5fe95d258e 100644
--- a/clang/test/CIR/CodeGen/delegating-ctor.cpp
+++ b/clang/test/CIR/CodeGen/delegating-ctor.cpp
@@ -150,11 +150,7 @@ Derived::Derived(const void *inVoid) { squawk(); }
 // CIR:        %[[VPTR_BASE:.*]] = cir.load{{.*}} %[[VPTR_BASE_PTR]] : !cir.ptr<!cir.vptr>, !cir.vptr
 // CIR:        %[[VPTR_DERIVED_ADDR:.*]] = cir.vtable.get_vptr %[[THIS]] : !cir.ptr<!rec_Derived> -> !cir.ptr<!cir.vptr>
 // CIR:        %[[VPTR_DERIVED:.*]] = cir.load{{.*}} %[[VPTR_DERIVED_ADDR]] : !cir.ptr<!cir.vptr>, !cir.vptr
-// CIR:        %[[VPTR_DERIVED_AS_I8PTR:.*]] = cir.cast bitcast %[[VPTR_DERIVED]] : !cir.vptr -> !cir.ptr<!u8i>
-// CIR:        %[[BASE_LOC_OFFSET:.*]] = cir.const #cir.int<-32> : !s64i
-// CIR:        %[[BASE_OFFSET_PTR:.*]] = cir.ptr_stride %[[VPTR_DERIVED_AS_I8PTR]], %[[BASE_LOC_OFFSET]] : (!cir.ptr<!u8i>, !s64i) -> !cir.ptr<!u8i>
-// CIR:        %[[BASE_OFFSET_I64PTR:.*]] = cir.cast bitcast %[[BASE_OFFSET_PTR]] : !cir.ptr<!u8i> -> !cir.ptr<!s64i>
-// CIR:        %[[BASE_OFFSET:.*]] = cir.load{{.*}} %[[BASE_OFFSET_I64PTR]] : !cir.ptr<!s64i>, !s64i
+// CIR:        %[[BASE_OFFSET:.*]] = cir.vtable.get_offset %[[VPTR_DERIVED]][-4] : !cir.vptr -> !s64i
 // CIR:        %[[THIS_AS_I8PTR:.*]] = cir.cast bitcast %[[THIS]] : !cir.ptr<!rec_Derived> -> !cir.ptr<!u8i>
 // CIR:        %[[BASE_PTR:.*]] = cir.ptr_stride %[[THIS_AS_I8PTR]], %[[BASE_OFFSET]] : (!cir.ptr<!u8i>, !s64i) -> !cir.ptr<!u8i>
 // CIR:        %[[BASE_AS_I8PTR:.*]] = cir.cast bitcast %[[BASE_PTR]] : !cir.ptr<!u8i> -> !cir.ptr<!rec_Derived>
diff --git a/clang/test/CIR/CodeGen/dynamic-cast.cpp b/clang/test/CIR/CodeGen/dynamic-cast.cpp
index 5d8d61a80d513..baae72a1f6c73 100644
--- a/clang/test/CIR/CodeGen/dynamic-cast.cpp
+++ b/clang/test/CIR/CodeGen/dynamic-cast.cpp
@@ -116,10 +116,7 @@ void *ptr_cast_to_complete(Base *ptr) {
 // CIR-AFTER-NEXT:   %{{.+}} = cir.ternary(%[[SRC_IS_NOT_NULL]], true {
 // CIR-AFTER-NEXT:     %[[VPTR_PTR:.*]] = cir.vtable.get_vptr %[[SRC]] : !cir.ptr<!rec_Base> -> !cir.ptr<!cir.vptr>
 // CIR-AFTER-NEXT:     %[[VPTR:.*]] = cir.load {{.*}} %[[VPTR_PTR]] : !cir.ptr<!cir.vptr>, !cir.vptr
-// CIR-AFTER-NEXT:     %[[ELEM_PTR:.*]] = cir.cast bitcast %[[VPTR]] : !cir.vptr -> !cir.ptr<!s64i>
-// CIR-AFTER-NEXT:     %[[MINUS_TWO:.*]] = cir.const #cir.int<-2> : !s64i
-// CIR-AFTER-NEXT:     %[[BASE_OFFSET_PTR:.*]] = cir.ptr_stride %[[ELEM_PTR]], %[[MINUS_TWO]] : (!cir.ptr<!s64i>, !s64i) -> !cir.ptr<!s64i>
-// CIR-AFTER-NEXT:     %[[BASE_OFFSET:.*]] = cir.load{{.*}} %[[BASE_OFFSET_PTR]] : !cir.ptr<!s64i>, !s64i
+// CIR-AFTER-NEXT:     %[[BASE_OFFSET:.*]] = cir.vtable.get_offset %[[VPTR]][-2] : !cir.vptr -> !s64i
 // CIR-AFTER-NEXT:     %[[SRC_BYT...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/242032


More information about the cfe-commits mailing list