[clang] fbea5aa - [Driver] Add ClangFlags::TargetSpecific to simplify err_drv_unsupported_opt_for_target processing
Fangrui Song via cfe-commits
cfe-commits at lists.llvm.org
Tue May 30 11:21:23 PDT 2023
Author: Fangrui Song
Date: 2023-05-30T11:21:17-07:00
New Revision: fbea5aada14315da14c2e296831b1cb1cc1ddd61
URL: https://github.com/llvm/llvm-project/commit/fbea5aada14315da14c2e296831b1cb1cc1ddd61
DIFF: https://github.com/llvm/llvm-project/commit/fbea5aada14315da14c2e296831b1cb1cc1ddd61.diff
LOG: [Driver] Add ClangFlags::TargetSpecific to simplify err_drv_unsupported_opt_for_target processing
clang/lib/Driver/ToolChains/Clang.cpp has a lot of fragments like the following:
```
if (const Arg *A = Args.getLastArg(...)) {
if (Triple is xxx)
A->render(Args, CmdArgs);
else
D.Diag(diag::err_drv_unsupported_opt_for_target) << ...;
}
```
The problem is more apparent with a recent surge of AIX-specific options.
Introduce the TargetSpecific flag so that we can move the target-specific
options to ToolChains/*.cpp and ToolChains/Arch/*.cpp and overload the
warn_drv_unused_argument mechanism to give an err_drv_unsupported_opt_for_target
error.
Migrate -march=/-mcpu= and some AIX-specific options to use this simplified pattern.
Reviewed By: jansvoboda11
Differential Revision: https://reviews.llvm.org/D151590
Added:
Modified:
clang/include/clang/Driver/Options.h
clang/include/clang/Driver/Options.td
clang/lib/Driver/Driver.cpp
clang/lib/Driver/ToolChains/AIX.cpp
clang/lib/Driver/ToolChains/AIX.h
clang/lib/Driver/ToolChains/Arch/PPC.cpp
clang/lib/Driver/ToolChains/Arch/Sparc.cpp
clang/lib/Driver/ToolChains/Clang.cpp
clang/lib/Driver/ToolChains/CommonArgs.cpp
clang/test/Driver/mdefault-visibility-export-mapping.c
Removed:
################################################################################
diff --git a/clang/include/clang/Driver/Options.h b/clang/include/clang/Driver/Options.h
index f7ee154b7a7ab..54c6f5faa37c2 100644
--- a/clang/include/clang/Driver/Options.h
+++ b/clang/include/clang/Driver/Options.h
@@ -38,6 +38,7 @@ enum ClangFlags {
DXCOption = (1 << 17),
CLDXCOption = (1 << 18),
Ignored = (1 << 19),
+ TargetSpecific = (1 << 20),
};
enum ID {
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 93732f2b0768a..f3bfc26f271cc 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -75,6 +75,10 @@ def FlangOnlyOption : OptionFlag;
// FC1Option - This option should be accepted by flang -fc1.
def FC1Option : OptionFlag;
+// This is a target-specific option for compilation. Using it on an unsupported
+// target will lead to an err_drv_unsupported_opt_for_target error.
+def TargetSpecific : OptionFlag;
+
// A short name to show in documentation. The name will be interpreted as rST.
class DocName<string name> { string DocName = name; }
@@ -89,6 +93,8 @@ class DocFlatten { bit DocFlatten = 1; }
// GCC compatibility.
class IgnoredGCCCompat : Flags<[HelpHidden]> {}
+class TargetSpecific : Flags<[TargetSpecific]> {}
+
/////////
// Groups
@@ -3123,7 +3129,7 @@ def mdefault_visibility_export_mapping_EQ : Joined<["-"], "mdefault-visibility-e
NormalizedValuesScope<"LangOptions::DefaultVisiblityExportMapping">,
NormalizedValues<["None", "Explicit", "All"]>,
HelpText<"Mapping between default visibility and export">,
- Group<m_Group>, Flags<[CC1Option]>,
+ Group<m_Group>, Flags<[CC1Option,TargetSpecific]>,
MarshallingInfoEnum<LangOpts<"DefaultVisibilityExportMapping">,"None">;
defm new_infallible : BoolFOption<"new-infallible",
LangOpts<"NewInfallible">, DefaultFalse,
@@ -3507,7 +3513,7 @@ def mappletvsimulator_version_min_EQ : Joined<["-"], "mappletvsimulator-version-
def mwatchos_version_min_EQ : Joined<["-"], "mwatchos-version-min=">, Group<m_Group>;
def mwatchos_simulator_version_min_EQ : Joined<["-"], "mwatchos-simulator-version-min=">;
def mwatchsimulator_version_min_EQ : Joined<["-"], "mwatchsimulator-version-min=">, Alias<mwatchos_simulator_version_min_EQ>;
-def march_EQ : Joined<["-"], "march=">, Group<m_Group>, Flags<[CoreOption]>,
+def march_EQ : Joined<["-"], "march=">, Group<m_Group>, Flags<[CoreOption,TargetSpecific]>,
HelpText<"For a list of available architectures for the target use '-mcpu=help'">;
def masm_EQ : Joined<["-"], "masm=">, Group<m_Group>, Flags<[NoXarchOption]>;
def inline_asm_EQ : Joined<["-"], "inline-asm=">, Group<m_Group>, Flags<[CC1Option]>,
@@ -3532,7 +3538,7 @@ def mthreads : Joined<["-"], "mthreads">, Group<m_Group>, Flags<[NoXarchOption]>
def mguard_EQ : Joined<["-"], "mguard=">, Group<m_Group>, Flags<[NoXarchOption]>,
HelpText<"Enable or disable Control Flow Guard checks and guard tables emission">,
Values<"none,cf,cf-nochecks">;
-def mcpu_EQ : Joined<["-"], "mcpu=">, Group<m_Group>,
+def mcpu_EQ : Joined<["-"], "mcpu=">, Group<m_Group>, TargetSpecific,
HelpText<"For a list of available CPUs for the target use '-mcpu=help'">;
def mmcu_EQ : Joined<["-"], "mmcu=">, Group<m_Group>;
def msim : Flag<["-"], "msim">, Group<m_Group>;
@@ -3925,9 +3931,9 @@ def maix_struct_return : Flag<["-"], "maix-struct-return">,
def msvr4_struct_return : Flag<["-"], "msvr4-struct-return">,
Group<m_Group>, Flags<[CC1Option]>,
HelpText<"Return small structs in registers (PPC32 only)">;
-def mxcoff_roptr : Flag<["-"], "mxcoff-roptr">, Group<m_Group>, Flags<[CC1Option]>,
+def mxcoff_roptr : Flag<["-"], "mxcoff-roptr">, Group<m_Group>, Flags<[CC1Option,TargetSpecific]>,
HelpText<"Place constant objects with relocatable address values in the RO data section and add -bforceimprw to the linker flags (AIX only)">;
-def mno_xcoff_roptr : Flag<["-"], "mno-xcoff-roptr">, Group<m_Group>;
+def mno_xcoff_roptr : Flag<["-"], "mno-xcoff-roptr">, Group<m_Group>, TargetSpecific;
def mvx : Flag<["-"], "mvx">, Group<m_Group>;
def mno_vx : Flag<["-"], "mno-vx">, Group<m_Group>;
@@ -3943,7 +3949,7 @@ def mxcoff_build_id_EQ : Joined<["-"], "mxcoff-build-id=">, Group<Link_Group>, M
HelpText<"On AIX, request creation of a build-id string, \"0xHEXSTRING\", in the string table of the loader section inside the linked binary">;
def mignore_xcoff_visibility : Flag<["-"], "mignore-xcoff-visibility">, Group<m_Group>,
HelpText<"Not emit the visibility attribute for asm in AIX OS or give all symbols 'unspecified' visibility in XCOFF object file">,
- Flags<[CC1Option]>;
+ Flags<[CC1Option,TargetSpecific]>;
defm backchain : BoolOption<"m", "backchain",
CodeGenOpts<"Backchain">, DefaultFalse,
PosFlag<SetTrue, [], "Link stack frames through backchain on System Z">,
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 9f26ed676224b..ade59f45384fd 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -4885,9 +4885,15 @@ void Driver::BuildJobs(Compilation &C) const {
// In clang-cl, don't mention unknown arguments here since they have
// already been warned about.
- if (!IsCLMode() || !A->getOption().matches(options::OPT_UNKNOWN))
- Diag(clang::diag::warn_drv_unused_argument)
- << A->getAsString(C.getArgs());
+ if (!IsCLMode() || !A->getOption().matches(options::OPT_UNKNOWN)) {
+ if (A->getOption().hasFlag(options::TargetSpecific)) {
+ Diag(diag::err_drv_unsupported_opt_for_target)
+ << A->getSpelling() << getTargetTriple();
+ } else {
+ Diag(clang::diag::warn_drv_unused_argument)
+ << A->getAsString(C.getArgs());
+ }
+ }
}
}
}
diff --git a/clang/lib/Driver/ToolChains/AIX.cpp b/clang/lib/Driver/ToolChains/AIX.cpp
index 46ad8231764db..ad7f3edeb9384 100644
--- a/clang/lib/Driver/ToolChains/AIX.cpp
+++ b/clang/lib/Driver/ToolChains/AIX.cpp
@@ -409,6 +409,18 @@ void AIX::AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
llvm_unreachable("Unexpected C++ library type; only libc++ is supported.");
}
+void AIX::addClangTargetOptions(
+ const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CC1Args,
+ Action::OffloadKind DeviceOffloadingKind) const {
+ Args.AddLastArg(CC1Args, options::OPT_mignore_xcoff_visibility);
+ Args.AddLastArg(CC1Args, options::OPT_mdefault_visibility_export_mapping_EQ);
+ Args.addOptInFlag(CC1Args, options::OPT_mxcoff_roptr, options::OPT_mno_xcoff_roptr);
+
+ if (Args.hasFlag(options::OPT_fxl_pragma_pack,
+ options::OPT_fno_xl_pragma_pack, true))
+ CC1Args.push_back("-fxl-pragma-pack");
+}
+
void AIX::addProfileRTLibs(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) const {
// Add linker option -u__llvm_profile_runtime to cause runtime
diff --git a/clang/lib/Driver/ToolChains/AIX.h b/clang/lib/Driver/ToolChains/AIX.h
index e03aebcc3e7f0..cc74e5ea85efc 100644
--- a/clang/lib/Driver/ToolChains/AIX.h
+++ b/clang/lib/Driver/ToolChains/AIX.h
@@ -80,6 +80,10 @@ class LLVM_LIBRARY_VISIBILITY AIX : public ToolChain {
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) const override;
+ void addClangTargetOptions(
+ const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CC1Args,
+ Action::OffloadKind DeviceOffloadingKind) const override;
+
void addProfileRTLibs(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) const override;
diff --git a/clang/lib/Driver/ToolChains/Arch/PPC.cpp b/clang/lib/Driver/ToolChains/Arch/PPC.cpp
index befbd365fd03f..ab24d14992cd7 100644
--- a/clang/lib/Driver/ToolChains/Arch/PPC.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/PPC.cpp
@@ -87,10 +87,6 @@ std::string ppc::getPPCTuneCPU(const ArgList &Args, const llvm::Triple &T) {
/// Get the (LLVM) name of the PowerPC cpu we are targeting.
std::string ppc::getPPCTargetCPU(const Driver &D, const ArgList &Args,
const llvm::Triple &T) {
- if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ)) {
- D.Diag(diag::err_drv_unsupported_opt_for_target)
- << A->getSpelling() << T.getTriple();
- }
if (Arg *A = Args.getLastArg(clang::driver::options::OPT_mcpu_EQ))
return normalizeCPUName(A->getValue(), T);
return getPPCGenericTargetCPU(T);
diff --git a/clang/lib/Driver/ToolChains/Arch/Sparc.cpp b/clang/lib/Driver/ToolChains/Arch/Sparc.cpp
index e775599e8f5f7..11c9444fde2b1 100644
--- a/clang/lib/Driver/ToolChains/Arch/Sparc.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/Sparc.cpp
@@ -118,12 +118,6 @@ sparc::FloatABI sparc::getSparcFloatABI(const Driver &D,
std::string sparc::getSparcTargetCPU(const Driver &D, const ArgList &Args,
const llvm::Triple &Triple) {
- if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ)) {
- D.Diag(diag::err_drv_unsupported_opt_for_target)
- << A->getSpelling() << Triple.getTriple();
- return "";
- }
-
if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_mcpu_EQ)) {
StringRef CPUName = A->getValue();
if (CPUName == "native") {
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index e9d49fb556416..d5e8718641754 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -5275,19 +5275,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
<< A->getSpelling() << RawTriple.str();
}
- if (Args.hasArg(options::OPT_mxcoff_roptr) ||
- Args.hasArg(options::OPT_mno_xcoff_roptr)) {
- bool HasRoptr = Args.hasFlag(options::OPT_mxcoff_roptr,
- options::OPT_mno_xcoff_roptr, false);
- StringRef OptStr = HasRoptr ? "-mxcoff-roptr" : "-mno-xcoff-roptr";
- if (!Triple.isOSAIX())
- D.Diag(diag::err_drv_unsupported_opt_for_target)
- << OptStr << RawTriple.str();
-
- if (HasRoptr)
- CmdArgs.push_back("-mxcoff-roptr");
- }
-
if (Arg *A = Args.getLastArg(options::OPT_Wframe_larger_than_EQ)) {
StringRef V = A->getValue(), V1 = V;
unsigned Size;
@@ -6147,23 +6134,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
}
}
- if (const Arg *A = Args.getLastArg(options::OPT_mignore_xcoff_visibility)) {
- if (Triple.isOSAIX())
- CmdArgs.push_back("-mignore-xcoff-visibility");
- else
- D.Diag(diag::err_drv_unsupported_opt_for_target)
- << A->getAsString(Args) << TripleStr;
- }
-
- if (const Arg *A =
- Args.getLastArg(options::OPT_mdefault_visibility_export_mapping_EQ)) {
- if (Triple.isOSAIX())
- A->render(Args, CmdArgs);
- else
- D.Diag(diag::err_drv_unsupported_opt_for_target)
- << A->getAsString(Args) << TripleStr;
- }
-
if (Args.hasFlag(options::OPT_fvisibility_inlines_hidden,
options::OPT_fno_visibility_inlines_hidden, false))
CmdArgs.push_back("-fvisibility-inlines-hidden");
@@ -6976,10 +6946,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Args.addOptInFlag(CmdArgs, options::OPT_fapple_pragma_pack,
options::OPT_fno_apple_pragma_pack);
- if (Args.hasFlag(options::OPT_fxl_pragma_pack,
- options::OPT_fno_xl_pragma_pack, RawTriple.isOSAIX()))
- CmdArgs.push_back("-fxl-pragma-pack");
-
// Remarks can be enabled with any of the `-f.*optimization-record.*` flags.
if (willEmitRemarks(Args) && checkRemarksOptions(D, Args, Triple))
renderRemarksOptions(Args, CmdArgs, Triple, Input, Output, JA);
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 1ed93ba8b61b8..57bf345f1708e 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -337,6 +337,7 @@ void tools::AddTargetFeature(const ArgList &Args,
/// Get the (LLVM) name of the AMDGPU gpu we are targeting.
static std::string getAMDGPUTargetGPU(const llvm::Triple &T,
const ArgList &Args) {
+ Arg *MArch = Args.getLastArg(options::OPT_march_EQ);
if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) {
auto GPUName = getProcessorFromTargetID(T, A->getValue());
return llvm::StringSwitch<std::string>(GPUName)
@@ -349,9 +350,8 @@ static std::string getAMDGPUTargetGPU(const llvm::Triple &T,
.Case("aruba", "cayman")
.Default(GPUName.str());
}
- if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
- return getProcessorFromTargetID(T, A->getValue()).str();
- }
+ if (MArch)
+ return getProcessorFromTargetID(T, MArch->getValue()).str();
return "";
}
diff --git a/clang/test/Driver/mdefault-visibility-export-mapping.c b/clang/test/Driver/mdefault-visibility-export-mapping.c
index 506149b897e04..2f8f246373d57 100644
--- a/clang/test/Driver/mdefault-visibility-export-mapping.c
+++ b/clang/test/Driver/mdefault-visibility-export-mapping.c
@@ -4,4 +4,4 @@
// CHECK: "-mdefault-visibility-export-mapping=explicit"
-// ERROR: unsupported option '-mdefault-visibility-export-mapping=explicit' for target 'powerpc-unknown-linux'
+// ERROR: error: unsupported option '-mdefault-visibility-export-mapping=' for target 'powerpc-unknown-linux'
More information about the cfe-commits
mailing list