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

S. Bharadwaj Yadavalli via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 6 11:51:20 PST 2024


================
@@ -63,17 +114,63 @@ void ComputedShaderFlags::print(raw_ostream &OS) const {
   OS << ";\n";
 }
 
+/// Insert the pair <Func, FlagMask> into the sorted vector
+/// FunctionFlags. The insertion is expected to be in-order and hence
+/// is done at the end of the already sorted list.
+void DXILModuleShaderFlagsInfo::insertInorderFunctionFlags(
+    const Function *Func, ComputedShaderFlags FlagMask) {
+  FunctionFlags.push_back({Func, FlagMask});
+}
+
+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) {
+  return computeFlags(M);
+}
+
+bool ShaderFlagsAnalysisWrapper::runOnModule(Module &M) {
+  MSFI = computeFlags(M);
+  return false;
 }
 
 PreservedAnalyses ShaderFlagsAnalysisPrinter::run(Module &M,
                                                   ModuleAnalysisManager &AM) {
-  ComputedShaderFlags Flags = AM.getResult<ShaderFlagsAnalysis>(M);
-  Flags.print(OS);
+  DXILModuleShaderFlagsInfo FlagsInfo = AM.getResult<ShaderFlagsAnalysis>(M);
+  OS << "; Shader Flags mask for Module:\n";
+  FlagsInfo.getModuleFlags().print(OS);
+  for (const auto &F : M.getFunctionList()) {
+    if (F.isDeclaration())
+      continue;
+    OS << "; Shader Flags mask for Function: " << F.getName() << "\n";
+    auto SFMask = FlagsInfo.getShaderFlagsMask(&F);
+    if (Error E = SFMask.takeError()) {
+      M.getContext().diagnose(
+          DiagnosticInfoShaderFlags(M, toString(std::move(E))));
+    }
----------------
bharadwajy wrote:

> I'm a bit confused by the runtime error handling here. Is there any scenario where the absence of the shader flags for a function _isn't_ a bug in our code? 

The API `DXILModuleShaderFlagsInfo::getShaderFlagsMask(const Function *Func)` is designed to return a result of type `Expected<const ComputedShaderFlags&>` to accommodate for returning `Error` when shader flags mask for `Func` is not found. While its call _here_ is not expected to fail, `takeError()` is called on `SFMask` to perform the required check before accessing the result.  Hence the error check.

> If so, wouldn't we want to fail fast here rather than carry on doing work in a broken state?

The call  `M.getContext().diagnose(DiagnosticInfoShaderFlags(M, toString(std::move(E))));` results in immediate exit upon printing the diagnostic message.

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


More information about the llvm-commits mailing list