Skip to content

Commit 889b2d6

Browse files
authored
[HLSL] Adds diagnostics for missing/ambiguous shader entry function. (#184892)
Addresses #119260.
1 parent be1b1a7 commit 889b2d6

156 files changed

Lines changed: 336 additions & 266 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13712,6 +13712,9 @@ def err_hlsl_matrix_layout_non_matrix
1371213712
def err_hlsl_matrix_layout_conflict
1371313713
: Error<"%0 and %1 attributes are not compatible">;
1371413714

13715+
def err_hlsl_ambiguous_entry_point : Error<
13716+
"ambiguous entry point definition '%0'">;
13717+
def err_hlsl_missing_entry_point : Error<"missing entry point definition '%0'">;
1371513718
def err_hlsl_entry_shader_attr_mismatch : Error<
1371613719
"%0 attribute on entry function does not match the target profile">;
1371713720
def err_hlsl_numthreads_argument_oor : Error<"argument '%select{X|Y|Z}0' to numthreads attribute cannot exceed %1">;

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,6 +2993,11 @@ void DiagnoseHLSLAvailability::HandleFunctionOrMethodRef(FunctionDecl *FD,
29932993

29942994
void DiagnoseHLSLAvailability::RunOnTranslationUnit(
29952995
const TranslationUnitDecl *TU) {
2996+
const TargetInfo &TargetInfo = SemaRef.getASTContext().getTargetInfo();
2997+
std::string &EntryName = TargetInfo.getTargetOpts().HLSLEntry;
2998+
bool IsLibraryShader = TargetInfo.getTriple().getEnvironment() ==
2999+
llvm::Triple::EnvironmentType::Library;
3000+
SourceLocation EntryLoc{};
29963001

29973002
// Iterate over all shader entry functions and library exports, and for those
29983003
// that have a body (definiton), run diag scan on each, setting appropriate
@@ -3023,6 +3028,17 @@ void DiagnoseHLSLAvailability::RunOnTranslationUnit(
30233028

30243029
// shader entry point
30253030
if (HLSLShaderAttr *ShaderAttr = FD->getAttr<HLSLShaderAttr>()) {
3031+
if (!IsLibraryShader && FD->getName() == EntryName) {
3032+
if (EntryLoc.isValid()) {
3033+
SemaRef.Diag(FD->getLocation(),
3034+
diag::err_hlsl_ambiguous_entry_point)
3035+
<< EntryName;
3036+
SemaRef.Diag(EntryLoc, diag::note_previous_declaration_as)
3037+
<< EntryName;
3038+
return;
3039+
}
3040+
EntryLoc = FD->getLocation();
3041+
}
30263042
SetShaderStageContext(ShaderAttr->getType());
30273043
RunOnFunction(FD);
30283044
continue;
@@ -3046,6 +3062,12 @@ void DiagnoseHLSLAvailability::RunOnTranslationUnit(
30463062
}
30473063
}
30483064
}
3065+
3066+
if (!IsLibraryShader && EntryLoc.isInvalid()) {
3067+
SemaRef.Diag(TU->getLocation(), diag::err_hlsl_missing_entry_point)
3068+
<< EntryName;
3069+
return;
3070+
}
30493071
}
30503072

30513073
void DiagnoseHLSLAvailability::RunOnFunction(const FunctionDecl *FD) {

clang/test/AST/HLSL/HLSLControlFlowHint.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-compute -ast-dump %s | FileCheck %s
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -ast-dump %s | FileCheck %s
22

33
// CHECK: FunctionDecl {{.*}} used branch 'int (int)'
44
// CHECK: AttributedStmt

clang/test/AST/HLSL/matrix-alias.hlsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -ast-dump -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -hlsl-entry entry -ast-dump -o - %s | FileCheck %s
22

33
// Test that matrix aliases are set up properly for HLSL
44

@@ -20,8 +20,8 @@
2020
// Make sure we got a using directive at the end.
2121
// CHECK: UsingDirectiveDecl 0x{{[0-9a-fA-F]+}} <<invalid sloc>> <invalid sloc> Namespace 0x{{[0-9a-fA-F]+}} 'hlsl'
2222

23-
[numthreads(1,1,1)]
24-
int entry() {
23+
[shader("compute"), numthreads(1,1,1)]
24+
void entry() {
2525
// Verify that the alias is generated inside the hlsl namespace.
2626
hlsl::matrix<float, 2, 2> Mat2x2f;
2727

@@ -45,5 +45,5 @@ int entry() {
4545

4646
// CHECK: DeclStmt 0x{{[0-9a-fA-F]+}} <line:44:3, col:21>
4747
// CHECK-NEXT: VarDecl 0x{{[0-9a-fA-F]+}} <col:3, col:12> col:12 ImpMat4x4 'matrix<>':'matrix<float, 4, 4>'
48-
return 1;
48+
return;
4949
}

clang/test/AST/HLSL/matrix-constructors.hlsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -ast-dump -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -hlsl-entry ok -ast-dump -o - %s | FileCheck %s
22

33

44
typedef float float2x1 __attribute__((matrix_type(2,1)));
@@ -8,6 +8,7 @@ typedef float float4x4 __attribute__((matrix_type(4,4)));
88
typedef float float2 __attribute__((ext_vector_type(2)));
99
typedef float float4 __attribute__((ext_vector_type(4)));
1010

11+
[shader("compute")]
1112
[numthreads(1,1,1)]
1213
void ok() {
1314
// CHECK: VarDecl 0x{{[0-9a-fA-F]+}} <col:{{[0-9]+}}, col:{{[0-9]+}}> col:{{[0-9]+}} A 'float2x3':'matrix<float, 2, 3>' cinit

clang/test/AST/HLSL/matrix-elementexpr-tree-transform.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -std=hlsl202x \
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -std=hlsl202x \
22
// RUN: -finclude-default-header -ast-dump -ast-dump-filter=get00 %s | FileCheck %s
33

44
template <typename T>

clang/test/AST/HLSL/matrix-general-initializer.hlsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -ast-dump -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -hlsl-entry ok -ast-dump -o - %s | FileCheck %s
22

33
typedef float float4x2 __attribute__((matrix_type(4,2)));
44
typedef float float2x2 __attribute__((matrix_type(2,2)));
55
typedef int int4x4 __attribute__((matrix_type(4,4)));
66

7-
7+
[shader("compute")]
88
[numthreads(1,1,1)]
99
void ok() {
1010

@@ -257,4 +257,4 @@ float2x2 m2 = {0.xxxx};
257257
// CHECK-NEXT: IntegerLiteral 0x{{[0-9a-fA-F]+}} <col:{{[0-9]+}}> 'int' 1
258258
int4x4 m3 = {m2, m2, m2, m2};
259259

260-
}
260+
}

clang/test/AST/HLSL/matrix-member-access-scalar.hlsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -ast-dump -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -hlsl-entry ok -ast-dump -o - %s | FileCheck %s
22

33
typedef float float3x3 __attribute__((matrix_type(3,3)));
44

5+
[shader("compute")]
56
[numthreads(1,1,1)]
67
void ok() {
78
float3x3 A;

clang/test/AST/HLSL/matrix-member-access-swizzle.hlsl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -ast-dump -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -hlsl-entry ok -ast-dump -o - %s | FileCheck %s
22

33
typedef float float3x3 __attribute__((matrix_type(3,3)));
44
typedef float float4x4 __attribute__((matrix_type(4,4)));
55
typedef float float2 __attribute__((ext_vector_type(2)));
66
typedef float float3 __attribute__((ext_vector_type(3)));
77
typedef float float4 __attribute__((ext_vector_type(4)));
88

9+
[shader("compute")]
910
[numthreads(1,1,1)]
1011
void ok() {
1112
float3x3 A;

clang/test/AST/HLSL/resources-in-structs-errors.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -verify %s
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -verify %s
22

33
struct A {
44
RWBuffer<float> Buf;

0 commit comments

Comments
 (0)