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 <[email protected]> Date: Wed, 14 Oct 2026 12:54:57 -0700 Subject: [PATCH 01/11] [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@-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@-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@-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@-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@-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@-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 <[email protected]> Date: Wed, 14 Oct 2026 14:21:43 -0700 Subject: [PATCH 02/11] 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 <[email protected]> Date: Fri, 16 Oct 2026 14:13:48 -0700 Subject: [PATCH 03/11] 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@-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@-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@-1 {{semantic 'SV_IsFrontFace' must be a scalar of bool type (was 'float')}} +// expected-error@-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@-1 {{semantic 'SV_IsFrontFace' must be a scalar of bool type (was 'bool3' (aka 'vector<bool, 3>'))}} +// expected-error@-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 <[email protected]> Date: Fri, 16 Oct 2026 14:52:40 -0700 Subject: [PATCH 04/11] [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 <[email protected]> Date: Tue, 3 Nov 2026 16:29:15 -0800 Subject: [PATCH 05/11] 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@-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@-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@-1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit unsigned integer type (was 'float')}} +// expected-error@-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@-1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit unsigned integer type (was 'bool3' (aka 'vector<bool, 3>'))}} +// expected-error@-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 >From dcd4d6f19bceb23b3d6398ed3246c333b5e39e3e Mon Sep 17 00:00:00 2001 From: Jonathan Zakharov <[email protected]> Date: Wed, 4 Nov 2026 08:57:01 -0800 Subject: [PATCH 06/11] clang-format --- llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp b/llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp index 90614e0476fd89..3f4e38d178a3c9 100644 --- a/llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp +++ b/llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp @@ -111,8 +111,7 @@ static Error diagnoseSemanticType(const SemanticSignatureElement &E) { 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"); + return signatureError("SV_IsFrontFace requires a scalar 32-bit integer"); break; case dxbc::PSV::SemanticKind::Barycentrics: if (E.Rows != 1 || E.Cols != 3 || >From 498230a9c92dc5421e6e82ab0f4f00961ecf2ca2 Mon Sep 17 00:00:00 2001 From: Jonathan Zakharov <[email protected]> Date: Mon, 9 Nov 2026 14:48:28 -0800 Subject: [PATCH 07/11] review: add DXIL container tests --- .../SemanticSignatures-is-front-face.ll | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 llvm/test/CodeGen/DirectX/ContainerData/SemanticSignatures-is-front-face.ll diff --git a/llvm/test/CodeGen/DirectX/ContainerData/SemanticSignatures-is-front-face.ll b/llvm/test/CodeGen/DirectX/ContainerData/SemanticSignatures-is-front-face.ll new file mode 100644 index 00000000000000..b7394fcca29b46 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/ContainerData/SemanticSignatures-is-front-face.ll @@ -0,0 +1,83 @@ +; RUN: split-file %s %t +; RUN: llc -filetype=obj %t/uint.ll -o %t/uint.dxbc +; RUN: obj2yaml %t/uint.dxbc | FileCheck %s --check-prefixes=CHECK,U32 +; RUN: llc -filetype=obj %t/int.ll -o %t/int.dxbc +; RUN: obj2yaml %t/int.dxbc | FileCheck %s --check-prefixes=CHECK,I32 + +; SV_IsFrontFace is written to the ISG1 and PSV0 parts as an allocated input +; element with the IsFrontFace system value. A bool or uint is carried as +; UInt32 and an int as SInt32. Packing is covered by +; Metadata/semantic-signatures-is-front-face.ll. + +;--- 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} + +;--- 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} + +; The test runs once per file. U32 and I32 differ only in the component type. +; CHECK: Name: ISG1 +; CHECK: Name: SV_IsFrontFace +; CHECK-NEXT: Index: 0 +; CHECK-NEXT: SystemValue: IsFrontFace +; U32-NEXT: CompType: UInt32 +; I32-NEXT: CompType: SInt32 +; CHECK-NEXT: Register: 1 +; CHECK-NEXT: Mask: 1 +; CHECK-NEXT: ExclusiveMask: 1 +; CHECK: Name: PSV0 +; CHECK: SampleFrequency: 0 +; CHECK: SigInputElements: +; The first Kind is element A, which comes before SV_IsFrontFace. +; CHECK: Kind: Arbitrary +; CHECK: Indices: [ 0 ] +; CHECK-NEXT: StartRow: 1 +; CHECK-NEXT: Cols: 1 +; CHECK-NEXT: StartCol: 0 +; CHECK-NEXT: Allocated: true +; CHECK-NEXT: Kind: IsFrontFace +; U32-NEXT: ComponentType: UInt32 +; I32-NEXT: ComponentType: SInt32 +; CHECK-NEXT: Interpolation: Constant >From f5f1d10d3913560809ff4e9c02e3f6a0e0ea8a7e Mon Sep 17 00:00:00 2001 From: Jonathan Zakharov <[email protected]> Date: Mon, 9 Nov 2026 14:48:31 -0800 Subject: [PATCH 08/11] review: merge CodeGen tests into one --- .../semantics/SV_IsFrontFace.int.ps.hlsl | 22 ---------- .../semantics/SV_IsFrontFace.ps.hlsl | 41 +++++++++++++++---- .../semantics/SV_IsFrontFace.struct.ps.hlsl | 25 +++++++++++ .../semantics/SV_IsFrontFace.uint.ps.hlsl | 21 ---------- 4 files changed, 57 insertions(+), 52 deletions(-) delete mode 100644 clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.int.ps.hlsl create mode 100644 clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.struct.ps.hlsl delete mode 100644 clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.uint.ps.hlsl diff --git a/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.int.ps.hlsl b/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.int.ps.hlsl deleted file mode 100644 index 2d561461b69786..00000000000000 --- a/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.int.ps.hlsl +++ /dev/null @@ -1,22 +0,0 @@ -// 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/CodeGenHLSL/semantics/SV_IsFrontFace.ps.hlsl b/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.ps.hlsl index 7065fa5ab50ace..ca224dfa847552 100644 --- a/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.ps.hlsl +++ b/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.ps.hlsl @@ -1,20 +1,43 @@ -// 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 +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -DTYPE=bool -o - %s | FileCheck -DMANGLE=b %s --check-prefixes=CHECK-DXIL,CHECK-DXIL-BOOL,CHECK-DXIL-U32 +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -DTYPE=uint -o - %s | FileCheck -DMANGLE=j %s --check-prefixes=CHECK-DXIL,CHECK-DXIL-INT,CHECK-DXIL-U32 +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -DTYPE=int -o - %s | FileCheck -DMANGLE=i %s --check-prefixes=CHECK-DXIL,CHECK-DXIL-INT,CHECK-DXIL-I32 +// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -DTYPE=bool -o - %s | FileCheck -DMANGLE=b %s --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-BOOL +// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -DTYPE=uint -o - %s | FileCheck -DMANGLE=j %s --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-INT +// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -DTYPE=int -o - %s | FileCheck -DMANGLE=i %s --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-INT +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -DTYPE=bool -DMODIFIER=nointerpolation -o - %s | FileCheck -DMANGLE=b %s --check-prefixes=CHECK-DXIL,CHECK-DXIL-BOOL,CHECK-DXIL-U32 +// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -DTYPE=bool -DMODIFIER=nointerpolation -o - %s | FileCheck -DMANGLE=b %s --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-BOOL + +// SV_IsFrontFace accepts a bool, a uint or an int. DXIL carries a +// bool or uint as U32 (5) and an int as I32 (4). The SPIR-V FrontFacing +// variable is always a bool, and an integer parameter is converted from it. +// The last two RUN lines add the one legal interpolation modifier, +// nointerpolation, which must leave the output unchanged. // 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(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-LABEL: define void @main() +// CHECK-SPIRV-LABEL: define void @main() +#ifndef MODIFIER +#define MODIFIER +#endif +float4 main(MODIFIER TYPE ff : SV_IsFrontFace) : SV_Target { // 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]]) + // CHECK-DXIL-BOOL: %[[BOOL:.*]] = icmp ne i32 %[[INPUT]], 0 + // CHECK-DXIL-BOOL: call <4 x float> @_Z4main[[MANGLE]](i1 %[[BOOL]]) + // CHECK-DXIL-INT: call <4 x float> @_Z4main[[MANGLE]](i32 %[[INPUT]]) + + // CHECK-SPIRV: %[[FF:.*]] = load i1, ptr addrspace(7) @SV_IsFrontFace + // CHECK-SPIRV-BOOL: call spir_func <4 x float> @_Z4main[[MANGLE]](i1 %[[FF]]) + // CHECK-SPIRV-INT: %[[EXT:.*]] = zext i1 %[[FF]] to i32 + // CHECK-SPIRV-INT: call spir_func <4 x float> @_Z4main[[MANGLE]](i32 %[[EXT]]) return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1); } +// CHECK-DXIL-U32-DAG: !{i32 0, !"SV_IsFrontFace", i32 5, i32 13, ![[#ZERO:]], i32 1, i32 1, i8 1, i32 0, i8 0, i8 0, i8 0, i32 0} +// CHECK-DXIL-I32-DAG: !{i32 0, !"SV_IsFrontFace", i32 4, i32 13, ![[#ZERO:]], i32 1, i32 1, i8 1, i32 0, i8 0, i8 0, i8 0, i32 0} +// CHECK-DXIL-DAG: ![[#ZERO]] = !{i32 0} + // 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} diff --git a/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.struct.ps.hlsl b/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.struct.ps.hlsl new file mode 100644 index 00000000000000..396c620b201713 --- /dev/null +++ b/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.struct.ps.hlsl @@ -0,0 +1,25 @@ +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -DTYPE=uint -o - %s | FileCheck %s --check-prefixes=CHECK-DXIL +// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -DTYPE=uint -o - %s | FileCheck %s --check-prefixes=CHECK-SPIRV +// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-pixel -x hlsl -emit-llvm -finclude-default-header -disable-llvm-passes -DTYPE=bool -o - %s | FileCheck %s --check-prefixes=CHECK-SPIRV + +// SV_IsFrontFace as a member of an entry point input struct. The struct member +// is stored as a 32-bit integer, so SPIR-V converts the bool FrontFacing +// variable to i32 for both a uint and a bool member. +// +// A bool member is only tested for SPIR-V. The DXIL CodeGen path asserts on any +// bool struct member that has a semantic, which is a separate bug. + +struct S { + float4 pos : SV_Position; + TYPE ff : SV_IsFrontFace; +}; + +// CHECK-SPIRV-LABEL: define void @main() +// CHECK-DXIL-LABEL: define void @main() +float4 main(S s) : SV_Target { + // CHECK-SPIRV: %[[FF:.*]] = load i1, ptr addrspace(7) @SV_IsFrontFace + // CHECK-SPIRV: %[[EXT:.*]] = zext i1 %[[FF]] to i32 + + // CHECK-DXIL: call i32 @llvm.dx.load.input.i32(i32 1, i32 0, i8 0, i32 poison) + return s.ff ? s.pos : float4(0, 0, 0, 0); +} diff --git a/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.uint.ps.hlsl b/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.uint.ps.hlsl deleted file mode 100644 index c23ff46b176708..00000000000000 --- a/clang/test/CodeGenHLSL/semantics/SV_IsFrontFace.uint.ps.hlsl +++ /dev/null @@ -1,21 +0,0 @@ -// 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} >From 7117e4f2baa14be11ba770d6a96ac156609939cf Mon Sep 17 00:00:00 2001 From: Jonathan Zakharov <[email protected]> Date: Mon, 9 Nov 2026 14:48:31 -0800 Subject: [PATCH 09/11] review: split Sema tests by purpose --- .../Semantics/isfrontface.output.hlsl | 8 --- .../Semantics/isfrontface.ps.errors.hlsl | 52 ++++++++++++++++++ .../SemaHLSL/Semantics/isfrontface.ps.hlsl | 55 ++++++++----------- .../Semantics/isfrontface.stages.hlsl | 31 +++++++++++ .../SemaHLSL/Semantics/isfrontface.vs.hlsl | 7 --- 5 files changed, 105 insertions(+), 48 deletions(-) delete mode 100644 clang/test/SemaHLSL/Semantics/isfrontface.output.hlsl create mode 100644 clang/test/SemaHLSL/Semantics/isfrontface.ps.errors.hlsl create mode 100644 clang/test/SemaHLSL/Semantics/isfrontface.stages.hlsl delete mode 100644 clang/test/SemaHLSL/Semantics/isfrontface.vs.hlsl diff --git a/clang/test/SemaHLSL/Semantics/isfrontface.output.hlsl b/clang/test/SemaHLSL/Semantics/isfrontface.output.hlsl deleted file mode 100644 index 91379e44e03512..00000000000000 --- a/clang/test/SemaHLSL/Semantics/isfrontface.output.hlsl +++ /dev/null @@ -1,8 +0,0 @@ -// 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@-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.errors.hlsl b/clang/test/SemaHLSL/Semantics/isfrontface.ps.errors.hlsl new file mode 100644 index 00000000000000..9f041bb1dbe1ec --- /dev/null +++ b/clang/test/SemaHLSL/Semantics/isfrontface.ps.errors.hlsl @@ -0,0 +1,52 @@ +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -fnative-half-type -fnative-int16-type -finclude-default-header -fsyntax-only -verify %s +// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -fnative-half-type -fnative-int16-type -finclude-default-header -fsyntax-only -verify %s + +// Invalid type, index and interpolation uses of SV_IsFrontFace. The valid +// cases are covered by isfrontface.ps.hlsl, and the shader stage and IO +// direction by isfrontface.stages.hlsl. + +[shader("pixel")] +void bad_float(float s : SV_IsFrontFace) {} +// expected-error@-1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit integer type (was 'float')}} +[shader("pixel")] +void bad_int64(uint64_t s : SV_IsFrontFace) {} +// expected-error@-1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit integer type (was 'uint64_t' (aka 'unsigned long')}} +[shader("pixel")] +void bad_vector(bool3 s : SV_IsFrontFace) {} +// expected-error@-1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit integer type (was 'bool3' (aka 'vector<bool, 3>'))}} +[shader("pixel")] +void bad_vector1(bool1 s : SV_IsFrontFace) {} +// expected-error@-1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit integer type (was 'bool1' (aka 'vector<bool, 1>'))}} +[shader("pixel")] +void bad_matrix(bool2x2 s : SV_IsFrontFace) {} +// expected-error@-1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit integer type (was 'bool2x2' (aka 'matrix<bool, 2, 2>'))}} +// Only 32-bit integers are allowed, so 16-bit types are rejected. +[shader("pixel")] +void bad_uint16(uint16_t s : SV_IsFrontFace) {} +// expected-error@-1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit integer type (was 'uint16_t' (aka 'unsigned short')}} +[shader("pixel")] +void bad_int16(int16_t s : SV_IsFrontFace) {} +// expected-error@-1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit integer type (was 'int16_t' (aka 'short')}} +[shader("pixel")] +void bad_half(half s : SV_IsFrontFace) {} +// expected-error@-1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit integer type (was 'half')}} +[shader("pixel")] +void bad_array(bool s[1] : SV_IsFrontFace) {} +// expected-error@-1 {{semantic 'SV_IsFrontFace' must be a scalar of bool or 32 bit integer type (was 'bool[1]'}} + +[shader("pixel")] +void bad_index(bool s : SV_IsFrontFace1) {} +// expected-error@-1 {{semantic 'SV_IsFrontFace' does not allow indexing}} + +[shader("pixel")] +void same_index(bool s : SV_IsFrontFace, // expected-note {{previous use is here}} + bool t : SV_IsFrontFace0) {} // expected-note {{'t' declared here}} +// expected-error@-1 {{semantic index overlap SV_IsFrontFace0}} + +// Integer and bool inputs can only use nointerpolation. +[shader("pixel")] +void bad_interpolation_uint(sample uint s : SV_IsFrontFace) {} +// expected-error@-1 {{interpolation modifiers other than 'nointerpolation' cannot be used with type 'uint' (aka 'unsigned int')}} +[shader("pixel")] +void bad_interpolation_bool(linear bool s : SV_IsFrontFace) {} +// expected-error@-1 {{interpolation modifiers other than 'nointerpolation' cannot be used with type 'bool'}} diff --git a/clang/test/SemaHLSL/Semantics/isfrontface.ps.hlsl b/clang/test/SemaHLSL/Semantics/isfrontface.ps.hlsl index 589e6535402b85..c068891fc6f1f2 100644 --- a/clang/test/SemaHLSL/Semantics/isfrontface.ps.hlsl +++ b/clang/test/SemaHLSL/Semantics/isfrontface.ps.hlsl @@ -1,44 +1,33 @@ -// 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 +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -finclude-default-header -fsyntax-only -verify %s +// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -finclude-default-header -fsyntax-only -verify %s -// DXC accepts a bool or a 32-bit integer, signed or unsigned. The SPIR-V +// Valid type, index and interpolation uses of SV_IsFrontFace. The invalid +// cases are covered by isfrontface.ps.errors.hlsl, and the shader stage and IO +// direction by isfrontface.stages.hlsl. + +// expected-no-diagnostics + +// A bool or a 32-bit integer, signed or unsigned, is accepted. 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); -} - +void valid_bool(bool s : SV_IsFrontFace) {} [shader("pixel")] -float4 ok_int(int ff : SV_IsFrontFace) : SV_Target { - return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1); -} - +void valid_uint(uint s : SV_IsFrontFace) {} [shader("pixel")] -float4 bad_uint64(uint64_t ff : SV_IsFrontFace) : SV_Target { -// expected-error@-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); -} - +void valid_int(int s : SV_IsFrontFace) {} +// The semantic name is case insensitive, and index 0 is the same as no index. [shader("pixel")] -float4 bad_float(float ff : SV_IsFrontFace) : SV_Target { -// expected-error@-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); -} - +void valid_lowercase(bool s : sv_isfrontface) {} [shader("pixel")] -float4 bad_vec(bool3 ff : SV_IsFrontFace) : SV_Target { -// expected-error@-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); -} - +void valid_index(bool s : SV_IsFrontFace0) {} +[shader("pixel")] +void valid_flat_bool(nointerpolation bool s : SV_IsFrontFace) {} [shader("pixel")] -float4 bad_index(bool ff : SV_IsFrontFace1) : SV_Target { -// expected-error@-1 {{semantic 'SV_IsFrontFace' does not allow indexing}} - return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1); -} +void valid_flat_uint(nointerpolation uint s : SV_IsFrontFace) {} +struct Input { + bool s : SV_IsFrontFace; +}; [shader("pixel")] -float4 ok(bool ff : SV_IsFrontFace) : SV_Target { - return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1); -} +void valid_struct(Input i) {} diff --git a/clang/test/SemaHLSL/Semantics/isfrontface.stages.hlsl b/clang/test/SemaHLSL/Semantics/isfrontface.stages.hlsl new file mode 100644 index 00000000000000..9400293da11daf --- /dev/null +++ b/clang/test/SemaHLSL/Semantics/isfrontface.stages.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 + +// Stage and IO direction checks for SV_IsFrontFace: only a pixel input. +// Type, index and interpolation checks are covered by isfrontface.ps.hlsl and +// isfrontface.ps.errors.hlsl. + +[shader("pixel")] +float4 ok_ps_in(bool s : SV_IsFrontFace) : SV_Target { + return s ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1); +} + +[shader("pixel")] +bool bad_ps_out() : SV_IsFrontFace { return false; } +// expected-error@-1 {{semantic 'SV_IsFrontFace' is not supported in pixel shader outputs}} + +[shader("pixel")] +float4 bad_ps_out_param(out bool s : SV_IsFrontFace) : SV_Target { +// expected-error@-1 {{semantic 'SV_IsFrontFace' is not supported in pixel shader outputs}} + s = true; + return s ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1); +} + +[shader("vertex")] +float4 bad_vs_in(bool s : SV_IsFrontFace) : SV_Position { return 0; } +// expected-error@-1 {{semantic 'SV_IsFrontFace' is not supported in vertex shader inputs}} + +[shader("compute")] +[numthreads(1, 1, 1)] +void bad_cs_in(bool s : SV_IsFrontFace) {} +// expected-error@-1 {{semantic 'SV_IsFrontFace' is not supported in compute shader inputs}} diff --git a/clang/test/SemaHLSL/Semantics/isfrontface.vs.hlsl b/clang/test/SemaHLSL/Semantics/isfrontface.vs.hlsl deleted file mode 100644 index d3474838dc6040..00000000000000 --- a/clang/test/SemaHLSL/Semantics/isfrontface.vs.hlsl +++ /dev/null @@ -1,7 +0,0 @@ -// 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@-1 {{semantic 'SV_IsFrontFace' is not supported in vertex shader inputs}} - return ff ? float4(1, 0, 0, 1) : float4(0, 0, 1, 1); -} >From 80d176364b5415371faab727751f109b0ed32904 Mon Sep 17 00:00:00 2001 From: Jonathan Zakharov <[email protected]> Date: Mon, 9 Nov 2026 14:48:32 -0800 Subject: [PATCH 10/11] review: add SPIR-V backend tests --- .../SPIRV/semantics/is-front-face.ps.ll | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 llvm/test/CodeGen/SPIRV/semantics/is-front-face.ps.ll diff --git a/llvm/test/CodeGen/SPIRV/semantics/is-front-face.ps.ll b/llvm/test/CodeGen/SPIRV/semantics/is-front-face.ps.ll new file mode 100644 index 00000000000000..32347b2fb2c1b7 --- /dev/null +++ b/llvm/test/CodeGen/SPIRV/semantics/is-front-face.ps.ll @@ -0,0 +1,77 @@ +; RUN: split-file %s %t +; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv-unknown-vulkan1.3-pixel %t/bool.ll -o - | FileCheck %t/bool.ll --check-prefix=BOOL --implicit-check-not=OpExtension +; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv-unknown-vulkan1.2-pixel %t/bool.ll -o - | FileCheck %t/bool.ll --check-prefix=BOOL --implicit-check-not=OpExtension +; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv-unknown-vulkan1.3-pixel %t/uint.ll -o - | FileCheck %t/uint.ll --check-prefix=UINT --implicit-check-not=OpExtension +; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-vulkan1.3-pixel %t/bool.ll -o - -filetype=obj | spirv-val --target-env vulkan1.3 %} +; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-vulkan1.3-pixel %t/uint.ll -o - -filetype=obj | spirv-val --target-env vulkan1.3 %} + +; SV_IsFrontFace is lowered to a flat load of the FrontFacing built-in. The +; built-in is always an OpTypeBool, so a bool parameter uses the loaded value +; directly, and a uint or int parameter converts it with a select between 1 and +; 0. FrontFacing needs no SPIR-V extension. + +;--- bool.ll +; BOOL-DAG: OpDecorate %[[#FF:]] BuiltIn FrontFacing +; BOOL-DAG: OpDecorate %[[#FF]] Flat +; The built-in is part of the entry point interface. +; BOOL-DAG: OpEntryPoint Fragment %[[#]] "main" {{.*}}%[[#FF]]{{( |$)}} +; BOOL-DAG: %[[#BOOL:]] = OpTypeBool +; BOOL-DAG: %[[#PTR:]] = OpTypePointer Input %[[#BOOL]] +; BOOL-DAG: %[[#FF]] = OpVariable %[[#PTR]] Input + +@SV_IsFrontFace = external hidden thread_local addrspace(7) externally_initialized constant i1, !spirv.Decorations !0 +@SV_Target0 = external hidden thread_local addrspace(8) global float, !spirv.Decorations !3 + +define void @main() #0 { +entry: +; BOOL: %[[#LOAD:]] = OpLoad %[[#BOOL]] %[[#FF]] +; BOOL: OpSelect %[[#]] %[[#LOAD]] %[[#]] %[[#]] + %0 = load i1, ptr addrspace(7) @SV_IsFrontFace, align 1 + %1 = select i1 %0, float 1.0, float 0.0 + store float %1, ptr addrspace(8) @SV_Target0, align 4 + ret void +} + +attributes #0 = { "hlsl.shader"="pixel" } + +!0 = !{!1, !2} +!1 = !{i32 11, i32 17} +!2 = !{i32 14} +!3 = !{!4} +!4 = !{i32 30, i32 0} + +;--- uint.ll +; UINT-DAG: OpDecorate %[[#FF:]] BuiltIn FrontFacing +; UINT-DAG: OpDecorate %[[#FF]] Flat +; The built-in is part of the entry point interface. +; UINT-DAG: OpEntryPoint Fragment %[[#]] "main" {{.*}}%[[#FF]]{{( |$)}} +; UINT-DAG: %[[#BOOL:]] = OpTypeBool +; UINT-DAG: %[[#INT:]] = OpTypeInt 32 0 +; UINT-DAG: %[[#ONE:]] = OpConstant %[[#INT]] 1 +; UINT-DAG: %[[#ZERO:]] = {{OpConstantNull|OpConstant}} %[[#INT]]{{( 0)?$}} +; UINT-DAG: %[[#PTR:]] = OpTypePointer Input %[[#BOOL]] +; UINT-DAG: %[[#FF]] = OpVariable %[[#PTR]] Input + +@SV_IsFrontFace = external hidden thread_local addrspace(7) externally_initialized constant i1, !spirv.Decorations !0 +@SV_Target0 = external hidden thread_local addrspace(8) global float, !spirv.Decorations !3 + +define void @main() #0 { +entry: +; The variable stays bool. The conversion to uint is a zero extension, which is +; 1 when the face is front and 0 otherwise. +; UINT: %[[#LOAD:]] = OpLoad %[[#BOOL]] %[[#FF]] +; UINT: OpSelect %[[#INT]] %[[#LOAD]] %[[#ONE]] %[[#ZERO]] + %0 = load i1, ptr addrspace(7) @SV_IsFrontFace, align 1 + %1 = zext i1 %0 to i32 + %2 = uitofp i32 %1 to float + store float %2, ptr addrspace(8) @SV_Target0, align 4 + ret void +} + +attributes #0 = { "hlsl.shader"="pixel" } + +!0 = !{!1, !2} +!1 = !{i32 11, i32 17} +!2 = !{i32 14} +!3 = !{!4} +!4 = !{i32 30, i32 0} >From bda356d3086b81de684dcb3d4b7e58d085f9f3d7 Mon Sep 17 00:00:00 2001 From: Jonathan Zakharov <[email protected]> Date: Mon, 9 Nov 2026 14:48:32 -0800 Subject: [PATCH 11/11] review: tidy DXIL signature tests --- ...emantic-signatures-is-front-face-errors.ll | 21 +++++++++++++++---- .../semantic-signatures-is-front-face.ll | 4 ++-- 2 files changed, 19 insertions(+), 6 deletions(-) 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 index da0b236587efd6..36fb91892196b7 100644 --- 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 @@ -6,14 +6,15 @@ ; 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 +; RUN: not opt -disable-output -passes=dxil-translate-metadata %t/output.ll 2>&1 | FileCheck %s --check-prefix=CHECK-OUTPUT ; 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. +; 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. +; The component type is 1 (I1). Clang never emits it for SV_IsFrontFace, and it +; is not a valid type for this semantic. ; CHECK-BOOL: SV_IsFrontFace requires a scalar 32-bit integer target triple = "dxil-pc-shadermodel6.0-pixel" define void @main() #0 { ret void } @@ -90,3 +91,15 @@ attributes #0 = { "hlsl.shader"="vertex" } !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} + +;--- output.ll +; SV_IsFrontFace is only a pixel input, so a pixel output is rejected. +; CHECK-OUTPUT: invalid or unsupported semantic for this stage and signature category +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, null, !1} +!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} \ No newline at end of file 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 index ce52f6abb4b6e2..210a05e0dd97fc 100644 --- a/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-is-front-face.ll +++ b/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-is-front-face.ll @@ -5,8 +5,8 @@ ; 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 +; allocated like an arbitrary input, so it is packed at row 1 after A. 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 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
