Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -5068,15 +5068,9 @@ let Flags = [CC1Option, CC1AsOption, NoDriverOption] in { -def target_cpu : Separate<["-"], "target-cpu">, - HelpText<"Target a specific cpu type">, - MarshallingInfoString>; def tune_cpu : Separate<["-"], "tune-cpu">, HelpText<"Tune for a specific cpu type">, MarshallingInfoString>; -def target_feature : Separate<["-"], "target-feature">, - HelpText<"Target specific attributes">, - MarshallingInfoStringVector>; def target_abi : Separate<["-"], "target-abi">, HelpText<"Target a particular ABI type">, MarshallingInfoString>; @@ -5103,6 +5097,12 @@ let Flags = [CC1Option, CC1AsOption, FC1Option, NoDriverOption] in { +def target_cpu : Separate<["-"], "target-cpu">, + HelpText<"Target a specific cpu type">, + MarshallingInfoString>; +def target_feature : Separate<["-"], "target-feature">, + HelpText<"Target specific attributes">, + MarshallingInfoStringVector>; def triple : Separate<["-"], "triple">, HelpText<"Specify target triple (e.g. i686-apple-darwin9)">, MarshallingInfoString, "llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple())">, Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -292,98 +292,6 @@ } } -static void getWebAssemblyTargetFeatures(const ArgList &Args, - std::vector &Features) { - handleTargetFeaturesGroup(Args, Features, options::OPT_m_wasm_Features_Group); -} - -static void getTargetFeatures(const Driver &D, const llvm::Triple &Triple, - const ArgList &Args, ArgStringList &CmdArgs, - bool ForAS, bool IsAux = false) { - std::vector Features; - switch (Triple.getArch()) { - default: - break; - case llvm::Triple::mips: - case llvm::Triple::mipsel: - case llvm::Triple::mips64: - case llvm::Triple::mips64el: - mips::getMIPSTargetFeatures(D, Triple, Args, Features); - break; - - case llvm::Triple::arm: - case llvm::Triple::armeb: - case llvm::Triple::thumb: - case llvm::Triple::thumbeb: - arm::getARMTargetFeatures(D, Triple, Args, Features, ForAS); - break; - - case llvm::Triple::ppc: - case llvm::Triple::ppcle: - case llvm::Triple::ppc64: - case llvm::Triple::ppc64le: - ppc::getPPCTargetFeatures(D, Triple, Args, Features); - break; - case llvm::Triple::riscv32: - case llvm::Triple::riscv64: - riscv::getRISCVTargetFeatures(D, Triple, Args, Features); - break; - case llvm::Triple::systemz: - systemz::getSystemZTargetFeatures(D, Args, Features); - break; - case llvm::Triple::aarch64: - case llvm::Triple::aarch64_32: - case llvm::Triple::aarch64_be: - aarch64::getAArch64TargetFeatures(D, Triple, Args, Features, ForAS); - break; - case llvm::Triple::x86: - case llvm::Triple::x86_64: - x86::getX86TargetFeatures(D, Triple, Args, Features); - break; - case llvm::Triple::hexagon: - hexagon::getHexagonTargetFeatures(D, Args, Features); - break; - case llvm::Triple::wasm32: - case llvm::Triple::wasm64: - getWebAssemblyTargetFeatures(Args, Features); - break; - case llvm::Triple::sparc: - case llvm::Triple::sparcel: - case llvm::Triple::sparcv9: - sparc::getSparcTargetFeatures(D, Args, Features); - break; - case llvm::Triple::r600: - case llvm::Triple::amdgcn: - amdgpu::getAMDGPUTargetFeatures(D, Triple, Args, Features); - break; - case llvm::Triple::nvptx: - case llvm::Triple::nvptx64: - NVPTX::getNVPTXTargetFeatures(D, Triple, Args, Features); - break; - case llvm::Triple::m68k: - m68k::getM68kTargetFeatures(D, Triple, Args, Features); - break; - case llvm::Triple::msp430: - msp430::getMSP430TargetFeatures(D, Args, Features); - break; - case llvm::Triple::ve: - ve::getVETargetFeatures(D, Args, Features); - break; - case llvm::Triple::csky: - csky::getCSKYTargetFeatures(D, Triple, Args, CmdArgs, Features); - break; - case llvm::Triple::loongarch32: - case llvm::Triple::loongarch64: - loongarch::getLoongArchTargetFeatures(D, Triple, Args, Features); - break; - } - - for (auto Feature : unifyTargetFeatures(Features)) { - CmdArgs.push_back(IsAux ? "-aux-target-feature" : "-target-feature"); - CmdArgs.push_back(Feature.data()); - } -} - static bool shouldUseExceptionTablesForObjCExceptions(const ObjCRuntime &runtime, const llvm::Triple &Triple) { Index: clang/lib/Driver/ToolChains/CommonArgs.h =================================================================== --- clang/lib/Driver/ToolChains/CommonArgs.h +++ clang/lib/Driver/ToolChains/CommonArgs.h @@ -174,6 +174,11 @@ std::string getCPUName(const Driver &D, const llvm::opt::ArgList &Args, const llvm::Triple &T, bool FromAs = false); +void getTargetFeatures(const Driver &D, const llvm::Triple &Triple, + const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CmdArgs, bool ForAS, + bool IsAux = false); + /// Iterate \p Args and convert -mxxx to +xxx and -mno-xxx to -xxx and /// append it to \p Features. /// Index: clang/lib/Driver/ToolChains/CommonArgs.cpp =================================================================== --- clang/lib/Driver/ToolChains/CommonArgs.cpp +++ clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -9,6 +9,8 @@ #include "CommonArgs.h" #include "Arch/AArch64.h" #include "Arch/ARM.h" +#include "Arch/CSKY.h" +#include "Arch/LoongArch.h" #include "Arch/M68k.h" #include "Arch/Mips.h" #include "Arch/PPC.h" @@ -19,6 +21,7 @@ #include "Arch/X86.h" #include "HIPAMD.h" #include "Hexagon.h" +#include "MSP430.h" #include "clang/Basic/CharInfo.h" #include "clang/Basic/LangOptions.h" #include "clang/Basic/ObjCRuntime.h" @@ -470,6 +473,96 @@ } } +static void getWebAssemblyTargetFeatures(const ArgList &Args, + std::vector &Features) { + handleTargetFeaturesGroup(Args, Features, options::OPT_m_wasm_Features_Group); +} + +void tools::getTargetFeatures(const Driver &D, const llvm::Triple &Triple, + const ArgList &Args, ArgStringList &CmdArgs, + bool ForAS, bool IsAux) { + std::vector Features; + switch (Triple.getArch()) { + default: + break; + case llvm::Triple::mips: + case llvm::Triple::mipsel: + case llvm::Triple::mips64: + case llvm::Triple::mips64el: + mips::getMIPSTargetFeatures(D, Triple, Args, Features); + break; + case llvm::Triple::arm: + case llvm::Triple::armeb: + case llvm::Triple::thumb: + case llvm::Triple::thumbeb: + arm::getARMTargetFeatures(D, Triple, Args, Features, ForAS); + break; + case llvm::Triple::ppc: + case llvm::Triple::ppcle: + case llvm::Triple::ppc64: + case llvm::Triple::ppc64le: + ppc::getPPCTargetFeatures(D, Triple, Args, Features); + break; + case llvm::Triple::riscv32: + case llvm::Triple::riscv64: + riscv::getRISCVTargetFeatures(D, Triple, Args, Features); + break; + case llvm::Triple::systemz: + systemz::getSystemZTargetFeatures(D, Args, Features); + break; + case llvm::Triple::aarch64: + case llvm::Triple::aarch64_32: + case llvm::Triple::aarch64_be: + aarch64::getAArch64TargetFeatures(D, Triple, Args, Features, ForAS); + break; + case llvm::Triple::x86: + case llvm::Triple::x86_64: + x86::getX86TargetFeatures(D, Triple, Args, Features); + break; + case llvm::Triple::hexagon: + hexagon::getHexagonTargetFeatures(D, Args, Features); + break; + case llvm::Triple::wasm32: + case llvm::Triple::wasm64: + getWebAssemblyTargetFeatures(Args, Features); + break; + case llvm::Triple::sparc: + case llvm::Triple::sparcel: + case llvm::Triple::sparcv9: + sparc::getSparcTargetFeatures(D, Args, Features); + break; + case llvm::Triple::r600: + case llvm::Triple::amdgcn: + amdgpu::getAMDGPUTargetFeatures(D, Triple, Args, Features); + break; + case llvm::Triple::nvptx: + case llvm::Triple::nvptx64: + NVPTX::getNVPTXTargetFeatures(D, Triple, Args, Features); + break; + case llvm::Triple::m68k: + m68k::getM68kTargetFeatures(D, Triple, Args, Features); + break; + case llvm::Triple::msp430: + msp430::getMSP430TargetFeatures(D, Args, Features); + break; + case llvm::Triple::ve: + ve::getVETargetFeatures(D, Args, Features); + break; + case llvm::Triple::csky: + csky::getCSKYTargetFeatures(D, Triple, Args, CmdArgs, Features); + break; + case llvm::Triple::loongarch32: + case llvm::Triple::loongarch64: + loongarch::getLoongArchTargetFeatures(D, Triple, Args, Features); + break; + } + + for (auto Feature : unifyTargetFeatures(Features)) { + CmdArgs.push_back(IsAux ? "-aux-target-feature" : "-target-feature"); + CmdArgs.push_back(Feature.data()); + } +} + llvm::StringRef tools::getLTOParallelism(const ArgList &Args, const Driver &D) { Arg *LtoJobsArg = Args.getLastArg(options::OPT_flto_jobs_EQ); if (!LtoJobsArg) Index: clang/lib/Driver/ToolChains/Flang.h =================================================================== --- clang/lib/Driver/ToolChains/Flang.h +++ clang/lib/Driver/ToolChains/Flang.h @@ -48,6 +48,14 @@ void addPicOptions(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const; + /// Extract target options from the driver arguments and add them to + /// the command arguments. + /// + /// \param [in] Args The list of input driver arguments + /// \param [out] CmdArgs The list of output command arguments + void addTargetOptions(const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CmdArgs) const; + /// Extract other compilation options from the driver arguments and add them /// to the command arguments. /// Index: clang/lib/Driver/ToolChains/Flang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Flang.cpp +++ clang/lib/Driver/ToolChains/Flang.cpp @@ -80,6 +80,32 @@ } } +void Flang::addTargetOptions(const ArgList &Args, + ArgStringList &CmdArgs) const { + // return; + const auto &TC = getToolChain(); + const llvm::Triple &Triple = TC.getEffectiveTriple(); + const Driver &D = TC.getDriver(); + + std::string CPU = getCPUName(D, Args, Triple); + if (!CPU.empty()) { + CmdArgs.push_back("-target-cpu"); + CmdArgs.push_back(Args.MakeArgString(CPU)); + } + + // Add the target features. + switch (TC.getArch()) { + default: + // Untested for other targets but should work generally. + break; + case llvm::Triple::aarch64: + getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ false); + break; + } + + // TODO: Add target specific flags, ABI, mtune option etc. +} + static void addFloatingPointOptions(const Driver &D, const ArgList &Args, ArgStringList &CmdArgs) { StringRef FPContract; @@ -243,6 +269,9 @@ // Floating point related options addFloatingPointOptions(D, Args, CmdArgs); + // Add target args, features, etc. + addTargetOptions(Args, CmdArgs); + // Add other compile options addOtherOptions(Args, CmdArgs); Index: flang/include/flang/Frontend/TargetOptions.h =================================================================== --- flang/include/flang/Frontend/TargetOptions.h +++ flang/include/flang/Frontend/TargetOptions.h @@ -26,14 +26,19 @@ /// In the future, we will use this to specify various target options that /// will affect the generated code e.g.: /// * CPU to tune the code for -/// * available CPU/hardware extensions -/// * target specific features to enable/disable /// * options for accelerators (e.g. GPUs) /// * (...) class TargetOptions { public: /// The name of the target triple to compile for. std::string triple; + + /// If given, the name of the target CPU to generate code for. + std::string cpu; + + /// The list of target specific features to enable or disable, as written on + /// the command line. + std::vector featuresAsWritten; }; } // end namespace Fortran::frontend Index: flang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- flang/lib/Frontend/CompilerInvocation.cpp +++ flang/lib/Frontend/CompilerInvocation.cpp @@ -171,6 +171,14 @@ if (const llvm::opt::Arg *a = args.getLastArg(clang::driver::options::OPT_triple)) opts.triple = a->getValue(); + + if (const llvm::opt::Arg *a = + args.getLastArg(clang::driver::options::OPT_target_cpu)) + opts.cpu = a->getValue(); + + for (const auto *currentArg : + args.filtered(clang::driver::options::OPT_target_feature)) + opts.featuresAsWritten.emplace_back(currentArg->getValue()); } // Tweak the frontend configuration based on the frontend action Index: flang/lib/Frontend/FrontendActions.cpp =================================================================== --- flang/lib/Frontend/FrontendActions.cpp +++ flang/lib/Frontend/FrontendActions.cpp @@ -591,7 +591,8 @@ void CodeGenAction::setUpTargetMachine() { CompilerInstance &ci = this->getInstance(); - const std::string &theTriple = ci.getInvocation().getTargetOpts().triple; + const auto &targetOpts = ci.getInvocation().getTargetOpts(); + const std::string &theTriple = targetOpts.triple; // Create `Target` std::string error; @@ -602,9 +603,11 @@ // Create `TargetMachine` const auto &CGOpts = ci.getInvocation().getCodeGenOpts(); llvm::CodeGenOpt::Level OptLevel = getCGOptLevel(CGOpts); + std::string featuresStr = llvm::join(targetOpts.featuresAsWritten.begin(), + targetOpts.featuresAsWritten.end(), ","); tm.reset(theTarget->createTargetMachine( - theTriple, /*CPU=*/"", - /*Features=*/"", llvm::TargetOptions(), + theTriple, /*CPU=*/targetOpts.cpu, + /*Features=*/featuresStr, llvm::TargetOptions(), /*Reloc::Model=*/CGOpts.getRelocationModel(), /*CodeModel::Model=*/llvm::None, OptLevel)); assert(tm && "Failed to create TargetMachine"); Index: flang/test/Driver/driver-help.f90 =================================================================== --- flang/test/Driver/driver-help.f90 +++ flang/test/Driver/driver-help.f90 @@ -156,6 +156,8 @@ ! HELP-FC1-NEXT: -P Disable linemarker output in -E mode ! HELP-FC1-NEXT: -std= Language standard to compile for ! HELP-FC1-NEXT: -S Only run preprocess and compilation steps +! HELP-FC1-NEXT: -target-cpu Target a specific cpu type +! HELP-FC1-NEXT: -target-feature Target specific attributes ! HELP-FC1-NEXT: -test-io Run the InputOuputTest action. Use for development and testing only. ! HELP-FC1-NEXT: -triple Specify target triple (e.g. i686-apple-darwin9) ! HELP-FC1-NEXT: -U Undefine macro Index: flang/test/Driver/target-cpu-features.f90 =================================================================== --- /dev/null +++ flang/test/Driver/target-cpu-features.f90 @@ -0,0 +1,22 @@ +! REQUIRES: aarch64-registered-target, x86-registered-target + +! Test that -mcpu is forwarded to -target-cpu and that the target features are +! also added. + +! RUN: %flang --target=aarch64-linux-gnu -mcpu=cortex-a57 -emit-llvm -c %s \ +! RUN: -o %t.o -### 2>&1 | FileCheck %s -check-prefix=CHECK-A57 + +! RUN: %flang --target=aarch64-linux-gnu -mcpu=cortex-a76 -emit-llvm -c %s \ +! RUN: -o %t.o -### 2>&1 | FileCheck %s -check-prefix=CHECK-A76 + +! RUN: %flang --target=x86_64-linux-gnu -mcpu=cortex-a57 -emit-llvm -c %s \ +! RUN: -o %t.o -### 2>&1 | FileCheck %s -check-prefix=CHECK-NO-A57 + +! CHECK-A57: "-triple" "aarch64-unknown-linux-gnu" +! CHECK-A57: "-target-cpu" "cortex-a57" "-target-feature" "+v8a" "-target-feature" "+crc" "-target-feature" "+crypto" "-target-feature" "+fp-armv8" "-target-feature" "+neon" "-target-feature" "+sha2" "-target-feature" "+aes" + +! CHECK-A76: "-triple" "aarch64-unknown-linux-gnu" +! CHECK-A76: "-target-cpu" "cortex-a76" "-target-feature" "+v8.2a" "-target-feature" "+crc" "-target-feature" "+lse" "-target-feature" "+rdm" "-target-feature" "+crypto" "-target-feature" "+dotprod" "-target-feature" "+fp-armv8" "-target-feature" "+neon" "-target-feature" "+fullfp16" "-target-feature" "+ras" "-target-feature" "+rcpc" "-target-feature" "+ssbs" "-target-feature" "+sha2" "-target-feature" "+aes" + +! CHECK-NO-A57: "-fc1" +! CHECK-NO-A57-NOT: cortex-a57