Skip to content

Commit 11468c3

Browse files
authored
[mlir][tosa] Add profile-based operation validation (#126992)
TOSA MLIR profile-based validation is designed to identify the profile/extension requirements for each operation in TOSA MLIR graph, ensuring that TOSA operators conform to the profiles and extensions enabled by the target implementation. The available profiles/extensions are reflected in the availability property attached to each TOSA operator in the dialect. The design of availability, the profile/extension classes, and their interface, is inspired by the SPIRV implementation. This patch includes the following changes: - Introduces profile and extension knowledge within the dialect and establishes an interface to query this information. - Implements profile-based validation logic in the pass. - Adds a TargetEnv class that represents the capabilities enabled in the target implementation, such as profiles, extensions, and levels. - Adds a set of tests to ensure that profile and extension requirements are properly attached to the operations and that validation correctly verifies the requirements of a given operation against the target implementation.
1 parent 5bf3748 commit 11468c3

31 files changed

+3012
-53
lines changed

mlir/include/mlir/Conversion/TosaToLinalg/TosaToLinalg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void addTosaToLinalgPasses(
4040
// Note: Default to 'none' level unless otherwise specified.
4141
std::optional<tosa::TosaValidationOptions> validationOptions =
4242
tosa::TosaValidationOptions{
43-
{"none"}, false, tosa::TosaLevelEnum::None});
43+
{"none"}, {"none"}, false, tosa::TosaLevelEnum::None});
4444

4545
/// Populates TOSA to linalg pipelines
4646
/// Currently, this includes only the "tosa-to-linalg-pipeline".

mlir/include/mlir/Dialect/Tosa/IR/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,14 @@ add_public_tablegen_target(MLIRTosaAttributesIncGen)
1212
set(LLVM_TARGET_DEFINITIONS TosaDialectBytecode.td)
1313
mlir_tablegen(TosaDialectBytecode.cpp.inc -gen-bytecode -bytecode-dialect="Tosa")
1414
add_public_tablegen_target(MLIRTosaDialectBytecodeIncGen)
15+
16+
set(LLVM_TARGET_DEFINITIONS TosaOpBase.td)
17+
mlir_tablegen(TosaEnums.h.inc -gen-enum-decls)
18+
mlir_tablegen(TosaEnums.cpp.inc -gen-enum-defs)
19+
add_public_tablegen_target(MLIRTosaEnumsIncGen)
20+
21+
set(LLVM_TARGET_DEFINITIONS TosaOps.td)
22+
mlir_tablegen(TosaAvailability.h.inc -gen-avail-interface-decls)
23+
mlir_tablegen(TosaAvailability.cpp.inc -gen-avail-interface-defs)
24+
mlir_tablegen(TosaOpAvailabilityImpl.inc -gen-tosa-avail-impls)
25+
add_public_tablegen_target(MLIRTosaAvailabilityIncGen)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//===- TargetEnv.h - Tosa target environment utilities ----------*- 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 declares utilities for Tosa target environment (implementation).
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_DIALECT_TOSA_IR_TARGETENV_H
14+
#define MLIR_DIALECT_TOSA_IR_TARGETENV_H
15+
16+
#include "mlir/Dialect/Tosa/IR/TosaOps.h"
17+
#include "mlir/Support/LLVM.h"
18+
#include "llvm/ADT/SmallSet.h"
19+
20+
namespace mlir {
21+
namespace tosa {
22+
23+
/// This class represents the capability enabled in the target implementation
24+
/// such as profile, extension, and level.
25+
class TargetEnv {
26+
public:
27+
TargetEnv() {}
28+
explicit TargetEnv(const SmallVectorImpl<Profile> &profiles,
29+
const SmallVectorImpl<Extension> &extensions) {
30+
for (Profile prof : profiles)
31+
enabledProfiles.insert(prof);
32+
33+
for (Extension ext : extensions)
34+
enabledExtensions.insert(ext);
35+
}
36+
37+
void addProfile(Profile p) { enabledProfiles.insert(p); }
38+
void addExtension(Extension e) { enabledExtensions.insert(e); }
39+
40+
// TODO implement the following utilities.
41+
// Version getSpecVersion() const;
42+
// TosaLevel getLevel() const;
43+
44+
// Returns true if the given profile is allowed.
45+
bool allows(Profile prof) const { return enabledProfiles.count(prof) != 0; }
46+
47+
bool allowsAnyOf(ArrayRef<Profile> profs) const {
48+
const auto *chosen = llvm::find_if(
49+
profs, [this](tosa::Profile prof) { return allows(prof); });
50+
return chosen != profs.end() ? true : false;
51+
}
52+
53+
bool allowsAllOf(ArrayRef<Profile> profs) const {
54+
bool is_allowed = true;
55+
llvm::for_each(profs,
56+
[&](tosa::Profile prof) { is_allowed &= allows(prof); });
57+
return is_allowed;
58+
}
59+
60+
// Returns true if the given extension is allowed.
61+
bool allows(Extension ext) const { return enabledExtensions.count(ext) != 0; }
62+
63+
bool allowsAnyOf(ArrayRef<Extension> exts) const {
64+
const auto *chosen = llvm::find_if(
65+
exts, [this](tosa::Extension ext) { return allows(ext); });
66+
return chosen != exts.end() ? true : false;
67+
}
68+
69+
bool allowsAllOf(ArrayRef<Extension> exts) const {
70+
bool is_allowed = true;
71+
llvm::for_each(exts,
72+
[&](tosa::Extension ext) { is_allowed &= allows(ext); });
73+
return is_allowed;
74+
}
75+
76+
private:
77+
llvm::SmallSet<Profile, 3> enabledProfiles;
78+
llvm::SmallSet<Extension, 8> enabledExtensions;
79+
};
80+
81+
} // namespace tosa
82+
} // namespace mlir
83+
84+
#endif // MLIR_DIALECT_TOSA_IR_TARGETENV_H

0 commit comments

Comments
 (0)