Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5b7290

Browse files
authored
[mypyc] Merge exception ops (#9121)
Relates to mypyc/mypyc#734.
1 parent f73834e commit a5b7290

12 files changed

Lines changed: 188 additions & 192 deletions

mypyc/irbuild/for_helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def gen_cleanup(self) -> None:
386386
# an exception was raised during the loop, then err_reg wil be set to
387387
# True. If no_err_occurred_op returns False, then the exception will be
388388
# propagated using the ERR_FALSE flag.
389-
self.builder.primitive_op(no_err_occurred_op, [], self.line)
389+
self.builder.call_c(no_err_occurred_op, [], self.line)
390390

391391

392392
def unsafe_index(
@@ -539,7 +539,7 @@ def gen_step(self) -> None:
539539

540540
def gen_cleanup(self) -> None:
541541
# Same as for generic ForIterable.
542-
self.builder.primitive_op(no_err_occurred_op, [], self.line)
542+
self.builder.call_c(no_err_occurred_op, [], self.line)
543543

544544

545545
class ForDictionaryKeys(ForDictionaryCommon):

mypyc/irbuild/generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def add_raise_exception_blocks_to_generator_class(builder: IRBuilder, line: int)
114114
builder.add_bool_branch(comparison, error_block, ok_block)
115115

116116
builder.activate_block(error_block)
117-
builder.primitive_op(raise_exception_with_tb_op, [exc_type, exc_val, exc_tb], line)
117+
builder.call_c(raise_exception_with_tb_op, [exc_type, exc_val, exc_tb], line)
118118
builder.add(Unreachable())
119119
builder.goto_and_activate(ok_block)
120120

mypyc/irbuild/nonlocalcontrol.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def gen_return(self, builder: 'IRBuilder', value: Value, line: int) -> None:
9999
# StopIteration instead of using RaiseStandardError because
100100
# the obvious thing doesn't work if the value is a tuple
101101
# (???).
102-
builder.primitive_op(set_stop_iteration_value, [value], NO_TRACEBACK_LINE_NO)
102+
builder.call_c(set_stop_iteration_value, [value], NO_TRACEBACK_LINE_NO)
103103
builder.add(Unreachable())
104104
builder.builder.pop_error_handler()
105105

@@ -159,7 +159,7 @@ def __init__(self, outer: NonlocalControl, saved: Union[Value, AssignmentTarget]
159159
self.saved = saved
160160

161161
def gen_cleanup(self, builder: 'IRBuilder', line: int) -> None:
162-
builder.primitive_op(restore_exc_info_op, [builder.read(self.saved)], line)
162+
builder.call_c(restore_exc_info_op, [builder.read(self.saved)], line)
163163

164164

165165
class FinallyNonlocalControl(CleanupNonlocalControl):
@@ -187,5 +187,5 @@ def gen_cleanup(self, builder: 'IRBuilder', line: int) -> None:
187187
target, cleanup = BasicBlock(), BasicBlock()
188188
builder.add(Branch(self.saved, target, cleanup, Branch.IS_ERROR))
189189
builder.activate_block(cleanup)
190-
builder.primitive_op(restore_exc_info_op, [self.saved], line)
190+
builder.call_c(restore_exc_info_op, [self.saved], line)
191191
builder.goto_and_activate(target)

mypyc/irbuild/statement.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def transform_continue_stmt(builder: IRBuilder, node: ContinueStmt) -> None:
238238

239239
def transform_raise_stmt(builder: IRBuilder, s: RaiseStmt) -> None:
240240
if s.expr is None:
241-
builder.primitive_op(reraise_exception_op, [], NO_TRACEBACK_LINE_NO)
241+
builder.call_c(reraise_exception_op, [], NO_TRACEBACK_LINE_NO)
242242
builder.add(Unreachable())
243243
return
244244

@@ -278,7 +278,7 @@ def transform_try_except(builder: IRBuilder,
278278
# exception is raised, based on the exception in exc_info.
279279
builder.builder.push_error_handler(double_except_block)
280280
builder.activate_block(except_entry)
281-
old_exc = builder.maybe_spill(builder.primitive_op(error_catch_op, [], line))
281+
old_exc = builder.maybe_spill(builder.call_c(error_catch_op, [], line))
282282
# Compile the except blocks with the nonlocal control flow overridden to clear exc_info
283283
builder.nonlocal_control.append(
284284
ExceptNonlocalControl(builder.nonlocal_control[-1], old_exc))
@@ -288,7 +288,7 @@ def transform_try_except(builder: IRBuilder,
288288
next_block = None
289289
if type:
290290
next_block, body_block = BasicBlock(), BasicBlock()
291-
matches = builder.primitive_op(
291+
matches = builder.call_c(
292292
exc_matches_op, [builder.accept(type)], type.line
293293
)
294294
builder.add(Branch(matches, body_block, next_block, Branch.BOOL_EXPR))
@@ -297,7 +297,7 @@ def transform_try_except(builder: IRBuilder,
297297
target = builder.get_assignment_target(var)
298298
builder.assign(
299299
target,
300-
builder.primitive_op(get_exc_value_op, [], var.line),
300+
builder.call_c(get_exc_value_op, [], var.line),
301301
var.line
302302
)
303303
handler_body()
@@ -307,7 +307,7 @@ def transform_try_except(builder: IRBuilder,
307307

308308
# Reraise the exception if needed
309309
if next_block:
310-
builder.primitive_op(reraise_exception_op, [], NO_TRACEBACK_LINE_NO)
310+
builder.call_c(reraise_exception_op, [], NO_TRACEBACK_LINE_NO)
311311
builder.add(Unreachable())
312312

313313
builder.nonlocal_control.pop()
@@ -317,14 +317,14 @@ def transform_try_except(builder: IRBuilder,
317317
# restore the saved exc_info information and continue propagating
318318
# the exception if it exists.
319319
builder.activate_block(cleanup_block)
320-
builder.primitive_op(restore_exc_info_op, [builder.read(old_exc)], line)
320+
builder.call_c(restore_exc_info_op, [builder.read(old_exc)], line)
321321
builder.goto(exit_block)
322322

323323
# Cleanup for if we leave except through a raised exception:
324324
# restore the saved exc_info information and continue propagating
325325
# the exception.
326326
builder.activate_block(double_except_block)
327-
builder.primitive_op(restore_exc_info_op, [builder.read(old_exc)], line)
327+
builder.call_c(restore_exc_info_op, [builder.read(old_exc)], line)
328328
builder.primitive_op(keep_propagating_op, [], NO_TRACEBACK_LINE_NO)
329329
builder.add(Unreachable())
330330

@@ -402,7 +402,7 @@ def try_finally_entry_blocks(builder: IRBuilder,
402402
builder.add(LoadErrorValue(builder.ret_types[-1]))
403403
)
404404
)
405-
builder.add(Assign(old_exc, builder.primitive_op(error_catch_op, [], -1)))
405+
builder.add(Assign(old_exc, builder.call_c(error_catch_op, [], -1)))
406406
builder.goto(finally_block)
407407

408408
return old_exc
@@ -442,7 +442,7 @@ def try_finally_resolve_control(builder: IRBuilder,
442442

443443
# Reraise the exception if there was one
444444
builder.activate_block(reraise)
445-
builder.primitive_op(reraise_exception_op, [], NO_TRACEBACK_LINE_NO)
445+
builder.call_c(reraise_exception_op, [], NO_TRACEBACK_LINE_NO)
446446
builder.add(Unreachable())
447447
builder.builder.pop_error_handler()
448448

@@ -520,7 +520,7 @@ def transform_try_body() -> None:
520520

521521

522522
def get_sys_exc_info(builder: IRBuilder) -> List[Value]:
523-
exc_info = builder.primitive_op(get_exc_info_op, [], -1)
523+
exc_info = builder.call_c(get_exc_info_op, [], -1)
524524
return [builder.add(TupleGet(exc_info, i, -1)) for i in range(3)]
525525

526526

@@ -557,7 +557,7 @@ def except_body() -> None:
557557
reraise_block
558558
)
559559
builder.activate_block(reraise_block)
560-
builder.primitive_op(reraise_exception_op, [], NO_TRACEBACK_LINE_NO)
560+
builder.call_c(reraise_exception_op, [], NO_TRACEBACK_LINE_NO)
561561
builder.add(Unreachable())
562562
builder.activate_block(out_block)
563563

mypyc/primitives/exc_ops.py

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from mypyc.ir.ops import ERR_NEVER, ERR_FALSE, ERR_ALWAYS
44
from mypyc.ir.rtypes import bool_rprimitive, object_rprimitive, void_rtype, exc_rtuple
55
from mypyc.primitives.registry import (
6-
simple_emit, call_emit, call_void_emit, call_and_fail_emit, custom_op, c_custom_op
6+
simple_emit, custom_op, c_custom_op
77
)
88

99
# If the argument is a class, raise an instance of the class. Otherwise, assume
@@ -15,37 +15,33 @@
1515
error_kind=ERR_ALWAYS)
1616

1717
# Raise StopIteration exception with the specified value (which can be NULL).
18-
set_stop_iteration_value = custom_op(
18+
set_stop_iteration_value = c_custom_op(
1919
arg_types=[object_rprimitive],
20-
result_type=bool_rprimitive,
21-
error_kind=ERR_FALSE,
22-
format_str='set_stop_iteration_value({args[0]}); {dest} = 0',
23-
emit=call_and_fail_emit('CPyGen_SetStopIterationValue'))
20+
return_type=void_rtype,
21+
c_function_name='CPyGen_SetStopIterationValue',
22+
error_kind=ERR_ALWAYS)
2423

2524
# Raise exception with traceback.
2625
# Arguments are (exception type, exception value, traceback).
27-
raise_exception_with_tb_op = custom_op(
26+
raise_exception_with_tb_op = c_custom_op(
2827
arg_types=[object_rprimitive, object_rprimitive, object_rprimitive],
29-
result_type=bool_rprimitive,
30-
error_kind=ERR_FALSE,
31-
format_str='raise_exception_with_tb({args[0]}, {args[1]}, {args[2]}); {dest} = 0',
32-
emit=call_and_fail_emit('CPyErr_SetObjectAndTraceback'))
28+
return_type=void_rtype,
29+
c_function_name='CPyErr_SetObjectAndTraceback',
30+
error_kind=ERR_ALWAYS)
3331

3432
# Reraise the currently raised exception.
35-
reraise_exception_op = custom_op(
33+
reraise_exception_op = c_custom_op(
3634
arg_types=[],
37-
result_type=bool_rprimitive,
38-
error_kind=ERR_FALSE,
39-
format_str='reraise_exc; {dest} = 0',
40-
emit=call_and_fail_emit('CPy_Reraise'))
35+
return_type=void_rtype,
36+
c_function_name='CPy_Reraise',
37+
error_kind=ERR_ALWAYS)
4138

4239
# Propagate exception if the CPython error indicator is set (an exception was raised).
43-
no_err_occurred_op = custom_op(
40+
no_err_occurred_op = c_custom_op(
4441
arg_types=[],
45-
result_type=bool_rprimitive,
46-
error_kind=ERR_FALSE,
47-
format_str='{dest} = no_err_occurred',
48-
emit=call_emit('CPy_NoErrOccured'))
42+
return_type=bool_rprimitive,
43+
c_function_name='CPy_NoErrOccured',
44+
error_kind=ERR_FALSE)
4945

5046
# Assert that the error indicator has been set.
5147
assert_err_occured_op = custom_op(
@@ -68,42 +64,37 @@
6864
# handled exception" (by sticking it into sys.exc_info()). Returns the
6965
# exception that was previously being handled, which must be restored
7066
# later.
71-
error_catch_op = custom_op(
67+
error_catch_op = c_custom_op(
7268
arg_types=[],
73-
result_type=exc_rtuple,
74-
error_kind=ERR_NEVER,
75-
format_str='{dest} = error_catch',
76-
emit=call_emit('CPy_CatchError'))
69+
return_type=exc_rtuple,
70+
c_function_name='CPy_CatchError',
71+
error_kind=ERR_NEVER)
7772

7873
# Restore an old "currently handled exception" returned from.
7974
# error_catch (by sticking it into sys.exc_info())
80-
restore_exc_info_op = custom_op(
75+
restore_exc_info_op = c_custom_op(
8176
arg_types=[exc_rtuple],
82-
result_type=void_rtype,
83-
error_kind=ERR_NEVER,
84-
format_str='restore_exc_info {args[0]}',
85-
emit=call_void_emit('CPy_RestoreExcInfo'))
77+
return_type=void_rtype,
78+
c_function_name='CPy_RestoreExcInfo',
79+
error_kind=ERR_NEVER)
8680

8781
# Checks whether the exception currently being handled matches a particular type.
88-
exc_matches_op = custom_op(
82+
exc_matches_op = c_custom_op(
8983
arg_types=[object_rprimitive],
90-
result_type=bool_rprimitive,
91-
error_kind=ERR_NEVER,
92-
format_str='{dest} = exc_matches {args[0]}',
93-
emit=call_emit('CPy_ExceptionMatches'))
84+
return_type=bool_rprimitive,
85+
c_function_name='CPy_ExceptionMatches',
86+
error_kind=ERR_NEVER)
9487

9588
# Get the value of the exception currently being handled.
96-
get_exc_value_op = custom_op(
89+
get_exc_value_op = c_custom_op(
9790
arg_types=[],
98-
result_type=object_rprimitive,
99-
error_kind=ERR_NEVER,
100-
format_str='{dest} = get_exc_value',
101-
emit=call_emit('CPy_GetExcValue'))
91+
return_type=object_rprimitive,
92+
c_function_name='CPy_GetExcValue',
93+
error_kind=ERR_NEVER)
10294

10395
# Get exception info (exception type, exception instance, traceback object).
104-
get_exc_info_op = custom_op(
96+
get_exc_info_op = c_custom_op(
10597
arg_types=[],
106-
result_type=exc_rtuple,
107-
error_kind=ERR_NEVER,
108-
format_str='{dest} = get_exc_info',
109-
emit=call_emit('CPy_GetExcInfo'))
98+
return_type=exc_rtuple,
99+
c_function_name='CPy_GetExcInfo',
100+
error_kind=ERR_NEVER)

mypyc/test-data/analysis.test

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -568,49 +568,51 @@ def lol(x):
568568
r5 :: bool
569569
r6 :: short_int
570570
r7 :: int
571-
r8, r9 :: bool
572-
r10 :: short_int
573-
r11, r12 :: int
571+
r8 :: bool
572+
r9 :: short_int
573+
r10, r11 :: int
574+
r12 :: bool
574575
L0:
575576
L1:
576577
r0 = CPyTagged_Id(x)
577578
st = r0
578579
goto L10
579580
L2:
580-
r1 = error_catch
581+
r1 = CPy_CatchError()
581582
r2 = builtins :: module
582583
r3 = unicode_1 :: static ('Exception')
583584
r4 = getattr r2, r3
584585
if is_error(r4) goto L8 (error at lol:4) else goto L3
585586
L3:
586-
r5 = exc_matches r4
587+
r5 = CPy_ExceptionMatches(r4)
587588
if r5 goto L4 else goto L5 :: bool
588589
L4:
589590
r6 = 1
590591
r7 = CPyTagged_Negate(r6)
591-
restore_exc_info r1
592+
CPy_RestoreExcInfo(r1)
592593
return r7
593594
L5:
594-
reraise_exc; r8 = 0
595-
if not r8 goto L8 else goto L6 :: bool
595+
CPy_Reraise()
596+
r12 = 0
597+
if not r12 goto L8 else goto L6 :: bool
596598
L6:
597599
unreachable
598600
L7:
599-
restore_exc_info r1
601+
CPy_RestoreExcInfo(r1)
600602
goto L10
601603
L8:
602-
restore_exc_info r1
603-
r9 = keep_propagating
604-
if not r9 goto L11 else goto L9 :: bool
604+
CPy_RestoreExcInfo(r1)
605+
r8 = keep_propagating
606+
if not r8 goto L11 else goto L9 :: bool
605607
L9:
606608
unreachable
607609
L10:
608-
r10 = 1
609-
r11 = CPyTagged_Add(st, r10)
610-
return r11
610+
r9 = 1
611+
r10 = CPyTagged_Add(st, r9)
612+
return r10
611613
L11:
612-
r12 = <error> :: int
613-
return r12
614+
r11 = <error> :: int
615+
return r11
614616
(0, 0) {x} {x}
615617
(1, 0) {x} {r0}
616618
(1, 1) {r0} {st}
@@ -626,18 +628,19 @@ L11:
626628
(4, 1) {r1, r6} {r1, r7}
627629
(4, 2) {r1, r7} {r7}
628630
(4, 3) {r7} {}
629-
(5, 0) {r1} {r1, r8}
630-
(5, 1) {r1, r8} {r1}
631+
(5, 0) {r1} {r1}
632+
(5, 1) {r1} {r1, r12}
633+
(5, 2) {r1, r12} {r1}
631634
(6, 0) {} {}
632635
(7, 0) {r1, st} {st}
633636
(7, 1) {st} {st}
634637
(8, 0) {r1} {}
635-
(8, 1) {} {r9}
636-
(8, 2) {r9} {}
638+
(8, 1) {} {r8}
639+
(8, 2) {r8} {}
637640
(9, 0) {} {}
638-
(10, 0) {st} {r10, st}
639-
(10, 1) {r10, st} {r11}
640-
(10, 2) {r11} {}
641-
(11, 0) {} {r12}
642-
(11, 1) {r12} {}
641+
(10, 0) {st} {r9, st}
642+
(10, 1) {r9, st} {r10}
643+
(10, 2) {r10} {}
644+
(11, 0) {} {r11}
645+
(11, 1) {r11} {}
643646

0 commit comments

Comments
 (0)