Skip to content

[GDB][NFC] Introduce auto-formatting with black and fix a typo #5187

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

Merged
merged 2 commits into from
Jan 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 81 additions & 54 deletions sycl/gdb/libsycl.so-gdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### XMethod implementations ###


class Accessor:
"""Generalized base class for buffer index calculation"""

Expand All @@ -31,15 +32,17 @@ def index(self, arg):
return int(arg)
# unwrap if inside item
try:
arg = arg['MImpl']['MIndex']
arg = arg["MImpl"]["MIndex"]
except:
pass
# https://github.com/intel/llvm/blob/97272b7ebd569bfa13811913a31e30f926559217/sycl/include/CL/sycl/accessor.hpp#L678-L690
result = 0
for dim in range(self.depth):
result = result * self.memory_range(dim) + \
self.offset(dim) + \
arg['common_array'][dim]
result = (
result * self.memory_range(dim)
+ self.offset(dim)
+ arg["common_array"][dim]
)
return result

def value(self, arg):
Expand All @@ -50,16 +53,17 @@ class HostAccessor(Accessor):
"""For Host device memory layout"""

def payload(self):
return self.obj['impl']['_M_ptr'].dereference()
return self.obj["impl"]["_M_ptr"].dereference()

def memory_range(self, dim):
return self.payload()['MMemoryRange']['common_array'][dim]
return self.payload()["MMemoryRange"]["common_array"][dim]

def offset(self, dim):
return self.payload()['MOffset']['common_array'][dim]
return self.payload()["MOffset"]["common_array"][dim]

def data(self):
return self.payload()['MData']
return self.payload()["MData"]


class HostAccessorLocal(HostAccessor):
"""For Host device memory layout"""
Expand All @@ -68,27 +72,29 @@ def index(self, arg):
if arg.type.code == gdb.TYPE_CODE_INT:
return int(arg)
# https://github.com/intel/llvm/blob/97272b7ebd569bfa13811913a31e30f926559217/sycl/include/CL/sycl/accessor.hpp#L1049-L1053
result = 0;
result = 0
for dim in range(self.depth):
result = result * \
self.payload()['MSize']['common_array'][dim] + \
arg['common_array'][dim];
return result;
result = (
result * self.payload()["MSize"]["common_array"][dim]
+ arg["common_array"][dim]
)
return result

def data(self):
return self.payload()['MMem']
return self.payload()["MMem"]


class DeviceAccessor(Accessor):
"""For CPU/GPU memory layout"""

def memory_range(self, dim):
return self.obj['impl']['MemRange']['common_array'][dim]
return self.obj["impl"]["MemRange"]["common_array"][dim]

def offset(self, dim):
return self.obj['impl']['Offset']['common_array'][dim]
return self.obj["impl"]["Offset"]["common_array"][dim]

def data(self):
return self.obj['MData']
return self.obj["MData"]


class AccessorOpIndex(gdb.xmethod.XMethodWorker):
Expand All @@ -106,12 +112,12 @@ def get_result_type(self, *args):
return self.result_type

def __call__(self, obj, arg):
# No way to wasily figure out which devices is currently being used,
# No way to easily figure out which devices is currently being used,
# try all accessor implementations until one of them works:
accessors = [
DeviceAccessor(obj, self.result_type, self.depth),
HostAccessor(obj, self.result_type, self.depth),
HostAccessorLocal(obj, self.result_type, self.depth)
HostAccessorLocal(obj, self.result_type, self.depth),
]
for accessor in accessors:
try:
Expand All @@ -129,39 +135,41 @@ class AccessorOpIndex1D(AccessorOpIndex):

def get_arg_types(self):
assert self.depth == 1
return gdb.lookup_type('size_t')
return gdb.lookup_type("size_t")


class AccessorOpIndexItemTrue(AccessorOpIndex):
"""Introduces an extra overload for item wrapper"""

def get_arg_types(self):
return gdb.lookup_type("cl::sycl::item<%s, true>" % self.depth)


class AccessorOpIndexItemFalse(AccessorOpIndex):
"""Introduces an extra overload for item wrapper"""

def get_arg_types(self):
return gdb.lookup_type("cl::sycl::item<%s, false>" % self.depth)


class AccessorMatcher(gdb.xmethod.XMethodMatcher):
"""Entry point for cl::sycl::accessor"""

def __init__(self):
gdb.xmethod.XMethodMatcher.__init__(self, 'AccessorMatcher')
gdb.xmethod.XMethodMatcher.__init__(self, "AccessorMatcher")

def match(self, class_type, method_name):
if method_name != 'operator[]':
if method_name != "operator[]":
return None

result = re.match('^cl::sycl::accessor<.+>$', class_type.tag)
result = re.match("^cl::sycl::accessor<.+>$", class_type.tag)
if result is None:
return None

depth = int(class_type.template_argument(1))
result_type = class_type.template_argument(0)

methods = [
AccessorOpIndex(class_type, result_type, depth)
]
methods = [AccessorOpIndex(class_type, result_type, depth)]
try:
method = AccessorOpIndexItemTrue(class_type, result_type, depth)
method.get_arg_types()
Expand All @@ -178,38 +186,49 @@ def match(self, class_type, method_name):
methods.append(AccessorOpIndex1D(class_type, result_type, depth))
return methods


class PrivateMemoryOpCall(gdb.xmethod.XMethodWorker):
"""Provides operator() overload for h_item argument"""

class ItemBase:
"""Wrapper for cl::sycl::detail::ItemBase which reimplements index calculation"""

def __init__(self, obj, ):
result = re.match('^cl::sycl::detail::ItemBase<(.+), (.+)>$', str(obj.type))
def __init__(
self,
obj,
):
result = re.match("^cl::sycl::detail::ItemBase<(.+), (.+)>$", str(obj.type))
self.dim = int(result[1])
self.with_offset = (result[2] == 'true')
self.with_offset = result[2] == "true"
self.obj = obj

def get_linear_id(self):
index = self.obj['MIndex']['common_array']
extent = self.obj['MExtent']['common_array']
index = self.obj["MIndex"]["common_array"]
extent = self.obj["MExtent"]["common_array"]

if self.with_offset:
offset = self.obj['MOffset']['common_array']
offset = self.obj["MOffset"]["common_array"]
if self.dim == 1:
return index[0] - offset[0]
elif self.dim == 2:
return (index[0] - offset[0]) * extent[1] + (index[1] - offset[1])
else:
return ((index[0] - offset[0]) * extent[1] * extent[2]) + \
((index[1] - offset[1]) * extent[2]) + (index[2] - offset[2])
return (
((index[0] - offset[0]) * extent[1] * extent[2])
+ ((index[1] - offset[1]) * extent[2])
+ (index[2] - offset[2])
)
else:
if self.dim == 1:
return index[0]
elif self.dim == 2:
return index[0] * extent[1] + index[1]
else:
return (index[0] * extent[1] * extent[2]) + (index[1] * extent[2]) + index[2]
return (
(index[0] * extent[1] * extent[2])
+ (index[1] * extent[2])
+ index[2]
)

def __init__(self, result_type, dim):
self.result_type = result_type
Expand All @@ -222,38 +241,41 @@ def get_result_type(self, *args):
return self.result_type

def __call__(self, obj, *args):
if obj['Val'].type.tag.endswith(self.result_type):
if obj["Val"].type.tag.endswith(self.result_type):
# On device private_memory is a simple wrapper over actual value
return obj['Val']
return obj["Val"]
else:
# On host it wraps a unique_ptr to an array of items
item_base = args[0]['localItem']['MImpl']
item_base = args[0]["localItem"]["MImpl"]
item_base = self.ItemBase(item_base)
index = item_base.get_linear_id()
return obj['Val']['_M_t']['_M_t']['_M_head_impl'][index]
return obj["Val"]["_M_t"]["_M_t"]["_M_head_impl"][index]


class PrivateMemoryMatcher(gdb.xmethod.XMethodMatcher):
"""Entry point for cl::sycl::private_memory"""

def __init__(self):
gdb.xmethod.XMethodMatcher.__init__(self, 'PrivateMemoryMatcher')
gdb.xmethod.XMethodMatcher.__init__(self, "PrivateMemoryMatcher")

def match(self, class_type, method_name):
if method_name != 'operator()':
if method_name != "operator()":
return None

result = re.match('^cl::sycl::private_memory<((cl::)?(sycl::)?id<.+>), (.+)>$', class_type.tag)
result = re.match(
"^cl::sycl::private_memory<((cl::)?(sycl::)?id<.+>), (.+)>$", class_type.tag
)
if result is None:
return None
return PrivateMemoryOpCall(result[1], result[4])



gdb.xmethod.register_xmethod_matcher(None, AccessorMatcher(), replace=True)
gdb.xmethod.register_xmethod_matcher(None, PrivateMemoryMatcher(), replace=True)

### Pretty-printer implementations ###


class SyclArrayPrinter:
"""Print an object deriving from cl::sycl::detail::array"""

Expand All @@ -275,7 +297,7 @@ def __next__(self):
elt = self.data[count]
except:
elt = "<error reading variable>"
return ('[%d]' % count, elt)
return ("[%d]" % count, elt)

def __init__(self, value):
if value.type.code == gdb.TYPE_CODE_REF:
Expand All @@ -288,7 +310,7 @@ def __init__(self, value):

def children(self):
try:
return self.ElementIterator(self.value['common_array'], self.dimensions)
return self.ElementIterator(self.value["common_array"], self.dimensions)
except:
# There is no way to return an error from this method. Return an
# empty iterable to make GDB happy and rely on to_string method
Expand All @@ -300,13 +322,14 @@ def to_string(self):
# Check if accessing array value will succeed and resort to
# error message otherwise. Individual array element access failures
# will be caught by iterator itself.
_ = self.value['common_array']
_ = self.value["common_array"]
return self.type.tag
except:
return "<error reading variable>"

def display_hint(self):
return 'array'
return "array"


class SyclBufferPrinter:
"""Print a cl::sycl::buffer"""
Expand All @@ -316,18 +339,22 @@ def __init__(self, value):
self.type = value.type.unqualified().strip_typedefs()
self.elt_type = value.type.template_argument(0)
self.dimensions = value.type.template_argument(1)
self.typeregex = re.compile('^([a-zA-Z0-9_:]+)(<.*>)?$')
self.typeregex = re.compile("^([a-zA-Z0-9_:]+)(<.*>)?$")

def to_string(self):
match = self.typeregex.match(self.type.tag)
if not match:
return "<error parsing type>"
return ('%s<%s, %s> = {impl=%s}'
% (match.group(1), self.elt_type, self.dimensions,
self.value['impl'].address))
return "%s<%s, %s> = {impl=%s}" % (
match.group(1),
self.elt_type,
self.dimensions,
self.value["impl"].address,
)


sycl_printer = gdb.printing.RegexpCollectionPrettyPrinter("SYCL")
sycl_printer.add_printer("cl::sycl::id", '^cl::sycl::id<.*$', SyclArrayPrinter)
sycl_printer.add_printer("cl::sycl::range", '^cl::sycl::range<.*$', SyclArrayPrinter)
sycl_printer.add_printer("cl::sycl::buffer", '^cl::sycl::buffer<.*$', SyclBufferPrinter)
sycl_printer.add_printer("cl::sycl::id", "^cl::sycl::id<.*$", SyclArrayPrinter)
sycl_printer.add_printer("cl::sycl::range", "^cl::sycl::range<.*$", SyclArrayPrinter)
sycl_printer.add_printer("cl::sycl::buffer", "^cl::sycl::buffer<.*$", SyclBufferPrinter)
gdb.printing.register_pretty_printer(None, sycl_printer, True)