1- """
2- Find root ROCm directory.
3- """
1+ # use amdhip as query for a valid rocm_path
2+ function check_rocm_path (path:: String )
3+ libname = (Sys. islinux () ? " libamdhip64" : " amdhip64" ) * " ." * dlext
4+ path2 = path
5+ isfile (joinpath (path2, libname)) && @goto success
6+ path2 = joinpath (path, " lib" )
7+ isfile (joinpath (path2, libname)) && @goto success
8+ path2 = joinpath (path, " bin" )
9+ isfile (joinpath (path2, libname)) && @goto success
10+ path2 = joinpath (path, " lib64" )
11+ isfile (joinpath (path2, libname)) && @goto success
12+ return " "
13+ @label success
14+ @assert isdir (path2)
15+ return path2
16+ end
17+
18+ # Find root ROCm directory.
419function find_roc_path ():: String
520 env_dir = get (ENV , " ROCM_PATH" , " " )
6- isdir (env_dir) && return env_dir
21+ isdir (env_dir) && check_rocm_path (env_dir) != " " && return env_dir
722
823 if Sys. islinux ()
9- isdir (" /opt/rocm" ) && return " /opt/rocm" # shim for Ubuntu rocm packages.
24+ hipconfig = Sys. which (" hipconfig" )
25+ if ! isnothing (hipconfig)
26+ rocm_path = read (` $hipconfig --rocmpath` , String)
27+ rocm_path = check_rocm_path (rocm_path)
28+ isdir (rocm_path) && return rocm_path
29+ end
30+ rocm_path = check_rocm_path (" /opt/rocm" )
31+ isdir (rocm_path) && return rocm_path
32+ rocm_path = check_rocm_path (" /usr" )
33+ isdir (rocm_path) && return rocm_path
1034 elseif Sys. iswindows ()
1135 disk_dir = dirname (dirname (homedir ())) # Disk C root directory.
1236 rocm_dir = joinpath (disk_dir, " Program Files" , " AMD" , " ROCm" )
@@ -24,22 +48,52 @@ function find_roc_path()::String
2448 return " "
2549end
2650
51+ # use hip.bc as query for a valid device libs dir
52+ function check_device_libs (path:: String )
53+ if isdir (path)
54+ file_path = joinpath (path, " hip" * " .bc" )
55+ if ! ispath (file_path)
56+ file_path = joinpath (path, " hip" * " .amdgcn.bc" )
57+ if ! ispath (file_path)
58+ # failed to find matching bitcode file
59+ return " "
60+ end
61+ end
62+ return path
63+ else
64+ return " "
65+ end
66+ end
67+
2768function find_device_libs (rocm_path:: String ):: String
2869 env_dir = get (ENV , " ROCM_PATH" , " " )
2970 if isdir (env_dir)
3071 path = joinpath (env_dir, " amdgcn" , " bitcode" )
72+ path = check_device_libs (path)
3173 isdir (path) && return path
3274 end
33-
3475 # Might be set by tools like Spack or the user
3576 hip_devlibs_path = get (ENV , " HIP_DEVICE_LIB_PATH" , " " )
3677 hip_devlibs_path != = " " && return hip_devlibs_path
3778 devlibs_path = get (ENV , " DEVICE_LIB_PATH" , " " )
3879 devlibs_path != = " " && return devlibs_path
39-
80+ # Try using hipconfig to find the device libraries.
4081 # Try the canonical location.
4182 canonical_dir = joinpath (rocm_path, " amdgcn" , " bitcode" )
83+ canonical_dir = check_device_libs (canonical_dir)
4284 isdir (canonical_dir) && return canonical_dir
85+ # Fedora might put it in a weird place
86+ hipconfig = Sys. which (" hipconfig" )
87+ if ! isnothing (hipconfig)
88+ clang_path = read (` $hipconfig --hipclangpath` , String)
89+ lib_path = joinpath (clang_path ," .." , " lib" ," clang" )
90+ if isdir (lib_path)
91+ lib_path = joinpath (lib_path, only (readdir (lib_path)))
92+ lib_path = joinpath (lib_path, " amdgcn" , " bitcode" )
93+ lib_path = check_device_libs (lib_path)
94+ isdir (lib_path) && return lib_path
95+ end
96+ end
4397 return " "
4498end
4599
@@ -52,21 +106,27 @@ function find_rocm_library(libs::Vector; rocm_path::String, ext::String = dlext)
52106end
53107
54108function find_rocm_library (lib:: String ; rocm_path:: String , ext:: String = dlext):: String
55- libdir = joinpath (rocm_path, Sys. islinux () ? " lib" : " bin" )
56- isdir (libdir) || return " "
57-
109+ libdir = joinpath (rocm_path, rel_libdir)
110+ @assert isdir (libdir)
58111 for file in readdir (libdir; join= true )
59112 fname = basename (file)
60- matched = startswith (fname, lib) && endswith (fname, ext)
113+ matched = startswith (fname, lib) && contains (fname, ext)
61114 matched && return file
62115 end
63116 return " "
64117end
65118
66119function find_ld_lld (rocm_path:: String ):: String
67120 lld_name = " ld.lld" * (Sys. iswindows () ? " .exe" : " " )
68- for subdir in (joinpath (" llvm" , " bin" ), " bin" )
69- exp_ld_path = joinpath (rocm_path, subdir, lld_name)
121+
122+ dirs = (joinpath (rocm_path," llvm" , " bin" ), joinpath (rocm_path," bin" ))
123+ hipconfig = Sys. which (" hipconfig" )
124+ if ! isnothing (hipconfig)
125+ clang_path = read (` $hipconfig --hipclangpath` , String)
126+ dirs = (dirs ... , clang_path)
127+ end
128+ for dir in dirs
129+ exp_ld_path = joinpath (dir, lld_name)
70130 ispath (exp_ld_path) || continue
71131 try
72132 tmpfile = tempname (;cleanup= false )
0 commit comments