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

[Torch] emit aten.ne.str and add folder #3242

Merged
merged 1 commit into from
Apr 27, 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
25 changes: 25 additions & 0 deletions include/torch-mlir/Dialect/Torch/IR/GeneratedTorchOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -13423,6 +13423,31 @@ def Torch_AtenEqStrOp : Torch_Op<"aten.eq.str", [
let hasFolder = 1;
}

def Torch_AtenNeStrOp : Torch_Op<"aten.ne.str", [
AllowsTypeRefinement,
HasValueSemantics,
ReadOnly
]> {
let summary = "Generated op for `aten::ne.str : (str, str) -> (bool)`";
let arguments = (ins
Torch_StringType:$a,
Torch_StringType:$b
);
let results = (outs
Torch_BoolType:$result
);
let hasCustomAssemblyFormat = 1;
let extraClassDefinition = [{
ParseResult AtenNeStrOp::parse(OpAsmParser &parser, OperationState &result) {
return parseDefaultTorchOp(parser, result, 2, 1);
}
void AtenNeStrOp::print(OpAsmPrinter &printer) {
printDefaultTorchOp(printer, *this, 2, 1);
}
}];
let hasFolder = 1;
}

def Torch_AtenLenStrOp : Torch_Op<"aten.len.str", [
AllowsTypeRefinement,
HasValueSemantics,
Expand Down
20 changes: 17 additions & 3 deletions lib/Dialect/Torch/IR/TorchOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2364,14 +2364,28 @@ OpFoldResult AtenEqStrOp::fold(FoldAdaptor adaptor) {
if (getOperand(0) == getOperand(1))
return getI1IntegerAttr(getContext(), true);

auto aStr = getA().getDefiningOp<ConstantStrOp>();
auto bStr = getB().getDefiningOp<ConstantStrOp>();

auto aStr = adaptor.getA();
auto bStr = adaptor.getB();
if (aStr && bStr)
return getI1IntegerAttr(getContext(), aStr == bStr);
return nullptr;
}

//===----------------------------------------------------------------------===//
// AtenNeStrOp
//===----------------------------------------------------------------------===//

OpFoldResult AtenNeStrOp::fold(FoldAdaptor adaptor) {
if (getOperand(0) == getOperand(1))
return getI1IntegerAttr(getContext(), false);

auto aStr = adaptor.getA();
auto bStr = adaptor.getB();
if (aStr && bStr)
return getI1IntegerAttr(getContext(), aStr != bStr);
return nullptr;
}

//===----------------------------------------------------------------------===//
// AtenLtIntOp
//===----------------------------------------------------------------------===//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ def emit_with_mutating_variants(key, **kwargs):
# Str ops.
emit("aten::add.str : (str, str) -> (str)")
emit("aten::eq.str : (str, str) -> (bool)", has_folder=True)
emit("aten::ne.str : (str, str) -> (bool)", has_folder=True)
emit("aten::len.str : (str) -> (int)", has_folder=True)
emit("aten::str : (t) -> (str)")
emit("aten::format : (...) -> (str)")
Expand Down
29 changes: 29 additions & 0 deletions test/Dialect/Torch/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,35 @@ func.func @torch.aten.eq.str$same_value() -> !torch.bool {
return %2 : !torch.bool
}

// CHECK-LABEL: func.func @torch.aten.ne.str$different_value() -> !torch.bool {
// CHECK: %[[FALSE:.*]] = torch.constant.bool true
// CHECK: return %[[FALSE]] : !torch.bool
func.func @torch.aten.ne.str$different_value() -> !torch.bool {
%str4 = torch.constant.str "4"
%str5 = torch.constant.str "5"
%2 = torch.aten.ne.str %str4, %str5 : !torch.str, !torch.str -> !torch.bool
return %2 : !torch.bool
}

// CHECK-LABEL: func.func @torch.aten.ne.str$same_operand(
// CHECK-SAME: %{{.*}}: !torch.str) -> !torch.bool {
// CHECK-NEXT: %[[F:.*]] = torch.constant.bool false
// CHECK-NEXT: return %[[F]] : !torch.bool
func.func @torch.aten.ne.str$same_operand(%arg0: !torch.str) -> !torch.bool {
%0 = torch.aten.ne.str %arg0, %arg0 : !torch.str, !torch.str -> !torch.bool
return %0 : !torch.bool
}

// CHECK-LABEL: func.func @torch.aten.ne.str$same_value() -> !torch.bool {
// CHECK: %[[TRUE:.*]] = torch.constant.bool false
// CHECK: return %[[TRUE]] : !torch.bool
func.func @torch.aten.ne.str$same_value() -> !torch.bool {
%str4 = torch.constant.str "4"
%str4_0 = torch.constant.str "4"
%2 = torch.aten.ne.str %str4, %str4_0 : !torch.str, !torch.str -> !torch.bool
return %2 : !torch.bool
}

// CHECK-LABEL: func.func @torch.aten.len.str() -> !torch.int {
// CHECK: %[[INT7:.*]] = torch.constant.int 7
// CHECK: return %[[INT7]] : !torch.int
Expand Down
Loading