@@ -162,8 +162,22 @@ class FtoICastOpConversion : public OpConversionPattern<CastOp> {
162
162
return rewriter.notifyMatchFailure (castOp,
163
163
" unsupported cast destination type" );
164
164
165
- rewriter.replaceOpWithNewOp <emitc::CastOp>(castOp, dstType,
166
- adaptor.getOperands ());
165
+ // Convert to unsigned if it's the "ui" variant
166
+ // Signless is interpreted as signed, so no need to cast for "si"
167
+ Type actualResultType = dstType;
168
+ if (isa<arith::FPToUIOp>(castOp)) {
169
+ actualResultType =
170
+ rewriter.getIntegerType (operandType.getIntOrFloatBitWidth (),
171
+ /* isSigned=*/ false );
172
+ }
173
+
174
+ Value result = rewriter.create <emitc::CastOp>(
175
+ castOp.getLoc (), actualResultType, adaptor.getOperands ());
176
+
177
+ if (isa<arith::FPToUIOp>(castOp)) {
178
+ result = rewriter.create <emitc::CastOp>(castOp.getLoc (), dstType, result);
179
+ }
180
+ rewriter.replaceOp (castOp, result);
167
181
168
182
return success ();
169
183
}
@@ -179,7 +193,7 @@ class ItoFCastOpConversion : public OpConversionPattern<CastOp> {
179
193
LogicalResult
180
194
matchAndRewrite (CastOp castOp, typename CastOp::Adaptor adaptor,
181
195
ConversionPatternRewriter &rewriter) const override {
182
-
196
+ // Vectors in particular are not supported
183
197
Type operandType = adaptor.getIn ().getType ();
184
198
if (!emitc::isSupportedIntegerType (operandType))
185
199
return rewriter.notifyMatchFailure (castOp,
@@ -193,8 +207,20 @@ class ItoFCastOpConversion : public OpConversionPattern<CastOp> {
193
207
return rewriter.notifyMatchFailure (castOp,
194
208
" unsupported cast destination type" );
195
209
196
- rewriter.replaceOpWithNewOp <emitc::CastOp>(castOp, dstType,
197
- adaptor.getOperands ());
210
+ // Convert to unsigned if it's the "ui" variant
211
+ // Signless is interpreted as signed, so no need to cast for "si"
212
+ Type actualOperandType = operandType;
213
+ if (isa<arith::UIToFPOp>(castOp)) {
214
+ actualOperandType =
215
+ rewriter.getIntegerType (operandType.getIntOrFloatBitWidth (),
216
+ /* isSigned=*/ false );
217
+ }
218
+ Value fpCastOperand = adaptor.getIn ();
219
+ if (actualOperandType != operandType) {
220
+ fpCastOperand = rewriter.template create <emitc::CastOp>(
221
+ castOp.getLoc (), actualOperandType, fpCastOperand);
222
+ }
223
+ rewriter.replaceOpWithNewOp <emitc::CastOp>(castOp, dstType, fpCastOperand);
198
224
199
225
return success ();
200
226
}
0 commit comments