From 22949908f73f87104cd3d867b2f5ce0564b331f8 Mon Sep 17 00:00:00 2001 From: Yue Huang Date: Tue, 1 Apr 2025 15:08:51 +0800 Subject: [PATCH] [CIR][ThroughMLIR] Lower UnreachableOp --- .../Lowering/ThroughMLIR/LowerCIRToMLIR.cpp | 33 ++++++++++++------- .../Lowering/ThroughMLIR/LowerMLIRToLLVM.cpp | 11 ------- .../CIR/Lowering/ThroughMLIR/unreachable.cir | 10 ++++++ 3 files changed, 32 insertions(+), 22 deletions(-) create mode 100644 clang/test/CIR/Lowering/ThroughMLIR/unreachable.cir diff --git a/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp b/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp index 6ee5c2b414d3..fd1531f8eee4 100644 --- a/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp +++ b/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp @@ -16,8 +16,6 @@ #include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h" #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h" #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h" -#include "mlir/Conversion/LLVMCommon/ConversionTarget.h" -#include "mlir/Conversion/LLVMCommon/TypeConverter.h" #include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h" #include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h" #include "mlir/Dialect/Affine/IR/AffineOps.h" @@ -28,7 +26,6 @@ #include "mlir/Dialect/Math/IR/Math.h" #include "mlir/Dialect/MemRef/IR/MemRef.h" #include "mlir/Dialect/SCF/IR/SCF.h" -#include "mlir/Dialect/SCF/Transforms/Passes.h" #include "mlir/Dialect/Vector/IR/VectorOps.h" #include "mlir/IR/BuiltinDialect.h" #include "mlir/IR/BuiltinTypes.h" @@ -52,7 +49,6 @@ #include "clang/CIR/LoweringHelpers.h" #include "clang/CIR/Passes.h" #include "llvm/ADT/STLExtras.h" -#include "llvm/ADT/Sequence.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/TypeSwitch.h" #include "llvm/Support/TimeProfiler.h" @@ -83,7 +79,7 @@ struct ConvertCIRToMLIRPass mlir::affine::AffineDialect, mlir::memref::MemRefDialect, mlir::arith::ArithDialect, mlir::cf::ControlFlowDialect, mlir::scf::SCFDialect, mlir::math::MathDialect, - mlir::vector::VectorDialect>(); + mlir::vector::VectorDialect, mlir::LLVM::LLVMDialect>(); } void runOnOperation() final; @@ -1388,6 +1384,20 @@ class CIRPtrStrideOpLowering } }; +class CIRUnreachableOpLowering + : public mlir::OpConversionPattern { +public: + using OpConversionPattern::OpConversionPattern; + + mlir::LogicalResult + matchAndRewrite(cir::UnreachableOp op, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const override { + // match and rewrite. + rewriter.replaceOpWithNewOp(op); + return mlir::success(); + } +}; + void populateCIRToMLIRConversionPatterns(mlir::RewritePatternSet &patterns, mlir::TypeConverter &converter) { patterns.add(patterns.getContext()); @@ -1408,7 +1418,8 @@ void populateCIRToMLIRConversionPatterns(mlir::RewritePatternSet &patterns, CIRBitClrsbOpLowering, CIRBitFfsOpLowering, CIRBitParityOpLowering, CIRIfOpLowering, CIRVectorCreateLowering, CIRVectorInsertLowering, CIRVectorExtractLowering, CIRVectorCmpOpLowering, CIRACosOpLowering, - CIRASinOpLowering>(converter, patterns.getContext()); + CIRASinOpLowering, CIRUnreachableOpLowering>(converter, + patterns.getContext()); } static mlir::TypeConverter prepareTypeConverter() { @@ -1490,11 +1501,11 @@ void ConvertCIRToMLIRPass::runOnOperation() { mlir::ConversionTarget target(getContext()); target.addLegalOp(); - target - .addLegalDialect(); + target.addLegalDialect(); target.addIllegalDialect(); if (failed(applyPartialConversion(module, target, std::move(patterns)))) diff --git a/clang/lib/CIR/Lowering/ThroughMLIR/LowerMLIRToLLVM.cpp b/clang/lib/CIR/Lowering/ThroughMLIR/LowerMLIRToLLVM.cpp index 728c061dcac0..f46fe4533c2f 100644 --- a/clang/lib/CIR/Lowering/ThroughMLIR/LowerMLIRToLLVM.cpp +++ b/clang/lib/CIR/Lowering/ThroughMLIR/LowerMLIRToLLVM.cpp @@ -19,23 +19,12 @@ #include "mlir/Conversion/LLVMCommon/TypeConverter.h" #include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h" #include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h" -#include "mlir/Dialect/Affine/IR/AffineOps.h" -#include "mlir/Dialect/Arith/IR/Arith.h" -#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h" -#include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" -#include "mlir/Dialect/MemRef/IR/MemRef.h" -#include "mlir/Dialect/SCF/IR/SCF.h" -#include "mlir/Dialect/SCF/Transforms/Passes.h" -#include "mlir/IR/BuiltinDialect.h" #include "mlir/Pass/Pass.h" #include "mlir/Pass/PassManager.h" #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h" -#include "mlir/Target/LLVMIR/Export.h" #include "mlir/Transforms/DialectConversion.h" -#include "clang/CIR/Dialect/IR/CIRDialect.h" #include "clang/CIR/Passes.h" -#include "llvm/ADT/Sequence.h" using namespace cir; using namespace llvm; diff --git a/clang/test/CIR/Lowering/ThroughMLIR/unreachable.cir b/clang/test/CIR/Lowering/ThroughMLIR/unreachable.cir new file mode 100644 index 000000000000..1e94300b1b60 --- /dev/null +++ b/clang/test/CIR/Lowering/ThroughMLIR/unreachable.cir @@ -0,0 +1,10 @@ +// RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR + +module { + cir.func @test_unreachable() { + cir.unreachable + } + + // MLIR: func.func @test_unreachable() + // MLIR-NEXT: llvm.unreachable +}