Skip to content

Commit f3275fe

Browse files
tgs-scmahesh-attarde
authored andcommitted
[lldb][TypeSystemClang] Added unique builtins types for __bf16 and _Float16 (llvm#157674)
During debugging applization with __bf16 and _Float16 float types it was discovered that lldb creates the same CompilerType for them. This can cause an infinite recursion error, if one tries to create two struct specializations with these types and then inherit one specialization from another.
1 parent f3d758a commit f3275fe

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,12 @@ CompilerType TypeSystemClang::GetBuiltinTypeForDWARFEncodingAndBitSize(
960960
if (type_name == "long double" &&
961961
QualTypeMatchesBitSize(bit_size, ast, ast.LongDoubleTy))
962962
return GetType(ast.LongDoubleTy);
963+
if (type_name == "__bf16" &&
964+
QualTypeMatchesBitSize(bit_size, ast, ast.BFloat16Ty))
965+
return GetType(ast.BFloat16Ty);
966+
if (type_name == "_Float16" &&
967+
QualTypeMatchesBitSize(bit_size, ast, ast.Float16Ty))
968+
return GetType(ast.Float16Ty);
963969
// As Rust currently uses `TypeSystemClang`, match `f128` here as well so it
964970
// doesn't get misinterpreted as `long double` on targets where they are
965971
// the same size but different formats.
@@ -1792,6 +1798,8 @@ bool TypeSystemClang::RecordHasFields(const RecordDecl *record_decl) {
17921798
for (base_class = cxx_record_decl->bases_begin(),
17931799
base_class_end = cxx_record_decl->bases_end();
17941800
base_class != base_class_end; ++base_class) {
1801+
assert(record_decl != base_class->getType()->getAsCXXRecordDecl() &&
1802+
"Base can't inherit from itself.");
17951803
if (RecordHasFields(base_class->getType()->getAsCXXRecordDecl()))
17961804
return true;
17971805
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CXX_SOURCES := main.cpp
2+
3+
include Makefile.rules
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import lldb
2+
from lldbsuite.test.decorators import *
3+
from lldbsuite.test.lldbtest import *
4+
from lldbsuite.test import lldbutil
5+
6+
7+
class TestCase(TestBase):
8+
def test(self):
9+
self.build()
10+
lldbutil.run_to_source_breakpoint(
11+
self, "// break here", lldb.SBFileSpec("main.cpp", False)
12+
)
13+
14+
self.expect_expr("f0", result_type="Foo<__bf16>")
15+
self.expect_expr("f1", result_type="Foo<__fp16>")
16+
17+
# Test sizeof to ensure while computing layout we don't do
18+
# infinite recursion.
19+
v = self.frame().EvaluateExpression("sizeof(f0)")
20+
self.assertEqual(v.GetValueAsUnsigned() > 0, True)
21+
v = self.frame().EvaluateExpression("sizeof(f1)")
22+
self.assertEqual(v.GetValueAsUnsigned() > 0, True)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
template <typename T> struct Foo;
2+
3+
template <> struct Foo<__bf16> {};
4+
5+
template <> struct Foo<_Float16> : Foo<__bf16> {};
6+
7+
int main() {
8+
Foo<__bf16> f0;
9+
Foo<_Float16> f1;
10+
return 0; // break here
11+
}

lldb/test/API/lang/cpp/template-arguments/TestCppTemplateArguments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test(self):
8282
value = self.expect_expr("temp7", result_type="Foo<__fp16, __fp16>")
8383
self.assertFalse(value.GetType().GetTemplateArgumentValue(target, 1))
8484

85-
value = self.expect_expr("temp8", result_type="Foo<__fp16, __fp16>")
85+
value = self.expect_expr("temp8", result_type="Foo<__bf16, __bf16>")
8686
self.assertFalse(value.GetType().GetTemplateArgumentValue(target, 1))
8787

8888
value = self.expect_expr("temp9", result_type="Bar<double, 1.200000e+00>")

0 commit comments

Comments
 (0)