[clang] [libcxx] [clang] Add builtin to clear padding bytes (prework for P0528R3) (PR #75371)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 14 12:06:32 PDT 2024


================
@@ -2538,6 +2539,205 @@ static RValue EmitHipStdParUnsupportedBuiltin(CodeGenFunction *CGF,
   return RValue::get(CGF->Builder.CreateCall(UBF, Args));
 }
 
+template <class T>
+void RecursivelyClearPaddingImpl(CodeGenFunction &CGF, Value *Ptr, QualType Ty,
+                                 size_t CurrentStartOffset,
+                                 size_t &RunningOffset, T &&WriteZeroAtOffset,
+                                 bool VisitVirtualBase);
+
+template <class T>
+void ClearPaddingStruct(CodeGenFunction &CGF, Value *Ptr, QualType Ty,
+                        StructType *ST, size_t CurrentStartOffset,
+                        size_t &RunningOffset, T &&WriteZeroAtOffset,
+                        bool VisitVirtualBase) {
+  llvm::dbgs() << "clear padding struct: " << ST->getName().data() << '\n';
+  const auto &DL = CGF.CGM.getModule().getDataLayout();
+  auto *SL = DL.getStructLayout(ST);
+  auto *R = dyn_cast<CXXRecordDecl>(Ty->getAsRecordDecl());
+  if (!R) {
+    llvm::dbgs() << "Not a CXXRecordDecl\n";
+    return;
+  }
+  const ASTRecordLayout &ASTLayout = CGF.getContext().getASTRecordLayout(R);
+  if (ASTLayout.hasOwnVFPtr()) {
+    llvm::dbgs() << "vtable ptr. Incrementing RunningOffset from "
+                 << RunningOffset << " to "
+                 << RunningOffset + DL.getPointerSizeInBits() / 8 << '\n';
+    RunningOffset += DL.getPointerSizeInBits() / 8;
+  }
+  std::vector<std::pair<size_t, CXXBaseSpecifier>> Bases;
+  Bases.reserve(R->getNumBases());
+  // todo get vbases
+  for (auto Base : R->bases()) {
+    auto *BaseRecord = cast<CXXRecordDecl>(Base.getType()->getAsRecordDecl());
+    if (!Base.isVirtual()) {
+      auto Offset = static_cast<size_t>(
----------------
efriedma-quic wrote:

Don't use size_t for target offsets.  Prefer CharUnits, or if you need a raw number for some reason, uint64_t.  (We want to make sure cross-compilation works correctly on 32-bit hosts.)

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


More information about the cfe-commits mailing list