Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Main to release #199

Merged
merged 3 commits into from
Jun 13, 2024
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
4 changes: 3 additions & 1 deletion mlir/include/mlir/Conversion/TosaToArith/TosaToArith.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "mlir/Pass/Pass.h"

namespace mlir {
class TypeConverter;

#define GEN_PASS_DECL_TOSATOARITH
#include "mlir/Conversion/Passes.h.inc"
Expand All @@ -25,7 +26,8 @@ namespace tosa {
std::unique_ptr<Pass> createTosaToArith(bool includeApplyRescale = false,
bool use32BitApplyRescale = false);

void populateTosaToArithConversionPatterns(RewritePatternSet *patterns);
void populateTosaToArithConversionPatterns(TypeConverter &converter,
RewritePatternSet *patterns);

void populateTosaRescaleToArithConversionPatterns(RewritePatternSet *patterns,
bool include32Bit = false);
Expand Down
24 changes: 24 additions & 0 deletions mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,29 @@ class CmpIOpConversion : public OpConversionPattern<arith::CmpIOp> {
}
};

class NegFOpConversion : public OpConversionPattern<arith::NegFOp> {
public:
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(arith::NegFOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {

auto adaptedOp = adaptor.getOperand();
auto adaptedOpType = adaptedOp.getType();

if (!isa<FloatType>(adaptedOpType)) {
return rewriter.notifyMatchFailure(op.getLoc(),
"negf currently only supported on "
"floats, not tensors/vectors thereof");
}

rewriter.replaceOpWithNewOp<emitc::UnaryMinusOp>(op, adaptedOpType,
adaptedOp);
return success();
}
};

template <typename ArithOp, bool castToUnsigned>
class CastConversion : public OpConversionPattern<ArithOp> {
public:
Expand Down Expand Up @@ -716,6 +739,7 @@ void mlir::populateArithToEmitCPatterns(RewritePatternSet &patterns,
UnsignedShiftOpConversion<arith::ShRUIOp, emitc::BitwiseRightShiftOp>,
CmpFOpConversion,
CmpIOpConversion,
NegFOpConversion,
SelectOpConversion,
// Truncation is guaranteed for unsigned types.
UnsignedCastConversion<arith::TruncIOp>,
Expand Down
25 changes: 18 additions & 7 deletions mlir/lib/Conversion/TosaToArith/TosaToArith.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,31 @@
#include "mlir/Dialect/Tosa/IR/TosaOps.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/IR/TypeUtilities.h"
#include "mlir/Transforms/DialectConversion.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"

using namespace mlir;
using namespace tosa;

namespace {

class ConstOpConverter : public OpRewritePattern<tosa::ConstOp> {
class ConstOpConverter : public OpConversionPattern<tosa::ConstOp> {
public:
using OpRewritePattern<tosa::ConstOp>::OpRewritePattern;
using OpConversionPattern::OpConversionPattern;

LogicalResult matchAndRewrite(tosa::ConstOp op,
PatternRewriter &rewriter) const final {
rewriter.replaceOpWithNewOp<arith::ConstantOp>(op, op.getValue());
LogicalResult matchAndRewrite(tosa::ConstOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const final {

auto elements = dyn_cast<DenseElementsAttr>(adaptor.getValue());
if (!elements) {
return rewriter.notifyMatchFailure(op, "expected dense elements attr");
}

auto convertedElTy = getTypeConverter()->convertType(elements.getElementType());
if (!convertedElTy) {
return rewriter.notifyMatchFailure(op, "type conversion failed");
}
rewriter.replaceOpWithNewOp<arith::ConstantOp>(op, elements.bitcast(convertedElTy));
return success();
}
};
Expand Down Expand Up @@ -238,9 +249,9 @@ class ApplyScale32BitOpConverter : public OpRewritePattern<tosa::ApplyScaleOp> {

} // namespace

void mlir::tosa::populateTosaToArithConversionPatterns(
void mlir::tosa::populateTosaToArithConversionPatterns(TypeConverter &converter,
RewritePatternSet *patterns) {
patterns->add<ConstOpConverter>(patterns->getContext());
patterns->add<ConstOpConverter>(converter, patterns->getContext());
}

void mlir::tosa::populateTosaRescaleToArithConversionPatterns(
Expand Down
6 changes: 5 additions & 1 deletion mlir/lib/Conversion/TosaToArith/TosaToArithPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "mlir/Pass/PassManager.h"
#include "mlir/Transforms/DialectConversion.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "mlir/Conversion/TosaToLinalg/TosaToLinalg.h"

namespace mlir {
#define GEN_PASS_DEF_TOSATOARITH
Expand All @@ -34,12 +35,15 @@ struct TosaToArith : public impl::TosaToArithBase<TosaToArith> {
TosaToArith(TosaToArithOptions &options) : TosaToArithBase(options) {}

void runOnOperation() override {
TypeConverter converter;
mlir::tosa::populateTosaToLinalgTypeConversion(converter);

RewritePatternSet patterns(&getContext());
ConversionTarget target(getContext());
target.addIllegalOp<tosa::ConstOp>();
target.addLegalDialect<arith::ArithDialect>();

mlir::tosa::populateTosaToArithConversionPatterns(&patterns);
mlir::tosa::populateTosaToArithConversionPatterns(converter, &patterns);

if (this->includeApplyRescale) {
mlir::tosa::populateTosaRescaleToArithConversionPatterns(&patterns,
Expand Down
16 changes: 16 additions & 0 deletions mlir/test/Conversion/ArithToEmitC/arith-to-emitc-unsupported.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ func.func @arith_cast_fptoui_i1(%arg0: f32) -> i1 {

// -----

func.func @arith_negf_tensor(%arg0: tensor<5xf32>) -> tensor<5xf32> {
// expected-error @+1 {{failed to legalize operation 'arith.negf'}}
%n = arith.negf %arg0 : tensor<5xf32>
return %n: tensor<5xf32>
}

// -----

func.func @arith_negf_vector(%arg0: vector<5xf32>) -> vector<5xf32> {
// expected-error @+1 {{failed to legalize operation 'arith.negf'}}
%n = arith.negf %arg0 : vector<5xf32>
return %n: vector<5xf32>
}

// -----

func.func @arith_shli_i1(%arg0: i1, %arg1: i1) {
// expected-error @+1 {{failed to legalize operation 'arith.shli'}}
%shli = arith.shli %arg0, %arg1 : i1
Expand Down
10 changes: 10 additions & 0 deletions mlir/test/Conversion/ArithToEmitC/arith-to-emitc.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,16 @@ func.func @arith_cmpi_index(%arg0: i32, %arg1: i32) -> i1 {
return %slt: i1
}

// -----

func.func @arith_negf(%arg0: f32) -> f32 {
// CHECK-LABEL: arith_negf
// CHECK-SAME: %[[Arg0:[^ ]*]]: f32
// CHECK: %[[N:[^ ]*]] = emitc.unary_minus %[[Arg0]] : (f32) -> f32
%n = arith.negf %arg0 : f32
// CHECK: return %[[N]]
return %n: f32
}

// -----

Expand Down
14 changes: 9 additions & 5 deletions mlir/test/Conversion/TosaToArith/tosa-to-arith.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
// RUN: mlir-opt --split-input-file --tosa-to-arith="include-apply-rescale=false" %s -verify-diagnostics -o -| FileCheck --check-prefix="SCALE" %s

// CHECK-LABEL: func @const_test
func.func @const_test() -> (tensor<i32>) {
// CHECK: [[C3:%.+]] = arith.constant dense<3> : tensor<i32>
%result = "tosa.const"() {value = dense<3> : tensor<i32>} : () -> tensor<i32>
func.func @const_test() -> (tensor<i32>, tensor<ui32>) {
// CHECK: %[[CI32:.+]] = arith.constant dense<3> : tensor<i32>
%i32 = "tosa.const"() {value = dense<3> : tensor<i32>} : () -> tensor<i32>

// CHECK: return [[C3]]
return %result : tensor<i32>
// CHECK: %[[CUI32:.+]] = arith.constant dense<3> : tensor<i32>
// CHECK: %[[CAST:.*]] = builtin.unrealized_conversion_cast %[[CUI32]] : tensor<i32> to tensor<ui32>
%ui32 = "tosa.const"() {value = dense<3> : tensor<ui32>} : () -> tensor<ui32>

// CHECK: return %[[CI32]], %[[CAST]]
return %i32, %ui32 : tensor<i32>, tensor<ui32>
}

// -----
Expand Down