MLIR 24.0.0git
TosaProfileCompliance.cpp
Go to the documentation of this file.
1//===--- TosaProfileCompliance.cpp - Tosa Profile Compliance Validation ---===//
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
10#include "llvm/ADT/StringExtras.h"
11#include "llvm/Support/Compiler.h"
12#include "llvm/Support/raw_ostream.h"
13
14namespace mlir {
15namespace tosa {
16LLVM_ATTRIBUTE_NOINLINE
17VersionedTypeInfo::VersionedTypeInfo(std::initializer_list<TypeInfoRef> types,
18 SpecificationVersion version)
19 : second(version) {
20 first.reserve(types.size());
21 for (const TypeInfoRef &type : types)
22 first.push_back(*type.ptr);
23}
24
25LLVM_ATTRIBUTE_NOINLINE
27 SpecificationVersion version)
28 : first(std::move(types)), second(version) {}
29
30LLVM_ATTRIBUTE_NOINLINE
32
33LLVM_ATTRIBUTE_NOINLINE
35
36LLVM_ATTRIBUTE_NOINLINE
39
40LLVM_ATTRIBUTE_NOINLINE
43
44LLVM_ATTRIBUTE_NOINLINE
46
47template <typename T>
49 std::initializer_list<T> mode,
50 std::initializer_list<VersionedTypeInfo> operandTypeInfoSet,
53}
54
55template <typename T>
61
62template <typename T>
63LLVM_ATTRIBUTE_NOINLINE
65
66template <typename T>
67LLVM_ATTRIBUTE_NOINLINE
69
70template <typename T>
71LLVM_ATTRIBUTE_NOINLINE OpComplianceInfo<T> &
73
74template <typename T>
75LLVM_ATTRIBUTE_NOINLINE OpComplianceInfo<T> &
77
78template <typename T>
79LLVM_ATTRIBUTE_NOINLINE OpComplianceInfo<T>::~OpComplianceInfo() = default;
80
81template struct OpComplianceInfo<Profile>;
82template struct OpComplianceInfo<Extension>;
83
85 static const TypeInfo boolT = {mlir::IntegerType::getTypeID(), 1};
86 static const TypeInfo i4T = {mlir::IntegerType::getTypeID(), 4};
87 static const TypeInfo i8T = {mlir::IntegerType::getTypeID(), 8};
88 static const TypeInfo i16T = {mlir::IntegerType::getTypeID(), 16};
89 static const TypeInfo i32T = {mlir::IntegerType::getTypeID(), 32};
90 static const TypeInfo i48T = {mlir::IntegerType::getTypeID(), 48};
91 static const TypeInfo i64T = {mlir::IntegerType::getTypeID(), 64};
92 static const TypeInfo bf16T = {mlir::BFloat16Type::getTypeID(), 16};
93 static const TypeInfo fp16T = {mlir::Float16Type::getTypeID(), 16};
94 static const TypeInfo fp32T = {mlir::Float32Type::getTypeID(), 32};
95 static const TypeInfo fp8e4m3T = {mlir::Float8E4M3FNType::getTypeID(), 8};
96 static const TypeInfo fp8e5m2T = {mlir::Float8E5M2Type::getTypeID(), 8};
97
98 // micro-scaling formats
99 // Note: these types exist to support the deprecated block_scaled operations
100 // and can be removed once those operations are removed.
101 static const TypeInfo fp6e2m3T = {mlir::Float6E2M3FNType::getTypeID(), 6};
102 static const TypeInfo fp6e3m2T = {mlir::Float6E3M2FNType::getTypeID(), 6};
103 static const TypeInfo fp4e2m1T = {mlir::Float4E2M1FNType::getTypeID(), 4};
104 static const TypeInfo fp8ue8m0T = {mlir::Float8E8M0FNUType::getTypeID(), 8};
105 static const TypeInfo mxint8T = {mlir::MXI8Type::getTypeID(), 8};
106
107 // Block scaled formats
108 const TypeID blockScaledID = mlir::tosa::BlockScaledType::getTypeID();
109 const TypeID fp4e2m1ID = mlir::Float4E2M1FNType::getTypeID();
110 const TypeID fp6e2m3ID = mlir::Float6E2M3FNType::getTypeID();
111 const TypeID fp6e3m2ID = mlir::Float6E3M2FNType::getTypeID();
112 const TypeID fp8e4m3ID = mlir::Float8E4M3FNType::getTypeID();
113 const TypeID fp8e5m2ID = mlir::Float8E5M2Type::getTypeID();
114 const TypeID fp8ue8m0ID = mlir::Float8E8M0FNUType::getTypeID();
115 const TypeID mxint8ID = mlir::MXI8Type::getTypeID();
116
117 static const TypeInfo bs32_fp8ue8m0_fp4e2m1T = {
118 blockScaledID, 4, fp4e2m1ID, fp8ue8m0ID,
119 tosa::BlockShape::BLOCK_SHAPE_32};
120 static const TypeInfo bs32_fp8ue8m0_fp6e2m3T = {
121 blockScaledID, 6, fp6e2m3ID, fp8ue8m0ID,
122 tosa::BlockShape::BLOCK_SHAPE_32};
123 static const TypeInfo bs32_fp8ue8m0_fp6e3m2T = {
124 blockScaledID, 6, fp6e3m2ID, fp8ue8m0ID,
125 tosa::BlockShape::BLOCK_SHAPE_32};
126 static const TypeInfo bs32_fp8ue8m0_fp8e4m3T = {
127 blockScaledID, 8, fp8e4m3ID, fp8ue8m0ID,
128 tosa::BlockShape::BLOCK_SHAPE_32};
129 static const TypeInfo bs32_fp8ue8m0_fp8e5m2T = {
130 blockScaledID, 8, fp8e5m2ID, fp8ue8m0ID,
131 tosa::BlockShape::BLOCK_SHAPE_32};
132 static const TypeInfo bs32_fp8ue8m0_mxint8T = {
133 blockScaledID, 8, mxint8ID, fp8ue8m0ID, tosa::BlockShape::BLOCK_SHAPE_32};
134
135// The profile-based compliance content below is auto-generated by a script
136// in https://github.com/arm/tosa-specification
138 // End of auto-generated metadata
139}
140
141template <>
144 return profileComplianceMap;
145}
146
147template <>
150 return extensionComplianceMap;
151}
152
153// Base populating function
154LogicalResult ProfileInfoDepot::populateProfileInfo(ValueRange operands,
155 ValueRange results) {
156 for (const auto &operand : operands)
157 addValue(operand);
158 for (const auto &result : results)
160 return success();
161}
162
163template <>
164LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::ConcatOp op) {
165 addValue(op.getInput1().front());
166 addValue(op.getOutput());
167 return success();
168}
169
170template <>
171LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::AvgPool2dOp op) {
172 addValue(op.getInput());
173 addValue(op.getInputZp());
174 addValue(op.getOutputZp());
175 addType(op.getAccType());
176 addValue(op.getOutput());
177 return success();
178}
179
180template <>
181LogicalResult
182ProfileInfoDepot::populateProfileInfo(tosa::AvgPool2dAdaptiveOp op) {
183 addValue(op.getInput());
184 addValue(op.getInputZp());
185 addValue(op.getOutputZp());
186 addType(op.getAccType());
187 addValue(op.getOutput());
188 return success();
189}
190
191template <>
192LogicalResult
193ProfileInfoDepot::populateProfileInfo(tosa::MaxPool2dAdaptiveOp op) {
194 addValue(op.getInput());
195 addValue(op.getOutput());
196 return success();
197}
198
199template <typename T>
200LogicalResult ProfileInfoDepot::populateProfileInfoConv(T op) {
201 addValue(op.getInput());
202 addValue(op.getWeight());
203 addValue(op.getBias());
204 addValue(op.getInputZp());
205 addValue(op.getWeightZp());
206 addType(op.getAccType());
207 addValue(op.getOutput());
208 return success();
209}
210
211template <>
212LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::Conv2DOp op) {
213 return populateProfileInfoConv(op);
214}
215
216template <>
217LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::Conv3DOp op) {
218 return populateProfileInfoConv(op);
219}
220
221template <>
222LogicalResult
223ProfileInfoDepot::populateProfileInfo(tosa::TransposeConv2DOp op) {
224 return populateProfileInfoConv(op);
225}
226
227template <>
228LogicalResult
229ProfileInfoDepot::populateProfileInfo(tosa::DepthwiseConv2DOp op) {
230 return populateProfileInfoConv(op);
231}
232
233template <>
234LogicalResult
235ProfileInfoDepot::populateProfileInfo(tosa::Conv2DBlockScaledOp op) {
236 addValue(op.getInputData());
237 addValue(op.getInputScale());
238 addValue(op.getWeightData());
239 addValue(op.getWeightScale());
240 addValue(op.getBias());
241 addValue(op.getOutput());
242 return success();
243}
244
245template <>
246LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::PadOp op) {
247 addValue(op.getInput1());
248 addValue(op.getPadConst());
249 addValue(op.getOutput());
250 return success();
251}
252
253template <typename T>
254LogicalResult ProfileInfoDepot::populateProfileInfoDataLayout(T op) {
255 addValue(op.getInput1());
256 addValue(op.getOutput());
257 return success();
258}
259
260template <>
261LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::ReshapeOp op) {
262 return populateProfileInfoDataLayout(op);
263}
264
265template <>
266LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::SliceOp op) {
267 return populateProfileInfoDataLayout(op);
268}
269
270template <>
271LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::TileOp op) {
272 return populateProfileInfoDataLayout(op);
273}
274
275template <>
276LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::TransposeOp op) {
277 return populateProfileInfoDataLayout(op);
278}
279
280template <>
281LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::GatherOp op) {
282 addValue(op.getValues());
283 addValue(op.getIndices());
284 addValue(op.getOutput());
285 return success();
286}
287
288template <>
289LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::RowGatherOp op) {
290 addValue(op.getValues());
291 addValue(op.getIndices());
292 addValue(op.getOutput());
293 return success();
294}
295
296template <>
297LogicalResult
298ProfileInfoDepot::populateProfileInfo(tosa::RowGatherBlockScaledOp op) {
299 for (Value value : op.getValues())
300 addValue(value);
301 addValue(op.getIndices());
302 addValue(op.getRowCount());
303 for (Value result : op.getOutput())
305 return success();
306}
307
308template <>
309LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::ScatterOp op) {
310 addValue(op.getValuesIn());
311 addValue(op.getIndices());
312 addValue(op.getInput());
313 addValue(op.getValuesOut());
314 return success();
315}
316
317template <>
318LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::MulOp op) {
319 addValue(op.getInput1());
320 addValue(op.getInput2());
321 addValue(op.getOutput());
322 return success();
323}
324
325template <>
326LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::ResizeOp op) {
327 addValue(op.getInput());
328 addValue(op.getOutput());
329 return success();
330}
331
332template <>
333LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::SelectOp op) {
334 addValue(op.getOnTrue());
335 addValue(op.getOnFalse());
336 addValue(op.getOutput());
337 return success();
338}
339
340template <>
341LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::RescaleOp op) {
342 addValue(op.getInput());
343 addValue(op.getInputZp());
344 addValue(op.getOutputZp());
345 addValue(op.getOutput());
346 return success();
347}
348
349template <>
350LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::MatMulOp op) {
351 addValue(op.getA());
352 addValue(op.getB());
353 addValue(op.getAZp());
354 addValue(op.getBZp());
355 addValue(op.getOutput());
356 return success();
357}
358
359template <>
360LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::MatMulTOp op) {
361 addValue(op.getA());
362 addValue(op.getB());
363 addValue(op.getAZp());
364 addValue(op.getBZp());
365 addValue(op.getOutput());
366 return success();
367}
368
369template <>
370LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::VariableOp op) {
371 addType(op.getType());
372 return success();
373}
374
375template <>
376LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::VariableWriteOp op) {
377 addValue(op.getInput1());
378 return success();
379}
380
381template <>
382LogicalResult ProfileInfoDepot::populateProfileInfo(tosa::DimOp op) {
383 addValue(op.getInput1());
384 return success();
385}
386
387LogicalResult ProfileInfoDepot::populatationDispatch(Operation *op) {
388// This helper function only populates the info for the customised operands.
389#define POPULATE_PROFILE_INFO_CUSTOM(tosaOp) \
390 if (isa<tosa::tosaOp##Op>(op)) { \
391 return populateProfileInfo(cast<tosa::tosaOp##Op>(op)); \
392 }
393
394#define POPULATE_PROFILE_INFO_SKIP(tosaOp) \
395 if (isa<tosa::tosaOp##Op>(op)) \
396 return success();
397
398// This helper function populates the info for all operands.
399#define POPULATE_PROFILE_INFO_COMMON(tosaOp) \
400 if (isa<tosa::tosaOp##Op>(op)) { \
401 return populateProfileInfo(op->getOperands(), op->getResults()); \
402 }
403
404 // Skip irrelevant operands when they are independent and not tied to any
405 // specific profile/extension.
407 POPULATE_PROFILE_INFO_CUSTOM(AvgPool2dAdaptive)
408 POPULATE_PROFILE_INFO_CUSTOM(TransposeConv2D)
410 POPULATE_PROFILE_INFO_CUSTOM(Conv2DBlockScaled)
412 POPULATE_PROFILE_INFO_CUSTOM(DepthwiseConv2D)
422 POPULATE_PROFILE_INFO_CUSTOM(RowGatherBlockScaled)
430 POPULATE_PROFILE_INFO_CUSTOM(VariableWrite)
432 POPULATE_PROFILE_INFO_CUSTOM(MaxPool2dAdaptive)
433
434 // For the most of tosa operators, all operands are profile/extension related
435 // and hence are all considered in this profile-based compilance check.
436 POPULATE_PROFILE_INFO_COMMON(MatmulTBlockScaled)
440 POPULATE_PROFILE_INFO_COMMON(CastFromBlockScaled)
441 POPULATE_PROFILE_INFO_COMMON(CastToBlockScaled)
454 POPULATE_PROFILE_INFO_COMMON(ArithmeticRightShift)
459 POPULATE_PROFILE_INFO_COMMON(LogicalLeftShift)
460 POPULATE_PROFILE_INFO_COMMON(LogicalRightShift)
483 POPULATE_PROFILE_INFO_COMMON(ReduceProduct)
486 POPULATE_PROFILE_INFO_COMMON(GreaterEqual)
490 POPULATE_PROFILE_INFO_COMMON(VariableRead)
491
492 // Type Invariant Extension, a capability extension that is independent
493 // of the data type, meaning any compatible type can be used. No type
494 // constraint for those operations.
496 POPULATE_PROFILE_INFO_SKIP(AssertEqualShape)
497 POPULATE_PROFILE_INFO_SKIP(ConcatShape)
499 POPULATE_PROFILE_INFO_SKIP(DivCeilShape)
500 POPULATE_PROFILE_INFO_SKIP(DivFloorShape)
502 POPULATE_PROFILE_INFO_SKIP(Log2CeilShape)
503 POPULATE_PROFILE_INFO_SKIP(Log2FloorShape)
513
514 return failure();
515}
516
517//===----------------------------------------------------------------------===//
518// Tosa Profile And Extension Compliance Checker
519//===----------------------------------------------------------------------===//
520
521template <typename T>
522FailureOr<SmallVector<OpComplianceInfo<T>>>
523TosaProfileCompliance::getOperatorMatchedEntries(Operation *op) {
524 const std::string opName = op->getName().getStringRef().str();
525 const auto &complianceMap = getProfileComplianceMap<T>();
526 const auto it = complianceMap.find(opName);
527 if (it == complianceMap.end())
528 return {};
529
530 return findMatchedEntries<T>(op, it->second);
531}
532
533template <typename T>
534static bool isModeSupported(const OpComplianceInfo<T> &info,
535 const TargetEnv &targetEnv) {
536 return info.condition == CheckCondition::allOf
537 ? targetEnv.allowsAllOf(info.mode)
538 : targetEnv.allowsAnyOf(info.mode);
539}
540
541template <typename Range>
542static bool satisfiesComplianceRequirements(const Range &entries,
543 const TargetEnv &targetEnv) {
544 return entries.empty() || llvm::any_of(entries, [&](const auto &info) {
545 return isModeSupported(info, targetEnv);
546 });
547}
548
549template <typename Range>
550static auto entriesAtVersion(const Range &entries,
551 SpecificationVersion version) {
552 return llvm::make_filter_range(entries, [version](const auto &info) {
553 return info.operandTypeInfoSet.front().second == version;
554 });
555}
556
557template <typename Range>
558static void emitModeRequirements(llvm::raw_ostream &os, const Range &entries) {
559 llvm::interleave(
560 entries, os,
561 [&](const auto &info) {
562 os << "requires "
563 << (info.condition == CheckCondition::allOf ? "all of " : "any of ")
564 << "[";
565 llvm::interleaveComma(info.mode, os,
566 [&](auto mode) { os << stringifyEnum(mode); });
567 os << "] profiles/extensions ";
568 },
569 "OR ");
570}
571
572template <typename ProfileRange, typename ExtensionRange>
573static LogicalResult
575 const ProfileRange &profiles,
576 const ExtensionRange &extensions) {
577 const TosaSpecificationVersion targetVersion{targetEnv.getSpecVersion()};
578 std::string message;
579 llvm::raw_string_ostream os(message);
580 os << "illegal: ";
581
583 const auto gatherVersions = [&versions](const auto &entries) {
584 for (const auto &info : entries) {
585 if (info.operandTypeInfoSet.empty())
586 continue;
587 const SpecificationVersion version =
588 info.operandTypeInfoSet.front().second;
589 if (!llvm::is_contained(versions, version))
590 versions.push_back(version);
591 }
592 };
593 gatherVersions(profiles);
594 gatherVersions(extensions);
595
596 if (versions.empty()) {
597 // Only untyped mandatory constraints
598 const bool requiresProfile =
599 !satisfiesComplianceRequirements(profiles, targetEnv);
600 if (requiresProfile)
601 emitModeRequirements(os, profiles);
602 if (!satisfiesComplianceRequirements(extensions, targetEnv)) {
603 if (requiresProfile)
604 os << "and ";
605 emitModeRequirements(os, extensions);
606 }
607 } else {
608 // Typed versioned constraints
609 llvm::interleave(
610 versions, os,
611 [&](SpecificationVersion version) {
612 const bool incompatible =
613 !targetVersion.isBackwardsCompatibleWith(version);
614 if (incompatible)
615 os << "requires specification version compatible with "
616 << stringifyVersion(version) << " (got "
617 << stringifyVersion(targetVersion) << ") ";
618 const auto profileEntries = entriesAtVersion(profiles, version);
619 const auto extensionEntries = entriesAtVersion(extensions, version);
620 const bool requiresProfile =
621 !satisfiesComplianceRequirements(profileEntries, targetEnv);
622 if (requiresProfile) {
623 if (incompatible)
624 os << "and ";
625 emitModeRequirements(os, profileEntries);
626 }
627 if (!satisfiesComplianceRequirements(extensionEntries, targetEnv)) {
628 if (incompatible || requiresProfile)
629 os << "and ";
630 emitModeRequirements(os, extensionEntries);
631 }
632 },
633 "OR ");
634 }
635 os << "to be specified in the target environment";
636 return op->emitOpError(message);
637}
638
639LogicalResult
641 const tosa::TargetEnv &targetEnv) {
642 // Collect Profile/Extension requirements for specified op.
643 const auto profiles = getOperatorMatchedEntries<Profile>(op).value_or(
645 const auto extensions = getOperatorMatchedEntries<Extension>(op).value_or(
647
648 // Compliance entries may not have a type set associated.
649 // In this case, the associated Profiles/Extensions are considered
650 // mandatory for all types.
651 const auto hasNoComplianceEntries = [](const auto &info) {
652 return info.operandTypeInfoSet.empty();
653 };
654 const auto untypedComplianceProfiles =
655 llvm::make_filter_range(profiles, hasNoComplianceEntries);
656 const auto untypedComplianceExtensions =
657 llvm::make_filter_range(extensions, hasNoComplianceEntries);
658
659 // Untyped mandatory constraints may not be supported by the current target.
660 const bool mandatoryConstraintsLegal =
661 satisfiesComplianceRequirements(untypedComplianceProfiles, targetEnv) &&
662 satisfiesComplianceRequirements(untypedComplianceExtensions, targetEnv);
663 if (!mandatoryConstraintsLegal)
664 return emitProfileOrExtensionError(op, targetEnv, untypedComplianceProfiles,
665 untypedComplianceExtensions);
666
667 // Collect remaining types constraints.
668 const auto hasComplianceEntries =
669 [&hasNoComplianceEntries](const auto &info) {
670 return !hasNoComplianceEntries(info);
671 };
672 const auto typedComplianceProfiles =
673 llvm::make_filter_range(profiles, hasComplianceEntries);
674 const auto typedComplianceExtensions =
675 llvm::make_filter_range(extensions, hasComplianceEntries);
676
677 // A legal candidate must satisfy the target specification version and
678 // profile/extension. A counterpart compliance entry (e.g. Extensions when
679 // observing Profiles) must also be valid under the same target. This accounts
680 // for the following relationships:
681 // - Profile and Extension requirements at the same version must both pass
682 // e.g. pro_int
683 // and variable.
684 // - Requirements at different versions can be considered independently e.g.
685 // concat 1.0
686 // int16 extension and 1.1 pro_fp profile.
687 const TosaSpecificationVersion targetVersion{targetEnv.getSpecVersion()};
688 const auto hasLegalEntry = [&targetVersion,
689 &targetEnv](const auto &candidates,
690 const auto &counterpartCandidates) {
691 return llvm::any_of(candidates, [&](const auto &candidate) {
692 const auto version = candidate.operandTypeInfoSet.front().second;
693 return targetVersion.isBackwardsCompatibleWith(version) &&
694 isModeSupported(candidate, targetEnv) &&
696 entriesAtVersion(counterpartCandidates, version), targetEnv);
697 });
698 };
699 if ((typedComplianceProfiles.empty() && typedComplianceExtensions.empty()) ||
700 hasLegalEntry(typedComplianceProfiles, typedComplianceExtensions) ||
701 hasLegalEntry(typedComplianceExtensions, typedComplianceProfiles))
702 return success();
703
704 return emitProfileOrExtensionError(op, targetEnv, typedComplianceProfiles,
705 typedComplianceExtensions);
706}
707
709 const auto maybeProfEntries = getOperatorMatchedEntries<Profile>(op);
710 const auto maybeExtEntries = getOperatorMatchedEntries<Extension>(op);
711 if (failed(maybeProfEntries) && failed(maybeExtEntries))
712 return success();
713
714 const bool hasEntry =
715 (succeeded(maybeProfEntries) && !maybeProfEntries.value().empty()) ||
716 (succeeded(maybeExtEntries) && !maybeExtEntries.value().empty());
717
718 if (!hasEntry) {
719 std::string message;
720 llvm::raw_string_ostream os(message);
721 os << "illegal: operation operand/result data types did not align with any "
722 "profile or extension, got (";
723
724 ProfileInfoDepot depot(op);
725 const ArrayRef<TypeInfo> current = depot.getInfo();
726 for (const auto &typeInfo : llvm::drop_end(current))
727 os << stringifyTypeInfo(typeInfo) << ",";
728 os << stringifyTypeInfo(current.back()) << ")";
729
730 // avoid polluting the error message output by outputting only
731 // the best match
732 const std::string opName = op->getName().getStringRef().str();
733 int maxMatches = -1;
734 ArrayRef<TypeInfo> bestTypeInfo;
735 const auto searchBestMatch = [&](const auto &map) {
736 const auto it = map.find(opName);
737 if (it == map.end())
738 return;
739 for (const auto &complianceInfos : it->second) {
740 for (const auto &versionedTypeInfos :
741 complianceInfos.operandTypeInfoSet) {
742 const ArrayRef<TypeInfo> typeInfos = versionedTypeInfos.first;
743 if (current.size() != typeInfos.size())
744 continue;
745 const int matches = llvm::count_if(
746 llvm::zip_equal(current, typeInfos), [&](const auto zipType) {
747 return isSameTypeInfo(std::get<0>(zipType),
748 std::get<1>(zipType));
749 });
750 if (matches > maxMatches) {
751 maxMatches = matches;
752 bestTypeInfo = typeInfos;
753 }
754 }
755 }
756 };
757 searchBestMatch(getProfileComplianceMap<Profile>());
758 searchBestMatch(getProfileComplianceMap<Extension>());
759
760 os << ", did you mean (";
761 for (const auto &typeInfo : llvm::drop_end(bestTypeInfo))
762 os << stringifyTypeInfo(typeInfo) << ",";
763 os << stringifyTypeInfo(bestTypeInfo.back()) << ")? ";
764 os << "Otherwise, please refer to the 'supported data types' for '"
765 << opName << "' in the specification.";
766 op->emitOpError(message);
767 return failure();
768 }
769
770 return success();
771}
772
773// Find the profiles or extensions requirement according to the signature of
774// type of the operand list.
775template <typename T>
777 Operation *op, ArrayRef<OpComplianceInfo<T>> compInfo) {
778 assert(compInfo.size() != 0 &&
779 "profile-based compliance information is empty");
780
781 // If there are no type constraints for the given operation, return the
782 // matched entries directly so required profile/extension information can
783 // be checked
785 if (llvm::all_of(compInfo, [&matchedInfos](const OpComplianceInfo<T> &info) {
786 if (info.operandTypeInfoSet.empty()) {
787 matchedInfos.push_back(info);
788 return true;
789 }
790 return false;
791 }))
792 return matchedInfos;
793
794 // Get the type information of the operator we're trying to match
795 ProfileInfoDepot depot(op);
796 ArrayRef<TypeInfo> present = depot.getInfo();
797 if (present.size() == 0)
798 return {};
799
800 // Compare 'present' against known type signatures in the compliance metadata
801 for (const auto &comp : compInfo) {
802 for (const auto &set : comp.operandTypeInfoSet) {
803 ArrayRef<TypeInfo> expected = set.first;
804 // Tensor-list operators can legitimately have multiple valid signatures
805 // with different operand/result counts, e.g. data-only and data+scale
806 // forms. Treat those as non-matches instead of asserting.
807 if (present.size() != expected.size())
808 continue;
809
810 bool isFound = true;
811 // Compare the type signature between the given operation and the
812 // compliance metadata.
813 for (size_t j = 0; j < expected.size(); j++) {
814 if (!isSameTypeInfo(present[j], expected[j])) {
815 // Verify the next mode set from the list.
816 isFound = false;
817 break;
818 }
819 }
820
821 if (isFound == true) {
822 SmallVector<VersionedTypeInfo> typeInfoSet{set};
823 matchedInfos.emplace_back(comp.mode, std::move(typeInfoSet),
824 comp.condition);
825 }
826 }
827 }
828
829 return matchedInfos;
830}
831
832// Debug utilites.
835 const auto stringifyScalarTypeInfo =
836 [](const TypeInfo &typeInfo) -> llvm::SmallString<32> {
837 if (typeInfo.typeID == mlir::IntegerType::getTypeID()) {
838 return {"i" + llvm::utostr(typeInfo.bitWidth)};
839 }
840 if (typeInfo.typeID == mlir::Float16Type::getTypeID()) {
841 return {"f16"};
842 } else if (typeInfo.typeID == mlir::Float32Type::getTypeID()) {
843 return {"f32"};
844 } else if (typeInfo.typeID == mlir::BFloat16Type::getTypeID()) {
845 return {"bf16"};
846 } else if (typeInfo.typeID == mlir::Float8E4M3FNType::getTypeID()) {
847 return {"fp8e4m3"};
848 } else if (typeInfo.typeID == mlir::Float8E5M2Type::getTypeID()) {
849 return {"fp8e5m2"};
850 } else if (typeInfo.typeID == mlir::Float6E2M3FNType::getTypeID()) {
851 return {"fp6e2m3"};
852 } else if (typeInfo.typeID == mlir::Float6E3M2FNType::getTypeID()) {
853 return {"fp6e3m2"};
854 } else if (typeInfo.typeID == mlir::Float4E2M1FNType::getTypeID()) {
855 return {"fp4e2m1"};
856 } else if (typeInfo.typeID == mlir::Float8E8M0FNUType::getTypeID()) {
857 return {"fp8e8m0"};
858 } else if (typeInfo.typeID == MXI8Type::getTypeID()) {
859 return {"mxint8"};
860 }
861 llvm_unreachable("unknown type");
862 };
863
864 if (typeInfo.typeID == tosa::BlockScaledType::getTypeID()) {
865 TypeInfo valueInfo = {typeInfo.valueTypeID, typeInfo.bitWidth};
866 TypeInfo scaleInfo = {typeInfo.scaleTypeID, 8};
868 llvm::raw_svector_ostream os(result);
869 os << "bs"
870 << tosa::BlockShapeAttr::getBlockShapeValue(typeInfo.blockShape.value())
871 << "_" << stringifyScalarTypeInfo(scaleInfo) << "_"
872 << stringifyScalarTypeInfo(valueInfo);
873 return result;
874 }
875
876 return stringifyScalarTypeInfo(typeInfo);
877}
878} // namespace tosa
879} // namespace mlir
for(Operation *op :ops)
return success()
#define POPULATE_PROFILE_INFO_CUSTOM(tosaOp)
#define POPULATE_PROFILE_INFO_COMMON(tosaOp)
#define POPULATE_PROFILE_INFO_SKIP(tosaOp)
@ Gather
StringRef getStringRef() const
Return the name of this operation. This always succeeds.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
OperationName getName()
The name of an operation is the key identifier for it.
Definition Operation.h:115
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
This class provides an efficient unique identifier for a specific C++ type.
Definition TypeID.h:107
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:392
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
ArrayRef< TypeInfo > getInfo() const
This class represents the capability enabled in the target implementation such as profile,...
Definition TargetEnv.h:119
bool allowsAllOf(ArrayRef< Profile > profs) const
Definition TargetEnv.h:145
bool allowsAnyOf(ArrayRef< Profile > profs) const
Definition TargetEnv.h:141
TosaSpecificationVersion getSpecVersion() const
Definition TargetEnv.h:132
static llvm::SmallString< 32 > stringifyTypeInfo(const TypeInfo &typeInfo)
LogicalResult checkInvalid(Operation *op)
SmallVector< OpComplianceInfo< T > > findMatchedEntries(Operation *op, ArrayRef< OpComplianceInfo< T > > compInfo)
LogicalResult checkCompliance(Operation *op, const tosa::TargetEnv &targetEnv)
const std::unordered_map< std::string, SmallVector< OpComplianceInfo< T > > > & getProfileComplianceMap() const
bool isSameTypeInfo(const TypeInfo &a, const TypeInfo &b)
A thin wrapper around the SpecificationVersion enum to represent and provide utilities around the TOS...
Definition TargetEnv.h:60
bool isBackwardsCompatibleWith(TosaSpecificationVersion baseVersion) const
Definition TargetEnv.h:69
llvm::SmallString< 4 > stringifyVersion(TosaSpecificationVersion version)
Definition TargetEnv.cpp:25
static bool isModeSupported(const OpComplianceInfo< T > &info, const TargetEnv &targetEnv)
static void emitModeRequirements(llvm::raw_ostream &os, const Range &entries)
static LogicalResult emitProfileOrExtensionError(Operation *op, const TargetEnv &targetEnv, const ProfileRange &profiles, const ExtensionRange &extensions)
std::unordered_map< std::string, SmallVector< OpComplianceInfo< Extension > > > OperationExtensionComplianceMap
static auto entriesAtVersion(const Range &entries, SpecificationVersion version)
std::unordered_map< std::string, SmallVector< OpComplianceInfo< Profile > > > OperationProfileComplianceMap
static bool satisfiesComplianceRequirements(const Range &entries, const TargetEnv &targetEnv)
Include the generated interface declarations.
@ Mul
RHS of mul is always a constant or a symbolic expression.
Definition AffineExpr.h:43
Represents a range (offset, size, and stride) where each element of the triple may be dynamic or stat...
SmallVector< VersionedTypeInfo > operandTypeInfoSet
OpComplianceInfo & operator=(const OpComplianceInfo &other)
std::optional< tosa::BlockShape > blockShape
VersionedTypeInfo & operator=(const VersionedTypeInfo &other)
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.