Skip to content

Commit

Permalink
Sort EnumAttr so it matches Attribute::operator<
Browse files Browse the repository at this point in the history
This means AttrBuilder will always create a sorted set of attributes and
we can skip the sorting step. Sorting attributes is surprisingly
expensive, and I recently made it worse by making it use array_pod_sort.
  • Loading branch information
d0k committed Apr 26, 2020
1 parent 39f6f29 commit ed766f1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
18 changes: 12 additions & 6 deletions llvm/include/llvm/IR/Attributes.td
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@ class Attr<string S> {
/// Enum attribute.
class EnumAttr<string S> : Attr<S>;

/// Int attribute.
class IntAttr<string S> : Attr<S>;

/// StringBool attribute.
class StrBoolAttr<string S> : Attr<S>;

/// Type attribute.
class TypeAttr<string S> : Attr<S>;

/// Target-independent enum attributes.

/// Alignment of parameter (5 bits) stored as log2 of alignment with +1 bias.
/// 0 means unaligned (different from align(1)).
def Alignment : EnumAttr<"align">;
def Alignment : IntAttr<"align">;

/// The result of the function is guaranteed to point to a number of bytes that
/// we can determine if we know the value of the function's arguments.
def AllocSize : EnumAttr<"allocsize">;
def AllocSize : IntAttr<"allocsize">;

/// inline=always.
def AlwaysInline : EnumAttr<"alwaysinline">;
Expand All @@ -31,7 +37,7 @@ def ArgMemOnly : EnumAttr<"argmemonly">;
def Builtin : EnumAttr<"builtin">;

/// Pass structure by value.
def ByVal : EnumAttr<"byval">;
def ByVal : TypeAttr<"byval">;

/// Marks function as being in a cold path.
def Cold : EnumAttr<"cold">;
Expand All @@ -40,10 +46,10 @@ def Cold : EnumAttr<"cold">;
def Convergent : EnumAttr<"convergent">;

/// Pointer is known to be dereferenceable.
def Dereferenceable : EnumAttr<"dereferenceable">;
def Dereferenceable : IntAttr<"dereferenceable">;

/// Pointer is either null or dereferenceable.
def DereferenceableOrNull : EnumAttr<"dereferenceable_or_null">;
def DereferenceableOrNull : IntAttr<"dereferenceable_or_null">;

/// Function may only access memory that is inaccessible from IR.
def InaccessibleMemOnly : EnumAttr<"inaccessiblememonly">;
Expand Down Expand Up @@ -153,7 +159,7 @@ def SExt : EnumAttr<"signext">;

/// Alignment of stack for function (3 bits) stored as log2 of alignment with
/// +1 bias 0 means unaligned (different from alignstack=(1)).
def StackAlignment : EnumAttr<"alignstack">;
def StackAlignment : IntAttr<"alignstack">;

/// Function can be speculated.
def Speculatable : EnumAttr<"speculatable">;
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/IR/AttributeImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ class AttributeSetNode final

AttributeSetNode(ArrayRef<Attribute> Attrs);

static AttributeSetNode *getSorted(LLVMContext &C,
ArrayRef<Attribute> SortedAttrs);

public:
// AttributesSetNode is uniqued, these should not be available.
AttributeSetNode(const AttributeSetNode &) = delete;
Expand Down
17 changes: 11 additions & 6 deletions llvm/lib/IR/Attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,16 +781,21 @@ AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)

AttributeSetNode *AttributeSetNode::get(LLVMContext &C,
ArrayRef<Attribute> Attrs) {
if (Attrs.empty())
SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
llvm::sort(SortedAttrs);
return getSorted(C, SortedAttrs);
}

AttributeSetNode *AttributeSetNode::getSorted(LLVMContext &C,
ArrayRef<Attribute> SortedAttrs) {
if (SortedAttrs.empty())
return nullptr;

// Otherwise, build a key to look up the existing attributes.
// Build a key to look up the existing attributes.
LLVMContextImpl *pImpl = C.pImpl;
FoldingSetNodeID ID;

SmallVector<Attribute, 8> SortedAttrs(Attrs.begin(), Attrs.end());
llvm::sort(SortedAttrs);

assert(llvm::is_sorted(SortedAttrs) && "Expected sorted attributes!");
for (const auto &Attr : SortedAttrs)
Attr.Profile(ID);

Expand Down Expand Up @@ -855,7 +860,7 @@ AttributeSetNode *AttributeSetNode::get(LLVMContext &C, const AttrBuilder &B) {
for (const auto &TDA : B.td_attrs())
Attrs.emplace_back(Attribute::get(C, TDA.first, TDA.second));

return get(C, Attrs);
return getSorted(C, Attrs);
}

bool AttributeSetNode::hasAttribute(StringRef Kind) const {
Expand Down
22 changes: 11 additions & 11 deletions llvm/utils/TableGen/Attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ void Attributes::emitTargetIndependentNames(raw_ostream &OS) {
OS << "#define ATTRIBUTE_ALL(FIRST, SECOND)\n";
OS << "#endif\n\n";

auto Emiter = [&](StringRef KindName, StringRef MacroName) {
std::vector<Record *> Attrs = Records.getAllDerivedDefinitions(KindName);

auto Emit = [&](ArrayRef<StringRef> KindNames, StringRef MacroName) {
OS << "#ifndef " << MacroName << "\n";
OS << "#define " << MacroName << "(FIRST, SECOND) ATTRIBUTE_ALL(FIRST, "
"SECOND)\n";
OS << "#define " << MacroName
<< "(FIRST, SECOND) ATTRIBUTE_ALL(FIRST, SECOND)\n";
OS << "#endif\n\n";

for (auto A : Attrs) {
OS << "" << MacroName << "(" << A->getName() << ","
<< A->getValueAsString("AttrString") << ")\n";
for (StringRef KindName : KindNames) {
for (auto A : Records.getAllDerivedDefinitions(KindName)) {
OS << MacroName << "(" << A->getName() << ","
<< A->getValueAsString("AttrString") << ")\n";
}
}
OS << "#undef " << MacroName << "\n\n";
};

Emiter("EnumAttr", "ATTRIBUTE_ENUM");
Emiter("StrBoolAttr", "ATTRIBUTE_STRBOOL");
// Emit attribute enums in the same order llvm::Attribute::operator< expects.
Emit({"EnumAttr", "TypeAttr", "IntAttr"}, "ATTRIBUTE_ENUM");
Emit({"StrBoolAttr"}, "ATTRIBUTE_STRBOOL");

OS << "#undef ATTRIBUTE_ALL\n";
OS << "#endif\n";
Expand Down

0 comments on commit ed766f1

Please sign in to comment.