Skip to content

Commit

Permalink
Prevent re-enabling of pass timings
Browse files Browse the repository at this point in the history
  • Loading branch information
yashssh committed Mar 5, 2025
1 parent 86255fe commit 395a996
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
16 changes: 15 additions & 1 deletion llvmlite/binding/newpassmanagers.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def __init__(self, tm, pto):
super().__init__(ffi.lib.LLVMPY_CreatePassBuilder(tm, pto))
self._pto = pto
self._tm = tm
self._time_passes_handler = TimePassesHandler()
self._time_passes_handler = None

def getModulePassManager(self):
return ModulePassManager(
Expand All @@ -238,18 +238,32 @@ def getFunctionPassManager(self):

def start_pass_timing(self):
"""Enable the pass timers.
Raises
------
RuntimeError
If pass timing is already enabled.
"""
if self._time_passes_handler:
raise RuntimeError("Pass builder should only have one \
pass timer at a time")
self._time_passes_handler = TimePassesHandler()
ffi.lib.LLVMPY_EnableTimePasses(self, self._time_passes_handler)

def finish_pass_timing(self):
"""Returns the pass timings report and disables the LLVM internal
timers. Pass timers are enabled by ``start_pass_timing()``. If the
timers are not enabled, this function will return an empty string.
Returns
-------
res : str
LLVM generated timing report.
"""

if not self._time_passes_handler:
raise RuntimeError("Pass timing is not enabled")

with ffi.OutputString() as buf:
ffi.lib.LLVMPY_ReportAndDisableTimePasses(
self._time_passes_handler, buf)
Expand Down
21 changes: 21 additions & 0 deletions llvmlite/tests/test_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -3091,10 +3091,31 @@ def test_empty_report(self):
pb = self.pb()
mpm = pb.getModulePassManager()
mpm.run(mod, pb)
pb.start_pass_timing()
report = pb.finish_pass_timing()
pb.close()
self.assertFalse(report)

def test_multiple_timers_error(self):
mod = self.module()
pb = self.pb()
pb.start_pass_timing()
mpm = pb.getModulePassManager()
mpm.run(mod, pb)
pb.finish_pass_timing()
with self.assertRaises(RuntimeError):
pb.start_pass_timing()
pb.close()

def test_empty_report_error(self):
mod = self.module()
pb = self.pb()
mpm = pb.getModulePassManager()
mpm.run(mod, pb)
with self.assertRaises(RuntimeError):
pb.finish_pass_timing()
pb.close()


class TestNewModulePassManager(BaseTest, NewPassManagerMixin):
def pm(self):
Expand Down

0 comments on commit 395a996

Please sign in to comment.