3232using namespace llvm ;
3333using 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,73 @@ void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,
207207 }
208208}
209209
210+ // / Set shader flags that apply to all functions within the module
211+ ComputedShaderFlags
212+ ModuleShaderFlags::gatherGlobalModuleFlags (const Module &M,
213+ const DXILResourceMap &DRM,
214+ const ModuleMetadataInfo &MMDI) {
215+
216+ ComputedShaderFlags CSF;
217+
218+ // Set DisableOptimizations flag based on the presence of OptimizeNone
219+ // attribute of entry functions.
220+ if (MMDI.EntryPropertyVec .size () > 0 ) {
221+ CSF.DisableOptimizations = MMDI.EntryPropertyVec [0 ].Entry ->hasFnAttribute (
222+ llvm::Attribute::OptimizeNone);
223+ // Ensure all entry functions have the same optimization attribute
224+ for (const auto &EntryFunProps : MMDI.EntryPropertyVec )
225+ if (CSF.DisableOptimizations !=
226+ EntryFunProps.Entry ->hasFnAttribute (llvm::Attribute::OptimizeNone))
227+ EntryFunProps.Entry ->getContext ().diagnose (DiagnosticInfoUnsupported (
228+ *(EntryFunProps.Entry ), " Inconsistent optnone attribute " ));
229+ }
230+
231+ CSF.UAVsAtEveryStage = hasUAVsAtEveryStage (DRM, MMDI);
232+
233+ // Set the Max64UAVs flag if the number of UAVs is > 8
234+ uint32_t NumUAVs = 0 ;
235+ for (auto &UAV : DRM.uavs ())
236+ if (MMDI.ValidatorVersion < VersionTuple (1 , 6 ))
237+ NumUAVs++;
238+ else // MMDI.ValidatorVersion >= VersionTuple(1, 6)
239+ NumUAVs += UAV.getBinding ().Size ;
240+ if (NumUAVs > 8 )
241+ CSF.Max64UAVs = true ;
242+
243+ // Set the module flag that enables native low-precision execution mode.
244+ // NativeLowPrecisionMode can only be set when the command line option
245+ // -enable-16bit-types is provided. This is indicated by the dx.nativelowprec
246+ // module flag being set
247+ // This flag is needed even if the module does not use 16-bit types because a
248+ // corresponding debug module may include 16-bit types, and tools that use the
249+ // debug module may expect it to have the same flags as the original
250+ if (auto *NativeLowPrec = mdconst::extract_or_null<ConstantInt>(
251+ M.getModuleFlag (" dx.nativelowprec" )))
252+ if (MMDI.ShaderModelVersion >= VersionTuple (6 , 2 ))
253+ CSF.NativeLowPrecisionMode = NativeLowPrec->getValue ().getBoolValue ();
254+
255+ // Set ResMayNotAlias to true if DXIL validator version < 1.8 and there
256+ // are UAVs present globally.
257+ if (CanSetResMayNotAlias && MMDI.ValidatorVersion < VersionTuple (1 , 8 ))
258+ CSF.ResMayNotAlias = !DRM.uavs ().empty ();
259+
260+ return CSF;
261+ }
262+
210263// / Construct ModuleShaderFlags for module Module M
211264void ModuleShaderFlags::initialize (Module &M, DXILResourceTypeMap &DRTM,
212- DXILResourceMap &DRM,
265+ const DXILResourceMap &DRM,
213266 const ModuleMetadataInfo &MMDI) {
214267
215268 CanSetResMayNotAlias = MMDI.DXILVersion >= VersionTuple (1 , 7 );
216269 // The command line option -res-may-alias will set the dx.resmayalias module
217270 // flag to 1, thereby disabling the ability to set the ResMayNotAlias flag
218271 if (auto *ResMayAlias = mdconst::extract_or_null<ConstantInt>(
219272 M.getModuleFlag (" dx.resmayalias" )))
220- CanSetResMayNotAlias = !ResMayAlias->getValue ().getBoolValue ();
273+ if (ResMayAlias->getValue ().getBoolValue ())
274+ CanSetResMayNotAlias = false ;
221275
222- // NativeLowPrecisionMode can only be set when the command line option
223- // -enable-16bit-types is provided. This is indicated by the dx.nativelowprec
224- // module flag being set
225- CanSetNativeLowPrecisionMode = false ;
226- if (auto *NativeLowPrec = mdconst::extract_or_null<ConstantInt>(
227- M.getModuleFlag (" dx.nativelowprec" )))
228- if (MMDI.ShaderModelVersion >= VersionTuple (6 , 2 ))
229- CanSetNativeLowPrecisionMode = NativeLowPrec->getValue ().getBoolValue ();
276+ ComputedShaderFlags GlobalSFMask = gatherGlobalModuleFlags (M, DRM, MMDI);
230277
231278 CallGraph CG (M);
232279
@@ -252,7 +299,7 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
252299 continue ;
253300 }
254301
255- ComputedShaderFlags CSF;
302+ ComputedShaderFlags CSF = GlobalSFMask ;
256303 for (const auto &BB : *F)
257304 for (const auto &I : BB)
258305 updateFunctionFlags (CSF, I, DRTM, MMDI);
@@ -273,43 +320,6 @@ void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
273320 // Merge SCCSF with that of F
274321 FunctionFlags[F].merge (SCCSF);
275322 }
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);
313323}
314324
315325void ComputedShaderFlags::print (raw_ostream &OS) const {
0 commit comments