diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaInterfaces.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaInterfaces.td --- a/mlir/include/mlir/Dialect/Tosa/IR/TosaInterfaces.td +++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaInterfaces.td @@ -17,8 +17,7 @@ def TosaOpInterface : OpInterface<"TosaOp"> { let description = [{ - Implements interfaces implemented by ops that correspond to the Tosa - specification. + Implemented by ops that correspond to the Tosa specification. }]; } diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaOpBase.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaOpBase.td --- a/mlir/include/mlir/Dialect/Tosa/IR/TosaOpBase.td +++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaOpBase.td @@ -114,9 +114,9 @@ (ins "Type":$outputType, "Value":$input, "Value":$weight, "Value":$bias, "ArrayAttr":$pad, "ArrayAttr":$stride, "ArrayAttr":$dilation), [{ - ::buildConvOpWithQuantInfo($_builder, $_state, outputType, - input, weight, bias, - pad, stride, dilation); + buildConvOpWithQuantInfo($_builder, $_state, outputType, + input, weight, bias, + pad, stride, dilation); }]>; // Handles tosa.transpose_conv2d which has an outpad and output shape attribute. @@ -125,10 +125,10 @@ "ArrayAttr":$outpad, "ArrayAttr":$stride, "ArrayAttr":$dilation, "ArrayAttr":$outputShape), [{ - ::buildTransConvOpWithQuantInfo($_builder, $_state, outputType, - input, weight, bias, - outpad, stride, dilation, - outputShape); + buildTransConvOpWithQuantInfo($_builder, $_state, outputType, + input, weight, bias, + outpad, stride, dilation, + outputShape); }]>; // The tosa.fully_connected op has its own builder as it does not have @@ -136,8 +136,8 @@ def Tosa_FCOpQuantInfoBuilder : OpBuilderDAG< (ins "Type":$outputType, "Value":$input, "Value":$weight, "Value":$bias), [{ - ::buildFCOpWithQuantInfo($_builder, $_state, outputType, - input, weight, bias); + buildFCOpWithQuantInfo($_builder, $_state, outputType, + input, weight, bias); }]>; // The tosa.matmul op is also intended to be generated where a fully_connected @@ -147,8 +147,8 @@ def Tosa_MatMulOpQuantInfoBuilder : OpBuilderDAG< (ins "Type":$outputType, "Value":$a, "Value":$b), [{ - ::buildMatMulOpWithQuantInfo($_builder, $_state, outputType, - a, b); + buildMatMulOpWithQuantInfo($_builder, $_state, outputType, + a, b); }]>; // Both the tosa.avg_pool2d and unary ops use the same @@ -158,8 +158,8 @@ (ins "Type":$outputType, "Value":$input, "ArrayAttr":$kernel, "ArrayAttr":$stride, "ArrayAttr":$pad), [{ - ::buildAvgPool2dOpWithQuantInfo($_builder, $_state, outputType, - input, kernel, stride, pad); + buildAvgPool2dOpWithQuantInfo($_builder, $_state, outputType, + input, kernel, stride, pad); }]>; // This builder is called on single-parameter unary operators that have a scale @@ -168,7 +168,7 @@ def Tosa_UnaryOpQuantInfoBuilder : OpBuilderDAG< (ins "Type":$outputType, "Value":$input), [{ - ::buildUnaryOpWithQuantInfo($_builder, $_state, outputType, input); + buildUnaryOpWithQuantInfo($_builder, $_state, outputType, input); }]>; // This builder is called on the TOSA pad operator that needs to create its own @@ -177,8 +177,8 @@ def Tosa_PadOpQuantInfoBuilder : OpBuilderDAG< (ins "Type":$outputType, "Value":$input, "Value":$paddings), [{ - ::buildPadOpWithQuantInfo($_builder, $_state, outputType, - input, paddings); + buildPadOpWithQuantInfo($_builder, $_state, outputType, + input, paddings); }]>; //===----------------------------------------------------------------------===// diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td --- a/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td +++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td @@ -104,7 +104,7 @@ let builders = [Tosa_ConvOpQuantInfoBuilder]; - let verifier = [{ return ::verifyConvOp(*this); }]; + let verifier = [{ return verifyConvOp(*this); }]; } //===----------------------------------------------------------------------===// @@ -134,7 +134,7 @@ let builders = [Tosa_ConvOpQuantInfoBuilder]; - let verifier = [{ return ::verifyConvOp(*this); }]; + let verifier = [{ return verifyConvOp(*this); }]; } //===----------------------------------------------------------------------===// @@ -165,7 +165,7 @@ let builders = [Tosa_ConvOpQuantInfoBuilder]; - let verifier = [{ return ::verifyConvOp(*this); }]; + let verifier = [{ return verifyConvOp(*this); }]; } //===----------------------------------------------------------------------===// @@ -191,7 +191,7 @@ let builders = [Tosa_FCOpQuantInfoBuilder]; - let verifier = [{ return ::verifyConvOp(*this); }]; + let verifier = [{ return verifyConvOp(*this); }]; } //===----------------------------------------------------------------------===// diff --git a/mlir/include/mlir/Dialect/Tosa/Transforms/Passes.h b/mlir/include/mlir/Dialect/Tosa/Transforms/Passes.h --- a/mlir/include/mlir/Dialect/Tosa/Transforms/Passes.h +++ b/mlir/include/mlir/Dialect/Tosa/Transforms/Passes.h @@ -16,7 +16,6 @@ #include "mlir/Pass/Pass.h" namespace mlir { - namespace tosa { std::unique_ptr createTosaMakeBroadcastablePass(); diff --git a/mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h b/mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h --- a/mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h +++ b/mlir/include/mlir/Dialect/Tosa/Utils/QuantUtils.h @@ -19,8 +19,8 @@ #include "mlir/Dialect/Quant/FakeQuantSupport.h" #include "mlir/Dialect/Quant/UniformSupport.h" -using namespace mlir; -using namespace mlir::tosa; +namespace mlir { +namespace tosa { //===----------------------------------------------------------------------===// // Utililty functions to support quantization handling in Tosa. @@ -65,4 +65,7 @@ IntegerAttr quantBits, int filterQuantDim, bool isSigned, BoolAttr narrowRange); +} // namespace tosa +} // namespace mlir + #endif // DIALECT_TOSA_UTILS_QUANT_UTILS_H diff --git a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp --- a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp +++ b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp @@ -93,7 +93,8 @@ // TOSA Operator Verifiers. //===----------------------------------------------------------------------===// -template static LogicalResult verifyConvOp(T op) { +template +static LogicalResult verifyConvOp(T op) { // All TOSA conv ops have an input() and weight(). auto inputType = op.input().getType().template dyn_cast(); auto weightType = op.weight().getType().template dyn_cast(); @@ -127,10 +128,10 @@ /// This builder is called on all convolution operators except TransposeConv, /// which has specialized output shape semantics. The builder also defines the /// bitwidth of the output given the bit width of the input & weight content. -void buildConvOpWithQuantInfo(OpBuilder &builder, OperationState &result, - Type outputType, Value input, Value weight, - Value bias, ArrayAttr pad, ArrayAttr stride, - ArrayAttr dilation) { +static void buildConvOpWithQuantInfo(OpBuilder &builder, OperationState &result, + Type outputType, Value input, Value weight, + Value bias, ArrayAttr pad, + ArrayAttr stride, ArrayAttr dilation) { result.addOperands({input, weight, bias}); result.addAttribute("pad", pad); @@ -148,11 +149,11 @@ } /// Handles tosa.transpose_conv2d which has outpad and output shape attributes. -void buildTransConvOpWithQuantInfo(OpBuilder &builder, OperationState &result, - Type outputType, Value input, Value weight, - Value bias, ArrayAttr outpad, - ArrayAttr stride, ArrayAttr dilation, - ArrayAttr outputShape) { +static void +buildTransConvOpWithQuantInfo(OpBuilder &builder, OperationState &result, + Type outputType, Value input, Value weight, + Value bias, ArrayAttr outpad, ArrayAttr stride, + ArrayAttr dilation, ArrayAttr outputShape) { result.addOperands({input, weight, bias}); result.addAttribute("out_pad", outpad); result.addAttribute("stride", stride); @@ -171,9 +172,9 @@ /// The tosa.fully_connected op has its own builder as it does not have /// strides/dilation/padding. -void buildFCOpWithQuantInfo(OpBuilder &builder, OperationState &result, - Type outputType, Value input, Value weight, - Value bias) { +static void buildFCOpWithQuantInfo(OpBuilder &builder, OperationState &result, + Type outputType, Value input, Value weight, + Value bias) { result.addOperands({input, weight, bias}); auto quantAttr = ::buildConvOpQuantizationAttr(builder, input, weight); @@ -190,8 +191,9 @@ /// op must be constructed where the weight is not a constant. In this case, /// the fully_connected op must be expressed using matmul. /// TODO: Add link to the leglization document explaining this. -void buildMatMulOpWithQuantInfo(OpBuilder &builder, OperationState &result, - Type outputType, Value a, Value b) { +static void buildMatMulOpWithQuantInfo(OpBuilder &builder, + OperationState &result, Type outputType, + Value a, Value b) { result.addOperands({a, b}); auto quantAttr = ::buildMatMulOpQuantizationAttr(builder, a, b); @@ -227,10 +229,11 @@ /// Both the tosa.avg_pool2d and unary ops use the same UnaruOpQuantizationAttr /// but avg_pool operator has its own builder as it has additional parameters /// not part of the unary ops. -void buildAvgPool2dOpWithQuantInfo(OpBuilder &builder, OperationState &result, - Type outputType, Value input, - ArrayAttr kernel, ArrayAttr stride, - ArrayAttr pad) { +static void buildAvgPool2dOpWithQuantInfo(OpBuilder &builder, + OperationState &result, + Type outputType, Value input, + ArrayAttr kernel, ArrayAttr stride, + ArrayAttr pad) { result.addOperands(input); result.addAttribute("kernel", kernel); result.addAttribute("stride", stride); @@ -244,8 +247,9 @@ /// This builder is called on single-parameter unary operators that have scale /// relationship between their input and output, expressed by the /// UnaryOpQuantizationAttr. -void buildUnaryOpWithQuantInfo(OpBuilder &builder, OperationState &result, - Type outputType, Value input) { +static void buildUnaryOpWithQuantInfo(OpBuilder &builder, + OperationState &result, Type outputType, + Value input) { result.addOperands(input); auto quantAttr = buildUnaryOpQuantizationAttr(builder, input, outputType); if (quantAttr) @@ -256,8 +260,9 @@ /// This builder is called on TOSA pad operator that needs to create its own /// OptionalAttr quantization_attr parameter to scale the padding values /// correctly. -void buildPadOpWithQuantInfo(OpBuilder &builder, OperationState &result, - Type outputType, Value input, Value paddings) { +static void buildPadOpWithQuantInfo(OpBuilder &builder, OperationState &result, + Type outputType, Value input, + Value paddings) { result.addOperands({input, paddings}); auto quantAttr = buildPadOpQuantizationAttr(builder, input); if (quantAttr) diff --git a/mlir/lib/Dialect/Tosa/Transforms/TosaMakeBroadcastable.cpp b/mlir/lib/Dialect/Tosa/Transforms/TosaMakeBroadcastable.cpp --- a/mlir/lib/Dialect/Tosa/Transforms/TosaMakeBroadcastable.cpp +++ b/mlir/lib/Dialect/Tosa/Transforms/TosaMakeBroadcastable.cpp @@ -130,6 +130,7 @@ ArrayRef outputRankShape = outputType.getShape(); ArrayRef higherRankShape = higherTensorValue.getType().cast().getShape(); + (void)higherRankShape; ArrayRef lowerRankShape = lowerTensorValue.getType().cast().getShape(); @@ -160,7 +161,8 @@ } namespace { -template struct ConvertTosaOp : public OpRewritePattern { +template +struct ConvertTosaOp : public OpRewritePattern { using OpRewritePattern::OpRewritePattern; LogicalResult matchAndRewrite(OpTy tosaBinaryOp, diff --git a/mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp b/mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp --- a/mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp +++ b/mlir/lib/Dialect/Tosa/Utils/QuantUtils.cpp @@ -19,8 +19,9 @@ /// From a scale value, generates multiplier and shift values where /// mantissa is in [-1.0,-0.5] or [0.5, 1.0] such that /// multiplier = mantissa*2^shift for 16-bit scaling. -void computeMultiplierAndShiftTosaScale16(double scale, int32_t &multiplier, - int32_t &shift) { +static void computeMultiplierAndShiftTosaScale16(double scale, + int32_t &multiplier, + int32_t &shift) { const double mantissa = std::frexp(scale, &shift); auto shiftedM = std::round(mantissa * (int64_t(1) << 15)); @@ -47,8 +48,9 @@ /// From a scale value, generates multiplier and shift values where /// mantissa is in [-1.0,-0.5] or [0.5, 1.0] such that /// multiplier = mantissa*2^shift for 32-bit scaling. -void computeMultiplierAndShiftTosaScale32(double scale, int32_t &multiplier, - int32_t &shift) { +static void computeMultiplierAndShiftTosaScale32(double scale, + int32_t &multiplier, + int32_t &shift) { const double mantissa = std::frexp(scale, &shift); auto shiftedM = std::round(mantissa * (int64_t(1) << 31)); @@ -72,8 +74,8 @@ } /// Generates a quantized multiplier/shift from double. -void computeMultiplierAndShift(double scale, int32_t &multiplier, - int32_t &shift, int32_t scaleWidth) { +void mlir::tosa::computeMultiplierAndShift(double scale, int32_t &multiplier, + int32_t &shift, int32_t scaleWidth) { switch (scaleWidth) { case 16: @@ -96,8 +98,9 @@ /// ConvOpQuantInfoBuilder/TransConvOpQuantInfoBuilder: /// input_zp: input zeropoint /// weight_zp: weight zeropoint. -ConvOpQuantizationAttr buildConvOpQuantizationAttr(OpBuilder &builder, - Value input, Value weight) { +ConvOpQuantizationAttr +mlir::tosa::buildConvOpQuantizationAttr(OpBuilder &builder, Value input, + Value weight) { auto inputType = input.getType().dyn_cast(); auto weightType = weight.getType().dyn_cast(); @@ -144,8 +147,9 @@ /// MatMulOpQuantInfoBuilder: /// aZp: input a zeropoint /// bZp: input b zeropoint. -MatMulOpQuantizationAttr buildMatMulOpQuantizationAttr(OpBuilder &builder, - Value a, Value b) { +MatMulOpQuantizationAttr +mlir::tosa::buildMatMulOpQuantizationAttr(OpBuilder &builder, Value a, + Value b) { auto aType = a.getType().dyn_cast(); auto bType = b.getType().dyn_cast(); @@ -179,9 +183,9 @@ /// UnaryOpQuantInfoBuilder: /// inputZp: input zeropoint /// outputZp: output zeropoint. -UnaryOpQuantizationAttr buildUnaryOpQuantizationAttr(OpBuilder &builder, - Value input, - Type outputRawType) { +UnaryOpQuantizationAttr +mlir::tosa::buildUnaryOpQuantizationAttr(OpBuilder &builder, Value input, + Type outputRawType) { auto inputType = input.getType().dyn_cast(); auto outputType = outputRawType.dyn_cast(); @@ -213,8 +217,8 @@ /// Builds PadOpQuantizationAttr, called from PadOpQuantInfoBuilder: /// inputZp: input zeropoint. -PadOpQuantizationAttr buildPadOpQuantizationAttr(OpBuilder &builder, - Value input) { +PadOpQuantizationAttr mlir::tosa::buildPadOpQuantizationAttr(OpBuilder &builder, + Value input) { auto inputType = input.getType().dyn_cast(); @@ -238,8 +242,8 @@ /// Builds output type for a quantized ConvOp with the right bitwidth. /// This is called by the builder when dealing with quantized content. -Type buildConvOpResultTypeInfo(OpBuilder &builder, Type outputType, Value input, - Value weight) { +Type mlir::tosa::buildConvOpResultTypeInfo(OpBuilder &builder, Type outputType, + Value input, Value weight) { auto inputType = input.getType().dyn_cast(); auto weightType = weight.getType().dyn_cast(); @@ -272,10 +276,10 @@ } /// Builds Tosa quantization attributes from min/max values. -Type buildQTypeFromMinMax(OpBuilder builder, Type inputDType, Attribute minAttr, - Attribute maxAttr, IntegerAttr quantBits, - int filterQuantDim, bool isSigned, - BoolAttr narrowRange) { +Type mlir::tosa::buildQTypeFromMinMax(OpBuilder builder, Type inputDType, + Attribute minAttr, Attribute maxAttr, + IntegerAttr quantBits, int filterQuantDim, + bool isSigned, BoolAttr narrowRange) { quant::QuantizedType retType; @@ -339,10 +343,11 @@ } /// Builds Tosa quantization attributes from min/max values. -TypeAttr buildQTypeAttrFromMinMax(OpBuilder builder, Type inputDtype, - Attribute minAttr, Attribute maxAttr, - IntegerAttr quantBits, int filterQuantDim, - bool isSigned, BoolAttr narrowRange) { +TypeAttr +mlir::tosa::buildQTypeAttrFromMinMax(OpBuilder builder, Type inputDtype, + Attribute minAttr, Attribute maxAttr, + IntegerAttr quantBits, int filterQuantDim, + bool isSigned, BoolAttr narrowRange) { return TypeAttr::get(buildQTypeFromMinMax(builder, inputDtype, minAttr, maxAttr, quantBits, filterQuantDim,