|
22 | 22 | #include "mlir/IR/BuiltinOps.h"
|
23 | 23 | #include "mlir/IR/PatternMatch.h"
|
24 | 24 | #include "mlir/Transforms/DialectConversion.h"
|
| 25 | +#include "llvm/ADT/StringRef.h" |
25 | 26 | #include <functional>
|
26 | 27 |
|
27 | 28 | using namespace mlir;
|
@@ -71,34 +72,108 @@ struct AssertOpLowering : public ConvertOpToLLVMPattern<cf::AssertOp> {
|
71 | 72 | }
|
72 | 73 | };
|
73 | 74 |
|
74 |
| -// Base class for LLVM IR lowering terminator operations with successors. |
75 |
| -template <typename SourceOp, typename TargetOp> |
76 |
| -struct OneToOneLLVMTerminatorLowering |
77 |
| - : public ConvertOpToLLVMPattern<SourceOp> { |
78 |
| - using ConvertOpToLLVMPattern<SourceOp>::ConvertOpToLLVMPattern; |
79 |
| - using Base = OneToOneLLVMTerminatorLowering<SourceOp, TargetOp>; |
| 75 | +/// The cf->LLVM lowerings for branching ops require that the blocks they jump |
| 76 | +/// to first have updated types which should be handled by a pattern operating |
| 77 | +/// on the parent op. |
| 78 | +static LogicalResult verifyMatchingValues(ConversionPatternRewriter &rewriter, |
| 79 | + ValueRange operands, |
| 80 | + ValueRange blockArgs, Location loc, |
| 81 | + llvm::StringRef messagePrefix) { |
| 82 | + for (const auto &idxAndTypes : |
| 83 | + llvm::enumerate(llvm::zip(blockArgs, operands))) { |
| 84 | + int64_t i = idxAndTypes.index(); |
| 85 | + Value argValue = |
| 86 | + rewriter.getRemappedValue(std::get<0>(idxAndTypes.value())); |
| 87 | + Type operandType = std::get<1>(idxAndTypes.value()).getType(); |
| 88 | + // In the case of an invalid jump, the block argument will have been |
| 89 | + // remapped to an UnrealizedConversionCast. In the case of a valid jump, |
| 90 | + // there might still be a no-op conversion cast with both types being equal. |
| 91 | + // Consider both of these details to see if the jump would be invalid. |
| 92 | + if (auto op = dyn_cast_or_null<UnrealizedConversionCastOp>( |
| 93 | + argValue.getDefiningOp())) { |
| 94 | + if (op.getOperandTypes().front() != operandType) { |
| 95 | + return rewriter.notifyMatchFailure(loc, [&](Diagnostic &diag) { |
| 96 | + diag << messagePrefix; |
| 97 | + diag << "mismatched types from operand # " << i << " "; |
| 98 | + diag << operandType; |
| 99 | + diag << " not compatible with destination block argument type "; |
| 100 | + diag << argValue.getType(); |
| 101 | + diag << " which should be converted with the parent op."; |
| 102 | + }); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + return success(); |
| 107 | +} |
| 108 | + |
| 109 | +/// Ensure that all block types were updated and then create an LLVM::BrOp |
| 110 | +struct BranchOpLowering : public ConvertOpToLLVMPattern<cf::BranchOp> { |
| 111 | + using ConvertOpToLLVMPattern<cf::BranchOp>::ConvertOpToLLVMPattern; |
80 | 112 |
|
81 | 113 | LogicalResult
|
82 |
| - matchAndRewrite(SourceOp op, typename SourceOp::Adaptor adaptor, |
| 114 | + matchAndRewrite(cf::BranchOp op, typename cf::BranchOp::Adaptor adaptor, |
83 | 115 | ConversionPatternRewriter &rewriter) const override {
|
84 |
| - rewriter.replaceOpWithNewOp<TargetOp>(op, adaptor.getOperands(), |
85 |
| - op->getSuccessors(), op->getAttrs()); |
| 116 | + if (failed(verifyMatchingValues(rewriter, adaptor.getDestOperands(), |
| 117 | + op.getSuccessor()->getArguments(), |
| 118 | + op.getLoc(), |
| 119 | + /*messagePrefix=*/""))) |
| 120 | + return failure(); |
| 121 | + |
| 122 | + rewriter.replaceOpWithNewOp<LLVM::BrOp>( |
| 123 | + op, adaptor.getOperands(), op->getSuccessors(), op->getAttrs()); |
86 | 124 | return success();
|
87 | 125 | }
|
88 | 126 | };
|
89 | 127 |
|
90 |
| -// FIXME: this should be tablegen'ed as well. |
91 |
| -struct BranchOpLowering |
92 |
| - : public OneToOneLLVMTerminatorLowering<cf::BranchOp, LLVM::BrOp> { |
93 |
| - using Base::Base; |
94 |
| -}; |
95 |
| -struct CondBranchOpLowering |
96 |
| - : public OneToOneLLVMTerminatorLowering<cf::CondBranchOp, LLVM::CondBrOp> { |
97 |
| - using Base::Base; |
| 128 | +/// Ensure that all block types were updated and then create an LLVM::CondBrOp |
| 129 | +struct CondBranchOpLowering : public ConvertOpToLLVMPattern<cf::CondBranchOp> { |
| 130 | + using ConvertOpToLLVMPattern<cf::CondBranchOp>::ConvertOpToLLVMPattern; |
| 131 | + |
| 132 | + LogicalResult |
| 133 | + matchAndRewrite(cf::CondBranchOp op, |
| 134 | + typename cf::CondBranchOp::Adaptor adaptor, |
| 135 | + ConversionPatternRewriter &rewriter) const override { |
| 136 | + if (failed(verifyMatchingValues(rewriter, adaptor.getFalseDestOperands(), |
| 137 | + op.getFalseDest()->getArguments(), |
| 138 | + op.getLoc(), "in false case branch "))) |
| 139 | + return failure(); |
| 140 | + if (failed(verifyMatchingValues(rewriter, adaptor.getTrueDestOperands(), |
| 141 | + op.getTrueDest()->getArguments(), |
| 142 | + op.getLoc(), "in true case branch "))) |
| 143 | + return failure(); |
| 144 | + |
| 145 | + rewriter.replaceOpWithNewOp<LLVM::CondBrOp>( |
| 146 | + op, adaptor.getOperands(), op->getSuccessors(), op->getAttrs()); |
| 147 | + return success(); |
| 148 | + } |
98 | 149 | };
|
99 |
| -struct SwitchOpLowering |
100 |
| - : public OneToOneLLVMTerminatorLowering<cf::SwitchOp, LLVM::SwitchOp> { |
101 |
| - using Base::Base; |
| 150 | + |
| 151 | +/// Ensure that all block types were updated and then create an LLVM::SwitchOp |
| 152 | +struct SwitchOpLowering : public ConvertOpToLLVMPattern<cf::SwitchOp> { |
| 153 | + using ConvertOpToLLVMPattern<cf::SwitchOp>::ConvertOpToLLVMPattern; |
| 154 | + |
| 155 | + LogicalResult |
| 156 | + matchAndRewrite(cf::SwitchOp op, typename cf::SwitchOp::Adaptor adaptor, |
| 157 | + ConversionPatternRewriter &rewriter) const override { |
| 158 | + if (failed(verifyMatchingValues(rewriter, adaptor.getDefaultOperands(), |
| 159 | + op.getDefaultDestination()->getArguments(), |
| 160 | + op.getLoc(), "in switch default case "))) |
| 161 | + return failure(); |
| 162 | + |
| 163 | + for (const auto &i : llvm::enumerate( |
| 164 | + llvm::zip(adaptor.getCaseOperands(), op.getCaseDestinations()))) { |
| 165 | + if (failed(verifyMatchingValues( |
| 166 | + rewriter, std::get<0>(i.value()), |
| 167 | + std::get<1>(i.value())->getArguments(), op.getLoc(), |
| 168 | + "in switch case " + std::to_string(i.index()) + " "))) { |
| 169 | + return failure(); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + rewriter.replaceOpWithNewOp<LLVM::SwitchOp>( |
| 174 | + op, adaptor.getOperands(), op->getSuccessors(), op->getAttrs()); |
| 175 | + return success(); |
| 176 | + } |
102 | 177 | };
|
103 | 178 |
|
104 | 179 | } // namespace
|
|
0 commit comments