https://github.com/arsenm created 
https://github.com/llvm/llvm-project/pull/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.

>From 7f1c1c79059e45b25734446bc19b4f109a3b973b Mon Sep 17 00:00:00 2001
From: Matt Arsenault <matthew.arsena...@amd.com>
Date: Wed, 17 Sep 2025 09:58:43 +0900
Subject: [PATCH] clang: Emit error if assembler fails to construct subtarget

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.
---
 clang/include/clang/Basic/DiagnosticFrontendKinds.td | 2 ++
 clang/tools/driver/cc1as_main.cpp                    | 5 ++++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td 
b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 2fd2ae434d7c5..254b13be005bb 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -115,6 +115,8 @@ def err_fe_unable_to_load_plugin : Error<
     "unable to load plugin '%0': '%1'">;
 def err_fe_unable_to_create_target : Error<
     "unable to create target: '%0'">;
+def err_fe_unable_to_create_subtarget : Error<
+    "unable to create subtarget: '%0'%select{ with features '%2'|}1">;
 def err_fe_unable_to_interface_with_target : Error<
     "unable to interface with target machine">;
 def err_fe_unable_to_open_output : Error<
diff --git a/clang/tools/driver/cc1as_main.cpp 
b/clang/tools/driver/cc1as_main.cpp
index 5c30de33c7b46..dc0f74957774a 100644
--- a/clang/tools/driver/cc1as_main.cpp
+++ b/clang/tools/driver/cc1as_main.cpp
@@ -480,7 +480,10 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
 
   std::unique_ptr<MCSubtargetInfo> STI(
       TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
-  assert(STI && "Unable to create subtarget info!");
+  if (!STI) {
+    return Diags.Report(diag::err_fe_unable_to_create_subtarget)
+           << Opts.CPU << FS.empty() << FS;
+  }
 
   MCContext Ctx(Triple(Opts.Triple), MAI.get(), MRI.get(), STI.get(), &SrcMgr,
                 &MCOptions);

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to