Skip to content
Draft
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
71 changes: 60 additions & 11 deletions mlir/include/mlir/Dialect/Tosa/IR/TargetEnv.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,66 @@ TargetEnvAttr getDefaultTargetEnv(MLIRContext *context);
/// returned by getDefaultTargetEnv() if not provided.
TargetEnvAttr lookupTargetEnvOrDefault(Operation *op);

/// A thin wrapper around the SpecificationVersion enum to represent
/// and provide utilities around the TOSA specification version.
class TosaSpecificationVersion {
public:
TosaSpecificationVersion() = default;

TosaSpecificationVersion(uint32_t major, uint32_t minor)
: majorVersion(major), minorVersion(minor) {}
TosaSpecificationVersion(SpecificationVersion version)
: TosaSpecificationVersion(fromVersionEnum(version)) {}

bool isBackwardsCompatibleWith(TosaSpecificationVersion baseVersion) const {
return this->majorVersion == baseVersion.majorVersion &&
this->minorVersion >= baseVersion.minorVersion;
}

uint32_t getMajor() const { return majorVersion; }
uint32_t getMinor() const { return minorVersion; }

private:
uint32_t majorVersion = 0;
uint32_t minorVersion = 0;

static TosaSpecificationVersion
fromVersionEnum(SpecificationVersion version) {
switch (version) {
case SpecificationVersion::V_1_0:
return TosaSpecificationVersion(1, 0);
case SpecificationVersion::V_1_1_DRAFT:
return TosaSpecificationVersion(1, 1);
}
llvm_unreachable("Unknown TOSA version");
}
};

TosaSpecificationVersion getMinVersion(const Profile &profile);
TosaSpecificationVersion getMinVersion(const Extension &extension);
TosaSpecificationVersion getMinVersion(const Level &level);

llvm::SmallString<4> stringifyVersion(TosaSpecificationVersion version);

/// This class represents the capability enabled in the target implementation
/// such as profile, extension, and level. It's a wrapper class around
/// tosa::TargetEnvAttr.
class TargetEnv {
public:
TargetEnv() {}
explicit TargetEnv(Level level, const ArrayRef<Profile> &profiles,
const ArrayRef<Extension> &extensions)
: level(level) {
enabledProfiles.insert_range(profiles);
enabledExtensions.insert_range(extensions);
}

explicit TargetEnv(TargetEnvAttr targetAttr)
: TargetEnv(targetAttr.getLevel(), targetAttr.getProfiles(),
targetAttr.getExtensions()) {}
static FailureOr<TargetEnv>
createTargetEnvFromAttr(TargetEnvAttr targetAttr, Location targetEnvAttrLoc);

static LogicalResult verifyTargetInformation(TargetEnvAttr targetAttr,
Location targetAttrLoc);

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

// TODO implement the following utilities.
// Version getSpecVersion() const;
TosaSpecificationVersion getSpecVersion() const {
return specificationVersion;
}

TosaLevel getLevel() const {
if (level == Level::eightK)
Expand Down Expand Up @@ -105,6 +143,17 @@ class TargetEnv {
}

private:
// Require target information is verified before constructing, via the use of
// `createTargetEnvFromAttr`.
explicit TargetEnv(SpecificationVersion specificationVersion, Level level,
const ArrayRef<Profile> &profiles,
const ArrayRef<Extension> &extensions)
: specificationVersion(specificationVersion), level(level) {
enabledProfiles.insert_range(profiles);
enabledExtensions.insert_range(extensions);
}

TosaSpecificationVersion specificationVersion;
Level level;
llvm::SmallSet<Profile, 3> enabledProfiles;
llvm::SmallSet<Extension, 13> enabledExtensions;
Expand Down
Loading
Loading