Skip to content

Commit 0f04dbc

Browse files
committed
Handle jumps into controlled sequences.
1 parent 9a7e250 commit 0f04dbc

File tree

5 files changed

+84
-5
lines changed

5 files changed

+84
-5
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13312,4 +13312,6 @@ def err_amdgcn_predicate_type_needs_explicit_bool_cast
1331213312
: Error<"%0 must be explicitly cast to %1; however, please note that this "
1331313313
"is almost always an error and that it prevents the effective "
1331413314
"guarding of target dependent code, and thus should be avoided">;
13315+
def note_amdgcn_protected_by_predicate
13316+
: Note<"jump enters statement controlled by AMDGPU feature predicate">;
1331513317
} // end of sema component.

clang/include/clang/Sema/SemaAMDGPU.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515

1616
#include "clang/AST/ASTFwd.h"
1717
#include "clang/Sema/SemaBase.h"
18+
#include "llvm/ADT/SmallPtrSet.h"
1819

1920
namespace clang {
2021
class AttributeCommonInfo;
22+
class Expr;
2123
class ParsedAttr;
2224

2325
class SemaAMDGPU : public SemaBase {
26+
llvm::SmallPtrSet<Expr *, 32> ExpandedPredicates;
2427
public:
2528
SemaAMDGPU(Sema &S);
2629

@@ -68,6 +71,7 @@ class SemaAMDGPU : public SemaBase {
6871
/// Expand a valid use of the feature identification builtins into its
6972
/// corresponding sequence of instructions.
7073
Expr *ExpandAMDGPUPredicateBI(CallExpr *CE);
74+
bool IsPredicate(Expr *E) const;
7175
};
7276
} // namespace clang
7377

clang/lib/Sema/JumpDiagnostics.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "clang/AST/StmtOpenACC.h"
2020
#include "clang/AST/StmtOpenMP.h"
2121
#include "clang/Basic/SourceLocation.h"
22+
#include "clang/Sema/SemaAMDGPU.h"
2223
#include "clang/Sema/SemaInternal.h"
2324
#include "llvm/ADT/BitVector.h"
2425
using namespace clang;
@@ -367,15 +368,19 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
367368

368369
case Stmt::IfStmtClass: {
369370
IfStmt *IS = cast<IfStmt>(S);
371+
bool AMDGPUPredicate = false;
370372
if (!(IS->isConstexpr() || IS->isConsteval() ||
371-
IS->isObjCAvailabilityCheck()))
373+
IS->isObjCAvailabilityCheck() ||
374+
(AMDGPUPredicate = this->S.AMDGPU().IsPredicate(IS->getCond()))))
372375
break;
373376

374377
unsigned Diag = diag::note_protected_by_if_available;
375378
if (IS->isConstexpr())
376379
Diag = diag::note_protected_by_constexpr_if;
377380
else if (IS->isConsteval())
378381
Diag = diag::note_protected_by_consteval_if;
382+
else if (AMDGPUPredicate)
383+
Diag = diag::note_amdgcn_protected_by_predicate;
379384

380385
if (VarDecl *Var = IS->getConditionVariable())
381386
BuildScopeInformation(Var, ParentScope);

clang/lib/Sema/SemaAMDGPU.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@ Expr *SemaAMDGPU::ExpandAMDGPUPredicateBI(CallExpr *CE) {
375375
auto Loc = CE->getExprLoc();
376376

377377
if (!CE->getBuiltinCallee())
378-
return IntegerLiteral::Create(Ctx, False, BoolTy, Loc);
378+
return *ExpandedPredicates.insert(
379+
IntegerLiteral::Create(Ctx, False, BoolTy, Loc)).first;
379380

380381
auto P = false;
381382
auto BI = CE->getBuiltinCallee();
@@ -398,7 +399,7 @@ Expr *SemaAMDGPU::ExpandAMDGPUPredicateBI(CallExpr *CE) {
398399
}
399400
if (Ctx.getTargetInfo().getTriple().isSPIRV()) {
400401
CE->setType(BoolTy);
401-
return CE;
402+
return *ExpandedPredicates.insert(CE).first;
402403
}
403404

404405
if (auto TID = Ctx.getTargetInfo().getTargetID())
@@ -412,7 +413,7 @@ Expr *SemaAMDGPU::ExpandAMDGPUPredicateBI(CallExpr *CE) {
412413

413414
if (Ctx.getTargetInfo().getTriple().isSPIRV()) {
414415
CE->setType(BoolTy);
415-
return CE;
416+
return *ExpandedPredicates.insert(CE).first;
416417
}
417418

418419
auto *FD = cast<FunctionDecl>(Arg->getReferencedDeclOfCallee());
@@ -424,6 +425,11 @@ Expr *SemaAMDGPU::ExpandAMDGPUPredicateBI(CallExpr *CE) {
424425
P = Builtin::evaluateRequiredTargetFeatures(RF, CF);
425426
}
426427

427-
return IntegerLiteral::Create(Ctx, P ? True : False, BoolTy, Loc);
428+
return *ExpandedPredicates.insert(
429+
IntegerLiteral::Create(Ctx, P ? True : False, BoolTy, Loc)).first;
430+
}
431+
432+
bool SemaAMDGPU::IsPredicate(Expr *E) const {
433+
return ExpandedPredicates.contains(E);
428434
}
429435
} // namespace clang
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// REQUIRES: amdgpu-registered-target
2+
// REQUIRES: spirv-registered-target
3+
// RUN: %clang_cc1 -fsyntax-only -verify -triple amdgcn -target-cpu gfx900 -Wno-unused-value %s
4+
// RUN: %clang_cc1 -fsyntax-only -verify -triple amdgcn -target-cpu gfx1201 -Wno-unused-value %s
5+
// RUN: %clang_cc1 -fsyntax-only -verify -triple spirv64-amd-amdhsa -Wno-unused-value %s
6+
// RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64 -aux-triple amdgcn -Wno-unused-value %s
7+
// RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64 -aux-triple spirv64-amd-amdhsa -Wno-unused-value %s
8+
9+
#define __device__ __attribute__((device))
10+
#define __global__ __attribute__((global))
11+
12+
__device__ void f(int *ptr, int size, bool f) {
13+
int i = 0;
14+
if (f)
15+
goto label; // expected-error {{cannot jump from this goto statement to its label}}
16+
17+
if (__builtin_amdgcn_processor_is("gfx900")) { // expected-note {{jump enters statement controlled by AMDGPU feature predicate}}
18+
for (i = 0; i < size; ++i) {
19+
label:
20+
ptr[i] = i;
21+
}
22+
}
23+
}
24+
25+
__device__ void g(int *ptr, int size, bool f) {
26+
int i = 0;
27+
if (f)
28+
goto label; // expected-error {{cannot jump from this goto statement to its label}}
29+
30+
if (__builtin_amdgcn_is_invocable(__builtin_amdgcn_s_sleep_var)) { // expected-note {{jump enters statement controlled by AMDGPU feature predicate}}
31+
for (i = 0; i < size; ++i) {
32+
label:
33+
ptr[i] = i;
34+
}
35+
}
36+
}
37+
38+
__global__ void h(int *ptr, int size, bool f) {
39+
int i = 0;
40+
if (f)
41+
goto label; // expected-error {{cannot jump from this goto statement to its label}}
42+
43+
if (__builtin_amdgcn_processor_is("gfx900")) { // expected-note {{jump enters statement controlled by AMDGPU feature predicate}}
44+
for (i = 0; i < size; ++i) {
45+
label:
46+
ptr[i] = i;
47+
}
48+
}
49+
}
50+
51+
__global__ void i(int *ptr, int size, bool f) {
52+
int i = 0;
53+
if (f)
54+
goto label; // expected-error {{cannot jump from this goto statement to its label}}
55+
56+
if (__builtin_amdgcn_is_invocable(__builtin_amdgcn_s_sleep_var)) { // expected-note {{jump enters statement controlled by AMDGPU feature predicate}}
57+
for (i = 0; i < size; ++i) {
58+
label:
59+
ptr[i] = i;
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)