Skip to content

Commit cf29d0a

Browse files
NatashaKnkrsuderman
authored andcommitted
[mlir][tosa]Create a check for i64 input in apply_scale lowering in TosaToArith
Reviewed By: rsuderman Differential Revision: https://reviews.llvm.org/D159473
1 parent 2cb357d commit cf29d0a

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

mlir/lib/Conversion/TosaToArith/TosaToArith.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ class ApplyScaleGenericOpConverter
8181
Value shift32 = rewriter.create<arith::ExtUIOp>(loc, i32Ty, op.getShift());
8282

8383
// Compute the multiplication in 64-bits then select the high / low parts.
84-
Value value64 = rewriter.create<arith::ExtSIOp>(loc, i64Ty, value);
84+
Value value64 = value;
85+
if (getElementTypeOrSelf(valueTy) != rewriter.getI64Type())
86+
value64 = rewriter.create<arith::ExtSIOp>(loc, i64Ty, value);
8587
Value multiplier64 =
8688
rewriter.create<arith::ExtSIOp>(loc, i64Ty, multiplier32);
8789
Value multiply64 =

mlir/test/Conversion/TosaToArith/tosa-to-arith.mlir

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,40 @@ func.func @apply_scale_test_i48(%arg0 : i48, %arg1 : i32, %arg2 : i8) -> (i32) {
118118
%res = tosa.apply_scale %arg0, %arg1, %arg2 {double_round = true} : (i48, i32, i8) -> i32
119119
return %res : i32
120120
}
121+
122+
// -----
123+
124+
// CHECK-LABEL: @apply_scale_test_i64
125+
// SCALE: tosa.apply_scale
126+
func.func @apply_scale_test_i64(%arg0 : i64, %arg1 : i32, %arg2 : i8) -> (i32) {
127+
// CHECK-DAG: %[[C0:.+]] = arith.constant 0 : i64
128+
// CHECK-DAG: %[[C1:.+]] = arith.constant 1 : i64
129+
// CHECK-DAG: %[[C31:.+]] = arith.constant 31 : i32
130+
131+
// Multiply in 64 bits.
132+
// CHECK-DAG: %[[M64:.+]] = arith.extsi %arg1 : i32 to i64
133+
// CHECK-DAG: %[[MUL:.+]] = arith.muli %arg0, %[[M64]]
134+
135+
// Round normally.
136+
// CHECK-DAG: %[[S32:.+]] = arith.extui %arg2 : i8 to i32
137+
// CHECK-DAG: %[[S64:.+]] = arith.extui %[[S32]] : i32 to i64
138+
// CHECK-DAG: %[[ONEL:.+]] = arith.shli %[[C1]], %[[S64]] : i64
139+
// CHECK-DAG: %[[ONER:.+]] = arith.shrui %[[ONEL]], %[[C1]]
140+
// CHECK-DAG: %[[ROUND:.+]] = arith.addi %[[MUL]], %[[ONER]]
141+
142+
// Apply double rounding.
143+
// CHECK-DAG: %[[DUP:.+]] = arith.constant 1073741824 : i64
144+
// CHECK-DAG: %[[DDOWN:.+]] = arith.constant -1073741824 : i64
145+
// CHECK-DAG: %[[POS:.+]] = arith.cmpi sge, %arg0, %[[C0]]
146+
// CHECK-DAG: %[[DBIT:.+]] = arith.select %[[POS]], %[[DUP]], %[[DDOWN]]
147+
// CHECK-DAG: %[[DRND:.+]] = arith.addi %[[DBIT]], %[[ROUND]]
148+
// CHECK-DAG: %[[USED:.+]] = arith.cmpi sgt, %[[S32]], %[[C31]] : i32
149+
// CHECK-DAG: %[[RES64:.+]] = arith.select %[[USED]], %[[DRND]], %[[ROUND]] : i64
150+
151+
// Shift and truncate final answer.
152+
// CHECK-DAG: %[[SHR:.+]] = arith.shrsi %[[RES64]], %[[S64]]
153+
// CHECK-DAG: %[[TRUNC:.+]] = arith.trunci %[[SHR]] : i64 to i32
154+
// CHECK: return %[[TRUNC]]
155+
%res = tosa.apply_scale %arg0, %arg1, %arg2 {double_round = true} : (i64, i32, i8) -> i32
156+
return %res : i32
157+
}

0 commit comments

Comments
 (0)