[AMDGPU] Add a new function getIntegerPairAttribute (#133271)

The new function will return `std::nullopt` when any error occurs.
This commit is contained in:
Shilei Tian
2025-03-27 15:38:54 -04:00
committed by GitHub
parent e9d517d183
commit 02b45f4b81
3 changed files with 25 additions and 15 deletions

View File

@@ -107,7 +107,8 @@ static bool processUse(CallInst *CI, bool IsV5OrAbove) {
F->getFnAttribute("uniform-work-group-size").getValueAsBool();
SmallVector<unsigned> MaxNumWorkgroups =
AMDGPU::getIntegerVecAttribute(*F, "amdgpu-max-num-workgroups", 3);
AMDGPU::getIntegerVecAttribute(*F, "amdgpu-max-num-workgroups",
/*Size=*/3, /*DefaultVal=*/0);
if (!HasReqdWorkGroupSize && !HasUniformWorkGroupSize &&
none_of(MaxNumWorkgroups, [](unsigned X) { return X != 0; }))

View File

@@ -1398,16 +1398,25 @@ getIntegerPairAttribute(const Function &F, StringRef Name,
SmallVector<unsigned> getIntegerVecAttribute(const Function &F, StringRef Name,
unsigned Size,
unsigned DefaultVal) {
std::optional<SmallVector<unsigned>> R =
getIntegerVecAttribute(F, Name, Size);
return R.has_value() ? *R : SmallVector<unsigned>(Size, DefaultVal);
}
std::optional<SmallVector<unsigned>>
getIntegerVecAttribute(const Function &F, StringRef Name, unsigned Size) {
assert(Size > 2);
SmallVector<unsigned> Default(Size, DefaultVal);
LLVMContext &Ctx = F.getContext();
Attribute A = F.getFnAttribute(Name);
if (!A.isStringAttribute())
return Default;
if (!A.isValid())
return std::nullopt;
if (!A.isStringAttribute()) {
Ctx.emitError(Name + " is not a string attribute");
return std::nullopt;
}
SmallVector<unsigned> Vals(Size, DefaultVal);
LLVMContext &Ctx = F.getContext();
SmallVector<unsigned> Vals(Size);
StringRef S = A.getValueAsString();
unsigned i = 0;
@@ -1417,7 +1426,7 @@ SmallVector<unsigned> getIntegerVecAttribute(const Function &F, StringRef Name,
if (Strs.first.trim().getAsInteger(0, IntVal)) {
Ctx.emitError("can't parse integer attribute " + Strs.first + " in " +
Name);
return Default;
return std::nullopt;
}
Vals[i] = IntVal;
S = Strs.second;
@@ -1427,7 +1436,7 @@ SmallVector<unsigned> getIntegerVecAttribute(const Function &F, StringRef Name,
Ctx.emitError("attribute " + Name +
" has incorrect number of integers; expected " +
llvm::utostr(Size));
return Default;
return std::nullopt;
}
return Vals;
}

View File

@@ -958,14 +958,14 @@ getIntegerPairAttribute(const Function &F, StringRef Name,
/// \returns Generate a vector of integer values requested using \p F's \p Name
/// attribute.
///
/// \returns true if exactly Size (>2) number of integers are found in the
/// attribute.
///
/// \returns false if any error occurs.
/// \returns A vector of size \p Size, with all elements set to \p DefaultVal,
/// if any error occurs. The corresponding error will also be emitted.
SmallVector<unsigned> getIntegerVecAttribute(const Function &F, StringRef Name,
unsigned Size,
unsigned DefaultVal = 0);
unsigned DefaultVal);
/// Similar to the function above, but returns std::nullopt if any error occurs.
std::optional<SmallVector<unsigned>>
getIntegerVecAttribute(const Function &F, StringRef Name, unsigned Size);
/// Represents the counter values to wait for in an s_waitcnt instruction.
///