Skip to content

Commit 9290ccc

Browse files
author
serge-sans-paille
committed
Introduce the AttributeMask class
This class is solely used as a lightweight and clean way to build a set of attributes to be removed from an AttrBuilder. Previously AttrBuilder was used both for building and removing, which introduced odd situation like creation of Attribute with dummy value because the only relevant part was the attribute kind. Differential Revision: https://reviews.llvm.org/D116110
1 parent 2b1c38f commit 9290ccc

17 files changed

+153
-83
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2097,7 +2097,7 @@ void CodeGenModule::setNonAliasAttributes(GlobalDecl GD,
20972097
// We know that GetCPUAndFeaturesAttributes will always have the
20982098
// newest set, since it has the newest possible FunctionDecl, so the
20992099
// new ones should replace the old.
2100-
llvm::AttrBuilder RemoveAttrs;
2100+
llvm::AttributeMask RemoveAttrs;
21012101
RemoveAttrs.addAttribute("target-cpu");
21022102
RemoveAttrs.addAttribute("target-features");
21032103
RemoveAttrs.addAttribute("tune-cpu");

llvm/include/llvm/IR/Argument.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class Argument final : public Value {
162162
/// Remove attributes from an argument.
163163
void removeAttr(Attribute::AttrKind Kind);
164164

165-
void removeAttrs(const AttrBuilder &B);
165+
void removeAttrs(const AttributeMask &AM);
166166

167167
/// Check if an argument has a given attribute.
168168
bool hasAttribute(Attribute::AttrKind Kind) const;

llvm/include/llvm/IR/Attributes.h

Lines changed: 84 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828
#include <cassert>
2929
#include <cstdint>
3030
#include <map>
31+
#include <set>
3132
#include <string>
3233
#include <utility>
3334

3435
namespace llvm {
3536

3637
class AttrBuilder;
38+
class AttributeMask;
3739
class AttributeImpl;
3840
class AttributeListImpl;
3941
class AttributeSetNode;
@@ -320,7 +322,7 @@ class AttributeSet {
320322
/// Remove the specified attributes from this set. Returns a new set because
321323
/// attribute sets are immutable.
322324
LLVM_NODISCARD AttributeSet
323-
removeAttributes(LLVMContext &C, const AttrBuilder &AttrsToRemove) const;
325+
removeAttributes(LLVMContext &C, const AttributeMask &AttrsToRemove) const;
324326

325327
/// Return the number of attributes in this set.
326328
unsigned getNumAttributes() const;
@@ -580,7 +582,7 @@ class AttributeList {
580582
/// Remove the specified attributes at the specified index from this
581583
/// attribute list. Returns a new list because attribute lists are immutable.
582584
LLVM_NODISCARD AttributeList removeAttributesAtIndex(
583-
LLVMContext &C, unsigned Index, const AttrBuilder &AttrsToRemove) const;
585+
LLVMContext &C, unsigned Index, const AttributeMask &AttrsToRemove) const;
584586

585587
/// Remove all attributes at the specified index from this
586588
/// attribute list. Returns a new list because attribute lists are immutable.
@@ -604,7 +606,7 @@ class AttributeList {
604606
/// Remove the specified attribute at the function index from this
605607
/// attribute list. Returns a new list because attribute lists are immutable.
606608
LLVM_NODISCARD AttributeList
607-
removeFnAttributes(LLVMContext &C, const AttrBuilder &AttrsToRemove) const {
609+
removeFnAttributes(LLVMContext &C, const AttributeMask &AttrsToRemove) const {
608610
return removeAttributesAtIndex(C, FunctionIndex, AttrsToRemove);
609611
}
610612

@@ -630,8 +632,8 @@ class AttributeList {
630632

631633
/// Remove the specified attribute at the return value index from this
632634
/// attribute list. Returns a new list because attribute lists are immutable.
633-
LLVM_NODISCARD AttributeList
634-
removeRetAttributes(LLVMContext &C, const AttrBuilder &AttrsToRemove) const {
635+
LLVM_NODISCARD AttributeList removeRetAttributes(
636+
LLVMContext &C, const AttributeMask &AttrsToRemove) const {
635637
return removeAttributesAtIndex(C, ReturnIndex, AttrsToRemove);
636638
}
637639

@@ -652,8 +654,9 @@ class AttributeList {
652654

653655
/// Remove the specified attribute at the specified arg index from this
654656
/// attribute list. Returns a new list because attribute lists are immutable.
655-
LLVM_NODISCARD AttributeList removeParamAttributes(
656-
LLVMContext &C, unsigned ArgNo, const AttrBuilder &AttrsToRemove) const {
657+
LLVM_NODISCARD AttributeList
658+
removeParamAttributes(LLVMContext &C, unsigned ArgNo,
659+
const AttributeMask &AttrsToRemove) const {
657660
return removeAttributesAtIndex(C, ArgNo + FirstArgIndex, AttrsToRemove);
658661
}
659662

@@ -927,6 +930,65 @@ template <> struct DenseMapInfo<AttributeList, void> {
927930
}
928931
};
929932

933+
//===----------------------------------------------------------------------===//
934+
/// \class
935+
/// This class stores enough information to efficiently remove some attributes
936+
/// from an existing AttrBuilder, AttributeSet or AttributeList.
937+
class AttributeMask {
938+
std::bitset<Attribute::EndAttrKinds> Attrs;
939+
std::set<SmallString<32>, std::less<>> TargetDepAttrs;
940+
941+
public:
942+
AttributeMask() = default;
943+
AttributeMask(const AttributeMask &) = delete;
944+
AttributeMask(AttributeMask &&) = default;
945+
946+
AttributeMask(AttributeSet AS) {
947+
for (Attribute A : AS)
948+
addAttribute(A);
949+
}
950+
951+
/// Add an attribute to the mask.
952+
AttributeMask &addAttribute(Attribute::AttrKind Val) {
953+
assert((unsigned)Val < Attribute::EndAttrKinds &&
954+
"Attribute out of range!");
955+
Attrs[Val] = true;
956+
return *this;
957+
}
958+
959+
/// Add the Attribute object to the builder.
960+
AttributeMask &addAttribute(Attribute A) {
961+
if (A.isStringAttribute())
962+
addAttribute(A.getKindAsString());
963+
else
964+
addAttribute(A.getKindAsEnum());
965+
return *this;
966+
}
967+
968+
/// Add the target-dependent attribute to the builder.
969+
AttributeMask &addAttribute(StringRef A) {
970+
TargetDepAttrs.insert(A);
971+
return *this;
972+
}
973+
974+
/// Return true if the builder has the specified attribute.
975+
bool contains(Attribute::AttrKind A) const {
976+
assert((unsigned)A < Attribute::EndAttrKinds && "Attribute out of range!");
977+
return Attrs[A];
978+
}
979+
980+
/// Return true if the builder has the specified target-dependent
981+
/// attribute.
982+
bool contains(StringRef A) const { return TargetDepAttrs.count(A); }
983+
984+
using td_const_iterator = decltype(TargetDepAttrs)::const_iterator;
985+
using td_const_range = iterator_range<td_const_iterator>;
986+
td_const_range td_attrs() const {
987+
return {TargetDepAttrs.begin(), TargetDepAttrs.end()};
988+
}
989+
auto const &attrs() const { return Attrs; }
990+
};
991+
930992
//===----------------------------------------------------------------------===//
931993
/// \class
932994
/// This class is used in conjunction with the Attribute::get method to
@@ -975,21 +1037,29 @@ class AttrBuilder {
9751037
/// Remove an attribute from the builder.
9761038
AttrBuilder &removeAttribute(Attribute::AttrKind Val);
9771039

1040+
/// Remove the target-dependent attribute from the builder.
1041+
AttrBuilder &removeAttribute(StringRef A);
1042+
1043+
/// Remove the target-dependent attribute from the builder.
1044+
AttrBuilder &removeAttribute(Attribute A) {
1045+
if (A.isStringAttribute())
1046+
return removeAttribute(A.getKindAsString());
1047+
else
1048+
return removeAttribute(A.getKindAsEnum());
1049+
}
1050+
9781051
/// Remove the attributes from the builder.
9791052
AttrBuilder &removeAttributes(AttributeList A, uint64_t WithoutIndex);
9801053

981-
/// Remove the target-dependent attribute to the builder.
982-
AttrBuilder &removeAttribute(StringRef A);
983-
9841054
/// Add the attributes from the builder.
9851055
AttrBuilder &merge(const AttrBuilder &B);
9861056

9871057
/// Remove the attributes from the builder.
988-
AttrBuilder &remove(const AttrBuilder &B);
1058+
AttrBuilder &remove(const AttributeMask &AM);
9891059

9901060
/// Return true if the builder has any attribute that's in the
9911061
/// specified builder.
992-
bool overlaps(const AttrBuilder &B) const;
1062+
bool overlaps(const AttributeMask &AM) const;
9931063

9941064
/// Return true if the builder has the specified attribute.
9951065
bool contains(Attribute::AttrKind A) const {
@@ -1168,14 +1238,14 @@ class AttrBuilder {
11681238
namespace AttributeFuncs {
11691239

11701240
/// Which attributes cannot be applied to a type.
1171-
AttrBuilder typeIncompatible(Type *Ty);
1241+
AttributeMask typeIncompatible(Type *Ty);
11721242

11731243
/// Get param/return attributes which imply immediate undefined behavior if an
11741244
/// invalid value is passed. For example, this includes noundef (where undef
11751245
/// implies UB), but not nonnull (where null implies poison). It also does not
11761246
/// include attributes like nocapture, which constrain the function
11771247
/// implementation rather than the passed value.
1178-
AttrBuilder getUBImplyingAttributes();
1248+
AttributeMask getUBImplyingAttributes();
11791249

11801250
/// \returns Return true if the two functions have compatible target-independent
11811251
/// attributes for inlining purposes.

llvm/include/llvm/IR/Function.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ class LLVM_EXTERNAL_VISIBILITY Function : public GlobalObject,
364364
/// Remove function attribute from this function.
365365
void removeFnAttr(StringRef Kind);
366366

367-
void removeFnAttrs(const AttrBuilder &Attrs);
367+
void removeFnAttrs(const AttributeMask &Attrs);
368368

369369
/// removes the attribute from the return value list of attributes.
370370
void removeRetAttr(Attribute::AttrKind Kind);
@@ -373,7 +373,7 @@ class LLVM_EXTERNAL_VISIBILITY Function : public GlobalObject,
373373
void removeRetAttr(StringRef Kind);
374374

375375
/// removes the attributes from the return value list of attributes.
376-
void removeRetAttrs(const AttrBuilder &Attrs);
376+
void removeRetAttrs(const AttributeMask &Attrs);
377377

378378
/// removes the attribute from the list of attributes.
379379
void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);
@@ -382,7 +382,7 @@ class LLVM_EXTERNAL_VISIBILITY Function : public GlobalObject,
382382
void removeParamAttr(unsigned ArgNo, StringRef Kind);
383383

384384
/// removes the attribute from the list of attributes.
385-
void removeParamAttrs(unsigned ArgNo, const AttrBuilder &Attrs);
385+
void removeParamAttrs(unsigned ArgNo, const AttributeMask &Attrs);
386386

387387
/// Return true if the function has the attribute.
388388
bool hasFnAttribute(Attribute::AttrKind Kind) const;

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,7 @@ class CallBase : public Instruction {
15441544
}
15451545

15461546
/// Removes the attributes from the function
1547-
void removeFnAttrs(const AttrBuilder &AttrsToRemove) {
1547+
void removeFnAttrs(const AttributeMask &AttrsToRemove) {
15481548
Attrs = Attrs.removeFnAttributes(getContext(), AttrsToRemove);
15491549
}
15501550

@@ -1559,7 +1559,7 @@ class CallBase : public Instruction {
15591559
}
15601560

15611561
/// Removes the attributes from the return value
1562-
void removeRetAttrs(const AttrBuilder &AttrsToRemove) {
1562+
void removeRetAttrs(const AttributeMask &AttrsToRemove) {
15631563
Attrs = Attrs.removeRetAttributes(getContext(), AttrsToRemove);
15641564
}
15651565

@@ -1576,7 +1576,7 @@ class CallBase : public Instruction {
15761576
}
15771577

15781578
/// Removes the attributes from the given argument
1579-
void removeParamAttrs(unsigned ArgNo, const AttrBuilder &AttrsToRemove) {
1579+
void removeParamAttrs(unsigned ArgNo, const AttributeMask &AttrsToRemove) {
15801580
Attrs = Attrs.removeParamAttributes(getContext(), ArgNo, AttrsToRemove);
15811581
}
15821582

0 commit comments

Comments
 (0)