@@ -63,6 +63,34 @@ def test_insert_before_operation():
6363run (test_insert_before_operation )
6464
6565
66+ # CHECK-LABEL: TEST: test_insert_after_operation
67+ def test_insert_after_operation ():
68+ ctx = Context ()
69+ ctx .allow_unregistered_dialects = True
70+ with Location .unknown (ctx ):
71+ module = Module .parse (
72+ r"""
73+ func.func @foo() -> () {
74+ "custom.op1"() : () -> ()
75+ "custom.op2"() : () -> ()
76+ }
77+ """
78+ )
79+ entry_block = module .body .operations [0 ].regions [0 ].blocks [0 ]
80+ custom_op1 = entry_block .operations [0 ]
81+ custom_op2 = entry_block .operations [1 ]
82+ InsertionPoint .after (custom_op1 ).insert (Operation .create ("custom.op3" ))
83+ InsertionPoint .after (custom_op2 ).insert (Operation .create ("custom.op4" ))
84+ # CHECK: "custom.op1"
85+ # CHECK: "custom.op3"
86+ # CHECK: "custom.op2"
87+ # CHECK: "custom.op4"
88+ module .operation .print ()
89+
90+
91+ run (test_insert_after_operation )
92+
93+
6694# CHECK-LABEL: TEST: test_insert_at_block_begin
6795def test_insert_at_block_begin ():
6896 ctx = Context ()
@@ -111,14 +139,24 @@ def test_insert_at_terminator():
111139 """
112140 )
113141 entry_block = module .body .operations [0 ].regions [0 ].blocks [0 ]
142+ return_op = entry_block .operations [1 ]
114143 ip = InsertionPoint .at_block_terminator (entry_block )
115144 assert ip .block == entry_block
116- assert ip .ref_operation == entry_block .operations [1 ]
117- ip .insert (Operation .create ("custom.op2" ))
145+ assert ip .ref_operation == return_op
146+ custom_op2 = Operation .create ("custom.op2" )
147+ ip .insert (custom_op2 )
148+ InsertionPoint .after (custom_op2 ).insert (Operation .create ("custom.op3" ))
118149 # CHECK: "custom.op1"
119150 # CHECK: "custom.op2"
151+ # CHECK: "custom.op3"
120152 module .operation .print ()
121153
154+ try :
155+ InsertionPoint .after (return_op ).insert (Operation .create ("custom.op4" ))
156+ except IndexError as e :
157+ # CHECK: ERROR: Cannot insert operation at the end of a block that already has a terminator.
158+ print (f"ERROR: { e } " )
159+
122160
123161run (test_insert_at_terminator )
124162
@@ -187,10 +225,16 @@ def test_insertion_point_context():
187225 with InsertionPoint (entry_block ):
188226 Operation .create ("custom.op2" )
189227 with InsertionPoint .at_block_begin (entry_block ):
190- Operation .create ("custom.opa" )
228+ custom_opa = Operation .create ("custom.opa" )
191229 Operation .create ("custom.opb" )
192230 Operation .create ("custom.op3" )
231+ with InsertionPoint .after (custom_opa ):
232+ Operation .create ("custom.op4" )
233+ Operation .create ("custom.op5" )
234+
193235 # CHECK: "custom.opa"
236+ # CHECK: "custom.op4"
237+ # CHECK: "custom.op5"
194238 # CHECK: "custom.opb"
195239 # CHECK: "custom.op1"
196240 # CHECK: "custom.op2"
0 commit comments