https://github.com/bob80905 created https://github.com/llvm/llvm-project/pull/106782
The space parameter in the register binding annotation may not be used for global constants. There was previously no diagnostic emitted when this case occurred. This PR adds a diagnostic when this case occurs, and tests these cases. Fixes #104521 >From 99408f31a8946df7ef9efa223d0dba2ab876fcd0 Mon Sep 17 00:00:00 2001 From: Joshua Batista <jbati...@microsoft.com> Date: Fri, 30 Aug 2024 12:08:37 -0700 Subject: [PATCH] add diag and testing for space and global constants --- .../clang/Basic/DiagnosticSemaKinds.td | 1 + clang/lib/Sema/SemaHLSL.cpp | 16 +++++++++--- .../SemaHLSL/resource_binding_attr_error.hlsl | 17 +++---------- .../resource_binding_attr_error_basic.hlsl | 21 +++++++++------- .../resource_binding_attr_error_space.hlsl | 25 +++++++++++++++++++ 5 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 clang/test/SemaHLSL/resource_binding_attr_error_space.hlsl diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index edf22b909c4d57..e099cb954cac75 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -12357,6 +12357,7 @@ def warn_hlsl_deprecated_register_type_b: Warning<"binding type 'b' only applies def warn_hlsl_deprecated_register_type_i: Warning<"binding type 'i' ignored. The 'integer constant' binding type is no longer supported">, InGroup<LegacyConstantRegisterBinding>, DefaultError; def err_hlsl_unsupported_register_number : Error<"register number should be an integer">; def err_hlsl_expected_space : Error<"invalid space specifier '%0' used; expected 'space' followed by an integer, like space1">; +def err_hlsl_space_on_global_constant : Error<"register space cannot be specified on global constants">; def warn_hlsl_packoffset_mix : Warning<"cannot mix packoffset elements with nonpackoffset elements in a cbuffer">, InGroup<HLSLMixPackOffset>; def err_hlsl_packoffset_overlap : Error<"packoffset overlap between %0, %1">; diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp index 714e8f5cfa9926..61bed66445dc78 100644 --- a/clang/lib/Sema/SemaHLSL.cpp +++ b/clang/lib/Sema/SemaHLSL.cpp @@ -722,7 +722,8 @@ static void ValidateMultipleRegisterAnnotations(Sema &S, Decl *TheDecl, } static void DiagnoseHLSLRegisterAttribute(Sema &S, SourceLocation &ArgLoc, - Decl *TheDecl, RegisterType regType) { + Decl *TheDecl, RegisterType regType, + StringRef SpaceNum) { // Samplers, UAVs, and SRVs are VarDecl types VarDecl *TheVarDecl = dyn_cast<VarDecl>(TheDecl); @@ -795,6 +796,9 @@ static void DiagnoseHLSLRegisterAttribute(Sema &S, SourceLocation &ArgLoc, S.Diag(ArgLoc, diag::warn_hlsl_deprecated_register_type_b); else if (regType != RegisterType::C) S.Diag(ArgLoc, diag::err_hlsl_binding_type_mismatch) << regTypeNum; + // non-zero SpaceNum cannot be specified for global constants + if (SpaceNum != "0") + S.Diag(ArgLoc, diag::err_hlsl_space_on_global_constant); return; } @@ -817,8 +821,12 @@ static void DiagnoseHLSLRegisterAttribute(Sema &S, SourceLocation &ArgLoc, S.Diag(TheDecl->getLocation(), diag::warn_hlsl_user_defined_type_missing_member) << regTypeNum; - - return; + // non-zero SpaceNum cannot be specified for global constants + if (!isDeclaredWithinCOrTBuffer(TheDecl)) { + if (SpaceNum != "0") + S.Diag(ArgLoc, diag::err_hlsl_space_on_global_constant); + return; + } } } @@ -891,7 +899,7 @@ void SemaHLSL::handleResourceBindingAttr(Decl *TheDecl, const ParsedAttr &AL) { return; } - DiagnoseHLSLRegisterAttribute(SemaRef, ArgLoc, TheDecl, regType); + DiagnoseHLSLRegisterAttribute(SemaRef, ArgLoc, TheDecl, regType, SpaceNum); HLSLResourceBindingAttr *NewAttr = HLSLResourceBindingAttr::Create(getASTContext(), Slot, Space, AL); diff --git a/clang/test/SemaHLSL/resource_binding_attr_error.hlsl b/clang/test/SemaHLSL/resource_binding_attr_error.hlsl index 6a0b5956545dd8..823b75f14d1957 100644 --- a/clang/test/SemaHLSL/resource_binding_attr_error.hlsl +++ b/clang/test/SemaHLSL/resource_binding_attr_error.hlsl @@ -13,16 +13,9 @@ float a : register(c0); cbuffer b : register(i0) { } -// expected-error@+1 {{invalid space specifier 's2' used; expected 'space' followed by an integer, like space1}} -cbuffer c : register(b0, s2) { -} // expected-error@+1 {{register number should be an integer}} -cbuffer d : register(bf, s2) { - -} -// expected-error@+1 {{invalid space specifier 'spaces' used; expected 'space' followed by an integer, like space1}} -cbuffer e : register(b2, spaces) { +cbuffer c : register(bf, s2) { } @@ -35,9 +28,8 @@ cbuffer B : register(space1) {} // expected-error@+1 {{wrong argument format for hlsl attribute, use b2 instead}} cbuffer C : register(b 2) {} -// expected-error@+2 {{wrong argument format for hlsl attribute, use b2 instead}} -// expected-error@+1 {{wrong argument format for hlsl attribute, use space3 instead}} -cbuffer D : register(b 2, space 3) {} +// expected-error@+1 {{wrong argument format for hlsl attribute, use b2 instead}} +cbuffer D : register(b 2, space3) {} // expected-error@+1 {{'register' attribute only applies to cbuffer/tbuffer and external global variables}} static MyTemplatedSRV<float> U : register(u5); @@ -61,9 +53,6 @@ void foo2() { extern MyTemplatedSRV<float> U2 : register(u5); } -// expected-error@+1 {{binding type 'u' only applies to UAV resources}} -float b : register(u0, space1); - // expected-error@+1 {{'register' attribute only applies to cbuffer/tbuffer and external global variables}} void bar(MyTemplatedSRV<float> U : register(u3)) { diff --git a/clang/test/SemaHLSL/resource_binding_attr_error_basic.hlsl b/clang/test/SemaHLSL/resource_binding_attr_error_basic.hlsl index 0a547ed66af0a2..760c057630a7fa 100644 --- a/clang/test/SemaHLSL/resource_binding_attr_error_basic.hlsl +++ b/clang/test/SemaHLSL/resource_binding_attr_error_basic.hlsl @@ -3,37 +3,40 @@ // expected-error@+1{{binding type 't' only applies to SRV resources}} float f1 : register(t0); - -float f2 : register(c0); +// expected-error@+1 {{binding type 'u' only applies to UAV resources}} +float f2 : register(u0); // expected-error@+1{{binding type 'b' only applies to constant buffers. The 'bool constant' binding type is no longer supported}} float f3 : register(b9); +// expected-error@+1 {{binding type 's' only applies to sampler state}} +float f4 : register(s0); + // expected-error@+1{{binding type 'i' ignored. The 'integer constant' binding type is no longer supported}} -float f4 : register(i9); +float f5 : register(i9); // expected-error@+1{{binding type 'x' is invalid}} -float f5 : register(x9); +float f6 : register(x9); cbuffer g_cbuffer1 { // expected-error@+1{{binding type 'c' ignored in buffer declaration. Did you mean 'packoffset'?}} - float f6 : register(c2); + float f7 : register(c2); }; tbuffer g_tbuffer1 { // expected-error@+1{{binding type 'c' ignored in buffer declaration. Did you mean 'packoffset'?}} - float f7 : register(c2); + float f8 : register(c2); }; cbuffer g_cbuffer2 { // expected-error@+1{{binding type 'b' only applies to constant buffer resources}} - float f8 : register(b2); + float f9 : register(b2); }; tbuffer g_tbuffer2 { // expected-error@+1{{binding type 'i' ignored. The 'integer constant' binding type is no longer supported}} - float f9 : register(i2); + float f10 : register(i2); }; // expected-error@+1{{binding type 'c' only applies to numeric variables in the global scope}} -RWBuffer<float> f10 : register(c3); +RWBuffer<float> f11 : register(c3); diff --git a/clang/test/SemaHLSL/resource_binding_attr_error_space.hlsl b/clang/test/SemaHLSL/resource_binding_attr_error_space.hlsl new file mode 100644 index 00000000000000..2f1b19e1bfc696 --- /dev/null +++ b/clang/test/SemaHLSL/resource_binding_attr_error_space.hlsl @@ -0,0 +1,25 @@ +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -o - -fsyntax-only %s -verify + + +// expected-error@+1 {{invalid space specifier 's2' used; expected 'space' followed by an integer, like space1}} +cbuffer a : register(b0, s2) { + +} + +// expected-error@+1 {{invalid space specifier 'spaces' used; expected 'space' followed by an integer, like space1}} +cbuffer b : register(b2, spaces) { + +} + +// expected-error@+1 {{wrong argument format for hlsl attribute, use space3 instead}} +cbuffer c : register(b2, space 3) {} + +// expected-error@+1 {{register space cannot be specified on global constants}} +int d : register(c2, space3); + +struct S { + RWBuffer<int> a; +}; + +// expected-error@+1 {{register space cannot be specified on global constants}} +S thing : register(u2, space2); \ No newline at end of file _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits