[llvm] [AMDGPU] Infer amdgpu-no-flat-scratch-init attribute in AMDGPUAttributor (PR #94647)

Jun Wang via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 17 17:44:59 PDT 2024


================
@@ -677,6 +693,33 @@ struct AAAMDAttributesFunction : public AAAMDAttributes {
     return !A.checkForAllCallLikeInstructions(DoesNotRetrieve, *this,
                                               UsedAssumedInformation);
   }
+
+  // Returns true if FlatScratchInit is needed, i.e., no-flat-scratch-init is
+  // not to be set.
+  bool needFlatScratchInit(Attributor &A) {
+    // This is called on each callee; false means callee shouldn't have
+    // no-flat-scratch-init.
+    auto CheckForNoFlatScratchInit = [&](Instruction &I) {
+      const auto &CB = cast<CallBase>(I);
+      const Value *CalleeOp = CB.getCalledOperand();
+      const Function *Callee = dyn_cast<Function>(CalleeOp);
+      if (!Callee) // indirect call
+        return CB.isInlineAsm();
----------------
jwanggit86 wrote:

On the other hand, a patch where `checkForAllCallees()` is used is as follows:
```diff
   bool needFlatScratchInit(Attributor &A) {
     assert(isAssumed(FLAT_SCRATCH_INIT)); // only called if the bit is still set
     auto CheckForNoFlatScratchInit = [&](Instruction &I) {
       const auto &CB = cast<CallBase>(I);
-      const Function *Callee = CB.getCalledFunction();
-
-      if (Callee && Callee->isIntrinsic())
-        return Callee->getIntrinsicID() !=
-               Intrinsic::amdgcn_addrspacecast_nonnull;
-
+      auto PredOnCallees = [&](ArrayRef<const Function *> Callees) {
+        for (const Function *Callee : Callees) {
+          if (Callee->isIntrinsic())
+            if (Callee->getIntrinsicID() == Intrinsic::amdgcn_addrspacecast_nonnull)
+              return false;
+        }
+        return true;
+      };
+      if(!A.checkForAllCallees(PredOnCallees, *this, CB))
+        return CB.isInlineAsm();
       return true;
     };

     bool UsedAssumedInformation = false;
     return !A.checkForAllCallLikeInstructions(CheckForNoFlatScratchInit, *this,
```

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


More information about the llvm-commits mailing list