From e4e463e7476920d1f52667447bda2bc1635ae390 Mon Sep 17 00:00:00 2001 From: Mehdi Amini Date: Mon, 3 Jan 2022 06:02:21 +0000 Subject: [PATCH] Remove useless nesting blok and dead return statement in TosaToLinalg.cpp (NFC) Flagged by Coverity. --- .../Conversion/TosaToLinalg/TosaToLinalg.cpp | 462 +++++++++--------- 1 file changed, 227 insertions(+), 235 deletions(-) diff --git a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp index f28527d185c1..ed6b3847db96 100644 --- a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp +++ b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp @@ -1431,248 +1431,240 @@ public: getNParallelLoopsAttrs(resultTy.getRank())); rewriter.replaceOp(op, genericOp.getResult(0)); - { - OpBuilder::InsertionGuard regionGuard(rewriter); - rewriter.createBlock(&genericOp.region(), genericOp.region().end(), - TypeRange({resultElementTy})); - Value batch = rewriter.create(loc, 0); - Value y = rewriter.create(loc, 1); - Value x = rewriter.create(loc, 2); - Value channel = rewriter.create(loc, 3); + OpBuilder::InsertionGuard regionGuard(rewriter); + rewriter.createBlock(&genericOp.region(), genericOp.region().end(), + TypeRange({resultElementTy})); + Value batch = rewriter.create(loc, 0); + Value y = rewriter.create(loc, 1); + Value x = rewriter.create(loc, 2); + Value channel = rewriter.create(loc, 3); - auto hwMin = rewriter.create( - loc, rewriter.getI32IntegerAttr(0)); - auto hMax = rewriter.create( - loc, rewriter.getI32IntegerAttr(imageH - 1)); - auto wMax = rewriter.create( - loc, rewriter.getI32IntegerAttr(imageW - 1)); + auto hwMin = + rewriter.create(loc, rewriter.getI32IntegerAttr(0)); + auto hMax = rewriter.create( + loc, rewriter.getI32IntegerAttr(imageH - 1)); + auto wMax = rewriter.create( + loc, rewriter.getI32IntegerAttr(imageW - 1)); - Value inY = - rewriter.create(loc, rewriter.getI32Type(), y); - Value inX = - rewriter.create(loc, rewriter.getI32Type(), x); + Value inY = + rewriter.create(loc, rewriter.getI32Type(), y); + Value inX = + rewriter.create(loc, rewriter.getI32Type(), x); - int32_t shift = op.shift(); - bool floatingPointMode = shift == 0; + int32_t shift = op.shift(); + bool floatingPointMode = shift == 0; - Value yStride, xStride, yOffset, xOffset; - if (floatingPointMode) { - yStride = rewriter.create(loc, op.stride_fp()[0]); - xStride = rewriter.create(loc, op.stride_fp()[1]); - yOffset = rewriter.create(loc, op.offset_fp()[0]); - xOffset = rewriter.create(loc, op.offset_fp()[1]); - } else { - SmallVector stride, offset; - getValuesFromIntArrayAttribute(op.stride(), stride); - getValuesFromIntArrayAttribute(op.offset(), offset); + Value yStride, xStride, yOffset, xOffset; + if (floatingPointMode) { + yStride = rewriter.create(loc, op.stride_fp()[0]); + xStride = rewriter.create(loc, op.stride_fp()[1]); + yOffset = rewriter.create(loc, op.offset_fp()[0]); + xOffset = rewriter.create(loc, op.offset_fp()[1]); + } else { + SmallVector stride, offset; + getValuesFromIntArrayAttribute(op.stride(), stride); + getValuesFromIntArrayAttribute(op.offset(), offset); - yStride = rewriter.create( - loc, rewriter.getI32IntegerAttr(stride[0])); - xStride = rewriter.create( - loc, rewriter.getI32IntegerAttr(stride[1])); - yOffset = rewriter.create( - loc, rewriter.getI32IntegerAttr(offset[0])); - xOffset = rewriter.create( - loc, rewriter.getI32IntegerAttr(offset[1])); - } - - // Compute the the integer index and partial offset. - // x = x * stride + offset; - // ix = floor(x) - // dx = x - ix - Value ix, iy, dx, dy; - if (floatingPointMode) { - Value y = - rewriter.create(loc, rewriter.getF32Type(), inY); - Value x = - rewriter.create(loc, rewriter.getF32Type(), inX); - - y = rewriter.create(loc, y, yStride); - x = rewriter.create(loc, x, xStride); - - y = rewriter.create(loc, y, yOffset); - x = rewriter.create(loc, x, xOffset); - - iy = rewriter.create(loc, y); - ix = rewriter.create(loc, x); - - dy = rewriter.create(loc, y, iy); - dx = rewriter.create(loc, x, ix); - - iy = rewriter.create(loc, rewriter.getI32Type(), iy); - ix = rewriter.create(loc, rewriter.getI32Type(), ix); - } else { - Value shiftVal = rewriter.create( - loc, rewriter.getI32IntegerAttr(shift)); - - Value y = rewriter.create(loc, inY, yStride); - Value x = rewriter.create(loc, inX, xStride); - - y = rewriter.create(loc, y, yOffset); - x = rewriter.create(loc, x, xOffset); - - iy = rewriter.create(loc, y, shiftVal); - ix = rewriter.create(loc, x, shiftVal); - - Value yTrunc = rewriter.create(loc, iy, shiftVal); - Value xTrunc = rewriter.create(loc, ix, shiftVal); - - dy = rewriter.create(loc, y, yTrunc); - dx = rewriter.create(loc, x, xTrunc); - } - - if (op.mode() == "NEAREST_NEIGHBOR") { - Value yPred, xPred; - // Round the index position towards the closest pixel location. - if (floatingPointMode) { - auto halfVal = rewriter.create( - loc, rewriter.getF32FloatAttr(0.5f)); - yPred = rewriter.create(loc, arith::CmpFPredicate::OGE, - dy, halfVal); - xPred = rewriter.create(loc, arith::CmpFPredicate::OGE, - dx, halfVal); - } else { - auto halfVal = rewriter.create( - loc, rewriter.getI32IntegerAttr(1 << (shift - 1))); - yPred = rewriter.create(loc, arith::CmpIPredicate::sge, - dy, halfVal); - xPred = rewriter.create(loc, arith::CmpIPredicate::sge, - dx, halfVal); - } - - auto zeroVal = rewriter.create( - loc, rewriter.getI32IntegerAttr(0)); - auto oneVal = rewriter.create( - loc, rewriter.getI32IntegerAttr(1)); - - auto yOffset = - rewriter.create(loc, yPred, oneVal, zeroVal); - auto xOffset = - rewriter.create(loc, xPred, oneVal, zeroVal); - - iy = rewriter.create(loc, iy, yOffset); - ix = rewriter.create(loc, ix, xOffset); - - // Clamp the to be within the bounds of the input image. - - iy = clampHelper(loc, iy, hwMin, hMax, - arith::CmpIPredicate::slt, rewriter); - ix = clampHelper(loc, ix, hwMin, wMax, - arith::CmpIPredicate::slt, rewriter); - - // Read the value from the input array. - iy = rewriter.create(loc, rewriter.getIndexType(), - iy); - ix = rewriter.create(loc, rewriter.getIndexType(), - ix); - - Value result = rewriter.create( - loc, input, ValueRange{batch, iy, ix, channel}); - - rewriter.create(loc, result); - - return success(); - } - - if (op.mode() == "BILINEAR") { - Value y0 = iy; - Value x0 = ix; - - auto oneVal = rewriter.create( - loc, rewriter.getI32IntegerAttr(1)); - Value y1 = rewriter.create(loc, y0, oneVal); - Value x1 = rewriter.create(loc, x0, oneVal); - - y0 = clampHelper(loc, y0, hwMin, hMax, - arith::CmpIPredicate::slt, rewriter); - y1 = clampHelper(loc, y1, hwMin, hMax, - arith::CmpIPredicate::slt, rewriter); - - x0 = clampHelper(loc, x0, hwMin, wMax, - arith::CmpIPredicate::slt, rewriter); - x1 = clampHelper(loc, x1, hwMin, wMax, - arith::CmpIPredicate::slt, rewriter); - - y0 = rewriter.create(loc, rewriter.getIndexType(), - y0); - y1 = rewriter.create(loc, rewriter.getIndexType(), - y1); - x0 = rewriter.create(loc, rewriter.getIndexType(), - x0); - x1 = rewriter.create(loc, rewriter.getIndexType(), - x1); - - Value y0x0 = rewriter.create( - loc, input, ValueRange{batch, y0, x0, channel}); - Value y0x1 = rewriter.create( - loc, input, ValueRange{batch, y0, x1, channel}); - Value y1x0 = rewriter.create( - loc, input, ValueRange{batch, y1, x0, channel}); - Value y1x1 = rewriter.create( - loc, input, ValueRange{batch, y1, x1, channel}); - - if (floatingPointMode) { - auto oneVal = rewriter.create( - loc, rewriter.getF32FloatAttr(1.f)); - Value rightPart = dx; - Value leftPart = rewriter.create(loc, oneVal, dx); - - y0x0 = rewriter.create(loc, y0x0, leftPart); - y0x1 = rewriter.create(loc, y0x1, rightPart); - Value topAcc = rewriter.create(loc, y0x0, y0x1); - - y1x0 = rewriter.create(loc, y1x0, leftPart); - y1x1 = rewriter.create(loc, y1x1, rightPart); - Value bottomAcc = rewriter.create(loc, y1x0, y1x1); - - Value bottomPart = dy; - Value topPart = rewriter.create(loc, oneVal, dy); - topAcc = rewriter.create(loc, topAcc, topPart); - bottomAcc = - rewriter.create(loc, bottomAcc, bottomPart); - Value result = rewriter.create(loc, topAcc, bottomAcc); - - rewriter.create(loc, result); - return success(); - } - y0x0 = rewriter.create(loc, resultElementTy, y0x0); - y0x1 = rewriter.create(loc, resultElementTy, y0x1); - y1x0 = rewriter.create(loc, resultElementTy, y1x0); - y1x1 = rewriter.create(loc, resultElementTy, y1x1); - - if (resultElementTy.getIntOrFloatBitWidth() > 32) { - dx = rewriter.create(loc, resultElementTy, dx); - dy = rewriter.create(loc, resultElementTy, dy); - } - - auto unitVal = rewriter.create( - loc, rewriter.getIntegerAttr(resultElementTy, 1 << shift)); - Value rightPart = dx; - Value leftPart = rewriter.create(loc, unitVal, dx); - - y0x0 = rewriter.create(loc, y0x0, leftPart); - y0x1 = rewriter.create(loc, y0x1, rightPart); - Value topAcc = rewriter.create(loc, y0x0, y0x1); - - y1x0 = rewriter.create(loc, y1x0, leftPart); - y1x1 = rewriter.create(loc, y1x1, rightPart); - Value bottomAcc = rewriter.create(loc, y1x0, y1x1); - - Value bottomPart = dy; - Value topPart = rewriter.create(loc, unitVal, dy); - topAcc = rewriter.create(loc, topAcc, topPart); - bottomAcc = - rewriter.create(loc, bottomAcc, bottomPart); - Value result = rewriter.create(loc, topAcc, bottomAcc); - - rewriter.create(loc, result); - return success(); - } - - return failure(); + yStride = rewriter.create( + loc, rewriter.getI32IntegerAttr(stride[0])); + xStride = rewriter.create( + loc, rewriter.getI32IntegerAttr(stride[1])); + yOffset = rewriter.create( + loc, rewriter.getI32IntegerAttr(offset[0])); + xOffset = rewriter.create( + loc, rewriter.getI32IntegerAttr(offset[1])); } - return success(); + // Compute the the integer index and partial offset. + // x = x * stride + offset; + // ix = floor(x) + // dx = x - ix + Value ix, iy, dx, dy; + if (floatingPointMode) { + Value y = + rewriter.create(loc, rewriter.getF32Type(), inY); + Value x = + rewriter.create(loc, rewriter.getF32Type(), inX); + + y = rewriter.create(loc, y, yStride); + x = rewriter.create(loc, x, xStride); + + y = rewriter.create(loc, y, yOffset); + x = rewriter.create(loc, x, xOffset); + + iy = rewriter.create(loc, y); + ix = rewriter.create(loc, x); + + dy = rewriter.create(loc, y, iy); + dx = rewriter.create(loc, x, ix); + + iy = rewriter.create(loc, rewriter.getI32Type(), iy); + ix = rewriter.create(loc, rewriter.getI32Type(), ix); + } else { + Value shiftVal = rewriter.create( + loc, rewriter.getI32IntegerAttr(shift)); + + Value y = rewriter.create(loc, inY, yStride); + Value x = rewriter.create(loc, inX, xStride); + + y = rewriter.create(loc, y, yOffset); + x = rewriter.create(loc, x, xOffset); + + iy = rewriter.create(loc, y, shiftVal); + ix = rewriter.create(loc, x, shiftVal); + + Value yTrunc = rewriter.create(loc, iy, shiftVal); + Value xTrunc = rewriter.create(loc, ix, shiftVal); + + dy = rewriter.create(loc, y, yTrunc); + dx = rewriter.create(loc, x, xTrunc); + } + + if (op.mode() == "NEAREST_NEIGHBOR") { + Value yPred, xPred; + // Round the index position towards the closest pixel location. + if (floatingPointMode) { + auto halfVal = rewriter.create( + loc, rewriter.getF32FloatAttr(0.5f)); + yPred = rewriter.create(loc, arith::CmpFPredicate::OGE, + dy, halfVal); + xPred = rewriter.create(loc, arith::CmpFPredicate::OGE, + dx, halfVal); + } else { + auto halfVal = rewriter.create( + loc, rewriter.getI32IntegerAttr(1 << (shift - 1))); + yPred = rewriter.create(loc, arith::CmpIPredicate::sge, + dy, halfVal); + xPred = rewriter.create(loc, arith::CmpIPredicate::sge, + dx, halfVal); + } + + auto zeroVal = rewriter.create( + loc, rewriter.getI32IntegerAttr(0)); + auto oneVal = rewriter.create( + loc, rewriter.getI32IntegerAttr(1)); + + auto yOffset = + rewriter.create(loc, yPred, oneVal, zeroVal); + auto xOffset = + rewriter.create(loc, xPred, oneVal, zeroVal); + + iy = rewriter.create(loc, iy, yOffset); + ix = rewriter.create(loc, ix, xOffset); + + // Clamp the to be within the bounds of the input image. + + iy = clampHelper(loc, iy, hwMin, hMax, + arith::CmpIPredicate::slt, rewriter); + ix = clampHelper(loc, ix, hwMin, wMax, + arith::CmpIPredicate::slt, rewriter); + + // Read the value from the input array. + iy = + rewriter.create(loc, rewriter.getIndexType(), iy); + ix = + rewriter.create(loc, rewriter.getIndexType(), ix); + + Value result = rewriter.create( + loc, input, ValueRange{batch, iy, ix, channel}); + + rewriter.create(loc, result); + + return success(); + } + + if (op.mode() == "BILINEAR") { + Value y0 = iy; + Value x0 = ix; + + auto oneVal = rewriter.create( + loc, rewriter.getI32IntegerAttr(1)); + Value y1 = rewriter.create(loc, y0, oneVal); + Value x1 = rewriter.create(loc, x0, oneVal); + + y0 = clampHelper(loc, y0, hwMin, hMax, + arith::CmpIPredicate::slt, rewriter); + y1 = clampHelper(loc, y1, hwMin, hMax, + arith::CmpIPredicate::slt, rewriter); + + x0 = clampHelper(loc, x0, hwMin, wMax, + arith::CmpIPredicate::slt, rewriter); + x1 = clampHelper(loc, x1, hwMin, wMax, + arith::CmpIPredicate::slt, rewriter); + + y0 = + rewriter.create(loc, rewriter.getIndexType(), y0); + y1 = + rewriter.create(loc, rewriter.getIndexType(), y1); + x0 = + rewriter.create(loc, rewriter.getIndexType(), x0); + x1 = + rewriter.create(loc, rewriter.getIndexType(), x1); + + Value y0x0 = rewriter.create( + loc, input, ValueRange{batch, y0, x0, channel}); + Value y0x1 = rewriter.create( + loc, input, ValueRange{batch, y0, x1, channel}); + Value y1x0 = rewriter.create( + loc, input, ValueRange{batch, y1, x0, channel}); + Value y1x1 = rewriter.create( + loc, input, ValueRange{batch, y1, x1, channel}); + + if (floatingPointMode) { + auto oneVal = rewriter.create( + loc, rewriter.getF32FloatAttr(1.f)); + Value rightPart = dx; + Value leftPart = rewriter.create(loc, oneVal, dx); + + y0x0 = rewriter.create(loc, y0x0, leftPart); + y0x1 = rewriter.create(loc, y0x1, rightPart); + Value topAcc = rewriter.create(loc, y0x0, y0x1); + + y1x0 = rewriter.create(loc, y1x0, leftPart); + y1x1 = rewriter.create(loc, y1x1, rightPart); + Value bottomAcc = rewriter.create(loc, y1x0, y1x1); + + Value bottomPart = dy; + Value topPart = rewriter.create(loc, oneVal, dy); + topAcc = rewriter.create(loc, topAcc, topPart); + bottomAcc = rewriter.create(loc, bottomAcc, bottomPart); + Value result = rewriter.create(loc, topAcc, bottomAcc); + + rewriter.create(loc, result); + return success(); + } + y0x0 = rewriter.create(loc, resultElementTy, y0x0); + y0x1 = rewriter.create(loc, resultElementTy, y0x1); + y1x0 = rewriter.create(loc, resultElementTy, y1x0); + y1x1 = rewriter.create(loc, resultElementTy, y1x1); + + if (resultElementTy.getIntOrFloatBitWidth() > 32) { + dx = rewriter.create(loc, resultElementTy, dx); + dy = rewriter.create(loc, resultElementTy, dy); + } + + auto unitVal = rewriter.create( + loc, rewriter.getIntegerAttr(resultElementTy, 1 << shift)); + Value rightPart = dx; + Value leftPart = rewriter.create(loc, unitVal, dx); + + y0x0 = rewriter.create(loc, y0x0, leftPart); + y0x1 = rewriter.create(loc, y0x1, rightPart); + Value topAcc = rewriter.create(loc, y0x0, y0x1); + + y1x0 = rewriter.create(loc, y1x0, leftPart); + y1x1 = rewriter.create(loc, y1x1, rightPart); + Value bottomAcc = rewriter.create(loc, y1x0, y1x1); + + Value bottomPart = dy; + Value topPart = rewriter.create(loc, unitVal, dy); + topAcc = rewriter.create(loc, topAcc, topPart); + bottomAcc = rewriter.create(loc, bottomAcc, bottomPart); + Value result = rewriter.create(loc, topAcc, bottomAcc); + + rewriter.create(loc, result); + return success(); + } } };