Skip to content

[comgr][hotswap] Add <kernel>.stub symbols for entry trampolines#3280

Open
lmoriche wants to merge 2 commits into
amd-stagingfrom
users/lmoriche/comgr-hotswap-entry-trampoline-stub-symbols
Open

[comgr][hotswap] Add <kernel>.stub symbols for entry trampolines#3280
lmoriche wants to merge 2 commits into
amd-stagingfrom
users/lmoriche/comgr-hotswap-entry-trampoline-stub-symbols

Conversation

@lmoriche

@lmoriche lmoriche commented Jul 9, 2026

Copy link
Copy Markdown

With entry trampolines installed, a kernel descriptor's entry now points at the appended stub instead of the kernel body. Tools that resolve a dispatch's entry address to a name -- e.g. rocgdb info dispatches -- then print a bare address because the stub has no symbol.

Add a <kernel_name>.stub STT_FUNC symbol (covering the 256-byte stub) to the code object's .symtab for each appended stub. rocgdb/amd-dbgapi reads the non-alloc .symtab and now reports the dispatch as e.g. hello[stub].

Only the trailing non-alloc .symtab / .strtab sections grow, so no virtual addresses, program headers, or relocations change, and .dynsym (used by the HSA loader) is untouched -- the rewritten object still loads and runs identically. The string blob is padded so the section header table stays 8-byte aligned. Symbol addition is best-effort: on any structural problem the rewrite keeps the (correct) symbol-less object rather than failing.

Applies to every entry-trampoline rewrite. Idempotent: a second pass installs no new stubs and adds no duplicate symbols.

Verified that info dispatches shows hello[stub] and the kernel runs correctly through the trampoline.

@lmoriche lmoriche requested review from chinmaydd and lamb-j as code owners July 9, 2026 05:43
@lmoriche lmoriche requested review from harsh-amd and lancesix July 9, 2026 05:44
@lmoriche lmoriche force-pushed the users/lmoriche/comgr-hotswap-entry-trampoline-stub-symbols branch 2 times, most recently from 9fb235c to e7d306e Compare July 9, 2026 05:56

@lamb-j lamb-j 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.

LGTM, should wait for @chinmaydd and @xintin to take a look though

@chinmaydd chinmaydd left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM, but I'd also like an idempotency test since we claim that as a feature

Idempotent: a second pass installs no new stubs and adds no duplicate symbols

@chinmaydd chinmaydd added hotswap Related to the Comgr Hotswap feature comgr Related to Code Object Manager labels Jul 9, 2026
@lmoriche

lmoriche commented Jul 9, 2026

Copy link
Copy Markdown
Author

LGTM, but I'd also like an idempotency test since we claim that as a feature

Idempotent: a second pass installs no new stubs and adds no duplicate symbols

Done

// Build the appended string names and symbol entries.
SmallVector<uint8_t> StrBlob, SymBlob;
for (const KernelEntryTrampolineFixup &F : Fixups) {
std::string Name = F.KernelName + ".stub";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should we use a complex suffix to further minimize the chance of symbol name collision?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Compiler generated suffixes are part of the Itanium C++ ABI, and generally considered safe as '.' is not a legal character in a C++ mangled name. AFAIK, our compiler does not use ".stub", so Hotswap can use it.

@chinmaydd

chinmaydd commented Jul 9, 2026

Copy link
Copy Markdown

This requires a fix due to updated API for rewriteKernelEntryDescriptorOffsets

// internal.h:815
bool rewriteKernelEntryDescriptorOffsets(
    llvm::WritableMemoryBuffer &OutBuf, uint64_t PoolVAddr,
    llvm::StringRef TargetCpu,                       // <-- added
    llvm::ArrayRef<KernelEntryTrampolineFixup> Fixups);

@lancesix lancesix left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Just minimal comments. No concern from a debugger perspective.

Comment thread amd/comgr/src/comgr-hotswap-elf.cpp Outdated
Comment thread amd/comgr/src/comgr-hotswap-elf.cpp Outdated
lmoriche added 2 commits July 9, 2026 15:40
With entry trampolines installed, a kernel descriptor's entry now points at
the appended stub instead of the kernel body. Tools that resolve a dispatch's
entry address to a name -- e.g. rocgdb `info dispatches` -- then print a bare
address because the stub has no symbol.

Add a `<kernel_name>.stub` STT_FUNC symbol (covering the 256-byte stub) to the
code object's `.symtab` for each appended stub. rocgdb/amd-dbgapi reads the
non-alloc `.symtab` and now reports the dispatch as e.g. `hello[stub]`.

Only the trailing non-alloc `.symtab` / `.strtab` sections grow, so no virtual
addresses, program headers, or relocations change, and `.dynsym` (used by the
HSA loader) is untouched -- the rewritten object still loads and runs
identically. The string blob is padded so the section header table stays
8-byte aligned. Symbol addition is best-effort: on any structural problem the
rewrite keeps the (correct) symbol-less object rather than failing.

Applies to every entry-trampoline rewrite. Idempotent: a second pass installs
no new stubs and adds no duplicate symbols.

Verified that `info dispatches` shows `hello[stub]` and the kernel runs
correctly through the trampoline.
… symbols

Add KernelEntryTrampoline.SecondPassAddsNoDuplicateStubSymbol
(HotswapMCTest.cpp), guaranteeing the idempotency claimed by the stub-symbol
change: a second rewrite pass over an already-trampolined code object installs
no new stub and defines no duplicate `<kernel>.stub` symbol.

The test runs the full first pass on a synthetic gfx1250 object
(appendKernelEntryTrampolines -> growWithTrampolines ->
rewriteKernelEntryDescriptorOffsets -> addKernelEntryTrampolineSymbols) and
asserts exactly one "kernel.stub" symbol. It then re-parses that output and
runs appendKernelEntryTrampolines again; since the descriptor already targets
the appended stub, the second pass reports zero new stubs and empty fixups, so
the symbol pass never runs. Feeding the empty fixups to
addKernelEntryTrampolineSymbols returns nullptr (no new buffer), and
"kernel.stub" remains defined exactly once.

A helper, countSymtabSymbolsNamed, counts .symtab symbols by name.
@lmoriche lmoriche force-pushed the users/lmoriche/comgr-hotswap-entry-trampoline-stub-symbols branch from 2f6d3a3 to 124417e Compare July 9, 2026 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comgr Related to Code Object Manager hotswap Related to the Comgr Hotswap feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants