Sfoglia il codice sorgente

Set default global varaibles to const (#117)

Implement default global varaibles.
According to HLSL documentation, the global variables are set to default const unless it is specified with 'static' specifier. The front end therefore should assign 'const' specifiers for all global variables that will be in global constant buffers. We also show warnings for 'extern const' variables that have initializers since these variables should be imported from external sources.
Young Kim 8 anni fa
parent
commit
dc00d74028

+ 2 - 0
tools/clang/include/clang/Sema/SemaHLSL.h

@@ -243,6 +243,8 @@ clang::QualType CheckVectorConditional(
 
 }
 
+bool IsTypeNumeric(_In_ clang::Sema* self, _In_ clang::QualType &type);
+
 // This function reads the given declaration TSS and returns the corresponding parsedType with the
 // corresponding type. Replaces the given parsed type with the new type
 clang::QualType ApplyTypeSpecSignToParsedType(

+ 45 - 0
tools/clang/lib/Parse/ParseDecl.cpp

@@ -2175,6 +2175,51 @@ Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
     return DeclGroupPtrTy();
   }
 
+  // HLSL Change Starts: change global variables that will be in constant buffer to be constant by default 
+  // Global variables that are groupshared, static, or typedef 
+  // will not be part of constant buffer and therefore should not be const by default.
+  if (getLangOpts().HLSL && !D.isFunctionDeclarator() &&
+      D.getContext() == Declarator::TheContext::FileContext &&
+      DS.getStorageClassSpec() != DeclSpec::SCS::SCS_static &&
+      DS.getStorageClassSpec() != DeclSpec::SCS::SCS_typedef
+      ) {
+
+    // Check whether or not there is a 'groupshared' attribute
+    AttributeList *attrList = DS.getAttributes().getList();
+    bool isGroupShared = false;
+    while (attrList) {
+        if (attrList->getName()->getName().compare(
+            StringRef(tok::getTokenName(tok::kw_groupshared))) == 0) {
+            isGroupShared = true;
+            break;
+        }
+      attrList = attrList->getNext();
+    }
+    if (!isGroupShared) {
+      // check whether or not the given data is the typename or primitive types
+      if (DS.isTypeRep()) {
+        QualType type = DS.getRepAsType().get();
+        // canonical types of HLSL Object types are not canonical for some
+        // reason. other HLSL Object types of vector/matrix/array should be
+        // treated as const.
+        if (type.getCanonicalType().isCanonical() &&
+            IsTypeNumeric(&Actions, type)) {
+          unsigned int diagID;
+          const char *prevSpec;
+          DS.SetTypeQual(DeclSpec::TQ_const, D.getDeclSpec().getLocStart(),
+                         prevSpec, diagID, getLangOpts());
+        }
+      } else {
+        // If not a typename, it is a basic type and should be treated as const.
+        unsigned int diagID;
+        const char *prevSpec;
+        DS.SetTypeQual(DeclSpec::TQ_const, D.getDeclSpec().getLocStart(),
+                       prevSpec, diagID, getLangOpts());
+      }
+    }
+  }
+  // HLSL Change Ends
+
   // Save late-parsed attributes for now; they need to be parsed in the
   // appropriate function scope after the function Decl has been constructed.
   // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.

+ 1 - 2
tools/clang/lib/Sema/SemaDecl.cpp

@@ -9392,8 +9392,7 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init,
   } else if (VDecl->isFileVarDecl()) {
     if (VDecl->getStorageClass() == SC_Extern &&
         (!getLangOpts().CPlusPlus ||
-         !(Context.getBaseElementType(VDecl->getType()).isConstQualified() ||
-           VDecl->isExternC())) &&
+          !VDecl->isExternC()) &&
         !isTemplateInstantiation(VDecl->getTemplateSpecializationKind()))
       Diag(VDecl->getLocation(), diag::warn_extern_init);
 

+ 6 - 0
tools/clang/lib/Sema/SemaHLSL.cpp

@@ -10812,6 +10812,11 @@ clang::QualType hlsl::CheckVectorConditional(
   return HLSLExternalSource::FromSema(self)->CheckVectorConditional(Cond, LHS, RHS, QuestionLoc);
 }
 
+bool IsTypeNumeric(_In_ clang::Sema* self, _In_ clang::QualType &type) {
+  UINT count;
+  return HLSLExternalSource::FromSema(self)->IsTypeNumeric(type, &count);
+}
+
 void Sema::CheckHLSLArrayAccess(const Expr *expr) {
   DXASSERT_NOMSG(isa<CXXOperatorCallExpr>(expr));
   const CXXOperatorCallExpr *OperatorCallExpr = cast<CXXOperatorCallExpr>(expr);
@@ -10846,6 +10851,7 @@ void Sema::CheckHLSLArrayAccess(const Expr *expr) {
       }
   }
 }
+
 clang::QualType ApplyTypeSpecSignToParsedType(
     _In_ clang::Sema* self,
     _In_ clang::QualType &type,

+ 1 - 1
tools/clang/test/HLSL/attributes.hlsl

@@ -313,7 +313,7 @@ float4 f4;
 [clipplanes(float4(1, 2, f, 4))] // expected-error {{invalid expression for clipplanes argument: must be global reference, member access or array subscript}} fxc-pass {{}}
 void clipplanes_literals();
 
-[clipplanes(b)]           // expected-error {{clipplanes argument must be a float4 type but is 'bool'}} fxc-pass {{}}
+[clipplanes(b)]           // expected-error {{clipplanes argument must be a float4 type but is 'const bool'}} fxc-pass {{}}
 void clipplanes_const();
 
 // fxc error X3084: Clip plane attribute parameters must be non-literal constants

+ 48 - 0
tools/clang/test/HLSL/const-default.hlsl

@@ -0,0 +1,48 @@
+// RUN: %clang_cc1 -fsyntax-only -Wno-unused-value  -ffreestanding -verify %s
+
+float g_float1;                                             /* expected-note {{variable 'g_float1' declared const here}} expected-note {{variable 'g_float1' declared const here}} */
+int4 g_vec1;                                                /* expected-note {{variable 'g_vec1' declared const here}} expected-note {{variable 'g_vec1' declared const here}} */
+uint64_t3x4 g_mat1;
+
+cbuffer g_cbuffer {
+    min12int m_buffer_min12int;                             /* expected-note {{variable 'm_buffer_min12int' declared const here}} expected-warning {{min12int is promoted to min16int}} */
+    float4 m_buffer_float4;                                 /* expected-note {{variable 'm_buffer_float4' declared const here}} */
+    int3x4 m_buffer_int3x4;
+}
+
+tbuffer g_tbuffer {
+    float m_tbuffer_float;                                  /* expected-note {{variable 'm_tbuffer_float' declared const here}} */
+    int3 m_tbuffer_int3;                                    /* expected-note {{variable 'm_tbuffer_int3' declared const here}} */
+    double2x1 m_tbuffer_double2x1;                          /* expected-note {{variable 'm_tbuffer_double2x1' declared const here}} */
+}
+
+struct MyStruct {
+    float3 my_float3;
+    int3x4 my_int3x4;
+};
+
+ConstantBuffer<MyStruct> g_const_buffer;
+TextureBuffer<MyStruct> g_texture_buffer;                
+
+float4 main() : SV_TARGET
+{
+    g_float1 = g_float1 + 10.0;                             /* expected-error {{cannot assign to variable 'g_float1' with const-qualified type 'const float'}} */
+    g_float1 += 3.5;                                         /* expected-error {{cannot assign to variable 'g_float1' with const-qualified type 'const float'}} */
+    g_vec1 = g_vec1 + 3;                                    /* expected-error {{cannot assign to variable 'g_vec1' with const-qualified type 'const int4'}} */
+    g_vec1 -= 1;                                            /* expected-error {{cannot assign to variable 'g_vec1' with const-qualified type 'const int4'}} */
+    g_mat1._12 = 3;                                         /* expected-error {{read-only variable is not assignable}} */
+    g_mat1._34 *= 4;                                        /* expected-error {{read-only variable is not assignable}} */
+    m_buffer_min12int = 12;                                 /* expected-error {{cannot assign to variable 'm_buffer_min12int' with const-qualified type 'const min12int'}} */
+    m_buffer_float4 += 3.4;                                 /* expected-error {{cannot assign to variable 'm_buffer_float4' with const-qualified type 'const float4'}} */
+    m_buffer_int3x4._m01 -= 10;                             /* expected-error {{read-only variable is not assignable}} */
+    m_tbuffer_float *= 2;                                   /* expected-error {{cannot assign to variable 'm_tbuffer_float' with const-qualified type 'const float'}} */
+    m_tbuffer_int3 = 10;                                    /* expected-error {{cannot assign to variable 'm_tbuffer_int3' with const-qualified type 'const int3'}} */
+    m_tbuffer_double2x1 *= 3;                               /* expected-error {{cannot assign to variable 'm_tbuffer_double2x1' with const-qualified type 'const double2x1'}} */
+
+    g_const_buffer.my_float3.x = 1.5;                       /* expected-error {{read-only variable is not assignable}} */
+    g_const_buffer.my_int3x4._21 -= 2;                      /* expected-error {{read-only variable is not assignable}} */
+    g_texture_buffer.my_float3.y += 2.0;                      /* expected-error {{read-only variable is not assignable}} */
+    g_texture_buffer.my_int3x4._14 = 3;                     /* expected-error {{read-only variable is not assignable}} */
+
+    return (float4)g_float1;
+}

+ 14 - 14
tools/clang/test/HLSL/rewriter/correct_rewrites/attributes_gold.hlsl

@@ -18,11 +18,11 @@ int long_unroll() {
 
 
 RWByteAddressBuffer bab;
-int bab_address;
-bool g_bool;
-uint g_uint;
-uint g_dealiasTableOffset;
-uint g_dealiasTableSize;
+const int bab_address;
+const bool g_bool;
+const uint g_uint;
+const uint g_dealiasTableOffset;
+const uint g_dealiasTableSize;
 int uav() {
   uint i;
   [allow_uav_condition]
@@ -50,11 +50,11 @@ HSFoo HSMain(InputPatch<HSFoo, 16> p, uint i : SV_OutputControlPointID, uint Pat
 }
 
 
-float4 cp4[2];
-int4 i4;
-float4 cp5;
-float4x4 m4;
-float f;
+const float4 cp4[2];
+const int4 i4;
+const float4 cp5;
+const float4x4 m4;
+const float f;
 struct global_struct {
   float4 cp5[5];
 };
@@ -67,11 +67,11 @@ float4 myexpr() {
 
 
 static const float4 f4_const = float4(1, 2, 3, 4);
-bool b;
-int clip_index;
+const bool b;
+const int clip_index;
 static const bool b_true = true;
-global_struct gs;
-float4 f4;
+const global_struct gs;
+const float4 f4;
 [clipplanes(f4, cp4[0], gs.cp5[2])]
 float4 clipplanes_good();
 [clipplanes((f4), cp4[(0)], (gs).cp5[2], ((gs).cp5[2]))]

+ 12 - 12
tools/clang/test/HLSL/rewriter/correct_rewrites/cpp-errors_gold.hlsl

@@ -1,14 +1,14 @@
 // Rewrite unchanged result:
-float f_arr_empty_init[] = { 1, 2, 3 };
+const float f_arr_empty_init[] = { 1, 2, 3 };
 struct s_arr_i_f {
   int i;
   float f;
 };
-s_arr_i_f arr_struct_none[] = {  };
-s_arr_i_f arr_struct_one[] = { 1, 2 };
-s_arr_i_f arr_struct_two[] = { 1, 2, 3, 4 };
-int g_int;
-unsigned int g_unsigned_int;
+const s_arr_i_f arr_struct_none[] = {  };
+const s_arr_i_f arr_struct_one[] = { 1, 2 };
+const s_arr_i_f arr_struct_two[] = { 1, 2, 3, 4 };
+const int g_int;
+const unsigned int g_unsigned_int;
 struct s_with_bitfield {
   int f_bitfield : 3;
 };
@@ -36,7 +36,7 @@ float4 fn_with_semantic_arg(float4 arg : SV_SOMETHING) : SV_Target0 {
 namespace MyNS {
 }
 namespace MyNs {
-  int my_ns_extension;
+  const int my_ns_extension;
 }
 namespace MyNs2 {
   float outsideFunc(int x);
@@ -75,15 +75,15 @@ struct forward_struct;
 struct my_struct_type_decl {
   int a;
 };
-struct my_struct_type_decl my_struct_var_decl;
+const struct my_struct_type_decl my_struct_var_decl;
 struct my_struct_type_init {
   int a;
 };
-struct my_struct_type_init my_struct_type_init_one = { 1 };
-struct my_struct_type_init my_struct_type_init_two = { 2 };
+const struct my_struct_type_init my_struct_type_init_one = { 1 };
+const struct my_struct_type_init my_struct_type_init_two = { 2 };
 struct {
   int my_anon_struct_field;
-} my_anon_struct_type;
+} const my_anon_struct_type;
 void fn_my_struct_type_decl() {
   my_struct_type_decl local_var;
 }
@@ -219,7 +219,7 @@ void expressions() {
 }
 
 
-int unused_i;
+const int unused_i;
 float4 plain(float4 param4) {
   return is_supported();
 }

+ 4 - 4
tools/clang/test/HLSL/rewriter/correct_rewrites/effects-syntax_gold.hlsl

@@ -22,7 +22,7 @@ float4 main() : SV_Target {
 }
 
 
-int TechNiQue;
-int foobar3;
-int T5;
-int foobar4;
+const int TechNiQue;
+const int foobar3;
+const int T5;
+const int foobar4;

+ 2 - 2
tools/clang/test/HLSL/rewriter/correct_rewrites/intrinsic-examples_gold.hlsl

@@ -16,8 +16,8 @@ struct VS_OUT {
   float4 ChannelSelector : TEXCOORD1;
 };
 cbuffer OncePerDrawText : register(b0) {
-  float2 TexScale : register(c0);
-  float4 Color : register(c1);
+  const float2 TexScale : register(c0);
+  const float4 Color : register(c1);
 }
 ;
 sampler FontTexture;

+ 22 - 22
tools/clang/test/HLSL/rewriter/correct_rewrites/packreg_gold.hlsl

@@ -1,12 +1,12 @@
 // Rewrite unchanged result:
-float4 f_no_conflict : register(vs, c0) : register(ps, c1);
+const float4 f_no_conflict : register(vs, c0) : register(ps, c1);
 cbuffer MyFloats {
-  float4 f4_simple : packoffset(c0);
+  const float4 f4_simple : packoffset(c0);
 }
 tbuffer OtherFloats {
-  float4 f4_t_simple : packoffset(c10);
-  float3 f3_t_simple : packoffset(c11.y);
-  float2 f2_t_simple : packoffset(c12.z);
+  const float4 f4_t_simple : packoffset(c10);
+  const float3 f3_t_simple : packoffset(c11.y);
+  const float2 f2_t_simple : packoffset(c12.z);
 }
 sampler myVar : register(ps_6_0, s0);
 sampler myVar2 : register(vs, s0[8]);
@@ -21,45 +21,45 @@ sampler myVar_s : register(ps, s0);
 Texture2D myVar_t : register(ps, t0);
 Texture2D myVar_t_1 : register(ps, t0[1]);
 Texture2D myVar_t_1_1 : register(ps, t1[1]);
-float4 myVar_b : register(ps, b0);
-bool myVar_bool : register(ps, b0) : register(ps, c0);
+const float4 myVar_b : register(ps, b0);
+const bool myVar_bool : register(ps, b0) : register(ps, c0);
 sampler myVar_1 : register(ps, s0[1]);
 sampler myVar_11 : register(ps, s0[2]);
 sampler myVar_16 : register(ps, s0[15]);
 sampler myVar_n1p5 : register(ps, s0);
 sampler myVar_s1 : register(ps, s0[1]);
 cbuffer MyBuffer {
-  float4 Element1 : packoffset(c0);
-  float1 Element2 : packoffset(c1);
-  float1 Element3 : packoffset(c1.y);
-  float4 Element4 : packoffset(c10) : packoffset(c10);
+  const float4 Element1 : packoffset(c0);
+  const float1 Element2 : packoffset(c1);
+  const float1 Element3 : packoffset(c1.y);
+  const float4 Element4 : packoffset(c10) : packoffset(c10);
 }
 Texture2D<float4> Texture_ : register(t0);
 sampler Sampler : register(s0);
 cbuffer Parameters : register(b0) {
-  float4 DiffuseColor : packoffset(c0) : register(c0);
-  float4 AlphaTest : packoffset(c1);
-  float3 FogColor : packoffset(c2);
-  float4 FogVector : packoffset(c3);
-  float4x4 WorldViewProj : packoffset(c4);
+  const float4 DiffuseColor : packoffset(c0) : register(c0);
+  const float4 AlphaTest : packoffset(c1);
+  const float3 FogColor : packoffset(c2);
+  const float4 FogVector : packoffset(c3);
+  const float4x4 WorldViewProj : packoffset(c4);
 }
 ;
 cbuffer cbPerObject : register(b0) {
-  float4 g_vObjectColor : packoffset(c0);
+  const float4 g_vObjectColor : packoffset(c0);
 }
 ;
 cbuffer cbPerFrame : register(b1) {
-  float3 g_vLightDir : packoffset(c0);
-  float g_fAmbient : packoffset(c0.w);
+  const float3 g_vLightDir : packoffset(c0);
+  const float g_fAmbient : packoffset(c0.w);
 }
 ;
 cbuffer OuterBuffer {
-  float OuterItem0;
+  const float OuterItem0;
   cbuffer InnerBuffer {
-    float InnerItem0;
+    const float InnerItem0;
   }
   ;
-  float OuterItem1;
+  const float OuterItem1;
 }
 ;
 Texture2D g_txDiffuse : register(t0);

+ 32 - 32
tools/clang/test/HLSL/rewriter/correct_rewrites/typemods-syntax_gold.hlsl

@@ -1,20 +1,20 @@
 // Rewrite unchanged result:
-row_major float2x3 g_row;
-row_major snorm float2x3 g_row_sno;
-row_major unorm float2x3 g_row_uno;
-column_major float2x3 g_col;
-column_major snorm float2x3 g_col_sno;
-column_major unorm float2x3 g_col_uno;
-snorm float2x3 g_sno;
-unorm float2x3 g_uno;
-snorm float2x3 g_rev_sno;
-unorm float2x3 g_rev_uno;
-row_major float2x3 g_rev_row;
-column_major float2x3 g_rev_col;
-snorm float2 g_vector_sno;
-unorm float2 g_vector_uno;
-snorm float g_scalar_sno;
-unorm float g_scalar_uno;
+row_major float2x3 const g_row;
+row_major snorm float2x3 const g_row_sno;
+row_major unorm float2x3 const g_row_uno;
+column_major float2x3 const g_col;
+column_major snorm float2x3 const g_col_sno;
+column_major unorm float2x3 const g_col_uno;
+snorm float2x3 const g_sno;
+unorm float2x3 const g_uno;
+snorm float2x3 const g_rev_sno;
+unorm float2x3 const g_rev_uno;
+row_major float2x3 const g_rev_row;
+column_major float2x3 const g_rev_col;
+snorm float2 const g_vector_sno;
+unorm float2 const g_vector_uno;
+snorm float const g_scalar_sno;
+unorm float const g_scalar_uno;
 matrix<snorm float, 2, 3> g_mat_sno;
 matrix<unorm float, 2, 3> g_mat_uno;
 row_major matrix<snorm float, 2, 3> g_row_mat_sno;
@@ -40,22 +40,22 @@ static column_major unorm float2x3 s_col_uno_init = sc_col_uno_init_scalar;
 static snorm float2x3 s_sno_init = sc_sno_init_scalar;
 static unorm float2x3 s_uno_init = sc_uno_init_scalar;
 cbuffer CBInit {
-  row_major float2x3 g_row_init = sc_row_init_scalar;
-  row_major snorm float2x3 g_row_sno_init = sc_row_sno_init_scalar;
-  row_major unorm float2x3 g_row_uno_init = sc_row_uno_init_scalar;
-  column_major float2x3 g_col_init = sc_col_init_scalar;
-  column_major snorm float2x3 g_col_sno_init = sc_col_sno_init_scalar;
-  column_major unorm float2x3 g_col_uno_init = sc_col_uno_init_scalar;
-  snorm float2x3 g_sno_init = sc_sno_init_scalar;
-  unorm float2x3 g_uno_init = sc_uno_init_scalar;
-  row_major float2x3 g_row_init_scalar = 1.F;
-  row_major snorm float2x3 g_row_sno_init_scalar = 1.F;
-  row_major unorm float2x3 g_row_uno_init_scalar = 1.F;
-  column_major float2x3 g_col_init_scalar = 1.F;
-  column_major snorm float2x3 g_col_sno_init_scalar = 1.F;
-  column_major unorm float2x3 g_col_uno_init_scalar = 1.F;
-  snorm float2x3 g_sno_init_scalar = 1.F;
-  unorm float2x3 g_uno_init_scalar = 1.F;
+  row_major float2x3 const g_row_init = sc_row_init_scalar;
+  row_major snorm float2x3 const g_row_sno_init = sc_row_sno_init_scalar;
+  row_major unorm float2x3 const g_row_uno_init = sc_row_uno_init_scalar;
+  column_major float2x3 const g_col_init = sc_col_init_scalar;
+  column_major snorm float2x3 const g_col_sno_init = sc_col_sno_init_scalar;
+  column_major unorm float2x3 const g_col_uno_init = sc_col_uno_init_scalar;
+  snorm float2x3 const g_sno_init = sc_sno_init_scalar;
+  unorm float2x3 const g_uno_init = sc_uno_init_scalar;
+  row_major float2x3 const g_row_init_scalar = 1.F;
+  row_major snorm float2x3 const g_row_sno_init_scalar = 1.F;
+  row_major unorm float2x3 const g_row_uno_init_scalar = 1.F;
+  column_major float2x3 const g_col_init_scalar = 1.F;
+  column_major snorm float2x3 const g_col_sno_init_scalar = 1.F;
+  column_major unorm float2x3 const g_col_uno_init_scalar = 1.F;
+  snorm float2x3 const g_sno_init_scalar = 1.F;
+  unorm float2x3 const g_uno_init_scalar = 1.F;
 }
 typedef row_major float2x3 t_row;
 typedef row_major snorm float2x3 t_row_sno;

+ 10 - 10
tools/clang/test/HLSL/rewriter/correct_rewrites/varmods-syntax_gold.hlsl

@@ -3,23 +3,23 @@ groupshared float2 g_gro;
 groupshared precise float2 g_gro_pre;
 static groupshared precise float2 g_gro_pre_sta;
 static groupshared float2 g_gro_sta;
-extern float2 g_ext;
-extern precise float2 g_ext_pre;
-extern precise uniform float2 g_ext_pre_uni;
+extern const float2 g_ext;
+extern precise const float2 g_ext_pre;
+extern precise uniform const float2 g_ext_pre_uni;
 extern precise uniform const float2 g_ext_pre_uni_con;
 extern precise const float2 g_ext_pre_con;
-extern uniform float2 g_ext_uni;
+extern uniform const float2 g_ext_uni;
 extern uniform const float2 g_ext_uni_con;
 extern const float2 g_ext_con;
-precise float2 g_pre;
+precise const float2 g_pre;
 static precise float2 g_pre_sta;
 static precise const float2 g_pre_sta_con;
-precise uniform float2 g_pre_uni;
+precise uniform const float2 g_pre_uni;
 precise uniform const float2 g_pre_uni_con;
 precise const float2 g_pre_con;
 static float2 g_sta;
 static const float2 g_sta_con;
-uniform float2 g_uni;
+uniform const float2 g_uni;
 uniform const float2 g_uni_con;
 const float2 g_con;
 groupshared float g_gro_init = 1.F;
@@ -30,13 +30,13 @@ groupshared precise const float g_gro_pre_con_init = 1.F;
 static groupshared float g_gro_sta_init = 1.F;
 static groupshared const float g_gro_sta_con_init = 1.F;
 groupshared const float g_gro_con_init = 1.F;
-precise float g_pre_init = 1.F;
+precise const float g_pre_init = 1.F;
 static precise float g_pre_sta_init = 1.F;
 static precise const float g_pre_sta_con_init = 1.F;
-precise uniform float g_pre_uni_init = 1.F;
+precise uniform const float g_pre_uni_init = 1.F;
 static float g_sta_init = 1.F;
 static const float g_sta_con_init = 1.F;
-uniform float g_uni_init = 1.F;
+uniform const float g_uni_init = 1.F;
 typedef volatile float2 t_pre_vol;
 typedef const volatile float2 t_pre_vol_con;
 typedef const float2 t_pre_con;

+ 4 - 4
tools/clang/test/HLSL/rewriter/varmods-syntax_noerr.hlsl

@@ -163,17 +163,17 @@ groupshared const float g_gro_con_init = 1.0f;
 //extern precise uniform float g_ext_pre_uni_init = 1.0f;     /* expected-warning {{'extern' variable has an initializer}} fxc-pass {{}} */
 //extern precise uniform volatile float g_ext_pre_uni_vol_init = 1.0f;    /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_ext_pre_uni_vol_init': global variables cannot be declared 'volatile'}} */
 //extern precise uniform volatile const float g_ext_pre_uni_vol_con_init = 1.0f;    /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_ext_pre_uni_vol_con_init': global variables cannot be declared 'volatile'}} */
-//extern precise uniform const float g_ext_pre_uni_con_init = 1.0f;    /* fxc-warning {{X3207: Initializer used on a global 'const' variable. This requires setting an external constant. If a literal is desired, use 'static const' instead.}} */
+//extern precise uniform const float g_ext_pre_uni_con_init = 1.0f;    /* expected-warning {{'extern' variable has an initializer}} fxc-warning {{X3207: Initializer used on a global 'const' variable. This requires setting an external constant. If a literal is desired, use 'static const' instead.}} */
 //extern precise volatile float g_ext_pre_vol_init = 1.0f;             /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_ext_pre_vol_init': global variables cannot be declared 'volatile'}} */
 //extern precise volatile const float g_ext_pre_vol_con_init = 1.0f;   /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_ext_pre_vol_con_init': global variables cannot be declared 'volatile'}} */
-//extern precise const float g_ext_pre_con_init = 1.0f;       /* fxc-warning {{X3207: Initializer used on a global 'const' variable. This requires setting an external constant. If a literal is desired, use 'static const' instead.}} */
+//extern precise const float g_ext_pre_con_init = 1.0f;       /* expected-warning {{'extern' variable has an initializer}} fxc-warning {{X3207: Initializer used on a global 'const' variable. This requires setting an external constant. If a literal is desired, use 'static const' instead.}} */
 //extern uniform float g_ext_uni_init = 1.0f;                 /* expected-warning {{'extern' variable has an initializer}} fxc-pass {{}} */
 //extern uniform volatile float g_ext_uni_vol_init = 1.0f;    /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_ext_uni_vol_init': global variables cannot be declared 'volatile'}} */
 //extern uniform volatile const float g_ext_uni_vol_con_init = 1.0f;    /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_ext_uni_vol_con_init': global variables cannot be declared 'volatile'}} */
-//extern uniform const float g_ext_uni_con_init = 1.0f;       /* fxc-warning {{X3207: Initializer used on a global 'const' variable. This requires setting an external constant. If a literal is desired, use 'static const' instead.}} */
+//extern uniform const float g_ext_uni_con_init = 1.0f;       /* expected-warning {{'extern' variable has an initializer}} fxc-warning {{X3207: Initializer used on a global 'const' variable. This requires setting an external constant. If a literal is desired, use 'static const' instead.}} */
 //extern volatile float g_ext_vol_init = 1.0f;                /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_ext_vol_init': global variables cannot be declared 'volatile'}} */
 //extern volatile const float g_ext_vol_con_init = 1.0f;      /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_ext_vol_con_init': global variables cannot be declared 'volatile'}} */
-//extern const float g_ext_con_init = 1.0f;                   /* fxc-warning {{X3207: Initializer used on a global 'const' variable. This requires setting an external constant. If a literal is desired, use 'static const' instead.}} */
+//extern const float g_ext_con_init = 1.0f;                   /* expected-warning {{'extern' variable has an initializer}} fxc-warning {{X3207: Initializer used on a global 'const' variable. This requires setting an external constant. If a literal is desired, use 'static const' instead.}} */
 precise float g_pre_init = 1.0f;
 precise static float g_pre_sta_init = 1.0f;
 //precise static volatile float g_pre_sta_vol_init = 1.0f;    /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_pre_sta_vol_init': global variables cannot be declared 'volatile'}} */

+ 4 - 4
tools/clang/test/HLSL/varmods-syntax.hlsl

@@ -163,17 +163,17 @@ extern precise float g_ext_pre_init = 1.0f;                 /* expected-warning
 extern precise uniform float g_ext_pre_uni_init = 1.0f;     /* expected-warning {{'extern' variable has an initializer}} fxc-pass {{}} */
 extern precise uniform volatile float g_ext_pre_uni_vol_init = 1.0f;    /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_ext_pre_uni_vol_init': global variables cannot be declared 'volatile'}} */
 extern precise uniform volatile const float g_ext_pre_uni_vol_con_init = 1.0f;    /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_ext_pre_uni_vol_con_init': global variables cannot be declared 'volatile'}} */
-extern precise uniform const float g_ext_pre_uni_con_init = 1.0f;    /* fxc-warning {{X3207: Initializer used on a global 'const' variable. This requires setting an external constant. If a literal is desired, use 'static const' instead.}} */
+extern precise uniform const float g_ext_pre_uni_con_init = 1.0f;    /* expected-warning {{'extern' variable has an initializer}} fxc-warning {{X3207: Initializer used on a global 'const' variable. This requires setting an external constant. If a literal is desired, use 'static const' instead.}} */
 extern precise volatile float g_ext_pre_vol_init = 1.0f;             /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_ext_pre_vol_init': global variables cannot be declared 'volatile'}} */
 extern precise volatile const float g_ext_pre_vol_con_init = 1.0f;   /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_ext_pre_vol_con_init': global variables cannot be declared 'volatile'}} */
-extern precise const float g_ext_pre_con_init = 1.0f;       /* fxc-warning {{X3207: Initializer used on a global 'const' variable. This requires setting an external constant. If a literal is desired, use 'static const' instead.}} */
+extern precise const float g_ext_pre_con_init = 1.0f;       /* expected-warning {{'extern' variable has an initializer}} fxc-warning {{X3207: Initializer used on a global 'const' variable. This requires setting an external constant. If a literal is desired, use 'static const' instead.}} */
 extern uniform float g_ext_uni_init = 1.0f;                 /* expected-warning {{'extern' variable has an initializer}} fxc-pass {{}} */
 extern uniform volatile float g_ext_uni_vol_init = 1.0f;    /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_ext_uni_vol_init': global variables cannot be declared 'volatile'}} */
 extern uniform volatile const float g_ext_uni_vol_con_init = 1.0f;    /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_ext_uni_vol_con_init': global variables cannot be declared 'volatile'}} */
-extern uniform const float g_ext_uni_con_init = 1.0f;       /* fxc-warning {{X3207: Initializer used on a global 'const' variable. This requires setting an external constant. If a literal is desired, use 'static const' instead.}} */
+extern uniform const float g_ext_uni_con_init = 1.0f;       /* expected-warning {{'extern' variable has an initializer}} fxc-warning {{X3207: Initializer used on a global 'const' variable. This requires setting an external constant. If a literal is desired, use 'static const' instead.}} */
 extern volatile float g_ext_vol_init = 1.0f;                /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_ext_vol_init': global variables cannot be declared 'volatile'}} */
 extern volatile const float g_ext_vol_con_init = 1.0f;      /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_ext_vol_con_init': global variables cannot be declared 'volatile'}} */
-extern const float g_ext_con_init = 1.0f;                   /* fxc-warning {{X3207: Initializer used on a global 'const' variable. This requires setting an external constant. If a literal is desired, use 'static const' instead.}} */
+extern const float g_ext_con_init = 1.0f;                   /* expected-warning {{'extern' variable has an initializer}} fxc-warning {{X3207: Initializer used on a global 'const' variable. This requires setting an external constant. If a literal is desired, use 'static const' instead.}} */
 precise float g_pre_init = 1.0f;
 precise static float g_pre_sta_init = 1.0f;
 precise static volatile float g_pre_sta_vol_init = 1.0f;    /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_pre_sta_vol_init': global variables cannot be declared 'volatile'}} */

+ 1 - 1
tools/clang/unittests/HLSL/DXIsenseTest.cpp

@@ -757,5 +757,5 @@ void DXIntellisenseTest::TypeWhenICEThenEval()
   VERIFY_SUCCEEDED(cCursor->GetCursorType(&typeCursor));
   CComHeapPtr<char> name;
   VERIFY_SUCCEEDED(typeCursor->GetSpelling(&name));
-  VERIFY_ARE_EQUAL_STR("float [2]", name);
+  VERIFY_ARE_EQUAL_STR("const float [2]", name); // global variables converted to const by default
 }

+ 5 - 5
tools/clang/unittests/HLSL/RewriterTest.cpp

@@ -332,7 +332,7 @@ TEST_F(RewriterTest, RunUTF16OneByte) {
   CComPtr<IDxcBlob> result;
   VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
 
-  VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(), "// Rewrite unchanged result:\n\x69\x6e\x74\x20\x69\x3b\n") == 0); 
+  VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(), "// Rewrite unchanged result:\n\x63\x6f\x6e\x73\x74\x20\x69\x6e\x74\x20\x69\x3b\n") == 0); // const added by default
 }
 
 TEST_F(RewriterTest, RunUTF16TwoByte) {
@@ -350,7 +350,7 @@ TEST_F(RewriterTest, RunUTF16TwoByte) {
   CComPtr<IDxcBlob> result;
   VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
 
-  VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(), "// Rewrite unchanged result:\n\x69\x6e\x74\x20\xc3\xad\xc3\xb1\xc5\xa7\x3b\n") == 0);
+  VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(), "// Rewrite unchanged result:\n\x63\x6f\x6e\x73\x74\x20\x69\x6e\x74\x20\xc3\xad\xc3\xb1\xc5\xa7\x3b\n") == 0); // const added by default
 }
 
 TEST_F(RewriterTest, RunUTF16ThreeByteBadChar) {
@@ -368,7 +368,7 @@ TEST_F(RewriterTest, RunUTF16ThreeByteBadChar) {
   CComPtr<IDxcBlob> result;
   VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
 
-  VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(), "// Rewrite unchanged result:\n\x69\x6e\x74\x20\x41\x3b\n") == 0); //"int A;" -> should remove the weird characters
+  VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(), "// Rewrite unchanged result:\n\x63\x6f\x6e\x73\x74\x20\x69\x6e\x74\x20\x41\x3b\n") == 0); //"const int A;" -> should remove the weird characters
 }
 
 TEST_F(RewriterTest, RunUTF16ThreeByte) {
@@ -386,7 +386,7 @@ TEST_F(RewriterTest, RunUTF16ThreeByte) {
   CComPtr<IDxcBlob> result;
   VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
 
-  VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(), "// Rewrite unchanged result:\n\x69\x6e\x74\x20\xe1\xba\x8b\x3b\n") == 0);
+  VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(), "// Rewrite unchanged result:\n\x63\x6f\x6e\x73\x74\x20\x69\x6e\x74\x20\xe1\xba\x8b\x3b\n") == 0); // const added by default
 }
 
 TEST_F(RewriterTest, RunNonUnicode) {
@@ -404,7 +404,7 @@ TEST_F(RewriterTest, RunNonUnicode) {
   CComPtr<IDxcBlob> result;
   VERIFY_SUCCEEDED(pRewriteResult->GetResult(&result));
 
-  VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(), "// Rewrite unchanged result:\n\x69\x6e\x74\x20\xce\xb1\xce\xb2\xce\xb3\x3b\n") == 0);
+  VERIFY_IS_TRUE(strcmp(BlobToUtf8(result).c_str(), "// Rewrite unchanged result:\n\x63\x6f\x6e\x73\x74\x20\x69\x6e\x74\x20\xce\xb1\xce\xb2\xce\xb3\x3b\n") == 0); // const added by default
 }
 
 TEST_F(RewriterTest, RunEffect) {

+ 5 - 0
tools/clang/unittests/HLSL/VerifierTest.cpp

@@ -33,6 +33,7 @@ public:
   TEST_METHOD(RunAttributes);
   TEST_METHOD(RunConstExpr);
   TEST_METHOD(RunConstAssign);
+  TEST_METHOD(RunConstDefault);
   TEST_METHOD(RunCppErrors);
   TEST_METHOD(RunFunctions);
   TEST_METHOD(RunIndexingOperator);
@@ -127,6 +128,10 @@ TEST_F(VerifierTest, RunConstAssign) {
   CheckVerifiesHLSL(L"const-assign.hlsl");
 }
 
+TEST_F(VerifierTest, RunConstDefault) {
+  CheckVerifiesHLSL(L"const-default.hlsl");
+}
+
 TEST_F(VerifierTest, RunCppErrors) {
   CheckVerifiesHLSL(L"cpp-errors.hlsl");
 }