https://github.com/hekota created https://github.com/llvm/llvm-project/pull/243638
Adds support for `bool IsDebuggingEnabled()` to Shader Model 6.10. The function is defined in `hlsl::dx` namespace that is available only for the DirectX target. It uses a new built-in function `__builtin_hlsl_isdebuggingenabled` which is translated to LLVM intrinsic `llvm.dx.isdebuggingenabled` and lowered to DXIL operation `dx.op.isDebuggingEnabled`. Fixes #231326 Fixes #231276 >From f33eafa5e718fb323880bfda373b418d64e20a7a Mon Sep 17 00:00:00 2001 From: Helena Kotas <[email protected]> Date: Tue, 17 Nov 2026 13:51:54 -0800 Subject: [PATCH] [HLSL][DirectX] Add IsDebuggingEnabled() Adds support for `bool IsDebuggingEnabled()` to Shader Model 6.10. The function is defined in `hlsl::dx` namespace that is available only for the DirectX target. It uses a new built-in function `__builtin_hlsl_isdebuggingenabled` which is translated to LLVM intrinsic `llvm.dx.isdebuggingenabled` and lowered to DXIL operation 'dx.op.isDebuggingEnabled'. --- clang/include/clang/Basic/Builtins.td | 6 +++++ clang/lib/CodeGen/CGHLSLBuiltins.cpp | 8 ++++++ clang/lib/Headers/hlsl/hlsl_debugging.h | 11 ++++++++ clang/lib/Sema/SemaHLSL.cpp | 8 ++++++ .../builtins/IsDebuggingEnabled.hlsl | 14 ++++++++++ .../IsDebuggingEnabled-availability.hlsl | 26 +++++++++++++++++++ llvm/include/llvm/IR/IntrinsicsDirectX.td | 3 +++ llvm/lib/Target/DirectX/DXIL.td | 11 +++++++- .../CodeGen/DirectX/isdebuggingenabled.ll | 18 +++++++++++++ 9 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 clang/test/CodeGenHLSL/builtins/IsDebuggingEnabled.hlsl create mode 100644 clang/test/SemaHLSL/BuiltIns/IsDebuggingEnabled-availability.hlsl create mode 100644 llvm/test/CodeGen/DirectX/isdebuggingenabled.ll diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td index 15693d2fe128e5..d05936d2ee7b4a 100644 --- a/clang/include/clang/Basic/Builtins.td +++ b/clang/include/clang/Basic/Builtins.td @@ -6060,6 +6060,12 @@ def HLSLDdyFine : LangBuiltin<"HLSL_LANG"> { let Prototype = "void(...)"; } +def HLSLIsDebuggingEnabled : LangBuiltin<"HLSL_LANG"> { + let Spellings = ["__builtin_hlsl_isdebuggingenabled"]; + let Attributes = [NoThrow, Const]; + let Prototype = "bool()"; +} + // Builtins for XRay. def XRayCustomEvent : Builtin { let Spellings = ["__xray_customevent"]; diff --git a/clang/lib/CodeGen/CGHLSLBuiltins.cpp b/clang/lib/CodeGen/CGHLSLBuiltins.cpp index 31bcfd78a5b879..ed9cd8af643120 100644 --- a/clang/lib/CodeGen/CGHLSLBuiltins.cpp +++ b/clang/lib/CodeGen/CGHLSLBuiltins.cpp @@ -15,6 +15,7 @@ #include "CodeGenFunction.h" #include "clang/AST/HLSLResource.h" #include "clang/AST/MatrixUtils.h" +#include "clang/Basic/DiagnosticSema.h" #include "llvm/ADT/STLExtras.h" #include "llvm/IR/MatrixBuilder.h" #include "llvm/Support/DXILABI.h" @@ -1936,6 +1937,13 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID, ArrayRef<Value *>{Op0}, nullptr, "hlsl.ddy.fine"); } + case Builtin::BI__builtin_hlsl_isdebuggingenabled: { + assert(CGM.getTarget().getTriple().isDXIL() && + "hlsl.isdebuggingenabled is only supported on DXIL"); + return Builder.CreateIntrinsic( + Builder.getInt1Ty(), Intrinsic::dx_isdebuggingenabled, + ArrayRef<Value *>{}, nullptr, "hlsl.isdebuggingenabled"); + } case Builtin::BI__builtin_get_spirv_spec_constant_bool: case Builtin::BI__builtin_get_spirv_spec_constant_short: case Builtin::BI__builtin_get_spirv_spec_constant_ushort: diff --git a/clang/lib/Headers/hlsl/hlsl_debugging.h b/clang/lib/Headers/hlsl/hlsl_debugging.h index 5ca0f57936e590..527370e52f6528 100644 --- a/clang/lib/Headers/hlsl/hlsl_debugging.h +++ b/clang/lib/Headers/hlsl/hlsl_debugging.h @@ -14,6 +14,17 @@ namespace hlsl { _HLSL_AVAILABILITY(shadermodel, 6.10) void inline DebugBreak() { __builtin_debugtrap(); } +#if defined(__DIRECTX__) + +namespace dx { + +_HLSL_AVAILABILITY(shadermodel, 6.10) +bool inline IsDebuggingEnabled() { return __builtin_hlsl_isdebuggingenabled(); } + +} // namespace dx + +#endif // defined(__DIRECTX__) + } // namespace hlsl #endif // _HLSL_HLSL_DEBUGGING_H_ diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp index 7627bb10ccf6f1..5da688ff8e949c 100644 --- a/clang/lib/Sema/SemaHLSL.cpp +++ b/clang/lib/Sema/SemaHLSL.cpp @@ -5669,6 +5669,14 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { getASTContext().UnsignedIntTy); break; } + case Builtin::BI__builtin_hlsl_isdebuggingenabled: { + if (!getASTContext().getTargetInfo().getTriple().isDXIL()) { + SemaRef.Diag(TheCall->getBeginLoc(), + diag::err_builtin_target_unsupported); + return true; + } + return false; + } } return false; } diff --git a/clang/test/CodeGenHLSL/builtins/IsDebuggingEnabled.hlsl b/clang/test/CodeGenHLSL/builtins/IsDebuggingEnabled.hlsl new file mode 100644 index 00000000000000..ad5d6dab420a39 --- /dev/null +++ b/clang/test/CodeGenHLSL/builtins/IsDebuggingEnabled.hlsl @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.10-library \ +// RUN: -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s + +// CHECK-LABEL: define hidden noundef i1 @_Z4testv() +// CHECK: %[[RESULT:.*]] = call noundef i1 @_ZN4hlsl2dx18IsDebuggingEnabledEv() +// CHECK: ret i1 %[[RESULT]] + +// CHECK-LABEL: define linkonce_odr hidden noundef i1 @_ZN4hlsl2dx18IsDebuggingEnabledEv() +// CHECK: %[[RESULT:.*]] = call i1 @llvm.dx.isdebuggingenabled() +// CHECK: ret i1 %[[RESULT]] + +bool test() { + return hlsl::dx::IsDebuggingEnabled(); +} diff --git a/clang/test/SemaHLSL/BuiltIns/IsDebuggingEnabled-availability.hlsl b/clang/test/SemaHLSL/BuiltIns/IsDebuggingEnabled-availability.hlsl new file mode 100644 index 00000000000000..dabcb9fc978790 --- /dev/null +++ b/clang/test/SemaHLSL/BuiltIns/IsDebuggingEnabled-availability.hlsl @@ -0,0 +1,26 @@ +// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.9-library \ +// RUN: -fsyntax-only -verify %s +// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.0-compute \ +// RUN: -fsyntax-only -verify %s +// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.10-compute \ +// RUN: -fsyntax-only -verify=available %s +// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.10-compute \ +// RUN: -fsyntax-only -verify=available %s +// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.10-library \ +// RUN: -fsyntax-only -verify=available %s +// RUN: %clang_cc1 -finclude-default-header -triple spirv-unknown-vulkan-library \ +// RUN: -fsyntax-only -verify=spirv %s + +// available-no-diagnostics + +[shader("compute")] +[numthreads(1, 1, 1)] +void main() { + // expected-error@+3 {{'IsDebuggingEnabled' is only available on Shader Model 6.10 or newer}} + // expected-note@hlsl/hlsl_debugging.h:* {{'IsDebuggingEnabled' has been marked as being introduced in Shader Model 6.10 here}} + // spirv-error@+1 {{no member named 'dx' in namespace 'hlsl'}} + bool a = hlsl::dx::IsDebuggingEnabled(); + + // spirv-error@+1 {{builtin is not supported on this target}} + bool b = __builtin_hlsl_isdebuggingenabled(); +} diff --git a/llvm/include/llvm/IR/IntrinsicsDirectX.td b/llvm/include/llvm/IR/IntrinsicsDirectX.td index c15806170f1565..95421f28acf34a 100644 --- a/llvm/include/llvm/IR/IntrinsicsDirectX.td +++ b/llvm/include/llvm/IR/IntrinsicsDirectX.td @@ -667,4 +667,7 @@ let TargetPrefix = "dx" in { def int_dx_view_id : DefaultAttrsIntrinsic<[llvm_i32_ty], [], [IntrNoMem]>; + +def int_dx_isdebuggingenabled + : DefaultAttrsIntrinsic<[llvm_i1_ty], [], [IntrNoMem]>; } diff --git a/llvm/lib/Target/DirectX/DXIL.td b/llvm/lib/Target/DirectX/DXIL.td index 2107d9e5685267..d03d9f2844884f 100644 --- a/llvm/lib/Target/DirectX/DXIL.td +++ b/llvm/lib/Target/DirectX/DXIL.td @@ -20,7 +20,7 @@ class Version<int major, int minor> { } // Valid DXIL Version records -foreach i = 0...8 in { +foreach i = 0...10 in { def DXIL1_ #i : Version<1, i>; } @@ -103,6 +103,7 @@ defset list<DXILOpClass> OpClasses = { def createNodeOutputHandle : DXILOpClass; def cutStream : DXILOpClass; def cycleCounterLegacy : DXILOpClass; + def isDebuggingEnabled : DXILOpClass; def discard : DXILOpClass; def dispatchMesh : DXILOpClass; def dispatchRaysDimensions : DXILOpClass; @@ -1711,3 +1712,11 @@ def StartInstanceLocation : DXILOp<257, startInstanceLocation> { let stages = [Stages<DXIL1_8, [vertex]>]; let attributes = [Attributes<DXIL1_0, [DXILReadNone]>]; } + +def IsDebuggingEnabled : DXILOp<344, isDebuggingEnabled> { + let Doc = "query if debugging is enabled"; + let intrinsics = [IntrinSelect<int_dx_isdebuggingenabled>]; + let arguments = []; + let result = Int1Ty; + let stages = [Stages<DXIL1_10, [all_stages]>]; +} diff --git a/llvm/test/CodeGen/DirectX/isdebuggingenabled.ll b/llvm/test/CodeGen/DirectX/isdebuggingenabled.ll new file mode 100644 index 00000000000000..f58159e8179330 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/isdebuggingenabled.ll @@ -0,0 +1,18 @@ +; RUN: opt -S -dxil-op-lower -mtriple=dxil-pc-shadermodel6.10-library %s | FileCheck %s +; RUN: opt -S -dxil-op-lower -mtriple=dxil-pc-shadermodel6.10-compute %s | FileCheck %s +; RUN: not opt -S -dxil-op-lower -mtriple=dxil-pc-shadermodel6.9-library %s 2>&1 | FileCheck %s --check-prefix=ERROR + +; CHECK-LABEL: define i1 @test( +; CHECK: %[[RESULT:.*]] = call i1 @dx.op.isDebuggingEnabled(i32 344) +; CHECK-NEXT: ret i1 %[[RESULT]] +; CHECK: declare i1 @dx.op.isDebuggingEnabled(i32) + +; ERROR: Cannot create IsDebuggingEnabled operation: No valid stage for DXIL version 1.9 + +define i1 @test() { +entry: + %result = call i1 @llvm.dx.isdebuggingenabled() + ret i1 %result +} + +declare i1 @llvm.dx.isdebuggingenabled() _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
