[llvm] c16a58b - Attributes: Add function getter to parse integer string attributes
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 14 10:12:41 PST 2022
Author: Matt Arsenault
Date: 2022-12-14T13:12:35-05:00
New Revision: c16a58b36caebbc34dfa0f788a019d041e5484df
URL: https://github.com/llvm/llvm-project/commit/c16a58b36caebbc34dfa0f788a019d041e5484df
DIFF: https://github.com/llvm/llvm-project/commit/c16a58b36caebbc34dfa0f788a019d041e5484df.diff
LOG: Attributes: Add function getter to parse integer string attributes
The most common case for string attributes parses them as integers. We
don't have a convenient way to do this, and as a result we have
inconsistent missing attribute and invalid attribute handling
scattered around. We also have inconsistent radix usage to
getAsInteger; some places use the default 0 and others use base 10.
Update a few of the uses, but there are quite a lot of these.
Added:
Modified:
llvm/include/llvm/CodeGen/StackProtector.h
llvm/include/llvm/IR/Function.h
llvm/lib/CodeGen/StackProtector.cpp
llvm/lib/CodeGen/XRayInstrumentation.cpp
llvm/lib/IR/Function.cpp
llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp
llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
llvm/lib/Target/AMDGPU/SIFormMemoryClauses.cpp
llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp
llvm/lib/Target/ARM/ARMFrameLowering.cpp
llvm/lib/Target/PowerPC/PPCISelLowering.cpp
llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
llvm/lib/Target/X86/X86DynAllocaExpander.cpp
llvm/lib/Target/X86/X86ISelLowering.cpp
llvm/test/CodeGen/AMDGPU/attr-unparseable.ll
Removed:
################################################################################
diff --git a/llvm/include/llvm/CodeGen/StackProtector.h b/llvm/include/llvm/CodeGen/StackProtector.h
index 7eefc756c4f14..6150684236c8b 100644
--- a/llvm/include/llvm/CodeGen/StackProtector.h
+++ b/llvm/include/llvm/CodeGen/StackProtector.h
@@ -36,6 +36,8 @@ class Type;
class StackProtector : public FunctionPass {
private:
+ static constexpr unsigned DefaultSSPBufferSize = 8;
+
/// A mapping of AllocaInsts to their required SSP layout.
using SSPLayoutMap = DenseMap<const AllocaInst *,
MachineFrameInfo::SSPLayoutKind>;
@@ -59,7 +61,7 @@ class StackProtector : public FunctionPass {
/// The minimum size of buffers that will receive stack smashing
/// protection when -fstack-protection is used.
- unsigned SSPBufferSize = 0;
+ unsigned SSPBufferSize = DefaultSSPBufferSize;
/// VisitedPHIs - The set of PHI nodes visited when determining
/// if a variable's reference has been taken. This set
diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h
index 0e811e181fe77..a46462bb5ec45 100644
--- a/llvm/include/llvm/IR/Function.h
+++ b/llvm/include/llvm/IR/Function.h
@@ -406,6 +406,15 @@ class LLVM_EXTERNAL_VISIBILITY Function : public GlobalObject,
/// Return the attribute for the given attribute kind.
Attribute getFnAttribute(StringRef Kind) const;
+ /// For a string attribute \p Kind, parse attribute as an integer.
+ ///
+ /// \returns \p Default if attribute is not present.
+ ///
+ /// \returns \p Default if there is an error parsing the attribute integer,
+ /// and error is emitted to the LLVMContext
+ uint64_t getFnAttributeAsParsedInteger(StringRef Kind,
+ uint64_t Default = 0) const;
+
/// gets the specified attribute from the list of attributes.
Attribute getParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;
diff --git a/llvm/lib/CodeGen/StackProtector.cpp b/llvm/lib/CodeGen/StackProtector.cpp
index c7731a7e50fe8..f76877facc196 100644
--- a/llvm/lib/CodeGen/StackProtector.cpp
+++ b/llvm/lib/CodeGen/StackProtector.cpp
@@ -63,7 +63,7 @@ static cl::opt<bool> EnableSelectionDAGSP("enable-selectiondag-sp",
char StackProtector::ID = 0;
-StackProtector::StackProtector() : FunctionPass(ID), SSPBufferSize(8) {
+StackProtector::StackProtector() : FunctionPass(ID) {
initializeStackProtectorPass(*PassRegistry::getPassRegistry());
}
@@ -92,11 +92,8 @@ bool StackProtector::runOnFunction(Function &Fn) {
HasPrologue = false;
HasIRCheck = false;
- Attribute Attr = Fn.getFnAttribute("stack-protector-buffer-size");
- if (Attr.isStringAttribute() &&
- Attr.getValueAsString().getAsInteger(10, SSPBufferSize))
- return false; // Invalid integer string
-
+ SSPBufferSize = Fn.getFnAttributeAsParsedInteger(
+ "stack-protector-buffer-size", DefaultSSPBufferSize);
if (!RequiresStackProtector())
return false;
diff --git a/llvm/lib/CodeGen/XRayInstrumentation.cpp b/llvm/lib/CodeGen/XRayInstrumentation.cpp
index b66429d8a5bf5..13f45ae048bbb 100644
--- a/llvm/lib/CodeGen/XRayInstrumentation.cpp
+++ b/llvm/lib/CodeGen/XRayInstrumentation.cpp
@@ -151,19 +151,18 @@ bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) {
InstrAttr.getValueAsString() == "xray-never";
if (NeverInstrument && !AlwaysInstrument)
return false;
- auto ThresholdAttr = F.getFnAttribute("xray-instruction-threshold");
auto IgnoreLoopsAttr = F.getFnAttribute("xray-ignore-loops");
- unsigned int XRayThreshold = 0;
- if (!AlwaysInstrument) {
- if (!ThresholdAttr.isStringAttribute())
- return false; // XRay threshold attribute not found.
- if (ThresholdAttr.getValueAsString().getAsInteger(10, XRayThreshold))
- return false; // Invalid value for threshold.
+ uint64_t XRayThreshold = 0;
+ if (!AlwaysInstrument) {
bool IgnoreLoops = IgnoreLoopsAttr.isValid();
+ XRayThreshold = F.getFnAttributeAsParsedInteger(
+ "xray-instruction-threshold", std::numeric_limits<uint64_t>::max());
+ if (XRayThreshold == std::numeric_limits<uint64_t>::max())
+ return false;
// Count the number of MachineInstr`s in MachineFunction
- int64_t MICount = 0;
+ uint64_t MICount = 0;
for (const auto &MBB : MF)
MICount += MBB.size();
diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp
index f7a77512ba702..10bbdcfa0d818 100644
--- a/llvm/lib/IR/Function.cpp
+++ b/llvm/lib/IR/Function.cpp
@@ -659,6 +659,19 @@ Attribute Function::getFnAttribute(StringRef Kind) const {
return AttributeSets.getFnAttr(Kind);
}
+uint64_t Function::getFnAttributeAsParsedInteger(StringRef Name,
+ uint64_t Default) const {
+ Attribute A = getFnAttribute(Name);
+ uint64_t Result = Default;
+ if (A.isStringAttribute()) {
+ StringRef Str = A.getValueAsString();
+ if (Str.getAsInteger(0, Result))
+ getContext().emitError("cannot parse integer attribute " + Name);
+ }
+
+ return Result;
+}
+
/// gets the specified attribute from the list of attributes.
Attribute Function::getParamAttribute(unsigned ArgNo,
Attribute::AttrKind Kind) const {
diff --git a/llvm/lib/Target/AArch64/AArch64FrameLowering.cpp b/llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
index 572445ab76c13..7c969498c2f98 100644
--- a/llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64FrameLowering.cpp
@@ -871,11 +871,8 @@ static bool windowsRequiresStackProbe(MachineFunction &MF,
const Function &F = MF.getFunction();
// TODO: When implementing stack protectors, take that into account
// for the probe threshold.
- unsigned StackProbeSize = 4096;
- if (F.hasFnAttribute("stack-probe-size"))
- F.getFnAttribute("stack-probe-size")
- .getValueAsString()
- .getAsInteger(0, StackProbeSize);
+ unsigned StackProbeSize =
+ F.getFnAttributeAsParsedInteger("stack-probe-size", 4096);
return (StackSizeInBytes >= StackProbeSize) &&
!F.hasFnAttribute("no-stack-arg-probe");
}
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp b/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp
index 7e827d51586a6..7af0a7c9e0451 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp
@@ -524,7 +524,8 @@ unsigned AMDGPUSubtarget::getImplicitArgNumBytes(const Function &F) const {
// Assume all implicit inputs are used by default
unsigned NBytes = (AMDGPU::getAmdhsaCodeObjectVersion() >= 5) ? 256 : 56;
- return AMDGPU::getIntegerAttribute(F, "amdgpu-implicitarg-num-bytes", NBytes);
+ return F.getFnAttributeAsParsedInteger("amdgpu-implicitarg-num-bytes",
+ NBytes);
}
uint64_t AMDGPUSubtarget::getExplicitKernArgSize(const Function &F,
@@ -683,8 +684,8 @@ unsigned GCNSubtarget::getBaseMaxNumSGPRs(
// Check if maximum number of SGPRs was explicitly requested using
// "amdgpu-num-sgpr" attribute.
if (F.hasFnAttribute("amdgpu-num-sgpr")) {
- unsigned Requested = AMDGPU::getIntegerAttribute(
- F, "amdgpu-num-sgpr", MaxNumSGPRs);
+ unsigned Requested =
+ F.getFnAttributeAsParsedInteger("amdgpu-num-sgpr", MaxNumSGPRs);
// Make sure requested value does not violate subtarget's specifications.
if (Requested && (Requested <= ReservedNumSGPRs))
@@ -763,8 +764,8 @@ unsigned GCNSubtarget::getBaseMaxNumVGPRs(
// Check if maximum number of VGPRs was explicitly requested using
// "amdgpu-num-vgpr" attribute.
if (F.hasFnAttribute("amdgpu-num-vgpr")) {
- unsigned Requested = AMDGPU::getIntegerAttribute(
- F, "amdgpu-num-vgpr", MaxNumVGPRs);
+ unsigned Requested =
+ F.getFnAttributeAsParsedInteger("amdgpu-num-vgpr", MaxNumVGPRs);
if (hasGFX90AInsts())
Requested *= 2;
@@ -953,7 +954,8 @@ unsigned GCNSubtarget::getNSAThreshold(const MachineFunction &MF) const {
if (NSAThreshold.getNumOccurrences() > 0)
return std::max(NSAThreshold.getValue(), 2u);
- int Value = AMDGPU::getIntegerAttribute(MF.getFunction(), "amdgpu-nsa-threshold", -1);
+ int Value = MF.getFunction().getFnAttributeAsParsedInteger(
+ "amdgpu-nsa-threshold", -1);
if (Value > 0)
return std::max(Value, 2);
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
index b798c228127ec..af72ba2daa2d9 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp
@@ -106,7 +106,8 @@ void AMDGPUTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
TTI::UnrollingPreferences &UP,
OptimizationRemarkEmitter *ORE) {
const Function &F = *L->getHeader()->getParent();
- UP.Threshold = AMDGPU::getIntegerAttribute(F, "amdgpu-unroll-threshold", 300);
+ UP.Threshold =
+ F.getFnAttributeAsParsedInteger("amdgpu-unroll-threshold", 300);
UP.MaxCount = std::numeric_limits<unsigned>::max();
UP.Partial = true;
diff --git a/llvm/lib/Target/AMDGPU/SIFormMemoryClauses.cpp b/llvm/lib/Target/AMDGPU/SIFormMemoryClauses.cpp
index 56d456b29e84e..a1eb8150595f2 100644
--- a/llvm/lib/Target/AMDGPU/SIFormMemoryClauses.cpp
+++ b/llvm/lib/Target/AMDGPU/SIFormMemoryClauses.cpp
@@ -274,8 +274,8 @@ bool SIFormMemoryClauses::runOnMachineFunction(MachineFunction &MF) {
MaxVGPRs = TRI->getAllocatableSet(MF, &AMDGPU::VGPR_32RegClass).count();
MaxSGPRs = TRI->getAllocatableSet(MF, &AMDGPU::SGPR_32RegClass).count();
- unsigned FuncMaxClause = AMDGPU::getIntegerAttribute(
- MF.getFunction(), "amdgpu-max-memory-clause", MaxClause);
+ unsigned FuncMaxClause = MF.getFunction().getFnAttributeAsParsedInteger(
+ "amdgpu-max-memory-clause", MaxClause);
for (MachineBasicBlock &MBB : MF) {
GCNDownwardRPTracker RPT(*LIS);
diff --git a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp
index c77b2c1cf8045..351a73cbb674b 100644
--- a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp
@@ -1159,21 +1159,6 @@ bool shouldEmitConstantsToTextSection(const Triple &TT) {
return TT.getArch() == Triple::r600;
}
-int getIntegerAttribute(const Function &F, StringRef Name, int Default) {
- Attribute A = F.getFnAttribute(Name);
- int Result = Default;
-
- if (A.isStringAttribute()) {
- StringRef Str = A.getValueAsString();
- if (Str.getAsInteger(0, Result)) {
- LLVMContext &Ctx = F.getContext();
- Ctx.emitError("can't parse integer attribute " + Name);
- }
- }
-
- return Result;
-}
-
std::pair<int, int> getIntegerPairAttribute(const Function &F,
StringRef Name,
std::pair<int, int> Default,
@@ -1814,18 +1799,18 @@ uint64_t encodeMsg(uint64_t MsgId,
//===----------------------------------------------------------------------===//
unsigned getInitialPSInputAddr(const Function &F) {
- return getIntegerAttribute(F, "InitialPSInputAddr", 0);
+ return F.getFnAttributeAsParsedInteger("InitialPSInputAddr", 0);
}
bool getHasColorExport(const Function &F) {
// As a safe default always respond as if PS has color exports.
- return getIntegerAttribute(
- F, "amdgpu-color-export",
+ return F.getFnAttributeAsParsedInteger(
+ "amdgpu-color-export",
F.getCallingConv() == CallingConv::AMDGPU_PS ? 1 : 0) != 0;
}
bool getHasDepthExport(const Function &F) {
- return getIntegerAttribute(F, "amdgpu-depth-export", 0) != 0;
+ return F.getFnAttributeAsParsedInteger("amdgpu-depth-export", 0) != 0;
}
bool isShader(CallingConv::ID cc) {
diff --git a/llvm/lib/Target/ARM/ARMFrameLowering.cpp b/llvm/lib/Target/ARM/ARMFrameLowering.cpp
index 745e78bc88180..5fa7068c89eb7 100644
--- a/llvm/lib/Target/ARM/ARMFrameLowering.cpp
+++ b/llvm/lib/Target/ARM/ARMFrameLowering.cpp
@@ -557,10 +557,9 @@ static bool WindowsRequiresStackProbe(const MachineFunction &MF,
const MachineFrameInfo &MFI = MF.getFrameInfo();
const Function &F = MF.getFunction();
unsigned StackProbeSize = (MFI.getStackProtectorIndex() > 0) ? 4080 : 4096;
- if (F.hasFnAttribute("stack-probe-size"))
- F.getFnAttribute("stack-probe-size")
- .getValueAsString()
- .getAsInteger(0, StackProbeSize);
+
+ StackProbeSize =
+ F.getFnAttributeAsParsedInteger("stack-probe-size", StackProbeSize);
return (StackSizeInBytes >= StackProbeSize) &&
!F.hasFnAttribute("no-stack-arg-probe");
}
diff --git a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
index f15e653dd93aa..a74a43c72df49 100644
--- a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
+++ b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
@@ -12176,12 +12176,9 @@ unsigned PPCTargetLowering::getStackProbeSize(const MachineFunction &MF) const {
"Unexpected stack alignment");
// The default stack probe size is 4096 if the function has no
// stack-probe-size attribute.
- unsigned StackProbeSize = 4096;
const Function &Fn = MF.getFunction();
- if (Fn.hasFnAttribute("stack-probe-size"))
- Fn.getFnAttribute("stack-probe-size")
- .getValueAsString()
- .getAsInteger(0, StackProbeSize);
+ unsigned StackProbeSize =
+ Fn.getFnAttributeAsParsedInteger("stack-probe-size", 4096);
// Round down to the stack alignment.
StackProbeSize &= ~(StackAlign - 1);
return StackProbeSize ? StackProbeSize : StackAlign;
diff --git a/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp b/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
index 6a79af6cf9cfa..987c1c5b4462e 100644
--- a/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
@@ -7449,12 +7449,8 @@ SystemZTargetLowering::getStackProbeSize(const MachineFunction &MF) const {
"Unexpected stack alignment");
// The default stack probe size is 4096 if the function has no
// stack-probe-size attribute.
- unsigned StackProbeSize = 4096;
- const Function &Fn = MF.getFunction();
- if (Fn.hasFnAttribute("stack-probe-size"))
- Fn.getFnAttribute("stack-probe-size")
- .getValueAsString()
- .getAsInteger(0, StackProbeSize);
+ unsigned StackProbeSize =
+ MF.getFunction().getFnAttributeAsParsedInteger("stack-probe-size", 4096);
// Round down to the stack alignment.
StackProbeSize &= ~(StackAlign - 1);
return StackProbeSize ? StackProbeSize : StackAlign;
diff --git a/llvm/lib/Target/X86/X86DynAllocaExpander.cpp b/llvm/lib/Target/X86/X86DynAllocaExpander.cpp
index 2bcdca51031e1..8f237ee386b53 100644
--- a/llvm/lib/Target/X86/X86DynAllocaExpander.cpp
+++ b/llvm/lib/Target/X86/X86DynAllocaExpander.cpp
@@ -287,14 +287,7 @@ bool X86DynAllocaExpander::runOnMachineFunction(MachineFunction &MF) {
TRI = STI->getRegisterInfo();
StackPtr = TRI->getStackRegister();
SlotSize = TRI->getSlotSize();
-
- StackProbeSize = 4096;
- if (MF.getFunction().hasFnAttribute("stack-probe-size")) {
- MF.getFunction()
- .getFnAttribute("stack-probe-size")
- .getValueAsString()
- .getAsInteger(0, StackProbeSize);
- }
+ StackProbeSize = STI->getTargetLowering()->getStackProbeSize(MF);
NoStackArgProbe = MF.getFunction().hasFnAttribute("no-stack-arg-probe");
if (NoStackArgProbe)
StackProbeSize = INT64_MAX;
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 52933c8c95e4b..512d7a60d2bc3 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -57406,13 +57406,8 @@ unsigned
X86TargetLowering::getStackProbeSize(const MachineFunction &MF) const {
// The default stack probe size is 4096 if the function has no stackprobesize
// attribute.
- unsigned StackProbeSize = 4096;
- const Function &Fn = MF.getFunction();
- if (Fn.hasFnAttribute("stack-probe-size"))
- Fn.getFnAttribute("stack-probe-size")
- .getValueAsString()
- .getAsInteger(0, StackProbeSize);
- return StackProbeSize;
+ return MF.getFunction().getFnAttributeAsParsedInteger("stack-probe-size",
+ 4096);
}
Align X86TargetLowering::getPrefLoopAlignment(MachineLoop *ML) const {
diff --git a/llvm/test/CodeGen/AMDGPU/attr-unparseable.ll b/llvm/test/CodeGen/AMDGPU/attr-unparseable.ll
index 17adb89900cd4..8eb393f2e634b 100644
--- a/llvm/test/CodeGen/AMDGPU/attr-unparseable.ll
+++ b/llvm/test/CodeGen/AMDGPU/attr-unparseable.ll
@@ -1,20 +1,20 @@
; RUN: not llc -mtriple=amdgcn--amdhsa -mcpu=fiji -verify-machineinstrs < %s 2>&1 | FileCheck %s
-; CHECK: can't parse integer attribute amdgpu-num-sgpr
+; CHECK: cannot parse integer attribute amdgpu-num-sgpr
define amdgpu_kernel void @unparseable_single_0() #0 {
entry:
ret void
}
attributes #0 = {"amdgpu-num-sgpr"}
-; CHECK: can't parse integer attribute amdgpu-num-sgpr
+; CHECK: cannot parse integer attribute amdgpu-num-sgpr
define amdgpu_kernel void @unparseable_single_1() #1 {
entry:
ret void
}
attributes #1 = {"amdgpu-num-sgpr"="k"}
-; CHECK: can't parse integer attribute amdgpu-num-sgpr
+; CHECK: cannot parse integer attribute amdgpu-num-sgpr
define amdgpu_kernel void @unparseable_single_2() #2 {
entry:
ret void
More information about the llvm-commits
mailing list