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

Support integers of arbitrary width and signedness as ntensor.getitem… #349

Merged
merged 1 commit into from
Oct 6, 2022
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/imex/Dialect/imex_util/ImexUtilOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@ def ChangeLayoutOp : ImexUtil_Op<"change_layout", [ViewLikeOpInterface, NoSideEf
def SignCastOp : ImexUtil_Op<"sign_cast", [NoSideEffect]> {
let arguments = (ins AnyType : $value);

let results = (outs AnyType);
let results = (outs AnyType:$dest);
let hasFolder = 1;
let hasCanonicalizer = 1;

let assemblyFormat = "$value attr-dict `:` type($value) `to` type($dest)";
}

def ExtractMemrefMetadataOp
Expand Down
24 changes: 21 additions & 3 deletions mlir/lib/Dialect/ntensor/Transforms/ResolveArrayOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
#include <mlir/Transforms/GreedyPatternRewriteDriver.h>

static bool isIndexOrSlice(mlir::Type type) {
return type.isa<imex::ntensor::SliceType, mlir::IndexType>();
return type
.isa<imex::ntensor::SliceType, mlir::IndexType, mlir::IntegerType>();
}

static bool isValidGetitemIndex(mlir::Type type) {
Expand All @@ -35,6 +36,23 @@ static bool isValidGetitemIndex(mlir::Type type) {
return false;
}

static mlir::Value convertIndex(mlir::OpBuilder &builder, mlir::Location loc,
mlir::Value value) {
auto intType = value.getType().dyn_cast<mlir::IntegerType>();
if (intType) {
if (intType.getSignedness() != mlir::IntegerType::Signless) {
auto signlessType =
mlir::IntegerType::get(builder.getContext(), intType.getWidth());
value = builder.create<imex::util::SignCastOp>(loc, signlessType, value);
}

auto indexType = builder.getIndexType();
value = builder.create<mlir::arith::IndexCastOp>(loc, indexType, value);
}

return value;
}

static mlir::LogicalResult
computeIndices(mlir::OpBuilder &builder, mlir::Location loc, mlir::Value value,
mlir::Value index,
Expand Down Expand Up @@ -70,8 +88,8 @@ computeIndices(mlir::OpBuilder &builder, mlir::Location loc, mlir::Value value,
auto size = resolved.getCount();
return {foldConst(begin), foldConst(size), foldConst(step), true};
} else {
mlir::Value index =
builder.create<imex::ntensor::ResolveIndexOp>(loc, indexVal, len);
mlir::Value index = convertIndex(builder, loc, indexVal);
index = builder.create<imex::ntensor::ResolveIndexOp>(loc, index, len);
return {index, builder.getIndexAttr(1), builder.getIndexAttr(1), false};
}
};
Expand Down
62 changes: 62 additions & 0 deletions mlir/test/Dialect/ntensor/resolve-array-ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,37 @@ func.func @test(%arg1: !ntensor.ntensor<?xf32>, %arg2: index) -> f32 {

// -----

func.func @test(%arg1: !ntensor.ntensor<?xf32>, %arg2: i32) -> f32 {
%0 = ntensor.getitem(%arg1 : !ntensor.ntensor<?xf32>) [%arg2 : i32] -> f32
return %0 : f32
}
// CHECK-LABEL: func @test
// CHECK-SAME: (%[[ARG1:.*]]: !ntensor.ntensor<?xf32>, %[[ARG2:.*]]: i32)
// CHECK-NEXT: %[[C0:.*]] = arith.constant 0 : index
// CHECK-NEXT: %[[DIM:.*]] = ntensor.dim %[[ARG1]], %[[C0]] : !ntensor.ntensor<?xf32>
// CHECK-NEXT: %[[IDIM:.*]] = arith.index_cast %[[ARG2]] : i32 to index
// CHECK-NEXT: %[[IND:.*]] = ntensor.resolve_index %[[IDIM]], %[[DIM]]
// CHECK-NEXT: %[[RES:.*]] = ntensor.load %[[ARG1]][%[[IND]]] : !ntensor.ntensor<?xf32>
// CHECK-NEXT: return %[[RES]] : f32

// -----

func.func @test(%arg1: !ntensor.ntensor<?xf32>, %arg2: si32) -> f32 {
%0 = ntensor.getitem(%arg1 : !ntensor.ntensor<?xf32>) [%arg2 : si32] -> f32
return %0 : f32
}
// CHECK-LABEL: func @test
// CHECK-SAME: (%[[ARG1:.*]]: !ntensor.ntensor<?xf32>, %[[ARG2:.*]]: si32)
// CHECK-NEXT: %[[C0:.*]] = arith.constant 0 : index
// CHECK-NEXT: %[[DIM:.*]] = ntensor.dim %[[ARG1]], %[[C0]] : !ntensor.ntensor<?xf32>
// CHECK-NEXT: %[[SDIM:.*]] = imex_util.sign_cast %[[ARG2]] : si32 to i32
// CHECK-NEXT: %[[IDIM:.*]] = arith.index_cast %[[SDIM]] : i32 to index
// CHECK-NEXT: %[[IND:.*]] = ntensor.resolve_index %[[IDIM]], %[[DIM]]
// CHECK-NEXT: %[[RES:.*]] = ntensor.load %[[ARG1]][%[[IND]]] : !ntensor.ntensor<?xf32>
// CHECK-NEXT: return %[[RES]] : f32

// -----

func.func @test(%arg1: !ntensor.ntensor<?xf32>, %arg2: index, %arg3: f32) {
ntensor.setitem(%arg1 : !ntensor.ntensor<?xf32>) [%arg2 : index] = (%arg3 : f32)
return
Expand All @@ -28,6 +59,37 @@ func.func @test(%arg1: !ntensor.ntensor<?xf32>, %arg2: index, %arg3: f32) {

// -----

func.func @test(%arg1: !ntensor.ntensor<?xf32>, %arg2: i32, %arg3: f32) {
ntensor.setitem(%arg1 : !ntensor.ntensor<?xf32>) [%arg2 : i32] = (%arg3 : f32)
return
}
// CHECK-LABEL: func @test
// CHECK-SAME: (%[[ARG1:.*]]: !ntensor.ntensor<?xf32>, %[[ARG2:.*]]: i32, %[[ARG3:.*]]: f32)
// CHECK-NEXT: %[[C0:.*]] = arith.constant 0 : index
// CHECK-NEXT: %[[DIM:.*]] = ntensor.dim %[[ARG1]], %[[C0]] : !ntensor.ntensor<?xf32>
// CHECK-NEXT: %[[IDIM:.*]] = arith.index_cast %[[ARG2]] : i32 to index
// CHECK-NEXT: %[[IND:.*]] = ntensor.resolve_index %[[IDIM]], %[[DIM]]
// CHECK-NEXT: ntensor.store %[[ARG3]], %[[ARG1]][%[[IND]]] : !ntensor.ntensor<?xf32>
// CHECK-NEXT: return

// -----

func.func @test(%arg1: !ntensor.ntensor<?xf32>, %arg2: si32, %arg3: f32) {
ntensor.setitem(%arg1 : !ntensor.ntensor<?xf32>) [%arg2 : si32] = (%arg3 : f32)
return
}
// CHECK-LABEL: func @test
// CHECK-SAME: (%[[ARG1:.*]]: !ntensor.ntensor<?xf32>, %[[ARG2:.*]]: si32, %[[ARG3:.*]]: f32)
// CHECK-NEXT: %[[C0:.*]] = arith.constant 0 : index
// CHECK-NEXT: %[[DIM:.*]] = ntensor.dim %[[ARG1]], %[[C0]] : !ntensor.ntensor<?xf32>
// CHECK-NEXT: %[[SDIM:.*]] = imex_util.sign_cast %[[ARG2]] : si32 to i32
// CHECK-NEXT: %[[IDIM:.*]] = arith.index_cast %[[SDIM]] : i32 to index
// CHECK-NEXT: %[[IND:.*]] = ntensor.resolve_index %[[IDIM]], %[[DIM]]
// CHECK-NEXT: ntensor.store %[[ARG3]], %[[ARG1]][%[[IND]]] : !ntensor.ntensor<?xf32>
// CHECK-NEXT: return

// -----

func.func @test(%arg1: !ntensor.ntensor<?xf32>, %arg2: !ntensor.slice) -> !ntensor.ntensor<?xf32> {
%0 = ntensor.getitem(%arg1 : !ntensor.ntensor<?xf32>) [%arg2 : !ntensor.slice] -> !ntensor.ntensor<?xf32>
return %0 : !ntensor.ntensor<?xf32>
Expand Down