[clang] [llvm] [HLSL] Add allresourcesbound option to DXC driver and set corresponding module flag (PR #173411)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 23 11:12:14 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-hlsl
@llvm/pr-subscribers-backend-directx
Author: Joshua Batista (bob80905)
<details>
<summary>Changes</summary>
This PR adds a new CC1 option and a new dxc driver option. The DXC option, when set, is translated into the new CC1 option.
The `all-resources-bound` dxc option will create a metadata module flag, and the print-dx-shader-flags pass will set the appropriate shader module flag from this metadata module flag.
Fixes https://github.com/llvm/llvm-project/issues/112264
---
Full diff: https://github.com/llvm/llvm-project/pull/173411.diff
9 Files Affected:
- (modified) clang/include/clang/Basic/CodeGenOptions.def (+3)
- (modified) clang/include/clang/Options/Options.td (+7)
- (modified) clang/lib/CodeGen/CGHLSLRuntime.cpp (+3-1)
- (modified) clang/lib/Driver/ToolChains/Clang.cpp (+1)
- (modified) clang/lib/Driver/ToolChains/HLSL.cpp (+6)
- (added) clang/test/CodeGenHLSL/all-resources-bound.hlsl (+7)
- (added) clang/test/Options/all_resources_bound.hlsl (+10)
- (modified) llvm/lib/Target/DirectX/DXILShaderFlags.cpp (+7)
- (added) llvm/test/CodeGen/DirectX/ShaderFlags/all-resources-bound.ll (+25)
``````````diff
diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def
index a059803c433e3..06e3e4cd564b1 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -488,6 +488,9 @@ CODEGENOPT(StaticClosure, 1, 0, Benign)
/// Assume that UAVs/SRVs may alias
CODEGENOPT(ResMayAlias, 1, 0, Benign)
+/// Assume that not all resources are bound
+CODEGENOPT(AllResourcesBound, 1, 0, Benign)
+
/// Controls how unwind v2 (epilog) information should be generated for x64
/// Windows.
ENUM_CODEGENOPT(WinX64EHUnwindV2, WinX64EHUnwindV2Mode,
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 8cd31a3be109a..04756ce486eaf 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -9612,6 +9612,13 @@ class DXCJoinedOrSeparate<string name> : Option<["/", "-"], name,
KIND_JOINED_OR_SEPARATE>, Group<dxc_Group>,
Visibility<[DXCOption]>;
+def dxc_all_resources_bound : DXCFlag<"all-resources-bound">,
+ HelpText<"Enables agressive flattening">;
+def hlsl_all_resources_bound : Option<["/", "-"], "hlsl-all-resources-bound", KIND_FLAG>,
+ Group<dxc_Group>,
+ Visibility<[ClangOption, CC1Option]>,
+ HelpText<"Enables agressive flattening">,
+ MarshallingInfoFlag<CodeGenOpts<"AllResourcesBound">>;
def dxc_col_major : DXCFlag<"Zpc">, HelpText<"Pack matrices in column-major order">;
def dxc_row_major : DXCFlag<"Zpr">, HelpText<"Pack matrices in row-major order">;
def dxc_no_stdinc : DXCFlag<"hlsl-no-stdinc">,
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 2e9602d1b3793..4c0349d4be7bf 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -483,7 +483,9 @@ void CGHLSLRuntime::finishCodeGen() {
addDxilValVersion(TargetOpts.DxilValidatorVersion, M);
if (CodeGenOpts.ResMayAlias)
M.setModuleFlag(llvm::Module::ModFlagBehavior::Error, "dx.resmayalias", 1);
-
+ if (CodeGenOpts.AllResourcesBound)
+ M.setModuleFlag(llvm::Module::ModFlagBehavior::Error,
+ "dx.allresourcesbound", 1);
// NativeHalfType corresponds to the -fnative-half-type clang option which is
// aliased by clang-dxc's -enable-16bit-types option. This option is used to
// set the UseNativeLowPrecision DXIL module flag in the DirectX backend
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 310f3b58a211e..2721bac473070 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3699,6 +3699,7 @@ static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs,
static void RenderHLSLOptions(const ArgList &Args, ArgStringList &CmdArgs,
types::ID InputType) {
const unsigned ForwardedArguments[] = {
+ options::OPT_hlsl_all_resources_bound,
options::OPT_dxil_validator_version,
options::OPT_res_may_alias,
options::OPT_D,
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp b/clang/lib/Driver/ToolChains/HLSL.cpp
index b76a3d7287975..587481b5623e1 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -417,6 +417,12 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
getDriver().Diag(diag::err_drv_dxc_invalid_matrix_layout);
for (Arg *A : Args) {
+ if (A->getOption().getID() == options::OPT_dxc_all_resources_bound) {
+ DAL->AddFlagArg(nullptr,
+ Opts.getOption(options::OPT_hlsl_all_resources_bound));
+ A->claim();
+ continue;
+ }
if (A->getOption().getID() == options::OPT_dxil_validator_version) {
StringRef ValVerStr = A->getValue();
if (!isLegalValidatorVersion(ValVerStr, getDriver()))
diff --git a/clang/test/CodeGenHLSL/all-resources-bound.hlsl b/clang/test/CodeGenHLSL/all-resources-bound.hlsl
new file mode 100644
index 0000000000000..22703cc6eb0e5
--- /dev/null
+++ b/clang/test/CodeGenHLSL/all-resources-bound.hlsl
@@ -0,0 +1,7 @@
+// RUN: %clang_dxc -all-resources-bound -T lib_6_3 -HV 202x -Vd -Xclang -emit-llvm %s | FileCheck %s --check-prefix=FLAG
+// RUN: %clang_dxc -T lib_6_3 -HV 202x -Xclang -emit-llvm %s | FileCheck %s --check-prefix=NOFLAG
+
+// FLAG-DAG: ![[ARB:.*]] = !{i32 1, !"dx.allresourcesbound", i32 1}
+// FLAG-DAG: !llvm.module.flags = !{{{.*}}![[ARB]]{{.*}}}
+
+// NOFLAG-NOT: dx.allresourcesbound
diff --git a/clang/test/Options/all_resources_bound.hlsl b/clang/test/Options/all_resources_bound.hlsl
new file mode 100644
index 0000000000000..2dc1795bfb286
--- /dev/null
+++ b/clang/test/Options/all_resources_bound.hlsl
@@ -0,0 +1,10 @@
+// RUN: %clang_dxc -T lib_6_4 -all-resources-bound %s 2>&1 -### | FileCheck -check-prefix=ARB %s
+// RUN: %clang_dxc -T lib_6_4 %s 2>&1 -### | FileCheck -check-prefix=NO_ARB %s
+
+// ARB: "-hlsl-all-resources-bound"
+// NO_ARB-NOT: "-hlsl-all-resources-bound"
+// assert expected CC1 option is present
+float4 main(float4 a : A) : SV_TARGET
+{
+ return -a.yxxx;
+}
diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
index e0049dc75c0db..a4320209c53d6 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
@@ -287,6 +287,13 @@ ModuleShaderFlags::gatherGlobalModuleFlags(const Module &M,
if (CanSetResMayNotAlias && MMDI.ValidatorVersion < VersionTuple(1, 8))
CSF.ResMayNotAlias = !DRM.uavs().empty();
+ // The command line option -all-resources-bound will set the
+ // dx.allresourcesbound module flag to 1
+ if (auto *AllResourcesBound = mdconst::extract_or_null<ConstantInt>(
+ M.getModuleFlag("dx.allresourcesbound")))
+ if (AllResourcesBound->getValue().getBoolValue())
+ CSF.AllResourcesBound = true;
+
return CSF;
}
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/all-resources-bound.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/all-resources-bound.ll
new file mode 100644
index 0000000000000..14ddcfafdf5de
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/all-resources-bound.ll
@@ -0,0 +1,25 @@
+; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
+
+; This test checks to ensure that setting the LLVM module flag
+; "dx.allresourcesbound" to 1 sets the corresponding DXIL shader flag
+
+target triple = "dxil-pc-shadermodel6.8-library"
+
+; CHECK: Combined Shader Flags for Module
+; CHECK-NEXT: Shader Flags Value: 0x00000100
+
+; CHECK: Note: shader requires additional functionality:
+; CHECK-NEXT: extra DXIL module flags:
+; CHECK-NEXT: All resources bound for the duration of shader execution
+
+
+; CHECK: Function main : 0x00000100
+define float @main() #0 {
+ ret float 0.0
+}
+
+!llvm.module.flags = !{!0}
+
+!0 = !{i32 1, !"dx.allresourcesbound", i32 1}
+
+attributes #0 = { convergent norecurse nounwind "hlsl.export"}
``````````
</details>
https://github.com/llvm/llvm-project/pull/173411
More information about the llvm-commits
mailing list