Skip to content

Commit

Permalink
custom printer/parser to keep affine_maps inlined with the bind shape…
Browse files Browse the repository at this point in the history
… ops
  • Loading branch information
sjain-stanford committed May 24, 2024
1 parent 53c84b4 commit 65c2f76
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
4 changes: 1 addition & 3 deletions include/torch-mlir/Dialect/Torch/IR/TorchOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1396,9 +1396,7 @@ def Torch_BindSymbolicShapeOp : Torch_Op<"bind_symbolic_shape", []> {
Builtin_AffineMapAttr:$shape_expressions
);
let results = (outs);
let assemblyFormat = [{
$operand `,` `[` $shape_symbols `]` `,` $shape_expressions attr-dict `:` type($operand)
}];
let hasCustomAssemblyFormat = 1;
}

#endif // TORCH_OPS
46 changes: 46 additions & 0 deletions lib/Dialect/Torch/IR/TorchOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5026,3 +5026,49 @@ LogicalResult InitializeGlobalSlotsOp::verify() {
return emitOpError("expected number of operands to match number of slots");
return success();
}

//===----------------------------------------------------------------------===//
// BindSymbolicShapeOp
//===----------------------------------------------------------------------===//

//
// torch.bind_symbolic_shape %6, [%0, %1, %2], #affine_map<()[s0, s1, s2] ->
// (s0, 3, s1 * 2 + s2)> : !torch.vtensor<[?,3,?],f32>
//

ParseResult BindSymbolicShapeOp::parse(OpAsmParser &parser,
OperationState &result) {
OpAsmParser::UnresolvedOperand operand;
SmallVector<OpAsmParser::UnresolvedOperand> shapeSymbols;
AffineMapAttr shapeExpressions;
Type tensorType;

if (parser.parseOperand(operand) || parser.parseComma() ||
parser.parseLSquare() || parser.parseOperandList(shapeSymbols) ||
parser.parseRSquare() || parser.parseComma() ||
parser.parseAttribute(shapeExpressions, "shape_expressions",
result.attributes) ||
parser.parseOptionalAttrDict(result.attributes) ||
parser.parseColonType(tensorType)) {
return failure();
}

result.addTypes(tensorType);
if (parser.resolveOperand(operand, tensorType, result.operands) ||
parser.resolveOperands(shapeSymbols,
parser.getBuilder().getIntegerType(64),
result.operands)) {
return failure();
}

return success();
}

void BindSymbolicShapeOp::print(OpAsmPrinter &p) {
p << " " << getOperand() << ", [";
llvm::interleaveComma(getShapeSymbols(), p);
p << "], " << "#affine_map<" << getShapeExpressions().getValue() << ">";
p.printOptionalAttrDict((*this)->getAttrs(),
/*elidedAttrs=*/{"shape_expressions"});
p << " : " << getOperand().getType();
}

0 comments on commit 65c2f76

Please sign in to comment.