Skip to content

gdb/amdgcn: Dynamically select which bits to use to hold CORE_ADDR#56

Merged
amd-shahab merged 2 commits intoamd-stagingfrom
users/shvahedi/aspace-id-in-core-addr
Apr 16, 2026
Merged

gdb/amdgcn: Dynamically select which bits to use to hold CORE_ADDR#56
amd-shahab merged 2 commits intoamd-stagingfrom
users/shvahedi/aspace-id-in-core-addr

Conversation

@amd-shahab
Copy link
Copy Markdown
Contributor

Relies on dbgapi's PR: Add AMD_DBGAPI_PROCESS_INFO_SIGNIFICANT_ADDRESS_BITS

To support the various address-spaces from AMDGPU, amdgpu-tdep implements gdbarch hooks to encode the address-space ID in the top bits of a CORE_ADDR. Exactly which bits to use out of the CORE_ADDR is currently statically built into GDB. However, for AMDGPU, the "flat" address-space (also known as generic) is configured by the driver, and it is not part of the ABI which bits out of the top byte are used or not to carry meaning.

To address this, librocm-dbgapi >= 0.80 provides a mask of bits which carry information for the current process. GDB can use this mask to deduce which bits out of a CORE_ADDR can safely be used to encode address space IDs.

This patch provides an implementation for GDB to encode the address-space ID into the top bits reported as unused by dbgapi. Note that dbgapi reports which bits can be used by any address space, including "global". This means that the unused bits must be the top ones, and we should account for the fact that "normalized" addresses are sign extended in the top bits. This means that the top bits of a valid address can be either all 0s or all 1s. For this reason, we do not support encoding an address space ID whose binary representation on the available bits of a CORE_ADDR is all 1s, so the all 1s case can be reserved for sign-extended addresses.

Bug: AIROCGDB-26
Change-Id: I4cbb41a922443d64003ebc6415547c02ab43afcf

@amd-shahab amd-shahab force-pushed the users/shvahedi/aspace-id-in-core-addr branch from ed82c64 to 187e19d Compare April 7, 2026 12:57
@amd-shahab amd-shahab requested a review from a team as a code owner April 7, 2026 12:57
@amd-shahab amd-shahab force-pushed the users/shvahedi/aspace-id-in-core-addr branch from 187e19d to 0f7b779 Compare April 7, 2026 12:58
@amd-shahab
Copy link
Copy Markdown
Contributor Author

amd-shahab commented Apr 7, 2026

  • rebase
  • use .try_emplace() instead of .emplace()
  • rename invalidate_cache...() to amdgpu_observer_inferior_appeared().

@amd-shahab amd-shahab force-pushed the users/shvahedi/aspace-id-in-core-addr branch from 0f7b779 to c635173 Compare April 16, 2026 09:04
Comment thread gdb/amdgpu-tdep.c Outdated
Comment thread gdb/amdgpu-tdep.c Outdated
Comment thread gdb/amdgpu-tdep.c
(address_space_id) & 1) << shift));
shift++;
address_space_id >>= 1;
}
Copy link
Copy Markdown
Collaborator

@palves palves Apr 16, 2026

Choose a reason for hiding this comment

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

I think this be equivalent to the above. It'd be more efficient, for not having a nested loop, and you wouldn't need ctz:

uint64_t mask = aspace_id_mask;
uint64_t id = address_space_id;
uint64_t result = 0;

for (; id != 0 && mask != 0; id >>= 1) 
  {
     /* Isolate the lowest set bit in the mask.  */
      uint64_t lowbit = mask & ~(mask - 1);
      if (id & 1)
         result |= lowbit;
      /* Clear bit from mask.   */
      mask ^= lowbit;
  }
address |= result;

(disclaimer: completely untested, written in github comment directly.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

brilliant!

Comment thread gdb/amdgpu-tdep.c
<< aspace_id_shift++);
shift++;
}

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.

Here, I think we can do the same as in amdgpu_segment_address_to_core_address:

arch_addr_space_id aspace_id = 0;
uint64_t dest_bit = 1;    /* bit position in resulting ID */
uint64_t mask = aspace_id_mask;

/* Iterate for as many bits as are set in the mask.  */
while (mask != 0)
  {
    /* Isolate lowest set bit in mask.  */
    uint64_t lowbit = mask & ~(mask - 1);
    
    if (addr & lowbit)
      aspace_id |= dest_bit;

    dest_bit <<= 1;
    /* Clear bit from mask.   *
    mask ^= lowbit;
  }

return aspace_id;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

master piece.

lancesix and others added 2 commits April 16, 2026 14:25
To support the various address-spaces from AMDGPU, amdgpu-tdep
implements gdbarch hooks to encode the address-space ID in the top bits
of a CORE_ADDR.  Exactly which bits to use out of the CORE_ADDR is
currently statically built into GDB.  However, for AMDGPU, the "flat"
address-space (also known as generic) is configured by the driver, and
it is not part of the ABI which bits out of the top byte are used or not
to carry meaning.

To address this, librocm-dbgapi >= 0.80 provides a mask of bits which
carry information for the current process.  GDB can use this mask to
deduce which bits out of a CORE_ADDR can safely be used to encode
address space IDs.

This patch provides an implementation for GDB to encode the
address-space ID into the top bits reported as unused by dbgapi.  Note
that dbgapi reports which bits can be used by any address space,
including "global".  This means that the unused bits must be the top
ones, and we should account for the fact that "normalized" addresses
are sign extended in the top bits.  This means that the top bits of a
valid address can be either all 0s or all 1s.  For this reason, we do
not support encoding an address space ID whose binary representation on
the available bits of a CORE_ADDR is all 1s, so the all 1s case can be
reserved for sign-extended addresses.

Bug: AIROCGDB-26

Co-Authored-By: Pedro Palves <pedro@palves.net>
Change-Id: I4cbb41a922443d64003ebc6415547c02ab43afcf
Now that in its configure script, rocgdb requires dbgapi 0.80,
for querying significant address bits, remove the codes that
try to stay compatible with dbgapi versions lower than 0.79.

Change-Id: I675aaac0fdf3fa3a5e73ee71a79606e08d878754
@amd-shahab amd-shahab force-pushed the users/shvahedi/aspace-id-in-core-addr branch from c635173 to e7d1a38 Compare April 16, 2026 12:26
@amd-shahab
Copy link
Copy Markdown
Contributor Author

  • get rid of invocations of ctz() builtins in favour of better algorithms suggested by @palves

@amd-shahab amd-shahab merged commit c06eb06 into amd-staging Apr 16, 2026
5 of 9 checks passed
@amd-shahab amd-shahab deleted the users/shvahedi/aspace-id-in-core-addr branch April 16, 2026 14:35
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.

3 participants