[clang] [CIR] Change unreachable to diagnostic for ill-equipped clang (PR #152614)
Justin Stitt via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 8 11:07:43 PDT 2025
https://github.com/JustinStitt updated https://github.com/llvm/llvm-project/pull/152614
>From 590919c26c01c25bf7e0f16df37e3dd8d43bd2b2 Mon Sep 17 00:00:00 2001
From: Justin Stitt <justinstitt at google.com>
Date: Thu, 7 Aug 2025 16:09:54 -0700
Subject: [PATCH 1/2] [CIR] Replace unreachable with diagnostic when not built
with CLANG_ENABLE_CIR
Add a new frontend diagnostic that informs users with ill-equipped clang
binaries that they need to rebuild with -DCLANG_ENABLE_CIR=ON.
This requires adding some plumbing for the new `cir-support` lit option.
This improves the user experience as they may have a clang binary that
wasn't built with MLIR/CIR support and using `-emit-cir` results in an
unfriendly crash.
---
clang/include/clang/Basic/DiagnosticFrontendKinds.td | 2 ++
clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp | 3 ++-
clang/test/Frontend/cir-not-built.c | 12 ++++++++++++
clang/test/lit.cfg.py | 4 ++++
4 files changed, 20 insertions(+), 1 deletion(-)
create mode 100644 clang/test/Frontend/cir-not-built.c
diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 8a8db27490f06..89bca11540147 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -132,6 +132,8 @@ def err_fe_no_pch_in_dir : Error<
"no suitable precompiled header file found in directory '%0'">;
def err_fe_action_not_available : Error<
"action %0 not compiled in">;
+def err_fe_cir_not_built : Error<"clang IR support not available, rebuild "
+ "clang with -DCLANG_ENABLE_CIR=ON">;
def err_fe_invalid_multiple_actions : Error<
"'%0' action ignored; '%1' action specified previously">;
def err_fe_invalid_alignment : Error<
diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
index 443eb4f1a29bf..67372a37c05f7 100644
--- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
+++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
@@ -81,7 +81,8 @@ CreateFrontendBaseAction(CompilerInstance &CI) {
#if CLANG_ENABLE_CIR
return std::make_unique<cir::EmitCIRAction>();
#else
- llvm_unreachable("CIR suppport not built into clang");
+ CI.getDiagnostics().Report(diag::err_fe_cir_not_built);
+ return nullptr;
#endif
case EmitHTML: return std::make_unique<HTMLPrintAction>();
case EmitLLVM: {
diff --git a/clang/test/Frontend/cir-not-built.c b/clang/test/Frontend/cir-not-built.c
new file mode 100644
index 0000000000000..f9af2c4193b28
--- /dev/null
+++ b/clang/test/Frontend/cir-not-built.c
@@ -0,0 +1,12 @@
+// Test that using -emit-cir when clang is not built with CIR support gives a proper error message
+// instead of crashing.
+
+// This test should only run when CIR support is NOT enabled
+// REQUIRES: !cir-support
+
+// RUN: not %clang_cc1 -emit-cir %s 2>&1 | FileCheck %s
+// CHECK: error: clang IR support not available, rebuild clang with -DCLANG_ENABLE_CIR=ON
+
+int main(void) {
+ return 0;
+}
diff --git a/clang/test/lit.cfg.py b/clang/test/lit.cfg.py
index 1957bb1715eb6..4b2ecb0a6ca4c 100644
--- a/clang/test/lit.cfg.py
+++ b/clang/test/lit.cfg.py
@@ -224,6 +224,10 @@ def have_host_clang_repl_cuda():
)
)
+# ClangIR support
+if config.clang_enable_cir:
+ config.available_features.add("cir-support")
+
llvm_config.add_tool_substitutions(tools, tool_dirs)
config.substitutions.append(
>From 762c7f0e2753f738e689e99398c0e94559804313 Mon Sep 17 00:00:00 2001
From: Justin Stitt <justinstitt at google.com>
Date: Fri, 8 Aug 2025 11:06:36 -0700
Subject: [PATCH 2/2] use UNSUPPORTED over REQUIRES in lit test
---
clang/test/Frontend/cir-not-built.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/Frontend/cir-not-built.c b/clang/test/Frontend/cir-not-built.c
index f9af2c4193b28..57fbd280c6a45 100644
--- a/clang/test/Frontend/cir-not-built.c
+++ b/clang/test/Frontend/cir-not-built.c
@@ -2,7 +2,7 @@
// instead of crashing.
// This test should only run when CIR support is NOT enabled
-// REQUIRES: !cir-support
+// UNSUPPORTED: cir-support
// RUN: not %clang_cc1 -emit-cir %s 2>&1 | FileCheck %s
// CHECK: error: clang IR support not available, rebuild clang with -DCLANG_ENABLE_CIR=ON
More information about the cfe-commits
mailing list