|
15 | 15 |
|
16 | 16 | #include "mlir/Dialect/Arith/IR/Arith.h"
|
17 | 17 | #include "mlir/Dialect/EmitC/IR/EmitC.h"
|
| 18 | +#include "mlir/Tools/PDLL/AST/Types.h" |
18 | 19 | #include "mlir/Transforms/DialectConversion.h"
|
19 | 20 |
|
20 | 21 | using namespace mlir;
|
@@ -112,6 +113,93 @@ class CmpIOpConversion : public OpConversionPattern<arith::CmpIOp> {
|
112 | 113 | }
|
113 | 114 | };
|
114 | 115 |
|
| 116 | +template <typename ArithOp, bool castToUnsigned> |
| 117 | +class CastConversion : public OpConversionPattern<ArithOp> { |
| 118 | +public: |
| 119 | + using OpConversionPattern<ArithOp>::OpConversionPattern; |
| 120 | + |
| 121 | + LogicalResult |
| 122 | + matchAndRewrite(ArithOp op, typename ArithOp::Adaptor adaptor, |
| 123 | + ConversionPatternRewriter &rewriter) const override { |
| 124 | + |
| 125 | + Type opReturnType = this->getTypeConverter()->convertType(op.getType()); |
| 126 | + if (!isa_and_nonnull<IntegerType>(opReturnType)) |
| 127 | + return rewriter.notifyMatchFailure(op, "expected integer result type"); |
| 128 | + |
| 129 | + if (adaptor.getOperands().size() != 1) { |
| 130 | + return rewriter.notifyMatchFailure( |
| 131 | + op, "CastConversion only supports unary ops"); |
| 132 | + } |
| 133 | + |
| 134 | + Type operandType = adaptor.getIn().getType(); |
| 135 | + if (!isa_and_nonnull<IntegerType>(operandType)) |
| 136 | + return rewriter.notifyMatchFailure(op, "expected integer operand type"); |
| 137 | + |
| 138 | + // Signed (sign-extending) casts from i1 are not supported. |
| 139 | + if (operandType.isInteger(1) && !castToUnsigned) |
| 140 | + return rewriter.notifyMatchFailure(op, |
| 141 | + "operation not supported on i1 type"); |
| 142 | + |
| 143 | + // to-i1 conversions: arith semantics want truncation, whereas (bool)(v) is |
| 144 | + // equivalent to (v != 0). Implementing as (bool)(v & 0x01) gives |
| 145 | + // truncation. |
| 146 | + if (opReturnType.isInteger(1)) { |
| 147 | + auto constOne = rewriter.create<emitc::ConstantOp>( |
| 148 | + op.getLoc(), operandType, rewriter.getIntegerAttr(operandType, 1)); |
| 149 | + auto oneAndOperand = rewriter.create<emitc::BitwiseAndOp>( |
| 150 | + op.getLoc(), operandType, adaptor.getIn(), constOne); |
| 151 | + rewriter.replaceOpWithNewOp<emitc::CastOp>(op, opReturnType, |
| 152 | + oneAndOperand); |
| 153 | + return success(); |
| 154 | + } |
| 155 | + |
| 156 | + bool isTruncation = operandType.getIntOrFloatBitWidth() > |
| 157 | + opReturnType.getIntOrFloatBitWidth(); |
| 158 | + bool doUnsigned = castToUnsigned || isTruncation; |
| 159 | + |
| 160 | + Type castType = opReturnType; |
| 161 | + // If the op is a ui variant and the type wanted as |
| 162 | + // return type isn't unsigned, we need to issue an unsigned type to do |
| 163 | + // the conversion. |
| 164 | + if (castType.isUnsignedInteger() != doUnsigned) { |
| 165 | + castType = rewriter.getIntegerType(opReturnType.getIntOrFloatBitWidth(), |
| 166 | + /*isSigned=*/!doUnsigned); |
| 167 | + } |
| 168 | + |
| 169 | + Value actualOp = adaptor.getIn(); |
| 170 | + // Adapt the signedness of the operand if necessary |
| 171 | + if (operandType.isUnsignedInteger() != doUnsigned) { |
| 172 | + Type correctSignednessType = |
| 173 | + rewriter.getIntegerType(operandType.getIntOrFloatBitWidth(), |
| 174 | + /*isSigned=*/!doUnsigned); |
| 175 | + actualOp = rewriter.template create<emitc::CastOp>( |
| 176 | + op.getLoc(), correctSignednessType, actualOp); |
| 177 | + } |
| 178 | + |
| 179 | + auto result = rewriter.template create<emitc::CastOp>(op.getLoc(), castType, |
| 180 | + actualOp); |
| 181 | + |
| 182 | + // Cast to the expected output type |
| 183 | + if (castType != opReturnType) { |
| 184 | + result = rewriter.template create<emitc::CastOp>(op.getLoc(), |
| 185 | + opReturnType, result); |
| 186 | + } |
| 187 | + |
| 188 | + rewriter.replaceOp(op, result); |
| 189 | + return success(); |
| 190 | + } |
| 191 | +}; |
| 192 | + |
| 193 | +template <typename ArithOp> |
| 194 | +class UnsignedCastConversion : public CastConversion<ArithOp, true> { |
| 195 | + using CastConversion<ArithOp, true>::CastConversion; |
| 196 | +}; |
| 197 | + |
| 198 | +template <typename ArithOp> |
| 199 | +class SignedCastConversion : public CastConversion<ArithOp, false> { |
| 200 | + using CastConversion<ArithOp, false>::CastConversion; |
| 201 | +}; |
| 202 | + |
115 | 203 | template <typename ArithOp, typename EmitCOp>
|
116 | 204 | class ArithOpConversion final : public OpConversionPattern<ArithOp> {
|
117 | 205 | public:
|
@@ -313,6 +401,10 @@ void mlir::populateArithToEmitCPatterns(TypeConverter &typeConverter,
|
313 | 401 | IntegerOpConversion<arith::SubIOp, emitc::SubOp>,
|
314 | 402 | CmpIOpConversion,
|
315 | 403 | SelectOpConversion,
|
| 404 | + // Truncation is guaranteed for unsigned types. |
| 405 | + UnsignedCastConversion<arith::TruncIOp>, |
| 406 | + SignedCastConversion<arith::ExtSIOp>, |
| 407 | + UnsignedCastConversion<arith::ExtUIOp>, |
316 | 408 | ItoFCastOpConversion<arith::SIToFPOp>,
|
317 | 409 | ItoFCastOpConversion<arith::UIToFPOp>,
|
318 | 410 | FtoICastOpConversion<arith::FPToSIOp>,
|
|
0 commit comments