[llvm] 5413225 - [AttrBuilder] Add string attribute getter (NFC)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 18 03:20:39 PST 2022
Author: Nikita Popov
Date: 2022-01-18T12:20:30+01:00
New Revision: 541322540ea0e9e33dbe84db4a28ad4872a6a4cb
URL: https://github.com/llvm/llvm-project/commit/541322540ea0e9e33dbe84db4a28ad4872a6a4cb
DIFF: https://github.com/llvm/llvm-project/commit/541322540ea0e9e33dbe84db4a28ad4872a6a4cb.diff
LOG: [AttrBuilder] Add string attribute getter (NFC)
This avoids the need to scan through td_attrs() in AutoUpgrade,
decoupling it from AttrBuilder implementation details.
Added:
Modified:
llvm/include/llvm/IR/Attributes.h
llvm/lib/IR/Attributes.cpp
llvm/lib/IR/AutoUpgrade.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/Attributes.h b/llvm/include/llvm/IR/Attributes.h
index 236056612ee33..de559ce3cf172 100644
--- a/llvm/include/llvm/IR/Attributes.h
+++ b/llvm/include/llvm/IR/Attributes.h
@@ -1074,6 +1074,10 @@ class AttrBuilder {
/// Return true if the builder has an alignment attribute.
bool hasAlignmentAttr() const;
+ /// Return Attribute with the given Kind. The returned attribute will be
+ /// invalid if the Kind is not present in the builder.
+ Attribute getAttribute(StringRef Kind) const;
+
/// Return raw (possibly packed/encoded) value of integer attribute or 0 if
/// not set.
uint64_t getRawIntAttr(Attribute::AttrKind Kind) const;
diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp
index fa8cc44083a61..e9055d3f724a4 100644
--- a/llvm/lib/IR/Attributes.cpp
+++ b/llvm/lib/IR/Attributes.cpp
@@ -1791,9 +1791,15 @@ bool AttrBuilder::overlaps(const AttributeMask &AM) const {
return false;
}
-bool AttrBuilder::contains(StringRef A) const {
+Attribute AttrBuilder::getAttribute(StringRef A) const {
auto It = lower_bound(TargetDepAttrs, A, StringAttributeComparator());
- return It != TargetDepAttrs.end() && It->hasAttribute(A);
+ if (It != TargetDepAttrs.end() && It->hasAttribute(A))
+ return *It;
+ return {};
+}
+
+bool AttrBuilder::contains(StringRef A) const {
+ return getAttribute(A).isValid();
}
bool AttrBuilder::hasAttributes() const {
diff --git a/llvm/lib/IR/AutoUpgrade.cpp b/llvm/lib/IR/AutoUpgrade.cpp
index a7c34caab1c3e..6dd607ac85c6c 100644
--- a/llvm/lib/IR/AutoUpgrade.cpp
+++ b/llvm/lib/IR/AutoUpgrade.cpp
@@ -4585,11 +4585,10 @@ std::string llvm::UpgradeDataLayoutString(StringRef DL, StringRef TT) {
void llvm::UpgradeAttributes(AttrBuilder &B) {
StringRef FramePointer;
- if (B.contains("no-frame-pointer-elim")) {
+ Attribute A = B.getAttribute("no-frame-pointer-elim");
+ if (A.isValid()) {
// The value can be "true" or "false".
- for (const auto &A : B.td_attrs())
- if (A.getKindAsString() == "no-frame-pointer-elim")
- FramePointer = A.getValueAsString() == "true" ? "all" : "none";
+ FramePointer = A.getValueAsString() == "true" ? "all" : "none";
B.removeAttribute("no-frame-pointer-elim");
}
if (B.contains("no-frame-pointer-elim-non-leaf")) {
@@ -4601,12 +4600,10 @@ void llvm::UpgradeAttributes(AttrBuilder &B) {
if (!FramePointer.empty())
B.addAttribute("frame-pointer", FramePointer);
- if (B.contains("null-pointer-is-valid")) {
+ A = B.getAttribute("null-pointer-is-valid");
+ if (A.isValid()) {
// The value can be "true" or "false".
- bool NullPointerIsValid = false;
- for (const auto &A : B.td_attrs())
- if (A.getKindAsString() == "null-pointer-is-valid")
- NullPointerIsValid = A.getValueAsString() == "true";
+ bool NullPointerIsValid = A.getValueAsString() == "true";
B.removeAttribute("null-pointer-is-valid");
if (NullPointerIsValid)
B.addAttribute(Attribute::NullPointerIsValid);
More information about the llvm-commits
mailing list