diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 766a9b91e3c0a..1264ffa1ef7c8 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -6867,8 +6867,14 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-nogpulib"); if (Arg *A = Args.getLastArg(options::OPT_fcf_protection_EQ)) { - CmdArgs.push_back( - Args.MakeArgString(Twine("-fcf-protection=") + A->getValue())); + // Do not pass this argument to the offloading device if the target does not + // support it. + // TODO: We need a better way to detect incompatible options for offloading. + if (JA.getOffloadingDeviceKind() == Action::OFK_None || + (!TC.getTriple().isAMDGPU() && !TC.getTriple().isNVPTX() && + !TC.getTriple().isSPIRV())) + CmdArgs.push_back( + Args.MakeArgString(Twine("-fcf-protection=") + A->getValue())); } if (Arg *A = Args.getLastArg(options::OPT_mfunction_return_EQ)) diff --git a/clang/test/Driver/hip-options.hip b/clang/test/Driver/hip-options.hip index 2ba9032f16946..679f3084ace14 100644 --- a/clang/test/Driver/hip-options.hip +++ b/clang/test/Driver/hip-options.hip @@ -43,10 +43,10 @@ // MLLVM-NOT: "-mllvm"{{.*}}"-amdgpu-early-inline-all=true"{{.*}}"-mllvm"{{.*}}"-amdgpu-early-inline-all=true" // RUN: %clang -### -Xarch_device -g -nogpulib -nogpuinc --cuda-gpu-arch=gfx900 \ -// RUN: -Xarch_device -fcf-protection=branch -Xarch_device -mllvm=--inline-threshold=100 \ +// RUN: -Xarch_device -mllvm=--inline-threshold=100 \ // RUN: --cuda-gpu-arch=gfx906 %s 2>&1 | FileCheck -check-prefix=DEV %s -// DEV: "-cc1"{{.*}} "-fcuda-is-device" {{.*}} "-debug-info-kind={{.*}}" {{.*}} "-fcf-protection=branch" {{.*}}"-mllvm" "--inline-threshold=100" -// DEV: "-cc1"{{.*}} "-fcuda-is-device" {{.*}} "-debug-info-kind={{.*}}" {{.*}} "-fcf-protection=branch" {{.*}}"-mllvm" "--inline-threshold=100" +// DEV: "-cc1"{{.*}} "-fcuda-is-device" {{.*}} "-debug-info-kind={{.*}}"{{.*}}"-mllvm" "--inline-threshold=100" +// DEV: "-cc1"{{.*}} "-fcuda-is-device" {{.*}} "-debug-info-kind={{.*}}"{{.*}}"-mllvm" "--inline-threshold=100" // DEV-NOT: clang{{.*}} {{.*}} "-debug-info-kind={{.*}}" // RUN: %clang -### -Xarch_host -g -nogpulib -nogpuinc --cuda-gpu-arch=gfx900 \ @@ -244,4 +244,4 @@ // RUN: 2>&1 | FileCheck -check-prefix=NO-WARN-ATOMIC %s // NO-WARN-ATOMIC: clang{{.*}} "-triple" "amdgcn-amd-amdhsa" {{.*}} "-Werror=atomic-alignment" {{.*}} "-Wno-error=atomic-alignment" // NO-WARN-ATOMIC-NOT: clang{{.*}} "-triple" "x86_64-unknown-linux-gnu" {{.*}} "-Werror=atomic-alignment" -// NO-WARN-ATOMIC-NOT: clang{{.*}} "-triple" "x86_64-unknown-linux-gnu" {{.*}} "-Wno-error=atomic-alignment" \ No newline at end of file +// NO-WARN-ATOMIC-NOT: clang{{.*}} "-triple" "x86_64-unknown-linux-gnu" {{.*}} "-Wno-error=atomic-alignment" diff --git a/clang/test/Driver/offload-no-fcf-protection.c b/clang/test/Driver/offload-no-fcf-protection.c new file mode 100644 index 0000000000000..ef275881edbaf --- /dev/null +++ b/clang/test/Driver/offload-no-fcf-protection.c @@ -0,0 +1,39 @@ +// Check that -fcf-protection does not get passed to the device-side +// compilation. + +// RUN: %clang -### -x cuda --target=x86_64-unknown-linux-gnu -nogpulib \ +// RUN: -nogpuinc --offload-arch=sm_52 -fcf-protection=full -c %s 2>&1 \ +// RUN: | FileCheck %s --check-prefix=CUDA + +// CUDA: "-cc1" "-triple" "nvptx64-nvidia-cuda" +// CUDA-NOT: "-fcf-protection=full" +// CUDA: "-cc1" "-triple" "x86_64-unknown-linux-gnu" +// CUDA: "-fcf-protection=full" + +// RUN: %clang -### -x hip --target=x86_64-unknown-linux-gnu -nogpulib \ +// RUN: -nogpuinc --offload-arch=gfx90a -fcf-protection=full -c %s 2>&1 \ +// RUN: | FileCheck %s --check-prefix=HIP + +// HIP: "-cc1" "-triple" "amdgcn-amd-amdhsa" +// HIP-NOT: "-fcf-protection=full" +// HIP: "-cc1" "-triple" "x86_64-unknown-linux-gnu" +// HIP: "-fcf-protection=full" + +// RUN: %clang -### -x c --target=x86_64-unknown-linux-gnu -nogpulib -fopenmp=libomp \ +// RUN: -nogpuinc --offload-arch=gfx90a -fcf-protection=full -c %s 2>&1 \ +// RUN: | FileCheck %s --check-prefix=OMP + +// OMP: "-cc1" "-triple" "x86_64-unknown-linux-gnu" +// OMP: "-fcf-protection=full" +// OMP: "-cc1" "-triple" "amdgcn-amd-amdhsa" +// OMP-NOT: "-fcf-protection=full" +// OMP: "-cc1" "-triple" "x86_64-unknown-linux-gnu" +// OMP: "-fcf-protection=full" + +// RUN: %clang -### -x c --target=nvptx64-nvidia-cuda -nogpulib -nogpuinc \ +// RUN: -march=sm_52 -fcf-protection=full -c %s 2>&1 \ +// RUN: | FileCheck %s --check-prefix=DIRECT +// RUN: %clang -### -x c --target=amdgcn-amd-amdhsa -nogpulib -nogpuinc \ +// RUN: -mcpu=gfx90a -fcf-protection=full -c %s 2>&1 \ +// RUN: | FileCheck %s --check-prefix=DIRECT +// DIRECT: "-fcf-protection=full"