Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions mlir/lib/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,33 @@ static FailureOr<Block *> getConvertedBlock(ConversionPatternRewriter &rewriter,
return rewriter.applySignatureConversion(block, *conversion, converter);
}

/// Flatten the given value ranges into a single vector of values.
static SmallVector<Value> flattenValues(ArrayRef<ValueRange> values) {
SmallVector<Value> result;
for (const auto &vals : values)
llvm::append_range(result, vals);
return result;
}

/// Convert the destination block signature (if necessary) and lower the branch
/// op to llvm.br.
struct BranchOpLowering : public ConvertOpToLLVMPattern<cf::BranchOp> {
using ConvertOpToLLVMPattern<cf::BranchOp>::ConvertOpToLLVMPattern;
using Adaptor =
typename ConvertOpToLLVMPattern<cf::BranchOp>::OneToNOpAdaptor;

LogicalResult
matchAndRewrite(cf::BranchOp op, typename cf::BranchOp::Adaptor adaptor,
matchAndRewrite(cf::BranchOp op, Adaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
SmallVector<Value> flattenedAdaptor = flattenValues(adaptor.getOperands());
FailureOr<Block *> convertedBlock =
getConvertedBlock(rewriter, getTypeConverter(), op, op.getSuccessor(),
TypeRange(adaptor.getOperands()));
TypeRange(flattenedAdaptor));
if (failed(convertedBlock))
return failure();
DictionaryAttr attrs = op->getAttrDictionary();
Operation *newOp = rewriter.replaceOpWithNewOp<LLVM::BrOp>(
op, adaptor.getOperands(), *convertedBlock);
op, flattenedAdaptor, *convertedBlock);
// TODO: We should not just forward all attributes like that. But there are
// existing Flang tests that depend on this behavior.
newOp->setAttrs(attrs);
Expand All @@ -152,29 +163,37 @@ struct BranchOpLowering : public ConvertOpToLLVMPattern<cf::BranchOp> {
/// branch op to llvm.cond_br.
struct CondBranchOpLowering : public ConvertOpToLLVMPattern<cf::CondBranchOp> {
using ConvertOpToLLVMPattern<cf::CondBranchOp>::ConvertOpToLLVMPattern;
using Adaptor =
typename ConvertOpToLLVMPattern<cf::CondBranchOp>::OneToNOpAdaptor;

LogicalResult
matchAndRewrite(cf::CondBranchOp op,
typename cf::CondBranchOp::Adaptor adaptor,
matchAndRewrite(cf::CondBranchOp op, Adaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
SmallVector<Value> flattenedAdaptorTrue =
flattenValues(adaptor.getTrueDestOperands());
SmallVector<Value> flattenedAdaptorFalse =
flattenValues(adaptor.getFalseDestOperands());
if (!llvm::hasSingleElement(adaptor.getCondition()))
return rewriter.notifyMatchFailure(op,
"expected single element condition");
FailureOr<Block *> convertedTrueBlock =
getConvertedBlock(rewriter, getTypeConverter(), op, op.getTrueDest(),
TypeRange(adaptor.getTrueDestOperands()));
TypeRange(flattenedAdaptorTrue));
if (failed(convertedTrueBlock))
return failure();
FailureOr<Block *> convertedFalseBlock =
getConvertedBlock(rewriter, getTypeConverter(), op, op.getFalseDest(),
TypeRange(adaptor.getFalseDestOperands()));
TypeRange(flattenedAdaptorFalse));
if (failed(convertedFalseBlock))
return failure();
DictionaryAttr attrs = op->getAttrDictionary();
DictionaryAttr attrs = op->getDiscardableAttrDictionary();
auto newOp = rewriter.replaceOpWithNewOp<LLVM::CondBrOp>(
op, adaptor.getCondition(), adaptor.getTrueDestOperands(),
adaptor.getFalseDestOperands(), op.getBranchWeightsAttr(),
op, llvm::getSingleElement(adaptor.getCondition()),
flattenedAdaptorTrue, flattenedAdaptorFalse, op.getBranchWeightsAttr(),
*convertedTrueBlock, *convertedFalseBlock);
// TODO: We should not just forward all attributes like that. But there are
// existing Flang tests that depend on this behavior.
newOp->setAttrs(attrs);
newOp->setDiscardableAttrs(attrs);
return success();
}
};
Expand Down
17 changes: 17 additions & 0 deletions mlir/test/Conversion/MemRefToLLVM/type-conversion.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,20 @@ func.func @caller(%arg0: i1, %arg1: i17) -> (i17, i1, i17) {
%res:2 = func.call @multi_return(%arg1, %arg0) : (i17, i1) -> (i17, i1)
return %res#0, %res#1, %res#0 : i17, i1, i17
}

// -----

// CHECK-LABEL: llvm.func @branch(
// CHECK-SAME: %[[arg0:.*]]: i1, %[[arg1:.*]]: i18, %[[arg2:.*]]: i18)
// CHECK: llvm.br ^[[bb1:.*]](%[[arg1]], %[[arg2]], %[[arg0]] : i18, i18, i1)
// CHECK: ^[[bb1]](%[[arg3:.*]]: i18, %[[arg4:.*]]: i18, %[[arg5:.*]]: i1):
// CHECK: llvm.cond_br %[[arg5]], ^[[bb1]](%[[arg1]], %[[arg2]], %[[arg5]] : i18, i18, i1), ^[[bb2:.*]](%[[arg3]], %[[arg4]] : i18, i18)
// CHECK: ^bb2(%{{.*}}: i18, %{{.*}}: i18):
// CHECK: llvm.return
func.func @branch(%arg0: i1, %arg1: i17) {
cf.br ^bb1(%arg1, %arg0: i17, i1)
^bb1(%arg2: i17, %arg3: i1):
cf.cond_br %arg3, ^bb1(%arg1, %arg3 : i17, i1), ^bb2(%arg2 : i17)
^bb2(%arg4: i17):
return
}
2 changes: 2 additions & 0 deletions mlir/test/lib/Dialect/LLVM/TestPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
Expand Down Expand Up @@ -70,6 +71,7 @@ struct TestLLVMLegalizePatternsPass
mlir::RewritePatternSet patterns(ctx);
patterns.add<TestDirectReplacementOp>(ctx, converter);
populateFuncToLLVMConversionPatterns(converter, patterns);
cf::populateControlFlowToLLVMConversionPatterns(converter, patterns);

// Define the conversion target used for the test.
ConversionTarget target(*ctx);
Expand Down