Kaynağa Gözat

[SPIR-V] Fix composites with empty struct fields. (#5017)

- Fix index assertion in getFieldIndexInStruct.
- Don't generate composite for empty structs in struct fields.
Laura Hermanns 2 yıl önce
ebeveyn
işleme
b248b4c571

+ 9 - 7
tools/clang/lib/SPIRV/InitListHandler.cpp

@@ -366,13 +366,15 @@ InitListHandler::createInitForStructType(QualType type, SourceLocation srcLoc,
     while (tryToSplitConstantArray())
       ;
 
-    auto init = initializers.back();
-    // We can only avoid decomposing and reconstructing when the type is
-    // exactly the same.
-    if (type.getCanonicalType() ==
-        init->getAstResultType().getCanonicalType()) {
-      initializers.pop_back();
-      return init;
+    if (!initializers.empty()) {
+      auto init = initializers.back();
+      // We can only avoid decomposing and reconstructing when the type is
+      // exactly the same.
+      if (type.getCanonicalType() ==
+          init->getAstResultType().getCanonicalType()) {
+        initializers.pop_back();
+        return init;
+      }
     }
 
     // Otherwise, if the next initializer is a struct, it is not of the same

+ 1 - 1
tools/clang/lib/SPIRV/SpirvEmitter.cpp

@@ -556,7 +556,7 @@ uint32_t getFieldIndexInStruct(const StructType *spirvStructType,
       getNumBaseClasses(astStructType) + fieldDecl->getFieldIndex();
 
   const auto &fields = spirvStructType->getFields();
-  assert(indexAST <= fields.size());
+  assert(indexAST < fields.size());
   return fields[indexAST].fieldIndex;
 }
 

+ 15 - 0
tools/clang/test/CodeGenSPIRV/type.template.function.empty-struct-argument.hlsl

@@ -0,0 +1,15 @@
+// RUN: %dxc -T ps_6_0 -E main -HV 2021
+
+struct A {};
+
+template <typename T0, typename T1 = A>
+struct B {
+  T0 m0;
+  T1 m1;
+};
+
+float4 main() : SV_Target {
+  // CHECK: %12 = OpConstantComposite %v4float %float_1 %float_2 %float_3 %float_4
+  B<float4> b = { float4(1, 2, 3, 4) };
+  return b.m0;
+}