[llvm] [AMDGPU] Add a new function `getIntegerPairAttribute` (PR #133271)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 27 09:03:54 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-amdgpu
Author: Shilei Tian (shiltian)
<details>
<summary>Changes</summary>
The new function will return `std::nullopt` when any error occurs.
---
Full diff: https://github.com/llvm/llvm-project/pull/133271.diff
3 Files Affected:
- (modified) llvm/lib/Target/AMDGPU/AMDGPULowerKernelAttributes.cpp (+2-1)
- (modified) llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp (+11-5)
- (modified) llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h (+6-6)
``````````diff
diff --git a/llvm/lib/Target/AMDGPU/AMDGPULowerKernelAttributes.cpp b/llvm/lib/Target/AMDGPU/AMDGPULowerKernelAttributes.cpp
index 6c8116abd8dcd..fed7a13a69bc4 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPULowerKernelAttributes.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPULowerKernelAttributes.cpp
@@ -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; }))
diff --git a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp
index 8a919a780bb75..c1800f50c41d9 100644
--- a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp
@@ -1398,14 +1398,20 @@ 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);
Attribute A = F.getFnAttribute(Name);
if (!A.isStringAttribute())
- return Default;
+ return std::nullopt;
- SmallVector<unsigned> Vals(Size, DefaultVal);
+ SmallVector<unsigned> Vals(Size);
LLVMContext &Ctx = F.getContext();
@@ -1417,7 +1423,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 +1433,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;
}
diff --git a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h
index 184f40bccfff8..f61a99c37e669 100644
--- a/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h
+++ b/llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h
@@ -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.
///
``````````
</details>
https://github.com/llvm/llvm-project/pull/133271
More information about the llvm-commits
mailing list