[clang] [llvm] [DirectX] Set Shader Flag DisableOptimizations (PR #126813)
S. Bharadwaj Yadavalli via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 12 12:35:49 PST 2025
https://github.com/bharadwajy updated https://github.com/llvm/llvm-project/pull/126813
>From 216c451521eaacb1f2116dca3671ecedff1f8e70 Mon Sep 17 00:00:00 2001
From: "S. Bharadwaj Yadavalli" <Bharadwaj.Yadavalli at microsoft.com>
Date: Tue, 11 Feb 2025 14:40:25 -0500
Subject: [PATCH 1/2] [DirectX] Set Shader Flag DisableOptimizations
Set the shader flag DisableOptimizations based on the optnone
attribute of shader entry functions.
Add DXIL Metadata Analysis pass as pre-requisite for
Shader Flags pass to obtain entry function information
collected therein.
Delete generation of named metadata dx.disable_optimization
that it is intended to indicate commandline flag specifying
disable opt. However, its intent is fulfilled using optnone
attribute of shader entry functions and not needed.
Delete corresponding test file disable_opt.ll
Named module metadata `dx.disable_optimizations` is intended
to indicate disabling optimizations (`-O0`) via commandline
flag. However, its intent is fulfilled via an `optnone` attribute
of shader entry functions via a recent change and thus not
needed. Delete generation of named metadata and corresponding
test file `disable_opt.ll`.
Add tests to verify correctness of setting shader flag.
---
clang/lib/CodeGen/CGHLSLRuntime.cpp | 6 ---
clang/test/CodeGenHLSL/disable_opt.hlsl | 12 -----
llvm/lib/Target/DirectX/DXILShaderFlags.cpp | 28 ++++++++--
llvm/lib/Target/DirectX/DXILShaderFlags.h | 4 +-
.../DirectX/ShaderFlags/disable-opt-cs.ll | 43 +++++++++++++++
.../DirectX/ShaderFlags/disable-opt-lib.ll | 53 +++++++++++++++++++
.../ShaderFlags/lib-entry-attr-error.ll | 30 +++++++++++
llvm/test/CodeGen/DirectX/llc-pipeline.ll | 2 +-
8 files changed, 155 insertions(+), 23 deletions(-)
delete mode 100644 clang/test/CodeGenHLSL/disable_opt.hlsl
create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll
create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll
create mode 100644 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 6cccd353cff96..03ddc87d8d3df 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -54,10 +54,6 @@ void addDxilValVersion(StringRef ValVersionStr, llvm::Module &M) {
auto *DXILValMD = M.getOrInsertNamedMetadata(DXILValKey);
DXILValMD->addOperand(Val);
}
-void addDisableOptimizations(llvm::Module &M) {
- StringRef Key = "dx.disable_optimizations";
- M.addModuleFlag(llvm::Module::ModFlagBehavior::Override, Key, 1);
-}
// cbuffer will be translated into global variable in special address space.
// If translate into C,
// cbuffer A {
@@ -171,8 +167,6 @@ void CGHLSLRuntime::finishCodeGen() {
addDxilValVersion(TargetOpts.DxilValidatorVersion, M);
generateGlobalCtorDtorCalls();
- if (CGM.getCodeGenOpts().OptimizationLevel == 0)
- addDisableOptimizations(M);
const DataLayout &DL = M.getDataLayout();
diff --git a/clang/test/CodeGenHLSL/disable_opt.hlsl b/clang/test/CodeGenHLSL/disable_opt.hlsl
deleted file mode 100644
index bfffe76cfa9de..0000000000000
--- a/clang/test/CodeGenHLSL/disable_opt.hlsl
+++ /dev/null
@@ -1,12 +0,0 @@
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -O0 -emit-llvm -xhlsl -o - %s | FileCheck %s
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -O3 -emit-llvm -xhlsl -o - %s | FileCheck %s --check-prefix=OPT
-
-// CHECK:!"dx.disable_optimizations", i32 1}
-
-// OPT-NOT:"dx.disable_optimizations"
-
-float bar(float a, float b);
-
-float foo(float a, float b) {
- return bar(a, b);
-}
diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
index 6a15bac153d85..bb502d8f189f6 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
@@ -17,6 +17,8 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/Analysis/DXILResource.h"
+#include "llvm/IR/Attributes.h"
+#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
@@ -96,7 +98,8 @@ void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,
}
/// Construct ModuleShaderFlags for module Module M
-void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM) {
+void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
+ const ModuleMetadataInfo &MMDI) {
CallGraph CG(M);
// Compute Shader Flags Mask for all functions using post-order visit of SCC
@@ -142,6 +145,20 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM) {
// Merge SCCSF with that of F
FunctionFlags[F].merge(SCCSF);
}
+
+ // Set DisableOptimizations flag based on the presence of OptimizeNone
+ // attribute of entry functions.
+ if (MMDI.EntryPropertyVec.size() > 0) {
+ CombinedSFMask.DisableOptimizations =
+ MMDI.EntryPropertyVec[0].Entry->hasFnAttribute(
+ llvm::Attribute::OptimizeNone);
+ // Ensure all entry functions have the same optimization attribute
+ for (auto EF : MMDI.EntryPropertyVec)
+ if (CombinedSFMask.DisableOptimizations !=
+ EF.Entry->hasFnAttribute(llvm::Attribute::OptimizeNone))
+ EF.Entry->getContext().diagnose(DiagnosticInfoUnsupported(
+ *(EF.Entry), "Inconsistent optnone attribute "));
+ }
}
void ComputedShaderFlags::print(raw_ostream &OS) const {
@@ -180,9 +197,10 @@ AnalysisKey ShaderFlagsAnalysis::Key;
ModuleShaderFlags ShaderFlagsAnalysis::run(Module &M,
ModuleAnalysisManager &AM) {
DXILResourceTypeMap &DRTM = AM.getResult<DXILResourceTypeAnalysis>(M);
+ const ModuleMetadataInfo MMDI = AM.getResult<DXILMetadataAnalysis>(M);
ModuleShaderFlags MSFI;
- MSFI.initialize(M, DRTM);
+ MSFI.initialize(M, DRTM, MMDI);
return MSFI;
}
@@ -212,14 +230,17 @@ PreservedAnalyses ShaderFlagsAnalysisPrinter::run(Module &M,
bool ShaderFlagsAnalysisWrapper::runOnModule(Module &M) {
DXILResourceTypeMap &DRTM =
getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
+ const ModuleMetadataInfo MMDI =
+ getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
- MSFI.initialize(M, DRTM);
+ MSFI.initialize(M, DRTM, MMDI);
return false;
}
void ShaderFlagsAnalysisWrapper::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequiredTransitive<DXILResourceTypeWrapperPass>();
+ AU.addRequired<DXILMetadataAnalysisWrapperPass>();
}
char ShaderFlagsAnalysisWrapper::ID = 0;
@@ -227,5 +248,6 @@ char ShaderFlagsAnalysisWrapper::ID = 0;
INITIALIZE_PASS_BEGIN(ShaderFlagsAnalysisWrapper, "dx-shader-flag-analysis",
"DXIL Shader Flag Analysis", true, true)
INITIALIZE_PASS_DEPENDENCY(DXILResourceTypeWrapperPass)
+INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass)
INITIALIZE_PASS_END(ShaderFlagsAnalysisWrapper, "dx-shader-flag-analysis",
"DXIL Shader Flag Analysis", true, true)
diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.h b/llvm/lib/Target/DirectX/DXILShaderFlags.h
index e6c6d56402c1a..abf7cc86259ed 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.h
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.h
@@ -14,6 +14,7 @@
#ifndef LLVM_TARGET_DIRECTX_DXILSHADERFLAGS_H
#define LLVM_TARGET_DIRECTX_DXILSHADERFLAGS_H
+#include "llvm/Analysis/DXILMetadataAnalysis.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Pass.h"
@@ -83,7 +84,8 @@ struct ComputedShaderFlags {
};
struct ModuleShaderFlags {
- void initialize(Module &, DXILResourceTypeMap &DRTM);
+ void initialize(Module &, DXILResourceTypeMap &DRTM,
+ const ModuleMetadataInfo &MMDI);
const ComputedShaderFlags &getFunctionFlags(const Function *) const;
const ComputedShaderFlags &getCombinedFlags() const { return CombinedSFMask; }
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll
new file mode 100644
index 0000000000000..4c5c77fabbb9d
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll
@@ -0,0 +1,43 @@
+; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
+
+
+; CHECK: ; Combined Shader Flags for Module
+; CHECK-NEXT: ; Shader Flags Value: 0x00000001
+
+; CHECK: ; Note: extra DXIL module flags:
+; CHECK-NEXT: ; D3D11_1_SB_GLOBAL_FLAG_SKIP_OPTIMIZATION
+
+; CHECK: ; Shader Flags for Module Functions
+; CHECK: ; Function main : 0x00000000
+; The test source in this file generated from the following command:
+; clang -cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -emit-llvm -O0 -o - <<EOF
+; [numthreads(1,1,1)]
+; [shader("compute")]
+; void main() {}
+; EOF
+
+; ModuleID = '-'
+source_filename = "-"
+target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
+target triple = "dxilv1.0-pc-shadermodel6.0-compute"
+
+; Function Attrs: convergent noinline norecurse optnone
+define void @main() #0 {
+entry:
+ ret void
+}
+
+; Function Attrs: alwaysinline convergent mustprogress norecurse nounwind
+define noundef i32 @_Z3foov() #1 {
+entry:
+ ret i32 0
+}
+
+attributes #0 = { convergent noinline norecurse optnone "approx-func-fp-math"="true" "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 "approx-func-fp-math"="true" "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}
+!dx.valver = !{!1}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 1, i32 8}
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll
new file mode 100644
index 0000000000000..5ad11a2f07881
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll
@@ -0,0 +1,53 @@
+; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
+
+
+; CHECK: ; Combined Shader Flags for Module
+; CHECK-NEXT: ; Shader Flags Value: 0x00000001
+
+; CHECK: ; Note: extra DXIL module flags:
+; CHECK-NEXT: ; D3D11_1_SB_GLOBAL_FLAG_SKIP_OPTIMIZATION
+
+; CHECK: ; Shader Flags for Module Functions
+; CHECK: ; Function main : 0x00000000
+; The test source in this file generated from the following command:
+; clang -cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -O0 -o - <<EOF
+
+; [numthreads(1,1,1)]
+; [shader("compute")]
+; void main() {}
+
+; int foo() {return 0;}
+; EOF
+
+; ModuleID = '-'
+source_filename = "-"
+target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
+target triple = "dxilv1.3-pc-shadermodel6.3-library"
+
+; Function Attrs: convergent mustprogress noinline norecurse nounwind optnone
+define internal void @_Z4mainv() #0 {
+entry:
+ ret void
+}
+
+; Function Attrs: convergent noinline norecurse optnone
+define void @main() #1 {
+entry:
+ call void @_Z4mainv()
+ ret void
+}
+
+; Function Attrs: convergent mustprogress noinline norecurse nounwind optnone
+define noundef i32 @_Z3foov() #0 {
+entry:
+ ret i32 0
+}
+
+attributes #0 = { convergent mustprogress noinline norecurse nounwind optnone "approx-func-fp-math"="true" "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 "approx-func-fp-math"="true" "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}
+!dx.valver = !{!1}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 1, i32 8}
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/lib-entry-attr-error.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/lib-entry-attr-error.ll
new file mode 100644
index 0000000000000..74d5fd093ded6
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/lib-entry-attr-error.ll
@@ -0,0 +1,30 @@
+; 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 "approx-func-fp-math"="true" "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 "approx-func-fp-math"="true" "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}
diff --git a/llvm/test/CodeGen/DirectX/llc-pipeline.ll b/llvm/test/CodeGen/DirectX/llc-pipeline.ll
index b071557249414..03b2150bbc1dc 100644
--- a/llvm/test/CodeGen/DirectX/llc-pipeline.ll
+++ b/llvm/test/CodeGen/DirectX/llc-pipeline.ll
@@ -23,8 +23,8 @@
; CHECK-NEXT: Scalarize vector operations
; CHECK-NEXT: DXIL Resource Binding Analysis
; CHECK-NEXT: DXIL resource Information
-; CHECK-NEXT: DXIL Shader Flag Analysis
; CHECK-NEXT: DXIL Module Metadata analysis
+; CHECK-NEXT: DXIL Shader Flag Analysis
; CHECK-NEXT: DXIL Translate Metadata
; CHECK-NEXT: DXIL Op Lowering
; CHECK-NEXT: DXIL Prepare Module
>From 11cbfe128e557b5754c7b06e076de06dc90a49a3 Mon Sep 17 00:00:00 2001
From: "S. Bharadwaj Yadavalli" <Bharadwaj.Yadavalli at microsoft.com>
Date: Wed, 12 Feb 2025 15:35:35 -0500
Subject: [PATCH 2/2] Address PR feedback
---
llvm/lib/Target/DirectX/DXILShaderFlags.cpp | 8 ++++----
llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll | 9 ---------
llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll | 9 ---------
3 files changed, 4 insertions(+), 22 deletions(-)
diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
index bb502d8f189f6..453aacf49abb3 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
@@ -153,11 +153,11 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
MMDI.EntryPropertyVec[0].Entry->hasFnAttribute(
llvm::Attribute::OptimizeNone);
// Ensure all entry functions have the same optimization attribute
- for (auto EF : MMDI.EntryPropertyVec)
+ for (const auto &EntryFunProps : MMDI.EntryPropertyVec)
if (CombinedSFMask.DisableOptimizations !=
- EF.Entry->hasFnAttribute(llvm::Attribute::OptimizeNone))
- EF.Entry->getContext().diagnose(DiagnosticInfoUnsupported(
- *(EF.Entry), "Inconsistent optnone attribute "));
+ EntryFunProps.Entry->hasFnAttribute(llvm::Attribute::OptimizeNone))
+ EntryFunProps.Entry->getContext().diagnose(DiagnosticInfoUnsupported(
+ *(EntryFunProps.Entry), "Inconsistent optnone attribute "));
}
}
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll
index 4c5c77fabbb9d..421c8b67350c2 100644
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll
@@ -16,9 +16,6 @@
; void main() {}
; EOF
-; ModuleID = '-'
-source_filename = "-"
-target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
target triple = "dxilv1.0-pc-shadermodel6.0-compute"
; Function Attrs: convergent noinline norecurse optnone
@@ -35,9 +32,3 @@ entry:
attributes #0 = { convergent noinline norecurse optnone "approx-func-fp-math"="true" "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 "approx-func-fp-math"="true" "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}
-!dx.valver = !{!1}
-
-!0 = !{i32 1, !"wchar_size", i32 4}
-!1 = !{i32 1, i32 8}
diff --git a/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll
index 5ad11a2f07881..240bd314cd1b2 100644
--- a/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll
@@ -19,9 +19,6 @@
; int foo() {return 0;}
; EOF
-; ModuleID = '-'
-source_filename = "-"
-target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
target triple = "dxilv1.3-pc-shadermodel6.3-library"
; Function Attrs: convergent mustprogress noinline norecurse nounwind optnone
@@ -45,9 +42,3 @@ entry:
attributes #0 = { convergent mustprogress noinline norecurse nounwind optnone "approx-func-fp-math"="true" "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 "approx-func-fp-math"="true" "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}
-!dx.valver = !{!1}
-
-!0 = !{i32 1, !"wchar_size", i32 4}
-!1 = !{i32 1, i32 8}
More information about the cfe-commits
mailing list