Repository: pytorch
Issue: [jit] WithInsertPoint can't get back to the prev_ node if the prev_ node has been destroyed
Labels: oncall: jit

### 🐛 Describe the bug

**The WithInsertPoint code is here:** https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/ir/ir.h#L1444

I create a insert point guard1 before node1 and destroy node1,  then I create another insert point guard2 before node2, the error will happen when the guard2 is destructed.  

**The code to reproduce is like this:**
``` c++
{
    WithInsertPoint guard1(node1);
    node1->destroy()
    WithInsertPoint guard2(node2)
}
```

**The output is like this:**
```c++
terminate called after throwing an instance of 'c10::Error'
  what():  n->owningGraph() == this && n->inBlockList() INTERNAL ASSERT FAILED at "/torch/src/pytorch/torch/csrc/jit/ir/ir.h":1396, please report a bug to PyTorch. 
Exception raised from setInsertPoint at /torch/src/pytorch/torch/csrc/jit/ir/ir.h:1213 (most recent call first):
frame #0: <unknown function> + 0xe14ad (0x7faf2773e4ad in /torch/src/pytorch/build/lib/libc10.so)
frame #1: std::function<std::string ()>::operator()() const + 0x3d (0x7faf2cbfc5fb in /torch/src/pytorch/build/lib/libtorch_cpu.so)
frame #2: c10::Error::Error(c10::SourceLocation, std::string) + 0x28 (0x7faf2773d854 in /torch/src/pytorch/build/lib/libc10.so)
frame #3: torch::jit::Graph::setInsertPoint(torch::jit::Node*) + 0xbf (0x431113 in ./build/bin/magicmind/test_remove_unnecessary_casts)
frame #4: torch::jit::WithInsertPoint::~WithInsertPoint() + 0x30 (0x431226 in ./build/bin/magicmind/test_remove_unnecessary_casts)
frame #5: torch_mlu::jit::parser::core::lowering::passes::RemoveSingleUse0DTensors(std::shared_ptr<torch::jit::Graph>&) + 0x14f0 (0x7faf3794ba9e in /torch/src/catch/build/jit/libjit_engine_adaptor.so)
frame #6: LoweringPasses_RemoveSingleUse0DTensorsIntCorrectly_Test::TestBody() + 0x314 (0x42ca96 in ./build/bin/magicmind/test_remove_unnecessary_casts)
frame #7: void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) + 0x65 (0x7faf35b2fcc7 in /torch/src/catch/build/test/libgtest_shared.so)
frame #8: void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*) + 0x4b (0x7faf35b2a2b6 in /torch/src/catch/build/test/libgtest_shared.so)
frame #9: testing::Test::Run() + 0xd2 (0x7faf35b0b520 in /torch/src/catch/build/test/libgtest_shared.so)
frame #10: testing::TestInfo::Run() + 0xed (0x7faf35b0bd73 in /torch/src/catch/build/test/libgtest_shared.so)
frame #11: testing::TestCase::Run() + 0x107 (0x7faf35b0c3c7 in /torch/src/catch/build/test/libgtest_shared.so)
frame #12: testing::internal::UnitTestImpl::RunAllTests() + 0x2a9 (0x7faf35b1674d in /torch/src/catch/build/test/libgtest_shared.so)
frame #13: bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) + 0x65 (0x7faf35b30cb3 in /torch/src/catch/build/test/libgtest_shared.so)
frame #14: bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*) + 0x4b (0x7faf35b2b016 in /torch/src/catch/build/test/libgtest_shared.so)
frame #15: testing::UnitTest::Run() + 0xba (0x7faf35b15330 in /torch/src/catch/build/test/libgtest_shared.so)
frame #16: RUN_ALL_TESTS() + 0x11 (0x4268fa in ./build/bin/magicmind/test_remove_unnecessary_casts)
frame #17: main + 0x31 (0x4262be in ./build/bin/magicmind/test_remove_unnecessary_casts)
frame #18: __libc_start_main + 0xf5 (0x7faef5fca555 in /lib64/libc.so.6)
frame #19: ./build/bin/magicmind/test_remove_unnecessary_casts() [0x425dd9]
```

**The reason for the error is:**
When the guard2 destructs, it tries to  set insert_before_ of the graph to the prev_ that has been destroyed, and the assert here: https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/ir/ir.h#L1396  can not be satistified.


**Should the destructor of WithInsertPoint add a check whether the prev_ is valid ?**



### Versions

master