Skip to content

Commit

Permalink
[Sema] Fix reporting of invalid shader attribute on HLSL entry function
Browse files Browse the repository at this point in the history
If the HLSL entry function had a shader attribute that conflicted
with the pipeline stage specified in the target triple, Clang
would emit:

   error: (null) attribute parameters do not match the previous declaration
   conflicting attribute is here

(where the second line doesn't reference an attribute).

This was because the code constructed a dummy attribute that had
only a source location, but no kind or syntax.

Noticed while doing some changes to the attribute handling.

Differential Revision: https://reviews.llvm.org/D147657
  • Loading branch information
rsandifo-arm committed Apr 6, 2023
1 parent b29ec28 commit 03a9a1e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -11752,6 +11752,8 @@ def err_hlsl_attr_invalid_type : Error<
"attribute %0 only applies to a field or parameter of type '%1'">;
def err_hlsl_attr_invalid_ast_node : Error<
"attribute %0 only applies to %1">;
def err_hlsl_entry_shader_attr_mismatch : Error<
"%0 attribute on entry function does not match the pipeline stage">;
def err_hlsl_numthreads_argument_oor : Error<"argument '%select{X|Y|Z}0' to numthreads attribute cannot exceed %1">;
def err_hlsl_numthreads_invalid : Error<"total number of threads cannot exceed %0">;
def err_hlsl_missing_numthreads : Error<"missing numthreads attribute for %0 shader entry">;
Expand Down
11 changes: 8 additions & 3 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10205,14 +10205,19 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
CheckHLSLEntryPoint(NewFD);
if (!NewFD->isInvalidDecl()) {
auto Env = TargetInfo.getTriple().getEnvironment();
AttributeCommonInfo AL(NewFD->getBeginLoc());
HLSLShaderAttr::ShaderType ShaderType =
static_cast<HLSLShaderAttr::ShaderType>(
hlsl::getStageFromEnvironment(Env));
// To share code with HLSLShaderAttr, add HLSLShaderAttr to entry
// function.
if (HLSLShaderAttr *Attr = mergeHLSLShaderAttr(NewFD, AL, ShaderType))
NewFD->addAttr(Attr);
if (HLSLShaderAttr *NT = NewFD->getAttr<HLSLShaderAttr>()) {
if (NT->getType() != ShaderType)
Diag(NT->getLocation(), diag::err_hlsl_entry_shader_attr_mismatch)
<< NT;
} else {
NewFD->addAttr(HLSLShaderAttr::Create(Context, ShaderType,
NewFD->getBeginLoc()));
}
}
}
// HLSL does not support specifying an address space on a function return
Expand Down
8 changes: 8 additions & 0 deletions clang/test/SemaHLSL/entry_shader.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-compute -x hlsl -hlsl-entry foo -o - %s -DSHADER='"anyHit"' -verify
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-compute -x hlsl -hlsl-entry foo -o - %s -DSHADER='"compute"'

// expected-error@+1 {{'shader' attribute on entry function does not match the pipeline stage}}
[numthreads(1,1,1), shader(SHADER)]
void foo() {

}

0 comments on commit 03a9a1e

Please sign in to comment.