Skip to content

Commit

Permalink
[mypyc] LoadInt store doubled value if tagged integer (#9162)
Browse files Browse the repository at this point in the history
Mypyc currently represents int and short_int using a tagged representation, 
which requires doubling the value when emitting to C. Since we are moving 
towards low-level IR, we change LoadInt to store the doubled value directly 
if the type is int/short_int, to be explicit about the tagged representation.
  • Loading branch information
TH3CHARLie authored Jul 17, 2020
1 parent 53ec9f3 commit 5e31019
Show file tree
Hide file tree
Showing 16 changed files with 321 additions and 321 deletions.
7 changes: 2 additions & 5 deletions mypyc/codegen/emitfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
NAMESPACE_TYPE, NAMESPACE_MODULE, RaiseStandardError, CallC, LoadGlobal, Truncate,
BinaryIntOp
)
from mypyc.ir.rtypes import RType, RTuple, is_int32_rprimitive, is_int64_rprimitive
from mypyc.ir.rtypes import RType, RTuple
from mypyc.ir.func_ir import FuncIR, FuncDecl, FUNC_STATICMETHOD, FUNC_CLASSMETHOD
from mypyc.ir.class_ir import ClassIR

Expand Down Expand Up @@ -175,10 +175,7 @@ def visit_assign(self, op: Assign) -> None:

def visit_load_int(self, op: LoadInt) -> None:
dest = self.reg(op)
if is_int32_rprimitive(op.type) or is_int64_rprimitive(op.type):
self.emit_line('%s = %d;' % (dest, op.value))
else:
self.emit_line('%s = %d;' % (dest, op.value * 2))
self.emit_line('%s = %d;' % (dest, op.value))

def visit_load_error_value(self, op: LoadErrorValue) -> None:
if isinstance(op.type, RTuple):
Expand Down
5 changes: 4 additions & 1 deletion mypyc/ir/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,10 @@ class LoadInt(RegisterOp):

def __init__(self, value: int, line: int = -1, rtype: RType = short_int_rprimitive) -> None:
super().__init__(line)
self.value = value
if is_short_int_rprimitive(rtype) or is_int_rprimitive(rtype):
self.value = value * 2
else:
self.value = value
self.type = rtype

def sources(self) -> List[Value]:
Expand Down
60 changes: 30 additions & 30 deletions mypyc/test-data/analysis.test
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def f(a):
z :: int
r10 :: None
L0:
r0 = 1
r0 = 2
x = r0
r2 = 1
r3 = x & r2
Expand All @@ -38,11 +38,11 @@ L2:
L3:
if r1 goto L4 else goto L5 :: bool
L4:
r8 = 1
r8 = 2
y = r8
goto L6
L5:
r9 = 1
r9 = 2
z = r9
L6:
r10 = None
Expand Down Expand Up @@ -85,9 +85,9 @@ def f(a):
r1 :: short_int
r2 :: bool
L0:
r0 = 1
r0 = 2
x = r0
r1 = 1
r1 = 2
r2 = CPyTagged_IsEq(x, r1)
if r2 goto L1 else goto L2 :: bool
L1:
Expand Down Expand Up @@ -119,11 +119,11 @@ def f():
y :: int
r2 :: short_int
L0:
r0 = 1
r0 = 2
x = r0
r1 = 1
r1 = 2
y = r1
r2 = 2
r2 = 4
x = r2
return x
(0, 0) {} {r0}
Expand All @@ -145,11 +145,11 @@ def f(a):
a :: int
r0, r1, r2 :: short_int
L0:
r0 = 1
r0 = 2
a = r0
r1 = 2
r1 = 4
a = r1
r2 = 3
r2 = 6
a = r2
return a
(0, 0) {} {r0}
Expand Down Expand Up @@ -179,17 +179,17 @@ def f(a):
r4 :: short_int
r5 :: None
L0:
r0 = 1
r0 = 2
r1 = CPyTagged_IsEq(a, r0)
if r1 goto L1 else goto L2 :: bool
L1:
r2 = 1
r2 = 2
y = r2
r3 = 2
r3 = 4
x = r3
goto L3
L2:
r4 = 2
r4 = 4
x = r4
L3:
r5 = None
Expand Down Expand Up @@ -233,11 +233,11 @@ def f(n):
r4 :: None
L0:
L1:
r0 = 5
r0 = 10
r1 = CPyTagged_IsLt(n, r0)
if r1 goto L2 else goto L3 :: bool
L2:
r2 = 1
r2 = 2
r3 = CPyTagged_Add(n, r2)
n = r3
m = n
Expand Down Expand Up @@ -280,22 +280,22 @@ def f(n):
r6 :: short_int
r7 :: None
L0:
r0 = 1
r0 = 2
x = r0
r1 = 1
r1 = 2
y = r1
L1:
r2 = 1
r2 = 2
r3 = CPyTagged_IsLt(n, r2)
if r3 goto L2 else goto L6 :: bool
L2:
n = y
L3:
r4 = 2
r4 = 4
r5 = CPyTagged_IsLt(n, r4)
if r5 goto L4 else goto L5 :: bool
L4:
r6 = 1
r6 = 2
n = r6
n = x
goto L3
Expand Down Expand Up @@ -335,7 +335,7 @@ def f(x):
r0 :: short_int
r1, a, r2, r3, r4 :: int
L0:
r0 = 1
r0 = 2
r1 = f(r0)
if is_error(r1) goto L3 (error at f:2) else goto L1
L1:
Expand Down Expand Up @@ -494,7 +494,7 @@ def f(a):
r0 :: short_int
L0:
b = a
r0 = 1
r0 = 2
a = r0
return a
(0, 0) {a} {a}
Expand Down Expand Up @@ -535,13 +535,13 @@ L2:
L3:
if r0 goto L4 else goto L5 :: bool
L4:
r7 = 2
r7 = 4
x = r7
r8 = 1
r8 = 2
a = r8
goto L6
L5:
r9 = 1
r9 = 2
x = r9
L6:
return x
Expand Down Expand Up @@ -597,7 +597,7 @@ L1:
L2:
r3 = CPyTagged_Add(sum, i)
sum = r3
r4 = 1
r4 = 2
r5 = CPyTagged_Add(i, r4)
i = r5
goto L1
Expand Down Expand Up @@ -659,7 +659,7 @@ L3:
r5 = CPy_ExceptionMatches(r4)
if r5 goto L4 else goto L5 :: bool
L4:
r6 = 1
r6 = 2
r7 = CPyTagged_Negate(r6)
CPy_RestoreExcInfo(r1)
return r7
Expand All @@ -679,7 +679,7 @@ L8:
L9:
unreachable
L10:
r9 = 1
r9 = 2
r10 = CPyTagged_Add(st, r9)
return r10
L11:
Expand Down
8 changes: 4 additions & 4 deletions mypyc/test-data/exceptions.test
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ L0:
r2 = x is r1
if r2 goto L1 else goto L2 :: bool
L1:
r3 = 1
r3 = 2
return r3
L2:
inc_ref x
Expand All @@ -104,10 +104,10 @@ L3:
r8 = !r7
if r8 goto L4 else goto L5 :: bool
L4:
r9 = 2
r9 = 4
return r9
L5:
r10 = 3
r10 = 6
return r10
L6:
r11 = <error> :: int
Expand Down Expand Up @@ -176,7 +176,7 @@ L7:
dec_ref sum :: int
dec_ref r15 :: int
sum = r16
r17 = 1
r17 = 2
r18 = CPyTagged_Add(i, r17)
dec_ref i :: int
i = r18
Expand Down
Loading

0 comments on commit 5e31019

Please sign in to comment.