[clang] [CIR] Add framework for CIR to LLVM IR lowering (PR #124650)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 28 12:51:34 PST 2025


================
@@ -7,42 +7,56 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/CIR/FrontendAction/CIRGenAction.h"
-#include "clang/CIR/CIRGenerator.h"
-#include "clang/Frontend/CompilerInstance.h"
-
 #include "mlir/IR/MLIRContext.h"
 #include "mlir/IR/OwningOpRef.h"
+#include "clang/CIR/CIRGenerator.h"
+#include "clang/CIR/LowerToLLVM.h"
+#include "clang/CodeGen/BackendUtil.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "llvm/IR/Module.h"
 
 using namespace cir;
 using namespace clang;
 
 namespace cir {
 
+static BackendAction
+getBackendActionFromOutputType(CIRGenAction::OutputType Action) {
+  switch (Action) {
+  case CIRGenAction::OutputType::EmitLLVM:
+    return BackendAction::Backend_EmitLL;
+  default:
+    llvm_unreachable("Unsupported action");
----------------
andykaylor wrote:

That makes sense. When the assertion is hit, would you rather see the code proceed to call llvm_unreachable for non-assert builds or return Backend_EmitMCNull? I'm thinking to make this forward-looking, I'll convert it to a fully-covered switch with no default and assert(false) in the Backend_EmitCIR case. That way when the other output types are added the compiler will force us to add them to the asserting case here.

So

```
  case CIRGenAction::OutputType::EmitCIR:
    assert(false && "Unsupported output type for getBackendActionFromOutputType!");
    return Backend_EmitMCNull;
```

or

```
  case CIRGenAction::OutputType::EmitCIR:
    assert(false && "Unsupported output type for getBackendActionFromOutputType!");
    llvm_unreachable("Unsuppported output type");

```
?

That does leave the `(CIRGenAction::OutputType)12` case unhandled, but the fully covered enum switch seems more consistent with general LLVM coding standards. So maybe:

```
  switch (Action) {
  case CIRGenAction::OutputType::EmitCIR:
    assert(false && "Unsupported output type for getBackendActionFromOutputType!");
    break; // Unreachable, but fall through to report that
  case CIRGenAction::OutputType::EmitLLVM:
    return BackendAction::Backend_EmitLL;
  }
  llvm_unreachable("Unsuppported output type");

```
?

https://github.com/llvm/llvm-project/pull/124650


More information about the cfe-commits mailing list