[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
Tue Jun 18 10:26:29 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:

Could you check what happens with `-fblocks` that capture the variable if they're part of its initializer?
There is some special-case handling for this (when `capturedByInit == true`), I feel we might not be catching that case here.

See https://gcc.godbolt.org/z/cEexo5n77 for an example.
```cpp
void foo();
void test() {
 __block int calls = ^() {
    while (++calls < 100) {
        foo();
    }
    return calls;
 }();
}
 
```

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


More information about the cfe-commits mailing list