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

Add parser for StableHLO_Dim #546

Merged
merged 1 commit into from
Nov 16, 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
15 changes: 9 additions & 6 deletions stablehlo/dialect/StablehloAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ limitations under the License.
include "mlir/IR/OpBase.td"
include "mlir/IR/TensorEncoding.td"

def StableHLO_Dim : ArrayRefParameter<"int64_t", "Dimension">;
def StableHLO_Dim : ArrayRefParameter<"int64_t", "Dimension"> {
let parser = "mlir::stablehlo::parseIntArray($_parser)";
let printer = "mlir::stablehlo::printIntArray($_printer, $_self)";
}
atondwal marked this conversation as resolved.
Show resolved Hide resolved

def StableHLO_ScatterDimensionNumbers : AttrDef<StableHLO_Dialect, "ScatterDimensionNumbers"> {
let cppNamespace = "::mlir::stablehlo";
Expand Down Expand Up @@ -98,11 +101,11 @@ def OutputOperandAlias : AttrDef<StableHLO_Dialect, "OutputOperandAlias"> {
"int64_t":$operandIndex,
StableHLO_Dim:$operandTupleIndices
);
let assemblyFormat = "`<` "
"`output_tuple_indices` `=` `[` $outputTupleIndices `]` `,`"
"`operand_index` `=` $operandIndex `,`"
"`operand_tuple_indices` `=` `[` $operandTupleIndices `]`"
"`>`";
let assemblyFormat = [{
`<` `output_tuple_indices` `=` $outputTupleIndices `,`
`operand_index` `=` $operandIndex `,`
`operand_tuple_indices` `=` $operandTupleIndices `>`
}];
}

def StableHLO_ArgResultAlias : AttrDef<StableHLO_Dialect, "ArgResultAlias"> {
Expand Down
12 changes: 12 additions & 0 deletions stablehlo/dialect/StablehloOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4509,6 +4509,18 @@ static ParseResult parseDimsWithMinimumElements(AsmParser& parser,
return success();
}

FailureOr<SmallVector<int64_t>> parseIntArray(AsmParser& parser) {
SmallVector<int64_t> ints;
if (failed(parseDims(parser, ints))) return failure();
return ints;
}

void printIntArray(AsmPrinter& printer, ArrayRef<int64_t> ints) {
printer << '[';
llvm::interleaveComma(ints, printer);
printer << ']';
}

/// Parse a custom attribute that resembles a struct of the form
/// <
/// foo = something_parsed_by_custom_parser,
Expand Down
3 changes: 3 additions & 0 deletions stablehlo/dialect/StablehloOps.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ ParseResult parseWindowAttributes(OpAsmParser &parser,
DenseIntElementsAttr &rhsDilation,
DenseElementsAttr &windowReversal);

// Print and parse IntArrays
FailureOr<SmallVector<int64_t>> parseIntArray(AsmParser &parser);
void printIntArray(AsmPrinter &printer, ArrayRef<int64_t> ints);
} // end namespace stablehlo
} // end namespace mlir

Expand Down