[clang] [CIR] Handle lowering for most of Relative-Vtables (PR #247288)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 30 15:49:43 PST 2026


================
@@ -3491,6 +3576,147 @@ CIRToLLVMGlobalOpLowering::setupRegionInitializedLLVMGlobalOp(
   return newGlobalOp;
 }
 
+static mlir::SymbolRefAttr
+getComdatAttrHelper(mlir::ModuleOp modOp, mlir::OpBuilder &builder,
+                    StringRef symName, mlir::LLVM::ComdatOp &comdatOp,
+                    mlir::SymbolTableCollection &symbolTables);
+
+/// Build the initializer of the relative layout vtable that is the LLVM global
+/// \p vtableSym.
+///
+/// Every entry is 32 bits wide. An offset is stored as is. A function or RTTI
+/// entry is stored as the distance from the address point of the vtable it is
+/// in to the entry's target, which is the function itself, or the proxy of the
+/// RTTI. The RTTI symbols whose proxy is needed are appended to \p rttiRefs.
+static mlir::Value lowerRelativeVTableInit(
+    mlir::ConversionPatternRewriter &rewriter,
+    mlir::SymbolTableCollection &symbolTables, mlir::ModuleOp mod,
+    mlir::Location loc, llvm::StringRef vtableSym, cir::VTableAttr vtable,
+    mlir::Type llvmTy, llvm::SmallVectorImpl<llvm::StringRef> &rttiRefs) {
+  mlir::MLIRContext *ctx = rewriter.getContext();
+  auto ptrTy = mlir::LLVM::LLVMPointerType::get(ctx);
+  mlir::Type i32Ty = rewriter.getI32Type();
+  mlir::Type intPtrTy =
+      rewriter.getIntegerType(mlir::DataLayout(mod).getTypeSizeInBits(ptrTy));
+  auto vtablesTy = mlir::cast<mlir::LLVM::LLVMStructType>(llvmTy);
+
+  mlir::Value vtableAddr =
+      mlir::LLVM::AddressOfOp::create(rewriter, loc, ptrTy, vtableSym);
+  mlir::Value result = mlir::LLVM::UndefOp::create(rewriter, loc, llvmTy);
+
+  for (auto [vtableIdx, vtableAttr] : llvm::enumerate(vtable.getData())) {
+    auto entries = mlir::cast<cir::ConstArrayAttr>(vtableAttr);
+    auto addrPoint =
+        mlir::cast<cir::AddressPointAttr>(vtable.getAddressPoints()[vtableIdx]);
+    assert(addrPoint.getIndex() == static_cast<int32_t>(vtableIdx) &&
+           "unordered address points");
+
+    mlir::Value addrPointInt = mlir::LLVM::PtrToIntOp::create(
+        rewriter, loc, intPtrTy,
+        mlir::LLVM::GEPOp::create(
+            rewriter, loc, ptrTy, llvmTy, vtableAddr,
+            llvm::ArrayRef<mlir::LLVM::GEPArg>{
+                0, static_cast<int32_t>(vtableIdx),
+                static_cast<int32_t>(addrPoint.getOffset())},
+            mlir::LLVM::GEPNoWrapFlags::inbounds));
+
+    mlir::Type vtableTy = vtablesTy.getBody()[vtableIdx];
+    mlir::Value vtableVal =
+        entries.hasTrailingZeros()
+            ? mlir::LLVM::ZeroOp::create(rewriter, loc, vtableTy).getResult()
+            : mlir::LLVM::UndefOp::create(rewriter, loc, vtableTy).getResult();
+
+    for (auto [idx, entry] :
+         llvm::enumerate(mlir::cast<mlir::ArrayAttr>(entries.getElts()))) {
+      mlir::Value entryVal;
+      if (auto ptr = mlir::dyn_cast<cir::ConstPtrAttr>(entry)) {
+        // An offset, or nothing at all.
+        entryVal = mlir::LLVM::ConstantOp::create(
+            rewriter, loc, i32Ty,
+            rewriter.getI32IntegerAttr(ptr.getValue().getInt()));
+      } else {
+        auto view = mlir::cast<cir::GlobalViewAttr>(entry);
+        assert(!view.getIndices() && "vtable entry refers into a symbol");
+        mlir::Operation *symbol =
+            symbolTables.lookupSymbolIn(mod, view.getSymbol());
+        assert(symbol && "vtable entry refers to a missing symbol");
+
+        mlir::Value target;
+        if (mlir::isa<cir::FuncOp, mlir::LLVM::LLVMFuncOp>(symbol)) {
----------------
erichkeane wrote:

Oooh, yep, good catch! 

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


More information about the cfe-commits mailing list