[PATCH] D122821: CallBase: fix getFnAttr so it also checks the function

Augie Fackler via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 31 07:46:34 PDT 2022


durin42 created this revision.
Herald added a subscriber: dexonsmith.
Herald added a project: All.
durin42 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Prior to this change, CallBase::hasFnAttr checked the called function to
see if it had an attribute if it wasn't set on the CallBase, but
getFnAttr didn't do the same delegation, which led to very confusing
behavior. This patch fixes the issue by making CallBase::getFnAttr also
check the function under the same circumstances.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122821

Files:
  llvm/include/llvm/IR/InstrTypes.h


Index: llvm/include/llvm/IR/InstrTypes.h
===================================================================
--- llvm/include/llvm/IR/InstrTypes.h
+++ llvm/include/llvm/IR/InstrTypes.h
@@ -25,6 +25,7 @@
 #include "llvm/ADT/iterator_range.h"
 #include "llvm/IR/Attributes.h"
 #include "llvm/IR/CallingConv.h"
+#include "llvm/IR/Constants.h"
 #include "llvm/IR/DerivedTypes.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/Instruction.h"
@@ -1613,12 +1614,16 @@
 
   /// Get the attribute of a given kind for the function.
   Attribute getFnAttr(StringRef Kind) const {
-    return getAttributes().getFnAttr(Kind);
+    if (Attrs.hasFnAttr(Kind))
+      return getAttributes().getFnAttr(Kind);
+    return getFnAttrOnCalledFunction(Kind);
   }
 
   /// Get the attribute of a given kind for the function.
   Attribute getFnAttr(Attribute::AttrKind Kind) const {
-    return getAttributes().getFnAttr(Kind);
+    if (Attrs.hasFnAttr(Kind))
+      return getAttributes().getFnAttr(Kind);
+    return getFnAttrOnCalledFunction(Kind);
   }
 
   /// Get the attribute of a given kind from a given arg
@@ -2300,6 +2305,22 @@
   bool hasFnAttrOnCalledFunction(Attribute::AttrKind Kind) const;
   bool hasFnAttrOnCalledFunction(StringRef Kind) const;
 
+  template <typename AK> Attribute getFnAttrOnCalledFunction(AK Kind) const {
+    // Operand bundles override attributes on the called function, but don't
+    // override attributes directly present on the call instruction.
+    if (isFnAttrDisallowedByOpBundle(Kind))
+      return Attribute();
+    Value *V = getCalledOperand();
+    if (auto *CE = dyn_cast<ConstantExpr>(V))
+      if (CE->getOpcode() == BitCast)
+        V = CE->getOperand(0);
+
+    if (auto *F = dyn_cast<Function>(V))
+      return F->getAttributes().getFnAttr(Kind);
+
+    return Attribute();
+  }
+
   template <typename AttrKind> bool hasFnAttrImpl(AttrKind Kind) const {
     if (Attrs.hasFnAttr(Kind))
       return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122821.419457.patch
Type: text/x-patch
Size: 1954 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220331/9d06f321/attachment.bin>


More information about the llvm-commits mailing list