[clang] [llvm] [HLSL] Reintroduce dx.disable_optimizations to set DisableOptimization Shader Flag (PR #180069)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 5 15:51:37 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-hlsl
@llvm/pr-subscribers-clang-codegen
Author: None (joaosaffran)
<details>
<summary>Changes</summary>
This patch removes optnone from HLSL entry functions and instead uses `dx.disable_optimizations` module flag to know when to enable `DisableOptimization` Shader Flag. This is part of: #<!-- -->167936
---
Full diff: https://github.com/llvm/llvm-project/pull/180069.diff
6 Files Affected:
- (modified) clang/lib/CodeGen/CGHLSLRuntime.cpp (+8-1)
- (modified) clang/test/CodeGenHLSL/inline-functions.hlsl (+5-9)
- (modified) llvm/lib/Target/DirectX/DXILShaderFlags.cpp (+8-12)
- (modified) llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll (+6-2)
- (modified) llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll (+10-5)
- (removed) llvm/test/CodeGen/DirectX/ShaderFlags/lib-entry-attr-error.ll (-30)
``````````diff
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 985bc8640118e..006861a1efe43 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -267,6 +267,13 @@ static std::optional<llvm::Value *> initializeLocalResourceArray(
return Index;
}
+static void addDisableOptimizations(llvm::Module &M) {
+ if (M.getModuleFlag("dx.disable_optimizations"))
+ return;
+ StringRef Key = "dx.disable_optimizations";
+ M.addModuleFlag(llvm::Module::ModFlagBehavior::Override, Key, 1);
+}
+
} // namespace
llvm::Type *
@@ -523,7 +530,7 @@ void clang::CodeGen::CGHLSLRuntime::setHLSLEntryAttributes(
// hence not able to set attributes of the newly materialized entry functions.
// So, set attributes of entry function here, as appropriate.
if (CGM.getCodeGenOpts().OptimizationLevel == 0)
- Fn->addFnAttr(llvm::Attribute::OptimizeNone);
+ addDisableOptimizations(CGM.getModule());
Fn->addFnAttr(llvm::Attribute::NoInline);
if (CGM.getLangOpts().HLSLSpvEnableMaximalReconvergence) {
diff --git a/clang/test/CodeGenHLSL/inline-functions.hlsl b/clang/test/CodeGenHLSL/inline-functions.hlsl
index 0c7467e2f972e..d24c07cf5224f 100644
--- a/clang/test/CodeGenHLSL/inline-functions.hlsl
+++ b/clang/test/CodeGenHLSL/inline-functions.hlsl
@@ -1,9 +1,9 @@
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes -o - | FileCheck %s --check-prefixes=CHECK,NOINLINE,OPT_ATTR
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library %s -emit-llvm -O0 -o - | FileCheck %s --check-prefixes=CHECK,INLINE,OPT_ATTR
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library %s -emit-llvm -O1 -o - | FileCheck %s --check-prefixes=CHECK,INLINE,NOOPT_ATTR
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes -o - | FileCheck %s --check-prefixes=CHECK,NOINLINE
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library %s -emit-llvm -O0 -o - | FileCheck %s --check-prefixes=CHECK,INLINE
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library %s -emit-llvm -O1 -o - | FileCheck %s --check-prefixes=CHECK,INLINE
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute %s -emit-llvm -disable-llvm-passes -o - | FileCheck %s --check-prefixes=CHECK,NOINLINE
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute %s -emit-llvm -O0 -o - | FileCheck %s --check-prefixes=CHECK,INLINE,OPT_ATTR
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute %s -emit-llvm -O1 -o - | FileCheck %s --check-prefixes=CHECK,INLINE,NOOPT_ATTR
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute %s -emit-llvm -O0 -o - | FileCheck %s --check-prefixes=CHECK,INLINE
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute %s -emit-llvm -O1 -o - | FileCheck %s --check-prefixes=CHECK,INLINE
// Tests that user functions will always be inlined.
// This includes exported functions and mangled entry point implementation functions.
@@ -71,8 +71,6 @@ RWBuffer<unsigned> Indices;
// NOINLINE: ret void
// The unmangled version is not inlined, EntryAttr reflects that
-// OPT_ATTR: Function Attrs: {{.*}}optnone
-// NOOPT_ATTR-NOT: Function Attrs: {{.*}}optnone
// CHECK: define void @main() {{[a-z_ ]*}}[[EntryAttr:\#[0-9]+]]
// Make sure function calls are inlined when AlwaysInline is run
// This only leaves calls to llvm. intrinsics
@@ -99,8 +97,6 @@ void main(unsigned int GI : SV_GroupIndex) {
// NOINLINE: ret void
// The unmangled version is not inlined, EntryAttr reflects that
-// OPT_ATTR: Function Attrs: {{.*}}optnone
-// NOOPT_ATTR-NOT: Function Attrs: {{.*}}optnone
// CHECK: define void @main10() {{[a-z_ ]*}}[[EntryAttr]]
// Make sure function calls are inlined when AlwaysInline is run
// This only leaves calls to llvm. intrinsics
diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
index 54cf56f92277b..87d531489b757 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
@@ -104,6 +104,13 @@ static bool checkWaveOps(Intrinsic::ID IID) {
}
}
+static bool isOptimizationDisabled(const Module &M) {
+ const StringRef Key = "dx.disable_optimizations";
+ if (auto *Flag = mdconst::extract_or_null<ConstantInt>(M.getModuleFlag(Key)))
+ return Flag->getValue().getBoolValue();
+ return false;
+}
+
// Checks to see if the status bit from a load with status
// instruction is ever extracted. If it is, the module needs
// to have the TiledResources shader flag set.
@@ -249,18 +256,7 @@ ModuleShaderFlags::gatherGlobalModuleFlags(const Module &M,
ComputedShaderFlags CSF;
- // Set DisableOptimizations flag based on the presence of OptimizeNone
- // attribute of entry functions.
- if (MMDI.EntryPropertyVec.size() > 0) {
- CSF.DisableOptimizations = MMDI.EntryPropertyVec[0].Entry->hasFnAttribute(
- llvm::Attribute::OptimizeNone);
- // Ensure all entry functions have the same optimization attribute
- for (const auto &EntryFunProps : MMDI.EntryPropertyVec)
- if (CSF.DisableOptimizations !=
- EntryFunProps.Entry->hasFnAttribute(llvm::Attribute::OptimizeNone))
- EntryFunProps.Entry->getContext().diagnose(DiagnosticInfoUnsupported(
- *(EntryFunProps.Entry), "Inconsistent optnone attribute "));
- }
+ CSF.DisableOptimizations = isOptimizationDisabled(M);
CSF.UAVsAtEveryStage = hasUAVsAtEveryStage(DRM, MMDI);
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll
index 4bdb7ec50f6f4..4cb46043b41fc 100644
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll
@@ -18,7 +18,7 @@
target triple = "dxilv1.0-pc-shadermodel6.0-compute"
-; Function Attrs: convergent noinline norecurse optnone
+; Function Attrs: convergent noinline norecurse
define void @main() #0 {
entry:
ret void
@@ -30,5 +30,9 @@ entry:
ret i32 0
}
-attributes #0 = { convergent noinline norecurse optnone "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+attributes #0 = { convergent noinline norecurse "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { alwaysinline convergent mustprogress norecurse nounwind "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+
+!llvm.module.flags = !{!0}
+
+!0 = !{i32 4, !"dx.disable_optimizations", i32 1}
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll
index 03756710adc3a..27d1e2a0910c3 100644
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll
@@ -21,24 +21,29 @@
target triple = "dxilv1.3-pc-shadermodel6.3-library"
-; Function Attrs: convergent mustprogress noinline norecurse nounwind optnone
+; Function Attrs: convergent mustprogress noinline norecurse nounwind
define internal void @_Z4mainv() #0 {
entry:
ret void
}
-; Function Attrs: convergent noinline norecurse optnone
+; Function Attrs: convergent noinline norecurse
define void @main() #1 {
entry:
call void @_Z4mainv()
ret void
}
-; Function Attrs: convergent mustprogress noinline norecurse nounwind optnone
+; Function Attrs: convergent mustprogress noinline norecurse nounwind
define noundef i32 @_Z3foov() #0 {
entry:
ret i32 0
}
-attributes #0 = { convergent mustprogress noinline norecurse nounwind optnone "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
-attributes #1 = { convergent noinline norecurse optnone "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+attributes #0 = { convergent mustprogress noinline norecurse nounwind "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+attributes #1 = { convergent noinline norecurse "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
+
+
+!llvm.module.flags = !{!0}
+
+!0 = !{i32 4, !"dx.disable_optimizations", i32 1}
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/lib-entry-attr-error.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/lib-entry-attr-error.ll
deleted file mode 100644
index ce35c03d6bcfa..0000000000000
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/lib-entry-attr-error.ll
+++ /dev/null
@@ -1,30 +0,0 @@
-; RUN: not opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
-
-target triple = "dxilv1.3-pc-shadermodel6.3-library"
-
-; All entry functions of a library shader need to either have optnone
-; or not have the attribute
-; CHECK: error:
-; CHECK-SAME: in function entry_two
-; CHECK-SAME: Inconsistent optnone attribute
-; Function Attrs: convergent noinline norecurse optnone
-define void @entry_one() #0 {
-entry:
- ret void
-}
-
-; Function Attrs: convergent noinline norecurse
-define void @entry_two() #1 {
-entry:
- ret void
-}
-
-attributes #0 = { convergent noinline norecurse optnone "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
-attributes #1 = { convergent noinline norecurse "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
-
-!llvm.module.flags = !{!0, !1}
-!dx.valver = !{!2}
-
-!0 = !{i32 1, !"wchar_size", i32 4}
-!1 = !{i32 4, !"dx.disable_optimizations", i32 1}
-!2 = !{i32 1, i32 8}
``````````
</details>
https://github.com/llvm/llvm-project/pull/180069
More information about the cfe-commits
mailing list