Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@ class ConvertGenericOpWithIntegerTensorType : public ConversionPattern {
}
};

class ConvertTosaConstWithIntegerTensorType
: public OpConversionPattern<tosa::ConstOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(tosa::ConstOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const final {
const ElementsAttr oldAttr = op.getValues();
const auto oldTy = llvm::cast<ShapedType>(oldAttr.getType());
const auto newTy =
llvm::cast<ShapedType>(typeConverter->convertType(oldTy));
if (oldTy == newTy)
return success();

ElementsAttr newAttr = oldAttr;
if (auto denseAttr = llvm::dyn_cast<DenseElementsAttr>(oldAttr)) {
newAttr = DenseElementsAttr::get(newTy, denseAttr.getRawData());
} else {
return rewriter.notifyMatchFailure(op, "unknown elements attribute type");
}

rewriter.replaceOpWithNewOp<tosa::ConstOp>(op, newTy, newAttr);
return success();
}
};

class TosaConvertIntegerTypeToSignless
: public impl::TosaConvertIntegerTypeToSignlessBase<
TosaConvertIntegerTypeToSignless> {
Expand All @@ -116,6 +142,10 @@ class TosaConvertIntegerTypeToSignless
return typeConverter.isSignatureLegal(op.getFunctionType()) &&
typeConverter.isLegal(&op.getBody());
});
target.addDynamicallyLegalOp<tosa::ConstOp>([&](tosa::ConstOp op) {
return typeConverter.isLegal(op.getType()) &&
typeConverter.isLegal(op.getValues().getType());
});
target.markUnknownOpDynamicallyLegal([&](Operation *op) {
return typeConverter.isLegal(op->getOperandTypes()) &&
typeConverter.isLegal(op->getResultTypes());
Expand All @@ -125,6 +155,7 @@ class TosaConvertIntegerTypeToSignless
populateFunctionOpInterfaceTypeConversionPattern<func::FuncOp>(
patterns, typeConverter);
patterns.add<ConvertGenericOpWithIntegerTensorType>(typeConverter, context);
patterns.add<ConvertTosaConstWithIntegerTensorType>(typeConverter, context);

if (failed(
applyFullConversion(getOperation(), target, std::move(patterns))))
Expand Down
24 changes: 24 additions & 0 deletions mlir/test/Dialect/Tosa/tosa-convert-integer-type-to-signless.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ func.func @test_rescale_input_unsigned(%arg0: tensor<1x1xui16>) -> (tensor<1x1xi

// -----

// CHECK-LABEL: test_rescale_unsigned_zp
// CHECK: %[[ZP_IN:.*]] = "tosa.const"() <{values = dense<-2> : tensor<1xi8>}> : () -> tensor<1xi8>
// CHECK: %[[ZP_OUT:.*]] = "tosa.const"() <{values = dense<2> : tensor<1xi8>}> : () -> tensor<1xi8>
// CHECK: tosa.rescale %arg0, %0, %1, %[[ZP_IN]], %[[ZP_OUT]] {input_unsigned = true, output_unsigned = false, per_channel = false, rounding_mode = SINGLE_ROUND, scale32 = true} : (tensor<1x1xi8>, tensor<1xi32>, tensor<1xi8>, tensor<1xi8>, tensor<1xi8>)
func.func @test_rescale_unsigned_zp(%arg0: tensor<1x1xui8>) -> tensor<1x1xi8> {
%0 = "tosa.const"() <{values = dense<2> : tensor<1xi32>}> : () -> tensor<1xi32>
%1 = "tosa.const"() <{values = dense<1> : tensor<1xi8>}> : () -> tensor<1xi8>
%2 = "tosa.const"() <{values = dense<254> : tensor<1xui8>}> : () -> tensor<1xui8>
%3 = "tosa.const"() <{values = dense<2> : tensor<1xi8>}> : () -> tensor<1xi8>
%r = tosa.rescale %arg0, %0, %1, %2, %3 {input_unsigned = true, output_unsigned = false, per_channel = false, rounding_mode = SINGLE_ROUND, scale32 = true} : (tensor<1x1xui8>, tensor<1xi32>, tensor<1xi8>, tensor<1xui8>, tensor<1xi8>) -> tensor<1x1xi8>
return %r : tensor<1x1xi8>
}

// -----

// CHECK-LABEL: test_unsigned_function_signature
// CHECK: %arg0: tensor<1xi8>, %arg1: tensor<1xi8>
func.func @test_unsigned_function_signature(%arg0: tensor<1xui8>, %arg1: tensor<1xui8>) -> (tensor<1xui8>, tensor<1xui8>) {
Expand All @@ -41,6 +56,15 @@ func.func @test_unsigned_function_signature(%arg0: tensor<1xui8>, %arg1: tensor<

// -----

// CHECK-LABEL: test_unsigned_const_data
// CHECK: "tosa.const"() <{values = dense<[-1, -2, 0, 1, -128]> : tensor<5xi8>}> : () -> tensor<5xi8>
func.func @test_unsigned_const_data() -> tensor<5xui8> {
%0 = "tosa.const"() <{values = dense<[255, 254, 0, 1, 128]> : tensor<5xui8>}> : () -> tensor<5xui8>
return %0 : tensor<5xui8>
}

// -----

// CHECK-LABEL: test_no_change
// CHECK: %arg0: tensor<13x21x3xi8>
func.func @test_no_change(%arg0: tensor<13x21x3xi8>) -> tensor<13x21x3xi8> {
Expand Down
Loading