LLVM 20.0.0git
SPIRVCommandLine.cpp
Go to the documentation of this file.
1//===--- SPIRVCommandLine.cpp ---- Command Line Options ---------*- 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 contains definitions of classes and functions needed for
10// processing, parsing, and using CLI options for the SPIR-V backend.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SPIRVCommandLine.h"
15#include "llvm/ADT/StringRef.h"
16#include <algorithm>
17#include <map>
18
19#define DEBUG_TYPE "spirv-commandline"
20
21using namespace llvm;
22
23static const std::map<std::string, SPIRV::Extension::Extension>
25 {"SPV_EXT_shader_atomic_float_add",
26 SPIRV::Extension::Extension::SPV_EXT_shader_atomic_float_add},
27 {"SPV_EXT_shader_atomic_float16_add",
28 SPIRV::Extension::Extension::SPV_EXT_shader_atomic_float16_add},
29 {"SPV_EXT_shader_atomic_float_min_max",
30 SPIRV::Extension::Extension::SPV_EXT_shader_atomic_float_min_max},
31 {"SPV_INTEL_arbitrary_precision_integers",
32 SPIRV::Extension::Extension::SPV_INTEL_arbitrary_precision_integers},
33 {"SPV_INTEL_cache_controls",
34 SPIRV::Extension::Extension::SPV_INTEL_cache_controls},
35 {"SPV_INTEL_global_variable_fpga_decorations",
36 SPIRV::Extension::Extension::
37 SPV_INTEL_global_variable_fpga_decorations},
38 {"SPV_INTEL_global_variable_host_access",
39 SPIRV::Extension::Extension::SPV_INTEL_global_variable_host_access},
40 {"SPV_INTEL_optnone", SPIRV::Extension::Extension::SPV_INTEL_optnone},
41 {"SPV_INTEL_usm_storage_classes",
42 SPIRV::Extension::Extension::SPV_INTEL_usm_storage_classes},
43 {"SPV_INTEL_subgroups",
44 SPIRV::Extension::Extension::SPV_INTEL_subgroups},
45 {"SPV_KHR_uniform_group_instructions",
46 SPIRV::Extension::Extension::SPV_KHR_uniform_group_instructions},
47 {"SPV_KHR_no_integer_wrap_decoration",
48 SPIRV::Extension::Extension::SPV_KHR_no_integer_wrap_decoration},
49 {"SPV_KHR_float_controls",
50 SPIRV::Extension::Extension::SPV_KHR_float_controls},
51 {"SPV_KHR_expect_assume",
52 SPIRV::Extension::Extension::SPV_KHR_expect_assume},
53 {"SPV_KHR_bit_instructions",
54 SPIRV::Extension::Extension::SPV_KHR_bit_instructions},
55 {"SPV_KHR_linkonce_odr",
56 SPIRV::Extension::Extension::SPV_KHR_linkonce_odr},
57 {"SPV_INTEL_inline_assembly",
58 SPIRV::Extension::Extension::SPV_INTEL_inline_assembly},
59 {"SPV_INTEL_bfloat16_conversion",
60 SPIRV::Extension::Extension::SPV_INTEL_bfloat16_conversion},
61 {"SPV_KHR_subgroup_rotate",
62 SPIRV::Extension::Extension::SPV_KHR_subgroup_rotate},
63 {"SPV_INTEL_variable_length_array",
64 SPIRV::Extension::Extension::SPV_INTEL_variable_length_array},
65 {"SPV_INTEL_function_pointers",
66 SPIRV::Extension::Extension::SPV_INTEL_function_pointers},
67 {"SPV_KHR_shader_clock",
68 SPIRV::Extension::Extension::SPV_KHR_shader_clock},
69 {"SPV_KHR_cooperative_matrix",
70 SPIRV::Extension::Extension::SPV_KHR_cooperative_matrix},
71};
72
74 llvm::StringRef ArgValue,
75 std::set<SPIRV::Extension::Extension> &Vals) {
77 ArgValue.split(Tokens, ",", -1, false);
78 std::sort(Tokens.begin(), Tokens.end());
79
80 std::set<SPIRV::Extension::Extension> EnabledExtensions;
81
82 for (const auto &Token : Tokens) {
83 if (Token == "all") {
84 for (const auto &[ExtensionName, ExtensionEnum] : SPIRVExtensionMap)
85 EnabledExtensions.insert(ExtensionEnum);
86
87 continue;
88 }
89
90 if (Token.empty() || (!Token.starts_with("+") && !Token.starts_with("-")))
91 return O.error("Invalid extension list format: " + Token.str());
92
93 llvm::StringRef ExtensionName = Token.substr(1);
94 auto NameValuePair = SPIRVExtensionMap.find(ExtensionName.str());
95
96 if (NameValuePair == SPIRVExtensionMap.end())
97 return O.error("Unknown SPIR-V extension: " + Token.str());
98
99 if (Token.starts_with("+")) {
100 EnabledExtensions.insert(NameValuePair->second);
101 } else if (EnabledExtensions.count(NameValuePair->second)) {
102 if (std::find(Tokens.begin(), Tokens.end(), "+" + ExtensionName.str()) !=
103 Tokens.end())
104 return O.error(
105 "Extension cannot be allowed and disallowed at the same time: " +
106 ExtensionName.str());
107
108 EnabledExtensions.erase(NameValuePair->second);
109 }
110 }
111
112 Vals = std::move(EnabledExtensions);
113 return false;
114}
static const std::map< std::string, SPIRV::Extension::Extension > SPIRVExtensionMap
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:685
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:215
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:556
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool parse(cl::Option &O, StringRef ArgName, StringRef ArgValue, std::set< SPIRV::Extension::Extension > &Vals)
Parses SPIR-V extension name from CLI arguments.