Commit e08e89e
committed
feat(core): Program.compile(cache=...) convenience wrapper
Adds a ``cache=`` keyword to :meth:`cuda.core.Program.compile` that
threads the persistent cache machinery into the high-level compile
path. With ``cache=None`` (the default) the call is byte-identical
to the un-cached path -- no key derivation, no extra import, no
behavior change.
When a cache is provided, the wrapper derives a key via
:func:`~cuda.core.utils.make_program_cache_key` from the program's
source, options, and target type; checks the cache; on hit,
returns a fresh
``ObjectCode._init(hit_bytes, target_type, name=self._options.name)``;
on miss, runs the underlying compile and stores
``cache[key] = compiled`` (the cache extracts ``bytes(obj.code)``).
Two compile-time guards close obvious footguns:
* ``name_expressions`` plus ``cache=`` raises ``ValueError``.
NVRTC populates ``ObjectCode.symbol_mapping`` from name-expression
mangling at compile time, and that mapping isn't carried in the
binary the cache stores. Without this guard the first call (miss)
would return an ObjectCode with mappings populated, while every
subsequent call (hit) would return one without -- silently
breaking later ``get_kernel(name_expression)`` lookups that work
on the uncached path. Compiles that need name_expressions should
run without ``cache=``, or look up mangled symbols by hand from
the cached ``ObjectCode``.
* Inputs whose compilation effect isn't captured by the key
(``include_path``, ``pre_include``, ``pch``, ``use_pch``,
``pch_dir``, NVVM ``use_libdevice=True``, NVRTC ``options.name``
with a directory component, side-effect options like
``create_pch`` / ``time`` / ``fdevice_time_trace``) propagate the
``ValueError`` from ``make_program_cache_key`` -- those callers
should use ``make_program_cache_key`` directly with an
``extra_digest`` covering the external content.
Supporting refactors:
* Unify ``Program``'s source retention into a single ``_code``
field (was split between ``_code`` for NVVM and a separate
``_source`` for c++/ptx). ``_code`` is now always bytes; the
cache wrapper decodes back to ``str`` for c++/ptx before passing
to ``make_program_cache_key`` (which only accepts bytes for NVVM).
* Move the actual compile call into a module-level
``_program_compile_uncached`` so tests can monkeypatch the seam
without going through NVRTC. ``Program`` is a ``cdef class``, so
its methods cannot be reassigned from Python -- the seam has to
live outside the class.
* The unified ``_code`` field also exposed a pre-existing bug on
the NVVM path: the C pointer was being recomputed from the
caller's original ``code`` argument rather than from
``self._code``, which crashed for ``bytearray`` inputs that the
field's bytes coercion handled cleanly. Fixed; regression test
added in ``test_program.py``.
Tests in ``test_program_compile_cache.py`` cover both halves of the
contract: the wrapper-level miss/hit/error paths against a recording
stub (verifying it's duck-typed and doesn't require subclassing
``ProgramCacheResource``), the rejection paths (name_expressions,
extra_digest-required options, side-effect options, NVRTC
``options.name`` with a directory component), and a real NVRTC
end-to-end roundtrip using ``FileStreamProgramCache`` across reopen
so the bytes match across processes.1 parent 88b472f commit e08e89e
4 files changed
Lines changed: 429 additions & 8 deletions
File tree
- cuda_core
- cuda/core
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
| 21 | + | |
21 | 22 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | | - | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
89 | 94 | | |
90 | 95 | | |
91 | 96 | | |
| |||
98 | 103 | | |
99 | 104 | | |
100 | 105 | | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
101 | 126 | | |
102 | 127 | | |
103 | 128 | | |
104 | 129 | | |
105 | 130 | | |
106 | 131 | | |
107 | | - | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
108 | 182 | | |
109 | 183 | | |
110 | 184 | | |
| |||
503 | 577 | | |
504 | 578 | | |
505 | 579 | | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
506 | 593 | | |
507 | 594 | | |
508 | 595 | | |
| |||
618 | 705 | | |
619 | 706 | | |
620 | 707 | | |
| 708 | + | |
621 | 709 | | |
622 | 710 | | |
623 | 711 | | |
| |||
638 | 726 | | |
639 | 727 | | |
640 | 728 | | |
641 | | - | |
| 729 | + | |
642 | 730 | | |
643 | 731 | | |
644 | 732 | | |
645 | 733 | | |
646 | 734 | | |
647 | 735 | | |
648 | 736 | | |
| 737 | + | |
| 738 | + | |
649 | 739 | | |
650 | | - | |
| 740 | + | |
651 | 741 | | |
652 | 742 | | |
653 | 743 | | |
| |||
657 | 747 | | |
658 | 748 | | |
659 | 749 | | |
| 750 | + | |
660 | 751 | | |
661 | | - | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
662 | 755 | | |
663 | | - | |
| 756 | + | |
664 | 757 | | |
665 | 758 | | |
666 | 759 | | |
| |||
832 | 925 | | |
833 | 926 | | |
834 | 927 | | |
835 | | - | |
| 928 | + | |
836 | 929 | | |
837 | 930 | | |
838 | 931 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
434 | 434 | | |
435 | 435 | | |
436 | 436 | | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
437 | 454 | | |
438 | 455 | | |
439 | 456 | | |
| |||
0 commit comments