Skip to content

Commit 0fb1057

Browse files
s-perronVictor Lomuller
andauthored
[SPIRV] Filter disallowed extensions for env (#150051)
Not all SPIR-V extensions are allows in every environment. When we use the `-spirv-ext=all` option, the backend currently believes that all extensions can be used. This commit filters out the extensions on the command line to remove those that are not known to be allowed for the current environment. Alternatives considered: I considered modifying the SPIRVExtensionsParser::parse to use a different list of extensions for "all" depending on the target triple. However that does not work because the target triple is not available, and cannot be made available in a reasonable way. Fixes #147717 --------- Co-authored-by: Victor Lomuller <[email protected]>
1 parent ade755d commit 0fb1057

File tree

7 files changed

+368
-175
lines changed

7 files changed

+368
-175
lines changed

llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVBaseInfo.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,15 @@ struct CapabilityEntry {
3838
Capability::Capability ReqCapability;
3939
};
4040

41+
struct EnvironmentEntry {
42+
OperandCategory::OperandCategory Category;
43+
uint32_t Value;
44+
Environment::Environment AllowedEnvironment;
45+
};
46+
4147
using namespace OperandCategory;
4248
using namespace Extension;
49+
using namespace Environment;
4350
using namespace Capability;
4451
using namespace InstructionSet;
4552
#define GET_SymbolicOperands_DECL
@@ -48,6 +55,8 @@ using namespace InstructionSet;
4855
#define GET_ExtensionEntries_IMPL
4956
#define GET_CapabilityEntries_DECL
5057
#define GET_CapabilityEntries_IMPL
58+
#define GET_EnvironmentEntries_DECL
59+
#define GET_EnvironmentEntries_IMPL
5160
#define GET_ExtendedBuiltins_DECL
5261
#define GET_ExtendedBuiltins_IMPL
5362
#include "SPIRVGenTables.inc"
@@ -133,6 +142,23 @@ getSymbolicOperandCapabilities(SPIRV::OperandCategory::OperandCategory Category,
133142
return Capabilities;
134143
}
135144

145+
EnvironmentList getSymbolicOperandAllowedEnvironments(
146+
SPIRV::OperandCategory::OperandCategory Category, uint32_t Value) {
147+
EnvironmentList Environments;
148+
const SPIRV::EnvironmentEntry *Environment =
149+
SPIRV::lookupEnvironmentByCategoryAndValue(Category, Value);
150+
auto TableEnd = ArrayRef(SPIRV::EnvironmentEntries).end();
151+
while (Environment && Environment->Category == Category &&
152+
Environment->Value == Value) {
153+
Environments.push_back(static_cast<SPIRV::Environment::Environment>(
154+
Environment->AllowedEnvironment));
155+
if (++Environment == TableEnd)
156+
break;
157+
}
158+
159+
return Environments;
160+
}
161+
136162
CapabilityList
137163
getCapabilitiesEnabledByExtension(SPIRV::Extension::Extension Extension) {
138164
const SPIRV::ExtensionEntry *Entry =

llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVBaseInfo.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ namespace Capability {
3737
#include "SPIRVGenTables.inc"
3838
} // namespace Capability
3939

40+
namespace Environment {
41+
#define GET_Environment_DECL
42+
#include "SPIRVGenTables.inc"
43+
} // namespace Environment
44+
4045
namespace SourceLanguage {
4146
#define GET_SourceLanguage_DECL
4247
#include "SPIRVGenTables.inc"
@@ -241,6 +246,7 @@ enum InstFlags {
241246

242247
using CapabilityList = SmallVector<SPIRV::Capability::Capability, 8>;
243248
using ExtensionList = SmallVector<SPIRV::Extension::Extension, 8>;
249+
using EnvironmentList = SmallVector<SPIRV::Environment::Environment, 8>;
244250

245251
std::string
246252
getSymbolicOperandMnemonic(SPIRV::OperandCategory::OperandCategory Category,
@@ -254,6 +260,8 @@ getSymbolicOperandMaxVersion(SPIRV::OperandCategory::OperandCategory Category,
254260
CapabilityList
255261
getSymbolicOperandCapabilities(SPIRV::OperandCategory::OperandCategory Category,
256262
uint32_t Value);
263+
EnvironmentList getSymbolicOperandAllowedEnvironments(
264+
SPIRV::OperandCategory::OperandCategory Category, uint32_t Value);
257265
CapabilityList
258266
getCapabilitiesEnabledByExtension(SPIRV::Extension::Extension Extension);
259267
ExtensionList

llvm/lib/Target/SPIRV/SPIRVCommandLine.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "SPIRVCommandLine.h"
15-
#include "llvm/ADT/StringRef.h"
15+
#include "MCTargetDesc/SPIRVBaseInfo.h"
16+
#include "llvm/TargetParser/Triple.h"
1617
#include <algorithm>
1718
#include <map>
1819

@@ -171,3 +172,23 @@ StringRef SPIRVExtensionsParser::checkExtensions(
171172
}
172173
return StringRef();
173174
}
175+
176+
std::set<SPIRV::Extension::Extension>
177+
SPIRVExtensionsParser::getValidExtensions(const Triple &TT) {
178+
std::set<SPIRV::Extension::Extension> R;
179+
SPIRV::Environment::Environment CurrentEnvironment =
180+
SPIRV::Environment::Environment::EnvOpenCL;
181+
if (TT.getOS() == Triple::Vulkan)
182+
CurrentEnvironment = SPIRV::Environment::Environment::EnvVulkan;
183+
184+
for (const auto &[ExtensionName, ExtensionEnum] : SPIRVExtensionMap) {
185+
EnvironmentList AllowedEnv = getSymbolicOperandAllowedEnvironments(
186+
SPIRV::OperandCategory::OperandCategory::ExtensionOperand,
187+
ExtensionEnum);
188+
189+
if (std::count(AllowedEnv.begin(), AllowedEnv.end(), CurrentEnvironment))
190+
R.insert(ExtensionEnum);
191+
}
192+
193+
return R;
194+
}

llvm/lib/Target/SPIRV/SPIRVCommandLine.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace llvm {
2323
class StringRef;
24+
class Triple;
2425

2526
/// Command line parser for toggling SPIR-V extensions.
2627
struct SPIRVExtensionsParser
@@ -42,6 +43,11 @@ struct SPIRVExtensionsParser
4243
static StringRef
4344
checkExtensions(const std::vector<std::string> &ExtNames,
4445
std::set<SPIRV::Extension::Extension> &AllowedExtensions);
46+
47+
/// Returns the list of extensions that are valid for a particular
48+
/// target environment (i.e., OpenCL or Vulkan).
49+
static std::set<SPIRV::Extension::Extension>
50+
getValidExtensions(const Triple &TT);
4551
};
4652

4753
} // namespace llvm

llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,13 @@ void SPIRVSubtarget::initAvailableExtInstSets() {
166166
void SPIRVSubtarget::initAvailableExtensions(
167167
const std::set<SPIRV::Extension::Extension> &AllowedExtIds) {
168168
AvailableExtensions.clear();
169-
AvailableExtensions.insert_range(AllowedExtIds);
169+
const std::set<SPIRV::Extension::Extension> &ValidExtensions =
170+
SPIRVExtensionsParser::getValidExtensions(TargetTriple);
171+
172+
for (const auto &Ext : AllowedExtIds) {
173+
if (ValidExtensions.count(Ext))
174+
AvailableExtensions.insert(Ext);
175+
}
170176

171177
accountForAMDShaderTrinaryMinmax();
172178
}

0 commit comments

Comments
 (0)