Skip to content

Commit d46715a

Browse files
authored
clang: Emit error if assembler fails to construct subtarget (#159219)
We do not have consistent or good error handling of this situation. Some tools check for errors, some just assert. The backend has no proper way of reporting an invalid subtarget specification. MCSubtargetInfo currently does unreasonable things like spam warnings to errs, and then silently proceed in an invalid state. I have a patch which starts returning null on some invalid subtargets, but all the tools need to start erroring cleanly first. I don't think there is a reliable way to test this today. It would have to be an incomplete backend. Ideally we would thread through some kind of error context from the target to report the reason it's an invalid subtarget.
1 parent 18420d8 commit d46715a

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

clang/include/clang/Basic/DiagnosticFrontendKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ def err_fe_unable_to_load_plugin : Error<
115115
"unable to load plugin '%0': '%1'">;
116116
def err_fe_unable_to_create_target : Error<
117117
"unable to create target: '%0'">;
118+
def err_fe_unable_to_create_subtarget : Error<
119+
"unable to create subtarget: '%0'%select{ with features '%2'|}1">;
118120
def err_fe_unable_to_interface_with_target : Error<
119121
"unable to interface with target machine">;
120122
def err_fe_unable_to_open_output : Error<

clang/tools/driver/cc1as_main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,10 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
480480

481481
std::unique_ptr<MCSubtargetInfo> STI(
482482
TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
483-
assert(STI && "Unable to create subtarget info!");
483+
if (!STI) {
484+
return Diags.Report(diag::err_fe_unable_to_create_subtarget)
485+
<< Opts.CPU << FS.empty() << FS;
486+
}
484487

485488
MCContext Ctx(Triple(Opts.Triple), MAI.get(), MRI.get(), STI.get(), &SrcMgr,
486489
&MCOptions);

0 commit comments

Comments
 (0)