[PATCH] D124115: [Attributes] Update Attribute::get API to consider zero value for int attributes

Anna Thomas via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 20 12:26:50 PDT 2022


anna created this revision.
anna added reviewers: nikic, jdoerfert, Tyker.
Herald added subscribers: dexonsmith, hiraditya.
Herald added a project: All.
anna requested review of this revision.
Herald added a reviewer: sstefan1.
Herald added a reviewer: baziotis.
Herald added a project: LLVM.

This patch allows Attribute::get API to consider zero for the uint64_t
argument passed in for int attributes.
Until now, the implicit assumption was that if the value is zero, then
it is an enum attribute. However, we can have cases (such a case exists
downstream for us), where an int attribute can have a zero value.

I initially tried Optional<uint64_t> instead of adding an extra default
argument. However there are places in Attributor where we pass in an enum
attribute with a zero value (see IRPosition::getAttrsFromAssumes). So the
alternate approach is recognize such cases and pass in `None` as the optional
argument.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D124115

Files:
  llvm/include/llvm/IR/Attributes.h
  llvm/lib/IR/Attributes.cpp


Index: llvm/lib/IR/Attributes.cpp
===================================================================
--- llvm/lib/IR/Attributes.cpp
+++ llvm/lib/IR/Attributes.cpp
@@ -89,8 +89,9 @@
 }
 
 Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
-                         uint64_t Val) {
-  if (Val)
+                         uint64_t Val, bool ConsiderZeroValueForIntAttr) {
+  const bool isIntAttr = Val || ConsiderZeroValueForIntAttr;
+  if (isIntAttr)
     assert(Attribute::isIntAttrKind(Kind) && "Not an int attribute");
   else
     assert(Attribute::isEnumAttrKind(Kind) && "Not an enum attribute");
@@ -98,7 +99,7 @@
   LLVMContextImpl *pImpl = Context.pImpl;
   FoldingSetNodeID ID;
   ID.AddInteger(Kind);
-  if (Val) ID.AddInteger(Val);
+  if (isIntAttr) ID.AddInteger(Val);
 
   void *InsertPoint;
   AttributeImpl *PA = pImpl->AttrsSet.FindNodeOrInsertPos(ID, InsertPoint);
@@ -106,7 +107,7 @@
   if (!PA) {
     // If we didn't find any existing attributes of the same shape then create a
     // new one and insert it.
-    if (!Val)
+    if (!isIntAttr)
       PA = new (pImpl->Alloc) EnumAttributeImpl(Kind);
     else
       PA = new (pImpl->Alloc) IntAttributeImpl(Kind, Val);
Index: llvm/include/llvm/IR/Attributes.h
===================================================================
--- llvm/include/llvm/IR/Attributes.h
+++ llvm/include/llvm/IR/Attributes.h
@@ -108,7 +108,12 @@
   //===--------------------------------------------------------------------===//
 
   /// Return a uniquified Attribute object.
-  static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val = 0);
+  /// Val has a default value of zero which implies the AttrKind is enum.
+  /// If ConsiderZeroValueForIntAttr is true, then Val (even if zero) is
+  /// considered as the value for the attribute, which should be an Int
+  /// attribute.
+  static Attribute get(LLVMContext &Context, AttrKind Kind,
+                       uint64_t Val = 0, bool ConsiderZeroValueForIntAttr = false);
   static Attribute get(LLVMContext &Context, StringRef Kind,
                        StringRef Val = StringRef());
   static Attribute get(LLVMContext &Context, AttrKind Kind, Type *Ty);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124115.423995.patch
Type: text/x-patch
Size: 2201 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220420/52766b9a/attachment.bin>


More information about the llvm-commits mailing list