Skip to content

Commit 91bbf4b

Browse files
committed
Set whole-module flags prior to evaluating per-function flags
1 parent db099f1 commit 91bbf4b

11 files changed

+79
-69
lines changed

llvm/lib/Target/DirectX/DXILShaderFlags.cpp

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
using namespace llvm;
3333
using namespace llvm::dxil;
3434

35-
static bool hasUAVsAtEveryStage(DXILResourceMap &DRM,
35+
static bool hasUAVsAtEveryStage(const DXILResourceMap &DRM,
3636
const ModuleMetadataInfo &MMDI) {
3737
if (DRM.uavs().empty())
3838
return false;
@@ -143,7 +143,7 @@ void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,
143143
}
144144

145145
if (CSF.LowPrecisionPresent) {
146-
if (CanSetNativeLowPrecisionMode)
146+
if (CSF.NativeLowPrecisionMode)
147147
CSF.NativeLowPrecision = true;
148148
else
149149
CSF.MinimumPrecision = true;
@@ -207,26 +207,71 @@ void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,
207207
}
208208
}
209209

210-
/// Construct ModuleShaderFlags for module Module M
211-
void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
212-
DXILResourceMap &DRM,
213-
const ModuleMetadataInfo &MMDI) {
210+
/// Set shader flags that apply to all functions within the module
211+
void ModuleShaderFlags::gatherGlobalModuleFlags(
212+
ComputedShaderFlags &CSF, const Module &M, const DXILResourceMap &DRM,
213+
const ModuleMetadataInfo &MMDI) {
214214

215-
CanSetResMayNotAlias = MMDI.DXILVersion >= VersionTuple(1, 7);
216-
// The command line option -res-may-alias will set the dx.resmayalias module
217-
// flag to 1, thereby disabling the ability to set the ResMayNotAlias flag
218-
if (auto *ResMayAlias = mdconst::extract_or_null<ConstantInt>(
219-
M.getModuleFlag("dx.resmayalias")))
220-
CanSetResMayNotAlias = !ResMayAlias->getValue().getBoolValue();
215+
// Set DisableOptimizations flag based on the presence of OptimizeNone
216+
// attribute of entry functions.
217+
if (MMDI.EntryPropertyVec.size() > 0) {
218+
CSF.DisableOptimizations =
219+
MMDI.EntryPropertyVec[0].Entry->hasFnAttribute(
220+
llvm::Attribute::OptimizeNone);
221+
// Ensure all entry functions have the same optimization attribute
222+
for (const auto &EntryFunProps : MMDI.EntryPropertyVec)
223+
if (CSF.DisableOptimizations !=
224+
EntryFunProps.Entry->hasFnAttribute(llvm::Attribute::OptimizeNone))
225+
EntryFunProps.Entry->getContext().diagnose(DiagnosticInfoUnsupported(
226+
*(EntryFunProps.Entry), "Inconsistent optnone attribute "));
227+
}
221228

229+
CSF.UAVsAtEveryStage = hasUAVsAtEveryStage(DRM, MMDI);
230+
231+
// Set the Max64UAVs flag if the number of UAVs is > 8
232+
uint32_t NumUAVs = 0;
233+
for (auto &UAV : DRM.uavs())
234+
if (MMDI.ValidatorVersion < VersionTuple(1, 6))
235+
NumUAVs++;
236+
else // MMDI.ValidatorVersion >= VersionTuple(1, 6)
237+
NumUAVs += UAV.getBinding().Size;
238+
if (NumUAVs > 8)
239+
CSF.Max64UAVs = true;
240+
241+
// Set the module flag that enables native low-precision execution mode.
222242
// NativeLowPrecisionMode can only be set when the command line option
223243
// -enable-16bit-types is provided. This is indicated by the dx.nativelowprec
224244
// module flag being set
225-
CanSetNativeLowPrecisionMode = false;
245+
// This flag is needed even if the module does not use 16-bit types because a
246+
// corresponding debug module may include 16-bit types, and tools that use the
247+
// debug module may expect it to have the same flags as the original
226248
if (auto *NativeLowPrec = mdconst::extract_or_null<ConstantInt>(
227249
M.getModuleFlag("dx.nativelowprec")))
228250
if (MMDI.ShaderModelVersion >= VersionTuple(6, 2))
229-
CanSetNativeLowPrecisionMode = NativeLowPrec->getValue().getBoolValue();
251+
CSF.NativeLowPrecisionMode = NativeLowPrec->getValue().getBoolValue();
252+
253+
CanSetResMayNotAlias = MMDI.DXILVersion >= VersionTuple(1, 7);
254+
// The command line option -res-may-alias will set the dx.resmayalias module
255+
// flag to 1, thereby disabling the ability to set the ResMayNotAlias flag
256+
if (auto *ResMayAlias = mdconst::extract_or_null<ConstantInt>(
257+
M.getModuleFlag("dx.resmayalias")))
258+
if (ResMayAlias->getValue().getBoolValue())
259+
CanSetResMayNotAlias = false;
260+
261+
// Set ResMayNotAlias to true if DXIL validator version < 1.8 and there
262+
// are UAVs present globally.
263+
if (CanSetResMayNotAlias && MMDI.ValidatorVersion < VersionTuple(1, 8))
264+
CSF.ResMayNotAlias = !DRM.uavs().empty();
265+
}
266+
267+
/// Construct ModuleShaderFlags for module Module M
268+
void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
269+
const DXILResourceMap &DRM,
270+
const ModuleMetadataInfo &MMDI) {
271+
272+
ComputedShaderFlags GlobalSFMask;
273+
gatherGlobalModuleFlags(GlobalSFMask, M, DRM, MMDI);
274+
CombinedSFMask.merge(GlobalSFMask);
230275

231276
CallGraph CG(M);
232277

@@ -252,7 +297,7 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
252297
continue;
253298
}
254299

255-
ComputedShaderFlags CSF;
300+
ComputedShaderFlags CSF = GlobalSFMask;
256301
for (const auto &BB : *F)
257302
for (const auto &I : BB)
258303
updateFunctionFlags(CSF, I, DRTM, MMDI);
@@ -273,43 +318,6 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
273318
// Merge SCCSF with that of F
274319
FunctionFlags[F].merge(SCCSF);
275320
}
276-
277-
// Set DisableOptimizations flag based on the presence of OptimizeNone
278-
// attribute of entry functions.
279-
if (MMDI.EntryPropertyVec.size() > 0) {
280-
CombinedSFMask.DisableOptimizations =
281-
MMDI.EntryPropertyVec[0].Entry->hasFnAttribute(
282-
llvm::Attribute::OptimizeNone);
283-
// Ensure all entry functions have the same optimization attribute
284-
for (const auto &EntryFunProps : MMDI.EntryPropertyVec)
285-
if (CombinedSFMask.DisableOptimizations !=
286-
EntryFunProps.Entry->hasFnAttribute(llvm::Attribute::OptimizeNone))
287-
EntryFunProps.Entry->getContext().diagnose(DiagnosticInfoUnsupported(
288-
*(EntryFunProps.Entry), "Inconsistent optnone attribute "));
289-
}
290-
291-
// Set ResMayNotAlias to true if DXIL validator version < 1.8 and there
292-
// are UAVs present globally.
293-
if (CanSetResMayNotAlias && MMDI.ValidatorVersion < VersionTuple(1, 8))
294-
CombinedSFMask.ResMayNotAlias = !DRM.uavs().empty();
295-
296-
// Set the module flag that enables native low-precision execution mode. This
297-
// is needed even if the module does not use 16-bit types because a
298-
// corresponding debug module may include 16-bit types, and tools that use the
299-
// debug module may expect it to have the same flags as the original
300-
CombinedSFMask.NativeLowPrecisionMode = CanSetNativeLowPrecisionMode;
301-
302-
// Set the Max64UAVs flag if the number of UAVs is > 8
303-
uint32_t NumUAVs = 0;
304-
for (auto &UAV : DRM.uavs())
305-
if (MMDI.ValidatorVersion < VersionTuple(1, 6))
306-
NumUAVs++;
307-
else // MMDI.ValidatorVersion >= VersionTuple(1, 6)
308-
NumUAVs += UAV.getBinding().Size;
309-
if (NumUAVs > 8)
310-
CombinedSFMask.Max64UAVs = true;
311-
312-
CombinedSFMask.UAVsAtEveryStage = hasUAVsAtEveryStage(DRM, MMDI);
313321
}
314322

315323
void ComputedShaderFlags::print(raw_ostream &OS) const {

llvm/lib/Target/DirectX/DXILShaderFlags.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,24 @@ struct ComputedShaderFlags {
8585
};
8686

8787
struct ModuleShaderFlags {
88-
void initialize(Module &, DXILResourceTypeMap &DRTM, DXILResourceMap &DRM,
89-
const ModuleMetadataInfo &MMDI);
88+
void initialize(Module &, DXILResourceTypeMap &DRTM,
89+
const DXILResourceMap &DRM, const ModuleMetadataInfo &MMDI);
9090
const ComputedShaderFlags &getFunctionFlags(const Function *) const;
9191
const ComputedShaderFlags &getCombinedFlags() const { return CombinedSFMask; }
9292

9393
private:
94-
// Booleans set by module flags
95-
bool CanSetResMayNotAlias; // dx.resmayalias
96-
bool CanSetNativeLowPrecisionMode; // dx.nativelowprec
94+
// This boolean is inversely set by the LLVM module flag dx.resmayalias to
95+
// determine whether or not the ResMayNotAlias DXIL module flag can be set
96+
bool CanSetResMayNotAlias;
9797

9898
/// Map of Function-Shader Flag Mask pairs representing properties of each of
9999
/// the functions in the module. Shader Flags of each function represent both
100100
/// module-level and function-level flags
101101
DenseMap<const Function *, ComputedShaderFlags> FunctionFlags;
102102
/// Combined Shader Flag Mask of all functions of the module
103103
ComputedShaderFlags CombinedSFMask{};
104+
void gatherGlobalModuleFlags(ComputedShaderFlags &, const Module &M, const DXILResourceMap &,
105+
const ModuleMetadataInfo &);
104106
void updateFunctionFlags(ComputedShaderFlags &, const Instruction &,
105107
DXILResourceTypeMap &, const ModuleMetadataInfo &);
106108
};

llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-cs.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
; CHECK-NEXT: ; Disable shader optimizations
99

1010
; CHECK: ; Shader Flags for Module Functions
11-
; CHECK: ; Function main : 0x00000000
11+
; CHECK: ; Function main : 0x00000001
1212
; The test source in this file generated from the following command:
1313
; clang -cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -emit-llvm -O0 -o - <<EOF
1414
; [numthreads(1,1,1)]

llvm/test/CodeGen/DirectX/ShaderFlags/disable-opt-lib.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
; CHECK-NEXT: ; Disable shader optimizations
99

1010
; CHECK: ; Shader Flags for Module Functions
11-
; CHECK: ; Function main : 0x00000000
11+
; CHECK: ; Function main : 0x00000001
1212
; The test source in this file generated from the following command:
1313
; clang -cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -emit-llvm -O0 -o - <<EOF
1414

llvm/test/CodeGen/DirectX/ShaderFlags/max-64-uavs-array-valver1.5.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ target triple = "dxil-pc-shadermodel6.7-library"
1313
; CHECK: UAVs at every shader stage
1414
; CHECK-NOT: 64 UAV slots
1515

16-
; CHECK: Function test : 0x00000000
16+
; CHECK: Function test : 0x00010000
1717
define void @test() "hlsl.export" {
1818
; RWBuffer<float> Buf : register(u0, space0)
1919
%buf0 = call target("dx.TypedBuffer", float, 1, 0, 1)

llvm/test/CodeGen/DirectX/ShaderFlags/max-64-uavs-array-valver1.6.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ target triple = "dxil-pc-shadermodel6.7-library"
1414
; CHECK: UAVs at every shader stage
1515
; CHECK: 64 UAV slots
1616

17-
; CHECK: Function test : 0x00000000
17+
; CHECK: Function test : 0x00018000
1818
define void @test() "hlsl.export" {
1919
; RWBuffer<float> Buf : register(u0, space0)
2020
%buf0 = call target("dx.TypedBuffer", float, 1, 0, 1)

llvm/test/CodeGen/DirectX/ShaderFlags/max-64-uavs.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ target triple = "dxil-pc-shadermodel6.7-library"
1313
; CHECK: 64 UAV slots
1414

1515
; Note: 64 UAV slots does not get set per-function
16-
; CHECK: Function test : 0x00000000
16+
; CHECK: Function test : 0x00008000
1717
define void @test() "hlsl.export" {
1818
; RWBuffer<float> Buf : register(u0, space0)
1919
%buf0 = call target("dx.TypedBuffer", float, 1, 0, 1)

llvm/test/CodeGen/DirectX/ShaderFlags/res-may-not-alias-sm6.7.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ target triple = "dxil-pc-shadermodel6.7-library"
1919
; CHECK: Any UAV may not alias any other UAV
2020
;
2121

22-
; CHECK: Function loadUAV : 0x00000000
22+
; CHECK: Function loadUAV : 0x200010000
2323
define float @loadUAV() #0 {
2424
%res = call target("dx.TypedBuffer", float, 1, 0, 0)
2525
@llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)
@@ -29,7 +29,7 @@ define float @loadUAV() #0 {
2929
ret float %val
3030
}
3131

32-
; CHECK: Function loadSRV : 0x00000010
32+
; CHECK: Function loadSRV : 0x200010010
3333
define float @loadSRV() #0 {
3434
%res = tail call target("dx.RawBuffer", float, 0, 0)
3535
@llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false)

llvm/test/CodeGen/DirectX/ShaderFlags/uavs-at-every-stage-lib-valver1.7.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ target triple = "dxil-pc-shadermodel6.5-library"
1212
; CHECK: Note: shader requires additional functionality:
1313
; CHECK: UAVs at every shader stage
1414

15-
; CHECK: Function test : 0x00000000
15+
; CHECK: Function test : 0x00010000
1616
define void @test() "hlsl.export" {
1717
; RWBuffer<float> Buf : register(u0, space0)
1818
%buf0 = call target("dx.TypedBuffer", float, 1, 0, 1)

llvm/test/CodeGen/DirectX/ShaderFlags/uavs-at-every-stage-vs.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ target triple = "dxil-pc-shadermodel6.5-vertex"
1313
; CHECK: Note: shader requires additional functionality:
1414
; CHECK: UAVs at every shader stage
1515

16-
; CHECK: Function VSMain : 0x00000000
16+
; CHECK: Function VSMain : 0x00010000
1717
define void @VSMain() {
1818
; RWBuffer<float> Buf : register(u0, space0)
1919
%buf0 = call target("dx.TypedBuffer", float, 1, 0, 1)

0 commit comments

Comments
 (0)