Skip to content

gdb/testsuite: support optimized code in gdb.rocm testsuite#103

Open
spatrang wants to merge 4 commits into
amd-stagingfrom
users/spatrang/testsuite-optimized-code
Open

gdb/testsuite: support optimized code in gdb.rocm testsuite#103
spatrang wants to merge 4 commits into
amd-stagingfrom
users/spatrang/testsuite-optimized-code

Conversation

@spatrang
Copy link
Copy Markdown

@spatrang spatrang commented Apr 27, 2026

Summary

This series makes the gdb.rocm testsuite pass when the test programs are
compiled with optimization (-O1/-O2/-O3, -flto) passed via
GDB_TESTCASE_OPTIONS, rather than the implicit -O0 the suite assumes today.

Three commits, split by concern:

  1. Update gdb.rocm framework to support optimized code — adds helpers in
    gdb/testsuite/lib/rocm.exp:

    • check_for_any_flags {flags} — detects whether GDB_TESTCASE_OPTIONS
      contains any of the listed flags.
    • gdb_caching_proc no_optimized_code — predicate for -O1/-O2/-O3.
    • gdb_caching_proc no_lto — predicate for -flto.
  2. Update some gdb.rocm tests to support optimized code (22 files) —
    two patterns applied surgically:

    • In .cpp/.c: add volatile on locals the test inspects, and
      __attribute__((optnone)) on helper functions the test breakpoints into.
      Add a trivial volatile int statement to empty "break here" functions
      so their body is not elided.
    • In .exp: relax line-number arithmetic, backtrace PC formatting,
      expected source strings after step, and add a bounded retry loop for
      extra ticks under optimization.
  3. Skip unsupported tests in case of optimized code (19 files) — for tests
    whose semantics cannot be reconciled with optimized code, add
    require no_optimized_code at the top; mi-aspace.exp also gets
    require no_lto. register-watchpoint.exp soft-skips the tail via
    check_for_any_flags after the earlier portion has run.

Diffstat: 42 files changed, +146 / -46.

Context

This continues the work started in internal PR
AMD-ROCm-Internal/ROCgdb#6,
rebased onto the current amd-staging of the public repo. The commits are
content-identical to the internal PR, preserving the original authors
(Aleksandar Rikalo, Andrey Kasaurov) with their Co-Authored-By: trailers
and adding my Signed-off-by.

@spatrang
Copy link
Copy Markdown
Author

Hi @lancesix

This PR is a re-submission of the optimized-code series that was originally
opened in the internal rocgdb repository. I'm summarising your comments and
how they've been addressed in the latest push:

  1. gdb.rocm/deep-stack.exp"This part of the change seems odd. Is
    it really necessary?"

    Reverted. Frames #10..#12 now use the same ".* <name>" regex
    style as #0..#9; the dropped space was unnecessary.

  2. gdb.rocm/device-attach.cpp"Is code still valid without this
    line?"

    No, it isn't. Restored the __global__ void qualifier on
    bit_extract_kernel; without it the function isn't a HIP kernel
    and hipLaunchKernelGGL would fail to compile.

  3. gdb.rocm/hip-builtin-variables.cpp"Not a fan of the implicit
    int … const volatile reads off … Looks like it could just be
    volatile int."

    Agreed. All const volatile X = …; declarators have been replaced
    with volatile int X = …;.

  4. gdb.rocm/precise-memory-exec.c"Why volatile int sometimes,
    attribute((optnone)) other times?"

    Good point. second() now uses __attribute__((optnone)) instead
    of an artificial volatile int i = 0;, matching the convention
    used in corefile.cpp, names.cpp, ocp_mx.cpp, and
    step-schedlock-spurious-waves.cpp (touched in the same series).

  5. lib/rocm.exp"missing '.'" (×2) and "indentation" (×2)
    on no_optimized_code and no_lto.
    Both comments were added a trailing period, and the body of the
    if-blocks in both procs is now tab-indented to match the
    surrounding style.

The corresponding fix-up commit is on top of this branch. Please take
another look when you have a moment

cc: @lumachad

@lumachad
Copy link
Copy Markdown
Collaborator

pre-commit seems to be grumpy.

@spatrang spatrang changed the title gdb/testsuite: support optimized code in gdb.rocm testsuite (AIROCGDB-573) gdb/testsuite: support optimized code in gdb.rocm testsuite Apr 27, 2026
@spatrang spatrang force-pushed the users/spatrang/testsuite-optimized-code branch from e05abcd to 12346d2 Compare April 28, 2026 09:26
@spatrang spatrang marked this pull request as ready for review April 28, 2026 14:57
@spatrang spatrang requested a review from a team as a code owner April 28, 2026 14:57
@lumachad
Copy link
Copy Markdown
Collaborator

lumachad commented May 7, 2026

As stated elsewhere, the series only handles GDB_TESTCASE_OPTIONS. If someone passes CFLAGS directly to the compiler the optimization level checks won't work properly. Do we want to fix that as well? Maybe as a follow up if not in this one?

@lumachad
Copy link
Copy Markdown
Collaborator

lumachad commented May 7, 2026

There's a pre-commit failure.

Other than that, I'm still seeing FAIL's for gdb.rocm/watchpoint-basic.exp when running the testsuite with -O1 or -O3. It passes when running with -O0. Could you please check that you're running the testsuite correctly?

@spatrang spatrang force-pushed the users/spatrang/testsuite-optimized-code branch 3 times, most recently from 7c21341 to a912e0e Compare May 7, 2026 14:03
@lumachad
Copy link
Copy Markdown
Collaborator

lumachad commented May 8, 2026

Did the latest force-pushes provide anything that would fix gdb.rocm/watchpoint-basic.exp? I can't tell.

Comment thread gdb/testsuite/gdb.rocm/watchpoint-basic.exp Outdated
Comment thread gdb/testsuite/lib/rocm.exp Outdated
@spatrang spatrang force-pushed the users/spatrang/testsuite-optimized-code branch from 7320b08 to c729dec Compare May 11, 2026 07:06
Copy link
Copy Markdown
Collaborator

@lumachad lumachad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just small issues.

Comment thread gdb/testsuite/lib/rocm.exp Outdated
Comment thread gdb/testsuite/gdb.rocm/deep-stack.exp Outdated
Comment thread gdb/testsuite/gdb.rocm/deref-scoped-pointer.cpp Outdated
Comment thread gdb/testsuite/gdb.rocm/hip-builtin-variables.exp
Comment thread gdb/testsuite/gdb.rocm/register-watchpoint.exp Outdated
Comment thread gdb/testsuite/gdb.rocm/snapshot-objfile-on-load.exp
Comment thread gdb/testsuite/gdb.rocm/static-global.cpp Outdated
Comment thread gdb/testsuite/gdb.rocm/step-schedlock-spurious-waves.exp Outdated
@lumachad
Copy link
Copy Markdown
Collaborator

lumachad commented May 11, 2026

For -lto runs, it looks like gdb.rocm/hip-builtin-variables.exp and gdb.rocm/hip-lang-detect.exp are still failing.

FAIL: gdb.rocm/hip-lang-detect.exp: language detection (dwarfdump execution)
FAIL: gdb.rocm/hip-lang-detect.exp: expected compiler language (found "unknown")

@spatrang
Copy link
Copy Markdown
Author

For -lto runs, it looks like gdb.rocm/hip-builtin-variables.exp and gdb.rocm/hip-lang-detect.exp are still failing.

FAIL: gdb.rocm/hip-lang-detect.exp: language detection (dwarfdump execution)
FAIL: gdb.rocm/hip-lang-detect.exp: expected compiler language (found "unknown")

Both hip-builtin-variables.exp and hip-lang-detect.exp now require no_lto. LTO can re-inline across TUs (defeating noinline/optnone boundaries) and rewrite the per-CU DW_LANG attribute, so the test premises don't hold at -flto. Matches the existing require no_lto pattern in watchpoint-at-end-of-shader.exp and mi-aspace.exp.

@spatrang spatrang force-pushed the users/spatrang/testsuite-optimized-code branch from c729dec to 65a4a86 Compare May 11, 2026 14:18
Copy link
Copy Markdown
Collaborator

@lumachad lumachad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only had a minor comment. Otherwise this LGTM. I'd like @lancesix to eyeball this as well.

Comment thread gdb/testsuite/gdb.rocm/watchpoint-basic.exp Outdated
Comment thread gdb/testsuite/gdb.rocm/deep-stack.exp Outdated
Comment thread gdb/testsuite/gdb.rocm/alu-exceptions.cpp Outdated
Comment thread gdb/testsuite/gdb.rocm/corefile.cpp Outdated
Comment thread gdb/testsuite/gdb.rocm/deep-stack.exp Outdated
Comment thread gdb/testsuite/gdb.rocm/deref-scoped-pointer.cpp Outdated
Comment thread gdb/testsuite/gdb.rocm/deref-scoped-pointer.cpp Outdated
Comment thread gdb/testsuite/gdb.rocm/precise-memory-exec.c Outdated
Comment thread gdb/testsuite/gdb.rocm/precise-memory-fork.c Outdated
Comment thread gdb/testsuite/gdb.rocm/static-global.cpp Outdated
Comment thread gdb/testsuite/lib/rocm.exp Outdated
Comment thread gdb/testsuite/lib/rocm.exp Outdated
@spatrang spatrang force-pushed the users/spatrang/testsuite-optimized-code branch from 65a4a86 to 24d7933 Compare May 12, 2026 14:11
Introduce the necessary helper functions into the gdb.rocm
framework so that support for optimized code can be implemented.

  - check_for_any_flags: detects whether GDB_TESTCASE_OPTIONS,
    CFLAGS_FOR_TARGET or CXXFLAGS_FOR_TARGET contains any of the
    given flags.  Designed for whitespace-free flags such as
    -O2, -flto.
  - gdb_caching_proc no_optimized_code: predicate for -O1/-O2/-O3.
  - gdb_caching_proc no_lto: predicate for -flto.

Co-Authored-By: Andrey Kasaurov <andrey.kasaurov@amd.com>
Co-Authored-By: Luis Machado <luis.machado@amd.com>

Change-Id: I750d64e842413429e281bb8f7d85c4f46319a627
Signed-off-by: Sarang Patrange <spatrang@amd.com>
@spatrang spatrang force-pushed the users/spatrang/testsuite-optimized-code branch from 24d7933 to d415a28 Compare May 27, 2026 08:29
@lumachad
Copy link
Copy Markdown
Collaborator

Things look clean from -O1 through -O3. I do see a FAIL with -flto though:

FAIL: gdb.rocm/hip-builtin-completions.exp: language detection (dwarfdump execution)

Since we claim to be clean on -flto runs as well, we should fix that or skip it.

@spatrang spatrang requested a review from lancesix May 27, 2026 11:41
@spatrang spatrang force-pushed the users/spatrang/testsuite-optimized-code branch from d415a28 to ea1067d Compare May 27, 2026 12:23
@lumachad
Copy link
Copy Markdown
Collaborator

Confirmed a clean run for -flto as well. Thanks.

@spatrang spatrang force-pushed the users/spatrang/testsuite-optimized-code branch from ea1067d to 42d183e Compare May 28, 2026 06:50
lumachad
lumachad previously approved these changes May 28, 2026
Copy link
Copy Markdown
Collaborator

@lumachad lumachad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving my approval here. If Lancelot doesn't have any restrictions on this series, feel free to merge.

Copy link
Copy Markdown
Collaborator

@lancesix lancesix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments on patch 1.

Comment thread gdb/testsuite/lib/rocm.exp Outdated
Comment thread gdb/testsuite/lib/rocm.exp Outdated
Comment thread gdb/testsuite/lib/rocm.exp Outdated
Comment thread gdb/testsuite/lib/rocm.exp Outdated
Comment thread gdb/testsuite/lib/rocm.exp
Copy link
Copy Markdown
Collaborator

@lancesix lancesix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments for patch 2.

It seems that this pattern is not consistently implemented

  • Make breakpoint-anchor helper functions ("done", "foo",
    "bar") non-static and noinline, with an asm volatile body,
    so the symbol survives the optimiser and the breakpoint
    can be inserted.

Seems that some of that logic could be factored out to help (would avoid having to repeat the comment giving the rationale for the asm and so on).

Comment thread gdb/testsuite/gdb.rocm/alu-exceptions.cpp Outdated
Comment thread gdb/testsuite/gdb.rocm/corefile.cpp Outdated
Comment thread gdb/testsuite/gdb.rocm/corefile.cpp Outdated
Comment thread gdb/testsuite/gdb.rocm/deep-stack.exp Outdated
Comment thread gdb/testsuite/gdb.rocm/finish.cpp Outdated
Comment thread gdb/testsuite/gdb.rocm/hip-builtin-variables.cpp
Comment thread gdb/testsuite/gdb.rocm/lane-info.cpp
Comment thread gdb/testsuite/gdb.rocm/lane-info.cpp Outdated
Comment thread gdb/testsuite/gdb.rocm/lane-info.cpp
Comment thread gdb/testsuite/gdb.rocm/step-schedlock-spurious-waves.exp Outdated
Copy link
Copy Markdown
Collaborator

@lancesix lancesix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments for the 3rd path.

It really seems that many of the changes from this patch belong to the previous one (many changes are just modifying what the previous patch). Arguably the previous patch does too much at once and could be split, but regardless the fixup should not end-up here.

Comment thread gdb/testsuite/gdb.rocm/alu-exceptions.cpp
Comment thread gdb/testsuite/gdb.rocm/corefile.cpp
Comment thread gdb/testsuite/gdb.rocm/deep-stack.exp Outdated
Comment thread gdb/testsuite/gdb.rocm/deref-scoped-pointer.cpp
Comment thread gdb/testsuite/gdb.rocm/deref-scoped-pointer.cpp
Comment thread gdb/testsuite/gdb.rocm/multi-inferior-gpu.cpp
Comment thread gdb/testsuite/gdb.rocm/precise-memory-exec.c
Comment thread gdb/testsuite/gdb.rocm/precise-memory-fork.c
Comment thread gdb/testsuite/gdb.rocm/static-global.cpp
Comment thread gdb/testsuite/gdb.rocm/watchpoint-basic.exp Outdated
@lumachad lumachad dismissed their stale review May 29, 2026 10:07

Seems like Lancelot has more comments. I'll let him take over.

@spatrang spatrang self-assigned this May 29, 2026
Adapt several gdb.rocm tests so they pass under -O1/-O2/-O3:

  - Apply __attribute__((optnone)) to device functions whose
    locals or call structure the tests rely on, and to host
    main() where its store-then-exit pattern would otherwise
    be tail-optimised.

  - Make breakpoint-anchor helper functions ("done", "foo",
    "bar") non-static and noinline, with an asm volatile body,
    so the symbol survives the optimiser and the breakpoint
    can be inserted.

  - Mark local variables volatile where the test reads them
    via convenience variables or takes their address.

  - Restore "__global__ void" qualifier on device-attach
    kernel, and mark its spin-loop guard volatile so the loop
    survives optimisation.

  - Relax regex/line-number expectations under optimisation
    in deep-stack, hip-builtin-variables, snapshot-objfile-on-
    load, and step-schedlock-spurious-waves.

  - Brace expr-substitutions in deep-stack and until-tests
    (tclint hygiene).

Co-Authored-By: Luis Machado <luis.machado@amd.com>

Change-Id: I750d64e842413429e281bb8f7d85c4f46319a627
Signed-off-by: Sarang Patrange <spatrang@amd.com>
@spatrang
Copy link
Copy Markdown
Author

Hello @lancesix @lumachad
Pushed an updated revision addressing the latest review round:

  • Split out the deep-stack.exp backtrace regex into its own commit (it's an independent fix from the optimization work).
  • Folded the "belongs in previous patch" churn back into the right commits (no more add-then-revert across commits, e.g. deref-scoped-pointer, corefile).
  • rocm.exp: check_for_any_flags/no_optimized_code/no_lto now use conventional boolean returns (1/0) and inline if {[check_for_any_flags ...]}; dropped the happy-case log lines.
  • Style: attribute ((...)) spacing in corefile.cpp and finish.cpp.
  • alu-exceptions / fork-exec-* / precise-memory-*: dropped the artificial volatile int locals in favor of noinline + asm volatile ("" ::: "memory").
  • lane-info.cpp: fixed the UB infinite loop (volatile bool keep_going); kept noinline where the test needs a real frame.
  • step-schedlock-spurious-waves.exp: merged the redundant retry loop into the existing max_iter budget.
  • hip-builtin-completions.exp: added require no_lto (the dwarfdump language check is unreliable under -flto).

Re-tested the modified tests at -O0/-O1/-O2/-O3: 0 failures (the 3 UNSUPPORTED at -O1+ are the intended watchpoint-basic before-runtime-load skips).

@spatrang spatrang assigned lancesix and unassigned spatrang May 29, 2026
@spatrang spatrang force-pushed the users/spatrang/testsuite-optimized-code branch from 42d183e to b94cbc7 Compare May 29, 2026 14:07
@spatrang spatrang requested review from lancesix and lumachad May 29, 2026 14:12
spatrang and others added 2 commits May 29, 2026 09:47
Match the optional "0xADDR in " prefix explicitly: GDB prints it at
-O0 (and for non-innermost frames) but omits it at -O1/-O2/-O3.  The
old "#N .* NAME .*" patterns also depended on frame-number column
alignment for frames #10-#12.  Make the address prefix optional for
all frames #0-#12 so the backtrace matches at every optimization
level.

Signed-off-by: Sarang Patrange <spatrang@amd.com>
For tests whose semantics cannot be reconciled with optimised
code, gate them so they UNSUPPORTED-out cleanly instead of
failing.

  - "require no_optimized_code" at the top of: aspace-user-
    input, aspace-watchpoint, convenience_variables, device-
    barrier, finish, fork-exec-non-gpu-to-gpu, lane-execution,
    mi-aspace, mi-lanes, nonstop-displaced, precise-memory-
    warning-watchpoint, register-watchpoint, runtime-core,
    shared-memory.

  - "require no_lto" at the top of: hip-builtin-variables,
    hip-lang-detect, mi-aspace, watchpoint-at-end-of-shader.

  - watchpoint-basic.exp: proc-level skip of
    test_non_write_watchpoint_before_runtime_load's rwatch/
    awatch sub-cases under optimisation.  The HIP runtime is
    initialised before main() reaches "Break before runtime
    load", so amd-dbgapi is already attached and rejects the
    host-side read/access watchpoint at insert time.  Only
    this sub-test depends on the "before runtime load"
    premise; the other procs still work under optimisation,
    so use a proc-level check_for_any_flags skip rather than
    a file-level "require no_optimized_code".

Co-Authored-By: Andrey Kasaurov <andrey.kasaurov@amd.com>
Co-Authored-By: Luis Machado <luis.machado@amd.com>

Change-Id: I750d64e842413429e281bb8f7d85c4f46319a627
Signed-off-by: Sarang Patrange <spatrang@amd.com>
@spatrang spatrang force-pushed the users/spatrang/testsuite-optimized-code branch from b94cbc7 to 83529fb Compare May 29, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants