Skip to content

Commit 1dbf4f7

Browse files
committed
[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.
1 parent 6a214ec commit 1dbf4f7

File tree

6 files changed

+58
-5
lines changed

6 files changed

+58
-5
lines changed

llvm/include/llvm/Analysis/DXILMetadataAnalysis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct ModuleMetadataInfo {
3737
Triple::EnvironmentType ShaderProfile{Triple::UnknownEnvironment};
3838
VersionTuple ValidatorVersion{};
3939
SmallVector<EntryProperties> EntryPropertyVec{};
40+
bool DisableOptimizations{false};
4041
void print(raw_ostream &OS) const;
4142
};
4243

llvm/lib/Analysis/DXILMetadataAnalysis.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ static ModuleMetadataInfo collectMetadataInfo(Module &M) {
6868
}
6969
MMDAI.EntryPropertyVec.push_back(EFP);
7070
}
71+
72+
// Set shader flags based on Module properties
73+
SmallVector<llvm::Module::ModuleFlagEntry> FlagEntries;
74+
M.getModuleFlagsMetadata(FlagEntries);
75+
for (const auto &Flag : FlagEntries) {
76+
if (Flag.Behavior != Module::ModFlagBehavior::Override)
77+
continue;
78+
if (Flag.Key->getString().compare("dx.disable_optimizations") == 0) {
79+
const auto *V = mdconst::extract<llvm::ConstantInt>(Flag.Val);
80+
if (V->isOne()) {
81+
MMDAI.DisableOptimizations = true;
82+
break;
83+
}
84+
}
85+
}
86+
7187
return MMDAI;
7288
}
7389

llvm/lib/Target/DirectX/DXILShaderFlags.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,
8585
}
8686

8787
/// Construct ModuleShaderFlags for module Module M
88-
void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM) {
88+
void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
89+
const ModuleMetadataInfo &MMDI) {
8990
CallGraph CG(M);
9091

9192
// Compute Shader Flags Mask for all functions using post-order visit of SCC
@@ -131,6 +132,9 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM) {
131132
// Merge SCCSF with that of F
132133
FunctionFlags[F].merge(SCCSF);
133134
}
135+
136+
// Set shader flags based on Module properties based on module metadata
137+
CombinedSFMask.DisableOptimizations = MMDI.DisableOptimizations;
134138
}
135139

136140
void ComputedShaderFlags::print(raw_ostream &OS) const {
@@ -169,9 +173,10 @@ AnalysisKey ShaderFlagsAnalysis::Key;
169173
ModuleShaderFlags ShaderFlagsAnalysis::run(Module &M,
170174
ModuleAnalysisManager &AM) {
171175
DXILResourceTypeMap &DRTM = AM.getResult<DXILResourceTypeAnalysis>(M);
176+
const ModuleMetadataInfo MMDI = AM.getResult<DXILMetadataAnalysis>(M);
172177

173178
ModuleShaderFlags MSFI;
174-
MSFI.initialize(M, DRTM);
179+
MSFI.initialize(M, DRTM, MMDI);
175180

176181
return MSFI;
177182
}
@@ -201,20 +206,24 @@ PreservedAnalyses ShaderFlagsAnalysisPrinter::run(Module &M,
201206
bool ShaderFlagsAnalysisWrapper::runOnModule(Module &M) {
202207
DXILResourceTypeMap &DRTM =
203208
getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
209+
const ModuleMetadataInfo MMDI =
210+
getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
204211

205-
MSFI.initialize(M, DRTM);
212+
MSFI.initialize(M, DRTM, MMDI);
206213
return false;
207214
}
208215

209216
void ShaderFlagsAnalysisWrapper::getAnalysisUsage(AnalysisUsage &AU) const {
210217
AU.setPreservesAll();
211218
AU.addRequiredTransitive<DXILResourceTypeWrapperPass>();
219+
AU.addRequired<DXILMetadataAnalysisWrapperPass>();
212220
}
213221

214222
char ShaderFlagsAnalysisWrapper::ID = 0;
215223

216224
INITIALIZE_PASS_BEGIN(ShaderFlagsAnalysisWrapper, "dx-shader-flag-analysis",
217225
"DXIL Shader Flag Analysis", true, true)
218226
INITIALIZE_PASS_DEPENDENCY(DXILResourceTypeWrapperPass)
227+
INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass)
219228
INITIALIZE_PASS_END(ShaderFlagsAnalysisWrapper, "dx-shader-flag-analysis",
220229
"DXIL Shader Flag Analysis", true, true)

llvm/lib/Target/DirectX/DXILShaderFlags.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_TARGET_DIRECTX_DXILSHADERFLAGS_H
1515
#define LLVM_TARGET_DIRECTX_DXILSHADERFLAGS_H
1616

17+
#include "llvm/Analysis/DXILMetadataAnalysis.h"
1718
#include "llvm/IR/Function.h"
1819
#include "llvm/IR/PassManager.h"
1920
#include "llvm/Pass.h"
@@ -83,7 +84,8 @@ struct ComputedShaderFlags {
8384
};
8485

8586
struct ModuleShaderFlags {
86-
void initialize(Module &, DXILResourceTypeMap &DRTM);
87+
void initialize(Module &, DXILResourceTypeMap &DRTM,
88+
const ModuleMetadataInfo &MMDI);
8789
const ComputedShaderFlags &getFunctionFlags(const Function *) const;
8890
const ComputedShaderFlags &getCombinedFlags() const { return CombinedSFMask; }
8991

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
2+
3+
target triple = "dxilv1.6-unknown-shadermodel6.6-library"
4+
5+
; CHECK: ; Combined Shader Flags for Module
6+
; CHECK-NEXT: ; Shader Flags Value: 0x00000001
7+
8+
; CHECK: ; Note: extra DXIL module flags:
9+
; CHECK-NEXT: ; D3D11_1_SB_GLOBAL_FLAG_SKIP_OPTIMIZATION
10+
11+
; CHECK: ; Shader Flags for Module Functions
12+
13+
target triple = "dxilv1.6-unknown-shadermodel6.6-library"
14+
15+
; CHECK: ; Function main : 0x00000000
16+
define void @main() {
17+
entry:
18+
ret void
19+
}
20+
21+
!llvm.module.flags = !{!0}
22+
23+
!0 = !{i32 4, !"dx.disable_optimizations", i32 1}
24+
25+

llvm/test/CodeGen/DirectX/llc-pipeline.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
; CHECK-NEXT: Scalarize vector operations
2424
; CHECK-NEXT: DXIL Resource Binding Analysis
2525
; CHECK-NEXT: DXIL resource Information
26-
; CHECK-NEXT: DXIL Shader Flag Analysis
2726
; CHECK-NEXT: DXIL Module Metadata analysis
27+
; CHECK-NEXT: DXIL Shader Flag Analysis
2828
; CHECK-NEXT: DXIL Translate Metadata
2929
; CHECK-NEXT: DXIL Op Lowering
3030
; CHECK-NEXT: DXIL Prepare Module

0 commit comments

Comments
 (0)