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

Julius Alexandre via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 4 18:16:44 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 =
----------------
medievalghoul wrote:

presuming I have to also change 
```
  auto *UI = cast<sandboxir::UnreachableInst>(&*It++);
  auto *Ret = &*It++;
```
to
```
  auto *Ret = &*It++;
  auto *UI = cast<sandboxir::UnreachableInst>(&*It++);
```
I encountered a seg fault. So I decided to add another instruction to help alleviate 
```Assertion `It != ItE && "Already at end!"' failed.```

now my LL looks like this

```
define void @foo() {
  call void @llvm.donothing()
  unreachable
  ret void
}
```

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


More information about the llvm-commits mailing list