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. --- mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp | 408 +++++++++++----------- 1 file changed, 200 insertions(+), 208 deletions(-) diff --git a/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp b/mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp index f28527d..ed6b384 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); - - 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)); + 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)); + + 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; + + 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])); + } - Value inY = - rewriter.create(loc, rewriter.getI32Type(), y); - Value inX = - rewriter.create(loc, rewriter.getI32Type(), x); + // 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); - int32_t shift = op.shift(); - bool floatingPointMode = shift == 0; + y = rewriter.create(loc, y, yStride); + x = rewriter.create(loc, x, xStride); - 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])); - } + y = rewriter.create(loc, y, yOffset); + x = rewriter.create(loc, x, xOffset); - // 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); + iy = rewriter.create(loc, y); + ix = rewriter.create(loc, x); - y = rewriter.create(loc, y, yStride); - x = rewriter.create(loc, x, xStride); + dy = rewriter.create(loc, y, iy); + dx = rewriter.create(loc, x, ix); - y = rewriter.create(loc, y, yOffset); - x = rewriter.create(loc, x, xOffset); + iy = rewriter.create(loc, rewriter.getI32Type(), iy); + ix = rewriter.create(loc, rewriter.getI32Type(), ix); + } else { + Value shiftVal = rewriter.create( + loc, rewriter.getI32IntegerAttr(shift)); - iy = rewriter.create(loc, y); - ix = rewriter.create(loc, x); + Value y = rewriter.create(loc, inY, yStride); + Value x = rewriter.create(loc, inX, xStride); - dy = rewriter.create(loc, y, iy); - dx = rewriter.create(loc, x, ix); + y = rewriter.create(loc, y, yOffset); + x = rewriter.create(loc, x, xOffset); - iy = rewriter.create(loc, rewriter.getI32Type(), iy); - ix = rewriter.create(loc, rewriter.getI32Type(), ix); - } else { - Value shiftVal = rewriter.create( - loc, rewriter.getI32IntegerAttr(shift)); + 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); - Value y = rewriter.create(loc, inY, yStride); - Value x = rewriter.create(loc, inX, xStride); + dy = rewriter.create(loc, y, yTrunc); + dx = rewriter.create(loc, x, xTrunc); + } - y = rewriter.create(loc, y, yOffset); - x = rewriter.create(loc, x, xOffset); + 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); + } - iy = rewriter.create(loc, y, shiftVal); - ix = rewriter.create(loc, x, shiftVal); + auto zeroVal = rewriter.create( + loc, rewriter.getI32IntegerAttr(0)); + auto oneVal = rewriter.create( + loc, rewriter.getI32IntegerAttr(1)); - Value yTrunc = rewriter.create(loc, iy, shiftVal); - Value xTrunc = rewriter.create(loc, ix, shiftVal); + auto yOffset = + rewriter.create(loc, yPred, oneVal, zeroVal); + auto xOffset = + rewriter.create(loc, xPred, oneVal, zeroVal); - dy = rewriter.create(loc, y, yTrunc); - dx = rewriter.create(loc, x, xTrunc); - } + iy = rewriter.create(loc, iy, yOffset); + ix = rewriter.create(loc, ix, xOffset); - 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); - } + // Clamp the to be within the bounds of the input image. - auto zeroVal = rewriter.create( - loc, rewriter.getI32IntegerAttr(0)); - auto oneVal = rewriter.create( - loc, rewriter.getI32IntegerAttr(1)); + iy = clampHelper(loc, iy, hwMin, hMax, + arith::CmpIPredicate::slt, rewriter); + ix = clampHelper(loc, ix, hwMin, wMax, + arith::CmpIPredicate::slt, rewriter); - auto yOffset = - rewriter.create(loc, yPred, oneVal, zeroVal); - auto xOffset = - rewriter.create(loc, xPred, oneVal, zeroVal); + // Read the value from the input array. + iy = + rewriter.create(loc, rewriter.getIndexType(), iy); + ix = + rewriter.create(loc, rewriter.getIndexType(), ix); - iy = rewriter.create(loc, iy, yOffset); - ix = rewriter.create(loc, ix, xOffset); + Value result = rewriter.create( + loc, input, ValueRange{batch, iy, ix, channel}); - // Clamp the to be within the bounds of the input image. + rewriter.create(loc, result); - iy = clampHelper(loc, iy, hwMin, hMax, - arith::CmpIPredicate::slt, rewriter); - ix = clampHelper(loc, ix, hwMin, wMax, - arith::CmpIPredicate::slt, rewriter); + return success(); + } - // Read the value from the input array. - iy = rewriter.create(loc, rewriter.getIndexType(), - iy); - ix = rewriter.create(loc, rewriter.getIndexType(), - ix); + if (op.mode() == "BILINEAR") { + Value y0 = iy; + Value x0 = ix; - Value result = rewriter.create( - loc, input, ValueRange{batch, iy, ix, channel}); + auto oneVal = rewriter.create( + loc, rewriter.getI32IntegerAttr(1)); + Value y1 = rewriter.create(loc, y0, oneVal); + Value x1 = rewriter.create(loc, x0, oneVal); - rewriter.create(loc, result); + y0 = clampHelper(loc, y0, hwMin, hMax, + arith::CmpIPredicate::slt, rewriter); + y1 = clampHelper(loc, y1, hwMin, hMax, + arith::CmpIPredicate::slt, rewriter); - return success(); - } + x0 = clampHelper(loc, x0, hwMin, wMax, + arith::CmpIPredicate::slt, rewriter); + x1 = clampHelper(loc, x1, hwMin, wMax, + arith::CmpIPredicate::slt, rewriter); - if (op.mode() == "BILINEAR") { - Value y0 = iy; - Value x0 = ix; + 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.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); - } + loc, rewriter.getF32FloatAttr(1.f)); + Value rightPart = dx; + Value leftPart = rewriter.create(loc, oneVal, dx); - 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); - 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); - 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); - 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(); + 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); } - return failure(); - } + auto unitVal = rewriter.create( + loc, rewriter.getIntegerAttr(resultElementTy, 1 << shift)); + Value rightPart = dx; + Value leftPart = rewriter.create(loc, unitVal, dx); - return success(); + 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(); + } } }; -- 2.7.4