Browse Source

[spirv] Update SPIR-V Tools and Fix Builtin usage. (#1183)

Support uint/uint2 for DispatchThreadID
Support uint/uint2 for GroupThreadID
Support uint/uint2 for GroupID

Also updated SPIR-V Tools.
Ehsan 7 years ago
parent
commit
4e5e56d0cd

+ 1 - 1
external/SPIRV-Tools

@@ -1 +1 @@
-Subproject commit 2e644e45785bb221294c32bf02a4ac867de49dc4
+Subproject commit 5e07ab1358dcb1593656b256bc49cf13905940bd

+ 28 - 0
tools/clang/lib/SPIRV/DeclResultIdMapper.cpp

@@ -1278,6 +1278,11 @@ bool DeclResultIdMapper::createStageVars(const hlsl::SigPoint *sigPoint,
     //   SampleMask, must be an array of integers.
     // * SV_InnerCoverage is an uint value, but the corresponding builtin,
     //   FullyCoveredEXT, must be an boolean value.
+    // * SV_DispatchThreadID and SV_GroupThreadID are allowed to be uint, uint2,
+    //   or uint3, but the corresponding builtins (GlobalInvocationId and
+    //   LocalInvocationId) must be a uint3.
+    // * SV_GroupID is allowed to be uint, uint2, or uint3, but the
+    //   corresponding builtin (WorkgroupId) must be a uint3.
 
     if (glPerVertex.tryToAccess(sigPoint->GetKind(), semanticKind,
                                 semanticToUse->index, invocationId, value,
@@ -1307,6 +1312,11 @@ bool DeclResultIdMapper::createStageVars(const hlsl::SigPoint *sigPoint,
     case hlsl::Semantic::Kind::Barycentrics:
       typeId = theBuilder.getVecType(theBuilder.getFloat32Type(), 2);
       break;
+    case hlsl::Semantic::Kind::DispatchThreadID:
+    case hlsl::Semantic::Kind::GroupThreadID:
+    case hlsl::Semantic::Kind::GroupID:
+      typeId = theBuilder.getVecType(theBuilder.getUint32Type(), 3);
+      break;
     }
 
     // Handle the extra arrayness
@@ -1436,6 +1446,24 @@ bool DeclResultIdMapper::createStageVars(const hlsl::SigPoint *sigPoint,
 
         *value = theBuilder.createCompositeConstruct(v3f32Type, {x, y, z});
       }
+      // Special handling of SV_DispatchThreadID and SV_GroupThreadID, which may
+      // be a uint or uint2, but the underlying stage input variable is a uint3.
+      // The last component(s) should be discarded in needed.
+      else if ((semanticKind == hlsl::Semantic::Kind::DispatchThreadID ||
+                semanticKind == hlsl::Semantic::Kind::GroupThreadID ||
+                semanticKind == hlsl::Semantic::Kind::GroupID) &&
+               (!hlsl::IsHLSLVecType(type) ||
+                hlsl::GetHLSLVecSize(type) != 3)) {
+        const auto vecSize =
+            hlsl::IsHLSLVecType(type) ? hlsl::GetHLSLVecSize(type) : 1;
+        if (vecSize == 1)
+          *value = theBuilder.createCompositeExtract(theBuilder.getUint32Type(),
+                                                     *value, {0});
+        else if (vecSize == 2)
+          *value = theBuilder.createVectorShuffle(
+              theBuilder.getVecType(theBuilder.getUint32Type(), 2), *value,
+              *value, {0, 1});
+      }
     } else {
       if (noWriteBack)
         return true;

+ 1 - 1
tools/clang/test/CodeGenSPIRV/cs.groupshared.hlsl

@@ -23,7 +23,7 @@ groupshared              float2   d[5];
 groupshared              S        s;
 
 [numthreads(8, 8, 8)]
-void main(uint2 tid : SV_DispatchThreadID, uint2 gid : SV_GroupID) {
+void main(uint3 tid : SV_DispatchThreadID, uint2 gid : SV_GroupID) {
 // Make sure pointers have the correct storage class
 // CHECK:    {{%\d+}} = OpAccessChain %_ptr_Workgroup_float %s %int_0
 // CHECK: [[d0:%\d+]] = OpAccessChain %_ptr_Workgroup_v2float %d %int_0

+ 1 - 0
tools/clang/test/CodeGenSPIRV/semantic.dispatch-thread-id.cs.hlsl

@@ -4,4 +4,5 @@
 // CHECK: OpDecorate %gl_GlobalInvocationID BuiltIn GlobalInvocationId
 // CHECK: %gl_GlobalInvocationID = OpVariable %_ptr_Input_v3uint Input
 
+[numthreads(8, 8, 8)]
 void main(uint3 tid : SV_DispatchThreadId) {}

+ 12 - 0
tools/clang/test/CodeGenSPIRV/semantic.dispatch-thread-id.uint.cs.hlsl

@@ -0,0 +1,12 @@
+// Run: %dxc -T cs_6_0 -E main
+
+// CHECK: OpEntryPoint GLCompute %main "main" %gl_GlobalInvocationID
+// CHECK: OpDecorate %gl_GlobalInvocationID BuiltIn GlobalInvocationId
+// CHECK: %gl_GlobalInvocationID = OpVariable %_ptr_Input_v3uint Input
+
+// CHECK: [[gl_GlobalInvocationID:%\d+]] = OpLoad %v3uint %gl_GlobalInvocationID
+// CHECK: [[uint_DispatchThreadID:%\d+]] = OpCompositeExtract %uint [[gl_GlobalInvocationID]] 0
+// CHECK:                                  OpStore %param_var_tid [[uint_DispatchThreadID]]
+
+[numthreads(8, 8, 8)]
+void main(uint tid : SV_DispatchThreadId) {}

+ 12 - 0
tools/clang/test/CodeGenSPIRV/semantic.dispatch-thread-id.uint2.cs.hlsl

@@ -0,0 +1,12 @@
+// Run: %dxc -T cs_6_0 -E main
+
+// CHECK: OpEntryPoint GLCompute %main "main" %gl_GlobalInvocationID
+// CHECK: OpDecorate %gl_GlobalInvocationID BuiltIn GlobalInvocationId
+// CHECK: %gl_GlobalInvocationID = OpVariable %_ptr_Input_v3uint Input
+
+// CHECK:  [[gl_GlobalInvocationID:%\d+]] = OpLoad %v3uint %gl_GlobalInvocationID
+// CHECK: [[uint2_DispatchThreadID:%\d+]] = OpVectorShuffle %v2uint [[gl_GlobalInvocationID]] [[gl_GlobalInvocationID]] 0 1
+// CHECK:                                   OpStore %param_var_tid [[uint2_DispatchThreadID]]
+
+[numthreads(8, 8, 8)]
+void main(uint2 tid : SV_DispatchThreadId) {}

+ 1 - 0
tools/clang/test/CodeGenSPIRV/semantic.group-id.cs.hlsl

@@ -4,4 +4,5 @@
 // CHECK: OpDecorate %gl_WorkGroupID BuiltIn WorkgroupId
 // CHECK: %gl_WorkGroupID = OpVariable %_ptr_Input_v3uint Input
 
+[numthreads(8, 8, 8)]
 void main(uint3 tid : SV_GroupID) {}

+ 11 - 0
tools/clang/test/CodeGenSPIRV/semantic.group-id.uint.cs.hlsl

@@ -0,0 +1,11 @@
+// Run: %dxc -T cs_6_0 -E main
+
+// CHECK: OpEntryPoint GLCompute %main "main" %gl_WorkGroupID
+// CHECK: OpDecorate %gl_WorkGroupID BuiltIn WorkgroupId
+// CHECK: %gl_WorkGroupID = OpVariable %_ptr_Input_v3uint Input
+
+// CHECK: [[gl_WorkGroupID:%\d+]] = OpLoad %v3uint %gl_WorkGroupID
+// CHECK:   [[uint_GroupID:%\d+]] = OpCompositeExtract %uint [[gl_WorkGroupID]] 0
+// CHECK:                           OpStore %param_var_tid [[uint_GroupID]]
+[numthreads(8, 8, 8)]
+void main(uint tid : SV_GroupID) {}

+ 12 - 0
tools/clang/test/CodeGenSPIRV/semantic.group-id.uint2.cs.hlsl

@@ -0,0 +1,12 @@
+// Run: %dxc -T cs_6_0 -E main
+
+// CHECK: OpEntryPoint GLCompute %main "main" %gl_WorkGroupID
+// CHECK: OpDecorate %gl_WorkGroupID BuiltIn WorkgroupId
+// CHECK: %gl_WorkGroupID = OpVariable %_ptr_Input_v3uint Input
+
+// CHECK: [[gl_WorkGrouID:%\d+]] = OpLoad %v3uint %gl_WorkGroupID
+// CHECK: [[uint2_GroupID:%\d+]] = OpVectorShuffle %v2uint [[gl_WorkGrouID]] [[gl_WorkGrouID]] 0 1
+// CHECK:                          OpStore %param_var_tid [[uint2_GroupID]]
+
+[numthreads(8, 8, 8)]
+void main(uint2 tid : SV_GroupID) {}

+ 2 - 1
tools/clang/test/CodeGenSPIRV/semantic.group-thread-id.cs.hlsl

@@ -4,4 +4,5 @@
 // CHECK: OpDecorate %gl_LocalInvocationID BuiltIn LocalInvocationId
 // CHECK: %gl_LocalInvocationID = OpVariable %_ptr_Input_v3uint Input
 
-void main(uint3 gtid : SV_GroupThreadID) : A {}
+[numthreads(8, 8, 8)]
+void main(uint3 gtid : SV_GroupThreadID) {}

+ 12 - 0
tools/clang/test/CodeGenSPIRV/semantic.group-thread-id.uint.cs.hlsl

@@ -0,0 +1,12 @@
+// Run: %dxc -T cs_6_0 -E main
+
+// CHECK: OpEntryPoint GLCompute %main "main" %gl_LocalInvocationID
+// CHECK: OpDecorate %gl_LocalInvocationID BuiltIn LocalInvocationId
+// CHECK: %gl_LocalInvocationID = OpVariable %_ptr_Input_v3uint Input
+
+// CHECK: [[gl_LocalInvocationID:%\d+]] = OpLoad %v3uint %gl_LocalInvocationID
+// CHECK:   [[uint_GroupThreadID:%\d+]] = OpCompositeExtract %uint [[gl_LocalInvocationID]] 0
+// CHECK:                                 OpStore %param_var_gtid [[uint_GroupThreadID]]
+
+[numthreads(8, 8, 8)]
+void main(uint gtid : SV_GroupThreadID) {}

+ 12 - 0
tools/clang/test/CodeGenSPIRV/semantic.group-thread-id.uint2.cs.hlsl

@@ -0,0 +1,12 @@
+// Run: %dxc -T cs_6_0 -E main
+
+// CHECK: OpEntryPoint GLCompute %main "main" %gl_LocalInvocationID
+// CHECK: OpDecorate %gl_LocalInvocationID BuiltIn LocalInvocationId
+// CHECK: %gl_LocalInvocationID = OpVariable %_ptr_Input_v3uint Input
+
+// CHECK: [[gl_LocalInvocationID:%\d+]] = OpLoad %v3uint %gl_LocalInvocationID
+// CHECK:  [[uint2_GroupThreadID:%\d+]] = OpVectorShuffle %v2uint [[gl_LocalInvocationID]] [[gl_LocalInvocationID]] 0 1
+// CHECK:                                 OpStore %param_var_gtid [[uint2_GroupThreadID]]
+
+[numthreads(8, 8, 8)]
+void main(uint2 gtid : SV_GroupThreadID) {}

+ 18 - 0
tools/clang/unittests/SPIRV/CodeGenSPIRVTest.cpp

@@ -494,10 +494,28 @@ TEST_F(FileTest, SemanticIsFrontFacePS) {
 TEST_F(FileTest, SemanticDispatchThreadId) {
   runFileTest("semantic.dispatch-thread-id.cs.hlsl");
 }
+TEST_F(FileTest, SemanticDispatchThreadIdUint) {
+  runFileTest("semantic.dispatch-thread-id.uint.cs.hlsl");
+}
+TEST_F(FileTest, SemanticDispatchThreadIdUint2) {
+  runFileTest("semantic.dispatch-thread-id.uint2.cs.hlsl");
+}
 TEST_F(FileTest, SemanticGroupID) { runFileTest("semantic.group-id.cs.hlsl"); }
+TEST_F(FileTest, SemanticGroupIDUint) {
+  runFileTest("semantic.group-id.uint.cs.hlsl");
+}
+TEST_F(FileTest, SemanticGroupIDUint2) {
+  runFileTest("semantic.group-id.uint2.cs.hlsl");
+}
 TEST_F(FileTest, SemanticGroupThreadID) {
   runFileTest("semantic.group-thread-id.cs.hlsl");
 }
+TEST_F(FileTest, SemanticGroupThreadIDUint) {
+  runFileTest("semantic.group-thread-id.uint.cs.hlsl");
+}
+TEST_F(FileTest, SemanticGroupThreadIDUint2) {
+  runFileTest("semantic.group-thread-id.uint2.cs.hlsl");
+}
 TEST_F(FileTest, SemanticGroupIndex) {
   runFileTest("semantic.group-index.cs.hlsl");
 }