[llvm] [SPIR-V] Add Fragment execution model (PR #141787)

Nathan Gauër via llvm-commits llvm-commits at lists.llvm.org
Wed May 28 08:06:46 PDT 2025


https://github.com/Keenuts created https://github.com/llvm/llvm-project/pull/141787

This commits allows the fragment execution model to be set using the hlsl.shader attribute.

Fixes #136962

>From 9316e41fa30a70feb1154b0b676d7578aefcab75 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= <brioche at google.com>
Date: Wed, 28 May 2025 16:51:08 +0200
Subject: [PATCH] [SPIR-V] Add Fragment execution model

This commits allows the fragment execution model to be set
using the hlsl.shader attribute.

Fixes #136962
---
 llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp       | 15 ++++++++++++++-
 llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp     |  2 ++
 llvm/test/CodeGen/SPIRV/ExecutionMode_Fragment.ll | 12 ++++++++++++
 3 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CodeGen/SPIRV/ExecutionMode_Fragment.ll

diff --git a/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp b/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
index 5991a9af6364d..3a02f2764ad40 100644
--- a/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVCallLowering.cpp
@@ -279,6 +279,8 @@ getExecutionModel(const SPIRVSubtarget &STI, const Function &F) {
   const auto value = attribute.getValueAsString();
   if (value == "compute")
     return SPIRV::ExecutionModel::GLCompute;
+  if (value == "pixel")
+    return SPIRV::ExecutionModel::Fragment;
 
   report_fatal_error("This HLSL entry point is not supported by this backend.");
 }
@@ -439,10 +441,21 @@ bool SPIRVCallLowering::lowerFormalArguments(MachineIRBuilder &MIRBuilder,
 
   // Handle entry points and function linkage.
   if (isEntryPoint(F)) {
+    SPIRV::ExecutionModel::ExecutionModel ExecutionModel =
+        getExecutionModel(*ST, F);
     auto MIB = MIRBuilder.buildInstr(SPIRV::OpEntryPoint)
-                   .addImm(static_cast<uint32_t>(getExecutionModel(*ST, F)))
+                   .addImm(static_cast<uint32_t>(ExecutionModel))
                    .addUse(FuncVReg);
     addStringImm(F.getName(), MIB);
+
+    if (ExecutionModel == SPIRV::ExecutionModel::Fragment) {
+      // SPIR-V common validation: Fragment requires OriginUpperLeft or
+      // OriginLowerLeft VUID-StandaloneSpirv-OriginLowerLeft-04653: Fragment
+      // must declare OriginUpperLeft.
+      MIRBuilder.buildInstr(SPIRV::OpExecutionMode)
+          .addUse(FuncVReg)
+          .addImm(static_cast<uint32_t>(SPIRV::ExecutionMode::OriginUpperLeft));
+    }
   } else if (F.getLinkage() != GlobalValue::InternalLinkage &&
              F.getLinkage() != GlobalValue::PrivateLinkage) {
     SPIRV::LinkageType::LinkageType LnkTy =
diff --git a/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp b/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
index 2fdd54fdfc390..1de0784e16255 100644
--- a/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
@@ -595,6 +595,8 @@ void SPIRVModuleAnalysis::processOtherInstrs(const Module &M) {
           collectOtherInstr(MI, MAI, SPIRV::MB_DebugNames, IS);
         } else if (OpCode == SPIRV::OpEntryPoint) {
           collectOtherInstr(MI, MAI, SPIRV::MB_EntryPoints, IS);
+        } else if (OpCode == SPIRV::OpExecutionMode) {
+          collectOtherInstr(MI, MAI, SPIRV::MB_EntryPoints, IS);
         } else if (TII->isAliasingInstr(MI)) {
           collectOtherInstr(MI, MAI, SPIRV::MB_AliasingInsts, IS);
         } else if (TII->isDecorationInstr(MI)) {
diff --git a/llvm/test/CodeGen/SPIRV/ExecutionMode_Fragment.ll b/llvm/test/CodeGen/SPIRV/ExecutionMode_Fragment.ll
new file mode 100644
index 0000000000000..3374274bb3445
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/ExecutionMode_Fragment.ll
@@ -0,0 +1,12 @@
+; RUN: llc -O0 -mtriple=spirv-unknown-vulkan1.3-pixel %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-vulkan1.3-pixel %s -o - -filetype=obj | spirv-val %}
+
+; CHECK-DAG: OpEntryPoint Fragment %[[#entry:]] "main"
+; CHECK-DAG: OpExecutionMode %[[#entry]] OriginUpperLeft
+
+define void @main() #1 {
+entry:
+  ret void
+}
+
+attributes #1 = { "hlsl.shader"="pixel" }



More information about the llvm-commits mailing list