[llvm] [DirectX] Infrastructure to collect shader flags for each function (PR #112967)

Damyan Pepper via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 14 16:48:54 PST 2024


================
@@ -63,17 +105,57 @@ void ComputedShaderFlags::print(raw_ostream &OS) const {
   OS << ";\n";
 }
 
+const SmallVector<std::pair<Function const *, ComputedShaderFlags>> &
+DXILModuleShaderFlagsInfo::getFunctionFlags() const {
+  return FunctionFlags;
+}
+
+const ComputedShaderFlags &DXILModuleShaderFlagsInfo::getModuleFlags() const {
+  return ModuleFlags;
+}
+
+Expected<const ComputedShaderFlags &>
+DXILModuleShaderFlagsInfo::getShaderFlagsMask(const Function *Func) const {
+  std::pair<Function const *, ComputedShaderFlags> V{Func, {}};
+  const auto Iter = llvm::lower_bound(FunctionFlags, V);
+  if (Iter == FunctionFlags.end() || Iter->first != Func) {
+    return createStringError("Shader Flags information of Function '" +
+                             Func->getName() + "' not found");
+  }
+  return Iter->second;
+}
+
 AnalysisKey ShaderFlagsAnalysis::Key;
 
-ComputedShaderFlags ShaderFlagsAnalysis::run(Module &M,
-                                             ModuleAnalysisManager &AM) {
-  return ComputedShaderFlags::computeFlags(M);
+DXILModuleShaderFlagsInfo ShaderFlagsAnalysis::run(Module &M,
+                                                   ModuleAnalysisManager &AM) {
+  DXILModuleShaderFlagsInfo MSFI;
+  MSFI.initialize(M);
+  return MSFI;
+}
+
+bool ShaderFlagsAnalysisWrapper::runOnModule(Module &M) {
+  MSFI.initialize(M);
----------------
damyanp wrote:

We talked a bit about this a bit previously, and it is something that I tend to worry a lot about, but I think we should generally prefer to use a constructor rather than a construct/initialize pattern if there's no way for initialization to fail.

Do we also need to protect against `ShaderFlagsAnalysisWrapper::getShaderFlags` being called before `runOnModule`, at least with an assert?  If so then maybe make MSFI be `std::optional<DXILModuleShaderFlagsInfo>` would be a way to let you write that assert.

https://github.com/llvm/llvm-project/pull/112967


More information about the llvm-commits mailing list