Browse Source

[spirv] Ignore static when seeing both static and groupshared (#1101)

Lei Zhang 7 years ago
parent
commit
c8970cdf6b

+ 2 - 0
docs/SPIR-V.rst

@@ -823,6 +823,8 @@ placed in the ``Uniform`` or ``UniformConstant`` storage class.
 
   - Global variables with ``groupshared`` modifier will be placed in the
     ``Workgroup`` storage class.
+  - Note that this modifier overrules ``static``; if both ``groupshared`` and
+    ``static`` are applied to a variable, ``static`` will be ignored.
 
 - ``uinform``
 

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

@@ -159,7 +159,12 @@ const Expr *isStructuredBufferLoad(const Expr *expr, const Expr **index) {
 /// Returns true if the given VarDecl will be translated into a SPIR-V variable
 /// not in the Private or Function storage class.
 inline bool isExternalVar(const VarDecl *var) {
-  return var->isExternallyVisible() && !var->isStaticDataMember();
+  // Class static variables should be put in the Private storage class.
+  // groupshared variables are allowed to be declared as "static". But we still
+  // need to put them in the Workgroup storage class. That is, when seeing
+  // "static groupshared", ignore "static".
+  return var->isExternallyVisible() ? !var->isStaticDataMember()
+                                    : var->getAttr<HLSLGroupSharedAttr>();
 }
 
 /// Returns the referenced variable's DeclContext if the given expr is

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

@@ -14,7 +14,7 @@ struct S {
 // CHECK: %a = OpVariable %_ptr_Workgroup_float Workgroup
 groupshared              float    a;
 // CHECK: %b = OpVariable %_ptr_Workgroup_v3float Workgroup
-groupshared              float3   b;
+static groupshared       float3   b;  // Ignore static modifier
 // CHECK: %c = OpVariable %_ptr_Workgroup_mat2v3float Workgroup
 groupshared column_major float2x3 c;
 // CHECK: %d = OpVariable %_ptr_Workgroup__arr_v2float_uint_5 Workgroup