Skip to content

Commit cee07d3

Browse files
authored
[SYCL] Fix integer type overflow in SYCLDeviceRequirements (#10614)
For example, if reqd_workg_roup_size has max unsigned value then it doesn't fit into signed integer type. So use signed integer type only for sycl_used_aspects which may have negative values.
1 parent 62422f9 commit cee07d3

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

llvm/tools/sycl-post-link/SYCLDeviceRequirements.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ using namespace llvm;
2121
void llvm::getSYCLDeviceRequirements(
2222
const module_split::ModuleDesc &MD,
2323
std::map<StringRef, util::PropertyValue> &Requirements) {
24-
auto ExtractIntegerFromMDNodeOperand = [=](const MDNode *N,
25-
unsigned OpNo) -> int32_t {
24+
auto ExtractSignedIntegerFromMDNodeOperand = [=](const MDNode *N,
25+
unsigned OpNo) -> int64_t {
2626
Constant *C =
2727
cast<ConstantAsMetadata>(N->getOperand(OpNo).get())->getValue();
28-
return static_cast<int32_t>(C->getUniqueInteger().getSExtValue());
28+
return C->getUniqueInteger().getSExtValue();
29+
};
30+
31+
auto ExtractUnsignedIntegerFromMDNodeOperand =
32+
[=](const MDNode *N, unsigned OpNo) -> uint64_t {
33+
Constant *C =
34+
cast<ConstantAsMetadata>(N->getOperand(OpNo).get())->getValue();
35+
return C->getUniqueInteger().getZExtValue();
2936
};
3037

3138
// { LLVM-IR metadata name , [SYCL/Device requirements] property name }, see:
@@ -42,11 +49,15 @@ void llvm::getSYCLDeviceRequirements(
4249
for (const Function &F : MD.getModule()) {
4350
if (const MDNode *MDN = F.getMetadata(MDName)) {
4451
for (size_t I = 0, E = MDN->getNumOperands(); I < E; ++I) {
45-
// Don't put internal aspects (with negative integer value) into the
46-
// requirements, they are used only for device image splitting.
47-
auto Val = ExtractIntegerFromMDNodeOperand(MDN, I);
48-
if (Val >= 0)
49-
Values.insert(Val);
52+
if (std::string(MDName) == "sycl_used_aspects") {
53+
// Don't put internal aspects (with negative integer value) into the
54+
// requirements, they are used only for device image splitting.
55+
auto Val = ExtractSignedIntegerFromMDNodeOperand(MDN, I);
56+
if (Val >= 0)
57+
Values.insert(Val);
58+
} else {
59+
Values.insert(ExtractUnsignedIntegerFromMDNodeOperand(MDN, I));
60+
}
5061
}
5162
}
5263
}
@@ -69,8 +80,7 @@ void llvm::getSYCLDeviceRequirements(
6980
for (const Function *F : MD.entries()) {
7081
if (auto *MDN = F->getMetadata("intel_reqd_sub_group_size")) {
7182
assert(MDN->getNumOperands() == 1);
72-
auto MDValue = ExtractIntegerFromMDNodeOperand(MDN, 0);
73-
assert(MDValue >= 0);
83+
auto MDValue = ExtractUnsignedIntegerFromMDNodeOperand(MDN, 0);
7484
if (!SubGroupSize)
7585
SubGroupSize = MDValue;
7686
else

0 commit comments

Comments
 (0)