Skip to content

serve: a rejected IREE_HAL_AMDGPU_LIBHSA_PATH does not reach the backend either (follow-up to #3323, engine#3314) - #3326

Merged
bong-water-water-bong merged 1 commit into
mainfrom
cl-kernel/libhsa-rejected-env
Nov 22, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
cl-kernel/libhsa-rejected-env

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Follow-up to #3323 (engine#3314), for the defect raised in my review there before it merged.

#3323 rejects a user IREE_HAL_AMDGPU_LIBHSA_PATH that is not the absolute path of an existing libhsa-runtime64.so*. In that case it logs …is not used and names the runtime it falls back to. But the rejected value was still inherited by every child: the launch paths pass serve's choice as merge_env's env_extra (serve.cpp:1460/1698/1951/2253), and an inherited variable wins over env_extra. So the backend opened the rejected file while the log named a different one. --print-backend-env had the same hole, because it prints a variable only when serve's own environment doesn't have it.

Change (app/serve.cpp, +11): at startup, in every mode, when the variable is non-empty, --hrx-libhsa is not set, and user_libhsa() rejects the value, serve removes it from its own environment (unsetenv; _putenv_s(…, "") on Windows). From then on the launch paths' value is the one a backend gets, and the plan prints it. A valid user value and --hrx-libhsa behave exactly as before.

Tests (tests/hrx_runtime_route.sh; no new file, so no ctest row and no new skip)

  • For each of the four rejected values: the backend's IREE_HAL_AMDGPU_LIBHSA_PATH equals the runtime serve's log names. That is the build copy, the /opt/rocm-therock scan, or none; which one depends on the machine, so the row compares against the log rather than a fixed path.
  • --print-backend-env with a rejected value prints the same runtime line as with no user value.

Measured on ryzen (CI-style build: cmake -G Ninja -DCMAKE_BUILD_TYPE=Release, gcc-16, target onebit)

  • main's serve.cpp: hrx_runtime_route.sh gives 22 ok, 4 FAIL (the four new backend rows).
  • This branch: 26 ok, 0 FAIL.
  • ctest -R 'hrx_runtime_route|hrx_cwsr_route|laya_gguf_route|recipes_route|print_backend|lemonade_hrx|mem_admit_route': 7/7 pass.
  • bash scripts/pre-pr-checks.sh --base origin/main: 18 green, 2 skipped (the gpu-kernels audits).

Caveat: this build machine has no TheRock runtime, so serve's fallback here is "none". The plan row therefore compares two empty plans on ryzen; it only bites on a machine where a runtime is found. The four backend rows fail on main either way. Not run on hardware; the change is in environment handling only.

🤖 Generated with Claude Code

…end either (follow-up to #3323, engine#3314)

#3323 made serve refuse a user value that is not the absolute path of a libhsa-runtime64.so* file
and log "is not used", then name the runtime it fell back to. The value was still inherited by
every child: serve passes its choice as merge_env's env_extra, which an inherited variable beats,
so the backend opened the rejected file while the log named another. --print-backend-env had the
same hole (it prints a variable only when serve's environment does not have it).

serve now drops a rejected value from its own environment at startup, in every mode, so the
launch paths' value is the one a backend gets and the plan prints it. tests/hrx_runtime_route.sh:
for each rejected value the backend gets the runtime the log names (fails on main, 4 rows), and
the --print-backend-env plan under a rejected value equals the plan with no user value.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Environment Variable Cleanup

The PR addresses a correctness issue where a rejected IREE_HAL_AMDGPU_LIBHSA_PATH value was still inherited by child processes, causing a mismatch between the runtime logged by serve and the one actually used by the backend. The fix ensures that when a user-provided value is rejected, it is removed from serve's own environment to prevent it from being passed down to backends.

    if (const char* v = std::getenv("IREE_HAL_AMDGPU_LIBHSA_PATH"); v && *v && o.hrx_libhsa.empty() && user_libhsa().empty()) {
#ifdef _WIN32
        (void) _putenv_s("IREE_HAL_AMDGPU_LIBHSA_PATH", "");
#else
        (void) ::unsetenv("IREE_HAL_AMDGPU_LIBHSA_PATH");
#endif
    }
Test Coverage for Rejected Environment Variables

The tests now verify that a rejected IREE_HAL_AMDGPU_LIBHSA_PATH value does not reach the backend and that --print-backend-env correctly reflects the runtime chosen by serve rather than the rejected user value. This ensures the fix is properly validated.

for bad in "user/libhsa-runtime64.so.1" "$scratch/user/../user/libhsa-runtime64.so.1" "$scratch/plain.gguf" "$scratch/nope/libhsa-runtime64.so.1"; do
    IREE_HAL_AMDGPU_LIBHSA_PATH=$bad ONEBIT_HRX_NO_TRIO_OK=1 run "$scratch/bad.json" --device hrx --hrx-cwsr-placement off
    check "user env '$bad': not used" '! grep -q "HSA runtime $bad" "$scratch/bad.json.log"'
    check "user env '$bad': serve says it is not used" \
        'grep -q "IREE_HAL_AMDGPU_LIBHSA_PATH=$bad is not used" "$scratch/bad.json.log"'
    # ...and the backend does not get it either: inherited, it would beat serve's choice (merge_env) and the backend
    # would open a file the log says is not used. Which runtime serve falls back to depends on the build machine (the
    # build's copy, the /opt/rocm-therock scan, or none), so the row asserts that the backend gets the one the log names.
    named=$(sed -n 's/^1bit serve: HRX: HSA runtime \(.*\) \(sha256 [0-9a-f]*\|(unreadable.*\)$/\1/p' "$scratch/bad.json.log" | head -n 1)
    check "user env '$bad': the backend gets the runtime the log names, not the rejected value" \
        'env_is "$scratch/bad.json" "{\"libhsa\": \"$named\"}"'
done
# The plan --print-backend-env hands a caller with a rejected value in its environment: the runtime line serve would
# print with no user value at all (the control), so the caller's rejected value is replaced, not kept.
plan() { "$bin" serve -m "$scratch/plain.gguf" --device hrx --hrx-cwsr-placement off --no-mem-check --print-backend-env 2>/dev/null |
    grep '^IREE_HAL_AMDGPU_LIBHSA_PATH=' || true; }
want_plan=$(ONEBIT_HRX_NO_TRIO_OK=1 plan)
got_plan=$(IREE_HAL_AMDGPU_LIBHSA_PATH=user/libhsa-runtime64.so.1 ONEBIT_HRX_NO_TRIO_OK=1 plan)
check "--print-backend-env: a rejected user value gets serve's own runtime line (${want_plan:-none on this machine})" \
    '[ "$got_plan" = "$want_plan" ]'

@bong-water-water-bong
bong-water-water-bong enabled auto-merge (squash) November 22, 2026 20:12
@bong-water-water-bong
bong-water-water-bong merged commit c3cab73 into main Nov 22, 2026
11 of 12 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the cl-kernel/libhsa-rejected-env branch November 22, 2026 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant