Index: include/clang/Driver/Options.td =================================================================== --- include/clang/Driver/Options.td +++ include/clang/Driver/Options.td @@ -1999,6 +1999,9 @@ def mno_soft_float : Flag<["-"], "mno-soft-float">, Group; def mno_stackrealign : Flag<["-"], "mno-stackrealign">, Group; +def mretpoline : Flag<["-"], "mretpoline">, Group, Flags<[CoreOption,DriverOption]>; +def mno_retpoline : Flag<["-"], "mno-retpoline">, Group, Flags<[CoreOption,DriverOption]>; + def mrelax : Flag<["-"], "mrelax">, Group, HelpText<"Enable linker relaxation">; def mno_relax : Flag<["-"], "mno-relax">, Group, @@ -2824,8 +2827,6 @@ def mno_xsaves : Flag<["-"], "mno-xsaves">, Group; def mshstk : Flag<["-"], "mshstk">, Group; def mno_shstk : Flag<["-"], "mno-shstk">, Group; -def mretpoline : Flag<["-"], "mretpoline">, Group; -def mno_retpoline : Flag<["-"], "mno-retpoline">, Group; def mretpoline_external_thunk : Flag<["-"], "mretpoline-external-thunk">, Group; def mno_retpoline_external_thunk : Flag<["-"], "mno-retpoline-external-thunk">, Group; Index: lib/Basic/Targets/X86.h =================================================================== --- lib/Basic/Targets/X86.h +++ lib/Basic/Targets/X86.h @@ -98,7 +98,6 @@ bool HasMOVBE = false; bool HasPREFETCHWT1 = false; bool HasRDPID = false; - bool HasRetpoline = false; bool HasRetpolineExternalThunk = false; bool HasLAHFSAHF = false; bool HasWBNOINVD = false; Index: lib/Basic/Targets/X86.cpp =================================================================== --- lib/Basic/Targets/X86.cpp +++ lib/Basic/Targets/X86.cpp @@ -796,8 +796,6 @@ HasCLDEMOTE = true; } else if (Feature == "+rdpid") { HasRDPID = true; - } else if (Feature == "+retpoline") { - HasRetpoline = true; } else if (Feature == "+retpoline-external-thunk") { HasRetpolineExternalThunk = true; } else if (Feature == "+sahf") { @@ -1397,7 +1395,6 @@ .Case("rdpid", HasRDPID) .Case("rdrnd", HasRDRND) .Case("rdseed", HasRDSEED) - .Case("retpoline", HasRetpoline) .Case("retpoline-external-thunk", HasRetpolineExternalThunk) .Case("rtm", HasRTM) .Case("sahf", HasLAHFSAHF) Index: lib/Driver/ToolChains/Arch/X86.cpp =================================================================== --- lib/Driver/ToolChains/Arch/X86.cpp +++ lib/Driver/ToolChains/Arch/X86.cpp @@ -144,6 +144,26 @@ Features.push_back("+ssse3"); } + // Translate the high level `-mretpoline` flag to the specific target feature + // flags. We also detect if the user asked for retpoline external thunks but + // failed to ask for retpolines themselves. This is a bit hacky but keeps + // existing usages working. We should consider deprecated this and instead + // warning if the user requests external retpoline thunks and *doesn't* + // request some form of retpolines. + if (Args.hasArgNoClaim(options::OPT_mretpoline, options::OPT_mno_retpoline)) { + if (Args.hasFlag(options::OPT_mretpoline, options::OPT_mno_retpoline, + false)) { + Features.push_back("+retpoline-indirect-calls"); + Features.push_back("+retpoline-indirect-branches"); + } + } else if (Args.hasFlag(options::OPT_mretpoline_external_thunk, + options::OPT_mno_retpoline_external_thunk, false)) { + // FIXME: Add a warning about failing to specify `-mretpoline` and + // eventually switch to an error here. + Features.push_back("+retpoline-indirect-calls"); + Features.push_back("+retpoline-indirect-branches"); + } + // Now add any that the user explicitly requested on the command line, // which may override the defaults. handleTargetFeaturesGroup(Args, Features, options::OPT_m_x86_Features_Group); Index: test/Driver/x86-target-features.c =================================================================== --- test/Driver/x86-target-features.c +++ test/Driver/x86-target-features.c @@ -132,8 +132,8 @@ // RUN: %clang -target i386-linux-gnu -mretpoline %s -### -o %t.o 2>&1 | FileCheck -check-prefix=RETPOLINE %s // RUN: %clang -target i386-linux-gnu -mno-retpoline %s -### -o %t.o 2>&1 | FileCheck -check-prefix=NO-RETPOLINE %s -// RETPOLINE: "-target-feature" "+retpoline" -// NO-RETPOLINE: "-target-feature" "-retpoline" +// RETPOLINE: "-target-feature" "+retpoline-indirect-calls" "-target-feature" "+retpoline-indirect-branches" +// NO-RETPOLINE-NOT: retpoline // RUN: %clang -target i386-linux-gnu -mretpoline -mretpoline-external-thunk %s -### -o %t.o 2>&1 | FileCheck -check-prefix=RETPOLINE-EXTERNAL-THUNK %s // RUN: %clang -target i386-linux-gnu -mretpoline -mno-retpoline-external-thunk %s -### -o %t.o 2>&1 | FileCheck -check-prefix=NO-RETPOLINE-EXTERNAL-THUNK %s