Skip to content

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

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

LocationKindValues
Program.__init__(code_type)arg"c++", "ptx", "nvvm"
Program.compile(target_type)arg"ptx", "cubin", "ltoir"
Program.backendreturn"NVRTC", "NVVM", "nvJitLink", "driver"
Program.pch_statusreturn"created", "not_attempted", "failed"
Linker.link(target_type)arg"cubin", "ptx"
Linker.backendreturn"nvJitLink", "driver"
ObjectCode.code_typereturn"cubin", "ptx", "ltoir", "fatbin", "object", "library"

Memory

LocationKindValues
ManagedMemoryResourceOptions.preferred_location_typearg"device", "host", "host_numa"
ManagedMemoryResource.preferred_locationreturn (tuple[0])"device", "host", "host_numa"
VirtualMemoryResourceOptions.allocation_typearg"pinned", "managed"
VirtualMemoryResourceOptions.location_typearg"device", "host", "host_numa", "host_numa_current"
VirtualMemoryResourceOptions.handle_typearg"posix_fd", "generic", "win32_kmt", "fabric"
VirtualMemoryResourceOptions.granularityarg"recommended", "minimum"
VirtualMemoryResourceOptions.self_access / peer_accessarg"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

LocationKindValues
GraphAllocOptions.memory_typearg"device", "host", "managed"
AllocNode.memory_typereturn"device", "host", "managed"
ConditionalNode.cond_typereturn"if", "while", "switch"

Proposed enum groupings

Enum nameValuesConsumers
CodeTypecubin, ptx, ltoir, fatbin, object, libraryObjectCode.code_type, Linker.link, Program.compile
SourceTypec++, ptx, nvvmProgram.__init__
CompilerBackendNVRTC, NVVM, nvJitLink, driverProgram.backend, Linker.backend
PchStatuscreated, not_attempted, failedProgram.pch_status
MemoryTypedevice, host, managedGraphAllocOptions, AllocNode, ManagedMemoryResource
LocationTypedevice, host, host_numa, host_numa_currentVirtualMemoryResourceOptions, ManagedMemoryResourceOptions
ConditionalTypeif_, while_, switch_ConditionalNode.cond_type
HandleTypeposix_fd, generic, win32_kmt, fabricVirtualMemoryResourceOptions
Granularityrecommended, minimumVirtualMemoryResourceOptions
AccessModerw, rVirtualMemoryResourceOptions

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