[llvm] [SandboxIR] Implement UnreachableInst (PR #101856)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 4 19:01:14 PDT 2024


================
@@ -2037,3 +2037,32 @@ define void @foo(i32 %arg) {
   }
   EXPECT_EQ(NewPHI->getNumIncomingValues(), PHI->getNumIncomingValues());
 }
+
+TEST_F(SandboxIRTest, UnreachableInst) {
+  parseIR(C, R"IR(
+define void @foo() {
+  ret void
+  unreachable
+}
+)IR");
+  llvm::Function *LLVMF = &*M->getFunction("foo");
+  sandboxir::Context Ctx(C);
+  sandboxir::Function *F = Ctx.createFunction(LLVMF);
+  auto *BB = &*F->begin();
+  auto It = BB->begin();
+  auto *Ret = &*It++;
+  auto *UI = cast<sandboxir::UnreachableInst>(&*It++);
+
+  EXPECT_EQ(UI->getNumSuccessors(), 0u);
+  EXPECT_EQ(UI->getNumOfIRInstrs(), 1u);
+  // Check create(InsertBefore)
+  sandboxir::UnreachableInst *NewUI =
+      sandboxir::UnreachableInst::create(/*InsertBefore=*/Ret, Ctx);
+  EXPECT_NE(NewUI, nullptr);
+  EXPECT_EQ(NewUI->getParent(), BB);
+  // Check create(InsertAtEnd)
+  sandboxir::UnreachableInst *NewUIEnd =
----------------
vporpo wrote:

> presuming I have to also change

Yes, change it to:
```
  auto *UI = cast<sandboxir::UnreachableInst>(&*It++);
  auto *Ret = `cast<sandboxir::ReturnInst>(&*It++);
```

You shouldn't need another instruction. Calling create with `/*InsertBefore=*/Ret` should work fine.

What kind of crash are you getting with this code? Could you share the backtrace?

https://github.com/llvm/llvm-project/pull/101856


More information about the llvm-commits mailing list