diff --git a/clang/lib/Driver/ToolChains/HIP.cpp b/clang/lib/Driver/ToolChains/HIP.cpp --- a/clang/lib/Driver/ToolChains/HIP.cpp +++ b/clang/lib/Driver/ToolChains/HIP.cpp @@ -62,6 +62,34 @@ } return OutputFileName; } + +static void addOptLevelArgs(const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CmdArgs, + bool IsLlc = false) { + if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { + StringRef OOpt = "3"; + if (A->getOption().matches(options::OPT_O4) || + A->getOption().matches(options::OPT_Ofast)) + OOpt = "3"; + else if (A->getOption().matches(options::OPT_O0)) + OOpt = "0"; + else if (A->getOption().matches(options::OPT_O)) { + // Clang and opt support -Os/-Oz; llc only supports -O0, -O1, -O2 and -O3 + // so we map -Os/-Oz to -O2. + // Only clang supports -Og, and maps it to -O1. + // We map anything else to -O2. + OOpt = llvm::StringSwitch(A->getValue()) + .Case("1", "1") + .Case("2", "2") + .Case("3", "3") + .Case("s", IsLlc ? "2" : "s") + .Case("z", IsLlc ? "2" : "z") + .Case("g", "1") + .Default("2"); + } + CmdArgs.push_back(Args.MakeArgString("-O" + OOpt)); + } +} } // namespace const char *AMDGCN::Linker::constructLLVMLinkCommand( @@ -93,25 +121,7 @@ // The input to opt is the output from llvm-link. OptArgs.push_back(InputFileName); // Pass optimization arg to opt. - if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { - StringRef OOpt = "3"; - if (A->getOption().matches(options::OPT_O4) || - A->getOption().matches(options::OPT_Ofast)) - OOpt = "3"; - else if (A->getOption().matches(options::OPT_O0)) - OOpt = "0"; - else if (A->getOption().matches(options::OPT_O)) { - // -Os, -Oz, and -O(anything else) map to -O2 - OOpt = llvm::StringSwitch(A->getValue()) - .Case("1", "1") - .Case("2", "2") - .Case("3", "3") - .Case("s", "2") - .Case("z", "2") - .Default("2"); - } - OptArgs.push_back(Args.MakeArgString("-O" + OOpt)); - } + addOptLevelArgs(Args, OptArgs); OptArgs.push_back("-mtriple=amdgcn-amd-amdhsa"); OptArgs.push_back(Args.MakeArgString("-mcpu=" + SubArchName)); @@ -136,10 +146,15 @@ llvm::StringRef OutputFilePrefix, const char *InputFileName, bool OutputIsAsm) const { // Construct llc command. - ArgStringList LlcArgs{ - InputFileName, "-mtriple=amdgcn-amd-amdhsa", - Args.MakeArgString(Twine("-filetype=") + (OutputIsAsm ? "asm" : "obj")), - Args.MakeArgString("-mcpu=" + SubArchName)}; + ArgStringList LlcArgs; + // The input to llc is the output from opt. + LlcArgs.push_back(InputFileName); + // Pass optimization arg to llc. + addOptLevelArgs(Args, LlcArgs, true); + LlcArgs.push_back("-mtriple=amdgcn-amd-amdhsa"); + LlcArgs.push_back(Args.MakeArgString("-mcpu=" + SubArchName)); + LlcArgs.push_back( + Args.MakeArgString(Twine("-filetype=") + (OutputIsAsm ? "asm" : "obj"))); // Extract all the -m options std::vector Features; diff --git a/clang/test/Driver/hip-toolchain-opt.hip b/clang/test/Driver/hip-toolchain-opt.hip new file mode 100644 --- /dev/null +++ b/clang/test/Driver/hip-toolchain-opt.hip @@ -0,0 +1,101 @@ +// REQUIRES: clang-driver +// REQUIRES: x86-registered-target +// REQUIRES: amdgpu-registered-target + +// RUN: %clang -### \ +// RUN: -target x86_64-unknown-linux-gnu \ +// RUN: -x hip --cuda-gpu-arch=gfx900 \ +// RUN: -c -nogpulib \ +// RUN: %S/Inputs/hip_multiple_inputs/b.hip \ +// RUN: 2>&1 | FileCheck --check-prefixes=ALL,DEFAULT %s + +// RUN: %clang -### -O0 \ +// RUN: -target x86_64-unknown-linux-gnu \ +// RUN: -x hip --cuda-gpu-arch=gfx900 \ +// RUN: -c -nogpulib \ +// RUN: %S/Inputs/hip_multiple_inputs/b.hip \ +// RUN: 2>&1 | FileCheck --check-prefixes=ALL,O0 %s + +// RUN: %clang -### -O1 \ +// RUN: -target x86_64-unknown-linux-gnu \ +// RUN: -x hip --cuda-gpu-arch=gfx900 \ +// RUN: -c -nogpulib \ +// RUN: %S/Inputs/hip_multiple_inputs/b.hip \ +// RUN: 2>&1 | FileCheck --check-prefixes=ALL,O1 %s + +// RUN: %clang -### -O2 \ +// RUN: -target x86_64-unknown-linux-gnu \ +// RUN: -x hip --cuda-gpu-arch=gfx900 \ +// RUN: -c -nogpulib \ +// RUN: %S/Inputs/hip_multiple_inputs/b.hip \ +// RUN: 2>&1 | FileCheck --check-prefixes=ALL,O2 %s + +// RUN: %clang -### -O3 \ +// RUN: -target x86_64-unknown-linux-gnu \ +// RUN: -x hip --cuda-gpu-arch=gfx900 \ +// RUN: -c -nogpulib \ +// RUN: %S/Inputs/hip_multiple_inputs/b.hip \ +// RUN: 2>&1 | FileCheck --check-prefixes=ALL,O3 %s + +// RUN: %clang -### -Os \ +// RUN: -target x86_64-unknown-linux-gnu \ +// RUN: -x hip --cuda-gpu-arch=gfx900 \ +// RUN: -c -nogpulib \ +// RUN: %S/Inputs/hip_multiple_inputs/b.hip \ +// RUN: 2>&1 | FileCheck --check-prefixes=ALL,Os %s + +// RUN: %clang -### -Oz \ +// RUN: -target x86_64-unknown-linux-gnu \ +// RUN: -x hip --cuda-gpu-arch=gfx900 \ +// RUN: -c -nogpulib \ +// RUN: %S/Inputs/hip_multiple_inputs/b.hip \ +// RUN: 2>&1 | FileCheck --check-prefixes=ALL,Oz %s + +// RUN: %clang -### -Og \ +// RUN: -target x86_64-unknown-linux-gnu \ +// RUN: -x hip --cuda-gpu-arch=gfx900 \ +// RUN: -c -nogpulib \ +// RUN: %S/Inputs/hip_multiple_inputs/b.hip \ +// RUN: 2>&1 | FileCheck --check-prefixes=ALL,Og %s + +// ALL: "{{.*}}clang{{.*}}" "-cc1" "-triple" "amdgcn-amd-amdhsa" +// DEFAULT-NOT: "-O{{.}}" +// O0-SAME: "-O0" +// O1-SAME: "-O1" +// O2-SAME: "-O2" +// O3-SAME: "-O3" +// Os-SAME: "-Os" +// Oz-SAME: "-Oz" +// Og-SAME: "-Og" + +// ALL: "{{.*}}opt" +// DEFAULT-NOT: "-O{{.}}" +// O0-SAME: "-O0" +// O1-SAME: "-O1" +// O2-SAME: "-O2" +// O3-SAME: "-O3" +// Os-SAME: "-Os" +// Oz-SAME: "-Oz" +// Og-SAME: "-O1" +// ALL-SAME: "-mtriple=amdgcn-amd-amdhsa" + +// ALL: "{{.*}}llc" +// DEFAULT-NOT: "-O{{.}}" +// O0-SAME: "-O0" +// O1-SAME: "-O1" +// O2-SAME: "-O2" +// O3-SAME: "-O3" +// Os-SAME: "-O2" +// Oz-SAME: "-O2" +// Og-SAME: "-O1" +// ALL-SAME: "-mtriple=amdgcn-amd-amdhsa" + +// ALL: "{{.*}}clang{{.*}}" "-cc1" "-triple" "x86_64-unknown-linux-gnu" +// DEFAULT-NOT: "-O{{.}}" +// O0-SAME: "-O0" +// O1-SAME: "-O1" +// O2-SAME: "-O2" +// O3-SAME: "-O3" +// Os-SAME: "-Os" +// Oz-SAME: "-Oz" +// Og-SAME: "-Og"