[clang] [CIR] Initial implementation of CIR-to-LLVM IR lowering pass (PR #125260)
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 31 13:20:53 PST 2025
================
@@ -22,16 +34,156 @@ using namespace llvm;
namespace cir {
namespace direct {
+struct ConvertCIRToLLVMPass
+ : public mlir::PassWrapper<ConvertCIRToLLVMPass,
+ mlir::OperationPass<mlir::ModuleOp>> {
+ void getDependentDialects(mlir::DialectRegistry ®istry) const override {
+ registry.insert<mlir::BuiltinDialect, mlir::DLTIDialect,
+ mlir::LLVM::LLVMDialect, mlir::func::FuncDialect>();
+ }
+ void runOnOperation() final;
+
+ StringRef getDescription() const override {
+ return "Convert the prepared CIR dialect module to LLVM dialect";
+ }
+
+ StringRef getArgument() const override { return "cir-flat-to-llvm"; }
+};
+
+// This pass requires the CIR to be in a "flat" state. All blocks in each
+// function must belong to the parent region.
+mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
+ cir::GlobalOp op, OpAdaptor adaptor,
+ mlir::ConversionPatternRewriter &rewriter) const {
+
+ // Fetch required values to create LLVM op.
+ const mlir::Type cirSymType = op.getSymType();
+
+ // This is the LLVM dialect type.
+ const mlir::Type llvmType = getTypeConverter()->convertType(cirSymType);
+ // FIXME: These default values are placeholders until the the equivalent
+ // attributes are available on cir.global ops.
+ const bool isConst = false;
+ const unsigned addrSpace = 0;
+ const bool isDsoLocal = true;
+ const bool isThreadLocal = false;
+ const uint64_t alignment = 0;
+ const mlir::LLVM::Linkage linkage = mlir::LLVM::Linkage::External;
+ const StringRef symbol = op.getSymName();
+ std::optional<mlir::Attribute> init = op.getInitialValue();
+
+ SmallVector<mlir::NamedAttribute> attributes;
+
+ if (init.has_value()) {
+ if (auto fltAttr = mlir::dyn_cast<cir::FPAttr>(init.value())) {
+ // Initializer is a constant floating-point number: convert to MLIR
----------------
erichkeane wrote:
At a certain point, a type-visitor is going to be something we want for here, but ATM this is fine.
https://github.com/llvm/llvm-project/pull/125260
More information about the cfe-commits
mailing list