|
| 1 | +/*========================== begin_copyright_notice ============================ |
| 2 | +
|
| 3 | +Copyright (C) 2025 Intel Corporation |
| 4 | +
|
| 5 | +SPDX-License-Identifier: MIT |
| 6 | +
|
| 7 | +============================= end_copyright_notice ===========================*/ |
| 8 | + |
| 9 | +/*============================================================================== |
| 10 | + * Platform support types and helpers for IGCCSPIRVSupportTblGen |
| 11 | + *============================================================================*/ |
| 12 | +#pragma once |
| 13 | + |
| 14 | +#include "llvm/ADT/StringRef.h" |
| 15 | +#include "llvm/ADT/SmallVector.h" |
| 16 | +#include "llvm/TableGen/Record.h" |
| 17 | +#include "llvm/TableGen/Error.h" |
| 18 | + |
| 19 | +namespace IGCCSPIRVPlatformSupport { |
| 20 | +// Selective LLVM symbols imported to avoid polluting including translation units. |
| 21 | +using llvm::PrintFatalError; |
| 22 | +using llvm::Record; |
| 23 | +using llvm::RecordKeeper; |
| 24 | +using llvm::SmallVector; |
| 25 | +using llvm::StringRef; |
| 26 | + |
| 27 | +// Classification of platform support descriptors. |
| 28 | +enum class PlatformSupportKind { |
| 29 | + All, |
| 30 | + NotSupported, |
| 31 | + InheritFromExtension, |
| 32 | + CoreChildOf, |
| 33 | + ExactPlatform, |
| 34 | + InGroup, |
| 35 | + AnyOf, |
| 36 | + Unknown |
| 37 | +}; |
| 38 | + |
| 39 | +// Capability entry for an extension. |
| 40 | +struct CapabilityEntry { |
| 41 | + StringRef Name; |
| 42 | + const Record *Root; // original capability record |
| 43 | + const Record *EffectivePlatform; // resolved support (extension or capability) |
| 44 | +}; |
| 45 | + |
| 46 | +struct ExtensionEntry { |
| 47 | + StringRef Name; |
| 48 | + StringRef SpecURL; |
| 49 | + const Record *Root; // original extension record |
| 50 | + const Record *Platforms; // extension support record (raw) |
| 51 | + bool IsInheritFromCapabilitiesMode; // true if extSupport == InheritFromCapabilities (aggregated capability mode) |
| 52 | + SmallVector<CapabilityEntry, 8> Capabilities; |
| 53 | +}; |
| 54 | + |
| 55 | +// Container alias for list of extensions |
| 56 | +using SPIRVExtensions = SmallVector<ExtensionEntry, 64>; |
| 57 | + |
| 58 | +inline PlatformSupportKind classifyPlatformSupport(const Record *R) { |
| 59 | + StringRef Name = R->getName(); |
| 60 | + if (Name == "AllPlatformSupport") |
| 61 | + return PlatformSupportKind::All; |
| 62 | + if (Name == "NotSupported") |
| 63 | + return PlatformSupportKind::NotSupported; |
| 64 | + if (Name == "InheritFromExtension") |
| 65 | + return PlatformSupportKind::InheritFromExtension; |
| 66 | + if (R->isSubClassOf("isCoreChildOf")) |
| 67 | + return PlatformSupportKind::CoreChildOf; |
| 68 | + if (R->isSubClassOf("ExactPlatform")) |
| 69 | + return PlatformSupportKind::ExactPlatform; |
| 70 | + if (R->isSubClassOf("isInGroup")) |
| 71 | + return PlatformSupportKind::InGroup; |
| 72 | + if (R->isSubClassOf("AnyOf")) |
| 73 | + return PlatformSupportKind::AnyOf; |
| 74 | + return PlatformSupportKind::Unknown; |
| 75 | +} |
| 76 | + |
| 77 | +static void validateExtensionPlatformSupport(const Record *Ext) { |
| 78 | + const Record *ExtSupport = Ext->getValueAsDef("ExtSupport"); |
| 79 | + const StringRef ExtSupportName = ExtSupport->getName(); |
| 80 | + const StringRef ExtName = Ext->getValueAsString("ExtName"); |
| 81 | + const bool IsAggregateMode = (ExtSupportName == "InheritFromCapabilities"); |
| 82 | + |
| 83 | + if (ExtSupportName == "InheritFromExtension") { |
| 84 | + PrintFatalError( |
| 85 | + Ext->getLoc(), |
| 86 | + "Extension '" + ExtName.str() + |
| 87 | + "' cannot use InheritFromExtension; specify explicit platform support or InheritFromCapabilities."); |
| 88 | + } |
| 89 | + |
| 90 | + auto Caps = Ext->getValueAsListOfDefs("ExtCapabilities"); |
| 91 | + for (const Record *Cap : Caps) { |
| 92 | + const Record *CapSupport = Cap->getValueAsDef("Support"); |
| 93 | + const StringRef CapSupportName = CapSupport->getName(); |
| 94 | + const StringRef CapName = Cap->getValueAsString("Name"); |
| 95 | + |
| 96 | + if (CapSupportName == "InheritFromCapabilities") { |
| 97 | + PrintFatalError(Cap->getLoc(), "Capability '" + CapName + "' in extension '" + ExtName + |
| 98 | + "' cannot use InheritFromCapabilities."); |
| 99 | + } |
| 100 | + if (IsAggregateMode) { |
| 101 | + if (CapSupportName == "InheritFromExtension") { |
| 102 | + PrintFatalError(Cap->getLoc(), "Capability '" + CapName + "' in extension '" + ExtName + |
| 103 | + "' must specify explicit platform support."); |
| 104 | + } |
| 105 | + } else { |
| 106 | + if (CapSupportName != "InheritFromExtension") { |
| 107 | + PrintFatalError(Cap->getLoc(), |
| 108 | + "Capability '" + CapName + "' in extension '" + ExtName + |
| 109 | + "' sets explicit Support; set extension platform support to InheritFromCapabilities."); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +static SPIRVExtensions collectSPIRVExtensionSupport(const RecordKeeper &Records) { |
| 116 | + SPIRVExtensions Result; |
| 117 | + auto AllExtensions = Records.getAllDerivedDefinitions("SPIRVExtension"); |
| 118 | + Result.reserve(AllExtensions.size()); |
| 119 | + for (const Record *Ext : AllExtensions) { |
| 120 | + validateExtensionPlatformSupport(Ext); |
| 121 | + ExtensionEntry Entry; |
| 122 | + Entry.Name = Ext->getValueAsString("ExtName"); |
| 123 | + Entry.SpecURL = Ext->getValueAsString("ExtSpecURL"); |
| 124 | + Entry.Root = Ext; |
| 125 | + Entry.Platforms = Ext->getValueAsDef("ExtSupport"); |
| 126 | + Entry.IsInheritFromCapabilitiesMode = Entry.Platforms->getName() == "InheritFromCapabilities"; |
| 127 | + auto Caps = Ext->getValueAsListOfDefs("ExtCapabilities"); |
| 128 | + for (const Record *Cap : Caps) { |
| 129 | + CapabilityEntry CapEntry; |
| 130 | + CapEntry.Name = Cap->getValueAsString("Name"); |
| 131 | + CapEntry.Root = Cap; |
| 132 | + const Record *CapSupport = Cap->getValueAsDef("Support"); |
| 133 | + if (CapSupport->getName() == "InheritFromExtension" && Entry.Platforms->getName() != "InheritFromCapabilities") { |
| 134 | + CapEntry.EffectivePlatform = Entry.Platforms; |
| 135 | + } else { |
| 136 | + CapEntry.EffectivePlatform = CapSupport; // explicit capability support or InheritFromCapabilities mode |
| 137 | + } |
| 138 | + Entry.Capabilities.push_back(CapEntry); |
| 139 | + } |
| 140 | + Result.push_back(std::move(Entry)); |
| 141 | + } |
| 142 | + return Result; |
| 143 | +} |
| 144 | + |
| 145 | +} // namespace IGCCSPIRVPlatformSupport |
0 commit comments