Skip to content

[SYCL] Simplify detection of device bitcode libraries for RTC #19756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions clang/include/clang/Driver/CudaInstallationDetector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//===-- CudaInstallationDetector.h - Cuda Instalation Detector --*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_DRIVER_CUDAINSTALLATIONDETECTOR_H
#define LLVM_CLANG_DRIVER_CUDAINSTALLATIONDETECTOR_H

#include "clang/Basic/Cuda.h"
#include "clang/Driver/Driver.h"
#include <bitset>

namespace clang {
namespace driver {

/// A class to find a viable CUDA installation
class CudaInstallationDetector {
private:
const Driver &D;
bool IsValid = false;
CudaVersion Version = CudaVersion::UNKNOWN;
std::string InstallPath;
std::string BinPath;
std::string LibDevicePath;
std::string IncludePath;
llvm::StringMap<std::string> LibDeviceMap;

// CUDA architectures for which we have raised an error in
// CheckCudaVersionSupportsArch.
mutable std::bitset<(int)OffloadArch::LAST> ArchsWithBadVersion;

public:
CudaInstallationDetector(const Driver &D, const llvm::Triple &HostTriple,
const llvm::opt::ArgList &Args);

void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const;

/// Emit an error if Version does not support the given Arch.
///
/// If either Version or Arch is unknown, does not emit an error. Emits at
/// most one error per Arch.
void CheckCudaVersionSupportsArch(OffloadArch Arch) const;

/// Check whether we detected a valid Cuda install.
bool isValid() const { return IsValid; }
/// Print information about the detected CUDA installation.
void print(raw_ostream &OS) const;

/// Get the detected Cuda install's version.
CudaVersion version() const {
return Version == CudaVersion::NEW ? CudaVersion::PARTIALLY_SUPPORTED
: Version;
}
/// Get the detected Cuda installation path.
StringRef getInstallPath() const { return InstallPath; }
/// Get the detected path to Cuda's bin directory.
StringRef getBinPath() const { return BinPath; }
/// Get the detected Cuda Include path.
StringRef getIncludePath() const { return IncludePath; }
/// Get the detected Cuda device library path.
StringRef getLibDevicePath() const { return LibDevicePath; }
/// Get libdevice file for given architecture
std::string getLibDeviceFile(StringRef Gpu) const {
return LibDeviceMap.lookup(Gpu);
}
void WarnIfUnsupportedVersion() const;
};

} // namespace driver
} // namespace clang

#endif // LLVM_CLANG_DRIVER_CUDAINSTALLATIONDETECTOR_H
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_LAZYDETECTOR_H
#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_LAZYDETECTOR_H
#ifndef LLVM_CLANG_DRIVER_LAZYDETECTOR_H
#define LLVM_CLANG_DRIVER_LAZYDETECTOR_H

#include "clang/Driver/Tool.h"
#include "clang/Driver/ToolChain.h"
Expand Down Expand Up @@ -42,4 +42,4 @@ template <class T> class LazyDetector {

} // end namespace clang

#endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_LAZYDETECTOR_H
#endif // LLVM_CLANG_DRIVER_LAZYDETECTOR_H
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
//===--- ROCm.h - ROCm installation detector --------------------*- C++ -*-===//
//===-- RocmInstallationDetector.h - ROCm Instalation Detector --*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_ROCM_H
#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_ROCM_H
#ifndef LLVM_CLANG_DRIVER_ROCMINSTALLATIONDETECTOR_H
#define LLVM_CLANG_DRIVER_ROCMINSTALLATIONDETECTOR_H

#include "clang/Basic/Cuda.h"
#include "clang/Basic/LLVM.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/Options.h"
#include "clang/Driver/SanitizerArgs.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Support/VersionTuple.h"
#include "llvm/TargetParser/Triple.h"

namespace clang {
namespace driver {
Expand Down Expand Up @@ -77,6 +68,24 @@ class RocmInstallationDetector {
SPACKReleaseStr(SPACKReleaseStr.str()) {}
};

struct CommonBitcodeLibsPreferences {
CommonBitcodeLibsPreferences(const Driver &D,
const llvm::opt::ArgList &DriverArgs,
StringRef GPUArch,
const Action::OffloadKind DeviceOffloadingKind,
const bool NeedsASanRT);

DeviceLibABIVersion ABIVer;
bool IsOpenMP;
bool Wave64;
bool DAZ;
bool FiniteOnly;
bool UnsafeMathOpt;
bool FastRelaxedMath;
bool CorrectSqrt;
bool GPUSan;
};

const Driver &D;
bool HasHIPRuntime = false;
bool HasDeviceLibrary = false;
Expand Down Expand Up @@ -175,11 +184,11 @@ class RocmInstallationDetector {

/// Get file paths of default bitcode libraries common to AMDGPU based
/// toolchains.
llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12> getCommonBitcodeLibs(
const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile,
bool Wave64, bool DAZ, bool FiniteOnly, bool UnsafeMathOpt,
bool FastRelaxedMath, bool CorrectSqrt, DeviceLibABIVersion ABIVer,
bool GPUSan, bool isOpenMP) const;
llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
getCommonBitcodeLibs(const llvm::opt::ArgList &DriverArgs,
StringRef LibDeviceFile, StringRef GPUArch,
const Action::OffloadKind DeviceOffloadingKind,
const bool NeedsASanRT) const;
/// Check file paths of default bitcode libraries common to AMDGPU based
/// toolchains. \returns false if there are invalid or missing files.
bool checkCommonBitcodeLibs(StringRef GPUArch, StringRef LibDeviceFile,
Expand Down Expand Up @@ -288,7 +297,7 @@ class RocmInstallationDetector {
StringRef getHIPVersion() const { return DetectedVersion; }
};

} // end namespace driver
} // end namespace clang
} // namespace driver
} // namespace clang

#endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_ROCM_H
#endif // LLVM_CLANG_DRIVER_ROCMINSTALLATIONDETECTOR_H
49 changes: 49 additions & 0 deletions clang/include/clang/Driver/SyclInstallationDetector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===-- SyclInstallationDetector.h - SYCL Instalation Detector --*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_DRIVER_SYCLINSTALLATIONDETECTOR_H
#define LLVM_CLANG_DRIVER_SYCLINSTALLATIONDETECTOR_H

#include "clang/Driver/Driver.h"

namespace clang {
namespace driver {

class SYCLInstallationDetector {
public:
SYCLInstallationDetector(const Driver &D);
SYCLInstallationDetector(const Driver &D, const llvm::Triple &HostTriple,
const llvm::opt::ArgList &Args);

/// \brief Find and return the path to the libspirv library for the target
/// \return The path to the libspirv library if found, otherwise nullptr.
/// The lifetime of the returned string is managed by \p Args.
const char *findLibspirvPath(const llvm::Triple &DeviceTriple,
const llvm::opt::ArgList &Args,
const llvm::Triple &HostTriple) const;

void addLibspirvLinkArgs(const llvm::Triple &DeviceTriple,
const llvm::opt::ArgList &DriverArgs,
const llvm::Triple &HostTriple,
llvm::opt::ArgStringList &CC1Args) const;

void getSYCLDeviceLibPath(
llvm::SmallVector<llvm::SmallString<128>, 4> &DeviceLibPaths) const;
void addSYCLIncludeArgs(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const;
void print(llvm::raw_ostream &OS) const;

private:
const Driver &D;
llvm::SmallVector<llvm::SmallString<128>, 4> InstallationCandidates;
};

} // namespace driver
} // namespace clang

#endif // LLVM_CLANG_DRIVER_SYCLINSTALLATIONDETECTOR_H
5 changes: 2 additions & 3 deletions clang/lib/Driver/ToolChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1692,9 +1692,8 @@ void ToolChain::addSYCLIncludeArgs(const ArgList &DriverArgs,
ArgStringList &CC1Args) const {}

llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12>
ToolChain::getDeviceLibs(
const ArgList &DriverArgs,
const Action::OffloadKind DeviceOffloadingKind) const {
ToolChain::getDeviceLibs(const ArgList &DriverArgs,
const Action::OffloadKind DeviceOffloadingKind) const {
return {};
}

Expand Down
Loading