[compiler-rt] 762316e - [compiler-rt][ROCm] Collect Linux profiles from resident images (#229518)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 10 07:43:08 PDT 2026
Author: Yaxun (Sam) Liu
Date: 2026-10-10T10:43:00-04:00
New Revision: 762316e293d1db4f495bdbb545c31d588a1a2d62
URL: https://github.com/llvm/llvm-project/commit/762316e293d1db4f495bdbb545c31d588a1a2d62
DIFF: https://github.com/llvm/llvm-project/commit/762316e293d1db4f495bdbb545c31d588a1a2d62.diff
LOG: [compiler-rt][ROCm] Collect Linux profiles from resident images (#229518)
On Linux, profile collection looks up every registered HIP image.
Those lookups can load images the program never used just to collect
zero counters.
Collect profiles from images already loaded by the HSA runtime. If
the loader inspection APIs are unavailable, keep the existing image
lookup path.
Added:
compiler-rt/test/profile/AMDGPU/device-resident-images.hip
Modified:
compiler-rt/lib/profile/InstrProfilingPlatformROCm.cpp
compiler-rt/lib/profile/InstrProfilingPlatformROCmHSA.cpp
compiler-rt/lib/profile/InstrProfilingPlatformROCmInternal.h
compiler-rt/test/profile/GPU/instrprof-hip-collect-after.hip
compiler-rt/test/profile/GPU/instrprof-hip-multi-gpu.hip
compiler-rt/test/profile/GPU/instrprof-hip-nondefault-device.hip
Removed:
################################################################################
diff --git a/compiler-rt/lib/profile/InstrProfilingPlatformROCm.cpp b/compiler-rt/lib/profile/InstrProfilingPlatformROCm.cpp
index cae2e118e9f1a..376957b117784 100644
--- a/compiler-rt/lib/profile/InstrProfilingPlatformROCm.cpp
+++ b/compiler-rt/lib/profile/InstrProfilingPlatformROCm.cpp
@@ -1165,15 +1165,22 @@ static int isHipAvailable(void) {
/* Collect device-side profile data */
/* -------------------------------------------------------------------------- */
-/* Host-shadow drain: static-linked kernels (host __hipRegisterVar shadows) and
- * intercepted dynamic modules. The caller gates this on
- * (NumShadowVariables || NumDynamicModules) && isHipAvailable(); pure
- * device-linked programs (RCCL) are handled by the supplemental HSA pass. */
+/* Host-shadow drain: static-linked kernels on Windows, or if HSA loader
+ * introspection is unavailable on Linux, plus intercepted dynamic modules.
+ * The Linux HSA pass normally drains resident static images without loading
+ * images that were only registered by the host. */
static int collectHostShadowData(void) {
int Ret = 0;
- /* Shadow variables (static-linked kernels): drain from every device. */
- if (NumShadowVariables > 0) {
+ /* On Linux, use host shadows only if HSA cannot enumerate resident images.
+ * Looking up every shadow otherwise loads unused static images. */
+#if defined(__linux__) && !defined(_WIN32)
+ const bool CollectStaticShadows =
+ NumShadowVariables != 0 && !hsaRuntimeAvailable();
+#else
+ constexpr bool CollectStaticShadows = true;
+#endif
+ if (CollectStaticShadows && NumShadowVariables > 0) {
int OrigDevice = -1;
hipGetDevice(&OrigDevice);
@@ -1250,7 +1257,7 @@ extern "C" int __llvm_profile_hip_collect_device_data(void) {
Ret = -1;
#if defined(__linux__) && !defined(_WIN32)
- /* Supplemental HSA-introspection drain */
+ /* Drain resident device images through HSA. */
if (drainDevicesViaHsa() != 0)
Ret = -1;
#endif
diff --git a/compiler-rt/lib/profile/InstrProfilingPlatformROCmHSA.cpp b/compiler-rt/lib/profile/InstrProfilingPlatformROCmHSA.cpp
index 5e365ff24bb14..86e72dd385275 100644
--- a/compiler-rt/lib/profile/InstrProfilingPlatformROCmHSA.cpp
+++ b/compiler-rt/lib/profile/InstrProfilingPlatformROCmHSA.cpp
@@ -6,15 +6,13 @@
//
//===----------------------------------------------------------------------===//
//
-// Supplemental HSA-introspection drain (Linux only).
+// HSA resident-image drain (Linux only).
//
-// The host-shadow drain in InstrProfilingPlatformROCm.cpp only sees device code
-// objects with a host-side shadow (__hipRegisterVar) or an intercepted
-// hipModuleLoad*. Device-linked code with no host shadow (e.g. RCCL) is
-// invisible to it. This pass walks every GPU agent's loaded executables via
-// HSA, finds each __llvm_profile_sections table on the device, and drains the
-// ones the host-shadow pass missed (deduped by the section-bounds tuple). It
-// reuses processDeviceOffloadPrf() so the profraw layout is identical.
+// This pass walks loaded executables on each GPU agent and drains their
+// __llvm_profile_sections tables. It covers static HIP images and device-linked
+// programs without host shadows, while avoiding lookups that would load unused
+// static images. It shares processDeviceOffloadPrf() and section-bounds dedup
+// with the dynamic-module drain.
//
//===----------------------------------------------------------------------===//
@@ -209,6 +207,10 @@ static int loadHsaRuntimePointers(void) {
return setHsaRuntimeState(1);
}
+int __prof_rocm::hsaRuntimeAvailable(void) {
+ return loadHsaRuntimePointers() == 0;
+}
+
/* The canonical device bounds-table symbol from InstrProfilingPlatformGPU.c. */
static const char ProfileSectionsSymbol[] = "__llvm_profile_sections";
diff --git a/compiler-rt/lib/profile/InstrProfilingPlatformROCmInternal.h b/compiler-rt/lib/profile/InstrProfilingPlatformROCmInternal.h
index e1531fddd5524..db0dbb2b4067d 100644
--- a/compiler-rt/lib/profile/InstrProfilingPlatformROCmInternal.h
+++ b/compiler-rt/lib/profile/InstrProfilingPlatformROCmInternal.h
@@ -121,6 +121,9 @@ int processDeviceOffloadPrf(void *DeviceOffloadPrf, const char *Target,
void profRecordDrainedBounds(const void *Data, const void *Counters,
const void *Names);
+// True when the HSA loader can enumerate resident device images.
+int hsaRuntimeAvailable(void);
+
// Walk every GPU agent's loaded executables via HSA and drain each
// __llvm_profile_sections table the host-shadow pass did not already handle.
int drainDevicesViaHsa(void);
diff --git a/compiler-rt/test/profile/AMDGPU/device-resident-images.hip b/compiler-rt/test/profile/AMDGPU/device-resident-images.hip
new file mode 100644
index 0000000000000..51d0fa6508167
--- /dev/null
+++ b/compiler-rt/test/profile/AMDGPU/device-resident-images.hip
@@ -0,0 +1,59 @@
+// A host-registered HIP image with no launched kernel must stay unloaded when
+// profiles are collected at exit. Build two non-RDC translation units so each
+// has its own static device image. Only the hot image executes.
+//
+// REQUIRES: hip, amdgpu
+// UNSUPPORTED: windows
+// RUN: rm -rf %t.dir && mkdir -p %t.dir
+// RUN: %clang -x hip --offload-arch=%amdgpu_arch -fno-gpu-rdc \
+// RUN: -fprofile-instr-generate -c %s -o %t.dir/hot.o
+// RUN: %clang -x hip --offload-arch=%amdgpu_arch -fno-gpu-rdc \
+// RUN: -fprofile-instr-generate -DCOLD_IMAGE -c %s -o %t.dir/cold.o
+// RUN: %clang --hip-link --offload-arch=%amdgpu_arch \
+// RUN: -fprofile-instr-generate %t.dir/hot.o %t.dir/cold.o \
+// RUN: -o %t.dir/a.out -L%hip_lib_path -lamdhip64
+// RUN: cd %t.dir && llvm-objdump --offloading a.out \
+// RUN: | FileCheck %s --check-prefix=IMAGES
+// RUN: env LLVM_PROFILE_FILE=%t.dir/host.%%p.profraw \
+// RUN: LD_LIBRARY_PATH=%hip_lib_path:$LD_LIBRARY_PATH \
+// RUN: HIP_VISIBLE_DEVICES=0 %t.dir/a.out
+// RUN: /bin/sh -c 'test "$(find %t.dir -name "gfx*.profraw" | wc -l)" -eq 1'
+// RUN: llvm-profdata show --all-functions --counts %t.dir/gfx*.profraw \
+// RUN: | FileCheck %s --check-prefix=PROFILE --implicit-check-not=cold_kernel
+
+#include <hip/hip_runtime.h>
+
+#ifdef COLD_IMAGE
+
+extern "C" __global__ void cold_kernel(int *P) { ++*P; }
+extern "C" void cold_host_anchor() {}
+
+#else
+
+extern "C" void cold_host_anchor();
+extern "C" __global__ void hot_kernel(int *P) { ++*P; }
+
+int main() {
+ cold_host_anchor(); // Keep the cold translation unit linked into the host.
+ int *Device = nullptr;
+ if (hipMalloc(&Device, sizeof(int)) != hipSuccess)
+ return 1;
+ int Value = 0;
+ if (hipMemcpy(Device, &Value, sizeof(Value), hipMemcpyHostToDevice) !=
+ hipSuccess)
+ return 2;
+ hot_kernel<<<1, 1>>>(Device);
+ if (hipMemcpy(&Value, Device, sizeof(Value), hipMemcpyDeviceToHost) !=
+ hipSuccess)
+ return 3;
+ if (hipFree(Device) != hipSuccess)
+ return 4;
+ return Value == 1 ? 0 : 5;
+}
+
+#endif
+
+// IMAGES-DAG: a.out.0.hip-amdgcn-amd-amdhsa--
+// IMAGES-DAG: a.out.1.hip-amdgcn-amd-amdhsa--
+// PROFILE: hot_kernel:
+// PROFILE: Function count: 1
diff --git a/compiler-rt/test/profile/GPU/instrprof-hip-collect-after.hip b/compiler-rt/test/profile/GPU/instrprof-hip-collect-after.hip
index 5a2393f8dcc47..6c8ef3e806df2 100644
--- a/compiler-rt/test/profile/GPU/instrprof-hip-collect-after.hip
+++ b/compiler-rt/test/profile/GPU/instrprof-hip-collect-after.hip
@@ -1,10 +1,8 @@
-// Explicit-collect idempotency: a program that calls
-// __llvm_profile_hip_collect_device_data() itself *after* a launch (e.g. a
-// periodic profile dump) and then also exits normally must not double-count the
-// device counters. The explicit drain and the atexit drain write the same
-// arch-named device profraw, so the merged profile must reflect a single launch
-// (function count 64, even-branch 32), not two. Complements device-early-collect
-// (which covers a collect *before* the first launch).
+// Explicit-collect idempotency: a program that collects after a launch and
+// then exits normally must not double-count the device counters. The HSA pass
+// drains the resident image once, so the merged profile reflects one launch
+// (function count 64, even-branch 32). Complements device-early-collect, which
+// calls the collector before the first launch.
//
// REQUIRES: hip, amdgpu
// RUN: rm -rf %t.dir && mkdir -p %t.dir
@@ -18,8 +16,7 @@
// RUN: llvm-profdata show --all-functions --counts %t.dir/a.profdata \
// RUN: | FileCheck %s
//
-// A single launch of 64 threads, drained twice (explicit + atexit), must still
-// merge to exactly one launch's worth of counts.
+// An explicit collection followed by normal exit records one launch.
// CHECK: _Z8classifyPii:
// CHECK: Function count: 64
// CHECK: Block counts: [0, 32]
diff --git a/compiler-rt/test/profile/GPU/instrprof-hip-multi-gpu.hip b/compiler-rt/test/profile/GPU/instrprof-hip-multi-gpu.hip
index 6a99546d34bdb..65631f68200a5 100644
--- a/compiler-rt/test/profile/GPU/instrprof-hip-multi-gpu.hip
+++ b/compiler-rt/test/profile/GPU/instrprof-hip-multi-gpu.hip
@@ -1,13 +1,10 @@
// Test that HIP PGO works on multi-GPU systems. The kernel runs on the default
-// device, so the host-shadow drain (guarded by upstream's launch tracking)
-// collects only that device and the supplemental HSA agent-walk then finds the
-// same code object and dedups it out. The point of the test is that neither
-// pass crashes or hangs reading a non-resident device on a host with several
-// GPUs (the failure mode that the launch tracking + HSA residency walk fix).
+// device. On Linux, the HSA agent walk drains the resident code object
+// without accessing images on other devices. The program must exit normally
+// on a host with several GPUs.
//
// REQUIRES: hip, amdgpu
-// The "walk complete" / dedup notes are Linux-only HSA-drain strings; the
-// Windows host-shadow drain collects only the current device.
+// The HSA walk notes are Linux-only; Windows uses the host-shadow drain.
// UNSUPPORTED: windows
// RUN: %clang -x hip -fprofile-instr-generate -fcoverage-mapping \
// RUN: --offload-arch=%amdgpu_arch %s -o %t -L%hip_lib_path -lamdhip64
@@ -15,11 +12,9 @@
// RUN: LD_LIBRARY_PATH=%hip_lib_path:$LD_LIBRARY_PATH \
// RUN: LLVM_PROFILE_VERBOSE=1 %run %t 2>&1 | FileCheck %s
//
-// The host-shadow pass drains the launched device, the HSA walk finds that same
-// code object and dedups it (drained=0), and the program does not crash.
+// The HSA walk drains the loaded code object, and the program does not crash.
// CHECK: Copied device sections:
-// CHECK: device bounds already drained, skipping
-// CHECK: walk complete: agents={{[0-9]+}} pairs={{[0-9]+}} found={{[1-9][0-9]*}} drained={{[0-9]+}}
+// CHECK: HSA walk complete: agents={{[0-9]+}} pairs={{[0-9]+}} found={{[1-9][0-9]*}} drained={{[1-9][0-9]*}}
// CHECK: PASS
#include <hip/hip_runtime.h>
diff --git a/compiler-rt/test/profile/GPU/instrprof-hip-nondefault-device.hip b/compiler-rt/test/profile/GPU/instrprof-hip-nondefault-device.hip
index 5d3dea671047b..88c6804fdf2c2 100644
--- a/compiler-rt/test/profile/GPU/instrprof-hip-nondefault-device.hip
+++ b/compiler-rt/test/profile/GPU/instrprof-hip-nondefault-device.hip
@@ -1,13 +1,9 @@
// Test PGO when the kernel runs on a non-default device (here the program
-// selects device 1). Upstream's launch tracking records that device 1 was used,
-// so the host-shadow drain skips the other devices and collects device 1, and
-// the supplemental HSA agent-walk then finds that same code object and dedups it
-// out. This exercises both that the correct device is drained and that an
-// unused device is never read (which would fault/hang on a multi-GPU host).
+// selects device 1). The HSA walk must drain its resident image without reading
+// an image from the unused device.
//
// REQUIRES: hip, amdgpu, multi-device
-// The "walk complete" / dedup notes are Linux-only HSA-drain strings; the
-// Windows host-shadow drain only collects the current device.
+// The HSA walk notes are Linux-only; Windows uses the host-shadow drain.
// UNSUPPORTED: windows
// RUN: %clang -x hip -fprofile-instr-generate -fcoverage-mapping \
// RUN: --offload-arch=%amdgpu_arch %s -o %t -L%hip_lib_path -lamdhip64
@@ -15,13 +11,9 @@
// RUN: LD_LIBRARY_PATH=%hip_lib_path:$LD_LIBRARY_PATH \
// RUN: LLVM_PROFILE_VERBOSE=1 %run %t 2>&1 | FileCheck %s
//
-// The launched device (1) is drained, the unused default device is skipped, and
-// the HSA walk finds the same code object and dedups it (drained=0).
-// CHECK: Skipping unused device 0
-// CHECK: Collecting static profile data from device 1
+// The HSA walk drains the launched image on device 1.
// CHECK: Copied device sections:
-// CHECK: device bounds already drained, skipping
-// CHECK: walk complete: agents={{[0-9]+}} pairs={{[0-9]+}} found={{[1-9][0-9]*}} drained={{[0-9]+}}
+// CHECK: HSA walk complete: agents={{[0-9]+}} pairs={{[0-9]+}} found={{[1-9][0-9]*}} drained={{[1-9][0-9]*}}
// CHECK: PASS
#include <hip/hip_runtime.h>
More information about the llvm-commits
mailing list