Ver Fonte

[spirv] Handle vector<literal int/float, 1> in type hints (#1017)

isVectorType() only returns true for real vectors with more than
one element.
Lei Zhang há 7 anos atrás
pai
commit
fe57ed4c9b

+ 2 - 1
tools/clang/lib/SPIRV/SPIRVEmitter.cpp

@@ -4106,7 +4106,8 @@ SPIRVEmitter::doHLSLVectorElementExpr(const HLSLVectorElementExpr *expr) {
   }
 
   if (baseSize == 1) {
-    // Selecting one element from a size-1 vector. Construct the vector.
+    // Selecting more than one element from a size-1 vector, for example,
+    // <scalar>.xx. Construct the vector.
     auto info = loadIfGLValue(baseExpr);
     llvm::SmallVector<uint32_t, 4> components(accessorSize, info);
     return info

+ 3 - 4
tools/clang/lib/SPIRV/TypeTranslator.cpp

@@ -144,10 +144,9 @@ bool TypeTranslator::LiteralTypeHint::isLiteralType(QualType type) {
       type->isSpecificBuiltinType(BuiltinType::LitFloat))
     return true;
 
-  // For cases such as 'vector<literal int, 2>'
-  QualType elemType = {};
-  if (isVectorType(type, &elemType))
-    return isLiteralType(elemType);
+  // For cases such as 'vector<literal int, 2>' or 'vector<literal float, 1>'
+  if (hlsl::IsHLSLVecType(type))
+    return isLiteralType(hlsl::GetHLSLVecElementType(type));
 
   return false;
 }

+ 5 - 4
tools/clang/lib/SPIRV/TypeTranslator.h

@@ -260,10 +260,11 @@ public:
   QualType getIntendedLiteralType(QualType type);
 
 public:
-  // A RAII class for maintaining the intendedLiteralTypes stack.
-  // Instantiating an object of this class ensures that as long as the
-  // object lives, the hint lives in the TypeTranslator, and once the object is
-  // destroyed, the hint is automatically removed from the stack.
+  /// A RAII class for maintaining the intendedLiteralTypes stack.
+  ///
+  /// Instantiating an object of this class ensures that as long as the
+  /// object lives, the hint lives in the TypeTranslator, and once the object is
+  /// destroyed, the hint is automatically removed from the stack.
   class LiteralTypeHint {
   public:
     LiteralTypeHint(TypeTranslator &t, QualType ty);

+ 8 - 1
tools/clang/test/CodeGenSPIRV/op.vector.swizzle.const-scalar.hlsl

@@ -4,7 +4,7 @@
 // CHECK: [[v4f25:%\d+]] = OpConstantComposite %v4float %float_2_5 %float_2_5 %float_2_5 %float_2_5
 // CHECK:  [[v4f0:%\d+]] = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
 
-float4 main() : SV_Target {
+float4 main(float input: INPUT) : SV_Target {
 
 // CHECK: OpStore %a [[v4f1]]
   float4 a = (1).xxxx;
@@ -15,6 +15,13 @@ float4 main() : SV_Target {
 // CHECK: OpStore %c [[v4f0]]
   float4 c = (false).xxxx;
 
+// CHECK:       [[cc:%\d+]] = OpCompositeConstruct %v2float %float_0 %float_0
+// CHECK-NEXT: [[cc0:%\d+]] = OpCompositeExtract %float [[cc]] 0
+// CHECK-NEXT: [[cc1:%\d+]] = OpCompositeExtract %float [[cc]] 1
+// CHECK-NEXT: [[rhs:%\d+]] = OpCompositeConstruct %v4float {{%\d+}} {{%\d+}} [[cc0]] [[cc1]]
+// CHECK-NEXT:                OpStore %d [[rhs]]
+  float4 d = float4(input, input, 0.0.xx);
+
 // CHECK: OpReturnValue [[v4f0]]
   return 0.0.xxxx;
 }