[clang] [llvm] [HLSL][SPIRV] Implement SV_IsFrontFace (PR #232071)
Jonathan Zakharov via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 4 08:50:47 PST 2026
https://github.com/jzakharovnv updated https://github.com/llvm/llvm-project/pull/232071
>From 8004477315936fd156cfe9cb961213b7fe012df8 Mon Sep 17 00:00:00 2001
From: Jonathan Zakharov <jzakharov at nvidia.com>
Date: Wed, 14 Oct 2026 12:54:57 -0700
Subject: [PATCH 1/5] [HLSL][SPIRV] Implement SV_IsFrontFace
---
.../clang/Basic/DiagnosticSemaKinds.td | 2 +-
clang/lib/CodeGen/CGHLSLRuntime.cpp | 17 +++++++++-
clang/lib/Sema/SemaHLSL.cpp | 9 +++++-
.../semantics/SV_IsFrontFace.ps.hlsl | 20 ++++++++++++
.../Semantics/isfrontface.output.hlsl | 8 +++++
.../SemaHLSL/Semantics/isfrontface.ps.hlsl | 31 +++++++++++++++++++
.../SemaHLSL/Semantics/isfrontface.vs.hlsl | 7 +++++
7 files changed, 91 insertions(+), 3 deletions(-)
create mode 100644 clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.ps.hlsl
create mode 100644 clang/test/SemaHLSL/Semantics/isfrontface.output.hlsl
create mode 100644 clang/test/SemaHLSL/Semantics/isfrontface.ps.hlsl
create mode 100644 clang/test/SemaHLSL/Semantics/isfrontface.vs.hlsl
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index a2fc0206c445be..21dee2cdbe3a1d 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -13833,7 +13833,7 @@ def err_hlsl_semantic_invalid_type
"%select{scalar|scalar or vector of up to %2 components|vector of %2 components}1 of "
"%select{16 or 32 bit integer|32 bit integer|"
"16 or 32 bit floating-point|16 or 32 bit unsigned integer|"
- "32 bit unsigned integer}3 type (was %4)">;
+ "32 bit unsigned integer|bool}3 type (was %4)">;
def err_hlsl_init_priority_unsupported : Error<
"initializer priorities are not supported in HLSL">;
def err_hlsl_semantic_index_overlap : Error<"semantic index overlap %0">;
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index e65ada1ee8100e..1f4edf133c64ac 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -1185,8 +1185,10 @@ static llvm::Value *createSPIRVInputLoad(
llvm::hlsl::InterpolationMode InterpMode) {
// SPIR-V integer built-ins are 32-bit, so a narrower integer declaration
// loads the 32-bit built-in and truncates it. Other types are loaded as is.
+ // i1 is excluded: bool built-ins such as FrontFacing are OpTypeBool.
bool Truncate = Decoration == llvm::hlsl::SPIRVInterfaceDecoration::BuiltIn &&
- Ty->isIntegerTy() && Ty->getIntegerBitWidth() < 32;
+ Ty->isIntegerTy() && Ty->getIntegerBitWidth() > 1 &&
+ Ty->getIntegerBitWidth() < 32;
llvm::Type *LoadTy = Truncate ? B.getInt32Ty() : Ty;
auto *GV = new llvm::GlobalVariable(
M, LoadTy, /* isConstant= */ true, llvm::GlobalValue::ExternalLinkage,
@@ -1675,6 +1677,19 @@ llvm::Value *CGHLSLRuntime::emitSystemSemanticLoad(
return emitDXILUserSemanticLoad(B, Type, Decl, Semantic, Index,
Signature);
break;
+ case llvm::dxbc::PSV::SemanticKind::IsFrontFace:
+ assert(Stage == llvm::Triple::Pixel &&
+ "SV_IsFrontFace is in an unavailable stage and should have been "
+ "diagnosed by Sema");
+ if (CGM.getTarget().getTriple().isSPIRV())
+ return createSPIRVInputLoad(B, CGM.getModule(), Type,
+ Semantic->getAttrName()->getName(),
+ llvm::hlsl::SPIRVInterfaceDecoration::BuiltIn,
+ /* FrontFacing */ 17, InterpMode);
+ if (CGM.getTarget().getTriple().isDXIL())
+ return emitDXILUserSemanticLoad(B, Type, Decl, Semantic, Index,
+ Signature);
+ break;
case llvm::dxbc::PSV::SemanticKind::ShadingRate:
assert(llvm::is_contained({llvm::Triple::Hull, llvm::Triple::Domain,
llvm::Triple::Geometry, llvm::Triple::Pixel},
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 746c45662030a1..14efb8a48150a4 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -1007,6 +1007,8 @@ static bool isVkPipelineBuiltin(const ASTContext &AstContext, FunctionDecl *FD,
return true;
case SemanticKind::InstanceID:
return ST == llvm::Triple::Vertex && IsInput;
+ case SemanticKind::IsFrontFace:
+ return ST == llvm::Triple::Pixel && IsInput;
case SemanticKind::ShadingRate:
// Lowered to:
// - ShadingRateKHR built-in for fragment input.
@@ -1464,6 +1466,12 @@ void SemaHLSL::diagnoseSemanticType(const Decl *D,
<< /* 16 or 32 bit unsigned integer / 32 bit unsigned integer */
(IsSPIRV ? 4 : 3) << DeclaredTy;
return;
+ case SemanticKind::IsFrontFace:
+ if (!T->isBooleanType())
+ Diag(A->getLoc(), diag::err_hlsl_semantic_invalid_type)
+ << A->getAttrName() << /* scalar */ 0 << 1 << /* bool */ 5
+ << DeclaredTy;
+ return;
default:
// Other semantics only have the general signature type restrictions.
break;
@@ -2267,7 +2275,6 @@ void SemaHLSL::handleSemanticAttr(Decl *D, const ParsedAttr &AL) {
case SemanticKind::OutputControlPointID:
case SemanticKind::DomainLocation:
case SemanticKind::GSInstanceID:
- case SemanticKind::IsFrontFace:
case SemanticKind::Coverage:
case SemanticKind::InnerCoverage:
case SemanticKind::Depth:
diff --git a/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.ps.hlsl b/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.ps.hlsl
new file mode 100644
index 00000000000000..73e989601cb251
--- /dev/null
+++ b/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.ps.hlsl
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s --check-prefix=CHECK-SPIRV
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s --check-prefix=CHECK-DXIL
+
+// CHECK-SPIRV: @SV_IsFrontFace = external hidden thread_local addrspace(7) externally_initialized constant i1, !spirv.Decorations ![[#MD_0:]]
+
+// CHECK: define void @main() {{.*}} {
+float4 main(bool ff : SV_IsFrontFace) : SV_Target {
+ // CHECK-SPIRV: %[[FF:.*]] = load i1, ptr addrspace(7) @SV_IsFrontFace
+ // CHECK-SPIRV: %[[R:.*]] = call spir_func <4 x float> @_Z4mainb(i1 %[[FF]])
+
+ // CHECK-DXIL: %[[INPUT:.*]] = call i32 @llvm.dx.load.input.i32(i32 0, i32 0, i8 0, i32 poison)
+ // CHECK-DXIL: %[[BOOL:.*]] = icmp ne i32 %[[INPUT]], 0
+ // CHECK-DXIL: %[[R:.*]] = call <4 x float> @_Z4mainb(i1 %[[BOOL]])
+ return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
+}
+
+// Two decorations in one node: BuiltIn FrontFacing (17) and Flat (14).
+// CHECK-SPIRV-DAG: ![[#MD_0]] = !{![[#MD_1:]], ![[#MD_2:]]}
+// CHECK-SPIRV-DAG: ![[#MD_1]] = !{i32 11, i32 17}
+// CHECK-SPIRV-DAG: ![[#MD_2]] = !{i32 14}
diff --git a/clang/test/SemaHLSL/Semantics/isfrontface.output.hlsl b/clang/test/SemaHLSL/Semantics/isfrontface.output.hlsl
new file mode 100644
index 00000000000000..91379e44e03512
--- /dev/null
+++ b/clang/test/SemaHLSL/Semantics/isfrontface.output.hlsl
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-pixel -finclude-default-header -x hlsl -verify -o - %s
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-pixel -finclude-default-header -x hlsl -verify -o - %s
+
+float4 main(out bool ff : SV_IsFrontFace) : SV_Target {
+// expected-error at -1 {{semantic 'SV_IsFrontFace' is not supported in pixel shader outputs}}
+ ff = true; // Written, so the parameter is used.
+ return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
+}
diff --git a/clang/test/SemaHLSL/Semantics/isfrontface.ps.hlsl b/clang/test/SemaHLSL/Semantics/isfrontface.ps.hlsl
new file mode 100644
index 00000000000000..b2e92ff4938060
--- /dev/null
+++ b/clang/test/SemaHLSL/Semantics/isfrontface.ps.hlsl
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -finclude-default-header -x hlsl -verify -o - %s
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -finclude-default-header -x hlsl -verify -o - %s
+
+[shader("pixel")]
+float4 bad_uint(uint ff : SV_IsFrontFace) : SV_Target {
+// expected-error at -1 {{semantic 'SV_IsFrontFace' must be a scalar of bool type (was 'uint' (aka 'unsigned int'))}}
+ return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
+}
+
+[shader("pixel")]
+float4 bad_float(float ff : SV_IsFrontFace) : SV_Target {
+// expected-error at -1 {{semantic 'SV_IsFrontFace' must be a scalar of bool type (was 'float')}}
+ return (ff.x > 0.f) ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
+}
+
+[shader("pixel")]
+float4 bad_vec(bool3 ff : SV_IsFrontFace) : SV_Target {
+// expected-error at -1 {{semantic 'SV_IsFrontFace' must be a scalar of bool type (was 'bool3' (aka 'vector<bool, 3>'))}}
+ return ff.x ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
+}
+
+[shader("pixel")]
+float4 bad_index(bool ff : SV_IsFrontFace1) : SV_Target {
+// expected-error at -1 {{semantic 'SV_IsFrontFace' does not allow indexing}}
+ return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
+}
+
+[shader("pixel")]
+float4 ok(bool ff : SV_IsFrontFace) : SV_Target {
+ return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
+}
diff --git a/clang/test/SemaHLSL/Semantics/isfrontface.vs.hlsl b/clang/test/SemaHLSL/Semantics/isfrontface.vs.hlsl
new file mode 100644
index 00000000000000..d3474838dc6040
--- /dev/null
+++ b/clang/test/SemaHLSL/Semantics/isfrontface.vs.hlsl
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-vertex -finclude-default-header -x hlsl -verify -o - %s
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-vertex -finclude-default-header -x hlsl -verify -o - %s
+
+float4 main(bool ff : SV_IsFrontFace) : SV_Position {
+// expected-error at -1 {{semantic 'SV_IsFrontFace' is not supported in vertex shader inputs}}
+ return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
+}
>From 1ed016fe8be2be30eae33d4863697a7f6773581e Mon Sep 17 00:00:00 2001
From: Jonathan Zakharov <jzakharov at nvidia.com>
Date: Wed, 14 Oct 2026 14:21:43 -0700
Subject: [PATCH 2/5] fix codegen test
---
clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.ps.hlsl | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.ps.hlsl b/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.ps.hlsl
index 73e989601cb251..7065fa5ab50ace 100644
--- a/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.ps.hlsl
+++ b/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.ps.hlsl
@@ -3,7 +3,8 @@
// CHECK-SPIRV: @SV_IsFrontFace = external hidden thread_local addrspace(7) externally_initialized constant i1, !spirv.Decorations ![[#MD_0:]]
-// CHECK: define void @main() {{.*}} {
+// CHECK-SPIRV: define void @main() {{.*}} {
+// CHECK-DXIL: define void @main() {{.*}} {
float4 main(bool ff : SV_IsFrontFace) : SV_Target {
// CHECK-SPIRV: %[[FF:.*]] = load i1, ptr addrspace(7) @SV_IsFrontFace
// CHECK-SPIRV: %[[R:.*]] = call spir_func <4 x float> @_Z4mainb(i1 %[[FF]])
>From 0ac733695604427bfe635e9a9591ce2dead0b4f4 Mon Sep 17 00:00:00 2001
From: Jonathan Zakharov <jzakharov at nvidia.com>
Date: Fri, 16 Oct 2026 14:13:48 -0700
Subject: [PATCH 3/5] review: fixed check divergence
---
.../clang/Basic/DiagnosticSemaKinds.td | 3 ++-
clang/lib/CodeGen/CGHLSLRuntime.cpp | 17 ++++++++++-----
clang/lib/Sema/SemaHLSL.cpp | 9 +++++---
.../semantics/SV_IsFrontFace.uint.ps.hlsl | 21 +++++++++++++++++++
.../SemaHLSL/Semantics/isfrontface.ps.hlsl | 15 +++++++++----
5 files changed, 52 insertions(+), 13 deletions(-)
create mode 100644 clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.uint.ps.hlsl
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 21dee2cdbe3a1d..6add80c9473ba0 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -13833,7 +13833,8 @@ def err_hlsl_semantic_invalid_type
"%select{scalar|scalar or vector of up to %2 components|vector of %2 components}1 of "
"%select{16 or 32 bit integer|32 bit integer|"
"16 or 32 bit floating-point|16 or 32 bit unsigned integer|"
- "32 bit unsigned integer|bool}3 type (was %4)">;
+ "32 bit unsigned integer|bool or 32 bit unsigned integer}3 type "
+ "(was %4)">;
def err_hlsl_init_priority_unsupported : Error<
"initializer priorities are not supported in HLSL">;
def err_hlsl_semantic_index_overlap : Error<"semantic index overlap %0">;
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 1f4edf133c64ac..e921b454a8f08b 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -1681,11 +1681,18 @@ llvm::Value *CGHLSLRuntime::emitSystemSemanticLoad(
assert(Stage == llvm::Triple::Pixel &&
"SV_IsFrontFace is in an unavailable stage and should have been "
"diagnosed by Sema");
- if (CGM.getTarget().getTriple().isSPIRV())
- return createSPIRVInputLoad(B, CGM.getModule(), Type,
- Semantic->getAttrName()->getName(),
- llvm::hlsl::SPIRVInterfaceDecoration::BuiltIn,
- /* FrontFacing */ 17, InterpMode);
+ if (CGM.getTarget().getTriple().isSPIRV()) {
+ // The FrontFacing BuiltIn is always a bool in SPIR-V, whatever type the
+ // entry point declares. Load it as bool, then convert below.
+ llvm::Type *BoolTy = B.getInt1Ty();
+ // Integer pixel inputs cannot be interpolated and must be Flat.
+ llvm::Value *FF = createSPIRVInputLoad(
+ B, CGM.getModule(), BoolTy, Semantic->getAttrName()->getName(),
+ llvm::hlsl::SPIRVInterfaceDecoration::BuiltIn,
+ /* FrontFacing */ 17, llvm::hlsl::InterpolationMode::Constant);
+ // Convert to the declared parameter type (eg uint).
+ return Type == BoolTy ? FF : B.CreateZExt(FF, Type);
+ }
if (CGM.getTarget().getTriple().isDXIL())
return emitDXILUserSemanticLoad(B, Type, Decl, Semantic, Index,
Signature);
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 14efb8a48150a4..8011baa154fa8c 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -1467,10 +1467,13 @@ void SemaHLSL::diagnoseSemanticType(const Decl *D,
(IsSPIRV ? 4 : 3) << DeclaredTy;
return;
case SemanticKind::IsFrontFace:
- if (!T->isBooleanType())
+ // DXC accepts bool and uint. The SPIR-V FrontFacing variable is always
+ // bool, so CodeGen converts it to the declared type.
+ if (!T->isBooleanType() &&
+ !(T->isUnsignedIntegerType() && isIntElementOfWidth(Ctx, T, 32)))
Diag(A->getLoc(), diag::err_hlsl_semantic_invalid_type)
- << A->getAttrName() << /* scalar */ 0 << 1 << /* bool */ 5
- << DeclaredTy;
+ << A->getAttrName() << /* scalar */ 0 << 1
+ << /* bool or 32 bit unsigned integer */ 5 << DeclaredTy;
return;
default:
// Other semantics only have the general signature type restrictions.
diff --git a/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.uint.ps.hlsl b/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.uint.ps.hlsl
new file mode 100644
index 00000000000000..c23ff46b176708
--- /dev/null
+++ b/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.uint.ps.hlsl
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s --check-prefix=CHECK-SPIRV
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s --check-prefix=CHECK-DXIL
+
+// The SPIR-V FrontFacing variable stays bool even when the parameter is uint.
+// CHECK-SPIRV: @SV_IsFrontFace = external hidden thread_local addrspace(7) externally_initialized constant i1, !spirv.Decorations ![[#MD_0:]]
+
+// CHECK-SPIRV: define void @main() {{.*}} {
+// CHECK-DXIL: define void @main() {{.*}} {
+float4 main(uint ff : SV_IsFrontFace) : SV_Target {
+ // CHECK-SPIRV: %[[FF:.*]] = load i1, ptr addrspace(7) @SV_IsFrontFace
+ // CHECK-SPIRV: %[[U:.*]] = zext i1 %[[FF]] to i32
+ // CHECK-SPIRV: %[[R:.*]] = call spir_func <4 x float> @_Z4mainj(i32 %[[U]])
+
+ // CHECK-DXIL: %[[INPUT:.*]] = call i32 @llvm.dx.load.input.i32(i32 0, i32 0, i8 0, i32 poison)
+ // CHECK-DXIL: %[[R:.*]] = call <4 x float> @_Z4mainj(i32 %[[INPUT]])
+ return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
+}
+
+// CHECK-SPIRV-DAG: ![[#MD_0]] = !{![[#MD_1:]], ![[#MD_2:]]}
+// CHECK-SPIRV-DAG: ![[#MD_1]] = !{i32 11, i32 17}
+// CHECK-SPIRV-DAG: ![[#MD_2]] = !{i32 14}
diff --git a/clang/test/SemaHLSL/Semantics/isfrontface.ps.hlsl b/clang/test/SemaHLSL/Semantics/isfrontface.ps.hlsl
index b2e92ff4938060..698185f27e095e 100644
--- a/clang/test/SemaHLSL/Semantics/isfrontface.ps.hlsl
+++ b/clang/test/SemaHLSL/Semantics/isfrontface.ps.hlsl
@@ -1,21 +1,28 @@
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -finclude-default-header -x hlsl -verify -o - %s
// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -finclude-default-header -x hlsl -verify -o - %s
+// DXC accepts both bool and uint. The SPIR-V FrontFacing variable is always
+// bool, and CodeGen converts it to the declared type.
[shader("pixel")]
-float4 bad_uint(uint ff : SV_IsFrontFace) : SV_Target {
-// expected-error at -1 {{semantic 'SV_IsFrontFace' must be a scalar of bool type (was 'uint' (aka 'unsigned int'))}}
+float4 ok_uint(uint ff : SV_IsFrontFace) : SV_Target {
+ return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
+}
+
+[shader("pixel")]
+float4 bad_int(int ff : SV_IsFrontFace) : SV_Target {
+// expected-error at -1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit unsigned integer type (was 'int')}}
return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
}
[shader("pixel")]
float4 bad_float(float ff : SV_IsFrontFace) : SV_Target {
-// expected-error at -1 {{semantic 'SV_IsFrontFace' must be a scalar of bool type (was 'float')}}
+// expected-error at -1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit unsigned integer type (was 'float')}}
return (ff.x > 0.f) ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
}
[shader("pixel")]
float4 bad_vec(bool3 ff : SV_IsFrontFace) : SV_Target {
-// expected-error at -1 {{semantic 'SV_IsFrontFace' must be a scalar of bool type (was 'bool3' (aka 'vector<bool, 3>'))}}
+// expected-error at -1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit unsigned integer type (was 'bool3' (aka 'vector<bool, 3>'))}}
return ff.x ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
}
>From cebc15bdd1fd0849190ab1808cbd4352ba1ddca4 Mon Sep 17 00:00:00 2001
From: Jonathan Zakharov <jzakharov at nvidia.com>
Date: Fri, 16 Oct 2026 14:52:40 -0700
Subject: [PATCH 4/5] [HLSL] Accept uint for SV_IsFrontFace, convert from bool
FrontFacing in SPIR-V
---
clang/lib/Sema/SemaHLSL.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 8011baa154fa8c..e8873179baa8f7 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -1467,8 +1467,8 @@ void SemaHLSL::diagnoseSemanticType(const Decl *D,
(IsSPIRV ? 4 : 3) << DeclaredTy;
return;
case SemanticKind::IsFrontFace:
- // DXC accepts bool and uint. The SPIR-V FrontFacing variable is always
- // bool, so CodeGen converts it to the declared type.
+ // Accept bool or uint; SPIR-V FrontFacing BuiltIn is always a bool
+ // and CodeGen converts it to the declared type.
if (!T->isBooleanType() &&
!(T->isUnsignedIntegerType() && isIntElementOfWidth(Ctx, T, 32)))
Diag(A->getLoc(), diag::err_hlsl_semantic_invalid_type)
>From c0eaacc4718bc7aea35fd33f3968c087360cd06c Mon Sep 17 00:00:00 2001
From: Jonathan Zakharov <jzakharov at nvidia.com>
Date: Tue, 3 Nov 2026 16:29:15 -0800
Subject: [PATCH 5/5] review: signed int support and DXIL type validation
---
.../clang/Basic/DiagnosticSemaKinds.td | 2 +-
clang/lib/Sema/SemaHLSL.cpp | 8 +-
.../semantics/SV_IsFrontFace.int.ps.hlsl | 22 +++++
.../SemaHLSL/Semantics/isfrontface.ps.hlsl | 18 ++--
.../Target/DirectX/DXILSignatureAnalysis.cpp | 10 ++
...emantic-signatures-is-front-face-errors.ll | 92 +++++++++++++++++++
.../semantic-signatures-is-front-face.ll | 68 ++++++++++++++
7 files changed, 209 insertions(+), 11 deletions(-)
create mode 100644 clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.int.ps.hlsl
create mode 100644 llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-is-front-face-errors.ll
create mode 100644 llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-is-front-face.ll
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 6add80c9473ba0..9db8514d21c6ed 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -13833,7 +13833,7 @@ def err_hlsl_semantic_invalid_type
"%select{scalar|scalar or vector of up to %2 components|vector of %2 components}1 of "
"%select{16 or 32 bit integer|32 bit integer|"
"16 or 32 bit floating-point|16 or 32 bit unsigned integer|"
- "32 bit unsigned integer|bool or 32 bit unsigned integer}3 type "
+ "32 bit unsigned integer|bool or 32 bit integer}3 type "
"(was %4)">;
def err_hlsl_init_priority_unsupported : Error<
"initializer priorities are not supported in HLSL">;
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index e8873179baa8f7..bad52ba240b83a 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -1467,13 +1467,13 @@ void SemaHLSL::diagnoseSemanticType(const Decl *D,
(IsSPIRV ? 4 : 3) << DeclaredTy;
return;
case SemanticKind::IsFrontFace:
- // Accept bool or uint; SPIR-V FrontFacing BuiltIn is always a bool
+ // Accept bool or int; SPIR-V FrontFacing BuiltIn is always a bool
// and CodeGen converts it to the declared type.
- if (!T->isBooleanType() &&
- !(T->isUnsignedIntegerType() && isIntElementOfWidth(Ctx, T, 32)))
+ if (DeclaredTy != T || !T->isScalarType() ||
+ !(T->isBooleanType() || isIntElementOfWidth(Ctx, T, 32)))
Diag(A->getLoc(), diag::err_hlsl_semantic_invalid_type)
<< A->getAttrName() << /* scalar */ 0 << 1
- << /* bool or 32 bit unsigned integer */ 5 << DeclaredTy;
+ << /* bool or 32 bit integer */ 5 << DeclaredTy;
return;
default:
// Other semantics only have the general signature type restrictions.
diff --git a/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.int.ps.hlsl b/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.int.ps.hlsl
new file mode 100644
index 00000000000000..2d561461b69786
--- /dev/null
+++ b/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.int.ps.hlsl
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s --check-prefix=CHECK-SPIRV
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s --check-prefix=CHECK-DXIL
+
+// As in DXC, a signed 32-bit integer is accepted. The SPIR-V FrontFacing
+// variable stays bool even when the parameter is int.
+// CHECK-SPIRV: @SV_IsFrontFace = external hidden thread_local addrspace(7) externally_initialized constant i1, !spirv.Decorations ![[#MD_0:]]
+
+// CHECK-SPIRV: define void @main() {{.*}} {
+// CHECK-DXIL: define void @main() {{.*}} {
+float4 main(int ff : SV_IsFrontFace) : SV_Target {
+ // CHECK-SPIRV: %[[FF:.*]] = load i1, ptr addrspace(7) @SV_IsFrontFace
+ // CHECK-SPIRV: %[[I:.*]] = zext i1 %[[FF]] to i32
+ // CHECK-SPIRV: %[[R:.*]] = call spir_func <4 x float> @_Z4maini(i32 %[[I]])
+
+ // CHECK-DXIL: %[[INPUT:.*]] = call i32 @llvm.dx.load.input.i32(i32 0, i32 0, i8 0, i32 poison)
+ // CHECK-DXIL: %[[R:.*]] = call <4 x float> @_Z4maini(i32 %[[INPUT]])
+ return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
+}
+
+// CHECK-SPIRV-DAG: ![[#MD_0]] = !{![[#MD_1:]], ![[#MD_2:]]}
+// CHECK-SPIRV-DAG: ![[#MD_1]] = !{i32 11, i32 17}
+// CHECK-SPIRV-DAG: ![[#MD_2]] = !{i32 14}
diff --git a/clang/test/SemaHLSL/Semantics/isfrontface.ps.hlsl b/clang/test/SemaHLSL/Semantics/isfrontface.ps.hlsl
index 698185f27e095e..589e6535402b85 100644
--- a/clang/test/SemaHLSL/Semantics/isfrontface.ps.hlsl
+++ b/clang/test/SemaHLSL/Semantics/isfrontface.ps.hlsl
@@ -1,28 +1,34 @@
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -finclude-default-header -x hlsl -verify -o - %s
// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -finclude-default-header -x hlsl -verify -o - %s
-// DXC accepts both bool and uint. The SPIR-V FrontFacing variable is always
-// bool, and CodeGen converts it to the declared type.
+// DXC accepts a bool or a 32-bit integer, signed or unsigned. The SPIR-V
+// FrontFacing variable is always bool, and CodeGen converts it to the declared
+// type.
[shader("pixel")]
float4 ok_uint(uint ff : SV_IsFrontFace) : SV_Target {
return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
}
[shader("pixel")]
-float4 bad_int(int ff : SV_IsFrontFace) : SV_Target {
-// expected-error at -1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit unsigned integer type (was 'int')}}
+float4 ok_int(int ff : SV_IsFrontFace) : SV_Target {
+ return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
+}
+
+[shader("pixel")]
+float4 bad_uint64(uint64_t ff : SV_IsFrontFace) : SV_Target {
+// expected-error at -1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit integer type (was 'uint64_t' (aka 'unsigned long'))}}
return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
}
[shader("pixel")]
float4 bad_float(float ff : SV_IsFrontFace) : SV_Target {
-// expected-error at -1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit unsigned integer type (was 'float')}}
+// expected-error at -1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit integer type (was 'float')}}
return (ff.x > 0.f) ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
}
[shader("pixel")]
float4 bad_vec(bool3 ff : SV_IsFrontFace) : SV_Target {
-// expected-error at -1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit unsigned integer type (was 'bool3' (aka 'vector<bool, 3>'))}}
+// expected-error at -1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit integer type (was 'bool3' (aka 'vector<bool, 3>'))}}
return ff.x ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1);
}
diff --git a/llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp b/llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp
index 3fa9e72d5d84fa..90614e0476fd89 100644
--- a/llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp
+++ b/llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp
@@ -108,6 +108,12 @@ static Error diagnoseSemanticType(const SemanticSignatureElement &E) {
return signatureError(
"SV_PrimitiveID requires a scalar 16- or 32-bit unsigned integer");
break;
+ case dxbc::PSV::SemanticKind::IsFrontFace:
+ if (E.Rows != 1 || E.Cols != 1 ||
+ (E.CompType != ElementType::I32 && E.CompType != ElementType::U32))
+ return signatureError(
+ "SV_IsFrontFace requires a scalar 32-bit integer");
+ break;
case dxbc::PSV::SemanticKind::Barycentrics:
if (E.Rows != 1 || E.Cols != 3 ||
(E.CompType != ElementType::F16 && E.CompType != ElementType::F32))
@@ -143,6 +149,10 @@ static Error diagnoseSemanticIndex(const SemanticSignatureElement &E) {
if (E.SemanticIndices.front() != 0)
return signatureError("SV_PrimitiveID requires semantic index 0");
break;
+ case dxbc::PSV::SemanticKind::IsFrontFace:
+ if (E.SemanticIndices.front() != 0)
+ return signatureError("SV_IsFrontFace requires semantic index 0");
+ break;
default:
break;
}
diff --git a/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-is-front-face-errors.ll b/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-is-front-face-errors.ll
new file mode 100644
index 00000000000000..da0b236587efd6
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-is-front-face-errors.ll
@@ -0,0 +1,92 @@
+; RUN: split-file %s %t
+; RUN: not opt -disable-output -passes=dxil-translate-metadata %t/bool.ll 2>&1 | FileCheck %s --check-prefix=CHECK-BOOL
+; RUN: not opt -disable-output -passes=dxil-translate-metadata %t/float.ll 2>&1 | FileCheck %s --check-prefix=CHECK-FLOAT
+; RUN: not opt -disable-output -passes=dxil-translate-metadata %t/cols.ll 2>&1 | FileCheck %s --check-prefix=CHECK-COLS
+; RUN: not opt -disable-output -passes=dxil-translate-metadata %t/index.ll 2>&1 | FileCheck %s --check-prefix=CHECK-INDEX
+; RUN: not opt -disable-output -passes=dxil-translate-metadata %t/linear.ll 2>&1 | FileCheck %s --check-prefix=CHECK-LINEAR
+; RUN: not opt -disable-output -passes=dxil-translate-metadata %t/unallocated.ll 2>&1 | FileCheck %s --check-prefix=CHECK-UNALLOCATED
+; RUN: not opt -disable-output -passes=dxil-translate-metadata %t/vertex.ll 2>&1 | FileCheck %s --check-prefix=CHECK-VERTEX
+
+; Each case is a complete module with one invalid SV_IsFrontFace declaration.
+; As in the DXC validator, the only valid component types are I32 (4) and U32
+; (5). Clang carries a bool as U32.
+
+;--- bool.ll
+; The component type is 1 (I1). Clang never emits it for SV_IsFrontFace, and the
+; DXC validator rejects it.
+; CHECK-BOOL: SV_IsFrontFace requires a scalar 32-bit integer
+target triple = "dxil-pc-shadermodel6.0-pixel"
+define void @main() #0 { ret void }
+attributes #0 = { "hlsl.shader"="pixel" }
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, !1, null}
+!1 = !{!2}
+!2 = !{i32 0, !"SV_IsFrontFace", i32 1, i32 13, !3, i32 1, i32 1, i8 1, i32 0, i8 0, i8 0, i8 0, i32 0}
+!3 = !{i32 0}
+
+;--- float.ll
+; The component type is 9 (F32).
+; CHECK-FLOAT: SV_IsFrontFace requires a scalar 32-bit integer
+target triple = "dxil-pc-shadermodel6.0-pixel"
+define void @main() #0 { ret void }
+attributes #0 = { "hlsl.shader"="pixel" }
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, !1, null}
+!1 = !{!2}
+!2 = !{i32 0, !"SV_IsFrontFace", i32 9, i32 13, !3, i32 1, i32 1, i8 1, i32 0, i8 0, i8 0, i8 0, i32 0}
+!3 = !{i32 0}
+
+;--- cols.ll
+; CHECK-COLS: SV_IsFrontFace requires a scalar 32-bit integer
+target triple = "dxil-pc-shadermodel6.0-pixel"
+define void @main() #0 { ret void }
+attributes #0 = { "hlsl.shader"="pixel" }
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, !1, null}
+!1 = !{!2}
+!2 = !{i32 0, !"SV_IsFrontFace", i32 5, i32 13, !3, i32 1, i32 1, i8 2, i32 0, i8 0, i8 0, i8 0, i32 0}
+!3 = !{i32 0}
+
+;--- index.ll
+; CHECK-INDEX: SV_IsFrontFace requires semantic index 0
+target triple = "dxil-pc-shadermodel6.0-pixel"
+define void @main() #0 { ret void }
+attributes #0 = { "hlsl.shader"="pixel" }
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, !1, null}
+!1 = !{!2}
+!2 = !{i32 0, !"SV_IsFrontFace", i32 5, i32 13, !3, i32 1, i32 1, i8 1, i32 0, i8 0, i8 0, i8 0, i32 0}
+!3 = !{i32 1}
+
+;--- linear.ll
+; CHECK-LINEAR: integer pixel inputs require constant interpolation
+target triple = "dxil-pc-shadermodel6.0-pixel"
+define void @main() #0 { ret void }
+attributes #0 = { "hlsl.shader"="pixel" }
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, !1, null}
+!1 = !{!2}
+!2 = !{i32 0, !"SV_IsFrontFace", i32 5, i32 13, !3, i32 2, i32 1, i8 1, i32 0, i8 0, i8 0, i8 0, i32 0}
+!3 = !{i32 0}
+
+;--- unallocated.ll
+; CHECK-UNALLOCATED: signature element 0 ('SV_IsFrontFace') has no allocated location
+target triple = "dxil-pc-shadermodel6.0-pixel"
+define void @main() #0 { ret void }
+attributes #0 = { "hlsl.shader"="pixel" }
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, !1, null}
+!1 = !{!2}
+!2 = !{i32 0, !"SV_IsFrontFace", i32 5, i32 13, !3, i32 1, i32 1, i8 1, i32 -1, i8 -1, i8 0, i8 0, i32 0}
+!3 = !{i32 0}
+
+;--- vertex.ll
+; CHECK-VERTEX: invalid or unsupported semantic for this stage and signature category
+target triple = "dxil-pc-shadermodel6.0-vertex"
+define void @main() #0 { ret void }
+attributes #0 = { "hlsl.shader"="vertex" }
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, !1, null}
+!1 = !{!2}
+!2 = !{i32 0, !"SV_IsFrontFace", i32 5, i32 13, !3, i32 1, i32 1, i8 1, i32 0, i8 0, i8 0, i8 0, i32 0}
+!3 = !{i32 0}
diff --git a/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-is-front-face.ll b/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-is-front-face.ll
new file mode 100644
index 00000000000000..ce52f6abb4b6e2
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-is-front-face.ll
@@ -0,0 +1,68 @@
+; RUN: split-file %s %t
+; RUN: opt -disable-output -passes='print<dxil-signature>' %t/uint.ll 2>&1 | FileCheck %s --check-prefix=ANALYSIS
+; RUN: opt -disable-output -passes='print<dxil-signature>' %t/int.ll 2>&1 | FileCheck %s --check-prefix=ANALYSIS
+; RUN: opt -S -dxil-translate-metadata %t/uint.ll | FileCheck %s --check-prefix=MD-UINT
+; RUN: opt -S -dxil-translate-metadata %t/int.ll | FileCheck %s --check-prefix=MD-INT
+
+; SV_IsFrontFace is a system-generated pixel input. Like SV_PrimitiveID, it is
+; allocated like an arbitrary input, so it is packed at row 1 after A. As in
+; DXC, a bool or uint is carried as U32 (5) and an int as I32 (4). All of them
+; are read with the i32 load overload.
+
+;--- uint.ll
+target triple = "dxil-pc-shadermodel6.0-pixel"
+
+define void @main() #0 {
+ %a = call float @llvm.dx.load.input.f32(i32 0, i32 0, i8 0, i32 poison)
+ %ff = call i32 @llvm.dx.load.input.i32(i32 1, i32 0, i8 0, i32 poison)
+ %f = uitofp i32 %ff to float
+ %result = fadd float %a, %f
+ call void @llvm.dx.store.output.f32(i32 0, i32 0, i8 0, float %result)
+ ret void
+}
+attributes #0 = { "hlsl.shader"="pixel" }
+
+!dx.valver = !{!10}
+!10 = !{i32 1, i32 8}
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, !1, !2}
+!1 = !{!3, !4}
+!2 = !{!6}
+!3 = !{i32 0, !"A", i32 9, i32 0, !7, i32 2, i32 1, i8 1, i32 0, i8 0, i8 0, i8 0, i32 0}
+!4 = !{i32 1, !"SV_IsFrontFace", i32 5, i32 13, !7, i32 1, i32 1, i8 1, i32 1, i8 0, i8 0, i8 0, i32 0}
+!6 = !{i32 0, !"SV_Target", i32 9, i32 16, !7, i32 0, i32 1, i8 1, i32 0, i8 0, i8 0, i8 0, i32 0}
+!7 = !{i32 0}
+
+; MD-UINT-DAG: !{i32 0, !"A", i8 9, i8 0, !{{[0-9]+}}, i8 2, i32 1, i8 1, i32 0, i8 0, !{{[0-9]+}}}
+; MD-UINT-DAG: !{i32 1, !"SV_IsFrontFace", i8 5, i8 13, !{{[0-9]+}}, i8 1, i32 1, i8 1, i32 1, i8 0, !{{[0-9]+}}}
+
+;--- int.ll
+target triple = "dxil-pc-shadermodel6.0-pixel"
+
+define void @main() #0 {
+ %a = call float @llvm.dx.load.input.f32(i32 0, i32 0, i8 0, i32 poison)
+ %ff = call i32 @llvm.dx.load.input.i32(i32 1, i32 0, i8 0, i32 poison)
+ %f = sitofp i32 %ff to float
+ %result = fadd float %a, %f
+ call void @llvm.dx.store.output.f32(i32 0, i32 0, i8 0, float %result)
+ ret void
+}
+attributes #0 = { "hlsl.shader"="pixel" }
+
+!dx.valver = !{!10}
+!10 = !{i32 1, i32 8}
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, !1, !2}
+!1 = !{!3, !4}
+!2 = !{!6}
+!3 = !{i32 0, !"A", i32 9, i32 0, !7, i32 2, i32 1, i8 1, i32 0, i8 0, i8 0, i8 0, i32 0}
+!4 = !{i32 1, !"SV_IsFrontFace", i32 4, i32 13, !7, i32 1, i32 1, i8 1, i32 1, i8 0, i8 0, i8 0, i32 0}
+!6 = !{i32 0, !"SV_Target", i32 9, i32 16, !7, i32 0, i32 1, i8 1, i32 0, i8 0, i8 0, i8 0, i32 0}
+!7 = !{i32 0}
+
+; MD-INT-DAG: !{i32 0, !"A", i8 9, i8 0, !{{[0-9]+}}, i8 2, i32 1, i8 1, i32 0, i8 0, !{{[0-9]+}}}
+; MD-INT-DAG: !{i32 1, !"SV_IsFrontFace", i8 4, i8 13, !{{[0-9]+}}, i8 1, i32 1, i8 1, i32 1, i8 0, !{{[0-9]+}}}
+
+; ANALYSIS: Inputs: 2 elements, 2 vectors
+; ANALYSIS-NEXT: 0: A rows=1 cols=1 at 0:0 usage=1 dynamic=0
+; ANALYSIS-NEXT: 1: SV_IsFrontFace rows=1 cols=1 at 1:0 usage=1 dynamic=0
More information about the cfe-commits
mailing list