|
| 1 | +//====- LowerCIRToMLIR.cpp - Lowering from CIR to MLIR --------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file implements lowering of CIR operations to MLIR. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 14 | +#include "mlir/IR/BuiltinDialect.h" |
| 15 | +#include "mlir/Pass/Pass.h" |
| 16 | +#include "mlir/Pass/PassManager.h" |
| 17 | +#include "mlir/Transforms/DialectConversion.h" |
| 18 | +#include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 19 | +#include "clang/CIR/Dialect/IR/CIRTypes.h" |
| 20 | +#include "clang/CIR/LowerToLLVM.h" |
| 21 | +#include "clang/CIR/MissingFeatures.h" |
| 22 | +#include "llvm/ADT/TypeSwitch.h" |
| 23 | +#include "llvm/Support/TimeProfiler.h" |
| 24 | + |
| 25 | +using namespace cir; |
| 26 | +using namespace llvm; |
| 27 | + |
| 28 | +namespace cir { |
| 29 | + |
| 30 | +struct ConvertCIRToMLIRPass |
| 31 | + : public mlir::PassWrapper<ConvertCIRToMLIRPass, |
| 32 | + mlir::OperationPass<mlir::ModuleOp>> { |
| 33 | + void getDependentDialects(mlir::DialectRegistry ®istry) const override { |
| 34 | + registry.insert<mlir::BuiltinDialect, mlir::memref::MemRefDialect>(); |
| 35 | + } |
| 36 | + void runOnOperation() final; |
| 37 | + |
| 38 | + StringRef getDescription() const override { |
| 39 | + return "Convert the CIR dialect module to MLIR standard dialects"; |
| 40 | + } |
| 41 | + |
| 42 | + StringRef getArgument() const override { return "cir-to-mlir"; } |
| 43 | +}; |
| 44 | + |
| 45 | +class CIRGlobalOpLowering : public mlir::OpConversionPattern<cir::GlobalOp> { |
| 46 | +public: |
| 47 | + using OpConversionPattern<cir::GlobalOp>::OpConversionPattern; |
| 48 | + mlir::LogicalResult |
| 49 | + matchAndRewrite(cir::GlobalOp op, OpAdaptor adaptor, |
| 50 | + mlir::ConversionPatternRewriter &rewriter) const override { |
| 51 | + auto moduleOp = op->getParentOfType<mlir::ModuleOp>(); |
| 52 | + if (!moduleOp) |
| 53 | + return mlir::failure(); |
| 54 | + |
| 55 | + mlir::OpBuilder b(moduleOp.getContext()); |
| 56 | + |
| 57 | + const auto cirSymType = op.getSymType(); |
| 58 | + assert(!cir::MissingFeatures::convertTypeForMemory()); |
| 59 | + auto convertedType = getTypeConverter()->convertType(cirSymType); |
| 60 | + if (!convertedType) |
| 61 | + return mlir::failure(); |
| 62 | + auto memrefType = dyn_cast<mlir::MemRefType>(convertedType); |
| 63 | + if (!memrefType) |
| 64 | + memrefType = mlir::MemRefType::get({}, convertedType); |
| 65 | + // Add an optional alignment to the global memref. |
| 66 | + assert(!cir::MissingFeatures::opGlobalAlignment()); |
| 67 | + mlir::IntegerAttr memrefAlignment = mlir::IntegerAttr(); |
| 68 | + // Add an optional initial value to the global memref. |
| 69 | + mlir::Attribute initialValue = mlir::Attribute(); |
| 70 | + std::optional<mlir::Attribute> init = op.getInitialValue(); |
| 71 | + if (init.has_value()) { |
| 72 | + initialValue = |
| 73 | + llvm::TypeSwitch<mlir::Attribute, mlir::Attribute>(init.value()) |
| 74 | + .Case<cir::IntAttr>([&](cir::IntAttr attr) { |
| 75 | + auto rtt = mlir::RankedTensorType::get({}, convertedType); |
| 76 | + return mlir::DenseIntElementsAttr::get(rtt, attr.getValue()); |
| 77 | + }) |
| 78 | + .Case<cir::FPAttr>([&](cir::FPAttr attr) { |
| 79 | + auto rtt = mlir::RankedTensorType::get({}, convertedType); |
| 80 | + return mlir::DenseFPElementsAttr::get(rtt, attr.getValue()); |
| 81 | + }) |
| 82 | + .Default([&](mlir::Attribute attr) { |
| 83 | + llvm_unreachable("GlobalOp lowering with initial value is not " |
| 84 | + "fully supported yet"); |
| 85 | + return mlir::Attribute(); |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + // Add symbol visibility |
| 90 | + assert(!cir::MissingFeatures::opGlobalLinkage()); |
| 91 | + std::string symVisibility = "public"; |
| 92 | + |
| 93 | + assert(!cir::MissingFeatures::opGlobalConstant()); |
| 94 | + bool isConstant = false; |
| 95 | + |
| 96 | + rewriter.replaceOpWithNewOp<mlir::memref::GlobalOp>( |
| 97 | + op, b.getStringAttr(op.getSymName()), |
| 98 | + /*sym_visibility=*/b.getStringAttr(symVisibility), |
| 99 | + /*type=*/memrefType, initialValue, |
| 100 | + /*constant=*/isConstant, |
| 101 | + /*alignment=*/memrefAlignment); |
| 102 | + |
| 103 | + return mlir::success(); |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +void populateCIRToMLIRConversionPatterns(mlir::RewritePatternSet &patterns, |
| 108 | + mlir::TypeConverter &converter) { |
| 109 | + patterns.add<CIRGlobalOpLowering>(converter, patterns.getContext()); |
| 110 | +} |
| 111 | + |
| 112 | +static mlir::TypeConverter prepareTypeConverter() { |
| 113 | + mlir::TypeConverter converter; |
| 114 | + converter.addConversion([&](cir::PointerType type) -> mlir::Type { |
| 115 | + assert(!cir::MissingFeatures::convertTypeForMemory()); |
| 116 | + mlir::Type ty = converter.convertType(type.getPointee()); |
| 117 | + // FIXME: The pointee type might not be converted (e.g. struct) |
| 118 | + if (!ty) |
| 119 | + return nullptr; |
| 120 | + return mlir::MemRefType::get({}, ty); |
| 121 | + }); |
| 122 | + converter.addConversion( |
| 123 | + [&](mlir::IntegerType type) -> mlir::Type { return type; }); |
| 124 | + converter.addConversion( |
| 125 | + [&](mlir::FloatType type) -> mlir::Type { return type; }); |
| 126 | + converter.addConversion([&](cir::VoidType type) -> mlir::Type { return {}; }); |
| 127 | + converter.addConversion([&](cir::IntType type) -> mlir::Type { |
| 128 | + // arith dialect ops doesn't take signed integer -- drop cir sign here |
| 129 | + return mlir::IntegerType::get( |
| 130 | + type.getContext(), type.getWidth(), |
| 131 | + mlir::IntegerType::SignednessSemantics::Signless); |
| 132 | + }); |
| 133 | + converter.addConversion([&](cir::SingleType type) -> mlir::Type { |
| 134 | + return mlir::Float32Type::get(type.getContext()); |
| 135 | + }); |
| 136 | + converter.addConversion([&](cir::DoubleType type) -> mlir::Type { |
| 137 | + return mlir::Float64Type::get(type.getContext()); |
| 138 | + }); |
| 139 | + converter.addConversion([&](cir::FP80Type type) -> mlir::Type { |
| 140 | + return mlir::Float80Type::get(type.getContext()); |
| 141 | + }); |
| 142 | + converter.addConversion([&](cir::LongDoubleType type) -> mlir::Type { |
| 143 | + return converter.convertType(type.getUnderlying()); |
| 144 | + }); |
| 145 | + converter.addConversion([&](cir::FP128Type type) -> mlir::Type { |
| 146 | + return mlir::Float128Type::get(type.getContext()); |
| 147 | + }); |
| 148 | + converter.addConversion([&](cir::FP16Type type) -> mlir::Type { |
| 149 | + return mlir::Float16Type::get(type.getContext()); |
| 150 | + }); |
| 151 | + converter.addConversion([&](cir::BF16Type type) -> mlir::Type { |
| 152 | + return mlir::BFloat16Type::get(type.getContext()); |
| 153 | + }); |
| 154 | + |
| 155 | + return converter; |
| 156 | +} |
| 157 | + |
| 158 | +void ConvertCIRToMLIRPass::runOnOperation() { |
| 159 | + auto module = getOperation(); |
| 160 | + |
| 161 | + auto converter = prepareTypeConverter(); |
| 162 | + |
| 163 | + mlir::RewritePatternSet patterns(&getContext()); |
| 164 | + |
| 165 | + populateCIRToMLIRConversionPatterns(patterns, converter); |
| 166 | + |
| 167 | + mlir::ConversionTarget target(getContext()); |
| 168 | + target.addLegalOp<mlir::ModuleOp>(); |
| 169 | + target.addLegalDialect<mlir::memref::MemRefDialect>(); |
| 170 | + target.addIllegalDialect<cir::CIRDialect>(); |
| 171 | + |
| 172 | + if (failed(applyPartialConversion(module, target, std::move(patterns)))) |
| 173 | + signalPassFailure(); |
| 174 | +} |
| 175 | + |
| 176 | +std::unique_ptr<mlir::Pass> createConvertCIRToMLIRPass() { |
| 177 | + return std::make_unique<ConvertCIRToMLIRPass>(); |
| 178 | +} |
| 179 | + |
| 180 | +mlir::ModuleOp lowerFromCIRToMLIR(mlir::ModuleOp mlirModule, |
| 181 | + mlir::MLIRContext &mlirCtx) { |
| 182 | + llvm::TimeTraceScope scope("Lower CIR To MLIR"); |
| 183 | + |
| 184 | + mlir::PassManager pm(&mlirCtx); |
| 185 | + |
| 186 | + pm.addPass(createConvertCIRToMLIRPass()); |
| 187 | + |
| 188 | + auto result = !mlir::failed(pm.run(mlirModule)); |
| 189 | + if (!result) |
| 190 | + llvm::report_fatal_error( |
| 191 | + "The pass manager failed to lower CIR to MLIR standard dialects!"); |
| 192 | + |
| 193 | + // Now that we ran all the lowering passes, verify the final output. |
| 194 | + if (mlirModule.verify().failed()) |
| 195 | + llvm::report_fatal_error( |
| 196 | + "Verification of the final MLIR in standard dialects failed!"); |
| 197 | + |
| 198 | + return mlirModule; |
| 199 | +} |
| 200 | + |
| 201 | +} // namespace cir |
0 commit comments