llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-hlsl
Author: Diego Novillo (dnovillo)
<details>
<summary>Changes</summary>
Clang rejects `[[vk::builtin("PointSize")]]` on a pixel shader input with
`'PointSize' builtin cannot be used as pixel shader inputs`.
HLSL shaders often reuse the vertex shader's output struct as the pixel
shader's input, to avoid declaring a second struct. The pixel shader then
inherits the attribute and fails to compile, although it does not use
`PointSize`. I found this in Vulkan-Samples `compute_nbody`
([particle.frag.hlsl](https://github.com/KhronosGroup/Vulkan-Samples/blob/main/shaders/compute_nbody/hlsl/particle.frag.hlsl)).
Vulkan does not allow `BuiltIn PointSize` in the `Fragment` execution model
([VUID-PointSize-PointSize-04314](https://docs.vulkan.org/refpages/latest/refpages/source/PointSize.html)).
A pixel shader that declares the input but does not use it breaks this rule
only if the module still references the input. Clang should accept the
declaration and report an error only when a pixel entry point still accesses
the input after optimization.
This change does that in two parts:
1. Sema accepts the attribute on a pixel shader input. A pixel shader output,
including an `inout` parameter, is still an error.
2. The SPIR-V backend reports an error if a `Fragment` entry point or its
callees still access a `BuiltIn PointSize` variable after optimization. Vertex
entry points in the same module are unaffected.
Tests:
- `vk-builtin-point-size-stages.hlsl` and
`vk-builtin-point-size-inout-pixel.hlsl`: Sema accepts the pixel input and
still rejects pixel outputs.
- `vk-builtin-point-size-pixel-input.hlsl`: the pixel input lowers to a
`BuiltIn PointSize` input and does not take a location.
- `vk-builtin-point-size-pixel-input-spirv.hlsl`: an unread or dead-read input
compiles at `-O3` without `PointSize`. A direct, helper or scalar read, and any
use at `-O0`, report the error.
- `point-size.ll`: the backend reports the error for a direct read and a read
in a called function, and accepts a vertex output alone and beside a pixel
entry point.
Follow-up to [#<!--
-->231275](https://github.com/llvm/llvm-project/pull/231275).
---
Full diff: https://github.com/llvm/llvm-project/pull/243513.diff
8 Files Affected:
- (modified) clang/include/clang/Basic/AttrDocs.td (+5-2)
- (modified) clang/lib/Sema/SemaHLSL.cpp (+5-1)
- (added) clang/test/CodeGenHLSL/vk-builtin-point-size-pixel-input-spirv.hlsl
(+56)
- (added) clang/test/CodeGenHLSL/vk-builtin-point-size-pixel-input.hlsl (+29)
- (modified) clang/test/SemaHLSL/vk-builtin-point-size-inout-pixel.hlsl (+1-1)
- (modified) clang/test/SemaHLSL/vk-builtin-point-size-stages.hlsl (+4-5)
- (modified) llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp (+69)
- (added) llvm/test/CodeGen/SPIRV/semantics/point-size.ll (+113)
``````````diff
diff --git a/clang/include/clang/Basic/AttrDocs.td
b/clang/include/clang/Basic/AttrDocs.td
index 9828968c9d0f41..12e06b29c9e495 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -9790,8 +9790,11 @@ Currently supported built-ins:
* `PointSize`: the GLSL equivalent is `gl_PointSize`. Must be a scalar
`float`, or a `half` when native 16-bit types are not enabled (it is then
treated as `float`). Can be used as a vertex or mesh shader output, or as a
- hull, domain or geometry shader input or output. It cannot be used in a pixel
- shader.
+ hull, domain or geometry shader input or output. It can be declared as a
+ pixel shader input, so that a pixel shader can reuse the vertex shader's
+ output struct, but Vulkan does not allow its use there: compilation fails
+ if the shader uses the value, or if the input survives optimization. It
+ cannot be a pixel shader output.
A declaration annotated with `[[vk::builtin]]` does not take part in
`vk::location` assignment, and cannot also be annotated with
`[[vk::location]]`.
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 7627bb10ccf6f1..cb9ce76a9e8cfc 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -1075,7 +1075,11 @@ static void validateVkBuiltinUsage(Sema &S, FunctionDecl
*FD,
((Stage == llvm::Triple::Vertex && IsOut) ||
Stage == llvm::Triple::Hull || Stage == llvm::Triple::Domain ||
Stage == llvm::Triple::Geometry ||
- (Stage == llvm::Triple::Mesh && IsOut));
+ (Stage == llvm::Triple::Mesh && IsOut) ||
+ // Allowed so a pixel shader can take the vertex shader's output
+ // struct. The SPIR-V backend rejects the module if the input is
+ // still used after optimization.
+ (Stage == llvm::Triple::Pixel && IsIn && !IsOut));
if (!LegalStage) {
StringRef IOTypeName = (IsIn && IsOut) ? "inputs and outputs"
: IsIn ? "inputs"
diff --git
a/clang/test/CodeGenHLSL/vk-builtin-point-size-pixel-input-spirv.hlsl
b/clang/test/CodeGenHLSL/vk-builtin-point-size-pixel-input-spirv.hlsl
new file mode 100644
index 00000000000000..ecd4b81cb845bf
--- /dev/null
+++ b/clang/test/CodeGenHLSL/vk-builtin-point-size-pixel-input-spirv.hlsl
@@ -0,0 +1,56 @@
+// REQUIRES: spirv-registered-target
+
+// RUN: %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-pixel
-hlsl-entry main -O3 -S -o - %s | FileCheck %s --check-prefix=UNREAD
--implicit-check-not=PointSize
+// RUN: %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-pixel
-hlsl-entry main -O3 -DDEAD -S -o - %s | FileCheck %s --check-prefix=UNREAD
--implicit-check-not=PointSize
+// RUN: not %clang_cc1 -finclude-default-header -triple
spirv-pc-vulkan1.3-pixel -hlsl-entry main -O0 -S -o /dev/null %s 2>&1 |
FileCheck %s --check-prefix=ERROR
+// RUN: not %clang_cc1 -finclude-default-header -triple
spirv-pc-vulkan1.3-pixel -hlsl-entry main -O3 -DLIVE -S -o /dev/null %s 2>&1 |
FileCheck %s --check-prefix=ERROR
+// RUN: not %clang_cc1 -finclude-default-header -triple
spirv-pc-vulkan1.3-pixel -hlsl-entry main -O3 -DHELPER -S -o /dev/null %s 2>&1
| FileCheck %s --check-prefix=ERROR
+
+// RUN: %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-pixel
-hlsl-entry main -O3 -DSCALAR -S -o - %s | FileCheck %s --check-prefix=UNREAD
--implicit-check-not=PointSize
+// RUN: not %clang_cc1 -finclude-default-header -triple
spirv-pc-vulkan1.3-pixel -hlsl-entry main -O3 -DSCALAR -DLIVE -S -o /dev/null
%s 2>&1 | FileCheck %s --check-prefix=ERROR
+
+// RUN: %if spirv-val %{ %clang_cc1 -finclude-default-header -triple
spirv-pc-vulkan1.3-pixel -hlsl-entry main -O3 -emit-obj -o %t.spv %s %}
+// RUN: %if spirv-val %{ spirv-val --target-env vulkan1.3 %t.spv %}
+
+// A pixel shader PointSize input compiles when optimization removes it, and is
+// an error when the shader uses it or at -O0. It does not take a location.
+
+// UNREAD-DAG: OpEntryPoint Fragment
+// UNREAD-DAG: OpDecorate %[[#]] Location 0
+// UNREAD-DAG: OpDecorate %[[#]] Location 1
+
+// ERROR: error: BuiltIn PointSize cannot be used in a Fragment entry point
(VUID-PointSize-PointSize-04314)
+
+#ifdef SCALAR
+[shader("pixel")]
+float4 main(float a : A, [[vk::builtin("PointSize")]] float size : PSIZE,
+ float b : B) : SV_Target {
+#ifdef LIVE
+ return size;
+#else
+ return float4(a, b, 0, 1);
+#endif
+}
+#else
+struct Shared {
+ float a : A;
+ [[vk::builtin("PointSize")]] float size : PSIZE;
+ float b : B;
+};
+
+float readSize(Shared input) { return input.size; }
+
+[shader("pixel")]
+float4 main(Shared input) : SV_Target {
+#if defined(LIVE)
+ return input.size;
+#elif defined(HELPER)
+ return readSize(input);
+#else
+#ifdef DEAD
+ float dead = input.size;
+#endif
+ return float4(input.a, input.b, 0, 1);
+#endif
+}
+#endif
diff --git a/clang/test/CodeGenHLSL/vk-builtin-point-size-pixel-input.hlsl
b/clang/test/CodeGenHLSL/vk-builtin-point-size-pixel-input.hlsl
new file mode 100644
index 00000000000000..0224d92ad3d3a7
--- /dev/null
+++ b/clang/test/CodeGenHLSL/vk-builtin-point-size-pixel-input.hlsl
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-pixel
-hlsl-entry main -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s
+
+// A pixel shader PointSize input is a BuiltIn PointSize input and leaves the
+// other locations unchanged.
+
+struct VSOutput {
+ float4 pos : SV_Position;
+ [[vk::location(0)]] float a : A;
+ [[vk::builtin("PointSize")]] float size : PSIZE;
+ [[vk::location(1)]] float b : B;
+};
+
+[shader("pixel")]
+float4 main(VSOutput input) : SV_Target {
+ return float4(input.a, input.b, 0, 1);
+}
+
+// CHECK: @A0 = {{.*}}!spirv.Decorations ![[A_MD:[0-9]+]]
+// CHECK: @PSIZE = {{.*}}addrspace(7){{.*}}!spirv.Decorations
![[PSIZE_MD:[0-9]+]]
+// CHECK: @B0 = {{.*}}!spirv.Decorations ![[B_MD:[0-9]+]]
+// CHECK: define void @main()
+// CHECK: load float, ptr addrspace(7) @PSIZE
+
+// CHECK: ![[A_MD]] = !{![[A_DEC:[0-9]+]]}
+// CHECK: ![[A_DEC]] = !{i32 30, i32 0}
+// CHECK: ![[PSIZE_MD]] = !{![[PSIZE_DEC:[0-9]+]]}
+// CHECK: ![[PSIZE_DEC]] = !{i32 11, i32 1}
+// CHECK: ![[B_MD]] = !{![[B_DEC:[0-9]+]]}
+// CHECK: ![[B_DEC]] = !{i32 30, i32 1}
diff --git a/clang/test/SemaHLSL/vk-builtin-point-size-inout-pixel.hlsl
b/clang/test/SemaHLSL/vk-builtin-point-size-inout-pixel.hlsl
index c4daf2e1c88828..5b1891657383c6 100644
--- a/clang/test/SemaHLSL/vk-builtin-point-size-inout-pixel.hlsl
+++ b/clang/test/SemaHLSL/vk-builtin-point-size-inout-pixel.hlsl
@@ -1,6 +1,6 @@
// RUN: %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-pixel
-hlsl-entry ps -fsyntax-only -verify %s
-// PointSize is never legal in a pixel shader. An `inout` parameter is
+// PointSize is never legal as a pixel shader output. An `inout` parameter is
// currently treated as output-only here, so this is rejected (see the sibling
// vk-builtin-point-size-inout-vertex.hlsl, where `inout` on a stage where
// PointSize is a legal output is accepted).
diff --git a/clang/test/SemaHLSL/vk-builtin-point-size-stages.hlsl
b/clang/test/SemaHLSL/vk-builtin-point-size-stages.hlsl
index 61da95d27c4c18..d7d06d88d73643 100644
--- a/clang/test/SemaHLSL/vk-builtin-point-size-stages.hlsl
+++ b/clang/test/SemaHLSL/vk-builtin-point-size-stages.hlsl
@@ -4,8 +4,8 @@
// RUN: %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-pixel
-hlsl-entry ps_out -fsyntax-only -verify -DCASE_PS_OUT %s
// RUN: %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-compute
-hlsl-entry cs -fsyntax-only -verify -DCASE_CS %s
-// PointSize is legal as a vertex shader output; vertex shader input, pixel
-// shader input and output, and compute are not.
+// PointSize is legal as a vertex shader output and is ignored on a pixel
shader
+// input; vertex shader input, pixel shader output, and compute are errors.
#ifdef CASE_VS_OUT_OK
// expected-no-diagnostics
@@ -25,9 +25,8 @@ float4 vs_err([[vk::builtin("PointSize")]] float size :
PSIZE) : SV_Position {
#endif
#ifdef CASE_PS_IN
-// Vulkan does not allow BuiltIn PointSize in a fragment shader
-// (VUID-PointSize-PointSize-04314).
-// expected-error@+1 {{'PointSize' builtin cannot be used as pixel shader
inputs}}
+// expected-no-diagnostics
+// A pixel shader input is accepted and ignored.
float4 ps_in([[vk::builtin("PointSize")]] float size : PSIZE) : SV_Target {
return size;
}
diff --git a/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
b/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
index c3766765bc4bc7..925db4d3f39010 100644
--- a/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
@@ -26,10 +26,12 @@
#include "SPIRVTargetMachine.h"
#include "SPIRVUtils.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/IR/InstIterator.h"
using namespace llvm;
@@ -2806,8 +2808,75 @@ void addInstrRequirements(const MachineInstr &MI,
SPIRV::Capability::Shader);
}
+// Returns true if MF accesses a variable decorated BuiltIn PointSize.
+static bool accessesPointSize(const MachineFunction &MF) {
+ const MachineRegisterInfo &MRI = MF.getRegInfo();
+ for (const MachineBasicBlock &MBB : MF)
+ for (const MachineInstr &MI : MBB) {
+ if (MI.getOpcode() != SPIRV::OpDecorate ||
+ MI.getOperand(1).getImm() != SPIRV::Decoration::BuiltIn ||
+ MI.getOperand(2).getImm() != SPIRV::BuiltIn::PointSize)
+ continue;
+ for (const MachineInstr &User :
+ MRI.use_nodbg_instructions(MI.getOperand(0).getReg()))
+ if (User.getOpcode() != SPIRV::OpDecorate &&
+ User.getOpcode() != SPIRV::OpName)
+ return true;
+ }
+ return false;
+}
+
+// Vulkan allows BuiltIn PointSize only in the MeshEXT, MeshNV, Vertex,
+// TessellationControl, TessellationEvaluation and Geometry execution models
+// (VUID-PointSize-PointSize-04314), so the static call tree of a Fragment
entry
+// point must not access it.
+static void checkFragmentPointSize(const Module &M,
+ MachineFunctionGetter GetMF) {
+ for (const Function &EntryF : M) {
+ const MachineFunction *EntryMF = GetMF(EntryF);
+ if (!EntryMF)
+ continue;
+
+ // Find the Fragment entry point, if EntryF is one.
+ const MachineInstr *EntryPoint = nullptr;
+ for (const MachineBasicBlock &MBB : *EntryMF)
+ for (const MachineInstr &MI : MBB)
+ if (MI.getOpcode() == SPIRV::OpEntryPoint &&
+ MI.getOperand(0).getImm() == SPIRV::ExecutionModel::Fragment)
+ EntryPoint = &MI;
+ if (!EntryPoint)
+ continue;
+
+ // Walk its static call tree and report the first function that accesses
+ // PointSize.
+ SmallPtrSet<const Function *, 8> Visited;
+ SmallVector<const Function *, 8> Worklist = {&EntryF};
+ while (!Worklist.empty()) {
+ const Function *F = Worklist.pop_back_val();
+ if (!Visited.insert(F).second)
+ continue;
+ if (const MachineFunction *MF = GetMF(*F); MF && accessesPointSize(*MF))
{
+ reportUnsupported(*EntryPoint,
+ "BuiltIn PointSize cannot be used in a Fragment "
+ "entry point (VUID-PointSize-PointSize-04314)");
+ break;
+ }
+
+ // Queue the defined functions called by F.
+ for (const Instruction &I : instructions(*F))
+ if (const auto *Call = dyn_cast<CallBase>(&I))
+ if (const Function *Callee = Call->getCalledFunction();
+ Callee && !Callee->isDeclaration())
+ Worklist.push_back(Callee);
+ }
+ }
+}
+
static void collectReqs(const Module &M, SPIRV::ModuleAnalysisInfo &MAI,
MachineFunctionGetter GetMF, const SPIRVSubtarget &ST)
{
+ if (ST.isShader())
+ checkFragmentPointSize(M, GetMF);
+
// Collect requirements for existing instructions.
for (const Function &F : M) {
MachineFunction *MF = GetMF(F);
diff --git a/llvm/test/CodeGen/SPIRV/semantics/point-size.ll
b/llvm/test/CodeGen/SPIRV/semantics/point-size.ll
new file mode 100644
index 00000000000000..8da4e7cb9da022
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/semantics/point-size.ll
@@ -0,0 +1,113 @@
+; RUN: split-file %s %t
+; RUN: not llc -O0 -mtriple=spirv-unknown-vulkan1.3-pixel %t/pixel.ll -o
/dev/null 2>&1 | FileCheck %s --check-prefix=ERROR
+; RUN: not llc -O0 -mtriple=spirv-unknown-vulkan1.3-pixel %t/helper.ll -o
/dev/null 2>&1 | FileCheck %s --check-prefix=ERROR
+; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv-unknown-vulkan1.3-vertex
%t/vertex.ll -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-vulkan1.3-vertex
%t/vertex.ll -o - -filetype=obj | spirv-val --target-env vulkan1.3 %}
+; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv-unknown-vulkan1.3
%t/mixed.ll -o - | FileCheck %s --check-prefix=MIXED
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-vulkan1.3 %t/mixed.ll
-o - -filetype=obj | spirv-val --target-env vulkan1.3 %}
+
+; BuiltIn PointSize variables cannot be used in Fragment shaders, directly or
+; through a called function. A Vertex entry point in the same module can still
+; write one.
+
+; ERROR: error: <unknown>:0:0: in function main void (): BuiltIn PointSize
cannot be used in a Fragment entry point (VUID-PointSize-PointSize-04314)
+
+; CHECK-DAG: OpEntryPoint Vertex
+; CHECK-DAG: OpDecorate %[[#PSIZE:]] BuiltIn PointSize
+; CHECK-DAG: %[[#FLOAT:]] = OpTypeFloat 32
+; CHECK-DAG: %[[#PTR:]] = OpTypePointer Output %[[#FLOAT]]
+; CHECK-DAG: %[[#PSIZE]] = OpVariable %[[#PTR]] Output
+; CHECK: OpStore %[[#PSIZE]]
+
+; MIXED-DAG: OpEntryPoint Vertex %[[#]] "vs"
+; MIXED-DAG: OpEntryPoint Fragment %[[#]] "ps"
+; MIXED-DAG: OpDecorate %[[#]] BuiltIn PointSize
+
+;--- pixel.ll
+@PSIZE = external hidden thread_local addrspace(7) externally_initialized
constant float, !spirv.Decorations !0
+@SV_Target0 = external hidden thread_local addrspace(8) global float,
!spirv.Decorations !2
+
+define void @main() #0 {
+entry:
+ %0 = load float, ptr addrspace(7) @PSIZE, align 4
+ store float %0, ptr addrspace(8) @SV_Target0, align 4
+ ret void
+}
+
+attributes #0 = { "hlsl.shader"="pixel" }
+
+!0 = !{!1}
+!1 = !{i32 11, i32 1}
+!2 = !{!3}
+!3 = !{i32 30, i32 0}
+
+;--- helper.ll
+@PSIZE = external hidden thread_local addrspace(7) externally_initialized
constant float, !spirv.Decorations !0
+@SV_Target0 = external hidden thread_local addrspace(8) global float,
!spirv.Decorations !2
+
+define internal spir_func float @read_size() #1 {
+entry:
+ %0 = load float, ptr addrspace(7) @PSIZE, align 4
+ ret float %0
+}
+
+define void @main() #0 {
+entry:
+ %0 = call spir_func float @read_size()
+ store float %0, ptr addrspace(8) @SV_Target0, align 4
+ ret void
+}
+
+attributes #0 = { "hlsl.shader"="pixel" }
+attributes #1 = { noinline }
+
+!0 = !{!1}
+!1 = !{i32 11, i32 1}
+!2 = !{!3}
+!3 = !{i32 30, i32 0}
+
+;--- vertex.ll
+@SV_Position = external hidden thread_local addrspace(8) global <4 x float>,
!spirv.Decorations !0
+@PSIZE = external hidden thread_local addrspace(8) global float,
!spirv.Decorations !2
+
+define void @main() #0 {
+entry:
+ store <4 x float> zeroinitializer, ptr addrspace(8) @SV_Position, align 16
+ store float 1.000000e+00, ptr addrspace(8) @PSIZE, align 4
+ ret void
+}
+
+attributes #0 = { "hlsl.shader"="vertex" }
+
+!0 = !{!1}
+!1 = !{i32 11, i32 0}
+!2 = !{!3}
+!3 = !{i32 11, i32 1}
+
+;--- mixed.ll
+@SV_Position = external hidden thread_local addrspace(8) global <4 x float>,
!spirv.Decorations !0
+@PSIZE = external hidden thread_local addrspace(8) global float,
!spirv.Decorations !2
+@SV_Target0 = external hidden thread_local addrspace(8) global float,
!spirv.Decorations !4
+
+define void @vs() #0 {
+entry:
+ store <4 x float> zeroinitializer, ptr addrspace(8) @SV_Position, align 16
+ store float 1.000000e+00, ptr addrspace(8) @PSIZE, align 4
+ ret void
+}
+
+define void @ps() #1 {
+entry:
+ store float 1.000000e+00, ptr addrspace(8) @SV_Target0, align 4
+ ret void
+}
+
+attributes #0 = { "hlsl.shader"="vertex" }
+attributes #1 = { "hlsl.shader"="pixel" }
+
+!0 = !{!1}
+!1 = !{i32 11, i32 0}
+!2 = !{!3}
+!3 = !{i32 11, i32 1}
+!4 = !{!5}
+!5 = !{i32 30, i32 0}
``````````
</details>
https://github.com/llvm/llvm-project/pull/243513
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits