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,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
315323void ComputedShaderFlags::print (raw_ostream &OS) const {
0 commit comments