Skip to content

Commit 52325f7

Browse files
AdUhTkJmlanza
authored andcommitted
[CIR][ThroughMLIR] Lower UnreachableOp (llvm#1539)
This also removes some unused `#include`s. I choose to allow lowering to LLVM dialect when we're lowering CIR to MLIR core dialects, because some operations don't have their counterparts in these dialects (for example, `UnreachableOp -> llvm.unreachable` and `LLVMIntrinsicCallOp -> cir.llvm.intr.xxx`). I don't think we can delay them to the MLIR->LLVM pass as it seems we assume all CIR operations have been lowered after CIR->MLIR conversion. Co-authored-by: Yue Huang <[email protected]>
1 parent 6f3eeb8 commit 52325f7

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
1717
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
1818
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h"
19-
#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
20-
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
2119
#include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h"
2220
#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
2321
#include "mlir/Dialect/Affine/IR/AffineOps.h"
@@ -28,7 +26,6 @@
2826
#include "mlir/Dialect/Math/IR/Math.h"
2927
#include "mlir/Dialect/MemRef/IR/MemRef.h"
3028
#include "mlir/Dialect/SCF/IR/SCF.h"
31-
#include "mlir/Dialect/SCF/Transforms/Passes.h"
3229
#include "mlir/Dialect/Vector/IR/VectorOps.h"
3330
#include "mlir/IR/BuiltinDialect.h"
3431
#include "mlir/IR/BuiltinTypes.h"
@@ -87,7 +84,7 @@ struct ConvertCIRToMLIRPass
8784
mlir::affine::AffineDialect, mlir::memref::MemRefDialect,
8885
mlir::arith::ArithDialect, mlir::cf::ControlFlowDialect,
8986
mlir::scf::SCFDialect, mlir::math::MathDialect,
90-
mlir::vector::VectorDialect>();
87+
mlir::vector::VectorDialect, mlir::LLVM::LLVMDialect>();
9188
}
9289
void runOnOperation() final;
9390

@@ -1415,6 +1412,20 @@ class CIRPtrStrideOpLowering
14151412
}
14161413
};
14171414

1415+
class CIRUnreachableOpLowering
1416+
: public mlir::OpConversionPattern<cir::UnreachableOp> {
1417+
public:
1418+
using OpConversionPattern<cir::UnreachableOp>::OpConversionPattern;
1419+
1420+
mlir::LogicalResult
1421+
matchAndRewrite(cir::UnreachableOp op, OpAdaptor adaptor,
1422+
mlir::ConversionPatternRewriter &rewriter) const override {
1423+
// match and rewrite.
1424+
rewriter.replaceOpWithNewOp<mlir::LLVM::UnreachableOp>(op);
1425+
return mlir::success();
1426+
}
1427+
};
1428+
14181429
void populateCIRToMLIRConversionPatterns(mlir::RewritePatternSet &patterns,
14191430
mlir::TypeConverter &converter) {
14201431
patterns.add<CIRReturnLowering, CIRBrOpLowering>(patterns.getContext());
@@ -1435,7 +1446,8 @@ void populateCIRToMLIRConversionPatterns(mlir::RewritePatternSet &patterns,
14351446
CIRBitClrsbOpLowering, CIRBitFfsOpLowering, CIRBitParityOpLowering,
14361447
CIRIfOpLowering, CIRVectorCreateLowering, CIRVectorInsertLowering,
14371448
CIRVectorExtractLowering, CIRVectorCmpOpLowering, CIRACosOpLowering,
1438-
CIRASinOpLowering>(converter, patterns.getContext());
1449+
CIRASinOpLowering, CIRUnreachableOpLowering>(converter,
1450+
patterns.getContext());
14391451
}
14401452

14411453
static mlir::TypeConverter prepareTypeConverter() {
@@ -1517,11 +1529,11 @@ void ConvertCIRToMLIRPass::runOnOperation() {
15171529

15181530
mlir::ConversionTarget target(getContext());
15191531
target.addLegalOp<mlir::ModuleOp>();
1520-
target
1521-
.addLegalDialect<mlir::affine::AffineDialect, mlir::arith::ArithDialect,
1522-
mlir::memref::MemRefDialect, mlir::func::FuncDialect,
1523-
mlir::scf::SCFDialect, mlir::cf::ControlFlowDialect,
1524-
mlir::math::MathDialect, mlir::vector::VectorDialect>();
1532+
target.addLegalDialect<mlir::affine::AffineDialect, mlir::arith::ArithDialect,
1533+
mlir::memref::MemRefDialect, mlir::func::FuncDialect,
1534+
mlir::scf::SCFDialect, mlir::cf::ControlFlowDialect,
1535+
mlir::math::MathDialect, mlir::vector::VectorDialect,
1536+
mlir::LLVM::LLVMDialect>();
15251537
target.addIllegalDialect<cir::CIRDialect>();
15261538

15271539
patterns.add<CIRCastOpLowering, CIRIfOpLowering, CIRScopeOpLowering, CIRYieldOpLowering>(converter, context);

clang/lib/CIR/Lowering/ThroughMLIR/LowerMLIRToLLVM.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,12 @@
1919
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
2020
#include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h"
2121
#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
22-
#include "mlir/Dialect/Affine/IR/AffineOps.h"
23-
#include "mlir/Dialect/Arith/IR/Arith.h"
24-
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
25-
#include "mlir/Dialect/Func/IR/FuncOps.h"
2622
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
27-
#include "mlir/Dialect/MemRef/IR/MemRef.h"
28-
#include "mlir/Dialect/SCF/IR/SCF.h"
29-
#include "mlir/Dialect/SCF/Transforms/Passes.h"
30-
#include "mlir/IR/BuiltinDialect.h"
3123
#include "mlir/Pass/Pass.h"
3224
#include "mlir/Pass/PassManager.h"
3325
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
34-
#include "mlir/Target/LLVMIR/Export.h"
3526
#include "mlir/Transforms/DialectConversion.h"
36-
#include "clang/CIR/Dialect/IR/CIRDialect.h"
3727
#include "clang/CIR/Passes.h"
38-
#include "llvm/ADT/Sequence.h"
3928

4029
using namespace cir;
4130
using namespace llvm;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR
2+
3+
module {
4+
cir.func @test_unreachable() {
5+
cir.unreachable
6+
}
7+
8+
// MLIR: func.func @test_unreachable()
9+
// MLIR-NEXT: llvm.unreachable
10+
}

0 commit comments

Comments
 (0)