-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[CIR] Add framework for CIR to LLVM IR lowering #124650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| //====- LowerToLLVM.h- Lowering from CIR to LLVM --------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file declares an interface for converting CIR modules to LLVM IR. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| #ifndef CLANG_CIR_LOWERTOLLVM_H | ||
| #define CLANG_CIR_LOWERTOLLVM_H | ||
|
|
||
| #include "mlir/Pass/Pass.h" | ||
|
|
||
| #include <memory> | ||
|
|
||
| namespace llvm { | ||
| class LLVMContext; | ||
| class Module; | ||
| } // namespace llvm | ||
|
|
||
| namespace mlir { | ||
| class ModuleOp; | ||
| } // namespace mlir | ||
|
|
||
| namespace cir { | ||
|
|
||
| namespace direct { | ||
| std::unique_ptr<llvm::Module> | ||
| lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp M, llvm::LLVMContext &Ctx); | ||
| } // namespace direct | ||
| } // namespace cir | ||
|
|
||
| #endif // CLANG_CIR_LOWERTOLLVM_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| add_subdirectory(DirectToLLVM) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| set(LLVM_LINK_COMPONENTS | ||
| Core | ||
| Support | ||
| ) | ||
|
|
||
| add_clang_library(clangCIRLoweringDirectToLLVM | ||
| LowerToLLVM.cpp | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| //====- LowerToLLVM.cpp - Lowering from CIR to LLVMIR ---------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file implements lowering of CIR operations to LLVMIR. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "clang/CIR/LowerToLLVM.h" | ||
|
|
||
| #include "mlir/IR/BuiltinOps.h" | ||
| #include "llvm/IR/Module.h" | ||
| #include "llvm/Support/TimeProfiler.h" | ||
|
|
||
| using namespace cir; | ||
| using namespace llvm; | ||
|
|
||
| namespace cir { | ||
| namespace direct { | ||
|
|
||
| std::unique_ptr<llvm::Module> | ||
| lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp MOp, LLVMContext &LLVMCtx) { | ||
| llvm::TimeTraceScope scope("lower from CIR to LLVM directly"); | ||
|
|
||
| std::optional<StringRef> ModuleName = MOp.getName(); | ||
| auto M = std::make_unique<llvm::Module>( | ||
| ModuleName ? *ModuleName : "CIRToLLVMModule", LLVMCtx); | ||
|
|
||
| if (!M) | ||
| report_fatal_error("Lowering from LLVMIR dialect to llvm IR failed!"); | ||
|
Comment on lines
+33
to
+34
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this mostly for debugging purposes while we get stuff up and running? The concern here is that CIR is a library interface and reporting a fatal error would be frustrating if the consumer of the library has different ideas about how to handle such a failure.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The incubator implementation uses report_fatal_error in several situations similar to this. I don't know if it was intended as a temporary measure or not. Is the right way to handle this passing in a reference to a DiagnosticsEngine?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "It depends" (sorry for the total non-answer, lol). If the reason for the failure is because of user error, then we would want to thread in a DiagnosticsEngine so we can report diagnostics the usual way. If the reason for the failure is because the CIR developer is holding things wrong, then we probably would want an assertion closer to the actual issue, and then silently return null here. If the reason for the failure leads to "there's no way to recover from this, everything is in a horrible state", then report_fatal_error is more reasonable.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. User errors will have been reported before this. I think the expectation is that we shouldn't hit this failure, but while the CIR code is still partially implemented we probably will. For now, this is kind of acting as a backstop against failures. So, returning to your original question, I think, yes, this is "mostly for debugging purposes while we get stuff up and running."
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of the things using
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a temporary measure, this is fine to leave in for now. However, I think this should migrate away from |
||
|
|
||
| return M; | ||
| } | ||
| } // namespace direct | ||
| } // namespace cir | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // Smoke test for ClangIR-to-LLVM IR code generation | ||
| // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o - | FileCheck %s | ||
|
|
||
| // TODO: Add checks when proper lowering is implemented. | ||
| // For now, we're just creating an empty module. | ||
| // CHECK: ModuleID | ||
|
|
||
| void foo() {} |
Uh oh!
There was an error while loading. Please reload this page.