@@ -946,8 +946,12 @@ void ROCMToolChain::addClangTargetOptions(
946946 StringRef LibDeviceFile = RocmInstallation->getLibDeviceFile (CanonArch);
947947 auto ABIVer = DeviceLibABIVersion::fromCodeObjectVersion (
948948 getAMDGPUCodeObjectVersion (getDriver (), DriverArgs));
949+ std::tuple<bool , const SanitizerArgs> GPUSan (
950+ DriverArgs.hasFlag (options::OPT_fgpu_sanitize,
951+ options::OPT_fno_gpu_sanitize, true ),
952+ getSanitizerArgs (DriverArgs));
949953 if (!RocmInstallation->checkCommonBitcodeLibs (CanonArch, LibDeviceFile,
950- ABIVer))
954+ ABIVer, GPUSan ))
951955 return ;
952956
953957 bool Wave64 = isWave64 (DriverArgs, Kind);
@@ -965,28 +969,32 @@ void ROCMToolChain::addClangTargetOptions(
965969 DriverArgs.hasArg (options::OPT_cl_fp32_correctly_rounded_divide_sqrt);
966970
967971 // Add the OpenCL specific bitcode library.
968- llvm::SmallVector<std::string , 12 > BCLibs;
969- BCLibs.push_back (RocmInstallation->getOpenCLPath ().str ());
972+ llvm::SmallVector<BitCodeLibraryInfo , 12 > BCLibs;
973+ BCLibs.emplace_back (RocmInstallation->getOpenCLPath ().str ());
970974
971975 // Add the generic set of libraries.
972976 BCLibs.append (RocmInstallation->getCommonBitcodeLibs (
973977 DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt,
974- FastRelaxedMath, CorrectSqrt, ABIVer, false ));
978+ FastRelaxedMath, CorrectSqrt, ABIVer, GPUSan, false ));
975979
976- if (getSanitizerArgs (DriverArgs).needsAsanRt ()) {
977- CC1Args.push_back (" -mlink-bitcode-file" );
978- CC1Args.push_back (
979- DriverArgs.MakeArgString (RocmInstallation->getAsanRTLPath ()));
980- }
981- for (StringRef BCFile : BCLibs) {
982- CC1Args.push_back (" -mlink-builtin-bitcode" );
980+ for (auto [BCFile, Internalize] : BCLibs) {
981+ if (Internalize)
982+ CC1Args.push_back (" -mlink-builtin-bitcode" );
983+ else
984+ CC1Args.push_back (" -mlink-bitcode-file" );
983985 CC1Args.push_back (DriverArgs.MakeArgString (BCFile));
984986 }
985987}
986988
987989bool RocmInstallationDetector::checkCommonBitcodeLibs (
988- StringRef GPUArch, StringRef LibDeviceFile,
989- DeviceLibABIVersion ABIVer) const {
990+ StringRef GPUArch, StringRef LibDeviceFile, DeviceLibABIVersion ABIVer,
991+ std::tuple<bool , const SanitizerArgs> &GPUSan) const {
992+ if (std::get<bool >(GPUSan))
993+ if (std::get<const SanitizerArgs>(GPUSan).needsAsanRt () &&
994+ getAsanRTLPath ().empty ()) {
995+ D.Diag (diag::err_drv_no_asan_rt_lib);
996+ return false ;
997+ }
990998 if (!hasDeviceLibrary ()) {
991999 D.Diag (diag::err_drv_no_rocm_device_lib) << 0 ;
9921000 return false ;
@@ -1002,18 +1010,35 @@ bool RocmInstallationDetector::checkCommonBitcodeLibs(
10021010 return true ;
10031011}
10041012
1005- llvm::SmallVector<std::string , 12 >
1013+ llvm::SmallVector<ToolChain::BitCodeLibraryInfo , 12 >
10061014RocmInstallationDetector::getCommonBitcodeLibs (
10071015 const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile, bool Wave64,
10081016 bool DAZ, bool FiniteOnly, bool UnsafeMathOpt, bool FastRelaxedMath,
1009- bool CorrectSqrt, DeviceLibABIVersion ABIVer, bool isOpenMP = false ) const {
1010- llvm::SmallVector<std::string, 12 > BCLibs;
1011-
1012- auto AddBCLib = [&](StringRef BCFile) { BCLibs.push_back (BCFile.str ()); };
1017+ bool CorrectSqrt, DeviceLibABIVersion ABIVer,
1018+ const std::tuple<bool , const SanitizerArgs> &GPUSan,
1019+ bool isOpenMP = false ) const {
1020+ llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12 > BCLibs;
1021+
1022+ auto GPUSanEnabled = [GPUSan]() { return std::get<bool >(GPUSan); };
1023+ auto AddBCLib = [&](ToolChain::BitCodeLibraryInfo BCLib,
1024+ bool Internalize = true ) {
1025+ BCLib.ShouldInternalize = Internalize;
1026+ BCLibs.emplace_back (BCLib);
1027+ };
1028+ auto AddSanBCLibs = [&]() {
1029+ auto SanArgs = std::get<const SanitizerArgs>(GPUSan);
1030+ if (GPUSanEnabled ()) {
1031+ if (SanArgs.needsAsanRt ())
1032+ AddBCLib (getAsanRTLPath (), false );
1033+ }
1034+ };
10131035
1036+ AddSanBCLibs ();
10141037 AddBCLib (getOCMLPath ());
10151038 if (!isOpenMP)
10161039 AddBCLib (getOCKLPath ());
1040+ else if (GPUSanEnabled () && isOpenMP)
1041+ AddBCLib (getOCKLPath (), false );
10171042 AddBCLib (getDenormalsAreZeroPath (DAZ));
10181043 AddBCLib (getUnsafeMathPath (UnsafeMathOpt || FastRelaxedMath));
10191044 AddBCLib (getFiniteOnlyPath (FiniteOnly || FastRelaxedMath));
@@ -1027,7 +1052,7 @@ RocmInstallationDetector::getCommonBitcodeLibs(
10271052 return BCLibs;
10281053}
10291054
1030- llvm::SmallVector<std::string , 12 >
1055+ llvm::SmallVector<ToolChain::BitCodeLibraryInfo , 12 >
10311056ROCMToolChain::getCommonDeviceLibNames (const llvm::opt::ArgList &DriverArgs,
10321057 const std::string &GPUArch,
10331058 bool isOpenMP) const {
@@ -1037,8 +1062,12 @@ ROCMToolChain::getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs,
10371062 StringRef LibDeviceFile = RocmInstallation->getLibDeviceFile (CanonArch);
10381063 auto ABIVer = DeviceLibABIVersion::fromCodeObjectVersion (
10391064 getAMDGPUCodeObjectVersion (getDriver (), DriverArgs));
1065+ std::tuple<bool , const SanitizerArgs> GPUSan (
1066+ DriverArgs.hasFlag (options::OPT_fgpu_sanitize,
1067+ options::OPT_fno_gpu_sanitize, true ),
1068+ getSanitizerArgs (DriverArgs));
10401069 if (!RocmInstallation->checkCommonBitcodeLibs (CanonArch, LibDeviceFile,
1041- ABIVer))
1070+ ABIVer, GPUSan ))
10421071 return {};
10431072
10441073 // If --hip-device-lib is not set, add the default bitcode libraries.
@@ -1061,7 +1090,7 @@ ROCMToolChain::getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs,
10611090
10621091 return RocmInstallation->getCommonBitcodeLibs (
10631092 DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt,
1064- FastRelaxedMath, CorrectSqrt, ABIVer, isOpenMP);
1093+ FastRelaxedMath, CorrectSqrt, ABIVer, GPUSan, isOpenMP);
10651094}
10661095
10671096bool AMDGPUToolChain::shouldSkipSanitizeOption (
0 commit comments