blob: cae0a7a6f0666031f80de72c71438381038116c6 [file] [log] [blame]
David Neto482550a2018-03-24 05:21:07 -07001// Copyright 2018 The Clspv Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// This translation unit defines all Clspv command line option variables.
16
Diego Novilloa4c44fa2019-04-11 10:56:15 -040017#include "llvm/PassRegistry.h"
David Neto118188e2018-08-24 11:27:54 -040018#include "llvm/Support/CommandLine.h"
David Neto482550a2018-03-24 05:21:07 -070019
Diego Novilloa4c44fa2019-04-11 10:56:15 -040020#include "Passes.h"
Kévin Petitf0515712020-01-07 18:29:20 +000021#include "clspv/Option.h"
Diego Novilloa4c44fa2019-04-11 10:56:15 -040022
David Neto482550a2018-03-24 05:21:07 -070023namespace {
David Neto862b7d82018-06-14 18:48:37 -040024
Alan Bakeraf289ab2018-08-29 11:26:44 -040025llvm::cl::opt<bool>
26 inline_entry_points("inline-entry-points", llvm::cl::init(false),
27 llvm::cl::desc("Exhaustively inline entry points."));
28
Alan Baker0dd3fd22018-08-24 11:03:12 -040029llvm::cl::opt<bool> no_inline_single_call_site(
30 "no-inline-single", llvm::cl::init(false),
31 llvm::cl::desc("Disable inlining functions with single call sites."));
32
David Netoc5fb5242018-07-30 13:28:31 -040033// Should the compiler try to use direct resource accesses within helper
David Neto862b7d82018-06-14 18:48:37 -040034// functions instead of passing pointers via function arguments?
David Netoc5fb5242018-07-30 13:28:31 -040035llvm::cl::opt<bool> no_direct_resource_access(
36 "no-dra", llvm::cl::init(false),
David Neto862b7d82018-06-14 18:48:37 -040037 llvm::cl::desc(
David Netoc5fb5242018-07-30 13:28:31 -040038 "No Direct Resource Access: Avoid rewriting helper functions "
39 "to access resources directly instead of by pointers "
40 "in function arguments. Affects kernel arguments of type "
41 "pointer-to-global, pointer-to-constant, image, and sampler."));
David Neto862b7d82018-06-14 18:48:37 -040042
Alan Bakerfc6888e2018-08-20 20:54:33 -040043llvm::cl::opt<bool> no_share_module_scope_variables(
44 "no-smsv", llvm::cl::init(false),
45 llvm::cl::desc("No Share Module Scope Variables: Avoid de-duplicating "
46 "module scope variables."));
47
David Neto482550a2018-03-24 05:21:07 -070048// By default, reuse the same descriptor set number for all arguments.
49// To turn that off, use -distinct-kernel-descriptor-sets
50llvm::cl::opt<bool> distinct_kernel_descriptor_sets(
51 "distinct-kernel-descriptor-sets", llvm::cl::init(false),
Alan Bakerfc6888e2018-08-20 20:54:33 -040052 llvm::cl::desc("Each kernel uses its own descriptor set for its arguments. "
53 "Turns off direct-resource-access optimizations."));
David Neto482550a2018-03-24 05:21:07 -070054
David Netob6e2e062018-04-25 10:32:06 -040055llvm::cl::opt<bool> hack_initializers(
56 "hack-initializers", llvm::cl::init(false),
57 llvm::cl::desc(
58 "At the start of each kernel, explicitly write the initializer "
59 "value for a compiler-generated variable containing the workgroup "
60 "size. Required by some drivers to make the get_global_size builtin "
61 "function work when used with non-constant dimension index."));
62
David Neto862b7d82018-06-14 18:48:37 -040063llvm::cl::opt<bool> hack_dis(
64 "hack-dis", llvm::cl::init(false),
Alan Bakerfc6888e2018-08-20 20:54:33 -040065 llvm::cl::desc("Force use of a distinct image or sampler variable for each "
66 "image or sampler kernel argument. This prevents sharing "
67 "of resource variables."));
David Neto862b7d82018-06-14 18:48:37 -040068
David Neto482550a2018-03-24 05:21:07 -070069llvm::cl::opt<bool> hack_inserts(
70 "hack-inserts", llvm::cl::init(false),
71 llvm::cl::desc(
72 "Avoid all single-index OpCompositInsert instructions "
73 "into struct types by using complete composite construction and "
74 "extractions"));
75
David Neto3a0df832018-08-03 14:35:42 -040076llvm::cl::opt<bool> hack_signed_compare_fixup(
77 "hack-scf", llvm::cl::init(false),
78 llvm::cl::desc("Rewrite signed integer comparisons to use other kinds of "
79 "instructions"));
80
David Neto482550a2018-03-24 05:21:07 -070081// Some drivers don't like to see constant composite values constructed
82// from scalar Undef values. Replace numeric scalar and vector Undef with
83// corresponding OpConstantNull. We need to keep Undef for image values,
84// for example. In the LLVM domain, image values are passed as pointer to
85// struct.
86// See https://github.com/google/clspv/issues/95
87llvm::cl::opt<bool> hack_undef(
88 "hack-undef", llvm::cl::init(false),
89 llvm::cl::desc("Use OpConstantNull instead of OpUndef for floating point, "
90 "integer, or vectors of them"));
91
Alan Baker33376ea2018-08-30 12:02:31 -040092llvm::cl::opt<bool> hack_phis(
93 "hack-phis", llvm::cl::init(false),
94 llvm::cl::desc(
95 "Scalarize phi instructions of struct type before code generation"));
96
alan-baker3fa76d92018-11-12 14:54:40 -050097llvm::cl::opt<bool> hack_block_order(
98 "hack-block-order", llvm::cl::init(false),
99 llvm::cl::desc("Order basic blocks using structured order"));
100
David Neto482550a2018-03-24 05:21:07 -0700101llvm::cl::opt<bool>
102 pod_ubo("pod-ubo", llvm::cl::init(false),
103 llvm::cl::desc("POD kernel arguments are in uniform buffers"));
104
alan-baker9b0ec3c2020-04-06 14:45:34 -0400105llvm::cl::opt<bool> pod_pushconstant(
106 "pod-pushconstant",
107 llvm::cl::desc("POD kernel arguments are in the push constant interface"),
108 llvm::cl::init(false));
109
David Neto85082642018-03-24 06:55:20 -0700110llvm::cl::opt<bool> module_constants_in_storage_buffer(
111 "module-constants-in-storage-buffer", llvm::cl::init(false),
112 llvm::cl::desc(
113 "Module-scope __constants are collected into a single storage buffer. "
114 "The binding and initialization data are reported in the descriptor "
115 "map."));
116
David Neto482550a2018-03-24 05:21:07 -0700117llvm::cl::opt<bool> show_ids("show-ids", llvm::cl::init(false),
118 llvm::cl::desc("Show SPIR-V IDs for functions"));
119
Alan Bakerfcda9482018-10-02 17:09:59 -0400120llvm::cl::opt<bool> constant_args_in_uniform_buffer(
121 "constant-args-ubo", llvm::cl::init(false),
122 llvm::cl::desc("Put pointer-to-constant kernel args in UBOs."));
123
alan-baker3d9e2012019-01-11 14:55:30 -0500124// Default to 64kB.
125llvm::cl::opt<int> maximum_ubo_size(
126 "max-ubo-size", llvm::cl::init(64 << 10),
127 llvm::cl::desc("Specify the maximum UBO array size in bytes."));
128
alan-baker9b0ec3c2020-04-06 14:45:34 -0400129llvm::cl::opt<int> maximum_pushconstant_size(
130 "max-pushconstant-size", llvm::cl::init(128),
131 llvm::cl::desc(
132 "Specify the maximum push constant interface size in bytes."));
133
alan-baker3d9e2012019-01-11 14:55:30 -0500134llvm::cl::opt<bool> relaxed_ubo_layout(
135 "relaxed-ubo-layout",
136 llvm::cl::desc("Allow UBO layouts, that do not satisfy the restriction "
137 "that ArrayStride is a multiple of array alignment. This "
138 "does not generate valid SPIR-V for the Vulkan environment; "
139 "however, some drivers may accept it."));
140
alan-bakera3e02382019-02-15 08:27:27 -0500141llvm::cl::opt<bool> std430_ubo_layout(
alan-baker4217b322019-03-06 08:56:12 -0500142 "std430-ubo-layout", llvm::cl::init(false),
alan-bakera3e02382019-02-15 08:27:27 -0500143 llvm::cl::desc("Allow UBO layouts that conform to std430 (SSBO) layout "
144 "requirements. This does not generate valid SPIR-V for the "
145 "Vulkan environment; however, some drivers may accept it."));
146
alan-baker4217b322019-03-06 08:56:12 -0500147llvm::cl::opt<bool> keep_unused_arguments(
148 "keep-unused-arguments", llvm::cl::init(false),
149 llvm::cl::desc("Do not remove unused non-kernel function arguments."));
150
alan-bakerace4e592019-04-09 08:43:22 -0400151llvm::cl::opt<bool> int8_support("int8", llvm::cl::init(true),
alan-bakerb39c8262019-03-08 14:03:37 -0500152 llvm::cl::desc("Allow 8-bit integers"));
153
Marco Antognini535998c2020-09-16 18:48:51 +0100154llvm::cl::opt<bool> long_vector_support(
155 "long-vector", llvm::cl::init(false),
156 llvm::cl::desc("Allow vectors of 8 and 16 elements. Experimental"));
157
Marco Antognini589d72e2021-03-23 19:28:16 +0000158llvm::cl::opt<bool> cl_arm_non_uniform_work_group_size(
159 "cl-arm-non-uniform-work-group-size", llvm::cl::init(false),
160 llvm::cl::desc("Enable the cl_arm_non_uniform_work_group_size extension."));
161
Kévin Petitf0515712020-01-07 18:29:20 +0000162llvm::cl::opt<clspv::Option::SourceLanguage> cl_std(
163 "cl-std", llvm::cl::desc("Select OpenCL standard"),
164 llvm::cl::init(clspv::Option::SourceLanguage::OpenCL_C_12),
165 llvm::cl::values(clEnumValN(clspv::Option::SourceLanguage::OpenCL_C_10,
166 "CL1.0", "OpenCL C 1.0"),
167 clEnumValN(clspv::Option::SourceLanguage::OpenCL_C_11,
168 "CL1.1", "OpenCL C 1.1"),
169 clEnumValN(clspv::Option::SourceLanguage::OpenCL_C_12,
170 "CL1.2", "OpenCL C 1.2"),
171 clEnumValN(clspv::Option::SourceLanguage::OpenCL_C_20,
172 "CL2.0", "OpenCL C 2.0"),
Kévin Petit77838ff2020-10-19 18:54:51 +0100173 clEnumValN(clspv::Option::SourceLanguage::OpenCL_C_30,
174 "CL3.0", "OpenCL C 3.0"),
Kévin Petitf0515712020-01-07 18:29:20 +0000175 clEnumValN(clspv::Option::SourceLanguage::OpenCL_CPP,
176 "CLC++", "C++ for OpenCL")));
Kévin Petita624c0c2019-05-07 20:27:43 +0800177
SJW806a5d82020-07-15 12:51:38 -0500178llvm::cl::opt<clspv::Option::SPIRVVersion> spv_version(
179 "spv-version", llvm::cl::desc("Specify the SPIR-V binary version"),
180 llvm::cl::init(clspv::Option::SPIRVVersion::SPIRV_1_0),
181 llvm::cl::values(
182 clEnumValN(clspv::Option::SPIRVVersion::SPIRV_1_0, "1.0",
183 "SPIR-V version 1.0 (Vulkan 1.0)"),
184 clEnumValN(clspv::Option::SPIRVVersion::SPIRV_1_3, "1.3",
alan-baker3f772c02021-06-15 22:18:11 -0400185 "SPIR-V version 1.3 (Vulkan 1.1). Experimental"),
186 clEnumValN(clspv::Option::SPIRVVersion::SPIRV_1_4, "1.4",
187 "SPIR-V version 1.4 (Vulkan 1.1). Experimental"),
188 clEnumValN(clspv::Option::SPIRVVersion::SPIRV_1_5, "1.5",
189 "SPIR-V version 1.5 (Vulkan 1.2). Experimental")));
SJW806a5d82020-07-15 12:51:38 -0500190
Kévin Petita624c0c2019-05-07 20:27:43 +0800191static llvm::cl::opt<bool> images("images", llvm::cl::init(true),
192 llvm::cl::desc("Enable support for images"));
alan-baker09cb9802019-12-10 13:16:27 -0500193
Kévin Petitbbbda972020-03-03 19:16:31 +0000194static llvm::cl::opt<bool>
195 scalar_block_layout("scalar-block-layout", llvm::cl::init(false),
196 llvm::cl::desc("Assume VK_EXT_scalar_block_layout"));
197
198static llvm::cl::opt<bool> work_dim(
alan-bakerbed3a882020-04-21 14:42:41 -0400199 "work-dim", llvm::cl::init(true),
Kévin Petitbbbda972020-03-03 19:16:31 +0000200 llvm::cl::desc("Enable support for get_work_dim() built-in function"));
201
202static llvm::cl::opt<bool>
203 global_offset("global-offset", llvm::cl::init(false),
204 llvm::cl::desc("Enable support for global offsets"));
205
alan-bakere1996972020-05-04 08:38:12 -0400206static llvm::cl::opt<bool> global_offset_push_constant(
207 "global-offset-push-constant", llvm::cl::init(false),
208 llvm::cl::desc("Enable support for global offsets in push constants"));
209
alan-baker09cb9802019-12-10 13:16:27 -0500210static bool use_sampler_map = false;
alan-baker9b0ec3c2020-04-06 14:45:34 -0400211
212static llvm::cl::opt<bool> cluster_non_pointer_kernel_args(
alan-baker374ce1a2020-04-07 20:37:20 -0400213 "cluster-pod-kernel-args", llvm::cl::init(true),
alan-baker9b0ec3c2020-04-06 14:45:34 -0400214 llvm::cl::desc("Collect plain-old-data kernel arguments into a struct in "
215 "a single storage buffer, using a binding number after "
216 "other arguments. Use this to reduce storage buffer "
217 "descriptors."));
218
alan-baker7efcaaa2020-05-06 19:33:27 -0400219static llvm::cl::list<clspv::Option::StorageClass> no_16bit_storage(
220 "no-16bit-storage",
221 llvm::cl::desc("Disable fine-grained 16-bit storage capabilities."),
222 llvm::cl::Prefix, llvm::cl::CommaSeparated, llvm::cl::ZeroOrMore,
223 llvm::cl::values(
224 clEnumValN(clspv::Option::StorageClass::kSSBO, "ssbo",
225 "Disallow 16-bit types in SSBO interfaces"),
226 clEnumValN(clspv::Option::StorageClass::kUBO, "ubo",
227 "Disallow 16-bit types in UBO interfaces"),
228 clEnumValN(clspv::Option::StorageClass::kPushConstant, "pushconstant",
229 "Disallow 16-bit types in push constant interfaces")));
230
231static llvm::cl::list<clspv::Option::StorageClass> no_8bit_storage(
232 "no-8bit-storage",
233 llvm::cl::desc("Disable fine-grained 8-bit storage capabilities."),
234 llvm::cl::Prefix, llvm::cl::CommaSeparated, llvm::cl::ZeroOrMore,
235 llvm::cl::values(
236 clEnumValN(clspv::Option::StorageClass::kSSBO, "ssbo",
237 "Disallow 8-bit types in SSBO interfaces"),
238 clEnumValN(clspv::Option::StorageClass::kUBO, "ubo",
239 "Disallow 8-bit types in UBO interfaces"),
240 clEnumValN(clspv::Option::StorageClass::kPushConstant, "pushconstant",
241 "Disallow 8-bit types in push constant interfaces")));
242
alan-baker869cd682021-03-05 11:21:19 -0500243static llvm::cl::opt<bool> cl_native_math(
244 "cl-native-math", llvm::cl::init(false),
245 llvm::cl::desc("Perform all math as fast as possible. This option does not "
246 "guarantee that OpenCL precision bounds are maintained. "
247 "Implies -cl-fast-relaxed-math."));
248
Kévin Petitaab5bb82021-03-30 16:26:11 +0100249static llvm::cl::opt<bool>
250 fp16("fp16", llvm::cl::init(true),
251 llvm::cl::desc("Enable support for cl_khr_fp16."));
252
253static llvm::cl::opt<bool>
254 fp64("fp64", llvm::cl::init(true),
255 llvm::cl::desc(
256 "Enable support for FP64 (cl_khr_fp64 and/or __opencl_c_fp64)."));
257
Mehmet Oguz Derin047cc872021-04-15 19:56:41 +0300258static llvm::cl::opt<bool> uniform_workgroup_size(
259 "uniform-workgroup-size", llvm::cl::init(false),
260 llvm::cl::desc("Assume all workgroups are uniformly sized."));
261
David Neto482550a2018-03-24 05:21:07 -0700262} // namespace
263
264namespace clspv {
265namespace Option {
266
Alan Bakeraf289ab2018-08-29 11:26:44 -0400267bool InlineEntryPoints() { return inline_entry_points; }
Alan Baker0dd3fd22018-08-24 11:03:12 -0400268bool InlineSingleCallSite() { return !no_inline_single_call_site; }
David Netoc5fb5242018-07-30 13:28:31 -0400269bool DirectResourceAccess() {
270 return !(no_direct_resource_access || distinct_kernel_descriptor_sets);
271}
Alan Bakerfc6888e2018-08-20 20:54:33 -0400272bool ShareModuleScopeVariables() { return !no_share_module_scope_variables; }
David Neto482550a2018-03-24 05:21:07 -0700273bool DistinctKernelDescriptorSets() { return distinct_kernel_descriptor_sets; }
David Neto862b7d82018-06-14 18:48:37 -0400274bool HackDistinctImageSampler() { return hack_dis; }
David Netob6e2e062018-04-25 10:32:06 -0400275bool HackInitializers() { return hack_initializers; }
David Neto482550a2018-03-24 05:21:07 -0700276bool HackInserts() { return hack_inserts; }
David Neto3a0df832018-08-03 14:35:42 -0400277bool HackSignedCompareFixup() { return hack_signed_compare_fixup; }
David Neto482550a2018-03-24 05:21:07 -0700278bool HackUndef() { return hack_undef; }
Alan Baker33376ea2018-08-30 12:02:31 -0400279bool HackPhis() { return hack_phis; }
alan-baker3fa76d92018-11-12 14:54:40 -0500280bool HackBlockOrder() { return hack_block_order; }
Alan Bakerfc6888e2018-08-20 20:54:33 -0400281bool ModuleConstantsInStorageBuffer() {
282 return module_constants_in_storage_buffer;
283}
David Neto482550a2018-03-24 05:21:07 -0700284bool PodArgsInUniformBuffer() { return pod_ubo; }
alan-baker9b0ec3c2020-04-06 14:45:34 -0400285bool PodArgsInPushConstants() { return pod_pushconstant; }
David Neto482550a2018-03-24 05:21:07 -0700286bool ShowIDs() { return show_ids; }
alan-baker3fa76d92018-11-12 14:54:40 -0500287bool ConstantArgsInUniformBuffer() { return constant_args_in_uniform_buffer; }
alan-baker3d9e2012019-01-11 14:55:30 -0500288uint64_t MaxUniformBufferSize() { return maximum_ubo_size; }
alan-baker9b0ec3c2020-04-06 14:45:34 -0400289uint32_t MaxPushConstantsSize() { return maximum_pushconstant_size; }
alan-baker3d9e2012019-01-11 14:55:30 -0500290bool RelaxedUniformBufferLayout() { return relaxed_ubo_layout; }
alan-bakera3e02382019-02-15 08:27:27 -0500291bool Std430UniformBufferLayout() { return std430_ubo_layout; }
alan-baker4217b322019-03-06 08:56:12 -0500292bool KeepUnusedArguments() { return keep_unused_arguments; }
alan-bakerb39c8262019-03-08 14:03:37 -0500293bool Int8Support() { return int8_support; }
Marco Antognini535998c2020-09-16 18:48:51 +0100294bool LongVectorSupport() { return long_vector_support; }
Kévin Petita624c0c2019-05-07 20:27:43 +0800295bool ImageSupport() { return images; }
alan-baker09cb9802019-12-10 13:16:27 -0500296bool UseSamplerMap() { return use_sampler_map; }
297void SetUseSamplerMap(bool use) { use_sampler_map = use; }
Kévin Petitf0515712020-01-07 18:29:20 +0000298SourceLanguage Language() { return cl_std; }
SJW806a5d82020-07-15 12:51:38 -0500299SPIRVVersion SpvVersion() { return spv_version; }
Kévin Petitbbbda972020-03-03 19:16:31 +0000300bool ScalarBlockLayout() { return scalar_block_layout; }
301bool WorkDim() { return work_dim; }
302bool GlobalOffset() { return global_offset; }
alan-bakere1996972020-05-04 08:38:12 -0400303bool GlobalOffsetPushConstant() { return global_offset_push_constant; }
Marco Antognini589d72e2021-03-23 19:28:16 +0000304bool NonUniformNDRangeSupported() {
Mehmet Oguz Derin047cc872021-04-15 19:56:41 +0300305 return ((Language() == SourceLanguage::OpenCL_CPP) ||
306 (Language() == SourceLanguage::OpenCL_C_20) ||
307 (Language() == SourceLanguage::OpenCL_C_30) ||
308 ArmNonUniformWorkGroupSize()) &&
309 !UniformWorkgroupSize();
Marco Antognini589d72e2021-03-23 19:28:16 +0000310}
alan-baker9b0ec3c2020-04-06 14:45:34 -0400311bool ClusterPodKernelArgs() { return cluster_non_pointer_kernel_args; }
David Neto482550a2018-03-24 05:21:07 -0700312
alan-baker7efcaaa2020-05-06 19:33:27 -0400313bool Supports16BitStorageClass(StorageClass sc) {
314 // -no-16bit-storage removes storage capabilities.
315 for (auto storage_class : no_16bit_storage) {
316 if (storage_class == sc)
317 return false;
318 }
319
320 return true;
321}
322
323bool Supports8BitStorageClass(StorageClass sc) {
324 // -no-8bit-storage removes storage capabilities.
325 for (auto storage_class : no_8bit_storage) {
326 if (storage_class == sc)
327 return false;
328 }
329
330 return true;
331}
332
alan-baker869cd682021-03-05 11:21:19 -0500333bool NativeMath() { return cl_native_math; }
334
Kévin Petitaab5bb82021-03-30 16:26:11 +0100335bool FP16() { return fp16; }
336bool FP64() { return fp64; }
337
Mehmet Oguz Derin047cc872021-04-15 19:56:41 +0300338bool ArmNonUniformWorkGroupSize() { return cl_arm_non_uniform_work_group_size; }
339bool UniformWorkgroupSize() { return uniform_workgroup_size; }
340
David Neto482550a2018-03-24 05:21:07 -0700341} // namespace Option
342} // namespace clspv