[llvm] [NVPTX] Model SM architectures as subtarget features (NFC) (PR #214335)

Durgadoss R via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 6 08:44:12 PDT 2026


================
@@ -14,100 +14,111 @@
 
 include "llvm/Target/Target.td"
 
-include "NVPTXRegisterInfo.td"
-include "NVPTXInstrInfo.td"
-
 //===----------------------------------------------------------------------===//
-// Subtarget Features.
-// - We use the SM version number instead of explicit feature table.
-// - Need at least one feature to avoid generating zero sized array by
-//   TableGen in NVPTXGenSubtarget.inc.
+// PTX ISA versions.
 //===----------------------------------------------------------------------===//
 
-class FeatureSM<string sm, int value>:
-   SubtargetFeature<"sm_"# sm, "FullSmVersion",
-                    "" # value,
-                    "Target SM " # sm>;
-
 class FeaturePTX<int version>:
    SubtargetFeature<"ptx"# version, "PTXVersion",
                     "" # version,
                     "Use PTX version " # version>;
-// NVPTX Architecture Hierarchy and Ordering:
-//
-// GPU architectures: sm_2Y/sm_3Y/sm_5Y/sm_6Y/sm_7Y/sm_8Y/sm_9Y/sm_10Y/sm_12Y
-// ('Y' represents version within the architecture)
-// The architectures have name of form sm_XYz where 'X' represent the generation
-// number, 'Y' represents the version within the architecture, and 'z' represents
-// the optional feature suffix.
-// If X1Y1 <= X2Y2, then GPU capabilities of sm_X1Y1 are included in sm_X2Y2.
-// For example, take sm_90 (9 represents 'X', 0 represents 'Y', and no feature
-// suffix) and sm_103 architectures (10 represents 'X', 3 represents 'Y', and no
-// feature suffix). Since 90 <= 103, sm_90 is compatible with sm_103.
-//
-// The family-specific variants have 'f' feature suffix and they follow
-// following order:
-// sm_X{Y2}f > sm_X{Y1}f iff Y2 > Y1
-// sm_XY{f} > sm_{XY}{}
-//
-// For example, take sm_100f (10 represents 'X', 0 represents 'Y', and 'f'
-// represents 'z') and sm_103f (10 represents 'X', 3 represents 'Y', and 'f'
-// represents 'z') architecture variants. Since Y1 < Y2, sm_100f is compatible with
-// sm_103f. Similarly based on the second rule, sm_90 is compatible with sm_103f.
+
+foreach version = [32, 40, 41, 42, 43, 50, 60, 61, 62, 63, 64, 65, 70, 71, 72,
+                   73, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 85, 86, 87, 88,
+                   90, 91, 92, 93, 94] in
+  def PTX#version : FeaturePTX<version>;
+
+//===----------------------------------------------------------------------===//
+// Target architectures.
 //
-// Some counter examples, take sm_100f and sm_120f (12 represents 'X', 0
-// represents 'Y', and 'f' represents 'z') architecture variants. Since both
-// belongs to different family i.e. X1 != X2, sm_100f is not compatible with
-// sm_120f.
+// Architectures are named sm_XYz, where 'X' is the generation, 'Y' the version
+// within the generation, and 'z' an optional feature suffix. Together they form
+// a lattice, spelled out below as `Implies` edges: sm_A implies sm_B exactly
+// when PTX written against sm_B is accepted for sm_A. Every edge is listed
+// rather than leaning on `Implies` being transitive, which costs nothing
+// because a feature's implied set is a fixed-size bitset either way. Asking
+// whether a target may use what sm_B introduced is then a single bit test.
 //
-// The architecture-specific variants have 'a' feature suffix and they follow
-// following order:
-// sm_XY{a} > sm_XY{f} > sm_{XY}{}
+// The three tiers below are the three portability guarantees the PTX ISA
+// defines for a `.target`; see the .target directive in the PTX ISA manual.
 //
-// For example, take sm_103a (10 represents 'X', 3 represents 'Y', and 'a'
-// represents 'z'), sm_103f, and sm_103 architecture variants. The sm_103 is
-// compatible with sm_103a and sm_103f, and sm_103f is compatible with sm_103a.
+//  * Base architectures are portable: what they introduce is supported on all
+//    later architectures, including ones in other families. This is the "onion
+//    layer" model, and it makes them totally ordered by their number, so sm_120
+//    implies sm_110, which implies sm_107, and so on.
 //
-// Encoding := Arch * 10 + ArchSuffixOffset
-// Arch := X * 10 + Y
-// ArchSuffixOffset := 0 (base), 2 ('f'), or 3 ('a')
+//  * A family-specific variant sm_XYf is portable within its family only, so
+//    sm_103f implies sm_100f. A family is the set of architectures sharing an
+//    SM major version. There are no edges between families, which is what makes
+//    sm_120f and sm_100f incomparable.
 //
-// For example, sm_103a is encoded as 1033 (103 * 10 + 3) and sm_103f is
-// encoded as 1032 (103 * 10 + 2).
+//  * An architecture-specific variant sm_XYa is not portable at all, so
+//    sm_103a implies sm_103f and nothing implies sm_103a.
 //
-// This encoding allows simple partial ordering of the architectures.
-//  + Compare Family and Arch by dividing FullSMVersion by 100 and 10
-//    respectively before the comparison.
-//  + Compare within the family by comparing FullSMVersion, given both belongs to
-//    the same family.
-//  + Detect 'a' variants by checking FullSMVersion & 1.
+// There is deliberately no numeric encoding of an architecture. Asking whether a
+// target may use something is a bit test against the node that introduced it,
+// which is what the partial order above requires; a number would only invite the
+// ordered comparisons it cannot answer.
+//===----------------------------------------------------------------------===//
+
+class FeatureSM<list<SubtargetFeature> implies>:
+   SubtargetFeature<"sm_"# !substr(NAME, 2), "HasArchitecture", "true",
+                    "Target SM " # !substr(NAME, 2), implies>,
+   Predicate<"Subtarget->hasFeature(NVPTX::" # NAME # ")"> {
+  assert !eq(!substr(NAME, 0, 2), "SM"),
+         "architecture record " # NAME # " must be named SM<version>";
+}
+
 class Proc<FeatureSM SM>
  : Processor<SM.Name, NoItineraries, [SM]>;
 
-foreach sm = [20, 21, 30, 32, 35, 37, 50, 52, 53, 60,
-              61, 62, 70, 72, 75, 80, 86, 87, 88, 89,
-              90, 100, 101, 103, 107, 110, 120, 121] in {
-  // Base SM version (e.g. FullSMVersion for sm_100 is 1000)
-  def SM#sm : FeatureSM<""#sm, !mul(sm, 10)>;
-  def : Proc<!cast<FeatureSM>("SM"#sm)>;
-
-  // Family-specific variants, compatible within same family (e.g. sm_100f = 1002)
-  if !ge(sm, 100) then {
-    def SM#sm#f : FeatureSM<""#sm#"f", !add(!mul(sm, 10), 2)>;
-    def : Proc<!cast<FeatureSM>("SM"#sm#"f")>;
-  }
-
-  // Architecture-specific variants, incompatible across architectures (e.g. sm_100a = 1003)
-  if !ge(sm, 90) then {
-    def SM#sm#a : FeatureSM<""#sm#"a", !add(!mul(sm, 10), 3)>;
-    def : Proc<!cast<FeatureSM>("SM"#sm#"a")>;
-  }
+multiclass ProcSM<list<SubtargetFeature> implies> {
+  def NAME : FeatureSM<implies>;
+  def : Proc<!cast<FeatureSM>(NAME)>;
 }
 
-foreach version = [32, 40, 41, 42, 43, 50, 60, 61, 62, 63, 64, 65, 70, 71, 72,
-                   73, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 85, 86, 87, 88,
-                   90, 91, 92, 93, 94] in
-  def PTX#version : FeaturePTX<version>;
+// Base architectures, in increasing order. Each implies the features of every
+// earlier one.
+defvar SMVersions = [20, 21, 30, 32, 35, 37, 50, 52, 53, 60,
+                     61, 62, 70, 72, 75, 80, 86, 87, 88, 89,
+                     90, 100, 103, 107, 110, 120, 121];
+
+foreach sm = SMVersions in
+  defm SM#sm : ProcSM<!foreach(earlier, !filter(v, SMVersions, !lt(v, sm)),
+                               !cast<SubtargetFeature>("SM"#earlier))>;
+
+// Defines the family-specific ('f') and architecture-specific ('a') variants of
+// the base architecture this is named after.
+multiclass FeatureSMVariants<list<SubtargetFeature> EarlierInFamily> {
+  defm f : ProcSM<[!cast<SubtargetFeature>(NAME)] # EarlierInFamily>;
+  defm a : ProcSM<[!cast<SubtargetFeature>(NAME # "f")]>;
+}
+
+// The families, each listed in increasing order, mirroring the architecture
+// family table in the PTX ISA manual.
+defvar SMFamilies = [[100, 103, 107],  // sm_10x
+                     [110],            // sm_11x
+                     [120, 121]];      // sm_12x
+
+foreach Family = SMFamilies in
+  foreach sm = Family in
+    defm SM#sm : FeatureSMVariants<
+        !foreach(earlier, !filter(v, Family, !lt(v, sm)),
+                 !cast<SubtargetFeature>("SM" # earlier # "f"))>;
+
+// sm_90 predates family-specific variants, so sm_90a is the only member of the
+// sm_9x family and stands in for its 'f' variant, which does not exist.
+defm SM90a : ProcSM<[SM90]>;
+
+// sm_101 was the name PTX 8.6 through 8.8 used for what PTX 9.0 renamed to
+// sm_110. The two names describe the same hardware, so they share a node rather
+// than duplicating one.
+def : ProcessorAlias<"sm_101", "sm_110">;
+def : ProcessorAlias<"sm_101f", "sm_110f">;
+def : ProcessorAlias<"sm_101a", "sm_110a">;
----------------
durga4github wrote:

So, this will generate .target as sm_110 (where we had sm_101 before) right?

I wonder why there are no test updates corresponding to this.

https://github.com/llvm/llvm-project/pull/214335


More information about the llvm-commits mailing list