Skip to content

Commit 590919c

Browse files
committed
[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.
1 parent dddeb07 commit 590919c

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

clang/include/clang/Basic/DiagnosticFrontendKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ def err_fe_no_pch_in_dir : Error<
132132
"no suitable precompiled header file found in directory '%0'">;
133133
def err_fe_action_not_available : Error<
134134
"action %0 not compiled in">;
135+
def err_fe_cir_not_built : Error<"clang IR support not available, rebuild "
136+
"clang with -DCLANG_ENABLE_CIR=ON">;
135137
def err_fe_invalid_multiple_actions : Error<
136138
"'%0' action ignored; '%1' action specified previously">;
137139
def err_fe_invalid_alignment : Error<

clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ CreateFrontendBaseAction(CompilerInstance &CI) {
8181
#if CLANG_ENABLE_CIR
8282
return std::make_unique<cir::EmitCIRAction>();
8383
#else
84-
llvm_unreachable("CIR suppport not built into clang");
84+
CI.getDiagnostics().Report(diag::err_fe_cir_not_built);
85+
return nullptr;
8586
#endif
8687
case EmitHTML: return std::make_unique<HTMLPrintAction>();
8788
case EmitLLVM: {

clang/test/Frontend/cir-not-built.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Test that using -emit-cir when clang is not built with CIR support gives a proper error message
2+
// instead of crashing.
3+
4+
// This test should only run when CIR support is NOT enabled
5+
// REQUIRES: !cir-support
6+
7+
// RUN: not %clang_cc1 -emit-cir %s 2>&1 | FileCheck %s
8+
// CHECK: error: clang IR support not available, rebuild clang with -DCLANG_ENABLE_CIR=ON
9+
10+
int main(void) {
11+
return 0;
12+
}

clang/test/lit.cfg.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ def have_host_clang_repl_cuda():
224224
)
225225
)
226226

227+
# ClangIR support
228+
if config.clang_enable_cir:
229+
config.available_features.add("cir-support")
230+
227231
llvm_config.add_tool_substitutions(tools, tool_dirs)
228232

229233
config.substitutions.append(

0 commit comments

Comments
 (0)