[Python] Expose compiler invocation phase controls (#25250)

While using IREE's compiler from Python, I noticed that the C API's
compile-from and compile-to controls aren't exposed through Invocation.

This adds the two Python methods and extends the existing API tests to
cover stopping/resuming compilation, invalid phase names and invalid
ordering.

Signed-off-by: anutosh491 <andersonbhat491@gmail.com>
diff --git a/compiler/bindings/python/iree/compiler/api/ctypes_dl.py b/compiler/bindings/python/iree/compiler/api/ctypes_dl.py
index 9e75724..bfdab1b 100644
--- a/compiler/bindings/python/iree/compiler/api/ctypes_dl.py
+++ b/compiler/bindings/python/iree/compiler/api/ctypes_dl.py
@@ -60,6 +60,10 @@
     _setsig(_dylib.ireeCompilerInvocationCreate, c_void_p, [c_void_p])
     _setsig(_dylib.ireeCompilerInvocationDestroy, None, [c_void_p])
     _setsig(_dylib.ireeCompilerInvocationEnableConsoleDiagnostics, None, [c_void_p])
+    _setsig(
+        _dylib.ireeCompilerInvocationSetCompileFromPhase, None, [c_void_p, c_char_p]
+    )
+    _setsig(_dylib.ireeCompilerInvocationSetCompileToPhase, None, [c_void_p, c_char_p])
     _setsig(_dylib.ireeCompilerInvocationParseSource, c_bool, [c_void_p, c_void_p])
     _setsig(_dylib.ireeCompilerInvocationPipeline, c_bool, [c_void_p, c_int])
     _setsig(_dylib.ireeCompilerInvocationRunPassPipeline, c_bool, [c_void_p, c_char_p])
@@ -436,6 +440,14 @@
     def enable_console_diagnostics(self):
         _dylib.ireeCompilerInvocationEnableConsoleDiagnostics(self._inv_p)
 
+    def set_compile_from_phase(self, phase: str):
+        """Resumes compilation after the named pipeline phase."""
+        _dylib.ireeCompilerInvocationSetCompileFromPhase(self._inv_p, phase.encode())
+
+    def set_compile_to_phase(self, phase: str):
+        """Stops compilation after the named pipeline phase."""
+        _dylib.ireeCompilerInvocationSetCompileToPhase(self._inv_p, phase.encode())
+
     def export_module(self):
         """Exports the module."""
         from .. import ir
diff --git a/compiler/bindings/python/test/api/api_test.py b/compiler/bindings/python/test/api/api_test.py
index 2a5863c..76e407d 100644
--- a/compiler/bindings/python/test/api/api_test.py
+++ b/compiler/bindings/python/test/api/api_test.py
@@ -100,6 +100,46 @@
             session = Session()
             inv = session.invocation()
 
+        def testCompilePhases(self):
+            with Session() as session, session.invocation() as inv:
+                source = Source.wrap_buffer(
+                    session,
+                    b"""
+                    func.func @add(%a: tensor<4xf32>, %b: tensor<4xf32>)
+                        -> tensor<4xf32> {
+                        %result = arith.addf %a, %b : tensor<4xf32>
+                        return %result : tensor<4xf32>
+                    }
+                    """,
+                )
+                self.assertTrue(inv.parse_source(source))
+                inv.set_compile_to_phase("input")
+                self.assertTrue(inv.execute())
+                with Output.open_membuffer() as out:
+                    inv.output_ir(out)
+                    self.assertNotIn(b"hal.tensor.import", bytes(out.map_memory()))
+
+                inv.set_compile_from_phase("input")
+                inv.set_compile_to_phase("abi")
+                self.assertTrue(inv.execute())
+                with Output.open_membuffer() as out:
+                    inv.output_ir(out)
+                    self.assertIn(b"hal.tensor.import", bytes(out.map_memory()))
+
+        def testInvalidCompilePhases(self):
+            for start, end in (
+                ("invalid", "end"),
+                ("start", "invalid"),
+                ("abi", "input"),
+            ):
+                with self.subTest(start=start, end=end):
+                    with Session() as session, session.invocation() as inv:
+                        source = Source.wrap_buffer(session, b"module {}")
+                        self.assertTrue(inv.parse_source(source))
+                        inv.set_compile_from_phase(start)
+                        inv.set_compile_to_phase(end)
+                        self.assertFalse(inv.execute())
+
         def testInputFile(self):
             session = Session()
             inv = session.invocation()