Skip to content

Commit

Permalink
[Torch Dialect] emit aten::mul and add folder (llvm#3007)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyunqu authored Mar 11, 2024
1 parent a3fe130 commit 229ca3a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
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 @@ -14275,6 +14275,31 @@ def Torch_Aten_SetItemTOp : Torch_Op<"aten._set_item.t", [
}];
}

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

def Torch_AtenDivOp : Torch_Op<"aten.div", [
AllowsTypeRefinement,
HasValueSemantics,
Expand Down
19 changes: 19 additions & 0 deletions lib/Dialect/Torch/IR/TorchOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3346,6 +3346,25 @@ OpFoldResult AtenAddOp::fold(FoldAdaptor adaptor) {
[](double a, double b) -> double { return a + b; });
}

//===----------------------------------------------------------------------===//
// AtenMulOp
//===----------------------------------------------------------------------===//

OpFoldResult AtenMulOp::fold(FoldAdaptor adaptor) {
if (!adaptor.getA() || !adaptor.getB()) {
return nullptr;
}

if (adaptor.getA().isa<IntegerAttr>() && adaptor.getB().isa<IntegerAttr>()) {
return atenBinaryIntOperatorFoldHelper(
adaptor.getOperands(),
[](int64_t a, int64_t b) -> int64_t { return a * b; });
}
return atenBinaryFloatOperatorFoldHelper(
adaptor.getOperands(),
[](double a, double b) -> double { return a * b; });
}

//===----------------------------------------------------------------------===//
// AtenSubOp
//===----------------------------------------------------------------------===//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ def emit_with_mutating_variants(key, **kwargs):
has_canonicalizer=True)
emit("aten::__getitem__.t : (t[], int) -> (t)", has_canonicalizer=True)
emit("aten::_set_item.t : (t[], int, t) -> (t[])")
emit("aten::mul : (Scalar, Scalar) -> (Scalar)", has_folder=True)
emit("aten::div : (Scalar, Scalar) -> (float)", has_folder=True)
emit("aten::add : (Scalar, Scalar) -> (Scalar)", has_folder=True)
emit("aten::sub : (Scalar, Scalar) -> (Scalar)", has_folder=True)
Expand Down

0 comments on commit 229ca3a

Please sign in to comment.