From 2c420066da0e4f6abb2be21d0c24fbff6c928fb0 Mon Sep 17 00:00:00 2001 From: Bart Coppens Date: Fri, 12 Jan 2024 18:56:09 +0100 Subject: [PATCH 1/2] Add support for the llvm.is.constant.* intrinsics. (#185) Add support for the llvm.is.constant.* intrinsics. According to the LLVM LangRef, this intrinsic will never return true when constant folding is not run, so always returning false is safe. Partially resolves issue #149. --- lib/Target/CBackend/CBackend.cpp | 6 ++++++ test/cpp_tests/test_is_constant.cpp | 8 ++++++++ 2 files changed, 14 insertions(+) create mode 100644 test/cpp_tests/test_is_constant.cpp diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp index 339e4a9b..98cb6986 100644 --- a/lib/Target/CBackend/CBackend.cpp +++ b/lib/Target/CBackend/CBackend.cpp @@ -2573,6 +2573,7 @@ void CWriter::generateHeader(Module &M) { case Intrinsic::trunc: case Intrinsic::umax: case Intrinsic::umin: + case Intrinsic::is_constant: intrinsicsToDefine.push_back(&*I); continue; } @@ -4643,6 +4644,9 @@ void CWriter::printIntrinsicDefinition(FunctionType *funT, unsigned Opcode, case Intrinsic::umin: Out << " r = a < b ? a : b;\n"; break; + case Intrinsic::is_constant: + Out << " r = 0 /* llvm.is.constant */;\n"; + break; } } else { @@ -4764,6 +4768,7 @@ bool CWriter::lowerIntrinsics(Function &F) { case Intrinsic::dbg_declare: case Intrinsic::umax: case Intrinsic::umin: + case Intrinsic::is_constant: // We directly implement these intrinsics break; @@ -5078,6 +5083,7 @@ bool CWriter::visitBuiltinCall(CallInst &I, Intrinsic::ID ID) { case Intrinsic::trap: case Intrinsic::umax: case Intrinsic::umin: + case Intrinsic::is_constant: return false; // these use the normal function call emission } } diff --git a/test/cpp_tests/test_is_constant.cpp b/test/cpp_tests/test_is_constant.cpp new file mode 100644 index 00000000..e1e1f9f7 --- /dev/null +++ b/test/cpp_tests/test_is_constant.cpp @@ -0,0 +1,8 @@ +int f(int i) { + if (__builtin_constant_p(i) && + i == 1) /* generates the llvm.is.constant.i32 intrinsic at -O0 */ + return i + 5; + return 6; +} + +int main() { return f(1); } From 1bc2950a391b80ac6812f2c5101f04e052b5e861 Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Fri, 12 Jan 2024 14:22:29 -0500 Subject: [PATCH 2/2] build: explicit specify LLVM_DIR to cmake (#189) --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5e289e19..e5ed6a75 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -65,7 +65,7 @@ jobs: - name: Build run: | mkdir build - cmake -S . -B build -DLLVM_INCLUDE_TESTS=On + cmake -S . -B build -DLLVM_INCLUDE_TESTS=On -DLLVM_DIR=/usr/lib/llvm-16/cmake cmake --build build - name: Test