1//===- TilingInterface.cpp - Tiling interface -------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the definitions of the interface in `TilingInterface.td`.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Interfaces/TilingInterface.h"
14
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/SmallVectorExtras.h"
17#include "llvm/ADT/StringSwitch.h"
18
19using namespace mlir;
20
21LogicalResult mlir::verifyInnerTileAlignments(Operation *op,
22 ArrayRef<int64_t> alignments) {
23 for (int64_t a : alignments)
24 if (!isValidInnerTileAlignment(value: a))
25 return op->emitOpError()
26 << "expected inner_tile_alignments entries to be one of 0 "
27 "(Unknown), 1 (Multiple) or 2 (Equal), but got "
28 << a;
29 return success();
30}
31
32SmallVector<InnerTileAlignment>
33mlir::convertInnerTileAlignments(ArrayRef<int64_t> alignments) {
34 return llvm::map_to_vector(C&: alignments, F: [](int64_t v) {
35 assert(isValidInnerTileAlignment(v) &&
36 "invalid InnerTileAlignment; should be rejected by the verifier");
37 return static_cast<InnerTileAlignment>(v);
38 });
39}
40
41StringRef mlir::stringifyInnerTileAlignment(InnerTileAlignment alignment) {
42 switch (alignment) {
43 case InnerTileAlignment::Unknown:
44 return "Unknown";
45 case InnerTileAlignment::Multiple:
46 return "Multiple";
47 case InnerTileAlignment::Equal:
48 return "Equal";
49 }
50 llvm_unreachable("unknown InnerTileAlignment");
51}
52
53std::optional<InnerTileAlignment>
54mlir::symbolizeInnerTileAlignment(StringRef keyword) {
55 return llvm::StringSwitch<std::optional<InnerTileAlignment>>(keyword)
56 .Case(S: "Unknown", Value: InnerTileAlignment::Unknown)
57 .Case(S: "Multiple", Value: InnerTileAlignment::Multiple)
58 .Case(S: "Equal", Value: InnerTileAlignment::Equal)
59 .Default(Value: std::nullopt);
60}
61
62ParseResult mlir::parseInnerTileAlignmentArray(OpAsmParser &parser,
63 DenseI64ArrayAttr &alignments) {
64 SmallVector<int64_t> values;
65 auto parseEntry = [&]() -> ParseResult {
66 StringRef keyword;
67 llvm::SMLoc loc = parser.getCurrentLocation();
68 if (parser.parseKeyword(keyword: &keyword))
69 return failure();
70 std::optional<InnerTileAlignment> alignment =
71 symbolizeInnerTileAlignment(keyword);
72 if (!alignment)
73 return parser.emitError(loc)
74 << "expected one of 'Unknown', 'Multiple' or 'Equal', but got '"
75 << keyword << "'";
76 values.push_back(Elt: static_cast<int64_t>(*alignment));
77 return success();
78 };
79 if (parser.parseCommaSeparatedList(delimiter: AsmParser::Delimiter::Square, parseElementFn: parseEntry))
80 return failure();
81 alignments = DenseI64ArrayAttr::get(context: parser.getContext(), content: values);
82 return success();
83}
84
85void mlir::printInnerTileAlignmentArray(OpAsmPrinter &printer, Operation *,
86 DenseI64ArrayAttr alignments) {
87 printer << "[";
88 llvm::interleaveComma(c: alignments.asArrayRef(), os&: printer, each_fn: [&](int64_t value) {
89 printer << stringifyInnerTileAlignment(
90 alignment: static_cast<InnerTileAlignment>(value));
91 });
92 printer << "]";
93}
94
95#include "mlir/Interfaces/TilingInterface.cpp.inc"
96