-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
compiler: Improve quality of generated code #2263
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a73e497
compiler: Enhance rcompile
FabioLuporini 5023983
compiler: Add IET type KernelLaunch
FabioLuporini 864cddc
compiler: Increase granularity of abstract_object
FabioLuporini 9825006
compiler: Improve Cast to perform automatic simplification
FabioLuporini 18f3098
tests: Unpickle won't unpick factorization
FabioLuporini ecf07c4
compiler: Simplify asyncs lowering
FabioLuporini 4af722f
compiler: Add UCHAR and UCHARP for unsigned char casts
FabioLuporini 71a3a23
compiler: Add CallFromComposite
FabioLuporini 2c6c80c
compiler: Enhance automated routine abstraction
FabioLuporini 65bc3ef
compiler: Avoid useless alignments
FabioLuporini 56f485e
compiler: Add blank lines where appropriate
FabioLuporini cbe9448
compiler: Trim Section code gen
FabioLuporini a463e4e
compiler: Extend Uxreplace
FabioLuporini 1cc6a0a
compiler: Abstract semantically identical compounds
FabioLuporini 720b50e
compiler: Abstract orchestration routines
FabioLuporini 4590a42
examples: Patch notebook
FabioLuporini 18fe96f
tests: Update expected output for omp offloading
FabioLuporini 4dfc228
tests: Remove obsolete test
FabioLuporini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,8 @@ | |
from devito.ir.support.space import Backward | ||
from devito.symbolics import ListInitializer, ccode, uxreplace | ||
from devito.tools import (GenericVisitor, as_tuple, ctypes_to_cstr, filter_ordered, | ||
filter_sorted, flatten, is_external_ctype, c_restrict_void_p) | ||
filter_sorted, flatten, is_external_ctype, | ||
c_restrict_void_p, sorted_priority) | ||
from devito.types.basic import AbstractFunction, Basic | ||
from devito.types import (ArrayObject, CompositeObject, Dimension, Pointer, | ||
IndexedData, DeviceMap) | ||
|
@@ -224,7 +225,7 @@ def _gen_struct_decl(self, obj, masked=()): | |
|
||
def _gen_value(self, obj, level=2, masked=()): | ||
qualifiers = [v for k, v in self._qualifiers_mapper.items() | ||
if getattr(obj, k, False) and v not in masked] | ||
if getattr(obj.function, k, False) and v not in masked] | ||
|
||
if (obj._mem_stack or obj._mem_constant) and level == 2: | ||
strtype = obj._C_typedata | ||
|
@@ -233,7 +234,8 @@ def _gen_value(self, obj, level=2, masked=()): | |
strtype = ctypes_to_cstr(obj._C_ctype) | ||
strshape = '' | ||
if isinstance(obj, (AbstractFunction, IndexedData)) and level >= 1: | ||
strtype = '%s%s' % (strtype, self._restrict_keyword) | ||
if not obj._mem_stack: | ||
strtype = '%s%s' % (strtype, self._restrict_keyword) | ||
strtype = ' '.join(qualifiers + [strtype]) | ||
|
||
strname = obj._C_name | ||
|
@@ -324,7 +326,9 @@ def _blankline_logic(self, children): | |
prev is ExpressionBundle and | ||
all(i.dim.is_Stencil for i in g)): | ||
rebuilt.extend(g) | ||
elif prev in candidates and k in candidates: | ||
elif (prev in candidates and k in candidates) or \ | ||
(prev is not None and k is Section) or \ | ||
(prev is Section): | ||
rebuilt.append(BlankLine) | ||
rebuilt.extend(g) | ||
else: | ||
|
@@ -375,7 +379,7 @@ def visit_PointerCast(self, o): | |
else: | ||
rshape = '*' | ||
lvalue = c.Value(i._C_typedata, '*%s' % v) | ||
if o.alignment: | ||
if o.alignment and f._data_alignment: | ||
lvalue = c.AlignedAttribute(f._data_alignment, lvalue) | ||
|
||
# rvalue | ||
|
@@ -406,15 +410,13 @@ def visit_Dereference(self, o): | |
shape = ''.join("[%s]" % ccode(i) for i in a0.symbolic_shape[1:]) | ||
rvalue = '(%s (*)%s) %s[%s]' % (i._C_typedata, shape, a1.name, | ||
a1.dim.name) | ||
lvalue = c.AlignedAttribute( | ||
a0._data_alignment, | ||
c.Value(i._C_typedata, '(*restrict %s)%s' % (a0.name, shape)) | ||
) | ||
lvalue = c.Value(i._C_typedata, | ||
'(*restrict %s)%s' % (a0.name, shape)) | ||
else: | ||
rvalue = '(%s *) %s[%s]' % (i._C_typedata, a1.name, a1.dim.name) | ||
lvalue = c.AlignedAttribute( | ||
a0._data_alignment, c.Value(i._C_typedata, '*restrict %s' % a0.name) | ||
) | ||
lvalue = c.Value(i._C_typedata, '*restrict %s' % a0.name) | ||
if a0._data_alignment: | ||
lvalue = c.AlignedAttribute(a0._data_alignment, lvalue) | ||
else: | ||
rvalue = '%s->%s' % (a1.name, a0._C_name) | ||
lvalue = self._gen_value(a0, 0) | ||
|
@@ -430,13 +432,7 @@ def visit_List(self, o): | |
|
||
def visit_Section(self, o): | ||
body = flatten(self._visit(i) for i in o.children) | ||
if o.is_subsection: | ||
header = [] | ||
footer = [] | ||
else: | ||
header = [c.Comment("Begin %s" % o.name)] | ||
footer = [c.Comment("End %s" % o.name)] | ||
return c.Module(header + body + footer) | ||
return c.Module(body) | ||
|
||
def visit_Return(self, o): | ||
v = 'return' | ||
|
@@ -576,6 +572,19 @@ def visit_HaloSpot(self, o): | |
body = flatten(self._visit(i) for i in o.children) | ||
return c.Collection(body) | ||
|
||
def visit_KernelLaunch(self, o): | ||
arguments = self._args_call(o.arguments) | ||
arguments = ','.join(arguments) | ||
|
||
launch_args = [o.grid, o.block] | ||
if o.shm is not None: | ||
launch_args.append(o.shm) | ||
if o.stream is not None: | ||
launch_args.append(o.stream) | ||
launch_config = ','.join(str(i) for i in launch_args) | ||
|
||
return c.Statement('%s<<<%s>>>(%s)' % (o.name, launch_config, arguments)) | ||
|
||
# Operator-handle machinery | ||
|
||
def _operator_includes(self, o): | ||
|
@@ -625,10 +634,10 @@ def visit_Operator(self, o, mode='all'): | |
# Elemental functions | ||
esigns = [] | ||
efuncs = [blankline] | ||
for i in o._func_table.values(): | ||
if i.local: | ||
esigns.append(self._gen_signature(i.root)) | ||
efuncs.extend([self._visit(i.root), blankline]) | ||
items = [i.root for i in o._func_table.values() if i.local] | ||
for i in sorted_efuncs(items): | ||
esigns.append(self._gen_signature(i)) | ||
efuncs.extend([self._visit(i), blankline]) | ||
|
||
# Definitions | ||
headers = [c.Define(*i) for i in o._headers] + [blankline] | ||
|
@@ -1177,13 +1186,19 @@ def visit_Conditional(self, o): | |
condition = uxreplace(o.condition, self.mapper) | ||
then_body = self._visit(o.then_body) | ||
else_body = self._visit(o.else_body) | ||
return o._rebuild(condition=condition, then_body=then_body, else_body=else_body) | ||
return o._rebuild(condition=condition, then_body=then_body, | ||
else_body=else_body) | ||
|
||
def visit_PointerCast(self, o): | ||
function = self.mapper.get(o.function, o.function) | ||
obj = self.mapper.get(o.obj, o.obj) | ||
return o._rebuild(function=function, obj=obj) | ||
|
||
def visit_Dereference(self, o): | ||
pointee = self.mapper.get(o.pointee, o.pointee) | ||
pointer = self.mapper.get(o.pointer, o.pointer) | ||
return o._rebuild(pointee=pointee, pointer=pointer) | ||
|
||
def visit_Pragma(self, o): | ||
arguments = [uxreplace(i, self.mapper) for i in o.arguments] | ||
return o._rebuild(arguments=arguments) | ||
|
@@ -1200,8 +1215,21 @@ def visit_HaloSpot(self, o): | |
body = self._visit(o.body) | ||
return o._rebuild(halo_scheme=halo_scheme, body=body) | ||
|
||
def visit_While(self, o, **kwargs): | ||
condition = uxreplace(o.condition, self.mapper) | ||
body = self._visit(o.body) | ||
return o._rebuild(condition=condition, body=body) | ||
|
||
visit_ThreadedProdder = visit_Call | ||
|
||
def visit_KernelLaunch(self, o): | ||
arguments = [uxreplace(i, self.mapper) for i in o.arguments] | ||
grid = self.mapper.get(o.grid, o.grid) | ||
block = self.mapper.get(o.block, o.block) | ||
stream = self.mapper.get(o.stream, o.stream) | ||
return o._rebuild(grid=grid, block=block, stream=stream, | ||
arguments=arguments) | ||
|
||
|
||
# Utils | ||
|
||
|
@@ -1253,3 +1281,16 @@ def generate(self): | |
if self.cast: | ||
tip = '(%s)%s' % (self.cast, tip) | ||
yield tip | ||
|
||
|
||
def sorted_efuncs(efuncs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be simpler to define that in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. circular import, can't import efunc from visitors |
||
from devito.ir.iet.efunc import (CommCallable, DeviceFunction, | ||
ThreadCallable, ElementalFunction) | ||
|
||
priority = { | ||
DeviceFunction: 3, | ||
ThreadCallable: 2, | ||
ElementalFunction: 1, | ||
CommCallable: 1 | ||
} | ||
return sorted_priority(efuncs, priority) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not local?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because this one really needs to be on the host as it will ultimately be accessed in user-land
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's not exceptionally elegant, admittedly