[llvm] [DXIL] Consume Metadata Analysis information in passes (PR #108034)

S. Bharadwaj Yadavalli via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 23 09:02:59 PDT 2024


================
@@ -247,9 +247,10 @@ class DXILPrepareModule : public ModulePass {
 
   DXILPrepareModule() : ModulePass(ID) {}
   void getAnalysisUsage(AnalysisUsage &AU) const override {
+    AU.setPreservesAll();
     AU.addPreserved<ShaderFlagsAnalysisWrapper>();
     AU.addPreserved<DXILResourceMDWrapper>();
-    AU.addPreserved<DXILMetadataAnalysisWrapperPass>();
+    AU.addRequired<DXILMetadataAnalysisWrapperPass>();
----------------
bharadwajy wrote:

> I still don't understand why we would change this to `setPreservesAll`. If you just need to preserve `DXILMetadataAnalysis` then the existing `AU.addPreserved<DXILMetadataAnalysisWrapperPass>()` call should do that, no?


If `DXILMetadataAnalysis` pass were not specified as required by any pass running after `DXILTranslateMetadata`, it would be freed (along with any others that are similarly not required by later passes) after `DXILTraslateMetadata`. In the current pass order of `llc` invocation, `DXILPrepare` runs after `DXILTranslateMetadata` and does not specify `DXILMetadataAnalysis` as required, prior to this change. So, the existing `AU.addPreserved<DXILMetadataAnalysisWrapperPass>()` call in `DXILPrepare::getAnalysisUsage()` that would only preserve analysis info, if it exists - does not preserve anything. See ouput at the end of this note [1]. Hence, the need to specify `DXILMetadataAnalysis` as required using `AU.addRequired<DXILMetadataAnalysisWrapperPass>()`.

Regarding the need for calling `AU.setPreservesAll()`:  `DXILMetadataAnalysis` can be preserved in `DXILPrepare` pass for use in later passes that require it - such as DXIL Global Emitter - by adding a fine-grained call to `AU.addPreserved<DXILMetadataAnalysisWrapperPass>()` instead of `AU.SetPreservesAll()` in `DXILPrepare::getAnalysisusage()`. However, that would free `DXIL Resource analysis` and `DXIL resource Information` pass info needed for later passes. Hence the coarse-grained call `AU.setPreservesAll()` preserving `DXILMetadataAnalysis` along with other pass info is used.


A finer-grained control of pass requirements and preservation may be achieved with the following change that avoids the coarse-grained call `AU.setPreservesAll()`, to both `DXILPrepare` and `DXILMetadataAnalysis` passes and may be considered in a follow-on PR, if there is agreement.

```
diff --git a/llvm/lib/Target/DirectX/DXILPrepare.cpp b/llvm/lib/Target/DirectX/DXILPrepare.cpp
index 1a766c5fb7b4..93d3214bde06 100644
--- a/llvm/lib/Target/DirectX/DXILPrepare.cpp
+++ b/llvm/lib/Target/DirectX/DXILPrepare.cpp
@@ -247,8 +247,10 @@ public:
 
   DXILPrepareModule() : ModulePass(ID) {}
   void getAnalysisUsage(AnalysisUsage &AU) const override {
-    AU.setPreservesAll();
     AU.addRequired<DXILMetadataAnalysisWrapperPass>();
+    AU.addPreserved<DXILResourceMDWrapper>();
+    AU.addPreserved<DXILResourceWrapperPass>();
+    AU.addPreserved<DXILMetadataAnalysisWrapperPass>();
   }
   static char ID; // Pass identification.
 };
diff --git a/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp b/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
index bea82ccf6006..be370e10df69 100644
--- a/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
+++ b/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
@@ -379,11 +379,13 @@ public:
   StringRef getPassName() const override { return "DXIL Translate Metadata"; }
 
   void getAnalysisUsage(AnalysisUsage &AU) const override {
-    AU.setPreservesAll();
     AU.addRequired<DXILResourceWrapperPass>();
     AU.addRequired<DXILResourceMDWrapper>();
     AU.addRequired<ShaderFlagsAnalysisWrapper>();
     AU.addRequired<DXILMetadataAnalysisWrapperPass>();
+    AU.addPreserved<DXILResourceWrapperPass>();
+    AU.addPreserved<DXILResourceMDWrapper>();
+    AU.addPreserved<DXILMetadataAnalysisWrapperPass>();
```

Additional info:

[1] Execution output just with `AU.addPreserved<DXILMetadataAnalysisWrapperPass>()` in `DXILPrepare::getAnalysisUsage()`

```
/Users/bharadwaj/github/llvm-build/hlsl/RelWithDebInfo/bin/llc /Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll --filetype=asm -o /dev/null --debug-pass=Details
 -- 'Scalarize vector operations' is not preserving 'DXIL Intrinsic Expansion'
 -- 'Scalarize vector operations' is not preserving 'Function Pass Manager'
 -- 'DXIL Op Lowering' is not preserving 'DXIL Intrinsic Expansion'
 -- 'DXIL Finalize Linkage' is not preserving 'DXIL Op Lowering'
 -- 'DXIL Prepare Module' is not preserving 'DXIL Finalize Linkage'
 -- 'DXIL Prepare Module' is not preserving 'DXIL Shader Flag Analysis'
 -- 'DXIL Prepare Module' is not preserving 'DXIL resource Information'
 -- 'DXIL Prepare Module' is not preserving 'DXIL Resource analysis'
 -- 'DXIL Prepare Module' is not preserving 'DXIL Translate Metadata'
Pass Arguments:  -targetlibinfo -dxil-intrinsic-expansion -domtree -scalarizer -dxil-intrinsic-expansion -dxil-resource -dxil-op-lower -dxil-finalize-linkage -dxil-resource-analysis -dx-shader-flag-analysis -dxil-metadata-analysis -dxil-translate-metadata -dxil-prepare -dxil-resource -dxil-resource-analysis -dxil-pretty-printer -print-module
Target Library Information
  ModulePass Manager
    DXIL Intrinsic Expansion
--    DXIL Intrinsic Expansion
    FunctionPass Manager
      Dominator Tree Construction
      Scalarize vector operations
--      Dominator Tree Construction
--      Scalarize vector operations
    DXIL Intrinsic Expansion
    DXIL Resource analysis
    DXIL Op Lowering
--    DXIL Intrinsic Expansion
--    DXIL Op Lowering
    DXIL Finalize Linkage
--    DXIL Finalize Linkage
    DXIL resource Information
    DXIL Shader Flag Analysis
    DXIL Module Metadata analysis
    DXIL Translate Metadata
--    DXIL Resource analysis
--    DXIL resource Information
--    DXIL Shader Flag Analysis
--    DXIL Module Metadata analysis
--    DXIL Translate Metadata
    DXIL Prepare Module
--    DXIL Prepare Module
    DXIL Resource analysis
    DXIL resource Information
    DXIL Metadata Pretty Printer
--    DXIL Resource analysis
--    DXIL resource Information
--    DXIL Metadata Pretty Printer
    Print Module IR
--    Print Module IR
[2024-09-23 11:58:57.496635000] 0x13dd08950   Executing Pass 'DXIL Intrinsic Expansion' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
[2024-09-23 11:58:57.497185000] 0x13dd08950   Made Modification 'DXIL Intrinsic Expansion' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
0x13dd09cf0     Preserved Analyses: DXIL Resource analysis
 -*- 'DXIL Intrinsic Expansion' is the last user of following pass instances. Free these instances
[2024-09-23 11:58:57.497207000] 0x13dd08950    Freeing Pass 'DXIL Intrinsic Expansion' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
[2024-09-23 11:58:57.497218000] 0x13dd08950   Executing Pass 'Function Pass Manager' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
[2024-09-23 11:58:57.497230000] 0x13dd0a7d0     Executing Pass 'Dominator Tree Construction' on Function 'fma'...
[2024-09-23 11:58:57.497244000] 0x13dd0a7d0     Executing Pass 'Scalarize vector operations' on Function 'fma'...
0x13dd09d90       Required Analyses: Dominator Tree Construction
0x13dd09d90       Preserved Analyses: Dominator Tree Construction
 -*- 'Scalarize vector operations' is the last user of following pass instances. Free these instances
[2024-09-23 11:58:57.497279000] 0x13dd0a7d0      Freeing Pass 'Dominator Tree Construction' on Function 'fma'...
[2024-09-23 11:58:57.497291000] 0x13dd0a7d0      Freeing Pass 'Scalarize vector operations' on Function 'fma'...
[2024-09-23 11:58:57.497301000] 0x13dd08950   Executing Pass 'DXIL Intrinsic Expansion' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
[2024-09-23 11:58:57.497313000] 0x13dd08950   Made Modification 'DXIL Intrinsic Expansion' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
0x13dd0a3e0     Preserved Analyses: DXIL Resource analysis
 -- 'DXIL Intrinsic Expansion' is not preserving 'Function Pass Manager'
[2024-09-23 11:58:57.497333000] 0x13dd08950   Executing Pass 'DXIL Resource analysis' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
[2024-09-23 11:58:57.497345000] 0x13dd08950   Executing Pass 'DXIL Op Lowering' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
0x13dd0a3c0     Required Analyses: DXIL Intrinsic Expansion, DXIL Resource analysis
0x13dd0a3c0     Preserved Analyses: DXIL Resource analysis
 -*- 'DXIL Op Lowering' is the last user of following pass instances. Free these instances
[2024-09-23 11:58:57.497378000] 0x13dd08950    Freeing Pass 'DXIL Intrinsic Expansion' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
[2024-09-23 11:58:57.497389000] 0x13dd08950    Freeing Pass 'DXIL Op Lowering' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
[2024-09-23 11:58:57.497399000] 0x13dd08950   Executing Pass 'DXIL Finalize Linkage' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
0x13dd0a9b0     Preserved Analyses: DXIL Resource analysis
 -*- 'DXIL Finalize Linkage' is the last user of following pass instances. Free these instances
[2024-09-23 11:58:57.497419000] 0x13dd08950    Freeing Pass 'DXIL Finalize Linkage' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
[2024-09-23 11:58:57.497429000] 0x13dd08950   Executing Pass 'DXIL resource Information' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
[2024-09-23 11:58:57.497441000] 0x13dd08950   Executing Pass 'DXIL Shader Flag Analysis' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
[2024-09-23 11:58:57.497453000] 0x13dd08950   Executing Pass 'DXIL Module Metadata analysis' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
[2024-09-23 11:58:57.497466000] 0x13dd08950   Executing Pass 'DXIL Translate Metadata' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
0x13dd0a9f0     Required Analyses: DXIL Resource analysis, DXIL resource Information, DXIL Shader Flag Analysis, DXIL Module Metadata analysis
[2024-09-23 11:58:57.497524000] 0x13dd08950   Made Modification 'DXIL Translate Metadata' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
 -*- 'DXIL Translate Metadata' is the last user of following pass instances. Free these instances
[2024-09-23 11:58:57.497540000] 0x13dd08950    Freeing Pass 'DXIL Resource analysis' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
[2024-09-23 11:58:57.497551000] 0x13dd08950    Freeing Pass 'DXIL resource Information' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
[2024-09-23 11:58:57.497561000] 0x13dd08950    Freeing Pass 'DXIL Shader Flag Analysis' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
[2024-09-23 11:58:57.497572000] 0x13dd08950    Freeing Pass 'DXIL Module Metadata analysis' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
[2024-09-23 11:58:57.497583000] 0x13dd08950    Freeing Pass 'DXIL Translate Metadata' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
[2024-09-23 11:58:57.497594000] 0x13dd08950   Executing Pass 'DXIL Prepare Module' on Module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'...
Assertion failed: (ResultPass && "getAnalysis*() called on an analysis that was not " "'required' by pass!"), function getAnalysisID, file PassAnalysisSupport.h, line 245.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /Users/bharadwaj/github/llvm-build/hlsl/RelWithDebInfo/bin/llc /Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll --filetype=asm -o /dev/null --debug-pass=Details
1.      Running pass 'DXIL Prepare Module' on module '/Users/bharadwaj/github/llvm-project/llvm/test/CodeGen/DirectX/strip-fn-attrs.ll'.
 #0 0x0000000106021ad8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/Users/bharadwaj/github/llvm-build/hlsl/RelWithDebInfo/bin/llc+0x101481ad8)
 <...>
```

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


More information about the llvm-commits mailing list