Repository: llvm/llvm-project
File: clang/lib/Driver/ToolChains/AMDGPU.cpp (C++)

Code before:
    const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile,
    StringRef GPUArch, const Action::OffloadKind DeviceOffloadingKind,
    const bool NeedsASanRT) const {
  llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> BCLibs;

  CommonBitcodeLibsPreferences Pref{D, DriverArgs, GPUArch,
                                    DeviceOffloadingKind, NeedsASanRT};

  auto AddBCLib = [&](ToolChain::BitCodeLibraryInfo BCLib,
                      bool Internalize = true) {
    if (!BCLib.Path.empty()) {
      BCLib.ShouldInternalize = Internalize;
      BCLibs.emplace_back(BCLib);
    }
  };
  auto AddSanBCLibs = [&]() {
    if (Pref.GPUSan)
      AddBCLib(getAsanRTLPath());
  };

  AddSanBCLibs();
  AddBCLib(getOCMLPath());
  if (!Pref.IsOpenMP)
    AddBCLib(getOCKLPath());
  else if (Pref.GPUSan && Pref.IsOpenMP)
    AddBCLib(getOCKLPath());
  AddBCLib(getUnsafeMathPath(Pref.UnsafeMathOpt || Pref.FastRelaxedMath));
  AddBCLib(getFiniteOnlyPath(Pref.FiniteOnly || Pref.FastRelaxedMath));
  AddBCLib(getWavefrontSize64Path(Pref.Wave64));
  AddBCLib(LibDeviceFile);
  auto ABIVerPath = getABIVersionPath(Pref.ABIVer);
  if (!ABIVerPath.empty())
    AddBCLib(ABIVerPath);

  return BCLibs;
}

llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
ROCMToolChain::getCommonDeviceLibNames(
    const llvm::opt::ArgList &DriverArgs, const std::string &GPUArch,
    Action::OffloadKind DeviceOffloadingKind) const {
  auto Kind = llvm::AMDGPU::parseArchAMDGCN(GPUArch);
  const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind);

  StringRef LibDeviceFile = RocmInstallation->getLibDeviceFile(CanonArch);
  auto ABIVer = DeviceLibABIVersion::fromCodeObjectVersion(
      getAMDGPUCodeObjectVersion(getDriver(), DriverArgs));
  if (!RocmInstallation->checkCommonBitcodeLibs(CanonArch, LibDeviceFile,
                                                ABIVer))
    return {};


Review comment (security):
These should be 2 separate changes. Fixing the OCKL link is a very different change from the sanitizer part 

Code after:
    const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile,
    StringRef GPUArch, const Action::OffloadKind DeviceOffloadingKind,
    const bool NeedsASanRT) const {
  llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> BCLibs;

  CommonBitcodeLibsPreferences Pref{D, DriverArgs, GPUArch,
                                    DeviceOffloadingKind, NeedsASanRT};

  auto AddBCLib = [&](ToolChain::BitCodeLibraryInfo BCLib,
                      bool Internalize = true) {
    if (!BCLib.Path.empty()) {
      BCLib.ShouldInternalize = Internalize;
      BCLibs.emplace_back(BCLib);
    }
  };
  auto AddSanBCLibs = [&]() {
    if (Pref.GPUSan)
      AddBCLib(getAsanRTLPath());
  };

  AddSanBCLibs();
  AddBCLib(getOCMLPath());
  if (!Pref.IsOpenMP)
    AddBCLib(getOCKLPath());
  else if (Pref.GPUSan && Pref.IsOpenMP)
    AddBCLib(getOCKLPath(), false);
  AddBCLib(getUnsafeMathPath(Pref.UnsafeMathOpt || Pref.FastRelaxedMath));
  AddBCLib(getFiniteOnlyPath(Pref.FiniteOnly || Pref.FastRelaxedMath));
  AddBCLib(getWavefrontSize64Path(Pref.Wave64));
  AddBCLib(LibDeviceFile);
  auto ABIVerPath = getABIVersionPath(Pref.ABIVer);
  if (!ABIVerPath.empty())
    AddBCLib(ABIVerPath);

  return BCLibs;
}

llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
ROCMToolChain::getCommonDeviceLibNames(
    const llvm::opt::ArgList &DriverArgs, const std::string &GPUArch,
    Action::OffloadKind DeviceOffloadingKind) const {
  auto Kind = llvm::AMDGPU::parseArchAMDGCN(GPUArch);
  const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN(Kind);

  StringRef LibDeviceFile = RocmInstallation->getLibDeviceFile(CanonArch);
  auto ABIVer = DeviceLibABIVersion::fromCodeObjectVersion(
      getAMDGPUCodeObjectVersion(getDriver(), DriverArgs));
  if (!RocmInstallation->checkCommonBitcodeLibs(CanonArch, LibDeviceFile,
                                                ABIVer))
    return {};