MLIR 24.0.0git
NVVMRequiresSMTraits.h
Go to the documentation of this file.
1//===--- NVVMRequiresSMTraits.h - NVVM Requires SM Traits -----*- 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 defines op traits for the NVVM Dialect in MLIR
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef NVVM_DIALECT_NVVM_IR_NVVMREQUIRESSMTRAITS_H_
14#define NVVM_DIALECT_NVVM_IR_NVVMREQUIRESSMTRAITS_H_
15
18#include "llvm/ADT/StringRef.h"
19
20#include <limits>
21#include <optional>
22
23namespace mlir {
24
25namespace NVVM {
26
27// Struct to store and check compatibility of SM versions.
29 static constexpr llvm::StringLiteral kStreamingMultiprocessorPrefix = "sm_";
30 static constexpr char kArchAcceleratedSuffix = 'a';
31 static constexpr char kFamilySpecificSuffix = 'f';
32
33 // List of supported full SM versions.
34 // This is used to check compatibility with a target SM version.
35 // The full SM version is encoded as SM * 10 + ArchSuffixOffset where:
36 // - SM is the SM version (e.g., 100)
37 // - ArchSuffixOffset is 0 for base, 2 for family-specific, and 3 for
38 // architecture-accelerated
39 //
40 // For example, sm_100 is encoded as 1000 (100 * 10 + 0), sm_100f is encoded
41 // as 1002 (100 * 10 + 2) and sm_100a is encoded as 1003 (100 * 10 + 3).
43
44 template <typename... Versions>
45 NVVMCheckSMVersion(Versions... fullSmVersions)
46 : fullSmVersionList({fullSmVersions...}) {}
47
48 bool isCompatibleWith(const unsigned &targetFullSmVersion) const {
49 return llvm::any_of(
50 fullSmVersionList, [&](const unsigned &requiredFullSmVersion) {
51 if (hasArchAcceleratedFeatures(requiredFullSmVersion))
52 return hasArchAcceleratedFeatures(targetFullSmVersion) &&
53 (getSMVersion(targetFullSmVersion) ==
54 getSMVersion(requiredFullSmVersion));
55
56 if (hasFamilySpecificFeatures(requiredFullSmVersion))
57 return hasFamilySpecificFeatures(targetFullSmVersion) &&
58 (getSMFamily(targetFullSmVersion) ==
59 getSMFamily(requiredFullSmVersion)) &&
60 (getSMVersion(targetFullSmVersion) >=
61 getSMVersion(requiredFullSmVersion));
62
63 return targetFullSmVersion >= requiredFullSmVersion;
64 });
65 }
66
67 // Parses an SM version string and returns an equivalent full SM version
68 // integer.
69 static std::optional<unsigned>
70 getTargetFullSmVersionFromStr(StringRef smVersionString) {
71 if (!smVersionString.consume_front(kStreamingMultiprocessorPrefix))
72 return std::nullopt;
73
74 unsigned suffix = 0;
75 if (!smVersionString.empty()) {
76 if (smVersionString.back() == kArchAcceleratedSuffix)
77 suffix = 3;
78 else if (smVersionString.back() == kFamilySpecificSuffix)
79 suffix = 2;
80 if (suffix)
81 smVersionString = smVersionString.drop_back();
82 }
83
84 unsigned smVersion;
85 if (smVersionString.empty() ||
86 smVersionString.getAsInteger(10, smVersion) ||
87 smVersion > (std::numeric_limits<unsigned>::max() - suffix) / 10)
88 return std::nullopt;
89
90 return smVersion * 10 + suffix;
91 }
92
93 static bool isMinimumSMVersion(unsigned fullSmVersion) {
94 return getSMVersion(fullSmVersion) >= 20;
95 }
96
97private:
98 static bool hasFamilySpecificFeatures(unsigned fullSmVersion) {
99 return (fullSmVersion % 10) >= 2;
100 }
101
102 static bool hasArchAcceleratedFeatures(unsigned fullSmVersion) {
103 return (fullSmVersion % 10) == 3;
104 }
105
106 static unsigned getSMVersion(unsigned fullSmVersion) {
107 return fullSmVersion / 10;
108 }
109
110 static unsigned getSMFamily(unsigned fullSmVersion) {
111 return fullSmVersion / 100;
112 }
113};
114
115} // namespace NVVM
116} // namespace mlir
117
118#include "mlir/Dialect/LLVMIR/NVVMRequiresSMTraits.h.inc"
119
120namespace mlir {
121
122namespace OpTrait {
123
124template <unsigned... FullSMVersions>
126public:
127 template <typename ConcreteOp>
128 class Impl
129 : public OpTrait::TraitBase<ConcreteOp,
130 NVVMRequiresSM<FullSMVersions...>::Impl>,
131 public mlir::NVVM::RequiresSMInterface::Trait<ConcreteOp> {
132 public:
134 return NVVM::NVVMCheckSMVersion(FullSMVersions...);
135 }
136 };
137};
138} // namespace OpTrait
139} // namespace mlir
140#endif // NVVM_DIALECT_NVVM_IR_NVVMREQUIRESSMTRAITS_H_
NVVM::NVVMCheckSMVersion getRequiredMinSMVersion() const
Helper class for implementing traits.
Include the generated interface declarations.
NVVMCheckSMVersion(Versions... fullSmVersions)
static constexpr char kArchAcceleratedSuffix
static bool isMinimumSMVersion(unsigned fullSmVersion)
static std::optional< unsigned > getTargetFullSmVersionFromStr(StringRef smVersionString)
llvm::SmallVector< unsigned > fullSmVersionList
static constexpr llvm::StringLiteral kStreamingMultiprocessorPrefix
bool isCompatibleWith(const unsigned &targetFullSmVersion) const
static constexpr char kFamilySpecificSuffix