Skip to content

Commit aa360a5

Browse files
bharadwajyflovent
authored andcommitted
[DirectX] Set Shader Flag DisableOptimizations (llvm#126813)
- Set the shader flag `DisableOptimizations` based on `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. - Named module metadata `dx.disable_optimizations` is intended to indicate disabling optimizations (`-O0`) via commandline flag. However, its intent is fulfilled by `optnone` attribute of shader entry functions as implemented in 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. Closes llvm#112263
1 parent 0b4a71d commit aa360a5

File tree

8 files changed

+137
-23
lines changed

8 files changed

+137
-23
lines changed

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ void addDxilValVersion(StringRef ValVersionStr, llvm::Module &M) {
5454
auto *DXILValMD = M.getOrInsertNamedMetadata(DXILValKey);
5555
DXILValMD->addOperand(Val);
5656
}
57-
void addDisableOptimizations(llvm::Module &M) {
58-
StringRef Key = "dx.disable_optimizations";
59-
M.addModuleFlag(llvm::Module::ModFlagBehavior::Override, Key, 1);
60-
}
6157
// cbuffer will be translated into global variable in special address space.
6258
// If translate into C,
6359
// cbuffer A {
@@ -171,8 +167,6 @@ void CGHLSLRuntime::finishCodeGen() {
171167
addDxilValVersion(TargetOpts.DxilValidatorVersion, M);
172168

173169
generateGlobalCtorDtorCalls();
174-
if (CGM.getCodeGenOpts().OptimizationLevel == 0)
175-
addDisableOptimizations(M);
176170

177171
const DataLayout &DL = M.getDataLayout();
178172

clang/test/CodeGenHLSL/disable_opt.hlsl

Lines changed: 0 additions & 12 deletions
This file was deleted.

llvm/lib/Target/DirectX/DXILShaderFlags.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "llvm/ADT/SmallVector.h"
1818
#include "llvm/Analysis/CallGraph.h"
1919
#include "llvm/Analysis/DXILResource.h"
20+
#include "llvm/IR/Attributes.h"
21+
#include "llvm/IR/DiagnosticInfo.h"
2022
#include "llvm/IR/Instruction.h"
2123
#include "llvm/IR/Instructions.h"
2224
#include "llvm/IR/IntrinsicInst.h"
@@ -96,7 +98,8 @@ void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,
9698
}
9799

98100
/// Construct ModuleShaderFlags for module Module M
99-
void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM) {
101+
void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
102+
const ModuleMetadataInfo &MMDI) {
100103
CallGraph CG(M);
101104

102105
// Compute Shader Flags Mask for all functions using post-order visit of SCC
@@ -142,6 +145,20 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM) {
142145
// Merge SCCSF with that of F
143146
FunctionFlags[F].merge(SCCSF);
144147
}
148+
149+
// Set DisableOptimizations flag based on the presence of OptimizeNone
150+
// attribute of entry functions.
151+
if (MMDI.EntryPropertyVec.size() > 0) {
152+
CombinedSFMask.DisableOptimizations =
153+
MMDI.EntryPropertyVec[0].Entry->hasFnAttribute(
154+
llvm::Attribute::OptimizeNone);
155+
// Ensure all entry functions have the same optimization attribute
156+
for (const auto &EntryFunProps : MMDI.EntryPropertyVec)
157+
if (CombinedSFMask.DisableOptimizations !=
158+
EntryFunProps.Entry->hasFnAttribute(llvm::Attribute::OptimizeNone))
159+
EntryFunProps.Entry->getContext().diagnose(DiagnosticInfoUnsupported(
160+
*(EntryFunProps.Entry), "Inconsistent optnone attribute "));
161+
}
145162
}
146163

147164
void ComputedShaderFlags::print(raw_ostream &OS) const {
@@ -180,9 +197,10 @@ AnalysisKey ShaderFlagsAnalysis::Key;
180197
ModuleShaderFlags ShaderFlagsAnalysis::run(Module &M,
181198
ModuleAnalysisManager &AM) {
182199
DXILResourceTypeMap &DRTM = AM.getResult<DXILResourceTypeAnalysis>(M);
200+
const ModuleMetadataInfo MMDI = AM.getResult<DXILMetadataAnalysis>(M);
183201

184202
ModuleShaderFlags MSFI;
185-
MSFI.initialize(M, DRTM);
203+
MSFI.initialize(M, DRTM, MMDI);
186204

187205
return MSFI;
188206
}
@@ -212,20 +230,24 @@ PreservedAnalyses ShaderFlagsAnalysisPrinter::run(Module &M,
212230
bool ShaderFlagsAnalysisWrapper::runOnModule(Module &M) {
213231
DXILResourceTypeMap &DRTM =
214232
getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
233+
const ModuleMetadataInfo MMDI =
234+
getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
215235

216-
MSFI.initialize(M, DRTM);
236+
MSFI.initialize(M, DRTM, MMDI);
217237
return false;
218238
}
219239

220240
void ShaderFlagsAnalysisWrapper::getAnalysisUsage(AnalysisUsage &AU) const {
221241
AU.setPreservesAll();
222242
AU.addRequiredTransitive<DXILResourceTypeWrapperPass>();
243+
AU.addRequired<DXILMetadataAnalysisWrapperPass>();
223244
}
224245

225246
char ShaderFlagsAnalysisWrapper::ID = 0;
226247

227248
INITIALIZE_PASS_BEGIN(ShaderFlagsAnalysisWrapper, "dx-shader-flag-analysis",
228249
"DXIL Shader Flag Analysis", true, true)
229250
INITIALIZE_PASS_DEPENDENCY(DXILResourceTypeWrapperPass)
251+
INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass)
230252
INITIALIZE_PASS_END(ShaderFlagsAnalysisWrapper, "dx-shader-flag-analysis",
231253
"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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
2+
3+
4+
; CHECK: ; Combined Shader Flags for Module
5+
; CHECK-NEXT: ; Shader Flags Value: 0x00000001
6+
7+
; CHECK: ; Note: extra DXIL module flags:
8+
; CHECK-NEXT: ; D3D11_1_SB_GLOBAL_FLAG_SKIP_OPTIMIZATION
9+
10+
; CHECK: ; Shader Flags for Module Functions
11+
; CHECK: ; Function main : 0x00000000
12+
; The test source in this file generated from the following command:
13+
; clang -cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -emit-llvm -O0 -o - <<EOF
14+
; [numthreads(1,1,1)]
15+
; [shader("compute")]
16+
; void main() {}
17+
; EOF
18+
19+
target triple = "dxilv1.0-pc-shadermodel6.0-compute"
20+
21+
; Function Attrs: convergent noinline norecurse optnone
22+
define void @main() #0 {
23+
entry:
24+
ret void
25+
}
26+
27+
; Function Attrs: alwaysinline convergent mustprogress norecurse nounwind
28+
define noundef i32 @_Z3foov() #1 {
29+
entry:
30+
ret i32 0
31+
}
32+
33+
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" }
34+
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" }
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
; RUN: opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
2+
3+
4+
; CHECK: ; Combined Shader Flags for Module
5+
; CHECK-NEXT: ; Shader Flags Value: 0x00000001
6+
7+
; CHECK: ; Note: extra DXIL module flags:
8+
; CHECK-NEXT: ; D3D11_1_SB_GLOBAL_FLAG_SKIP_OPTIMIZATION
9+
10+
; CHECK: ; Shader Flags for Module Functions
11+
; CHECK: ; Function main : 0x00000000
12+
; The test source in this file generated from the following command:
13+
; clang -cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -O0 -o - <<EOF
14+
15+
; [numthreads(1,1,1)]
16+
; [shader("compute")]
17+
; void main() {}
18+
19+
; int foo() {return 0;}
20+
; EOF
21+
22+
target triple = "dxilv1.3-pc-shadermodel6.3-library"
23+
24+
; Function Attrs: convergent mustprogress noinline norecurse nounwind optnone
25+
define internal void @_Z4mainv() #0 {
26+
entry:
27+
ret void
28+
}
29+
30+
; Function Attrs: convergent noinline norecurse optnone
31+
define void @main() #1 {
32+
entry:
33+
call void @_Z4mainv()
34+
ret void
35+
}
36+
37+
; Function Attrs: convergent mustprogress noinline norecurse nounwind optnone
38+
define noundef i32 @_Z3foov() #0 {
39+
entry:
40+
ret i32 0
41+
}
42+
43+
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" }
44+
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" }
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; RUN: not opt -S --passes="print-dx-shader-flags" 2>&1 %s | FileCheck %s
2+
3+
target triple = "dxilv1.3-pc-shadermodel6.3-library"
4+
5+
; All entry functions of a library shader need to either have optnone
6+
; or not have the attribute
7+
; CHECK: error:
8+
; CHECK-SAME: in function entry_two
9+
; CHECK-SAME: Inconsistent optnone attribute
10+
; Function Attrs: convergent noinline norecurse optnone
11+
define void @entry_one() #0 {
12+
entry:
13+
ret void
14+
}
15+
16+
; Function Attrs: convergent noinline norecurse
17+
define void @entry_two() #1 {
18+
entry:
19+
ret void
20+
}
21+
22+
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" }
23+
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" }
24+
25+
!llvm.module.flags = !{!0, !1}
26+
!dx.valver = !{!2}
27+
28+
!0 = !{i32 1, !"wchar_size", i32 4}
29+
!1 = !{i32 4, !"dx.disable_optimizations", i32 1}
30+
!2 = !{i32 1, i32 8}

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)