14#include "llvm/ADT/StringExtras.h"
15#include "llvm/ADT/StringMap.h"
16#include "llvm/Option/ArgList.h"
17#include "llvm/TargetParser/Host.h"
25 const llvm::Triple &Triple) {
26 if (
const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ)) {
27 StringRef CPU = A->getValue();
29 return std::string(CPU);
33 CPU = llvm::sys::getHostCPUName();
34 if (!CPU.empty() && CPU !=
"generic")
35 return std::string(CPU);
38 if (
const Arg *A = Args.getLastArg(options::OPT__SLASH_arch)) {
42 llvm::StringMap<StringRef> ArchMap({
43 {
"AVX",
"sandybridge"},
46 {
"AVX512",
"skylake-avx512"},
48 if (Triple.getArch() == llvm::Triple::x86) {
56 StringRef CPU = ArchMap.lookup(A->getValue());
58 std::vector<StringRef> ValidArchs{ArchMap.keys().begin(),
59 ArchMap.keys().end()};
61 D.Diag(diag::warn_drv_invalid_arch_name_with_suggestion)
62 << A->getValue() << (Triple.getArch() == llvm::Triple::x86)
63 << join(ValidArchs,
", ");
65 return std::string(CPU);
73 bool Is64Bit = Triple.getArch() == llvm::Triple::x86_64;
76 if (Triple.isOSDarwin()) {
77 if (Triple.getArchName() ==
"x86_64h")
81 if (Triple.isMacOSX() && !Triple.isOSVersionLT(10, 12))
84 if (Triple.isDriverKit())
88 return Is64Bit ?
"core2" :
"yonah";
98 if (Triple.isAndroid())
99 return Is64Bit ?
"x86-64" :
"i686";
105 switch (Triple.getOS()) {
106 case llvm::Triple::NetBSD:
108 case llvm::Triple::Haiku:
109 case llvm::Triple::OpenBSD:
111 case llvm::Triple::FreeBSD:
121 std::vector<StringRef> &Features) {
124 if (
const Arg *A = Args.getLastArg(clang::driver::options::OPT_mabi_EQ)) {
125 StringRef DefaultAbi = Triple.isOSWindows() ?
"ms" :
"sysv";
126 if (A->getValue() != DefaultAbi)
127 D.Diag(diag::err_drv_unsupported_opt_for_target)
128 << A->getSpelling() << Triple.getTriple();
132 if (
const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ)) {
133 if (StringRef(A->getValue()) ==
"native") {
134 for (
auto &F : llvm::sys::getHostCPUFeatures())
136 Args.MakeArgString((F.second ?
"+" :
"-") + F.first()));
140 if (Triple.getArchName() ==
"x86_64h") {
143 Features.push_back(
"-rdrnd");
144 Features.push_back(
"-aes");
145 Features.push_back(
"-pclmul");
146 Features.push_back(
"-rtm");
147 Features.push_back(
"-fsgsbase");
150 const llvm::Triple::ArchType ArchType = Triple.getArch();
152 if (Triple.isAndroid()) {
153 if (ArchType == llvm::Triple::x86_64) {
154 Features.push_back(
"+sse4.2");
155 Features.push_back(
"+popcnt");
156 Features.push_back(
"+cx16");
158 Features.push_back(
"+ssse3");
168 if (Args.hasArgNoClaim(options::OPT_mretpoline, options::OPT_mno_retpoline,
169 options::OPT_mspeculative_load_hardening,
170 options::OPT_mno_speculative_load_hardening)) {
171 if (Args.hasFlag(options::OPT_mretpoline, options::OPT_mno_retpoline,
173 Features.push_back(
"+retpoline-indirect-calls");
174 Features.push_back(
"+retpoline-indirect-branches");
175 SpectreOpt = options::OPT_mretpoline;
176 }
else if (Args.hasFlag(options::OPT_mspeculative_load_hardening,
177 options::OPT_mno_speculative_load_hardening,
181 Features.push_back(
"+retpoline-indirect-calls");
182 SpectreOpt = options::OPT_mspeculative_load_hardening;
184 }
else if (Args.hasFlag(options::OPT_mretpoline_external_thunk,
185 options::OPT_mno_retpoline_external_thunk,
false)) {
188 Features.push_back(
"+retpoline-indirect-calls");
189 Features.push_back(
"+retpoline-indirect-branches");
190 SpectreOpt = options::OPT_mretpoline_external_thunk;
194 if (Args.hasFlag(options::OPT_mlvi_hardening, options::OPT_mno_lvi_hardening,
196 Features.push_back(
"+lvi-load-hardening");
197 Features.push_back(
"+lvi-cfi");
198 LVIOpt = options::OPT_mlvi_hardening;
199 }
else if (Args.hasFlag(options::OPT_mlvi_cfi, options::OPT_mno_lvi_cfi,
201 Features.push_back(
"+lvi-cfi");
202 LVIOpt = options::OPT_mlvi_cfi;
205 if (Args.hasFlag(options::OPT_m_seses, options::OPT_mno_seses,
false)) {
206 if (LVIOpt == options::OPT_mlvi_hardening)
207 D.Diag(diag::err_drv_argument_not_allowed_with)
208 <<
D.getOpts().getOptionName(options::OPT_mlvi_hardening)
209 <<
D.getOpts().getOptionName(options::OPT_m_seses);
212 D.Diag(diag::err_drv_argument_not_allowed_with)
213 <<
D.getOpts().getOptionName(SpectreOpt)
214 <<
D.getOpts().getOptionName(options::OPT_m_seses);
216 Features.push_back(
"+seses");
217 if (!Args.hasArg(options::OPT_mno_lvi_cfi)) {
218 Features.push_back(
"+lvi-cfi");
219 LVIOpt = options::OPT_mlvi_cfi;
225 D.Diag(diag::err_drv_argument_not_allowed_with)
226 <<
D.getOpts().getOptionName(SpectreOpt)
227 <<
D.getOpts().getOptionName(LVIOpt);
230 for (
const Arg *A : Args.filtered(options::OPT_m_x86_AVX10_Features_Group)) {
231 StringRef Name = A->getOption().getName();
235 assert(Name.starts_with(
"m") &&
"Invalid feature name.");
236 Name = Name.substr(1);
238 bool IsNegative = Name.consume_front(
"no-");
241 assert(Name.starts_with(
"avx10.") &&
"Invalid AVX10 feature name.");
242 StringRef Version, Width;
243 std::tie(Version, Width) = Name.substr(6).split(
'-');
244 assert((Version ==
"1" || Version ==
"2") &&
"Invalid AVX10 feature name.");
245 assert((Width ==
"256" || Width ==
"512") &&
"Invalid AVX10 feature name.");
248 Features.push_back(Args.MakeArgString((IsNegative ?
"-" :
"+") + Name));
253 for (
const Arg *A : Args.filtered(options::OPT_m_x86_Features_Group,
254 options::OPT_mgeneral_regs_only)) {
255 StringRef Name = A->getOption().getName();
259 assert(Name.starts_with(
"m") &&
"Invalid feature name.");
260 Name = Name.substr(1);
263 if (A->getOption().getID() == options::OPT_mgeneral_regs_only) {
264 Features.insert(Features.end(), {
"-x87",
"-mmx",
"-sse"});
268 bool IsNegative = Name.starts_with(
"no-");
270 bool Not64Bit = ArchType != llvm::Triple::x86_64;
271 if (Not64Bit && Name ==
"uintr")
272 D.Diag(diag::err_drv_unsupported_opt_for_target)
273 << A->getSpelling() << Triple.getTriple();
275 if (A->getOption().matches(options::OPT_mapx_features_EQ) ||
276 A->getOption().matches(options::OPT_mno_apx_features_EQ)) {
278 if (Not64Bit && !IsNegative)
279 D.Diag(diag::err_drv_unsupported_opt_for_target)
280 << StringRef(A->getSpelling().str() +
"|-mapxf")
281 << Triple.getTriple();
283 for (StringRef
Value : A->getValues()) {
287 D.Diag(clang::diag::err_drv_unsupported_option_argument)
288 << A->getSpelling() <<
Value;
291 Args.MakeArgString((IsNegative ?
"-" :
"+") +
Value));
296 Name = Name.substr(3);
297 Features.push_back(Args.MakeArgString((IsNegative ?
"-" :
"+") + Name));
301 if (Arg *A = Args.getLastArg(options::OPT_mharden_sls_EQ)) {
302 StringRef
Scope = A->getValue();
303 if (
Scope ==
"all") {
304 Features.push_back(
"+harden-sls-ijmp");
305 Features.push_back(
"+harden-sls-ret");
306 }
else if (
Scope ==
"return") {
307 Features.push_back(
"+harden-sls-ret");
308 }
else if (
Scope ==
"indirect-jmp") {
309 Features.push_back(
"+harden-sls-ijmp");
310 }
else if (
Scope !=
"none") {
311 D.Diag(diag::err_drv_unsupported_option_argument)
312 << A->getSpelling() <<
Scope;
317 if (Args.hasArg(options::OPT_mno_gather))
318 Features.push_back(
"+prefer-no-gather");
319 if (Args.hasArg(options::OPT_mno_scatter))
320 Features.push_back(
"+prefer-no-scatter");
321 if (Args.hasArg(options::OPT_mapx_inline_asm_use_gpr32))
322 Features.push_back(
"+inline-asm-use-gpr32");
326 Args.getLastArg(options::OPT_m3dnowa, options::OPT_mno_3dnowa,
327 options::OPT_mno_3dnow)) {
328 if (A->getOption().matches(options::OPT_m3dnowa))
329 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
332 Args.getLastArg(options::OPT_m3dnow, options::OPT_mno_3dnow)) {
333 if (A->getOption().matches(options::OPT_m3dnow))
334 D.Diag(diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Scope - A scope is a transient data structure that is used while parsing the program.
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
The JSON file list parser is used to communicate input to InstallAPI.