Repository: pytorch
Issue: torch.compile generates wrong code on CPU and compiled code replaces original function
Labels: triaged,oncall: pt2,oncall: cpu inductor

### 🐛 Describe the bug

```python
import torch
import torch._inductor.config

torch._inductor.config.force_disable_caches = True  # doesn't seem to help

print(torch.__version__)

def func(tensor: torch.Tensor) -> torch.Tensor:
    return tensor * 2.0 ** (-127 + 3)

x = torch.tensor(0.1875)
print("Eager (before):", func(x))
print("Compile:", torch.compile(func)(x))
print("Eager (after):", func(x))
```

Output

```
2.4.0.dev20240521
Eager (before): tensor(8.8162e-39)
Compile: tensor(0.)
Eager (after): tensor(0.)
```

There are 2 separate issues
1. Compiled version produces wrong results (compare `Eager (before)` and `Compile` lines)
    - I compared the generated code (using `TORCH_LOGS="output_code"`). The generated C++ kernel is identical, only the accompanied header file is different.
2. After running `torch.compile()`, the original eager function produces wrong results like the compiled version (compare `Eager (before)` and `Eager (after)`)
    - Seems like some caching issue that shouldn't happen?
 
I only observe this issue for nightly build. v2.3 is not affected.

This bug was discovered while working on https://github.com/pytorch/ao/pull/248

@msaroufim

### Versions

```
Collecting environment information...
PyTorch version: 2.4.0.dev20240521
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.6 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Clang version: Could not collect
CMake version: version 3.16.3
Libc version: glibc-2.31

Python version: 3.10.14 (main, May  6 2024, 19:42:50) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.0-1058-aws-x86_64-with-glibc2.31
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
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
Byte Order:                         Little Endian
Address sizes:                      46 bits physical, 48 bits virtual
CPU(s):                             4
On-line CPU(s) list:                0-3
Thread(s) per core:                 2
Core(s) per socket:                 2
Socket(s):                          1
NUMA node(s):                       1
Vendor ID:                          GenuineIntel
CPU family:                         6
Model:                              85
Model name:                         Intel(R) Xeon(R) Platinum 8175M CPU @ 2.50GHz
Stepping:                           4
CPU MHz:                            3103.782
BogoMIPS:                           4999.99
Hypervisor vendor:                  KVM
Virtualization type:                full
L1d cache:                          64 KiB
L1i cache:                          64 KiB
L2 cache:                           2 MiB
L3 cache:                           33 MiB
NUMA node0 CPU(s):                  0-3
Vulnerability Gather data sampling: Unknown: Dependent on hypervisor status
Vulnerability Itlb multihit:        KVM: Mitigation: VMX unsupported
Vulnerability L1tf:                 Mitigation; PTE Inversion
Vulnerability Mds:                  Vulnerable: Clear CPU buffers attempted, no microcode; SMT Host state unknown
Vulnerability Meltdown:             Mitigation; PTI
Vulnerability Mmio stale data:      Vulnerable: Clear CPU buffers attempted, no microcode; SMT Host state unknown
Vulnerability Retbleed:             Vulnerable
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass:    Vulnerable
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Retpolines, STIBP disabled, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single pti fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves ida arat pku ospke

Versions of relevant libraries:
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.2
[pip3] pytorch-lightning==2.2.3
[pip3] torch==2.2.1+cu121
[pip3] torch-xla==2.3.0
[pip3] torchao==0.2.0
[pip3] torchaudio==2.2.0.dev20240521
[pip3] torchmetrics==1.3.1
[pip3] torchvision==0.17.1+cu121
[pip3] triton==2.2.0
[conda] blas                      1.0                         mkl  
[conda] libjpeg-turbo             2.0.0                h9bf148f_0    pytorch-nightly
[conda] mkl                       2023.1.0         h213fc3f_46344  
[conda] mkl-service               2.4.0           py310h5eee18b_1  
[conda] mkl_fft                   1.3.8           py310h5eee18b_0  
[conda] mkl_random                1.2.4           py310hdb19cb5_0  
[conda] numpy                     1.26.2                   pypi_0    pypi
[conda] numpy-base                1.26.4          py310hb5e798b_0  
[conda] pytorch                   2.4.0.dev20240521 py3.10_cuda12.1_cudnn8.9.2_0    pytorch-nightly
[conda] pytorch-cuda              12.1                 ha16c6d3_6    pytorch-nightly
[conda] pytorch-lightning         2.2.3                    pypi_0    pypi
[conda] pytorch-mutex             1.0                        cuda    pytorch-nightly
[conda] torch                     2.2.1+cu121              pypi_0    pypi
[conda] torch-xla                 2.3.0                    pypi_0    pypi
[conda] torchao                   0.2.0                     dev_0    <develop>
[conda] torchaudio                2.2.0.dev20240521     py310_cu121    pytorch-nightly
[conda] torchmetrics              1.3.1                    pypi_0    pypi
[conda] torchtriton               3.0.0+45fff310c8           py310    pytorch-nightly
[conda] torchvision               0.17.1+cu121             pypi_0    pypi
[conda] triton                    2.2.0                    pypi_0    pypi
```

cc @ezyang @gchanan @zou3519 @kadeng @msaroufim @bdhirsh @anijain2305 @chauhang