diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td --- a/clang/include/clang/Basic/DiagnosticDriverKinds.td +++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -693,6 +693,10 @@ "-fjmc works only for ELF; option ignored">, InGroup; +def warn_drv_for_elf_only : Warning< + "'%0' works only for ELF; option ignored">, + InGroup; + def warn_target_override_arm64ec : Warning< "/arm64EC has been overridden by specified target: %0; option ignored">, InGroup; diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5859,14 +5859,15 @@ if (Arg *A = Args.getLastArg(options::OPT_fsplit_machine_functions, options::OPT_fno_split_machine_functions)) { - // This codegen pass is only available on x86-elf targets. - if (Triple.isX86() && Triple.isOSBinFormatELF()) { - if (A->getOption().matches(options::OPT_fsplit_machine_functions)) + if (A->getOption().matches(options::OPT_fsplit_machine_functions)) { + // This codegen pass is only available on elf targets. + if (Triple.isOSBinFormatELF()) A->render(Args, CmdArgs); - } else { - D.Diag(diag::err_drv_unsupported_opt_for_target) - << A->getAsString(Args) << TripleStr; + else + D.Diag(diag::warn_drv_for_elf_only) << A->getAsString(Args); } + // Do not issue warnings for -fno-split-machine-functions even it is not + // on ELF. } Args.AddLastArg(CmdArgs, options::OPT_finstrument_functions, diff --git a/clang/test/Driver/fsplit-machine-functions.c b/clang/test/Driver/fsplit-machine-functions.c --- a/clang/test/Driver/fsplit-machine-functions.c +++ b/clang/test/Driver/fsplit-machine-functions.c @@ -1,8 +1,8 @@ // RUN: %clang -### -target x86_64 -fprofile-use=default.profdata -fsplit-machine-functions %s -c 2>&1 | FileCheck -check-prefix=CHECK-OPT %s // RUN: %clang -### -target x86_64 -fsplit-machine-functions %s -c 2>&1 | FileCheck -check-prefix=CHECK-OPT %s // RUN: %clang -### -target x86_64 -fprofile-use=default.profdata -fsplit-machine-functions -fno-split-machine-functions %s -c 2>&1 | FileCheck -check-prefix=CHECK-NOOPT %s -// RUN: not %clang -c -target arm-unknown-linux -fsplit-machine-functions %s 2>&1 | FileCheck -check-prefix=CHECK-TRIPLE %s +// RUN: %clang -c -target arm-unknown-linux -fsplit-machine-functions %s 2>&1 | FileCheck -check-prefix=CHECK-TRIPLE %s // CHECK-OPT: "-fsplit-machine-functions" // CHECK-NOOPT-NOT: "-fsplit-machine-functions" -// CHECK-TRIPLE: error: unsupported option '-fsplit-machine-functions' for target +// CHECK-TRIPLE: warning: -fsplit-machine-functions is only valid for X86. diff --git a/llvm/include/llvm/IR/DiagnosticInfo.h b/llvm/include/llvm/IR/DiagnosticInfo.h --- a/llvm/include/llvm/IR/DiagnosticInfo.h +++ b/llvm/include/llvm/IR/DiagnosticInfo.h @@ -86,6 +86,7 @@ DK_SrcMgr, DK_DontCall, DK_MisExpect, + DK_MachineFunctionSplit, DK_FirstPluginKind // Must be last value to work with // getNextAvailablePluginDiagnosticKind }; @@ -1117,6 +1118,20 @@ } }; +class DiagnosticInfoMachineFunctionSplit : public DiagnosticInfo { + StringRef TargetTriple; + +public: + DiagnosticInfoMachineFunctionSplit(StringRef TargetTriple, + DiagnosticSeverity DS) + : DiagnosticInfo(DK_MachineFunctionSplit, DS), + TargetTriple(TargetTriple) {} + void print(DiagnosticPrinter &DP) const override; + static bool classof(const DiagnosticInfo *DI) { + return DI->getKind() == DK_MachineFunctionSplit; + } +}; + } // end namespace llvm #endif // LLVM_IR_DIAGNOSTICINFO_H diff --git a/llvm/lib/CodeGen/MachineFunctionSplitter.cpp b/llvm/lib/CodeGen/MachineFunctionSplitter.cpp --- a/llvm/lib/CodeGen/MachineFunctionSplitter.cpp +++ b/llvm/lib/CodeGen/MachineFunctionSplitter.cpp @@ -35,9 +35,11 @@ #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineModuleInfo.h" #include "llvm/CodeGen/Passes.h" +#include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/Function.h" #include "llvm/InitializePasses.h" #include "llvm/Support/CommandLine.h" +#include "llvm/TargetParser/Triple.h" #include using namespace llvm; @@ -82,6 +84,11 @@ void getAnalysisUsage(AnalysisUsage &AU) const override; bool runOnMachineFunction(MachineFunction &F) override; + + bool doInitialization(Module &) override; + +private: + bool UnsupportedTriple = false; }; } // end anonymous namespace @@ -127,7 +134,20 @@ return (*Count < ColdCountThreshold); } +bool MachineFunctionSplitter::doInitialization(Module &M) { + StringRef TripleStr = M.getTargetTriple(); + if (!Triple(TripleStr).isX86()) { + UnsupportedTriple = true; + M.getContext().diagnose( + DiagnosticInfoMachineFunctionSplit(TripleStr, DS_Warning)); + return false; + } + return MachineFunctionPass::doInitialization(M); +} + bool MachineFunctionSplitter::runOnMachineFunction(MachineFunction &MF) { + if (UnsupportedTriple) + return false; // We target functions with profile data. Static information in the form // of exception handling code may be split to cold if user passes the // mfs-split-ehcode flag. diff --git a/llvm/lib/IR/DiagnosticInfo.cpp b/llvm/lib/IR/DiagnosticInfo.cpp --- a/llvm/lib/IR/DiagnosticInfo.cpp +++ b/llvm/lib/IR/DiagnosticInfo.cpp @@ -449,3 +449,7 @@ if (!getNote().empty()) DP << ": " << getNote(); } + +void DiagnosticInfoMachineFunctionSplit::print(DiagnosticPrinter &DP) const { + DP << "-fsplit-machine-functions is not valid for " << TargetTriple; +}