Skip to content

Commit

Permalink
[mlir][tosa] Add tosa.logical_* to linalg lowerings
Browse files Browse the repository at this point in the history
Adds lowerings for logical_* boolean operations. Each of these ops only operate
on booleans allowing simple lowerings.

Reviewed By: NatashaKnk

Differential Revision: https://reviews.llvm.org/D98910
  • Loading branch information
rsuderman committed Mar 19, 2021
1 parent a2e0312 commit 1b74981
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
25 changes: 24 additions & 1 deletion mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
Expand Up @@ -149,10 +149,29 @@ createLinalgBodyCalculationForElementwiseOp(Operation *op, ValueRange args,
if (isa<tosa::LogicalLeftShiftOp>(op) && elementTy.isa<IntegerType>())
return rewriter.create<mlir::ShiftLeftOp>(loc, resultTypes, args);

// tosa::LogicalrightShiftOp
// tosa::LogicalRightShiftOp
if (isa<tosa::LogicalRightShiftOp>(op) && elementTy.isa<IntegerType>())
return rewriter.create<mlir::UnsignedShiftRightOp>(loc, resultTypes, args);

// tosa::LogicalAnd
if (isa<tosa::LogicalAndOp>(op) && elementTy.isInteger(1))
return rewriter.create<mlir::AndOp>(loc, resultTypes, args);

// tosa::LogicalNot
if (isa<tosa::LogicalNotOp>(op) && elementTy.isInteger(1)) {
auto one = rewriter.create<mlir::ConstantOp>(
loc, rewriter.getIntegerAttr(elementTy, 1));
return rewriter.create<mlir::XOrOp>(loc, resultTypes, args[0], one);
}

// tosa::LogicalOr
if (isa<tosa::LogicalOrOp>(op) && elementTy.isInteger(1))
return rewriter.create<mlir::OrOp>(loc, resultTypes, args);

// tosa::LogicalXor
if (isa<tosa::LogicalXorOp>(op) && elementTy.isInteger(1))
return rewriter.create<mlir::XOrOp>(loc, resultTypes, args);

// tosa::PowOp
if (isa<tosa::PowOp>(op) && elementTy.isa<FloatType>())
return rewriter.create<mlir::math::PowFOp>(loc, resultTypes, args);
Expand Down Expand Up @@ -869,6 +888,10 @@ void mlir::tosa::populateTosaToLinalgOnTensorsConversionPatterns(
PointwiseConverter<tosa::BitwiseAndOp>,
PointwiseConverter<tosa::BitwiseOrOp>,
PointwiseConverter<tosa::BitwiseXorOp>,
PointwiseConverter<tosa::LogicalAndOp>,
PointwiseConverter<tosa::LogicalNotOp>,
PointwiseConverter<tosa::LogicalOrOp>,
PointwiseConverter<tosa::LogicalXorOp>,
PointwiseConverter<tosa::LogicalLeftShiftOp>,
PointwiseConverter<tosa::LogicalRightShiftOp>,
PointwiseConverter<tosa::SelectOp>, PointwiseConverter<tosa::GreaterOp>,
Expand Down
24 changes: 24 additions & 0 deletions mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir
Expand Up @@ -260,6 +260,30 @@ func @test_simple_i32(%arg0: tensor<1xi32>) -> () {

// -----

// CHECK-LABEL: @test_bool
func @test_bool(%arg0: tensor<1xi1>, %arg1: tensor<1xi1>) -> () {
// CHECK: linalg.generic
// CHECK: and
%0 = "tosa.logical_and"(%arg0, %arg1) : (tensor<1xi1>, tensor<1xi1>) -> tensor<1xi1>

// CHECK: linalg.generic
// CHECK: or
%1 = "tosa.logical_or"(%arg0, %arg1) : (tensor<1xi1>, tensor<1xi1>) -> tensor<1xi1>

// CHECK: linalg.generic
// CHECK: xor
%2 = "tosa.logical_xor"(%arg0, %arg1) : (tensor<1xi1>, tensor<1xi1>) -> tensor<1xi1>

// CHECK: linalg.generic
// CHECK: constant true
// CHECK: xor
%3 = "tosa.logical_not"(%arg0) : (tensor<1xi1>) -> tensor<1xi1>

return
}

// -----

// CHECK: #[[$MAP0:.*]] = affine_map<(d0, d1) -> (d0, d1)>
// CHECK-LABEL: @test_reshape_downrank
func @test_reshape_downrank(%arg0: tensor<2x3xf32>) -> tensor<6xf32> {
Expand Down

0 comments on commit 1b74981

Please sign in to comment.