Kaynağa Gözat

Correct size for empty struct element in debug info. (#2103)

Adam Yang 6 yıl önce
ebeveyn
işleme
75df1b1436

+ 24 - 0
lib/HLSL/HLModule.cpp

@@ -1209,6 +1209,22 @@ static void AddDIGlobalVariable(DIBuilder &Builder, DIGlobalVariable *LocDIGV,
   DXVERIFY_NOMSG(DbgInfoFinder.appendGlobalVariable(EltDIGV));
 }
 
+static unsigned GetCompositeTypeSize(DIType *Ty) {
+  DICompositeType *StructTy = nullptr;
+  DITypeIdentifierMap EmptyMap;
+
+  if (DIDerivedType *DerivedTy = dyn_cast<DIDerivedType>(Ty)) {
+    DXASSERT_NOMSG(DerivedTy->getTag() == dwarf::DW_TAG_const_type || DerivedTy->getTag() == dwarf::DW_TAG_typedef);
+    DIType *BaseTy = DerivedTy->getBaseType().resolve(EmptyMap);
+    return GetCompositeTypeSize(BaseTy);
+  }
+  else {
+    StructTy = cast<DICompositeType>(Ty);
+  }
+
+  return StructTy->getSizeInBits();
+}
+
 void HLModule::CreateElementGlobalVariableDebugInfo(
     GlobalVariable *GV, DebugInfoFinder &DbgInfoFinder, GlobalVariable *EltGV,
     unsigned sizeInBits, unsigned alignInBits, unsigned offsetInBits,
@@ -1220,6 +1236,14 @@ void HLModule::CreateElementGlobalVariableDebugInfo(
 
   DIType *DITy = DIGV->getType().resolve(EmptyMap);
   DIScope *DITyScope = DITy->getScope().resolve(EmptyMap);
+
+  // If element size is greater than base size make sure we're dealing with an empty struct.
+  unsigned compositeSize = GetCompositeTypeSize(DITy);
+  if (sizeInBits > compositeSize) {
+    DXASSERT_NOMSG(offsetInBits == 0 && compositeSize == 8);
+    sizeInBits = compositeSize;
+  }
+
   // Create Elt type.
   DIType *EltDITy =
       Builder.createMemberType(DITyScope, DITy->getName().str() + eltName.str(),

+ 16 - 0
tools/clang/test/CodeGenHLSL/debug/empty_global_struct.hlsl

@@ -0,0 +1,16 @@
+// RUN: %dxc -E main -T ps_6_0 %s -Zi -Od | FileCheck %s
+
+// CHECK-DAG: !DIGlobalVariable(name: "foo.0", linkageName: "foo.0", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: ![[ty:[0-9]+]]
+// CHECK-DAG: ![[ty]] = !DIDerivedType(tag: DW_TAG_member, name: "S.0", file: !1, line: 6, baseType: !{{[0-9]+}}, size: 8
+
+struct S {
+  float f() {
+    return 420;
+  }
+};
+
+static S foo;
+
+float main() : SV_Target {
+  return foo.f();
+}

+ 17 - 0
tools/clang/test/CodeGenHLSL/debug/empty_global_struct_const.hlsl

@@ -0,0 +1,17 @@
+// RUN: %dxc -E main -T ps_6_0 %s -Zi -Od | FileCheck %s
+
+// CHECK-DAG: !DIGlobalVariable(name: "foo.0", linkageName: "foo.0", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: ![[ty:[0-9]+]]
+// CHECK-DAG: ![[ty]] = !DIDerivedType(tag: DW_TAG_member, name: ".0", baseType: !{{[0-9]+}}, size: 8
+
+struct S {
+  float f() {
+    return 420;
+  }
+};
+
+const static S foo;
+
+float main() : SV_Target {
+  return foo.f();
+}
+