Skip to content

[mlir][tosa] Add profile-based operation validation #126992

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

Merged
merged 1 commit into from
Feb 20, 2025
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
2 changes: 1 addition & 1 deletion mlir/include/mlir/Conversion/TosaToLinalg/TosaToLinalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void addTosaToLinalgPasses(
// Note: Default to 'none' level unless otherwise specified.
std::optional<tosa::TosaValidationOptions> validationOptions =
tosa::TosaValidationOptions{
{"none"}, false, tosa::TosaLevelEnum::None});
{"none"}, {"none"}, false, tosa::TosaLevelEnum::None});

/// Populates TOSA to linalg pipelines
/// Currently, this includes only the "tosa-to-linalg-pipeline".
Expand Down
11 changes: 11 additions & 0 deletions mlir/include/mlir/Dialect/Tosa/IR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ add_public_tablegen_target(MLIRTosaAttributesIncGen)
set(LLVM_TARGET_DEFINITIONS TosaDialectBytecode.td)
mlir_tablegen(TosaDialectBytecode.cpp.inc -gen-bytecode -bytecode-dialect="Tosa")
add_public_tablegen_target(MLIRTosaDialectBytecodeIncGen)

set(LLVM_TARGET_DEFINITIONS TosaOpBase.td)
mlir_tablegen(TosaEnums.h.inc -gen-enum-decls)
mlir_tablegen(TosaEnums.cpp.inc -gen-enum-defs)
add_public_tablegen_target(MLIRTosaEnumsIncGen)

set(LLVM_TARGET_DEFINITIONS TosaOps.td)
mlir_tablegen(TosaAvailability.h.inc -gen-avail-interface-decls)
mlir_tablegen(TosaAvailability.cpp.inc -gen-avail-interface-defs)
mlir_tablegen(TosaOpAvailabilityImpl.inc -gen-tosa-avail-impls)
add_public_tablegen_target(MLIRTosaAvailabilityIncGen)
84 changes: 84 additions & 0 deletions mlir/include/mlir/Dialect/Tosa/IR/TargetEnv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//===- TargetEnv.h - Tosa target environment utilities ----------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file declares utilities for Tosa target environment (implementation).
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_DIALECT_TOSA_IR_TARGETENV_H
#define MLIR_DIALECT_TOSA_IR_TARGETENV_H

#include "mlir/Dialect/Tosa/IR/TosaOps.h"
#include "mlir/Support/LLVM.h"
#include "llvm/ADT/SmallSet.h"

namespace mlir {
namespace tosa {

/// This class represents the capability enabled in the target implementation
/// such as profile, extension, and level.
class TargetEnv {
public:
TargetEnv() {}
explicit TargetEnv(const SmallVectorImpl<Profile> &profiles,
const SmallVectorImpl<Extension> &extensions) {
for (Profile prof : profiles)
enabledProfiles.insert(prof);

for (Extension ext : extensions)
enabledExtensions.insert(ext);
}

void addProfile(Profile p) { enabledProfiles.insert(p); }
void addExtension(Extension e) { enabledExtensions.insert(e); }

// TODO implement the following utilities.
// Version getSpecVersion() const;
// TosaLevel getLevel() const;

// Returns true if the given profile is allowed.
bool allows(Profile prof) const { return enabledProfiles.count(prof) != 0; }

bool allowsAnyOf(ArrayRef<Profile> profs) const {
const auto *chosen = llvm::find_if(
profs, [this](tosa::Profile prof) { return allows(prof); });
return chosen != profs.end() ? true : false;
}

bool allowsAllOf(ArrayRef<Profile> profs) const {
bool is_allowed = true;
llvm::for_each(profs,
[&](tosa::Profile prof) { is_allowed &= allows(prof); });
return is_allowed;
}

// Returns true if the given extension is allowed.
bool allows(Extension ext) const { return enabledExtensions.count(ext) != 0; }

bool allowsAnyOf(ArrayRef<Extension> exts) const {
const auto *chosen = llvm::find_if(
exts, [this](tosa::Extension ext) { return allows(ext); });
return chosen != exts.end() ? true : false;
}

bool allowsAllOf(ArrayRef<Extension> exts) const {
bool is_allowed = true;
llvm::for_each(exts,
[&](tosa::Extension ext) { is_allowed &= allows(ext); });
return is_allowed;
}

private:
llvm::SmallSet<Profile, 3> enabledProfiles;
llvm::SmallSet<Extension, 8> enabledExtensions;
};

} // namespace tosa
} // namespace mlir

#endif // MLIR_DIALECT_TOSA_IR_TARGETENV_H
Loading