Skip to content

Added support for MCDM and GPU-PV through the DXCore API - #72

Open
lihaoyun6 wants to merge 3 commits into
Comfy-Org:masterfrom
lihaoyun6:master
Open

Added support for MCDM and GPU-PV through the DXCore API#72
lihaoyun6 wants to merge 3 commits into
Comfy-Org:masterfrom
lihaoyun6:master

Conversation

@lihaoyun6

@lihaoyun6 lihaoyun6 commented Jul 8, 2026

Copy link
Copy Markdown

Contribution Agreement

  • I agree that my contributions are licensed under the GPLv3.
  • I grant Comfy Org the rights to relicense these contributions as outlined in CONTRIBUTING.md.

By introducing Microsoft's DXCore API, more modern computing device models can be supported, such as MCDM and GPU-PV.

After applying this patch, I can now load aimdo on my A100 (running in MCDM mode) and enable new features such as dynamic vram and multi-threaded loading.

DXCore can only be called via C++. After applying this patch, this project will depend on the MSVC++ runtime!
Maybe we can use the /MT compiler parameter to avoid this, and dxcore.lib also needs to be added to the compilation command in workflow:

cl.exe /LD /MT /O2 ^
  src/*.c src-cuda/dispatch.c src-win/*.c src-win/shmem-detect.cpp ^
  /Isrc /Isrc-win /IC:/Detours/include /FIcompiler.h /Fe:comfy_aimdo/aimdo.dll ^
  /link /LIBPATH:%DETOURS_LIB_DIR% ^
  dxgi.lib dxguid.lib detours.lib onecore.lib dxcore.lib
cl.exe /LD /MT /O2 /D__HIP_PLATFORM_AMD__ ^
  src/*.c src-hip/dispatch.c src-win/*.c src-win/shmem-detect.cpp ^
  /Isrc /Isrc-win /IC:/Detours/include /FIcompiler.h /Fe:comfy_aimdo/aimdo_rocm.dll ^
  /link /LIBPATH:%DETOURS_LIB_DIR% ^
  dxgi.lib dxguid.lib detours.lib onecore.lib dxcore.lib

@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

🎉 Thank you for your contribution, we really appreciate it! 🎉

Like many open source projects, we require contributors to sign our Contributor License Agreement (CLA). A CLA makes the ownership of contributions explicit, so contributors and the project share a clear understanding of how the code can be used. By signing, you:

  • Confirm that you own your contribution.
  • Keep the right to reuse your own code.
  • Grant us a copyright license to include and share it within our projects.

CLAs are standard practice across major open source projects including those under the Apache Software Foundation and the Linux Foundation. Ours is based on the Apache Software Foundation's CLA. Most importantly, it would enable us to relicense the project under a more permissive license in the future, giving the project and its community greater flexibility.

To sign, please post a new comment on this PR with exactly the following text:


I have read and agree to the Contributor License Agreement


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@lihaoyun6, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 46 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: d33e3a83-47cf-4f7f-952d-b51a09e6c7cd

📥 Commits

Reviewing files that changed from the base of the PR and between a88c675 and 3fd1b1b.

📒 Files selected for processing (1)
  • src-win/shmem-detect.c
📝 Walkthrough

Walkthrough

Adds C-compatible DXCore interfaces and integrates DXCore adapter matching, VRAM budget polling, fallback WDDM queries, re-initialization handling, and cleanup into Windows shared-memory detection. The plat.h declaration is repositioned without changing its signature.

Changes

DXCore VRAM budget detection

Layer / File(s) Summary
DXCore C interface contract
src-win/dxcore_c.h
Defines DXCore enums, budget structures, COM-style interfaces, GUIDs, and the adapter factory declaration.
DXCore adapter discovery and initialization
src-win/shmem-detect.c, src/plat.h
Tracks DXCore adapters, matches CUDA device LUIDs, releases stale handles during re-initialization, and repositions the unchanged aimdo_analyze declaration.
Budget polling and adapter cleanup
src-win/shmem-detect.c
Uses DXCore local-segment budgets when available, falls back to WDDM budget queries, records the selected method, and releases the DXCore adapter during cleanup.

Sequence Diagram(s)

sequenceDiagram
  participant CUDADevice
  participant aimdo_wddm_init
  participant DXCoreFactory
  participant DXCoreAdapter
  participant poll_budget_deficit
  CUDADevice->>aimdo_wddm_init: provide InstanceLuid
  aimdo_wddm_init->>DXCoreFactory: enumerate adapters
  DXCoreFactory-->>aimdo_wddm_init: adapter list
  aimdo_wddm_init->>DXCoreAdapter: match InstanceLuid
  DXCoreAdapter-->>aimdo_wddm_init: matched adapter
  aimdo_wddm_init-->>CUDADevice: initialization success
  CUDADevice->>poll_budget_deficit: poll memory budget
  poll_budget_deficit->>DXCoreAdapter: QueryState local segment budget
  DXCoreAdapter-->>poll_budget_deficit: budget and usage
Loading
🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
✨ Simplify code
  • Create PR with simplified code

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@lihaoyun6

Copy link
Copy Markdown
Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown
✅ Action performed

Full review finished.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src-win/shmem-detect.cpp`:
- Around line 55-61: The initialization of `AimdoHipDeviceProp` in
`shmem-detect.cpp` only zeroes the first union member, so `hip_props.bytes` may
contain uninitialized data before `g_device_get_properties` fills it. Update the
`__HIP_PLATFORM_AMD__` handling around `hip_props` to zero the entire union
storage before use (or otherwise ensure the full `bytes` buffer is initialized),
then keep using `g_device_get_properties` and `hip_props.luid` as the source for
the LUID copy. This should be fixed in the same block where `CHECK_CU` and
`memcpy` are used.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 3c535a26-889f-4835-a887-bc010fe76535

📥 Commits

Reviewing files that changed from the base of the PR and between ce3c59e and 5dc1cfc.

📒 Files selected for processing (3)
  • src-win/shmem-detect.c
  • src-win/shmem-detect.cpp
  • src/plat.h
💤 Files with no reviewable changes (1)
  • src-win/shmem-detect.c

Comment thread src-win/shmem-detect.cpp Outdated
Comment on lines +55 to +61
#if defined(__HIP_PLATFORM_AMD__)
AimdoHipDeviceProp hip_props = {0};
if (!g_device_get_properties || !CHECK_CU(g_device_get_properties(hip_props.bytes, dev))) {
log(ERROR, "comfy-aimdo: failed to get HIP device properties\n");
return false;
}
memcpy(&cuda_luid, hip_props.luid, sizeof(cuda_luid));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Zero-init the whole union with memset, or don't trust bytes beyond the first member.

AimdoHipDeviceProp hip_props = {0}; only initializes the first union member (the ~280-byte struct); the remaining storage up to bytes[4096] isn't guaranteed zeroed. You then hand hip_props.bytes to g_device_get_properties and read hip_props.luid back. If the property call doesn't populate the LUID region fully, you'd match against uninitialized memory. Cheap fix, and it hushes the Cppcheck UnionZeroInit warning too.

🛡️ Proposed fix
-    AimdoHipDeviceProp hip_props = {0};
+    AimdoHipDeviceProp hip_props;
+    memset(&hip_props, 0, sizeof(hip_props));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#if defined(__HIP_PLATFORM_AMD__)
AimdoHipDeviceProp hip_props = {0};
if (!g_device_get_properties || !CHECK_CU(g_device_get_properties(hip_props.bytes, dev))) {
log(ERROR, "comfy-aimdo: failed to get HIP device properties\n");
return false;
}
memcpy(&cuda_luid, hip_props.luid, sizeof(cuda_luid));
`#if` defined(__HIP_PLATFORM_AMD__)
AimdoHipDeviceProp hip_props;
memset(&hip_props, 0, sizeof(hip_props));
if (!g_device_get_properties || !CHECK_CU(g_device_get_properties(hip_props.bytes, dev))) {
log(ERROR, "comfy-aimdo: failed to get HIP device properties\n");
return false;
}
memcpy(&cuda_luid, hip_props.luid, sizeof(cuda_luid));
🧰 Tools
🪛 Cppcheck (2.21.0)

[portability] 56-56: Zero initializing union 'hip_props' does not guarantee its complete storage to be zero initialized as its largest member is not declared as the first member. Consider making bytes the first member or favor memset().

(UnionZeroInit)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src-win/shmem-detect.cpp` around lines 55 - 61, The initialization of
`AimdoHipDeviceProp` in `shmem-detect.cpp` only zeroes the first union member,
so `hip_props.bytes` may contain uninitialized data before
`g_device_get_properties` fills it. Update the `__HIP_PLATFORM_AMD__` handling
around `hip_props` to zero the entire union storage before use (or otherwise
ensure the full `bytes` buffer is initialized), then keep using
`g_device_get_properties` and `hip_props.luid` as the source for the LUID copy.
This should be fixed in the same block where `CHECK_CU` and `memcpy` are used.

Source: Linters/SAST tools

@rattus128 rattus128 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.

Please keep it in pure C.

@lihaoyun6

lihaoyun6 commented Jul 11, 2026

Copy link
Copy Markdown
Author

Please keep it in pure C.

That's a really strict tandard. 😂
Okay, I implemented it by using a custom C-style header file.
And we still need to modify the compilation parameters in the workflow to add dxcore.lib

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src-win/shmem-detect.c`:
- Around line 79-91: Update the DXCore match path in the adapter discovery logic
so it does not return before initializing g_wddm_adapter; continue into
DXGI/WDDM discovery after a DXCore match. In the fallback condition around the
DXCore budget query and WDDM setup, attempt WDDM whenever the DXCore query is
unsuccessful, including when g_dxcore_adapter is already non-null, rather than
gating it solely on the pointer being null.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: e5a408e7-87e1-465e-970e-44cb7485986a

📥 Commits

Reviewing files that changed from the base of the PR and between 5dc1cfc and a88c675.

📒 Files selected for processing (3)
  • src-win/dxcore_c.h
  • src-win/shmem-detect.c
  • src/plat.h
💤 Files with no reviewable changes (1)
  • src/plat.h

Comment thread src-win/shmem-detect.c Outdated
@lihaoyun6
lihaoyun6 requested a review from rattus128 July 11, 2026 14:23
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.

2 participants