Repository: pytorch
Issue: Torchscript optimizer incorrectly applies constant propagation to convert prim::ListConstruct() into prim::Constant
Labels: oncall: jit

### 🐛 Describe the bug

The following code fails when run from C++:

```python
@torch.jit.script
class Container:
    def __init__(self) -> None:
        self.values: typing.List[int] = []
        assert len(self.values) == 0  # fails for the second or third instance Container

    def append(self, value: int):
        self.values.append(value)

class TorchscriptExample(torch.nn.Module):
    @torch.jit.export
    def create_container_object(self) -> Container:
        return Container()

torch.jit.script(TorchscriptExample()).save("example.pt")
```

I also observed that this fails with optimization enabled (as by default).

Setting `torch::jit::setGraphExecutorOptimize(false);` passes.

```c++
    auto mod = torch::jit::script::Module(torch::jit::load("example.pt"));

    auto c1 = mod.create_class("__torch__.Container", {});
    auto c2 = mod.create_class("__torch__.Container", {});

    if (c1.toObjectRef().getAttr("values").is(c2.toObjectRef().getAttr("values"))) {
        throw std::runtime_error("expect different lists"); // fails here
    }
```

By running with `PYTORCH_JIT_LOG_LEVEL='>>profiling_graph_executor_impl'` I saw:
```
[DEBUG profiling_graph_executor_impl.cpp:558] After DecomposeOps, before ConstantPropagation
[DEBUG profiling_graph_executor_impl.cpp:558] graph(%self.1 : __torch__.Container):
[DEBUG profiling_graph_executor_impl.cpp:558]   %1 : NoneType = prim::Constant()
[DEBUG profiling_graph_executor_impl.cpp:558]   %2 : str = prim::Constant[value="AssertionError: "]() # :0:0
[DEBUG profiling_graph_executor_impl.cpp:558]   %3 : int = prim::Constant[value=0]() 
[DEBUG profiling_graph_executor_impl.cpp:558]   %4 : __torch__.Container[] = prim::ListConstruct()
[DEBUG profiling_graph_executor_impl.cpp:558]    = prim::SetAttr[name="values"](%self.1, %4)
[DEBUG profiling_graph_executor_impl.cpp:558]   %values.1 : __torch__.Container[] = prim::GetAttr[name="values"](%self.1)
[DEBUG profiling_graph_executor_impl.cpp:558]   %6 : int = aten::len(%values.1)
[DEBUG profiling_graph_executor_impl.cpp:558]   %7 : bool = aten::eq(%6, %3) 
[DEBUG profiling_graph_executor_impl.cpp:558]    = prim::If(%7) 
[DEBUG profiling_graph_executor_impl.cpp:558]     block0():
[DEBUG profiling_graph_executor_impl.cpp:558]       -> ()
[DEBUG profiling_graph_executor_impl.cpp:558]     block1():
[DEBUG profiling_graph_executor_impl.cpp:558]        = prim::RaiseException(%2, %1) 
[DEBUG profiling_graph_executor_impl.cpp:558]       -> ()
[DEBUG profiling_graph_executor_impl.cpp:558]   return (%1)
[DEBUG profiling_graph_executor_impl.cpp:560] After ConstantPropagation, before EliminateDeadCode
[DEBUG profiling_graph_executor_impl.cpp:560] graph(%self.1 : __torch__.Container):
[DEBUG profiling_graph_executor_impl.cpp:560]   %1 : NoneType = prim::Constant()
[DEBUG profiling_graph_executor_impl.cpp:560]   %2 : str = prim::Constant[value="AssertionError: "]() # :0:0
[DEBUG profiling_graph_executor_impl.cpp:560]   %3 : int = prim::Constant[value=0]() 
[DEBUG profiling_graph_executor_impl.cpp:560]   %8 : __torch__.Container[] = prim::Constant[value=annotate(List[__torch__.Container], [])]()
[DEBUG profiling_graph_executor_impl.cpp:560]    = prim::SetAttr[name="values"](%self.1, %8)
[DEBUG profiling_graph_executor_impl.cpp:560]   %values.1 : __torch__.Container[] = prim::GetAttr[name="values"](%self.1)
[DEBUG profiling_graph_executor_impl.cpp:560]   %6 : int = aten::len(%values.1)
[DEBUG profiling_graph_executor_impl.cpp:560]   %7 : bool = aten::eq(%6, %3) 
[DEBUG profiling_graph_executor_impl.cpp:560]    = prim::If(%7) 
[DEBUG profiling_graph_executor_impl.cpp:560]     block0():
[DEBUG profiling_graph_executor_impl.cpp:560]       -> ()
[DEBUG profiling_graph_executor_impl.cpp:560]     block1():
[DEBUG profiling_graph_executor_impl.cpp:560]        = prim::RaiseException(%2, %1) 
[DEBUG profiling_graph_executor_impl.cpp:560]       -> ()
[DEBUG profiling_graph_executor_impl.cpp:560]   return (%1)
```

### Versions

PyTorch version: 2.0.1+cu118.post4
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.2 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: Could not collect
CMake version: version 3.16.3
Libc version: glibc-2.31

Python version: 3.10.7 (main, Sep  9 2022, 04:02:34) [GCC 9.4.0] (64-bit runtime)
Python platform: Linux-5.15.0-53-generic-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA GeForce GTX 1080
GPU 1: Quadro P2000

Nvidia driver version: 470.82.01
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
Byte Order:                      Little Endian
Address sizes:                   46 bits physical, 48 bits virtual
CPU(s):                          24
On-line CPU(s) list:             0-23
Thread(s) per core:              2
Core(s) per socket:              12
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           79
Model name:                      Intel(R) Xeon(R) CPU E5-2687W v4 @ 3.00GHz
Stepping:                        1
CPU MHz:                         1200.000
CPU max MHz:                     3500.0000
CPU min MHz:                     1200.0000
BogoMIPS:                        5986.61
Virtualization:                  VT-x
L1d cache:                       384 KiB
L1i cache:                       384 KiB
L2 cache:                        3 MiB
L3 cache:                        30 MiB
NUMA node0 CPU(s):               0-23
Vulnerability Itlb multihit:     KVM: Mitigation: VMX disabled
Vulnerability L1tf:              Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds:               Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown:          Mitigation; PTI
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Retpolines, IBPB conditional, IBRS_FW, STIBP conditional, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Mitigation; Clear CPU buffers; SMT vulnerable
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 vmx 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 invpcid_single pti intel_ppin ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad 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

Versions of relevant libraries:
[pip3] fft-conv-pytorch==1.1.3
[pip3] gpytorch==1.9.1
[pip3] mypy==0.960
[pip3] mypy-extensions==1.0.0
[pip3] mypy-protobuf==3.3.0
[pip3] numpy==1.22.4
[pip3] pytorch-lightning==1.7.3
[pip3] pytorch3d==0.7.4
[pip3] torch==2.0.1+cu118.post4
[pip3] torch-geometric==2.3.0
[pip3] torch-tb-profiler==0.4.1
[pip3] torch-tensorrt==1.4.0
[pip3] torchmetrics==0.11.0
[pip3] torchvision==0.15.2+cu118
[pip3] triton==2.0.0
[conda] Could not collect

cc @EikanWang @jgong5 @wenzhe-nrv @sanchitintel