[clang] [llvm] [VK][HLSL][SPIRV] Accept vk::builtin("PointSize") on pixel shader inputs (PR #243513)

Diego Novillo via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 13:05:05 PST 2026


https://github.com/dnovillo updated https://github.com/llvm/llvm-project/pull/243513

>From 5e2efe2e31a06eb38597ff59b02e4c445437eff2 Mon Sep 17 00:00:00 2001
From: Diego Novillo <dnovillo at nvidia.com>
Date: Tue, 17 Nov 2026 10:31:36 -0500
Subject: [PATCH 1/2] [VK][HLSL][SPIRV] Accept vk::builtin("PointSize") on
 pixel shader inputs

---
 clang/include/clang/Basic/AttrDocs.td         |   7 +-
 clang/lib/Sema/SemaHLSL.cpp                   |   6 +-
 ...-builtin-point-size-pixel-input-spirv.hlsl |  56 +++++++++
 .../vk-builtin-point-size-pixel-input.hlsl    |  29 +++++
 .../vk-builtin-point-size-inout-pixel.hlsl    |   2 +-
 .../vk-builtin-point-size-stages.hlsl         |   9 +-
 llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp |  69 +++++++++++
 .../CodeGen/SPIRV/semantics/point-size.ll     | 113 ++++++++++++++++++
 8 files changed, 282 insertions(+), 9 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/vk-builtin-point-size-pixel-input-spirv.hlsl
 create mode 100644 clang/test/CodeGenHLSL/vk-builtin-point-size-pixel-input.hlsl
 create mode 100644 llvm/test/CodeGen/SPIRV/semantics/point-size.ll

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 at +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
+ at PSIZE = external hidden thread_local addrspace(7) externally_initialized constant float, !spirv.Decorations !0
+ at 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
+ at PSIZE = external hidden thread_local addrspace(7) externally_initialized constant float, !spirv.Decorations !0
+ at 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
+ at SV_Position = external hidden thread_local addrspace(8) global <4 x float>, !spirv.Decorations !0
+ at 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
+ at SV_Position = external hidden thread_local addrspace(8) global <4 x float>, !spirv.Decorations !0
+ at PSIZE = external hidden thread_local addrspace(8) global float, !spirv.Decorations !2
+ at 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}

>From cd8802ca331b03b0b7d19da82244a7d6aab0739a Mon Sep 17 00:00:00 2001
From: Diego Novillo <dnovillo at nvidia.com>
Date: Thu, 26 Nov 2026 16:04:38 -0500
Subject: [PATCH 2/2] address review feedback

---
 clang/include/clang/Basic/AttrDocs.td         |  8 +-
 ...n-point-size-pixel-input-spirv-errors.hlsl | 57 ++++++++++++++
 ...-builtin-point-size-pixel-input-spirv.hlsl | 78 +++++++++----------
 .../vk-builtin-point-size-stages.hlsl         | 10 ++-
 llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp | 20 ++---
 .../CodeGen/SPIRV/semantics/point-size.ll     |  2 +-
 6 files changed, 112 insertions(+), 63 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/vk-builtin-point-size-pixel-input-spirv-errors.hlsl

diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index 12e06b29c9e495..69dd0f42cfd3e1 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -9792,9 +9792,11 @@ Currently supported built-ins:
   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 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.
+  output struct, but Vulkan does not allow the pixel shader to use it.
+  Compilation fails if a read of the value survives optimization. At `-O0`,
+  it also fails for an unread struct field, because the entry point copies
+  every field of the struct. An unread scalar input compiles at every
+  optimization level. 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/test/CodeGenHLSL/vk-builtin-point-size-pixel-input-spirv-errors.hlsl b/clang/test/CodeGenHLSL/vk-builtin-point-size-pixel-input-spirv-errors.hlsl
new file mode 100644
index 00000000000000..ac16a058ff8851
--- /dev/null
+++ b/clang/test/CodeGenHLSL/vk-builtin-point-size-pixel-input-spirv-errors.hlsl
@@ -0,0 +1,57 @@
+// REQUIRES: spirv-registered-target
+
+// RUN: split-file %s %t
+// RUN: %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-pixel -hlsl-entry main -O3 -S -o /dev/null -verify %t/read.hlsl
+// RUN: %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-pixel -hlsl-entry main -O3 -S -o /dev/null -verify %t/helper.hlsl
+// RUN: %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-pixel -hlsl-entry main -O3 -S -o /dev/null -verify %t/scalar.hlsl
+// RUN: %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-pixel -hlsl-entry main -O0 -S -o /dev/null -verify %t/unread-O0.hlsl
+
+// The SPIR-V backend reports a pixel shader PointSize input that is still used
+// after optimization. The error does not carry a source location.
+
+//--- read.hlsl
+struct Shared {
+  float a : A;
+  [[vk::builtin("PointSize")]] float size : PSIZE;
+};
+
+// expected-error@* {{BuiltIn PointSize cannot be used in a Fragment entry point (VUID-PointSize-PointSize-04314)}}
+[shader("pixel")]
+float4 main(Shared input) : SV_Target {
+  return input.size;
+}
+
+//--- helper.hlsl
+struct Shared {
+  float a : A;
+  [[vk::builtin("PointSize")]] float size : PSIZE;
+};
+
+float readSize(Shared input) { return input.size; }
+
+// expected-error@* {{BuiltIn PointSize cannot be used in a Fragment entry point (VUID-PointSize-PointSize-04314)}}
+[shader("pixel")]
+float4 main(Shared input) : SV_Target {
+  return readSize(input);
+}
+
+//--- scalar.hlsl
+// expected-error@* {{BuiltIn PointSize cannot be used in a Fragment entry point (VUID-PointSize-PointSize-04314)}}
+[shader("pixel")]
+float4 main([[vk::builtin("PointSize")]] float size : PSIZE) : SV_Target {
+  return size;
+}
+
+//--- unread-O0.hlsl
+// At -O0 the entry point copies every field of the input struct, so an unread
+// field is still used.
+struct Shared {
+  float a : A;
+  [[vk::builtin("PointSize")]] float size : PSIZE;
+};
+
+// expected-error@* {{BuiltIn PointSize cannot be used in a Fragment entry point (VUID-PointSize-PointSize-04314)}}
+[shader("pixel")]
+float4 main(Shared input) : SV_Target {
+  return float4(input.a, 0, 0, 1);
+}
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
index ecd4b81cb845bf..649fc6fa56677b 100644
--- a/clang/test/CodeGenHLSL/vk-builtin-point-size-pixel-input-spirv.hlsl
+++ b/clang/test/CodeGenHLSL/vk-builtin-point-size-pixel-input-spirv.hlsl
@@ -1,56 +1,54 @@
 // 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)
+// RUN: split-file %s %t
+// RUN: %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-pixel -hlsl-entry main -O3 -S -o - %t/unread.hlsl | FileCheck %s --implicit-check-not=PointSize --implicit-check-not="Location 2"
+// RUN: %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-pixel -hlsl-entry main -O3 -S -o - %t/dead.hlsl | FileCheck %s --implicit-check-not=PointSize --implicit-check-not="Location 2"
+// RUN: %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-pixel -hlsl-entry main -O3 -S -o - %t/scalar.hlsl | FileCheck %s --implicit-check-not=PointSize --implicit-check-not="Location 2"
+// RUN: %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-pixel -hlsl-entry main -O0 -S -o - %t/scalar.hlsl | FileCheck %s --implicit-check-not=PointSize --implicit-check-not="Location 2"
+
+// RUN: %if spirv-val %{ %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-pixel -hlsl-entry main -O3 -emit-obj -o %t/unread.spv %t/unread.hlsl %}
+// RUN: %if spirv-val %{ spirv-val --target-env vulkan1.3 %t/unread.spv %}
+// RUN: %if spirv-val %{ %clang_cc1 -finclude-default-header -triple spirv-pc-vulkan1.3-pixel -hlsl-entry main -O0 -emit-obj -o %t/scalar.spv %t/scalar.hlsl %}
+// RUN: %if spirv-val %{ spirv-val --target-env vulkan1.3 %t/scalar.spv %}
+
+// A pixel shader PointSize input that optimization removes compiles. The
+// checks establish that PointSize is gone and that the input does not take a
+// location: B keeps Location 1 and nothing takes Location 2. The error cases
+// are in vk-builtin-point-size-pixel-input-spirv-errors.hlsl.
+
+// CHECK-DAG: OpEntryPoint Fragment
+// CHECK-DAG: OpDecorate %[[#]] Location 0
+// CHECK-DAG: OpDecorate %[[#]] Location 1
+
+//--- unread.hlsl
+struct Shared {
+  float a : A;
+  [[vk::builtin("PointSize")]] float size : PSIZE;
+  float b : B;
+};
 
-#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
+float4 main(Shared input) : SV_Target {
+  return float4(input.a, input.b, 0, 1);
 }
-#else
+
+//--- dead.hlsl
 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
+
+//--- scalar.hlsl
+// Unlike an unread struct field, an unread scalar also compiles at -O0.
+[shader("pixel")]
+float4 main(float a : A, [[vk::builtin("PointSize")]] float size : PSIZE,
+            float b : B) : SV_Target {
+  return float4(a, b, 0, 1);
+}
diff --git a/clang/test/SemaHLSL/vk-builtin-point-size-stages.hlsl b/clang/test/SemaHLSL/vk-builtin-point-size-stages.hlsl
index d7d06d88d73643..cace13fb3308ae 100644
--- a/clang/test/SemaHLSL/vk-builtin-point-size-stages.hlsl
+++ b/clang/test/SemaHLSL/vk-builtin-point-size-stages.hlsl
@@ -4,8 +4,9 @@
 // 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 and is ignored on a pixel shader
-// input; vertex shader input, pixel shader output, and compute are errors.
+// PointSize is legal as a vertex shader output and is accepted on a pixel
+// shader input; vertex shader input, pixel shader output, and compute are
+// errors.
 
 #ifdef CASE_VS_OUT_OK
 // expected-no-diagnostics
@@ -26,9 +27,10 @@ float4 vs_err([[vk::builtin("PointSize")]] float size : PSIZE) : SV_Position {
 
 #ifdef CASE_PS_IN
 // expected-no-diagnostics
-// A pixel shader input is accepted and ignored.
+// Sema accepts a pixel shader input. A use of it that survives optimization
+// is a SPIR-V backend error.
 float4 ps_in([[vk::builtin("PointSize")]] float size : PSIZE) : SV_Target {
-  return size;
+  return 0;
 }
 #endif
 
diff --git a/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp b/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
index 925db4d3f39010..0ef126c0bb7b6d 100644
--- a/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
@@ -2833,18 +2833,8 @@ static bool accessesPointSize(const MachineFunction &MF) {
 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)
+    // Pixel shaders become Fragment entry points.
+    if (EntryF.getFnAttribute("hlsl.shader").getValueAsString() != "pixel")
       continue;
 
     // Walk its static call tree and report the first function that accesses
@@ -2856,9 +2846,9 @@ static void checkFragmentPointSize(const Module &M,
       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)");
+        EntryF.getContext().diagnose(DiagnosticInfoUnsupported(
+            EntryF, "BuiltIn PointSize cannot be used in a Fragment entry "
+                    "point (VUID-PointSize-PointSize-04314)"));
         break;
       }
 
diff --git a/llvm/test/CodeGen/SPIRV/semantics/point-size.ll b/llvm/test/CodeGen/SPIRV/semantics/point-size.ll
index 8da4e7cb9da022..91193543fc7c0f 100644
--- a/llvm/test/CodeGen/SPIRV/semantics/point-size.ll
+++ b/llvm/test/CodeGen/SPIRV/semantics/point-size.ll
@@ -10,7 +10,7 @@
 ; 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)
+; ERROR: error: {{.*}}BuiltIn PointSize cannot be used in a Fragment entry point (VUID-PointSize-PointSize-04314)
 
 ; CHECK-DAG: OpEntryPoint Vertex
 ; CHECK-DAG: OpDecorate %[[#PSIZE:]] BuiltIn PointSize



More information about the cfe-commits mailing list