Skip to content

Commit 395a996

Browse files
committed
Prevent re-enabling of pass timings
1 parent 86255fe commit 395a996

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

llvmlite/binding/newpassmanagers.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def __init__(self, tm, pto):
222222
super().__init__(ffi.lib.LLVMPY_CreatePassBuilder(tm, pto))
223223
self._pto = pto
224224
self._tm = tm
225-
self._time_passes_handler = TimePassesHandler()
225+
self._time_passes_handler = None
226226

227227
def getModulePassManager(self):
228228
return ModulePassManager(
@@ -238,18 +238,32 @@ def getFunctionPassManager(self):
238238

239239
def start_pass_timing(self):
240240
"""Enable the pass timers.
241+
242+
Raises
243+
------
244+
RuntimeError
245+
If pass timing is already enabled.
241246
"""
247+
if self._time_passes_handler:
248+
raise RuntimeError("Pass builder should only have one \
249+
pass timer at a time")
250+
self._time_passes_handler = TimePassesHandler()
242251
ffi.lib.LLVMPY_EnableTimePasses(self, self._time_passes_handler)
243252

244253
def finish_pass_timing(self):
245254
"""Returns the pass timings report and disables the LLVM internal
246255
timers. Pass timers are enabled by ``start_pass_timing()``. If the
247256
timers are not enabled, this function will return an empty string.
257+
248258
Returns
249259
-------
250260
res : str
251261
LLVM generated timing report.
252262
"""
263+
264+
if not self._time_passes_handler:
265+
raise RuntimeError("Pass timing is not enabled")
266+
253267
with ffi.OutputString() as buf:
254268
ffi.lib.LLVMPY_ReportAndDisableTimePasses(
255269
self._time_passes_handler, buf)

llvmlite/tests/test_binding.py

+21
Original file line numberDiff line numberDiff line change
@@ -3091,10 +3091,31 @@ def test_empty_report(self):
30913091
pb = self.pb()
30923092
mpm = pb.getModulePassManager()
30933093
mpm.run(mod, pb)
3094+
pb.start_pass_timing()
30943095
report = pb.finish_pass_timing()
30953096
pb.close()
30963097
self.assertFalse(report)
30973098

3099+
def test_multiple_timers_error(self):
3100+
mod = self.module()
3101+
pb = self.pb()
3102+
pb.start_pass_timing()
3103+
mpm = pb.getModulePassManager()
3104+
mpm.run(mod, pb)
3105+
pb.finish_pass_timing()
3106+
with self.assertRaises(RuntimeError):
3107+
pb.start_pass_timing()
3108+
pb.close()
3109+
3110+
def test_empty_report_error(self):
3111+
mod = self.module()
3112+
pb = self.pb()
3113+
mpm = pb.getModulePassManager()
3114+
mpm.run(mod, pb)
3115+
with self.assertRaises(RuntimeError):
3116+
pb.finish_pass_timing()
3117+
pb.close()
3118+
30983119

30993120
class TestNewModulePassManager(BaseTest, NewPassManagerMixin):
31003121
def pm(self):

0 commit comments

Comments
 (0)