Skip to content

Defer static init to first API call.#235

Closed
jordan-turbofish wants to merge 8 commits intodevelopfrom
users/jpatters/defer-init
Closed

Defer static init to first API call.#235
jordan-turbofish wants to merge 8 commits intodevelopfrom
users/jpatters/defer-init

Conversation

@jordan-turbofish
Copy link
Copy Markdown
Collaborator

@jordan-turbofish jordan-turbofish commented Mar 24, 2026

This PR moves static initialization of hipFile objects to the first API call. This allows mocking of static objects that call mockable functions, as we can now mock the calls before they are initialized.

Changes:

  • Context has been updated to handle setting the default type for an interface
  • StatsServer has been changed to use an interface
  • Context::get() will return a replacement before static initializing a default, if the replacement exists
  • hipFile static init has been moved to a function that gets called on any API call
  • Added a system test for calling hipFileRead/Write in a new thread
  • HIPFILE_STATIC has been removed, reverting to static
  • Added a concepts check which only occurs if built with C++20
  • Added a C++20 build to CI

@jordan-turbofish jordan-turbofish force-pushed the users/jpatters/defer-init branch 3 times, most recently from f4488fc to 30811a5 Compare March 24, 2026 20:53
@jordan-turbofish jordan-turbofish force-pushed the users/jpatters/defer-init branch 9 times, most recently from 9d0038c to 9f0e017 Compare April 6, 2026 21:03
@jordan-turbofish jordan-turbofish requested a review from Copilot April 6, 2026 21:08
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR shifts hipFile’s AMD backend initialization from a global/static constructor pattern to explicit, first-API-call initialization, and updates stats plumbing to support that change while adding CI coverage for C++20 builds.

Changes:

  • Replace global static initialization with hipFileInit() invoked at the top of public C API entry points.
  • Introduce IStatsServer and update Context<> to allow an interface type to map to a concrete default implementation.
  • Add a C++ standard toggle (17/20) to CMake and CI, plus tests covering the deferred-init behavior.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
test/system/amd/io.cpp Adds a regression test for multi-threaded first-use initialization behavior.
test/amd_detail/mstats.h Updates mock stats server to implement the new IStatsServer interface.
test/amd_detail/fastpath.cpp Adjusts expectations for configuration/stats interactions during IO paths.
test/amd_detail/fallback.cpp Adds mocked stats server usage and expectations in fallback IO tests.
src/amd_detail/stats.h Introduces IStatsServer and makes StatsServer implement it.
src/amd_detail/stats.cpp Routes stats collection through Context<IStatsServer> instead of Context<StatsServer>.
src/amd_detail/hipfile.cpp Calls hipFileInit() in many public API functions to defer initialization.
src/amd_detail/hip.cpp Adjusts symbol lookup helpers (removes HIPFILE_STATIC caching in helper functions).
src/amd_detail/context.h Adds default-implementation mapping for Context<> and replaces HipFileInit class with hipFileInit().
src/amd_detail/context.cpp Implements hipFileInit() and removes the global/static init object.
src/amd_detail/configuration.cpp Removes cached env/symbol checks (previously HIPFILE_STATIC) for some configuration getters.
shared/hipfile-cpp20.h Adds a portability macro for C++20 requires usage.
CMakeLists.txt Adds AIS_CXX_STANDARD cache option (17/20).
cmake/AISAddLibraries.cmake Uses AIS_CXX_STANDARD to set target C++ standard.
cmake/AISAddExecutable.cmake Uses AIS_CXX_STANDARD to set target C++ standard.
.github/workflows/build-ais.yml Adds cxx_standard and generate_coverage inputs; passes standard to CMake.
.github/workflows/ais-ci.yml Adds a dedicated C++20 build/test job.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jordan-turbofish jordan-turbofish force-pushed the users/jpatters/defer-init branch 2 times, most recently from 05c0364 to 839cb5c Compare April 6, 2026 21:54
@jordan-turbofish jordan-turbofish requested a review from Copilot April 6, 2026 21:55
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated 8 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jordan-turbofish jordan-turbofish force-pushed the users/jpatters/defer-init branch 2 times, most recently from 005e409 to bb49eea Compare April 7, 2026 14:34
@jordan-turbofish jordan-turbofish changed the title TESTING: Defer static init to first API call. Defer static init to first API call. Apr 7, 2026
@jordan-turbofish jordan-turbofish force-pushed the users/jpatters/defer-init branch from bb49eea to 3206016 Compare April 7, 2026 15:56
Adds ContextDefaultImpl, which has a type that defaults to the template
type.  Can be specialized to use different default type for a given
template type.
Add an interface for StatsServer, so that a mock doesn't need to call
StatsServer constructor. Adds specialization to ContextDefaultImpl so
that StatsServer is used as default implementation for IStatsServer.
If multiple interfaces are being mocked, the standard implementation
may end up calling mocked functions when constructing. Return the
replacement first, so we only construct the standard implementation when
it is actually needed.
@jordan-turbofish jordan-turbofish force-pushed the users/jpatters/defer-init branch from 3206016 to 90845dd Compare April 7, 2026 19:59
@jordan-turbofish jordan-turbofish marked this pull request as ready for review April 7, 2026 20:08
Copilot AI review requested due to automatic review settings April 7, 2026 20:08
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 172 to 177
ssize_t
hipFileIo(IoType type, hipFileHandle_t fh, const void *buffer_base, size_t size, hoff_t file_offset,
hoff_t buffer_offset, const vector<shared_ptr<Backend>> &backends)
try {
hipFileInit();
auto [file, buffer] = Context<DriverState>::get()->getFileAndBuffer(fh, buffer_base);
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

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

hipFileInit() was moved into hipFileIo(), but hipFileRead()/hipFileWrite() pass getCachedBackends() as a function argument. The order of evaluation of function arguments is not guaranteed, so getCachedBackends() (and the underlying Context<DriverState>::get()->getBackends() static initialization) may run before hipFileInit(). That undermines the goal of deferring static initialization until after mocks are installed and can reintroduce startup/threading issues.

Consider fetching the cached backends inside hipFileIo() after calling hipFileInit() (or otherwise ensure hipFileInit() is sequenced before any context/backend initialization), rather than relying on argument-evaluation order.

Copilot uses AI. Check for mistakes.

StrictMock<MHip> mhip{};
StrictMock<MSys> msys{};
StrictMock<MStatsServer> mserver{};
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.

In the change i'm working on, I've made the stats collections functions a mockable class which removes the need to mock the calls to statsLevel() and getStats()

Moves current static init to a hipFileInit function. It will be called
from any public API function.
Allows use when compiled in C++20 for better type checking, but left out
when compiled in C++17 mode.
@ammallya
Copy link
Copy Markdown
Collaborator

ammallya commented Apr 9, 2026

Imported to ROCm/rocm-systems pull 4864

@ammallya ammallya closed this Apr 9, 2026
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.

4 participants