xref: /llvm-project/mlir/test/python/ir/context_managers.py (revision 225648e91ccd951eab9a4ab3200248d5617df1cc)
19f3f6d7bSStella Laurenzo# RUN: %PYTHON %s | FileCheck %s
29f3f6d7bSStella Laurenzo
39f3f6d7bSStella Laurenzoimport gc
49f3f6d7bSStella Laurenzofrom mlir.ir import *
59f3f6d7bSStella Laurenzo
6f9008e63STobias Hieta
79f3f6d7bSStella Laurenzodef run(f):
89f3f6d7bSStella Laurenzo    print("\nTEST:", f.__name__)
99f3f6d7bSStella Laurenzo    f()
109f3f6d7bSStella Laurenzo    gc.collect()
119f3f6d7bSStella Laurenzo    assert Context._get_live_count() == 0
129f3f6d7bSStella Laurenzo
139f3f6d7bSStella Laurenzo
149f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testContextEnterExit
159f3f6d7bSStella Laurenzodef testContextEnterExit():
169f3f6d7bSStella Laurenzo    with Context() as ctx:
179f3f6d7bSStella Laurenzo        assert Context.current is ctx
18*225648e9SMaksim Levental    assert Context.current is None
19f9008e63STobias Hieta
209f3f6d7bSStella Laurenzo
219f3f6d7bSStella Laurenzorun(testContextEnterExit)
229f3f6d7bSStella Laurenzo
239f3f6d7bSStella Laurenzo
249f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testLocationEnterExit
259f3f6d7bSStella Laurenzodef testLocationEnterExit():
269f3f6d7bSStella Laurenzo    ctx1 = Context()
279f3f6d7bSStella Laurenzo    with Location.unknown(ctx1) as loc1:
289f3f6d7bSStella Laurenzo        assert Context.current is ctx1
299f3f6d7bSStella Laurenzo        assert Location.current is loc1
309f3f6d7bSStella Laurenzo
319f3f6d7bSStella Laurenzo        # Re-asserting the same context should not change the location.
329f3f6d7bSStella Laurenzo        with ctx1:
339f3f6d7bSStella Laurenzo            assert Context.current is ctx1
349f3f6d7bSStella Laurenzo            assert Location.current is loc1
359f3f6d7bSStella Laurenzo            # Asserting a different context should clear it.
369f3f6d7bSStella Laurenzo            with Context() as ctx2:
379f3f6d7bSStella Laurenzo                assert Context.current is ctx2
389f3f6d7bSStella Laurenzo                try:
399f3f6d7bSStella Laurenzo                    _ = Location.current
40f9008e63STobias Hieta                except ValueError:
41f9008e63STobias Hieta                    pass
42f9008e63STobias Hieta                else:
43f9008e63STobias Hieta                    assert False, "Expected exception"
449f3f6d7bSStella Laurenzo
459f3f6d7bSStella Laurenzo            # And should restore.
469f3f6d7bSStella Laurenzo            assert Context.current is ctx1
479f3f6d7bSStella Laurenzo            assert Location.current is loc1
489f3f6d7bSStella Laurenzo
499f3f6d7bSStella Laurenzo    # All should clear.
509f3f6d7bSStella Laurenzo    try:
519f3f6d7bSStella Laurenzo        _ = Location.current
529f3f6d7bSStella Laurenzo    except ValueError as e:
539f3f6d7bSStella Laurenzo        # CHECK: No current Location
549f3f6d7bSStella Laurenzo        print(e)
55f9008e63STobias Hieta    else:
56f9008e63STobias Hieta        assert False, "Expected exception"
57f9008e63STobias Hieta
589f3f6d7bSStella Laurenzo
599f3f6d7bSStella Laurenzorun(testLocationEnterExit)
609f3f6d7bSStella Laurenzo
619f3f6d7bSStella Laurenzo
629f3f6d7bSStella Laurenzo# CHECK-LABEL: TEST: testInsertionPointEnterExit
639f3f6d7bSStella Laurenzodef testInsertionPointEnterExit():
649f3f6d7bSStella Laurenzo    ctx1 = Context()
659f3f6d7bSStella Laurenzo    m = Module.create(Location.unknown(ctx1))
669f3f6d7bSStella Laurenzo    ip = InsertionPoint(m.body)
679f3f6d7bSStella Laurenzo
689f3f6d7bSStella Laurenzo    with ip:
699f3f6d7bSStella Laurenzo        assert InsertionPoint.current is ip
709f3f6d7bSStella Laurenzo        # Asserting a location from the same context should preserve.
719f3f6d7bSStella Laurenzo        with Location.unknown(ctx1) as loc1:
729f3f6d7bSStella Laurenzo            assert InsertionPoint.current is ip
739f3f6d7bSStella Laurenzo            assert Location.current is loc1
749f3f6d7bSStella Laurenzo        # Location should clear.
759f3f6d7bSStella Laurenzo        try:
769f3f6d7bSStella Laurenzo            _ = Location.current
77f9008e63STobias Hieta        except ValueError:
78f9008e63STobias Hieta            pass
79f9008e63STobias Hieta        else:
80f9008e63STobias Hieta            assert False, "Expected exception"
819f3f6d7bSStella Laurenzo
829f3f6d7bSStella Laurenzo        # Asserting the same Context should preserve.
839f3f6d7bSStella Laurenzo        with ctx1:
849f3f6d7bSStella Laurenzo            assert InsertionPoint.current is ip
859f3f6d7bSStella Laurenzo
869f3f6d7bSStella Laurenzo        # Asserting a different context should clear it.
879f3f6d7bSStella Laurenzo        with Context() as ctx2:
889f3f6d7bSStella Laurenzo            assert Context.current is ctx2
899f3f6d7bSStella Laurenzo            try:
909f3f6d7bSStella Laurenzo                _ = InsertionPoint.current
91f9008e63STobias Hieta            except ValueError:
92f9008e63STobias Hieta                pass
93f9008e63STobias Hieta            else:
94f9008e63STobias Hieta                assert False, "Expected exception"
959f3f6d7bSStella Laurenzo
969f3f6d7bSStella Laurenzo    # All should clear.
979f3f6d7bSStella Laurenzo    try:
989f3f6d7bSStella Laurenzo        _ = InsertionPoint.current
999f3f6d7bSStella Laurenzo    except ValueError as e:
1009f3f6d7bSStella Laurenzo        # CHECK: No current InsertionPoint
1019f3f6d7bSStella Laurenzo        print(e)
102f9008e63STobias Hieta    else:
103f9008e63STobias Hieta        assert False, "Expected exception"
104f9008e63STobias Hieta
1059f3f6d7bSStella Laurenzo
1069f3f6d7bSStella Laurenzorun(testInsertionPointEnterExit)
107