[llvm] [DXIL][Analysis] Collect Function properties in Metadata Analysis (PR #105728)

Justin Bogner via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 27 16:11:44 PDT 2024


================
@@ -33,6 +38,42 @@ static ModuleMetadataInfo collectMetadataInfo(Module &M) {
     MMDAI.ValidatorVersion =
         VersionTuple(MajorMD->getZExtValue(), MinorMD->getZExtValue());
   }
+
+  // For all HLSL Shader functions
+  for (auto &F : M.functions()) {
+    if (!F.hasFnAttribute("hlsl.shader"))
+      continue;
+
+    FunctionProperties EFP{};
+    // Get "hlsl.shader" attribute
+    Attribute EntryAttr = F.getFnAttribute("hlsl.shader");
+    StringRef EntryProfile = EntryAttr.getValueAsString();
+    Triple T("", "", "", EntryProfile);
+    EFP.ShaderStage = T.getEnvironment();
+    // Get numthreads attribute value, if one exists
+    StringRef NumThreadsStr =
+        F.getFnAttribute("hlsl.numthreads").getValueAsString();
+    if (!NumThreadsStr.empty()) {
+      SmallVector<StringRef> NumThreadsVec;
+      NumThreadsStr.split(NumThreadsVec, ',');
+      if (NumThreadsVec.size() != 3) {
+        report_fatal_error(Twine(F.getName()) +
+                               ": Invalid numthreads specified",
+                           /* gen_crash_diag */ false);
+      }
+      auto Zip =
+          llvm::zip(NumThreadsVec, MutableArrayRef<unsigned>(EFP.NumThreads));
+      for (auto It : Zip) {
+        StringRef Str = std::get<0>(It);
+        APInt V;
+        assert(!Str.getAsInteger(10, V) &&
+               "Failed to parse numthreads components as integer values");
+        unsigned &Num = std::get<1>(It);
+        Num = V.getLimitedValue();
+      }
----------------
bogner wrote:

I don't think the use of `zip` here makes this clearer than doing it the obvious way. Also the code inside an assert isn't executed in release builds, so it really shouldn't have side effects.
```suggestion
      for (int I = 0, E = 3; I != E; ++I) {
        unsigned V;
        [[maybe_unused]] bool Result = NumThreadsVec[I].getAsInteger(10, V);
        assert(!Result && "Failed to parse numthreads");
        EFP.NumThreads[I] = V;
      }
```

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


More information about the llvm-commits mailing list