@@ -31,6 +31,68 @@ using namespace clang::driver::toolchains;
3131using namespace clang ;
3232using namespace llvm ::opt;
3333
34+ RocmInstallationDetector::CommonBitcodeLibsPreferences::
35+ CommonBitcodeLibsPreferences (const Driver &D,
36+ const llvm::opt::ArgList &DriverArgs,
37+ StringRef GPUArch,
38+ const Action::OffloadKind DeviceOffloadingKind,
39+ const bool NeedsASanRT)
40+ : ABIVer(DeviceLibABIVersion::fromCodeObjectVersion(
41+ tools::getAMDGPUCodeObjectVersion (D, DriverArgs))) {
42+ const auto Kind = llvm::AMDGPU::parseArchAMDGCN (GPUArch);
43+ const unsigned ArchAttr = llvm::AMDGPU::getArchAttrAMDGCN (Kind);
44+
45+ IsOpenMP = DeviceOffloadingKind == Action::OFK_OpenMP;
46+
47+ const bool HasWave32 = (ArchAttr & llvm::AMDGPU::FEATURE_WAVE32);
48+ Wave64 =
49+ !HasWave32 || DriverArgs.hasFlag (options::OPT_mwavefrontsize64,
50+ options::OPT_mno_wavefrontsize64, false );
51+
52+ const bool IsKnownOffloading = DeviceOffloadingKind == Action::OFK_OpenMP ||
53+ DeviceOffloadingKind == Action::OFK_HIP;
54+
55+ // Default to enabling f32 denormals on subtargets where fma is fast with
56+ // denormals
57+ const bool DefaultDAZ =
58+ (Kind == llvm::AMDGPU::GK_NONE)
59+ ? false
60+ : !((ArchAttr & llvm::AMDGPU::FEATURE_FAST_FMA_F32) &&
61+ (ArchAttr & llvm::AMDGPU::FEATURE_FAST_DENORMAL_F32));
62+ // TODO: There are way too many flags that change this. Do we need to
63+ // check them all?
64+ DAZ = IsKnownOffloading
65+ ? DriverArgs.hasFlag (options::OPT_fgpu_flush_denormals_to_zero,
66+ options::OPT_fno_gpu_flush_denormals_to_zero,
67+ DefaultDAZ)
68+ : DriverArgs.hasArg (options::OPT_cl_denorms_are_zero) || DefaultDAZ;
69+
70+ FiniteOnly = DriverArgs.hasArg (options::OPT_cl_finite_math_only) ||
71+ DriverArgs.hasFlag (options::OPT_ffinite_math_only,
72+ options::OPT_fno_finite_math_only, false );
73+
74+ UnsafeMathOpt =
75+ DriverArgs.hasArg (options::OPT_cl_unsafe_math_optimizations) ||
76+ DriverArgs.hasFlag (options::OPT_funsafe_math_optimizations,
77+ options::OPT_fno_unsafe_math_optimizations, false );
78+
79+ FastRelaxedMath = DriverArgs.hasArg (options::OPT_cl_fast_relaxed_math) ||
80+ DriverArgs.hasFlag (options::OPT_ffast_math,
81+ options::OPT_fno_fast_math, false );
82+
83+ const bool DefaultSqrt = IsKnownOffloading ? true : false ;
84+ CorrectSqrt =
85+ DriverArgs.hasArg (options::OPT_cl_fp32_correctly_rounded_divide_sqrt) ||
86+ DriverArgs.hasFlag (
87+ options::OPT_fhip_fp32_correctly_rounded_divide_sqrt,
88+ options::OPT_fno_hip_fp32_correctly_rounded_divide_sqrt, DefaultSqrt);
89+ // GPU Sanitizer currently only supports ASan and is enabled through host
90+ // ASan.
91+ GPUSan = (DriverArgs.hasFlag (options::OPT_fgpu_sanitize,
92+ options::OPT_fno_gpu_sanitize, true ) &&
93+ NeedsASanRT);
94+ }
95+
3496void RocmInstallationDetector::scanLibDevicePath (llvm::StringRef Path) {
3597 assert (!Path.empty ());
3698
@@ -884,33 +946,14 @@ void ROCMToolChain::addClangTargetOptions(
884946 ABIVer))
885947 return ;
886948
887- bool Wave64 = isWave64 (DriverArgs, Kind);
888- // TODO: There are way too many flags that change this. Do we need to check
889- // them all?
890- bool DAZ = DriverArgs.hasArg (options::OPT_cl_denorms_are_zero) ||
891- getDefaultDenormsAreZeroForTarget (Kind);
892- bool FiniteOnly = DriverArgs.hasArg (options::OPT_cl_finite_math_only);
893-
894- bool UnsafeMathOpt =
895- DriverArgs.hasArg (options::OPT_cl_unsafe_math_optimizations);
896- bool FastRelaxedMath = DriverArgs.hasArg (options::OPT_cl_fast_relaxed_math);
897- bool CorrectSqrt =
898- DriverArgs.hasArg (options::OPT_cl_fp32_correctly_rounded_divide_sqrt);
899-
900- // GPU Sanitizer currently only supports ASan and is enabled through host
901- // ASan.
902- bool GPUSan = DriverArgs.hasFlag (options::OPT_fgpu_sanitize,
903- options::OPT_fno_gpu_sanitize, true ) &&
904- getSanitizerArgs (DriverArgs).needsAsanRt ();
905-
906949 // Add the OpenCL specific bitcode library.
907950 llvm::SmallVector<BitCodeLibraryInfo, 12 > BCLibs;
908951 BCLibs.emplace_back (RocmInstallation->getOpenCLPath ().str ());
909952
910953 // Add the generic set of libraries.
911954 BCLibs.append (RocmInstallation->getCommonBitcodeLibs (
912- DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt ,
913- FastRelaxedMath, CorrectSqrt, ABIVer, GPUSan, false ));
955+ DriverArgs, LibDeviceFile, GpuArch, DeviceOffloadingKind ,
956+ getSanitizerArgs (DriverArgs). needsAsanRt () ));
914957
915958 for (auto [BCFile, Internalize] : BCLibs) {
916959 if (Internalize)
@@ -947,45 +990,47 @@ bool RocmInstallationDetector::checkCommonBitcodeLibs(
947990
948991llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12 >
949992RocmInstallationDetector::getCommonBitcodeLibs (
950- const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile, bool Wave64,
951- bool DAZ, bool FiniteOnly, bool UnsafeMathOpt, bool FastRelaxedMath,
952- bool CorrectSqrt, DeviceLibABIVersion ABIVer, bool GPUSan,
953- bool isOpenMP) const {
993+ const llvm::opt::ArgList &DriverArgs, StringRef LibDeviceFile,
994+ StringRef GPUArch, const Action::OffloadKind DeviceOffloadingKind,
995+ const bool NeedsASanRT) const {
954996 llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12 > BCLibs;
955997
998+ CommonBitcodeLibsPreferences Pref{D, DriverArgs, GPUArch,
999+ DeviceOffloadingKind, NeedsASanRT};
1000+
9561001 auto AddBCLib = [&](ToolChain::BitCodeLibraryInfo BCLib,
9571002 bool Internalize = true ) {
9581003 BCLib.ShouldInternalize = Internalize;
9591004 BCLibs.emplace_back (BCLib);
9601005 };
9611006 auto AddSanBCLibs = [&]() {
962- if (GPUSan)
1007+ if (Pref. GPUSan )
9631008 AddBCLib (getAsanRTLPath (), false );
9641009 };
9651010
9661011 AddSanBCLibs ();
9671012 AddBCLib (getOCMLPath ());
968- if (!isOpenMP )
1013+ if (!Pref. IsOpenMP )
9691014 AddBCLib (getOCKLPath ());
970- else if (GPUSan && isOpenMP )
1015+ else if (Pref. GPUSan && Pref. IsOpenMP )
9711016 AddBCLib (getOCKLPath (), false );
972- AddBCLib (getDenormalsAreZeroPath (DAZ));
973- AddBCLib (getUnsafeMathPath (UnsafeMathOpt || FastRelaxedMath));
974- AddBCLib (getFiniteOnlyPath (FiniteOnly || FastRelaxedMath));
975- AddBCLib (getCorrectlyRoundedSqrtPath (CorrectSqrt));
976- AddBCLib (getWavefrontSize64Path (Wave64));
1017+ AddBCLib (getDenormalsAreZeroPath (Pref. DAZ ));
1018+ AddBCLib (getUnsafeMathPath (Pref. UnsafeMathOpt || Pref. FastRelaxedMath ));
1019+ AddBCLib (getFiniteOnlyPath (Pref. FiniteOnly || Pref. FastRelaxedMath ));
1020+ AddBCLib (getCorrectlyRoundedSqrtPath (Pref. CorrectSqrt ));
1021+ AddBCLib (getWavefrontSize64Path (Pref. Wave64 ));
9771022 AddBCLib (LibDeviceFile);
978- auto ABIVerPath = getABIVersionPath (ABIVer);
1023+ auto ABIVerPath = getABIVersionPath (Pref. ABIVer );
9791024 if (!ABIVerPath.empty ())
9801025 AddBCLib (ABIVerPath);
9811026
9821027 return BCLibs;
9831028}
9841029
9851030llvm::SmallVector<ToolChain::BitCodeLibraryInfo, 12 >
986- ROCMToolChain::getCommonDeviceLibNames (const llvm::opt::ArgList &DriverArgs,
987- const std::string &GPUArch,
988- bool isOpenMP ) const {
1031+ ROCMToolChain::getCommonDeviceLibNames (
1032+ const llvm::opt::ArgList &DriverArgs, const std::string &GPUArch,
1033+ Action::OffloadKind DeviceOffloadingKind ) const {
9891034 auto Kind = llvm::AMDGPU::parseArchAMDGCN (GPUArch);
9901035 const StringRef CanonArch = llvm::AMDGPU::getArchNameAMDGCN (Kind);
9911036
@@ -996,33 +1041,9 @@ ROCMToolChain::getCommonDeviceLibNames(const llvm::opt::ArgList &DriverArgs,
9961041 ABIVer))
9971042 return {};
9981043
999- // If --hip-device-lib is not set, add the default bitcode libraries.
1000- // TODO: There are way too many flags that change this. Do we need to check
1001- // them all?
1002- bool DAZ = DriverArgs.hasFlag (options::OPT_fgpu_flush_denormals_to_zero,
1003- options::OPT_fno_gpu_flush_denormals_to_zero,
1004- getDefaultDenormsAreZeroForTarget (Kind));
1005- bool FiniteOnly = DriverArgs.hasFlag (
1006- options::OPT_ffinite_math_only, options::OPT_fno_finite_math_only, false );
1007- bool UnsafeMathOpt =
1008- DriverArgs.hasFlag (options::OPT_funsafe_math_optimizations,
1009- options::OPT_fno_unsafe_math_optimizations, false );
1010- bool FastRelaxedMath = DriverArgs.hasFlag (options::OPT_ffast_math,
1011- options::OPT_fno_fast_math, false );
1012- bool CorrectSqrt = DriverArgs.hasFlag (
1013- options::OPT_fhip_fp32_correctly_rounded_divide_sqrt,
1014- options::OPT_fno_hip_fp32_correctly_rounded_divide_sqrt, true );
1015- bool Wave64 = isWave64 (DriverArgs, Kind);
1016-
1017- // GPU Sanitizer currently only supports ASan and is enabled through host
1018- // ASan.
1019- bool GPUSan = DriverArgs.hasFlag (options::OPT_fgpu_sanitize,
1020- options::OPT_fno_gpu_sanitize, true ) &&
1021- getSanitizerArgs (DriverArgs).needsAsanRt ();
1022-
10231044 return RocmInstallation->getCommonBitcodeLibs (
1024- DriverArgs, LibDeviceFile, Wave64, DAZ, FiniteOnly, UnsafeMathOpt ,
1025- FastRelaxedMath, CorrectSqrt, ABIVer, GPUSan, isOpenMP );
1045+ DriverArgs, LibDeviceFile, GPUArch, DeviceOffloadingKind ,
1046+ getSanitizerArgs (DriverArgs). needsAsanRt () );
10261047}
10271048
10281049bool AMDGPUToolChain::shouldSkipSanitizeOption (
0 commit comments