[llvm] [IR] Add assertions for unchecked getAsInteger() calls in atrib merging (PR #194713)
JP Hafer via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 29 06:05:53 PDT 2026
https://github.com/jph-13 updated https://github.com/llvm/llvm-project/pull/194713
>From 20a25740c944e2e10866f3772f53d10eb71af993 Mon Sep 17 00:00:00 2001
From: Jason Hafer <jhafer at mathworks.com>
Date: Tue, 28 Apr 2026 15:52:53 -0400
Subject: [PATCH] [IR] Add Attribute::getValueAsParsedInteger; use it in
attribute merging
Add Attribute::getValueAsParsedInteger() to parse a string attribute's
value as an integer, avoiding the need to go back through Function when
the Attribute is already in hand. Use it in adjustCallerStackProbeSize,
adjustMinLegalVectorWidth, and updateMinLegalVectorWidthAttr to
eliminate redundant attribute lookups. Also simplify
Function::getFnAttributeAsParsedInteger with an early return.
---
llvm/include/llvm/IR/Attributes.h | 6 ++++++
llvm/lib/IR/Attributes.cpp | 21 +++++++++++++--------
llvm/lib/IR/Function.cpp | 10 ++++------
3 files changed, 23 insertions(+), 14 deletions(-)
diff --git a/llvm/include/llvm/IR/Attributes.h b/llvm/include/llvm/IR/Attributes.h
index c88d230bc1321..696c628bafea4 100644
--- a/llvm/include/llvm/IR/Attributes.h
+++ b/llvm/include/llvm/IR/Attributes.h
@@ -290,6 +290,12 @@ class Attribute {
/// attribute to be a string attribute.
LLVM_ABI StringRef getValueAsString() const;
+ /// Return the attribute's value parsed as an integer. This requires the
+ /// attribute to be a string attribute.
+ ///
+ /// \returns \p Default if parsing fails.
+ LLVM_ABI uint64_t getValueAsParsedInteger(uint64_t Default = 0) const;
+
/// Return the attribute's value as a Type. This requires the attribute to be
/// a type attribute.
LLVM_ABI Type *getValueAsType() const;
diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp
index 4087b25951a1c..533a96a8174d6 100644
--- a/llvm/lib/IR/Attributes.cpp
+++ b/llvm/lib/IR/Attributes.cpp
@@ -409,6 +409,14 @@ StringRef Attribute::getValueAsString() const {
return pImpl->getValueAsString();
}
+uint64_t Attribute::getValueAsParsedInteger(uint64_t Default) const {
+ if (!isStringAttribute())
+ return Default;
+ uint64_t Result = Default;
+ getValueAsString().getAsInteger(0, Result);
+ return Result;
+}
+
Type *Attribute::getValueAsType() const {
if (!pImpl) return {};
assert(isTypeAttribute() &&
@@ -2686,9 +2694,8 @@ adjustCallerStackProbeSize(Function &Caller, const Function &Callee) {
if (CalleeAttr.isValid()) {
Attribute CallerAttr = Caller.getFnAttribute("stack-probe-size");
if (CallerAttr.isValid()) {
- uint64_t CallerStackProbeSize, CalleeStackProbeSize;
- CallerAttr.getValueAsString().getAsInteger(0, CallerStackProbeSize);
- CalleeAttr.getValueAsString().getAsInteger(0, CalleeStackProbeSize);
+ uint64_t CallerStackProbeSize = CallerAttr.getValueAsParsedInteger();
+ uint64_t CalleeStackProbeSize = CalleeAttr.getValueAsParsedInteger();
if (CallerStackProbeSize > CalleeStackProbeSize) {
Caller.addFnAttr(CalleeAttr);
@@ -2714,9 +2721,8 @@ adjustMinLegalVectorWidth(Function &Caller, const Function &Callee) {
if (CallerAttr.isValid()) {
Attribute CalleeAttr = Callee.getFnAttribute("min-legal-vector-width");
if (CalleeAttr.isValid()) {
- uint64_t CallerVectorWidth, CalleeVectorWidth;
- CallerAttr.getValueAsString().getAsInteger(0, CallerVectorWidth);
- CalleeAttr.getValueAsString().getAsInteger(0, CalleeVectorWidth);
+ uint64_t CallerVectorWidth = CallerAttr.getValueAsParsedInteger();
+ uint64_t CalleeVectorWidth = CalleeAttr.getValueAsParsedInteger();
if (CallerVectorWidth < CalleeVectorWidth)
Caller.addFnAttr(CalleeAttr);
} else {
@@ -2812,8 +2818,7 @@ void AttributeFuncs::updateMinLegalVectorWidthAttr(Function &Fn,
uint64_t Width) {
Attribute Attr = Fn.getFnAttribute("min-legal-vector-width");
if (Attr.isValid()) {
- uint64_t OldWidth;
- Attr.getValueAsString().getAsInteger(0, OldWidth);
+ uint64_t OldWidth = Attr.getValueAsParsedInteger();
if (Width > OldWidth)
Fn.addFnAttr("min-legal-vector-width", llvm::utostr(Width));
}
diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp
index a6568bb50f0c8..2cb3f9a31fa48 100644
--- a/llvm/lib/IR/Function.cpp
+++ b/llvm/lib/IR/Function.cpp
@@ -775,13 +775,11 @@ Attribute Function::getRetAttribute(Attribute::AttrKind Kind) const {
uint64_t Function::getFnAttributeAsParsedInteger(StringRef Name,
uint64_t Default) const {
Attribute A = getFnAttribute(Name);
+ if (!A.isStringAttribute())
+ return Default;
uint64_t Result = Default;
- if (A.isStringAttribute()) {
- StringRef Str = A.getValueAsString();
- if (Str.getAsInteger(0, Result))
- getContext().emitError("cannot parse integer attribute " + Name);
- }
-
+ if (A.getValueAsString().getAsInteger(0, Result))
+ getContext().emitError("cannot parse integer attribute " + Name);
return Result;
}
More information about the llvm-commits
mailing list