Skip to content

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

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

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

Conversation

@spatrang

@spatrang spatrang commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

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
Contributor 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

lumachad commented May 7, 2026

Copy link
Copy Markdown
Collaborator

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

lumachad commented May 7, 2026

Copy link
Copy Markdown
Collaborator

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

lumachad commented May 8, 2026

Copy link
Copy Markdown
Collaborator

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

@lumachad lumachad left a comment

Copy link
Copy Markdown
Collaborator

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

lumachad commented May 11, 2026

Copy link
Copy Markdown
Collaborator

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
Contributor 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

@lumachad lumachad left a comment

Copy link
Copy Markdown
Collaborator

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
@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
@spatrang spatrang self-assigned this Jun 3, 2026

@lumachad lumachad left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since we're doing a new pass. Please cleanup the following:

Inconsistent spacing around attribute across the PR.

  • gdb/testsuite/gdb.rocm/hip-builtin-variables.cpp:37,45,52,71 — attribute((optnone))
  • gdb/testsuite/gdb.rocm/lane-info.cpp:38,49,53 — attribute((noinline)), attribute((noinline, disable_tail_calls))
  • gdb/testsuite/gdb.rocm/line-breakpoint-in-kernel.cpp:34 — attribute((optnone))
  • gdb/testsuite/gdb.rocm/multi-inferior-fork.cpp:39 — attribute((optnone))
  • gdb/testsuite/gdb.rocm/multi-inferior-gpu.cpp:35 — attribute((optnone))
  • gdb/testsuite/gdb.rocm/names.cpp:127 — attribute((optnone))
  • gdb/testsuite/gdb.rocm/ocp_mx.cpp — five occurrences of attribute((optnone))
  • gdb/testsuite/gdb.rocm/scheduler-locking.cpp:36 — attribute((optnone))
  • gdb/testsuite/gdb.rocm/static-global.cpp:40 — attribute((optnone))
  • gdb/testsuite/gdb.rocm/step-schedlock-spurious-waves.cpp:27 — attribute((optnone))
  • gdb/testsuite/gdb.rocm/unaligned-memory-access.cpp:52,57 — attribute((noinline)), attribute((optnone))
  • gdb/testsuite/gdb.rocm/watch-gpu-global-from-host.cpp:27 — attribute((optnone))
  • gdb/testsuite/gdb.rocm/finish.cpp:225 — new attribute((optnone)) (still missing space, only on the kernel declaration)
  • Fix: normalise to attribute ((...)) everywhere added by the patch.

Typos / Comments

  • Spelling: inconsistent British/American mix introduced by the PR. New comments use optimiser, optimisation, initialisation, optimised in some files (alu-exceptions.cpp, fork-exec-.cpp, precise-memory-.c, deref-scoped-pointer.exp, hip-builtin-variables.exp,
    watchpoint-basic.exp) and optimization/optimized in others (the new lib/rocm.exp predicates, static-global.cpp, several require no_optimized_code skips). Existing gdb tree leans American (optimize); pick one for the whole PR.
  • gdb/testsuite/gdb.rocm/static-global.cpp:37–39 — the new comment explains why optnone is used but doesn't explain the unrelated change on the same line from static to non-static. The companion file deref-scoped-pointer.cpp:35–37 does call out "Non-static so a line
    breakpoint…". Add the same rationale here for consistency.

Bugs

  • gdb/testsuite/gdb.rocm/watch-gpu-global-from-host.cpp:34 — silently downgrades a C++ cast to a C-style cast:
  -  int *devGlobal;
  -  if (hipGetSymbolAddress (reinterpret_cast<void **> (&devGlobal), global))
  +  volatile int *devGlobal;
  +  if (hipGetSymbolAddress ((void **) &devGlobal, global))
  • The cast change is presumably because reinterpret_cast<void**> won't strip the new volatile qualifier — but the C-style cast hides exactly that fact. This is the kind of cast that the GNU/ROCgdb style explicitly discourages in C++ code. Better: keep an explicit const_cast/reinterpret_cast pair so the volatile strip is visible, or store devGlobal as non-volatile and only mark the access path volatile.
  • gdb/testsuite/gdb.rocm/lane-info.cpp:51–55 — the trailing asm volatile ("" : : : "memory"); in bar() is unreachable because sleep_forever() is an endless loop (volatile bool keep_going = true; while (keep_going) …). The author's stated rationale ("volatile asm prevents the call from being constant-folded away at the call site") refers to foo(), where it makes sense. In bar() the disable_tail_calls attribute is the load-bearing piece; the asm is dead. Either drop the asm in bar() and rely on disable_tail_calls, or annotate sleep_forever as attribute ((noreturn)) and remove the dead asm.
  • gdb/testsuite/lib/rocm.exp check_for_any_flags (lines ~165–200) — uses lsearch -exact $src $flag, which relies on Tcl's string→list coercion of $src. If a future caller puts a flag with {} or unbalanced quotes into GDB_TESTCASE_OPTIONS/CFLAGS_FOR_TARGET, the implicit list conversion throws. Defensive form: if {[lsearch -exact [split $src] $flag] != -1} — split with no separators yields whitespace-tokenised words and avoids the auto-list coercion. Same logic, harder to break later.

@spatrang spatrang force-pushed the users/spatrang/testsuite-optimized-code branch from 83529fb to 9157ed9 Compare June 3, 2026 11:45
@spatrang spatrang requested a review from lumachad June 3, 2026 11:46
@lumachad

lumachad commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator

Sent offline comments.

@lumachad lumachad left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just one nit this time:

  • gdb/testsuite/gdb.rocm/until-tests.exp:76-79 — the test was: until lands on exactly breakpoint_loc+1. After this patch it accepts +1 or +2 unconditionally, even at -O0. That weakens the assertion for the non-optimized path it used to cover. Gate the loose pattern on
    [check_for_any_flags {-O1 -O2 -O3}] and keep the strict +1 check for the unoptimized build.

I'd give Lancelot a chance to go through it again.

@spatrang

spatrang commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

Just one nit this time:

  • gdb/testsuite/gdb.rocm/until-tests.exp:76-79 — the test was: until lands on exactly breakpoint_loc+1. After this patch it accepts +1 or +2 unconditionally, even at -O0. That weakens the assertion for the non-optimized path it used to cover. Gate the loose pattern on
    [check_for_any_flags {-O1 -O2 -O3}] and keep the strict +1 check for the unoptimized build.

I'd give Lancelot a chance to go through it again.

Thanks! Pushed 86c2e23: gated the (+1|+2) alternation on check_for_any_flags {-O1 -O2 -O3}, restored the strict +1 check at -O0. until-tests.exp 28/28 PASS at -O0 and -O2.

@lancesix lancesix left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Note that this time I only looked at the overall diff, but having individual commits is still important, I'll check this on next round.

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/finish.cpp Outdated
Comment thread gdb/testsuite/gdb.rocm/fork-exec-gpu-to-non-gpu-execer.cpp
Comment thread gdb/testsuite/gdb.rocm/lane-info.cpp Outdated
Comment thread gdb/testsuite/gdb.rocm/multi-inferior-fork.cpp Outdated
Comment thread gdb/testsuite/gdb.rocm/ocp_mx.cpp Outdated
@spatrang spatrang assigned lancesix and unassigned spatrang Jun 5, 2026
@spatrang spatrang requested review from aktemur, lancesix and lumachad June 5, 2026 09:09
@aktemur aktemur self-assigned this Jun 9, 2026
@spatrang spatrang force-pushed the users/spatrang/testsuite-optimized-code branch from c0fae00 to 3aba2e8 Compare June 10, 2026 07:59

@aktemur aktemur left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm having some difficulty following the changes introduced by patches. Some patches should've been fixups -- they modify previous changes introduced by patches in the same PR. Can we please split, re-order, re-organize the patches so that the changes are focused?

Comment thread gdb/testsuite/lib/rocm.exp Outdated
Comment thread gdb/testsuite/lib/rocm.exp
Comment thread gdb/testsuite/gdb.rocm/mi-aspace.exp Outdated
Comment thread gdb/testsuite/gdb.rocm/hip-builtin-variables.exp
Comment thread gdb/testsuite/gdb.rocm/aspace-user-input.exp
Comment thread gdb/testsuite/gdb.rocm/deref-scoped-pointer.cpp Outdated
Comment thread gdb/testsuite/gdb.rocm/finish.cpp
Comment thread gdb/testsuite/gdb.rocm/hip-builtin-variables.cpp Outdated
Comment thread gdb/testsuite/lib/rocm.exp
Comment thread gdb/testsuite/gdb.rocm/until-tests.exp Outdated
@aktemur aktemur assigned spatrang and unassigned aktemur and lancesix Jun 10, 2026

@lancesix lancesix left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hi,

I did not really re-read evertything, but it seems you push commits to this branch to fix previous commits of the branch still under review (gdb/testsuite/gdb.rocm: drop redundant test decorations for example).

We do not squash commits in GDB, but rather craft series and refine patches in the series as needed. The bits you change in that last patch have to be integrated into previous patches.

@spatrang spatrang force-pushed the users/spatrang/testsuite-optimized-code branch from 3aba2e8 to 41a9d39 Compare June 17, 2026 18:19
@spatrang

spatrang commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

Commit refactor (force-pushed). Squashed the series into two single-purpose commits:

  1. gdb/testsuite: update gdb.rocm framework to support optimized code -- the lib/rocm.exp helpers (check_for_any_flags, no_optimized_code, no_lto).
  2. gdb/testsuite: update gdb.rocm tests to support optimized code -- all test source/expectation adjustments plus the require no_optimized_code / require no_lto gates.

Folded in this review round's feedback:

  • Commit titles now carry the gdb/testsuite: label (@aktemur).
  • check_for_any_flags uses [llength $flags] == 0 instead of a boolean test.
  • mi-aspace.exp combines the two predicates into require no_optimized_code no_lto.
  • no_optimized_code now also catches -O, -Os, -Ofast, -Og (whole-token match).
  • Trailing periods added to the verbose -log strings.

The numerous "this hunk should've been a fixup of the originating patch" comments are addressed by the squash itself -- the intra-file churn from the old multi-commit history is collapsed into these two logical commits.

Verified deep-stack.exp passes at -O3 (full #0..#12 backtrace).

One open question: should the Change-Id trailers be kept (amd-staging / Gerrit convention) or dropped? Happy to do either.

motokultivator and others added 2 commits June 19, 2026 07:33
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>
Using the framework helpers added in the previous commit, adjust the
gdb.rocm tests so they behave correctly when the test programs are
built with optimization (-O1/-O2/-O3, -flto):

  - Adapt test sources and expectations so the tests pass under
    optimization (optnone/noinline on the relevant functions,
    relaxed regex and line-number expectations, etc.).
  - Gate the tests whose semantics cannot be reconciled with
    optimized code behind "require no_optimized_code" / "require
    no_lto" so they are cleanly reported as unsupported.

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

Copy link
Copy Markdown
Contributor Author

Rebased onto the latest amd-staging.

The only conflicts were in the require lines of the gated .exp tests: amd-staging renamed allow_hipcc_tests -> allow_hip_tests. Resolved by taking the new proc name and keeping the require no_optimized_code / require no_lto gates this series adds.

Re-tested the full gdb.rocm/*.exp suite on the rebased branch (freshly built rocgdb), no unexpected failures at any level:

Level pass fail unsupported
-O0 1891 0 8
-O1 1456 0 27
-O2 1456 0 27
-O3 1456 0 27

The lower pass count at -O1+ is the expected effect of the require no_optimized_code / require no_lto gates. Remaining XFAIL/UNTESTED entries (finish, runtime-core, ocp_mx) are pre-existing environment limitations unrelated to this series.

@spatrang spatrang force-pushed the users/spatrang/testsuite-optimized-code branch from 41a9d39 to d7d84ec Compare June 19, 2026 16:17
@spatrang spatrang removed their assignment Jun 22, 2026
@spatrang spatrang marked this pull request as draft June 22, 2026 12:04
@spatrang spatrang marked this pull request as ready for review June 22, 2026 12:04
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.

7 participants