1//===- mlir-linalg-ods-yaml-gen.cpp - Linalg ODS generation from yaml ----===//
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 implements an ODS (and C++) generator from a YAML form
10// derived from the mathematical expression of linalg named ops. Typically a
11// math oriented DSL will be used to export the essential representation to
12// this form, and maintaining the SOT at the math level (versus recreating it
13// in MLIR) is deemed to have systemic value.
14//
15//===----------------------------------------------------------------------===//
16
17#include "mlir/AsmParser/AsmParser.h"
18#include "mlir/IR/AffineMap.h"
19#include "mlir/IR/Diagnostics.h"
20#include "mlir/IR/MLIRContext.h"
21#include "mlir/Support/FileUtilities.h"
22#include "mlir/Support/LLVM.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/FormatVariadic.h"
27#include "llvm/Support/ToolOutputFile.h"
28#include "llvm/Support/YAMLTraits.h"
29#include <optional>
30
31using namespace mlir;
32
33using llvm::yaml::Input;
34
35#define DEBUG_TYPE "linalg-ods-gen"
36
37//===----------------------------------------------------------------------===//
38// Mapping structs (correspond to data types in the YAML description).
39// TODO: Since this is a schema/part of the contract, it should be moved to
40// a real header.
41//===----------------------------------------------------------------------===//
42
43namespace {
44
45struct LinalgYAMLContext {
46 MLIRContext *mlirContext;
47};
48
49struct LinalgOpMetadata {
50 std::string name;
51 std::string cppClassName;
52 std::optional<std::string> doc;
53 SmallVector<std::string> implements;
54 SmallVector<std::string> defines;
55};
56
57struct SerializedAffineMap {
58 AffineMapAttr affineMapAttr;
59
60 AffineMap affineMap() { return affineMapAttr.getValue(); }
61};
62
63enum class LinalgOperandDefKind {
64 InputTensor,
65 Scalar,
66 OutputTensor,
67 IndexAttr,
68 UnaryFnAttr,
69 BinaryFnAttr,
70 TernaryFnAttr,
71 TypeFnAttr
72};
73
74struct LinalgOperandDef {
75 std::string name;
76 LinalgOperandDefKind kind;
77 std::optional<std::string> typeVar;
78 std::optional<SerializedAffineMap> shapeMap;
79 std::optional<SerializedAffineMap> indexAttrMap;
80 std::optional<SmallVector<int64_t>> defaultIndices;
81 std::optional<std::string> defaultFn;
82};
83
84enum class LinalgIteratorTypeDef {
85 parallel,
86 reduction,
87};
88
89struct LinalgIndexingMapsConfig {
90 std::optional<SmallVector<SerializedAffineMap>> staticIndexingMaps;
91};
92
93struct ScalarExpression;
94
95enum class ScalarFnKind { Unary, Binary, Ternary, Type };
96
97struct ScalarFn {
98 ScalarFnKind kind;
99 std::optional<std::string> fnName;
100 std::optional<std::string> attrName;
101 std::optional<std::string> typeVar;
102 // NOTE: This must be of arity 1, but to break the self-referential cycle,
103 // we use a heap allocated vector.
104 std::vector<ScalarExpression> operands;
105};
106
107struct ScalarExpression {
108 std::optional<std::string> arg;
109 std::optional<std::string> constant;
110 std::optional<int64_t> index;
111 std::optional<ScalarFn> scalarFn;
112};
113
114struct ScalarAssign {
115 std::string arg;
116 ScalarExpression value;
117};
118
119struct LinalgStructuredOpConfig {
120 SmallVector<LinalgOperandDef, 4> args;
121 LinalgIndexingMapsConfig indexingMaps;
122 SmallVector<LinalgIteratorTypeDef, 4> iteratorTypes;
123 std::vector<ScalarAssign> assignments;
124};
125
126struct LinalgOpConfig {
127 std::optional<LinalgOpMetadata> metadata;
128 std::optional<LinalgStructuredOpConfig> structuredOp;
129};
130
131} // namespace
132
133//===----------------------------------------------------------------------===//
134// Mapping traits.
135//===----------------------------------------------------------------------===//
136
137LLVM_YAML_IS_SEQUENCE_VECTOR(LinalgOperandDef)
138LLVM_YAML_IS_SEQUENCE_VECTOR(SerializedAffineMap)
139LLVM_YAML_IS_SEQUENCE_VECTOR(LinalgIteratorTypeDef)
140LLVM_YAML_IS_SEQUENCE_VECTOR(ScalarAssign)
141LLVM_YAML_IS_SEQUENCE_VECTOR(ScalarExpression)
142LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(LinalgOpConfig)
143
144namespace llvm {
145namespace yaml {
146
147/// Top-level type containing op metadata and one of a concrete op type.
148/// Currently, the only defined op type is `structured_op` (maps to
149/// `LinalgStructuredOpConfig`).
150template <>
151struct MappingTraits<LinalgOpConfig> {
152 static void mapping(IO &io, LinalgOpConfig &info) {
153 io.mapOptional(Key: "metadata", Val&: info.metadata);
154 io.mapOptional(Key: "structured_op", Val&: info.structuredOp);
155 }
156};
157
158/// A structured op models (at most) a single contraction by modeling
159/// - A list of named arguments (`LinalgOperandDef`), which can be inputs,
160/// outputs, or index attributes.
161/// - List of indexing maps (see `LinalgIndexingMaps`).
162/// - Iterator types (see `LinalgIteratorTypeDef`).
163/// - List of scalar level assignment (see `ScalarAssign`).
164template <>
165struct MappingTraits<LinalgStructuredOpConfig> {
166 static void mapping(IO &io, LinalgStructuredOpConfig &info) {
167 io.mapRequired(Key: "args", Val&: info.args);
168 io.mapRequired(Key: "indexing_maps", Val&: info.indexingMaps);
169 io.mapRequired(Key: "iterator_types", Val&: info.iteratorTypes);
170 io.mapRequired(Key: "assignments", Val&: info.assignments);
171 }
172};
173
174/// Maps a named tensor, scalar or attribute argument to an operation,
175/// consisting of:
176/// - `name`: Must be unique within the operation.
177/// - `usage`: How the argument is used (input, output, attribute, etc).
178/// - `type_var`: The symbolic type variable that binds to the element or self
179/// type of the tensor or scalar argument, respectively.
180/// - `shape_map`: An optional AffineMap from all op symbols to the shape of
181/// the argument. Only tensor arguments have a `shape_map`. Each shape must
182/// be normalized over the same list of symbols and have no dimension
183/// inputs.
184/// - `index_attr_map`: An optional AffineMap from all op symbols to the
185/// index attribute symbols. During op creation these symbols are replaced
186/// by the corresponding `name` index attribue values. Only index attribute
187/// arguments have an `index_attr_map`.
188/// - `default_indices`: An optional default initialization for index
189/// attribute arguments.
190/// - `default_fn`: An optional default initialization for function attribute
191/// arguments.
192template <>
193struct MappingTraits<LinalgOperandDef> {
194 static void mapping(IO &io, LinalgOperandDef &info) {
195 io.mapRequired(Key: "name", Val&: info.name);
196 io.mapRequired(Key: "kind", Val&: info.kind);
197 io.mapOptional(Key: "type_var", Val&: info.typeVar);
198 io.mapOptional(Key: "shape_map", Val&: info.shapeMap);
199 io.mapOptional(Key: "index_attr_map", Val&: info.indexAttrMap);
200 io.mapOptional(Key: "default_indices", Val&: info.defaultIndices);
201 io.mapOptional(Key: "default_fn", Val&: info.defaultFn);
202 }
203};
204
205/// Usage enum for a named argument.
206template <>
207struct ScalarEnumerationTraits<LinalgOperandDefKind> {
208 static void enumeration(IO &io, LinalgOperandDefKind &value) {
209 io.enumCase(Val&: value, Str: "input_tensor", ConstVal: LinalgOperandDefKind::InputTensor);
210 io.enumCase(Val&: value, Str: "scalar", ConstVal: LinalgOperandDefKind::Scalar);
211 io.enumCase(Val&: value, Str: "output_tensor", ConstVal: LinalgOperandDefKind::OutputTensor);
212 io.enumCase(Val&: value, Str: "index_attr", ConstVal: LinalgOperandDefKind::IndexAttr);
213 io.enumCase(Val&: value, Str: "unary_fn_attr", ConstVal: LinalgOperandDefKind::UnaryFnAttr);
214 io.enumCase(Val&: value, Str: "binary_fn_attr", ConstVal: LinalgOperandDefKind::BinaryFnAttr);
215 io.enumCase(Val&: value, Str: "ternary_fn_attr", ConstVal: LinalgOperandDefKind::TernaryFnAttr);
216 io.enumCase(Val&: value, Str: "type_fn_attr", ConstVal: LinalgOperandDefKind::TypeFnAttr);
217 }
218};
219
220/// Iterator type enum.
221template <>
222struct ScalarEnumerationTraits<LinalgIteratorTypeDef> {
223 static void enumeration(IO &io, LinalgIteratorTypeDef &value) {
224 io.enumCase(Val&: value, Str: "parallel", ConstVal: LinalgIteratorTypeDef::parallel);
225 io.enumCase(Val&: value, Str: "reduction", ConstVal: LinalgIteratorTypeDef::reduction);
226 }
227};
228
229/// Metadata about the op (name, C++ name, and documentation).
230template <>
231struct MappingTraits<LinalgOpMetadata> {
232 static void mapping(IO &io, LinalgOpMetadata &info) {
233 io.mapRequired(Key: "name", Val&: info.name);
234 io.mapRequired(Key: "cpp_class_name", Val&: info.cppClassName);
235 io.mapOptional(Key: "doc", Val&: info.doc);
236 io.mapOptional(Key: "implements", Val&: info.implements);
237 io.mapOptional(Key: "defines", Val&: info.defines);
238 }
239};
240
241/// How the ops indexing maps are produced. Must be one of:
242/// - static_indexing_maps: A static list of AffineMaps, possibly with
243/// some symbols that bind to attributes of the op. Each indexing map must
244/// be normalized over the same list of dimensions, and its symbols must
245/// match the symbols for argument shapes.
246template <>
247struct MappingTraits<LinalgIndexingMapsConfig> {
248 static void mapping(IO &io, LinalgIndexingMapsConfig &info) {
249 io.mapOptional(Key: "static_indexing_maps", Val&: info.staticIndexingMaps);
250 }
251};
252
253/// Models an assignment to a named output.
254/// - The `arg` name must match a named output.
255/// - The `value` is a scalar expression for computing the value to
256/// assign (see `ScalarExpression`).
257template <>
258struct MappingTraits<ScalarAssign> {
259 static void mapping(IO &io, ScalarAssign &info) {
260 io.mapRequired(Key: "arg", Val&: info.arg);
261 io.mapRequired(Key: "value", Val&: info.value);
262 }
263};
264
265/// A scalar expression (RHS of an assignment). Must be one of:
266/// - `scalar_arg`: An operation argument.
267/// - `scalar_const`: A constant definition.
268/// - `scalar_index`: An iteration index.
269/// - `scalar_fn`: A named function (see `ScalarFn`).
270template <>
271struct MappingTraits<ScalarExpression> {
272 static void mapping(IO &io, ScalarExpression &info) {
273 io.mapOptional(Key: "scalar_arg", Val&: info.arg);
274 io.mapOptional(Key: "scalar_const", Val&: info.constant);
275 io.mapOptional(Key: "scalar_index", Val&: info.index);
276 io.mapOptional(Key: "scalar_fn", Val&: info.scalarFn);
277 }
278};
279
280/// Scalar function kind enum.
281template <>
282struct ScalarEnumerationTraits<ScalarFnKind> {
283 static void enumeration(IO &io, ScalarFnKind &value) {
284 io.enumCase(Val&: value, Str: "unary", ConstVal: ScalarFnKind::Unary);
285 io.enumCase(Val&: value, Str: "binary", ConstVal: ScalarFnKind::Binary);
286 io.enumCase(Val&: value, Str: "ternary", ConstVal: ScalarFnKind::Ternary);
287 io.enumCase(Val&: value, Str: "type", ConstVal: ScalarFnKind::Type);
288 }
289};
290
291/// A scalar expression that evaluates a named function.
292/// Functions are generally "math" level and type polymorphic. Builtin
293/// functions include:
294/// - `add(lhs, rhs)`
295/// - `mul(lhs, rhs)`
296template <>
297struct MappingTraits<ScalarFn> {
298 static void mapping(IO &io, ScalarFn &info) {
299 io.mapRequired(Key: "kind", Val&: info.kind);
300 io.mapOptional(Key: "fn_name", Val&: info.fnName);
301 io.mapOptional(Key: "attr_name", Val&: info.attrName);
302 io.mapOptional(Key: "type_var", Val&: info.typeVar);
303 io.mapRequired(Key: "operands", Val&: info.operands);
304 }
305};
306
307/// Helper mapping which accesses an AffineMapAttr as a serialized string of
308/// the same.
309template <>
310struct ScalarTraits<SerializedAffineMap> {
311 static void output(const SerializedAffineMap &value, void *rawYamlContext,
312 raw_ostream &out) {
313 assert(value.affineMapAttr);
314 value.affineMapAttr.print(os&: out);
315 }
316 static StringRef input(StringRef scalar, void *rawYamlContext,
317 SerializedAffineMap &value) {
318 assert(rawYamlContext);
319 auto *yamlContext = static_cast<LinalgYAMLContext *>(rawYamlContext);
320 if (auto attr = dyn_cast_or_null<AffineMapAttr>(
321 Val: mlir::parseAttribute(attrStr: scalar, context: yamlContext->mlirContext)))
322 value.affineMapAttr = attr;
323 else if (!value.affineMapAttr || !isa<AffineMapAttr>(Val: value.affineMapAttr))
324 return "could not parse as an affine map attribute";
325 return StringRef();
326 }
327 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
328};
329
330} // namespace yaml
331} // namespace llvm
332
333namespace {
334
335//===----------------------------------------------------------------------===//
336// Generation utilities
337//===----------------------------------------------------------------------===//
338
339class GenerationContext {
340public:
341 GenerationContext(MLIRContext *context, raw_ostream *odsOut,
342 raw_ostream *defnOut)
343 : context(context), loc(UnknownLoc::get(context)), odsOut(odsOut),
344 defnOut(defnOut) {}
345
346 MLIRContext *getContext() { return context; }
347
348 void setLoc(Location loc) { this->loc = loc; }
349 Location getLoc() { return loc; }
350
351 bool shouldGenerateOds() { return odsOut; }
352 bool shouldGenerateDefns() { return defnOut; }
353
354 raw_ostream &odss() {
355 assert(odsOut && "ODS stream not defined");
356 return *odsOut;
357 }
358
359 raw_ostream &defns() {
360 assert(defnOut && "Definition stream not defined");
361 return *defnOut;
362 }
363
364private:
365 MLIRContext *context;
366 Location loc;
367 raw_ostream *odsOut;
368 raw_ostream *defnOut;
369};
370
371} // namespace
372
373static std::string generateCppExpression(SerializedAffineMap self,
374 StringRef contextName) {
375 std::string printedStr;
376 llvm::raw_string_ostream printedSs(printedStr);
377 self.affineMapAttr.print(os&: printedSs);
378
379 static const char exprFormat[] =
380 R"FMT(llvm::cast<AffineMapAttr>(mlir::parseAttribute("{0}", {1})).getValue())FMT";
381 return llvm::formatv(Fmt: exprFormat, Vals&: printedStr, Vals&: contextName);
382}
383
384template <typename Container>
385static std::string interleaveToString(Container &container,
386 StringRef separator) {
387 std::string result;
388 llvm::raw_string_ostream ss(result);
389 llvm::interleave(container, ss, separator);
390 return result;
391}
392
393static std::optional<int>
394findTensorDefArgIndex(StringRef name, SmallVectorImpl<LinalgOperandDef> &args) {
395 for (const auto &it : llvm::enumerate(First&: args)) {
396 if (it.value().name == name)
397 return it.index();
398 }
399 return std::nullopt;
400}
401
402// Try to map the TypeVar to a predefined or an argument type.
403static std::optional<std::string>
404findTypeValue(StringRef typeVar, SmallVectorImpl<LinalgOperandDef> &args) {
405 // Handle all predefined types.
406 if (typeVar == "I32")
407 return std::string("helper.getIntegerType(32)");
408 if (typeVar == "I64")
409 return std::string("helper.getIntegerType(64)");
410 if (typeVar == "F32")
411 return std::string("helper.getFloat32Type()");
412 if (typeVar == "F64")
413 return std::string("helper.getFloat64Type()");
414
415 // Search all argument types.
416 for (const auto &it : llvm::enumerate(First&: args)) {
417 if (it.value().kind != LinalgOperandDefKind::InputTensor &&
418 it.value().kind != LinalgOperandDefKind::Scalar &&
419 it.value().kind != LinalgOperandDefKind::OutputTensor)
420 continue;
421 if (*it.value().typeVar == typeVar)
422 return llvm::formatv(Fmt: "block.getArgument({0}).getType()", Vals: it.index())
423 .str();
424 }
425
426 return std::nullopt;
427}
428
429static ScalarAssign *findAssignment(StringRef name,
430 std::vector<ScalarAssign> &assignments) {
431 for (auto &assign : assignments) {
432 if (assign.arg == name)
433 return &assign;
434 }
435 return nullptr;
436}
437
438// Return true if the operand is a function attribute.
439static bool isFunctionAttribute(LinalgOperandDefKind kind) {
440 return kind == LinalgOperandDefKind::UnaryFnAttr ||
441 kind == LinalgOperandDefKind::BinaryFnAttr ||
442 kind == LinalgOperandDefKind::TernaryFnAttr ||
443 kind == LinalgOperandDefKind::TypeFnAttr;
444}
445
446// Return true if the operand is an attribute.
447static bool isAttribute(LinalgOperandDefKind kind) {
448 return kind == LinalgOperandDefKind::IndexAttr || isFunctionAttribute(kind);
449}
450
451// Get the enum name for the given operand kind.
452static std::string convertOperandKindToEnumName(LinalgOperandDefKind kind) {
453 switch (kind) {
454 case LinalgOperandDefKind::UnaryFnAttr:
455 return std::string("UnaryFn");
456 case LinalgOperandDefKind::BinaryFnAttr:
457 return std::string("BinaryFn");
458 case LinalgOperandDefKind::TernaryFnAttr:
459 return std::string("TernaryFn");
460 case LinalgOperandDefKind::TypeFnAttr:
461 return std::string("TypeFn");
462 default:
463 break;
464 }
465 llvm_unreachable("unsupported function attribute kind");
466}
467
468// Get the enum name for the given function kind.
469static std::string convertFunctionKindToEnumName(ScalarFnKind kind) {
470 switch (kind) {
471 case ScalarFnKind::Unary:
472 return std::string("UnaryFn");
473 case ScalarFnKind::Binary:
474 return std::string("BinaryFn");
475 case ScalarFnKind::Ternary:
476 return std::string("TernaryFn");
477 case ScalarFnKind::Type:
478 return std::string("TypeFn");
479 }
480 llvm_unreachable("unsupported function kind");
481}
482
483//===----------------------------------------------------------------------===//
484// Templates
485//===----------------------------------------------------------------------===//
486
487// A single line banner format. Parameters:
488// {0}: Single line comment
489static const char bannerFormat[] = R"FMT(
490//===----------------------------------------------------------------------===//
491// {0}
492//===----------------------------------------------------------------------===//
493)FMT";
494
495//===----------------------------------------------------------------------===//
496// Named generic op generation.
497// These ops map at most a single contraction that complies with the limitations
498// of a linalg.generic.
499//===----------------------------------------------------------------------===//
500
501// Template for Linalg named ops' ODS definitions. Parameters:
502// {0}: ODS/C++ op name
503// {1}: assembly op mnemonic
504// {2}: op interface list
505// {3}: documentation (summary + description)
506// {4}: op attribute list
507// {5}: builder methods taking standalone attribute parameters
508// {6}: additional method definitions
509// {7}: additional methods for attributes used by indexing maps
510static const char structuredOpOdsHeaderFormat[] = R"FMT(
511//===----------------------------------------------------------------------===//
512// Op definition for {0}
513//===----------------------------------------------------------------------===//
514
515def {0} : LinalgStructuredBase_Op<"{1}", !listconcat([AttrSizedOperandSegments],
516 /*extraInterfaces=*/[{2}])> {
517 {3}
518 let arguments = (ins
519 Variadic<AnyType>:$inputs,
520 Variadic<AnyShaped>:$outputs{4}
521 );
522 let results = (outs Variadic<AnyRankedTensor>:$result_tensors);
523 let regions = (region AnyRegion:$region);
524
525 let skipDefaultBuilders = 1;
526 let builders = [
527 OpBuilder<
528 (ins "ValueRange":$inputs, "ValueRange":$outputs,
529 CArg<"ArrayRef<NamedAttribute>", "{{}">:$attributes),
530 [{{
531 buildStructuredOp($_builder, $_state, std::nullopt, inputs, outputs,
532 attributes, {0}::getRegionBuilder());
533 }]>,
534 OpBuilder<
535 (ins "TypeRange":$resultTensorTypes, "ValueRange":$inputs,
536 "ValueRange":$outputs,
537 CArg<"ArrayRef<NamedAttribute>", "{{}">:$attributes),
538 [{{
539 buildStructuredOp($_builder, $_state, resultTensorTypes,
540 inputs, outputs, attributes, {0}::getRegionBuilder());
541 }]>,
542 OpBuilder<
543 (ins "TypeRange":$resultTensorTypes, "ValueRange":$operands,
544 CArg<"ArrayRef<NamedAttribute>", "{{}">:$attributes),
545 [{{
546 $_state.addOperands(operands);
547 $_state.addAttributes(attributes);
548 $_state.addTypes(resultTensorTypes);
549 (void)$_state.addRegion();
550 }]>
551 {5}
552 ];
553 let hasCustomAssemblyFormat = 1;
554 let hasFolder = 1;
555 {6}
556
557 let extraClassDeclaration = structuredOpsBaseDecls # [{{
558 // Auto-generated.
559 SmallVector<utils::IteratorType> getIteratorTypesArray();
560 ArrayAttr getIndexingMaps();
561 static void regionBuilder(ImplicitLocOpBuilder &b,
562 Block &block, ArrayRef<NamedAttribute> attrs,
563 function_ref<InFlightDiagnostic()> emitError);
564 static std::function<void(ImplicitLocOpBuilder &,
565 Block &, ArrayRef<NamedAttribute>, function_ref<InFlightDiagnostic()> emitError)>
566 getRegionBuilder() {{
567 return regionBuilder;
568 }
569
570 ::mlir::MutableOperandRange getDpsInitsMutable() {{
571 return getOutputsMutable();
572 }
573
574 // Generic methods.
575 static unsigned getNumRegionArgs();
576 std::string getLibraryCallName();
577 {7}
578 }];
579}
580)FMT";
581
582// Builder method taking attribute parameters. Parameters:
583// {0}: Class name
584// {1}: Comma interleaved attribute parameters
585// {2}: Attribute initialization
586static const char structuredOpBuilderFormat[] = R"FMT(
587 , OpBuilder<
588 (ins "TypeRange":$resultTensorTypes, "ValueRange":$inputs,
589 "ValueRange":$outputs, {1},
590 CArg<"ArrayRef<NamedAttribute>", "{{}">:$attributes),
591 [{{
592 {2}
593 buildStructuredOp($_builder, $_state, resultTensorTypes, inputs, outputs,
594 attributes, {0}::getRegionBuilder());
595 }]>
596)FMT";
597
598// The getIteratorTypesArray() method for structured ops. Parameters:
599// {0}: Class name
600// {1}: Comma interleaved iterator type names.
601static const char structuredOpIteratorTypesFormat[] =
602 R"FMT(
603SmallVector<utils::IteratorType> {0}::getIteratorTypesArray() {{
604 return SmallVector<utils::IteratorType>{{ {1} };
605}
606)FMT";
607
608// The getIteratorTypesArray() method for rank polymorphic structured ops.
609// Parameters:
610// {0}: Class name
611static const char rankPolyStructuredOpIteratorTypesFormat[] =
612 R"FMT(
613SmallVector<utils::IteratorType> {0}::getIteratorTypesArray() {{
614 int64_t rank = getRank(getDpsInitOperand(0));
615 return SmallVector<utils::IteratorType>(rank, utils::IteratorType::parallel);
616}
617)FMT";
618
619// The indexing_maps() method for structured ops. Parameters:
620// {0}: Class name
621// {1}: Comma-separated list of dimension variable names.
622// {2}: Statements
623static const char structuredOpIndexingMapsFormat[] = R"FMT(
624ArrayAttr {0}::getIndexingMaps() {{
625 static const char memoizeAttr[] = "linalg.memoized_indexing_maps";
626 ArrayAttr cached =
627 getOperation()->getDiscardableAttrOfType<ArrayAttr>(memoizeAttr);
628 if (cached)
629 return cached;
630
631 MLIRContext *context = getContext();
632 auto symbolBindings = getSymbolBindings(*this);
633 SmallVector<AffineMap> maps;
634 {1}
635 cached = Builder(context).getAffineMapArrayAttr(maps);
636 getOperation()->setDiscardableAttr(memoizeAttr, cached);
637 return cached;
638}
639)FMT";
640
641// The indexing_maps() method for rank polymorphic structured ops. Parameters:
642// {0}: Class name
643static const char rankPolyStructuredOpIndexingMapsFormat[] = R"FMT(
644ArrayAttr {0}::getIndexingMaps() {{
645 MLIRContext *context = getContext();
646 AffineMap scalarMap = AffineMap::get(getNumParallelLoops(), 0, context);
647 AffineMap tensorMap = AffineMap::getMultiDimIdentityMap(
648 getNumParallelLoops(), context);
649 SmallVector<AffineMap> indexingMaps;
650 for (OpOperand &opOperand : getOperation()->getOpOperands())
651 indexingMaps.push_back(getRank(&opOperand) == 0 ? scalarMap : tensorMap);
652 return Builder(getContext()).getAffineMapArrayAttr(indexingMaps);
653}
654)FMT";
655
656// Implementations of fold, getEffects and getSpeculatability.
657// Parameters:
658// {0}: Class name
659const char structuredOpFoldersFormat[] = R"FMT(
660LogicalResult {0}::fold(FoldAdaptor,
661 SmallVectorImpl<OpFoldResult> &) {{
662 return memref::foldMemRefCast(*this);
663}
664void {0}::getEffects(SmallVectorImpl<
665 SideEffects::EffectInstance<MemoryEffects::Effect> >&effects) {{
666 if (hasPureTensorSemantics()) return;
667 getGenericEffectsImpl(effects, cast<LinalgOp>(getOperation()));
668}
669Speculation::Speculatability {0}::getSpeculatability() {{
670 return getGenericSpeculatabilityImpl(cast<LinalgOp>(getOperation()));
671}
672)FMT";
673
674// Implementation of parse/print.
675// Parameters:
676// {0}: Class name
677static const char structuredOpParserFormat[] = R"FMT(
678ParseResult {0}::parse(OpAsmParser &parser, OperationState &result) {{
679 return ::parseNamedStructuredOp(parser, result,
680 {0}::getNumRegionArgs(), {0}::getRegionBuilder());
681}
682void {0}::print(OpAsmPrinter &p) {{
683 SmallVector<StringRef, 3> elidedAttrs = {{"operandSegmentSizes",
684 "linalg.memoized_indexing_maps"};
685 ::printNamedStructuredOp(p, getOperation(), getInputs(), getOutputs(),
686 elidedAttrs);
687}
688)FMT";
689
690static LogicalResult generateNamedGenericOpOds(LinalgOpConfig &opConfig,
691 GenerationContext &genContext) {
692 if (!genContext.shouldGenerateOds())
693 return success();
694
695 raw_ostream &os = genContext.odss();
696
697 std::string interfaceNameList;
698 std::string attrList;
699 std::string attrMethods;
700 std::string attrBuilder;
701
702 std::string doc;
703 if (opConfig.metadata->doc) {
704 static const char structuredOpDocFmt[] = R"FMT(
705 let summary = [{{{0}}];
706 let description = [{{{1}}];
707)FMT";
708 StringRef summary, description;
709 std::tie(args&: summary, args&: description) =
710 StringRef(*opConfig.metadata->doc).trim().split(Separator: "\n\n");
711
712 doc = llvm::formatv(Fmt: structuredOpDocFmt, Vals: summary.trim(), Vals: description.trim());
713 }
714
715 interfaceNameList = interleaveToString(container&: opConfig.metadata->implements, separator: ", ");
716
717 std::string definitionList;
718 for (const std::string &definition : opConfig.metadata->defines) {
719 static const char definitionFmt[] = "let {0} = 1;\n";
720 definitionList.append(str: llvm::formatv(Fmt: definitionFmt, Vals: definition));
721 }
722
723 if (llvm::any_of(Range&: opConfig.structuredOp->args, P: [](LinalgOperandDef &arg) {
724 return isAttribute(kind: arg.kind);
725 })) {
726 SmallVector<std::string> attrDefs;
727 SmallVector<std::string> attrParams;
728 SmallVector<std::string> attrStmts;
729 for (LinalgOperandDef &arg : opConfig.structuredOp->args) {
730 static const char paramFmt[] = "\"Attribute\":${0}";
731 static const char stmtFmt[] = "$_state.addAttribute(\"{0}\", {0});";
732 // Add the type conversion attributes to the op definition and builders.
733 if (isFunctionAttribute(kind: arg.kind)) {
734 assert(arg.defaultFn);
735 std::string enumName = convertOperandKindToEnumName(kind: arg.kind);
736 static const char typeFmt[] = "{0}::{1}";
737 static const char defFmt[] =
738 "DefaultValuedOptionalAttr<{0}, \"{1}\">:${2}";
739 attrDefs.push_back(Elt: llvm::formatv(
740 Fmt: defFmt, Vals: llvm::formatv(Fmt: "{0}Attr", Vals&: enumName),
741 Vals: llvm::formatv(Fmt: typeFmt, Vals&: enumName, Vals&: arg.defaultFn), Vals&: arg.name));
742 attrParams.push_back(Elt: llvm::formatv(Fmt: paramFmt, Vals&: arg.name));
743 attrStmts.push_back(Elt: llvm::formatv(Fmt: stmtFmt, Vals&: arg.name));
744 }
745 // Add the index attributes to the op definition and builders.
746 if (arg.kind == LinalgOperandDefKind::IndexAttr) {
747 assert(arg.indexAttrMap.has_value());
748 assert(arg.defaultIndices.has_value());
749 size_t size = arg.indexAttrMap->affineMap().getNumResults();
750 assert(arg.defaultIndices->size() == size);
751 static const char typeFmt[] = "RankedI64ElementsAttr<[{0}]>";
752 static const char defFmt[] =
753 "DefaultValuedOptionalAttr<{0}, \"{ {1} }\">:${2}";
754 std::string defaultVals;
755 llvm::raw_string_ostream ss(defaultVals);
756 llvm::interleave(
757 c: *arg.defaultIndices, os&: ss,
758 each_fn: [&](int64_t val) { ss << "static_cast<int64_t>(" << val << ")"; },
759 separator: ", ");
760 attrDefs.push_back(Elt: llvm::formatv(Fmt: defFmt, Vals: llvm::formatv(Fmt: typeFmt, Vals&: size),
761 Vals&: ss.str(), Vals&: arg.name));
762 attrParams.push_back(Elt: llvm::formatv(Fmt: paramFmt, Vals&: arg.name));
763 attrStmts.push_back(Elt: llvm::formatv(Fmt: stmtFmt, Vals&: arg.name));
764 }
765 }
766 if (llvm::any_of(Range&: opConfig.structuredOp->args, P: [](LinalgOperandDef &arg) {
767 return arg.kind == LinalgOperandDefKind::IndexAttr;
768 })) {
769 attrMethods = R"(
770 bool hasDynamicIndexingMaps();
771 LogicalResult verifyIndexingMapRequiredAttributes();
772 )";
773 }
774 attrList = ",\n" + llvm::join(R&: attrDefs, Separator: ",\n");
775 attrBuilder = llvm::formatv(
776 Fmt: structuredOpBuilderFormat, Vals&: opConfig.metadata->cppClassName,
777 Vals: llvm::join(R&: attrParams, Separator: ", "), Vals: llvm::join(R&: attrStmts, Separator: "\n"));
778 }
779
780 os << llvm::formatv(Fmt: structuredOpOdsHeaderFormat,
781 Vals&: opConfig.metadata->cppClassName, Vals&: opConfig.metadata->name,
782 Vals&: interfaceNameList, Vals&: doc, Vals&: attrList, Vals&: attrBuilder,
783 Vals&: definitionList, Vals&: attrMethods);
784
785 return success();
786}
787
788static LogicalResult
789generateNamedGenericOpDefns(LinalgOpConfig &opConfig,
790 GenerationContext &genContext) {
791 if (!genContext.shouldGenerateDefns())
792 return success();
793
794 raw_ostream &os = genContext.defns();
795 StringRef className = opConfig.metadata->cppClassName;
796
797 // Implementation banner.
798 std::string bannerComment = llvm::formatv(Fmt: "Implementation of {0}", Vals&: className);
799 os << llvm::formatv(Fmt: bannerFormat, Vals&: bannerComment);
800
801 // Compute the number of scalar and tensor arguments.
802 int64_t numOfArgs =
803 llvm::count_if(Range&: opConfig.structuredOp->args, P: [](LinalgOperandDef &arg) {
804 return arg.kind == LinalgOperandDefKind::InputTensor ||
805 arg.kind == LinalgOperandDefKind::Scalar ||
806 arg.kind == LinalgOperandDefKind::OutputTensor;
807 });
808
809 // An operation that accesses only scalars and scalar/rank zero tensors is
810 // rank polymorhpic. We implement rank polymorphism by generating different
811 // indexing maps and iterators that match the rank of the first output tensor.
812 // An operation is rank polymorphic if the iteration domain has rank zero.
813 bool isRankPolymorphic = opConfig.structuredOp->iteratorTypes.empty();
814
815 // Generate the iterator_types() method.
816 if (!isRankPolymorphic) {
817 std::string iteratorsStr;
818 llvm::raw_string_ostream ss(iteratorsStr);
819 llvm::interleaveComma(c: opConfig.structuredOp->iteratorTypes, os&: ss,
820 each_fn: [&](LinalgIteratorTypeDef it) {
821 switch (it) {
822 case LinalgIteratorTypeDef::parallel:
823 ss << "utils::IteratorType::parallel";
824 break;
825 case LinalgIteratorTypeDef::reduction:
826 ss << "utils::IteratorType::reduction";
827 break;
828 }
829 });
830 os << llvm::formatv(Fmt: structuredOpIteratorTypesFormat, Vals&: className,
831 Vals&: iteratorsStr);
832 } else {
833 os << llvm::formatv(Fmt: rankPolyStructuredOpIteratorTypesFormat, Vals&: className);
834 }
835
836 // Generating the getIndexingMaps() method.
837 if (auto &staticMaps =
838 opConfig.structuredOp->indexingMaps.staticIndexingMaps) {
839 if (staticMaps->empty())
840 return emitError(loc: genContext.getLoc()) << "op has no indexing maps";
841 if (!isRankPolymorphic) {
842 AffineMap firstMap = staticMaps->front().affineMap();
843
844 // Symbol bindings.
845 {
846 // For each symbol, generate a declaration for it, either with an
847 // AffineSymbolExpr or an AffineConstantExpr (if the symbol derives from
848 // an attribute).
849 // TODO: Possibly lift into a top-level method.
850 static const char structuredOpSymbolBindingsFormat[] = R"FMT(
851static SmallVector<AffineExpr> getSymbolBindings({0} self) {
852 MLIRContext *context = self.getContext();
853 SmallVector<AffineExpr> exprs;
854{1}
855 return exprs;
856}
857)FMT";
858
859 unsigned symbolCount = firstMap.getNumSymbols();
860 SmallVector<std::string> symbolBindings;
861 for (unsigned i = 0; i < symbolCount; ++i) {
862 symbolBindings.push_back(Elt: llvm::formatv(
863 Fmt: " exprs.push_back(getAffineSymbolExpr({0}, context));", Vals&: i));
864 }
865
866 // Access an index attribute. Parameters:
867 // {0}: Attribute name
868 // {1}: Symbol position
869 // {2}: Attribute index
870 static const char structuredOpAccessAttrFormat[] = R"FMT(
871int64_t cst{1} = self.get{0}().getValues<int64_t>()[{2}];
872exprs.push_back(getAffineConstantExpr(cst{1}, context));
873)FMT";
874 // Update all symbol bindings mapped to an attribute.
875 for (LinalgOperandDef &arg : opConfig.structuredOp->args) {
876 if (arg.kind != LinalgOperandDefKind::IndexAttr)
877 continue;
878 assert(arg.indexAttrMap);
879 for (auto [idx, result] :
880 llvm::enumerate(First: arg.indexAttrMap->affineMap().getResults())) {
881 if (auto symbol = dyn_cast<AffineSymbolExpr>(Val: result)) {
882 std::string argName = arg.name;
883 argName[0] = toupper(c: argName[0]);
884 symbolBindings[symbol.getPosition()] =
885 llvm::formatv(Fmt: structuredOpAccessAttrFormat, Vals&: argName,
886 Vals: symbol.getPosition(), Vals&: idx);
887 }
888 }
889 }
890
891 std::string symbolBindingsStr;
892 llvm::raw_string_ostream symbolBindingsSs(symbolBindingsStr);
893 llvm::interleave(c: symbolBindings, os&: symbolBindingsSs, separator: "\n");
894
895 os << llvm::formatv(Fmt: structuredOpSymbolBindingsFormat, Vals&: className,
896 Vals&: symbolBindingsStr);
897 }
898
899 // Indexing maps.
900 {
901 unsigned dimCount = firstMap.getNumDims();
902
903 // Generate a comma-separated list of dim identifiers to be passed to
904 // bindDims, ensuring tht AffineExpr identifiers are bound in the right
905 // order to the proper AffineDimExpr.
906 // This results in vars in scope like: d0, d1, d2...
907 SmallVector<unsigned> dimIndices;
908 for (unsigned i = 0; i < dimCount; ++i)
909 dimIndices.push_back(Elt: i);
910 std::string dimIdentsStr;
911 llvm::raw_string_ostream dimIdentsSs(dimIdentsStr);
912 llvm::interleaveComma(c: dimIndices, os&: dimIdentsSs,
913 each_fn: [&](unsigned i) { dimIdentsSs << "d" << i; });
914
915 // Statements to add and simplify each affine map.
916 SmallVector<std::string> stmts;
917 for (auto &indexingMap : *staticMaps) {
918 // TODO: Assert that dim and symbol count match the first.
919 stmts.push_back(
920 Elt: llvm::formatv(Fmt: "maps.push_back({0});",
921 Vals: generateCppExpression(self: indexingMap, contextName: "context")));
922 stmts.push_back(Elt: llvm::formatv(
923 Fmt: "maps.back() = "
924 "simplifyAffineMap(maps.back().replaceDimsAndSymbols({{}, "
925 "symbolBindings, {0}, 0));",
926 Vals&: dimCount));
927 }
928
929 // TODO: This needs to be memoized and/or converted to non-parser based
930 // C++ codegen prior to real use.
931 os << llvm::formatv(Fmt: structuredOpIndexingMapsFormat, Vals&: className,
932 Vals: interleaveToString(container&: stmts, separator: "\n "));
933 }
934 } else {
935 os << llvm::formatv(Fmt: rankPolyStructuredOpIndexingMapsFormat, Vals&: className);
936 }
937 } else {
938 return emitError(loc: genContext.getLoc())
939 << "generating code for non static indexing maps not currently "
940 "supported";
941 }
942
943 // getNumRegionArgs()
944 {
945 // Generates a getNumRegionArgs() method. Parameters:
946 // {0}: Class name
947 // {1}: Number of region args
948 static const char structuredOpGetNumRegionArgsFormat[] = R"FMT(
949unsigned {0}::getNumRegionArgs() {{ return {1}; }
950)FMT";
951 os << llvm::formatv(Fmt: structuredOpGetNumRegionArgsFormat, Vals&: className,
952 Vals&: numOfArgs);
953 }
954
955 // getLibraryCallName()
956 {
957 // Generates a getLibraryCallName method. Parameters:
958 // {0}: Class name
959 static const char structuredOpGetLibraryCallFormat[] = R"FMT(
960std::string {0}::getLibraryCallName() {{
961 return generateLibraryCallName(getOperation());
962}
963)FMT";
964 os << llvm::formatv(Fmt: structuredOpGetLibraryCallFormat, Vals&: className);
965 }
966
967 // hasDynamicIndexingMaps() and verifyIndexingMapRequiredAttributes()
968 if (llvm::any_of(Range&: opConfig.structuredOp->args, P: [](LinalgOperandDef &arg) {
969 return arg.kind == LinalgOperandDefKind::IndexAttr;
970 })) {
971 std::vector<std::string> attrVerifications;
972 for (LinalgOperandDef &arg : opConfig.structuredOp->args) {
973 if (arg.kind != LinalgOperandDefKind::IndexAttr)
974 continue;
975 assert(arg.indexAttrMap);
976 // Verify index attribute. Paramters:
977 // {0}: Attribute name
978 // {1}: Attribute size
979 static const char attrFmt[] = R"FMT(
980if (auto attr = op->getInherentAttrOfType<DenseElementsAttr>("{0}")) {{
981 if (!attr.getType().getElementType().isInteger(64))
982 return op->emitError("incorrect element type for index attribute '{0}'");
983 if (attr.getType().getShape() != ArrayRef<int64_t>{{ {1} })
984 return op->emitError("incorrect shape for index attribute '{0}'");
985}
986)FMT";
987 attrVerifications.push_back(x: llvm::formatv(
988 Fmt: attrFmt, Vals&: arg.name, Vals: arg.indexAttrMap->affineMap().getNumResults()));
989 }
990
991 // Generates the verifyIndexingMapRequiredAttributes method. Parameters:
992 // {0}: Class name
993 // {1}: Attribute verification
994 static const char structuredOpVerifyIndexingMapRequiredAttributes[] = R"FMT(
995bool {0}::hasDynamicIndexingMaps() {{ return true; }
996LogicalResult {0}::verifyIndexingMapRequiredAttributes() {{
997 Operation *op = getOperation();
998 {1}
999 return success();
1000}
1001)FMT";
1002 os << llvm::formatv(Fmt: structuredOpVerifyIndexingMapRequiredAttributes,
1003 Vals&: className, Vals: llvm::join(R&: attrVerifications, Separator: "\n"));
1004 }
1005
1006 // regionBuilder()
1007 {
1008 // Generates a regionBuilder method. Parameters.
1009 // {0}: Class name
1010 // {1}: Number of args
1011 // {2}: Attributes
1012 // {3}: Statements
1013 static const char structuredOpRegionBuilderFormat[] = R"FMT(
1014void {0}::regionBuilder(ImplicitLocOpBuilder &b,
1015 Block &block, ArrayRef<NamedAttribute> attrs,
1016 function_ref<InFlightDiagnostic()> emitError) {{
1017 assert({1} > 0 && block.getNumArguments() == {1} &&
1018 "{0} regionBuilder expects {1} (>=0) args");
1019 RegionBuilderHelper helper(b, block);
1020 SmallVector<Value> yields;
1021 {2}
1022 {3}
1023 helper.yieldOutputs(yields);
1024}
1025)FMT";
1026 auto &args = opConfig.structuredOp->args;
1027 auto &assignments = opConfig.structuredOp->assignments;
1028 size_t generatedAssignmentCount = 0;
1029 int localCounter = 0;
1030 SmallVector<std::string> attrs;
1031 SmallVector<std::string> stmts;
1032 for (LinalgOperandDef &arg : args) {
1033 if (!isFunctionAttribute(kind: arg.kind))
1034 continue;
1035 // Obtain the type function attribute values. Parameters.
1036 // {0}: enum name
1037 // {1}: attribute name
1038 // {2}: default type function name
1039 static const char attrDef[] = R"FMT(
1040 {0} {1}Val = {0}::{2};
1041 auto {1}Iter = llvm::find_if(attrs, [&](const NamedAttribute &attr) {{
1042 return attr.getName() == "{1}"; });
1043 if ({1}Iter != attrs.end()) {{
1044 if (auto attr = llvm::dyn_cast<{0}Attr>({1}Iter->getValue()))
1045 {1}Val = attr.getValue();
1046 }
1047)FMT";
1048 std::string enumName = convertOperandKindToEnumName(kind: arg.kind);
1049 attrs.push_back(
1050 Elt: llvm::formatv(Fmt: attrDef, Vals&: enumName, Vals&: arg.name, Vals&: arg.defaultFn));
1051 }
1052 for (LinalgOperandDef &arg : args) {
1053 if (arg.kind != LinalgOperandDefKind::OutputTensor)
1054 continue;
1055
1056 // Find the assignment that correlates with the argument.
1057 ScalarAssign *assignment = findAssignment(name: arg.name, assignments);
1058 if (!assignment)
1059 return emitError(loc: genContext.getLoc())
1060 << "no assignment found for output argument " << arg.name;
1061 ++generatedAssignmentCount;
1062
1063 // Recursively generate the expression.
1064 std::function<std::optional<std::string>(ScalarExpression &)>
1065 generateExpression =
1066 [&](ScalarExpression &expression) -> std::optional<std::string> {
1067 if (expression.arg) {
1068 // Argument reference.
1069 std::optional<int> argIndex =
1070 findTensorDefArgIndex(name: *expression.arg, args);
1071 if (!argIndex) {
1072 emitError(loc: genContext.getLoc())
1073 << "scalar argument not defined on the op: " << *expression.arg;
1074 return std::nullopt;
1075 }
1076 return std::string(
1077 llvm::formatv(Fmt: "block.getArgument({0})", Vals&: *argIndex));
1078 }
1079 if (expression.constant) {
1080 std::string cppIdent = llvm::formatv(Fmt: "value{0}", Vals&: ++localCounter);
1081 stmts.push_back(
1082 Elt: llvm::formatv(Fmt: R"FMT(Value {0} = helper.constant("{1}");)FMT",
1083 Vals&: cppIdent, Vals&: expression.constant));
1084 return cppIdent;
1085 }
1086 if (expression.index) {
1087 // Access an iteration index.
1088 std::string cppIdent = llvm::formatv(Fmt: "value{0}", Vals&: ++localCounter);
1089 stmts.push_back(Elt: llvm::formatv(Fmt: "Value {0} = helper.index({1});",
1090 Vals&: cppIdent, Vals&: *expression.index));
1091 return cppIdent;
1092 }
1093 if (expression.scalarFn) {
1094 std::string enumName =
1095 convertFunctionKindToEnumName(kind: expression.scalarFn->kind);
1096
1097 // Get the function or attribute name.
1098 assert(expression.scalarFn->fnName || expression.scalarFn->attrName);
1099 std::string funcType;
1100 if (expression.scalarFn->fnName) {
1101 funcType = llvm::formatv(Fmt: "{0}::{1}", Vals&: enumName,
1102 Vals&: *expression.scalarFn->fnName);
1103 }
1104 if (expression.scalarFn->attrName) {
1105 if (llvm::none_of(Range&: args, P: [&](LinalgOperandDef &arg) {
1106 return isFunctionAttribute(kind: arg.kind) &&
1107 arg.name == *expression.scalarFn->attrName;
1108 })) {
1109 emitError(loc: genContext.getLoc()) << "missing function attribute "
1110 << *expression.scalarFn->attrName;
1111 }
1112 funcType = llvm::formatv(Fmt: "{0}Val", Vals&: *expression.scalarFn->attrName);
1113 }
1114 assert(!funcType.empty());
1115
1116 // Add the optional type parameter to the operands.
1117 SmallVector<std::string> operandCppValues;
1118 if (expression.scalarFn->kind == ScalarFnKind::Type) {
1119 assert(expression.scalarFn->typeVar.has_value());
1120 std::optional<std::string> typeCppValue =
1121 findTypeValue(typeVar: *expression.scalarFn->typeVar, args);
1122 if (!typeCppValue) {
1123 emitError(loc: genContext.getLoc())
1124 << "type variable " << *expression.scalarFn->typeVar
1125 << ", used in a type conversion, must map to a predefined or "
1126 << "an argument type but it does not";
1127 return std::nullopt;
1128 }
1129 operandCppValues.push_back(Elt: *typeCppValue);
1130 }
1131
1132 // Collect the scalar operands.
1133 for (ScalarExpression &operand : expression.scalarFn->operands) {
1134 auto operandCppValue = generateExpression(operand);
1135 if (!operandCppValue)
1136 return std::nullopt;
1137 operandCppValues.push_back(Elt: *operandCppValue);
1138 }
1139
1140 // Call the function builder.
1141 std::string cppIdent = llvm::formatv(Fmt: "value{0}", Vals&: ++localCounter);
1142 stmts.push_back(Elt: llvm::formatv(
1143 Fmt: R"mlir(
1144 Value {0} = helper.build{1}({2}, {3}, emitError);
1145 if (!{0})
1146 return;
1147 )mlir",
1148 Vals&: cppIdent, Vals&: enumName, Vals&: funcType,
1149 Vals: interleaveToString(container&: operandCppValues, separator: ", ")));
1150 return cppIdent;
1151 }
1152 emitError(loc: genContext.getLoc()) << "unknown ScalarExpression type";
1153 return std::nullopt;
1154 };
1155 std::optional<std::string> cppValue =
1156 generateExpression(assignment->value);
1157 if (!cppValue)
1158 return failure();
1159 stmts.push_back(Elt: llvm::formatv(Fmt: "yields.push_back({0});", Vals&: *cppValue));
1160 }
1161
1162 if (generatedAssignmentCount != assignments.size())
1163 return emitError(loc: genContext.getLoc())
1164 << "mismatched number of assignments vs output arguments";
1165
1166 os << llvm::formatv(Fmt: structuredOpRegionBuilderFormat, Vals&: className, Vals&: numOfArgs,
1167 Vals: interleaveToString(container&: attrs, separator: "\n "),
1168 Vals: interleaveToString(container&: stmts, separator: "\n "));
1169 }
1170
1171 // Parser and printer.
1172 os << llvm::formatv(Fmt: structuredOpParserFormat, Vals&: className);
1173
1174 // Canonicalizers and folders.
1175 os << llvm::formatv(Fmt: structuredOpFoldersFormat, Vals&: className);
1176
1177 return success();
1178}
1179
1180static LogicalResult generateOp(LinalgOpConfig &opConfig,
1181 GenerationContext &genContext) {
1182 // Switch on op type being generated.
1183 if (opConfig.structuredOp) {
1184 return success(
1185 IsSuccess: succeeded(Result: generateNamedGenericOpOds(opConfig, genContext)) &&
1186 succeeded(Result: generateNamedGenericOpDefns(opConfig, genContext)));
1187 }
1188 return emitError(loc: genContext.getLoc()) << "unsupported operation type";
1189}
1190
1191//===----------------------------------------------------------------------===//
1192// Command line options and main
1193//===----------------------------------------------------------------------===//
1194
1195static llvm::cl::opt<std::string>
1196 inputFilename(llvm::cl::Positional, llvm::cl::desc("<input file>"),
1197 llvm::cl::init(Val: "-"), llvm::cl::value_desc("YAML filename"));
1198
1199static llvm::cl::opt<std::string>
1200 outputOdsDeclFilename("o-ods-decl", llvm::cl::desc("ODS output filename"),
1201 llvm::cl::value_desc("filename"), llvm::cl::init(Val: ""));
1202
1203static llvm::cl::opt<std::string>
1204 outputCppImplFilename("o-impl",
1205 llvm::cl::desc("C++ implementation file name"),
1206 llvm::cl::value_desc("filename"), llvm::cl::init(Val: ""));
1207
1208int main(int argc, char **argv) {
1209 llvm::cl::ParseCommandLineOptions(argc, argv, Overview: "Linalg ODS Gen from YAML");
1210
1211 // Set up the input file.
1212 std::string errorMessage;
1213 std::unique_ptr<llvm::MemoryBuffer> file =
1214 mlir::openInputFile(inputFilename, errorMessage: &errorMessage);
1215 if (!file) {
1216 llvm::errs() << errorMessage << "\n";
1217 return 1;
1218 }
1219
1220 MLIRContext mlirContext;
1221 LinalgYAMLContext yamlContext{.mlirContext: &mlirContext};
1222
1223 std::vector<LinalgOpConfig> opConfigs;
1224
1225 // Parse input.
1226 Input yin(file->getBuffer(), &yamlContext);
1227 yin >> opConfigs;
1228
1229 if (yin.error())
1230 return 1;
1231
1232 // Open output files.
1233 std::unique_ptr<llvm::ToolOutputFile> outputOdsDecl;
1234 if (!outputOdsDeclFilename.empty()) {
1235 outputOdsDecl = openOutputFile(outputFilename: outputOdsDeclFilename, errorMessage: &errorMessage);
1236 if (!outputOdsDecl) {
1237 llvm::errs() << errorMessage << "\n";
1238 return 1;
1239 }
1240 }
1241
1242 std::unique_ptr<llvm::ToolOutputFile> outputCppImpl;
1243 if (!outputCppImplFilename.empty()) {
1244 outputCppImpl = openOutputFile(outputFilename: outputCppImplFilename, errorMessage: &errorMessage);
1245 if (!outputCppImpl) {
1246 llvm::errs() << errorMessage << "\n";
1247 return 1;
1248 }
1249 }
1250
1251 if (!outputOdsDecl && !outputCppImpl) {
1252 llvm::errs() << "error: No output files specified\n";
1253 return 1;
1254 }
1255
1256 // Generate.
1257 GenerationContext genContext(&mlirContext,
1258 outputOdsDecl ? &outputOdsDecl->os() : nullptr,
1259 outputCppImpl ? &outputCppImpl->os() : nullptr);
1260
1261 for (auto &opConfig : opConfigs) {
1262 if (!opConfig.metadata) {
1263 emitError(loc: genContext.getLoc())
1264 << "missing operation metadata on subsequent op";
1265 return 1;
1266 }
1267
1268 genContext.setLoc(NameLoc::get(
1269 name: StringAttr::get(context: &mlirContext, bytes: opConfig.metadata->cppClassName)));
1270 if (failed(Result: generateOp(opConfig, genContext))) {
1271 return 1;
1272 }
1273 }
1274
1275 if (outputOdsDecl)
1276 outputOdsDecl->keep();
1277 if (outputCppImpl)
1278 outputCppImpl->keep();
1279
1280 return 0;
1281}
1282