[clang] Reapply "[clang][CodeGen] Zero init unspecified fields in initializers in C" (#109898) (PR #110051)

via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 4 15:27:50 PDT 2024


================
@@ -1774,6 +1805,50 @@ void AggExprEmitter::VisitCXXParenListOrInitListExpr(
       }
     }
   }
+  if (ZeroInitPadding) {
+    uint64_t TotalSize = CGF.getContext().toBits(
+        Dest.getPreferredSize(CGF.getContext(), DestLV.getType()));
+    DoZeroInitPadding(BaseAddr, LastFieldBitOffset, TotalSize);
+  }
+}
+
+void AggExprEmitter::DoZeroInitPadding(const Address BaseAddr,
+                                       uint64_t StartBitOffset,
+                                       uint64_t EndBitOffset) {
+  if (StartBitOffset >= EndBitOffset)
+    return;
+
+  auto InitBytes = [&](uint64_t Start, uint64_t End) {
+    Address Addr = CGF.Builder.CreateConstGEP(BaseAddr, Start);
+    llvm::Constant *SizeVal = CGF.Builder.getInt64(End - Start);
+    CGF.Builder.CreateMemSet(Addr, CGF.Builder.getInt8(0), SizeVal, false);
+  };
+  auto InitBits = [&](uint64_t Byte, uint64_t Start, uint64_t End) {
+    Address Addr = CGF.Builder.CreateConstGEP(BaseAddr, Byte);
+    llvm::Value *Val = Builder.CreateLoad(Addr);
----------------
yabinc wrote:

Originally it was Int8Ty. Now changed to store 0 to Info.StorageSize before initializing bitfields. I tested in -O2 that the store was always merged with initialization for bitfields.
Not sure if I need to consider Info.VolatileStorageSize, but I ran check-clang and test-suite successfully.

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


More information about the cfe-commits mailing list