Author: joaosaffran
Date: 2026-03-02T10:53:55-08:00
New Revision: 0cabe933812f23ecd605baa82adb2b03fa0ab37d

URL: 
https://github.com/llvm/llvm-project/commit/0cabe933812f23ecd605baa82adb2b03fa0ab37d
DIFF: 
https://github.com/llvm/llvm-project/commit/0cabe933812f23ecd605baa82adb2b03fa0ab37d.diff

LOG: [HLSL] Reintroduce dx.disable_optimizations to set DisableOptimization 
Shader Flag (#180069)

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

---------

Co-authored-by: Joao Saffran <[email protected]>

Added: 
    

Modified: 
    clang/lib/CodeGen/CGHLSLRuntime.cpp
    clang/test/CodeGenHLSL/inline-functions.hlsl
    llvm/lib/Target/DirectX/DXILShaderFlags.cpp
    llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll
    llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll

Removed: 
    llvm/test/CodeGen/DirectX/ShaderFlags/lib-entry-attr-error.ll


################################################################################
diff  --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 79d709867dc02..c1329ede7430f 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -487,6 +487,10 @@ void CGHLSLRuntime::finishCodeGen() {
   if (CodeGenOpts.AllResourcesBound)
     M.setModuleFlag(llvm::Module::ModFlagBehavior::Error,
                     "dx.allresourcesbound", 1);
+  if (CodeGenOpts.OptimizationLevel == 0)
+    M.addModuleFlag(llvm::Module::ModFlagBehavior::Override,
+                    "dx.disable_optimizations", 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
@@ -523,8 +527,6 @@ void clang::CodeGen::CGHLSLRuntime::setHLSLEntryAttributes(
   // later in the compiler-flow for such module functions is not aware of and
   // 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);
   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 52993ee1c1220..7e16dcda87a57 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
@@ -106,6 +106,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.
@@ -251,18 +258,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}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to