Skip to content

Commit a05c6aa

Browse files
committed
Fix type mismatches, possibly null operands
1 parent d889b08 commit a05c6aa

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,9 @@ class CmpIOpConversion : public OpConversionPattern<arith::CmpIOp> {
275275
ConversionPatternRewriter &rewriter) const override {
276276

277277
Type type = adaptor.getLhs().getType();
278-
if (type && !(isa<IntegerType>(type) || emitc::isPointerWideType(type))) {
278+
if (!type || !(isa<IntegerType>(type) || emitc::isPointerWideType(type))) {
279279
return rewriter.notifyMatchFailure(
280-
op, "expected integer or size_t/ssize_t type");
280+
op, "expected integer or size_t/ssize_t/ptrdiff_t type");
281281
}
282282

283283
bool needsUnsigned = needsUnsignedCmp(op.getPredicate());
@@ -302,21 +302,21 @@ class CastConversion : public OpConversionPattern<ArithOp> {
302302
ConversionPatternRewriter &rewriter) const override {
303303

304304
Type opReturnType = this->getTypeConverter()->convertType(op.getType());
305-
if (opReturnType && !(isa_and_nonnull<IntegerType>(opReturnType) ||
305+
if (!opReturnType || !(isa<IntegerType>(opReturnType) ||
306306
emitc::isPointerWideType(opReturnType)))
307307
return rewriter.notifyMatchFailure(
308-
op, "expected integer or size_t/ssize_t result type");
308+
op, "expected integer or size_t/ssize_t/ptrdiff_t result type");
309309

310310
if (adaptor.getOperands().size() != 1) {
311311
return rewriter.notifyMatchFailure(
312312
op, "CastConversion only supports unary ops");
313313
}
314314

315315
Type operandType = adaptor.getIn().getType();
316-
if (operandType && !(isa_and_nonnull<IntegerType>(operandType) ||
316+
if (!operandType || !(isa<IntegerType>(operandType) ||
317317
emitc::isPointerWideType(operandType)))
318318
return rewriter.notifyMatchFailure(
319-
op, "expected integer or size_t/ssize_t operand type");
319+
op, "expected integer or size_t/ssize_t/ptrdiff_t operand type");
320320

321321
// Signed (sign-extending) casts from i1 are not supported.
322322
if (operandType.isInteger(1) && !castToUnsigned)
@@ -405,7 +405,7 @@ class IntegerOpConversion final : public OpConversionPattern<ArithOp> {
405405
ConversionPatternRewriter &rewriter) const override {
406406

407407
Type type = this->getTypeConverter()->convertType(op.getType());
408-
if (type && !(isa_and_nonnull<IntegerType>(type) ||
408+
if (!type || !(isa_and_nonnull<IntegerType>(type) ||
409409
emitc::isPointerWideType(type))) {
410410
return rewriter.notifyMatchFailure(
411411
op, "expected integer or size_t/ssize_t/ptrdiff_t type");
@@ -489,10 +489,10 @@ class ShiftOpConversion : public OpConversionPattern<ArithOp> {
489489
ConversionPatternRewriter &rewriter) const override {
490490

491491
Type type = this->getTypeConverter()->convertType(op.getType());
492-
if (type && !(isa_and_nonnull<IntegerType>(type) ||
492+
if (!type || !(isa_and_nonnull<IntegerType>(type) ||
493493
emitc::isPointerWideType(type))) {
494494
return rewriter.notifyMatchFailure(
495-
op, "expected integer or size_t/ssize_t type");
495+
op, "expected integer or size_t/ssize_t/ptrdiff_t type");
496496
}
497497

498498
if (type.isInteger(1)) {

mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir

+11-11
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ func.func @arith_shift_left_index(%amount: i32) {
199199
%cst0 = "arith.constant"() {value = 42 : index} : () -> (index)
200200
%cast1 = arith.index_cast %amount : i32 to index
201201
// CHECK-DAG: %[[C1:[^ ]*]] = "emitc.constant"(){{.*}}value = 42{{.*}}!emitc.size_t
202-
// CHECK-DAG: %[[Cast1:[^ ]*]] = emitc.cast %[[AMOUNT]] : i32 to !emitc.ssize_t
203-
// CHECK-DAG: %[[AmountIdx:[^ ]*]] = emitc.cast %[[Cast1]] : !emitc.ssize_t to !emitc.size_t
202+
// CHECK-DAG: %[[Cast1:[^ ]*]] = emitc.cast %[[AMOUNT]] : i32 to !emitc.ptrdiff_t
203+
// CHECK-DAG: %[[AmountIdx:[^ ]*]] = emitc.cast %[[Cast1]] : !emitc.ptrdiff_t to !emitc.size_t
204204
// CHECK-DAG: %[[Byte:[^ ]*]] = "emitc.constant"{{.*}}value = 8{{.*}}index
205205
// CHECK-DAG: %[[SizeOf:[^ ]*]] = emitc.call_opaque "sizeof"(%[[Byte]]) : (!emitc.size_t) -> !emitc.size_t
206206
// CHECK-DAG: %[[SizeConstant:[^ ]*]] = emitc.mul %[[Byte]], %[[SizeOf]] : (!emitc.size_t, !emitc.size_t) -> !emitc.size_t
@@ -220,8 +220,8 @@ func.func @arith_shift_left_index(%amount: i32) {
220220
// CHECK-SAME: %[[AMOUNT:.*]]: i32
221221
func.func @arith_shift_right_index(%amount: i32) {
222222
// CHECK-DAG: %[[C1:[^ ]*]] = "emitc.constant"(){{.*}}value = 42{{.*}}!emitc.size_t
223-
// CHECK-DAG: %[[Cast1:[^ ]*]] = emitc.cast %[[AMOUNT]] : i32 to !emitc.ssize_t
224-
// CHECK-DAG: %[[AmountIdx:[^ ]*]] = emitc.cast %[[Cast1]] : !emitc.ssize_t to !emitc.size_t
223+
// CHECK-DAG: %[[Cast1:[^ ]*]] = emitc.cast %[[AMOUNT]] : i32 to !emitc.ptrdiff_t
224+
// CHECK-DAG: %[[AmountIdx:[^ ]*]] = emitc.cast %[[Cast1]] : !emitc.ptrdiff_t to !emitc.size_t
225225
%arg0 = "arith.constant"() {value = 42 : index} : () -> (index)
226226
%arg1 = arith.index_cast %amount : i32 to index
227227

@@ -236,17 +236,17 @@ func.func @arith_shift_right_index(%amount: i32) {
236236
// CHECK: emitc.yield %[[Ternary]] : !emitc.size_t
237237
%2 = arith.shrui %arg0, %arg1 : index
238238

239-
// CHECK-DAG: %[[SC1:[^ ]*]] = emitc.cast %[[C1]] : !emitc.size_t to !emitc.ssize_t
239+
// CHECK-DAG: %[[SC1:[^ ]*]] = emitc.cast %[[C1]] : !emitc.size_t to !emitc.ptrdiff_t
240240
// CHECK-DAG: %[[SByte:[^ ]*]] = "emitc.constant"{{.*}}value = 8{{.*}}index{{.*}}!emitc.size_t
241241
// CHECK-DAG: %[[SSizeOf:[^ ]*]] = emitc.call_opaque "sizeof"(%[[SByte]]) : (!emitc.size_t) -> !emitc.size_t
242242
// CHECK-DAG: %[[SSizeConstant:[^ ]*]] = emitc.mul %[[SByte]], %[[SSizeOf]] : (!emitc.size_t, !emitc.size_t) -> !emitc.size_t
243243
// CHECK-DAG: %[[SCmpNoExcess:[^ ]*]] = emitc.cmp lt, %[[AmountIdx]], %[[SSizeConstant]] : (!emitc.size_t, !emitc.size_t) -> i1
244-
// CHECK-DAG: %[[SZero:[^ ]*]] = "emitc.constant"{{.*}}value = 0{{.*}}!emitc.ssize_t
245-
// CHECK: %[[SShiftRes:[^ ]*]] = emitc.expression : !emitc.ssize_t
246-
// CHECK: %[[SHRSI:[^ ]*]] = emitc.bitwise_right_shift %[[SC1]], %[[AmountIdx]] : (!emitc.ssize_t, !emitc.size_t) -> !emitc.ssize_t
247-
// CHECK: %[[STernary:[^ ]*]] = emitc.conditional %[[SCmpNoExcess]], %[[SHRSI]], %[[SZero]] : !emitc.ssize_t
248-
// CHECK: emitc.yield %[[STernary]] : !emitc.ssize_t
249-
// CHECK: emitc.cast %[[SShiftRes]] : !emitc.ssize_t to !emitc.size_t
244+
// CHECK-DAG: %[[SZero:[^ ]*]] = "emitc.constant"{{.*}}value = 0{{.*}}!emitc.ptrdiff_t
245+
// CHECK: %[[SShiftRes:[^ ]*]] = emitc.expression : !emitc.ptrdiff_t
246+
// CHECK: %[[SHRSI:[^ ]*]] = emitc.bitwise_right_shift %[[SC1]], %[[AmountIdx]] : (!emitc.ptrdiff_t, !emitc.size_t) -> !emitc.ptrdiff_t
247+
// CHECK: %[[STernary:[^ ]*]] = emitc.conditional %[[SCmpNoExcess]], %[[SHRSI]], %[[SZero]] : !emitc.ptrdiff_t
248+
// CHECK: emitc.yield %[[STernary]] : !emitc.ptrdiff_t
249+
// CHECK: emitc.cast %[[SShiftRes]] : !emitc.ptrdiff_t to !emitc.size_t
250250
%3 = arith.shrsi %arg0, %arg1 : index
251251

252252
return

0 commit comments

Comments
 (0)