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
|
/*****************************************************************************\
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 "common_clang.h"
#include "options.h"
#include "clang/Driver/Options.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Mutex.h"
#include <sstream>
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
HELPTEXT, METAVAR, VALUES)
#include "opencl_clang_options.inc"
#undef OPTION
#undef PREFIX
using namespace llvm::opt;
static llvm::ManagedStatic<llvm::sys::SmartMutex<true> > compileOptionsMutex;
static const OptTable::Info ClangOptionsInfoTable[] = {
#define PREFIX(NAME, VALUE)
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
HELPTEXT, METAVAR, VALUES) \
{ \
PREFIX, NAME, HELPTEXT, METAVAR, OPT_COMPILE_##ID, \
llvm::opt::Option::KIND##Class, PARAM, FLAGS, OPT_COMPILE_##GROUP, \
OPT_COMPILE_##ALIAS, ALIASARGS \
} \
,
#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;
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_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_uniform_work_group_size:
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_profiling:
case OPT_COMPILE_g_Flag:
effectiveArgs.push_back("-debug-info-kind=limited");
effectiveArgs.push_back("-dwarf-version=4");
break;
case OPT_COMPILE_gline_tables_only_Flag:
effectiveArgs.push_back("-debug-info-kind=line-tables-only");
effectiveArgs.push_back("-dwarf-version=4");
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 IR, 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');
// 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 {*compileOptionsMutex};
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;
}
}
|