Skip to content

cuda.core: Replace string literals with enums in public API #1995

@mdboom

Description

@mdboom

This is just a result of a sweep for using strings where enums could be used. This is just to aid the discussion about whether they should be addressed.

Several places in the cuda.core public API accept or return string values chosen from a small, fixed vocabulary. These are candidates for replacement with proper Python enum types, which would improve discoverability, IDE autocompletion, isinstance checking, and catch typos at the call site rather than at runtime.


Findings

Compilation / Linking pipeline

Location Kind Values
Program.__init__(code_type) arg "c++", "ptx", "nvvm"
Program.compile(target_type) arg "ptx", "cubin", "ltoir"
Program.backend return "NVRTC", "NVVM", "nvJitLink", "driver"
Program.pch_status return "created", "not_attempted", "failed"
Linker.link(target_type) arg "cubin", "ptx"
Linker.backend return "nvJitLink", "driver"
ObjectCode.code_type return "cubin", "ptx", "ltoir", "fatbin", "object", "library"

Memory

Location Kind Values
ManagedMemoryResourceOptions.preferred_location_type arg "device", "host", "host_numa"
ManagedMemoryResource.preferred_location return (tuple[0]) "device", "host", "host_numa"
VirtualMemoryResourceOptions.allocation_type arg "pinned", "managed"
VirtualMemoryResourceOptions.location_type arg "device", "host", "host_numa", "host_numa_current"
VirtualMemoryResourceOptions.handle_type arg "posix_fd", "generic", "win32_kmt", "fabric"
VirtualMemoryResourceOptions.granularity arg "recommended", "minimum"
VirtualMemoryResourceOptions.self_access / peer_access arg "rw", "r"

Note: the VirtualMemoryResourceOptions fields already use Literal[...] type hints, which partially addresses the issue, but proper enums would still improve iteration, isinstance checks, and user discoverability.

Graph API

Location Kind Values
GraphAllocOptions.memory_type arg "device", "host", "managed"
AllocNode.memory_type return "device", "host", "managed"
ConditionalNode.cond_type return "if", "while", "switch"

Proposed enum groupings

Enum name Values Consumers
CodeType cubin, ptx, ltoir, fatbin, object, library ObjectCode.code_type, Linker.link, Program.compile
SourceType c++, ptx, nvvm Program.__init__
CompilerBackend NVRTC, NVVM, nvJitLink, driver Program.backend, Linker.backend
PchStatus created, not_attempted, failed Program.pch_status
MemoryType device, host, managed GraphAllocOptions, AllocNode, ManagedMemoryResource
LocationType device, host, host_numa, host_numa_current VirtualMemoryResourceOptions, ManagedMemoryResourceOptions
ConditionalType if_, while_, switch_ ConditionalNode.cond_type
HandleType posix_fd, generic, win32_kmt, fabric VirtualMemoryResourceOptions
Granularity recommended, minimum VirtualMemoryResourceOptions
AccessMode rw, r VirtualMemoryResourceOptions

Discussion points

  • The compilation/linking pipeline (Program, Linker, ObjectCode) is the highest-impact area since string values flow across multiple API boundaries.
  • Switching from str to enum would be a breaking change for existing callers. One migration path is to accept both str and the enum type during a deprecation window.
  • Some values ("c++", "if", "while") are not valid Python identifiers and would need renaming (e.g. SourceType.CPP, ConditionalType.IF).

Metadata

Metadata

Assignees

Labels

P0High priority - Must do!RFCPlans and announcementscuda.coreEverything related to the cuda.core moduleenhancementAny code-related improvements

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions