-
Notifications
You must be signed in to change notification settings - Fork 316
Enable building and running on macOS #3711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
taalexander
wants to merge
58
commits into
NVIDIA:main
Choose a base branch
from
taalexander:osx-cuda-quantum-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Collaborator
Command Bot: Processing... |
Collaborator
Command Bot: Processing... |
Collaborator
Command Bot: Processing... |
Collaborator
Command Bot: Processing... |
bettinaheim
reviewed
Jan 5, 2026
Collaborator
Author
|
/ok to test f0b2fd3 |
|
CUDA Quantum Docs Bot: A preview of the documentation can be found here. |
Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
- Fix POSIX environ issue with OSX - Fix Stim symbol exporting with OSX Signed-off-by: Thomas Alexander <[email protected]>
- Fix Zlib/minizip issue with OSX and brew packages Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
…including osx/linux) by injecting through CMAKE instead of doing the dynamic lookup in the cpp program. Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
…ith local diff. Signed-off-by: Thomas Alexander <[email protected]>
… linking of system libraries. Signed-off-by: Thomas Alexander <[email protected]>
…rary. Update OSX installation scripts/instructions to simplify process. Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
…eeded by forcing pre req install always. Signed-off-by: Thomas Alexander <[email protected]>
…ylib is being used which is currently required for OSX. The stack of hacks is quickly growing though and we might consider dropping dylibs and going back to static linking. Signed-off-by: Thomas Alexander <[email protected]>
…rrounds local libraries (to avoid sudo requirements given this is not in a docker). Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
Ultimately this won't work on LLVM 16 with the python libraries without significant patching due to llvm/llvm-project#123781 not being available. See docs/OSX_BUILD.md for more information. Signed-off-by: Thomas Alexander <[email protected]>
- Update CMake configuration to use static LLVM/MLIR libraries instead of dylib. - Add -Wl,-flat_namespace linker flag for macOS symbol resolution. - Fix use-after-free bug in LoopUnrollPatterns.inc when allow-early-exit is enabled. - Replace bash-only |& syntax with POSIX-compatible 2>&1 | in test files. - Remove mlir_execution_engine_shared.diff patch (no longer needed). - Update build_llvm.sh script for static library build. - Fix force loading to be a bit more careful about where used with LLVMCodeGen Signed-off-by: Thomas Alexander <[email protected]>
…l due to two-level namespacing in OSX. Bugs uncovered on OSX: - ConvertStmt.cpp: Fix use-after-free by saving operands before erasing call. - CombineMeasurements.cpp: Return success() after erase/replace ops for rewriter/ - RegToMem.cpp: Return WalkResult::skip() after erasing ops during walk. - ArgumentConversion.cpp: Fix lambda capture by value instead of reference. Updating tests for OSX: - Fix regex patterns for OSX ABI. - A few floating point regexes - Mark one of the KAK tests as unsupported on OSX as the values are different due to a different found decomposition. We could have used the OSX values but it seemed lik e overkill. Signed-off-by: Thomas Alexander <[email protected]>
…ers caused by canonicalization. Signed-off-by: Thomas Alexander <[email protected]>
…aries on OSX. Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
…-mlir-runtime to reduce symbol visibility issues with two-level namespace - Make LLVM/MLIR include directories PUBLIC so dependent targets can still compile We are still using flat namespace but I'm hopefully we can extend this fix to remove it. Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
- Add macOS setup instructions to Dev_Setup.md - Add macOS-specific build notes to Building.md - Create requirements-dev.txt with pinned versions for dev dependencies - Update devdeps.Dockerfile to use requirements-dev.txt - Update install_toolchain.sh to use Apple Clang on macOS - Remove standalone docs/OSX_BUILD.md (content integrated into existing docs) Signed-off-by: Thomas Alexander <[email protected]>
…. Add new requirements-dev.txt. Add new library target for utilities to work around issues with link order. Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
- Make openssl build use cmake to avoid package config resolution issues with flat namespace static linking - Force linking of cudaq common to use two-level namespace to prevent collisions/bugs with flat namespace and open ssl. Signed-off-by: Thomas Alexander <[email protected]>
- Split qvector_init_from_vector.cpp into two tests with one failing on OSX. This is a result of the OSX stack size and should be resolved in a future PR/issue. This can be viewed as a limitation of the OSX support currently. Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
Signed-off-by: Thomas Alexander <[email protected]>
…perators. DCO Remediation Commit for Thomas Alexander <[email protected]> I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 8c03cca I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: e0f9bb0 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 30d3fb7 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 749cc13 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 5888a8c I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 22bdf22 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 5be224e I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: b48ecdd I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 3e4bf6c I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 102db1b I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 61c2802 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 74f8fb5 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 5ad3862 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: f96c0e8 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 7ed3d44 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 4725e36 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 18961f0 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 45a574d I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: d3fb829 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 7a62b91 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 7a81210 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 428d133 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 306eb5d I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 6857cb0 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: dba1b56 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: deb451b I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 918e12c I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 51ff943 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: a815037 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 8b98bb1 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 79eab8c I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: a806c58 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: e5ef650 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 7bbdf85 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 925e1cd I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: e2f9aba I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 2883df6 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: fb5f997 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 8797349 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: acdfd87 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: ac113d3 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 758057b I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 4c81ab8 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: a9a93d0 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: b616592 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 9091b9f I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: ce7caee I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: d9bff6b I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 52b62ca I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 56b482c I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 2a3cc57 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 2570675 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: f0b2fd3 I, Thomas Alexander <[email protected]>, hereby add my Signed-off-by to this commit: 9e7f5f2 Signed-off-by: Thomas Alexander <[email protected]>
52c8afd to
8241423
Compare
Collaborator
Author
Command Bot: Processing... |
|
CUDA Quantum Docs Bot: A preview of the documentation can be found here. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds macOS support for CUDA-Q, addressing platform-specific differences in linking, symbol visibility, shell compatibility, and library handling. The changes should enable building and running CUDA-Q on macOS with Apple Silicon (arm64) and Intel (x86_64) architectures but has currently only been tested on x86.
This is a large PR and full Python support also requires Python wheels and CI enablement. I have structured this PR such that it should not impact existing Linux builds. I recommend we treat this as phase one and merge after review/passing CI. We will then follow up with Python wheel and CI PRs to complete support.
PRs:
I've tried my best to summarize the contents of the PR below:
1. Build System (CMake)
Platform Detection & Configuration
xcrun --show-sdk-pathfor C++ stdlib headers--no-as-neededfor Linux, alternatives for macOS)CUDAQ_LIBCXX_PATHandCUDAQ_SYSROOT_PATHconfiguration for cross-platform header discovery@executable_pathon macOS vs$ORIGINon LinuxCMAKE_INSTALL_RPATHto use semicolon separators on macOSLibrary Linking Changes
cudaq-mlir-runtimeto reduce symbol visibility issues due to two-level namespace.dylibon macOS vs.soon Linux--start-groupnot available on Apple ld)cudaq-commonto prevent symbol collisions with OpenSSLNew
cudaq-utilsLibraryCreated a new low-level utilities library which resolves a circular dependency where
cudaq-operatorneedscomplex_matrixfunctions but is built beforelibcudaq2. Two-Level Namespace Workarounds
macOS uses two-level namespace linking by default, where symbols are bound to specific libraries. This causes issues with LLVM/MLIR's static initializer pattern (PassRegistry, TargetRegistry, cl::Options).
Workarounds Implemented
flat_namespacelinker flagCMakeLists.txtforce_loadfor LLVM CodeGencmake/BuildHelpers.cmakeadd_lib_loading_macos_workaroundcmake/BuildHelpers.cmakelib/Support/Config/CMakeLists.txtCUDAQTargetConfigUtilto prevent symbol collisionsInitializeNativeTargetCUDAQuantumExtension.cppexecution_manager.hFuture Removal Pathway
The targeted fixes (PRIVATE linking, force_load, unexport lists) should eventually allow removing the global
flat_namespaceflag. To test removal:add_link_options("-Wl,-flat_namespace")inCMakeLists.txtforce_loadHowever, there is an immediate blocker with cudaq-mlir-runtime and cudaq libraries both containing pass registries and both containing passes. To resolve this would likely require centralizing these registries would likely require larger library restructuring/registry development and I consider this work for a follow up PR.
3. Platform Portability Fixes
Type Size Differences
unsigned longis 8 bytes on macOS/arm64 but 4 bytes on some Linux systems so we switch usages to std::uint64_tCCTypes.cppto use explicit size types where neededLibrary Path Handling
TargetConfig.cpp: Handle.dylibvs.soextensionsfixup-linkage.cpp: Added handling fordefine weaklinkage (macOS clang emits some functions with weak linkage)Shell Compatibility (POSIX)
Replaced bash commands with POSIX-compatible alternatives:
|&→2>&1 |for stderr pipingmktemptemplate usage (macOS requiresXXXXXXsuffix)Standard Library Differences
std::vector<bool>has different internal layout between libc++ and libstdc++extern char **environdeclaration inMQPUUtils.cpp(POSIX requires explicit declaration on macOS)Other
4. Third-Party Patches
Added patches in
tpls/customizations/for compatibility:llvm/idempotent_option_category.diffcl::OptionCategoryregistration idempotent to handle multiple LLVM copies registering the same category (avoids assertion failures)pybind11/pybind11Common.cmake.diff-flto=flag generation for ClangXtensor xio.hpp Workaround
#include <xtensor/xio.hpp>inmolecule.cppand replaced with manual printingrebind_container(LLVM #91504)5. (Pre-existing) Bug Fixes
Note most of these would have been caught by static code analysis. The majority of the bugs were likely a result of more aggressive allocator on OSX.
RegToMem.cppWalkResultreturn afterop->erase()to prevent iterator invalidationLoopUnrollPatterns.incResetBeforeReuse.cppCombineMeasurements.cpp6. Python Bindings
#ifdef __APPLE__conditional forInitializeNativeTargetcallsadd_lib_loading_macos_workaroundfor Python extension targets7. Documentation & Developer Setup
Dev_Setup.mdfor developer environment setuprequirements-dev.txtfor Python development dependenciesBuilding.mdwith platform-specific notesscripts/install_toolchain.shandscripts/install_prerequisites.shfor macOS build instructionsscripts/build_cudaq.shwith improved macOS build support8. Test Updates
RUNlines to use platform-appropriate flags (calling_convention.cpp,infinite_loop.cpp, kernel exec transform tests).sowith%cudaq_plugin_extsubstitution for cross-platform testsDISCOVERY_TIMEOUT 120to backend unit tests (likely just required for my slow machine)qvector_init_from_vector.cppto separate large array test (qvector_init_large_array.cpp) which is skipped on macOS due to stack sizecudaq-qpudlinking to enforce two-level namespace for braket backend testsKnown Limitations
Stack Size
macOS has a smaller default stack size (8MB) compared to Linux. Some tests with large stack allocations (e.g., large array initializations) may fail. The
qvector_init_large_array.cpptest is currently skipped on macOS for this reason. Future work may address this viaulimit -sor code refactoring.flat_namespace
The
flat_namespacelinker flag can cause symbol collisions with system libraries. We should work toward removing this in a follow-up PR.LLVM cl::OptionCategory Duplicate Registration (Requires LLVM Patch)
Both
libcudaqandcudaq-mlir-runtimelink LLVM and therefore each contain their own copy of LLVM'scl::OptionCategorystatic globals. When both libraries are loaded, LLVM's default behavior asserts on duplicate category names.The
idempotent_option_category.diffpatch makes registration idempotent, allowing the same category to be registered multiple times without assertion failures. This is a workaround—the proper fix would be to restructure the libraries so only one contains LLVM command-line infrastructure, but that requires more significant refactoring.Testing
All tests passing on x86 with
ctest --output-on-failure