diff --git a/debuginfo-tests/llvm-prettyprinters/gdb/mlir-support.cpp b/debuginfo-tests/llvm-prettyprinters/gdb/mlir-support.cpp --- a/debuginfo-tests/llvm-prettyprinters/gdb/mlir-support.cpp +++ b/debuginfo-tests/llvm-prettyprinters/gdb/mlir-support.cpp @@ -11,15 +11,31 @@ mlir::Value Value({reinterpret_cast(0x8), mlir::Value::Kind::TrailingOpResult}); -auto Location = mlir::FileLineColLoc::get("file", 1, 2, &Context); - -mlir::Attribute StringAttribute = mlir::StringAttr::get("foo", &Context); - mlir::Type Type; +mlir::Type IndexType = mlir::IndexType::get(&Context); mlir::Type IntegerType = mlir::IntegerType::get(3, mlir::IntegerType::Unsigned, &Context); mlir::Type FloatType = mlir::Float32Type::get(&Context); mlir::Type MemRefType = mlir::MemRefType::get({4, 5}, FloatType); mlir::Type UnrankedMemRefType = mlir::UnrankedMemRefType::get(IntegerType, 6); +mlir::Type VectorType = mlir::VectorType::get({1, 2}, FloatType); +mlir::Type TupleType = + mlir::TupleType::get(mlir::TypeRange({IndexType, FloatType}), &Context); + +auto UnknownLoc = mlir::UnknownLoc::get(&Context); +auto FileLineColLoc = mlir::FileLineColLoc::get("file", 7, 8, &Context); +auto OpaqueLoc = mlir::OpaqueLoc::get(9, &Context); +auto NameLoc = mlir::NameLoc::get(Identifier, &Context); +auto CallSiteLoc = mlir::CallSiteLoc::get(FileLineColLoc, OpaqueLoc); +auto FusedLoc = mlir::FusedLoc::get({FileLineColLoc, NameLoc}, &Context); + +mlir::Attribute UnitAttr = mlir::UnitAttr::get(&Context); +mlir::Attribute FloatAttr = mlir::FloatAttr::get(FloatType, 1.0); +mlir::Attribute IntegerAttr = mlir::IntegerAttr::get(IntegerType, 10); +mlir::Attribute TypeAttr = mlir::TypeAttr::get(IndexType); +mlir::Attribute ArrayAttr = mlir::ArrayAttr::get({}, &Context); +mlir::Attribute StringAttr = mlir::StringAttr::get("foo", &Context); +mlir::Attribute ElementsAttr = mlir::DenseElementsAttr::get( + VectorType.cast(), llvm::ArrayRef{2.0f, 3.0f}); int main() { return 0; } diff --git a/debuginfo-tests/llvm-prettyprinters/gdb/mlir-support.gdb b/debuginfo-tests/llvm-prettyprinters/gdb/mlir-support.gdb --- a/debuginfo-tests/llvm-prettyprinters/gdb/mlir-support.gdb +++ b/debuginfo-tests/llvm-prettyprinters/gdb/mlir-support.gdb @@ -10,35 +10,101 @@ # CHECK: "FooOp" p OperationName -# CHECK: mlir::FileLineColLoc -# CHECK: filename = "file", -# CHECK: line = 1, -# CHECK: column = 2 -p Location - -# CHECK: mlir::StringAttr -# CHECK: value = "foo" -p StringAttribute +# CHECK: 0x8 +# CHECK: TrailingOpResult +p Value # CHECK: impl = 0x0 p Type -# CHECK: mlir::IntegerType -# CHECK: width = 3, -# CHECK: mlir::IntegerType::Unsigned +# CHECK: cast +p IndexType + +# CHECK: cast +# CHECK: width = 3 +# CHECK: Unsigned p IntegerType -# CHECK: mlir::Float32Type +# CHECK: cast p FloatType -# CHECK: mlir::MemRefType -# CHECK: elementType = mlir::Float32Type -# CHECK: shape[0] = 4 -# CHECK: shape[1] = 5 -# CHECK: memorySpace = 0 +# CHECK: cast +# CHECK: shapeSize = 2 +# CHECK: shapeElements[0] = 4 +# CHECK: shapeElements[1] = 5 p MemRefType -# CHECK: mlir::UnrankedMemRefType +# CHECK: cast # CHECK: memorySpace = 6 p UnrankedMemRefType +# CHECK: cast +# CHECK: shapeSize = 2 +# CHECK: shapeElements[0] = 1 +# CHECK: shapeElements[1] = 2 +p VectorType + +# CHECK: cast +# CHECK: numElements = 2 +# CHECK: elements[0] +# CHECK: mlir::IndexType +# CHECK: elements[1] +# CHECK: mlir::Float32Type +p TupleType + +# CHECK: cast +p UnknownLoc + +# CHECK: cast +# CHECK: filename = "file", +# CHECK: line = 1, +# CHECK: column = 2 +p FileLineColLoc + +# CHECK: cast +# CHECK: underlyingLocation = 9 +p OpaqueLoc + +# CHECK: cast +# CHECK: name = "foo" +# CHECK: mlir::UnknownLoc +p NameLoc + +# CHECK: cast +# CHECK: callee +# CHECK: mlir::FileLineColLoc +# CHECK: caller +# CHECK: mlir::OpaqueLoc +p CallSiteLoc + +# CHECK: cast +# CHECK: numLocs = 2 +# CHECK: locs[0] +# CHECK: mlir::FileLineColLoc +# CHECK: locs[1] +# CHECK: mlir::NameLoc +p FusedLoc + +# CHECK: cast +p UnitAttr + +# CHECK: cast +p FloatAttr + +# CHECK: cast +p IntegerAttr + +# CHECK: cast +# CHECK: mlir::IndexType +p TypeAttr + +# CHECK: cast +# CHECK: llvm::ArrayRef of length 0 +p ArrayAttr + +# CHECK: cast +# CHECK: value = "foo" +p StringAttribute + +# CHECK: cast +p ElementsAttr diff --git a/mlir/utils/gdb-scripts/prettyprinters.py b/mlir/utils/gdb-scripts/prettyprinters.py --- a/mlir/utils/gdb-scripts/prettyprinters.py +++ b/mlir/utils/gdb-scripts/prettyprinters.py @@ -17,11 +17,8 @@ return 'string' -class StructPrinter: - """Prints bases of a struct and its fields. - - This mimics GDB's printing of instances without a custom printer. - """ +class StoragePrinter: + """Prints bases of a struct and its fields.""" def __init__(self, val): self.val = val @@ -34,86 +31,23 @@ yield (field.name, self.val[field.name]) -def get_default_or_struct_printer(val): - """Returns gdb.default_visualizer(val) or StructPrinter(val). - - A printer factory which forwards to a single instance field may want to always - return a valid printer. Otherwise, GDB will print the instance as raw, - defeating the purpose of forwarding. This function returns a printer whether - a custom printer for 'val' has been registered or not. - """ - default_printer = gdb.default_visualizer(val) - if default_printer: - return default_printer - # No custom printer available for val, fall back to StructPrinter. - return StructPrinter(val) - - -def get_operation_name_printer(val): - """Returns printer for an mlir::OperationName instance.""" - rep_printer = gdb.default_visualizer(val['representation']) - if not rep_printer: - return None # If the PointerUnion couldn't be analyzed, print as raw value. - pointer = rep_printer.pointer - if pointer.type.code == gdb.TYPE_CODE_PTR and pointer: - pointer = pointer.dereference() - return get_default_or_struct_printer(pointer) - - -def get_first_member_printer(val): - """Returns printer for the first member of val.""" - for field in val.type.fields(): - if not field.is_base_class: - field = val[field.name] - if field.type.code == gdb.TYPE_CODE_PTR and field: - field = field.dereference() - return get_default_or_struct_printer(field) - return None - - -def get_first_base_printer(val): - """Returns printer for the first base class of val.""" - for field in val.type.fields(): - if field.is_base_class and field.type: - return get_default_or_struct_printer(val.cast(field.type)) - return None - - -def get_storage_user_base_printer(val): - """Returns printer for an mlir::detail::StorageUserBase instance.""" - storage_type = val.type.template_argument(2) - if not val['impl']: - return None - storage_value = val['impl'].dereference().cast(storage_type) - return get_default_or_struct_printer(storage_value) - - -class IntegerAttributeStoragePrinter: - """Prints the raw bits of the APInteger.""" - - def __init__(self, val): - self.val = val +class TupleTypeStoragePrinter(StoragePrinter): def children(self): - base_type = self.val.type.fields()[0].type - yield ('<%s>' % base_type.name, self.val.cast(base_type)) - bits_type = gdb.lookup_type('uint64_t') - bits_ptr = (self.val.address + 1).cast(bits_type.pointer()) - for i in range(self.val['numObjects']): - yield ('bits[%u]' % i, (bits_ptr + i).dereference()) + for child in StoragePrinter.children(self): + yield child + elements = (self.val.address + 1).cast(gdb.lookup_type('mlir::Type*')) + for i in range(self.val['numElements']): + yield 'elements[%u]' % i, elements[i] -class RankedTypeStoragePrinter: - - def __init__(self, val): - self.val = val +class RankedTypeStoragePrinter(StoragePrinter): def children(self): - for field in self.val.type.fields(): - if field.is_base_class: - yield ('<%s>' % field.name, self.val.cast(field.type)) + for child in StoragePrinter.children(self): + yield child for i in range(self.val['shapeSize']): - yield ('shape[%u]' % i, self.val['shapeElements'][i]) + yield 'shapeElements[%u]' % i, self.val['shapeElements'][i] class MemRefTypeStoragePrinter(RankedTypeStoragePrinter): @@ -122,8 +56,28 @@ for child in RankedTypeStoragePrinter.children(self): yield child for i in range(self.val['numAffineMaps']): - yield ('affineMap[%u]' % i, self.val['affineMapsList'][i]) - yield ('memorySpace', self.val['memorySpace']) + yield 'affineMapsList[%u]' % i, self.val['affineMapsList'][i] + + +class FusedLocationStoragePrinter(StoragePrinter): + + def children(self): + for child in StoragePrinter.children(self): + yield child + elements = (self.val.address + 1).cast(gdb.lookup_type('mlir::Location*')) + for i in range(self.val['numLocs']): + yield 'locs[%u]' % i, elements[i] + + +class StorageUserBasePrinter: + """Printer for an mlir::detail::StorageUserBase instance.""" + + def __init__(self, val): + self.val = val + + def children(self): + storage_type = self.val.type.template_argument(2) + yield 'impl', self.val['impl'].dereference().cast(storage_type) class StorageTypeMap: @@ -162,58 +116,51 @@ storage_type_map = StorageTypeMap() -def get_abstract_base_printer(val, get_type_id): - """Creates a printer of the concrete storage type.""" - if not val['impl']: - return None - attr_storage = val['impl'].dereference() - type_id = get_type_id(attr_storage) - concrete_type = storage_type_map[type_id] - if concrete_type: - return get_default_or_struct_printer(val.cast(concrete_type)) - return None +def get_type_id_printer(val): + """Returns a printer of the name of a mlir::TypeID.""" + class StringPrinter: -class AbstractStoragePrinter: - """Prints the name of the concrete type.""" + def __init__(self, string): + self.string = string - def __init__(self, val, get_type_id): - type_id = get_type_id(val) - concrete_type = storage_type_map[type_id] - self.type_name = concrete_type.name if concrete_type else '' + def to_string(self): + return self.string - def to_string(self): - return self.type_name + concrete_type = storage_type_map[val] + if not concrete_type: + return None + return StringPrinter('"%s"' % concrete_type.name) -pp = gdb.printing.RegexpCollectionPrettyPrinter('MLIRSupport') +def get_attr_or_type_printer(val, get_type_id): + """Returns a printer for mlir::Attribute or mlir::Type.""" -pp.add_printer('mlir::Identifier', '^mlir::Identifier$', IdentifierPrinter) -pp.add_printer('mlir::OperationName', '^mlir::OperationName$', - get_operation_name_printer) + class UpcastPrinter: -# Forwarding printers to only relevant field. -for name in ['Location', 'Value']: - pp.add_printer('mlir::%s' % name, '^mlir::%s$' % name, - get_first_member_printer) + def __init__(self, val, type): + self.val = val.cast(type) -# Forwarding printers to the only relevant base class. -for name in [ - 'BoolAttr', 'LocationAttr', 'ElementsAttr', 'BaseMemRefType', 'FloatType', - 'ShapedType', 'TensorType' -]: - pp.add_printer('mlir::%s' % name, '^mlir::%s$' % name, get_first_base_printer) + def children(self): + yield 'cast<%s>' % self.val.type.name, self.val -pp.add_printer('mlir::detail::StorageUserBase', - '^mlir::detail::StorageUserBase<.*>$', - get_storage_user_base_printer) + if not val['impl']: + return None + type_id = get_type_id(val['impl'].dereference()) + concrete_type = storage_type_map[type_id] + if not concrete_type: + return None + return UpcastPrinter(val, concrete_type) -pp.add_printer('mlir::detail::IntegerAttributeStorage', - '^mlir::detail::IntegerAttributeStorage$', - IntegerAttributeStoragePrinter) -pp.add_printer('mlir::detail::ShapedTypeStorage', - '^mlir::detail::ShapedTypeStorage$', StructPrinter) +pp = gdb.printing.RegexpCollectionPrettyPrinter('MLIRSupport') + +pp.add_printer('mlir::Identifier', '^mlir::Identifier$', IdentifierPrinter) + +# Printers for types deriving from AttributeStorage or TypeStorage. +pp.add_printer('mlir::detail::FusedLocationStorage', + '^mlir::detail::FusedLocationStorage', + FusedLocationStoragePrinter) pp.add_printer('mlir::detail::VectorTypeStorage', '^mlir::detail::VectorTypeStorage', RankedTypeStoragePrinter) pp.add_printer('mlir::detail::RankedTensorTypeStorage', @@ -221,8 +168,14 @@ RankedTypeStoragePrinter) pp.add_printer('mlir::detail::MemRefTypeStorage', '^mlir::detail::MemRefTypeStorage$', MemRefTypeStoragePrinter) +pp.add_printer('mlir::detail::TupleTypeStorage', + '^mlir::detail::TupleTypeStorage$', TupleTypeStoragePrinter) -# Printers of types deriving from mlir::StorageUserBase. +# Printers for Attribute::AttrBase or Type::TypeBase typedefs. +pp.add_printer('mlir::detail::StorageUserBase', + '^mlir::detail::StorageUserBase<.*>$', StorageUserBasePrinter) + +# Printers of types deriving from Attribute::AttrBase or Type::TypeBase. for name in [ # mlir/IR/Attributes.h 'ArrayAttr', @@ -262,19 +215,21 @@ 'UnknownLoc' ]: storage_type_map.register_type('mlir::%s' % name) # Register for upcasting. - pp.add_printer('mlir::%s' % name, '^mlir::%s$' % name, get_first_base_printer) + +pp.add_printer('mlir::TypeID', '^mlir::TypeID$', get_type_id_printer) -def add_abstract_printers(name): +def add_attr_or_type_printers(name): + """Adds printers for mlir::Attribute or mlir::Type and their Storage type.""" get_type_id = lambda val: val['abstract%s' % name]['typeID'] pp.add_printer('mlir::%s' % name, '^mlir::%s$' % name, - lambda val: get_abstract_base_printer(val, get_type_id)) + lambda val: get_attr_or_type_printer(val, get_type_id)) pp.add_printer('mlir::%sStorage' % name, '^mlir::%sStorage$' % name, - lambda val: AbstractStoragePrinter(val, get_type_id)) + lambda val: get_type_id_printer(get_type_id(val))) # Upcasting printers of mlir::Attribute and mlir::Type. for name in ['Attribute', 'Type']: - add_abstract_printers(name) + add_attr_or_type_printers(name) gdb.printing.register_pretty_printer(gdb.current_objfile(), pp)