alexey-bataev wrote:
Silent miscompile and broken AST printing for target enter data / target exit
data / target update variants
```
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -2494,7 +2494,7 @@ void CodeGenFunction::EmitOMPMetaDirective(const
OMPMetaDirective &S) {
EmitBlock(BB);
EmitStmt(V);
const auto *D = dyn_cast<OMPExecutableDirective>(V);
- EmitBranch(D && D->hasAssociatedStmt() ? EndBB : SharedBB);
+ EmitBranch(D && !D->isStandaloneDirective() ? EndBB : SharedBB);
}
if (Shared) {
EmitBlock(SharedBB);
--- a/clang/lib/AST/StmtPrinter.cpp
+++ b/clang/lib/AST/StmtPrinter.cpp
@@ -829,7 +829,7 @@ void StmtPrinter::VisitOMPMetaDirective(OMPMetaDirective
*Node) {
// the statement follows the metadirective.
for (Stmt *V : Variants) {
auto *D = dyn_cast<OMPExecutableDirective>(V);
- if (D && D->hasAssociatedStmt()) {
+ if (D && !D->isStandaloneDirective()) {
PrintStmt(D->getRawStmt());
break;
}
```
Missing cleanup scope in EmitOMPMetaDirective and missing ParseScope around
SharedStmt
```
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -2822,6 +2822,10 @@ StmtResult
Parser::ParseOpenMPDeclarativeOrExecutableDirective(
if (HasSharedStmt) {
ConsumeAnnotationToken();
ParsingOpenMPDirectiveRAII NormalScope(*this, /*Value=*/false);
+ ParseScope SharedScope(
+ this, Scope::DeclScope,
+ /*EnteredScope=*/getLangOpts().C99 || getLangOpts().CPlusPlus,
+ /*BeforeCompoundStmt=*/Tok.is(tok::l_brace));
SharedStmt = ParseStatement();
HasError |= SharedStmt.isInvalid();
}
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -2492,13 +2492,17 @@ void CodeGenFunction::EmitOMPMetaDirective(const
OMPMetaDirective &S) {
// their target regions get the same names on both sides.
for (auto [V, BB] : llvm::zip_equal(Variants, VariantBBs)) {
EmitBlock(BB);
- EmitStmt(V);
+ {
+ RunCleanupsScope VariantScope(*this);
+ EmitStmt(V);
+ }
const auto *D = dyn_cast<OMPExecutableDirective>(V);
EmitBranch(D && !D->isStandaloneDirective() ? EndBB : SharedBB);
}
if (Shared) {
EmitBlock(SharedBB);
+ RunCleanupsScope SharedScope(*this);
EmitStmt(Shared);
}
EmitBlock(EndBB, /*IsFinished=*/true);
```
Constant and side-effecting user conditions mishandled in EmitOMPMetaDirective
and IsSubset
```
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -2436,7 +2436,9 @@ void CodeGenFunction::EmitOMPMetaDirective(const
OMPMetaDirective &S) {
SmallVector<llvm::omp::VariantMatchInfo, 4> VMIs;
SmallVector<unsigned, 4> VariantIdx;
SmallVector<unsigned, 4> ConditionIds;
- SmallVector<llvm::FoldingSetNodeID, 4> CondProfiles;
+ SmallVector<std::pair<llvm::FoldingSetNodeID, unsigned>, 4> CondProfiles;
+ SmallVector<const Expr *, 4> RuntimeConds(Variants.size(), nullptr);
+ unsigned NextCondId = 1;
std::optional<unsigned> Fallback;
for (auto [I, TI] : llvm::enumerate(S.traitInfos())) {
if (TI->Sets.empty()) {
@@ -2445,24 +2447,36 @@ void CodeGenFunction::EmitOMPMetaDirective(const
OMPMetaDirective &S) {
}
llvm::omp::VariantMatchInfo &VMI = VMIs.emplace_back();
TI->getAsVariantMatchInfo(getContext(), VMI);
- // Treat the condition as true here; it is checked at runtime.
- if (Conditions[I]) {
- VMI.RequiredTraits.reset(
- unsigned(llvm::omp::TraitProperty::user_condition_false));
- VMI.RequiredTraits.set(
- unsigned(llvm::omp::TraitProperty::user_condition_true));
- }
- VariantIdx.push_back(I);
- // Identical conditions share an id; 0 means no condition.
+ // Non-constant conditions are checked at runtime; treat them as true here.
+ // Pure identical conditions share an id; 0 means no runtime condition.
unsigned Id = 0;
if (const Expr *Cond = Conditions[I]) {
- llvm::FoldingSetNodeID Profile;
- Cond->Profile(Profile, getContext(), /*Canonical=*/true);
- auto *It = llvm::find(CondProfiles, Profile);
- Id = std::distance(CondProfiles.begin(), It) + 1;
- if (It == CondProfiles.end())
- CondProfiles.push_back(Profile);
+ std::optional<llvm::APSInt> ConstVal =
+ Cond->getIntegerConstantExpr(getContext());
+ if (!ConstVal || !ConstVal->isZero()) {
+ VMI.RequiredTraits.reset(
+ unsigned(llvm::omp::TraitProperty::user_condition_false));
+ VMI.RequiredTraits.set(
+ unsigned(llvm::omp::TraitProperty::user_condition_true));
+ }
+ if (!ConstVal) {
+ RuntimeConds[I] = Cond;
+ if (Cond->HasSideEffects(getContext())) {
+ Id = NextCondId++;
+ } else {
+ llvm::FoldingSetNodeID Profile;
+ Cond->Profile(Profile, getContext(), /*Canonical=*/true);
+ auto *It = llvm::find_if(CondProfiles, [&](const auto &Entry) {
+ return Entry.first == Profile;
+ });
+ if (It == CondProfiles.end())
+ Id = CondProfiles.emplace_back(Profile, NextCondId++).second;
+ else
+ Id = It->second;
+ }
+ }
}
+ VariantIdx.push_back(I);
ConditionIds.push_back(Id);
}
@@ -2474,7 +2488,7 @@ void CodeGenFunction::EmitOMPMetaDirective(const
OMPMetaDirective &S) {
VMIs, OMPCtx, ConditionIds)) {
unsigned I = VariantIdx[Idx];
// Without a condition the variant always applies; later ones never run.
- const Expr *Cond = Conditions[I];
+ const Expr *Cond = RuntimeConds[I];
if (!Cond) {
Fallback = I;
break;
--- a/llvm/lib/Frontend/OpenMP/OMPContext.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPContext.cpp
@@ -467,6 +467,8 @@ int llvm::omp::getBestVariantMatchForContext(
SmallVector<unsigned> llvm::omp::getOrderedVariantMatchesForContext(
const SmallVectorImpl<VariantMatchInfo> &VMIs, const OMPContext &Ctx,
ArrayRef<unsigned> ConditionIds) {
+ assert((ConditionIds.empty() || ConditionIds.size() == VMIs.size()) &&
+ "Expected one condition id per variant");
SmallVector<std::pair<APInt, unsigned>> Scores;
for (auto [Idx, VMI] : enumerate(VMIs)) {
SmallVector<unsigned, 8> ConstructMatches;
@@ -485,8 +487,9 @@ SmallVector<unsigned>
llvm::omp::getOrderedVariantMatchesForContext(
// variant with the same score. OpenMP 6.0 [9.3] would give it a score of
// zero instead.
auto IsSubset = [&](unsigned I, unsigned J) {
- unsigned Cond = ConditionIds.empty() ? 0 : ConditionIds[I];
- return I != J && (Cond == 0 || Cond == ConditionIds[J]) &&
+ unsigned CondI = ConditionIds.empty() ? 0 : ConditionIds[I];
+ unsigned CondJ = ConditionIds.empty() ? 0 : ConditionIds[J];
+ return I != J && (CondI == 0 || CondJ == 0 || CondI == CondJ) &&
isStrictSubset(VMIs[I], VMIs[J]);
};
auto SameScore = [](const APInt &A, const APInt &B) {
```
https://github.com/llvm/llvm-project/pull/239292
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits