Browse Source

[spirv] Support signed integer types for some builtins (#1215)

* SV_DispatchThreadID
* SV_GroupID
* SV_GroupThreadID
Lei Zhang 7 years ago
parent
commit
794840785f

+ 13 - 10
tools/clang/lib/SPIRV/DeclResultIdMapper.cpp

@@ -1243,11 +1243,9 @@ 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.
+    // * SV_DispatchThreadID, SV_GroupThreadID, and SV_GroupID are allowed to be
+    //   uint, uint2, or uint3, but the corresponding builtins
+    //   (GlobalInvocationId, LocalInvocationId, WorkgroupId) must be a uint3.
 
     if (glPerVertex.tryToAccess(sigPoint->GetKind(), semanticKind,
                                 semanticToUse->index, invocationId, value,
@@ -1255,6 +1253,7 @@ bool DeclResultIdMapper::createStageVars(const hlsl::SigPoint *sigPoint,
       return true;
 
     const uint32_t srcTypeId = typeId; // Variable type in source code
+    uint32_t srcVecElemTypeId = 0;     // Variable element type if vector
 
     switch (semanticKind) {
     case hlsl::Semantic::Kind::DomainLocation:
@@ -1280,7 +1279,10 @@ bool DeclResultIdMapper::createStageVars(const hlsl::SigPoint *sigPoint,
     case hlsl::Semantic::Kind::DispatchThreadID:
     case hlsl::Semantic::Kind::GroupThreadID:
     case hlsl::Semantic::Kind::GroupID:
-      typeId = theBuilder.getVecType(theBuilder.getUint32Type(), 3);
+      // Keep the original integer signedness
+      srcVecElemTypeId = typeTranslator.translateType(
+          hlsl::IsHLSLVecType(type) ? hlsl::GetHLSLVecElementType(type) : type);
+      typeId = theBuilder.getVecType(srcVecElemTypeId, 3);
       break;
     }
 
@@ -1419,15 +1421,16 @@ bool DeclResultIdMapper::createStageVars(const hlsl::SigPoint *sigPoint,
                 semanticKind == hlsl::Semantic::Kind::GroupID) &&
                (!hlsl::IsHLSLVecType(type) ||
                 hlsl::GetHLSLVecSize(type) != 3)) {
+        assert(srcVecElemTypeId);
         const auto vecSize =
             hlsl::IsHLSLVecType(type) ? hlsl::GetHLSLVecSize(type) : 1;
         if (vecSize == 1)
-          *value = theBuilder.createCompositeExtract(theBuilder.getUint32Type(),
-                                                     *value, {0});
+          *value =
+              theBuilder.createCompositeExtract(srcVecElemTypeId, *value, {0});
         else if (vecSize == 2)
           *value = theBuilder.createVectorShuffle(
-              theBuilder.getVecType(theBuilder.getUint32Type(), 2), *value,
-              *value, {0, 1});
+              theBuilder.getVecType(srcVecElemTypeId, 2), *value, *value,
+              {0, 1});
       }
     } else {
       if (noWriteBack)

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

@@ -0,0 +1,13 @@
+// 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_v3int Input
+
+// CHECK:                  %param_var_tid = OpVariable %_ptr_Function_v2int Function
+// CHECK:  [[gl_GlobalInvocationID:%\d+]] = OpLoad %v3int %gl_GlobalInvocationID
+// CHECK:  [[int2_DispatchThreadID:%\d+]] = OpVectorShuffle %v2int [[gl_GlobalInvocationID]] [[gl_GlobalInvocationID]] 0 1
+// CHECK:                                   OpStore %param_var_tid [[int2_DispatchThreadID]]
+
+[numthreads(8, 8, 8)]
+void main(int2 tid : SV_DispatchThreadId) {}

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

@@ -1,12 +0,0 @@
-// 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) {}

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

@@ -0,0 +1,13 @@
+// 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_v3int Input
+
+// CHECK:         %param_var_tid = OpVariable %_ptr_Function_v2int Function
+// CHECK: [[gl_WorkGrouID:%\d+]] = OpLoad %v3int %gl_WorkGroupID
+// CHECK:  [[int2_GroupID:%\d+]] = OpVectorShuffle %v2int [[gl_WorkGrouID]] [[gl_WorkGrouID]] 0 1
+// CHECK:                          OpStore %param_var_tid [[int2_GroupID]]
+
+[numthreads(8, 8, 8)]
+void main(int2 tid : SV_GroupID) {}

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

@@ -1,12 +0,0 @@
-// 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) {}

+ 13 - 0
tools/clang/test/CodeGenSPIRV/semantic.group-thread-id.int2.cs.hlsl

@@ -0,0 +1,13 @@
+// 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_v3int Input
+
+// CHECK:               %param_var_gtid = OpVariable %_ptr_Function_v2int Function
+// CHECK: [[gl_LocalInvocationID:%\d+]] = OpLoad %v3int %gl_LocalInvocationID
+// CHECK:   [[int2_GroupThreadID:%\d+]] = OpVectorShuffle %v2int [[gl_LocalInvocationID]] [[gl_LocalInvocationID]] 0 1
+// CHECK:                                 OpStore %param_var_gtid [[int2_GroupThreadID]]
+
+[numthreads(8, 8, 8)]
+void main(int2 gtid : SV_GroupThreadID) {}

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

@@ -1,12 +0,0 @@
-// 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) {}

+ 6 - 6
tools/clang/unittests/SPIRV/CodeGenSPIRVTest.cpp

@@ -501,15 +501,15 @@ TEST_F(FileTest, SemanticDispatchThreadId) {
 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, SemanticDispatchThreadIdInt2) {
+  runFileTest("semantic.dispatch-thread-id.int2.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, SemanticGroupIDInt2) {
+  runFileTest("semantic.group-id.int2.cs.hlsl");
 }
 TEST_F(FileTest, SemanticGroupThreadID) {
   runFileTest("semantic.group-thread-id.cs.hlsl");
@@ -517,8 +517,8 @@ TEST_F(FileTest, SemanticGroupThreadID) {
 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, SemanticGroupThreadIDInt2) {
+  runFileTest("semantic.group-thread-id.int2.cs.hlsl");
 }
 TEST_F(FileTest, SemanticGroupIndex) {
   runFileTest("semantic.group-index.cs.hlsl");