llvm-project

Форк
0
/
mlirDataFormatters.py 
832 строки · 28.8 Кб
1
"""
2
LLDB Formatters for MLIR data types.
3

4
Load into LLDB with 'command script import /path/to/mlirDataFormatters.py'
5
"""
6

7
import re
8
import lldb
9

10

11
def get_expression_path(val: lldb.SBValue):
12
    """Compute the expression path for the given value."""
13

14
    stream = lldb.SBStream()
15
    if not val.GetExpressionPath(stream):
16
        return None
17
    return stream.GetData()
18

19

20
def build_ptr_str_from_addr(addrValue: lldb.SBValue, type: lldb.SBType):
21
    """Build a string that computes a pointer using the given address value and type."""
22

23
    if type.is_reference:
24
        type = type.GetDereferencedType()
25
    if not type.is_pointer:
26
        type = type.GetPointerType()
27
    return f"(({type}){addrValue.GetData().GetUnsignedInt64(lldb.SBError(), 0)})"
28

29

30
# ===----------------------------------------------------------------------=== #
31
# Attributes and Types
32
# ===----------------------------------------------------------------------=== #
33

34
# This variable defines various mnemonic strings for use by the builtin
35
# dialect attributes and types, which often have special formatting within
36
# the parser/printer.
37
builtin_attr_type_mnemonics = {
38
    "mlir::AffineMapAttr": '"affine_map<...>"',
39
    "mlir::ArrayAttr": '"[...]"',
40
    "mlir::DenseArray": '"array<...>"',
41
    "mlir::DenseResourceElementsAttr": '"dense_resource<...>"',
42
    "mlir::DictionaryAttr": '"{...}"',
43
    "mlir::IntegerAttr": '"float"',
44
    "mlir::IntegerAttr": '"integer"',
45
    "mlir::IntegerSetAttr": '"affine_set<...>"',
46
    "mlir::SparseElementsAttr": '"sparse<...>"',
47
    "mlir::StringAttr": '""...""',
48
    "mlir::StridedLayout": '"strided_layout"',
49
    "mlir::UnitAttr": '"unit"',
50
    "mlir::CallSiteLoc": '"loc(callsite(...))"',
51
    "mlir::FusedLoc": '"loc(fused<...>[...])"',
52
    "mlir::UnknownLoc": '"loc(unknown)"',
53
    "mlir::Float8E5M2Type": '"f8E5M2"',
54
    "mlir::Float8E4M3FNType": '"f8E4M3FN"',
55
    "mlir::Float8E5M2FNUZType": '"f8E5M2FNUZ"',
56
    "mlir::Float8E4M3FNUZType": '"f8E4M3FNUZ"',
57
    "mlir::Float8E4M3B11FNUZType": '"f8E4M3B11FNUZ"',
58
    "mlir::BFloat16Type": '"bf16"',
59
    "mlir::Float16Type": '"f16"',
60
    "mlir::FloatTF32Type": '"tf32"',
61
    "mlir::Float32Type": '"f32"',
62
    "mlir::Float64Type": '"f64"',
63
    "mlir::Float80Type": '"f80"',
64
    "mlir::Float128Type": '"f128"',
65
    "mlir::FunctionType": '"(...) -> (...)"',
66
    "mlir::IndexType": '"index"',
67
    "mlir::IntegerType": '"iN"',
68
    "mlir::NoneType": '"none"',
69
    "mlir::TupleType": '"tuple<...>"',
70
    "mlir::MemRefType": '"memref<...>"',
71
    "mlir::UnrankedMemRef": '"memref<...>"',
72
    "mlir::UnrankedTensorType": '"tensor<...>"',
73
    "mlir::RankedTensorType": '"tensor<...>"',
74
    "mlir::VectorType": '"vector<...>"',
75
}
76

77

78
class ComputedTypeIDMap:
79
    """Compute a map of type ids to derived attributes, types, and locations.
80

81
    This is necessary for determining the C++ type when holding a base class,
82
    where we really only have access to dynamic information.
83
    """
84

85
    def __init__(self, target: lldb.SBTarget, internal_dict: dict):
86
        self.resolved_typeids = {}
87

88
        # Find all of the `id` variables, which are the name of TypeID variables
89
        # defined within the TypeIDResolver.
90
        type_ids = target.FindGlobalVariables("id", lldb.UINT32_MAX)
91
        for type_id in type_ids:
92
            # Strip out any matches that didn't come from a TypeID resolver. This
93
            # also lets us extract the derived type name.
94
            name = type_id.GetName()
95
            match = re.search("^mlir::detail::TypeIDResolver<(.*), void>::id$", name)
96
            if not match:
97
                continue
98
            type_name = match.group(1)
99

100
            # Filter out types that we don't care about.
101
            if not type_name.endswith(("Attr", "Loc", "Type")):
102
                continue
103

104
            # Find the LLDB type for the derived type.
105
            type = None
106
            for typeIt in target.FindTypes(type_name):
107
                if not typeIt or not typeIt.IsValid():
108
                    continue
109
                type = typeIt
110
                break
111
            if not type or not type.IsValid():
112
                continue
113

114
            # Map the raw address of the type id variable to the LLDB type.
115
            self.resolved_typeids[type_id.AddressOf().GetValueAsUnsigned()] = type
116

117
    # Resolve the type for the given TypeID address.
118
    def resolve_type(self, typeIdAddr: lldb.SBValue):
119
        try:
120
            return self.resolved_typeids[typeIdAddr.GetValueAsUnsigned()]
121
        except KeyError:
122
            return None
123

124

125
def is_derived_attribute_or_type(sbtype: lldb.SBType, internal_dict):
126
    """Return if the given type is a derived attribute or type."""
127

128
    # We only expect an AttrBase/TypeBase base class.
129
    if sbtype.num_bases != 1:
130
        return False
131
    base_name = sbtype.GetDirectBaseClassAtIndex(0).GetName()
132
    return base_name.startswith(("mlir::Attribute::AttrBase", "mlir::Type::TypeBase"))
133

134

135
def get_typeid_map(target: lldb.SBTarget, internal_dict: dict):
136
    """Get or construct a TypeID map for the given target."""
137

138
    if "typeIdMap" not in internal_dict:
139
        internal_dict["typeIdMap"] = ComputedTypeIDMap(target, internal_dict)
140
    return internal_dict["typeIdMap"]
141

142

143
def is_attribute_or_type(sbtype: lldb.SBType, internal_dict):
144
    """Return if the given type is an attribute or type."""
145

146
    num_bases = sbtype.GetNumberOfDirectBaseClasses()
147
    typeName = sbtype.GetName()
148

149
    # We bottom out at Attribute/Type/Location.
150
    if num_bases == 0:
151
        return typeName in ["mlir::Attribute", "mlir::Type", "mlir::Location"]
152

153
    # Check the easy cases of AttrBase/TypeBase.
154
    if typeName.startswith(("mlir::Attribute::AttrBase", "mlir::Type::TypeBase")):
155
        return True
156

157
    # Otherwise, recurse into the base class.
158
    return is_attribute_or_type(
159
        sbtype.GetDirectBaseClassAtIndex(0).GetType(), internal_dict
160
    )
161

162

163
def resolve_attr_type_from_value(
164
    valobj: lldb.SBValue, abstractVal: lldb.SBValue, internal_dict
165
):
166
    """Resolve the derived C++ type of an Attribute/Type value."""
167

168
    # Derived attribute/types already have the desired type.
169
    if is_derived_attribute_or_type(valobj.GetType(), internal_dict):
170
        return valobj.GetType()
171

172
    # Otherwise, we need to resolve the ImplTy from the TypeID. This is
173
    # done dynamically, because we don't use C++ RTTI of any kind.
174
    typeIdMap = get_typeid_map(valobj.GetTarget(), internal_dict)
175
    return typeIdMap.resolve_type(
176
        abstractVal.GetChildMemberWithName("typeID").GetChildMemberWithName("storage")
177
    )
178

179

180
class AttrTypeSynthProvider:
181
    """Define an LLDB synthetic children provider for Attributes and Types."""
182

183
    def __init__(self, valobj: lldb.SBValue, internal_dict):
184
        self.valobj = valobj
185

186
        # Grab the impl variable, which if this is a Location needs to be
187
        # resolved through the LocationAttr impl variable.
188
        impl: lldb.SBValue = self.valobj.GetChildMemberWithName("impl")
189
        if self.valobj.GetTypeName() == "mlir::Location":
190
            impl = impl.GetChildMemberWithName("impl")
191
        self.abstractVal = impl.GetChildMemberWithName("abstractType")
192
        if not self.abstractVal.IsValid():
193
            self.abstractVal = impl.GetChildMemberWithName("abstractAttribute")
194

195
        self.type = resolve_attr_type_from_value(
196
            valobj, self.abstractVal, internal_dict
197
        )
198
        if not self.type:
199
            self.impl_type = None
200
            return
201

202
        # Grab the ImplTy from the resolved type. This is the 3rd template
203
        # argument of the base class.
204
        self.impl_type = (
205
            self.type.GetDirectBaseClassAtIndex(0).GetType().GetTemplateArgumentType(2)
206
        )
207
        self.impl_pointer_ty = self.impl_type.GetPointerType()
208
        self.num_fields = self.impl_type.GetNumberOfFields()
209

210
        # Optionally add a mnemonic field.
211
        type_name = self.type.GetName()
212
        if type_name in builtin_attr_type_mnemonics:
213
            self.mnemonic = builtin_attr_type_mnemonics[type_name]
214
        elif type_name.startswith("mlir::Dense"):
215
            self.mnemonic = "dense<...>"
216
        else:
217
            self.mnemonic = self.valobj.CreateValueFromExpression(
218
                "mnemonic", f"(llvm::StringRef){type_name}::getMnemonic()"
219
            )
220
            if not self.mnemonic.summary:
221
                self.mnemonic = None
222
        if self.mnemonic:
223
            self.num_fields += 1
224

225
    def num_children(self):
226
        if not self.impl_type:
227
            return 0
228
        return self.num_fields
229

230
    def get_child_index(self, name):
231
        if not self.impl_type:
232
            return None
233
        if self.mnemonic and name == "[mnemonic]":
234
            return self.impl_type.GetNumberOfFields()
235
        for i in range(self.impl_type.GetNumberOfFields()):
236
            if self.impl_type.GetFieldAtIndex(i).GetName() == name:
237
                return i
238
        return None
239

240
    def get_child_at_index(self, index):
241
        if not self.impl_type or index >= self.num_fields:
242
            return None
243

244
        impl: lldb.SBValue = self.valobj.GetChildMemberWithName("impl")
245
        impl_ptr: lldb.SBValue = self.valobj.CreateValueFromData(
246
            build_ptr_str_from_addr(impl, self.impl_pointer_ty),
247
            impl.GetData(),
248
            self.impl_pointer_ty,
249
        )
250

251
        # Check for the mnemonic field.
252
        if index == self.impl_type.GetNumberOfFields():
253
            return self.valobj.CreateValueFromExpression(
254
                "[mnemonic]", self.get_mnemonic_string(impl_ptr)
255
            )
256

257
        # Otherwise, we expect the index to be a field.
258
        field: lldb.SBTypeMember = self.impl_type.GetFieldAtIndex(index)
259

260
        # Build the field access by resolving through the impl variable.
261
        return impl_ptr.GetChildMemberWithName(field.GetName())
262

263
    def get_mnemonic_string(self, impl_ptr: lldb.SBValue):
264
        if isinstance(self.mnemonic, str):
265
            return self.mnemonic
266

267
        # If we don't already have the mnemonic in string form, compute
268
        # it from the dialect name and the mnemonic.
269
        dialect_name = self.abstractVal.GetChildMemberWithName(
270
            "dialect"
271
        ).GetChildMemberWithName("name")
272
        self.mnemonic = f'{dialect_name.summary}"."{self.mnemonic.summary}'
273
        return self.mnemonic
274

275

276
def AttrTypeSummaryProvider(valobj: lldb.SBValue, internal_dict):
277
    """Define an LLDB summary provider for Attributes and Types."""
278

279
    # Check for a value field.
280
    value = valobj.GetChildMemberWithName("value")
281
    if value and value.summary:
282
        return value.summary
283

284
    # Otherwise, try the mnemoic.
285
    mnemonic: lldb.SBValue = valobj.GetChildMemberWithName("[mnemonic]")
286
    if not mnemonic.summary:
287
        return ""
288
    mnemonicStr = mnemonic.summary.strip('"')
289

290
    # Handle a few extremely common builtin attributes/types.
291
    ## IntegerType
292
    if mnemonicStr == "iN":
293
        signedness = valobj.GetChildMemberWithName("signedness").GetValueAsUnsigned()
294
        prefix = "i"
295
        if signedness == 1:
296
            prefix = "si"
297
        elif signedness == 2:
298
            prefix = "ui"
299
        return f"{prefix}{valobj.GetChildMemberWithName('width').GetValueAsUnsigned()}"
300
    ## IntegerAttr
301
    if mnemonicStr == "integer":
302
        value = valobj.GetChildMemberWithName("value")
303
        bitwidth = value.GetChildMemberWithName("BitWidth").GetValueAsUnsigned()
304
        if bitwidth <= 64:
305
            intVal = (
306
                value.GetChildMemberWithName("U")
307
                .GetChildMemberWithName("VAL")
308
                .GetValueAsUnsigned()
309
            )
310

311
            if bitwidth == 1:
312
                return "true" if intVal else "false"
313
            return f"{intVal} : i{bitwidth}"
314

315
    return mnemonicStr
316

317

318
# ===----------------------------------------------------------------------=== #
319
# mlir::Block
320
# ===----------------------------------------------------------------------=== #
321

322

323
class BlockSynthProvider:
324
    """Define an LLDB synthetic children provider for Blocks."""
325

326
    def __init__(self, valobj, internal_dict):
327
        self.valobj = valobj
328

329
    def num_children(self):
330
        return 3
331

332
    def get_child_index(self, name):
333
        if name == "parent":
334
            return 0
335
        if name == "operations":
336
            return 1
337
        if name == "arguments":
338
            return 2
339
        return None
340

341
    def get_child_at_index(self, index):
342
        if index >= 3:
343
            return None
344
        if index == 1:
345
            return self.valobj.GetChildMemberWithName("operations")
346
        if index == 2:
347
            return self.valobj.GetChildMemberWithName("arguments")
348

349
        expr_path = build_ptr_str_from_addr(self.valobj, self.valobj.GetType())
350
        return self.valobj.CreateValueFromExpression(
351
            "parent", f"{expr_path}->getParent()"
352
        )
353

354

355
# ===----------------------------------------------------------------------=== #
356
# mlir::Operation
357
# ===----------------------------------------------------------------------=== #
358

359

360
def is_op(sbtype: lldb.SBType, internal_dict):
361
    """Return if the given type is an operation."""
362

363
    # Bottom out at OpState/Op.
364
    typeName = sbtype.GetName()
365
    if sbtype.GetNumberOfDirectBaseClasses() == 0:
366
        return typeName == "mlir::OpState"
367
    if typeName == "mlir::Operation" or typeName.startswith("mlir::Op<"):
368
        return True
369

370
    # Otherwise, recurse into the base class.
371
    return is_op(sbtype.GetDirectBaseClassAtIndex(0).GetType(), internal_dict)
372

373

374
class OperationSynthProvider:
375
    """Define an LLDB synthetic children provider for Operations."""
376

377
    def __init__(self, valobj, internal_dict):
378
        self.valobj = valobj
379
        self.fields = []
380
        self.update()
381

382
    def num_children(self):
383
        return len(self.fields)
384

385
    def get_child_index(self, name):
386
        try:
387
            return self.fields.index(name)
388
        except ValueError:
389
            return None
390

391
    def get_child_at_index(self, index):
392
        if index >= len(self.fields):
393
            return None
394
        name = self.fields[index]
395
        if name == "name":
396
            return self.opobj.GetChildMemberWithName("name")
397
        if name == "parent":
398
            return self.opobj.GetChildMemberWithName("block").Clone("parent")
399
        if name == "location":
400
            return self.opobj.GetChildMemberWithName("location")
401
        if name == "attributes":
402
            return self.opobj.GetChildMemberWithName("attrs")
403

404
        expr_path = build_ptr_str_from_addr(self.opobj, self.opobj.GetType())
405
        if name == "operands":
406
            return self.opobj.CreateValueFromExpression(
407
                "operands", f"{expr_path}->debug_getOperands()"
408
            )
409
        if name == "results":
410
            return self.opobj.CreateValueFromExpression(
411
                "results", f"{expr_path}->debug_getResults()"
412
            )
413
        if name == "successors":
414
            return self.opobj.CreateValueFromExpression(
415
                "successors", f"{expr_path}->debug_getSuccessors()"
416
            )
417
        if name == "regions":
418
            return self.opobj.CreateValueFromExpression(
419
                "regions", f"{expr_path}->debug_getRegions()"
420
            )
421
        return None
422

423
    def update(self):
424
        # If this is a derived operation, we need to resolve through the
425
        # state field.
426
        self.opobj = self.valobj
427
        if "mlir::Operation" not in self.valobj.GetTypeName():
428
            self.opobj = self.valobj.GetChildMemberWithName("state")
429

430
        self.fields = ["parent", "name", "location", "attributes"]
431
        if (
432
            self.opobj.GetChildMemberWithName("hasOperandStorage").GetValueAsUnsigned(0)
433
            != 0
434
        ):
435
            self.fields.append("operands")
436
        if self.opobj.GetChildMemberWithName("numResults").GetValueAsUnsigned(0) != 0:
437
            self.fields.append("results")
438
        if self.opobj.GetChildMemberWithName("numSuccs").GetValueAsUnsigned(0) != 0:
439
            self.fields.append("successors")
440
        if self.opobj.GetChildMemberWithName("numRegions").GetValueAsUnsigned(0) != 0:
441
            self.fields.append("regions")
442

443

444
def OperationSummaryProvider(valobj: lldb.SBValue, internal_dict):
445
    """Define an LLDB summary provider for Operations."""
446

447
    name = valobj.GetChildMemberWithName("name")
448
    if name and name.summary:
449
        return name.summary
450
    return ""
451

452

453
# ===----------------------------------------------------------------------=== #
454
# Ranges
455
# ===----------------------------------------------------------------------=== #
456

457

458
class DirectRangeSynthProvider:
459
    """Define an LLDB synthetic children provider for direct ranges, i.e. those
460
    with a base pointer that points to the type of element we want to display.
461
    """
462

463
    def __init__(self, valobj, internal_dict):
464
        self.valobj = valobj
465
        self.update()
466

467
    def num_children(self):
468
        return self.length
469

470
    def get_child_index(self, name):
471
        try:
472
            return int(name.lstrip("[").rstrip("]"))
473
        except:
474
            return None
475

476
    def get_child_at_index(self, index):
477
        if index >= self.num_children():
478
            return None
479
        offset = index * self.type_size
480
        return self.data.CreateChildAtOffset(f"[{index}]", offset, self.data_type)
481

482
    def update(self):
483
        length_obj = self.valobj.GetChildMemberWithName("count")
484
        self.length = length_obj.GetValueAsUnsigned(0)
485

486
        self.data = self.valobj.GetChildMemberWithName("base")
487
        self.data_type = self.data.GetType().GetPointeeType()
488
        self.type_size = self.data_type.GetByteSize()
489
        assert self.type_size != 0
490

491

492
class InDirectRangeSynthProvider:
493
    """Define an LLDB synthetic children provider for ranges
494
    that transform the underlying base pointer, e.g. to convert
495
    it to a different type depending on various characteristics
496
    (e.g. mlir::ValueRange).
497
    """
498

499
    def __init__(self, valobj, internal_dict):
500
        self.valobj = valobj
501
        self.update()
502

503
    def num_children(self):
504
        return self.length
505

506
    def get_child_index(self, name):
507
        try:
508
            return int(name.lstrip("[").rstrip("]"))
509
        except:
510
            return None
511

512
    def get_child_at_index(self, index):
513
        if index >= self.num_children():
514
            return None
515
        expr_path = get_expression_path(self.valobj)
516
        return self.valobj.CreateValueFromExpression(
517
            f"[{index}]", f"{expr_path}[{index}]"
518
        )
519

520
    def update(self):
521
        length_obj = self.valobj.GetChildMemberWithName("count")
522
        self.length = length_obj.GetValueAsUnsigned(0)
523

524

525
class IPListRangeSynthProvider:
526
    """Define an LLDB synthetic children provider for an IPList."""
527

528
    def __init__(self, valobj, internal_dict):
529
        self.valobj = valobj
530
        self.update()
531

532
    def num_children(self):
533
        sentinel = self.valobj.GetChildMemberWithName("Sentinel")
534
        sentinel_addr = sentinel.AddressOf().GetValueAsUnsigned(0)
535

536
        # Iterate the next pointers looking for the sentinel.
537
        count = 0
538
        current = sentinel.GetChildMemberWithName("Next")
539
        while current.GetValueAsUnsigned(0) != sentinel_addr:
540
            current = current.GetChildMemberWithName("Next")
541
            count += 1
542

543
        return count
544

545
    def get_child_index(self, name):
546
        try:
547
            return int(name.lstrip("[").rstrip("]"))
548
        except:
549
            return None
550

551
    def get_child_at_index(self, index):
552
        if index >= self.num_children():
553
            return None
554

555
        # Start from the sentinel and grab the next pointer.
556
        value: lldb.SBValue = self.valobj.GetChildMemberWithName("Sentinel")
557
        it = 0
558
        while it <= index:
559
            value = value.GetChildMemberWithName("Next")
560
            it += 1
561

562
        return value.CreateValueFromExpression(
563
            f"[{index}]",
564
            f"(({self.value_type})({value.GetTypeName()}){value.GetValueAsUnsigned()})",
565
        )
566

567
    def update(self):
568
        self.value_type = (
569
            self.valobj.GetType().GetTemplateArgumentType(0).GetPointerType()
570
        )
571

572

573
# ===----------------------------------------------------------------------=== #
574
# mlir::Value
575
# ===----------------------------------------------------------------------=== #
576

577

578
class ValueSynthProvider:
579
    """Define an LLDB synthetic children provider for Values."""
580

581
    def __init__(self, valobj, internal_dict):
582
        self.valobj = valobj
583
        self.update()
584

585
    def num_children(self):
586
        # 7: BlockArgument:
587
        #  index, type, owner, firstUse, location
588
        if self.kind == 7:
589
            return 5
590

591
        # 0-6: OpResult:
592
        #  index, type, owner, firstUse
593
        return 4
594

595
    def get_child_index(self, name):
596
        if name == "index":
597
            return 0
598
        if name == "type":
599
            return 1
600
        if name == "owner":
601
            return 2
602
        if name == "firstUse":
603
            return 3
604
        if name == "location":
605
            return 4
606
        return None
607

608
    def get_child_at_index(self, index):
609
        if index >= self.num_children():
610
            return None
611

612
        # Check if the current value is already an Impl struct.
613
        if self.valobj.GetTypeName().endswith("Impl"):
614
            impl_ptr_str = build_ptr_str_from_addr(
615
                self.valobj.AddressOf(), self.valobj.GetType().GetPointerType()
616
            )
617
        else:
618
            impl = self.valobj.GetChildMemberWithName("impl")
619
            impl_ptr_str = build_ptr_str_from_addr(impl, impl.GetType())
620

621
        # Cast to the derived Impl type.
622
        if self.kind == 7:
623
            derived_impl_str = f"((mlir::detail::BlockArgumentImpl *){impl_ptr_str})"
624
        elif self.kind == 6:
625
            derived_impl_str = f"((mlir::detail::OutOfLineOpResult *){impl_ptr_str})"
626
        else:
627
            derived_impl_str = f"((mlir::detail::InlineOpResult *){impl_ptr_str})"
628

629
        # Handle the shared fields when possible.
630
        if index == 1:
631
            return self.valobj.CreateValueFromExpression(
632
                "type", f"{derived_impl_str}->debug_getType()"
633
            )
634
        if index == 3:
635
            return self.valobj.CreateValueFromExpression(
636
                "firstUse", f"{derived_impl_str}->firstUse"
637
            )
638

639
        # Handle Block argument children.
640
        if self.kind == 7:
641
            impl = self.valobj.CreateValueFromExpression("impl", derived_impl_str)
642
            if index == 0:
643
                return impl.GetChildMemberWithName("index")
644
            if index == 2:
645
                return impl.GetChildMemberWithName("owner")
646
            if index == 4:
647
                return impl.GetChildMemberWithName("loc")
648

649
        # Handle OpResult children.
650
        if index == 0:
651
            # Handle the out of line case.
652
            if self.kind == 6:
653
                return self.valobj.CreateValueFromExpression(
654
                    "index", f"{derived_impl_str}->outOfLineIndex + 6"
655
                )
656
            return self.valobj.CreateValueFromExpression("index", f"{self.kind}")
657
        if index == 2:
658
            return self.valobj.CreateValueFromExpression(
659
                "owner", f"{derived_impl_str}->getOwner()"
660
            )
661
        return None
662

663
    def update(self):
664
        # Check if the current value is already an Impl struct.
665
        if self.valobj.GetTypeName().endswith("Impl"):
666
            impl_ptr_str = build_ptr_str_from_addr(
667
                self.valobj, self.valobj.GetType().GetPointerType()
668
            )
669
        else:
670
            impl = self.valobj.GetChildMemberWithName("impl")
671
            impl_ptr_str = build_ptr_str_from_addr(impl, impl.GetType())
672

673
        # Compute the kind of value we are dealing with.
674
        self.kind = self.valobj.CreateValueFromExpression(
675
            "kind", f"{impl_ptr_str}->debug_getKind()"
676
        ).GetValueAsUnsigned()
677

678

679
def ValueSummaryProvider(valobj: lldb.SBValue, internal_dict):
680
    """Define an LLDB summary provider for Values."""
681

682
    index = valobj.GetChildMemberWithName("index").GetValueAsUnsigned()
683
    # Check if this is a block argument or not (block arguments have locations).
684
    if valobj.GetChildMemberWithName("location").IsValid():
685
        summary = f"Block Argument {index}"
686
    else:
687
        owner_name = (
688
            valobj.GetChildMemberWithName("owner")
689
            .GetChildMemberWithName("name")
690
            .summary
691
        )
692
        summary = f"{owner_name} Result {index}"
693

694
    # Grab the type to help form the summary.
695
    type = valobj.GetChildMemberWithName("type")
696
    if type.summary:
697
        summary += f": {type.summary}"
698

699
    return summary
700

701

702
# ===----------------------------------------------------------------------=== #
703
# Initialization
704
# ===----------------------------------------------------------------------=== #
705

706

707
def __lldb_init_module(debugger: lldb.SBDebugger, internal_dict):
708
    cat: lldb.SBTypeCategory = debugger.CreateCategory("mlir")
709
    cat.SetEnabled(True)
710

711
    # Attributes and Types
712
    cat.AddTypeSummary(
713
        lldb.SBTypeNameSpecifier(
714
            "mlirDataFormatters.is_attribute_or_type", lldb.eFormatterMatchCallback
715
        ),
716
        lldb.SBTypeSummary.CreateWithFunctionName(
717
            "mlirDataFormatters.AttrTypeSummaryProvider"
718
        ),
719
    )
720
    cat.AddTypeSynthetic(
721
        lldb.SBTypeNameSpecifier(
722
            "mlirDataFormatters.is_attribute_or_type", lldb.eFormatterMatchCallback
723
        ),
724
        lldb.SBTypeSynthetic.CreateWithClassName(
725
            "mlirDataFormatters.AttrTypeSynthProvider"
726
        ),
727
    )
728

729
    # Operation
730
    cat.AddTypeSynthetic(
731
        lldb.SBTypeNameSpecifier("mlir::Block", lldb.eFormatterMatchExact),
732
        lldb.SBTypeSynthetic.CreateWithClassName(
733
            "mlirDataFormatters.BlockSynthProvider"
734
        ),
735
    )
736

737
    # NamedAttribute
738
    cat.AddTypeSummary(
739
        lldb.SBTypeNameSpecifier("mlir::NamedAttribute", lldb.eFormatterMatchExact),
740
        lldb.SBTypeSummary.CreateWithSummaryString("${var.name%S} = ${var.value%S}"),
741
    )
742

743
    # OperationName
744
    cat.AddTypeSummary(
745
        lldb.SBTypeNameSpecifier("mlir::OperationName", lldb.eFormatterMatchExact),
746
        lldb.SBTypeSummary.CreateWithSummaryString("${var.impl->name%S}"),
747
    )
748

749
    # Operation
750
    cat.AddTypeSummary(
751
        lldb.SBTypeNameSpecifier(
752
            "mlirDataFormatters.is_op", lldb.eFormatterMatchCallback
753
        ),
754
        lldb.SBTypeSummary.CreateWithFunctionName(
755
            "mlirDataFormatters.OperationSummaryProvider"
756
        ),
757
    )
758
    cat.AddTypeSynthetic(
759
        lldb.SBTypeNameSpecifier(
760
            "mlirDataFormatters.is_op", lldb.eFormatterMatchCallback
761
        ),
762
        lldb.SBTypeSynthetic.CreateWithClassName(
763
            "mlirDataFormatters.OperationSynthProvider"
764
        ),
765
    )
766

767
    # Ranges
768
    def add_direct_range_summary_and_synth(name):
769
        cat.AddTypeSummary(
770
            lldb.SBTypeNameSpecifier(name, lldb.eFormatterMatchExact),
771
            lldb.SBTypeSummary.CreateWithSummaryString("size=${svar%#}"),
772
        )
773
        cat.AddTypeSynthetic(
774
            lldb.SBTypeNameSpecifier(name, lldb.eFormatterMatchExact),
775
            lldb.SBTypeSynthetic.CreateWithClassName(
776
                "mlirDataFormatters.DirectRangeSynthProvider"
777
            ),
778
        )
779

780
    def add_indirect_range_summary_and_synth(name):
781
        cat.AddTypeSummary(
782
            lldb.SBTypeNameSpecifier(name, lldb.eFormatterMatchExact),
783
            lldb.SBTypeSummary.CreateWithSummaryString("size=${svar%#}"),
784
        )
785
        cat.AddTypeSynthetic(
786
            lldb.SBTypeNameSpecifier(name, lldb.eFormatterMatchExact),
787
            lldb.SBTypeSynthetic.CreateWithClassName(
788
                "mlirDataFormatters.InDirectRangeSynthProvider"
789
            ),
790
        )
791

792
    def add_iplist_range_summary_and_synth(name):
793
        cat.AddTypeSummary(
794
            lldb.SBTypeNameSpecifier(name, lldb.eFormatterMatchExact),
795
            lldb.SBTypeSummary.CreateWithSummaryString("size=${svar%#}"),
796
        )
797
        cat.AddTypeSynthetic(
798
            lldb.SBTypeNameSpecifier(name, lldb.eFormatterMatchExact),
799
            lldb.SBTypeSynthetic.CreateWithClassName(
800
                "mlirDataFormatters.IPListRangeSynthProvider"
801
            ),
802
        )
803

804
    add_direct_range_summary_and_synth("mlir::Operation::operand_range")
805
    add_direct_range_summary_and_synth("mlir::OperandRange")
806
    add_direct_range_summary_and_synth("mlir::Operation::result_range")
807
    add_direct_range_summary_and_synth("mlir::ResultRange")
808
    add_direct_range_summary_and_synth("mlir::SuccessorRange")
809
    add_indirect_range_summary_and_synth("mlir::ValueRange")
810
    add_indirect_range_summary_and_synth("mlir::TypeRange")
811
    add_iplist_range_summary_and_synth("mlir::Block::OpListType")
812
    add_iplist_range_summary_and_synth("mlir::Region::BlockListType")
813

814
    # Values
815
    def add_value_summary_and_synth(name):
816
        cat.AddTypeSummary(
817
            lldb.SBTypeNameSpecifier(name, lldb.eFormatterMatchExact),
818
            lldb.SBTypeSummary.CreateWithFunctionName(
819
                "mlirDataFormatters.ValueSummaryProvider"
820
            ),
821
        )
822
        cat.AddTypeSynthetic(
823
            lldb.SBTypeNameSpecifier(name, lldb.eFormatterMatchExact),
824
            lldb.SBTypeSynthetic.CreateWithClassName(
825
                "mlirDataFormatters.ValueSynthProvider"
826
            ),
827
        )
828

829
    add_value_summary_and_synth("mlir::BlockArgument")
830
    add_value_summary_and_synth("mlir::Value")
831
    add_value_summary_and_synth("mlir::OpResult")
832
    add_value_summary_and_synth("mlir::detail::OpResultImpl")
833

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.