[PATCH] D91054: [Clang][OpenMP] Frontend work for sections - D89671

2021-04-23 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.



> This is generally fine with me, @fghanim @Meinersbur any concerns?

I have none. All good for me


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91054/new/

https://reviews.llvm.org/D91054

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D91054: [Clang][OpenMP] Frontend work for sections - D89671

2021-04-15 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

You can update the tests as long as long as the output is correct. for example 
the difference is only in names, ordering of basicblocks and instructions that 
doesn't affect correctness, etc.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91054/new/

https://reviews.llvm.org/D91054

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D91054: [Clang][OpenMP] Frontend work for sections - D89671

2021-01-07 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

Ping. Please add the Lit test for this.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91054/new/

https://reviews.llvm.org/D91054

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D92189: [OpenMPIRBuilder] forward arguments as pointers to outlined function

2020-12-01 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

Thanks; just two more mediocre things if possible. if not. you are good to go. 
:)

@jdoerfert

In D92189#2424677 , @jdoerfert wrote:

> In D92189#2424216 , @fghanim wrote:
>
>> To be honest, I am not exactly sure what is the problem you are trying to 
>> address here, is it about passing shared variables as proper pointers? or is 
>> it about creating locals for said arguments
>
> The problem was discussed in the Flang + OpenMP call lately and got attention 
> by at least 4 people since then, as far as I can tell independently. I'm 
> rather confused TBH, but it's a "good thing", as far as bug fixes can be good 
> ;)

I don't disagree. As I said, "I am very much not against doing so here"




Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:697
+  IRBuilder<>::InsertPoint ReloadIP(ZeroAddrUse->getParent(),
+ZeroAddrUse->getNextNode()->getIterator());
+

ftynse wrote:
> fghanim wrote:
> > Why not use `InnerAllocaIP` instead? it's in the entry block of the 
> > parallel region.
> I started with that, but it did not work out. `InnerAllocaIP` is used by 
> `PrivCB` to construct IR that may be using the value defined at `ReloadIP`. 
> If I literally replace the use of `ReloadIP` with `InnerAllocaIP`, the 
> instructions with have the wrong order. I also considered inserting this at 
> the top of the entry block, but this messes up the order of arguments in the 
> outlined function. Suggestions on how to structure this better are welcome.
OK; The only instructions generated at `InnerAllocaIP` up to this point are the 
things generated in `bodyCB`, are these the users that you are talking about?

In any case, and since you already reset `InnerAllocaIP` to ` ReloadIP`, I 
prefer we do that before privatization (i.e. reset `InnerAllocaIP`, to 
`ZeroAddrUse->getNextNode()` directly), and use that instead. 



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:716
+  Builder.SetInsertPoint(InsertBB,
+ InsertBB->getTerminator()->getIterator());
+  Value *Ptr = Builder.CreateAlloca(V.getType());

ftynse wrote:
> fghanim wrote:
> > For the alloca, should use `OuterAllocaIP` or `InnerAllocaIP` based on 
> > which function the alloca belongs to.
> > 
> > allocations in InsertBB will end right before the fork call, which suggests 
> > you probably should use outerAllocaIP. but why do you need two allocations 
> > for same variable. or is this meant to be the allocations where we store 
> > the arguments in the outlined function? if that's the case, then you should 
> > use `InnerAllocaIP`
> This belongs to the outer function, but `OuterAllocaIP` does not seem valid 
> at this point anymore. Using it leads to segfaults, I can investigate later, 
> after we decide whether this code should remain here or move below as 
> Johannes suggested.
That's probably because the `OuterAllocaIP` iterator got invalidated between 
the beginning and here.

If you want; get the insertion BasicBlock early on, and then use that to 
reupdate `OuterAllocaIP` and use that to place whatever you want. right now 
those alloca.s are not in an entry block.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D92189/new/

https://reviews.llvm.org/D92189

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D91054: [Clang][OpenMP] Frontend work for sections - D89671

2020-11-10 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

Thanks for doing this.

Please add proper lit test. you can find that under 
`clang/test/OpenMP/sections_codegen.cpp`. add a case for using the OMPBuilder.
As an example look in the same directory for `parallel_codegen.cpp` , 
`master_codegen.cpp` , `critical_codegen.cpp`

In case you need it; you can check that your changes works by doing `ninja/make 
-check-clang-openmp`


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91054/new/

https://reviews.llvm.org/D91054

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D91109: [OMPIRBuilder] Start 'Create' methods with lower case. NFC.

2020-11-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim accepted this revision.
fghanim added a comment.

LGTM


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91109/new/

https://reviews.llvm.org/D91109

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79677: [Clang][OpenMP][OMPBuilder] (1/4) Privatize `parallel` for `OMPBuilder`

2020-09-01 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

In D79677#2248663 , @lebedev.ri wrote:

> Tests missing

I am not sure what to test here that isn't tested elsewhere in the series. This 
patch is the last in a series, and it represents the "usage" of the 
functionality added by the other patches. Each of the other patches has its 
test as a part of it.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79677/new/

https://reviews.llvm.org/D79677

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85619: [clang][OpenMP][OMPBuilder] Use OMPBuilder to CG `omp single`

2020-09-01 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

In D85619#2248195 , @kiranchandramohan 
wrote:

> What is the plan for this patch?

Waiting on you to review it ;)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D85619/new/

https://reviews.llvm.org/D85619

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79677: [Clang][OpenMP][OMPBuilder] (1/4) Privatize `parallel` for `OMPBuilder`

2020-08-31 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

In D79677#2248197 , @kiranchandramohan 
wrote:

> What is the plan for this patch?

To commit it ... eventually :)
Once it (and the rest in the series) get reviewed. As it stands,  I cannot 
commit this patch without the rest in the series, and I cannot find reviewers 
for the other patches.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79677/new/

https://reviews.llvm.org/D79677

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85619: [clang][OpenMP][OMPBuilder] Use OMPBuilder to CG `omp single`

2020-08-13 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

ping


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D85619/new/

https://reviews.llvm.org/D85619

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85619: [clang][OpenMP][OMPBuilder] Use OMPBuilder to CG `omp single`

2020-08-10 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

In D85619#2208425 , @kiranchandramohan 
wrote:

> Before I start, Why is this change all new code and no modification or 
> deletion of existing Clang functionality for omp single/copyprivate?

Because the OMPBuilder currently is not the default way to CG OMP code in 
clang, which means the old way needs to keep working. So, we cannot delete or 
modify anything, we can only add the OMPBuilder specific code, and condition 
its usage on whether `-fopenmp-enable-irbuilder` flag has been issued, or not.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D85619/new/

https://reviews.llvm.org/D85619

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85619: [clang][OpenMP][OMPBuilder] Use OMPBuilder to CG `omp single`

2020-08-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

Feel free to add other reviewers. Thanks.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D85619/new/

https://reviews.llvm.org/D85619

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D85619: [clang][OpenMP][OMPBuilder] Use OMPBuilder to CG `omp single`

2020-08-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim created this revision.
Herald added subscribers: cfe-commits, aaron.ballman, guansong, yaxunl.
Herald added a project: clang.
fghanim requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added a subscriber: sstefan1.

ADD support to allow `omp single` to be CG-ed by the OMPBuilder. This
also uses the OMPBuilder to generate `__kmpc_copyprivate` calls.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85619

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CodeGenFunction.h
  clang/test/OpenMP/single_codegen.cpp

Index: clang/test/OpenMP/single_codegen.cpp
===
--- clang/test/OpenMP/single_codegen.cpp
+++ clang/test/OpenMP/single_codegen.cpp
@@ -1,11 +1,12 @@
-// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=50 -fnoopenmp-use-tls -x c++ -std=c++11 -triple x86_64-unknown-unknown -emit-llvm %s -fexceptions -fcxx-exceptions -o - | FileCheck %s --check-prefixes=OMP50,CHECK
-// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=50 -fnoopenmp-use-tls -x c++ -std=c++11 -triple x86_64-unknown-unknown -emit-llvm %s -fexceptions -fcxx-exceptions -fopenmp-version=45 -o - | FileCheck %s --check-prefixes=OMP45,CHECK
+// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=50 -fnoopenmp-use-tls -x c++ -std=c++11 -triple x86_64-unknown-unknown -emit-llvm %s -fexceptions -fcxx-exceptions -o - | FileCheck %s --check-prefixes=OMP50,ALL,CHECK
+// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=50 -fnoopenmp-use-tls -x c++ -std=c++11 -triple x86_64-unknown-unknown -emit-llvm %s -fexceptions -fcxx-exceptions -fopenmp-version=45 -o - | FileCheck %s --check-prefixes=OMP45,ALL,CHECK
+// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=50 -fnoopenmp-use-tls -x c++ -std=c++11 -DOMPBUILDER -triple x86_64-unknown-unknown -emit-llvm %s -fexceptions -fcxx-exceptions -o - | FileCheck %s --check-prefixes=ALL,OMPBUILDER
 
 // RUN: %clang_cc1 -fopenmp -fopenmp-version=50 -fnoopenmp-use-tls -x c++ -std=c++11 -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -emit-pch -o %t %s
-// RUN: %clang_cc1 -fopenmp -fopenmp-version=50 -fnoopenmp-use-tls -x c++ -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s --check-prefixes=OMP50,CHECK
+// RUN: %clang_cc1 -fopenmp -fopenmp-version=50 -fnoopenmp-use-tls -x c++ -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s --check-prefixes=OMP50,ALL,CHECK
 
 // RUN: %clang_cc1 -fopenmp -fopenmp-version=45 -fnoopenmp-use-tls -x c++ -std=c++11 -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -emit-pch -o %t %s
-// RUN: %clang_cc1 -fopenmp -fopenmp-version=45 -fnoopenmp-use-tls -x c++ -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s --check-prefixes=OMP45,CHECK
+// RUN: %clang_cc1 -fopenmp -fopenmp-version=45 -fnoopenmp-use-tls -x c++ -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s --check-prefixes=OMP45,ALL,CHECK
 
 // RUN: %clang_cc1 -verify -triple x86_64-apple-darwin10 -std=c++11 -fopenmp -fnoopenmp-use-tls -fexceptions -fcxx-exceptions -debug-info-kind=line-tables-only -x c++ -emit-llvm %s -o - | FileCheck %s --check-prefix=TERM_DEBUG
 // RUN: %clang_cc1 -verify -fopenmp -fnoopenmp-use-tls -x c++ -std=c++11 -DARRAY -triple x86_64-apple-darwin10 -emit-llvm %s -o - | FileCheck -check-prefix=ARRAY %s
@@ -30,13 +31,13 @@
   ~TestClass(){};
 };
 
-// CHECK-DAG:   [[TEST_CLASS_TY:%.+]] = type { i{{[0-9]+}} }
+// ALL-DAG:   [[TEST_CLASS_TY:%.+]] = type { i{{[0-9]+}} }
 // CHECK-DAG:   [[SST_TY:%.+]] = type { double }
 // CHECK-DAG:   [[SS_TY:%.+]] = type { i32, i8, i32* }
-// CHECK-DAG:   [[IDENT_T_TY:%.+]] = type { i32, i32, i32, i32, i8* }
-// CHECK:   [[IMPLICIT_BARRIER_SINGLE_LOC:@.+]] = private unnamed_addr global %{{.+}} { i32 0, i32 322, i32 0, i32 0, i8*
+// ALL-DAG:   [[IDENT_T_TY:%.+]] = type { i32, i32, i32, i32, i8* }
+// ALL:   [[IMPLICIT_BARRIER_SINGLE_LOC:@.+]] = private unnamed_addr global %{{.+}} { i32 0, i32 322, i32 0, i32 0, i8*
 
-// CHECK:   define void [[FOO:@.+]]()
+// ALL:   define void [[FOO:@.+]]()
 
 TestClass tc;
 TestClass tc2[2];
@@ -44,6 +45,7 @@
 
 void foo() { extern void mayThrow(); mayThrow(); }
 
+#ifndef OMPBUILDER
 struct SS {
   int a;
   int b : 4;
@@ -76,129 +78,132 @@
 }();
   }
 };
+#endif
 
-// CHECK-LABEL: @main
+// ALL-LABEL: @main
 // TERM_DEBUG-LABEL: @main
 int main() {
-  // CHECK: alloca i32
-  // CHECK-DAG: [[A_ADDR:%.+]] = alloca i8
-  // CHECK-DAG: [[A2_ADDR:%.+]] = alloca [2 x i8]
-  // CHECK-DAG: [[C_ADDR:%.+]] = alloca [[TEST_CLASS_TY]]
-  // CHECK-DAG: [[DID_IT:%.+]] = alloca i32,
-  // CHECK-DAG: [[COPY_LIST:%.+]] = alloca [5 x i8*],
+  // ALL: alloca i32
+  // 

[PATCH] D82470: [OpenMP][IRBuilder] Support allocas in nested parallel regions

2020-07-17 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim accepted this revision.
fghanim added a comment.
This revision is now accepted and ready to land.

Great. Thank you!
LGTM


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D82470/new/

https://reviews.llvm.org/D82470



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D82470: [OpenMP][IRBuilder] Support allocas in nested parallel regions

2020-07-16 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added inline comments.



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:433
 
-  Builder.SetInsertPoint(OuterFn->getEntryBlock().getFirstNonPHI());
-  AllocaInst *TIDAddr = Builder.CreateAlloca(Int32, nullptr, "tid.addr");
-  AllocaInst *ZeroAddr = Builder.CreateAlloca(Int32, nullptr, "zero.addr");
+  IRBuilder<> AllocaBuilder(AllocaIP.getBlock(), AllocaIP.getPoint());
+

jdoerfert wrote:
> fghanim wrote:
> > jdoerfert wrote:
> > > fghanim wrote:
> > > > Here and elsewhere: You seem to have forgotten to undo the changes 
> > > > introduced by using a separate `AllocaBuilder`?
> > > > 
> > > > Also, Nit: before using `AllocaIP`, either add an assert that it is not 
> > > > empty, or alternatively, add logic to just set it to entry block of 
> > > > `outerfn` if it is
> > > > Here and elsewhere: You seem to have forgotten to undo the changes 
> > > > introduced by using a separate AllocaBuilder?
> > > 
> > > I don't get this. These changes are on purpose. 
> > Oh, my bad. I assumed that since we now pass `allocaip` to communicate 
> > where is the insertion point, using `builder` the way we used to is 
> > sufficient, and this was leftover code. So now what purpose does 
> > `AllocaBuilder` serve?
> 1) No switching the IP back and forth in one builder all the time.
> 2) Showing by the name of the builder directly where the instructions will be 
> created.
>No switching the IP back and forth in one builder all the time.
- Is this about something you expect in the future? because unless I am missing 
something, and looking at it side by side, I don't see any less setting 
/reseting of IPs going on, except in the new version we to set the debug info 
IP in `allocabuilder` in addition to in `builder`.
- we don't use either `AllocaIP` after returning from `bodyCB`
- Even if we did, both `bodyCB`, and `FiniCB` have IPGuards inside them.
- Also, since IRBuilders do `insertbefore` an IP, both `AllocaIP` should be 
up-to-date except if the iterator for an IP gets invalidated, the risk of which 
is sort of proportional to increase in number of IRbuilders.

>Showing by the name of the builder directly where the instructions will be 
>created.
Wouldn't a comment be better? 
Personally, I find it confusing when something called `AllocaBuilder`, is being 
used to emit loads and stores.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D82470/new/

https://reviews.llvm.org/D82470



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D82470: [OpenMP][IRBuilder] Support allocas in nested parallel regions

2020-07-14 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

> I'll address the nits.

Thanks :)

> Unsure if that is all.

depends on the comment about `AllocaBuilder`




Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:433
 
-  Builder.SetInsertPoint(OuterFn->getEntryBlock().getFirstNonPHI());
-  AllocaInst *TIDAddr = Builder.CreateAlloca(Int32, nullptr, "tid.addr");
-  AllocaInst *ZeroAddr = Builder.CreateAlloca(Int32, nullptr, "zero.addr");
+  IRBuilder<> AllocaBuilder(AllocaIP.getBlock(), AllocaIP.getPoint());
+

jdoerfert wrote:
> fghanim wrote:
> > Here and elsewhere: You seem to have forgotten to undo the changes 
> > introduced by using a separate `AllocaBuilder`?
> > 
> > Also, Nit: before using `AllocaIP`, either add an assert that it is not 
> > empty, or alternatively, add logic to just set it to entry block of 
> > `outerfn` if it is
> > Here and elsewhere: You seem to have forgotten to undo the changes 
> > introduced by using a separate AllocaBuilder?
> 
> I don't get this. These changes are on purpose. 
Oh, my bad. I assumed that since we now pass `allocaip` to communicate where is 
the insertion point, using `builder` the way we used to is sufficient, and this 
was leftover code. So now what purpose does `AllocaBuilder` serve?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D82470/new/

https://reviews.llvm.org/D82470



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D82470: [OpenMP][IRBuilder] Support allocas in nested parallel regions

2020-07-13 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

Thanks for the update. Just a couple of Nits, and a quick note




Comment at: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h:288
   struct OutlineInfo {
-SmallVector Blocks;
 using PostOutlineCBTy = std::function;

Nit: isn't this supposed to be part of one of the other patches you split of 
this?



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:433
 
-  Builder.SetInsertPoint(OuterFn->getEntryBlock().getFirstNonPHI());
-  AllocaInst *TIDAddr = Builder.CreateAlloca(Int32, nullptr, "tid.addr");
-  AllocaInst *ZeroAddr = Builder.CreateAlloca(Int32, nullptr, "zero.addr");
+  IRBuilder<> AllocaBuilder(AllocaIP.getBlock(), AllocaIP.getPoint());
+

Here and elsewhere: You seem to have forgotten to undo the changes introduced 
by using a separate `AllocaBuilder`?

Also, Nit: before using `AllocaIP`, either add an assert that it is not empty, 
or alternatively, add logic to just set it to entry block of `outerfn` if it is



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:490
   // of the outlined function.
-  InsertPointTy AllocaIP(PRegEntryBB,
- PRegEntryBB->getTerminator()->getIterator());
-  Builder.restoreIP(AllocaIP);
+  AllocaIP =
+  InsertPointTy(PRegEntryBB, PRegEntryBB->getTerminator()->getIterator());

NIT: I think it would be better to give each a different name, to avoid 
unnecessary confusion. maybe `OuterAllocaIP` and `innerAllocaIP`? Also, we 
don't overwrite the `outerAllocaIP` in case it's needed later?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D82470/new/

https://reviews.llvm.org/D82470



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D82470: [OpenMP][IRBuilder] Support allocas in nested parallel regions

2020-07-03 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added inline comments.



Comment at: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h:282
+  IRBuilder<> AllocaBuilder;
+
   /// Map to remember source location strings

jdoerfert wrote:
> fghanim wrote:
> > jdoerfert wrote:
> > > fghanim wrote:
> > > > jdoerfert wrote:
> > > > > fghanim wrote:
> > > > > > What's the benefit of this over just maintaining an alloca 
> > > > > > insertion point?
> > > > > > 
> > > > > > With this we will have 3 `IRBuilder`s to maintain and keep track 
> > > > > > of: the clang (or flang) `IRBuilder`, the OMP `IRBuilder` and the 
> > > > > > `allocaBuilder`
> > > > > No functional difference. Basically the same reason why clang has an 
> > > > > AllocIRBuilder (I think), we can use it independently of the other 
> > > > > one. The alternative is to save the builder IP, set it to the alloca 
> > > > > IP, do alloca stuff, reset the builder IP, when you use the 
> > > > > AllocaIRBuilder below. Again, no functional difference, just less 
> > > > > setting/resetting of IPs.
> > > > I understand where you are coming from, and I understand that 
> > > > functionally there is mostly no change - for now. But I prefer we do a 
> > > > small struct with some RAII, over adding an entire builder. I think 
> > > > it's a bit overkill and will be a source to lots of trouble.
> > > > 
> > > > Clang maintains an instruction `AllocaInsertPt`, not a specific 
> > > > builder. Speaking of which, This is completely from memory (i.e. check 
> > > > to make sure); I think we already save the previous `AllocaInsertionPt` 
> > > > in clang as part of the `bodyGenCB`. In which case, this is not needed; 
> > > > a nested `parallel` will always be generated by clang as part of the 
> > > > `bodyGenCB`, which in turn is going to save the Alloca insertion point 
> > > > for us. 
> > > > To enable the creation of the allocas for `TID` and `ZeroAddr` in the 
> > > > outer function, Why not pass current `AllocaInsertionPt` as an arg. to 
> > > > `createParallel`
> > > > I understand where you are coming from, and I understand that 
> > > > functionally there is mostly no change - for now. But I prefer we do a 
> > > > small struct with some RAII, over adding an entire builder. I think 
> > > > it's a bit overkill and will be a source to lots of trouble.
> > > 
> > > Could you explain what kind of overkill you see here? And maybe also the 
> > > lots of trouble you expect from a builder versus a insertion point? It is 
> > > the same information, just packed in a different struct, right?
> > > 
> > > 
> > > > Clang maintains an instruction AllocaInsertPt, not a specific builder. 
> > > 
> > > Right.
> > > 
> > > 
> > > > Speaking of which, This is completely from memory (i.e. check to make 
> > > > sure); I think we already save the previous AllocaInsertionPt in clang 
> > > > as part of the bodyGenCB. 
> > > 
> > > I think so.
> > > 
> > > 
> > > > In which case, this is not needed; 
> > > 
> > > Not strictly, no. 
> > > 
> > > > a nested parallel will always be generated by clang as part of the 
> > > > bodyGenCB,
> > > 
> > > Yes.
> > > 
> > > > which in turn is going to save the Alloca insertion point for us.
> > > 
> > > Yes.
> > > 
> > > > To enable the creation of the allocas for TID and ZeroAddr in the outer 
> > > > function, Why not pass current AllocaInsertionPt as an arg. to 
> > > > createParallel
> > > 
> > > So far, because this will not be the only one that needs to create 
> > > allocas. If you want to add the allocaIP to all runtime calls that create 
> > > allocas, I'm fine with that too.
> > > Could you explain what kind of overkill you see here?
> > Using an IRBuilder when an insertion point suffices
> > 
> > > And maybe also the lots of trouble you expect from a builder versus a 
> > > insertion point?
> > Currently, when using the OMPBuilder you need to juggle two IRBuilders, 
> > let's not make them 3 :)
> > 
> > > It is the same information, just packed in a different struct, right?
> > no it is not. `InsetrionPoint` vs  struct that contains `Insertionpoint` + 
> > other things. But that's beside the point
> > 
> > I mean, is the `AllocaBuilder` providing a new functionality other than a 
> > convenient way to keep track of the Alloca `InsertionPoint`?
> > Personally, I prefer some suitable structure to your needs and a stack 
> > (i.e. the way clang does it) . This should also resolve the todo below. 
> > Alternatively, pass an allocation point as an argument as suggested 
> > earlier. I am open to any third way if you have any.
> > Personally, I prefer some suitable structure to your needs and a stack 
> > (i.e. the way clang does it).
> 
> The stack is already here, implicitly, and actually on the stack. With
> `IRBuilder<>::InsertPointGuard AIPG(AllocaBuilder);`
> we have all the stack we need without leaking the state.
> The updates to the insertion point are done for use and the AllocaBuilder is 
> always ready to be used. No need to 

[PATCH] D82822: [OpenMP][FIX] Consistently use OpenMPIRBuilder if requested

2020-07-02 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

LGTM


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D82822/new/

https://reviews.llvm.org/D82822



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79677: [Clang][OpenMP][OMPBuilder] (1/4) Privatize `parallel` for `OMPBuilder`

2020-07-01 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked 3 inline comments as done.
fghanim added a comment.

In D79677#2119019 , @kiranchandramohan 
wrote:

> Is the ordering of code generation for clauses important?
>  copyin -> firstprivate -> barrier -> private


if we emitted a copyin, then prob. yes. otherwise no.




Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:1673
 //
 // TODO: This defaults to shared right now.
 auto PrivCB = [](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,

kiranchandramohan wrote:
> Should this change in this patch?
No, still used for shared variables.

but the todo should go away.



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:1729-1736
+  if (!OMP_Entry->getTerminator()) {
+OMP_Entry->getInstList().push_back(EntryBI);
+  } else if (Builder.GetInsertBlock()->getTerminator()) {
+EntryBI->dropAllReferences();
+EntryBI->deleteValue();
+  } else {
+Builder.Insert(EntryBI);

kiranchandramohan wrote:
> Nit: What do these three cases correspond to? A comment might be useful.
Just checks to see what to do with the terminator of the entry block.

I'll add a comment in an update



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:1743
+  Builder.SetInsertPoint();
+  PrivateScope.ForceCleanup();
+  Builder.Insert(ContTI);

kiranchandramohan wrote:
> Not a comment about this patch: While the context makes it clear, the name 
> does not suggest that this function is emitting something.
you mean `ForceClean()`?
it forces the emission of cleanup code - instead of just as part of the 
Privatescope Dctor.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79677/new/

https://reviews.llvm.org/D79677



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D82722: [OpenMP][IRBuilder] Support nested parallel regions

2020-06-29 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

OK. Thanks :)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D82722/new/

https://reviews.llvm.org/D82722



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D82470: [OpenMP][IRBuilder] Support allocas in nested parallel regions

2020-06-29 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim requested changes to this revision.
fghanim added inline comments.
This revision now requires changes to proceed.



Comment at: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h:282
+  IRBuilder<> AllocaBuilder;
+
   /// Map to remember source location strings

jdoerfert wrote:
> fghanim wrote:
> > jdoerfert wrote:
> > > fghanim wrote:
> > > > What's the benefit of this over just maintaining an alloca insertion 
> > > > point?
> > > > 
> > > > With this we will have 3 `IRBuilder`s to maintain and keep track of: 
> > > > the clang (or flang) `IRBuilder`, the OMP `IRBuilder` and the 
> > > > `allocaBuilder`
> > > No functional difference. Basically the same reason why clang has an 
> > > AllocIRBuilder (I think), we can use it independently of the other one. 
> > > The alternative is to save the builder IP, set it to the alloca IP, do 
> > > alloca stuff, reset the builder IP, when you use the AllocaIRBuilder 
> > > below. Again, no functional difference, just less setting/resetting of 
> > > IPs.
> > I understand where you are coming from, and I understand that functionally 
> > there is mostly no change - for now. But I prefer we do a small struct with 
> > some RAII, over adding an entire builder. I think it's a bit overkill and 
> > will be a source to lots of trouble.
> > 
> > Clang maintains an instruction `AllocaInsertPt`, not a specific builder. 
> > Speaking of which, This is completely from memory (i.e. check to make 
> > sure); I think we already save the previous `AllocaInsertionPt` in clang as 
> > part of the `bodyGenCB`. In which case, this is not needed; a nested 
> > `parallel` will always be generated by clang as part of the `bodyGenCB`, 
> > which in turn is going to save the Alloca insertion point for us. 
> > To enable the creation of the allocas for `TID` and `ZeroAddr` in the outer 
> > function, Why not pass current `AllocaInsertionPt` as an arg. to 
> > `createParallel`
> > I understand where you are coming from, and I understand that functionally 
> > there is mostly no change - for now. But I prefer we do a small struct with 
> > some RAII, over adding an entire builder. I think it's a bit overkill and 
> > will be a source to lots of trouble.
> 
> Could you explain what kind of overkill you see here? And maybe also the lots 
> of trouble you expect from a builder versus a insertion point? It is the same 
> information, just packed in a different struct, right?
> 
> 
> > Clang maintains an instruction AllocaInsertPt, not a specific builder. 
> 
> Right.
> 
> 
> > Speaking of which, This is completely from memory (i.e. check to make 
> > sure); I think we already save the previous AllocaInsertionPt in clang as 
> > part of the bodyGenCB. 
> 
> I think so.
> 
> 
> > In which case, this is not needed; 
> 
> Not strictly, no. 
> 
> > a nested parallel will always be generated by clang as part of the 
> > bodyGenCB,
> 
> Yes.
> 
> > which in turn is going to save the Alloca insertion point for us.
> 
> Yes.
> 
> > To enable the creation of the allocas for TID and ZeroAddr in the outer 
> > function, Why not pass current AllocaInsertionPt as an arg. to 
> > createParallel
> 
> So far, because this will not be the only one that needs to create allocas. 
> If you want to add the allocaIP to all runtime calls that create allocas, I'm 
> fine with that too.
> Could you explain what kind of overkill you see here?
Using an IRBuilder when an insertion point suffices

> And maybe also the lots of trouble you expect from a builder versus a 
> insertion point?
Currently, when using the OMPBuilder you need to juggle two IRBuilders, let's 
not make them 3 :)

> It is the same information, just packed in a different struct, right?
no it is not. `InsetrionPoint` vs  struct that contains `Insertionpoint` + 
other things. But that's beside the point

I mean, is the `AllocaBuilder` providing a new functionality other than a 
convenient way to keep track of the Alloca `InsertionPoint`?
Personally, I prefer some suitable structure to your needs and a stack (i.e. 
the way clang does it) . This should also resolve the todo below. 
Alternatively, pass an allocation point as an argument as suggested earlier. I 
am open to any third way if you have any.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D82470/new/

https://reviews.llvm.org/D82470



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79676: [Clang][OpenMP][OMPBuilder] Moving OMP allocation and cache creation code to OMPBuilderCBHelpers

2020-06-29 Thread Fady Ghanim via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG80e15b4574f4: [Clang][OpenMP][OMPBuilder] Moving OMP 
allocation and cache creation code to… (authored by fghanim).

Changed prior to commit:
  https://reviews.llvm.org/D79676?vs=269584=273983#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79676/new/

https://reviews.llvm.org/D79676

Files:
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CGExpr.cpp
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CodeGenFunction.h

Index: clang/lib/CodeGen/CodeGenFunction.h
===
--- clang/lib/CodeGen/CodeGenFunction.h
+++ clang/lib/CodeGen/CodeGenFunction.h
@@ -26,6 +26,7 @@
 #include "clang/AST/ExprCXX.h"
 #include "clang/AST/ExprObjC.h"
 #include "clang/AST/ExprOpenMP.h"
+#include "clang/AST/StmtOpenMP.h"
 #include "clang/AST/Type.h"
 #include "clang/Basic/ABI.h"
 #include "clang/Basic/CapturedStmt.h"
@@ -80,6 +81,7 @@
 class OMPUseDeviceAddrClause;
 class ReturnsNonNullAttr;
 class SVETypeFlags;
+class OMPExecutableDirective;
 
 namespace analyze_os_log {
 class OSLogBufferLayout;
@@ -259,114 +261,6 @@
 unsigned Index;
   };
 
-  // Helper class for the OpenMP IR Builder. Allows reusability of code used for
-  // region body, and finalization codegen callbacks. This will class will also
-  // contain privatization functions used by the privatization call backs
-  struct OMPBuilderCBHelpers {
-
-using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
-
-/// Emit the Finalization for an OMP region
-/// \param CGF	The Codegen function this belongs to
-/// \param IP	Insertion point for generating the finalization code.
-static void FinalizeOMPRegion(CodeGenFunction , InsertPointTy IP) {
-  CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
-  assert(IP.getBlock()->end() != IP.getPoint() &&
- "OpenMP IR Builder should cause terminated block!");
-
-  llvm::BasicBlock *IPBB = IP.getBlock();
-  llvm::BasicBlock *DestBB = IPBB->getUniqueSuccessor();
-  assert(DestBB && "Finalization block should have one successor!");
-
-  // erase and replace with cleanup branch.
-  IPBB->getTerminator()->eraseFromParent();
-  CGF.Builder.SetInsertPoint(IPBB);
-  CodeGenFunction::JumpDest Dest = CGF.getJumpDestInCurrentScope(DestBB);
-  CGF.EmitBranchThroughCleanup(Dest);
-}
-
-/// Emit the body of an OMP region
-/// \param CGF	The Codegen function this belongs to
-/// \param RegionBodyStmt	The body statement for the OpenMP region being
-/// 			 generated
-/// \param CodeGenIP	Insertion point for generating the body code.
-/// \param FiniBB	The finalization basic block
-static void EmitOMPRegionBody(CodeGenFunction ,
-  const Stmt *RegionBodyStmt,
-  InsertPointTy CodeGenIP,
-  llvm::BasicBlock ) {
-  llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
-  if (llvm::Instruction *CodeGenIPBBTI = CodeGenIPBB->getTerminator())
-CodeGenIPBBTI->eraseFromParent();
-
-  CGF.Builder.SetInsertPoint(CodeGenIPBB);
-
-  CGF.EmitStmt(RegionBodyStmt);
-
-  if (CGF.Builder.saveIP().isSet())
-CGF.Builder.CreateBr();
-}
-
-/// RAII for preserving necessary info during Outlined region body codegen.
-class OutlinedRegionBodyRAII {
-
-  llvm::AssertingVH OldAllocaIP;
-  CodeGenFunction::JumpDest OldReturnBlock;
-  CodeGenFunction 
-
-public:
-  OutlinedRegionBodyRAII(CodeGenFunction , InsertPointTy ,
- llvm::BasicBlock )
-  : CGF(cgf) {
-assert(AllocaIP.isSet() &&
-   "Must specify Insertion point for allocas of outlined function");
-OldAllocaIP = CGF.AllocaInsertPt;
-CGF.AllocaInsertPt = &*AllocaIP.getPoint();
-
-OldReturnBlock = CGF.ReturnBlock;
-CGF.ReturnBlock = CGF.getJumpDestInCurrentScope();
-  }
-
-  ~OutlinedRegionBodyRAII() {
-CGF.AllocaInsertPt = OldAllocaIP;
-CGF.ReturnBlock = OldReturnBlock;
-  }
-};
-
-/// RAII for preserving necessary info during inlined region body codegen.
-class InlinedRegionBodyRAII {
-
-  llvm::AssertingVH OldAllocaIP;
-  CodeGenFunction 
-
-public:
-  InlinedRegionBodyRAII(CodeGenFunction , InsertPointTy ,
-llvm::BasicBlock )
-  : CGF(cgf) {
-// Alloca insertion block should be in the entry block of the containing
-// function so it expects an empty AllocaIP in which case will reuse the
-// old alloca insertion point, or a new AllocaIP in the same block as
-// the old one
-assert((!AllocaIP.isSet() ||
-CGF.AllocaInsertPt->getParent() == AllocaIP.getBlock()) &&
-   "Insertion point should be in the 

[PATCH] D82722: [OpenMP][IRBuilder] Support nested parallel regions

2020-06-29 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

Thanks for working on this. LGTM.
Did you make any changes other than splitting from D82470 
 ?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D82722/new/

https://reviews.llvm.org/D82722



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-06-27 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim closed this revision.
fghanim added a comment.

commited:  rG82b8236cf248 



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79675/new/

https://reviews.llvm.org/D79675



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D82470: [OpenMP][IRBuilder] Support nested parallel regions

2020-06-24 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added inline comments.



Comment at: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h:282
+  IRBuilder<> AllocaBuilder;
+
   /// Map to remember source location strings

jdoerfert wrote:
> fghanim wrote:
> > What's the benefit of this over just maintaining an alloca insertion point?
> > 
> > With this we will have 3 `IRBuilder`s to maintain and keep track of: the 
> > clang (or flang) `IRBuilder`, the OMP `IRBuilder` and the `allocaBuilder`
> No functional difference. Basically the same reason why clang has an 
> AllocIRBuilder (I think), we can use it independently of the other one. The 
> alternative is to save the builder IP, set it to the alloca IP, do alloca 
> stuff, reset the builder IP, when you use the AllocaIRBuilder below. Again, 
> no functional difference, just less setting/resetting of IPs.
I understand where you are coming from, and I understand that functionally 
there is mostly no change - for now. But I prefer we do a small struct with 
some RAII, over adding an entire builder. I think it's a bit overkill and will 
be a source to lots of trouble.

Clang maintains an instruction `AllocaInsertPt`, not a specific builder. 
Speaking of which, This is completely from memory (i.e. check to make sure); I 
think we already save the previous `AllocaInsertionPt` in clang as part of the 
`bodyGenCB`. In which case, this is not needed; a nested `parallel` will always 
be generated by clang as part of the `bodyGenCB`, which in turn is going to 
save the Alloca insertion point for us. 
To enable the creation of the allocas for `TID` and `ZeroAddr` in the outer 
function, Why not pass current `AllocaInsertionPt` as an arg. to 
`createParallel`



Comment at: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h:294
 PostOutlineCBTy PostOutlineCB;
+BasicBlock *EntryBB, *ExitBB;
+

jdoerfert wrote:
> fghanim wrote:
> > I see two benefits of passing entry and exit blocks, as opposed to what we 
> > used to do:
> > 1. less memory, but in return we collect blocks twice (i.e. O(N) mem & 
> > O(N+E) work vs O(1) mem and 2 * O(N+E) work ). Do you expect that the 
> > vector is likely to become large enough where it is a problem? if not, 
> > what's the benefit of the change?
> > 
> > 2. If some blocks are added later, then this becomes a correctness issue. 
> > Which is unlikely since it happens after the body codegen is complete. 
> > However, if I am mistaken, shouldn't we also delay searching for 
> > inputs/outputs?
> It is 2. Here, finalization steps will outline the inner region and introduce 
> split blocks in the process. Those were not in the outer regions blocks 
> vector which caused problems. The inputs/outputs are not allowed to change 
> though, that invariant seems to hold fine.
> (Once we do OpenMP loop transformations on this level other new blocks would 
> be introduced during finalize.)
Oh right. Thanks for explaining that. :)
Nit: if possible, add a simple assert to check that inputs/outputs didn't change



Comment at: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h:299
+void collectBlocks(SmallPtrSetImpl ,
+   SmallVectorImpl );
   };

jdoerfert wrote:
> fghanim wrote:
> > What is the benefit of passing `blockSet` when it is exclusively used 
> > inside of `collectBlocks`? 
> > I don't think I saw a usage of it in calling functions. am I missing 
> > something?
> It's used in line 684 outside of collectBlocks.
Oh, Thanks. I missed that. :)
In this case; I think both the Blockset, and BlockVector have the same 
contents, correct? can't we use the vector instead on line 684, and keep the 
set local?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D82470/new/

https://reviews.llvm.org/D82470



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D82470: [OpenMP][IRBuilder] Support nested parallel regions

2020-06-24 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

Thanks for the Patch. I have few questions to help me understand what's going 
on.




Comment at: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h:282
+  IRBuilder<> AllocaBuilder;
+
   /// Map to remember source location strings

What's the benefit of this over just maintaining an alloca insertion point?

With this we will have 3 `IRBuilder`s to maintain and keep track of: the clang 
(or flang) `IRBuilder`, the OMP `IRBuilder` and the `allocaBuilder`



Comment at: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h:294
 PostOutlineCBTy PostOutlineCB;
+BasicBlock *EntryBB, *ExitBB;
+

I see two benefits of passing entry and exit blocks, as opposed to what we used 
to do:
1. less memory, but in return we collect blocks twice (i.e. O(N) mem & O(N+E) 
work vs O(1) mem and 2 * O(N+E) work ). Do you expect that the vector is likely 
to become large enough where it is a problem? if not, what's the benefit of the 
change?

2. If some blocks are added later, then this becomes a correctness issue. Which 
is unlikely since it happens after the body codegen is complete. However, if I 
am mistaken, shouldn't we also delay searching for inputs/outputs?



Comment at: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h:299
+void collectBlocks(SmallPtrSetImpl ,
+   SmallVectorImpl );
   };

What is the benefit of passing `blockSet` when it is exclusively used inside of 
`collectBlocks`? 
I don't think I saw a usage of it in calling functions. am I missing something?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D82470/new/

https://reviews.llvm.org/D82470



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79677: [Clang][OpenMP][OMPBuilder] (1/4) Privatize `parallel` for `OMPBuilder`

2020-06-15 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

ping - please suggest reviewers I can add to review the clang side of things?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79677/new/

https://reviews.llvm.org/D79677



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-06-15 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

Ping. 
Does this patch need further changes?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79675/new/

https://reviews.llvm.org/D79675



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-06-10 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

you are responding to a comment from 2 weeks ago, so let's just move on.

I uploaded a new patch yesterday. You have any comments on this patch?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79675/new/

https://reviews.llvm.org/D79675



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D81484: [Clang][OpenMP][OMPBuilder] (4/4) Privatize `parallel` for `OMPBuilder`

2020-06-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked 6 inline comments as done.
fghanim added inline comments.



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:1777
 }
+
+bool CodeGenFunction::OMPBuilderCBHelpers::EmitOMPFirstprivateClause(

Same as current implementation, with certain changes around the end. changes 
will be noted below.



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:1816
+  !FD->getType()->isReferenceType() &&
+  (!VD || !VD->hasAttr())) {
+if (CapturedVarsInfoMap[OrigVD].CapturedKind !=

next line is new



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:1901
+return Emission.getAllocatedAddress();
+  });
+} else {

The else part below is almost entirely rewritten



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:1982
+
+CGF.LocalDeclMap.erase(VDInit);
+if (ThisFirstprivateIsLastprivate &&

end of new impl.



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:2052
+CV = CGF.EmitLoadOfScalar(DstLV, CurCap->getLocation());
+  }
+  CapturedVarsValueMap[CurCap->getCapturedVar()] = {

next line is new



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:2056
+} else {
+  assert(CurCap->capturesVariable() && "Expected capture by reference.");
+  CapturedVarsValueMap[CurCap->getCapturedVar()] = {

next line is new


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D81484/new/

https://reviews.llvm.org/D81484



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D81483: [Clang][OpenMP][OMPBuilder] (3/4) Privatize `parallel` for `OMPBuilder`

2020-06-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked 3 inline comments as done.
fghanim added inline comments.



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:1554
  llvm::SmallVectorImpl &) {}
 
+bool CodeGenFunction::OMPBuilderCBHelpers::EmitOMPCopyinClause(

Note to reviewers:
This keeps most of old implementation of `copyin` clause, except it uses part 
of the `OMPBuilder` now. Changes are noted below.



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:1582
+  assert(CGF.CapturedStmtInfo->lookup(VD) &&
+ "Copyin threadprivates should have been captured!");
+  const auto *VDCanon = VD->getCanonicalDecl();

added following few lines



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:1600
+}
+
+// Get the address of the threadprivate variable.

Wherever needed - changed the following section to use `OMPBuilder`


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D81483/new/

https://reviews.llvm.org/D81483



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79677: [clang][OpenMP][OMPIRBuilder] Adding some Privatization clauses to OpenMP `parallel` Directive

2020-06-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

@jdoerfert Please suggest reviewer's for this, and I will add them to other 
clang related patches


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79677/new/

https://reviews.llvm.org/D79677



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79677: [clang][OpenMP][OMPIRBuilder] Adding some Privatization clauses to OpenMP `parallel` Directive

2020-06-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 269589.
fghanim added a comment.
Herald added subscribers: aaron.ballman, sstefan1.

- rebase
- splitting patch into 4 ( this, D81482  , 
D81483  , D81484 
 )
- addressing reviewer's comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79677/new/

https://reviews.llvm.org/D79677

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp


Index: clang/lib/CodeGen/CGStmtOpenMP.cpp
===
--- clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -1683,13 +1683,65 @@
 const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel);
 const Stmt *ParallelRegionBodyStmt = CS->getCapturedStmt();
 
-auto BodyGenCB = [ParallelRegionBodyStmt,
-  this](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
-llvm::BasicBlock ) {
+OMPParallelScope Scope(*this, S);
+llvm::SmallDenseMap
+CapturedVarsInfoMap;
+
+OMPBuilderCBHelpers::GenerateOpenMPCapturedVars(*this, *CS,
+CapturedVarsInfoMap);
+
+auto BodyGenCB = [ParallelRegionBodyStmt, , this, ,
+  ](InsertPointTy AllocaIP,
+InsertPointTy CodeGenIP,
+llvm::BasicBlock ) {
   OMPBuilderCBHelpers::OutlinedRegionBodyRAII ORB(*this, AllocaIP,
   ContinuationBB);
+
+  OMPPrivateScope PrivateScope(*this);
+  llvm::BasicBlock *OMP_Entry = AllocaIP.getBlock();
+  // Emitting Copyin clause
+  Builder.SetInsertPoint(OMP_Entry->getTerminator());
+  bool Copyins =
+  OMPBuilderCBHelpers::EmitOMPCopyinClause(*this, S, AllocaIP);
+
+  // restoring alloca insertion point to entry block since it moved while
+  // emitting 'copyin' blocks
+  AllocaInsertPt = OMP_Entry->getFirstNonPHIOrDbgOrLifetime();
+  llvm::BranchInst *EntryBI =
+  cast(OMP_Entry->getTerminator());
+  EntryBI->removeFromParent();
+
+  if (Builder.GetInsertBlock() == OMP_Entry)
+Builder.SetInsertPoint(OMP_Entry);
+  OMPBuilderCBHelpers::EmitOMPFirstprivateClause(*this, S, PrivateScope,
+ CapturedVarsInfoMap);
+  if (Copyins) {
+// Emit implicit barrier to synchronize threads and avoid data races on
+// propagation master's thread values of threadprivate variables to
+// local instances of that variables of all other implicit threads.
+OMPBuilder->CreateBarrier(Builder, OMPD_barrier, /*EmitChecks=*/false,
+  /*ForceSimpleCall=*/true);
+  }
+
+  EmitOMPPrivateClause(S, PrivateScope);
+  (void)PrivateScope.Privatize();
+
+  if (!OMP_Entry->getTerminator()) {
+OMP_Entry->getInstList().push_back(EntryBI);
+  } else if (Builder.GetInsertBlock()->getTerminator()) {
+EntryBI->dropAllReferences();
+EntryBI->deleteValue();
+  } else {
+Builder.Insert(EntryBI);
+  }
+
   OMPBuilderCBHelpers::EmitOMPRegionBody(*this, ParallelRegionBodyStmt,
  CodeGenIP, ContinuationBB);
+  llvm::Instruction *ContTI = ContinuationBB.getTerminator();
+  ContTI->removeFromParent();
+  Builder.SetInsertPoint();
+  PrivateScope.ForceCleanup();
+  Builder.Insert(ContTI);
 };
 
 CGCapturedStmtInfo CGSI(*CS, CR_OpenMP);


Index: clang/lib/CodeGen/CGStmtOpenMP.cpp
===
--- clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -1683,13 +1683,65 @@
 const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel);
 const Stmt *ParallelRegionBodyStmt = CS->getCapturedStmt();
 
-auto BodyGenCB = [ParallelRegionBodyStmt,
-  this](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
-llvm::BasicBlock ) {
+OMPParallelScope Scope(*this, S);
+llvm::SmallDenseMap
+CapturedVarsInfoMap;
+
+OMPBuilderCBHelpers::GenerateOpenMPCapturedVars(*this, *CS,
+CapturedVarsInfoMap);
+
+auto BodyGenCB = [ParallelRegionBodyStmt, , this, ,
+  ](InsertPointTy AllocaIP,
+InsertPointTy CodeGenIP,
+llvm::BasicBlock ) {
   OMPBuilderCBHelpers::OutlinedRegionBodyRAII ORB(*this, AllocaIP,
   ContinuationBB);
+
+  OMPPrivateScope PrivateScope(*this);
+  llvm::BasicBlock *OMP_Entry = AllocaIP.getBlock();
+  // Emitting Copyin clause
+  

[PATCH] D81484: [Clang][OpenMP][OMPBuilder] (4/4) Privatize `parallel` for `OMPBuilder`

2020-06-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim created this revision.
fghanim added a reviewer: jdoerfert.
Herald added subscribers: cfe-commits, sstefan1, guansong, yaxunl.
Herald added a project: clang.
fghanim added a child revision: D79677: [clang][OpenMP][OMPIRBuilder] Adding 
some Privatization clauses to OpenMP `parallel` Directive.
fghanim added a parent revision: D79676: [Clang][OpenMP][OMPBuilder] Moving OMP 
allocation and cache creation code to OMPBuilderCBHelpers.

Adding support/implementation of `firstprivate` to OMPBuilder's
implementation of `omp parallel`


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81484

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CodeGenFunction.h
  clang/test/OpenMP/parallel_firstprivate_codegen.cpp

Index: clang/test/OpenMP/parallel_firstprivate_codegen.cpp
===
--- clang/test/OpenMP/parallel_firstprivate_codegen.cpp
+++ clang/test/OpenMP/parallel_firstprivate_codegen.cpp
@@ -1,8 +1,11 @@
-// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple i386-pc-linux-gnu -emit-llvm %s -o - | FileCheck %s -check-prefix=CHECK -check-prefix=CHECK-32
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple i386-pc-linux-gnu -emit-llvm %s -o - | FileCheck %s -check-prefixes=ALL,ALL-32,CHECK,CHECK-32
 // RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple i386-pc-linux-gnu -emit-pch -o %t %s
-// RUN: %clang_cc1 -fopenmp -x c++ -triple i386-pc-linux-gnu -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s -check-prefix=CHECK -check-prefix=CHECK-32
+// RUN: %clang_cc1 -fopenmp -x c++ -triple i386-pc-linux-gnu -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s -check-prefixes=ALL,ALL-32,CHECK,CHECK-32
 // RUN: %clang_cc1 -verify -fopenmp -x c++ -std=c++11 -DLAMBDA -triple i386-pc-linux-gnu -emit-llvm %s -o - | FileCheck -check-prefix=LAMBDA -check-prefix=LAMBDA-32 %s
 // RUN: %clang_cc1 -verify -fopenmp -x c++ -fblocks -DBLOCKS -triple i386-pc-linux-gnu -emit-llvm %s -o - | FileCheck -check-prefix=BLOCKS -check-prefix=BLOCKS-32 %s
+// RUN: %clang_cc1 -verify -fopenmp -fopenmp-enable-irbuilder -x c++ -triple i386-pc-linux-gnu -emit-llvm %s -o - | FileCheck %s -check-prefixes=ALL,ALL-32,IRBUILDER,IRBUILDER-32
+// RUN: %clang_cc1 -fopenmp  -fopenmp-enable-irbuilder -x c++ -std=c++11 -triple i386-pc-linux-gnu -emit-pch -o %t %s
+// RUN: %clang_cc1 -fopenmp  -fopenmp-enable-irbuilder -x c++ -triple i386-pc-linux-gnu -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s -check-prefixes=ALL,ALL-32,IRBUILDER,IRBUILDER-32
 
 // RUN: %clang_cc1 -verify -fopenmp-simd -x c++ -triple i386-pc-linux-gnu -emit-llvm %s -o - | FileCheck --check-prefix SIMD-ONLY0 %s
 // RUN: %clang_cc1 -fopenmp-simd -x c++ -std=c++11 -triple i386-pc-linux-gnu -emit-pch -o %t %s
@@ -11,11 +14,14 @@
 // RUN: %clang_cc1 -verify -fopenmp-simd -x c++ -fblocks -DBLOCKS -triple i386-pc-linux-gnu -emit-llvm %s -o - | FileCheck --check-prefix SIMD-ONLY0 %s
 // SIMD-ONLY0-NOT: {{__kmpc|__tgt}}
 
-// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-pc-linux-gnu -emit-llvm %s -o - | FileCheck %s -check-prefix=CHECK -check-prefix=CHECK-64
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-pc-linux-gnu -emit-llvm %s -o - | FileCheck %s -check-prefixes=ALL,ALL-64,CHECK,CHECK-64
 // RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple x86_64-pc-linux-gnu -emit-pch -o %t %s
-// RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-pc-linux-gnu -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s -check-prefix=CHECK -check-prefix=CHECK-64
+// RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-pc-linux-gnu -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s -check-prefixes=ALL,ALL-64,CHECK,CHECK-64
 // RUN: %clang_cc1 -verify -fopenmp -x c++ -std=c++11 -DLAMBDA -triple x86_64-pc-linux-gnu -emit-llvm %s -o - | FileCheck -check-prefix=LAMBDA -check-prefix=LAMBDA-64 %s
 // RUN: %clang_cc1 -verify -fopenmp -x c++ -fblocks -DBLOCKS -triple x86_64-pc-linux-gnu -emit-llvm %s -o - | FileCheck -check-prefix=BLOCKS -check-prefix=BLOCKS-64 %s
+// RUN: %clang_cc1 -verify -fopenmp -fopenmp-enable-irbuilder -x c++ -triple x86_64-pc-linux-gnu -emit-llvm %s -o - | FileCheck %s -check-prefixes=ALL,ALL-64,IRBUILDER,IRBUILDER-64
+// RUN: %clang_cc1 -fopenmp -fopenmp-enable-irbuilder -x c++ -std=c++11 -triple x86_64-pc-linux-gnu -emit-pch -o %t %s
+// RUN: %clang_cc1 -fopenmp -fopenmp-enable-irbuilder -x c++ -triple x86_64-pc-linux-gnu -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s -check-prefixes=ALL,ALL-64,IRBUILDER,IRBUILDER-64
 
 // RUN: %clang_cc1 -verify -fopenmp-simd -x c++ -triple x86_64-pc-linux-gnu -emit-llvm %s -o - | FileCheck --check-prefix SIMD-ONLY1 %s
 // RUN: %clang_cc1 -fopenmp-simd -x c++ -std=c++11 -triple x86_64-pc-linux-gnu -emit-pch -o %t %s
@@ -62,6 +68,7 @@
   int e[4];
   SS(int ) : a(0), b(0), c(d) {
 #pragma omp parallel firstprivate(a, b, c, e)
+
 #ifdef LAMBDA
 [&]() 

[PATCH] D81482: [Clang][OpenMP][OMPBuilder] (2/4) Privatize `parallel` for `OMPBuilder`

2020-06-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim created this revision.
fghanim added a reviewer: jdoerfert.
Herald added subscribers: cfe-commits, sstefan1, guansong, yaxunl.
Herald added a project: clang.
fghanim added a child revision: D79677: [clang][OpenMP][OMPIRBuilder] Adding 
some Privatization clauses to OpenMP `parallel` Directive.
fghanim added a parent revision: D79676: [Clang][OpenMP][OMPBuilder] Moving OMP 
allocation and cache creation code to OMPBuilderCBHelpers.

adding lit-test for usage of 'private' in the OMPBuilder implementation
of `omp parallel`

note: this was originally a part of D79677 


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81482

Files:
  clang/test/OpenMP/parallel_private_codegen.cpp

Index: clang/test/OpenMP/parallel_private_codegen.cpp
===
--- clang/test/OpenMP/parallel_private_codegen.cpp
+++ clang/test/OpenMP/parallel_private_codegen.cpp
@@ -1,8 +1,9 @@
-// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-unknown -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-unknown -emit-llvm %s -o - | FileCheck %s -check-prefixes=ALL,CHECK
 // RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple x86_64-unknown-unknown -emit-pch -o %t %s
-// RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-unknown-unknown -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-unknown-unknown -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s -check-prefixes=ALL,CHECK
 // RUN: %clang_cc1 -verify -fopenmp -x c++ -std=c++11 -DLAMBDA -triple %itanium_abi_triple -emit-llvm %s -o - | FileCheck -check-prefix=LAMBDA %s
 // RUN: %clang_cc1 -verify -fopenmp -x c++ -fblocks -DBLOCKS -triple %itanium_abi_triple -emit-llvm %s -o - | FileCheck -check-prefix=BLOCKS %s
+// RUN: %clang_cc1 -verify -fopenmp -fopenmp-enable-irbuilder -x c++ -triple x86_64-unknown-unknown -emit-llvm %s -o - | FileCheck %s -check-prefixes=ALL,IRBUILDER
 
 // RUN: %clang_cc1 -verify -fopenmp-simd -x c++ -triple x86_64-unknown-unknown -emit-llvm %s -o - | FileCheck --check-prefix SIMD-ONLY0 %s
 // RUN: %clang_cc1 -fopenmp-simd -x c++ -std=c++11 -triple x86_64-unknown-unknown -emit-pch -o %t %s
@@ -91,12 +92,12 @@
   }
 };
 
-// CHECK: [[SS_TY:%.+]] = type { i{{[0-9]+}}, i8
+// ALL: [[SS_TY:%.+]] = type { i{{[0-9]+}}, i8
 // LAMBDA: [[SS_TY:%.+]] = type { i{{[0-9]+}}, i8
 // BLOCKS: [[SS_TY:%.+]] = type { i{{[0-9]+}}, i8
-// CHECK: [[S_FLOAT_TY:%.+]] = type { float }
-// CHECK: [[S_INT_TY:%.+]] = type { i{{[0-9]+}} }
-// CHECK: [[SST_TY:%.+]] = type { i{{[0-9]+}} }
+// ALL: [[S_FLOAT_TY:%.+]] = type { float }
+// ALL: [[S_INT_TY:%.+]] = type { i{{[0-9]+}} }
+// ALL: [[SST_TY:%.+]] = type { i{{[0-9]+}} }
 template 
 T tmain() {
   S test;
@@ -273,63 +274,93 @@
 #endif
 }
 
-// CHECK: define i{{[0-9]+}} @main()
-// CHECK: [[TEST:%.+]] = alloca [[S_FLOAT_TY]],
-// CHECK: call {{.*}} [[S_FLOAT_TY_DEF_CONSTR:@.+]]([[S_FLOAT_TY]]* [[TEST]])
-// CHECK: call void (%{{.+}}*, i{{[0-9]+}}, void (i{{[0-9]+}}*, i{{[0-9]+}}*, ...)*, ...) @__kmpc_fork_call(%{{.+}}* @{{.+}}, i{{[0-9]+}} 0, void (i{{[0-9]+}}*, i{{[0-9]+}}*, ...)* bitcast (void (i{{[0-9]+}}*, i{{[0-9]+}}*)* [[MAIN_MICROTASK:@.+]] to void
-// CHECK: = call i{{.+}} [[TMAIN_INT:@.+]]()
-// CHECK: call void [[S_FLOAT_TY_DESTR:@.+]]([[S_FLOAT_TY]]*
-// CHECK: ret
+// ALL: define i{{[0-9]+}} @main()
+// ALL: [[TEST:%.+]] = alloca [[S_FLOAT_TY]],
+// ALL: call {{.*}} [[S_FLOAT_TY_DEF_CONSTR:@.+]]([[S_FLOAT_TY]]* [[TEST]])
+// ALL: call void (%{{.+}}*, i{{[0-9]+}}, void (i{{[0-9]+}}*, i{{[0-9]+}}*, ...)*, ...) @__kmpc_fork_call(%{{.+}}* @{{.+}}, i{{[0-9]+}} 0, void (i{{[0-9]+}}*, i{{[0-9]+}}*, ...)* bitcast (void (i{{[0-9]+}}*, i{{[0-9]+}}*)* [[MAIN_MICROTASK:@.+]] to void
+// ALL: = call i{{.+}} [[TMAIN_INT:@.+]]()
+// ALL: call void [[S_FLOAT_TY_DESTR:@.+]]([[S_FLOAT_TY]]*
+// ALL: ret
 //
-// CHECK: define internal void [[MAIN_MICROTASK]](i{{[0-9]+}}* noalias [[GTID_ADDR:%.+]], i{{[0-9]+}}* noalias %{{.+}})
-// CHECK: [[T_VAR_PRIV:%.+]] = alloca i{{[0-9]+}},
-// CHECK: [[VEC_PRIV:%.+]] = alloca [2 x i{{[0-9]+}}],
-// CHECK: [[S_ARR_PRIV:%.+]] = alloca [2 x [[S_FLOAT_TY]]],
-// CHECK: [[VAR_PRIV:%.+]] = alloca [[S_FLOAT_TY]],
-// CHECK: [[SIVAR_PRIV:%.+]] = alloca i{{[0-9]+}},
+// ALL: define internal void [[MAIN_MICROTASK]](i{{[0-9]+}}* noalias [[GTID_ADDR:%.+]], i{{[0-9]+}}* noalias %{{.+}})
+// ALL: [[T_VAR_PRIV:%.+]] = alloca i{{[0-9]+}},
+// ALL: [[VEC_PRIV:%.+]] = alloca [2 x i{{[0-9]+}}],
+// ALL: [[S_ARR_PRIV:%.+]] = alloca [2 x [[S_FLOAT_TY]]],
+// ALL: [[VAR_PRIV:%.+]] = alloca [[S_FLOAT_TY]],
+// ALL: [[SIVAR_PRIV:%.+]] = alloca i{{[0-9]+}},
 // CHECK: store i{{[0-9]+}}* [[GTID_ADDR]], i{{[0-9]+}}** [[GTID_ADDR_REF:%.+]]
-// CHECK-NOT: [[T_VAR_PRIV]]
-// CHECK-NOT: [[VEC_PRIV]]
-// CHECK: {{.+}}:
+// IRBUILDER: %{{[0-9]+}} = load i{{[0-9]+}}, i{{[0-9]+}}* 

[PATCH] D81483: [Clang][OpenMP][OMPBuilder] (3/4) Privatize `parallel` for `OMPBuilder`

2020-06-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim created this revision.
fghanim added a reviewer: jdoerfert.
Herald added subscribers: cfe-commits, aaron.ballman, sstefan1, guansong, 
yaxunl.
Herald added a project: clang.
fghanim added a child revision: D79677: [clang][OpenMP][OMPIRBuilder] Adding 
some Privatization clauses to OpenMP `parallel` Directive.
fghanim added a parent revision: D79676: [Clang][OpenMP][OMPBuilder] Moving OMP 
allocation and cache creation code to OMPBuilderCBHelpers.
fghanim marked 3 inline comments as done.
fghanim added inline comments.



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:1554
  llvm::SmallVectorImpl &) {}
 
+bool CodeGenFunction::OMPBuilderCBHelpers::EmitOMPCopyinClause(

Note to reviewers:
This keeps most of old implementation of `copyin` clause, except it uses part 
of the `OMPBuilder` now. Changes are noted below.



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:1582
+  assert(CGF.CapturedStmtInfo->lookup(VD) &&
+ "Copyin threadprivates should have been captured!");
+  const auto *VDCanon = VD->getCanonicalDecl();

added following few lines



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:1600
+}
+
+// Get the address of the threadprivate variable.

Wherever needed - changed the following section to use `OMPBuilder`


Adding support / implementation for `copyin` in OMPBuilder's
implementation of `omp parallel`


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81483

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CodeGenFunction.h
  clang/test/OpenMP/parallel_copyin_codegen.cpp

Index: clang/test/OpenMP/parallel_copyin_codegen.cpp
===
--- clang/test/OpenMP/parallel_copyin_codegen.cpp
+++ clang/test/OpenMP/parallel_copyin_codegen.cpp
@@ -1,9 +1,12 @@
-// RUN: %clang_cc1 -verify -fopenmp -fnoopenmp-use-tls -x c++ -triple x86_64-linux -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -verify -fopenmp -fnoopenmp-use-tls -x c++ -triple x86_64-linux -emit-llvm %s -o - | FileCheck %s -check-prefixes=ALL,CHECK
 // RUN: %clang_cc1 -fopenmp -fnoopenmp-use-tls -x c++ -std=c++11 -triple x86_64-linux -emit-pch -o %t %s
-// RUN: %clang_cc1 -fopenmp -fnoopenmp-use-tls -x c++ -triple x86_64-linux -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 -fopenmp -fnoopenmp-use-tls -x c++ -triple x86_64-linux -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s -check-prefixes=ALL,CHECK
 // RUN: %clang_cc1 -verify -fopenmp -fnoopenmp-use-tls -x c++ -std=c++11 -DLAMBDA -triple x86_64-linux -emit-llvm %s -o - | FileCheck -check-prefix=LAMBDA %s
 // RUN: %clang_cc1 -verify -fopenmp -fnoopenmp-use-tls -x c++ -fblocks -DBLOCKS -triple x86_64-linux -emit-llvm %s -o - | FileCheck -check-prefix=BLOCKS %s
 // RUN: %clang_cc1 -verify -fopenmp -fnoopenmp-use-tls -x c++ -std=c++11 -DARRAY -triple x86_64-apple-darwin10 -emit-llvm %s -o - | FileCheck -check-prefix=ARRAY %s
+// RUN: %clang_cc1 -verify -fopenmp -fopenmp-enable-irbuilder -fnoopenmp-use-tls -x c++ -triple x86_64-linux -emit-llvm %s -o - | FileCheck %s -check-prefixes=ALL,IRBUILDER
+// RUN: %clang_cc1 -fopenmp -fopenmp-enable-irbuilder -fnoopenmp-use-tls -x c++ -std=c++11 -triple x86_64-linux -emit-pch -o %t %s
+// RUN: %clang_cc1 -fopenmp -fopenmp-enable-irbuilder -fnoopenmp-use-tls -x c++ -triple x86_64-linux -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s -check-prefixes=ALL,IRBUILDER
 
 // RUN: %clang_cc1 -verify -fopenmp-simd -fnoopenmp-use-tls -x c++ -triple x86_64-linux -emit-llvm %s -o - | FileCheck --check-prefix SIMD-ONLY0 %s
 // RUN: %clang_cc1 -fopenmp-simd -fnoopenmp-use-tls -x c++ -std=c++11 -triple x86_64-linux -emit-pch -o %t %s
@@ -46,21 +49,22 @@
   ~S() {}
 };
 
-// CHECK-DAG: [[S_FLOAT_TY:%.+]] = type { float }
-// CHECK-DAG: [[S_INT_TY:%.+]] = type { i{{[0-9]+}} }
+// ALL-DAG: [[S_FLOAT_TY:%.+]] = type { float }
+// ALL-DAG: [[S_INT_TY:%.+]] = type { i{{[0-9]+}} }
 // CHECK-DAG: [[IMPLICIT_BARRIER_LOC:@.+]] = private unnamed_addr global %{{.+}} { i32 0, i32 66, i32 0, i32 0, i8*
+// IRBUILDER-DAG: [[IMPLICIT_BARRIER_LOC:@.+]] = private unnamed_addr global %{{.+}} { i32 0, i32 34, i32 0, i32 0, i8*
 // TLS-CHECK-DAG: [[S_FLOAT_TY:%.+]] = type { float }
 // TLS-CHECK-DAG: [[S_INT_TY:%.+]] = type { i{{[0-9]+}} }
 // TLS-CHECK-DAG: [[IMPLICIT_BARRIER_LOC:@.+]] = private unnamed_addr global %{{.+}} { i32 0, i32 66, i32 0, i32 0, i8*
 
-// CHECK-DAG: [[T_VAR:@.+]] = internal global i{{[0-9]+}} 1122,
-// CHECK-DAG: [[VEC:@.+]] = internal global [2 x i{{[0-9]+}}] [i{{[0-9]+}} 1, i{{[0-9]+}} 2],
-// CHECK-DAG: [[S_ARR:@.+]] = internal global [2 x [[S_FLOAT_TY]]] zeroinitializer,
-// CHECK-DAG: [[VAR:@.+]] = internal global [[S_FLOAT_TY]] zeroinitializer,
-// CHECK-DAG: [[TMAIN_T_VAR:@.+]] = linkonce_odr 

[PATCH] D79676: [Clang][OpenMP][OMPBuilder] Moving OMP allocation and cache creation code to OMPBuilderCBHelpers

2020-06-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 269584.
fghanim added a comment.
Herald added a subscriber: sstefan1.

- rebase
- addressing reviewer's comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79676/new/

https://reviews.llvm.org/D79676

Files:
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CGExpr.cpp
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CodeGenFunction.h

Index: clang/lib/CodeGen/CodeGenFunction.h
===
--- clang/lib/CodeGen/CodeGenFunction.h
+++ clang/lib/CodeGen/CodeGenFunction.h
@@ -26,6 +26,7 @@
 #include "clang/AST/ExprCXX.h"
 #include "clang/AST/ExprObjC.h"
 #include "clang/AST/ExprOpenMP.h"
+#include "clang/AST/StmtOpenMP.h"
 #include "clang/AST/Type.h"
 #include "clang/Basic/ABI.h"
 #include "clang/Basic/CapturedStmt.h"
@@ -78,6 +79,7 @@
 class ObjCAutoreleasePoolStmt;
 class ReturnsNonNullAttr;
 class SVETypeFlags;
+class OMPExecutableDirective;
 
 namespace analyze_os_log {
 class OSLogBufferLayout;
@@ -257,114 +259,6 @@
 unsigned Index;
   };
 
-  // Helper class for the OpenMP IR Builder. Allows reusability of code used for
-  // region body, and finalization codegen callbacks. This will class will also
-  // contain privatization functions used by the privatization call backs
-  struct OMPBuilderCBHelpers {
-
-using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
-
-/// Emit the Finalization for an OMP region
-/// \param CGF	The Codegen function this belongs to
-/// \param IP	Insertion point for generating the finalization code.
-static void FinalizeOMPRegion(CodeGenFunction , InsertPointTy IP) {
-  CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
-  assert(IP.getBlock()->end() != IP.getPoint() &&
- "OpenMP IR Builder should cause terminated block!");
-
-  llvm::BasicBlock *IPBB = IP.getBlock();
-  llvm::BasicBlock *DestBB = IPBB->getUniqueSuccessor();
-  assert(DestBB && "Finalization block should have one successor!");
-
-  // erase and replace with cleanup branch.
-  IPBB->getTerminator()->eraseFromParent();
-  CGF.Builder.SetInsertPoint(IPBB);
-  CodeGenFunction::JumpDest Dest = CGF.getJumpDestInCurrentScope(DestBB);
-  CGF.EmitBranchThroughCleanup(Dest);
-}
-
-/// Emit the body of an OMP region
-/// \param CGF	The Codegen function this belongs to
-/// \param RegionBodyStmt	The body statement for the OpenMP region being
-/// 			 generated
-/// \param CodeGenIP	Insertion point for generating the body code.
-/// \param FiniBB	The finalization basic block
-static void EmitOMPRegionBody(CodeGenFunction ,
-  const Stmt *RegionBodyStmt,
-  InsertPointTy CodeGenIP,
-  llvm::BasicBlock ) {
-  llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
-  if (llvm::Instruction *CodeGenIPBBTI = CodeGenIPBB->getTerminator())
-CodeGenIPBBTI->eraseFromParent();
-
-  CGF.Builder.SetInsertPoint(CodeGenIPBB);
-
-  CGF.EmitStmt(RegionBodyStmt);
-
-  if (CGF.Builder.saveIP().isSet())
-CGF.Builder.CreateBr();
-}
-
-/// RAII for preserving necessary info during Outlined region body codegen.
-class OutlinedRegionBodyRAII {
-
-  llvm::AssertingVH OldAllocaIP;
-  CodeGenFunction::JumpDest OldReturnBlock;
-  CodeGenFunction 
-
-public:
-  OutlinedRegionBodyRAII(CodeGenFunction , InsertPointTy ,
- llvm::BasicBlock )
-  : CGF(cgf) {
-assert(AllocaIP.isSet() &&
-   "Must specify Insertion point for allocas of outlined function");
-OldAllocaIP = CGF.AllocaInsertPt;
-CGF.AllocaInsertPt = &*AllocaIP.getPoint();
-
-OldReturnBlock = CGF.ReturnBlock;
-CGF.ReturnBlock = CGF.getJumpDestInCurrentScope();
-  }
-
-  ~OutlinedRegionBodyRAII() {
-CGF.AllocaInsertPt = OldAllocaIP;
-CGF.ReturnBlock = OldReturnBlock;
-  }
-};
-
-/// RAII for preserving necessary info during inlined region body codegen.
-class InlinedRegionBodyRAII {
-
-  llvm::AssertingVH OldAllocaIP;
-  CodeGenFunction 
-
-public:
-  InlinedRegionBodyRAII(CodeGenFunction , InsertPointTy ,
-llvm::BasicBlock )
-  : CGF(cgf) {
-// Alloca insertion block should be in the entry block of the containing
-// function so it expects an empty AllocaIP in which case will reuse the
-// old alloca insertion point, or a new AllocaIP in the same block as
-// the old one
-assert((!AllocaIP.isSet() ||
-CGF.AllocaInsertPt->getParent() == AllocaIP.getBlock()) &&
-   "Insertion point should be in the entry block of containing "
-   "function!");
-OldAllocaIP = CGF.AllocaInsertPt;
-if (AllocaIP.isSet())
-   

[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-06-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 269583.
fghanim added a comment.
Herald added a subscriber: aaron.ballman.

- Rebase + refactor based on D80222 
- addressed reviewer comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79675/new/

https://reviews.llvm.org/D79675

Files:
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -779,4 +779,41 @@
   EXPECT_EQ(CriticalEndCI->getArgOperand(2)->getType(), CriticalNamePtrTy);
 }
 
+TEST_F(OpenMPIRBuilderTest, CopyinBlocks) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+  IntegerType* Int32 = Type::getInt32Ty(M->getContext());
+  AllocaInst* MasterAddress = Builder.CreateAlloca(Int32->getPointerTo());
+	AllocaInst* PrivAddress = Builder.CreateAlloca(Int32->getPointerTo());
+
+  BasicBlock *EntryBB = BB;
+
+  OMPBuilder.CreateCopyinClauseBlocks(Builder.saveIP(), MasterAddress, PrivAddress, Int32, /*BranchtoEnd*/true);
+
+  BranchInst* EntryBr = dyn_cast_or_null(EntryBB->getTerminator());
+
+  EXPECT_NE(EntryBr, nullptr);
+  EXPECT_TRUE(EntryBr->isConditional());
+
+  BasicBlock* NotMasterBB = EntryBr->getSuccessor(0);
+  BasicBlock* CopyinEnd = EntryBr->getSuccessor(1);
+  CmpInst* CMP = dyn_cast_or_null(EntryBr->getCondition());
+
+  EXPECT_NE(CMP, nullptr);
+  EXPECT_NE(NotMasterBB, nullptr);
+  EXPECT_NE(CopyinEnd, nullptr);
+
+  BranchInst* NotMasterBr = dyn_cast_or_null(NotMasterBB->getTerminator());
+  EXPECT_NE(NotMasterBr, nullptr);
+  EXPECT_FALSE(NotMasterBr->isConditional());
+  EXPECT_EQ(CopyinEnd,NotMasterBr->getSuccessor(0));
+}
+
 } // namespace
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -611,9 +611,6 @@
  "Unexpected finalization stack state!");
 
   Instruction *PRegPreFiniTI = PRegPreFiniBB->getTerminator();
-  assert(PRegPreFiniTI->getNumSuccessors() == 1 &&
- PRegPreFiniTI->getSuccessor(0) == PRegExitBB &&
- "Unexpected CFG structure!");
 
   InsertPointTy PreFiniIP(PRegPreFiniBB, PRegPreFiniTI->getIterator());
   FiniCB(PreFiniIP);
@@ -948,6 +945,105 @@
   ExitCall->getIterator());
 }
 
+OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::CreateCopyinClauseBlocks(
+InsertPointTy IP, Value *MasterAddr, Value *PrivateAddr,
+llvm::IntegerType *IntPtrTy, bool BranchtoEnd) {
+  if (!IP.isSet())
+return IP;
+
+  IRBuilder<>::InsertPointGuard IPG(Builder);
+
+  // creates the following CFG structure
+  //	   OMP_Entry : (MasterAddr != PrivateAddr)?
+  //   F T
+  //   |  \
+  //   | copin.not.master
+  //   |  /
+  //   v /
+  //   copyin.not.master.end
+  //		 |
+  // v
+  //   OMP.Entry.Next
+
+  BasicBlock *OMP_Entry = IP.getBlock();
+  Function *CurFn = OMP_Entry->getParent();
+  BasicBlock *CopyBegin =
+  BasicBlock::Create(M.getContext(), "copyin.not.master", CurFn);
+  BasicBlock *CopyEnd = nullptr;
+
+  // If entry block is terminated, split to preserve the branch to following
+  // basic block (i.e. OMP.Entry.Next), otherwise, leave everything as is.
+  if (isa_and_nonnull(OMP_Entry->getTerminator())) {
+CopyEnd = OMP_Entry->splitBasicBlock(OMP_Entry->getTerminator(),
+ "copyin.not.master.end");
+OMP_Entry->getTerminator()->eraseFromParent();
+  } else {
+CopyEnd =
+BasicBlock::Create(M.getContext(), "copyin.not.master.end", CurFn);
+  }
+
+  Builder.SetInsertPoint(OMP_Entry);
+  Value *MasterPtr = Builder.CreatePtrToInt(MasterAddr, IntPtrTy);
+  Value *PrivatePtr = Builder.CreatePtrToInt(PrivateAddr, IntPtrTy);
+  Value *cmp = Builder.CreateICmpNE(MasterPtr, PrivatePtr);
+  Builder.CreateCondBr(cmp, CopyBegin, CopyEnd);
+
+  Builder.SetInsertPoint(CopyBegin);
+  if (BranchtoEnd)
+Builder.SetInsertPoint(Builder.CreateBr(CopyEnd));
+
+  return Builder.saveIP();
+}
+
+CallInst *OpenMPIRBuilder::CreateOMPAlloc(const LocationDescription ,
+  Value *Size, Value *Allocator,
+  std::string Name) {
+  IRBuilder<>::InsertPointGuard IPG(Builder);
+  Builder.restoreIP(Loc.IP);
+
+  Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
+  Value *Ident = 

[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-05-27 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked an inline comment as done.
fghanim added a comment.

I am moving on because we are not getting anywhere. However, There are few 
things I need to point out very quickly.

> I fail to see the point in committing for example your target type solution 
> if we found a more generic version in the meantime.
>  We can for sure commit them and then replace them subsequently, but is that 
> really helping anyone? It would not be a question if
>  they were in, since they are not it seems to me there is no benefit in 
> blocking the other patch on them. I mean, the time you worked
>  on that part is not "less wasted" if we commit it. TBH, I don't thin it is 
> wasted at all but that is a different conversation.

At one point, you said I was delaying D80222  
moments after it was uploaded. Now, D79675  
and D79676  , cannot be committed because of 
the artificial dependency on that patch.

> I'm sorry you **feel** I waste your time. I really would not do so on purpose.

It is not a feeling. It is a matter of record, and never said you did so on 
purpose. Freudian slip? :p

> While more reviewers would obviously help, it is known that smaller patches 
> do too.

D79739  has been merged with D80222 
. I kinda feel bad for the reviewer ;)
You are the code owner of the `OMPBuilder`, who do you suggest as reviewers 
that I can add, in the future?

> If you have ideas on other improvements of the process, I'm happy to try them 
> out.

Let people know that you changed your mind before they put in the time and 
effort. I am sure that is not a big ask.

---

Anyways, I suggested something that you didn't reply to, which you may have 
missed. To resolve this, would you be willing to go for:

1. You handle any typing problems with this patch when you commit it and D79676 
 after D80222 
2. I bring back all my runtime def.s that I need, and use macros per your 
original suggestion, and you commit this and D79676 
 today or tomorrow and that patch can merge 
based on head commit which it will do anyway?




Comment at: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h:62
+  /// return a copy of the current insertion point information
+  InsertPointTy SaveIP() { return Builder.saveIP(); }
+

jdoerfert wrote:
> fghanim wrote:
> > jdoerfert wrote:
> > > Unused?
> > I'll happily drop them if you want. I needed them at one point, and assumed 
> > we may need them later, and left them in to see what you think. So still 
> > LGTM , or no LGTM?
> Generally we should not include code we don't need (or that is not tested).
We don't need it at the moment, however, I do not think an IRBuilder should go 
without a way to specify where you want it to point at.
This doesn't need a test. it just passes an argument to a private `IRBuilder` - 
if that works, this should just work.
Bottom line, should I remove it?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79675/new/

https://reviews.llvm.org/D79675



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-05-22 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked 2 inline comments as done.
fghanim added a comment.

I am going to omit parts of the quote, because who wants to look at a wall of 
test - readability is important ;)

In D79675#2051079 , @jdoerfert wrote:

> In D79675#2047154 , @fghanim wrote:
>
> > Until the OMPBuilder becomes the way to CG OMP IR, we will always be 
> > playing catch-up. All the OMPKinds def.s are very copy/paste-able one or 
> > two-liners and very easy to move. However, the actual code to CG the IR is 
> > not.
>
>
> That is not true, with the other patch we require all new code gen 
> functionality to list the runtime functions in OMPKinds.def.


That is not what I said, and FWIW I don't disagree.

> This is not about number of lines. Don't take my word for it, ask the 
> community if you want. We want the smallest logical and testable patches 
> possible, unrelated to the size. (FWIW, 300 lines is not nothing either.)

Number of lines has some correlation with readability, and readability is a 
factor.

> The time spend arguing is more than splitting would have taken in the first 
> place.

In my defense, I have long build times as a result of frequent updates. ;)
As for this one, I am doing it out of work hours :p

> Conceptual parts:
> 
> - Target dependent types (which we can initialize w/o the frontend based on 
> the datalayout)
> - Insert point changes (which seem to be unsued in this patch)
> - The create functions

This is the current state of the patch, not what it was before it was gutted by 
various updates. Back then it was a "the smallest logical and testable patches 
possible, unrelated to the size". it was `createXXX` methods along with all 
related types and run time calls. FWIW, it was modeled after D70109 
 , which -I can only assume- fit yours and the 
community's criteria? ;)

> With D80222  we don't need the first. If you 
> think the way it's done in there is for some reason less good, please say so, 
> otherwise I fail to see why we would not go ahead with that one and rebase 
> this on top.

I'll suggest something below. I want to be done with this patch.

>> However, What wasted everyone's time my friend, is removing integral parts 
>> to this patch which has two other feature patches depend on it, which meant 
>> I needed to build and rebuild to make sure things still work. it would have 
>> been way easier to make D79739  modify and 
>> build on the typing in this one as I suggested there, and in retrospect, is 
>> something I should've pushed harder for. Anyways, let's move on. :D
> 
> I don't think this is true, even if, this is not how this works. The only way 
> to make fast progress is small patches. I give you almost instant feedback on 
> you patches but the more is included in one, the more revision we have to go 
> through. Unrelated problems stall parts we depend on.
> 
> Maybe your setup needs tweaking or you should bundle changes in smaller patch 
> from the beginning to avoid this, either way, the guidelines are clear:
>  https://llvm.org/docs/CodeReview.html

The only reason I brought my setup and long build times up is to indicate to 
you to please be mindful of my time with your comments. per the guidelines: 
"Aim to Make Efficient Use of **Everyone’s** Time" - emphasis is mine

Let me recap what happened for your benefit and to see if the guidelines were 
followed: (all of this can be verified - it is a matter of record, not an 
opinion)

- I upload the patch on 9th.
- On the 11th, You ask me to make the `void*` and macro changes.
- Also on the 11th, patch D79739  was created 
and included many of the things I had, and was using `ptr8*` in place of 
`void*` - i.e. **had no definition for `void*`**. You also ask the author of 
that patch to make the same macro changes. You don't tell me someone else is 
working on it or anything.
- On the 12th, I respond to some items and start working on implementing the 
things I didn't comment on as part of new patch update. - I am still unaware 
others are working on things that were asked of me
- On the 13th, you told me about patch D79739  
, and that they implemented the `void*` macros you asked me to do - at which 
point I had them implemented and verified, but not uploaded
- Also, on the 13th, you tell the author of that patch, that the 
**target-typing issues will be handled by me**. I ask you if I should drop the 
duplicate def.s between this and the other patch. and when you didn't respond 
to that, I drop them because it makes more sense to have these things as part 
of D79739 
- On the 14th, I uploaded a patch with these changes, and I keep 
target-specific types and Def.s that weren't in that patch - we exchange 
comments, and you ask me to change 

[PATCH] D80222: Replace Clang's createRuntimeFunction with the definitions in OMPKinds.def

2020-05-22 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added inline comments.



Comment at: llvm/include/llvm/Frontend/OpenMP/OMPKinds.def:244
 
-// TODO: Replace this with the real size_t type
-#define __OMP_SIZE_TYPE(NAME) OMP_TYPE(NAME, Type::getInt64Ty(Ctx))
+#define __OMP_SIZE_TYPE(NAME) OMP_TYPE(NAME, 
M.getDataLayout().getIntPtrType(Ctx))
 __OMP_SIZE_TYPE(SizeTy)

jhuber6 wrote:
> fghanim wrote:
> > jdoerfert wrote:
> > > jhuber6 wrote:
> > > > I'm just directly getting the SizeTy from the Module, I'm not sure if 
> > > > this is a permanent solution.
> > > I actually think this is fine. Less burden on the frontends also. 
> > > @fghanim looks good?
> > Agreed. But to avoid conflict with patch D79675, let this change go in a 
> > separate patch after that. This way, we can also do some clean ups related 
> > to this, and add `Int8PtrPtr`, `Int8PtrPtrPtr`, and any other typing 
> > issues, without delaying patch D79675 any further - I am done with that 
> > patch, and made my final changes a couple of days ago.
> > 
> > Nit: Also, I saw below that some formatting was done. Wouldn't it be better 
> > if formatting/comments related to changes in D79739, are handled as part of 
> > that patch?
> Doesn't this patch already support Int8PtrPtrPtr? It's the same thing as a 
> VoidPtrPtrPtr which I have defined above here.
You are correct. But think of it in the same way we have voidptr and int8ptr, 
even though they are the same.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80222/new/

https://reviews.llvm.org/D80222



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D80222: Replace Clang's createRuntimeFunction with the definitions in OMPKinds.def

2020-05-21 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added inline comments.



Comment at: llvm/include/llvm/Frontend/OpenMP/OMPKinds.def:244
 
-// TODO: Replace this with the real size_t type
-#define __OMP_SIZE_TYPE(NAME) OMP_TYPE(NAME, Type::getInt64Ty(Ctx))
+#define __OMP_SIZE_TYPE(NAME) OMP_TYPE(NAME, 
M.getDataLayout().getIntPtrType(Ctx))
 __OMP_SIZE_TYPE(SizeTy)

jdoerfert wrote:
> jhuber6 wrote:
> > I'm just directly getting the SizeTy from the Module, I'm not sure if this 
> > is a permanent solution.
> I actually think this is fine. Less burden on the frontends also. @fghanim 
> looks good?
Agreed. But to avoid conflict with patch D79675, let this change go in a 
separate patch after that. This way, we can also do some clean ups related to 
this, and add `Int8PtrPtr`, `Int8PtrPtrPtr`, and any other typing issues, 
without delaying patch D79675 any further - I am done with that patch, and made 
my final changes a couple of days ago.

Nit: Also, I saw below that some formatting was done. Wouldn't it be better if 
formatting/comments related to changes in D79739, are handled as part of that 
patch?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80222/new/

https://reviews.llvm.org/D80222



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-05-20 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 265293.
fghanim added a comment.

Addressing more reviewers comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79675/new/

https://reviews.llvm.org/D79675

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPConstants.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/lib/Frontend/OpenMP/OMPConstants.cpp
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/lib/Transforms/IPO/OpenMPOpt.cpp
  llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -62,7 +62,7 @@
 
 TEST_F(OpenMPIRBuilderTest, CreateBarrier) {
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(M->getDataLayout().getIntPtrType(M->getContext()));
 
   IRBuilder<> Builder(BB);
 
@@ -102,7 +102,7 @@
 TEST_F(OpenMPIRBuilderTest, CreateCancel) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(M->getDataLayout().getIntPtrType(M->getContext()));
 
   BasicBlock *CBB = BasicBlock::Create(Ctx, "", F);
   new UnreachableInst(Ctx, CBB);
@@ -157,7 +157,7 @@
 TEST_F(OpenMPIRBuilderTest, CreateCancelIfCond) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(M->getDataLayout().getIntPtrType(M->getContext()));
 
   BasicBlock *CBB = BasicBlock::Create(Ctx, "", F);
   new UnreachableInst(Ctx, CBB);
@@ -218,7 +218,7 @@
 TEST_F(OpenMPIRBuilderTest, CreateCancelBarrier) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(M->getDataLayout().getIntPtrType(M->getContext()));
 
   BasicBlock *CBB = BasicBlock::Create(Ctx, "", F);
   new UnreachableInst(Ctx, CBB);
@@ -272,7 +272,7 @@
 
 TEST_F(OpenMPIRBuilderTest, DbgLoc) {
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(M->getDataLayout().getIntPtrType(M->getContext()));
   F->setName("func");
 
   IRBuilder<> Builder(BB);
@@ -308,7 +308,7 @@
 TEST_F(OpenMPIRBuilderTest, ParallelSimple) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(M->getDataLayout().getIntPtrType(M->getContext()));
   F->setName("func");
   IRBuilder<> Builder(BB);
 
@@ -405,7 +405,7 @@
 TEST_F(OpenMPIRBuilderTest, ParallelIfCond) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(M->getDataLayout().getIntPtrType(M->getContext()));
   F->setName("func");
   IRBuilder<> Builder(BB);
 
@@ -516,7 +516,7 @@
 TEST_F(OpenMPIRBuilderTest, ParallelCancelBarrier) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(M->getDataLayout().getIntPtrType(M->getContext()));
   F->setName("func");
   IRBuilder<> Builder(BB);
 
@@ -625,7 +625,7 @@
 TEST_F(OpenMPIRBuilderTest, MasterDirective) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(M->getDataLayout().getIntPtrType(M->getContext()));
   F->setName("func");
   IRBuilder<> Builder(BB);
 
@@ -704,7 +704,7 @@
 TEST_F(OpenMPIRBuilderTest, CriticalDirective) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(M->getDataLayout().getIntPtrType(M->getContext()));
   F->setName("func");
   IRBuilder<> Builder(BB);
 
@@ -779,4 +779,41 @@
   EXPECT_EQ(CriticalEndCI->getArgOperand(2)->getType(), CriticalNamePtrTy);
 }
 
+TEST_F(OpenMPIRBuilderTest, CopyinBlocks) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize(M->getDataLayout().getIntPtrType(M->getContext()));
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+  IntegerType* Int32 = Type::getInt32Ty(M->getContext());
+  AllocaInst* MasterAddress = Builder.CreateAlloca(Int32->getPointerTo());
+	AllocaInst* PrivAddress = Builder.CreateAlloca(Int32->getPointerTo());
+
+  BasicBlock *EntryBB = BB;
+
+  OMPBuilder.CreateCopyinClauseBlocks(Builder.saveIP(), MasterAddress, PrivAddress, Int32, /*BranchtoEnd*/true);
+
+  BranchInst* EntryBr = dyn_cast_or_null(EntryBB->getTerminator());
+
+  EXPECT_NE(EntryBr, nullptr);
+  EXPECT_TRUE(EntryBr->isConditional());
+
+  BasicBlock* 

[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-05-20 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked an inline comment as done.
fghanim added inline comments.



Comment at: llvm/lib/Frontend/OpenMP/OMPConstants.cpp:86
+  llvm::omp::types::Int8PtrPtr = Int8Ptr->getPointerTo();
+  llvm::omp::types::Int8PtrPtrPtr = Int8PtrPtr->getPointerTo();
+

fghanim wrote:
> jdoerfert wrote:
> > I think the macro way to specify pointer is easier to use.
> It is. But that patch has landed yet, and so I cannot use that. so for the 
> time being, I am going to keep this way. and after both patches land, I'll 
> make a minor patch that will just make this small modification.
I meant to say the patch has *not* landed yet. sorry for the confusion.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79675/new/

https://reviews.llvm.org/D79675



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-05-20 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked 10 inline comments as done.
fghanim added a comment.

In D79675#2045826 , @jdoerfert wrote:

> In D79675#2045563 , @fghanim wrote:
>
> > So this whole thing was about moving Def.s out of `CGOMPRuntime`? Given how 
> > low priority making the-soon-to-be-deprecated `CGOMPRuntime` use the new 
> > Def.s is, and that I actually use these stuff as part of 
> > the-soon-to-be-the-way-to-CG-OMP `OMPBuilder`, wouldn't it have been better 
> > to postpone both patches until we are done with this one then add anything 
> > I didn't have already as part of D79739  ? 
> > It would have certainly saved everyone a lot of time, and made more sense 
> > given that the earlier patch came out 2 days after mine, and the other 
> > patch today? :)
>
>
>
>
> 1. Soon is relative.


"Soon" is indeed relative, and so is "later", and so is 99% of the words. 
However, words have specific meanings, otherwise opposites would refer to the 
same thing, and words become useless and meaningless. "Soon" means soon.

> 2. In the meantime people work on clang and add runtime functions into the 
> CGOMPRuntime but not into OMPKinds.def. We are playing catch up all the time, 
> thus wasting time.

Until the `OMPBuilder` becomes the way to CG OMP IR, we will always be playing 
catch-up. All the `OMPKinds` def.s are very copy/paste-able one or two-liners 
and very easy to move. However, the actual code to CG the IR is not. 
Therefore, I hereby declare, that henceforth, for as long as I am working on 
the `OMPBuilder`, if people are willing to write the CG code of new things as 
part of the `OMPBuilder`, I am happy to be the one to move the def.s for them 
at anytime, including on my deathbed. :D

> 3. I said it before, please split them up in small pieces. It does really not 
> help if we combine unrelated things in a single patch. It doesn't make it 
> faster and it is not less work at the end of the day.

Johannes Comon .. This patch IS Small (~300 lines) and everything here is 
related (with one exception below). This patch adds 4 new  `create` calls 
to the `OMPBuilder` needed by the privatization stuff in patches D79676 
 and D79677  
. These calls require certain runtime calls and typing def.s. It is how we have 
always done it for any new `create` method starting with 
`CreateOMPParallel()`. The Def.s I added are almost completely gone. The 
exception I mentioned earlier is the pass-throughs to the `OMPBuilder`'s 
`IRBuilder` which were LGTM-ed early on and nothing uses them yet, so it is 
really a non-issue.

However, What wasted everyone's time my friend, is removing integral parts to 
this patch which has two other feature patches depend on it, which meant I 
needed to build and rebuild to make sure things still work. it would have been 
way easier to make D79739  modify and build on 
the typing in this one as I suggested there, and in retrospect, is something I 
should've pushed harder for. Anyways, let's move on. :D

Let me know, if there are any further comments.




Comment at: llvm/lib/Frontend/OpenMP/OMPConstants.cpp:86
+  llvm::omp::types::Int8PtrPtr = Int8Ptr->getPointerTo();
+  llvm::omp::types::Int8PtrPtrPtr = Int8PtrPtr->getPointerTo();
+

jdoerfert wrote:
> I think the macro way to specify pointer is easier to use.
It is. But that patch has landed yet, and so I cannot use that. so for the time 
being, I am going to keep this way. and after both patches land, I'll make a 
minor patch that will just make this small modification.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79675/new/

https://reviews.llvm.org/D79675



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-05-19 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

In D79675#2045405 , @jdoerfert wrote:

> > Could you please list the other patches that are being held back by this 
> > one? I'd be interested to have a look at them. :)
>
> We need the target type support for D80222 , 
> D79739  can go in but we need to modify it 
> afterwards.


So this whole thing was about moving Def.s out of `CGOMPRuntime`? Given how low 
priority making the-soon-to-be-deprecated `CGOMPRuntime` use the new Def.s is, 
and that I actually use these stuff as part of the-soon-to-be-the-way-to-CG-OMP 
`OMPBuilder`, wouldn't it have been better to postpone both patches until we 
are done with this one then add anything I didn't have already as part of 
D79739  ? It would have certainly saved 
everyone a lot of time, and made more sense given that the earlier patch came 
out 2 days after mine, and the other patch today? :)

In any case, I addressed everything based on your earlier comments. Thanks for 
reviewing my patches. Let me know if you think other changes are needed here, 
otherwise could you please commit this for me, I still don't have commit access.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79675/new/

https://reviews.llvm.org/D79675



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-05-19 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 265071.
fghanim marked 2 inline comments as done.
fghanim added a comment.

Addressing reviewer Comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79675/new/

https://reviews.llvm.org/D79675

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPConstants.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/lib/Frontend/OpenMP/OMPConstants.cpp
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/lib/Transforms/IPO/OpenMPOpt.cpp
  llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -62,7 +62,7 @@
 
 TEST_F(OpenMPIRBuilderTest, CreateBarrier) {
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(Type::getInt32Ty(M->getContext()));
 
   IRBuilder<> Builder(BB);
 
@@ -102,7 +102,7 @@
 TEST_F(OpenMPIRBuilderTest, CreateCancel) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(Type::getInt32Ty(M->getContext()));
 
   BasicBlock *CBB = BasicBlock::Create(Ctx, "", F);
   new UnreachableInst(Ctx, CBB);
@@ -157,7 +157,7 @@
 TEST_F(OpenMPIRBuilderTest, CreateCancelIfCond) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(Type::getInt32Ty(M->getContext()));
 
   BasicBlock *CBB = BasicBlock::Create(Ctx, "", F);
   new UnreachableInst(Ctx, CBB);
@@ -218,7 +218,7 @@
 TEST_F(OpenMPIRBuilderTest, CreateCancelBarrier) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(Type::getInt32Ty(M->getContext()));
 
   BasicBlock *CBB = BasicBlock::Create(Ctx, "", F);
   new UnreachableInst(Ctx, CBB);
@@ -272,7 +272,7 @@
 
 TEST_F(OpenMPIRBuilderTest, DbgLoc) {
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(Type::getInt32Ty(M->getContext()));
   F->setName("func");
 
   IRBuilder<> Builder(BB);
@@ -308,7 +308,7 @@
 TEST_F(OpenMPIRBuilderTest, ParallelSimple) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(Type::getInt32Ty(M->getContext()));
   F->setName("func");
   IRBuilder<> Builder(BB);
 
@@ -405,7 +405,7 @@
 TEST_F(OpenMPIRBuilderTest, ParallelIfCond) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(Type::getInt32Ty(M->getContext()));
   F->setName("func");
   IRBuilder<> Builder(BB);
 
@@ -516,7 +516,7 @@
 TEST_F(OpenMPIRBuilderTest, ParallelCancelBarrier) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(Type::getInt32Ty(M->getContext()));
   F->setName("func");
   IRBuilder<> Builder(BB);
 
@@ -625,7 +625,7 @@
 TEST_F(OpenMPIRBuilderTest, MasterDirective) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(Type::getInt32Ty(M->getContext()));
   F->setName("func");
   IRBuilder<> Builder(BB);
 
@@ -704,7 +704,7 @@
 TEST_F(OpenMPIRBuilderTest, CriticalDirective) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
-  OMPBuilder.initialize();
+  OMPBuilder.initialize(Type::getInt32Ty(M->getContext()));
   F->setName("func");
   IRBuilder<> Builder(BB);
 
@@ -779,4 +779,41 @@
   EXPECT_EQ(CriticalEndCI->getArgOperand(2)->getType(), CriticalNamePtrTy);
 }
 
+TEST_F(OpenMPIRBuilderTest, CopyinBlocks) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize(Type::getInt32Ty(M->getContext()));
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+  IntegerType* Int32 = Type::getInt32Ty(M->getContext());
+  AllocaInst* MasterAddress = Builder.CreateAlloca(Int32->getPointerTo());
+	AllocaInst* PrivAddress = Builder.CreateAlloca(Int32->getPointerTo());
+
+  BasicBlock *EntryBB = BB;
+
+  OMPBuilder.CreateCopyinClauseBlocks(Builder.saveIP(), MasterAddress, PrivAddress, Int32, /*BranchtoEnd*/true);
+
+  BranchInst* EntryBr = dyn_cast_or_null(EntryBB->getTerminator());
+
+  EXPECT_NE(EntryBr, nullptr);
+  EXPECT_TRUE(EntryBr->isConditional());
+
+  BasicBlock* NotMasterBB = EntryBr->getSuccessor(0);
+  BasicBlock* CopyinEnd = EntryBr->getSuccessor(1);
+  CmpInst* CMP = 

[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-05-19 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked 21 inline comments as done.
fghanim added a comment.

In D79675#2044809 , @jdoerfert wrote:

> What's the status? Can we split the target specific types stuff if this may 
> take a while, other patches depend on that :)


Could you please list the other patches that are being held back by this one? 
I'd be interested to have a look at them. :)




Comment at: llvm/include/llvm/Frontend/OpenMP/OMPKinds.def:234
 __OMP_RTL(__kmpc_critical, false, Void, IdentPtr, Int32, KmpCriticalNamePtrTy)
-__OMP_RTL(__kmpc_critical_with_hint, false, Void, IdentPtr, Int32, 
KmpCriticalNamePtrTy, Int32)
+__OMP_RTL(__kmpc_critical_with_hint, false, Void, IdentPtr, Int32, 
KmpCriticalNamePtrTy, IntPtrTy)
 __OMP_RTL(__kmpc_end_critical, false, Void, IdentPtr, Int32, 
KmpCriticalNamePtrTy)

jdoerfert wrote:
> fghanim wrote:
> > jdoerfert wrote:
> > > fghanim wrote:
> > > > jdoerfert wrote:
> > > > > fghanim wrote:
> > > > > > jdoerfert wrote:
> > > > > > > I think this was correct before:
> > > > > > > ```
> > > > > > >   KMP_EXPORT void __kmpc_critical_with_hint(ident_t *, kmp_int32 
> > > > > > > global_tid, kmp_critical_name *, uint32_t hint);
> > > > > > > ```
> > > > > > Nop, it's supposed to be whatever `IntPtrTy` is in the frontend 
> > > > > > (i.e. 32 for 32bit, 64 for 64bit).
> > > > > > 
> > > > > > `IntPtrTy` is actually a union with `sizety` in `CodeGenModule`
> > > > > I'm confused. I copied the declaration above from the runtime. It 
> > > > > doesn't contain an `IntPtrTy` or similar. I agree that `IntPtrTy` is 
> > > > > machine dependent and we need your initializers. What I tried to say 
> > > > > is that at least one declaration in the runtime has 
> > > > > `__kmpc_critical_with_hint` with an int32 as last argument. That 
> > > > > said, the runtime is not shy of incompatible declarations for 
> > > > > functions.
> > > > I cannot speak for what's in the runtime. However, in clang, this 
> > > > currently is defined to use `IntPtrTy`. If you go check, I have a todo 
> > > > in the current implementation for critical to come back and fix it.
> > > That is just an existing bug in Clang. The runtime is consistent with the 
> > > type and arguably it is the runtime we must match, not the other way 
> > > around ;)
> > Apparently, this used to be `uintptr_t` in the runtime and was changed by 
> > D51235 . It doesn't say why the change was made.
> > 
> > Clang uses this in multiple places, which makes me think it may have been 
> > intentional. So I suggest we go by the standard. Where can I find what does 
> > the OMP standard say about how the signature of the runtime calls should 
> > look like? This way I'll fix this one accordingly, and later we may go and 
> > make sure that all the rest match up with the standard - where possible.
> This is not a standard function. It is a runtime function. I suspect it is a 
> int32_t because enums default to `int` which is 32 bit on all interesting 
> platforms. Should probably be a `int` but I would need to read up to confirm. 
> Long story short, and as I said before, it is a bug in clang. The code here 
> was correct.
> 
> FWIW, we will soon replace clang's runtime function generation with these 
> macros. That will happen in the next days I hope.
Oh, my bad. I thought OMP runtime functions are designed a specific way based 
on the OMP standard. If that's not the case, then it doesn't matter. I'll drop 
this change as well. There is a handful of hints anyway.



Comment at: llvm/lib/Frontend/OpenMP/OMPConstants.cpp:122
+  }
+}
+

jdoerfert wrote:
> fghanim wrote:
> > jdoerfert wrote:
> > > fghanim wrote:
> > > > jdoerfert wrote:
> > > > > (I doubt we need the `initVoidPtr` function but if we do, the 
> > > > > condition looks wrong)
> > > > Forgot to refractor when I changed names.
> > > > 
> > > > Regarding `void*` , I am not sure how it is defined in fortran. That's 
> > > > why I made it possible to initialize it separately.
> > > I see. Let's cross that bridge once we get to it.
> > Modified it a bit and removed usage from initialization. If fortran people 
> > don't end using it, then we can remove it.
> Let's not add stuff that might be used.
I just looked this up. Apparently, Fortran doesn't seem to have the concept of 
void pointer natively. However, They have something called `Assumed types` , 
which was added for interoperability with C, which *probably* means it will 
follow whatever C does to handle void pointers.

I'll remove it now, and if they choose, people can add it later.



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:106
+  initializeVoidPtrTy(VoidPtrTy);
+}
+

jdoerfert wrote:
> fghanim wrote:
> > jdoerfert wrote:
> > > I guess we can directly call the initialize functions, right?
> > > With an assert that SizeTy is set we can make sure users do it ;)
> > Sure, then again, I am just 

[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-05-14 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked 3 inline comments as done.
fghanim added a comment.

In D79675#2037314 , @jdoerfert wrote:

> I left some comments on the type stuff. The rest looks good.
>  I think if you rebase the type stuff on D79739 
>  (which I can merge) we should only need to 
> expand `initializeTypes` to make this work as expected. WDYT?


Currently, I implemented the changes relevant to me from that and made a 
separate local commit prior to this one to make sure things work.
I build llvm locally, and so it take 6 - 8 hours, so when all patches are 
accepted, I'll do a rebase, etc. in one go to make sure there are no problems.




Comment at: llvm/include/llvm/Frontend/OpenMP/OMPKinds.def:234
 __OMP_RTL(__kmpc_critical, false, Void, IdentPtr, Int32, KmpCriticalNamePtrTy)
-__OMP_RTL(__kmpc_critical_with_hint, false, Void, IdentPtr, Int32, 
KmpCriticalNamePtrTy, Int32)
+__OMP_RTL(__kmpc_critical_with_hint, false, Void, IdentPtr, Int32, 
KmpCriticalNamePtrTy, IntPtrTy)
 __OMP_RTL(__kmpc_end_critical, false, Void, IdentPtr, Int32, 
KmpCriticalNamePtrTy)

jdoerfert wrote:
> fghanim wrote:
> > jdoerfert wrote:
> > > fghanim wrote:
> > > > jdoerfert wrote:
> > > > > I think this was correct before:
> > > > > ```
> > > > >   KMP_EXPORT void __kmpc_critical_with_hint(ident_t *, kmp_int32 
> > > > > global_tid, kmp_critical_name *, uint32_t hint);
> > > > > ```
> > > > Nop, it's supposed to be whatever `IntPtrTy` is in the frontend (i.e. 
> > > > 32 for 32bit, 64 for 64bit).
> > > > 
> > > > `IntPtrTy` is actually a union with `sizety` in `CodeGenModule`
> > > I'm confused. I copied the declaration above from the runtime. It doesn't 
> > > contain an `IntPtrTy` or similar. I agree that `IntPtrTy` is machine 
> > > dependent and we need your initializers. What I tried to say is that at 
> > > least one declaration in the runtime has `__kmpc_critical_with_hint` with 
> > > an int32 as last argument. That said, the runtime is not shy of 
> > > incompatible declarations for functions.
> > I cannot speak for what's in the runtime. However, in clang, this currently 
> > is defined to use `IntPtrTy`. If you go check, I have a todo in the current 
> > implementation for critical to come back and fix it.
> That is just an existing bug in Clang. The runtime is consistent with the 
> type and arguably it is the runtime we must match, not the other way around ;)
Apparently, this used to be `uintptr_t` in the runtime and was changed by 
D51235 . It doesn't say why the change was made.

Clang uses this in multiple places, which makes me think it may have been 
intentional. So I suggest we go by the standard. Where can I find what does the 
OMP standard say about how the signature of the runtime calls should look like? 
This way I'll fix this one accordingly, and later we may go and make sure that 
all the rest match up with the standard - where possible.



Comment at: llvm/lib/Frontend/OpenMP/OMPConstants.cpp:69
 
 void llvm::omp::types::initializeTypes(Module ) {
   if (Void)

jdoerfert wrote:
> Maybe we can/should take the IntPtr and SizeTy here so there is only a single 
> call necessary that initializes all types. I fear "default" values are 
> problematic.
> 
> WDYT?
you mean pass it as a second argument? Sure, I would love that. I mean it's 
fine with me either way. I originally wanted to do that. but then thought maybe 
it is slightly better to explicitly initialize target specific data types, to 
give initialize type a cleaner look ... maybe?!



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:106
+  initializeVoidPtrTy(VoidPtrTy);
+}
+

jdoerfert wrote:
> I guess we can directly call the initialize functions, right?
> With an assert that SizeTy is set we can make sure users do it ;)
Sure, then again, I am just following in the stylistic footsteps of the 
pioneers who started the OMPBuilder, who created an initialize for initialize ;)

On second thought, I think I will just create something like 
`initializeTargetSpecificTypes()` sort of thing, and be done with it. instead 
of having one for each type.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79675/new/

https://reviews.llvm.org/D79675



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79677: [clang][OpenMP][OMPIRBuilder] Adding some Privatization clauses to OpenMP `parallel` Directive

2020-05-14 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked 2 inline comments as done.
fghanim added a comment.

In D79677#2032942 , @jdoerfert wrote:

> Generally you copied the existing Clang logic, correct?


Well, Yes and no. I tried to keep as much as I can of the original 
implementation, however, some required more extensive changes.

The things added to `emitparalleldirective` are all new
Almost the latter half of the OMPBuilder version of `emitfirstprivateclause`
somethings in the the OMPBuilder version of `emitcopyinclause`
The rest had minor or smaller changes, but generally the same
the lit tests had some changes




Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:803
-  OrigVD);
-else
-  (void)CGM.getOpenMPRuntime().registerTargetFirstprivateCopy(*this,

jdoerfert wrote:
> Wasn't this part of D79675?
> 
> (btw I tried to comprehend why this is needed and it is on my list for things 
> we replace eventually).
ignore - I originally wanted to use the original `emitfirstprivate`, before I 
had to make some changes. This is remaining from that code. The same comment / 
todo is in OMPBuilder specific version below.

I also, removed this from D79676


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79677/new/

https://reviews.llvm.org/D79677



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79676: [Clang][OpenMP][OMPBuilder] Moving OMP allocation and cache creation code to OMPBuilderCBHelpers

2020-05-14 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked 4 inline comments as done.
fghanim added inline comments.



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:878
+  [this, VDInit, OriginalAddr, VD, ThisFirstprivateIsLastprivate,
+   OrigVD, , IRef, ]() {
 // Emit private VarDecl with copy init.

jdoerfert wrote:
> Is this needed?
All these are changes that I forgot to remove after I created a special version 
for `EmitOMPFirstprivateClause`  in D79677 for the OMPBuilder



Comment at: clang/lib/CodeGen/CodeGenFunction.h:1581
+++I;
+
+  return &*I;

jdoerfert wrote:
> What is wrong with `BB->getFirstNonPHIOrDbgOrLifetime()`? If it is to avoid 
> too many test changes, please add a TODO. I think from a functional 
> perspective there is no need to advance it after the first set of allocas 
> that may be separated from the phis by non-allocas?
Nothing wrong with it. The first while loop, I just came across a case that I 
don't remember at the moment that had an instruction that wasn't an alloca, and 
I wanted to skip over that.
the second while, is to get to the first non-alloca instruction. The purpose is 
to be able to insert new allocas, after already generated allocas.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79676/new/

https://reviews.llvm.org/D79676



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79676: [Clang][OpenMP][OMPBuilder] Moving OMP allocation and cache creation code to OMPBuilderCBHelpers

2020-05-14 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 264012.
fghanim marked an inline comment as done.
fghanim added a comment.

updating in response to review comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79676/new/

https://reviews.llvm.org/D79676

Files:
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CGExpr.cpp
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CodeGenFunction.h

Index: clang/lib/CodeGen/CodeGenFunction.h
===
--- clang/lib/CodeGen/CodeGenFunction.h
+++ clang/lib/CodeGen/CodeGenFunction.h
@@ -26,6 +26,7 @@
 #include "clang/AST/ExprCXX.h"
 #include "clang/AST/ExprObjC.h"
 #include "clang/AST/ExprOpenMP.h"
+#include "clang/AST/StmtOpenMP.h"
 #include "clang/AST/Type.h"
 #include "clang/Basic/ABI.h"
 #include "clang/Basic/CapturedStmt.h"
@@ -77,6 +78,7 @@
 class ObjCAtSynchronizedStmt;
 class ObjCAutoreleasePoolStmt;
 class ReturnsNonNullAttr;
+class OMPExecutableDirective;
 
 namespace analyze_os_log {
 class OSLogBufferLayout;
@@ -256,114 +258,6 @@
 unsigned Index;
   };
 
-  // Helper class for the OpenMP IR Builder. Allows reusability of code used for
-  // region body, and finalization codegen callbacks. This will class will also
-  // contain privatization functions used by the privatization call backs
-  struct OMPBuilderCBHelpers {
-
-using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
-
-/// Emit the Finalization for an OMP region
-/// \param CGF	The Codegen function this belongs to
-/// \param IP	Insertion point for generating the finalization code.
-static void FinalizeOMPRegion(CodeGenFunction , InsertPointTy IP) {
-  CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
-  assert(IP.getBlock()->end() != IP.getPoint() &&
- "OpenMP IR Builder should cause terminated block!");
-
-  llvm::BasicBlock *IPBB = IP.getBlock();
-  llvm::BasicBlock *DestBB = IPBB->getUniqueSuccessor();
-  assert(DestBB && "Finalization block should have one successor!");
-
-  // erase and replace with cleanup branch.
-  IPBB->getTerminator()->eraseFromParent();
-  CGF.Builder.SetInsertPoint(IPBB);
-  CodeGenFunction::JumpDest Dest = CGF.getJumpDestInCurrentScope(DestBB);
-  CGF.EmitBranchThroughCleanup(Dest);
-}
-
-/// Emit the body of an OMP region
-/// \param CGF	The Codegen function this belongs to
-/// \param RegionBodyStmt	The body statement for the OpenMP region being
-/// 			 generated
-/// \param CodeGenIP	Insertion point for generating the body code.
-/// \param FiniBB	The finalization basic block
-static void EmitOMPRegionBody(CodeGenFunction ,
-  const Stmt *RegionBodyStmt,
-  InsertPointTy CodeGenIP,
-  llvm::BasicBlock ) {
-  llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
-  if (llvm::Instruction *CodeGenIPBBTI = CodeGenIPBB->getTerminator())
-CodeGenIPBBTI->eraseFromParent();
-
-  CGF.Builder.SetInsertPoint(CodeGenIPBB);
-
-  CGF.EmitStmt(RegionBodyStmt);
-
-  if (CGF.Builder.saveIP().isSet())
-CGF.Builder.CreateBr();
-}
-
-/// RAII for preserving necessary info during Outlined region body codegen.
-class OutlinedRegionBodyRAII {
-
-  llvm::AssertingVH OldAllocaIP;
-  CodeGenFunction::JumpDest OldReturnBlock;
-  CodeGenFunction 
-
-public:
-  OutlinedRegionBodyRAII(CodeGenFunction , InsertPointTy ,
- llvm::BasicBlock )
-  : CGF(cgf) {
-assert(AllocaIP.isSet() &&
-   "Must specify Insertion point for allocas of outlined function");
-OldAllocaIP = CGF.AllocaInsertPt;
-CGF.AllocaInsertPt = &*AllocaIP.getPoint();
-
-OldReturnBlock = CGF.ReturnBlock;
-CGF.ReturnBlock = CGF.getJumpDestInCurrentScope();
-  }
-
-  ~OutlinedRegionBodyRAII() {
-CGF.AllocaInsertPt = OldAllocaIP;
-CGF.ReturnBlock = OldReturnBlock;
-  }
-};
-
-/// RAII for preserving necessary info during inlined region body codegen.
-class InlinedRegionBodyRAII {
-
-  llvm::AssertingVH OldAllocaIP;
-  CodeGenFunction 
-
-public:
-  InlinedRegionBodyRAII(CodeGenFunction , InsertPointTy ,
-llvm::BasicBlock )
-  : CGF(cgf) {
-// Alloca insertion block should be in the entry block of the containing
-// function so it expects an empty AllocaIP in which case will reuse the
-// old alloca insertion point, or a new AllocaIP in the same block as
-// the old one
-assert((!AllocaIP.isSet() ||
-CGF.AllocaInsertPt->getParent() == AllocaIP.getBlock()) &&
-   "Insertion point should be in the entry block of containing "
-   "function!");
-OldAllocaIP = CGF.AllocaInsertPt;
-if 

[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-05-14 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 264006.
fghanim marked an inline comment as done.
fghanim added a comment.

- removed many Definitions from OMPKinds.def due to being added in D79739 

- made changes based on reviewer comments
- added unit test for `CreateCopyinClauseBlocks()`


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79675/new/

https://reviews.llvm.org/D79675

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPConstants.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
  llvm/lib/Frontend/OpenMP/OMPConstants.cpp
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -779,4 +779,41 @@
   EXPECT_EQ(CriticalEndCI->getArgOperand(2)->getType(), CriticalNamePtrTy);
 }
 
+TEST_F(OpenMPIRBuilderTest, CopyinBlocks) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+  IntegerType* Int32 = Type::getInt32Ty(M->getContext());
+  AllocaInst* MasterAddress = Builder.CreateAlloca(Int32->getPointerTo());
+	AllocaInst* PrivAddress = Builder.CreateAlloca(Int32->getPointerTo());
+
+  BasicBlock *EntryBB = BB;
+
+  OMPBuilder.CreateCopyinClauseBlocks(Builder.saveIP(), MasterAddress, PrivAddress, Int32, /*BranchtoEnd*/true);
+
+  BranchInst* EntryBr = dyn_cast_or_null(EntryBB->getTerminator());
+
+  EXPECT_NE(EntryBr, nullptr);
+  EXPECT_TRUE(EntryBr->isConditional());
+
+  BasicBlock* NotMasterBB = EntryBr->getSuccessor(0);
+  BasicBlock* CopyinEnd = EntryBr->getSuccessor(1);
+  CmpInst* CMP = dyn_cast_or_null(EntryBr->getCondition());
+
+  EXPECT_NE(CMP, nullptr);
+  EXPECT_NE(NotMasterBB, nullptr);
+  EXPECT_NE(CopyinEnd, nullptr);
+
+  BranchInst* NotMasterBr = dyn_cast_or_null(NotMasterBB->getTerminator());
+  EXPECT_NE(NotMasterBr, nullptr);
+  EXPECT_FALSE(NotMasterBr->isConditional());
+  EXPECT_EQ(CopyinEnd,NotMasterBr->getSuccessor(0));
+}
+
 } // namespace
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -93,6 +93,18 @@
 
 void OpenMPIRBuilder::initialize() { initializeTypes(M); }
 
+void OpenMPIRBuilder::setIntPtrTy(IntegerType *IntPtrTy) {
+  initializeIntPtrTy(IntPtrTy);
+}
+
+void OpenMPIRBuilder::setSizeTy(IntegerType *SizeTy) {
+  initializeSizeTy(SizeTy);
+}
+
+void OpenMPIRBuilder::setVoidPtrTy(PointerType *VoidPtrTy) {
+  initializeVoidPtrTy(VoidPtrTy);
+}
+
 void OpenMPIRBuilder::finalize() {
   for (OutlineInfo  : OutlineInfos) {
 assert(!OI.Blocks.empty() &&
@@ -576,9 +588,6 @@
  "Unexpected finalization stack state!");
 
   Instruction *PRegPreFiniTI = PRegPreFiniBB->getTerminator();
-  assert(PRegPreFiniTI->getNumSuccessors() == 1 &&
- PRegPreFiniTI->getSuccessor(0) == PRegExitBB &&
- "Unexpected CFG structure!");
 
   InsertPointTy PreFiniIP(PRegPreFiniBB, PRegPreFiniTI->getIterator());
   FiniCB(PreFiniIP);
@@ -912,6 +921,106 @@
   ExitCall->getIterator());
 }
 
+OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::CreateCopyinClauseBlocks(
+InsertPointTy IP, Value *MasterAddr, Value *PrivateAddr,
+llvm::IntegerType *IntPtrTy, bool BranchtoEnd) {
+  if (!IP.isSet())
+return IP;
+
+  IRBuilder<>::InsertPointGuard IPG(Builder);
+
+  // creates the following CFG structure
+  //	   OMP_Entry : (MasterAddr != PrivateAddr)?
+  //   F T
+  //   |  \
+  //   | copin.not.master
+  //   |  /
+  //   v /
+  //   copyin.not.master.end
+  //		 |
+  // v
+  //   OMP.Entry.Next
+
+  BasicBlock *OMP_Entry = IP.getBlock();
+  Function *CurFn = OMP_Entry->getParent();
+  BasicBlock *CopyBegin =
+  BasicBlock::Create(M.getContext(), "copyin.not.master", CurFn);
+  BasicBlock *CopyEnd = nullptr;
+
+  // If entry block is terminated, split to preserve the branch to following
+  // basic block (i.e. OMP.Entry.Next), otherwise, leave everything as is.
+  if (isa_and_nonnull(OMP_Entry->getTerminator())) {
+CopyEnd = OMP_Entry->splitBasicBlock(OMP_Entry->getTerminator(),
+ "copyin.not.master.end");
+OMP_Entry->getTerminator()->eraseFromParent();
+  } else {
+CopyEnd =
+BasicBlock::Create(M.getContext(), "copyin.not.master.end", CurFn);
+  }
+
+  Builder.SetInsertPoint(OMP_Entry);
+  

[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-05-14 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked 7 inline comments as done.
fghanim added inline comments.



Comment at: llvm/include/llvm/Frontend/OpenMP/OMPConstants.h:101
+extern Type *IntPtrTy;
+extern Type *SizeTy;
+

jdoerfert wrote:
> jdoerfert wrote:
> > I can totally see why we need `int` and `size_t` but I'm not sure about the 
> > others.
> > `void` is universally translated to `i8*` Adding a pointer to a type should 
> > be done in OMPKinds.def, something like:
> > ```
> > #define __OMP_PTR_TYPE(NAME, BASE) OMP_TYPE(NAME, Base->getPointerTo());
> > __OMP_PTR_TYPE(VoidPtr, Int8)
> > __OMP_PTR_TYPE(VoidPtrPtr, VoidPtr)
> > ```
> My proposed scheme for the pointers was integrated in D79739 and will be 
> commited shortly.
Kept a couple a Ineed in OMPConstants. will add them in OMPKinds.def after the 
other patch lands.



Comment at: llvm/include/llvm/Frontend/OpenMP/OMPKinds.def:234
 __OMP_RTL(__kmpc_critical, false, Void, IdentPtr, Int32, KmpCriticalNamePtrTy)
-__OMP_RTL(__kmpc_critical_with_hint, false, Void, IdentPtr, Int32, 
KmpCriticalNamePtrTy, Int32)
+__OMP_RTL(__kmpc_critical_with_hint, false, Void, IdentPtr, Int32, 
KmpCriticalNamePtrTy, IntPtrTy)
 __OMP_RTL(__kmpc_end_critical, false, Void, IdentPtr, Int32, 
KmpCriticalNamePtrTy)

jdoerfert wrote:
> fghanim wrote:
> > jdoerfert wrote:
> > > I think this was correct before:
> > > ```
> > >   KMP_EXPORT void __kmpc_critical_with_hint(ident_t *, kmp_int32 
> > > global_tid, kmp_critical_name *, uint32_t hint);
> > > ```
> > Nop, it's supposed to be whatever `IntPtrTy` is in the frontend (i.e. 32 
> > for 32bit, 64 for 64bit).
> > 
> > `IntPtrTy` is actually a union with `sizety` in `CodeGenModule`
> I'm confused. I copied the declaration above from the runtime. It doesn't 
> contain an `IntPtrTy` or similar. I agree that `IntPtrTy` is machine 
> dependent and we need your initializers. What I tried to say is that at least 
> one declaration in the runtime has `__kmpc_critical_with_hint` with an int32 
> as last argument. That said, the runtime is not shy of incompatible 
> declarations for functions.
I cannot speak for what's in the runtime. However, in clang, this currently is 
defined to use `IntPtrTy`. If you go check, I have a todo in the current 
implementation for critical to come back and fix it.



Comment at: llvm/include/llvm/Frontend/OpenMP/OMPKinds.def:240
+__OMP_RTL(__kmpc_threadprivate_cached, false, VoidPtr, IdentPtr, Int32, 
VoidPtr, SizeTy, Void3Ptr)
+__OMP_RTL(__kmpc_threadprivate_register, false, Void, IdentPtr, VoidPtr, 
KmpcCtorTy, KmpcCopyCtorTy, KmpcDtorTy)
+

jdoerfert wrote:
> Can you add attributes (below) for these and call them in 
> `llvm/test/Transforms/OpenMP/add_attributes.ll` [if not already present]. 
> These changes and the additional types should go in a separate patch.
Added in D79739



Comment at: llvm/lib/Frontend/OpenMP/OMPConstants.cpp:122
+  }
+}
+

jdoerfert wrote:
> fghanim wrote:
> > jdoerfert wrote:
> > > (I doubt we need the `initVoidPtr` function but if we do, the condition 
> > > looks wrong)
> > Forgot to refractor when I changed names.
> > 
> > Regarding `void*` , I am not sure how it is defined in fortran. That's why 
> > I made it possible to initialize it separately.
> I see. Let's cross that bridge once we get to it.
Modified it a bit and removed usage from initialization. If fortran people 
don't end using it, then we can remove it.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79675/new/

https://reviews.llvm.org/D79675



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-05-12 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked 5 inline comments as done.
fghanim added a comment.

This is a small patch as it is. splitting it any further would make it very 
very small :D




Comment at: llvm/include/llvm/Frontend/OpenMP/OMPKinds.def:234
 __OMP_RTL(__kmpc_critical, false, Void, IdentPtr, Int32, KmpCriticalNamePtrTy)
-__OMP_RTL(__kmpc_critical_with_hint, false, Void, IdentPtr, Int32, 
KmpCriticalNamePtrTy, Int32)
+__OMP_RTL(__kmpc_critical_with_hint, false, Void, IdentPtr, Int32, 
KmpCriticalNamePtrTy, IntPtrTy)
 __OMP_RTL(__kmpc_end_critical, false, Void, IdentPtr, Int32, 
KmpCriticalNamePtrTy)

jdoerfert wrote:
> I think this was correct before:
> ```
>   KMP_EXPORT void __kmpc_critical_with_hint(ident_t *, kmp_int32 global_tid, 
> kmp_critical_name *, uint32_t hint);
> ```
Nop, it's supposed to be whatever `IntPtrTy` is in the frontend (i.e. 32 for 
32bit, 64 for 64bit).

`IntPtrTy` is actually a union with `sizety` in `CodeGenModule`



Comment at: llvm/lib/Frontend/OpenMP/OMPConstants.cpp:122
+  }
+}
+

jdoerfert wrote:
> (I doubt we need the `initVoidPtr` function but if we do, the condition looks 
> wrong)
Forgot to refractor when I changed names.

Regarding `void*` , I am not sure how it is defined in fortran. That's why I 
made it possible to initialize it separately.



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:594
+  // PRegPreFiniTI->getSuccessor(0) == PRegExitBB &&
+  // "Unexpected CFG structure!");
 

jdoerfert wrote:
> Preferably we would have a modified assertion. If that is too cumbersome, we 
> can probably remove it. 
I was going to remove it actually. When running Dtor over an array, clang emits 
some loop logic that makes this untrue.



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:1013
+  return Builder.CreateCall(Fn, Args);
+}
+

jdoerfert wrote:
> I think we should have a unit test for each of these.
> 
> Style: drop the `llvm::`, not needed.
Aside from `CreateCopyinclauseblocks()`, I couldn't think of a unittest for the 
others, all they do is create a runtime call based on the passed arg. That's 
why I didn't do it already.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79675/new/

https://reviews.llvm.org/D79675



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D79677: [clang][OpenMP][OMPIRBuilder] Adding some Privatization clauses to OpenMP `parallel` Directive

2020-05-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim created this revision.
fghanim added a reviewer: jdoerfert.
Herald added subscribers: cfe-commits, guansong, yaxunl.
Herald added a project: clang.
fghanim added a parent revision: D79676: [Clang][OpenMP][OMPBuilder] Moving OMP 
allocation and cache creation code to OMPBuilderCBHelpers.

- Added support for Codegen `private` clause
- Added support for Codegen `firstprivate` Clause
- Added support for CodeGen of `copyin` Clause
- Added/moved code to support above tasks on the OMPIRBuilder


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D79677

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CodeGenFunction.h
  clang/test/OpenMP/parallel_copyin_codegen.cpp
  clang/test/OpenMP/parallel_firstprivate_codegen.cpp
  clang/test/OpenMP/parallel_private_codegen.cpp

Index: clang/test/OpenMP/parallel_private_codegen.cpp
===
--- clang/test/OpenMP/parallel_private_codegen.cpp
+++ clang/test/OpenMP/parallel_private_codegen.cpp
@@ -1,8 +1,9 @@
-// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-unknown -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-unknown -emit-llvm %s -o - | FileCheck %s -check-prefixes=ALL,CHECK
 // RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple x86_64-unknown-unknown -emit-pch -o %t %s
-// RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-unknown-unknown -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-unknown-unknown -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s -check-prefixes=ALL,CHECK
 // RUN: %clang_cc1 -verify -fopenmp -x c++ -std=c++11 -DLAMBDA -triple %itanium_abi_triple -emit-llvm %s -o - | FileCheck -check-prefix=LAMBDA %s
 // RUN: %clang_cc1 -verify -fopenmp -x c++ -fblocks -DBLOCKS -triple %itanium_abi_triple -emit-llvm %s -o - | FileCheck -check-prefix=BLOCKS %s
+// RUN: %clang_cc1 -verify -fopenmp -fopenmp-enable-irbuilder -x c++ -triple x86_64-unknown-unknown -emit-llvm %s -o - | FileCheck %s -check-prefixes=ALL,IRBUILDER
 
 // RUN: %clang_cc1 -verify -fopenmp-simd -x c++ -triple x86_64-unknown-unknown -emit-llvm %s -o - | FileCheck --check-prefix SIMD-ONLY0 %s
 // RUN: %clang_cc1 -fopenmp-simd -x c++ -std=c++11 -triple x86_64-unknown-unknown -emit-pch -o %t %s
@@ -91,12 +92,12 @@
   }
 };
 
-// CHECK: [[SS_TY:%.+]] = type { i{{[0-9]+}}, i8
+// ALL: [[SS_TY:%.+]] = type { i{{[0-9]+}}, i8
 // LAMBDA: [[SS_TY:%.+]] = type { i{{[0-9]+}}, i8
 // BLOCKS: [[SS_TY:%.+]] = type { i{{[0-9]+}}, i8
-// CHECK: [[S_FLOAT_TY:%.+]] = type { float }
-// CHECK: [[S_INT_TY:%.+]] = type { i{{[0-9]+}} }
-// CHECK: [[SST_TY:%.+]] = type { i{{[0-9]+}} }
+// ALL: [[S_FLOAT_TY:%.+]] = type { float }
+// ALL: [[S_INT_TY:%.+]] = type { i{{[0-9]+}} }
+// ALL: [[SST_TY:%.+]] = type { i{{[0-9]+}} }
 template 
 T tmain() {
   S test;
@@ -273,63 +274,93 @@
 #endif
 }
 
-// CHECK: define i{{[0-9]+}} @main()
-// CHECK: [[TEST:%.+]] = alloca [[S_FLOAT_TY]],
-// CHECK: call {{.*}} [[S_FLOAT_TY_DEF_CONSTR:@.+]]([[S_FLOAT_TY]]* [[TEST]])
-// CHECK: call void (%{{.+}}*, i{{[0-9]+}}, void (i{{[0-9]+}}*, i{{[0-9]+}}*, ...)*, ...) @__kmpc_fork_call(%{{.+}}* @{{.+}}, i{{[0-9]+}} 0, void (i{{[0-9]+}}*, i{{[0-9]+}}*, ...)* bitcast (void (i{{[0-9]+}}*, i{{[0-9]+}}*)* [[MAIN_MICROTASK:@.+]] to void
-// CHECK: = call i{{.+}} [[TMAIN_INT:@.+]]()
-// CHECK: call void [[S_FLOAT_TY_DESTR:@.+]]([[S_FLOAT_TY]]*
-// CHECK: ret
+// ALL: define i{{[0-9]+}} @main()
+// ALL: [[TEST:%.+]] = alloca [[S_FLOAT_TY]],
+// ALL: call {{.*}} [[S_FLOAT_TY_DEF_CONSTR:@.+]]([[S_FLOAT_TY]]* [[TEST]])
+// ALL: call void (%{{.+}}*, i{{[0-9]+}}, void (i{{[0-9]+}}*, i{{[0-9]+}}*, ...)*, ...) @__kmpc_fork_call(%{{.+}}* @{{.+}}, i{{[0-9]+}} 0, void (i{{[0-9]+}}*, i{{[0-9]+}}*, ...)* bitcast (void (i{{[0-9]+}}*, i{{[0-9]+}}*)* [[MAIN_MICROTASK:@.+]] to void
+// ALL: = call i{{.+}} [[TMAIN_INT:@.+]]()
+// ALL: call void [[S_FLOAT_TY_DESTR:@.+]]([[S_FLOAT_TY]]*
+// ALL: ret
 //
-// CHECK: define internal void [[MAIN_MICROTASK]](i{{[0-9]+}}* noalias [[GTID_ADDR:%.+]], i{{[0-9]+}}* noalias %{{.+}})
-// CHECK: [[T_VAR_PRIV:%.+]] = alloca i{{[0-9]+}},
-// CHECK: [[VEC_PRIV:%.+]] = alloca [2 x i{{[0-9]+}}],
-// CHECK: [[S_ARR_PRIV:%.+]] = alloca [2 x [[S_FLOAT_TY]]],
-// CHECK: [[VAR_PRIV:%.+]] = alloca [[S_FLOAT_TY]],
-// CHECK: [[SIVAR_PRIV:%.+]] = alloca i{{[0-9]+}},
+// ALL: define internal void [[MAIN_MICROTASK]](i{{[0-9]+}}* noalias [[GTID_ADDR:%.+]], i{{[0-9]+}}* noalias %{{.+}})
+// ALL: [[T_VAR_PRIV:%.+]] = alloca i{{[0-9]+}},
+// ALL: [[VEC_PRIV:%.+]] = alloca [2 x i{{[0-9]+}}],
+// ALL: [[S_ARR_PRIV:%.+]] = alloca [2 x [[S_FLOAT_TY]]],
+// ALL: [[VAR_PRIV:%.+]] = alloca [[S_FLOAT_TY]],
+// ALL: [[SIVAR_PRIV:%.+]] = alloca i{{[0-9]+}},
 // CHECK: store i{{[0-9]+}}* [[GTID_ADDR]], i{{[0-9]+}}** [[GTID_ADDR_REF:%.+]]
-// CHECK-NOT: [[T_VAR_PRIV]]
-// CHECK-NOT: [[VEC_PRIV]]
-// CHECK: {{.+}}:

[PATCH] D79675: [OpenMP][OMPBuilder] Adding Privatization Requirements to OMPIRBuilder

2020-05-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim created this revision.
fghanim added a reviewer: jdoerfert.
Herald added subscribers: llvm-commits, cfe-commits, guansong, hiraditya, 
yaxunl.
Herald added projects: clang, LLVM.

Added new methods to generate new runtime calls
Added all required defenitions
Added some target-specific types


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D79675

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPConstants.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
  llvm/lib/Frontend/OpenMP/OMPConstants.cpp
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -93,6 +93,18 @@
 
 void OpenMPIRBuilder::initialize() { initializeTypes(M); }
 
+void OpenMPIRBuilder::setIntPtrTy(IntegerType *IntPtrTy) {
+  initializeIntPtrTy(IntPtrTy);
+}
+
+void OpenMPIRBuilder::setSizeTy(IntegerType *SizeTy) {
+  initializeSizeTy(SizeTy);
+}
+
+void OpenMPIRBuilder::setVoidPtrTy(PointerType *VoidPtrTy) {
+  initializeVoidPtrTy(VoidPtrTy);
+}
+
 void OpenMPIRBuilder::finalize() {
   for (OutlineInfo  : OutlineInfos) {
 assert(!OI.Blocks.empty() &&
@@ -576,9 +588,10 @@
  "Unexpected finalization stack state!");
 
   Instruction *PRegPreFiniTI = PRegPreFiniBB->getTerminator();
-  assert(PRegPreFiniTI->getNumSuccessors() == 1 &&
- PRegPreFiniTI->getSuccessor(0) == PRegExitBB &&
- "Unexpected CFG structure!");
+
+  //  assert(PRegPreFiniTI->getNumSuccessors() == 1 &&
+  // PRegPreFiniTI->getSuccessor(0) == PRegExitBB &&
+  // "Unexpected CFG structure!");
 
   InsertPointTy PreFiniIP(PRegPreFiniBB, PRegPreFiniTI->getIterator());
   FiniCB(PreFiniIP);
@@ -912,6 +925,93 @@
   ExitCall->getIterator());
 }
 
+OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::CreateCopyinClauseBlocks(
+InsertPointTy IP, Value *MasterAddr, Value *PrivateAddr,
+llvm::IntegerType *IntPtrTy, bool BranchtoEnd) {
+  if (!IP.isSet())
+return IP;
+
+  IRBuilder<>::InsertPointGuard IPG(Builder);
+
+  BasicBlock *OMP_Entry = IP.getBlock();
+  Function *CurFn = OMP_Entry->getParent();
+  BasicBlock *CopyBegin =
+  BasicBlock::Create(M.getContext(), "copyin.not.master", CurFn);
+  BasicBlock *CopyEnd = nullptr;
+  // If entry block is terminated, split to preserve the branch to following
+  // basic block, otherwise, leave everything as is.
+  if (isa_and_nonnull(OMP_Entry->getTerminator())) {
+CopyEnd = OMP_Entry->splitBasicBlock(OMP_Entry->getTerminator(),
+ "copyin.not.master.end");
+OMP_Entry->getTerminator()->eraseFromParent();
+  } else {
+CopyEnd =
+BasicBlock::Create(M.getContext(), "copyin.not.master.end", CurFn);
+  }
+
+  Builder.SetInsertPoint(OMP_Entry);
+  Value *MasterPtr = Builder.CreatePtrToInt(MasterAddr, IntPtrTy);
+  Value *PrivatePtr = Builder.CreatePtrToInt(PrivateAddr, IntPtrTy);
+  Value *cmp = Builder.CreateICmpNE(MasterPtr, PrivatePtr);
+  Builder.CreateCondBr(cmp, CopyBegin, CopyEnd);
+
+  Builder.SetInsertPoint(CopyBegin);
+  if (BranchtoEnd) {
+BranchInst *br = Builder.CreateBr(CopyEnd);
+Builder.SetInsertPoint(br);
+  }
+
+  return Builder.saveIP();
+}
+
+CallInst *OpenMPIRBuilder::CreateOMPAlloc(const LocationDescription ,
+  Value *Size, Value *Allocator,
+  std::string Name) {
+  IRBuilder<>::InsertPointGuard IPG(Builder);
+  Builder.restoreIP(Loc.IP);
+
+  Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
+  Value *Ident = getOrCreateIdent(SrcLocStr);
+  Value *ThreadId = getOrCreateThreadID(Ident);
+  Value *Args[] = {ThreadId, Size, Allocator};
+
+  Function *Fn = getOrCreateRuntimeFunction(OMPRTL___kmpc_alloc);
+
+  return Builder.CreateCall(Fn, Args, Name);
+}
+
+CallInst *OpenMPIRBuilder::CreateOMPFree(const LocationDescription ,
+ Value *Addr, Value *Allocator,
+ std::string Name) {
+  IRBuilder<>::InsertPointGuard IPG(Builder);
+  Builder.restoreIP(Loc.IP);
+
+  Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
+  Value *Ident = getOrCreateIdent(SrcLocStr);
+  Value *ThreadId = getOrCreateThreadID(Ident);
+  Value *Args[] = {ThreadId, Addr, Allocator};
+  Function *Fn = getOrCreateRuntimeFunction(OMPRTL___kmpc_free);
+  return Builder.CreateCall(Fn, Args, Name);
+}
+
+CallInst *OpenMPIRBuilder::CreateCachedThreadPrivate(
+const LocationDescription , llvm::Value *Pointer,
+llvm::ConstantInt *Size, const llvm::Twine ) {
+  IRBuilder<>::InsertPointGuard IPG(Builder);
+  Builder.restoreIP(Loc.IP);
+
+  Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
+  Value *Ident = getOrCreateIdent(SrcLocStr);
+  Value 

[PATCH] D79676: [Clang][OpenMP][OMPBuilder] Moving OMP allocation and cache creation code to OMPBuilderCBHelpers

2020-05-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim created this revision.
fghanim added a reviewer: jdoerfert.
Herald added subscribers: cfe-commits, guansong, yaxunl.
Herald added a project: clang.

Modified the OMPBuilderCBHelpers in the following ways:

- Moved location of class definition and deleted all constructors
- Moved OpenMP-specific address allocation of local variables
- Moved threadprivate variable creation for the current thread


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D79676

Files:
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CGExpr.cpp
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CodeGenFunction.h

Index: clang/lib/CodeGen/CodeGenFunction.h
===
--- clang/lib/CodeGen/CodeGenFunction.h
+++ clang/lib/CodeGen/CodeGenFunction.h
@@ -26,6 +26,7 @@
 #include "clang/AST/ExprCXX.h"
 #include "clang/AST/ExprObjC.h"
 #include "clang/AST/ExprOpenMP.h"
+#include "clang/AST/StmtOpenMP.h"
 #include "clang/AST/Type.h"
 #include "clang/Basic/ABI.h"
 #include "clang/Basic/CapturedStmt.h"
@@ -77,6 +78,7 @@
 class ObjCAtSynchronizedStmt;
 class ObjCAutoreleasePoolStmt;
 class ReturnsNonNullAttr;
+class OMPExecutableDirective;
 
 namespace analyze_os_log {
 class OSLogBufferLayout;
@@ -256,114 +258,6 @@
 unsigned Index;
   };
 
-  // Helper class for the OpenMP IR Builder. Allows reusability of code used for
-  // region body, and finalization codegen callbacks. This will class will also
-  // contain privatization functions used by the privatization call backs
-  struct OMPBuilderCBHelpers {
-
-using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
-
-/// Emit the Finalization for an OMP region
-/// \param CGF	The Codegen function this belongs to
-/// \param IP	Insertion point for generating the finalization code.
-static void FinalizeOMPRegion(CodeGenFunction , InsertPointTy IP) {
-  CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
-  assert(IP.getBlock()->end() != IP.getPoint() &&
- "OpenMP IR Builder should cause terminated block!");
-
-  llvm::BasicBlock *IPBB = IP.getBlock();
-  llvm::BasicBlock *DestBB = IPBB->getUniqueSuccessor();
-  assert(DestBB && "Finalization block should have one successor!");
-
-  // erase and replace with cleanup branch.
-  IPBB->getTerminator()->eraseFromParent();
-  CGF.Builder.SetInsertPoint(IPBB);
-  CodeGenFunction::JumpDest Dest = CGF.getJumpDestInCurrentScope(DestBB);
-  CGF.EmitBranchThroughCleanup(Dest);
-}
-
-/// Emit the body of an OMP region
-/// \param CGF	The Codegen function this belongs to
-/// \param RegionBodyStmt	The body statement for the OpenMP region being
-/// 			 generated
-/// \param CodeGenIP	Insertion point for generating the body code.
-/// \param FiniBB	The finalization basic block
-static void EmitOMPRegionBody(CodeGenFunction ,
-  const Stmt *RegionBodyStmt,
-  InsertPointTy CodeGenIP,
-  llvm::BasicBlock ) {
-  llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
-  if (llvm::Instruction *CodeGenIPBBTI = CodeGenIPBB->getTerminator())
-CodeGenIPBBTI->eraseFromParent();
-
-  CGF.Builder.SetInsertPoint(CodeGenIPBB);
-
-  CGF.EmitStmt(RegionBodyStmt);
-
-  if (CGF.Builder.saveIP().isSet())
-CGF.Builder.CreateBr();
-}
-
-/// RAII for preserving necessary info during Outlined region body codegen.
-class OutlinedRegionBodyRAII {
-
-  llvm::AssertingVH OldAllocaIP;
-  CodeGenFunction::JumpDest OldReturnBlock;
-  CodeGenFunction 
-
-public:
-  OutlinedRegionBodyRAII(CodeGenFunction , InsertPointTy ,
- llvm::BasicBlock )
-  : CGF(cgf) {
-assert(AllocaIP.isSet() &&
-   "Must specify Insertion point for allocas of outlined function");
-OldAllocaIP = CGF.AllocaInsertPt;
-CGF.AllocaInsertPt = &*AllocaIP.getPoint();
-
-OldReturnBlock = CGF.ReturnBlock;
-CGF.ReturnBlock = CGF.getJumpDestInCurrentScope();
-  }
-
-  ~OutlinedRegionBodyRAII() {
-CGF.AllocaInsertPt = OldAllocaIP;
-CGF.ReturnBlock = OldReturnBlock;
-  }
-};
-
-/// RAII for preserving necessary info during inlined region body codegen.
-class InlinedRegionBodyRAII {
-
-  llvm::AssertingVH OldAllocaIP;
-  CodeGenFunction 
-
-public:
-  InlinedRegionBodyRAII(CodeGenFunction , InsertPointTy ,
-llvm::BasicBlock )
-  : CGF(cgf) {
-// Alloca insertion block should be in the entry block of the containing
-// function so it expects an empty AllocaIP in which case will reuse the
-// old alloca insertion point, or a new AllocaIP in the same block as
-// the old one
-assert((!AllocaIP.isSet() ||
-CGF.AllocaInsertPt->getParent() == 

[PATCH] D77112: [OpenMP][NFCI] Move OpenMP clause information to `lib/Frontend/OpenMP`

2020-04-02 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim accepted this revision.
fghanim added a comment.
This revision is now accepted and ready to land.

OK, As I said a few days ago, I went over the patch, and I didn't see any 
functional changes. I am accepting this patch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77112/new/

https://reviews.llvm.org/D77112



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77112: [OpenMP][NFCI] Move OpenMP clause information to `lib/Frontend/OpenMP`

2020-03-31 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

Figured as much, just wanted to be sure. Anyways, this one also LGTM
I'll wait a couple of days in case any one has comments, if not I'll approve it


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77112/new/

https://reviews.llvm.org/D77112



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77113: [OpenMP][NFC] Move and simplify directive -> allowed clause mapping

2020-03-31 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

Thanks for working on this as well.
As an aside, I like the new allowed clause implementation much better. it is 
much simpler and cleaner than the previous one.
I'll wait to see if anyone else has comments, but if not, then it LGTM.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77113/new/

https://reviews.llvm.org/D77113



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77112: [OpenMP][NFCI] Move OpenMP clause information to `lib/Frontend/OpenMP`

2020-03-31 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

Thanks for doing this. I looked at all of it, and the changes seem to be you 
just moved things to llvm/Frontend, and fixed namespaces/includes to work 
correctly with the new location. Is there here anything else I am missing?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77112/new/

https://reviews.llvm.org/D77112



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D75591: [OpenMP] Add firstprivate as a default data-sharing attribute to clang

2020-03-20 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

My comments were nits so.

LGTM


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75591/new/

https://reviews.llvm.org/D75591



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D75591: [OpenMP] Add firstprivate as a default data-sharing attribute to clang

2020-03-13 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

@jdoerfert To answer your question as to why it is being codegened as shared, 
codegen doesn't handle `default` clause- at least I didn't come across such a 
thing- and if you think about it, it shouldn't need to. You either have `none` 
or `shared`:

- `none` is largly to throw diag errors - so clang either never leaves sema 
because it threw an error, or there are no errors, which suggests that the 
programmer has already listed all captured variables as other clauses. - either 
way, codegen doesn't need to handle it.
- `shared` is the default anyway, and codegen already makes any captured 
variableby the outlined function not "claimed" by any other privatization 
clause, shared - it just passes it as an arg. to the outlined function.

and Since @atmnpatel didn't add any new codegen for the `default{firstprivate}` 
case, codegen defaulted to shared - at least that's what I think happened.

@atmnpatel  The way privatization works currently in codegen, you go over every 
clause, find the list of variables for that clause, find some relevant info 
about the variable along with the proper way to initialize the variable (i.e. 
what constructor to use), and add it to list of all variables to be privatized, 
once done with all privatization clauses, you do codegen for privatization for 
all of them in one go. The exception to this is `copyin`. 
So what you may want to do is to handle the `default` clause in codegen. if 
it's `none` or `shared`, you ignore it. if it is `firstprivate` or `private`, 
find the captured variables of the soon to be outlined function that have not 
been captured by any other privatization clause, then figure a way to pass it 
to `codegenfunction::emitOMPFirstprivateClause` or  
`codegenfunction::emitPrivateClause`. Alternatively, find this list while 
building the AST, and add them as default captured variables, which will make 
codegen much easier. You are already checking for them to throw the `none` diag 
error anyway.




Comment at: clang/include/clang/ASTMatchers/ASTMatchers.h:7087
 
+/// Matches if the OpenMP ``default`` clause has ``shared`` kind specified.
+///

fix comment: ... has ``firstprivate`` kind ...



Comment at: clang/lib/Sema/SemaOpenMP.cpp:4993
 
   // Check variables in the clauses if default(none) was specified.
+  if (DSAStack->getDefaultDSA() == DSA_none ||

fix the comments to say firstprivate as well (here and elsewhere)



Comment at: clang/lib/Sema/SemaOpenMP.cpp:5102
   Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
   << P.first << P.second->getSourceRange();
   Diag(DSAStack->getDefaultDSALocation(), diag::note_omp_default_dsa_none);

Why is `firstprivate` throwing this error? isn't the purpose of specifying it 
as `default` is if a variable is not specified as anything, then it is 
automatically handled as `firstprivate`? or am I misunderstanding something?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75591/new/

https://reviews.llvm.org/D75591



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D75591: [OpenMP] Add firstprivate as a default data-sharing attribute to clang

2020-03-12 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

Done with `private` `firstprivate` `copyin` - working on modifying the clang 
test, and fixing some bugs.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75591/new/

https://reviews.llvm.org/D75591



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D73285: [OpenMP][OMPIRBuilder][BugFix] Handle Unreachable Finalization blocks in `parallel` generation

2020-02-24 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim abandoned this revision.
fghanim added a comment.

The bug this revision attempted to fix has been resolved as part of patch 
D74562 


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D73285/new/

https://reviews.llvm.org/D73285



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D74562: [OpenMP][OMPIRBuilder] Introducing the `OMPBuilderCBHelpers` helper class

2020-02-19 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

I am done updating this patch.
I still don't have commit access, I'd appreciate it if you'd commit this for me 
when you get a chance.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74562/new/

https://reviews.llvm.org/D74562



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D74562: [OpenMP][OMPIRBuilder] Introducing the `OMPBuilderCBHelpers` helper class

2020-02-18 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 245227.
fghanim added a comment.

Marking a call void


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74562/new/

https://reviews.llvm.org/D74562

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CodeGenFunction.h
  clang/test/OpenMP/cancel_codegen.cpp

Index: clang/test/OpenMP/cancel_codegen.cpp
===
--- clang/test/OpenMP/cancel_codegen.cpp
+++ clang/test/OpenMP/cancel_codegen.cpp
@@ -192,9 +192,7 @@
 // IRBUILDER: [[CMP:%.+]] = icmp eq i32 [[RES]], 0
 // IRBUILDER: br i1 [[CMP]], label %[[CONTINUE:[^,].+]], label %[[EXIT:.+]]
 // IRBUILDER: [[EXIT]]
-// IRBUILDER: br label %[[EXIT2:.+]]
-// IRBUILDER: [[EXIT2]]
-// IRBUILDER: br label %[[RETURN]]
+// IRBUILDER: br label %[[RETURN:.+]]
 // IRBUILDER: [[CONTINUE]]
 // IRBUILDER: br label %[[ELSE:.+]]
 
Index: clang/lib/CodeGen/CodeGenFunction.h
===
--- clang/lib/CodeGen/CodeGenFunction.h
+++ clang/lib/CodeGen/CodeGenFunction.h
@@ -36,6 +36,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
 #include "llvm/IR/ValueHandle.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Transforms/Utils/SanitizerStats.h"
@@ -255,6 +256,114 @@
 unsigned Index;
   };
 
+  // Helper class for the OpenMP IR Builder. Allows reusability of code used for
+  // region body, and finalization codegen callbacks. This will class will also
+  // contain privatization functions used by the privatization call backs
+  struct OMPBuilderCBHelpers {
+
+using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
+
+/// Emit the Finalization for an OMP region
+/// \param CGF	The Codegen function this belongs to
+/// \param IP	Insertion point for generating the finalization code.
+static void FinalizeOMPRegion(CodeGenFunction , InsertPointTy IP) {
+  CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
+  assert(IP.getBlock()->end() != IP.getPoint() &&
+ "OpenMP IR Builder should cause terminated block!");
+
+  llvm::BasicBlock *IPBB = IP.getBlock();
+  llvm::BasicBlock *DestBB = IPBB->getUniqueSuccessor();
+  assert(DestBB && "Finalization block should have one successor!");
+
+  // erase and replace with cleanup branch.
+  IPBB->getTerminator()->eraseFromParent();
+  CGF.Builder.SetInsertPoint(IPBB);
+  CodeGenFunction::JumpDest Dest = CGF.getJumpDestInCurrentScope(DestBB);
+  CGF.EmitBranchThroughCleanup(Dest);
+}
+
+/// Emit the body of an OMP region
+/// \param CGF	The Codegen function this belongs to
+/// \param RegionBodyStmt	The body statement for the OpenMP region being
+/// 			 generated
+/// \param CodeGenIP	Insertion point for generating the body code.
+/// \param FiniBB	The finalization basic block
+static void EmitOMPRegionBody(CodeGenFunction ,
+  const Stmt *RegionBodyStmt,
+  InsertPointTy CodeGenIP,
+  llvm::BasicBlock ) {
+  llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+  if (llvm::Instruction *CodeGenIPBBTI = CodeGenIPBB->getTerminator())
+CodeGenIPBBTI->eraseFromParent();
+
+  CGF.Builder.SetInsertPoint(CodeGenIPBB);
+
+  CGF.EmitStmt(RegionBodyStmt);
+
+  if (CGF.Builder.saveIP().isSet())
+CGF.Builder.CreateBr();
+}
+
+/// RAII for preserving necessary info during Outlined region body codegen.
+class OutlinedRegionBodyRAII {
+
+  llvm::AssertingVH OldAllocaIP;
+  CodeGenFunction::JumpDest OldReturnBlock;
+  CodeGenFunction 
+
+public:
+  OutlinedRegionBodyRAII(CodeGenFunction , InsertPointTy ,
+ llvm::BasicBlock )
+  : CGF(cgf) {
+assert(AllocaIP.isSet() &&
+   "Must specify Insertion point for allocas of outlined function");
+OldAllocaIP = CGF.AllocaInsertPt;
+CGF.AllocaInsertPt = &*AllocaIP.getPoint();
+
+OldReturnBlock = CGF.ReturnBlock;
+CGF.ReturnBlock = CGF.getJumpDestInCurrentScope();
+  }
+
+  ~OutlinedRegionBodyRAII() {
+CGF.AllocaInsertPt = OldAllocaIP;
+CGF.ReturnBlock = OldReturnBlock;
+  }
+};
+
+/// RAII for preserving necessary info during inlined region body codegen.
+class InlinedRegionBodyRAII {
+
+  llvm::AssertingVH OldAllocaIP;
+  CodeGenFunction 
+
+public:
+  InlinedRegionBodyRAII(CodeGenFunction , InsertPointTy ,
+llvm::BasicBlock )
+  : CGF(cgf) {
+// Alloca insertion block should be in the entry block of the containing
+// function so it expects an empty AllocaIP in which case will reuse the
+// old alloca insertion point, or a new AllocaIP in the 

[PATCH] D74562: [OpenMP][OMPIRBuilder] Introducing the `OMPBuilderCBHelpers` helper class

2020-02-18 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 245151.
fghanim added a comment.

addressing review comments - Adding a comment to explain minor change.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74562/new/

https://reviews.llvm.org/D74562

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CodeGenFunction.h
  clang/test/OpenMP/cancel_codegen.cpp

Index: clang/test/OpenMP/cancel_codegen.cpp
===
--- clang/test/OpenMP/cancel_codegen.cpp
+++ clang/test/OpenMP/cancel_codegen.cpp
@@ -192,9 +192,7 @@
 // IRBUILDER: [[CMP:%.+]] = icmp eq i32 [[RES]], 0
 // IRBUILDER: br i1 [[CMP]], label %[[CONTINUE:[^,].+]], label %[[EXIT:.+]]
 // IRBUILDER: [[EXIT]]
-// IRBUILDER: br label %[[EXIT2:.+]]
-// IRBUILDER: [[EXIT2]]
-// IRBUILDER: br label %[[RETURN]]
+// IRBUILDER: br label %[[RETURN:.+]]
 // IRBUILDER: [[CONTINUE]]
 // IRBUILDER: br label %[[ELSE:.+]]
 
Index: clang/lib/CodeGen/CodeGenFunction.h
===
--- clang/lib/CodeGen/CodeGenFunction.h
+++ clang/lib/CodeGen/CodeGenFunction.h
@@ -36,6 +36,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
 #include "llvm/IR/ValueHandle.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Transforms/Utils/SanitizerStats.h"
@@ -255,6 +256,114 @@
 unsigned Index;
   };
 
+  // Helper class for the OpenMP IR Builder. Allows reusability of code used for
+  // region body, and finalization codegen callbacks. This will class will also
+  // contain privatization functions used by the privatization call backs
+  struct OMPBuilderCBHelpers {
+
+using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
+
+/// Emit the Finalization for an OMP region
+/// \param CGF	The Codegen function this belongs to
+/// \param IP	Insertion point for generating the finalization code.
+static void FinalizeOMPRegion(CodeGenFunction , InsertPointTy IP) {
+  CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
+  assert(IP.getBlock()->end() != IP.getPoint() &&
+ "OpenMP IR Builder should cause terminated block!");
+
+  llvm::BasicBlock *IPBB = IP.getBlock();
+  llvm::BasicBlock *DestBB = IPBB->getUniqueSuccessor();
+  assert(DestBB && "Finalization block should have one successor!");
+
+  // erase and replace with cleanup branch.
+  IPBB->getTerminator()->eraseFromParent();
+  CGF.Builder.SetInsertPoint(IPBB);
+  CodeGenFunction::JumpDest Dest = CGF.getJumpDestInCurrentScope(DestBB);
+  CGF.EmitBranchThroughCleanup(Dest);
+}
+
+/// Emit the body of an OMP region
+/// \param CGF	The Codegen function this belongs to
+/// \param RegionBodyStmt	The body statement for the OpenMP region being
+/// 			 generated
+/// \param CodeGenIP	Insertion point for generating the body code.
+/// \param FiniBB	The finalization basic block
+static void EmitOMPRegionBody(CodeGenFunction ,
+  const Stmt *RegionBodyStmt,
+  InsertPointTy CodeGenIP,
+  llvm::BasicBlock ) {
+  llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+  if (llvm::Instruction *CodeGenIPBBTI = CodeGenIPBB->getTerminator())
+CodeGenIPBBTI->eraseFromParent();
+
+  CGF.Builder.SetInsertPoint(CodeGenIPBB);
+
+  CGF.EmitStmt(RegionBodyStmt);
+
+  if (CGF.Builder.saveIP().isSet())
+CGF.Builder.CreateBr();
+}
+
+/// RAII for preserving necessary info during Outlined region body codegen.
+class OutlinedRegionBodyRAII {
+
+  llvm::AssertingVH OldAllocaIP;
+  CodeGenFunction::JumpDest OldReturnBlock;
+  CodeGenFunction 
+
+public:
+  OutlinedRegionBodyRAII(CodeGenFunction , InsertPointTy ,
+ llvm::BasicBlock )
+  : CGF(cgf) {
+assert(AllocaIP.isSet() &&
+   "Must specify Insertion point for allocas of outlined function");
+OldAllocaIP = CGF.AllocaInsertPt;
+CGF.AllocaInsertPt = &*AllocaIP.getPoint();
+
+OldReturnBlock = CGF.ReturnBlock;
+CGF.ReturnBlock = CGF.getJumpDestInCurrentScope();
+  }
+
+  ~OutlinedRegionBodyRAII() {
+CGF.AllocaInsertPt = OldAllocaIP;
+CGF.ReturnBlock = OldReturnBlock;
+  }
+};
+
+/// RAII for preserving necessary info during inlined region body codegen.
+class InlinedRegionBodyRAII {
+
+  llvm::AssertingVH OldAllocaIP;
+  CodeGenFunction 
+
+public:
+  InlinedRegionBodyRAII(CodeGenFunction , InsertPointTy ,
+llvm::BasicBlock )
+  : CGF(cgf) {
+// Alloca insertion block should be in the entry block of the containing
+// function so it expects an empty AllocaIP in which case will reuse the
+// old 

[PATCH] D74562: [OpenMP][OMPIRBuilder] Introducing the `OMPBuilderCBHelpers` helper class

2020-02-17 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

Regarding the third comment (which was removed for some reason), This new 
update should fix that




Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:3134
 
 // TODO: Replace with a generic helper function for emitting body
 auto BodyGenCB = [MasterRegionBodyStmt, this](InsertPointTy AllocaIP,

jdoerfert wrote:
> These TODOs are now obsolete ;)
We need to split our patches to keep them small :p



Comment at: clang/lib/CodeGen/CodeGenFunction.h:354
+
+FinilizationBlock = CGF.getJumpDestInCurrentScope();
+  }

jdoerfert wrote:
> Don't we have to set/reset the `CGF.ReturnBlock` ? If not, why do we need 
> `FinilizationBlock` here?
Yes, and we still do for outlined regions (check `OutlinedRegionBodyRAII` ctor 
above). 

This is for inlined regions, and those shouldn't change the `CGF.ReturnBlock`. 
It should re-use the `CGF.ReturnBlock` for the enclosing outlined region.
The only reason I kept it, is because `CGF.getJumpDestInCurrentScope()` was 
updating a counter that seemed to me to relate to `EHStack`, and I didn't want 
to remove it without investigating first.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74562/new/

https://reviews.llvm.org/D74562



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D74562: [OpenMP][OMPIRBuilder] Introducing the `OMPBuilderCBHelpers` helper class

2020-02-17 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 245014.
fghanim marked 2 inline comments as done.
fghanim added a comment.

Fixed bug where variables where still being allocated in original function 
entry block


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74562/new/

https://reviews.llvm.org/D74562

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CodeGenFunction.h
  clang/test/OpenMP/cancel_codegen.cpp

Index: clang/test/OpenMP/cancel_codegen.cpp
===
--- clang/test/OpenMP/cancel_codegen.cpp
+++ clang/test/OpenMP/cancel_codegen.cpp
@@ -192,9 +192,7 @@
 // IRBUILDER: [[CMP:%.+]] = icmp eq i32 [[RES]], 0
 // IRBUILDER: br i1 [[CMP]], label %[[CONTINUE:[^,].+]], label %[[EXIT:.+]]
 // IRBUILDER: [[EXIT]]
-// IRBUILDER: br label %[[EXIT2:.+]]
-// IRBUILDER: [[EXIT2]]
-// IRBUILDER: br label %[[RETURN]]
+// IRBUILDER: br label %[[RETURN:.+]]
 // IRBUILDER: [[CONTINUE]]
 // IRBUILDER: br label %[[ELSE:.+]]
 
Index: clang/lib/CodeGen/CodeGenFunction.h
===
--- clang/lib/CodeGen/CodeGenFunction.h
+++ clang/lib/CodeGen/CodeGenFunction.h
@@ -36,6 +36,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
 #include "llvm/IR/ValueHandle.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Transforms/Utils/SanitizerStats.h"
@@ -255,6 +256,110 @@
 unsigned Index;
   };
 
+  // Helper class for the OpenMP IR Builder. Allows reusability of code used for
+  // region body, and finalization codegen callbacks. This will class will also
+  // contain privatization functions used by the privatization call backs
+  struct OMPBuilderCBHelpers {
+
+using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
+
+/// Emit the Finalization for an OMP region
+/// \param CGF	The Codegen function this belongs to
+/// \param IP	Insertion point for generating the finalization code.
+static void FinalizeOMPRegion(CodeGenFunction , InsertPointTy IP) {
+  CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
+  assert(IP.getBlock()->end() != IP.getPoint() &&
+ "OpenMP IR Builder should cause terminated block!");
+
+  llvm::BasicBlock *IPBB = IP.getBlock();
+  llvm::BasicBlock *DestBB = IPBB->getUniqueSuccessor();
+  assert(DestBB && "Finalization block should have one successor!");
+
+  // erase and replace with cleanup branch.
+  IPBB->getTerminator()->eraseFromParent();
+  CGF.Builder.SetInsertPoint(IPBB);
+  CodeGenFunction::JumpDest Dest = CGF.getJumpDestInCurrentScope(DestBB);
+  CGF.EmitBranchThroughCleanup(Dest);
+}
+
+/// Emit the body of an OMP region
+/// \param CGF	The Codegen function this belongs to
+/// \param RegionBodyStmt	The body statement for the OpenMP region being
+/// 			 generated
+/// \param CodeGenIP	Insertion point for generating the body code.
+/// \param FiniBB	The finalization basic block
+static void EmitOMPRegionBody(CodeGenFunction ,
+  const Stmt *RegionBodyStmt,
+  InsertPointTy CodeGenIP,
+  llvm::BasicBlock ) {
+  llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+  if (llvm::Instruction *CodeGenIPBBTI = CodeGenIPBB->getTerminator())
+CodeGenIPBBTI->eraseFromParent();
+
+  CGF.Builder.SetInsertPoint(CodeGenIPBB);
+
+  CGF.EmitStmt(RegionBodyStmt);
+
+  if (CGF.Builder.saveIP().isSet())
+CGF.Builder.CreateBr();
+}
+
+/// RAII for preserving necessary info during Outlined region body codegen.
+class OutlinedRegionBodyRAII {
+
+  llvm::AssertingVH OldAllocaIP;
+  CodeGenFunction::JumpDest OldReturnBlock;
+  CodeGenFunction 
+
+public:
+  OutlinedRegionBodyRAII(CodeGenFunction , InsertPointTy ,
+ llvm::BasicBlock )
+  : CGF(cgf) {
+assert(AllocaIP.isSet() &&
+   "Must specify Insertion point for allocas of outlined function");
+OldAllocaIP = CGF.AllocaInsertPt;
+CGF.AllocaInsertPt = &*AllocaIP.getPoint();
+
+OldReturnBlock = CGF.ReturnBlock;
+CGF.ReturnBlock = CGF.getJumpDestInCurrentScope();
+  }
+
+  ~OutlinedRegionBodyRAII() {
+CGF.AllocaInsertPt = OldAllocaIP;
+CGF.ReturnBlock = OldReturnBlock;
+  }
+};
+
+/// RAII for preserving necessary info during inlined region body codegen.
+class InlinedRegionBodyRAII {
+
+  llvm::AssertingVH OldAllocaIP;
+  CodeGenFunction::JumpDest FinilizationBlock;
+  CodeGenFunction 
+
+public:
+  InlinedRegionBodyRAII(CodeGenFunction , InsertPointTy ,
+llvm::BasicBlock )
+  : CGF(cgf) {
+// Alloca insertion block should be in the entry block of the 

[PATCH] D74562: [OpenMP][OMPIRBuilder] Introducing the `OMPBuilderCBHelpers` helper class

2020-02-13 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim created this revision.
fghanim added a reviewer: jdoerfert.
Herald added subscribers: cfe-commits, guansong.
Herald added a project: clang.

This patch introduces a new helper class `OMPBuilderCBHelpers`, 
which will contain all reusable C/C++ language specific function-
alities required by the `OMPIRBuilder`.

Initially, this helper class contains the body and finalization
codegen functionalities implemented using callbacks which were
moved here for reusability among the different directives
implemented in the `OMPIRBuilder`, along with RAIIs for preserving
state prior to emitting outlined and/or inlined OpenMP regions.

In the future this helper class will also contain all the different
call backs required by OpenMP clauses/variable privatization.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D74562

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/lib/CodeGen/CodeGenFunction.h
  clang/test/OpenMP/cancel_codegen.cpp
  clang/test/OpenMP/parallel_codegen.cpp

Index: clang/test/OpenMP/parallel_codegen.cpp
===
--- clang/test/OpenMP/parallel_codegen.cpp
+++ clang/test/OpenMP/parallel_codegen.cpp
@@ -112,7 +112,7 @@
 // ALL:   define linkonce_odr {{[a-z\_\b]*[ ]?i32}} [[TMAIN]](i8** %argc)
 // ALL:   store i8** %argc, i8*** [[ARGC_ADDR:%.+]],
 // CHECK:   call {{.*}}void (%struct.ident_t*, i32, void (i32*, i32*, ...)*, ...) @__kmpc_fork_call(%struct.ident_t* [[DEF_LOC_2]], i32 2, void (i32*, i32*, ...)* bitcast (void (i32*, i32*, i8***, i{{64|32}})* [[OMP_OUTLINED:@.+]] to void (i32*, i32*, ...)*), i8*** [[ARGC_ADDR]], i{{64|32}} %{{.+}})
-// IRBUILDER:   call {{.*}}void (%struct.ident_t*, i32, void (i32*, i32*, ...)*, ...) @__kmpc_fork_call(%struct.ident_t* [[DEF_LOC_2]], i32 2, void (i32*, i32*, ...)* bitcast (void (i32*, i32*, i8***, i{{64|32}})* [[OMP_OUTLINED:@.+]] to void (i32*, i32*, ...)*), i8*** [[ARGC_ADDR]], i{{64|32}} %{{.+}})
+// IRBUILDER:   call {{.*}}void (%struct.ident_t*, i32, void (i32*, i32*, ...)*, ...) @__kmpc_fork_call(%struct.ident_t* [[DEF_LOC_2]], i32 3, void (i32*, i32*, ...)* bitcast (void (i32*, i32*, i8***, double**, i{{64|32}})* [[OMP_OUTLINED:@.+]] to void (i32*, i32*, ...)*), i8*** [[ARGC_ADDR]], double** [[VAR:%.+]], i{{64|32}} %{{.+}})
 // ALL:  ret i32 0
 // ALL-NEXT:  }
 // ALL-DEBUG:   define linkonce_odr i32 [[TMAIN]](i8** %argc)
@@ -124,12 +124,12 @@
 // CHECK-DEBUG:  [[KMPC_LOC_PSOURCE_REF:%.+]] = getelementptr inbounds %struct.ident_t, %struct.ident_t* [[LOC_2_ADDR]], i32 0, i32 4
 // CHECK-DEBUG-NEXT:  store i8* getelementptr inbounds ([{{.+}} x i8], [{{.+}} x i8]* [[LOC2]], i32 0, i32 0), i8** [[KMPC_LOC_PSOURCE_REF]]
 // CHECK-DEBUG-NEXT:  call void (%struct.ident_t*, i32, void (i32*, i32*, ...)*, ...) @__kmpc_fork_call(%struct.ident_t* [[LOC_2_ADDR]], i32 2, void (i32*, i32*, ...)* bitcast (void (i32*, i32*, i8***, i64)* [[OMP_OUTLINED:@.+]] to void (i32*, i32*, ...)*), i8*** [[ARGC_ADDR]], i64 %{{.+}})
-// IRBUILDER-DEBUG:   call void (%struct.ident_t*, i32, void (i32*, i32*, ...)*, ...) @__kmpc_fork_call(%struct.ident_t* @{{.*}}, i32 2, void (i32*, i32*, ...)* bitcast (void (i32*, i32*, i8***, i64)* [[OMP_OUTLINED:@.+]] to void (i32*, i32*, ...)*), i8*** [[ARGC_ADDR]], i64 %{{.+}})
+// IRBUILDER-DEBUG:   call void (%struct.ident_t*, i32, void (i32*, i32*, ...)*, ...) @__kmpc_fork_call(%struct.ident_t* @{{.*}}, i32 3, void (i32*, i32*, ...)* bitcast (void (i32*, i32*, i8***, double**, i64)* [[OMP_OUTLINED:@.+]] to void (i32*, i32*, ...)*), i8*** [[ARGC_ADDR]], double** [[VAR:%.+]], i64 %{{.+}})
 // ALL-DEBUG:  ret i32 0
 // ALL-DEBUG-NEXT:  }
 
 // CHECK:   define internal {{.*}}void [[OMP_OUTLINED]](i32* noalias %.global_tid., i32* noalias %.bound_tid., i8*** dereferenceable({{4|8}}) %argc, i{{64|32}}{{.*}} %{{.+}})
-// IRBUILDER:   define internal {{.*}}void [[OMP_OUTLINED]](i32* noalias %{{.*}}, i32* noalias %{{.*}}, i8*** [[ARGC_REF:%.*]], i{{64|32}}{{.*}} %{{.+}})
+// IRBUILDER:   define internal {{.*}}void [[OMP_OUTLINED]](i32* noalias %{{.*}}, i32* noalias %{{.*}}, i8*** [[ARGC_REF:%.*]], double** [[VAR]], i{{64|32}}{{.*}} %{{.+}})
 // CHECK:   store i8*** %argc, i8 [[ARGC_PTR_ADDR:%.+]],
 // CHECK:   [[ARGC_REF:%.+]] = load i8***, i8 [[ARGC_PTR_ADDR]]
 // ALL: [[ARGC:%.+]] = load i8**, i8*** [[ARGC_REF]]
@@ -140,7 +140,7 @@
 // CHECK-NEXT:  unreachable
 // CHECK-NEXT:  }
 // CHECK-DEBUG:   define internal void [[OMP_OUTLINED_DEBUG:@.+]](i32* noalias %.global_tid., i32* noalias %.bound_tid., i8*** dereferenceable({{4|8}}) %argc, i64 %{{.+}})
-// IRBUILDER-DEBUG:   define internal void [[OMP_OUTLINED_DEBUG:@.+]](i32* noalias %{{.*}}, i32* noalias %{{.*}}, i8*** [[ARGC_REF:%.*]], i64 %{{.+}})
+// IRBUILDER-DEBUG:   define internal void [[OMP_OUTLINED_DEBUG:@.+]](i32* noalias %{{.*}}, i32* noalias %{{.*}}, i8*** [[ARGC_REF:%.*]], double** [[VAR]], i64 %{{.+}})
 // CHECK-DEBUG:   store i8*** %argc, i8 

[PATCH] D72304: [OpenMP][OMPIRBuilder] Add Directives (master and critical) to OMPBuilder.

2020-02-12 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 244249.
fghanim added a comment.

Remove unnecessary preservation of builder insertion point in 
`emitCommonDirectiveExit`


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72304/new/

https://reviews.llvm.org/D72304

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/test/OpenMP/critical_codegen.cpp
  clang/test/OpenMP/master_codegen.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPConstants.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
  llvm/lib/Frontend/OpenMP/OMPConstants.cpp
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -613,4 +613,161 @@
   }
 }
 
+TEST_F(OpenMPIRBuilderTest, MasterDirective) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = nullptr;
+
+  BasicBlock *EntryBB = nullptr;
+  BasicBlock *ExitBB = nullptr;
+  BasicBlock *ThenBB = nullptr;
+
+  auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+   BasicBlock ) {
+if (AllocaIP.isSet())
+  Builder.restoreIP(AllocaIP);
+else
+  Builder.SetInsertPoint(&*(F->getEntryBlock().getFirstInsertionPt()));
+PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+Builder.CreateStore(F->arg_begin(), PrivAI);
+
+llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+llvm::Instruction *CodeGenIPInst = &*CodeGenIP.getPoint();
+EXPECT_EQ(CodeGenIPBB->getTerminator(), CodeGenIPInst);
+
+Builder.restoreIP(CodeGenIP);
+
+// collect some info for checks later
+ExitBB = FiniBB.getUniqueSuccessor();
+ThenBB = Builder.GetInsertBlock();
+EntryBB = ThenBB->getUniquePredecessor();
+
+// simple instructions for body
+Value *PrivLoad = Builder.CreateLoad(PrivAI, "local.use");
+Builder.CreateICmpNE(F->arg_begin(), PrivLoad);
+  };
+
+  auto FiniCB = [&](InsertPointTy IP) {
+BasicBlock *IPBB = IP.getBlock();
+EXPECT_NE(IPBB->end(), IP.getPoint());
+  };
+
+  Builder.restoreIP(OMPBuilder.CreateMaster(Builder, BodyGenCB, FiniCB));
+  Value *EntryBBTI = EntryBB->getTerminator();
+  EXPECT_NE(EntryBBTI, nullptr);
+  EXPECT_TRUE(isa(EntryBBTI));
+  BranchInst *EntryBr = cast(EntryBB->getTerminator());
+  EXPECT_TRUE(EntryBr->isConditional());
+  EXPECT_EQ(EntryBr->getSuccessor(0), ThenBB);
+  EXPECT_EQ(ThenBB->getUniqueSuccessor(), ExitBB);
+  EXPECT_EQ(EntryBr->getSuccessor(1), ExitBB);
+
+  CmpInst *CondInst = cast(EntryBr->getCondition());
+  EXPECT_TRUE(isa(CondInst->getOperand(0)));
+
+  CallInst *MasterEntryCI = cast(CondInst->getOperand(0));
+  EXPECT_EQ(MasterEntryCI->getNumArgOperands(), 2U);
+  EXPECT_EQ(MasterEntryCI->getCalledFunction()->getName(), "__kmpc_master");
+  EXPECT_TRUE(isa(MasterEntryCI->getArgOperand(0)));
+
+  CallInst *MasterEndCI = nullptr;
+  for (auto  : *ThenBB) {
+Instruction *cur = 
+if (isa(cur)) {
+  MasterEndCI = cast(cur);
+  if (MasterEndCI->getCalledFunction()->getName() == "__kmpc_end_master")
+break;
+  MasterEndCI = nullptr;
+}
+  }
+  EXPECT_NE(MasterEndCI, nullptr);
+  EXPECT_EQ(MasterEndCI->getNumArgOperands(), 2U);
+  EXPECT_TRUE(isa(MasterEndCI->getArgOperand(0)));
+  EXPECT_EQ(MasterEndCI->getArgOperand(1), MasterEntryCI->getArgOperand(1));
+}
+
+TEST_F(OpenMPIRBuilderTest, CriticalDirective) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+
+  BasicBlock *EntryBB = nullptr;
+
+  auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+   BasicBlock ) {
+// collect some info for checks later
+EntryBB = FiniBB.getUniquePredecessor();
+
+// actual start for bodyCB
+llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+llvm::Instruction *CodeGenIPInst = &*CodeGenIP.getPoint();
+EXPECT_EQ(CodeGenIPBB->getTerminator(), CodeGenIPInst);
+EXPECT_EQ(EntryBB, CodeGenIPBB);
+
+// body begin
+Builder.restoreIP(CodeGenIP);
+Builder.CreateStore(F->arg_begin(), PrivAI);
+Value *PrivLoad = Builder.CreateLoad(PrivAI, "local.use");
+Builder.CreateICmpNE(F->arg_begin(), PrivLoad);
+  };
+
+  auto FiniCB = [&](InsertPointTy IP) {
+BasicBlock *IPBB = IP.getBlock();
+EXPECT_NE(IPBB->end(), IP.getPoint());
+  };
+
+  

[PATCH] D74372: [OpenMP][IRBuilder] Perform finalization (incl. outlining) late

2020-02-11 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added inline comments.



Comment at: clang/lib/CodeGen/CodeGenFunction.cpp:113
+  if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder())
+OMPBuilder->finalize();
 }

jdoerfert wrote:
> fghanim wrote:
> > jdoerfert wrote:
> > > fghanim wrote:
> > > > Does this mean that `finalize()` is being called twice everytime, here 
> > > > and `~OpenMPIRBuilder()`?
> > > > if yes, is there a reason why you want that?
> > > It's called twice now, not that it matters. The basic rule is, call it at 
> > > least once before you finalize the module. I'll remove the destructor for 
> > > now to make it explicit only.
> > Right now it doesn't matter, I agree. However, if later `finalize()` is 
> > modified for any reason, I worry this may introduce problems 
> > unintentionally.
> > 
> > In any case, if you decide to keep only one of them, I prefer to keep the 
> > one in `~OpenMPIRBuilder()`. This way the `OMPBuilder` is self contained, 
> > so regardless of the frontend using it, we can be certain it's always 
> > called at the end. You can indicate explicitly in the description comment 
> > for `finalize()` that it is being called in the destructor, if you want.
> That doesn't work, otherwise I would not call finalize here. At least it 
> doesn't work without extra changes. As it is right now, the OMPBuilder in 
> CodeGenModule is deleted *after* the IR is generated and emitted, so any 
> changes at destructor time are too late.
Got it. Then a minor suggestion is to just put a note in `finalize()` 
description about whatever you decide to do (i.e. keep both, remove the one in 
destructor, or something else entirely). This way, whoever is using/modifying 
the code is aware of it.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74372/new/

https://reviews.llvm.org/D74372



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D74372: [OpenMP][IRBuilder] Perform finalization (incl. outlining) late

2020-02-11 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added inline comments.



Comment at: clang/lib/CodeGen/CodeGenFunction.cpp:113
+  if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder())
+OMPBuilder->finalize();
 }

jdoerfert wrote:
> fghanim wrote:
> > Does this mean that `finalize()` is being called twice everytime, here and 
> > `~OpenMPIRBuilder()`?
> > if yes, is there a reason why you want that?
> It's called twice now, not that it matters. The basic rule is, call it at 
> least once before you finalize the module. I'll remove the destructor for now 
> to make it explicit only.
Right now it doesn't matter, I agree. However, if later `finalize()` is 
modified for any reason, I worry this may introduce problems unintentionally.

In any case, if you decide to keep only one of them, I prefer to keep the one 
in `~OpenMPIRBuilder()`. This way the `OMPBuilder` is self contained, so 
regardless of the frontend using it, we can be certain it's always called at 
the end. You can indicate explicitly in the description comment for 
`finalize()` that it is being called in the destructor, if you want.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74372/new/

https://reviews.llvm.org/D74372



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D74372: [OpenMP][IRBuilder] Perform finalization (incl. outlining) late

2020-02-11 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added inline comments.



Comment at: clang/lib/CodeGen/CodeGenFunction.cpp:113
+  if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder())
+OMPBuilder->finalize();
 }

Does this mean that `finalize()` is being called twice everytime, here and 
`~OpenMPIRBuilder()`?
if yes, is there a reason why you want that?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74372/new/

https://reviews.llvm.org/D74372



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D72304: [OpenMP][OMPIRBuilder] Add Directives (master and critical) to OMPBuilder.

2020-02-11 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 243839.
fghanim added a comment.

fixing unused variable.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72304/new/

https://reviews.llvm.org/D72304

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/test/OpenMP/critical_codegen.cpp
  clang/test/OpenMP/master_codegen.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPConstants.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
  llvm/lib/Frontend/OpenMP/OMPConstants.cpp
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -613,4 +613,161 @@
   }
 }
 
+TEST_F(OpenMPIRBuilderTest, MasterDirective) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = nullptr;
+
+  BasicBlock *EntryBB = nullptr;
+  BasicBlock *ExitBB = nullptr;
+  BasicBlock *ThenBB = nullptr;
+
+  auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+   BasicBlock ) {
+if (AllocaIP.isSet())
+  Builder.restoreIP(AllocaIP);
+else
+  Builder.SetInsertPoint(&*(F->getEntryBlock().getFirstInsertionPt()));
+PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+Builder.CreateStore(F->arg_begin(), PrivAI);
+
+llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+llvm::Instruction *CodeGenIPInst = &*CodeGenIP.getPoint();
+EXPECT_EQ(CodeGenIPBB->getTerminator(), CodeGenIPInst);
+
+Builder.restoreIP(CodeGenIP);
+
+// collect some info for checks later
+ExitBB = FiniBB.getUniqueSuccessor();
+ThenBB = Builder.GetInsertBlock();
+EntryBB = ThenBB->getUniquePredecessor();
+
+// simple instructions for body
+Value *PrivLoad = Builder.CreateLoad(PrivAI, "local.use");
+Builder.CreateICmpNE(F->arg_begin(), PrivLoad);
+  };
+
+  auto FiniCB = [&](InsertPointTy IP) {
+BasicBlock *IPBB = IP.getBlock();
+EXPECT_NE(IPBB->end(), IP.getPoint());
+  };
+
+  Builder.restoreIP(OMPBuilder.CreateMaster(Builder, BodyGenCB, FiniCB));
+  Value *EntryBBTI = EntryBB->getTerminator();
+  EXPECT_NE(EntryBBTI, nullptr);
+  EXPECT_TRUE(isa(EntryBBTI));
+  BranchInst *EntryBr = cast(EntryBB->getTerminator());
+  EXPECT_TRUE(EntryBr->isConditional());
+  EXPECT_EQ(EntryBr->getSuccessor(0), ThenBB);
+  EXPECT_EQ(ThenBB->getUniqueSuccessor(), ExitBB);
+  EXPECT_EQ(EntryBr->getSuccessor(1), ExitBB);
+
+  CmpInst *CondInst = cast(EntryBr->getCondition());
+  EXPECT_TRUE(isa(CondInst->getOperand(0)));
+
+  CallInst *MasterEntryCI = cast(CondInst->getOperand(0));
+  EXPECT_EQ(MasterEntryCI->getNumArgOperands(), 2U);
+  EXPECT_EQ(MasterEntryCI->getCalledFunction()->getName(), "__kmpc_master");
+  EXPECT_TRUE(isa(MasterEntryCI->getArgOperand(0)));
+
+  CallInst *MasterEndCI = nullptr;
+  for (auto  : *ThenBB) {
+Instruction *cur = 
+if (isa(cur)) {
+  MasterEndCI = cast(cur);
+  if (MasterEndCI->getCalledFunction()->getName() == "__kmpc_end_master")
+break;
+  MasterEndCI = nullptr;
+}
+  }
+  EXPECT_NE(MasterEndCI, nullptr);
+  EXPECT_EQ(MasterEndCI->getNumArgOperands(), 2U);
+  EXPECT_TRUE(isa(MasterEndCI->getArgOperand(0)));
+  EXPECT_EQ(MasterEndCI->getArgOperand(1), MasterEntryCI->getArgOperand(1));
+}
+
+TEST_F(OpenMPIRBuilderTest, CriticalDirective) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+
+  BasicBlock *EntryBB = nullptr;
+
+  auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+   BasicBlock ) {
+// collect some info for checks later
+EntryBB = FiniBB.getUniquePredecessor();
+
+// actual start for bodyCB
+llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+llvm::Instruction *CodeGenIPInst = &*CodeGenIP.getPoint();
+EXPECT_EQ(CodeGenIPBB->getTerminator(), CodeGenIPInst);
+EXPECT_EQ(EntryBB, CodeGenIPBB);
+
+// body begin
+Builder.restoreIP(CodeGenIP);
+Builder.CreateStore(F->arg_begin(), PrivAI);
+Value *PrivLoad = Builder.CreateLoad(PrivAI, "local.use");
+Builder.CreateICmpNE(F->arg_begin(), PrivLoad);
+  };
+
+  auto FiniCB = [&](InsertPointTy IP) {
+BasicBlock *IPBB = IP.getBlock();
+EXPECT_NE(IPBB->end(), IP.getPoint());
+  };
+
+  Builder.restoreIP(OMPBuilder.CreateCritical(Builder, BodyGenCB, FiniCB,
+  

[PATCH] D72304: [OpenMP][OMPIRBuilder] Add Directives (master and critical) to OMPBuilder.

2020-02-08 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

ping


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72304/new/

https://reviews.llvm.org/D72304



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D72304: [OpenMP][OMPIRBuilder] Add Directives (master and critical) to OMPBuilder.

2020-02-03 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 242144.
fghanim added a comment.

Addressing errors/warnings from commit build


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72304/new/

https://reviews.llvm.org/D72304

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/test/OpenMP/critical_codegen.cpp
  clang/test/OpenMP/master_codegen.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPConstants.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
  llvm/lib/Frontend/OpenMP/OMPConstants.cpp
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -613,4 +613,161 @@
   }
 }
 
+TEST_F(OpenMPIRBuilderTest, MasterDirective) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = nullptr;
+
+  BasicBlock *EntryBB = nullptr;
+  BasicBlock *ExitBB = nullptr;
+  BasicBlock *ThenBB = nullptr;
+
+  auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+   BasicBlock ) {
+if (AllocaIP.isSet())
+  Builder.restoreIP(AllocaIP);
+else
+  Builder.SetInsertPoint(&*(F->getEntryBlock().getFirstInsertionPt()));
+PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+Builder.CreateStore(F->arg_begin(), PrivAI);
+
+llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+llvm::Instruction *CodeGenIPInst = &*CodeGenIP.getPoint();
+EXPECT_EQ(CodeGenIPBB->getTerminator(), CodeGenIPInst);
+
+Builder.restoreIP(CodeGenIP);
+
+// collect some info for checks later
+ExitBB = FiniBB.getUniqueSuccessor();
+ThenBB = Builder.GetInsertBlock();
+EntryBB = ThenBB->getUniquePredecessor();
+
+// simple instructions for body
+Value *PrivLoad = Builder.CreateLoad(PrivAI, "local.use");
+Builder.CreateICmpNE(F->arg_begin(), PrivLoad);
+  };
+
+  auto FiniCB = [&](InsertPointTy IP) {
+BasicBlock *IPBB = IP.getBlock();
+EXPECT_NE(IPBB->end(), IP.getPoint());
+  };
+
+  Builder.restoreIP(OMPBuilder.CreateMaster(Builder, BodyGenCB, FiniCB));
+  Value *EntryBBTI = EntryBB->getTerminator();
+  EXPECT_NE(EntryBBTI, nullptr);
+  EXPECT_TRUE(isa(EntryBBTI));
+  BranchInst *EntryBr = cast(EntryBB->getTerminator());
+  EXPECT_TRUE(EntryBr->isConditional());
+  EXPECT_EQ(EntryBr->getSuccessor(0), ThenBB);
+  EXPECT_EQ(ThenBB->getUniqueSuccessor(), ExitBB);
+  EXPECT_EQ(EntryBr->getSuccessor(1), ExitBB);
+
+  CmpInst *CondInst = cast(EntryBr->getCondition());
+  EXPECT_TRUE(isa(CondInst->getOperand(0)));
+
+  CallInst *MasterEntryCI = cast(CondInst->getOperand(0));
+  EXPECT_EQ(MasterEntryCI->getNumArgOperands(), 2U);
+  EXPECT_EQ(MasterEntryCI->getCalledFunction()->getName(), "__kmpc_master");
+  EXPECT_TRUE(isa(MasterEntryCI->getArgOperand(0)));
+
+  CallInst *MasterEndCI = nullptr;
+  for (auto  : *ThenBB) {
+Instruction *cur = 
+if (isa(cur)) {
+  MasterEndCI = cast(cur);
+  if (MasterEndCI->getCalledFunction()->getName() == "__kmpc_end_master")
+break;
+  MasterEndCI = nullptr;
+}
+  }
+  EXPECT_NE(MasterEndCI, nullptr);
+  EXPECT_EQ(MasterEndCI->getNumArgOperands(), 2U);
+  EXPECT_TRUE(isa(MasterEndCI->getArgOperand(0)));
+  EXPECT_EQ(MasterEndCI->getArgOperand(1), MasterEntryCI->getArgOperand(1));
+}
+
+TEST_F(OpenMPIRBuilderTest, CriticalDirective) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+
+  BasicBlock *EntryBB = nullptr;
+
+  auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+   BasicBlock ) {
+// collect some info for checks later
+EntryBB = FiniBB.getUniquePredecessor();
+
+// actual start for bodyCB
+llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+llvm::Instruction *CodeGenIPInst = &*CodeGenIP.getPoint();
+EXPECT_EQ(CodeGenIPBB->getTerminator(), CodeGenIPInst);
+EXPECT_EQ(EntryBB, CodeGenIPBB);
+
+// body begin
+Builder.restoreIP(CodeGenIP);
+Builder.CreateStore(F->arg_begin(), PrivAI);
+Value *PrivLoad = Builder.CreateLoad(PrivAI, "local.use");
+Builder.CreateICmpNE(F->arg_begin(), PrivLoad);
+  };
+
+  auto FiniCB = [&](InsertPointTy IP) {
+BasicBlock *IPBB = IP.getBlock();
+EXPECT_NE(IPBB->end(), IP.getPoint());
+  };
+
+  Builder.restoreIP(OMPBuilder.CreateCritical(Builder, 

[PATCH] D72304: [OpenMP][OMPIRBuilder] Add Directives (master and critical) to OMPBuilder.

2020-02-03 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

ping


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72304/new/

https://reviews.llvm.org/D72304



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D72304: [OpenMP][OMPIRBuilder] Add Directives (master and critical) to OMPBuilder.

2020-01-29 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

Everything is fine!
I just cloned llvm from git hub, added this revision with `arc patch`. then:
`cmake -G"Unix Makefiles" -DLLVM_ENABLE_PROJECTS="clang;openmp" 
-DCMAKE_BUILD_TYPE=Release -DLLVM_OPTIMIZED_TABLEGEN=1 -DLLVM_USE_LINKER=gold 
../llvm-project/llvm`
`make` , `make check-llvm-unittest`, `make check-clang-openmp`

all of it works fine and I got no errors whatsoever. Let me know what you think.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72304/new/

https://reviews.llvm.org/D72304



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D73285: [OpenMP][OMPIRBuilder][BugFix] Handle Unreachable Finalization blocks in `parallel` generation

2020-01-29 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked an inline comment as done.
fghanim added inline comments.



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:661
+AfterIP = InsertPointTy(ForkBB, ForkBB->end());
+  }
 

jdoerfert wrote:
> fghanim wrote:
> > jdoerfert wrote:
> > > Why do we need all of this? Can't we just *not do it* instead? This is 
> > > code complexity that we should avoid.
> > Depends. 
> > If we want to conform with the way things are done in clang; namely, not 
> > have unreachable blocks, then yes we need to do this. If not, then no, 
> > nothing needs to change. An optimization pass will be executed at some 
> > point later that should clean all that up.
> > 
> > However, we should be careful, for example, The lit test for `critical` 
> > checks that no basic blocks were generated from the rest of the body that 
> > comes after the infinite loop. So if the choice is to not conform with 
> > clang, then we should keep an eye on these lit tests, and disable such 
> > checks for the OMPBuilder.
> > If we want to conform with the way things are done in clang;
> 
> It's not like we introduce much extra code, break anything, or make the final 
> result different.
> 
> 
> >  If not, then no, nothing needs to change. An optimization pass will be 
> > executed at some point later that should clean all that up.
> 
> Let's go with that solution and keep this code here simple, less error prone, 
> and easier to manage.
> 
> 
> > However, we should be careful, for example, The lit test for critical 
> > checks that no basic blocks were generated from the rest of the body that 
> > comes after the infinite loop. So if the choice is to not conform with 
> > clang, then we should keep an eye on these lit tests, and disable such 
> > checks for the OMPBuilder.
> 
> We already do things different and that will only become more evident 
> (TRegions!). For example, to simplify this code we do *not* cache runtime 
> calls (anymore). That is we emit a new get_thread_id call every time. (We 
> know the OpenMPOpt pass will clean it up eventually.) I get that the tests 
> change and for a while we will have clang and OMPBuilder check lines. Though, 
> once the clang CG is gone there is arguably no difference anymore because the 
> OMPBuilder behavior is then the default. As soon as we have the privatization 
> parts properly hooked up we can even start running the OMPBuilder by default 
> and soon after removing clang CG parts. If anything, we should modernize the 
> clang tests as they are a constant critique point that hinders outside 
> involvement. We could start using the llvm/utils/update__checks scripts 
> for example. We could also minimize the check lines and focus on the most 
> important bits only. (I prefer the update scripts with the pending 
> extensions, e.g., D69701)
> 
In that case, This revision is not necessary. The only fix needed is the branch 
erasure/creation change in the body CallBack (a bit more on this later), all 
the rest including the tests is not necessary. The only tests needed are 
already being done by the llvm verifier, which already reports if a BB is used 
by an orphan branch sticking around, or if a BB contains more than one 
terminator.

Regarding the issue of the branch, given that our finalization and body 
callbacks are very similar across different directives (Parallel, master, 
critical), the plan as we discussed on D72304 , is to write helper 
functions/class that we could use instead. So whoever, ends up writing that 
should make sure to include the branch changes, which makes them here redundant.

So, in the interest of everyone's time, my suggestion is to abandon this 
revision entirely for now, and just make sure that the implementation of these 
helper functions takes care of this everywhere.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D73285/new/

https://reviews.llvm.org/D73285



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D72304: [OpenMP][OMPIRBuilder] Add Directives (master and critical) to OMPBuilder.

2020-01-28 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

Thanks for trying.

It compiles fine with me. How can I reproduce the problems you're getting? is 
there a way to pack what you have and send it to me so I can have a look?

Also, a second question, how trustworthy is harbormaster when it says something 
is buildable?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72304/new/

https://reviews.llvm.org/D72304



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D73285: [OpenMP][OMPIRBuilder][BugFix] Handle Unreachable Finalization blocks in `parallel` generation

2020-01-28 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked an inline comment as done.
fghanim added inline comments.



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:661
+AfterIP = InsertPointTy(ForkBB, ForkBB->end());
+  }
 

jdoerfert wrote:
> Why do we need all of this? Can't we just *not do it* instead? This is code 
> complexity that we should avoid.
Depends. 
If we want to conform with the way things are done in clang; namely, not have 
unreachable blocks, then yes we need to do this. If not, then no, nothing needs 
to change. An optimization pass will be executed at some point later that 
should clean all that up.

However, we should be careful, for example, The lit test for `critical` checks 
that no basic blocks were generated from the rest of the body that comes after 
the infinite loop. So if the choice is to not conform with clang, then we 
should keep an eye on these lit tests, and disable such checks for the 
OMPBuilder.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D73285/new/

https://reviews.llvm.org/D73285



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D73285: [OpenMP][OMPIRBuilder][BugFix] Handle Unreachable Finalization blocks in `parallel` generation

2020-01-28 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 240949.
fghanim added a comment.

- Squashing all the commits


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D73285/new/

https://reviews.llvm.org/D73285

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/test/OpenMP/parallel_codegen.cpp
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -400,6 +400,78 @@
   EXPECT_EQ(ForkCI->getArgOperand(3), F->arg_begin());
 }
 
+TEST_F(OpenMPIRBuilderTest, ParallelEndless) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  unsigned NumBodiesGenerated = 0;
+  unsigned NumPrivatizedVars = 0;
+  unsigned NumFinalizationPoints = 0;
+
+  BasicBlock *OutlinedBodyBB = nullptr;
+  auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+   BasicBlock ) {
+++NumBodiesGenerated;
+
+auto *OldBB = OutlinedBodyBB = CodeGenIP.getBlock();
+
+// Create an endless loop.
+OldBB->getTerminator()->eraseFromParent();
+BranchInst::Create(OldBB, OldBB);
+
+Builder.ClearInsertionPoint();
+  };
+
+  auto PrivCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+Value , Value *) -> InsertPointTy {
+++NumPrivatizedVars;
+return CodeGenIP;
+  };
+
+  auto FiniCB = [&](InsertPointTy CodeGenIP) { ++NumFinalizationPoints; };
+
+  IRBuilder<>::InsertPoint AfterIP =
+  OMPBuilder.CreateParallel(Loc, BodyGenCB, PrivCB, FiniCB, nullptr,
+nullptr, OMP_PROC_BIND_default, false);
+
+  EXPECT_EQ(NumBodiesGenerated, 1U);
+  EXPECT_EQ(NumPrivatizedVars, 0U);
+  EXPECT_EQ(NumFinalizationPoints, 0U);
+
+  Builder.restoreIP(AfterIP);
+  Builder.CreateRetVoid();
+
+  ASSERT_NE(OutlinedBodyBB, nullptr);
+  Function *OutlinedFn = OutlinedBodyBB->getParent();
+  EXPECT_NE(F, OutlinedFn);
+  EXPECT_FALSE(verifyModule(*M));
+  EXPECT_TRUE(OutlinedFn->hasFnAttribute(Attribute::NoUnwind));
+  EXPECT_TRUE(OutlinedFn->hasFnAttribute(Attribute::NoRecurse));
+  EXPECT_TRUE(OutlinedFn->hasParamAttribute(0, Attribute::NoAlias));
+  EXPECT_TRUE(OutlinedFn->hasParamAttribute(1, Attribute::NoAlias));
+
+  EXPECT_TRUE(OutlinedFn->hasInternalLinkage());
+  EXPECT_EQ(OutlinedFn->arg_size(), 2U);
+
+  EXPECT_EQ(OutlinedFn->getNumUses(), 1U);
+  User *Usr = OutlinedFn->user_back();
+  ASSERT_TRUE(isa(Usr));
+  CallInst *ForkCI = dyn_cast(Usr->user_back());
+  ASSERT_NE(ForkCI, nullptr);
+
+  EXPECT_EQ(ForkCI->getCalledFunction()->getName(), "__kmpc_fork_call");
+  EXPECT_EQ(ForkCI->getNumArgOperands(), 3U);
+  EXPECT_TRUE(isa(ForkCI->getArgOperand(0)));
+  EXPECT_EQ(ForkCI->getArgOperand(1),
+ConstantInt::get(Type::getInt32Ty(Ctx), 0U));
+  EXPECT_EQ(ForkCI->getArgOperand(2), Usr);
+}
+
 TEST_F(OpenMPIRBuilderTest, ParallelIfCond) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -439,6 +439,18 @@
 Worklist.push_back(SuccBB);
   }
 
+  // If we didn't emit a branch to FiniBB during body generation, it means
+  // FiniBB is unreachable (e.g. while(1);). stop generating all the
+  // unreachable blocks, and remove anything we are not going to use.
+  // Check to see if PRegPreFiniBB is reachable from PRegionBodyBB.
+  bool FoundPreFiniBB = false;
+  for (auto BI : ParallelRegionBlocks) {
+if (BI == PRegPreFiniBB) {
+  FoundPreFiniBB = true;
+  break;
+}
+  }
+
   CodeExtractorAnalysisCache CEAC(*OuterFn);
   CodeExtractor Extractor(ParallelRegionBlocks, /* DominatorTree */ nullptr,
   /* AggregateArgs */ false,
@@ -564,7 +576,7 @@
 }
   }
 
-  Builder.CreateCall(RTLFn, RealArgs);
+  CallInst *ForkCall = Builder.CreateCall(RTLFn, RealArgs);
 
   LLVM_DEBUG(dbgs() << "With fork_call placed: "
 << *Builder.GetInsertBlock()->getParent() << "\n");
@@ -583,7 +595,6 @@
   if (!ElseTI) {
 CI->eraseFromParent();
   } else {
-
 // If an "if" clause was present we are now generating the serialized
 // version into the "else" branch.
 Builder.SetInsertPoint(ElseTI);
@@ -608,22 +619,46 @@
   << *Builder.GetInsertBlock()->getParent() << "\n");
   }
 
-  // Adjust the finalization stack, verify the adjustment, and call the
-  // finalize function a last time to finalize values between the pre-fini block
-  

[PATCH] D73285: [OpenMP][OMPIRBuilder][BugFix] Handle Unreachable Finalization blocks in `parallel` generation

2020-01-28 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 240886.
fghanim added a comment.

Adding lit test to clang for testing the fix


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D73285/new/

https://reviews.llvm.org/D73285

Files:
  clang/test/OpenMP/parallel_codegen.cpp


Index: clang/test/OpenMP/parallel_codegen.cpp
===
--- clang/test/OpenMP/parallel_codegen.cpp
+++ clang/test/OpenMP/parallel_codegen.cpp
@@ -21,11 +21,13 @@
 // CHECK-DEBUG-DAG: %struct.ident_t = type { i32, i32, i32, i32, i8* }
 // CHECK-DEBUG-DAG: [[STR:@.+]] = private unnamed_addr constant [23 x i8] 
c";unknown;unknown;0;0;;\00"
 // CHECK-DEBUG-DAG: [[DEF_LOC_2:@.+]] = private unnamed_addr global 
%struct.ident_t { i32 0, i32 2, i32 0, i32 0, i8* getelementptr inbounds ([23 x 
i8], [23 x i8]* [[STR]], i32 0, i32 0) }
-// CHECK-DEBUG-DAG: [[LOC1:@.+]] = private unnamed_addr constant [{{.+}} x i8] 
c";{{.*}}parallel_codegen.cpp;main;[[@LINE+22]];1;;\00"
-// CHECK-DEBUG-DAG: [[LOC2:@.+]] = private unnamed_addr constant [{{.+}} x i8] 
c";{{.*}}parallel_codegen.cpp;tmain;[[@LINE+11]];1;;\00"
+// CHECK-DEBUG-DAG: [[LOC1:@.+]] = private unnamed_addr constant [{{.+}} x i8] 
c";{{.*}}parallel_codegen.cpp;main;[[@LINE+29]];1;;\00"
+// CHECK-DEBUG-DAG: [[LOC2:@.+]] = private unnamed_addr constant [{{.+}} x i8] 
c";{{.*}}parallel_codegen.cpp;tmain;[[@LINE+13]];1;;\00"
+// CHECK-DEBUG-DAG: [[LOC3:@.+]] = private unnamed_addr constant [{{.+}} x i8] 
c";{{.*}}parallel_codegen.cpp;tmain;[[@LINE+19]];1;;\00"
 // IRBUILDER-DEBUG-DAG: %struct.ident_t = type { i32, i32, i32, i32, i8* }
-// IRBUILDER-DEBUG-DAG: [[LOC1:@.+]] = private unnamed_addr constant [{{.+}} x 
i8] c";{{.*}}parallel_codegen.cpp;main;[[@LINE+19]];0;;\00"
-// IRBUILDER-DEBUG-DAG: [[LOC2:@.+]] = private unnamed_addr constant [{{.+}} x 
i8] c";{{.*}}parallel_codegen.cpp;tmain;[[@LINE+8]];0;;\00"
+// IRBUILDER-DEBUG-DAG: [[LOC1:@.+]] = private unnamed_addr constant [{{.+}} x 
i8] c";{{.*}}parallel_codegen.cpp;main;[[@LINE+25]];0;;\00"
+// IRBUILDER-DEBUG-DAG: [[LOC2:@.+]] = private unnamed_addr constant [{{.+}} x 
i8] c";{{.*}}parallel_codegen.cpp;tmain;[[@LINE+9]];0;;\00"
+// IRBUILDER-DEBUG-DAG: [[LOC3:@.+]] = private unnamed_addr constant [{{.+}} x 
i8] c";{{.*}}parallel_codegen.cpp;tmain;[[@LINE+15]];0;;\00"
 
 template 
 void foo(T argc) {}
@@ -38,6 +40,11 @@
   foo(argc);
   chunk_t var;(void)var[0][0];
   }
+
+  if (argc[1])
+#pragma omp parallel
+   while(1);
+
   return 0;
 }
 
@@ -113,6 +120,8 @@
 // ALL:   store i8** %argc, i8*** [[ARGC_ADDR:%.+]],
 // CHECK:   call {{.*}}void (%struct.ident_t*, i32, void (i32*, i32*, 
...)*, ...) @__kmpc_fork_call(%struct.ident_t* [[DEF_LOC_2]], i32 2, void 
(i32*, i32*, ...)* bitcast (void (i32*, i32*, i8***, i{{64|32}})* 
[[OMP_OUTLINED:@.+]] to void (i32*, i32*, ...)*), i8*** [[ARGC_ADDR]], 
i{{64|32}} %{{.+}})
 // IRBUILDER:   call {{.*}}void (%struct.ident_t*, i32, void (i32*, i32*, 
...)*, ...) @__kmpc_fork_call(%struct.ident_t* [[DEF_LOC_2]], i32 2, void 
(i32*, i32*, ...)* bitcast (void (i32*, i32*, i8***, i{{64|32}})* 
[[OMP_OUTLINED:@.+]] to void (i32*, i32*, ...)*), i8*** [[ARGC_ADDR]], 
i{{64|32}} %{{.+}})
+// CHECK-DAG:  call {{.*}}void (%struct.ident_t*, i32, void 
(i32*, i32*, ...)*, ...) @__kmpc_fork_call(%struct.ident_t* [[DEF_LOC_2]], i32 
0, void (i32*, i32*, ...)* bitcast (void (i32*, i32*)* [[OMP_OUTLINED1:@.+]] to 
void (i32*, i32*, ...)*))
+// IRBUILDER-DAG:  call {{.*}}void (%struct.ident_t*, i32, void (i32*, 
i32*, ...)*, ...) @__kmpc_fork_call(%struct.ident_t* [[DEF_LOC_2]], i32 0, void 
(i32*, i32*, ...)* bitcast (void (i32*, i32*)* [[OMP_OUTLINED1:@.+]] to void 
(i32*, i32*, ...)*))
 // ALL:  ret i32 0
 // ALL-NEXT:  }
 // ALL-DEBUG:   define linkonce_odr i32 [[TMAIN]](i8** %argc)
@@ -128,6 +137,13 @@
 // ALL-DEBUG:  ret i32 0
 // ALL-DEBUG-NEXT:  }
 
+// IRBUILDER:   define internal {{.*}}void [[OMP_OUTLINED1]](i32* noalias 
%{{.*}}, i32* noalias %{{.*}})
+// IRBUILDER-SAME:  #[[FN_ATTRS:[0-9]+]]
+// IRBUILDER:  br label %while.body
+// IRBUILDER-NOT:  ret %{{.*}}
+// IRBUILDER:  br label %while.body
+// IRBUILDER-NOT:  ret %{{.*}}
+
 // CHECK:   define internal {{.*}}void [[OMP_OUTLINED]](i32* noalias 
%.global_tid., i32* noalias %.bound_tid., i8*** dereferenceable({{4|8}}) %argc, 
i{{64|32}}{{.*}} %{{.+}})
 // IRBUILDER:   define internal {{.*}}void [[OMP_OUTLINED]](i32* noalias 
%{{.*}}, i32* noalias %{{.*}}, i8*** [[ARGC_REF:%.*]], i{{64|32}}{{.*}} %{{.+}})
 // CHECK:   store i8*** %argc, i8 [[ARGC_PTR_ADDR:%.+]],
@@ -152,6 +168,12 @@
 // CHECK-DEBUG-NEXT:  }
 
 // ALL: define linkonce_odr {{.*}}void [[FOO1]](i8** %argc)
+// CHECK:   define internal {{.*}}void [[OMP_OUTLINED1]](i32* noalias 
%.global_tid., i32* noalias %.bound_tid.)
+// CHECK-SAME:  #[[FN_ATTRS:[0-9]+]]
+// CHECK:   

[PATCH] D73285: [OpenMP][OMPIRBuilder][BugFix] Handle Unreachable Finalization blocks in `parallel` generation

2020-01-26 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 240451.
fghanim added a comment.

Adding a new unittest for the this fix. Thanks to JDoerfert for Writing and 
providing me with this patch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D73285/new/

https://reviews.llvm.org/D73285

Files:
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -400,6 +400,78 @@
   EXPECT_EQ(ForkCI->getArgOperand(3), F->arg_begin());
 }
 
+TEST_F(OpenMPIRBuilderTest, ParallelEndless) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  unsigned NumBodiesGenerated = 0;
+  unsigned NumPrivatizedVars = 0;
+  unsigned NumFinalizationPoints = 0;
+
+  BasicBlock *OutlinedBodyBB = nullptr;
+  auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+   BasicBlock ) {
+++NumBodiesGenerated;
+
+auto *OldBB = OutlinedBodyBB = CodeGenIP.getBlock();
+
+// Create an endless loop.
+OldBB->getTerminator()->eraseFromParent();
+BranchInst::Create(OldBB, OldBB);
+
+Builder.ClearInsertionPoint();
+  };
+
+  auto PrivCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+Value , Value *) -> InsertPointTy {
+++NumPrivatizedVars;
+return CodeGenIP;
+  };
+
+  auto FiniCB = [&](InsertPointTy CodeGenIP) { ++NumFinalizationPoints; };
+
+  IRBuilder<>::InsertPoint AfterIP =
+  OMPBuilder.CreateParallel(Loc, BodyGenCB, PrivCB, FiniCB, nullptr,
+nullptr, OMP_PROC_BIND_default, false);
+
+  EXPECT_EQ(NumBodiesGenerated, 1U);
+  EXPECT_EQ(NumPrivatizedVars, 0U);
+  EXPECT_EQ(NumFinalizationPoints, 0U);
+
+  Builder.restoreIP(AfterIP);
+  Builder.CreateRetVoid();
+
+  ASSERT_NE(OutlinedBodyBB, nullptr);
+  Function *OutlinedFn = OutlinedBodyBB->getParent();
+  EXPECT_NE(F, OutlinedFn);
+  EXPECT_FALSE(verifyModule(*M));
+  EXPECT_TRUE(OutlinedFn->hasFnAttribute(Attribute::NoUnwind));
+  EXPECT_TRUE(OutlinedFn->hasFnAttribute(Attribute::NoRecurse));
+  EXPECT_TRUE(OutlinedFn->hasParamAttribute(0, Attribute::NoAlias));
+  EXPECT_TRUE(OutlinedFn->hasParamAttribute(1, Attribute::NoAlias));
+
+  EXPECT_TRUE(OutlinedFn->hasInternalLinkage());
+  EXPECT_EQ(OutlinedFn->arg_size(), 2U);
+
+  EXPECT_EQ(OutlinedFn->getNumUses(), 1U);
+  User *Usr = OutlinedFn->user_back();
+  ASSERT_TRUE(isa(Usr));
+  CallInst *ForkCI = dyn_cast(Usr->user_back());
+  ASSERT_NE(ForkCI, nullptr);
+
+  EXPECT_EQ(ForkCI->getCalledFunction()->getName(), "__kmpc_fork_call");
+  EXPECT_EQ(ForkCI->getNumArgOperands(), 3U);
+  EXPECT_TRUE(isa(ForkCI->getArgOperand(0)));
+  EXPECT_EQ(ForkCI->getArgOperand(1),
+ConstantInt::get(Type::getInt32Ty(Ctx), 0U));
+  EXPECT_EQ(ForkCI->getArgOperand(2), Usr);
+}
+
 TEST_F(OpenMPIRBuilderTest, ParallelIfCond) {
   using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
   OpenMPIRBuilder OMPBuilder(*M);
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -623,6 +623,7 @@
   auto FiniInfo = FinalizationStack.pop_back_val();
   assert(FiniInfo.DK == OMPD_parallel &&
  "Unexpected finalization stack state!");
+
   if (FoundPreFiniBB) {
 // PRegPreFiniBB is reachable. Adjust the finalization stack, verify the
 // adjustment, and call the finalize function a last time to finalize values
@@ -631,10 +632,7 @@
 (void)FiniInfo;
 
 Instruction *PreFiniTI = PRegPreFiniBB->getTerminator();
-assert(PreFiniTI->getNumSuccessors() == 1 &&
-   PreFiniTI->getSuccessor(0)->size() == 1 &&
-   isa(PreFiniTI->getSuccessor(0)->getTerminator()) &&
-   "Unexpected CFG structure!");
+assert(PreFiniTI->getNumSuccessors() == 1 && "Unexpected CFG structure!");
 
 InsertPointTy PreFiniIP(PRegPreFiniBB, PreFiniTI->getIterator());
 FiniCB(PreFiniIP);
@@ -665,7 +663,6 @@
   for (Instruction *I : ToBeDeleted)
 I->eraseFromParent();
 
-  AfterIP.getBlock()->dump();
   return AfterIP;
 }
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D73285: [OpenMP][OMPIRBuilder][BugFix] Handle Unreachable Finalization blocks in `parallel` generation

2020-01-23 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 239991.
fghanim added a comment.

- Cleaning up some leftover code.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D73285/new/

https://reviews.llvm.org/D73285

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -439,6 +439,18 @@
 Worklist.push_back(SuccBB);
   }
 
+  // If we didn't emit a branch to FiniBB during body generation, it means
+  // FiniBB is unreachable (e.g. while(1);). stop generating all the
+  // unreachable blocks, and remove anything we are not going to use.
+  // Check to see if PRegPreFiniBB is reachable from PRegionBodyBB.
+  bool FoundPreFiniBB = false;
+  for (auto BI : ParallelRegionBlocks) {
+if (BI == PRegPreFiniBB) {
+  FoundPreFiniBB = true;
+  break;
+}
+  }
+
   CodeExtractorAnalysisCache CEAC(*OuterFn);
   CodeExtractor Extractor(ParallelRegionBlocks, /* DominatorTree */ nullptr,
   /* AggregateArgs */ false,
@@ -564,7 +576,7 @@
 }
   }
 
-  Builder.CreateCall(RTLFn, RealArgs);
+  CallInst *ForkCall = Builder.CreateCall(RTLFn, RealArgs);
 
   LLVM_DEBUG(dbgs() << "With fork_call placed: "
 << *Builder.GetInsertBlock()->getParent() << "\n");
@@ -583,7 +595,6 @@
   if (!ElseTI) {
 CI->eraseFromParent();
   } else {
-
 // If an "if" clause was present we are now generating the serialized
 // version into the "else" branch.
 Builder.SetInsertPoint(ElseTI);
@@ -608,26 +619,53 @@
   << *Builder.GetInsertBlock()->getParent() << "\n");
   }
 
-  // Adjust the finalization stack, verify the adjustment, and call the
-  // finalize function a last time to finalize values between the pre-fini block
-  // and the exit block if we left the parallel "the normal way".
+  assert(!FinalizationStack.empty() && "Unexpected finalization stack state!");
   auto FiniInfo = FinalizationStack.pop_back_val();
-  (void)FiniInfo;
   assert(FiniInfo.DK == OMPD_parallel &&
  "Unexpected finalization stack state!");
+  if (FoundPreFiniBB) {
+// PRegPreFiniBB is reachable. Adjust the finalization stack, verify the
+// adjustment, and call the finalize function a last time to finalize values
+// between the pre-fini block and the exit block if we left the parallel
+// "the normal way".
+(void)FiniInfo;
+
+Instruction *PreFiniTI = PRegPreFiniBB->getTerminator();
+assert(PreFiniTI->getNumSuccessors() == 1 &&
+   PreFiniTI->getSuccessor(0)->size() == 1 &&
+   isa(PreFiniTI->getSuccessor(0)->getTerminator()) &&
+   "Unexpected CFG structure!");
+
+InsertPointTy PreFiniIP(PRegPreFiniBB, PreFiniTI->getIterator());
+FiniCB(PreFiniIP);
+  } else {
+// PRegPreFiniBB is unreachable. remove the unreachable blocks
+// and discard the finalization callback
+llvm::SmallVector ToBeDeletedBB;
+ToBeDeletedBB.push_back(PRegPreFiniBB);
+BranchInst *BBTerminator =
+dyn_cast_or_null(PRegPreFiniBB->getTerminator());
+while (BBTerminator) {
+  assert(!BBTerminator->isConditional() &&
+ "unexpected conditional branch in unreachable blocks");
+  BasicBlock *next = BBTerminator->getSuccessor(0);
+  ToBeDeletedBB.push_back(next);
+  BBTerminator = dyn_cast_or_null(next->getTerminator());
+}
 
-  Instruction *PreFiniTI = PRegPreFiniBB->getTerminator();
-  assert(PreFiniTI->getNumSuccessors() == 1 &&
- PreFiniTI->getSuccessor(0)->size() == 1 &&
- isa(PreFiniTI->getSuccessor(0)->getTerminator()) &&
- "Unexpected CFG structure!");
+for (auto BB : ToBeDeletedBB) {
+  BB->eraseFromParent();
+}
 
-  InsertPointTy PreFiniIP(PRegPreFiniBB, PreFiniTI->getIterator());
-  FiniCB(PreFiniIP);
+BasicBlock *ForkBB = ForkCall->getParent();
+ForkBB->getTerminator()->eraseFromParent();
+AfterIP = InsertPointTy(ForkBB, ForkBB->end());
+  }
 
   for (Instruction *I : ToBeDeleted)
 I->eraseFromParent();
 
+  AfterIP.getBlock()->dump();
   return AfterIP;
 }
 
Index: clang/lib/CodeGen/CGStmtOpenMP.cpp
===
--- clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -1377,15 +1377,15 @@
   ReturnBlock = getJumpDestInCurrentScope();
 
   llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
-  CodeGenIPBB->splitBasicBlock(CodeGenIP.getPoint());
   llvm::Instruction *CodeGenIPBBTI = CodeGenIPBB->getTerminator();
-  CodeGenIPBBTI->removeFromParent();
+  CodeGenIPBBTI->eraseFromParent();
 
   Builder.SetInsertPoint(CodeGenIPBB);
 
   EmitStmt(ParallelRegionBodyStmt);
 
-  Builder.Insert(CodeGenIPBBTI);
+  if 

[PATCH] D73285: [OpenMP][OMPIRBuilder][BugFix] Handle Unreachable Finalization blocks in `parallel` generation

2020-01-23 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim created this revision.
fghanim added a reviewer: jdoerfert.
Herald added subscribers: llvm-commits, cfe-commits, guansong, hiraditya.
Herald added projects: clang, LLVM.

In some situations (e.g. `while(1);` ) the body block(s) will not contain a 
branch to the finalization block.
In this patch, `CreateParallel` has been modified to check if the 
`PRegPreFiniBB` is reachable before
generating finalization code. If not, will remove all unreachable blocks.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D73285

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -439,6 +439,18 @@
 Worklist.push_back(SuccBB);
   }
 
+  // If we didn't emit a branch to FiniBB during body generation, it means
+  // FiniBB is unreachable (e.g. while(1);). stop generating all the
+  // unreachable blocks, and remove anything we are not going to use.
+  // Check to see if PRegPreFiniBB is reachable from PRegionBodyBB.
+  bool FoundPreFiniBB = false;
+  for (auto BI : ParallelRegionBlocks) {
+if (BI == PRegPreFiniBB) {
+  FoundPreFiniBB = true;
+  break;
+}
+  }
+
   CodeExtractorAnalysisCache CEAC(*OuterFn);
   CodeExtractor Extractor(ParallelRegionBlocks, /* DominatorTree */ nullptr,
   /* AggregateArgs */ false,
@@ -564,7 +576,8 @@
 }
   }
 
-  Builder.CreateCall(RTLFn, RealArgs);
+  Builder.GetInsertBlock()->dump();
+  CallInst *ForkCall = Builder.CreateCall(RTLFn, RealArgs);
 
   LLVM_DEBUG(dbgs() << "With fork_call placed: "
 << *Builder.GetInsertBlock()->getParent() << "\n");
@@ -583,7 +596,6 @@
   if (!ElseTI) {
 CI->eraseFromParent();
   } else {
-
 // If an "if" clause was present we are now generating the serialized
 // version into the "else" branch.
 Builder.SetInsertPoint(ElseTI);
@@ -608,26 +620,54 @@
   << *Builder.GetInsertBlock()->getParent() << "\n");
   }
 
-  // Adjust the finalization stack, verify the adjustment, and call the
-  // finalize function a last time to finalize values between the pre-fini block
-  // and the exit block if we left the parallel "the normal way".
+  assert(!FinalizationStack.empty() && "Unexpected finalization stack state!");
   auto FiniInfo = FinalizationStack.pop_back_val();
-  (void)FiniInfo;
   assert(FiniInfo.DK == OMPD_parallel &&
  "Unexpected finalization stack state!");
+  if (FoundPreFiniBB) {
+// PRegPreFiniBB is reachable. Adjust the finalization stack, verify the
+// adjustment, and call the finalize function a last time to finalize values
+// between the pre-fini block and the exit block if we left the parallel
+// "the normal way".
+(void)FiniInfo;
+
+Instruction *PreFiniTI = PRegPreFiniBB->getTerminator();
+assert(PreFiniTI->getNumSuccessors() == 1 &&
+   PreFiniTI->getSuccessor(0)->size() == 1 &&
+   isa(PreFiniTI->getSuccessor(0)->getTerminator()) &&
+   "Unexpected CFG structure!");
+
+InsertPointTy PreFiniIP(PRegPreFiniBB, PreFiniTI->getIterator());
+FiniCB(PreFiniIP);
+  } else {
+// PRegPreFiniBB is unreachable. remove the blocks and discard the
+// finalization callback
+llvm::SmallVector ToBeDeletedBB;
+ToBeDeletedBB.push_back(PRegPreFiniBB);
+PRegPreFiniBB->getUniquePredecessor();
+BranchInst *BBTerminator =
+dyn_cast_or_null(PRegPreFiniBB->getTerminator());
+while (BBTerminator) {
+  assert(!BBTerminator->isConditional() &&
+ "unexpected conditional branch in unreachable blocks");
+  BasicBlock *next = BBTerminator->getSuccessor(0);
+  ToBeDeletedBB.push_back(next);
+  BBTerminator = dyn_cast_or_null(next->getTerminator());
+}
 
-  Instruction *PreFiniTI = PRegPreFiniBB->getTerminator();
-  assert(PreFiniTI->getNumSuccessors() == 1 &&
- PreFiniTI->getSuccessor(0)->size() == 1 &&
- isa(PreFiniTI->getSuccessor(0)->getTerminator()) &&
- "Unexpected CFG structure!");
+for (auto BB : ToBeDeletedBB) {
+  BB->eraseFromParent();
+}
 
-  InsertPointTy PreFiniIP(PRegPreFiniBB, PreFiniTI->getIterator());
-  FiniCB(PreFiniIP);
+BasicBlock *ForkBB = ForkCall->getParent();
+ForkBB->getTerminator()->eraseFromParent();
+AfterIP = InsertPointTy(ForkBB, ForkBB->end());
+  }
 
   for (Instruction *I : ToBeDeleted)
 I->eraseFromParent();
 
+  AfterIP.getBlock()->dump();
   return AfterIP;
 }
 
Index: clang/lib/CodeGen/CGStmtOpenMP.cpp
===
--- clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -1377,15 +1377,16 @@
   ReturnBlock = getJumpDestInCurrentScope();
 
   llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
-   

[PATCH] D72304: [OpenMP][OMPIRBuilder] Add Directives (master and critical) to OMPBuilder.

2020-01-17 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim added a comment.

No, I don't have commit privileges. I'd appreciate if you'd commit this for me. 
Thanks :)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72304/new/

https://reviews.llvm.org/D72304



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D72304: [OpenMP][OMPIRBuilder] Add Directives (master and critical) to OMPBuilder.

2020-01-17 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 238858.
fghanim added a comment.

Addressing reviewer's comments regarding styling.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72304/new/

https://reviews.llvm.org/D72304

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/test/OpenMP/critical_codegen.cpp
  clang/test/OpenMP/master_codegen.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPConstants.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
  llvm/lib/Frontend/OpenMP/OMPConstants.cpp
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -613,4 +613,161 @@
   }
 }
 
+TEST_F(OpenMPIRBuilderTest, MasterDirective) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = nullptr;
+
+  BasicBlock *EntryBB = nullptr;
+  BasicBlock *ExitBB = nullptr;
+  BasicBlock *ThenBB = nullptr;
+
+  auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+   BasicBlock ) {
+if (AllocaIP.isSet())
+  Builder.restoreIP(AllocaIP);
+else
+  Builder.SetInsertPoint(&*(F->getEntryBlock().getFirstInsertionPt()));
+PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+Builder.CreateStore(F->arg_begin(), PrivAI);
+
+llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+llvm::Instruction *CodeGenIPInst = &*CodeGenIP.getPoint();
+EXPECT_EQ(CodeGenIPBB->getTerminator(), CodeGenIPInst);
+
+Builder.restoreIP(CodeGenIP);
+
+// collect some info for checks later
+ExitBB = FiniBB.getUniqueSuccessor();
+ThenBB = Builder.GetInsertBlock();
+EntryBB = ThenBB->getUniquePredecessor();
+
+// simple instructions for body
+Value *PrivLoad = Builder.CreateLoad(PrivAI, "local.use");
+Builder.CreateICmpNE(F->arg_begin(), PrivLoad);
+  };
+
+  auto FiniCB = [&](InsertPointTy IP) {
+BasicBlock *IPBB = IP.getBlock();
+EXPECT_NE(IPBB->end(), IP.getPoint());
+  };
+
+  Builder.restoreIP(OMPBuilder.CreateMaster(Builder, BodyGenCB, FiniCB));
+  Value *EntryBBTI = EntryBB->getTerminator();
+  EXPECT_NE(EntryBBTI, nullptr);
+  EXPECT_TRUE(isa(EntryBBTI));
+  BranchInst *EntryBr = cast(EntryBB->getTerminator());
+  EXPECT_TRUE(EntryBr->isConditional());
+  EXPECT_EQ(EntryBr->getSuccessor(0), ThenBB);
+  EXPECT_EQ(ThenBB->getUniqueSuccessor(), ExitBB);
+  EXPECT_EQ(EntryBr->getSuccessor(1), ExitBB);
+
+  CmpInst *CondInst = cast(EntryBr->getCondition());
+  EXPECT_TRUE(isa(CondInst->getOperand(0)));
+
+  CallInst *MasterEntryCI = cast(CondInst->getOperand(0));
+  EXPECT_EQ(MasterEntryCI->getNumArgOperands(), 2U);
+  EXPECT_EQ(MasterEntryCI->getCalledFunction()->getName(), "__kmpc_master");
+  EXPECT_TRUE(isa(MasterEntryCI->getArgOperand(0)));
+
+  CallInst *MasterEndCI = nullptr;
+  for (auto  : *ThenBB) {
+Instruction *cur = 
+if (isa(cur)) {
+  MasterEndCI = cast(cur);
+  if (MasterEndCI->getCalledFunction()->getName() == "__kmpc_end_master")
+break;
+  MasterEndCI = nullptr;
+}
+  }
+  EXPECT_NE(MasterEndCI, nullptr);
+  EXPECT_EQ(MasterEndCI->getNumArgOperands(), 2U);
+  EXPECT_TRUE(isa(MasterEndCI->getArgOperand(0)));
+  EXPECT_EQ(MasterEndCI->getArgOperand(1), MasterEntryCI->getArgOperand(1));
+}
+
+TEST_F(OpenMPIRBuilderTest, CriticalDirective) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+
+  BasicBlock *EntryBB = nullptr;
+
+  auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+   BasicBlock ) {
+// collect some info for checks later
+EntryBB = FiniBB.getUniquePredecessor();
+
+// actual start for bodyCB
+llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+llvm::Instruction *CodeGenIPInst = &*CodeGenIP.getPoint();
+EXPECT_EQ(CodeGenIPBB->getTerminator(), CodeGenIPInst);
+EXPECT_EQ(EntryBB, CodeGenIPBB);
+
+// body begin
+Builder.restoreIP(CodeGenIP);
+Builder.CreateStore(F->arg_begin(), PrivAI);
+Value *PrivLoad = Builder.CreateLoad(PrivAI, "local.use");
+Builder.CreateICmpNE(F->arg_begin(), PrivLoad);
+  };
+
+  auto FiniCB = [&](InsertPointTy IP) {
+BasicBlock *IPBB = IP.getBlock();
+EXPECT_NE(IPBB->end(), IP.getPoint());
+  };
+
+  

[PATCH] D72304: [OpenMP]{OMPIRBuilder] Add Directives (master and critical) to OMPBuilder.

2020-01-16 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked an inline comment as done and an inline comment as not done.
fghanim added a comment.

Thanks for reviewing this.

After I address the last two comments below, How do I merge with llvm using 
phab? I'd appreciate an llvm specific link if possible.




Comment at: llvm/lib/Frontend/OpenMP/OMPConstants.cpp:45
 ///{
-
 #define OMP_TYPE(VarName, InitValue) Type *llvm::omp::types::VarName = nullptr;

jdoerfert wrote:
> Leftover
what is this referring to?



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:749
+BasicBlock *ExitPredBB = SplitPos->getParent();
+auto InsertBB = merged ? ExitPredBB : ExitBB;
+if (!isa_and_nonnull(SplitPos))

jdoerfert wrote:
> I have the feeling the merging makes it harder without providing a benefit 
> but I'm OK with it for now
more difficult in what way?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72304/new/

https://reviews.llvm.org/D72304



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D72304: [OpenMP]{OMPIRBuilder] Add Directives (master and critical) to OMPBuilder.

2020-01-16 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 238591.
fghanim marked 9 inline comments as done.
fghanim added a comment.

Addressing reviewer's comments

- fixed styling and naming according to llvm conventions


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72304/new/

https://reviews.llvm.org/D72304

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/test/OpenMP/critical_codegen.cpp
  clang/test/OpenMP/master_codegen.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPConstants.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
  llvm/lib/Frontend/OpenMP/OMPConstants.cpp
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -613,4 +613,164 @@
   }
 }
 
+TEST_F(OpenMPIRBuilderTest, MasterDirective) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = nullptr;
+
+  BasicBlock *EntryBB = nullptr;
+  BasicBlock *ExitBB = nullptr;
+  BasicBlock *ThenBB = nullptr;
+
+  auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+   BasicBlock ) {
+if (AllocaIP.isSet())
+  Builder.restoreIP(AllocaIP);
+else
+  Builder.SetInsertPoint(&*(F->getEntryBlock().getFirstInsertionPt()));
+PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+Builder.CreateStore(F->arg_begin(), PrivAI);
+
+llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+llvm::Instruction *CodeGenIPInst = &*CodeGenIP.getPoint();
+EXPECT_EQ(CodeGenIPBB->getTerminator(), CodeGenIPInst);
+
+Builder.restoreIP(CodeGenIP);
+
+// collect some info for checks later
+ExitBB = FiniBB.getUniqueSuccessor();
+ThenBB = Builder.GetInsertBlock();
+EntryBB = ThenBB->getUniquePredecessor();
+
+// simple instructions for body
+Value *PrivLoad = Builder.CreateLoad(PrivAI, "local.use");
+Builder.CreateICmpNE(F->arg_begin(), PrivLoad);
+  };
+
+  auto FiniCB = [&](InsertPointTy IP) {
+BasicBlock *IPBB = IP.getBlock();
+EXPECT_NE(IPBB->end(), IP.getPoint());
+  };
+
+  Builder.restoreIP(OMPBuilder.CreateMaster(Builder, BodyGenCB, FiniCB));
+  Value *EntryBBTI = EntryBB->getTerminator();
+  EXPECT_NE(EntryBBTI, nullptr);
+  EXPECT_TRUE(isa(EntryBBTI));
+  BranchInst *EntryBr = cast(EntryBB->getTerminator());
+  EXPECT_TRUE(EntryBr->isConditional());
+  EXPECT_EQ(EntryBr->getSuccessor(0), ThenBB);
+  EXPECT_EQ(ThenBB->getUniqueSuccessor(), ExitBB);
+  EXPECT_EQ(EntryBr->getSuccessor(1), ExitBB);
+
+  CmpInst *CondInst = cast(EntryBr->getCondition());
+  EXPECT_TRUE(isa(CondInst->getOperand(0)));
+
+  CallInst *MasterEntryCI = cast(CondInst->getOperand(0));
+  EXPECT_EQ(MasterEntryCI->getNumArgOperands(), 2U);
+  EXPECT_EQ(MasterEntryCI->getCalledFunction()->getName(), "__kmpc_master");
+  EXPECT_TRUE(isa(MasterEntryCI->getArgOperand(0)));
+
+  CallInst *MasterEndCI = nullptr;
+  for (auto  : *ThenBB) {
+Instruction *cur = 
+if (isa(cur)) {
+  MasterEndCI = cast(cur);
+  if (MasterEndCI->getCalledFunction()->getName() == "__kmpc_end_master")
+break;
+  else
+MasterEndCI = nullptr;
+}
+  }
+  EXPECT_NE(MasterEndCI, nullptr);
+  EXPECT_EQ(MasterEndCI->getNumArgOperands(), 2U);
+  EXPECT_TRUE(isa(MasterEndCI->getArgOperand(0)));
+  EXPECT_EQ(MasterEndCI->getArgOperand(1), MasterEntryCI->getArgOperand(1));
+}
+
+TEST_F(OpenMPIRBuilderTest, CriticalDirective) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+
+  BasicBlock *EntryBB = nullptr;
+
+  auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+   BasicBlock ) {
+// collect some info for checks later
+EntryBB = FiniBB.getUniquePredecessor();
+
+// actual start for bodyCB
+llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+llvm::Instruction *CodeGenIPInst = &*CodeGenIP.getPoint();
+EXPECT_EQ(CodeGenIPBB->getTerminator(), CodeGenIPInst);
+EXPECT_EQ(EntryBB, CodeGenIPBB);
+
+// body begin
+Builder.restoreIP(CodeGenIP);
+Builder.CreateStore(F->arg_begin(), PrivAI);
+Value *PrivLoad = Builder.CreateLoad(PrivAI, "local.use");
+Builder.CreateICmpNE(F->arg_begin(), PrivLoad);
+  };
+
+  auto FiniCB = [&](InsertPointTy IP) {
+BasicBlock *IPBB = IP.getBlock();
+

[PATCH] D72304: [OpenMP]{OMPIRBuilder] Add Directives (master and critical) to OMPBuilder.

2020-01-15 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 238301.
fghanim marked 25 inline comments as done.
fghanim added a comment.

Addressing reviewer comments

- Added regression tests
- styling
- adding asserts and todo where needed.

Also, Now `EmitInlinedRegion` will merge blocks where legal.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72304/new/

https://reviews.llvm.org/D72304

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  clang/test/OpenMP/critical_codegen.cpp
  clang/test/OpenMP/master_codegen.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPConstants.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
  llvm/lib/Frontend/OpenMP/OMPConstants.cpp
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -613,4 +613,164 @@
   }
 }
 
+TEST_F(OpenMPIRBuilderTest, MasterDirective) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = nullptr;
+
+  BasicBlock *EntryBB = nullptr;
+  BasicBlock *ExitBB = nullptr;
+  BasicBlock *ThenBB = nullptr;
+
+  auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+   BasicBlock ) {
+if (AllocaIP.isSet())
+  Builder.restoreIP(AllocaIP);
+else
+  Builder.SetInsertPoint(&*(F->getEntryBlock().getFirstInsertionPt()));
+PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+Builder.CreateStore(F->arg_begin(), PrivAI);
+
+llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+llvm::Instruction *CodeGenIPInst = &*CodeGenIP.getPoint();
+EXPECT_EQ(CodeGenIPBB->getTerminator(), CodeGenIPInst);
+
+Builder.restoreIP(CodeGenIP);
+
+// collect some info for checks later
+ExitBB = FiniBB.getUniqueSuccessor();
+ThenBB = Builder.GetInsertBlock();
+EntryBB = ThenBB->getUniquePredecessor();
+
+// simple instructions for body
+Value *PrivLoad = Builder.CreateLoad(PrivAI, "local.use");
+Builder.CreateICmpNE(F->arg_begin(), PrivLoad);
+  };
+
+  auto FiniCB = [&](InsertPointTy IP) {
+BasicBlock *IPBB = IP.getBlock();
+EXPECT_NE(IPBB->end(), IP.getPoint());
+  };
+
+  Builder.restoreIP(OMPBuilder.CreateMaster(Builder, BodyGenCB, FiniCB));
+  Value *EntryBBTI = EntryBB->getTerminator();
+  EXPECT_NE(EntryBBTI, nullptr);
+  EXPECT_TRUE(isa(EntryBBTI));
+  BranchInst *EntryBr = cast(EntryBB->getTerminator());
+  EXPECT_TRUE(EntryBr->isConditional());
+  EXPECT_EQ(EntryBr->getSuccessor(0), ThenBB);
+  EXPECT_EQ(ThenBB->getUniqueSuccessor(), ExitBB);
+  EXPECT_EQ(EntryBr->getSuccessor(1), ExitBB);
+
+  CmpInst *CondInst = cast(EntryBr->getCondition());
+  EXPECT_TRUE(isa(CondInst->getOperand(0)));
+
+  CallInst *MasterEntryCI = cast(CondInst->getOperand(0));
+  EXPECT_EQ(MasterEntryCI->getNumArgOperands(), 2U);
+  EXPECT_EQ(MasterEntryCI->getCalledFunction()->getName(), "__kmpc_master");
+  EXPECT_TRUE(isa(MasterEntryCI->getArgOperand(0)));
+
+  CallInst *MasterEndCI = nullptr;
+  for (auto  : *ThenBB) {
+Instruction *cur = 
+if (isa(cur)) {
+  MasterEndCI = cast(cur);
+  if (MasterEndCI->getCalledFunction()->getName() == "__kmpc_end_master")
+break;
+  else
+MasterEndCI = nullptr;
+}
+  }
+  EXPECT_NE(MasterEndCI, nullptr);
+  EXPECT_EQ(MasterEndCI->getNumArgOperands(), 2U);
+  EXPECT_TRUE(isa(MasterEndCI->getArgOperand(0)));
+  EXPECT_EQ(MasterEndCI->getArgOperand(1), MasterEntryCI->getArgOperand(1));
+}
+
+TEST_F(OpenMPIRBuilderTest, CriticalDirective) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+
+  BasicBlock *EntryBB = nullptr;
+
+  auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+   BasicBlock ) {
+// collect some info for checks later
+EntryBB = FiniBB.getUniquePredecessor();
+
+// actual start for bodyCB
+llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+llvm::Instruction *CodeGenIPInst = &*CodeGenIP.getPoint();
+EXPECT_EQ(CodeGenIPBB->getTerminator(), CodeGenIPInst);
+EXPECT_EQ(EntryBB, CodeGenIPBB);
+
+// body begin
+Builder.restoreIP(CodeGenIP);
+Builder.CreateStore(F->arg_begin(), PrivAI);
+Value *PrivLoad = Builder.CreateLoad(PrivAI, "local.use");
+Builder.CreateICmpNE(F->arg_begin(), PrivLoad);
+  };
+
+  auto 

[PATCH] D72304: [OpenMP]{OMPIRBuilder] Add Directives (master and critical) to OMPBuilder.

2020-01-12 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked 4 inline comments as done.
fghanim added a comment.

I am working on a patch. In the mean time ;)




Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:3020
+  if (AllocaIP.isSet())
+AllocaInsertPt = &*AllocaIP.getPoint();
+  auto OldReturnBlock = ReturnBlock;

jdoerfert wrote:
> fghanim wrote:
> > jdoerfert wrote:
> > > Why the `isSet` check? If we need it, then it's missing in other places 
> > > as well (I think).
> > While I was writing the response I realized that I should've made it
> > ` assert((!AllocaIP.isSet() || AllocaInsertPt == AllocaIP.getpoint()) && 
> > "Inlined directives allocas should be emitted to entry block of the inner 
> > most containing outlined function"; ` 
> > or something less verbose.
> > 
> > Inlined directives should emit their `alloca`s to the entry block of the 
> > containing outined function, right? For that reason, and since we don't 
> > store an `alloca` insertion point in the OMPBuilder, I pass an empty 
> > insertion point, and back up the current point, without changing it.
> > I was planning, in the future, to keep a copy of the most recent alloca 
> > insertion point which we set when we outline, and pass that for `BodyCB`s 
> > within inlined directives.
> I try not to make assumptions like "Inlined directives should emit their 
> allocas to the entry block of the containing outined function, right" if we 
> don't need to. If we do, the assert is probably a better way to go. I would 
> actually say we want a helper that sets/resets all these things for *all* 
> body functions. If the OMPBuilder doesn't want to set a new allocaip, it 
> should provide an "invalid" location to indicate that. Now I finally see what 
> you do here and why. Go with the assert for now, and make it `!isSet()` if 
> there is no reason to complicate it further.
I think most (if not all) llvm passes that are concerned with allocas, expect 
them to be in the entry block, this is why I made this assumption. The only 
time that I can think of, where this will not be true is in case of a delayed 
outlining. In that case, the insertion block is going to be the "future" entry 
block of the "future" outlined function, and needs to be passed. Then I think 
it is also necessary to keep track of the insertion block somewhere inside the 
OMPBuilder.



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:800
+  ExitCall->removeFromParent();
+  Builder.Insert(ExitCall);
+

jdoerfert wrote:
> `ExitCall->moveBefore(IP)`, potentially with some IP = ... above.
> We should specify what the builder insert position is after this call, maybe 
> "unspecified"?
before this point, the builder is always set to insert before the terminator of 
the finalization block. when `emitInlinedFunction` is done, it sets it to 
`ExitBB.end()`



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:824
+  Out << Name;
+  StringRef RuntimeName = Out.str();
+  auto  = *InternalVars.try_emplace(RuntimeName, nullptr).first;

jdoerfert wrote:
> fghanim wrote:
> > jdoerfert wrote:
> > > This seems to be too much trouble to get a StringRef from a Twine. Maybe 
> > > start with a StringRef and avoid all of it?
> > Same as prev.
> Here, the argument that people recognize it doesn't work at all, it's a type. 
> So please pass a StringRef if there is no other reason not to.
You are right. *That* argument doesn't work. However the argument that I found 
it this way in clang kinda does. Also the argument that it makes things much 
easier to move. ;)
I assume whoever did it this way had his/her good reason. I understand you may 
not think it is, but given that it's used a lot in clang for getting and 
creating internal OMP variables, you can't really judge without going over 
every case to make sure that what you want can be done in every case, as such I 
am against the change *NOW*. However, for the time being, I included a todo 
basically saying that.



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:836
+ /*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal,
+ AddressSpace);
+}

jdoerfert wrote:
> fghanim wrote:
> > jdoerfert wrote:
> > > Do we really want common linkage for "internal variables"? Internal or 
> > > private would have been my first guess. Maybe the "internal" part is 
> > > different than I expect it.
> > > 
> > > ---
> > > 
> > > FWIW, the usual pattern is
> > > ```
> > > if (!Elem.second)
> > >   Elem.second = ...
> > > assert(...)
> > > return Elem.second;
> > > ```
> > same as prev.
> The code pattern is unrelated from "same as prev", I hope.
> 
> For the linkage, add a TODO to investigate if private linkage would work as 
> well.
The assert is only needed if `Elem.second` already exist. It's quite useless if 
it's not and we are creating it, right?
I'll change it to an `if..else`, and return it once.

critical name 

[PATCH] D72304: Summary: Add OpenMP Directives (master and critical) to OMPBuilder, and use them in clang.

2020-01-11 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked 10 inline comments as done.
fghanim added a comment.

> Commits need a message that explains the change.
>  Also the "Summary" should not be in the commit title.

Is this something for future commits or do you want me to change these?
If the latter, how do I do that?

> This adds support for clang to build the directives via the OMPIRBuilder so 
> we need to add tests for that. Probably "just" running existing codegen tests 
> for the directives with the OMPIRBuidlder enabled.

You mean unit tests? or something else? If not unit tests, Where do I do that?

As for rest of comments, anything without response, is something I will do.




Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:3020
+  if (AllocaIP.isSet())
+AllocaInsertPt = &*AllocaIP.getPoint();
+  auto OldReturnBlock = ReturnBlock;

jdoerfert wrote:
> Why the `isSet` check? If we need it, then it's missing in other places as 
> well (I think).
While I was writing the response I realized that I should've made it
` assert((!AllocaIP.isSet() || AllocaInsertPt == AllocaIP.getpoint()) && 
"Inlined directives allocas should be emitted to entry block of the inner most 
containing outlined function"; ` 
or something less verbose.

Inlined directives should emit their `alloca`s to the entry block of the 
containing outined function, right? For that reason, and since we don't store 
an `alloca` insertion point in the OMPBuilder, I pass an empty insertion point, 
and back up the current point, without changing it.
I was planning, in the future, to keep a copy of the most recent alloca 
insertion point which we set when we outline, and pass that for `BodyCB`s 
within inlined directives.



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:3060
+? nullptr
+: Builder.CreateIntCast(EmitScalarExpr(Hint), CGM.Int32Ty, false);
+

jdoerfert wrote:
> Can we do
> ```
> llvm::Value *HintInst = nullptr;
> if (Hint)
>   ...
> ```
> these three lines look ugly :(
np.

btw, the todo note is about the `CGM.Int32Ty`. Currently in clang it's 
`CGM.IntTy`. I'll expand on the todo



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:3076
+  EmitBranchThroughCleanup(Dest);
+};
+

jdoerfert wrote:
> I somehow have the feeling we could have a "FiniCB" helper function as they 
> seem to be always the same. A TODO mentioning that is also OK for now.
I'll do a Todo for now. I am using a very similar `FiniCB` to the one you used 
for `parallel`. So it doesn't make sense to change these here and not that one 
with them.



Comment at: clang/lib/CodeGen/CGStmtOpenMP.cpp:3099
+  ReturnBlock = OldReturnBlock;
+};
+

jdoerfert wrote:
> Same here, that looks like the other "BodyGenCB". We should really make them 
> helpers. 
Same as prev.



Comment at: llvm/lib/Frontend/OpenMP/OMPConstants.cpp:68
+  // Type::getInt32Ty(M.getContext()),/*NumElements*/ 8);
+  //  KmpCriticalNamePtrTy = PointerType::getUnqual(KmpCriticalNameTy);
   // Create all simple and struct types exposed by the runtime and remember

jdoerfert wrote:
> These comments that reference `KmpCriticalNameTy` do not help here. If you 
> want, feel free to add comments that explain how the result looks in a 
> generic way but only showing `KmpCriticalNameTy` seems counterproductive to 
> me.
I was testing something, and I forgot to remove them when I was done.



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:717
+  // emit exit call and do any needed finalization.
+  auto FinIP = InsertPointTy(FiniBB, FiniBB->getFirstInsertionPt());
+  assert(FiniBB->getTerminator()->getNumSuccessors() == 1 &&

jdoerfert wrote:
> Nit: `InsertPointTy FiniIP(FiniBB, FiniBB->getFirstInsertionPt());`
Nit?



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:721
+ "Unexpected insertion point for finalization call!");
+  emitCommonDirectiveExit(OMPD, FinIP, ExitBB, ExitCall, /*hasFinalize*/ true);
+

jdoerfert wrote:
> Shouldn't we use the argument here?
"The argument" refers to?



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:808
+ StringRef FirstSeparator,
+ StringRef Separator) const {
+  SmallString<128> Buffer;

jdoerfert wrote:
> This can be a static helper and the name should be more descriptive, 
> `getName` is very generic.
This is taken from clang (I think CG_OMP)  as is. I am keeping everything as is 
for now in case somebody decided to help and came across it being used 
somewhere else, they should recognize it immediately.

So for now, I suggest a todo. cos if I find that it's not used except with 
`critical`, I'll merge all 3 functions into 1 and be done with it.



Comment at: 

[PATCH] D72304: Summary: Add OpenMP Directives (master and critical) to OMPBuilder, and use them in clang.

2020-01-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked 8 inline comments as done.
fghanim added a comment.

So I modified the patch based on your comments. I removed all the code that I 
intended for llvm pass writers for now. 
I will look at it again later, figure something to do with the whole 
switch-if-cascade thing, and will introduce it in a separate patch. I think 
that would be better than to include it here.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72304/new/

https://reviews.llvm.org/D72304



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D72304: Summary: Add OpenMP Directives (master and critical) to OMPBuilder, and use them in clang.

2020-01-09 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim updated this revision to Diff 237149.
fghanim added a comment.

- Adding array types to OMPKinds.def. Inlining runtime function calls 
generation. reformatting with clang format


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72304/new/

https://reviews.llvm.org/D72304

Files:
  clang/lib/CodeGen/CGStmtOpenMP.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPConstants.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
  llvm/lib/Frontend/OpenMP/OMPConstants.cpp
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Index: llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
===
--- llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
+++ llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
@@ -613,4 +613,179 @@
   }
 }
 
+TEST_F(OpenMPIRBuilderTest, MasterDirective) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = nullptr;
+
+  BasicBlock *EntryBB = nullptr;
+  BasicBlock *FinalBB = nullptr;
+  BasicBlock *ExitBB = nullptr;
+  BasicBlock *ThenBB = nullptr;
+
+  auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+   BasicBlock ) {
+if (AllocaIP.isSet())
+  Builder.restoreIP(AllocaIP);
+else
+  Builder.SetInsertPoint(&*(F->getEntryBlock().getFirstInsertionPt()));
+PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+Builder.CreateStore(F->arg_begin(), PrivAI);
+
+llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+llvm::Instruction *CodeGenIPInst = &*CodeGenIP.getPoint();
+EXPECT_EQ(CodeGenIPBB->getTerminator(), CodeGenIPInst);
+
+Builder.restoreIP(CodeGenIP);
+
+// collect some info for checks later
+FinalBB = 
+ExitBB = FiniBB.getUniqueSuccessor();
+ThenBB = Builder.GetInsertBlock();
+EntryBB = ThenBB->getUniquePredecessor();
+
+// simple instructions for body
+Value *PrivLoad = Builder.CreateLoad(PrivAI, "local.use");
+Builder.CreateICmpNE(F->arg_begin(), PrivLoad);
+  };
+
+  auto FiniCB = [&](InsertPointTy IP) {
+BasicBlock *IPBB = IP.getBlock();
+EXPECT_NE(IPBB->end(), IP.getPoint());
+  };
+
+  Builder.restoreIP(OMPBuilder.CreateMaster(Builder, BodyGenCB, FiniCB));
+  Value *EntryBBTI = EntryBB->getTerminator();
+  EXPECT_NE(EntryBBTI, nullptr);
+  EXPECT_TRUE(isa(EntryBBTI));
+  BranchInst *EntryBr = cast(EntryBB->getTerminator());
+  EXPECT_TRUE(EntryBr->isConditional());
+  EXPECT_EQ(EntryBr->getSuccessor(0), ThenBB);
+  EXPECT_EQ(ThenBB->getUniqueSuccessor(), FinalBB);
+  EXPECT_EQ(FinalBB->getUniqueSuccessor(), ExitBB);
+  EXPECT_EQ(EntryBr->getSuccessor(1), ExitBB);
+
+  CmpInst *CondInst = cast(EntryBr->getCondition());
+  EXPECT_TRUE(isa(CondInst->getOperand(0)));
+
+  CallInst *MasterEntryCI = cast(CondInst->getOperand(0));
+  EXPECT_EQ(MasterEntryCI->getNumArgOperands(), 2U);
+  EXPECT_EQ(MasterEntryCI->getCalledFunction()->getName(), "__kmpc_omp_master");
+  EXPECT_TRUE(isa(MasterEntryCI->getArgOperand(0)));
+
+  CallInst *MasterEndCI = nullptr;
+  for (auto  : *FinalBB) {
+Instruction *cur = 
+if (isa(cur)) {
+  MasterEndCI = cast(cur);
+  if (MasterEndCI->getCalledFunction()->getName() ==
+  "__kmpc_omp_end_master")
+break;
+  else
+MasterEndCI = nullptr;
+}
+  }
+  EXPECT_NE(MasterEndCI, nullptr);
+  EXPECT_EQ(MasterEndCI->getNumArgOperands(), 2U);
+  EXPECT_TRUE(isa(MasterEndCI->getArgOperand(0)));
+  EXPECT_EQ(MasterEndCI->getArgOperand(1), MasterEntryCI->getArgOperand(1));
+}
+
+TEST_F(OpenMPIRBuilderTest, CriticalDirective) {
+  using InsertPointTy = OpenMPIRBuilder::InsertPointTy;
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  F->setName("func");
+  IRBuilder<> Builder(BB);
+
+  OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
+
+  AllocaInst *PrivAI = Builder.CreateAlloca(F->arg_begin()->getType());
+
+  BasicBlock *EntryBB = nullptr;
+  BasicBlock *FinalBB = nullptr;
+  BasicBlock *ExitBB = nullptr;
+
+  auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP,
+   BasicBlock ) {
+// collect some info for checks later
+FinalBB = 
+ExitBB = FiniBB.getUniqueSuccessor();
+EntryBB = FinalBB->getUniquePredecessor();
+
+// actual start for bodyCB
+llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
+llvm::Instruction *CodeGenIPInst = &*CodeGenIP.getPoint();
+EXPECT_EQ(CodeGenIPBB->getTerminator(), CodeGenIPInst);
+EXPECT_EQ(EntryBB, CodeGenIPBB);
+
+// body begin
+Builder.restoreIP(CodeGenIP);
+Builder.CreateStore(F->arg_begin(), PrivAI);
+Value *PrivLoad = Builder.CreateLoad(PrivAI, 

[PATCH] D72304: Summary: Add OpenMP Directives (master and critical) to OMPBuilder, and use them in clang.

2020-01-07 Thread Fady Ghanim via Phabricator via cfe-commits
fghanim marked 5 inline comments as done.
fghanim added a comment.

I don't think there is enough in this patch to split it into two or three 
patches. The main part is `EmitOMPInlinedRegion` which does all the heavy 
lifting. At this point, both create `Master` & `Critical` are almost wrappers 
collecting arguments for `EmitOMPInlinedRegion`. So I really do not want to 
split them into multiple patches unless I have to.

As for the rest , I just wanted to provide my thinking when I did them. I'll 
update them if you think it's better to. let me know.




Comment at: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h:250
+   llvm::ArrayType *KmpCriticalNameTy;
+   llvm::PointerType *KmpCriticalNamePtrTy;
+

jdoerfert wrote:
> If there is no good reason agains it, these should go into the 
> `OMPKinds.def`/`OMPConstants.{h/cpp}`. We need support for array types but 
> that is not a problem. 
I tried to do that, but started to get compilation errors when I put them in 
`OMPConstants.{h/cpp}` . So I thought that given that these are used exactly 
once according to current implementation in clang (i.e. for 'critical name'). 
I'll do it as part of OMPBuilder for the time being.



Comment at: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h:259
+
+public:
+

jdoerfert wrote:
> A lot of the functions below should not be public. We should expose as little 
> as possible, mainly the `CreateDirective` methods.
I think the OMPbuilder should provide people writing llvm transformation passes 
(e.g. autoparallelizing) with a way to be able to emit correct OMP calls based 
on their needs.

If you have minor suggestions to make it better, I'd love to update this based 
on that.
Alternatively, I'll make them private for now, until we decide on something for 
that.



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:646
+
+   Directive OMPD = Directive::OMPD_master;
+   Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);

jdoerfert wrote:
> You can just write `OMPD_master` below instead of `OMPD`.
Yes. I just feel this is a bit cleaner is all.



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:656
+   
Args, /*Conditional*/true, /*hasFinalize*/true);
+}
+

jdoerfert wrote:
> Make sure the patch is clang formatted. See also: 
> https://clang.llvm.org/docs/ClangFormat.html#script-for-patch-reformatting
Will do



Comment at: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp:850
+   return RTLFunc;
+}
+

jdoerfert wrote:
> Is this switch + if-cascade + `callEntry/Exit` really helpful?
> I mean we can replace
>   `Instruction *EntryCall = CreateEntryCall(OMPD, Args);`
> with
>   `Instruction *EntryCall = 
> Builder.CreateCall(getOrCreateRuntimeFunction(OMPRTL___kmpc_omp_master), 
> Args);`
> right?
While I completely agree that the switch+if-cascade is not an elegant solution, 
It's helpful in enabling a generic way to the directive entry and exit calls. 
which is in turn helpful for `EmitCommonDirectiveEntry` and 
`EmitCommonDirectiveExit`.

FWIW, `CreateEntryCall` was used inside of `EmitCommonDirectiveEntry`, similar 
to how `CreateExitCall` is currently inside `EmitCommonDirectiveExit`. 
`RTLFuncName` enabled that. I only took it out because of Critical's 
`Entry_Hint` special case. I plan to put it back in, If critical is the only 
directive like that.

I am not too attached to it, but I think it's help-ing :)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72304/new/

https://reviews.llvm.org/D72304



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


  1   2   >