From 9a42ed51f58c002199f17dcb927ddd2d03d1c191 Mon Sep 17 00:00:00 2001 From: Chris B Date: Tue, 29 Sep 2026 18:19:50 -0500 Subject: [PATCH 1/6] [202x][0004] Disallow initializers on cbuffer members This PR does two things: first it refactors the existing warning on ignored global initializers to move the general case (implicit cbuffer) into Sema from the DXIL & SPIRV codegen layers. This generalization fixes issues where the warning wasn't firing, and expands it to all cbuffer members. Second, this PR turns that warning into an error in HLSL 202x+. Resolves #8483 --- docs/ReleaseNotes.md | 3 + .../include/clang/Basic/DiagnosticGroups.td | 1 + .../clang/Basic/DiagnosticSemaKinds.td | 5 ++ tools/clang/lib/CodeGen/CodeGenModule.cpp | 11 ---- tools/clang/lib/SPIRV/DeclResultIdMapper.cpp | 7 -- tools/clang/lib/SPIRV/SpirvEmitter.cpp | 21 +++--- tools/clang/lib/Sema/SemaDecl.cpp | 35 ++++++++++ .../groupshared.init.warning.hlsl | 2 +- .../CodeGenSPIRV/implicit.global.ubo.hlsl | 3 +- .../test/CodeGenSPIRV/var.globals.error.hlsl | 7 +- .../test/CodeGenSPIRV/var.init.cbuffer.hlsl | 11 ++-- .../test/CodeGenSPIRV/var.init.tbuffer.hlsl | 9 +-- .../var.init.warning.ignored.hlsl | 2 +- tools/clang/test/HLSL/cpp-errors-hv2015.hlsl | 2 +- tools/clang/test/HLSL/cpp-errors.hlsl | 12 ++-- .../test/HLSL/rewriter/cpp-errors_noerr.hlsl | 10 +-- .../HLSL/rewriter/typemods-syntax_noerr.hlsl | 34 +++++----- .../HLSL/rewriter/varmods-syntax_noerr.hlsl | 6 +- .../hlsl/auto/auto-no-reference.hlsl | 2 +- .../hlsl/buffer-initializers-202x.hlsl | 51 +++++++++++++++ .../hlsl/buffer-initializers-nowarn.hlsl | 18 ++++++ .../hlsl/buffer-initializers-pre202x.hlsl | 51 +++++++++++++++ .../rayquery-omm-diag-TU-export-sm65.hlsl | 4 +- .../test/SemaHLSL/removed-keywords-202x.hlsl | 7 +- .../test/SemaHLSL/subobjects-syntax.hlsl | 2 +- .../clang/test/SemaHLSL/typemods-syntax.hlsl | 62 +++++++++--------- .../v2021-static-assert-not-keyword.hlsl | 2 +- tools/clang/test/SemaHLSL/varmods-syntax.hlsl | 64 +++++++++---------- .../vector-syntax-exact-precision.hlsl | 4 +- tools/clang/test/SemaHLSL/vector-syntax.hlsl | 4 +- 30 files changed, 295 insertions(+), 157 deletions(-) create mode 100644 tools/clang/test/SemaHLSL/hlsl/buffer-initializers-202x.hlsl create mode 100644 tools/clang/test/SemaHLSL/hlsl/buffer-initializers-nowarn.hlsl create mode 100644 tools/clang/test/SemaHLSL/hlsl/buffer-initializers-pre202x.hlsl diff --git a/docs/ReleaseNotes.md b/docs/ReleaseNotes.md index 983ba0dcb0..4dc4817edf 100644 --- a/docs/ReleaseNotes.md +++ b/docs/ReleaseNotes.md @@ -51,6 +51,9 @@ line upon naming the release. Refer to previous for appropriate section names. - HLSL 202x now supports variadic templates and template function parameter packs, pack expansions, and `sizeof...()` [#8905](https://github.com/microsoft/DirectXShaderCompiler/issues/8905). +- HLSL 202x disallows initializers on variables in explicit and implicit + constant buffers and texture buffers + [#8483](https://github.com/microsoft/DirectXShaderCompiler/issues/8483). - HLSL 202x disallows putting cbuffer, tbuffer, or namespace declarations inside a cbuffer or tbuffer [#8484](https://github.com/microsoft/DirectXShaderCompiler/issues/8484). diff --git a/tools/clang/include/clang/Basic/DiagnosticGroups.td b/tools/clang/include/clang/Basic/DiagnosticGroups.td index 399223af30..9acf5e53b3 100644 --- a/tools/clang/include/clang/Basic/DiagnosticGroups.td +++ b/tools/clang/include/clang/Basic/DiagnosticGroups.td @@ -806,6 +806,7 @@ def HLSLParameterUsage : DiagGroup<"parameter-usage">; def HLSLAvailability: DiagGroup<"hlsl-availability">; def HLSLAvailabilityConstant: DiagGroup<"hlsl-availability-constant">; def HLSLBarrier : DiagGroup<"hlsl-barrier">; +def HLSLBufferInitializer : DiagGroup<"hlsl-buffer-initializer">; // HLSL 2026 compatibility warnings: these warn on code in older language modes // that changes meaning or becomes illegal in HLSL 2026. diff --git a/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td b/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td index b31ebc6077..ca64c04100 100644 --- a/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -7575,6 +7575,11 @@ def err_hlsl_packoffset_requires_cbuffer : Error< "packoffset is only allowed in a constant buffer">; def warn_hlsl_packoffset_mix : Warning< "cannot mix packoffset elements with nonpackoffset elements in a cbuffer">; +def warn_hlsl_buffer_initializer : Warning< + "initializer for a variable in a %select{tbuffer|cbuffer}0 will be ignored">, + InGroup; +def err_hlsl_buffer_initializer : Error< + "variable in a %select{tbuffer|cbuffer}0 cannot have an initializer">; def err_hlsl_packoffset_overlap : Error<"packoffset overlap between %0, %1">; def err_hlsl_register_semantics_conflicting : Error< "conflicting register semantics">; diff --git a/tools/clang/lib/CodeGen/CodeGenModule.cpp b/tools/clang/lib/CodeGen/CodeGenModule.cpp index b274ea9d64..dbbf1119e9 100644 --- a/tools/clang/lib/CodeGen/CodeGenModule.cpp +++ b/tools/clang/lib/CodeGen/CodeGenModule.cpp @@ -2109,12 +2109,6 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { NeedsGlobalCtor = true; // HLSL Change Begins. if (getLangOpts().HLSL && D->isExternallyVisible()) { - // For global constant with init, the init will be ignored. - // Warning here. - unsigned DiagID = Diags.getCustomDiagID( - DiagnosticsEngine::Warning, - "Initializer of external global will be ignored"); - Diags.Report(D->getLocation(), DiagID); // Don't create global ctor for it. NeedsGlobalCtor = false; } @@ -2131,12 +2125,7 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { DelayedCXXInitPosition.erase(D); // HLSL Change Begins. if (getLangOpts().HLSL && D->isExternallyVisible() && !D->isStaticDataMember()) { - // For global constant with init, the init will be ignored. Init = EmitNullConstant(D->getType()); - unsigned DiagID = Diags.getCustomDiagID( - DiagnosticsEngine::Warning, - "Initializer of external global will be ignored"); - Diags.Report(D->getLocation(), DiagID); } // HLSL Change Ends. } diff --git a/tools/clang/lib/SPIRV/DeclResultIdMapper.cpp b/tools/clang/lib/SPIRV/DeclResultIdMapper.cpp index c6affae44f..9aaa36c97b 100644 --- a/tools/clang/lib/SPIRV/DeclResultIdMapper.cpp +++ b/tools/clang/lib/SPIRV/DeclResultIdMapper.cpp @@ -1780,13 +1780,6 @@ void DeclResultIdMapper::createGlobalsCBuffer(const VarDecl *var) { uint32_t index = 0; for (const auto *decl : collectDeclsInDeclContext(context)) { if (const auto *varDecl = dyn_cast(decl)) { - if (!spirvOptions.noWarnIgnoredFeatures) { - if (const auto *init = varDecl->getInit()) - emitWarning( - "variable '%0' will be placed in $Globals so initializer ignored", - init->getExprLoc()) - << var->getName() << init->getSourceRange(); - } if (const auto *attr = varDecl->getAttr()) { emitError("variable '%0' will be placed in $Globals so cannot have " "vk::binding attribute", diff --git a/tools/clang/lib/SPIRV/SpirvEmitter.cpp b/tools/clang/lib/SPIRV/SpirvEmitter.cpp index a8c987288f..5d106be5a2 100644 --- a/tools/clang/lib/SPIRV/SpirvEmitter.cpp +++ b/tools/clang/lib/SPIRV/SpirvEmitter.cpp @@ -1937,21 +1937,11 @@ void SpirvEmitter::registerCapabilitiesAndExtensionsForVarDecl( void SpirvEmitter::doHLSLBufferDecl(const HLSLBufferDecl *bufferDecl) { // This is a cbuffer/tbuffer decl. - // Check and emit warnings for member intializers which are not - // supported in Vulkan for (const auto *member : bufferDecl->decls()) { if (const auto *varMember = dyn_cast(member)) { if (varMember->getStorageClass() == StorageClass::SC_Static) continue; - if (!spirvOptions.noWarnIgnoredFeatures) { - if (const auto *init = varMember->getInit()) - emitWarning("%select{tbuffer|cbuffer}0 member initializer " - "ignored since no Vulkan equivalent", - init->getExprLoc()) - << bufferDecl->isCBuffer() << init->getSourceRange(); - } - // We cannot handle external initialization of column-major matrices now. if (isOrContainsNonFpColMajorMatrix(astContext, spirvOptions, varMember->getType(), varMember)) { @@ -2199,9 +2189,14 @@ void SpirvEmitter::doVarDecl(const VarDecl *decl) { // variables) belongs to the Function storage class. if (isExternalVar(decl)) { var = declIdMapper.createExternVar(decl); - if (decl->hasInit()) { - emitWarning("Initializer of external global will be ignored", - decl->getLocation()); + // DXC does not support initializing groupshared memory when targeting + // SPIRV, so the initializer will be ignored. + if (decl->hasAttr() && + !spirvOptions.noWarnIgnoredFeatures) { + if (const auto *init = decl->getInit()) + emitWarning("initializer of 'groupshared' variable will be ignored", + init->getExprLoc()) + << init->getSourceRange(); } } else { // We already know the variable is not externally visible here. If it does diff --git a/tools/clang/lib/Sema/SemaDecl.cpp b/tools/clang/lib/Sema/SemaDecl.cpp index b6ed4fe8ca..d19a11abf1 100644 --- a/tools/clang/lib/Sema/SemaDecl.cpp +++ b/tools/clang/lib/Sema/SemaDecl.cpp @@ -66,6 +66,28 @@ Sema::DeclGroupPtrTy Sema::ConvertDeclToDeclGroup(Decl *Ptr, Decl *OwnedType) { namespace { +// HLSL Change Begin +// Returns true if VD is a shader constant: a variable stored in an explicit +// cbuffer/tbuffer declaration, or in the implicit $Globals constant buffer. +// On success IsConstantBuffer is false only for tbuffer members. +bool IsHLSLShaderConstant(const VarDecl *VD, bool &IsConstantBuffer) { + if (const auto *Buffer = dyn_cast(VD->getDeclContext())) { + IsConstantBuffer = Buffer->isCBuffer(); + return VD->getStorageClass() != SC_Static; + } + + IsConstantBuffer = true; + const DeclContext *DC = VD->getDeclContext(); + return (DC->isTranslationUnit() || DC->isNamespace()) && + VD->hasExternalFormalLinkage() && + !VD->hasAttr() && + !VD->hasAttr() && + !VD->hasAttr() && + !VD->hasAttr() && + hlsl::IsHLSLNumericOrAggregateOfNumericType(VD->getType()); +} +// HLSL Change End + class TypeNameValidatorCCC : public CorrectionCandidateCallback { public: TypeNameValidatorCCC(bool AllowInvalid, bool WantClass=false, @@ -9334,6 +9356,19 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, // Attach the initializer to the decl. VDecl->setInit(Init); + // HLSL Change Begin + // Variables in a cbuffer/tbuffer declaration, or in the implicit $Globals + // constant buffer, are initialized by the pipeline, so any initializer is + // ignored. This is an error starting with HLSL 202x. + bool IsConstantBuffer = false; + if (getLangOpts().HLSL && IsHLSLShaderConstant(VDecl, IsConstantBuffer)) { + Diag(Init->getExprLoc(), getLangOpts().HLSLVersion >= hlsl::LangStd::v202x + ? diag::err_hlsl_buffer_initializer + : diag::warn_hlsl_buffer_initializer) + << IsConstantBuffer << Init->getSourceRange(); + } + // HLSL Change End + if (VDecl->isLocalVarDecl()) { // C99 6.7.8p4: All the expressions in an initializer for an object that has // static storage duration shall be constant expressions or string literals. diff --git a/tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl b/tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl index c49534948b..76a1ac3e96 100644 --- a/tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl +++ b/tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl @@ -15,5 +15,5 @@ void main(uint local_thread_id_flat : SV_GroupIndex) { } } -// CHECK: warning: Initializer of external global will be ignored +// CHECK: warning: initializer of 'groupshared' variable will be ignored // CHECK-NEXT: groupshared uint testing = 0; \ No newline at end of file diff --git a/tools/clang/test/CodeGenSPIRV/implicit.global.ubo.hlsl b/tools/clang/test/CodeGenSPIRV/implicit.global.ubo.hlsl index e19376b895..a011be6d7d 100644 --- a/tools/clang/test/CodeGenSPIRV/implicit.global.ubo.hlsl +++ b/tools/clang/test/CodeGenSPIRV/implicit.global.ubo.hlsl @@ -1,4 +1,5 @@ -// RUN: %dxc -T ps_6_0 -E main %s -spirv | FileCheck %s +// RUN: %dxc -T ps_6_0 -E main -Werror %s -spirv | FileCheck %s +// RUN: %dxc -T ps_6_0 -E main -HV 202x %s -spirv | FileCheck %s // This test checks that the specialization constant and push constants are not // included in the implicit global ubo. diff --git a/tools/clang/test/CodeGenSPIRV/var.globals.error.hlsl b/tools/clang/test/CodeGenSPIRV/var.globals.error.hlsl index 0f7adb1206..526898f15c 100644 --- a/tools/clang/test/CodeGenSPIRV/var.globals.error.hlsl +++ b/tools/clang/test/CodeGenSPIRV/var.globals.error.hlsl @@ -1,8 +1,9 @@ // RUN: not %dxc -T vs_6_0 -E main -fcgl %s -spirv 2>&1 | FileCheck %s - +// Note: this test uses FileCheck instead of -verify because the error +// diagnostic comes from the code generation layer instead of sema. [[vk::binding(10, 2)]] float4 gVec = 1.0; float4 main() : A { return gVec; } -// CHECK: :3:38: warning: variable 'gVec' will be placed in $Globals so initializer ignored -// CHECK: :3:3: error: variable 'gVec' will be placed in $Globals so cannot have vk::binding attribute +// CHECK: :4:38: warning: initializer for a variable in a cbuffer will be ignored +// CHECK: :4:3: error: variable 'gVec' will be placed in $Globals so cannot have vk::binding attribute diff --git a/tools/clang/test/CodeGenSPIRV/var.init.cbuffer.hlsl b/tools/clang/test/CodeGenSPIRV/var.init.cbuffer.hlsl index 1f18dcaef6..aa489a006e 100644 --- a/tools/clang/test/CodeGenSPIRV/var.init.cbuffer.hlsl +++ b/tools/clang/test/CodeGenSPIRV/var.init.cbuffer.hlsl @@ -1,13 +1,10 @@ -// RUN: %dxc -T vs_6_0 -E main -fcgl %s -spirv 2>&1 | FileCheck %s +// RUN: %dxc -T vs_6_0 -E main -verify %s -spirv cbuffer MyCBuffer { - float a = 1.0; - float4 b = 2.0; + float a = 1.0; // expected-warning{{initializer for a variable in a cbuffer will be ignored}} + float4 b = 2.0; // expected-warning{{initializer for a variable in a cbuffer will be ignored}} }; float main() : A { return 1.0; -} - -// CHECK: :4:15: warning: cbuffer member initializer ignored since no Vulkan equivalent -// CHECK: :5:16: warning: cbuffer member initializer ignored since no Vulkan equivalent +} \ No newline at end of file diff --git a/tools/clang/test/CodeGenSPIRV/var.init.tbuffer.hlsl b/tools/clang/test/CodeGenSPIRV/var.init.tbuffer.hlsl index c727c7d148..b41b98a6ee 100644 --- a/tools/clang/test/CodeGenSPIRV/var.init.tbuffer.hlsl +++ b/tools/clang/test/CodeGenSPIRV/var.init.tbuffer.hlsl @@ -1,13 +1,10 @@ -// RUN: %dxc -T vs_6_0 -E main -fcgl %s -spirv 2>&1 | FileCheck %s +// RUN: %dxc -T vs_6_0 -E main -verify %s -spirv tbuffer MyTBuffer { - float a = 1.0; - float4 b = 2.0; + float a = 1.0; //expected-warning{{initializer for a variable in a tbuffer will be ignored}} + float4 b = 2.0; //expected-warning{{initializer for a variable in a tbuffer will be ignored}} }; float main() : A { return 1.0; } - -// CHECK: :4:15: warning: tbuffer member initializer ignored since no Vulkan equivalent -// CHECK: :5:16: warning: tbuffer member initializer ignored since no Vulkan equivalent diff --git a/tools/clang/test/CodeGenSPIRV/var.init.warning.ignored.hlsl b/tools/clang/test/CodeGenSPIRV/var.init.warning.ignored.hlsl index 5ba946587c..308cc7e7f2 100644 --- a/tools/clang/test/CodeGenSPIRV/var.init.warning.ignored.hlsl +++ b/tools/clang/test/CodeGenSPIRV/var.init.warning.ignored.hlsl @@ -1,4 +1,4 @@ -// RUN: %dxc -T vs_6_0 -E main -Wno-vk-ignored-features -fcgl %s -spirv 2>&1 | FileCheck %s +// RUN: %dxc -T vs_6_0 -E main -Wno-vk-ignored-features -Wno-hlsl-buffer-initializer -fcgl %s -spirv 2>&1 | FileCheck %s cbuffer MyCBuffer { float a = 1.0; diff --git a/tools/clang/test/HLSL/cpp-errors-hv2015.hlsl b/tools/clang/test/HLSL/cpp-errors-hv2015.hlsl index fa77e86d50..56715770cd 100644 --- a/tools/clang/test/HLSL/cpp-errors-hv2015.hlsl +++ b/tools/clang/test/HLSL/cpp-errors-hv2015.hlsl @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -HV 2015 -fsyntax-only -Wno-unused-value -ffreestanding -verify %s +// RUN: %clang_cc1 -Wno-hlsl-buffer-initializer -HV 2015 -fsyntax-only -Wno-unused-value -ffreestanding -verify %s float f_arr_empty_init[] = { 1, 2, 3 }; float f_arr_empty_pack[] = { 1, 2 ... }; // expected-error {{expansion is unsupported in HLSL}} diff --git a/tools/clang/test/HLSL/cpp-errors.hlsl b/tools/clang/test/HLSL/cpp-errors.hlsl index 2f23453407..8a20c3b2fc 100644 --- a/tools/clang/test/HLSL/cpp-errors.hlsl +++ b/tools/clang/test/HLSL/cpp-errors.hlsl @@ -1,18 +1,18 @@ // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -ffreestanding -HV 2018 -verify %s -float f_arr_empty_init[] = { 1, 2, 3 }; +float f_arr_empty_init[] = { 1, 2, 3 }; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ float f_arr_empty_pack[] = { 1, 2 ... }; // expected-error {{expansion is unsupported in HLSL}} struct s_arr_i_f { int i; float f; }; -s_arr_i_f arr_struct_none[] = { }; // TODO: this should fail - see comments in HLSLExternalSource::InitializeInitSequenceForHLSL -s_arr_i_f arr_struct_one[] = { 1, 2 }; +s_arr_i_f arr_struct_none[] = { }; // TODO: this should fail - see comments in HLSLExternalSource::InitializeInitSequenceForHLSL /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +s_arr_i_f arr_struct_one[] = { 1, 2 }; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ s_arr_i_f arr_struct_incomplete[] = { 1, 2, 3 }; // expected-error {{too few elements in vector initialization (expected 4 elements, have 3)}} -s_arr_i_f arr_struct_two[] = { 1, 2, 3, 4 }; +s_arr_i_f arr_struct_two[] = { 1, 2, 3, 4 }; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ int g_int; typeof(g_int) g_typeof_int; // expected-error {{HLSL requires a type specifier for all declarations}} expected-error {{expected ';' after top level declarator}} expected-error {{unknown type name 'typeof'; did you mean 'typedef'?}} typedef int (*fn_int)(int); // expected-error {{pointers are unsupported in HLSL}} -auto g_auto = 3; // auto is now supported in HLSL via type deduction; no error expected +auto g_auto = 3; // auto is now supported in HLSL via type deduction; no error expected /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ __is_signed g_is_signed; // expected-error {{'__is_signed' is a reserved keyword in HLSL}} expected-error {{HLSL requires a type specifier for all declarations}} register int g_register; // expected-error {{'register' is a reserved keyword in HLSL}} __thread int g_thread; // expected-error {{'__thread' is a reserved keyword in HLSL}} @@ -321,7 +321,7 @@ struct forward_struct; // this fails in fxc, but we allow it now struct my_struct_type_decl { int a; } my_struct_var_decl; struct my_struct_type_decl_parens { int a; } (my_struct_var_decl_parens); // expected-error {{HLSL requires a type specifier for all declarations}} expected-error {{expected ';' after struct}} struct my_struct_type_const { int a; } const my_struct_type_var; // // expected-error {{HLSL requires a type specifier for all declarations}} expected-error {{expected ';' after struct}} -struct my_struct_type_init { int a; } my_struct_type_init_one = { 1 }, my_struct_type_init_two = { 2 }; +struct my_struct_type_init { int a; } my_struct_type_init_one = { 1 }, my_struct_type_init_two = { 2 }; /* expected-warning 2 {{initializer for a variable in a cbuffer will be ignored}} */ struct my_struct_type_static { int a; } static my_struct_type_static; // expected-error {{expected ';' after struct}} expected-warning {{declaration does not declare anything}} struct { int my_anon_struct_field; } my_anon_struct_type; diff --git a/tools/clang/test/HLSL/rewriter/cpp-errors_noerr.hlsl b/tools/clang/test/HLSL/rewriter/cpp-errors_noerr.hlsl index 44c3f33538..3719729439 100644 --- a/tools/clang/test/HLSL/rewriter/cpp-errors_noerr.hlsl +++ b/tools/clang/test/HLSL/rewriter/cpp-errors_noerr.hlsl @@ -1,13 +1,13 @@ // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -ffreestanding -verify %s -float f_arr_empty_init[] = { 1, 2, 3 }; +float f_arr_empty_init[] = { 1, 2, 3 }; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ //float f_arr_empty_pack[] = { 1, 2 ... }; // expected-error {{expansion is unsupported in HLSL}} struct s_arr_i_f { int i; float f; }; -s_arr_i_f arr_struct_none[] = { }; // TODO: this should fail - see comments in HLSLExternalSource::InitializeInitSequenceForHLSL -s_arr_i_f arr_struct_one[] = { 1, 2 }; +s_arr_i_f arr_struct_none[] = { }; // TODO: this should fail - see comments in HLSLExternalSource::InitializeInitSequenceForHLSL /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +s_arr_i_f arr_struct_one[] = { 1, 2 }; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ //s_arr_i_f arr_struct_incomplete[] = { 1, 2, 3 }; // expected-error {{too few elements in vector initialization (expected 4 elements, have 3)}} -s_arr_i_f arr_struct_two[] = { 1, 2, 3, 4 }; +s_arr_i_f arr_struct_two[] = { 1, 2, 3, 4 }; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ int g_int; //typeof(g_int) g_typeof_int; // expected-error {{unknown type name 'typeof'; did you mean 'typedef'?}} expected-error {{HLSL requires a type specifier for all declarations}} expected-error {{expected ';' after top level declarator}} @@ -332,7 +332,7 @@ struct my_struct_5 : my_class, my_interface { }; struct my_struct_type_decl { int a; } my_struct_var_decl; //struct my_struct_type_decl_parens { int a; } (my_struct_var_decl_parens); // expected-error {{expected ';' after struct}} expected-error {{HLSL requires a type specifier for all declarations}} //struct my_struct_type_const { int a; } const my_struct_type_var; // // expected-error {{expected ';' after struct}} expected-error {{HLSL requires a type specifier for all declarations}} -struct my_struct_type_init { int a; } my_struct_type_init_one = { 1 }, my_struct_type_init_two = { 2 }; +struct my_struct_type_init { int a; } my_struct_type_init_one = { 1 }, my_struct_type_init_two = { 2 }; /* expected-warning 2 {{initializer for a variable in a cbuffer will be ignored}} */ //struct my_struct_type_static { int a; } static my_struct_type_static; // expected-error {{expected ';' after struct}} expected-warning {{declaration does not declare anything}} struct { int my_anon_struct_field; } my_anon_struct_type; diff --git a/tools/clang/test/HLSL/rewriter/typemods-syntax_noerr.hlsl b/tools/clang/test/HLSL/rewriter/typemods-syntax_noerr.hlsl index 0616f75cdd..6c76f9af91 100644 --- a/tools/clang/test/HLSL/rewriter/typemods-syntax_noerr.hlsl +++ b/tools/clang/test/HLSL/rewriter/typemods-syntax_noerr.hlsl @@ -263,14 +263,14 @@ cbuffer CBInit { // modify(lines, gen_code('%(mods)s float2x3 g_%(id)s_init = sc_%(id)s_init_scalar;')) // GENERATED_CODE:BEGIN - 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 = sc_row_init_scalar; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major snorm float2x3 g_row_sno_init = sc_row_sno_init_scalar; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major unorm float2x3 g_row_uno_init = sc_row_uno_init_scalar; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + column_major float2x3 g_col_init = sc_col_init_scalar; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + column_major snorm float2x3 g_col_sno_init = sc_col_sno_init_scalar; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + column_major unorm float2x3 g_col_uno_init = sc_col_uno_init_scalar; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + snorm float2x3 g_sno_init = sc_sno_init_scalar; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + unorm float2x3 g_uno_init = sc_uno_init_scalar; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ //row_major column_major float2x3 g_row_col_init = sc_row_col_init_scalar; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} fxc-error {{X3048: matrix types cannot be both column_major and row_major}} */ //row_major column_major snorm float2x3 g_row_col_sno_init = sc_row_col_sno_init_scalar; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} fxc-error {{X3048: matrix types cannot be both column_major and row_major}} */ //row_major column_major snorm unorm float2x3 g_row_col_sno_uno_init = sc_row_col_sno_uno_init_scalar; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} */ @@ -282,14 +282,14 @@ cbuffer CBInit // modify(lines, gen_code('%(mods)s float2x3 g_%(id)s_init_scalar = 1.0f;')) // GENERATED_CODE:BEGIN - row_major float2x3 g_row_init_scalar = 1.0f; - row_major snorm float2x3 g_row_sno_init_scalar = 1.0f; - row_major unorm float2x3 g_row_uno_init_scalar = 1.0f; - column_major float2x3 g_col_init_scalar = 1.0f; - column_major snorm float2x3 g_col_sno_init_scalar = 1.0f; - column_major unorm float2x3 g_col_uno_init_scalar = 1.0f; - snorm float2x3 g_sno_init_scalar = 1.0f; - unorm float2x3 g_uno_init_scalar = 1.0f; + row_major float2x3 g_row_init_scalar = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major snorm float2x3 g_row_sno_init_scalar = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major unorm float2x3 g_row_uno_init_scalar = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + column_major float2x3 g_col_init_scalar = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + column_major snorm float2x3 g_col_sno_init_scalar = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + column_major unorm float2x3 g_col_uno_init_scalar = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + snorm float2x3 g_sno_init_scalar = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + unorm float2x3 g_uno_init_scalar = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ //row_major column_major float2x3 g_row_col_init_scalar = 1.0f; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} fxc-error {{X3048: matrix types cannot be both column_major and row_major}} */ //row_major column_major snorm float2x3 g_row_col_sno_init_scalar = 1.0f; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} fxc-error {{X3048: matrix types cannot be both column_major and row_major}} */ //row_major column_major snorm unorm float2x3 g_row_col_sno_uno_init_scalar = 1.0f; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} */ @@ -683,4 +683,4 @@ class C // return (float3x4) 1.0; //} -}; \ No newline at end of file +}; diff --git a/tools/clang/test/HLSL/rewriter/varmods-syntax_noerr.hlsl b/tools/clang/test/HLSL/rewriter/varmods-syntax_noerr.hlsl index b0e205a869..9f009571da 100644 --- a/tools/clang/test/HLSL/rewriter/varmods-syntax_noerr.hlsl +++ b/tools/clang/test/HLSL/rewriter/varmods-syntax_noerr.hlsl @@ -174,12 +174,12 @@ groupshared const float g_gro_con_init = 1.0f; //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; /* 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 float g_pre_init = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ 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'}} */ //precise static volatile const float g_pre_sta_vol_con_init = 1.0f; /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_pre_sta_vol_con_init': global variables cannot be declared 'volatile'}} */ precise static const float g_pre_sta_con_init = 1.0f; -precise uniform float g_pre_uni_init = 1.0f; +precise uniform float g_pre_uni_init = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ //precise uniform volatile float g_pre_uni_vol_init = 1.0f; /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_pre_uni_vol_init': global variables cannot be declared 'volatile'}} */ //precise uniform volatile const float g_pre_uni_vol_con_init = 1.0f; /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_pre_uni_vol_con_init': global variables cannot be declared 'volatile'}} */ //precise uniform const float g_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.}} */ @@ -190,7 +190,7 @@ static float g_sta_init = 1.0f; //static volatile float g_sta_vol_init = 1.0f; /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_sta_vol_init': global variables cannot be declared 'volatile'}} */ //static volatile const float g_sta_vol_con_init = 1.0f; /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_sta_vol_con_init': global variables cannot be declared 'volatile'}} */ static const float g_sta_con_init = 1.0f; -uniform float g_uni_init = 1.0f; +uniform float g_uni_init = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ //uniform volatile float g_uni_vol_init = 1.0f; /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_uni_vol_init': global variables cannot be declared 'volatile'}} */ //uniform volatile const float g_uni_vol_con_init = 1.0f; /* expected-error {{'volatile' is not a valid modifier for a global variable}} fxc-error {{X3008: 'g_uni_vol_con_init': global variables cannot be declared 'volatile'}} */ //uniform const float g_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.}} */ diff --git a/tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-no-reference.hlsl b/tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-no-reference.hlsl index 499c26aec7..39b05f7dde 100644 --- a/tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-no-reference.hlsl +++ b/tools/clang/test/HLSLFileCheckLit/hlsl/auto/auto-no-reference.hlsl @@ -3,7 +3,7 @@ // Test that 'auto' cannot be used to declare reference types in HLSL. // References are unsupported in HLSL. -int gVal = 42; +static int gVal = 42; [numthreads(1,1,1)] void main() { diff --git a/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-202x.hlsl b/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-202x.hlsl new file mode 100644 index 0000000000..ac9f5be134 --- /dev/null +++ b/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-202x.hlsl @@ -0,0 +1,51 @@ +// RUN: %dxc -T lib_6_9 -HV 202x -verify %s + +// Starting with HLSL 202x, initializers on shader constants are an error. +// See buffer-initializers-pre202x.hlsl for earlier language versions. + +// Variables at global scope land in the implicit $Globals constant buffer. +float Implicit = 1; +// expected-error@-1 {{variable in a cbuffer cannot have an initializer}} + +const float ImplicitConst = 2; +// expected-error@-1 {{variable in a cbuffer cannot have an initializer}} + +float2 ImplicitVector = float2(1, 2); +// expected-error@-1 {{variable in a cbuffer cannot have an initializer}} + +float ImplicitArray[2] = {1, 2}; +// expected-error@-1 {{variable in a cbuffer cannot have an initializer}} + +struct S { float F; }; +S ImplicitStruct = {1}; +// expected-error@-1 {{variable in a cbuffer cannot have an initializer}} + +namespace N { +float NamespacedImplicit = 3; +// expected-error@-1 {{variable in a cbuffer cannot have an initializer}} +} + +cbuffer CB { + float CBMember = 4; + // expected-error@-1 {{variable in a cbuffer cannot have an initializer}} + + // 'static' members are not part of the buffer, so they keep their + // initializer. + static float CBStatic = 5; +} + +tbuffer TB { + float TBMember = 6; + // expected-error@-1 {{variable in a tbuffer cannot have an initializer}} + + static float TBStatic = 7; +} + +// These are not shader constants, so initializers remain valid. +static float StaticGlobal = 8; +groupshared float GroupShared = 9; + +float main() : OUT { + float Local = 10; + return Local + Implicit + CBMember + TBMember; +} diff --git a/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-nowarn.hlsl b/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-nowarn.hlsl new file mode 100644 index 0000000000..b04291d472 --- /dev/null +++ b/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-nowarn.hlsl @@ -0,0 +1,18 @@ +// RUN: %dxc -T lib_6_9 -HV 2021 -Wno-hlsl-buffer-initializer -verify %s + +// The pre-202x warning can be suppressed with -Wno-hlsl-buffer-initializer. +// expected-no-diagnostics + +float Implicit = 1; + +cbuffer CB { + float CBMember = 2; +} + +tbuffer TB { + float TBMember = 3; +} + +float main() : OUT { + return Implicit + CBMember + TBMember; +} diff --git a/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-pre202x.hlsl b/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-pre202x.hlsl new file mode 100644 index 0000000000..685e20ac50 --- /dev/null +++ b/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-pre202x.hlsl @@ -0,0 +1,51 @@ +// RUN: %dxc -T lib_6_9 -HV 2021 -verify %s + +// Before HLSL 202x, initializers on shader constants are ignored with a +// warning. See buffer-initializers-202x.hlsl for the HLSL 202x behavior. + +// Variables at global scope land in the implicit $Globals constant buffer. +float Implicit = 1; +// expected-warning@-1 {{initializer for a variable in a cbuffer will be ignored}} + +const float ImplicitConst = 2; +// expected-warning@-1 {{initializer for a variable in a cbuffer will be ignored}} + +float2 ImplicitVector = float2(1, 2); +// expected-warning@-1 {{initializer for a variable in a cbuffer will be ignored}} + +float ImplicitArray[2] = {1, 2}; +// expected-warning@-1 {{initializer for a variable in a cbuffer will be ignored}} + +struct S { float F; }; +S ImplicitStruct = {1}; +// expected-warning@-1 {{initializer for a variable in a cbuffer will be ignored}} + +namespace N { +float NamespacedImplicit = 3; +// expected-warning@-1 {{initializer for a variable in a cbuffer will be ignored}} +} + +cbuffer CB { + float CBMember = 4; + // expected-warning@-1 {{initializer for a variable in a cbuffer will be ignored}} + + // 'static' members are not part of the buffer, so they keep their + // initializer. + static float CBStatic = 5; +} + +tbuffer TB { + float TBMember = 6; + // expected-warning@-1 {{initializer for a variable in a tbuffer will be ignored}} + + static float TBStatic = 7; +} + +// These are not shader constants, so initializers remain valid. +static float StaticGlobal = 8; +groupshared float GroupShared = 9; + +float main() : OUT { + float Local = 10; + return Local + Implicit + CBMember + TBMember; +} diff --git a/tools/clang/test/SemaHLSL/rayquery-omm-diag-TU-export-sm65.hlsl b/tools/clang/test/SemaHLSL/rayquery-omm-diag-TU-export-sm65.hlsl index 3e2031e0a7..626c5ec518 100644 --- a/tools/clang/test/SemaHLSL/rayquery-omm-diag-TU-export-sm65.hlsl +++ b/tools/clang/test/SemaHLSL/rayquery-omm-diag-TU-export-sm65.hlsl @@ -1,9 +1,9 @@ // RUN: %dxc -T lib_6_5 -verify %s -// expect no diagnostics here, since global variables +// expect no availability diagnostic here, since global variables // are not picked up through the recursive AST visitor's // traversal of the exported function. -int x = RAYQUERY_FLAG_ALLOW_OPACITY_MICROMAPS; +int x = RAYQUERY_FLAG_ALLOW_OPACITY_MICROMAPS; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ export float4 MyExportedFunction(float4 color) { // expected-warning@+1{{potential misuse of built-in constant 'RAYQUERY_FLAG_ALLOW_OPACITY_MICROMAPS' in shader model lib_6_5; introduced in shader model 6.9}} diff --git a/tools/clang/test/SemaHLSL/removed-keywords-202x.hlsl b/tools/clang/test/SemaHLSL/removed-keywords-202x.hlsl index 477b1e93b4..0ce44f9206 100644 --- a/tools/clang/test/SemaHLSL/removed-keywords-202x.hlsl +++ b/tools/clang/test/SemaHLSL/removed-keywords-202x.hlsl @@ -1,8 +1,9 @@ // RUN: %dxc -T lib_6_3 -HV 202x -verify %s -// The removed keywords are identifiers in HLSL 202x. -float shared = 1; -float uniform = shared; +// The removed keywords are identifiers in HLSL 202x. These are declared +// 'static' so they are not shader constants, which cannot be initialized. +static float shared = 1; +static float uniform = shared; // Uses of the old modifier syntax now produce normal parsing diagnostics. shared float globalShared; // expected-error {{unknown type name 'shared'}} expected-error {{expected unqualified-id}} diff --git a/tools/clang/test/SemaHLSL/subobjects-syntax.hlsl b/tools/clang/test/SemaHLSL/subobjects-syntax.hlsl index 3ac41c75fe..5ab083a064 100644 --- a/tools/clang/test/SemaHLSL/subobjects-syntax.hlsl +++ b/tools/clang/test/SemaHLSL/subobjects-syntax.hlsl @@ -52,7 +52,7 @@ SubobjectToExportsAssociation sea2_6 = { 51 }; /* expected-error {{ SubobjectToExportsAssociation sea2_7 = "foo"; /* expected-error {{cannot initialize a variable of type 'SubobjectToExportsAssociation' with an lvalue of type 'literal string'}} */ SubobjectToExportsAssociation sea2_8 = 65412; /* expected-error {{cannot initialize a variable of type 'SubobjectToExportsAssociation' with an rvalue of type 'literal int'}} */ -int i1 = 10, i2 = 156; +int i1 = 10, i2 = 156; /* expected-warning 2 {{initializer for a variable in a cbuffer will be ignored}} */ RaytracingShaderConfig rsc1_1 = { 128, 64 }; RaytracingShaderConfig rsc1_2 = { int2(128, 64) }; diff --git a/tools/clang/test/SemaHLSL/typemods-syntax.hlsl b/tools/clang/test/SemaHLSL/typemods-syntax.hlsl index 9d7bc7585d..964d09a1fa 100644 --- a/tools/clang/test/SemaHLSL/typemods-syntax.hlsl +++ b/tools/clang/test/SemaHLSL/typemods-syntax.hlsl @@ -263,40 +263,40 @@ cbuffer CBInit { // modify(lines, gen_code('%(mods)s float2x3 g_%(id)s_init = sc_%(id)s_init_scalar;')) // GENERATED_CODE:BEGIN - 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 column_major float2x3 g_row_col_init = sc_row_col_init_scalar; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} fxc-error {{X3048: matrix types cannot be both column_major and row_major}} */ - row_major column_major snorm float2x3 g_row_col_sno_init = sc_row_col_sno_init_scalar; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} fxc-error {{X3048: matrix types cannot be both column_major and row_major}} */ - row_major column_major snorm unorm float2x3 g_row_col_sno_uno_init = sc_row_col_sno_uno_init_scalar; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} */ - row_major column_major unorm float2x3 g_row_col_uno_init = sc_row_col_uno_init_scalar; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} fxc-error {{X3048: matrix types cannot be both column_major and row_major}} */ - row_major snorm unorm float2x3 g_row_sno_uno_init = sc_row_sno_uno_init_scalar; /* expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} */ - column_major snorm unorm float2x3 g_col_sno_uno_init = sc_col_sno_uno_init_scalar; /* expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} */ - snorm unorm float2x3 g_sno_uno_init = sc_sno_uno_init_scalar; /* expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} */ + row_major float2x3 g_row_init = sc_row_init_scalar; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major snorm float2x3 g_row_sno_init = sc_row_sno_init_scalar; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major unorm float2x3 g_row_uno_init = sc_row_uno_init_scalar; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + column_major float2x3 g_col_init = sc_col_init_scalar; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + column_major snorm float2x3 g_col_sno_init = sc_col_sno_init_scalar; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + column_major unorm float2x3 g_col_uno_init = sc_col_uno_init_scalar; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + snorm float2x3 g_sno_init = sc_sno_init_scalar; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + unorm float2x3 g_uno_init = sc_uno_init_scalar; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major column_major float2x3 g_row_col_init = sc_row_col_init_scalar; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} fxc-error {{X3048: matrix types cannot be both column_major and row_major}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major column_major snorm float2x3 g_row_col_sno_init = sc_row_col_sno_init_scalar; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} fxc-error {{X3048: matrix types cannot be both column_major and row_major}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major column_major snorm unorm float2x3 g_row_col_sno_uno_init = sc_row_col_sno_uno_init_scalar; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major column_major unorm float2x3 g_row_col_uno_init = sc_row_col_uno_init_scalar; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} fxc-error {{X3048: matrix types cannot be both column_major and row_major}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major snorm unorm float2x3 g_row_sno_uno_init = sc_row_sno_uno_init_scalar; /* expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + column_major snorm unorm float2x3 g_col_sno_uno_init = sc_col_sno_uno_init_scalar; /* expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + snorm unorm float2x3 g_sno_uno_init = sc_sno_uno_init_scalar; /* expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ // GENERATED_CODE:END // modify(lines, gen_code('%(mods)s float2x3 g_%(id)s_init_scalar = 1.0f;')) // GENERATED_CODE:BEGIN - row_major float2x3 g_row_init_scalar = 1.0f; - row_major snorm float2x3 g_row_sno_init_scalar = 1.0f; - row_major unorm float2x3 g_row_uno_init_scalar = 1.0f; - column_major float2x3 g_col_init_scalar = 1.0f; - column_major snorm float2x3 g_col_sno_init_scalar = 1.0f; - column_major unorm float2x3 g_col_uno_init_scalar = 1.0f; - snorm float2x3 g_sno_init_scalar = 1.0f; - unorm float2x3 g_uno_init_scalar = 1.0f; - row_major column_major float2x3 g_row_col_init_scalar = 1.0f; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} fxc-error {{X3048: matrix types cannot be both column_major and row_major}} */ - row_major column_major snorm float2x3 g_row_col_sno_init_scalar = 1.0f; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} fxc-error {{X3048: matrix types cannot be both column_major and row_major}} */ - row_major column_major snorm unorm float2x3 g_row_col_sno_uno_init_scalar = 1.0f; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} */ - row_major column_major unorm float2x3 g_row_col_uno_init_scalar = 1.0f; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} fxc-error {{X3048: matrix types cannot be both column_major and row_major}} */ - row_major snorm unorm float2x3 g_row_sno_uno_init_scalar = 1.0f; /* expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} */ - column_major snorm unorm float2x3 g_col_sno_uno_init_scalar = 1.0f; /* expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} */ - snorm unorm float2x3 g_sno_uno_init_scalar = 1.0f; /* expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} */ + row_major float2x3 g_row_init_scalar = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major snorm float2x3 g_row_sno_init_scalar = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major unorm float2x3 g_row_uno_init_scalar = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + column_major float2x3 g_col_init_scalar = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + column_major snorm float2x3 g_col_sno_init_scalar = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + column_major unorm float2x3 g_col_uno_init_scalar = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + snorm float2x3 g_sno_init_scalar = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + unorm float2x3 g_uno_init_scalar = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major column_major float2x3 g_row_col_init_scalar = 1.0f; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} fxc-error {{X3048: matrix types cannot be both column_major and row_major}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major column_major snorm float2x3 g_row_col_sno_init_scalar = 1.0f; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} fxc-error {{X3048: matrix types cannot be both column_major and row_major}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major column_major snorm unorm float2x3 g_row_col_sno_uno_init_scalar = 1.0f; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major column_major unorm float2x3 g_row_col_uno_init_scalar = 1.0f; /* expected-error {{'row_major' and 'column_major' attributes are not compatible}} fxc-error {{X3048: matrix types cannot be both column_major and row_major}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + row_major snorm unorm float2x3 g_row_sno_uno_init_scalar = 1.0f; /* expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + column_major snorm unorm float2x3 g_col_sno_uno_init_scalar = 1.0f; /* expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ + snorm unorm float2x3 g_sno_uno_init_scalar = 1.0f; /* expected-error {{'unorm' and 'snorm' attributes are not compatible}} fxc-error {{X3000: syntax error: unexpected token 'unorm'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ // GENERATED_CODE:END } @@ -698,4 +698,4 @@ class C return (float3x4) 1.0; } -}; \ No newline at end of file +}; diff --git a/tools/clang/test/SemaHLSL/v2021-static-assert-not-keyword.hlsl b/tools/clang/test/SemaHLSL/v2021-static-assert-not-keyword.hlsl index 1f00248eab..1d7efead9f 100644 --- a/tools/clang/test/SemaHLSL/v2021-static-assert-not-keyword.hlsl +++ b/tools/clang/test/SemaHLSL/v2021-static-assert-not-keyword.hlsl @@ -4,7 +4,7 @@ // ordinary identifier. // expected-no-diagnostics -int static_assert = 1; +static int static_assert = 1; int use_static_assert(int static_assert) { return static_assert; diff --git a/tools/clang/test/SemaHLSL/varmods-syntax.hlsl b/tools/clang/test/SemaHLSL/varmods-syntax.hlsl index 879a66430c..bd00c21966 100644 --- a/tools/clang/test/SemaHLSL/varmods-syntax.hlsl +++ b/tools/clang/test/SemaHLSL/varmods-syntax.hlsl @@ -135,7 +135,7 @@ const float2 g_con; // modify(lines, gen_code('%(mods)s float g_%(id)s_init = 1.0f;', storage_combos)) // GENERATED_CODE:BEGIN groupshared extern float g_gro_ext_init = 1.0f; /* expected-error {{'extern' and 'groupshared' cannot be used together for a global variable}} fxc-error {{X3010: 'g_gro_ext_init': extern global variables cannot be declared 'groupshared'}} */ -extern static float g_ext_sta_init = 1.0f; /* expected-error {{cannot combine with previous 'extern' declaration specifier}} expected-warning {{'extern' variable has an initializer}} fxc-error {{X3007: 'g_ext_sta_init': extern global variables cannot be declared 'static'}} */ +extern static float g_ext_sta_init = 1.0f; /* expected-error {{cannot combine with previous 'extern' declaration specifier}} expected-warning {{'extern' variable has an initializer}} fxc-error {{X3007: 'g_ext_sta_init': extern global variables cannot be declared 'static'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ static uniform float g_sta_uni_init = 1.0f; /* expected-error {{'static' and 'uniform' cannot be used together for a global variable}} fxc-error {{X3007: 'g_sta_uni_init': uniform global variables cannot be declared 'static'}} */ groupshared float g_gro_init = 1.0f; groupshared precise float g_gro_pre_init = 1.0f; @@ -161,45 +161,45 @@ groupshared uniform const float g_gro_uni_con_init = 1.0f; /* fxc-error {{X3010 groupshared volatile float g_gro_vol_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_gro_vol_init': global variables cannot be declared 'volatile'}} */ groupshared volatile const float g_gro_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_gro_vol_con_init': global variables cannot be declared 'volatile'}} */ groupshared const float g_gro_con_init = 1.0f; -extern float g_ext_init = 1.0f; /* expected-warning {{'extern' variable has an initializer}} fxc-pass {{}} */ -extern precise float g_ext_pre_init = 1.0f; /* expected-warning {{'extern' variable has an initializer}} fxc-pass {{}} */ -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 a reserved keyword in HLSL}} fxc-error {{X3008: 'g_ext_pre_uni_vol_init': global variables cannot be declared 'volatile'}} expected-warning {{'extern' variable has an initializer}} */ -extern precise uniform volatile const float g_ext_pre_uni_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_ext_pre_uni_vol_con_init': global variables cannot be declared 'volatile'}} expected-warning {{'extern' variable has an initializer}} */ -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 a reserved keyword in HLSL}} fxc-error {{X3008: 'g_ext_pre_vol_init': global variables cannot be declared 'volatile'}} expected-warning {{'extern' variable has an initializer}} */ -extern precise volatile const float g_ext_pre_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_ext_pre_vol_con_init': global variables cannot be declared 'volatile'}} expected-warning {{'extern' variable has an initializer}} */ -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 a reserved keyword in HLSL}} fxc-error {{X3008: 'g_ext_uni_vol_init': global variables cannot be declared 'volatile'}} expected-warning {{'extern' variable has an initializer}} */ -extern uniform volatile const float g_ext_uni_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_ext_uni_vol_con_init': global variables cannot be declared 'volatile'}} expected-warning {{'extern' variable has an initializer}} */ -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 a reserved keyword in HLSL}} fxc-error {{X3008: 'g_ext_vol_init': global variables cannot be declared 'volatile'}} expected-warning {{'extern' variable has an initializer}} */ -extern volatile const float g_ext_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_ext_vol_con_init': global variables cannot be declared 'volatile'}} expected-warning {{'extern' variable has an initializer}} */ -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; +extern float g_ext_init = 1.0f; /* expected-warning {{'extern' variable has an initializer}} fxc-pass {{}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +extern precise float g_ext_pre_init = 1.0f; /* expected-warning {{'extern' variable has an initializer}} fxc-pass {{}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +extern precise uniform float g_ext_pre_uni_init = 1.0f; /* expected-warning {{'extern' variable has an initializer}} fxc-pass {{}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +extern precise uniform volatile float g_ext_pre_uni_vol_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_ext_pre_uni_vol_init': global variables cannot be declared 'volatile'}} expected-warning {{'extern' variable has an initializer}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +extern precise uniform volatile const float g_ext_pre_uni_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_ext_pre_uni_vol_con_init': global variables cannot be declared 'volatile'}} expected-warning {{'extern' variable has an initializer}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +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.}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +extern precise volatile float g_ext_pre_vol_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_ext_pre_vol_init': global variables cannot be declared 'volatile'}} expected-warning {{'extern' variable has an initializer}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +extern precise volatile const float g_ext_pre_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_ext_pre_vol_con_init': global variables cannot be declared 'volatile'}} expected-warning {{'extern' variable has an initializer}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +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.}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +extern uniform float g_ext_uni_init = 1.0f; /* expected-warning {{'extern' variable has an initializer}} fxc-pass {{}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +extern uniform volatile float g_ext_uni_vol_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_ext_uni_vol_init': global variables cannot be declared 'volatile'}} expected-warning {{'extern' variable has an initializer}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +extern uniform volatile const float g_ext_uni_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_ext_uni_vol_con_init': global variables cannot be declared 'volatile'}} expected-warning {{'extern' variable has an initializer}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +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.}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +extern volatile float g_ext_vol_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_ext_vol_init': global variables cannot be declared 'volatile'}} expected-warning {{'extern' variable has an initializer}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +extern volatile const float g_ext_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_ext_vol_con_init': global variables cannot be declared 'volatile'}} expected-warning {{'extern' variable has an initializer}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +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.}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +precise float g_pre_init = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ precise static float g_pre_sta_init = 1.0f; precise static volatile float g_pre_sta_vol_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_pre_sta_vol_init': global variables cannot be declared 'volatile'}} */ precise static volatile const float g_pre_sta_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_pre_sta_vol_con_init': global variables cannot be declared 'volatile'}} */ precise static const float g_pre_sta_con_init = 1.0f; -precise uniform float g_pre_uni_init = 1.0f; -precise uniform volatile float g_pre_uni_vol_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_pre_uni_vol_init': global variables cannot be declared 'volatile'}} */ -precise uniform volatile const float g_pre_uni_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_pre_uni_vol_con_init': global variables cannot be declared 'volatile'}} */ -precise uniform const float g_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.}} */ -precise volatile float g_pre_vol_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_pre_vol_init': global variables cannot be declared 'volatile'}} */ -precise volatile const float g_pre_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_pre_vol_con_init': global variables cannot be declared 'volatile'}} */ -precise const float g_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.}} */ +precise uniform float g_pre_uni_init = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +precise uniform volatile float g_pre_uni_vol_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_pre_uni_vol_init': global variables cannot be declared 'volatile'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +precise uniform volatile const float g_pre_uni_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_pre_uni_vol_con_init': global variables cannot be declared 'volatile'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +precise uniform const float g_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.}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +precise volatile float g_pre_vol_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_pre_vol_init': global variables cannot be declared 'volatile'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +precise volatile const float g_pre_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_pre_vol_con_init': global variables cannot be declared 'volatile'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +precise const float g_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.}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ static float g_sta_init = 1.0f; static volatile float g_sta_vol_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_sta_vol_init': global variables cannot be declared 'volatile'}} */ static volatile const float g_sta_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_sta_vol_con_init': global variables cannot be declared 'volatile'}} */ static const float g_sta_con_init = 1.0f; -uniform float g_uni_init = 1.0f; -uniform volatile float g_uni_vol_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_uni_vol_init': global variables cannot be declared 'volatile'}} */ -uniform volatile const float g_uni_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_uni_vol_con_init': global variables cannot be declared 'volatile'}} */ -uniform const float g_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.}} */ -volatile float g_vol_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_vol_init': global variables cannot be declared 'volatile'}} */ -volatile const float g_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_vol_con_init': global variables cannot be declared 'volatile'}} */ -const float g_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.}} */ +uniform float g_uni_init = 1.0f; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +uniform volatile float g_uni_vol_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_uni_vol_init': global variables cannot be declared 'volatile'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +uniform volatile const float g_uni_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_uni_vol_con_init': global variables cannot be declared 'volatile'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +uniform const float g_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.}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +volatile float g_vol_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_vol_init': global variables cannot be declared 'volatile'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +volatile const float g_vol_con_init = 1.0f; /* expected-error {{'volatile' is a reserved keyword in HLSL}} fxc-error {{X3008: 'g_vol_con_init': global variables cannot be declared 'volatile'}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +const float g_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.}} expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ // GENERATED_CODE:END // modify(lines, gen_code('%(mods)s float2 g_%(id)s;')) diff --git a/tools/clang/test/SemaHLSL/vector-syntax-exact-precision.hlsl b/tools/clang/test/SemaHLSL/vector-syntax-exact-precision.hlsl index 4265d87d89..e0a46c28e3 100644 --- a/tools/clang/test/SemaHLSL/vector-syntax-exact-precision.hlsl +++ b/tools/clang/test/SemaHLSL/vector-syntax-exact-precision.hlsl @@ -1,7 +1,7 @@ // RUN: %dxc -Tlib_6_3 -enable-16bit-types -verify -HV 2018 %s -float3 f3_ones = 1.0.xxx; -float3 f3_ones_exp = 2.0e+2.rrr; +float3 f3_ones = 1.0.xxx; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +float3 f3_ones_exp = 2.0e+2.rrr; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ vector v; vector v1p1; diff --git a/tools/clang/test/SemaHLSL/vector-syntax.hlsl b/tools/clang/test/SemaHLSL/vector-syntax.hlsl index 383222c5bf..a4c74f4fa8 100644 --- a/tools/clang/test/SemaHLSL/vector-syntax.hlsl +++ b/tools/clang/test/SemaHLSL/vector-syntax.hlsl @@ -1,7 +1,7 @@ // RUN: %dxc -Tlib_6_3 -verify %s -float3 f3_ones = 1.0.xxx; -float3 f3_ones_exp = 2.0e+2.rrr; +float3 f3_ones = 1.0.xxx; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ +float3 f3_ones_exp = 2.0e+2.rrr; /* expected-warning {{initializer for a variable in a cbuffer will be ignored}} */ vector v; vector v1p1; From b9731fb5b232b5adb442f2ba6baa5e2003cc3120 Mon Sep 17 00:00:00 2001 From: Chris B Date: Tue, 29 Sep 2026 19:35:21 -0500 Subject: [PATCH 2/6] clang-format --- tools/clang/lib/CodeGen/CodeGenModule.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/clang/lib/CodeGen/CodeGenModule.cpp b/tools/clang/lib/CodeGen/CodeGenModule.cpp index dbbf1119e9..8ac5730a67 100644 --- a/tools/clang/lib/CodeGen/CodeGenModule.cpp +++ b/tools/clang/lib/CodeGen/CodeGenModule.cpp @@ -2124,7 +2124,8 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { if (getLangOpts().CPlusPlus && !NeedsGlobalDtor) DelayedCXXInitPosition.erase(D); // HLSL Change Begins. - if (getLangOpts().HLSL && D->isExternallyVisible() && !D->isStaticDataMember()) { + if (getLangOpts().HLSL && D->isExternallyVisible() && + !D->isStaticDataMember()) { Init = EmitNullConstant(D->getType()); } // HLSL Change Ends. From 1df2785fe1b4b4aeb35b6facb081111cc18aeb07 Mon Sep 17 00:00:00 2001 From: Chris B Date: Tue, 29 Sep 2026 20:15:30 -0500 Subject: [PATCH 3/6] respond to feedback --- .../include/clang/Basic/DiagnosticGroups.td | 2 ++ .../clang/Basic/DiagnosticSemaKinds.td | 3 +++ tools/clang/lib/SPIRV/SpirvEmitter.cpp | 9 ------- tools/clang/lib/Sema/SemaDecl.cpp | 27 ++++++++++++------- .../groupshared.init.warning.hlsl | 13 ++++----- .../hlsl/entry/groupshared-initializer.hlsl | 17 ++++++++++++ .../hlsl/buffer-initializers-202x.hlsl | 4 ++- .../hlsl/buffer-initializers-nowarn.hlsl | 4 ++- .../hlsl/buffer-initializers-pre202x.hlsl | 4 ++- 9 files changed, 56 insertions(+), 27 deletions(-) create mode 100644 tools/clang/test/HLSLFileCheckLit/hlsl/entry/groupshared-initializer.hlsl diff --git a/tools/clang/include/clang/Basic/DiagnosticGroups.td b/tools/clang/include/clang/Basic/DiagnosticGroups.td index 9acf5e53b3..196577d5a1 100644 --- a/tools/clang/include/clang/Basic/DiagnosticGroups.td +++ b/tools/clang/include/clang/Basic/DiagnosticGroups.td @@ -807,6 +807,8 @@ def HLSLAvailability: DiagGroup<"hlsl-availability">; def HLSLAvailabilityConstant: DiagGroup<"hlsl-availability-constant">; def HLSLBarrier : DiagGroup<"hlsl-barrier">; def HLSLBufferInitializer : DiagGroup<"hlsl-buffer-initializer">; +def HLSLGroupSharedInitializer : + DiagGroup<"hlsl-groupshared-initializer">; // HLSL 2026 compatibility warnings: these warn on code in older language modes // that changes meaning or becomes illegal in HLSL 2026. diff --git a/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td b/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td index ca64c04100..7d798010d9 100644 --- a/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -7580,6 +7580,9 @@ def warn_hlsl_buffer_initializer : Warning< InGroup; def err_hlsl_buffer_initializer : Error< "variable in a %select{tbuffer|cbuffer}0 cannot have an initializer">; +def warn_hlsl_groupshared_initializer : Warning< + "initializer of 'groupshared' variable will be ignored">, + InGroup; def err_hlsl_packoffset_overlap : Error<"packoffset overlap between %0, %1">; def err_hlsl_register_semantics_conflicting : Error< "conflicting register semantics">; diff --git a/tools/clang/lib/SPIRV/SpirvEmitter.cpp b/tools/clang/lib/SPIRV/SpirvEmitter.cpp index 5d106be5a2..4768b2218d 100644 --- a/tools/clang/lib/SPIRV/SpirvEmitter.cpp +++ b/tools/clang/lib/SPIRV/SpirvEmitter.cpp @@ -2189,15 +2189,6 @@ void SpirvEmitter::doVarDecl(const VarDecl *decl) { // variables) belongs to the Function storage class. if (isExternalVar(decl)) { var = declIdMapper.createExternVar(decl); - // DXC does not support initializing groupshared memory when targeting - // SPIRV, so the initializer will be ignored. - if (decl->hasAttr() && - !spirvOptions.noWarnIgnoredFeatures) { - if (const auto *init = decl->getInit()) - emitWarning("initializer of 'groupshared' variable will be ignored", - init->getExprLoc()) - << init->getSourceRange(); - } } else { // We already know the variable is not externally visible here. If it does // not have local storage, it should be file scope variable. diff --git a/tools/clang/lib/Sema/SemaDecl.cpp b/tools/clang/lib/Sema/SemaDecl.cpp index d19a11abf1..1b1b6e1305 100644 --- a/tools/clang/lib/Sema/SemaDecl.cpp +++ b/tools/clang/lib/Sema/SemaDecl.cpp @@ -9357,15 +9357,24 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, VDecl->setInit(Init); // HLSL Change Begin - // Variables in a cbuffer/tbuffer declaration, or in the implicit $Globals - // constant buffer, are initialized by the pipeline, so any initializer is - // ignored. This is an error starting with HLSL 202x. - bool IsConstantBuffer = false; - if (getLangOpts().HLSL && IsHLSLShaderConstant(VDecl, IsConstantBuffer)) { - Diag(Init->getExprLoc(), getLangOpts().HLSLVersion >= hlsl::LangStd::v202x - ? diag::err_hlsl_buffer_initializer - : diag::warn_hlsl_buffer_initializer) - << IsConstantBuffer << Init->getSourceRange(); + if (getLangOpts().HLSL) { + if (VDecl->hasAttr() && + (getLangOpts().SPIRV || VDecl->isExternallyVisible())) { + Diag(Init->getExprLoc(), diag::warn_hlsl_groupshared_initializer) + << Init->getSourceRange(); + } else { + // Variables in a cbuffer/tbuffer declaration, or in the implicit + // $Globals constant buffer, are initialized by the pipeline, so any + // initializer is ignored. This is an error starting with HLSL 202x. + bool IsConstantBuffer = false; + if (IsHLSLShaderConstant(VDecl, IsConstantBuffer)) { + Diag(Init->getExprLoc(), + getLangOpts().HLSLVersion >= hlsl::LangStd::v202x + ? diag::err_hlsl_buffer_initializer + : diag::warn_hlsl_buffer_initializer) + << IsConstantBuffer << Init->getSourceRange(); + } + } } // HLSL Change End diff --git a/tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl b/tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl index 76a1ac3e96..324cd89712 100644 --- a/tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl +++ b/tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl @@ -1,11 +1,15 @@ -// RUN: %dxc -T cs_6_0 -E main -spirv %s 2>&1 | FileCheck %s +// RUN: %dxc -T cs_6_0 -E main -spirv -verify %s groupshared uint testing = 0; +// expected-warning@-1 {{initializer of 'groupshared' variable will be ignored}} + +static groupshared uint staticTesting = 1; +// expected-warning@-1 {{initializer of 'groupshared' variable will be ignored}} [numthreads(64, 1, 1)] void main(uint local_thread_id_flat : SV_GroupIndex) { - InterlockedAdd(testing, 1); + InterlockedAdd(testing, staticTesting); GroupMemoryBarrierWithGroupSync(); if (local_thread_id_flat == 0) { @@ -13,7 +17,4 @@ void main(uint local_thread_id_flat : SV_GroupIndex) { printf("testing is %u wtf", testing); } } -} - -// CHECK: warning: initializer of 'groupshared' variable will be ignored -// CHECK-NEXT: groupshared uint testing = 0; \ No newline at end of file +} \ No newline at end of file diff --git a/tools/clang/test/HLSLFileCheckLit/hlsl/entry/groupshared-initializer.hlsl b/tools/clang/test/HLSLFileCheckLit/hlsl/entry/groupshared-initializer.hlsl new file mode 100644 index 0000000000..e2e11a3ba3 --- /dev/null +++ b/tools/clang/test/HLSLFileCheckLit/hlsl/entry/groupshared-initializer.hlsl @@ -0,0 +1,17 @@ +// RUN: %dxc -T cs_6_0 -E main -fcgl -verify %s + +uint getValue() { return 2; } + +groupshared uint Constant = 1; +// expected-warning@-1 {{initializer of 'groupshared' variable will be ignored}} + +groupshared uint Dynamic = getValue(); +// expected-warning@-1 {{initializer of 'groupshared' variable will be ignored}} + +// DXIL retains initializers on static groupshared variables. +static groupshared uint Static = 3; + +[numthreads(1, 1, 1)] +void main() { + Constant += Dynamic + Static; +} diff --git a/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-202x.hlsl b/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-202x.hlsl index ac9f5be134..ee2168d53f 100644 --- a/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-202x.hlsl +++ b/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-202x.hlsl @@ -41,9 +41,11 @@ tbuffer TB { static float TBStatic = 7; } -// These are not shader constants, so initializers remain valid. +// Static globals are initialized normally. Groupshared initializers are +// accepted but ignored. static float StaticGlobal = 8; groupshared float GroupShared = 9; +// expected-warning@-1 {{initializer of 'groupshared' variable will be ignored}} float main() : OUT { float Local = 10; diff --git a/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-nowarn.hlsl b/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-nowarn.hlsl index b04291d472..fd97ecccb5 100644 --- a/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-nowarn.hlsl +++ b/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-nowarn.hlsl @@ -1,4 +1,4 @@ -// RUN: %dxc -T lib_6_9 -HV 2021 -Wno-hlsl-buffer-initializer -verify %s +// RUN: %dxc -T lib_6_9 -HV 2021 -Wno-hlsl-buffer-initializer -Wno-hlsl-groupshared-initializer -verify %s // The pre-202x warning can be suppressed with -Wno-hlsl-buffer-initializer. // expected-no-diagnostics @@ -13,6 +13,8 @@ tbuffer TB { float TBMember = 3; } +groupshared float GroupShared = 4; + float main() : OUT { return Implicit + CBMember + TBMember; } diff --git a/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-pre202x.hlsl b/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-pre202x.hlsl index 685e20ac50..ccb420bc7a 100644 --- a/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-pre202x.hlsl +++ b/tools/clang/test/SemaHLSL/hlsl/buffer-initializers-pre202x.hlsl @@ -41,9 +41,11 @@ tbuffer TB { static float TBStatic = 7; } -// These are not shader constants, so initializers remain valid. +// Static globals are initialized normally. Groupshared initializers are +// accepted but ignored. static float StaticGlobal = 8; groupshared float GroupShared = 9; +// expected-warning@-1 {{initializer of 'groupshared' variable will be ignored}} float main() : OUT { float Local = 10; From 8b64cbd22ac38d5e0bdc5d34edfaa6e40541c454 Mon Sep 17 00:00:00 2001 From: Chris B Date: Wed, 30 Sep 2026 10:32:30 -0500 Subject: [PATCH 4/6] Missed updating these tests --- tools/clang/test/SemaHLSL/rayquery-omm-diag-TU-sm65.hlsl | 2 +- tools/clang/test/SemaHLSL/varmods-syntax.hlsl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/clang/test/SemaHLSL/rayquery-omm-diag-TU-sm65.hlsl b/tools/clang/test/SemaHLSL/rayquery-omm-diag-TU-sm65.hlsl index 6904f58c7d..c2ffe7e36c 100644 --- a/tools/clang/test/SemaHLSL/rayquery-omm-diag-TU-sm65.hlsl +++ b/tools/clang/test/SemaHLSL/rayquery-omm-diag-TU-sm65.hlsl @@ -9,7 +9,7 @@ namespace MyNamespace { static const int badVar = RAYQUERY_FLAG_ALLOW_OPACITY_MICROMAPS; } -// expected-warning@+1{{potential misuse of built-in constant 'RAYQUERY_FLAG_ALLOW_OPACITY_MICROMAPS' in shader model vs_6_5; introduced in shader model 6.9}} +// expected-warning@+1{{potential misuse of built-in constant 'RAYQUERY_FLAG_ALLOW_OPACITY_MICROMAPS' in shader model vs_6_5; introduced in shader model 6.9}} expected-warning@+1{{initializer of 'groupshared' variable will be ignored}} groupshared const int otherBadVar = RAYQUERY_FLAG_ALLOW_OPACITY_MICROMAPS; int retNum(){ diff --git a/tools/clang/test/SemaHLSL/varmods-syntax.hlsl b/tools/clang/test/SemaHLSL/varmods-syntax.hlsl index bd00c21966..6fe76e17ee 100644 --- a/tools/clang/test/SemaHLSL/varmods-syntax.hlsl +++ b/tools/clang/test/SemaHLSL/varmods-syntax.hlsl @@ -1,4 +1,4 @@ -// RUN: %dxc -Tlib_6_3 -HV 2021 -Wno-hlsl-groupshared-202x -verify %s +// RUN: %dxc -Tlib_6_3 -HV 2021 -Wno-hlsl-groupshared-202x -Wno-hlsl-groupshared-initializer -verify %s // The following is meant to be processed by the CodeTags extension in the "VS For Everything" Visual Studio extension: /* From 3e4b2ada4521e3165e7c7d33e867c6dcf9fbeed8 Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Wed, 30 Sep 2026 21:43:11 -0500 Subject: [PATCH 5/6] missing newline ../tools/clang/test/CodeGenSPIRV/var.init.cbuffer.hlsl --- tools/clang/test/CodeGenSPIRV/var.init.cbuffer.hlsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/clang/test/CodeGenSPIRV/var.init.cbuffer.hlsl b/tools/clang/test/CodeGenSPIRV/var.init.cbuffer.hlsl index aa489a006e..3f2cdf50f9 100644 --- a/tools/clang/test/CodeGenSPIRV/var.init.cbuffer.hlsl +++ b/tools/clang/test/CodeGenSPIRV/var.init.cbuffer.hlsl @@ -7,4 +7,4 @@ cbuffer MyCBuffer { float main() : A { return 1.0; -} \ No newline at end of file +} From a11f1dc16fff66c853d198e2d139d7b34cdbbeb4 Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Tue, 6 Oct 2026 13:33:25 -0600 Subject: [PATCH 6/6] Missing newline ../tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl --- tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl b/tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl index 324cd89712..5a48eade22 100644 --- a/tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl +++ b/tools/clang/test/CodeGenSPIRV/groupshared.init.warning.hlsl @@ -17,4 +17,4 @@ void main(uint local_thread_id_flat : SV_GroupIndex) { printf("testing is %u wtf", testing); } } -} \ No newline at end of file +}