Avoid UB when indexing into size-0 tensors (#65878)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/65878
If we attempt to compute an offset into an empty tensor we trigger UB, since
we'd be adding an offset to a nullptr, which is UB
(https://reviews.llvm.org/D67122) even if we never use the pointer.
Since indexing into an empty tensor yields an empty tensor anyways, let's just
return the underlying (null) data ptr in this case.
ghstack-source-id: 139448496
Test Plan:
r-barnes originally pointed this out to me in a failing TE fuser test:
https://www.internalfb.com/intern/testinfra/diagnostics/5910974579561425.281475022329152.1632898053/
```
buck test mode/dev //caffe2/test:jit -- --exact 'caffe2/test:jit - test_unsupported_nn_functional_pad_circular_cpu_float32 (test_jit_fuser_te.TestNNCOpInfoCPU)'
```
But it turns out it's easily triggered by anything that tries to operate on a
slice of a size-0 tensor:
```
def test_pad(self):
F.pad(torch.ones(0, 3, 3), (1, 2), 'circular')
def test_index(self):
input = torch.zeros(0, 3, 3)
out = torch.zeros(0, 3, 6)
out[..., 1:4] = input[..., 0:3]
def test_add(self):
torch.ones(0, 2)[:, 1] + torch.ones(0, 1)
```
What's the right place for these sort of operator corner-case tests? Should
they be/are they part of OpInfo?
Reviewed By: jamesr66a
Differential Revision: D31296914
fbshipit-source-id: 0ef52ad311dceeed985498f8d9390bc6fbaefbfc