Skip to content

Commit

Permalink
update gdb-ginkgo
Browse files Browse the repository at this point in the history
Co-authored-by: Marcel Koch <marcel.koch@kit.edu>
  • Loading branch information
yhmtsai and MarcelKoch committed Oct 23, 2024
1 parent a9c8886 commit 2ab1acd
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions dev_tools/scripts/gdb-ginkgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def next(self):

_versioned_namespace = '__8::'


# new version adapted from https://gcc.gnu.org/pipermail/gcc-cvs/2021-November/356230.html
# necessary due to empty class optimization
def is_specialization_of(x, template_name):
Expand All @@ -64,6 +65,7 @@ def is_specialization_of(x, template_name):
expr = '^std::{}<.*>$'.format(template_name)
return re.match(expr, x) is not None


def get_template_arg_list(type_obj):
"Return a type's template arguments as a list"
n = 0
Expand All @@ -75,6 +77,7 @@ def get_template_arg_list(type_obj):
return template_args
n += 1


def _tuple_impl_get(val):
"Return the tuple element stored in a _Tuple_impl<N, T> base class."
bases = val.type.fields()
Expand All @@ -95,6 +98,7 @@ def _tuple_impl_get(val):
else:
raise ValueError("Unsupported implementation for std::tuple: %s" % str(val.type))


def tuple_get(n, val):
"Return the result of std::get<n>(val) on a std::tuple"
tuple_size = len(get_template_arg_list(val.type))
Expand All @@ -108,6 +112,7 @@ def tuple_get(n, val):
n -= 1
return _tuple_impl_get(node)


def get_unique_ptr_data_ptr(val):
"Return the result of val.get() on a std::unique_ptr"
# std::unique_ptr<T, D> contains a std::tuple<D::pointer, D>,
Expand Down Expand Up @@ -220,12 +225,28 @@ def display_hint(self):
return 'array'


class GkoHalfPrinter:
"Print a gko::half"

def __init__(self, val):
# GDB doesn't seem to consider the user-defined conversion in its Value.cast,
# so we need to call the conversion operator explicitly
address = hex(val.address)
self.float_val = gdb.parse_and_eval(f"reinterpret_cast<gko::half*>({address})->operator float()")

def to_string(self):
self.float_val.fetch_lazy()
return self.float_val


def lookup_type(val):
if not str(val.type.unqualified()).startswith('gko::'):
return None
suffix = str(val.type.unqualified())[5:]
if suffix.startswith('array<') and val.type.code == gdb.TYPE_CODE_STRUCT:
return GkoArrayPrinter(val)
if suffix.startswith("half") and val.type.code == gdb.TYPE_CODE_STRUCT:
return GkoHalfPrinter(val)
return None


Expand Down

0 comments on commit 2ab1acd

Please sign in to comment.