Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
b7a712bd
Commit
b7a712bd
authored
Oct 06, 2014
by
Robert Bradshaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Generalize assignment unrolling.
parent
ead141e6
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
99 additions
and
17 deletions
+99
-17
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+90
-17
tests/run/arrayassign.pyx
tests/run/arrayassign.pyx
+9
-0
No files found.
Cython/Compiler/Nodes.py
View file @
b7a712bd
...
@@ -4754,23 +4754,9 @@ class SingleAssignmentNode(AssignmentNode):
...
@@ -4754,23 +4754,9 @@ class SingleAssignmentNode(AssignmentNode):
self
.
rhs
=
self
.
rhs
.
analyse_types
(
env
)
self
.
rhs
=
self
.
rhs
.
analyse_types
(
env
)
if
self
.
rhs
.
type
.
is_ctuple
and
isinstance
(
self
.
lhs
,
ExprNodes
.
TupleNode
):
unrolled_assignment
=
self
.
unroll_rhs
(
env
)
if
self
.
rhs
.
type
.
size
==
len
(
self
.
lhs
.
args
):
if
unrolled_assignment
:
rhs
=
UtilNodes
.
LetRefNode
(
self
.
rhs
)
return
unrolled_assignment
nodes
=
[]
for
ix
,
lhs
in
enumerate
(
self
.
lhs
.
args
):
nodes
.
append
(
SingleAssignmentNode
(
pos
=
self
.
pos
,
lhs
=
lhs
,
rhs
=
ExprNodes
.
IndexNode
(
pos
=
self
.
pos
,
base
=
rhs
,
index
=
ExprNodes
.
IntNode
(
pos
=
self
.
pos
,
value
=
str
(
ix
))),
first
=
self
.
first
))
return
UtilNodes
.
LetNode
(
rhs
,
ParallelAssignmentNode
(
pos
=
self
.
pos
,
stats
=
nodes
).
analyse_expressions
(
env
))
else
:
error
(
self
.
pos
,
"Unpacking type %s requires exactly %s arguments."
%
(
self
.
rhs
.
type
,
self
.
rhs
.
type
.
size
))
self
.
lhs
=
self
.
lhs
.
analyse_target_types
(
env
)
self
.
lhs
=
self
.
lhs
.
analyse_target_types
(
env
)
self
.
lhs
.
gil_assignment_check
(
env
)
self
.
lhs
.
gil_assignment_check
(
env
)
...
@@ -4801,6 +4787,93 @@ class SingleAssignmentNode(AssignmentNode):
...
@@ -4801,6 +4787,93 @@ class SingleAssignmentNode(AssignmentNode):
self
.
rhs
=
rhs
self
.
rhs
=
rhs
return
self
return
self
def
unroll
(
self
,
node
,
target_size
,
env
):
from
.
import
ExprNodes
,
UtilNodes
if
node
.
type
.
is_ctuple
:
if
node
.
type
.
size
==
target_size
:
base
=
self
.
rhs
start_node
=
None
stop_node
=
None
step_node
=
None
check_node
=
None
else
:
error
(
self
.
pos
,
"Unpacking type %s requires exactly %s arguments."
%
(
self
.
rhs
.
type
,
self
.
rhs
.
type
.
size
))
return
elif
node
.
type
.
is_ptr
:
if
isinstance
(
self
.
rhs
,
ExprNodes
.
SliceIndexNode
):
base
=
self
.
rhs
.
base
start_node
=
self
.
rhs
.
start
if
start_node
:
start_node
=
start_node
.
coerce_to
(
PyrexTypes
.
c_py_ssize_t_type
,
env
)
stop_node
=
self
.
rhs
.
stop
if
stop_node
:
stop_node
=
stop_node
.
coerce_to
(
PyrexTypes
.
c_py_ssize_t_type
,
env
)
else
:
if
rhs
.
is_array
and
rhs
.
type
.
size
:
stop_node
=
ExprNodes
.
IntNode
(
pos
=
self
.
pos
,
value
=
str
(
rhs
.
type
.
size
))
else
:
error
(
self
.
pos
,
"C array iteration requires known end index"
)
return
step_node
=
None
#self.rhs.step
if
step_node
:
step_node
=
step_node
.
coerce_to
(
PyrexTypes
.
c_py_ssize_t_type
,
env
)
# TODO: check (stop - start) / slice
check_node
=
None
else
:
return
else
:
return
items
=
[]
base_ref
=
UtilNodes
.
LetRefNode
(
base
)
refs
=
[
base_ref
]
if
start_node
:
start_node
=
UtilNodes
.
LetRefNode
(
start_node
)
refs
.
append
(
start_node
)
if
stop_node
:
stop_node
=
UtilNodes
.
LetRefNode
(
stop_node
)
refs
.
append
(
stop_node
)
if
step_node
:
step_node
=
UtilNodes
.
LetRefNode
(
step_node
)
refs
.
append
(
step_node
)
for
ix
in
range
(
target_size
):
ix_node
=
ExprNodes
.
IntNode
(
pos
=
self
.
pos
,
value
=
str
(
ix
))
if
step_node
is
not
None
:
ix_node
=
ExprNodes
.
MulNode
(
pos
=
self
.
pos
,
operator
=
'*'
,
operand1
=
step_node
,
operand2
=
ix_node
).
analyse_types
(
env
)
if
start_node
is
not
None
:
ix_node
=
ExprNodes
.
AddNode
(
pos
=
self
.
pos
,
operator
=
'+'
,
operand1
=
start_node
,
operand2
=
ix_node
).
analyse_types
(
env
)
items
.
append
(
ExprNodes
.
IndexNode
(
pos
=
self
.
pos
,
base
=
base_ref
,
index
=
ix_node
))
return
check_node
,
refs
,
items
def
unroll_rhs
(
self
,
env
):
from
.
import
ExprNodes
,
UtilNodes
if
not
isinstance
(
self
.
lhs
,
ExprNodes
.
TupleNode
):
return
unrolled
=
self
.
unroll
(
self
.
rhs
,
len
(
self
.
lhs
.
args
),
env
)
if
not
unrolled
:
return
check_node
,
refs
,
items
=
unrolled
assignments
=
[]
for
lhs
,
rhs
in
zip
(
self
.
lhs
.
args
,
items
):
assignments
.
append
(
SingleAssignmentNode
(
pos
=
self
.
pos
,
lhs
=
lhs
,
rhs
=
rhs
,
first
=
self
.
first
))
all
=
ParallelAssignmentNode
(
pos
=
self
.
pos
,
stats
=
assignments
).
analyse_expressions
(
env
)
if
check_node
:
all
=
StatListNode
(
pos
=
self
.
pos
,
stats
=
[
check_node
,
all
])
for
ref
in
refs
:
all
=
UtilNodes
.
LetNode
(
ref
,
all
)
return
all
def
generate_rhs_evaluation_code
(
self
,
code
):
def
generate_rhs_evaluation_code
(
self
,
code
):
self
.
rhs
.
generate_evaluation_code
(
code
)
self
.
rhs
.
generate_evaluation_code
(
code
)
...
...
tests/run/arrayassign.pyx
View file @
b7a712bd
...
@@ -141,3 +141,12 @@ def test_ptr_literal_list_slice_end():
...
@@ -141,3 +141,12 @@ def test_ptr_literal_list_slice_end():
# cdef int a[5]
# cdef int a[5]
# a[:] = l
# a[:] = l
# return (a[0], a[1], a[2], a[3], a[4])
# return (a[0], a[1], a[2], a[3], a[4])
def
test_from_ptr
():
"""
>>> test_from_ptr()
(5, 4, 3)
"""
cdef
int
*
a
=
[
6
,
5
,
4
,
3
,
2
,
1
]
x
,
y
,
z
=
a
[
1
:
4
]
return
x
,
y
,
z
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment