Skip to content

Commit b04dd44

Browse files
authored
[Refactor][NFC] Vendor-in compiler_lock for future CUDA-specific changes (#565)
This change vendors in the global_compiler_lock that is used to protect the core compiler machinery for future CUDA-specific changes.
1 parent 5aa6b2e commit b04dd44

File tree

6 files changed

+90
-5
lines changed

6 files changed

+90
-5
lines changed

numba_cuda/numba/cuda/compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from numba.cuda.core import bytecode
1111
from numba.cuda import types
1212
from numba.cuda.core.options import ParallelOptions
13-
from numba.core.compiler_lock import global_compiler_lock
13+
from numba.cuda.core.compiler_lock import global_compiler_lock
1414
from numba.cuda.core.errors import NumbaWarning, NumbaInvalidConfigWarning
1515
from numba.cuda.core.interpreter import Interpreter
1616

numba_cuda/numba/cuda/core/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from numba.cuda.core import imputils, targetconfig, funcdesc
1616
from numba.cuda import cgutils, debuginfo, types, utils, datamodel, config
1717
from numba.cuda.core import errors
18-
from numba.core.compiler_lock import global_compiler_lock
18+
from numba.cuda.core.compiler_lock import global_compiler_lock
1919
from numba.cuda.core.pythonapi import PythonAPI
2020
from numba.cuda.core.imputils import (
2121
user_function,
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: BSD-2-Clause
3+
4+
import threading
5+
import functools
6+
import numba.cuda.core.event as ev
7+
from numba.cuda import HAS_NUMBA
8+
9+
if HAS_NUMBA:
10+
from numba.core.compiler_lock import (
11+
global_compiler_lock as _numba_compiler_lock,
12+
)
13+
else:
14+
_numba_compiler_lock = None
15+
16+
17+
# Lock for the preventing multiple compiler execution
18+
class _CompilerLock(object):
19+
def __init__(self):
20+
self._lock = threading.RLock()
21+
22+
def acquire(self):
23+
ev.start_event("numba-cuda:compiler_lock")
24+
self._lock.acquire()
25+
26+
def release(self):
27+
self._lock.release()
28+
ev.end_event("numba-cuda:compiler_lock")
29+
30+
def __enter__(self):
31+
self.acquire()
32+
33+
def __exit__(self, exc_val, exc_type, traceback):
34+
self.release()
35+
36+
def __call__(self, func):
37+
@functools.wraps(func)
38+
def _acquire_compile_lock(*args, **kwargs):
39+
with self:
40+
return func(*args, **kwargs)
41+
42+
return _acquire_compile_lock
43+
44+
45+
_numba_cuda_compiler_lock = _CompilerLock()
46+
47+
48+
# Wrapper that coordinates both numba and numba-cuda compiler locks
49+
class _DualCompilerLock(object):
50+
"""Wrapper that coordinates both the numba-cuda and upstream numba compiler locks."""
51+
52+
def __init__(self, cuda_lock, numba_lock):
53+
self._cuda_lock = cuda_lock
54+
self._numba_lock = numba_lock
55+
56+
def acquire(self):
57+
self._numba_lock.acquire()
58+
self._cuda_lock.acquire()
59+
60+
def release(self):
61+
self._cuda_lock.release()
62+
self._numba_lock.release()
63+
64+
def __enter__(self):
65+
self.acquire()
66+
67+
def __exit__(self, exc_val, exc_type, traceback):
68+
self.release()
69+
70+
def __call__(self, func):
71+
@functools.wraps(func)
72+
def _acquire_compile_lock(*args, **kwargs):
73+
with self:
74+
return func(*args, **kwargs)
75+
76+
return _acquire_compile_lock
77+
78+
79+
# Create the global compiler lock, wrapping both locks if numba is available
80+
if HAS_NUMBA:
81+
global_compiler_lock = _DualCompilerLock(
82+
_numba_cuda_compiler_lock, _numba_compiler_lock
83+
)
84+
else:
85+
global_compiler_lock = _numba_cuda_compiler_lock

numba_cuda/numba/cuda/core/compiler_machinery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import inspect
88

99

10-
from numba.core.compiler_lock import global_compiler_lock
1110
from numba.cuda.core import errors
11+
from numba.cuda.core.compiler_lock import global_compiler_lock
1212
from numba.cuda.core import config
1313
from numba.cuda import utils
1414
from numba.cuda.core import transforms

numba_cuda/numba/cuda/dispatcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from numba.cuda import serialize, utils
2020
from numba import cuda
2121

22-
from numba.core.compiler_lock import global_compiler_lock
22+
from numba.cuda.core.compiler_lock import global_compiler_lock
2323
from numba.cuda.typeconv.rules import default_type_manager
2424
from numba.cuda.typing.templates import fold_arguments
2525
from numba.cuda.typing.typeof import Purpose, typeof

numba_cuda/numba/cuda/target.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from numba.cuda import types
1212
from numba.cuda import HAS_NUMBA
13-
from numba.core.compiler_lock import global_compiler_lock
13+
from numba.cuda.core.compiler_lock import global_compiler_lock
1414
from numba.cuda.core.errors import NumbaWarning
1515
from numba.cuda.core.base import BaseContext
1616
from numba.cuda.typing import cmathdecl

0 commit comments

Comments
 (0)