Skip to content

Commit 6fec9de

Browse files
authored
Merge branch 'main' into llvm-exegesis-illegal-instr
2 parents a692e8a + 1b6cbaa commit 6fec9de

File tree

64 files changed

+1017
-217
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1017
-217
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,8 @@ clang-format
683683

684684
libclang
685685
--------
686+
- Fixed a bug in ``clang_File_isEqual`` that sometimes led to different
687+
in-memory files to be considered as equal.
686688
- Added ``clang_visitCXXMethods``, which allows visiting the methods
687689
of a class.
688690
- Added ``clang_getFullyQualifiedName``, which provides fully qualified type names as

clang/include/clang/Sema/ScopeInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,9 @@ class LambdaScopeInfo final :
949949

950950
SourceLocation PotentialThisCaptureLocation;
951951

952+
/// Variables that are potentially ODR-used in CUDA/HIP.
953+
llvm::SmallPtrSet<VarDecl *, 4> CUDAPotentialODRUsedVars;
954+
952955
LambdaScopeInfo(DiagnosticsEngine &Diag)
953956
: CapturingScopeInfo(Diag, ImpCap_None) {
954957
Kind = SK_Lambda;

clang/include/clang/Sema/SemaCUDA.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ class SemaCUDA : public SemaBase {
274274
/// parameters specified via <<<>>>.
275275
std::string getConfigureFuncName() const;
276276

277+
/// Record variables that are potentially ODR-used in CUDA/HIP.
278+
void recordPotentialODRUsedVariable(MultiExprArg Args,
279+
OverloadCandidateSet &CandidateSet);
280+
277281
private:
278282
unsigned ForceHostDeviceDepth = 0;
279283

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
210210

211211
switch (CE->getCastKind()) {
212212
case CK_LValueToRValue: {
213+
if (SubExpr->getType().isVolatileQualified())
214+
return this->emitInvalidCast(CastKind::Volatile, /*Fatal=*/true, CE);
215+
213216
std::optional<PrimType> SubExprT = classify(SubExpr->getType());
214217
// Prepare storage for the result.
215218
if (!Initializing && !SubExprT) {

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -641,11 +641,30 @@ static bool CheckVolatile(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
641641
if (!PtrType.isVolatileQualified())
642642
return true;
643643

644-
const SourceInfo &Loc = S.Current->getSource(OpPC);
645-
if (S.getLangOpts().CPlusPlus)
646-
S.FFDiag(Loc, diag::note_constexpr_access_volatile_type) << AK << PtrType;
647-
else
648-
S.FFDiag(Loc);
644+
if (!S.getLangOpts().CPlusPlus)
645+
return Invalid(S, OpPC);
646+
647+
const NamedDecl *ND = nullptr;
648+
int DiagKind;
649+
SourceLocation Loc;
650+
if (const auto *F = Ptr.getField()) {
651+
DiagKind = 2;
652+
Loc = F->getLocation();
653+
ND = F;
654+
} else if (auto *VD = Ptr.getFieldDesc()->asValueDecl()) {
655+
DiagKind = 1;
656+
Loc = VD->getLocation();
657+
ND = VD;
658+
} else {
659+
DiagKind = 0;
660+
if (const auto *E = Ptr.getFieldDesc()->asExpr())
661+
Loc = E->getExprLoc();
662+
}
663+
664+
S.FFDiag(S.Current->getLocation(OpPC),
665+
diag::note_constexpr_access_volatile_obj, 1)
666+
<< AK << DiagKind << ND;
667+
S.Note(Loc, diag::note_constexpr_volatile_here) << DiagKind;
649668
return false;
650669
}
651670

clang/lib/AST/ByteCode/Interp.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2885,12 +2885,22 @@ inline bool InvalidCast(InterpState &S, CodePtr OpPC, CastKind Kind,
28852885
bool Fatal) {
28862886
const SourceLocation &Loc = S.Current->getLocation(OpPC);
28872887

2888-
// FIXME: Support diagnosing other invalid cast kinds.
28892888
if (Kind == CastKind::Reinterpret) {
28902889
S.CCEDiag(Loc, diag::note_constexpr_invalid_cast)
28912890
<< static_cast<unsigned>(Kind) << S.Current->getRange(OpPC);
28922891
return !Fatal;
2892+
} else if (Kind == CastKind::Volatile) {
2893+
// FIXME: Technically not a cast.
2894+
const auto *E = cast<CastExpr>(S.Current->getExpr(OpPC));
2895+
if (S.getLangOpts().CPlusPlus)
2896+
S.FFDiag(E, diag::note_constexpr_access_volatile_type)
2897+
<< AK_Read << E->getSubExpr()->getType();
2898+
else
2899+
S.FFDiag(E);
2900+
2901+
return false;
28932902
}
2903+
28942904
return false;
28952905
}
28962906

clang/lib/AST/ByteCode/PrimType.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,17 @@ inline constexpr bool isPtrType(PrimType T) {
5555

5656
enum class CastKind : uint8_t {
5757
Reinterpret,
58-
Atomic,
58+
Volatile,
5959
};
60+
6061
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
6162
interp::CastKind CK) {
6263
switch (CK) {
6364
case interp::CastKind::Reinterpret:
6465
OS << "reinterpret_cast";
6566
break;
66-
case interp::CastKind::Atomic:
67-
OS << "atomic";
67+
case interp::CastKind::Volatile:
68+
OS << "volatile";
6869
break;
6970
}
7071
return OS;

clang/lib/CIR/CodeGen/CIRGenStmtOpenACC.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ class OpenACCClauseCIREmitter final
204204
if (!clause.getArchitectures().empty())
205205
operation.setDeviceType(
206206
decodeDeviceType(clause.getArchitectures()[0].getIdentifierInfo()));
207-
} else if constexpr (isOneOfTypes<OpTy, ParallelOp, SerialOp, KernelsOp>) {
207+
} else if constexpr (isOneOfTypes<OpTy, ParallelOp, SerialOp, KernelsOp,
208+
DataOp>) {
208209
// Nothing to do here, these constructs don't have any IR for these, as
209210
// they just modify the other clauses IR. So setting of `lastDeviceType`
210211
// (done above) is all we need.
@@ -243,7 +244,7 @@ class OpenACCClauseCIREmitter final
243244
}
244245

245246
void VisitAsyncClause(const OpenACCAsyncClause &clause) {
246-
if constexpr (isOneOfTypes<OpTy, ParallelOp, SerialOp, KernelsOp>) {
247+
if constexpr (isOneOfTypes<OpTy, ParallelOp, SerialOp, KernelsOp, DataOp>) {
247248
if (!clause.hasIntExpr()) {
248249
operation.setAsyncOnlyAttr(
249250
handleDeviceTypeAffectedClause(operation.getAsyncOnlyAttr()));
@@ -278,7 +279,7 @@ class OpenACCClauseCIREmitter final
278279

279280
void VisitIfClause(const OpenACCIfClause &clause) {
280281
if constexpr (isOneOfTypes<OpTy, ParallelOp, SerialOp, KernelsOp, InitOp,
281-
ShutdownOp, SetOp>) {
282+
ShutdownOp, SetOp, DataOp>) {
282283
operation.getIfCondMutable().append(
283284
createCondition(clause.getConditionExpr()));
284285
} else {

clang/lib/Sema/SemaCUDA.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "clang/Basic/TargetInfo.h"
1919
#include "clang/Lex/Preprocessor.h"
2020
#include "clang/Sema/Lookup.h"
21+
#include "clang/Sema/Overload.h"
2122
#include "clang/Sema/ScopeInfo.h"
2223
#include "clang/Sema/Sema.h"
2324
#include "clang/Sema/Template.h"
@@ -1100,3 +1101,49 @@ std::string SemaCUDA::getConfigureFuncName() const {
11001101
// Legacy CUDA kernel configuration call
11011102
return "cudaConfigureCall";
11021103
}
1104+
1105+
// Record any local constexpr variables that are passed one way on the host
1106+
// and another on the device.
1107+
void SemaCUDA::recordPotentialODRUsedVariable(
1108+
MultiExprArg Arguments, OverloadCandidateSet &Candidates) {
1109+
sema::LambdaScopeInfo *LambdaInfo = SemaRef.getCurLambda();
1110+
if (!LambdaInfo)
1111+
return;
1112+
1113+
for (unsigned I = 0; I < Arguments.size(); ++I) {
1114+
auto *DeclRef = dyn_cast<DeclRefExpr>(Arguments[I]);
1115+
if (!DeclRef)
1116+
continue;
1117+
auto *Variable = dyn_cast<VarDecl>(DeclRef->getDecl());
1118+
if (!Variable || !Variable->isLocalVarDecl() || !Variable->isConstexpr())
1119+
continue;
1120+
1121+
bool HostByValue = false, HostByRef = false;
1122+
bool DeviceByValue = false, DeviceByRef = false;
1123+
1124+
for (OverloadCandidate &Candidate : Candidates) {
1125+
FunctionDecl *Callee = Candidate.Function;
1126+
if (!Callee || I >= Callee->getNumParams())
1127+
continue;
1128+
1129+
CUDAFunctionTarget Target = IdentifyTarget(Callee);
1130+
if (Target == CUDAFunctionTarget::InvalidTarget ||
1131+
Target == CUDAFunctionTarget::Global)
1132+
continue;
1133+
1134+
bool CoversHost = (Target == CUDAFunctionTarget::Host ||
1135+
Target == CUDAFunctionTarget::HostDevice);
1136+
bool CoversDevice = (Target == CUDAFunctionTarget::Device ||
1137+
Target == CUDAFunctionTarget::HostDevice);
1138+
1139+
bool IsRef = Callee->getParamDecl(I)->getType()->isReferenceType();
1140+
HostByValue |= CoversHost && !IsRef;
1141+
HostByRef |= CoversHost && IsRef;
1142+
DeviceByValue |= CoversDevice && !IsRef;
1143+
DeviceByRef |= CoversDevice && IsRef;
1144+
}
1145+
1146+
if ((HostByValue && DeviceByRef) || (HostByRef && DeviceByValue))
1147+
LambdaInfo->CUDAPotentialODRUsedVars.insert(Variable);
1148+
}
1149+
}

clang/lib/Sema/SemaExpr.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19517,11 +19517,29 @@ static ExprResult rebuildPotentialResultsAsNonOdrUsed(Sema &S, Expr *E,
1951719517
return false;
1951819518
};
1951919519

19520+
// Check whether this expression may be odr-used in CUDA/HIP.
19521+
auto MaybeCUDAODRUsed = [&]() -> bool {
19522+
if (!S.LangOpts.CUDA)
19523+
return false;
19524+
LambdaScopeInfo *LSI = S.getCurLambda();
19525+
if (!LSI)
19526+
return false;
19527+
auto *DRE = dyn_cast<DeclRefExpr>(E);
19528+
if (!DRE)
19529+
return false;
19530+
auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
19531+
if (!VD)
19532+
return false;
19533+
return LSI->CUDAPotentialODRUsedVars.count(VD);
19534+
};
19535+
1952019536
// Mark that this expression does not constitute an odr-use.
1952119537
auto MarkNotOdrUsed = [&] {
19522-
S.MaybeODRUseExprs.remove(E);
19523-
if (LambdaScopeInfo *LSI = S.getCurLambda())
19524-
LSI->markVariableExprAsNonODRUsed(E);
19538+
if (!MaybeCUDAODRUsed()) {
19539+
S.MaybeODRUseExprs.remove(E);
19540+
if (LambdaScopeInfo *LSI = S.getCurLambda())
19541+
LSI->markVariableExprAsNonODRUsed(E);
19542+
}
1952519543
};
1952619544

1952719545
// C++2a [basic.def.odr]p2:

0 commit comments

Comments
 (0)