Repository: pytorch
Issue: c10::SmallVector unusable with gcc/g++ 12 and 13 with `-O3`
Labels: needs reproduction,module: build,module: cpp,triaged

### 🐛 Describe the bug

Recently, github hosted runners for `ubuntu-latest` is migrating from ubuntu 22.04 to ubuntu 24.04. As part of that migration, the default compiler has changed from gcc 11.4 to gcc 13.2. A lot of my CI workflows using libtorch are failing during this migration.

All errors look like the following
```
[build] /home/thu/micromamba/envs/neml2/lib/python3.12/site-packages/torch/include/c10/util/SmallVector.h:139:19: error: ‘net’ may be used uninitialized [-Werror=maybe-uninitialized]
[build]   139 |     Base::grow_pod(getFirstEl(), MinSize, TSize);
[build]       |     ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[build] /home/thu/micromamba/envs/neml2/lib/python3.12/site-packages/torch/include/c10/util/SmallVector.h: In function ‘neml2::TensorShape neml2::utils::add_shapes(S&& ...) [with S = {c10::SmallVector<long int, 6>, c10::ArrayRef<long int>&}]’:
[build] /home/thu/micromamba/envs/neml2/lib/python3.12/site-packages/torch/include/c10/util/SmallVector.h:73:8: note: by argument 2 of type ‘const void*’ to ‘void c10::SmallVectorBase<Size_T>::grow_pod(const void*, size_t, size_t) [with Size_T = unsigned int]’ declared here
[build]    73 |   void grow_pod(const void* FirstEl, size_t MinSize, size_t TSize);
[build]       |        ^~~~~~~~
[build] /home/thu/projects/neml2/include/neml2/misc/utils.h:303:15: note: ‘net’ declared here
[build]   303 |   TensorShape net;
[build]       |               ^~~
```

Not all builds fail, and after some digging, I can confirm that the following conditions are needed to reproduce this error
1. libtorch that comes with pytorch==2.5.1
1. gcc/g++ 12.4/13.2/13.3
2. -O3

I have not checked newer versions of gcc/g++, and I have not checked previous versions of pytorch
However, I can confirm that with `-O2` and `-O0` the code compiles. Also, with gcc/g++ 11.4, the code compiles with all optimization levels. This led me to believe that this is a compiler bug, not a fault on the pytorch side.

### Versions

<details>

<summary>output</summary>

```
PyTorch version: 2.5.1+cu124
Is debug build: False
CUDA used to build PyTorch: 12.4
ROCM used to build PyTorch: N/A

OS: Ubuntu 24.04.1 LTS (x86_64)
GCC version: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
Clang version: 18.1.8 (++20240731025011+3b5b5c1ec4a3-1~exp1~20240731145104.143)
CMake version: version 3.30.5
Libc version: glibc-2.39

Python version: 3.12.7 | packaged by conda-forge | (main, Oct  4 2024, 16:05:46) [GCC 13.3.0] (64-bit runtime)
Python platform: Linux-6.8.0-47-generic-x86_64-with-glibc2.39
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: Quadro M6000 24GB
Nvidia driver version: 550.120
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                         x86_64
CPU op-mode(s):                       32-bit, 64-bit
Address sizes:                        46 bits physical, 48 bits virtual
Byte Order:                           Little Endian
CPU(s):                               40
On-line CPU(s) list:                  0-39
Vendor ID:                            GenuineIntel
Model name:                           Intel(R) Xeon(R) CPU E5-2698 v4 @ 2.20GHz
CPU family:                           6
Model:                                79
Thread(s) per core:                   1
Core(s) per socket:                   20
Socket(s):                            2
Stepping:                             1
CPU(s) scaling MHz:                   44%
CPU max MHz:                          3600.0000
CPU min MHz:                          1200.0000
BogoMIPS:                             4389.81
Flags:                                fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 pti intel_ppin ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap intel_pt xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts md_clear flush_l1d
L1d cache:                            1.3 MiB (40 instances)
L1i cache:                            1.3 MiB (40 instances)
L2 cache:                             10 MiB (40 instances)
L3 cache:                             100 MiB (2 instances)
NUMA node(s):                         2
NUMA node0 CPU(s):                    0-19
NUMA node1 CPU(s):                    20-39
Vulnerability Gather data sampling:   Not affected
Vulnerability Itlb multihit:          KVM: Mitigation: VMX unsupported
Vulnerability L1tf:                   Mitigation; PTE Inversion
Vulnerability Mds:                    Mitigation; Clear CPU buffers; SMT disabled
Vulnerability Meltdown:               Mitigation; PTI
Vulnerability Mmio stale data:        Mitigation; Clear CPU buffers; SMT disabled
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed:               Not affected
Vulnerability Spec rstack overflow:   Not affected
Vulnerability Spec store bypass:      Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:             Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:             Mitigation; Retpolines; IBPB conditional; IBRS_FW; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                  Not affected
Vulnerability Tsx async abort:        Mitigation; Clear CPU buffers; SMT disabled

Versions of relevant libraries:
[pip3] numpy==1.26.4
[pip3] nvidia-cublas-cu12==12.4.5.8
[pip3] nvidia-cuda-cupti-cu12==12.4.127
[pip3] nvidia-cuda-nvrtc-cu12==12.4.127
[pip3] nvidia-cuda-runtime-cu12==12.4.127
[pip3] nvidia-cudnn-cu12==9.1.0.70
[pip3] nvidia-cufft-cu12==11.2.1.3
[pip3] nvidia-curand-cu12==10.3.5.147
[pip3] nvidia-cusolver-cu12==11.6.1.9
[pip3] nvidia-cusparse-cu12==12.3.1.170
[pip3] nvidia-nccl-cu12==2.21.5
[pip3] nvidia-nvjitlink-cu12==12.4.127
[pip3] nvidia-nvtx-cu12==12.4.127
[pip3] torch==2.5.1
[pip3] torch-tb-profiler==0.4.3
[pip3] triton==3.1.0
[conda] No relevant packages
```

</details>

cc @malfet @seemethere @jbschlosser