Files
Lang Hames 8748e5e3ac [orc-rt] Add ORC_RT_UNREACHABLE / ORC_RT_BUILTIN_UNREACHABLE (#209466)
ORC_RT_BUILTIN_UNREACHABLE is an optimizer hint that a location is
unreachable, wrapping __builtin_unreachable / __assume(false). Executing
it is undefined behavior.

ORC_RT_UNREACHABLE(MSG) marks a point the program must never reach. It
aborts with MSG in +Asserts builds; otherwise it lowers to
ORC_RT_BUILTIN_UNREACHABLE.

Adds a CompilerTest unit test covering both the returning-function
compile path and, in +Asserts builds only, the abort behavior via a
death test.
2026-07-14 13:05:55 +00:00

44 lines
1.2 KiB
C++

//===- CompilerTest.cpp ---------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Tests for the utilities in orc-rt/Compiler.h.
//
//===----------------------------------------------------------------------===//
#include "orc-rt/Compiler.h"
#include "gtest/gtest.h"
namespace {
int reachablePathReturns(int X) {
switch (X) {
case 0:
return 0;
default:
ORC_RT_UNREACHABLE("only 0 is expected");
}
}
} // anonymous namespace
TEST(CompilerTest, UnreachableCompilesInReturningFunction) {
EXPECT_EQ(reachablePathReturns(0), 0);
}
// ORC_RT_UNREACHABLE only aborts in +Asserts builds; under NDEBUG it lowers to
// a bare optimizer hint whose execution is undefined behavior, so there is
// nothing well-defined to assert on in a release build. Test for !NDEBUG builds
// only.
#ifndef NDEBUG
TEST(CompilerDeathTest, UnreachableAborts) {
EXPECT_DEATH(ORC_RT_UNREACHABLE("unreachable reached"),
"unreachable reached");
}
#endif