Skip to content

[SYCL] Move pretty-printers from the GDB repository #2174

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 1 commit into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion sycl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ add_subdirectory( source )
# Auxilliary extras for SYCL headers/library
if (NOT WIN32)
install(FILES
"${CMAKE_CURRENT_SOURCE_DIR}/xmethods/libsycl.so-gdb.py"
"${CMAKE_CURRENT_SOURCE_DIR}/gdb/libsycl.so-gdb.py"
RENAME "libsycl.so.${SYCL_VERSION_STRING}-gdb.py"
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/"
COMPONENT sycl-headers-extras)
Expand Down
88 changes: 88 additions & 0 deletions sycl/xmethods/libsycl.so-gdb.py → sycl/gdb/libsycl.so-gdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

import gdb
import gdb.xmethod
import gdb.printing
import itertools
import re

### XMethod implementations ###

"""
Generalized base class for buffer index calculation
"""
Expand Down Expand Up @@ -134,3 +139,86 @@ def match(self, class_type, method_name):


gdb.xmethod.register_xmethod_matcher(None, AccessorOpIndexMatcher(), replace=True)

### Pretty-printer implementations ###

"""
Print an object deriving from cl::sycl::detail::array
"""
class SyclArrayPrinter:
class ElementIterator:
def __init__(self, data, size):
self.data = data
self.size = size
self.count = 0

def __iter__(self):
return self

def __next__(self):
if self.count == self.size:
raise StopIteration
count = self.count
self.count = self.count + 1
try:
elt = self.data[count]
except:
elt = "<error reading variable>"
return ('[%d]' % count, elt)

def __init__(self, value):
if value.type.code == gdb.TYPE_CODE_REF:
if hasattr(gdb.Value,"referenced_value"):
value = value.referenced_value()

self.value = value
self.type = value.type.unqualified().strip_typedefs()
self.dimensions = self.type.template_argument(0)

def children(self):
try:
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
# to take care of formatting.
return [ ]

def to_string(self):
try:
# 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']
return self.type.tag
except:
return "<error reading variable>"

def display_hint(self):
return 'array'

"""
Print a cl::sycl::buffer
"""
class SyclBufferPrinter:
def __init__(self, value):
self.value = 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_:]+)(<.*>)?$')

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))

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)
gdb.printing.register_pretty_printer(None, sycl_printer, True)

File renamed without changes.
19 changes: 19 additions & 0 deletions sycl/test/gdb/printers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %clangxx -c -fno-color-diagnostics -I %sycl_include -Xclang -ast-dump %s | FileCheck %s
// UNSUPPORTED: windows
#include <CL/sycl/buffer.hpp>
#include <CL/sycl/detail/array.hpp>

typedef cl::sycl::id<1> dummy_id;
typedef cl::sycl::buffer<int> dummy_buffer;

// array must have common_array field

// CHECK: CXXRecordDecl {{.*}} class array definition
// CHECK-NOT: CXXRecordDecl {{.*}} definition
// CHECK: FieldDecl {{.*}} referenced common_array

// buffer must have impl field

// CHECK: CXXRecordDecl {{.*}} class buffer definition
// CHECK-NOT: CXXRecordDecl {{.*}} definition
// CHECK: FieldDecl {{.*}} referenced impl