[llvm] [DirectX] Set Shader Flag DisableOptimizations. (PR #123136)
S. Bharadwaj Yadavalli via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 15 15:01:27 PST 2025
https://github.com/bharadwajy created https://github.com/llvm/llvm-project/pull/123136
Disabling optimizations using the option `-Od` to `clang-dxc` results in generation of named metadata "dx.disable_optimizations" - as tested [here](https://github.com/llvm/llvm-project/blob/main/clang/test/CodeGenHLSL/disable_opt.hlsl).
- Following changes facilitate setting of Shader Flag `DisableOptimizations`.
- Detect metadata "dx.disable_optimizations" as part of Metadata Analysis pass and set a new `bool` field
`ModuleMetadatainfo::DisableOptimization` accordingly.
- Add `DXILMetadataAnalysis` as required pass for the pass `DXILShaderFlags` that sets shader flags mask.
- Set shader flag `DisableOptimizations` based on the value of `ModuleMetadatainfo::DisableOptimizations`.
- Added test to verify setting of shader flag `DisableOptimizations`.
- Updated `llc-pipeline.ll` to reflect changes in output resulting from addition of Metadata Analysis pass as required by `DXILShaderFlags` pass.
NOTE: An [alternative implementation](https://github.com/llvm/llvm-project/compare/main...bharadwajy:llvm-project:shader-flags/disable-opt-flag) that
1. does not detect presence of metadata "dx.disable_optimizations" in Metadata Analysis pass and thus does not add the pass as a pre-requisite pass for `DXILShaderFlags` but
2. detects presence of metadata "dx.disable_optimizations" directly in `DXILShaderFlags` pass
has been considered. However, Metadata Analysis pass seemed appropriate for detection of metadata relevant to setting the shader flag `DisableOptimizations`. Hence this PR.
Closes #112263
>From 1dbf4f7a1031fa3715d71e34350efc742d27ec39 Mon Sep 17 00:00:00 2001
From: "S. Bharadwaj Yadavalli" <Bharadwaj.Yadavalli at microsoft.com>
Date: Wed, 15 Jan 2025 15:56:37 -0500
Subject: [PATCH] [DirectX] Set Shader Flag DisableOptimizations.
Disabling optimizations using the option -Od to clang-dxc
results in generation of named metadata "dx.disable_optimizations".
- Detect metadata "dx.disable_optimizations" as part of Metadata
Analysis pass and set a new bool field
ModuleMetadatainfo::DisableOptimization accordingly.
- Add DXILMetadataAnalysis as required pass for the pass ShaderFlags
that sets shader flags mask.
- Set shader flag DisableOptimization based on the value of
ModuleMetadatainfo::DisableOptimization.
Updated llc-pipeline.ll to reflect changes in output resulting
from addition of Metadata Analysis pass as required by ShaderFlags pass.
---
.../llvm/Analysis/DXILMetadataAnalysis.h | 1 +
llvm/lib/Analysis/DXILMetadataAnalysis.cpp | 16 ++++++++++++
llvm/lib/Target/DirectX/DXILShaderFlags.cpp | 15 ++++++++---
llvm/lib/Target/DirectX/DXILShaderFlags.h | 4 ++-
.../DirectX/ShaderFlags/disable-opt.ll | 25 +++++++++++++++++++
llvm/test/CodeGen/DirectX/llc-pipeline.ll | 2 +-
6 files changed, 58 insertions(+), 5 deletions(-)
create mode 100644 llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt.ll
diff --git a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
index cb535ac14f1c613..93a926c8991a8c8 100644
--- a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
+++ b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
@@ -37,6 +37,7 @@ struct ModuleMetadataInfo {
Triple::EnvironmentType ShaderProfile{Triple::UnknownEnvironment};
VersionTuple ValidatorVersion{};
SmallVector<EntryProperties> EntryPropertyVec{};
+ bool DisableOptimizations{false};
void print(raw_ostream &OS) const;
};
diff --git a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
index a7f666a3f8b48f2..426559c64f87623 100644
--- a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
+++ b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
@@ -68,6 +68,22 @@ static ModuleMetadataInfo collectMetadataInfo(Module &M) {
}
MMDAI.EntryPropertyVec.push_back(EFP);
}
+
+ // Set shader flags based on Module properties
+ SmallVector<llvm::Module::ModuleFlagEntry> FlagEntries;
+ M.getModuleFlagsMetadata(FlagEntries);
+ for (const auto &Flag : FlagEntries) {
+ if (Flag.Behavior != Module::ModFlagBehavior::Override)
+ continue;
+ if (Flag.Key->getString().compare("dx.disable_optimizations") == 0) {
+ const auto *V = mdconst::extract<llvm::ConstantInt>(Flag.Val);
+ if (V->isOne()) {
+ MMDAI.DisableOptimizations = true;
+ break;
+ }
+ }
+ }
+
return MMDAI;
}
diff --git a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
index b1ff975d4dae967..3684411c63bc564 100644
--- a/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
+++ b/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
@@ -85,7 +85,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
@@ -131,6 +132,9 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM) {
// Merge SCCSF with that of F
FunctionFlags[F].merge(SCCSF);
}
+
+ // Set shader flags based on Module properties based on module metadata
+ CombinedSFMask.DisableOptimizations = MMDI.DisableOptimizations;
}
void ComputedShaderFlags::print(raw_ostream &OS) const {
@@ -169,9 +173,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;
}
@@ -201,14 +206,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;
@@ -216,5 +224,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 e6c6d56402c1a72..abf7cc86259ed81 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.ll b/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt.ll
new file mode 100644
index 000000000000000..ee42b5b758ca0c4
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt.ll
@@ -0,0 +1,25 @@
+; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
+
+target triple = "dxilv1.6-unknown-shadermodel6.6-library"
+
+; 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
+
+target triple = "dxilv1.6-unknown-shadermodel6.6-library"
+
+; CHECK: ; Function main : 0x00000000
+define void @main() {
+entry:
+ ret void
+}
+
+!llvm.module.flags = !{!0}
+
+!0 = !{i32 4, !"dx.disable_optimizations", i32 1}
+
+
diff --git a/llvm/test/CodeGen/DirectX/llc-pipeline.ll b/llvm/test/CodeGen/DirectX/llc-pipeline.ll
index b07155724941467..03b2150bbc1dc3f 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
More information about the llvm-commits
mailing list