Index: clang/lib/Driver/ToolChains/AMDGPU.h =================================================================== --- clang/lib/Driver/ToolChains/AMDGPU.h +++ clang/lib/Driver/ToolChains/AMDGPU.h @@ -90,6 +90,9 @@ /// Needed for translating LTO options. const char *getDefaultLinker() const override { return "ld.lld"; } + /// Should skip argument. + bool shouldSkipArgument(const llvm::opt::Arg *Arg) const; + protected: /// Check and diagnose invalid target ID specified by -mcpu. void checkTargetID(const llvm::opt::ArgList &DriverArgs) const; Index: clang/lib/Driver/ToolChains/AMDGPU.cpp =================================================================== --- clang/lib/Driver/ToolChains/AMDGPU.cpp +++ clang/lib/Driver/ToolChains/AMDGPU.cpp @@ -427,8 +427,11 @@ if (!DAL) DAL = new DerivedArgList(Args.getBaseArgs()); - for (auto *A : Args) - DAL->append(A); + + for (Arg *A : Args) { + if (!shouldSkipArgument(A)) + DAL->append(A); + } checkTargetID(*DAL); @@ -640,3 +643,10 @@ CC1Args.push_back(LinkBitcodeFlag); CC1Args.push_back(DriverArgs.MakeArgString(LibDeviceFile)); } + +bool AMDGPUToolChain::shouldSkipArgument(const llvm::opt::Arg *A) const { + Option O = A->getOption(); + if (O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie)) + return true; + return false; +} Index: clang/lib/Driver/ToolChains/HIP.cpp =================================================================== --- clang/lib/Driver/ToolChains/HIP.cpp +++ clang/lib/Driver/ToolChains/HIP.cpp @@ -349,7 +349,8 @@ const OptTable &Opts = getDriver().getOpts(); for (Arg *A : Args) { - DAL->append(A); + if (!shouldSkipArgument(A)) + DAL->append(A); } if (!BoundArch.empty()) { Index: clang/test/Driver/hip-fpie-option.hip =================================================================== --- /dev/null +++ clang/test/Driver/hip-fpie-option.hip @@ -0,0 +1,37 @@ +// REQUIRES: clang-driver, amdgpu-registered-target + +// -fPIC and -fPIE only affects host relocation model. +// device compilation always uses PIC. + +// RUN: %clang -### -target x86_64-unknown-linux-gnu \ +// RUN: --offload-arch=gfx906 %s -nogpulib -nogpuinc \ +// RUN: 2>&1 | FileCheck -check-prefixes=DEV,HOST-STATIC %s + +// RUN: %clang -### -target x86_64-unknown-linux-gnu \ +// RUN: -fgpu-rdc --offload-arch=gfx906 %s -nogpulib -nogpuinc \ +// RUN: 2>&1 | FileCheck -check-prefixes=DEV,HOST-STATIC %s + +// RUN: %clang -### -target x86_64-unknown-linux-gnu \ +// RUN: --offload-arch=gfx906 %s -nogpulib -nogpuinc \ +// RUN: -fPIC \ +// RUN: 2>&1 | FileCheck -check-prefixes=DEV,HOST-PIC %s + +// RUN: %clang -### -target x86_64-unknown-linux-gnu \ +// RUN: -fgpu-rdc --offload-arch=gfx906 %s -nogpulib -nogpuinc \ +// RUN: -fPIC \ +// RUN: 2>&1 | FileCheck -check-prefixes=DEV,HOST-PIC %s + +// RUN: %clang -### -target x86_64-unknown-linux-gnu \ +// RUN: --offload-arch=gfx906 %s -nogpulib -nogpuinc \ +// RUN: -fPIE \ +// RUN: 2>&1 | FileCheck -check-prefixes=DEV,HOST-PIE %s + +// RUN: %clang -### -target x86_64-unknown-linux-gnu \ +// RUN: -fgpu-rdc --offload-arch=gfx906 %s -nogpulib -nogpuinc \ +// RUN: -fPIE \ +// RUN: 2>&1 | FileCheck -check-prefixes=DEV,HOST-PIE %s + +// DEV-DAG: {{".*clang.*".* "-triple" "amdgcn-amd-amdhsa".* "-mrelocation-model" "pic" "-pic-level" "[1|2]" "-mframe-pointer=all"}} +// HOST-STATIC-DAG: {{".*clang.*".* "-triple" "x86_64-unknown-linux-gnu".* "-mrelocation-model" "static"}} +// HOST-PIC-DAG: {{".*clang.*".* "-triple" "x86_64-unknown-linux-gnu".* "-mrelocation-model" "pic" "-pic-level" "2" "-mframe-pointer=all"}} +// HOST-PIE-DAG: {{".*clang.*".* "-triple" "x86_64-unknown-linux-gnu".* "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie"}}