Skip to content

Commit 1e98bf3

Browse files
authored
[HLSL2021] Disallow bitfields and annotations (microsoft#4711)
This change makes mixing bitfields and HLSL annotations an error. Since the syntax for HLSL annotations and bitfields are ambiguous, this change works around some issues with the ambiguity by disallowing mixing the two on the same declaration. Fixes microsoft#4686
1 parent 4792ae5 commit 1e98bf3

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

tools/clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7457,6 +7457,8 @@ def err_hlsl_attribute_valid_on_function_only: Error<
74577457
"attribute is valid only on functions">;
74587458
def err_hlsl_bitfields: Error<
74597459
"bitfields are not supported in HLSL">;
7460+
def err_hlsl_bitfields_with_annotation: Error<
7461+
"bitfields are not allowed with HLSL annotations">;
74607462
def err_hlsl_cannot_convert: Error<
74617463
"cannot %select{implicitly |}0convert %select{|output parameter }1from %2 to %3">;
74627464
def err_hlsl_half_load_store: Error<

tools/clang/lib/Sema/SemaHLSL.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13389,9 +13389,15 @@ bool Sema::DiagnoseHLSLDecl(Declarator &D, DeclContext *DC, Expr *BitWidth,
1338913389
// SPIRV change ends
1339013390

1339113391
// Disallow bitfields where not enabled explicitly or by HV
13392-
if (BitWidth && !getLangOpts().EnableBitfields) {
13393-
Diag(BitWidth->getExprLoc(), diag::err_hlsl_bitfields);
13394-
result = false;
13392+
if (BitWidth) {
13393+
if (!getLangOpts().EnableBitfields) {
13394+
Diag(BitWidth->getExprLoc(), diag::err_hlsl_bitfields);
13395+
result = false;
13396+
} else if (!D.UnusualAnnotations.empty()) {
13397+
Diag(BitWidth->getExprLoc(),
13398+
diag::err_hlsl_bitfields_with_annotation);
13399+
result = false;
13400+
}
1339513401
}
1339613402

1339713403
// Validate unusual annotations.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 -fsyntax-only -HV 2021 -verify %s
2+
3+
struct [raypayload] Payload {
4+
int a : 1 : write(caller) : read(anyhit); // expected-error {{bitfields are not allowed with HLSL annotations}}
5+
int b : 17 : write(miss) : read(caller); // expected-error {{bitfields are not allowed with HLSL annotations}}
6+
int c : write(miss) : 13 : read(caller); // expected-error {{bitfields are not allowed with HLSL annotations}}
7+
int d : write(miss) : read(caller);
8+
};
9+
10+
struct Inputs {
11+
int a : 16 : SV_GroupIndex; // expected-error {{bitfields are not allowed with HLSL annotations}}
12+
int b : SV_Position : 16; // expected-error {{bitfields are not allowed with HLSL annotations}}
13+
};

tools/clang/unittests/HLSL/VerifierTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class VerifierTest TEST_CLASS_DERIVATION {
100100
TEST_METHOD(RunWriteConstArrays)
101101
TEST_METHOD(RunAtomicsOnBitfields)
102102
TEST_METHOD(RunUnboundedResourceArrays)
103+
TEST_METHOD(RunBitFieldAnnotations)
103104
void CheckVerifies(const wchar_t* path) {
104105
WEX::TestExecution::SetVerifyOutput verifySettings(WEX::TestExecution::VerifyOutputSettings::LogOnlyFailures);
105106
const char startMarker[] = "%clang_cc1";
@@ -433,3 +434,7 @@ TEST_F(VerifierTest, RunAtomicsOnBitfields) {
433434
TEST_F(VerifierTest, RunUnboundedResourceArrays) {
434435
CheckVerifiesHLSL(L"invalid-unbounded-resource-arrays.hlsl");
435436
}
437+
438+
TEST_F(VerifierTest, RunBitFieldAnnotations) {
439+
CheckVerifiesHLSL(L"bitfields-and-annotations.hlsl");
440+
}

0 commit comments

Comments
 (0)