Skip to content

Commit acba95f

Browse files
committed
PR: attribute check
1 parent 0d6b772 commit acba95f

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,6 @@ static std::enable_if_t<std::is_base_of_v<Attr, T>, SourceLocation>
207207
getAttrLoc(const T &AL) {
208208
return AL.getLocation();
209209
}
210-
template <typename T,
211-
std::enable_if_t<std::is_same_v<AttributeCommonInfo, T>, bool> = true>
212-
static SourceLocation getAttrLoc(const T &AL) {
213-
return AL.getScopeLoc();
214-
}
215210
static SourceLocation getAttrLoc(const ParsedAttr &AL) { return AL.getLoc(); }
216211

217212
/// If Expr is a valid integer constant, get the value of the integer
@@ -4452,6 +4447,25 @@ void Sema::AddSYCLIntelMaxGlobalWorkDimAttr(Decl *D,
44524447
D->addAttr(::new (Context) SYCLIntelMaxGlobalWorkDimAttr(Context, CI, E));
44534448
}
44544449

4450+
// Check that the value is a non-negative integer constant that can fit in
4451+
// 32-bits. Issue correct error message and return false on failure.
4452+
bool static check32BitInt(const Expr *E, Sema &S, llvm::APSInt &I,
4453+
const AttributeCommonInfo &CI) {
4454+
if (!I.isIntN(32)) {
4455+
S.Diag(E->getExprLoc(), diag::err_ice_too_large)
4456+
<< llvm::toString(I, 10, false) << 32 << /* Unsigned */ 1;
4457+
return false;
4458+
}
4459+
4460+
if (I.isSigned() && I.isNegative()) {
4461+
S.Diag(E->getExprLoc(), diag::err_attribute_requires_positive_integer)
4462+
<< CI << /* Non-negative */ 1;
4463+
return false;
4464+
}
4465+
4466+
return true;
4467+
}
4468+
44554469
void Sema::AddSYCLIntelMinWorkGroupsPerComputeUnitAttr(
44564470
Decl *D, const AttributeCommonInfo &CI, Expr *E) {
44574471
if (Context.getLangOpts().SYCLIsDevice &&
@@ -4461,20 +4475,15 @@ void Sema::AddSYCLIntelMinWorkGroupsPerComputeUnitAttr(
44614475
return;
44624476
}
44634477
if (!E->isValueDependent()) {
4464-
uint32_t Val;
4465-
if (!checkUInt32Argument(*this, CI, E, Val, UINT_MAX /* Idx */,
4466-
true /* StrictlyUnsigned */))
4467-
return;
4468-
44694478
// Validate that we have an integer constant expression and then store the
44704479
// converted constant expression into the semantic attribute so that we
44714480
// don't have to evaluate it again later.
44724481
llvm::APSInt ArgVal;
44734482
ExprResult Res = VerifyIntegerConstantExpression(E, &ArgVal);
44744483
if (Res.isInvalid())
44754484
return;
4476-
if (Val != ArgVal)
4477-
llvm_unreachable("Values must not differ.");
4485+
if (!check32BitInt(E, *this, ArgVal, CI))
4486+
return;
44784487
E = Res.get();
44794488

44804489
// Check to see if there's a duplicate attribute with different values
@@ -4526,21 +4535,16 @@ void Sema::AddSYCLIntelMaxWorkGroupsPerMultiprocessorAttr(
45264535
}
45274536
}
45284537
if (!E->isValueDependent()) {
4529-
uint32_t Val;
4530-
if (!checkUInt32Argument(*this, CI, E, Val, UINT_MAX /* Idx */,
4531-
true /* StrictlyUnsigned */))
4532-
return;
4533-
45344538
// Validate that we have an integer constant expression and then store the
45354539
// converted constant expression into the semantic attribute so that we
45364540
// don't have to evaluate it again later.
45374541
llvm::APSInt ArgVal;
45384542
ExprResult Res = VerifyIntegerConstantExpression(E, &ArgVal);
45394543
if (Res.isInvalid())
45404544
return;
4545+
if (!check32BitInt(E, *this, ArgVal, CI))
4546+
return;
45414547
E = Res.get();
4542-
if (Val != ArgVal)
4543-
llvm_unreachable("Values must not differ.");
45444548

45454549
// Check to see if there's a duplicate attribute with different values
45464550
// already applied to the declaration.

clang/test/SemaSYCL/lb_sm_70.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ int main() {
2626
intel::max_work_groups_per_mp(4)]] () { volatile int A = 42; });
2727

2828
constexpr float A = 2.0;
29-
// expected-error@+4 {{'min_work_groups_per_cu' attribute requires an integer constant}}
30-
// expected-warning@+4 {{'maxclusterrank' requires sm_90 or higher, CUDA arch provided: sm_70, ignoring 'max_work_groups_per_mp' attribute}}
29+
// expected-warning@+5{{'maxclusterrank' requires sm_90 or higher, CUDA arch provided: sm_70, ignoring 'max_work_groups_per_mp' attribute}}
30+
// expected-error@+3 {{integral constant expression must have integral or unscoped enumeration type, not 'float'}}
3131
cgh.single_task<class T2>(
3232
[=] [[intel::max_work_group_size(1, 1, 256),
3333
intel::min_work_groups_per_cu(A),
3434
intel::max_work_groups_per_mp(4)]] () { volatile int A = 42; });
3535

36-
// expected-error@+3 {{'min_work_groups_per_cu' attribute requires an integer constant}}
36+
// expected-error@+4 {{expression is not an integral constant expression}}
37+
// expected-note@+3 {{value 2147483648 is outside the range of representable values of type 'int'}}
3738
cgh.single_task<class T3>(
3839
[=] [[intel::max_work_group_size(1, 1, 256),
3940
intel::min_work_groups_per_cu(2147483647 + 1)]] () {

0 commit comments

Comments
 (0)