[clang] [clang] Skip auto-init on scalar vars that have a non-constant Init and no self-ref (PR #94642)

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 19 08:49:09 PDT 2024


================
@@ -1972,7 +1972,20 @@ void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) {
   }
 
   if (!constant) {
-    initializeWhatIsTechnicallyUninitialized(Loc);
+    if (trivialAutoVarInit !=
+        LangOptions::TrivialAutoVarInitKind::Uninitialized) {
+      // At this point, we know D has an Init expression, but isn't a constant.
+      // - If D is not a scalar, auto-var-init conservatively (members may be
+      // left uninitialized by constructor Init expressions for example).
+      // - If D is a scalar, we only need to auto-var-init if there is a
+      // self-reference. Otherwise, the Init expression should be sufficient.
+      // It may be that the Init expression uses other uninitialized memory,
+      // but auto-var-init here would not help, as auto-init would get
+      // overwritten by Init.
+      if (!D.getType()->isScalarType() || isAccessedBy(D, Init)) {
----------------
ilya-biryukov wrote:

Ah, so `isAccessedBy` also detects all captured block variables. I have missed that when skimming through the code for the first time. Thanks for pointing that out.

If I'm not mistaken, `capturedByInit || ` does not actually change the semantics of the code, so not sure it adds any safety. But it does make sense as an optimization and that's also present in the other code for `isAccessedBy`, so including it seems like an overall win (performance, consistency).

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


More information about the cfe-commits mailing list