[clang] [llvm] [Clang][OpenMP] Support for dispatch construct (Sema & Codegen) support (PR #131838)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 29 12:01:16 PDT 2025
================
@@ -4529,6 +4529,191 @@ void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) {
emitMaster(*this, S);
}
+static Expr *replaceWithNewTraitsOrDirectCall(CapturedDecl *CDecl,
+ Expr *NewExpr) {
+ Expr *CurrentCallExpr = nullptr;
+ Stmt *CallExprStmt = CDecl->getBody();
+
+ if (BinaryOperator *BinaryCopyOpr = dyn_cast<BinaryOperator>(CallExprStmt)) {
+ CurrentCallExpr = BinaryCopyOpr->getRHS();
+ BinaryCopyOpr->setRHS(NewExpr);
+ } else {
+ CurrentCallExpr = dyn_cast<Expr>(CallExprStmt);
+ CDecl->setBody(NewExpr);
+ }
+
+ return CurrentCallExpr;
+}
+
+static Expr *transformCallInStmt(Stmt *StmtP, bool NoContext = false) {
+ Expr *CurrentExpr = nullptr;
+ if (auto *CptStmt = dyn_cast<CapturedStmt>(StmtP)) {
+ CapturedDecl *CDecl = CptStmt->getCapturedDecl();
+
+ CallExpr *NewCallExpr = nullptr;
+ for (const auto *attr : CDecl->attrs()) {
+ if (NoContext) {
+ if (const auto *annotateAttr =
+ llvm::dyn_cast<clang::AnnotateAttr>(attr);
+ annotateAttr && annotateAttr->getAnnotation() == "NoContextAttr") {
+ NewCallExpr = llvm::dyn_cast<CallExpr>(*annotateAttr->args_begin());
+ }
+ } else {
+ if (const auto *annotateAttr =
+ llvm::dyn_cast<clang::AnnotateAttr>(attr);
+ annotateAttr && annotateAttr->getAnnotation() == "NoVariantsAttr") {
+ NewCallExpr = llvm::dyn_cast<CallExpr>(*annotateAttr->args_begin());
+ }
+ }
+ }
+
+ CurrentExpr = replaceWithNewTraitsOrDirectCall(CDecl, NewCallExpr);
+ }
+ return CurrentExpr;
+}
+
+// emitIfElse is used for the following conditions:
+//
+// NoVariants = 0 && NoContext = 1
+// if (Condition_NoContext) {
+// foo_variant2(); // Present in AnnotationAttr
+// } else {
+// foo_variant();
+// }
+//
+// NoVariants = 1 && NoContext = 0
+// if (Condition_NoVariants) {
+// foo();
+// } else {
+// foo_variant();
+// }
+//
+// NoVariants = 1 && NoContext = 1
+// if (Condition_NoVariants) { // ==> label if.then.NoVariants
+// foo();
+// } else { // ==> label else.NoVariants
+// if (Condition_NoContext) { // ==> label if.then.NoContext
+// foo_variant2(); // Present in AnnotationAttr
+// } else { // ==> label else
+// foo_variant();
+// }
+// }
+//
+static void emitIfElse(CodeGenFunction *CGF, Stmt *AssociatedStmt,
----------------
SunilKuravinakop wrote:
Do you have an example of constructors/destructors where it will not work? The codegen for the `nocontext` & `novariant` clauses occurring together is present in the lit test case. I have written the code according to truth table mentioned by Deepak:
```
novariants(0), nocontext(0): Allow selective variant substitution according to context match
novariants(0), nocontext(1): Allow selective variant substitution according to context match (NO MATCH
for selectors that require **dispatch** trait)
novariants(1), nocontext(0): Always call the base function (shuts off any variant substitution for the call)
novariants(1), nocontext(1): Always call the base function (shuts off any variant substitution for the call)
```
https://github.com/llvm/llvm-project/pull/131838
More information about the cfe-commits
mailing list