[clang] [llvm] [Clang][OpenMP] Support for dispatch construct (Sema & Codegen) support (PR #131838)
Alexey Bataev via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 9 07:28:17 PDT 2025
================
@@ -4528,6 +4528,115 @@ void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) {
emitMaster(*this, S);
}
+static Expr *getInitialExprFromCapturedExpr(Expr *Cond) {
+
+ Expr *SubExpr = Cond->IgnoreParenImpCasts();
+
+ if (auto *DeclRef = dyn_cast<DeclRefExpr>(SubExpr)) {
+ if (auto *CapturedExprDecl =
+ dyn_cast<OMPCapturedExprDecl>(DeclRef->getDecl())) {
+
+ // Retrieve the initial expression from the captured expression
+ return CapturedExprDecl->getInit();
+ }
+ }
+ return Cond;
+}
+
+static Expr *replaceWithNewTraitsOrDirectCall(Stmt *AssocExpr,
+ CallExpr *ReplacementFunction) {
+ Expr *FinalCall = ReplacementFunction;
+
+ if (BinaryOperator *BinaryCopyOpr = dyn_cast<BinaryOperator>(AssocExpr)) {
+ BinaryCopyOpr->setRHS(FinalCall);
+ return BinaryCopyOpr;
+ }
+
+ return FinalCall;
+}
+
+static void transformCallInStmt(Stmt *StmtP) {
+ if (auto *AssocStmt = dyn_cast<CapturedStmt>(StmtP)) {
+ CapturedDecl *CDecl = AssocStmt->getCapturedDecl();
+
+ // Access AnnotateAttr
+ CallExpr *NewCallExpr = nullptr;
+ for (const auto *attr : CDecl->attrs()) {
+ if (const auto *annotateAttr = llvm::dyn_cast<clang::AnnotateAttr>(attr);
+ annotateAttr &&
+ annotateAttr->getAnnotation() == "NoContextInvariant") {
+ NewCallExpr = llvm::dyn_cast<CallExpr>(*annotateAttr->args_begin());
+ }
+ }
+
+ Stmt *CallExprStmt = CDecl->getBody();
+ Stmt *NewCallExprStmt =
+ replaceWithNewTraitsOrDirectCall(CallExprStmt, NewCallExpr);
+ CDecl->setBody(NewCallExprStmt);
+ }
+}
+
+static void EmitIfElse(CodeGenFunction *CGF, Expr *Condition,
+ Stmt *AssociatedStmt) {
+ llvm::Value *CondValue = CGF->EvaluateExprAsBool(Condition);
+ llvm::BasicBlock *ThenBlock = CGF->createBasicBlock("if.then");
+ llvm::BasicBlock *ElseBlock = CGF->createBasicBlock("if.else");
+ llvm::BasicBlock *MergeBlock = CGF->createBasicBlock("if.end");
+
+ CGF->Builder.CreateCondBr(CondValue, ThenBlock, ElseBlock);
+
+ // Emit the else block.
+ Stmt *ElseStmt = AssociatedStmt;
+ CGF->EmitBlock(ElseBlock);
+ CGF->EmitStmt(ElseStmt);
+ CGF->Builder.CreateBr(MergeBlock);
+
+ // Emit the then block.
+ Stmt *ThenStmt = AssociatedStmt;
+ transformCallInStmt(ThenStmt);
+ CGF->EmitBlock(ThenBlock);
+ CGF->EmitStmt(ThenStmt);
+ CGF->Builder.CreateBr(MergeBlock);
+ CGF->EmitBlock(MergeBlock);
+}
+
+void CodeGenFunction::EmitOMPDispatchDirective(const OMPDispatchDirective &S) {
+ ArrayRef<OMPClause *> Clauses = S.clauses();
+
+ Stmt *AssociatedStmt = const_cast<Stmt *>(S.getAssociatedStmt());
+ if (auto *AssocStmt = dyn_cast<CapturedStmt>(AssociatedStmt))
+ if (auto *InnerCapturedStmt =
+ dyn_cast<CapturedStmt>(AssocStmt->getCapturedStmt())) {
+ AssociatedStmt = InnerCapturedStmt;
+ }
+ CodeGenFunction::CGCapturedStmtInfo CapStmtInfo;
+ if (!CapturedStmtInfo)
+ CapturedStmtInfo = &CapStmtInfo;
+
+ if (!Clauses.empty()) {
+ if (S.hasClausesOfKind<OMPDependClause>())
+ EmitOMPDispatchToTaskwaitDirective(S);
----------------
alexey-bataev wrote:
Try to insert something like `OMPLexicalScope Scope(CGF, S, OMPD_unknown);` before emitting if-based logic
https://github.com/llvm/llvm-project/pull/131838
More information about the cfe-commits
mailing list