Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sema: gracefully reject 0-sized IO patch
Browse files Browse the repository at this point in the history
InputPatch and OutputPatch array size must be greater than 0.
Before this patch we emitted a validation error, which told the
user this was a compiler issue.

Signed-off-by: Nathan Gauër <brioche@google.com>
Keenuts committed Jan 9, 2024
1 parent eb4cec4 commit a72ec35
Showing 4 changed files with 57 additions and 1 deletion.
4 changes: 4 additions & 0 deletions tools/clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
@@ -7884,6 +7884,10 @@ def err_hlsl_node_record_object : Error<
"object %0 may not appear in a node record">;
def err_hlsl_array_disallowed : Error<
"%select{entry parameter|declaration}1 of type %0 may not be an array">;
def err_hlsl_inputpatch_size: Error<
"InputPatch element count must be greater than 0">;
def err_hlsl_outputpatch_size: Error<
"OutputPatch element count must be greater than 0">;
def note_hlsl_node_array : Note<"'%0' cannot be used as an array; did you mean '%0Array'?">;
// HLSL Change Ends

21 changes: 20 additions & 1 deletion tools/clang/lib/Sema/SemaHLSL.cpp
Original file line number Diff line number Diff line change
@@ -15138,12 +15138,30 @@ void DiagnoseMeshEntry(Sema &S, FunctionDecl *FD, llvm::StringRef StageName) {
return;
}

void DiagnoseDomainEntry(Sema &S, FunctionDecl *FD, llvm::StringRef StageName) {
for (const auto *param : FD->params()) {
if (!hlsl::IsHLSLOutputPatchType(param->getType()))
continue;
if (hlsl::GetHLSLInputPatchCount(param->getType()) > 0)
continue;
S.Diags.Report(param->getLocation(), diag::err_hlsl_inputpatch_size);
}
return;
}

void DiagnoseHullEntry(Sema &S, FunctionDecl *FD, llvm::StringRef StageName) {
HLSLPatchConstantFuncAttr *Attr = FD->getAttr<HLSLPatchConstantFuncAttr>();
if (!Attr)
S.Diags.Report(FD->getLocation(), diag::err_hlsl_missing_attr)
<< StageName << "patchconstantfunc";

for (const auto *param : FD->params()) {
if (!hlsl::IsHLSLInputPatchType(param->getType()))
continue;
if (hlsl::GetHLSLInputPatchCount(param->getType()) > 0)
continue;
S.Diags.Report(param->getLocation(), diag::err_hlsl_inputpatch_size);
}
return;
}

@@ -15570,7 +15588,6 @@ void DiagnoseEntry(Sema &S, FunctionDecl *FD) {
switch (Stage) {
case DXIL::ShaderKind::Pixel:
case DXIL::ShaderKind::Vertex:
case DXIL::ShaderKind::Domain:
case DXIL::ShaderKind::Library:
case DXIL::ShaderKind::Invalid:
return;
@@ -15580,6 +15597,8 @@ void DiagnoseEntry(Sema &S, FunctionDecl *FD) {
case DXIL::ShaderKind::Mesh: {
return DiagnoseMeshEntry(S, FD, StageName);
}
case DXIL::ShaderKind::Domain:
return DiagnoseDomainEntry(S, FD, StageName);
case DXIL::ShaderKind::Hull: {
return DiagnoseHullEntry(S, FD, StageName);
}
11 changes: 11 additions & 0 deletions tools/clang/test/CodeGenHLSL/ds-error-outputpatch-size.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: not %dxc -T ds_6_0 -E main %s 2>&1 | FileCheck %s

struct ControlPoint {
float position : MY_BOOL;
};

ControlPoint main(const OutputPatch<ControlPoint, 0> patch) {
// CHECK: error: OutputPatch element count must be greater than 0
return patch[0];
}

22 changes: 22 additions & 0 deletions tools/clang/test/CodeGenHLSL/hs-error-inputpatch-size.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: not %dxc -T hs_6_0 -E main %s 2>&1 | FileCheck %s

struct ControlPoint {
float position : MY_BOOL;
};

struct PatchData {
float edge [3] : SV_TessFactor;
float inside : SV_InsideTessFactor;
};

PatchData HullConst () { return (PatchData)0; }

[domain("tri")]
[partitioning("fractional_odd")]
[outputtopology("triangle_cw")]
[patchconstantfunc("HullConst")]
[outputcontrolpoints(3)]
ControlPoint main(InputPatch<ControlPoint, 0> v, uint id : SV_OutputControlPointID) {
// CHECK: error: InputPatch element count must be greater than 0
return v[id];
}

0 comments on commit a72ec35

Please sign in to comment.