forked from intel/opencl-clang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_compile.cpp
477 lines (428 loc) · 17.8 KB
/
options_compile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*****************************************************************************\
Copyright (c) Intel Corporation (2009-2017).
INTEL MAKES NO WARRANTY OF ANY KIND REGARDING THE CODE. THIS CODE IS
LICENSED ON AN "AS IS" BASIS AND INTEL WILL NOT PROVIDE ANY SUPPORT,
ASSISTANCE, INSTALLATION, TRAINING OR OTHER SERVICES. INTEL DOES NOT
PROVIDE ANY UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY
DISCLAIMS ANY WARRANTY OF MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR ANY
PARTICULAR PURPOSE, OR ANY OTHER WARRANTY. Intel disclaims all liability,
including liability for infringement of any proprietary rights, relating to
use of the code. No license, express or implied, by estoppel or otherwise,
to any intellectual property rights is granted herein.
\file options_compile.cpp
\*****************************************************************************/
#include "opencl_clang.h"
#include "options.h"
#include "clang/Driver/Options.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Mutex.h"
#include <algorithm>
#include <map>
#include <sstream>
#define PREFIX(NAME, VALUE) \
static constexpr llvm::StringLiteral NAME##_init[] = VALUE; \
static constexpr llvm::ArrayRef<llvm::StringLiteral> NAME( \
NAME##_init, std::size(NAME##_init) - 1);
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, VISIBILITY, \
PARAM, HELPTEXT, METAVAR, VALUES)
#include "opencl_clang_options.inc"
#undef OPTION
#undef PREFIX
using namespace llvm::opt;
extern llvm::ManagedStatic<llvm::sys::SmartMutex<true>> compileMutex;
static constexpr OptTable::Info ClangOptionsInfoTable[] = {
#define PREFIX(NAME, VALUE)
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, VISIBILITY, \
PARAM, HELPTEXT, METAVAR, VALUES) \
{ \
PREFIX, NAME, HELPTEXT, METAVAR, OPT_COMPILE_##ID, \
llvm::opt::Option::KIND##Class, PARAM, FLAGS, VISIBILITY, \
OPT_COMPILE_##GROUP, OPT_COMPILE_##ALIAS, ALIASARGS, VALUES \
} \
,
#include "opencl_clang_options.inc"
};
OpenCLCompileOptTable::OpenCLCompileOptTable()
: OpenCLOptTable(ClangOptionsInfoTable) {}
int EffectiveOptionsFilter::s_progID = 1;
///
// Options filter that validates the opencl used options
//
std::string EffectiveOptionsFilter::processOptions(const OpenCLArgList &args,
const char *pszOptionsEx,
ArgsVector &effectiveArgs) {
// Reset args
int iCLStdSet = 0;
bool fp64Enabled = false;
std::string szTriple;
std::string sourceName(llvm::Twine(s_progID++).str());
for (OpenCLArgList::const_iterator it = args.begin(), ie = args.end();
it != ie; ++it) {
switch ((*it)->getOption().getID()) {
case OPT_COMPILE_w:
case OPT_COMPILE_D:
case OPT_COMPILE_I:
case OPT_COMPILE_Werror:
case OPT_COMPILE_cl_single_precision_constant:
case OPT_COMPILE_cl_fp32_correctly_rounded_divide_sqrt:
case OPT_COMPILE_cl_opt_disable:
case OPT_COMPILE_cl_mad_enable:
case OPT_COMPILE_cl_no_signed_zeros:
case OPT_COMPILE_cl_uniform_work_group_size:
case OPT_COMPILE_cl_unsafe_math_optimizations:
effectiveArgs.push_back((*it)->getAsString(args));
break;
case OPT_COMPILE_cl_denorms_are_zero:
effectiveArgs.push_back("-fdenormal-fp-math=preserve-sign");
break;
case OPT_COMPILE_s: {
std::string newSourceName = (*it)->getValue();
if (!newSourceName.empty()) {
sourceName = newSourceName;
// Normalize path to contain forward slashes
replace(sourceName.begin(), sourceName.end(), '\\', '/');
#ifdef _WIN32
// On Windows only, normalize the file name to lower case, since
// LLVM saves buffer names in a case-sensitive manner, while
// other Windows tools don't.
//
std::transform(sourceName.begin(), sourceName.end(), sourceName.begin(),
::tolower);
#endif
}
effectiveArgs.push_back("-main-file-name");
effectiveArgs.push_back(sourceName.substr(sourceName.rfind('/') + 1));
break;
}
case OPT_COMPILE_cl_finite_math_only:
effectiveArgs.push_back((*it)->getAsString(args));
effectiveArgs.push_back("-D");
effectiveArgs.push_back("__FINITE_MATH_ONLY__=1");
break;
case OPT_COMPILE_cl_fast_relaxed_math:
effectiveArgs.push_back((*it)->getAsString(args));
effectiveArgs.push_back("-D");
effectiveArgs.push_back("__FAST_RELAXED_MATH__=1");
break;
case OPT_COMPILE_cl_std_CL1_1:
iCLStdSet = 110;
effectiveArgs.push_back((*it)->getAsString(args));
break;
case OPT_COMPILE_cl_std_CL1_2:
iCLStdSet = 120;
effectiveArgs.push_back((*it)->getAsString(args));
break;
case OPT_COMPILE_cl_std_CL2_0:
iCLStdSet = 200;
effectiveArgs.push_back((*it)->getAsString(args));
break;
case OPT_COMPILE_cl_std_CL3_0:
iCLStdSet = 300;
effectiveArgs.push_back((*it)->getAsString(args));
break;
case OPT_COMPILE_triple:
szTriple = (*it)->getValue();
break;
case OPT_COMPILE_cl_no_subgroup_ifp:
case OPT_COMPILE_target_triple:
case OPT_COMPILE_spir_std_1_0:
case OPT_COMPILE_spir_std_1_2: // ignore for now
case OPT_COMPILE_cl_kernel_arg_info: // For SPIR, we always create kernel
// arg info, so ignoring it here
break;
case OPT_COMPILE_x:
// ensure that the value is spir
assert((*it)->getValue() == std::string("spir"));
// TODO: move the validation of the value to the check section of the
// option processing to be reported as an unknown option
break;
// Just ignore the unknown options ( they will be listed in the unknown
// list inside the ArgsList anyway)
// The below assert is usable for manual debugging only
// default:
// assert(false && "some unknown argument");
case OPT_COMPILE_coverage:
case OPT_COMPILE_profiling:
case OPT_COMPILE_gline_tables_only_Flag:
effectiveArgs.push_back("-debug-info-kind=line-tables-only");
effectiveArgs.push_back("-dwarf-version=4");
break;
case OPT_COMPILE_g_Flag:
effectiveArgs.push_back("-debug-info-kind=limited");
effectiveArgs.push_back("-dwarf-version=4");
#ifdef _WIN32
// Do not use column information on Windows.
effectiveArgs.push_back("-gno-column-info");
#endif
break;
}
}
if (!iCLStdSet) {
effectiveArgs.push_back("-cl-std=CL1.2");
iCLStdSet = 120;
}
effectiveArgs.push_back("-D");
effectiveArgs.push_back("__OPENCL_VERSION__=" + m_opencl_ver);
effectiveArgs.push_back("-x");
effectiveArgs.push_back("cl");
effectiveArgs.push_back("-cl-kernel-arg-info");
effectiveArgs.push_back("-fno-validate-pch");
effectiveArgs.push_back("-fno-caret-diagnostics");
if (std::find_if(effectiveArgs.begin(), effectiveArgs.end(),
[](const ArgsVector::value_type& a) {
return a == "-S" || a.find("-emit") == 0;
}) == effectiveArgs.end()) {
effectiveArgs.push_back("-emit-llvm-bc");
}
effectiveArgs.push_back("-triple");
if (szTriple.empty()) {
#if defined(_M_X64) || defined(__LP64__)
szTriple = "spir64-unknown-unknown";
#else
szTriple = "spir-unknown-unknown";
#endif
}
effectiveArgs.push_back(szTriple);
effectiveArgs.push_back("-include");
effectiveArgs.push_back("opencl-c.h");
// Don't optimize in the frontend
// clang defaults to -O0, and in that mode, does not produce IR that is
// intended to be optimized (e.g. produces 'optnone' attribute), so we are
// using '-disable-llvm-passes' to get out the unoptimized IR.
effectiveArgs.push_back("-disable-llvm-passes");
// Specifying the option makes clang emit function body for functions
// marked with inline keyword.
effectiveArgs.push_back("-fgnu89-inline");
// Do not support all extensions by default. Support for a particular
// extension should be enabled by passing a '-cl-ext' option in pszOptionsEx.
effectiveArgs.push_back("-cl-ext=-all");
// OpenCL v2.0 s6.9.u - Implicit function declaration is not supported.
// Behavior of clang is changed and now there is only warning about
// implicit function declarations. To be more user friendly and avoid
// unexpected indirect function calls in BE, let's force this warning to
// error.
effectiveArgs.push_back("-Werror=implicit-function-declaration");
// add the extended options verbatim
std::back_insert_iterator<ArgsVector> it(std::back_inserter(effectiveArgs));
quoted_tokenize(it, pszOptionsEx, " \t", '"', '\x00');
for (auto it = effectiveArgs.begin(), end = effectiveArgs.end(); it != end;
++it) {
if (it->compare("-Dcl_khr_fp64") == 0 || it->compare("-D cl_khr_fp64=1") == 0)
fp64Enabled = true;
else if (it->compare("-U cl_khr_fp64") == 0)
fp64Enabled = false;
// Find last position that enables or disables cl_khr_fp64
else if (it->find("cl_khr_fp64") != std::string::npos) {
auto NegFp64 = it->rfind("-cl_khr_fp64");
auto PosFp64 = it->rfind("+cl_khr_fp64");
if(NegFp64 != std::string::npos && PosFp64 != std::string::npos)
fp64Enabled = PosFp64 > NegFp64;
else if(NegFp64 != std::string::npos)
fp64Enabled = false;
else
fp64Enabled = true;
}
}
#ifdef PCH_EXTENSION
std::map<std::string, bool> extMap;
llvm::SmallVector<llvm::StringRef> extVec;
llvm::SplitString(PCH_EXTENSION, extVec, ",");
for (auto &ext : extVec)
extMap.insert({ext.str(), true});
#else
std::map<std::string, bool> extMap{
{"cl_khr_3d_image_writes", true},
{"cl_khr_depth_images", true},
{"cl_khr_fp16", true},
#ifdef _WIN32
// cl_khr_gl_msaa_sharing is only supported on Windows [NEO].
{"cl_khr_gl_msaa_sharing", true},
#endif
{"cl_khr_global_int32_base_atomics", true},
{"cl_khr_global_int32_extended_atomics", true},
{"cl_khr_int64_base_atomics", true},
{"cl_khr_int64_extended_atomics", true},
{"cl_khr_local_int32_base_atomics", true},
{"cl_khr_local_int32_extended_atomics", true},
{"cl_khr_mipmap_image", true},
{"cl_khr_mipmap_image_writes", true},
{"cl_khr_subgroups", true},
{"cl_intel_device_side_avc_motion_estimation", true},
{"cl_intel_planar_yuv", true},
{"cl_intel_subgroups", true},
{"cl_intel_subgroups_short", true}};
#endif
auto parseClExt = [&](const std::string &clExtStr) {
llvm::StringRef clExtRef(clExtStr);
bool hasPrefix = clExtRef.consume_front("-cl-ext=");
assert(hasPrefix && "clExtRef doesn't start with \"-cl-ext\" prefix");
(void)hasPrefix;
llvm::SmallVector<llvm::StringRef, 32> parsedExt;
clExtRef.split(parsedExt, ',');
for (auto &ext : parsedExt) {
char sign = ext.front();
bool enabled = sign != '-';
llvm::StringRef extName = ext;
if (sign == '+' || sign == '-')
extName = extName.drop_front();
if (extName == "all") {
for (auto &p : extMap)
p.second = enabled;
continue;
}
auto it = extMap.find(extName.str());
if (it != extMap.end())
it->second = enabled;
}
};
llvm::SmallSet<llvm::StringRef, 32> parsedOclCFeatures;
std::for_each(effectiveArgs.begin(), effectiveArgs.end(),
[&](const ArgsVector::value_type &a) {
if (a.find("-cl-ext=") == 0)
parseClExt(a);
else if (a.find("-D__opencl_c_") == 0)
parsedOclCFeatures.insert(a);
});
// "opencl-c-base.h" unconditionally enables a list of so-called "optional
// core" language features. We need to undef those that aren't explicitly
// defined within the compilation command (which would suggest that the
// target platform supports the corresponding feature).
const char* optionalCoreOclCFeaturesList[] = {
"__opencl_c_work_group_collective_functions",
"__opencl_c_atomic_order_seq_cst",
"__opencl_c_atomic_scope_device",
"__opencl_c_atomic_scope_all_devices",
"__opencl_c_read_write_images" };
for (std::string OclCFeature : optionalCoreOclCFeaturesList) {
if (!parsedOclCFeatures.contains(std::string("-D") + OclCFeature))
effectiveArgs.push_back(std::string("-D__undef_") + OclCFeature);
}
// extension is enabled in PCH but disabled or not specifed in options =>
// disable pch
bool useModules =
!std::any_of(extMap.begin(), extMap.end(),
[](const auto &p) { return p.second == false; });
if (useModules) {
effectiveArgs.push_back("-fmodules");
if (!fp64Enabled) {
if (szTriple.find("spir64") != szTriple.npos) {
if (iCLStdSet <= 120)
effectiveArgs.push_back("-fmodule-file=opencl-c-12-spir64.pcm");
else if (iCLStdSet == 200)
effectiveArgs.push_back("-fmodule-file=opencl-c-20-spir64.pcm");
else if (iCLStdSet == 300)
effectiveArgs.push_back("-fmodule-file=opencl-c-30-spir64.pcm");
} else if (szTriple.find("spir") != szTriple.npos) {
if (iCLStdSet <= 120)
effectiveArgs.push_back("-fmodule-file=opencl-c-12-spir.pcm");
else if (iCLStdSet == 200)
effectiveArgs.push_back("-fmodule-file=opencl-c-20-spir.pcm");
else if (iCLStdSet == 300)
effectiveArgs.push_back("-fmodule-file=opencl-c-30-spir.pcm");
}
} else {
if (szTriple.find("spir64") != szTriple.npos) {
if (iCLStdSet <= 120)
effectiveArgs.push_back("-fmodule-file=opencl-c-12-spir64-fp64.pcm");
else if (iCLStdSet == 200)
effectiveArgs.push_back("-fmodule-file=opencl-c-20-spir64-fp64.pcm");
else if (iCLStdSet == 300)
effectiveArgs.push_back("-fmodule-file=opencl-c-30-spir64-fp64.pcm");
} else if (szTriple.find("spir") != szTriple.npos) {
if (iCLStdSet <= 120)
effectiveArgs.push_back("-fmodule-file=opencl-c-12-spir-fp64.pcm");
else if (iCLStdSet == 200)
effectiveArgs.push_back("-fmodule-file=opencl-c-20-spir-fp64.pcm");
else if (iCLStdSet == 300)
effectiveArgs.push_back("-fmodule-file=opencl-c-30-spir-fp64.pcm");
}
}
}
// add source name to options as an input file
assert(!sourceName.empty() && "Empty source name.");
effectiveArgs.push_back(sourceName);
return sourceName;
}
void CompileOptionsParser::processOptions(const char *pszOptions,
const char *pszOptionsEx) {
// parse options
unsigned missingArgIndex, missingArgCount;
std::unique_ptr<OpenCLArgList> pArgs(
m_optTbl.ParseArgs(pszOptions, missingArgIndex, missingArgCount));
// post process logic
m_sourceName =
m_commonFilter.processOptions(*pArgs, pszOptionsEx, m_effectiveArgs);
// build the raw options array
for (ArgsVector::iterator it = m_effectiveArgs.begin(),
end = m_effectiveArgs.end();
it != end; ++it) {
if (it->compare("-cl-opt-disable") == 0) {
m_optDisable = true;
}
else if (it->compare("-emit-spirv") == 0) {
m_effectiveArgsRaw.push_back("-emit-llvm-bc");
m_emitSPIRV = true;
continue;
}
m_effectiveArgsRaw.push_back(it->c_str());
}
}
bool CompileOptionsParser::checkOptions(const char *pszOptions,
char *pszUnknownOptions,
size_t uiUnknownOptionsSize) {
// Parse the arguments.
unsigned missingArgIndex, missingArgCount;
std::unique_ptr<OpenCLArgList> pArgs(
m_optTbl.ParseArgs(pszOptions, missingArgIndex, missingArgCount));
// Check for missing argument error.
if (missingArgCount) {
std::fill_n(pszUnknownOptions, uiUnknownOptionsSize, '\0');
std::string missingArg(pArgs->getArgString(missingArgIndex));
missingArg.copy(pszUnknownOptions, uiUnknownOptionsSize - 1);
return false;
}
std::string unknownOptions = pArgs->getFilteredArgs(OPT_COMPILE_UNKNOWN);
if (!unknownOptions.empty()) {
std::fill_n(pszUnknownOptions, uiUnknownOptionsSize, '\0');
unknownOptions.copy(pszUnknownOptions, uiUnknownOptionsSize - 1);
return false;
}
// we do not support input options
std::string inputOptions = pArgs->getFilteredArgs(OPT_COMPILE_INPUT);
if (!inputOptions.empty()) {
std::fill_n(pszUnknownOptions, uiUnknownOptionsSize, '\0');
inputOptions.copy(pszUnknownOptions, uiUnknownOptionsSize - 1);
return false;
}
return true;
}
std::string CompileOptionsParser::getEffectiveOptionsAsString() const {
std::stringstream ss;
const char *const *it = beginArgs();
const char *const *ie = endArgs();
for (; it != ie; ++it) {
ss << *it << " ";
}
return ss.str();
}
extern "C" CC_DLL_EXPORT bool CheckCompileOptions(const char *pszOptions,
char *pszUnknownOptions,
size_t uiUnknownOptionsSize) {
// LLVM doesn't guarantee thread safety,
// therefore we serialize execution of LLVM code.
llvm::sys::SmartScopedLock<true> compileOptionsGuard{*compileMutex};
try {
CompileOptionsParser optionsParser("200");
return optionsParser.checkOptions(pszOptions, pszUnknownOptions,
uiUnknownOptionsSize);
} catch (std::bad_alloc &) {
if (pszUnknownOptions && uiUnknownOptionsSize > 0) {
std::fill_n(pszUnknownOptions, uiUnknownOptionsSize, '\0');
}
return false;
}
}