Skip to content

Commit 02b45f4

Browse files
authored
[AMDGPU] Add a new function getIntegerPairAttribute (llvm#133271)
The new function will return `std::nullopt` when any error occurs.
1 parent e9d517d commit 02b45f4

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

llvm/lib/Target/AMDGPU/AMDGPULowerKernelAttributes.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ static bool processUse(CallInst *CI, bool IsV5OrAbove) {
107107
F->getFnAttribute("uniform-work-group-size").getValueAsBool();
108108

109109
SmallVector<unsigned> MaxNumWorkgroups =
110-
AMDGPU::getIntegerVecAttribute(*F, "amdgpu-max-num-workgroups", 3);
110+
AMDGPU::getIntegerVecAttribute(*F, "amdgpu-max-num-workgroups",
111+
/*Size=*/3, /*DefaultVal=*/0);
111112

112113
if (!HasReqdWorkGroupSize && !HasUniformWorkGroupSize &&
113114
none_of(MaxNumWorkgroups, [](unsigned X) { return X != 0; }))

llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,16 +1398,25 @@ getIntegerPairAttribute(const Function &F, StringRef Name,
13981398
SmallVector<unsigned> getIntegerVecAttribute(const Function &F, StringRef Name,
13991399
unsigned Size,
14001400
unsigned DefaultVal) {
1401+
std::optional<SmallVector<unsigned>> R =
1402+
getIntegerVecAttribute(F, Name, Size);
1403+
return R.has_value() ? *R : SmallVector<unsigned>(Size, DefaultVal);
1404+
}
1405+
1406+
std::optional<SmallVector<unsigned>>
1407+
getIntegerVecAttribute(const Function &F, StringRef Name, unsigned Size) {
14011408
assert(Size > 2);
1402-
SmallVector<unsigned> Default(Size, DefaultVal);
1409+
LLVMContext &Ctx = F.getContext();
14031410

14041411
Attribute A = F.getFnAttribute(Name);
1405-
if (!A.isStringAttribute())
1406-
return Default;
1407-
1408-
SmallVector<unsigned> Vals(Size, DefaultVal);
1412+
if (!A.isValid())
1413+
return std::nullopt;
1414+
if (!A.isStringAttribute()) {
1415+
Ctx.emitError(Name + " is not a string attribute");
1416+
return std::nullopt;
1417+
}
14091418

1410-
LLVMContext &Ctx = F.getContext();
1419+
SmallVector<unsigned> Vals(Size);
14111420

14121421
StringRef S = A.getValueAsString();
14131422
unsigned i = 0;
@@ -1417,7 +1426,7 @@ SmallVector<unsigned> getIntegerVecAttribute(const Function &F, StringRef Name,
14171426
if (Strs.first.trim().getAsInteger(0, IntVal)) {
14181427
Ctx.emitError("can't parse integer attribute " + Strs.first + " in " +
14191428
Name);
1420-
return Default;
1429+
return std::nullopt;
14211430
}
14221431
Vals[i] = IntVal;
14231432
S = Strs.second;
@@ -1427,7 +1436,7 @@ SmallVector<unsigned> getIntegerVecAttribute(const Function &F, StringRef Name,
14271436
Ctx.emitError("attribute " + Name +
14281437
" has incorrect number of integers; expected " +
14291438
llvm::utostr(Size));
1430-
return Default;
1439+
return std::nullopt;
14311440
}
14321441
return Vals;
14331442
}

llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -958,14 +958,14 @@ getIntegerPairAttribute(const Function &F, StringRef Name,
958958

959959
/// \returns Generate a vector of integer values requested using \p F's \p Name
960960
/// attribute.
961-
///
962-
/// \returns true if exactly Size (>2) number of integers are found in the
963-
/// attribute.
964-
///
965-
/// \returns false if any error occurs.
961+
/// \returns A vector of size \p Size, with all elements set to \p DefaultVal,
962+
/// if any error occurs. The corresponding error will also be emitted.
966963
SmallVector<unsigned> getIntegerVecAttribute(const Function &F, StringRef Name,
967964
unsigned Size,
968-
unsigned DefaultVal = 0);
965+
unsigned DefaultVal);
966+
/// Similar to the function above, but returns std::nullopt if any error occurs.
967+
std::optional<SmallVector<unsigned>>
968+
getIntegerVecAttribute(const Function &F, StringRef Name, unsigned Size);
969969

970970
/// Represents the counter values to wait for in an s_waitcnt instruction.
971971
///

0 commit comments

Comments
 (0)