From b36d392efff5b560b197445f86622dc3cc38e977 Mon Sep 17 00:00:00 2001 From: Keyi Zhang Date: Mon, 25 Sep 2023 23:01:00 -0700 Subject: [PATCH] [CIR][Lowering] use cir.int type in LIT tests (#266) Here is the promised patch that adds proper type conversion to CIR -> MLIR conversion. I tried to keep the changes minimum but the existing implementation doesn't use `TypeConverter`. This should not have any functional changes except for one tiny fix that registers the `cf` dialect, which should allow `goto.mlir` to pass. Happy to break the PR into two if requested. --- .../Lowering/ThroughMLIR/LowerCIRToMLIR.cpp | 316 +++++++++--------- clang/test/CIR/Lowering/ThroughMLIR/array.cir | 11 +- .../ThroughMLIR/binop-unsigned-int.cir | 91 ++--- clang/test/CIR/Lowering/ThroughMLIR/cmp.cir | 41 +-- clang/test/CIR/Lowering/ThroughMLIR/goto.cir | 23 +- .../test/CIR/Lowering/ThroughMLIR/memref.cir | 13 +- clang/test/CIR/Lowering/ThroughMLIR/scope.cir | 7 +- .../Lowering/ThroughMLIR/unary-inc-dec.cir | 23 +- .../Lowering/ThroughMLIR/unary-plus-minus.cir | 23 +- 9 files changed, 278 insertions(+), 270 deletions(-) diff --git a/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp b/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp index b2921abd16c0..c3b819e16d6e 100644 --- a/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp +++ b/clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp @@ -72,47 +72,35 @@ struct ConvertCIRToMLIRPass virtual StringRef getArgument() const override { return "cir-to-mlir"; } }; -class CIRCallLowering : public mlir::OpRewritePattern { +class CIRCallLowering : public mlir::OpConversionPattern { public: - using OpRewritePattern::OpRewritePattern; + using OpConversionPattern::OpConversionPattern; mlir::LogicalResult - matchAndRewrite(mlir::cir::CallOp op, - mlir::PatternRewriter &rewriter) const override { + matchAndRewrite(mlir::cir::CallOp op, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const override { + SmallVector types; + if (mlir::failed( + getTypeConverter()->convertTypes(op.getResultTypes(), types))) + return mlir::failure(); rewriter.replaceOpWithNewOp( - op, mlir::SymbolRefAttr::get(op), op.getResultTypes(), - op.getArgOperands()); + op, mlir::SymbolRefAttr::get(op), types, adaptor.getOperands()); return mlir::LogicalResult::success(); } }; -class CIRAllocaLowering : public mlir::OpRewritePattern { +class CIRAllocaLowering + : public mlir::OpConversionPattern { public: - using OpRewritePattern::OpRewritePattern; + using OpConversionPattern::OpConversionPattern; mlir::LogicalResult - matchAndRewrite(mlir::cir::AllocaOp op, - mlir::PatternRewriter &rewriter) const override { - auto type = op.getAllocaType(); - mlir::MemRefType memreftype; - - if (type.isa()) { - auto integerType = - mlir::IntegerType::get(getContext(), 8, mlir::IntegerType::Signless); - memreftype = mlir::MemRefType::get({}, integerType); - } else if (type.isa()) { - mlir::cir::ArrayType arraytype = type.dyn_cast(); - memreftype = - mlir::MemRefType::get(arraytype.getSize(), arraytype.getEltType()); - } else if (type.isa() || type.isa()) { - memreftype = mlir::MemRefType::get({}, op.getAllocaType()); - } else if (type.isa()) { - auto ptrType = type.cast(); - auto innerMemref = mlir::MemRefType::get({-1}, ptrType.getPointee()); - memreftype = mlir::MemRefType::get({}, innerMemref); - } else { - llvm_unreachable("type to be allocated not supported yet"); - } + matchAndRewrite(mlir::cir::AllocaOp op, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const override { + auto type = adaptor.getAllocaType(); + auto mlirType = getTypeConverter()->convertType(type); + + auto memreftype = mlir::MemRefType::get({}, mlirType); rewriter.replaceOpWithNewOp(op, memreftype, op.getAlignmentAttr()); return mlir::LogicalResult::success(); @@ -131,44 +119,39 @@ class CIRLoadLowering : public mlir::OpConversionPattern { } }; -class CIRStoreLowering : public mlir::ConversionPattern { +class CIRStoreLowering : public mlir::OpConversionPattern { public: - CIRStoreLowering(mlir::MLIRContext *ctx) - : mlir::ConversionPattern(mlir::cir::StoreOp::getOperationName(), 1, - ctx) {} + using OpConversionPattern::OpConversionPattern; mlir::LogicalResult - matchAndRewrite(mlir::Operation *op, ArrayRef operands, + matchAndRewrite(mlir::cir::StoreOp op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) const override { - rewriter.replaceOpWithNewOp(op, operands[0], - operands[1]); + rewriter.replaceOpWithNewOp(op, adaptor.getValue(), + adaptor.getAddr()); return mlir::LogicalResult::success(); } }; class CIRConstantLowering - : public mlir::OpRewritePattern { + : public mlir::OpConversionPattern { public: - using OpRewritePattern::OpRewritePattern; + using OpConversionPattern::OpConversionPattern; mlir::LogicalResult - matchAndRewrite(mlir::cir::ConstantOp op, - mlir::PatternRewriter &rewriter) const override { - if (op.getType().isa()) { - mlir::Type type = - mlir::IntegerType::get(getContext(), 8, mlir::IntegerType::Signless); - mlir::TypedAttr IntegerAttr; - if (op.getValue() == - mlir::cir::BoolAttr::get( - getContext(), ::mlir::cir::BoolType::get(getContext()), true)) - IntegerAttr = mlir::IntegerAttr::get(type, 1); - else - IntegerAttr = mlir::IntegerAttr::get(type, 0); - rewriter.replaceOpWithNewOp(op, type, - IntegerAttr); - } else - rewriter.replaceOpWithNewOp(op, op.getType(), - op.getValue()); + matchAndRewrite(mlir::cir::ConstantOp op, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const override { + auto ty = getTypeConverter()->convertType(op.getType()); + mlir::TypedAttr value; + if (mlir::isa(op.getType())) { + auto boolValue = mlir::cast(op.getValue()); + value = rewriter.getIntegerAttr(ty, boolValue.getValue()); + } else { + auto cirIntAttr = mlir::dyn_cast(op.getValue()); + assert(cirIntAttr && "NYI non cir.int attr"); + value = rewriter.getIntegerAttr( + ty, cast(op.getValue()).getValue()); + } + rewriter.replaceOpWithNewOp(op, ty, value); return mlir::LogicalResult::success(); } }; @@ -211,29 +194,28 @@ class CIRFuncLowering : public mlir::OpConversionPattern { } }; -class CIRUnaryOpLowering : public mlir::OpRewritePattern { +class CIRUnaryOpLowering + : public mlir::OpConversionPattern { public: - using OpRewritePattern::OpRewritePattern; + using OpConversionPattern::OpConversionPattern; mlir::LogicalResult - matchAndRewrite(mlir::cir::UnaryOp op, - mlir::PatternRewriter &rewriter) const override { - mlir::Type type = op.getInput().getType(); - assert(type.isa() && "operand type not supported yet"); + matchAndRewrite(mlir::cir::UnaryOp op, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const override { + auto input = adaptor.getInput(); + auto type = getTypeConverter()->convertType(op.getType()); switch (op.getKind()) { case mlir::cir::UnaryOpKind::Inc: { auto One = rewriter.create( op.getLoc(), type, mlir::IntegerAttr::get(type, 1)); - rewriter.replaceOpWithNewOp(op, op.getType(), - op.getInput(), One); + rewriter.replaceOpWithNewOp(op, type, input, One); break; } case mlir::cir::UnaryOpKind::Dec: { auto One = rewriter.create( op.getLoc(), type, mlir::IntegerAttr::get(type, 1)); - rewriter.replaceOpWithNewOp(op, op.getType(), - op.getInput(), One); + rewriter.replaceOpWithNewOp(op, type, input, One); break; } case mlir::cir::UnaryOpKind::Plus: { @@ -243,15 +225,14 @@ class CIRUnaryOpLowering : public mlir::OpRewritePattern { case mlir::cir::UnaryOpKind::Minus: { auto Zero = rewriter.create( op.getLoc(), type, mlir::IntegerAttr::get(type, 0)); - rewriter.replaceOpWithNewOp(op, op.getType(), Zero, - op.getInput()); + rewriter.replaceOpWithNewOp(op, type, Zero, input); break; } case mlir::cir::UnaryOpKind::Not: { auto MinusOne = rewriter.create( op.getLoc(), type, mlir::IntegerAttr::get(type, -1)); - rewriter.replaceOpWithNewOp(op, op.getType(), - MinusOne, op.getInput()); + rewriter.replaceOpWithNewOp(op, type, MinusOne, + input); break; } } @@ -260,77 +241,78 @@ class CIRUnaryOpLowering : public mlir::OpRewritePattern { } }; -class CIRBinOpLowering : public mlir::OpRewritePattern { +class CIRBinOpLowering : public mlir::OpConversionPattern { public: - using OpRewritePattern::OpRewritePattern; + using OpConversionPattern::OpConversionPattern; mlir::LogicalResult - matchAndRewrite(mlir::cir::BinOp op, - mlir::PatternRewriter &rewriter) const override { - assert((op.getLhs().getType() == op.getRhs().getType()) && + matchAndRewrite(mlir::cir::BinOp op, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const override { + assert((adaptor.getLhs().getType() == adaptor.getRhs().getType()) && "inconsistent operands' types not supported yet"); - mlir::Type type = op.getRhs().getType(); - assert((type.isa() || type.isa()) && + mlir::Type mlirType = getTypeConverter()->convertType(op.getType()); + assert((mlirType.isa() || + mlirType.isa()) && "operand type not supported yet"); switch (op.getKind()) { case mlir::cir::BinOpKind::Add: - if (type.isa()) + if (mlirType.isa()) rewriter.replaceOpWithNewOp( - op, op.getType(), op.getLhs(), op.getRhs()); + op, mlirType, adaptor.getLhs(), adaptor.getRhs()); else rewriter.replaceOpWithNewOp( - op, op.getType(), op.getLhs(), op.getRhs()); + op, mlirType, adaptor.getLhs(), adaptor.getRhs()); break; case mlir::cir::BinOpKind::Sub: - if (type.isa()) + if (mlirType.isa()) rewriter.replaceOpWithNewOp( - op, op.getType(), op.getLhs(), op.getRhs()); + op, mlirType, adaptor.getLhs(), adaptor.getRhs()); else rewriter.replaceOpWithNewOp( - op, op.getType(), op.getLhs(), op.getRhs()); + op, mlirType, adaptor.getLhs(), adaptor.getRhs()); break; case mlir::cir::BinOpKind::Mul: - if (type.isa()) + if (mlirType.isa()) rewriter.replaceOpWithNewOp( - op, op.getType(), op.getLhs(), op.getRhs()); + op, mlirType, adaptor.getLhs(), adaptor.getRhs()); else rewriter.replaceOpWithNewOp( - op, op.getType(), op.getLhs(), op.getRhs()); + op, mlirType, adaptor.getLhs(), adaptor.getRhs()); break; case mlir::cir::BinOpKind::Div: - if (type.isa()) { - if (type.isSignlessInteger()) + if (mlirType.isa()) { + if (mlirType.isSignlessInteger()) rewriter.replaceOpWithNewOp( - op, op.getType(), op.getLhs(), op.getRhs()); + op, mlirType, adaptor.getLhs(), adaptor.getRhs()); else - llvm_unreachable("integer type not supported in CIR yet"); + llvm_unreachable("integer mlirType not supported in CIR yet"); } else rewriter.replaceOpWithNewOp( - op, op.getType(), op.getLhs(), op.getRhs()); + op, mlirType, adaptor.getLhs(), adaptor.getRhs()); break; case mlir::cir::BinOpKind::Rem: - if (type.isa()) { - if (type.isSignlessInteger()) + if (mlirType.isa()) { + if (mlirType.isSignlessInteger()) rewriter.replaceOpWithNewOp( - op, op.getType(), op.getLhs(), op.getRhs()); + op, mlirType, adaptor.getLhs(), adaptor.getRhs()); else - llvm_unreachable("integer type not supported in CIR yet"); + llvm_unreachable("integer mlirType not supported in CIR yet"); } else rewriter.replaceOpWithNewOp( - op, op.getType(), op.getLhs(), op.getRhs()); + op, mlirType, adaptor.getLhs(), adaptor.getRhs()); break; case mlir::cir::BinOpKind::And: rewriter.replaceOpWithNewOp( - op, op.getType(), op.getLhs(), op.getRhs()); + op, mlirType, adaptor.getLhs(), adaptor.getRhs()); break; case mlir::cir::BinOpKind::Or: - rewriter.replaceOpWithNewOp(op, op.getType(), - op.getLhs(), op.getRhs()); + rewriter.replaceOpWithNewOp( + op, mlirType, adaptor.getLhs(), adaptor.getRhs()); break; case mlir::cir::BinOpKind::Xor: rewriter.replaceOpWithNewOp( - op, op.getType(), op.getLhs(), op.getRhs()); + op, mlirType, adaptor.getLhs(), adaptor.getRhs()); break; } @@ -338,17 +320,18 @@ class CIRBinOpLowering : public mlir::OpRewritePattern { } }; -class CIRCmpOpLowering : public mlir::OpRewritePattern { +class CIRCmpOpLowering : public mlir::OpConversionPattern { public: - using OpRewritePattern::OpRewritePattern; + using OpConversionPattern::OpConversionPattern; mlir::LogicalResult - matchAndRewrite(mlir::cir::CmpOp op, - mlir::PatternRewriter &rewriter) const override { - auto type = op.getLhs().getType(); + matchAndRewrite(mlir::cir::CmpOp op, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const override { + auto type = adaptor.getLhs().getType(); auto integerType = mlir::IntegerType::get(getContext(), 1, mlir::IntegerType::Signless); + mlir::Value mlirResult; switch (op.getKind()) { case mlir::cir::CmpOpKind::gt: { if (type.isa()) { @@ -356,16 +339,16 @@ class CIRCmpOpLowering : public mlir::OpRewritePattern { if (!type.isSignlessInteger()) llvm_unreachable("integer type not supported in CIR yet"); cmpIType = mlir::arith::CmpIPredicate::ugt; - rewriter.replaceOpWithNewOp( - op, integerType, + mlirResult = rewriter.create( + op.getLoc(), integerType, mlir::arith::CmpIPredicateAttr::get(getContext(), cmpIType), - op.getLhs(), op.getRhs()); + adaptor.getLhs(), adaptor.getRhs()); } else if (type.isa()) { - rewriter.replaceOpWithNewOp( - op, integerType, + mlirResult = rewriter.create( + op.getLoc(), integerType, mlir::arith::CmpFPredicateAttr::get( getContext(), mlir::arith::CmpFPredicate::UGT), - op.getLhs(), op.getRhs(), + adaptor.getLhs(), adaptor.getRhs(), mlir::arith::FastMathFlagsAttr::get( getContext(), mlir::arith::FastMathFlags::none)); } else { @@ -379,16 +362,16 @@ class CIRCmpOpLowering : public mlir::OpRewritePattern { if (!type.isSignlessInteger()) llvm_unreachable("integer type not supported in CIR yet"); cmpIType = mlir::arith::CmpIPredicate::uge; - rewriter.replaceOpWithNewOp( - op, integerType, + mlirResult = rewriter.create( + op.getLoc(), integerType, mlir::arith::CmpIPredicateAttr::get(getContext(), cmpIType), - op.getLhs(), op.getRhs()); + adaptor.getLhs(), adaptor.getRhs()); } else if (type.isa()) { - rewriter.replaceOpWithNewOp( - op, integerType, + mlirResult = rewriter.create( + op.getLoc(), integerType, mlir::arith::CmpFPredicateAttr::get( getContext(), mlir::arith::CmpFPredicate::UGE), - op.getLhs(), op.getRhs(), + adaptor.getLhs(), adaptor.getRhs(), mlir::arith::FastMathFlagsAttr::get( getContext(), mlir::arith::FastMathFlags::none)); } else { @@ -402,19 +385,18 @@ class CIRCmpOpLowering : public mlir::OpRewritePattern { if (!type.isSignlessInteger()) llvm_unreachable("integer type not supported in CIR yet"); cmpIType = mlir::arith::CmpIPredicate::ult; - rewriter.replaceOpWithNewOp( - op, integerType, + mlirResult = rewriter.create( + op.getLoc(), integerType, mlir::arith::CmpIPredicateAttr::get(getContext(), cmpIType), - op.getLhs(), op.getRhs()); + adaptor.getLhs(), adaptor.getRhs()); } else if (type.isa()) { - rewriter.replaceOpWithNewOp( - op, integerType, + mlirResult = rewriter.create( + op.getLoc(), integerType, mlir::arith::CmpFPredicateAttr::get( getContext(), mlir::arith::CmpFPredicate::ULT), - op.getLhs(), op.getRhs(), + adaptor.getLhs(), adaptor.getRhs(), mlir::arith::FastMathFlagsAttr::get( getContext(), mlir::arith::FastMathFlags::none)); - } else { llvm_unreachable("Unknown Operand Type"); } @@ -426,16 +408,16 @@ class CIRCmpOpLowering : public mlir::OpRewritePattern { if (!type.isSignlessInteger()) llvm_unreachable("integer type not supported in CIR yet"); cmpIType = mlir::arith::CmpIPredicate::ule; - rewriter.replaceOpWithNewOp( - op, integerType, + mlirResult = rewriter.create( + op.getLoc(), integerType, mlir::arith::CmpIPredicateAttr::get(getContext(), cmpIType), - op.getLhs(), op.getRhs()); + adaptor.getLhs(), adaptor.getRhs()); } else if (type.isa()) { - rewriter.replaceOpWithNewOp( - op, integerType, + mlirResult = rewriter.create( + op.getLoc(), integerType, mlir::arith::CmpFPredicateAttr::get( getContext(), mlir::arith::CmpFPredicate::ULE), - op.getLhs(), op.getRhs(), + adaptor.getLhs(), adaptor.getRhs(), mlir::arith::FastMathFlagsAttr::get( getContext(), mlir::arith::FastMathFlags::none)); } else { @@ -445,17 +427,17 @@ class CIRCmpOpLowering : public mlir::OpRewritePattern { } case mlir::cir::CmpOpKind::eq: { if (type.isa()) { - rewriter.replaceOpWithNewOp( - op, integerType, + mlirResult = rewriter.create( + op.getLoc(), integerType, mlir::arith::CmpIPredicateAttr::get(getContext(), mlir::arith::CmpIPredicate::eq), - op.getLhs(), op.getRhs()); + adaptor.getLhs(), adaptor.getRhs()); } else if (type.isa()) { - rewriter.replaceOpWithNewOp( - op, integerType, + mlirResult = rewriter.create( + op.getLoc(), integerType, mlir::arith::CmpFPredicateAttr::get( getContext(), mlir::arith::CmpFPredicate::UEQ), - op.getLhs(), op.getRhs(), + adaptor.getLhs(), adaptor.getRhs(), mlir::arith::FastMathFlagsAttr::get( getContext(), mlir::arith::FastMathFlags::none)); } else { @@ -465,17 +447,17 @@ class CIRCmpOpLowering : public mlir::OpRewritePattern { } case mlir::cir::CmpOpKind::ne: { if (type.isa()) { - rewriter.replaceOpWithNewOp( - op, integerType, + mlirResult = rewriter.create( + op.getLoc(), integerType, mlir::arith::CmpIPredicateAttr::get(getContext(), mlir::arith::CmpIPredicate::ne), - op.getLhs(), op.getRhs()); + adaptor.getLhs(), adaptor.getRhs()); } else if (type.isa()) { - rewriter.replaceOpWithNewOp( - op, integerType, + mlirResult = rewriter.create( + op.getLoc(), integerType, mlir::arith::CmpFPredicateAttr::get( getContext(), mlir::arith::CmpFPredicate::UNE), - op.getLhs(), op.getRhs(), + adaptor.getLhs(), adaptor.getRhs(), mlir::arith::FastMathFlagsAttr::get( getContext(), mlir::arith::FastMathFlags::none)); } else { @@ -485,6 +467,13 @@ class CIRCmpOpLowering : public mlir::OpRewritePattern { } } + // MLIR comparison ops return i1, but cir::CmpOp returns the same type as + // the LHS value. Since this return value can be used later, we need to + // restore the type with the extension below. + auto mlirResultTy = getTypeConverter()->convertType(op.getType()); + rewriter.replaceOpWithNewOp(op, mlirResultTy, + mlirResult); + return mlir::LogicalResult::success(); } }; @@ -501,12 +490,13 @@ class CIRBrOpLowering : public mlir::OpRewritePattern { } }; -class CIRScopeOpLowering : public mlir::OpRewritePattern { - using mlir::OpRewritePattern::OpRewritePattern; +class CIRScopeOpLowering + : public mlir::OpConversionPattern { + using mlir::OpConversionPattern::OpConversionPattern; mlir::LogicalResult - matchAndRewrite(mlir::cir::ScopeOp scopeOp, - mlir::PatternRewriter &rewriter) const override { + matchAndRewrite(mlir::cir::ScopeOp scopeOp, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const override { // Empty scope: just remove it. if (scopeOp.getRegion().empty()) { rewriter.eraseOp(scopeOp); @@ -520,9 +510,14 @@ class CIRScopeOpLowering : public mlir::OpRewritePattern { terminator, terminator->getOperands()); } + SmallVector mlirResultTypes; + if (mlir::failed(getTypeConverter()->convertTypes(scopeOp->getResultTypes(), + mlirResultTypes))) + return mlir::LogicalResult::failure(); + rewriter.setInsertionPoint(scopeOp); auto newScopeOp = rewriter.create( - scopeOp.getLoc(), scopeOp.getResultTypes()); + scopeOp.getLoc(), mlirResultTypes); rewriter.inlineRegionBefore(scopeOp.getScopeRegion(), newScopeOp.getBodyRegion(), newScopeOp.getBodyRegion().end()); @@ -534,17 +529,19 @@ class CIRScopeOpLowering : public mlir::OpRewritePattern { void populateCIRToMLIRConversionPatterns(mlir::RewritePatternSet &patterns, mlir::TypeConverter &converter) { - patterns.add(patterns.getContext()); - patterns.add(converter, patterns.getContext()); + patterns.add(patterns.getContext()); + + patterns.add(converter, patterns.getContext()); } static mlir::TypeConverter prepareTypeConverter() { mlir::TypeConverter converter; converter.addConversion([&](mlir::cir::PointerType type) -> mlir::Type { - return mlir::MemRefType::get({-1}, type.getPointee()); + auto ty = converter.convertType(type.getPointee()); + return mlir::MemRefType::get({}, ty); }); converter.addConversion( [&](mlir::IntegerType type) -> mlir::Type { return type; }); @@ -552,6 +549,19 @@ static mlir::TypeConverter prepareTypeConverter() { [&](mlir::FloatType type) -> mlir::Type { return type; }); converter.addConversion( [&](mlir::cir::VoidType type) -> mlir::Type { return {}; }); + converter.addConversion([&](mlir::cir::IntType type) -> mlir::Type { + // arith dialect ops doesn't take signed integer -- drop cir sign here + return mlir::IntegerType::get( + type.getContext(), type.getWidth(), + mlir::IntegerType::SignednessSemantics::Signless); + }); + converter.addConversion([&](mlir::cir::BoolType type) -> mlir::Type { + return mlir::IntegerType::get(type.getContext(), 8); + }); + converter.addConversion([&](mlir::cir::ArrayType type) -> mlir::Type { + auto elementType = converter.convertType(type.getEltType()); + return mlir::MemRefType::get(type.getSize(), elementType); + }); return converter; } diff --git a/clang/test/CIR/Lowering/ThroughMLIR/array.cir b/clang/test/CIR/Lowering/ThroughMLIR/array.cir index fc69ff680f4e..40e622928769 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/array.cir +++ b/clang/test/CIR/Lowering/ThroughMLIR/array.cir @@ -1,5 +1,4 @@ // RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR -// RUN: cir-opt %s -cir-to-mlir -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM module { cir.func @foo() { @@ -10,15 +9,7 @@ module { // MLIR: module { // MLIR-NEXT: func @foo() { -// MLIR-NEXT: = memref.alloca() {alignment = 16 : i64} : memref<10xi32> +// MLIR-NEXT: = memref.alloca() {alignment = 16 : i64} : memref> // MLIR-NEXT: return // MLIR-NEXT: } // MLIR-NEXT: } - -// LLVM: = alloca i32, i64 10, align 16 -// LLVM-NEXT: = insertvalue { ptr, ptr, i64, [1 x i64], [1 x i64] } undef, ptr %1, 0 -// LLVM-NEXT: = insertvalue { ptr, ptr, i64, [1 x i64], [1 x i64] } %2, ptr %1, 1 -// LLVM-NEXT: = insertvalue { ptr, ptr, i64, [1 x i64], [1 x i64] } %3, i64 0, 2 -// LLVM-NEXT: = insertvalue { ptr, ptr, i64, [1 x i64], [1 x i64] } %4, i64 10, 3, 0 -// LLVM-NEXT: = insertvalue { ptr, ptr, i64, [1 x i64], [1 x i64] } %5, i64 1, 4, 0 -// LLVM-NEXT: ret void diff --git a/clang/test/CIR/Lowering/ThroughMLIR/binop-unsigned-int.cir b/clang/test/CIR/Lowering/ThroughMLIR/binop-unsigned-int.cir index 138ada1dd42e..51c89f564efa 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/binop-unsigned-int.cir +++ b/clang/test/CIR/Lowering/ThroughMLIR/binop-unsigned-int.cir @@ -1,55 +1,56 @@ // RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR // RUN: cir-opt %s -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM +!u32i = !cir.int module { cir.func @foo() { - %0 = cir.alloca i32, cir.ptr , ["a", init] {alignment = 4 : i64} - %1 = cir.alloca i32, cir.ptr , ["b", init] {alignment = 4 : i64} - %2 = cir.alloca i32, cir.ptr , ["x", init] {alignment = 4 : i64} - %3 = cir.const(2 : i32) : i32 cir.store %3, %0 : i32, cir.ptr - %4 = cir.const(1 : i32) : i32 cir.store %4, %1 : i32, cir.ptr - %5 = cir.load %0 : cir.ptr , i32 - %6 = cir.load %1 : cir.ptr , i32 - %7 = cir.binop(mul, %5, %6) : i32 - cir.store %7, %2 : i32, cir.ptr - %8 = cir.load %2 : cir.ptr , i32 - %9 = cir.load %1 : cir.ptr , i32 - %10 = cir.binop(div, %8, %9) : i32 - cir.store %10, %2 : i32, cir.ptr - %11 = cir.load %2 : cir.ptr , i32 - %12 = cir.load %1 : cir.ptr , i32 - %13 = cir.binop(rem, %11, %12) : i32 - cir.store %13, %2 : i32, cir.ptr - %14 = cir.load %2 : cir.ptr , i32 - %15 = cir.load %1 : cir.ptr , i32 - %16 = cir.binop(add, %14, %15) : i32 - cir.store %16, %2 : i32, cir.ptr - %17 = cir.load %2 : cir.ptr , i32 - %18 = cir.load %1 : cir.ptr , i32 - %19 = cir.binop(sub, %17, %18) : i32 - cir.store %19, %2 : i32, cir.ptr + %0 = cir.alloca !u32i, cir.ptr , ["a", init] {alignment = 4 : i64} + %1 = cir.alloca !u32i, cir.ptr , ["b", init] {alignment = 4 : i64} + %2 = cir.alloca !u32i, cir.ptr , ["x", init] {alignment = 4 : i64} + %3 = cir.const(#cir.int<2> : !u32i) : !u32i cir.store %3, %0 : !u32i, cir.ptr + %4 = cir.const(#cir.int<1> : !u32i) : !u32i cir.store %4, %1 : !u32i, cir.ptr + %5 = cir.load %0 : cir.ptr , !u32i + %6 = cir.load %1 : cir.ptr , !u32i + %7 = cir.binop(mul, %5, %6) : !u32i + cir.store %7, %2 : !u32i, cir.ptr + %8 = cir.load %2 : cir.ptr , !u32i + %9 = cir.load %1 : cir.ptr , !u32i + %10 = cir.binop(div, %8, %9) : !u32i + cir.store %10, %2 : !u32i, cir.ptr + %11 = cir.load %2 : cir.ptr , !u32i + %12 = cir.load %1 : cir.ptr , !u32i + %13 = cir.binop(rem, %11, %12) : !u32i + cir.store %13, %2 : !u32i, cir.ptr + %14 = cir.load %2 : cir.ptr , !u32i + %15 = cir.load %1 : cir.ptr , !u32i + %16 = cir.binop(add, %14, %15) : !u32i + cir.store %16, %2 : !u32i, cir.ptr + %17 = cir.load %2 : cir.ptr , !u32i + %18 = cir.load %1 : cir.ptr , !u32i + %19 = cir.binop(sub, %17, %18) : !u32i + cir.store %19, %2 : !u32i, cir.ptr // should move to cir.shift, which only accepts // CIR types. - // %20 = cir.load %2 : cir.ptr , i32 - // %21 = cir.load %1 : cir.ptr , i32 - // %22 = cir.binop(shr, %20, %21) : i32 - // cir.store %22, %2 : i32, cir.ptr - // %23 = cir.load %2 : cir.ptr , i32 - // %24 = cir.load %1 : cir.ptr , i32 - // %25 = cir.binop(shl, %23, %24) : i32 - // cir.store %25, %2 : i32, cir.ptr - %26 = cir.load %2 : cir.ptr , i32 - %27 = cir.load %1 : cir.ptr , i32 - %28 = cir.binop(and, %26, %27) : i32 - cir.store %28, %2 : i32, cir.ptr - %29 = cir.load %2 : cir.ptr , i32 - %30 = cir.load %1 : cir.ptr , i32 - %31 = cir.binop(xor, %29, %30) : i32 - cir.store %31, %2 : i32, cir.ptr - %32 = cir.load %2 : cir.ptr , i32 - %33 = cir.load %1 : cir.ptr , i32 - %34 = cir.binop(or, %32, %33) : i32 - cir.store %34, %2 : i32, cir.ptr + // %20 = cir.load %2 : cir.ptr , !u32i + // %21 = cir.load %1 : cir.ptr , !u32i + // %22 = cir.binop(shr, %20, %21) : !u32i + // cir.store %22, %2 : !u32i, cir.ptr + // %23 = cir.load %2 : cir.ptr , !u32i + // %24 = cir.load %1 : cir.ptr , !u32i + // %25 = cir.binop(shl, %23, %24) : !u32i + // cir.store %25, %2 : !u32i, cir.ptr + %26 = cir.load %2 : cir.ptr , !u32i + %27 = cir.load %1 : cir.ptr , !u32i + %28 = cir.binop(and, %26, %27) : !u32i + cir.store %28, %2 : !u32i, cir.ptr + %29 = cir.load %2 : cir.ptr , !u32i + %30 = cir.load %1 : cir.ptr , !u32i + %31 = cir.binop(xor, %29, %30) : !u32i + cir.store %31, %2 : !u32i, cir.ptr + %32 = cir.load %2 : cir.ptr , !u32i + %33 = cir.load %1 : cir.ptr , !u32i + %34 = cir.binop(or, %32, %33) : !u32i + cir.store %34, %2 : !u32i, cir.ptr cir.return } } diff --git a/clang/test/CIR/Lowering/ThroughMLIR/cmp.cir b/clang/test/CIR/Lowering/ThroughMLIR/cmp.cir index 5a8816a1ef99..190d8a2256d4 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/cmp.cir +++ b/clang/test/CIR/Lowering/ThroughMLIR/cmp.cir @@ -1,31 +1,32 @@ // RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR // RUN: cir-opt %s -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM +!s32i = !cir.int module { cir.func @foo() { - %0 = cir.alloca i32, cir.ptr , ["a"] {alignment = 4 : i64} - %1 = cir.alloca i32, cir.ptr , ["b"] {alignment = 4 : i64} + %0 = cir.alloca !s32i, cir.ptr , ["a"] {alignment = 4 : i64} + %1 = cir.alloca !s32i, cir.ptr , ["b"] {alignment = 4 : i64} %2 = cir.alloca f32, cir.ptr , ["c"] {alignment = 4 : i64} %3 = cir.alloca f32, cir.ptr , ["d"] {alignment = 4 : i64} %4 = cir.alloca !cir.bool, cir.ptr , ["e"] {alignment = 1 : i64} - %5 = cir.load %0 : cir.ptr , i32 - %6 = cir.load %1 : cir.ptr , i32 - %7 = cir.cmp(gt, %5, %6) : i32, !cir.bool - %8 = cir.load %0 : cir.ptr , i32 - %9 = cir.load %1 : cir.ptr , i32 - %10 = cir.cmp(eq, %8, %9) : i32, !cir.bool - %11 = cir.load %0 : cir.ptr , i32 - %12 = cir.load %1 : cir.ptr , i32 - %13 = cir.cmp(lt, %11, %12) : i32, !cir.bool - %14 = cir.load %0 : cir.ptr , i32 - %15 = cir.load %1 : cir.ptr , i32 - %16 = cir.cmp(ge, %14, %15) : i32, !cir.bool - %17 = cir.load %0 : cir.ptr , i32 - %18 = cir.load %1 : cir.ptr , i32 - %19 = cir.cmp(ne, %17, %18) : i32, !cir.bool - %20 = cir.load %0 : cir.ptr , i32 - %21 = cir.load %1 : cir.ptr , i32 - %22 = cir.cmp(le, %20, %21) : i32, !cir.bool + %5 = cir.load %0 : cir.ptr , !s32i + %6 = cir.load %1 : cir.ptr , !s32i + %7 = cir.cmp(gt, %5, %6) : !s32i, !cir.bool + %8 = cir.load %0 : cir.ptr , !s32i + %9 = cir.load %1 : cir.ptr , !s32i + %10 = cir.cmp(eq, %8, %9) : !s32i, !cir.bool + %11 = cir.load %0 : cir.ptr , !s32i + %12 = cir.load %1 : cir.ptr , !s32i + %13 = cir.cmp(lt, %11, %12) : !s32i, !cir.bool + %14 = cir.load %0 : cir.ptr , !s32i + %15 = cir.load %1 : cir.ptr , !s32i + %16 = cir.cmp(ge, %14, %15) : !s32i, !cir.bool + %17 = cir.load %0 : cir.ptr , !s32i + %18 = cir.load %1 : cir.ptr , !s32i + %19 = cir.cmp(ne, %17, %18) : !s32i, !cir.bool + %20 = cir.load %0 : cir.ptr , !s32i + %21 = cir.load %1 : cir.ptr , !s32i + %22 = cir.cmp(le, %20, %21) : !s32i, !cir.bool %23 = cir.load %2 : cir.ptr , f32 %24 = cir.load %3 : cir.ptr , f32 %25 = cir.cmp(gt, %23, %24) : f32, !cir.bool diff --git a/clang/test/CIR/Lowering/ThroughMLIR/goto.cir b/clang/test/CIR/Lowering/ThroughMLIR/goto.cir index 4f1b9cccb312..9cc9cc45b65f 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/goto.cir +++ b/clang/test/CIR/Lowering/ThroughMLIR/goto.cir @@ -1,23 +1,24 @@ // RUN: cir-opt %s -canonicalize -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR // RUN: cir-opt %s -canonicalize -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM +!u32i = !cir.int module { cir.func @foo() { - %0 = cir.alloca i32, cir.ptr , ["b", init] {alignment = 4 : i64} - %1 = cir.const(1 : i32) : i32 - cir.store %1, %0 : i32, cir.ptr + %0 = cir.alloca !u32i, cir.ptr , ["b", init] {alignment = 4 : i64} + %1 = cir.const(#cir.int<1> : !u32i) : !u32i + cir.store %1, %0 : !u32i, cir.ptr cir.br ^bb2 ^bb1: // no predecessors - %2 = cir.load %0 : cir.ptr , i32 - %3 = cir.const(1 : i32) : i32 - %4 = cir.binop(add, %2, %3) : i32 - cir.store %4, %0 : i32, cir.ptr + %2 = cir.load %0 : cir.ptr , !u32i + %3 = cir.const(#cir.int<1> : !u32i) : !u32i + %4 = cir.binop(add, %2, %3) : !u32i + cir.store %4, %0 : !u32i, cir.ptr cir.br ^bb2 ^bb2: // 2 preds: ^bb0, ^bb1 - %5 = cir.load %0 : cir.ptr , i32 - %6 = cir.const(2 : i32) : i32 - %7 = cir.binop(add, %5, %6) : i32 - cir.store %7, %0 : i32, cir.ptr + %5 = cir.load %0 : cir.ptr , !u32i + %6 = cir.const(#cir.int<2> : !u32i) : !u32i + %7 = cir.binop(add, %5, %6) : !u32i + cir.store %7, %0 : !u32i, cir.ptr cir.return } } diff --git a/clang/test/CIR/Lowering/ThroughMLIR/memref.cir b/clang/test/CIR/Lowering/ThroughMLIR/memref.cir index e957d3ef16cd..ad338992806b 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/memref.cir +++ b/clang/test/CIR/Lowering/ThroughMLIR/memref.cir @@ -1,13 +1,14 @@ // RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR // RUN: cir-opt %s -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM +!u32i = !cir.int module { - cir.func @foo() -> i32 { - %0 = cir.alloca i32, cir.ptr , ["x", init] {alignment = 4 : i64} - %1 = cir.const(1 : i32) : i32 - cir.store %1, %0 : i32, cir.ptr - %2 = cir.load %0 : cir.ptr , i32 - cir.return %2 : i32 + cir.func @foo() -> !u32i { + %0 = cir.alloca !u32i, cir.ptr , ["x", init] {alignment = 4 : i64} + %1 = cir.const(#cir.int<1> : !u32i) : !u32i + cir.store %1, %0 : !u32i, cir.ptr + %2 = cir.load %0 : cir.ptr , !u32i + cir.return %2 : !u32i } } diff --git a/clang/test/CIR/Lowering/ThroughMLIR/scope.cir b/clang/test/CIR/Lowering/ThroughMLIR/scope.cir index 310580683cd2..6d877351b7c6 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/scope.cir +++ b/clang/test/CIR/Lowering/ThroughMLIR/scope.cir @@ -1,12 +1,13 @@ // RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR // RUN: cir-opt %s -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM +!u32i = !cir.int module { cir.func @foo() { cir.scope { - %0 = cir.alloca i32, cir.ptr , ["a", init] {alignment = 4 : i64} - %1 = cir.const(4 : i32) : i32 - cir.store %1, %0 : i32, cir.ptr + %0 = cir.alloca !u32i, cir.ptr , ["a", init] {alignment = 4 : i64} + %1 = cir.const(#cir.int<4> : !u32i) : !u32i + cir.store %1, %0 : !u32i, cir.ptr } cir.return } diff --git a/clang/test/CIR/Lowering/ThroughMLIR/unary-inc-dec.cir b/clang/test/CIR/Lowering/ThroughMLIR/unary-inc-dec.cir index 57541c194206..45368fb48f40 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/unary-inc-dec.cir +++ b/clang/test/CIR/Lowering/ThroughMLIR/unary-inc-dec.cir @@ -1,21 +1,22 @@ // RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR // RUN: cir-opt %s -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM +!s32i = !cir.int module { cir.func @foo() { - %0 = cir.alloca i32, cir.ptr , ["a", init] {alignment = 4 : i64} - %1 = cir.alloca i32, cir.ptr , ["b", init] {alignment = 4 : i64} - %2 = cir.const(2 : i32) : i32 - cir.store %2, %0 : i32, cir.ptr - cir.store %2, %1 : i32, cir.ptr + %0 = cir.alloca !s32i, cir.ptr , ["a", init] {alignment = 4 : i64} + %1 = cir.alloca !s32i, cir.ptr , ["b", init] {alignment = 4 : i64} + %2 = cir.const(#cir.int<2> : !s32i) : !s32i + cir.store %2, %0 : !s32i, cir.ptr + cir.store %2, %1 : !s32i, cir.ptr - %3 = cir.load %0 : cir.ptr , i32 - %4 = cir.unary(inc, %3) : i32, i32 - cir.store %4, %0 : i32, cir.ptr + %3 = cir.load %0 : cir.ptr , !s32i + %4 = cir.unary(inc, %3) : !s32i, !s32i + cir.store %4, %0 : !s32i, cir.ptr - %5 = cir.load %1 : cir.ptr , i32 - %6 = cir.unary(dec, %5) : i32, i32 - cir.store %6, %1 : i32, cir.ptr + %5 = cir.load %1 : cir.ptr , !s32i + %6 = cir.unary(dec, %5) : !s32i, !s32i + cir.store %6, %1 : !s32i, cir.ptr cir.return } } diff --git a/clang/test/CIR/Lowering/ThroughMLIR/unary-plus-minus.cir b/clang/test/CIR/Lowering/ThroughMLIR/unary-plus-minus.cir index 09f16f4d342f..013bc65e95e3 100644 --- a/clang/test/CIR/Lowering/ThroughMLIR/unary-plus-minus.cir +++ b/clang/test/CIR/Lowering/ThroughMLIR/unary-plus-minus.cir @@ -1,21 +1,22 @@ // RUN: cir-opt %s -cir-to-mlir -o - | FileCheck %s -check-prefix=MLIR // RUN: cir-opt %s -cir-to-mlir -cir-mlir-to-llvm -o - | mlir-translate -mlir-to-llvmir | FileCheck %s -check-prefix=LLVM +!s32i = !cir.int module { cir.func @foo() { - %0 = cir.alloca i32, cir.ptr , ["a", init] {alignment = 4 : i64} - %1 = cir.alloca i32, cir.ptr , ["b", init] {alignment = 4 : i64} - %2 = cir.const(2 : i32) : i32 - cir.store %2, %0 : i32, cir.ptr - cir.store %2, %1 : i32, cir.ptr + %0 = cir.alloca !s32i, cir.ptr , ["a", init] {alignment = 4 : i64} + %1 = cir.alloca !s32i, cir.ptr , ["b", init] {alignment = 4 : i64} + %2 = cir.const(#cir.int<2> : !s32i) : !s32i + cir.store %2, %0 : !s32i, cir.ptr + cir.store %2, %1 : !s32i, cir.ptr - %3 = cir.load %0 : cir.ptr , i32 - %4 = cir.unary(plus, %3) : i32, i32 - cir.store %4, %0 : i32, cir.ptr + %3 = cir.load %0 : cir.ptr , !s32i + %4 = cir.unary(plus, %3) : !s32i, !s32i + cir.store %4, %0 : !s32i, cir.ptr - %5 = cir.load %1 : cir.ptr , i32 - %6 = cir.unary(minus, %5) : i32, i32 - cir.store %6, %1 : i32, cir.ptr + %5 = cir.load %1 : cir.ptr , !s32i + %6 = cir.unary(minus, %5) : !s32i, !s32i + cir.store %6, %1 : !s32i, cir.ptr cir.return } }