[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-07-20 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

In D149162#4520395 , @MaskRay wrote:

> In D149162#4517500 , @MaskRay wrote:
>
>> `registerTargetGlobalVariable` relies on the iteration order of StringMap, 
>> which is not guaranteed. The bug is caught by D155789 
>> 
>>
>>   curl -L 'https://reviews.llvm.org/D155789?download=1' | patch -p1
>>   cmake ... -DLLVM_ENABLE_REVERSE_ITERATION=on
>>   ninja -C ... check-llvm-unit  #  `LLVM-Unit :: 
>> Frontend/./LLVMFrontendTests/OpenMPIRBuilderTest/registerTargetGlobalVariable`
>>  fails
>
> Looks like the issue might be introduced by 
> 03f270c900e1f8563419fdd302683a9503e98722 in the iteration order of 
> `OffloadEntriesDeviceGlobalVarTy OffloadEntriesDeviceGlobalVar` (a StringMap).
> Unfortunately, changing it to `MapVector OffloadEntryInfoDeviceGlobalVar>` will break other tests like 
> `clang/test/OpenMP/declare_target_codegen.cpp`, which suggests that there are 
> other weird iteration order dependent behavior.
> Hopefully someone familiar with OpenMP can investigate :)

I wasn't the original creator of this segment of code 
(03f270c900e1f8563419fdd302683a9503e98722) unfortunately, I just moved it into 
the OMPIRBuilder with some slight modifications, I can investigate it but I 
will unfortunately be on vacation from tomorrow onwards until the 9th of 
August, so if it is urgent it'd greatly be appreciated if someone else could 
have a look into it. Otherwise, I can pick it up when I get back.

If it is just the test added by this patch that is causing breakage and it 
affects no other OpenMP components currently, then you could perhaps deactivate 
this test for the time being until I can get time to look at it, if no one else 
has a moment to spare. It is perhaps because I am verifying the index value 
that's given to each piece of metadata/named global (e.g. 
test_data_int_1_decl_tgt_ref_ptr) and expecting they will remain the same in 
every case for this test, but with the introduction of 
LLVM_ENABLE_REVERSE_ITERATION that's perhaps no longer the case.

I am very sorry for the trouble.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149162

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


[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-07-20 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

In D149162#4520747 , @MaskRay wrote:

> In D149162#4520497 , @agozillon 
> wrote:
>
>> In D149162#4520395 , @MaskRay 
>> wrote:
>>
>>> In D149162#4517500 , @MaskRay 
>>> wrote:
>>>
 `registerTargetGlobalVariable` relies on the iteration order of StringMap, 
 which is not guaranteed. The bug is caught by D155789 
 

   curl -L 'https://reviews.llvm.org/D155789?download=1' | patch -p1
   cmake ... -DLLVM_ENABLE_REVERSE_ITERATION=on
   ninja -C ... check-llvm-unit  #  `LLVM-Unit :: 
 Frontend/./LLVMFrontendTests/OpenMPIRBuilderTest/registerTargetGlobalVariable`
  fails
>>>
>>> Looks like the issue might be introduced by 
>>> 03f270c900e1f8563419fdd302683a9503e98722 in the iteration order of 
>>> `OffloadEntriesDeviceGlobalVarTy OffloadEntriesDeviceGlobalVar` (a 
>>> StringMap).
>>> Unfortunately, changing it to `MapVector>> OffloadEntryInfoDeviceGlobalVar>` will break other tests like 
>>> `clang/test/OpenMP/declare_target_codegen.cpp`, which suggests that there 
>>> are other weird iteration order dependent behavior.
>>> Hopefully someone familiar with OpenMP can investigate :)
>>
>> I wasn't the original creator of this segment of code 
>> (03f270c900e1f8563419fdd302683a9503e98722) unfortunately, I just moved it 
>> into the OMPIRBuilder with some slight modifications, I can investigate it 
>> but I will unfortunately be on vacation from tomorrow onwards until the 9th 
>> of August, so if it is urgent it'd greatly be appreciated if someone else 
>> could have a look into it. Otherwise, I can pick it up when I get back.
>>
>> If it is just the test added by this patch that is causing breakage and it 
>> affects no other OpenMP components currently, then you could perhaps 
>> deactivate this test for the time being until I can get time to look at it, 
>> if no one else has a moment to spare. It is perhaps because I am verifying 
>> the index value that's given to each piece of metadata/named global (e.g. 
>> test_data_int_1_decl_tgt_ref_ptr) and expecting they will remain the same in 
>> every case for this test, but with the introduction of 
>> LLVM_ENABLE_REVERSE_ITERATION that's perhaps no longer the case.
>>
>> I am very sorry for the trouble.
>
> Thank you for offering help! No hurry. For `LLVM_ENABLE_REVERSE_ITERATION`, 
> my commit 14c55e6e2fa1c342a1ef908445db3d31a3475485 
>  has 
> worked for me.
>
> The underlying issue  that `!omp_offload.info` has an operand order that is 
> dependent on `StringMap` can be fixed later :) FWIW one of my attempts was:
>
>   --- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
>   +++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
>   @@ -5982,8 +5982,12 @@ void 
> OffloadEntriesInfoManager::registerDeviceGlobalVarEntryInfo(
>void OffloadEntriesInfoManager::actOnDeviceGlobalVarEntriesInfo(
>const OffloadDeviceGlobalVarEntryInfoActTy &Action) {
>  // Scan all target region entries and perform the provided action.
>   +  SmallVector, 0> 
> Vec;
>  for (const auto &E : OffloadEntriesDeviceGlobalVar)
>   -Action(E.getKey(), E.getValue());
>   +Vec.emplace_back(E.getKey(), E.getValue());
>   +  llvm::sort(Vec, less_first());
>   +  for (const auto &E : Vec)
>   +Action(E.first, E.second);
>}
>   
>void CanonicalLoopInfo::collectControlBlocks(
>
> and it would break `clang/test/OpenMP/declare_target_codegen.cpp`, so I 
> didn't investigate further.

Thank you very much for looking into it and fixing it for the time being, I'm 
sorry for the bother! I'll add it to my TODO list for when I get back to have a 
deeper look into it, if someone else doesn't get to it first.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149162

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


[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-16 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added inline comments.



Comment at: clang/lib/Driver/ToolChains/Flang.cpp:128
+  if (IsHostOffloadingAction) {
+for (size_t i = 1; i < Inputs.size(); ++i) {
+  if (Inputs[i].getType() != types::TY_Nothing)

awarzynski wrote:
> What's the magic "1"? And given that the input count matters here - is there 
> a test with multiple inputs?
It aims to mimic the behavior of Clang: 
https://github.com/llvm/llvm-project/blob/main/clang/lib/Driver/ToolChains/Clang.cpp#L4561
 where the main input is skipped (the input currently being compiled or 
embedded into etc.), when adding to //-fembed-offload-object//. 

It does look different to Clang's as Clang has more cases and the logic is 
spread across the constructJob invocation, but the first if case is what the if 
statement inside of the loop and setting the loop index variable to 1 do. The 
HostOffloadingInputs array is what is being generated here, except we're 
skipping and directly applying it as arguments.

I tried to condense it a little in this case! Perhaps it loses readability 
though, I had hoped the comment might have kept it clear



Comment at: clang/test/Driver/flang/flang-omp.f90:6
+! Test regular -fopenmp with no offload
+! RUN: %clang --driver-mode=flang -### -fopenmp %s 2>&1 | FileCheck 
--check-prefixes=CHECK-OPENMP %s
+! CHECK-OPENMP: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}}.f90"

awarzynski wrote:
> Can you remind me why isn't it possible to test this with `flang-new`? 
I double checked, it seems possible to test these with flang-new directly, the 
main reason I've tested it like this is as it's based on the other tests in the 
same directory which use clang! 

However, I'm more than happy to move these tests to the 
omp-frontend-forwarding.f90 test, remove them or keep these and add flang-new 
variations into omp-frontend-forwarding.f90. 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145815

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


[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-16 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added inline comments.



Comment at: clang/lib/Driver/ToolChains/Flang.cpp:128
+  if (IsHostOffloadingAction) {
+for (size_t i = 1; i < Inputs.size(); ++i) {
+  if (Inputs[i].getType() != types::TY_Nothing)

awarzynski wrote:
> agozillon wrote:
> > awarzynski wrote:
> > > What's the magic "1"? And given that the input count matters here - is 
> > > there a test with multiple inputs?
> > It aims to mimic the behavior of Clang: 
> > https://github.com/llvm/llvm-project/blob/main/clang/lib/Driver/ToolChains/Clang.cpp#L4561
> >  where the main input is skipped (the input currently being compiled or 
> > embedded into etc.), when adding to //-fembed-offload-object//. 
> > 
> > It does look different to Clang's as Clang has more cases and the logic is 
> > spread across the constructJob invocation, but the first if case is what 
> > the if statement inside of the loop and setting the loop index variable to 
> > 1 do. The HostOffloadingInputs array is what is being generated here, 
> > except we're skipping and directly applying it as arguments.
> > 
> > I tried to condense it a little in this case! Perhaps it loses readability 
> > though, I had hoped the comment might have kept it clear
> Thanks for the link - that code in Clang doesn't really clarify what makes 
> `Inputs[0]` special 🤔 . 
> 
> Let me rephrase my question - what's so special about the first input? 
> (referred to in Clang as "main input") Is that something specific to OpenMP? 
> For example, in this case:
> ```
> flang-new  -fopenmp  file.f90
> ```
> I assume that `inputs[0]` is "file.f90", so nothing will happen?
> 
> > I tried to condense it a little in this case! Perhaps it loses readability 
> > though, I had hoped the comment might have kept it clear
> 
> Nah, I think that your implementation is fine. It's my ignorance with respect 
> to OpenMP that's the problem here ;-)
It's not specific to OpenMP I believe, as far as I am aware Clang's supported 
offload models (SYCL and CUDA as well as OpenMP) use it! In Flang's case we 
only really care about OpenMP as I believe it's the only offload programming 
model supported.

In the case of the command: 

```
flang-new -fopenmp file.f90
``` 
The code should never be executed as no part of the command will enable an 
offloading action (for device or host)! But yes inputs[0] would indeed refer to 
file.f90.

However, this code becomes relevant when you utilise an option that enables the 
clangDriver to perform some form of offloading action. For example a command 
like: 

```
flang-new -fopenmp --offload-arch=gfx90a file.f90 
```
Will trigger two phase compilation, one for the host device (your resident CPU 
in this command) and one for the device (gfx90a in this command), the regular 
host pass will invoke like your provided command and the device pass will then 
invoke with -fopenmp-is-device in addition alongside the device triple. This 
generates two bitcode files from the one file, one containing the host code 
from the file, the other the device code (extracted from OpenMP target regions 
or declare target etc.). 

However, now we have two files, with both parts of our program, we need to 
conjoin them together, the clangDriver generates an embeddable 
fat-binary/binary using the clang-offload-packager and then invokes flang-new 
again, and this is where the above code becomes relevant, as this binary (or 
multiple binaries, if we target multiple devices in the same program) is what 
is passed to -fembed-offload-object! And inputs[0] in this case would refer to 
the output from the original host pass, which is what we want to embed the 
device binary into, so we wish to skip this original host output and only pass 
the subsequent inputs (which should be device binaries when the clangDriver 
initiates a host offloading action) we want to embed as -fembed-offload-object 
arguments. 

The offloading driver is quite complex and my knowledge of it is not perfect as 
I am not our resident expert on it unfortunately (so if anyone sees anything 
incorrect, please do chime in and correct me)! 

But hopefully this answers your question and gives you an idea of when and how 
this -fembed-offload-object comes into play, essentially a way to get the 
device binaries we wish to insert into the host binary, so it can load the 
binaries at runtime and execute them. Currently upstream Flang doesn't utilise 
this option of course, but we intend to use this as part of our OpenMP 
offloading efforts for AMD devices (whilst leaving the door open for other 
vendors devices as well). We are trying to re-use/mimic as much of the existing 
machinery that the clangDriver implements as we can! 
 



Comment at: clang/test/Driver/flang/flang-omp.f90:6
+! Test regular -fopenmp with no offload
+! RUN: %clang --driver-mode=flang -### -fopenmp %s 2>&1 | FileCheck 
--check-prefixes=CHECK-OPENMP %s
+! CHECK-OPENMP: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp"

[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-16 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 505842.
agozillon added a comment.

- [Flang][Driver] Try to fix failing omp-frontend-forwarding.f90 test
- [Flang][Driver] Simplify omp-frontend-forwarding.f90 test
- [Flang][Driver][Test] Delete flang-omp test from Clang and merge tests into 
omp-frontend-forwarding.f90


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145815

Files:
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  flang/test/Driver/omp-frontend-forwarding.f90


Index: flang/test/Driver/omp-frontend-forwarding.f90
===
--- /dev/null
+++ flang/test/Driver/omp-frontend-forwarding.f90
@@ -0,0 +1,22 @@
+! Test that flang-new OpenMP and OpenMP offload related 
+! commands forward or expand to the appropriate commands 
+! for flang-new -fc1 as expected.
+
+! Test regular -fopenmp with no offload
+! RUN: %flang -### -fopenmp %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP 
%s
+! CHECK-OPENMP: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-OPENMP-NOT: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} 
"-fopenmp-is-device" {{.*}}.f90"
+
+! Test regular -fopenmp with offload for basic fopenmp-is-device flag addition 
and correct fopenmp 
+! RUN: %flang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa %s 2>&1 | 
FileCheck --check-prefixes=CHECK-OPENMP-IS-DEVICE %s
+! CHECK-OPENMP-IS-DEVICE: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} 
"-fopenmp-is-device" {{.*}}.f90"
+
+! Testing fembed-offload-object and fopenmp-is-device
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s --check-prefixes=CHECK-OPENMP-EMBED
+! CHECK-OPENMP-EMBED: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK-OPENMP-EMBED: "{{[^"]*}}clang-offload-packager" {{.*}} 
"--image=file={{.*}}.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp"
+! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} 
"-fembed-offload-object={{.*}}.out" {{.*}}.bc"
\ No newline at end of file
Index: clang/lib/Driver/ToolChains/Flang.h
===
--- clang/lib/Driver/ToolChains/Flang.h
+++ clang/lib/Driver/ToolChains/Flang.h
@@ -56,6 +56,17 @@
   void addTargetOptions(const llvm::opt::ArgList &Args,
 llvm::opt::ArgStringList &CmdArgs) const;
 
+  /// Extract offload options from the driver arguments and add them to
+  /// the command arguments.
+  /// \param [in] C The current compilation for the driver invocation
+  /// \param [in] Inputs The input infomration on the current file inputs
+  /// \param [in] JA The job action
+  /// \param [in] Args The list of input driver arguments
+  /// \param [out] CmdArgs The list of output command arguments
+  void addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
+ const JobAction &JA, const llvm::opt::ArgList &Args,
+ llvm::opt::ArgStringList &CmdArgs) const;
+
   /// Extract other compilation options from the driver arguments and add them
   /// to the command arguments.
   ///
Index: clang/lib/Driver/ToolChains/Flang.cpp
===
--- clang/lib/Driver/ToolChains/Flang.cpp
+++ clang/lib/Driver/ToolChains/Flang.cpp
@@ -114,6 +114,33 @@
   // TODO: Add target specific flags, ABI, mtune option etc.
 }
 
+void Flang::addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
+  const JobAction &JA, const ArgList &Args,
+  ArgStringList &CmdArgs) const {
+  bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP);
+  bool IsHostOffloadingAction = JA.isHostOffloading(Action::OFK_OpenMP) ||
+JA.isHostOffloading(C.getActiveOffloadKinds());
+
+  // Skips primary input file, but adds other input to the offload object. This
+  // is condensed logic from the Clang driver for embedding offload objects
+  // during HostOffloading.
+  if (IsHostOffloadingAction) {
+for (size_t i = 1; i < Inputs.size(); ++i) {
+  if (Inputs[i].getType() != types::TY_Nothing)
+CmdArgs.push_back(
+Args.MakeArgString("-fembed-offload-object=" +
+   getToolChain().getInputFilename(Inputs[i])));
+}
+  }
+
+  if (IsOpenMPDevice) {
+// -fopenmp-is-device is passed along to tell the frontend that it is
+// generating code for a device, so that only the relevant code is
+// emitted.
+CmdArgs.push_back("-fopenmp-is-device");
+  }
+}
+

[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-16 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

In D145815#4199848 , @tschuett wrote:

> Flang will also support OpenACC for offload. It is very similar to OpenMP.

You are correct, thank you for reminding me! No idea how I forgot it, my 
apologies.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145815

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


[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-16 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 505844.
agozillon added a comment.

- Readd missing newline


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145815

Files:
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  flang/test/Driver/omp-frontend-forwarding.f90


Index: flang/test/Driver/omp-frontend-forwarding.f90
===
--- /dev/null
+++ flang/test/Driver/omp-frontend-forwarding.f90
@@ -0,0 +1,22 @@
+! Test that flang-new OpenMP and OpenMP offload related 
+! commands forward or expand to the appropriate commands 
+! for flang-new -fc1 as expected.
+
+! Test regular -fopenmp with no offload
+! RUN: %flang -### -fopenmp %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP 
%s
+! CHECK-OPENMP: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-OPENMP-NOT: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} 
"-fopenmp-is-device" {{.*}}.f90"
+
+! Test regular -fopenmp with offload for basic fopenmp-is-device flag addition 
and correct fopenmp 
+! RUN: %flang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa %s 2>&1 | 
FileCheck --check-prefixes=CHECK-OPENMP-IS-DEVICE %s
+! CHECK-OPENMP-IS-DEVICE: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} 
"-fopenmp-is-device" {{.*}}.f90"
+
+! Testing fembed-offload-object and fopenmp-is-device
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s --check-prefixes=CHECK-OPENMP-EMBED
+! CHECK-OPENMP-EMBED: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK-OPENMP-EMBED: "{{[^"]*}}clang-offload-packager" {{.*}} 
"--image=file={{.*}}.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp"
+! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} 
"-fembed-offload-object={{.*}}.out" {{.*}}.bc"
Index: clang/lib/Driver/ToolChains/Flang.h
===
--- clang/lib/Driver/ToolChains/Flang.h
+++ clang/lib/Driver/ToolChains/Flang.h
@@ -56,6 +56,17 @@
   void addTargetOptions(const llvm::opt::ArgList &Args,
 llvm::opt::ArgStringList &CmdArgs) const;
 
+  /// Extract offload options from the driver arguments and add them to
+  /// the command arguments.
+  /// \param [in] C The current compilation for the driver invocation
+  /// \param [in] Inputs The input infomration on the current file inputs
+  /// \param [in] JA The job action
+  /// \param [in] Args The list of input driver arguments
+  /// \param [out] CmdArgs The list of output command arguments
+  void addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
+ const JobAction &JA, const llvm::opt::ArgList &Args,
+ llvm::opt::ArgStringList &CmdArgs) const;
+
   /// Extract other compilation options from the driver arguments and add them
   /// to the command arguments.
   ///
Index: clang/lib/Driver/ToolChains/Flang.cpp
===
--- clang/lib/Driver/ToolChains/Flang.cpp
+++ clang/lib/Driver/ToolChains/Flang.cpp
@@ -114,6 +114,33 @@
   // TODO: Add target specific flags, ABI, mtune option etc.
 }
 
+void Flang::addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
+  const JobAction &JA, const ArgList &Args,
+  ArgStringList &CmdArgs) const {
+  bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP);
+  bool IsHostOffloadingAction = JA.isHostOffloading(Action::OFK_OpenMP) ||
+JA.isHostOffloading(C.getActiveOffloadKinds());
+
+  // Skips primary input file, but adds other input to the offload object. This
+  // is condensed logic from the Clang driver for embedding offload objects
+  // during HostOffloading.
+  if (IsHostOffloadingAction) {
+for (size_t i = 1; i < Inputs.size(); ++i) {
+  if (Inputs[i].getType() != types::TY_Nothing)
+CmdArgs.push_back(
+Args.MakeArgString("-fembed-offload-object=" +
+   getToolChain().getInputFilename(Inputs[i])));
+}
+  }
+
+  if (IsOpenMPDevice) {
+// -fopenmp-is-device is passed along to tell the frontend that it is
+// generating code for a device, so that only the relevant code is
+// emitted.
+CmdArgs.push_back("-fopenmp-is-device");
+  }
+}
+
 static void addFloatingPointOptions(const Driver &D, const ArgList &Args,
 ArgStringList &CmdArgs) {
   StringRef FPContract;
@@ -327,6 +354,9 @@
   // Add other compile options
   addOtherOptions(Args, 

[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-16 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

Deleted the clang test that was forwarding to Flang and merged with the 
omp-frontend-forwarding.f90 test where relevant. Second push was because I 
forgot to add a missing newline, which I seem to do frequently...


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145815

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


[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-16 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added inline comments.



Comment at: clang/lib/Driver/ToolChains/Flang.cpp:128
+  if (IsHostOffloadingAction) {
+for (size_t i = 1; i < Inputs.size(); ++i) {
+  if (Inputs[i].getType() != types::TY_Nothing)

jhuber6 wrote:
> agozillon wrote:
> > awarzynski wrote:
> > > agozillon wrote:
> > > > awarzynski wrote:
> > > > > What's the magic "1"? And given that the input count matters here - 
> > > > > is there a test with multiple inputs?
> > > > It aims to mimic the behavior of Clang: 
> > > > https://github.com/llvm/llvm-project/blob/main/clang/lib/Driver/ToolChains/Clang.cpp#L4561
> > > >  where the main input is skipped (the input currently being compiled or 
> > > > embedded into etc.), when adding to //-fembed-offload-object//. 
> > > > 
> > > > It does look different to Clang's as Clang has more cases and the logic 
> > > > is spread across the constructJob invocation, but the first if case is 
> > > > what the if statement inside of the loop and setting the loop index 
> > > > variable to 1 do. The HostOffloadingInputs array is what is being 
> > > > generated here, except we're skipping and directly applying it as 
> > > > arguments.
> > > > 
> > > > I tried to condense it a little in this case! Perhaps it loses 
> > > > readability though, I had hoped the comment might have kept it clear
> > > Thanks for the link - that code in Clang doesn't really clarify what 
> > > makes `Inputs[0]` special 🤔 . 
> > > 
> > > Let me rephrase my question - what's so special about the first input? 
> > > (referred to in Clang as "main input") Is that something specific to 
> > > OpenMP? For example, in this case:
> > > ```
> > > flang-new  -fopenmp  file.f90
> > > ```
> > > I assume that `inputs[0]` is "file.f90", so nothing will happen?
> > > 
> > > > I tried to condense it a little in this case! Perhaps it loses 
> > > > readability though, I had hoped the comment might have kept it clear
> > > 
> > > Nah, I think that your implementation is fine. It's my ignorance with 
> > > respect to OpenMP that's the problem here ;-)
> > It's not specific to OpenMP I believe, as far as I am aware Clang's 
> > supported offload models (SYCL and CUDA as well as OpenMP) use it! In 
> > Flang's case we only really care about OpenMP as I believe it's the only 
> > offload programming model supported.
> > 
> > In the case of the command: 
> > 
> > ```
> > flang-new -fopenmp file.f90
> > ``` 
> > The code should never be executed as no part of the command will enable an 
> > offloading action (for device or host)! But yes inputs[0] would indeed 
> > refer to file.f90.
> > 
> > However, this code becomes relevant when you utilise an option that enables 
> > the clangDriver to perform some form of offloading action. For example a 
> > command like: 
> > 
> > ```
> > flang-new -fopenmp --offload-arch=gfx90a file.f90 
> > ```
> > Will trigger two phase compilation, one for the host device (your resident 
> > CPU in this command) and one for the device (gfx90a in this command), the 
> > regular host pass will invoke like your provided command and the device 
> > pass will then invoke with -fopenmp-is-device in addition alongside the 
> > device triple. This generates two bitcode files from the one file, one 
> > containing the host code from the file, the other the device code 
> > (extracted from OpenMP target regions or declare target etc.). 
> > 
> > However, now we have two files, with both parts of our program, we need to 
> > conjoin them together, the clangDriver generates an embeddable 
> > fat-binary/binary using the clang-offload-packager and then invokes 
> > flang-new again, and this is where the above code becomes relevant, as this 
> > binary (or multiple binaries, if we target multiple devices in the same 
> > program) is what is passed to -fembed-offload-object! And inputs[0] in this 
> > case would refer to the output from the original host pass, which is what 
> > we want to embed the device binary into, so we wish to skip this original 
> > host output and only pass the subsequent inputs (which should be device 
> > binaries when the clangDriver initiates a host offloading action) we want 
> > to embed as -fembed-offload-object arguments. 
> > 
> > The offloading driver is quite complex and my knowledge of it is not 
> > perfect as I am not our resident expert on it unfortunately (so if anyone 
> > sees anything incorrect, please do chime in and correct me)! 
> > 
> > But hopefully this answers your question and gives you an idea of when and 
> > how this -fembed-offload-object comes into play, essentially a way to get 
> > the device binaries we wish to insert into the host binary, so it can load 
> > the binaries at runtime and execute them. Currently upstream Flang doesn't 
> > utilise this option of course, but we intend to use this as part of our 
> > OpenMP offloading efforts for AMD devices (whilst leaving the door open for 
> > other vendors devices as well). W

[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-16 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added inline comments.



Comment at: clang/lib/Driver/ToolChains/Flang.cpp:128
+  if (IsHostOffloadingAction) {
+for (size_t i = 1; i < Inputs.size(); ++i) {
+  if (Inputs[i].getType() != types::TY_Nothing)

awarzynski wrote:
> jhuber6 wrote:
> > agozillon wrote:
> > > jhuber6 wrote:
> > > > agozillon wrote:
> > > > > awarzynski wrote:
> > > > > > agozillon wrote:
> > > > > > > awarzynski wrote:
> > > > > > > > What's the magic "1"? And given that the input count matters 
> > > > > > > > here - is there a test with multiple inputs?
> > > > > > > It aims to mimic the behavior of Clang: 
> > > > > > > https://github.com/llvm/llvm-project/blob/main/clang/lib/Driver/ToolChains/Clang.cpp#L4561
> > > > > > >  where the main input is skipped (the input currently being 
> > > > > > > compiled or embedded into etc.), when adding to 
> > > > > > > //-fembed-offload-object//. 
> > > > > > > 
> > > > > > > It does look different to Clang's as Clang has more cases and the 
> > > > > > > logic is spread across the constructJob invocation, but the first 
> > > > > > > if case is what the if statement inside of the loop and setting 
> > > > > > > the loop index variable to 1 do. The HostOffloadingInputs array 
> > > > > > > is what is being generated here, except we're skipping and 
> > > > > > > directly applying it as arguments.
> > > > > > > 
> > > > > > > I tried to condense it a little in this case! Perhaps it loses 
> > > > > > > readability though, I had hoped the comment might have kept it 
> > > > > > > clear
> > > > > > Thanks for the link - that code in Clang doesn't really clarify 
> > > > > > what makes `Inputs[0]` special 🤔 . 
> > > > > > 
> > > > > > Let me rephrase my question - what's so special about the first 
> > > > > > input? (referred to in Clang as "main input") Is that something 
> > > > > > specific to OpenMP? For example, in this case:
> > > > > > ```
> > > > > > flang-new  -fopenmp  file.f90
> > > > > > ```
> > > > > > I assume that `inputs[0]` is "file.f90", so nothing will happen?
> > > > > > 
> > > > > > > I tried to condense it a little in this case! Perhaps it loses 
> > > > > > > readability though, I had hoped the comment might have kept it 
> > > > > > > clear
> > > > > > 
> > > > > > Nah, I think that your implementation is fine. It's my ignorance 
> > > > > > with respect to OpenMP that's the problem here ;-)
> > > > > It's not specific to OpenMP I believe, as far as I am aware Clang's 
> > > > > supported offload models (SYCL and CUDA as well as OpenMP) use it! In 
> > > > > Flang's case we only really care about OpenMP as I believe it's the 
> > > > > only offload programming model supported.
> > > > > 
> > > > > In the case of the command: 
> > > > > 
> > > > > ```
> > > > > flang-new -fopenmp file.f90
> > > > > ``` 
> > > > > The code should never be executed as no part of the command will 
> > > > > enable an offloading action (for device or host)! But yes inputs[0] 
> > > > > would indeed refer to file.f90.
> > > > > 
> > > > > However, this code becomes relevant when you utilise an option that 
> > > > > enables the clangDriver to perform some form of offloading action. 
> > > > > For example a command like: 
> > > > > 
> > > > > ```
> > > > > flang-new -fopenmp --offload-arch=gfx90a file.f90 
> > > > > ```
> > > > > Will trigger two phase compilation, one for the host device (your 
> > > > > resident CPU in this command) and one for the device (gfx90a in this 
> > > > > command), the regular host pass will invoke like your provided 
> > > > > command and the device pass will then invoke with -fopenmp-is-device 
> > > > > in addition alongside the device triple. This generates two bitcode 
> > > > > files from the one file, one containing the host code from the file, 
> > > > > the other the device code (extracted from OpenMP target regions or 
> > > > > declare target etc.). 
> > > > > 
> > > > > However, now we have two files, with both parts of our program, we 
> > > > > need to conjoin them together, the clangDriver generates an 
> > > > > embeddable fat-binary/binary using the clang-offload-packager and 
> > > > > then invokes flang-new again, and this is where the above code 
> > > > > becomes relevant, as this binary (or multiple binaries, if we target 
> > > > > multiple devices in the same program) is what is passed to 
> > > > > -fembed-offload-object! And inputs[0] in this case would refer to the 
> > > > > output from the original host pass, which is what we want to embed 
> > > > > the device binary into, so we wish to skip this original host output 
> > > > > and only pass the subsequent inputs (which should be device binaries 
> > > > > when the clangDriver initiates a host offloading action) we want to 
> > > > > embed as -fembed-offload-object arguments. 
> > > > > 
> > > > > The offloading driver is quite complex and my knowledge of it is not 
> > > > > perfect as I am not our resident expert on it unfortunate

[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-16 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 505957.
agozillon edited the summary of this revision.
agozillon added a comment.

- [Flang][Driver] Expand further on the embed-offload-object comment


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145815

Files:
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  flang/test/Driver/omp-frontend-forwarding.f90


Index: flang/test/Driver/omp-frontend-forwarding.f90
===
--- /dev/null
+++ flang/test/Driver/omp-frontend-forwarding.f90
@@ -0,0 +1,22 @@
+! Test that flang-new OpenMP and OpenMP offload related 
+! commands forward or expand to the appropriate commands 
+! for flang-new -fc1 as expected.
+
+! Test regular -fopenmp with no offload
+! RUN: %flang -### -fopenmp %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP 
%s
+! CHECK-OPENMP: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-OPENMP-NOT: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} 
"-fopenmp-is-device" {{.*}}.f90"
+
+! Test regular -fopenmp with offload for basic fopenmp-is-device flag addition 
and correct fopenmp 
+! RUN: %flang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa %s 2>&1 | 
FileCheck --check-prefixes=CHECK-OPENMP-IS-DEVICE %s
+! CHECK-OPENMP-IS-DEVICE: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} 
"-fopenmp-is-device" {{.*}}.f90"
+
+! Testing fembed-offload-object and fopenmp-is-device
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s --check-prefixes=CHECK-OPENMP-EMBED
+! CHECK-OPENMP-EMBED: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK-OPENMP-EMBED: "{{[^"]*}}clang-offload-packager" {{.*}} 
"--image=file={{.*}}.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp"
+! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} 
"-fembed-offload-object={{.*}}.out" {{.*}}.bc"
Index: clang/lib/Driver/ToolChains/Flang.h
===
--- clang/lib/Driver/ToolChains/Flang.h
+++ clang/lib/Driver/ToolChains/Flang.h
@@ -56,6 +56,17 @@
   void addTargetOptions(const llvm::opt::ArgList &Args,
 llvm::opt::ArgStringList &CmdArgs) const;
 
+  /// Extract offload options from the driver arguments and add them to
+  /// the command arguments.
+  /// \param [in] C The current compilation for the driver invocation
+  /// \param [in] Inputs The input infomration on the current file inputs
+  /// \param [in] JA The job action
+  /// \param [in] Args The list of input driver arguments
+  /// \param [out] CmdArgs The list of output command arguments
+  void addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
+ const JobAction &JA, const llvm::opt::ArgList &Args,
+ llvm::opt::ArgStringList &CmdArgs) const;
+
   /// Extract other compilation options from the driver arguments and add them
   /// to the command arguments.
   ///
Index: clang/lib/Driver/ToolChains/Flang.cpp
===
--- clang/lib/Driver/ToolChains/Flang.cpp
+++ clang/lib/Driver/ToolChains/Flang.cpp
@@ -114,6 +114,35 @@
   // TODO: Add target specific flags, ABI, mtune option etc.
 }
 
+void Flang::addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
+  const JobAction &JA, const ArgList &Args,
+  ArgStringList &CmdArgs) const {
+  bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP);
+  bool IsHostOffloadingAction = JA.isHostOffloading(Action::OFK_OpenMP) ||
+JA.isHostOffloading(C.getActiveOffloadKinds());
+
+  // Skips the primary input file, which is the input file that the compilation
+  // proccess will be executed upon (e.g. the host bitcode file) and
+  // adds the other secondary input (e.g. device bitcode files for embedding)
+  // to the embed offload object. This is condensed logic from the Clang driver
+  // for embedding offload objects during HostOffloading.
+  if (IsHostOffloadingAction) {
+for (size_t i = 1; i < Inputs.size(); ++i) {
+  if (Inputs[i].getType() != types::TY_Nothing)
+CmdArgs.push_back(
+Args.MakeArgString("-fembed-offload-object=" +
+   getToolChain().getInputFilename(Inputs[i])));
+}
+  }
+
+  if (IsOpenMPDevice) {
+// -fopenmp-is-device is passed along to tell the frontend that it is
+// generating code for a device, so that only the relevant code is
+// emitted.
+CmdArgs.push_bac

[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-16 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

Recent commit added some more detail to the comment on the 
fembed-offload-object argument as requested by @awarzynski


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145815

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


[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-17 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

May I please request a final acceptance from both @jhuber6 and @awarzynski 
before I commit this upstream! If you have no further comments to add or 
requests of course.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145815

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


[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-17 Thread Andrew Gozillon via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG0cd31a7d3087: [Flang][Driver] Add support for 
fopenmp-is-device and fembed-offload-object to… (authored by agozillon).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145815

Files:
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  flang/test/Driver/omp-frontend-forwarding.f90


Index: flang/test/Driver/omp-frontend-forwarding.f90
===
--- /dev/null
+++ flang/test/Driver/omp-frontend-forwarding.f90
@@ -0,0 +1,22 @@
+! Test that flang-new OpenMP and OpenMP offload related 
+! commands forward or expand to the appropriate commands 
+! for flang-new -fc1 as expected.
+
+! Test regular -fopenmp with no offload
+! RUN: %flang -### -fopenmp %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP 
%s
+! CHECK-OPENMP: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-OPENMP-NOT: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} 
"-fopenmp-is-device" {{.*}}.f90"
+
+! Test regular -fopenmp with offload for basic fopenmp-is-device flag addition 
and correct fopenmp 
+! RUN: %flang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa %s 2>&1 | 
FileCheck --check-prefixes=CHECK-OPENMP-IS-DEVICE %s
+! CHECK-OPENMP-IS-DEVICE: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} 
"-fopenmp-is-device" {{.*}}.f90"
+
+! Testing fembed-offload-object and fopenmp-is-device
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s --check-prefixes=CHECK-OPENMP-EMBED
+! CHECK-OPENMP-EMBED: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK-OPENMP-EMBED: "{{[^"]*}}clang-offload-packager" {{.*}} 
"--image=file={{.*}}.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp"
+! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" 
"aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} 
"-fembed-offload-object={{.*}}.out" {{.*}}.bc"
Index: clang/lib/Driver/ToolChains/Flang.h
===
--- clang/lib/Driver/ToolChains/Flang.h
+++ clang/lib/Driver/ToolChains/Flang.h
@@ -56,6 +56,17 @@
   void addTargetOptions(const llvm::opt::ArgList &Args,
 llvm::opt::ArgStringList &CmdArgs) const;
 
+  /// Extract offload options from the driver arguments and add them to
+  /// the command arguments.
+  /// \param [in] C The current compilation for the driver invocation
+  /// \param [in] Inputs The input infomration on the current file inputs
+  /// \param [in] JA The job action
+  /// \param [in] Args The list of input driver arguments
+  /// \param [out] CmdArgs The list of output command arguments
+  void addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
+ const JobAction &JA, const llvm::opt::ArgList &Args,
+ llvm::opt::ArgStringList &CmdArgs) const;
+
   /// Extract other compilation options from the driver arguments and add them
   /// to the command arguments.
   ///
Index: clang/lib/Driver/ToolChains/Flang.cpp
===
--- clang/lib/Driver/ToolChains/Flang.cpp
+++ clang/lib/Driver/ToolChains/Flang.cpp
@@ -114,6 +114,35 @@
   // TODO: Add target specific flags, ABI, mtune option etc.
 }
 
+void Flang::addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
+  const JobAction &JA, const ArgList &Args,
+  ArgStringList &CmdArgs) const {
+  bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP);
+  bool IsHostOffloadingAction = JA.isHostOffloading(Action::OFK_OpenMP) ||
+JA.isHostOffloading(C.getActiveOffloadKinds());
+
+  // Skips the primary input file, which is the input file that the compilation
+  // proccess will be executed upon (e.g. the host bitcode file) and
+  // adds the other secondary input (e.g. device bitcode files for embedding)
+  // to the embed offload object. This is condensed logic from the Clang driver
+  // for embedding offload objects during HostOffloading.
+  if (IsHostOffloadingAction) {
+for (size_t i = 1; i < Inputs.size(); ++i) {
+  if (Inputs[i].getType() != types::TY_Nothing)
+CmdArgs.push_back(
+Args.MakeArgString("-fembed-offload-object=" +
+   getToolChain().getInputFilename(Inputs[i])));
+}
+  }
+
+  if (IsOpenMPDevice) {
+// -fopenmp-is-device is passed along to tell the frontend that it is
+// generating code for a device, so that only the relevant code is
+// emitted.
+

[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-29 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

In D145815#4230780 , @jeanPerier 
wrote:

> @agozillon, in the test added here (omp-frontend-forwarding.f90), I am seeing 
> failures in some patches windows pre-merge checks that I think are not 
> related to the patches.
> Could you check if there is a stability/reproducibility issue with the 
> omp-frontend-forwarding.f90 on windows?
>
> The failure message look like:
>
>   # command stderr:
>   
> C:\ws\w8\llvm-project\premerge-checks\flang\test\Driver\omp-frontend-forwarding.f90:21:23:
>  error: CHECK-OPENMP-EMBED: expected string not found in input
>   ! CHECK-OPENMP-EMBED: "{{[^"]*}}clang-offload-packager" {{.*}} 
> "--image=file={{.*}}.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp"
> ^
>   :6:435: note: scanning from here
>"c:\\ws\\w8\\llvm-project\\premerge-checks\\build\\bin\\flang-new" "-fc1" 
> "-triple" "amdgcn-amd-amdhsa" "-emit-llvm-bc" "-fopenmp" "-mrelocation-model" 
> "pic" "-pic-level" "2" "-fopenmp-is-device" "-o" 
> "C:\\Users\\ContainerAdministrator\\AppData\\Local\\Temp\\lit-tmp-1koflt_t\\omp-frontend-forwarding-gfx90a-6808f0.bc"
>  "-x" "f95-cpp-input" 
> "C:\\ws\\w8\\llvm-project\\premerge-checks\\flang\\test\\Driver\\omp-frontend-forwarding.f90"
>   
>   
>   
>   
>   
>   ^
>   :7:266: note: possible intended match here
>"c:\\program files\\llvm\\bin\\clang-offload-packager.exe" "-o" 
> "C:\\Users\\ContainerAdministrator\\AppData\\Local\\Temp\\lit-tmp-1koflt_t\\omp-frontend-forwarding-9beea6.out"
>  
> "--image=file=C:\\Users\\ContainerAdministrator\\AppData\\Local\\Temp\\lit-tmp-1koflt_t\\omp-frontend-forwarding-gfx90a-6808f0.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp"
>
> I wonder if this is an issue with the ".exe" command suffix in the windows 
> output.
>
> Example of pre-merge failures:
>
> https://reviews.llvm.org/D146989
> https://buildkite.com/llvm-project/premerge-checks/builds/143821#01872b65-9b0c-457b-8714-c1f0ca00d02b
>
> or
> https://reviews.llvm.org/D147130
> https://buildkite.com/llvm-project/premerge-checks/builds/143873#01872ccb-9251-499a-b9fd-9155e3ffb1f1

Hi @jeanPerier,

Thank you! I noticed this today as well and I'm currently looking into it, i 
have a fix upcoming, just forcing another patch 
(https://reviews.llvm.org/D144896) to test it via buildbot as I have no easy 
access to a windows machine to test it at the moment, and yes @jhuber6 is 
correct, it appears to be the .exe postfix on windows! I'll submit the fix as 
soon as it clears the buildbot.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145815

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


[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-02-27 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon created this revision.
Herald added subscribers: sunshaoce, Moerafaat, zero9178, bzcheeseman, 
sdasgup3, wenzhicui, wrengr, cota, teijeong, rdzhabarov, tatianashp, msifontes, 
jurahul, Kayjukh, grosul1, Joonsoo, liufengdb, aartbik, mgester, arpith-jacob, 
antiagainst, shauheen, rriddle, mehdi_amini, thopre, guansong, yaxunl.
Herald added a reviewer: sscalpone.
Herald added a reviewer: awarzynski.
Herald added projects: Flang, All.
agozillon requested review of this revision.
Herald added subscribers: cfe-commits, sstefan1, stephenneuendorffer, 
nicolasvasilache, jdoerfert, MaskRay.
Herald added a reviewer: jdoerfert.
Herald added a reviewer: nicolasvasilache.
Herald added projects: clang, MLIR.

This patch adds the -fopenmp-is-device flag to Flang's FC1 and also
links it up to the application of an attribute to the mlir builtin.module
on lowering. Indiciating whether the module is for host or device.

This omp.is_device module attribute is a light weight adhoc attribute
with noconcrete ODS or class specification, similar to FIR and LLVM's
data_layout, defaultkind and kindmap's. Its intent is simply to carry
information for the moment, it affects transformations (or will in the
future) but does no transformations itself.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D144864

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  flang/include/flang/Frontend/LangOptions.def
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/tools/bbc/bbc.cpp
  mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
  mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp

Index: mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
===
--- mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -1417,6 +1417,26 @@
   return success();
 }
 
+//===--===//
+// OpenMPDialect helper functions
+//===--===//
+
+// Set the omp.is_device attribute on the module with the specified boolean
+void OpenMPDialect::setIsDevice(mlir::ModuleOp module, bool isDevice) {
+  module->setAttr(
+  mlir::StringAttr::get(module->getContext(), llvm::Twine{"omp.is_device"}),
+  mlir::BoolAttr::get(module->getContext(), isDevice));
+}
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+bool OpenMPDialect::getIsDevice(mlir::ModuleOp module) {
+  if (Attribute isDevice = module->getAttr("omp.is_device"))
+if (isDevice.isa())
+  return isDevice.dyn_cast().getValue();
+  return false;
+}
+
 #define GET_ATTRDEF_CLASSES
 #include "mlir/Dialect/OpenMP/OpenMPOpsAttributes.cpp.inc"
 
Index: mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
===
--- mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -28,6 +28,15 @@
   let cppNamespace = "::mlir::omp";
   let dependentDialects = ["::mlir::LLVM::LLVMDialect"];
   let useDefaultAttributePrinterParser = 1;
+
+  let extraClassDeclaration = [{
+// Set the omp.is_device attribute on the module with the specified boolean
+static void setIsDevice(mlir::ModuleOp module, bool isDevice);
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+static bool getIsDevice(mlir::ModuleOp module);
+  }];
 }
 
 // OmpCommon requires definition of OpenACC_Dialect.
Index: flang/tools/bbc/bbc.cpp
===
--- flang/tools/bbc/bbc.cpp
+++ flang/tools/bbc/bbc.cpp
@@ -38,6 +38,7 @@
 #include "flang/Semantics/semantics.h"
 #include "flang/Semantics/unparse-with-symbols.h"
 #include "flang/Version.inc"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/IR/AsmState.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "mlir/IR/MLIRContext.h"
@@ -122,6 +123,11 @@
 llvm::cl::desc("enable openmp"),
 llvm::cl::init(false));
 
+static llvm::cl::opt
+enableOpenMPDevice("fopenmp-is-device",
+   llvm::cl::desc("enable openmp device compilation"),
+   llvm::cl::init(false));
+
 static llvm::cl::opt enableOpenACC("fopenacc",
  llvm::cl::desc("enable openacc"),
  llvm::cl::init(false));
@@ -238,6 +244,8 @@
   kindMap, loweringOptions, {});
   burnside.lower(parseTree, semanticsContext);
   mlir::ModuleOp mlirModule = burnside.getModule();
+  if (enableOpenMP)
+mlir::omp::OpenMPDialect::setIsDevice(mlirModule, enabl

[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-02-27 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 500744.
agozillon added a comment.

Forgot to add an additional test in initial patch!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  flang/include/flang/Frontend/LangOptions.def
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Lower/OpenMP/omp-is-device.f90
  flang/tools/bbc/bbc.cpp
  mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
  mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp

Index: mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
===
--- mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -1417,6 +1417,26 @@
   return success();
 }
 
+//===--===//
+// OpenMPDialect helper functions
+//===--===//
+
+// Set the omp.is_device attribute on the module with the specified boolean
+void OpenMPDialect::setIsDevice(mlir::ModuleOp module, bool isDevice) {
+  module->setAttr(
+  mlir::StringAttr::get(module->getContext(), llvm::Twine{"omp.is_device"}),
+  mlir::BoolAttr::get(module->getContext(), isDevice));
+}
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+bool OpenMPDialect::getIsDevice(mlir::ModuleOp module) {
+  if (Attribute isDevice = module->getAttr("omp.is_device"))
+if (isDevice.isa())
+  return isDevice.dyn_cast().getValue();
+  return false;
+}
+
 #define GET_ATTRDEF_CLASSES
 #include "mlir/Dialect/OpenMP/OpenMPOpsAttributes.cpp.inc"
 
Index: mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
===
--- mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -28,6 +28,15 @@
   let cppNamespace = "::mlir::omp";
   let dependentDialects = ["::mlir::LLVM::LLVMDialect"];
   let useDefaultAttributePrinterParser = 1;
+
+  let extraClassDeclaration = [{
+// Set the omp.is_device attribute on the module with the specified boolean
+static void setIsDevice(mlir::ModuleOp module, bool isDevice);
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+static bool getIsDevice(mlir::ModuleOp module);
+  }];
 }
 
 // OmpCommon requires definition of OpenACC_Dialect.
Index: flang/tools/bbc/bbc.cpp
===
--- flang/tools/bbc/bbc.cpp
+++ flang/tools/bbc/bbc.cpp
@@ -38,6 +38,7 @@
 #include "flang/Semantics/semantics.h"
 #include "flang/Semantics/unparse-with-symbols.h"
 #include "flang/Version.inc"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/IR/AsmState.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "mlir/IR/MLIRContext.h"
@@ -122,6 +123,11 @@
 llvm::cl::desc("enable openmp"),
 llvm::cl::init(false));
 
+static llvm::cl::opt
+enableOpenMPDevice("fopenmp-is-device",
+   llvm::cl::desc("enable openmp device compilation"),
+   llvm::cl::init(false));
+
 static llvm::cl::opt enableOpenACC("fopenacc",
  llvm::cl::desc("enable openacc"),
  llvm::cl::init(false));
@@ -238,6 +244,8 @@
   kindMap, loweringOptions, {});
   burnside.lower(parseTree, semanticsContext);
   mlir::ModuleOp mlirModule = burnside.getModule();
+  if (enableOpenMP)
+mlir::omp::OpenMPDialect::setIsDevice(mlirModule, enableOpenMPDevice);
   std::error_code ec;
   std::string outputName = outputFilename;
   if (!outputName.size())
Index: flang/test/Lower/OpenMP/omp-is-device.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/omp-is-device.f90
@@ -0,0 +1,7 @@
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEVICE
+!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s --check-prefix=HOST
+
+!DEVICE: module attributes {{{.*}}, omp.is_device = true{{.*}}}
+!HOST: module attributes {{{.*}}, omp.is_device = false{{.*}}}
+subroutine omp_subroutine()
+end subroutine omp_subroutine
\ No newline at end of file
Index: flang/test/Driver/driver-help.f90
===
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -138,6 +138,7 @@
 ! HELP-FC1-NEXT: -fno-signed-zeros  Allow optimizations that ignore the sign of floating point zero

[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-02-28 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

In D144864#4156764 , @jdoerfert wrote:

> Tests?

Happy to add more, I am just a little unsure what the best way to test this is 
other than application of the attribute to the module when the flag was used 
and verification that the flag was active in FC1, do you have any suggestions? 
I don't really think there is a way to test for the attribute in 
OpenMP/invalid.mlir (as no specified verifier) or in OpenMP/ops.mlir as it is 
not a predefined operation or attribute, but I can look at how fir.kindmap 
(which is applied similarly) is tested and see if they do anything particular.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

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


[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-02-28 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 501141.
agozillon added a comment.

- [MLIR][OpenMP] Fix attribute helpers to apply to more than builtin.module


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  flang/include/flang/Frontend/LangOptions.def
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Lower/OpenMP/omp-is-device.f90
  flang/tools/bbc/bbc.cpp
  mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
  mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp

Index: mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
===
--- mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -1417,6 +1417,26 @@
   return success();
 }
 
+//===--===//
+// OpenMPDialect helper functions
+//===--===//
+
+// Set the omp.is_device attribute on the module with the specified boolean
+void OpenMPDialect::setIsDevice(Operation* module, bool isDevice) {
+  module->setAttr(
+  mlir::StringAttr::get(module->getContext(), llvm::Twine{"omp.is_device"}),
+  mlir::BoolAttr::get(module->getContext(), isDevice));
+}
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+bool OpenMPDialect::getIsDevice(Operation* module) {
+  if (Attribute isDevice = module->getAttr("omp.is_device"))
+if (isDevice.isa())
+  return isDevice.dyn_cast().getValue();
+  return false;
+}
+
 #define GET_ATTRDEF_CLASSES
 #include "mlir/Dialect/OpenMP/OpenMPOpsAttributes.cpp.inc"
 
Index: mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
===
--- mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -28,6 +28,15 @@
   let cppNamespace = "::mlir::omp";
   let dependentDialects = ["::mlir::LLVM::LLVMDialect"];
   let useDefaultAttributePrinterParser = 1;
+
+  let extraClassDeclaration = [{
+// Set the omp.is_device attribute on the module with the specified boolean
+static void setIsDevice(Operation* module, bool isDevice);
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+static bool getIsDevice(Operation* module);
+  }];
 }
 
 // OmpCommon requires definition of OpenACC_Dialect.
Index: flang/tools/bbc/bbc.cpp
===
--- flang/tools/bbc/bbc.cpp
+++ flang/tools/bbc/bbc.cpp
@@ -38,6 +38,7 @@
 #include "flang/Semantics/semantics.h"
 #include "flang/Semantics/unparse-with-symbols.h"
 #include "flang/Version.inc"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/IR/AsmState.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "mlir/IR/MLIRContext.h"
@@ -122,6 +123,11 @@
 llvm::cl::desc("enable openmp"),
 llvm::cl::init(false));
 
+static llvm::cl::opt
+enableOpenMPDevice("fopenmp-is-device",
+   llvm::cl::desc("enable openmp device compilation"),
+   llvm::cl::init(false));
+
 static llvm::cl::opt enableOpenACC("fopenacc",
  llvm::cl::desc("enable openacc"),
  llvm::cl::init(false));
@@ -238,6 +244,8 @@
   kindMap, loweringOptions, {});
   burnside.lower(parseTree, semanticsContext);
   mlir::ModuleOp mlirModule = burnside.getModule();
+  if (enableOpenMP)
+mlir::omp::OpenMPDialect::setIsDevice(mlirModule, enableOpenMPDevice);
   std::error_code ec;
   std::string outputName = outputFilename;
   if (!outputName.size())
Index: flang/test/Lower/OpenMP/omp-is-device.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/omp-is-device.f90
@@ -0,0 +1,7 @@
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEVICE
+!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s --check-prefix=HOST
+
+!DEVICE: module attributes {{{.*}}, omp.is_device = true{{.*}}}
+!HOST: module attributes {{{.*}}, omp.is_device = false{{.*}}}
+subroutine omp_subroutine()
+end subroutine omp_subroutine
\ No newline at end of file
Index: flang/test/Driver/driver-help.f90
===
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -138,6 +138,7 @@
 ! HELP-FC1-NEXT: -fno-signed-zeros  Allow optimizations that ignore the sign of floating p

[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-03-01 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 501645.
agozillon added a comment.

- [Flang][mlir] Adding space at the end of the omp-is-device test file


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  flang/include/flang/Frontend/LangOptions.def
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Lower/OpenMP/omp-is-device.f90
  flang/tools/bbc/bbc.cpp
  mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
  mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp

Index: mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
===
--- mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -1417,6 +1417,26 @@
   return success();
 }
 
+//===--===//
+// OpenMPDialect helper functions
+//===--===//
+
+// Set the omp.is_device attribute on the module with the specified boolean
+void OpenMPDialect::setIsDevice(Operation* module, bool isDevice) {
+  module->setAttr(
+  mlir::StringAttr::get(module->getContext(), llvm::Twine{"omp.is_device"}),
+  mlir::BoolAttr::get(module->getContext(), isDevice));
+}
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+bool OpenMPDialect::getIsDevice(Operation* module) {
+  if (Attribute isDevice = module->getAttr("omp.is_device"))
+if (isDevice.isa())
+  return isDevice.dyn_cast().getValue();
+  return false;
+}
+
 #define GET_ATTRDEF_CLASSES
 #include "mlir/Dialect/OpenMP/OpenMPOpsAttributes.cpp.inc"
 
Index: mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
===
--- mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -28,6 +28,15 @@
   let cppNamespace = "::mlir::omp";
   let dependentDialects = ["::mlir::LLVM::LLVMDialect"];
   let useDefaultAttributePrinterParser = 1;
+
+  let extraClassDeclaration = [{
+// Set the omp.is_device attribute on the module with the specified boolean
+static void setIsDevice(Operation* module, bool isDevice);
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+static bool getIsDevice(Operation* module);
+  }];
 }
 
 // OmpCommon requires definition of OpenACC_Dialect.
Index: flang/tools/bbc/bbc.cpp
===
--- flang/tools/bbc/bbc.cpp
+++ flang/tools/bbc/bbc.cpp
@@ -38,6 +38,7 @@
 #include "flang/Semantics/semantics.h"
 #include "flang/Semantics/unparse-with-symbols.h"
 #include "flang/Version.inc"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/IR/AsmState.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "mlir/IR/MLIRContext.h"
@@ -122,6 +123,11 @@
 llvm::cl::desc("enable openmp"),
 llvm::cl::init(false));
 
+static llvm::cl::opt
+enableOpenMPDevice("fopenmp-is-device",
+   llvm::cl::desc("enable openmp device compilation"),
+   llvm::cl::init(false));
+
 static llvm::cl::opt enableOpenACC("fopenacc",
  llvm::cl::desc("enable openacc"),
  llvm::cl::init(false));
@@ -238,6 +244,8 @@
   kindMap, loweringOptions, {});
   burnside.lower(parseTree, semanticsContext);
   mlir::ModuleOp mlirModule = burnside.getModule();
+  if (enableOpenMP)
+mlir::omp::OpenMPDialect::setIsDevice(mlirModule, enableOpenMPDevice);
   std::error_code ec;
   std::string outputName = outputFilename;
   if (!outputName.size())
Index: flang/test/Lower/OpenMP/omp-is-device.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/omp-is-device.f90
@@ -0,0 +1,7 @@
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEVICE
+!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s --check-prefix=HOST
+
+!DEVICE: module attributes {{{.*}}, omp.is_device = true{{.*}}}
+!HOST: module attributes {{{.*}}, omp.is_device = false{{.*}}}
+subroutine omp_subroutine()
+end subroutine omp_subroutine
Index: flang/test/Driver/driver-help.f90
===
--- flang/test/Driver/driver-help.f90
+++ flang/test/Driver/driver-help.f90
@@ -138,6 +138,7 @@
 ! HELP-FC1-NEXT: -fno-signed-zeros  Allow optimizations that ignore the sign of floating point zeros
 ! HELP-FC1-NEXT: -fno

[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-03-01 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

After asking @jsjodin and @skatrak for some input on additional tests that 
could be added and having a look around the existing tests we couldn't really 
come up with any additional tests to add to the ones that are in the patch at 
the moment (inside of omp-is-device.f90 and driver-help.f90) unfortunately. 
Primarily as there isn't any use of this attribute yet, although there will be 
quite soon.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

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


[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-03-03 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 502134.
agozillon added a comment.

- [MLIR][OpenMP] Fix attribute helpers to apply to more than builtin.module
- [Flang][mlir] Adding space at the end of the omp-is-device test file
- [Clang][Flang][Driver][Test] Add flang-omp.f90 to test 
fopenmp/fopenmp-is-device for flang driver-mode
- [Flang][MLIR][OpenMP] Add additional tests to omp-is-device.f90 test


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  clang/test/Driver/flang/flang-omp.f90
  flang/include/flang/Frontend/LangOptions.def
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Lower/OpenMP/omp-is-device.f90
  flang/tools/bbc/bbc.cpp
  mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
  mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp

Index: mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
===
--- mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -1417,6 +1417,26 @@
   return success();
 }
 
+//===--===//
+// OpenMPDialect helper functions
+//===--===//
+
+// Set the omp.is_device attribute on the module with the specified boolean
+void OpenMPDialect::setIsDevice(Operation* module, bool isDevice) {
+  module->setAttr(
+  mlir::StringAttr::get(module->getContext(), llvm::Twine{"omp.is_device"}),
+  mlir::BoolAttr::get(module->getContext(), isDevice));
+}
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+bool OpenMPDialect::getIsDevice(Operation* module) {
+  if (Attribute isDevice = module->getAttr("omp.is_device"))
+if (isDevice.isa())
+  return isDevice.dyn_cast().getValue();
+  return false;
+}
+
 #define GET_ATTRDEF_CLASSES
 #include "mlir/Dialect/OpenMP/OpenMPOpsAttributes.cpp.inc"
 
Index: mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
===
--- mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -28,6 +28,15 @@
   let cppNamespace = "::mlir::omp";
   let dependentDialects = ["::mlir::LLVM::LLVMDialect"];
   let useDefaultAttributePrinterParser = 1;
+
+  let extraClassDeclaration = [{
+// Set the omp.is_device attribute on the module with the specified boolean
+static void setIsDevice(Operation* module, bool isDevice);
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+static bool getIsDevice(Operation* module);
+  }];
 }
 
 // OmpCommon requires definition of OpenACC_Dialect.
Index: flang/tools/bbc/bbc.cpp
===
--- flang/tools/bbc/bbc.cpp
+++ flang/tools/bbc/bbc.cpp
@@ -38,6 +38,7 @@
 #include "flang/Semantics/semantics.h"
 #include "flang/Semantics/unparse-with-symbols.h"
 #include "flang/Version.inc"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/IR/AsmState.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "mlir/IR/MLIRContext.h"
@@ -122,6 +123,11 @@
 llvm::cl::desc("enable openmp"),
 llvm::cl::init(false));
 
+static llvm::cl::opt
+enableOpenMPDevice("fopenmp-is-device",
+   llvm::cl::desc("enable openmp device compilation"),
+   llvm::cl::init(false));
+
 static llvm::cl::opt enableOpenACC("fopenacc",
  llvm::cl::desc("enable openacc"),
  llvm::cl::init(false));
@@ -237,6 +243,8 @@
   kindMap, loweringOptions, {});
   burnside.lower(parseTree, semanticsContext);
   mlir::ModuleOp mlirModule = burnside.getModule();
+  if (enableOpenMP)
+mlir::omp::OpenMPDialect::setIsDevice(mlirModule, enableOpenMPDevice);
   std::error_code ec;
   std::string outputName = outputFilename;
   if (!outputName.size())
Index: flang/test/Lower/OpenMP/omp-is-device.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/omp-is-device.f90
@@ -0,0 +1,19 @@
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEVICE
+!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s --check-prefix=HOST
+!RUN: %flang_fc1 -emit-fir -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEVICE-FLAG-ONLY
+!RUN: bbc -fopenmp -fopenmp-is-device -emit-fir -o - %s | FileCheck %s --check-prefix=BBC-DEVICE
+!RUN: bbc -fopenmp -emit-fir 

[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-03-03 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 502139.
agozillon added a comment.

- [Flang][MLIR][Test] Add newline at end of test file


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  clang/test/Driver/flang/flang-omp.f90
  flang/include/flang/Frontend/LangOptions.def
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Lower/OpenMP/omp-is-device.f90
  flang/tools/bbc/bbc.cpp
  mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
  mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp

Index: mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
===
--- mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -1417,6 +1417,26 @@
   return success();
 }
 
+//===--===//
+// OpenMPDialect helper functions
+//===--===//
+
+// Set the omp.is_device attribute on the module with the specified boolean
+void OpenMPDialect::setIsDevice(Operation* module, bool isDevice) {
+  module->setAttr(
+  mlir::StringAttr::get(module->getContext(), llvm::Twine{"omp.is_device"}),
+  mlir::BoolAttr::get(module->getContext(), isDevice));
+}
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+bool OpenMPDialect::getIsDevice(Operation* module) {
+  if (Attribute isDevice = module->getAttr("omp.is_device"))
+if (isDevice.isa())
+  return isDevice.dyn_cast().getValue();
+  return false;
+}
+
 #define GET_ATTRDEF_CLASSES
 #include "mlir/Dialect/OpenMP/OpenMPOpsAttributes.cpp.inc"
 
Index: mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
===
--- mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -28,6 +28,15 @@
   let cppNamespace = "::mlir::omp";
   let dependentDialects = ["::mlir::LLVM::LLVMDialect"];
   let useDefaultAttributePrinterParser = 1;
+
+  let extraClassDeclaration = [{
+// Set the omp.is_device attribute on the module with the specified boolean
+static void setIsDevice(Operation* module, bool isDevice);
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+static bool getIsDevice(Operation* module);
+  }];
 }
 
 // OmpCommon requires definition of OpenACC_Dialect.
Index: flang/tools/bbc/bbc.cpp
===
--- flang/tools/bbc/bbc.cpp
+++ flang/tools/bbc/bbc.cpp
@@ -38,6 +38,7 @@
 #include "flang/Semantics/semantics.h"
 #include "flang/Semantics/unparse-with-symbols.h"
 #include "flang/Version.inc"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/IR/AsmState.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "mlir/IR/MLIRContext.h"
@@ -122,6 +123,11 @@
 llvm::cl::desc("enable openmp"),
 llvm::cl::init(false));
 
+static llvm::cl::opt
+enableOpenMPDevice("fopenmp-is-device",
+   llvm::cl::desc("enable openmp device compilation"),
+   llvm::cl::init(false));
+
 static llvm::cl::opt enableOpenACC("fopenacc",
  llvm::cl::desc("enable openacc"),
  llvm::cl::init(false));
@@ -237,6 +243,8 @@
   kindMap, loweringOptions, {});
   burnside.lower(parseTree, semanticsContext);
   mlir::ModuleOp mlirModule = burnside.getModule();
+  if (enableOpenMP)
+mlir::omp::OpenMPDialect::setIsDevice(mlirModule, enableOpenMPDevice);
   std::error_code ec;
   std::string outputName = outputFilename;
   if (!outputName.size())
Index: flang/test/Lower/OpenMP/omp-is-device.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/omp-is-device.f90
@@ -0,0 +1,19 @@
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEVICE
+!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s --check-prefix=HOST
+!RUN: %flang_fc1 -emit-fir -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEVICE-FLAG-ONLY
+!RUN: bbc -fopenmp -fopenmp-is-device -emit-fir -o - %s | FileCheck %s --check-prefix=BBC-DEVICE
+!RUN: bbc -fopenmp -emit-fir -o - %s | FileCheck %s --check-prefix=BBC-HOST
+!RUN: bbc -fopenmp-is-device -emit-fir -o - %s | FileCheck %s --check-prefix=BBC-DEVICE-FLAG-ONLY
+
+!DEVICE: module attributes {{{.*}}, omp.is_device = true{{.*}}}
+!HOST: module attributes {{{.*}}, omp.is_device = false{

[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-03-03 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

The new update adds the suggested new tests! Thank you very much @awarzynski 
for suggesting them that was a great help. Hopefully the new additions cover 
what you have requested, if not I am of course happy to change or add more 
tests as needed.




Comment at: clang/lib/Driver/ToolChains/Flang.cpp:121-125
+  if (IsOpenMPDevice) {
+// -fopenmp-is-device is passed along to tell the frontend that it is
+// generating code for a device, so that only the relevant declarations are
+// emitted.
+CmdArgs.push_back("-fopenmp-is-device");

awarzynski wrote:
> Can you add a test for this section?
I have added a new test file flang-omp.f90 which aims to test that the 
appropriate flags are generated when openmp related flags are used it currently 
checks for -fopenmp/fopenmp-is-device



Comment at: flang/test/Lower/OpenMP/omp-is-device.f90:1
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s 
--check-prefix=DEVICE
+!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s --check-prefix=HOST

awarzynski wrote:
> What happens if `-fopenmp-is-device` is used without `-fopnemp`?
I think I now cover this in the test with the *-DEVICE-FLAG-ONLY tests, 
currently it is silently ignored and nothing is applied to the module.



Comment at: flang/tools/bbc/bbc.cpp:127
+static llvm::cl::opt
+enableOpenMPDevice("fopenmp-is-device",
+   llvm::cl::desc("enable openmp device compilation"),

awarzynski wrote:
> This option is not tested
I believe I now test this in the omp-is-device.f90 test!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

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


[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-03-03 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

Thank you very much @awarzynski I will wait for further approval!




Comment at: clang/test/Driver/flang/flang-omp.f90:1
+! Check that flang -fc1 is invoked when in --driver-mode=flang 
+! and the relevant openmp and openmp offload flags are utilised

awarzynski wrote:
> This test looks correct to me, but please note that:
> 
> ```
> ! Check that flang -fc1 is invoked when in --driver-mode=flang 
> ```
> 
> yet (`%clang` instead of `%flang`)
> 
> ```
> ! RUN: %clang --driver-mode=flang -### -fopenmp %s 2>&1 | FileCheck 
> --check-prefixes=CHECK-OPENMP %s
> ```
> 
> I'm not really sure whether we should be testing Flang-specific logic in 
> Clang. Having said that, Flang does use `clangDriver` to implement its driver 
> :) 
> 
> You could consider using 
> https://github.com/llvm/llvm-project/blob/main/flang/test/Driver/frontend-forwarding.f90
>  instead (or add an OpenMP specific file there).
> 
> Not a blocker.
Yes, I wasn't so sure either as it felt a little weird to test Flang components 
inside of Clang, but as you said it is a Clang toolchain (that this test is 
checking) and borrows from the clangDriver! 

I borrowed this test from other similar tests in the same folder that test 
other flang specific driver logic in a similar manner, but I am more than happy 
to add an additional flang specific driver test as you mention! 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

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


[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-03-03 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added inline comments.



Comment at: clang/test/Driver/flang/flang-omp.f90:1
+! Check that flang -fc1 is invoked when in --driver-mode=flang 
+! and the relevant openmp and openmp offload flags are utilised

agozillon wrote:
> awarzynski wrote:
> > This test looks correct to me, but please note that:
> > 
> > ```
> > ! Check that flang -fc1 is invoked when in --driver-mode=flang 
> > ```
> > 
> > yet (`%clang` instead of `%flang`)
> > 
> > ```
> > ! RUN: %clang --driver-mode=flang -### -fopenmp %s 2>&1 | FileCheck 
> > --check-prefixes=CHECK-OPENMP %s
> > ```
> > 
> > I'm not really sure whether we should be testing Flang-specific logic in 
> > Clang. Having said that, Flang does use `clangDriver` to implement its 
> > driver :) 
> > 
> > You could consider using 
> > https://github.com/llvm/llvm-project/blob/main/flang/test/Driver/frontend-forwarding.f90
> >  instead (or add an OpenMP specific file there).
> > 
> > Not a blocker.
> Yes, I wasn't so sure either as it felt a little weird to test Flang 
> components inside of Clang, but as you said it is a Clang toolchain (that 
> this test is checking) and borrows from the clangDriver! 
> 
> I borrowed this test from other similar tests in the same folder that test 
> other flang specific driver logic in a similar manner, but I am more than 
> happy to add an additional flang specific driver test as you mention! 
On further looking into the frontend-forwarding test, I don't know if it is 
suitable for fopenmp-is-device as it is an fc1 option and won't be forwarded 
from the flang-new frontend down to fc1 at the moment! 

I think this test will be more suitable when additional flags like the 
fopenmp-targets (or similar flags) that are used in this test are added to the 
Flang driver. As they spawn/propagate the openmp-is-device flag. However, 
perhaps I am incorrect.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

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


[PATCH] D145264: [OpenMP][MLIR] Lower and apply RTLModuleFlagsAttr

2023-03-03 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon created this revision.
Herald added subscribers: Moerafaat, zero9178, bzcheeseman, awarzynski, 
sdasgup3, wenzhicui, wrengr, cota, teijeong, rdzhabarov, tatianashp, msifontes, 
jurahul, Kayjukh, grosul1, Joonsoo, liufengdb, aartbik, mgester, arpith-jacob, 
antiagainst, shauheen, rriddle, mehdi_amini, thopre, guansong, yaxunl.
Herald added a reviewer: sscalpone.
Herald added a reviewer: ftynse.
Herald added a reviewer: awarzynski.
Herald added projects: Flang, All.
agozillon requested review of this revision.
Herald added subscribers: cfe-commits, sstefan1, stephenneuendorffer, 
nicolasvasilache, jdoerfert, MaskRay.
Herald added a reviewer: jdoerfert.
Herald added a reviewer: nicolasvasilache.
Herald added projects: clang, MLIR.

This patch adds the lowering for the RTLModuleFlagsAttr to LLVM-IR,
which lowers each individual arguent of the attribute to globals. AS well
as the application of it by flags in the frontend which have been activated
for flang fc1.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145264

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  clang/test/Driver/flang/flang-omp.f90
  flang/include/flang/Frontend/LangOptions.def
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Lower/OpenMP/rtl-flags.f90
  mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Index: mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -1351,6 +1351,40 @@
   return success();
 }
 
+/// Lowers the RTLModuleFlagsAttr which is applied to the module on the device
+/// pass when offloading, this attribute contains OpenMP RTL globals that can
+/// be passed as flags to the frontend, otherwise they are set to default
+LogicalResult
+convertRTLModuleFlagsAttr(Operation *op,
+  mlir::omp::RTLModuleFlagsAttr attribute,
+  LLVM::ModuleTranslation &moduleTranslation) {
+  llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
+
+  ompBuilder->createGlobalFlag(
+  attribute.getDebugKind() /*LangOpts().OpenMPTargetDebug*/,
+  "__omp_rtl_debug_kind");
+  ompBuilder->createGlobalFlag(
+  attribute
+  .getAssumeTeamsOversubscription() /*LangOpts().OpenMPTeamSubscription*/
+  ,
+  "__omp_rtl_assume_teams_oversubscription");
+  ompBuilder->createGlobalFlag(
+  attribute
+  .getAssumeThreadsOversubscription() /*LangOpts().OpenMPThreadSubscription*/
+  ,
+  "__omp_rtl_assume_threads_oversubscription");
+  ompBuilder->createGlobalFlag(
+  attribute.getAssumeNoThreadState() /*LangOpts().OpenMPNoThreadState*/,
+  "__omp_rtl_assume_no_thread_state");
+  ompBuilder->createGlobalFlag(
+  attribute
+  .getAssumeNoNestedParallelism() /*LangOpts().OpenMPNoNestedParallelism*/
+  ,
+  "__omp_rtl_assume_no_nested_parallelism");
+
+  return success();
+}
+
 namespace {
 
 /// Implementation of the dialect interface that converts operations belonging
@@ -1365,10 +1399,34 @@
   LogicalResult
   convertOperation(Operation *op, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) const final;
+
+  LogicalResult
+  amendOperation(Operation *op, NamedAttribute attribute,
+ LLVM::ModuleTranslation &moduleTranslation) const final;
 };
 
 } // namespace
 
+/// Given an OpenMP MLIR attribute, create the corresponding LLVM-IR, runtime
+/// calls, or operation amendments
+LogicalResult OpenMPDialectLLVMIRTranslationInterface::amendOperation(
+Operation *op, NamedAttribute attribute,
+LLVM::ModuleTranslation &moduleTranslation) const {
+
+  return llvm::TypeSwitch(attribute.getValue())
+  .Case([&](mlir::omp::RTLModuleFlagsAttr rtlAttr) {
+return convertRTLModuleFlagsAttr(op, rtlAttr, moduleTranslation);
+  })
+  .Default([&](Attribute attr) {
+// fall through for omp attributes that do not require lowering and/or
+// have no concrete definition and thus no type to define a case on
+// e.g. omp.is_device
+return success();
+  });
+
+  return failure();
+}
+
 /// Given an OpenMP MLIR operation, create the corresponding LLVM IR
 /// (including OpenMP runtime calls).
 LogicalResult OpenMPDialectLLVMIRTranslationInterface::convertOperation(
Index: flang/test/Lower/OpenMP/rtl-flags.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/rtl-flags.f90
@@ -0,0 +1,76 @@
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEFAULT-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp %s

[PATCH] D145264: [OpenMP][MLIR] Lower and apply RTLModuleFlagsAttr

2023-03-03 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

This builds on the following phabricator patches that are in review at the 
moment, so there are components that are used here that are added from these:

https://reviews.llvm.org/D144864 : add fopenmp-is-device and an mlir is-device 
attribute for the OpenMP dialect
https://reviews.llvm.org/D144896 : the RTLModuleFlagsAttr that is in use within 
this patch for lowering and application
https://reviews.llvm.org/D144883 : an infrastructure addition for the MLIR LLVM 
translation interface

For the moment, this patch is primarily to give context to the usage of the 
RTLModuleFlagsAttr mlir attribute and give some further understanding for a 
reviewer, however, please feel free to review it if you have time as I will 
eventually ask for some revision of it after all the components it depends on 
have been progressed.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145264

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


[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-03-06 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added inline comments.



Comment at: clang/test/Driver/flang/flang-omp.f90:1
+! Check that flang -fc1 is invoked when in --driver-mode=flang 
+! and the relevant openmp and openmp offload flags are utilised

awarzynski wrote:
> agozillon wrote:
> > agozillon wrote:
> > > awarzynski wrote:
> > > > This test looks correct to me, but please note that:
> > > > 
> > > > ```
> > > > ! Check that flang -fc1 is invoked when in --driver-mode=flang 
> > > > ```
> > > > 
> > > > yet (`%clang` instead of `%flang`)
> > > > 
> > > > ```
> > > > ! RUN: %clang --driver-mode=flang -### -fopenmp %s 2>&1 | FileCheck 
> > > > --check-prefixes=CHECK-OPENMP %s
> > > > ```
> > > > 
> > > > I'm not really sure whether we should be testing Flang-specific logic 
> > > > in Clang. Having said that, Flang does use `clangDriver` to implement 
> > > > its driver :) 
> > > > 
> > > > You could consider using 
> > > > https://github.com/llvm/llvm-project/blob/main/flang/test/Driver/frontend-forwarding.f90
> > > >  instead (or add an OpenMP specific file there).
> > > > 
> > > > Not a blocker.
> > > Yes, I wasn't so sure either as it felt a little weird to test Flang 
> > > components inside of Clang, but as you said it is a Clang toolchain (that 
> > > this test is checking) and borrows from the clangDriver! 
> > > 
> > > I borrowed this test from other similar tests in the same folder that 
> > > test other flang specific driver logic in a similar manner, but I am more 
> > > than happy to add an additional flang specific driver test as you 
> > > mention! 
> > On further looking into the frontend-forwarding test, I don't know if it is 
> > suitable for fopenmp-is-device as it is an fc1 option and won't be 
> > forwarded from the flang-new frontend down to fc1 at the moment! 
> > 
> > I think this test will be more suitable when additional flags like the 
> > fopenmp-targets (or similar flags) that are used in this test are added to 
> > the Flang driver. As they spawn/propagate the openmp-is-device flag. 
> > However, perhaps I am incorrect.
> > On further looking into the frontend-forwarding test, I don't know if it is 
> > suitable for fopenmp-is-device as it is an fc1 option and won't be 
> > forwarded from the flang-new frontend down to fc1 at the moment!
> 
> It should be - that's what the logic in Flang.cpp does, no? And if it 
> doesn't, is it a deliberate design decision?
I believe the logic in Flang.h/Flang.cpp just invokes for -fc1 not the Frontend 
driver, at least that's what it looks like from the ConstructJob task that 
Clang invokes. 

It's a deliberate design decision that -fopenmp-is-device is an fc1 option, it 
mimics the way Clang currently does it, which is a cc1 option, there is no way 
to directly specify it to clang without -cc1 I think. The hope is to keep the 
Flang OpenMP flags as similar to Clang's as possible for the time being I 
believe.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

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


[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-03-06 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added inline comments.



Comment at: clang/test/Driver/flang/flang-omp.f90:1
+! Check that flang -fc1 is invoked when in --driver-mode=flang 
+! and the relevant openmp and openmp offload flags are utilised

awarzynski wrote:
> agozillon wrote:
> > awarzynski wrote:
> > > agozillon wrote:
> > > > agozillon wrote:
> > > > > awarzynski wrote:
> > > > > > This test looks correct to me, but please note that:
> > > > > > 
> > > > > > ```
> > > > > > ! Check that flang -fc1 is invoked when in --driver-mode=flang 
> > > > > > ```
> > > > > > 
> > > > > > yet (`%clang` instead of `%flang`)
> > > > > > 
> > > > > > ```
> > > > > > ! RUN: %clang --driver-mode=flang -### -fopenmp %s 2>&1 | FileCheck 
> > > > > > --check-prefixes=CHECK-OPENMP %s
> > > > > > ```
> > > > > > 
> > > > > > I'm not really sure whether we should be testing Flang-specific 
> > > > > > logic in Clang. Having said that, Flang does use `clangDriver` to 
> > > > > > implement its driver :) 
> > > > > > 
> > > > > > You could consider using 
> > > > > > https://github.com/llvm/llvm-project/blob/main/flang/test/Driver/frontend-forwarding.f90
> > > > > >  instead (or add an OpenMP specific file there).
> > > > > > 
> > > > > > Not a blocker.
> > > > > Yes, I wasn't so sure either as it felt a little weird to test Flang 
> > > > > components inside of Clang, but as you said it is a Clang toolchain 
> > > > > (that this test is checking) and borrows from the clangDriver! 
> > > > > 
> > > > > I borrowed this test from other similar tests in the same folder that 
> > > > > test other flang specific driver logic in a similar manner, but I am 
> > > > > more than happy to add an additional flang specific driver test as 
> > > > > you mention! 
> > > > On further looking into the frontend-forwarding test, I don't know if 
> > > > it is suitable for fopenmp-is-device as it is an fc1 option and won't 
> > > > be forwarded from the flang-new frontend down to fc1 at the moment! 
> > > > 
> > > > I think this test will be more suitable when additional flags like the 
> > > > fopenmp-targets (or similar flags) that are used in this test are added 
> > > > to the Flang driver. As they spawn/propagate the openmp-is-device flag. 
> > > > However, perhaps I am incorrect.
> > > > On further looking into the frontend-forwarding test, I don't know if 
> > > > it is suitable for fopenmp-is-device as it is an fc1 option and won't 
> > > > be forwarded from the flang-new frontend down to fc1 at the moment!
> > > 
> > > It should be - that's what the logic in Flang.cpp does, no? And if it 
> > > doesn't, is it a deliberate design decision?
> > I believe the logic in Flang.h/Flang.cpp just invokes for -fc1 not the 
> > Frontend driver, at least that's what it looks like from the ConstructJob 
> > task that Clang invokes. 
> > 
> > It's a deliberate design decision that -fopenmp-is-device is an fc1 option, 
> > it mimics the way Clang currently does it, which is a cc1 option, there is 
> > no way to directly specify it to clang without -cc1 I think. The hope is to 
> > keep the Flang OpenMP flags as similar to Clang's as possible for the time 
> > being I believe.
> > I believe the logic in Flang.h/Flang.cpp just invokes for -fc1 not the 
> > Frontend driver.
> 
> `flang-new -fc1` **is** the frontend driver ;-) So:
> * you've added the  logic to forward `-fopenmp-is-device` that should work 
> for both `flang-new` and `clang --driver-mode=flang`, but
> * you're only testing one of these.
> 
> There should have tests for both. In particular for `flang-new` given that 
> this patch adds new functionality to LLVM Flang. If that doesn't work, let's 
> try to figure it out together.
> 
> > The hope is to keep the Flang OpenMP flags as similar to Clang's as 
> > possible for the time being I believe.
> 
> +1 We should try as much as we can to keep Clang's and Flang's behavior 
> consistent :) (which you do 👍🏻 )
Ah I didn't realise that, thank you very much for clarifying that for me! :) I 
thought it was bypassing it with -fc1, and yjsy flang-new without it was the 
frontend! I still have a lot to learn, so I apologies for the misunderstanding! 

In this case do you mean test with "flang-new -fc1 -fopenmp-is-device" or 
"flang-new -fopenmp-is-device", as the latter will not be accepted by flang-new 
in the current patch but I do test the -fc1 variation in the omp-is-device.f90 
test which is more about testing the attribute admittedly as opposed to the 
driver command. 

I don't know if testing it in the frontend-forwarding.f90 test is the right 
location in this case as it doesn't test with -fc1, it seems to test options 
are forwarded to -fc1 correctly, I could be incorrect though. In future passing 
an argument like -fopenmp-targets=amdgcn-amd-amdhsa to flang-new would likely 
expand into -fopenmp-is-device (and other arguments), like it does for Clang at 
the moment, so this is likely the option that would be tested inside of 
front

[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-03-07 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added inline comments.



Comment at: clang/test/Driver/flang/flang-omp.f90:1
+! Check that flang -fc1 is invoked when in --driver-mode=flang 
+! and the relevant openmp and openmp offload flags are utilised

awarzynski wrote:
> agozillon wrote:
> > awarzynski wrote:
> > > agozillon wrote:
> > > > awarzynski wrote:
> > > > > agozillon wrote:
> > > > > > agozillon wrote:
> > > > > > > awarzynski wrote:
> > > > > > > > This test looks correct to me, but please note that:
> > > > > > > > 
> > > > > > > > ```
> > > > > > > > ! Check that flang -fc1 is invoked when in --driver-mode=flang 
> > > > > > > > ```
> > > > > > > > 
> > > > > > > > yet (`%clang` instead of `%flang`)
> > > > > > > > 
> > > > > > > > ```
> > > > > > > > ! RUN: %clang --driver-mode=flang -### -fopenmp %s 2>&1 | 
> > > > > > > > FileCheck --check-prefixes=CHECK-OPENMP %s
> > > > > > > > ```
> > > > > > > > 
> > > > > > > > I'm not really sure whether we should be testing Flang-specific 
> > > > > > > > logic in Clang. Having said that, Flang does use `clangDriver` 
> > > > > > > > to implement its driver :) 
> > > > > > > > 
> > > > > > > > You could consider using 
> > > > > > > > https://github.com/llvm/llvm-project/blob/main/flang/test/Driver/frontend-forwarding.f90
> > > > > > > >  instead (or add an OpenMP specific file there).
> > > > > > > > 
> > > > > > > > Not a blocker.
> > > > > > > Yes, I wasn't so sure either as it felt a little weird to test 
> > > > > > > Flang components inside of Clang, but as you said it is a Clang 
> > > > > > > toolchain (that this test is checking) and borrows from the 
> > > > > > > clangDriver! 
> > > > > > > 
> > > > > > > I borrowed this test from other similar tests in the same folder 
> > > > > > > that test other flang specific driver logic in a similar manner, 
> > > > > > > but I am more than happy to add an additional flang specific 
> > > > > > > driver test as you mention! 
> > > > > > On further looking into the frontend-forwarding test, I don't know 
> > > > > > if it is suitable for fopenmp-is-device as it is an fc1 option and 
> > > > > > won't be forwarded from the flang-new frontend down to fc1 at the 
> > > > > > moment! 
> > > > > > 
> > > > > > I think this test will be more suitable when additional flags like 
> > > > > > the fopenmp-targets (or similar flags) that are used in this test 
> > > > > > are added to the Flang driver. As they spawn/propagate the 
> > > > > > openmp-is-device flag. However, perhaps I am incorrect.
> > > > > > On further looking into the frontend-forwarding test, I don't know 
> > > > > > if it is suitable for fopenmp-is-device as it is an fc1 option and 
> > > > > > won't be forwarded from the flang-new frontend down to fc1 at the 
> > > > > > moment!
> > > > > 
> > > > > It should be - that's what the logic in Flang.cpp does, no? And if it 
> > > > > doesn't, is it a deliberate design decision?
> > > > I believe the logic in Flang.h/Flang.cpp just invokes for -fc1 not the 
> > > > Frontend driver, at least that's what it looks like from the 
> > > > ConstructJob task that Clang invokes. 
> > > > 
> > > > It's a deliberate design decision that -fopenmp-is-device is an fc1 
> > > > option, it mimics the way Clang currently does it, which is a cc1 
> > > > option, there is no way to directly specify it to clang without -cc1 I 
> > > > think. The hope is to keep the Flang OpenMP flags as similar to Clang's 
> > > > as possible for the time being I believe.
> > > > I believe the logic in Flang.h/Flang.cpp just invokes for -fc1 not the 
> > > > Frontend driver.
> > > 
> > > `flang-new -fc1` **is** the frontend driver ;-) So:
> > > * you've added the  logic to forward `-fopenmp-is-device` that should 
> > > work for both `flang-new` and `clang --driver-mode=flang`, but
> > > * you're only testing one of these.
> > > 
> > > There should have tests for both. In particular for `flang-new` given 
> > > that this patch adds new functionality to LLVM Flang. If that doesn't 
> > > work, let's try to figure it out together.
> > > 
> > > > The hope is to keep the Flang OpenMP flags as similar to Clang's as 
> > > > possible for the time being I believe.
> > > 
> > > +1 We should try as much as we can to keep Clang's and Flang's behavior 
> > > consistent :) (which you do 👍🏻 )
> > Ah I didn't realise that, thank you very much for clarifying that for me! 
> > :) I thought it was bypassing it with -fc1, and yjsy flang-new without it 
> > was the frontend! I still have a lot to learn, so I apologies for the 
> > misunderstanding! 
> > 
> > In this case do you mean test with "flang-new -fc1 -fopenmp-is-device" or 
> > "flang-new -fopenmp-is-device", as the latter will not be accepted by 
> > flang-new in the current patch but I do test the -fc1 variation in the 
> > omp-is-device.f90 test which is more about testing the attribute admittedly 
> > as opposed to the driver command. 
> > 
> > I don't know if testing it in the frontend-

[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-03-07 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 503008.
agozillon added a comment.

- [Flang][ToolChain][OpenMP][Driver] Reduce changes to only add -fc1 support


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

Files:
  clang/include/clang/Driver/Options.td
  flang/include/flang/Frontend/LangOptions.def
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Lower/OpenMP/omp-is-device.f90
  flang/tools/bbc/bbc.cpp
  mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
  mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp

Index: mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
===
--- mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -1417,6 +1417,26 @@
   return success();
 }
 
+//===--===//
+// OpenMPDialect helper functions
+//===--===//
+
+// Set the omp.is_device attribute on the module with the specified boolean
+void OpenMPDialect::setIsDevice(Operation* module, bool isDevice) {
+  module->setAttr(
+  mlir::StringAttr::get(module->getContext(), llvm::Twine{"omp.is_device"}),
+  mlir::BoolAttr::get(module->getContext(), isDevice));
+}
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+bool OpenMPDialect::getIsDevice(Operation* module) {
+  if (Attribute isDevice = module->getAttr("omp.is_device"))
+if (isDevice.isa())
+  return isDevice.dyn_cast().getValue();
+  return false;
+}
+
 #define GET_ATTRDEF_CLASSES
 #include "mlir/Dialect/OpenMP/OpenMPOpsAttributes.cpp.inc"
 
Index: mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
===
--- mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -28,6 +28,15 @@
   let cppNamespace = "::mlir::omp";
   let dependentDialects = ["::mlir::LLVM::LLVMDialect"];
   let useDefaultAttributePrinterParser = 1;
+
+  let extraClassDeclaration = [{
+// Set the omp.is_device attribute on the module with the specified boolean
+static void setIsDevice(Operation* module, bool isDevice);
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+static bool getIsDevice(Operation* module);
+  }];
 }
 
 // OmpCommon requires definition of OpenACC_Dialect.
Index: flang/tools/bbc/bbc.cpp
===
--- flang/tools/bbc/bbc.cpp
+++ flang/tools/bbc/bbc.cpp
@@ -38,6 +38,7 @@
 #include "flang/Semantics/semantics.h"
 #include "flang/Semantics/unparse-with-symbols.h"
 #include "flang/Version.inc"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/IR/AsmState.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "mlir/IR/MLIRContext.h"
@@ -122,6 +123,11 @@
 llvm::cl::desc("enable openmp"),
 llvm::cl::init(false));
 
+static llvm::cl::opt
+enableOpenMPDevice("fopenmp-is-device",
+   llvm::cl::desc("enable openmp device compilation"),
+   llvm::cl::init(false));
+
 static llvm::cl::opt enableOpenACC("fopenacc",
  llvm::cl::desc("enable openacc"),
  llvm::cl::init(false));
@@ -237,6 +243,8 @@
   kindMap, loweringOptions, {});
   burnside.lower(parseTree, semanticsContext);
   mlir::ModuleOp mlirModule = burnside.getModule();
+  if (enableOpenMP)
+mlir::omp::OpenMPDialect::setIsDevice(mlirModule, enableOpenMPDevice);
   std::error_code ec;
   std::string outputName = outputFilename;
   if (!outputName.size())
Index: flang/test/Lower/OpenMP/omp-is-device.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/omp-is-device.f90
@@ -0,0 +1,19 @@
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEVICE
+!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s --check-prefix=HOST
+!RUN: %flang_fc1 -emit-fir -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEVICE-FLAG-ONLY
+!RUN: bbc -fopenmp -fopenmp-is-device -emit-fir -o - %s | FileCheck %s --check-prefix=BBC-DEVICE
+!RUN: bbc -fopenmp -emit-fir -o - %s | FileCheck %s --check-prefix=BBC-HOST
+!RUN: bbc -fopenmp-is-device -emit-fir -o - %s | FileCheck %s --check-prefix=BBC-DEVICE-FLAG-ONLY
+
+!DEVICE: module attributes {{{.*}}, omp.is_device = true{{.*}}}
+!HOST: module attributes {{{.*}}, omp.is_device = false{{.*}}}
+!DEVICE-FLAG-ONLY: module attributes {{{.*}}"
+!DEVICE-FLAG-ONLY-NOT: , omp.is_device =

[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-03-07 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

The recent update removed the added offload related code from Flang.h/.cpp and 
removed the related test.

Would it be possible to have an extra sign-off as requested by @awarzynski (or 
more review points to correct/discuss if we aren't happy with the 
state/direction of the patch) from either @kiranchandramohan or @jdoerfert my 
apologies for the pings!

And then provided @awarzynski is happy with the current state of the patch 
after this recent commit and no further comments I can commit and close the 
patch :)

Thank you very much for all the excellent comments in this review, and the 
attention given to it, I appreciate it greatly.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

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


[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-03-07 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

In D144864#4175257 , 
@kiranchandramohan wrote:

> LG. See one minor comment in the tests.
>
> I would prefer having an Interface for Target Modules if that could be made 
> to work. I guess this can be taken up separately after 
> https://reviews.llvm.org/D144883.

Thank you Kiran, by interface for Target Modules do you mean a new omp.module 
or this type of external interface: 
https://mlir.llvm.org/docs/Interfaces/#external-models-for-attribute-operation-and-type-interfaces

And quite possibly, @skatrak is currently looking into the review comments in 
the referenced patch (as he found similar use for the patch), so we shall see 
where the results lead to. Never opposed to improving on the infrastructure and 
this is something we will iterate on as we progress with offloading! :)




Comment at: flang/test/Lower/OpenMP/omp-is-device.f90:8-17
+!DEVICE: module attributes {{{.*}}, omp.is_device = true{{.*}}}
+!HOST: module attributes {{{.*}}, omp.is_device = false{{.*}}}
+!DEVICE-FLAG-ONLY: module attributes {{{.*}}"
+!DEVICE-FLAG-ONLY-NOT: , omp.is_device = {{.*}}
+!DEVICE-FLAG-ONLY-SAME: } 
+!BBC-DEVICE: module attributes {{{.*}}, omp.is_device = true{{.*}}}
+!BBC-HOST: module attributes {{{.*}}, omp.is_device = false{{.*}}}

kiranchandramohan wrote:
> Any reason to have separate checks for essentially the same (e.g: DEVICE vs 
> BBC-DEVICE)?
In this case to test the addition of the flag to bbc and also to the Flang 
driver, and testing the flag has the same behavior in both. There is perhaps a 
way to make the test simpler that I'm unaware of though? e.g. a way to merge 
DEVICE/BBC-DEVICE into one check


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

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


[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-03-07 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 503070.
agozillon added a comment.

- [Flang][Driver] Tidy up omp-is-device.f90 test


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

Files:
  clang/include/clang/Driver/Options.td
  flang/include/flang/Frontend/LangOptions.def
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Lower/OpenMP/omp-is-device.f90
  flang/tools/bbc/bbc.cpp
  mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
  mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp

Index: mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
===
--- mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -1417,6 +1417,26 @@
   return success();
 }
 
+//===--===//
+// OpenMPDialect helper functions
+//===--===//
+
+// Set the omp.is_device attribute on the module with the specified boolean
+void OpenMPDialect::setIsDevice(Operation* module, bool isDevice) {
+  module->setAttr(
+  mlir::StringAttr::get(module->getContext(), llvm::Twine{"omp.is_device"}),
+  mlir::BoolAttr::get(module->getContext(), isDevice));
+}
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+bool OpenMPDialect::getIsDevice(Operation* module) {
+  if (Attribute isDevice = module->getAttr("omp.is_device"))
+if (isDevice.isa())
+  return isDevice.dyn_cast().getValue();
+  return false;
+}
+
 #define GET_ATTRDEF_CLASSES
 #include "mlir/Dialect/OpenMP/OpenMPOpsAttributes.cpp.inc"
 
Index: mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
===
--- mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -28,6 +28,15 @@
   let cppNamespace = "::mlir::omp";
   let dependentDialects = ["::mlir::LLVM::LLVMDialect"];
   let useDefaultAttributePrinterParser = 1;
+
+  let extraClassDeclaration = [{
+// Set the omp.is_device attribute on the module with the specified boolean
+static void setIsDevice(Operation* module, bool isDevice);
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+static bool getIsDevice(Operation* module);
+  }];
 }
 
 // OmpCommon requires definition of OpenACC_Dialect.
Index: flang/tools/bbc/bbc.cpp
===
--- flang/tools/bbc/bbc.cpp
+++ flang/tools/bbc/bbc.cpp
@@ -38,6 +38,7 @@
 #include "flang/Semantics/semantics.h"
 #include "flang/Semantics/unparse-with-symbols.h"
 #include "flang/Version.inc"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/IR/AsmState.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "mlir/IR/MLIRContext.h"
@@ -122,6 +123,11 @@
 llvm::cl::desc("enable openmp"),
 llvm::cl::init(false));
 
+static llvm::cl::opt
+enableOpenMPDevice("fopenmp-is-device",
+   llvm::cl::desc("enable openmp device compilation"),
+   llvm::cl::init(false));
+
 static llvm::cl::opt enableOpenACC("fopenacc",
  llvm::cl::desc("enable openacc"),
  llvm::cl::init(false));
@@ -237,6 +243,8 @@
   kindMap, loweringOptions, {});
   burnside.lower(parseTree, semanticsContext);
   mlir::ModuleOp mlirModule = burnside.getModule();
+  if (enableOpenMP)
+mlir::omp::OpenMPDialect::setIsDevice(mlirModule, enableOpenMPDevice);
   std::error_code ec;
   std::string outputName = outputFilename;
   if (!outputName.size())
Index: flang/test/Lower/OpenMP/omp-is-device.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/omp-is-device.f90
@@ -0,0 +1,14 @@
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEVICE
+!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s --check-prefix=HOST
+!RUN: %flang_fc1 -emit-fir -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEVICE-FLAG-ONLY
+!RUN: bbc -fopenmp -fopenmp-is-device -emit-fir -o - %s | FileCheck %s --check-prefix=DEVICE
+!RUN: bbc -fopenmp -emit-fir -o - %s | FileCheck %s --check-prefix=HOST
+!RUN: bbc -fopenmp-is-device -emit-fir -o - %s | FileCheck %s --check-prefix=DEVICE-FLAG-ONLY
+
+!DEVICE: module attributes {{{.*}}, omp.is_device = true{{.*}}}
+!HOST: module attributes {{{.*}}, omp.is_device = false{{.*}}}
+!DEVICE-FLAG-ONLY: module attributes {{{.*}}"
+!DEVICE-FLAG-ONLY-NOT: , omp.is_device = {{.*}}
+!DEVICE-FLAG-ONLY-SAME: }
+subr

[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-03-07 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

Cleaned up the test, thank you both for teaching me more about the compiler and 
test infrastructure! If you're happy with the test changes @kiranchandramohan 
I'll commit the changes upstream to close out the review?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

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


[PATCH] D144864: [Flang][Driver][MLIR] Add -fopenmp-is-device to Flang and link to an omp.is_device attribute

2023-03-07 Thread Andrew Gozillon via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGe002a38b20e3: [Flang][OpenMP][MLIR][Driver][bbc] Add 
-fopenmp-is-device flag to Flang -fc1 &… (authored by agozillon).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D144864

Files:
  clang/include/clang/Driver/Options.td
  flang/include/flang/Frontend/LangOptions.def
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Lower/OpenMP/omp-is-device.f90
  flang/tools/bbc/bbc.cpp
  mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
  mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp

Index: mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
===
--- mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -1417,6 +1417,26 @@
   return success();
 }
 
+//===--===//
+// OpenMPDialect helper functions
+//===--===//
+
+// Set the omp.is_device attribute on the module with the specified boolean
+void OpenMPDialect::setIsDevice(Operation* module, bool isDevice) {
+  module->setAttr(
+  mlir::StringAttr::get(module->getContext(), llvm::Twine{"omp.is_device"}),
+  mlir::BoolAttr::get(module->getContext(), isDevice));
+}
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+bool OpenMPDialect::getIsDevice(Operation* module) {
+  if (Attribute isDevice = module->getAttr("omp.is_device"))
+if (isDevice.isa())
+  return isDevice.dyn_cast().getValue();
+  return false;
+}
+
 #define GET_ATTRDEF_CLASSES
 #include "mlir/Dialect/OpenMP/OpenMPOpsAttributes.cpp.inc"
 
Index: mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
===
--- mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
+++ mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td
@@ -28,6 +28,15 @@
   let cppNamespace = "::mlir::omp";
   let dependentDialects = ["::mlir::LLVM::LLVMDialect"];
   let useDefaultAttributePrinterParser = 1;
+
+  let extraClassDeclaration = [{
+// Set the omp.is_device attribute on the module with the specified boolean
+static void setIsDevice(Operation* module, bool isDevice);
+
+// Return the value of the omp.is_device attribute stored in the module if it
+// exists, otherwise return false by default
+static bool getIsDevice(Operation* module);
+  }];
 }
 
 // OmpCommon requires definition of OpenACC_Dialect.
Index: flang/tools/bbc/bbc.cpp
===
--- flang/tools/bbc/bbc.cpp
+++ flang/tools/bbc/bbc.cpp
@@ -38,6 +38,7 @@
 #include "flang/Semantics/semantics.h"
 #include "flang/Semantics/unparse-with-symbols.h"
 #include "flang/Version.inc"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/IR/AsmState.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "mlir/IR/MLIRContext.h"
@@ -122,6 +123,11 @@
 llvm::cl::desc("enable openmp"),
 llvm::cl::init(false));
 
+static llvm::cl::opt
+enableOpenMPDevice("fopenmp-is-device",
+   llvm::cl::desc("enable openmp device compilation"),
+   llvm::cl::init(false));
+
 static llvm::cl::opt enableOpenACC("fopenacc",
  llvm::cl::desc("enable openacc"),
  llvm::cl::init(false));
@@ -237,6 +243,8 @@
   kindMap, loweringOptions, {});
   burnside.lower(parseTree, semanticsContext);
   mlir::ModuleOp mlirModule = burnside.getModule();
+  if (enableOpenMP)
+mlir::omp::OpenMPDialect::setIsDevice(mlirModule, enableOpenMPDevice);
   std::error_code ec;
   std::string outputName = outputFilename;
   if (!outputName.size())
Index: flang/test/Lower/OpenMP/omp-is-device.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/omp-is-device.f90
@@ -0,0 +1,14 @@
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEVICE
+!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s --check-prefix=HOST
+!RUN: %flang_fc1 -emit-fir -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEVICE-FLAG-ONLY
+!RUN: bbc -fopenmp -fopenmp-is-device -emit-fir -o - %s | FileCheck %s --check-prefix=DEVICE
+!RUN: bbc -fopenmp -emit-fir -o - %s | FileCheck %s --check-prefix=HOST
+!RUN: bbc -fopenmp-is-device -emit-fir -o - %s | FileCheck %s --check-prefix=DEVICE-FLAG-ONLY
+
+!DEVICE: module attributes {{{.*}}, omp.is_device = true{{.*}}}
+!HOST: module attributes {{{.*}}, omp.is_device 

[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-10 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon created this revision.
Herald added subscribers: sunshaoce, guansong, yaxunl.
Herald added a reviewer: sscalpone.
Herald added a reviewer: awarzynski.
Herald added projects: Flang, All.
agozillon requested review of this revision.
Herald added subscribers: cfe-commits, sstefan1, jdoerfert, MaskRay.
Herald added a reviewer: jdoerfert.
Herald added a project: clang.

This allows-fembed-offload-object's and -fopenmp-is-device 
compiler invocation arguments to be passed to the Flang frontend 
during split compilation when offloading in OpenMP.

An example use case is when passing an offload-arch alongside 
-fopenmp to embed device objects compiled for the offload-arch 
within the host architecture.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145815

Files:
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  clang/test/Driver/flang/flang-omp.f90
  flang/test/Driver/omp-frontend-forwarding.f90

Index: flang/test/Driver/omp-frontend-forwarding.f90
===
--- /dev/null
+++ flang/test/Driver/omp-frontend-forwarding.f90
@@ -0,0 +1,17 @@
+! REQUIRES: amdgpu-registered-target
+
+! Test that flang-new OpenMP and OpenMP offload related 
+! commands forward or expand to the appropriate commands 
+! for flang-new -fc1 as expected.
+
+! Testing fembed-offload-object and fopenmp-is-device
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s
+! CHECK: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK: "{{[^"]*}}clang-offload-packager" {{.*}} "--image=file={{.*}}gfx90a.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp"
+! CHECK-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.bc"
+! CHECK: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fembed-offload-object={{.*}}.out" {{.*}}.bc"
Index: clang/test/Driver/flang/flang-omp.f90
===
--- /dev/null
+++ clang/test/Driver/flang/flang-omp.f90
@@ -0,0 +1,20 @@
+! Check that flang -fc1 is invoked when in --driver-mode=flang 
+! and the relevant openmp and openmp offload flags are utilised
+! and passed down correctly
+
+! Test regular -fopenmp with no offload
+! RUN: %clang --driver-mode=flang -### -fopenmp %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP %s
+! CHECK-OPENMP: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-OPENMP-NOT: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+
+! Test regular -fopenmp with offload for basic fopenmp-is-device flag addition and correct fopenmp 
+! RUN: %clang --driver-mode=flang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP-IS-DEVICE %s
+! CHECK-OPENMP-IS-DEVICE: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+
+! Test -fembed-object, -fopenmp-is-device and -fopenmp carry through when offload-arch is provided 
+! RUN: %clang --driver-mode=flang -### -S --target=aarch64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a %s 2>&1 | FileCheck --check-prefixes=CHECK-EMBED-OBJ-WITH-OARCH %s
+! CHECK-EMBED-OBJ-WITH-OARCH: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-EMBED-OBJ-WITH-OARCH-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK-EMBED-OBJ-WITH-OARCH: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK-EMBED-OBJ-WITH-OARCH-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.bc"
+! CHECK-EMBED-OBJ-WITH-OARCH: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fembed-offload-object={{.*}}.out" {{.*}}.bc"
Index: clang/lib/Driver/ToolChains/Flang.h
===
--- clang/lib/Driver/ToolChains/Flang.h
+++ clang/lib/Driver/ToolChains/Flang.h
@@ -56,6 +56,17 @@
   void addTargetOptions(const llvm::opt::ArgList &Args,
 llvm::opt::ArgStringList &CmdArgs) const;
 
+  /// Extract offload options from the driver arguments and add them to
+  /// the command arguments.
+  /// \param [in] C The current compilation for the driver invocation
+  /// \param [in] Inputs The input infomration on the current fi

[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-13 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 504592.
agozillon added a comment.

- [Flang][Driver] Try to fix failing omp-frontend-forwarding.f90 test


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145815

Files:
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  clang/test/Driver/flang/flang-omp.f90
  flang/test/Driver/omp-frontend-forwarding.f90

Index: flang/test/Driver/omp-frontend-forwarding.f90
===
--- /dev/null
+++ flang/test/Driver/omp-frontend-forwarding.f90
@@ -0,0 +1,17 @@
+! REQUIRES: amdgpu-registered-target
+
+! Test that flang-new OpenMP and OpenMP offload related 
+! commands forward or expand to the appropriate commands 
+! for flang-new -fc1 as expected.
+
+! Testing fembed-offload-object and fopenmp-is-device
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s
+! CHECK: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK: "{{[^"]*}}clang-offload-packager" {{.*}} "--image=file={{.*}}.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp"
+! CHECK-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.bc"
+! CHECK: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fembed-offload-object={{.*}}.out" {{.*}}.bc"
Index: clang/test/Driver/flang/flang-omp.f90
===
--- /dev/null
+++ clang/test/Driver/flang/flang-omp.f90
@@ -0,0 +1,20 @@
+! Check that flang -fc1 is invoked when in --driver-mode=flang 
+! and the relevant openmp and openmp offload flags are utilised
+! and passed down correctly
+
+! Test regular -fopenmp with no offload
+! RUN: %clang --driver-mode=flang -### -fopenmp %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP %s
+! CHECK-OPENMP: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-OPENMP-NOT: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+
+! Test regular -fopenmp with offload for basic fopenmp-is-device flag addition and correct fopenmp 
+! RUN: %clang --driver-mode=flang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP-IS-DEVICE %s
+! CHECK-OPENMP-IS-DEVICE: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+
+! Test -fembed-object, -fopenmp-is-device and -fopenmp carry through when offload-arch is provided 
+! RUN: %clang --driver-mode=flang -### -S --target=aarch64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a %s 2>&1 | FileCheck --check-prefixes=CHECK-EMBED-OBJ-WITH-OARCH %s
+! CHECK-EMBED-OBJ-WITH-OARCH: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-EMBED-OBJ-WITH-OARCH-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK-EMBED-OBJ-WITH-OARCH: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK-EMBED-OBJ-WITH-OARCH-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.bc"
+! CHECK-EMBED-OBJ-WITH-OARCH: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fembed-offload-object={{.*}}.out" {{.*}}.bc"
Index: clang/lib/Driver/ToolChains/Flang.h
===
--- clang/lib/Driver/ToolChains/Flang.h
+++ clang/lib/Driver/ToolChains/Flang.h
@@ -56,6 +56,17 @@
   void addTargetOptions(const llvm::opt::ArgList &Args,
 llvm::opt::ArgStringList &CmdArgs) const;
 
+  /// Extract offload options from the driver arguments and add them to
+  /// the command arguments.
+  /// \param [in] C The current compilation for the driver invocation
+  /// \param [in] Inputs The input infomration on the current file inputs
+  /// \param [in] JA The job action
+  /// \param [in] Args The list of input driver arguments
+  /// \param [out] CmdArgs The list of output command arguments
+  void addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
+ const JobAction &JA, const llvm::opt::ArgList &Args,
+ llvm::opt::ArgStringList &CmdArgs) const;
+
   /// Extract other compilation options from the driver arguments and add them
   /// to the command arguments.
   

[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-13 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added inline comments.



Comment at: flang/test/Driver/omp-frontend-forwarding.f90:15
+! CHECK: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} 
"-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK: "{{[^"]*}}clang-offload-packager" {{.*}} 
"--image=file={{.*}}gfx90a.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp"
+! CHECK-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" 
{{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.bc"

skatrak wrote:
> This unit test seems to be failing due to the pattern you've used here. 
> Probably a simple fix to get working, this is the test output: 
> https://reviews.llvm.org/harbormaster/unit/view/6153283/
Thank you! Hopefully it's working now. Not sure how it got passed me running 
check-all the first time... 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145815

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


[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-13 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 504829.
agozillon added a comment.

- [Flang][Driver] Simplify omp-frontend-forwarding.f90 test


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145815

Files:
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/lib/Driver/ToolChains/Flang.h
  clang/test/Driver/flang/flang-omp.f90
  flang/test/Driver/omp-frontend-forwarding.f90

Index: flang/test/Driver/omp-frontend-forwarding.f90
===
--- /dev/null
+++ flang/test/Driver/omp-frontend-forwarding.f90
@@ -0,0 +1,13 @@
+! Test that flang-new OpenMP and OpenMP offload related 
+! commands forward or expand to the appropriate commands 
+! for flang-new -fc1 as expected.
+
+! Testing fembed-offload-object and fopenmp-is-device
+! RUN: %flang -S -### %s -o %t 2>&1 \
+! RUN: -fopenmp --offload-arch=gfx90a \
+! RUN: --target=aarch64-unknown-linux-gnu \
+! RUN:   | FileCheck %s
+! CHECK: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK: "{{[^"]*}}clang-offload-packager" {{.*}} "--image=file={{.*}}.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp"
+! CHECK-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fembed-offload-object={{.*}}.out" {{.*}}.bc"
Index: clang/test/Driver/flang/flang-omp.f90
===
--- /dev/null
+++ clang/test/Driver/flang/flang-omp.f90
@@ -0,0 +1,20 @@
+! Check that flang -fc1 is invoked when in --driver-mode=flang 
+! and the relevant openmp and openmp offload flags are utilised
+! and passed down correctly
+
+! Test regular -fopenmp with no offload
+! RUN: %clang --driver-mode=flang -### -fopenmp %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP %s
+! CHECK-OPENMP: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-OPENMP-NOT: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+
+! Test regular -fopenmp with offload for basic fopenmp-is-device flag addition and correct fopenmp 
+! RUN: %clang --driver-mode=flang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP-IS-DEVICE %s
+! CHECK-OPENMP-IS-DEVICE: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+
+! Test -fembed-object, -fopenmp-is-device and -fopenmp carry through when offload-arch is provided 
+! RUN: %clang --driver-mode=flang -### -S --target=aarch64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a %s 2>&1 | FileCheck --check-prefixes=CHECK-EMBED-OBJ-WITH-OARCH %s
+! CHECK-EMBED-OBJ-WITH-OARCH: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-EMBED-OBJ-WITH-OARCH-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK-EMBED-OBJ-WITH-OARCH: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK-EMBED-OBJ-WITH-OARCH-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.bc"
+! CHECK-EMBED-OBJ-WITH-OARCH: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fembed-offload-object={{.*}}.out" {{.*}}.bc"
Index: clang/lib/Driver/ToolChains/Flang.h
===
--- clang/lib/Driver/ToolChains/Flang.h
+++ clang/lib/Driver/ToolChains/Flang.h
@@ -56,6 +56,17 @@
   void addTargetOptions(const llvm::opt::ArgList &Args,
 llvm::opt::ArgStringList &CmdArgs) const;
 
+  /// Extract offload options from the driver arguments and add them to
+  /// the command arguments.
+  /// \param [in] C The current compilation for the driver invocation
+  /// \param [in] Inputs The input infomration on the current file inputs
+  /// \param [in] JA The job action
+  /// \param [in] Args The list of input driver arguments
+  /// \param [out] CmdArgs The list of output command arguments
+  void addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
+ const JobAction &JA, const llvm::opt::ArgList &Args,
+ llvm::opt::ArgStringList &CmdArgs) const;
+
   /// Extract other compilation options from the driver arguments and add them
   /// to the command arguments.
   ///
Index: clang/lib/Driver/ToolChains/Flang.cpp
===
--- clang/lib/Driver/ToolChains/Flang.cpp
+++ clang/lib/Driver/ToolChains/Flang.cpp
@@ -114,6 +114,33 @@
   // TODO: Add target specific flags, ABI, mtune option etc.
 }
 
+void Flang::addOffloadOpt

[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-13 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

Recent update was to simplify the omp-frontend-forwarding.f90 test




Comment at: flang/test/Driver/omp-frontend-forwarding.f90:1
+! REQUIRES: amdgpu-registered-target
+

awarzynski wrote:
> Given that you use `-###`, I think that this can be skipped (please double 
> check).
It does appear that it can be, at the very least I can swap in an NVIIDIA arch 
when I haven't configured the project to target it and it has no issues! Thank 
you. 



Comment at: flang/test/Driver/omp-frontend-forwarding.f90:13
+! CHECK: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" 
{{.*}} "-fopenmp" {{.*}}.f90"
+! CHECK-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" 
{{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
+! CHECK: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} 
"-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"

awarzynski wrote:
> I feel that you can safely remove this line and replace the following with 
> `CHECK-NEXT`, no? Just wondering whether there's a way to reduce the noise in 
> these tests (without sacrificing the rigor).
I can indeed, thank you very much @awarzynski! I have a knack for 
overcomplicating these it seems


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145815

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


[PATCH] D145815: [Flang][Driver] Add support for fopenmp-is-device and fembed-offload-object to Flang ToolChain

2023-03-29 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

No problem at all, I broke it, happy to fix it! Submitting the fix in the next 
30 minutes~ after a rebase as the build bots seem to have passed happily, it 
took a lot longer than I expected for the build-bot to roundtable to my patch, 
my apologies, teaches me to use it as an adhoc test environment I suppose :-)


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145815

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


[PATCH] D145264: [OpenMP][MLIR] Lower and apply RTLModuleFlagsAttr

2023-03-29 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 509487.
agozillon edited the summary of this revision.
agozillon added a comment.
Herald added subscribers: jplehr, sunshaoce.

- [Flang][MLIR][OpenMP] Fix rebase inconsistencies
- [Flang][bbc][Tools] Add OpenMP RTL flags to bbc and adjust 
setOffloadModuleInterfaceAttributes to use LangOpts
- [Flang][bbc][Tools] Update rtl-flags.f90 test for bbc tests and generally fix 
due to rebase changes


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145264

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  clang/test/Driver/flang/flang-omp.f90
  flang/include/flang/Frontend/LangOptions.def
  flang/include/flang/Tools/CrossToolHelpers.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Lower/OpenMP/rtl-flags.f90
  flang/tools/bbc/CMakeLists.txt
  flang/tools/bbc/bbc.cpp
  mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Index: mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -1542,6 +1542,38 @@
   return bodyGenStatus;
 }
 
+/// Lowers the FlagsAttr which is applied to the module on the device
+/// pass when offloading, this attribute contains OpenMP RTL globals that can
+/// be passed as flags to the frontend, otherwise they are set to default
+LogicalResult convertFlagsAttr(Operation *op, mlir::omp::FlagsAttr attribute,
+   LLVM::ModuleTranslation &moduleTranslation) {
+  llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
+
+  ompBuilder->createGlobalFlag(
+  attribute.getDebugKind() /*LangOpts().OpenMPTargetDebug*/,
+  "__omp_rtl_debug_kind");
+  ompBuilder->createGlobalFlag(
+  attribute
+  .getAssumeTeamsOversubscription() /*LangOpts().OpenMPTeamSubscription*/
+  ,
+  "__omp_rtl_assume_teams_oversubscription");
+  ompBuilder->createGlobalFlag(
+  attribute
+  .getAssumeThreadsOversubscription() /*LangOpts().OpenMPThreadSubscription*/
+  ,
+  "__omp_rtl_assume_threads_oversubscription");
+  ompBuilder->createGlobalFlag(
+  attribute.getAssumeNoThreadState() /*LangOpts().OpenMPNoThreadState*/,
+  "__omp_rtl_assume_no_thread_state");
+  ompBuilder->createGlobalFlag(
+  attribute
+  .getAssumeNoNestedParallelism() /*LangOpts().OpenMPNoNestedParallelism*/
+  ,
+  "__omp_rtl_assume_no_nested_parallelism");
+
+  return success();
+}
+
 namespace {
 
 /// Implementation of the dialect interface that converts operations belonging
@@ -1556,10 +1588,34 @@
   LogicalResult
   convertOperation(Operation *op, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) const final;
+
+  LogicalResult
+  amendOperation(Operation *op, NamedAttribute attribute,
+ LLVM::ModuleTranslation &moduleTranslation) const final;
 };
 
 } // namespace
 
+/// Given an OpenMP MLIR attribute, create the corresponding LLVM-IR, runtime
+/// calls, or operation amendments
+LogicalResult OpenMPDialectLLVMIRTranslationInterface::amendOperation(
+Operation *op, NamedAttribute attribute,
+LLVM::ModuleTranslation &moduleTranslation) const {
+
+  return llvm::TypeSwitch(attribute.getValue())
+  .Case([&](mlir::omp::FlagsAttr rtlAttr) {
+return convertFlagsAttr(op, rtlAttr, moduleTranslation);
+  })
+  .Default([&](Attribute attr) {
+// fall through for omp attributes that do not require lowering and/or
+// have no concrete definition and thus no type to define a case on
+// e.g. omp.is_device
+return success();
+  });
+
+  return failure();
+}
+
 /// Given an OpenMP MLIR operation, create the corresponding LLVM IR
 /// (including OpenMP runtime calls).
 LogicalResult OpenMPDialectLLVMIRTranslationInterface::convertOperation(
Index: flang/tools/bbc/bbc.cpp
===
--- flang/tools/bbc/bbc.cpp
+++ flang/tools/bbc/bbc.cpp
@@ -16,6 +16,7 @@
 
 #include "flang/Common/Fortran-features.h"
 #include "flang/Common/default-kinds.h"
+#include "flang/Frontend/LangOptions.h"
 #include "flang/Lower/Bridge.h"
 #include "flang/Lower/PFTBuilder.h"
 #include "flang/Lower/Support/Verifier.h"
@@ -129,6 +130,39 @@
llvm::cl::desc("enable openmp device compilation"),
llvm::cl::init(false));
 
+// A simplified subset of the OpenMP RTL Flags from Flang, only the primary
+// positive options are available, no negative options e.g. fopen_assume* vs
+// fno_open_assume*
+static llvm::cl::opt setOpenMPTargetDebug(
+"fopenmp-target-debug",
+  

[PATCH] D145264: [OpenMP][MLIR] Lower and apply RTLModuleFlagsAttr

2023-03-29 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

All the mechanisms for this patch to now function are upstream, so I believe it 
is ready for a review if at all possible @kiranchandramohan @awarzynski and 
other reviewers that this patch may be of interest to.

Although if we wish for it to be split into two separate Phabricator patches, 
one for the lowering, the other for the flag additions and attribute creation, 
I can do so. However, I think this patch gives the big picture which may be 
useful.

As a side-note there will be a follow up commit in the near future (tomorrow if 
nothing steals my attention) which will do the following:

- Add an additional mlir-translate test, as I believe I owe @kiranchandramohan 
a test I promised from another patch!
- The clang/test/Driver/flang/flang-omp.f90 will be converted into a flang 
specific test and likely moved into the existing omp-frontend-forwarding.f90 
test


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145264

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


[PATCH] D145264: [OpenMP][MLIR] Lower and apply RTLModuleFlagsAttr

2023-03-30 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 509626.
agozillon added a comment.

- [Flang][Driver][Test] Move flang-omp.f90 tests into 
omp-frontend-forwarding.f90


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145264

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/LangOptions.def
  flang/include/flang/Tools/CrossToolHelpers.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/omp-frontend-forwarding.f90
  flang/test/Lower/OpenMP/rtl-flags.f90
  flang/tools/bbc/CMakeLists.txt
  flang/tools/bbc/bbc.cpp
  mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Index: mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -1542,6 +1542,38 @@
   return bodyGenStatus;
 }
 
+/// Lowers the FlagsAttr which is applied to the module on the device
+/// pass when offloading, this attribute contains OpenMP RTL globals that can
+/// be passed as flags to the frontend, otherwise they are set to default
+LogicalResult convertFlagsAttr(Operation *op, mlir::omp::FlagsAttr attribute,
+   LLVM::ModuleTranslation &moduleTranslation) {
+  llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
+
+  ompBuilder->createGlobalFlag(
+  attribute.getDebugKind() /*LangOpts().OpenMPTargetDebug*/,
+  "__omp_rtl_debug_kind");
+  ompBuilder->createGlobalFlag(
+  attribute
+  .getAssumeTeamsOversubscription() /*LangOpts().OpenMPTeamSubscription*/
+  ,
+  "__omp_rtl_assume_teams_oversubscription");
+  ompBuilder->createGlobalFlag(
+  attribute
+  .getAssumeThreadsOversubscription() /*LangOpts().OpenMPThreadSubscription*/
+  ,
+  "__omp_rtl_assume_threads_oversubscription");
+  ompBuilder->createGlobalFlag(
+  attribute.getAssumeNoThreadState() /*LangOpts().OpenMPNoThreadState*/,
+  "__omp_rtl_assume_no_thread_state");
+  ompBuilder->createGlobalFlag(
+  attribute
+  .getAssumeNoNestedParallelism() /*LangOpts().OpenMPNoNestedParallelism*/
+  ,
+  "__omp_rtl_assume_no_nested_parallelism");
+
+  return success();
+}
+
 namespace {
 
 /// Implementation of the dialect interface that converts operations belonging
@@ -1556,10 +1588,34 @@
   LogicalResult
   convertOperation(Operation *op, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) const final;
+
+  LogicalResult
+  amendOperation(Operation *op, NamedAttribute attribute,
+ LLVM::ModuleTranslation &moduleTranslation) const final;
 };
 
 } // namespace
 
+/// Given an OpenMP MLIR attribute, create the corresponding LLVM-IR, runtime
+/// calls, or operation amendments
+LogicalResult OpenMPDialectLLVMIRTranslationInterface::amendOperation(
+Operation *op, NamedAttribute attribute,
+LLVM::ModuleTranslation &moduleTranslation) const {
+
+  return llvm::TypeSwitch(attribute.getValue())
+  .Case([&](mlir::omp::FlagsAttr rtlAttr) {
+return convertFlagsAttr(op, rtlAttr, moduleTranslation);
+  })
+  .Default([&](Attribute attr) {
+// fall through for omp attributes that do not require lowering and/or
+// have no concrete definition and thus no type to define a case on
+// e.g. omp.is_device
+return success();
+  });
+
+  return failure();
+}
+
 /// Given an OpenMP MLIR operation, create the corresponding LLVM IR
 /// (including OpenMP runtime calls).
 LogicalResult OpenMPDialectLLVMIRTranslationInterface::convertOperation(
Index: flang/tools/bbc/bbc.cpp
===
--- flang/tools/bbc/bbc.cpp
+++ flang/tools/bbc/bbc.cpp
@@ -16,6 +16,7 @@
 
 #include "flang/Common/Fortran-features.h"
 #include "flang/Common/default-kinds.h"
+#include "flang/Frontend/LangOptions.h"
 #include "flang/Lower/Bridge.h"
 #include "flang/Lower/PFTBuilder.h"
 #include "flang/Lower/Support/Verifier.h"
@@ -129,6 +130,39 @@
llvm::cl::desc("enable openmp device compilation"),
llvm::cl::init(false));
 
+// A simplified subset of the OpenMP RTL Flags from Flang, only the primary
+// positive options are available, no negative options e.g. fopen_assume* vs
+// fno_open_assume*
+static llvm::cl::opt setOpenMPTargetDebug(
+"fopenmp-target-debug",
+llvm::cl::desc("Enable debugging in the OpenMP offloading device RTL"),
+llvm::cl::init(0));
+
+static llvm::cl::opt
+setOpenMPThreadSubscription(
+"fopenmp-assume-threads-oversubscription",
+llvm::cl::desc("Assume work-shared loops do not hav

[PATCH] D145264: [OpenMP][MLIR] Lower and apply RTLModuleFlagsAttr

2023-03-30 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 509628.
agozillon added a comment.

- [Flang][Driver][Test] Move flang-omp.f90 tests into 
omp-frontend-forwarding.f90


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145264

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/LangOptions.def
  flang/include/flang/Tools/CrossToolHelpers.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/omp-frontend-forwarding.f90
  flang/test/Lower/OpenMP/rtl-flags.f90
  flang/tools/bbc/CMakeLists.txt
  flang/tools/bbc/bbc.cpp
  mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Index: mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -1542,6 +1542,38 @@
   return bodyGenStatus;
 }
 
+/// Lowers the FlagsAttr which is applied to the module on the device
+/// pass when offloading, this attribute contains OpenMP RTL globals that can
+/// be passed as flags to the frontend, otherwise they are set to default
+LogicalResult convertFlagsAttr(Operation *op, mlir::omp::FlagsAttr attribute,
+   LLVM::ModuleTranslation &moduleTranslation) {
+  llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
+
+  ompBuilder->createGlobalFlag(
+  attribute.getDebugKind() /*LangOpts().OpenMPTargetDebug*/,
+  "__omp_rtl_debug_kind");
+  ompBuilder->createGlobalFlag(
+  attribute
+  .getAssumeTeamsOversubscription() /*LangOpts().OpenMPTeamSubscription*/
+  ,
+  "__omp_rtl_assume_teams_oversubscription");
+  ompBuilder->createGlobalFlag(
+  attribute
+  .getAssumeThreadsOversubscription() /*LangOpts().OpenMPThreadSubscription*/
+  ,
+  "__omp_rtl_assume_threads_oversubscription");
+  ompBuilder->createGlobalFlag(
+  attribute.getAssumeNoThreadState() /*LangOpts().OpenMPNoThreadState*/,
+  "__omp_rtl_assume_no_thread_state");
+  ompBuilder->createGlobalFlag(
+  attribute
+  .getAssumeNoNestedParallelism() /*LangOpts().OpenMPNoNestedParallelism*/
+  ,
+  "__omp_rtl_assume_no_nested_parallelism");
+
+  return success();
+}
+
 namespace {
 
 /// Implementation of the dialect interface that converts operations belonging
@@ -1556,10 +1588,34 @@
   LogicalResult
   convertOperation(Operation *op, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) const final;
+
+  LogicalResult
+  amendOperation(Operation *op, NamedAttribute attribute,
+ LLVM::ModuleTranslation &moduleTranslation) const final;
 };
 
 } // namespace
 
+/// Given an OpenMP MLIR attribute, create the corresponding LLVM-IR, runtime
+/// calls, or operation amendments
+LogicalResult OpenMPDialectLLVMIRTranslationInterface::amendOperation(
+Operation *op, NamedAttribute attribute,
+LLVM::ModuleTranslation &moduleTranslation) const {
+
+  return llvm::TypeSwitch(attribute.getValue())
+  .Case([&](mlir::omp::FlagsAttr rtlAttr) {
+return convertFlagsAttr(op, rtlAttr, moduleTranslation);
+  })
+  .Default([&](Attribute attr) {
+// fall through for omp attributes that do not require lowering and/or
+// have no concrete definition and thus no type to define a case on
+// e.g. omp.is_device
+return success();
+  });
+
+  return failure();
+}
+
 /// Given an OpenMP MLIR operation, create the corresponding LLVM IR
 /// (including OpenMP runtime calls).
 LogicalResult OpenMPDialectLLVMIRTranslationInterface::convertOperation(
Index: flang/tools/bbc/bbc.cpp
===
--- flang/tools/bbc/bbc.cpp
+++ flang/tools/bbc/bbc.cpp
@@ -16,6 +16,7 @@
 
 #include "flang/Common/Fortran-features.h"
 #include "flang/Common/default-kinds.h"
+#include "flang/Frontend/LangOptions.h"
 #include "flang/Lower/Bridge.h"
 #include "flang/Lower/PFTBuilder.h"
 #include "flang/Lower/Support/Verifier.h"
@@ -129,6 +130,39 @@
llvm::cl::desc("enable openmp device compilation"),
llvm::cl::init(false));
 
+// A simplified subset of the OpenMP RTL Flags from Flang, only the primary
+// positive options are available, no negative options e.g. fopen_assume* vs
+// fno_open_assume*
+static llvm::cl::opt setOpenMPTargetDebug(
+"fopenmp-target-debug",
+llvm::cl::desc("Enable debugging in the OpenMP offloading device RTL"),
+llvm::cl::init(0));
+
+static llvm::cl::opt
+setOpenMPThreadSubscription(
+"fopenmp-assume-threads-oversubscription",
+llvm::cl::desc("Assume work-shared loops do not hav

[PATCH] D145264: [OpenMP][MLIR] Lower and apply RTLModuleFlagsAttr

2023-03-30 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

Recent update(s) moved flag/driver testing of the flags from Clang to Flang's 
omp-frontend-forwarding.f90 aligning it with previous tests.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145264

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


[PATCH] D145264: [OpenMP][MLIR][Flang][Driver][bbc] Lower and apply Module FlagsAttr

2023-03-30 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

Unfortunately I do not believe an mlir-translate test that tests if the 
OffloadModuleInterface is accessible when directly utilizing mlir-translate is 
possible for this patch... I forgot I removed the is device check as it is 
already done at the initial creation of the attribute. However, I do have a 
future patch that it will be utilised in and when @jsjodin's initial TargetOp 
work is in I can likely create a test around that functionality.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145264

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


[PATCH] D145264: [OpenMP][MLIR][Flang][Driver][bbc] Lower and apply Module FlagsAttr

2023-03-30 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added inline comments.



Comment at: clang/include/clang/Driver/Options.td:2692
 def fno_openmp_assume_teams_oversubscription : Flag<["-"], 
"fno-openmp-assume-teams-oversubscription">,
-  Group, Flags<[CC1Option, NoArgumentUnused, HelpHidden]>;
+  Group, Flags<[CC1Option, FC1Option, NoArgumentUnused, HelpHidden]>;
 def fno_openmp_assume_threads_oversubscription : Flag<["-"], 
"fno-openmp-assume-threads-oversubscription">,

awarzynski wrote:
> Many of these options have identical flags. While not really needed for this 
> change, it would still be nice to re-organise them a bit. This file could 
> really benefit from some love :) Here's an example of what I have in mind: 
> https://github.com/llvm/llvm-project/blob/cf60d3f1a688671c8eb7859bf0572c403c3c0cca/clang/include/clang/Driver/Options.td#L6575-L6600
I can have a look into doing this in this patch :-)



Comment at: flang/tools/bbc/CMakeLists.txt:29
 FortranLower
+flangFrontendTool
 )

awarzynski wrote:
> This a frontend driver library and so far `bbc` and `flang-new -fc1` have 
> been entirely separate. Could this dependency be avoided?
I had hoped to share LangOpts so that the setOffloadModuleInterfaceAttributes 
function wouldn't turn into a monolithic set of arguments whenever it's invoked 
if more arguments are added, but the dependency isn't ideal I agree!

I could perhaps look into making some sort of shared data structure to be put 
inside of CrossToolHelpers that might remove the dependency and be similarly 
useable to how LangOpts works at the moment. If that doesn't work, I can revert 
the change to just be a regular argument list and we can revisit the topic if 
new options are ever added? 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145264

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


[PATCH] D145264: [OpenMP][MLIR][Flang][Driver][bbc] Lower and apply Module FlagsAttr

2023-03-30 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added inline comments.



Comment at: flang/tools/bbc/CMakeLists.txt:29
 FortranLower
+flangFrontendTool
 )

agozillon wrote:
> awarzynski wrote:
> > This a frontend driver library and so far `bbc` and `flang-new -fc1` have 
> > been entirely separate. Could this dependency be avoided?
> I had hoped to share LangOpts so that the setOffloadModuleInterfaceAttributes 
> function wouldn't turn into a monolithic set of arguments whenever it's 
> invoked if more arguments are added, but the dependency isn't ideal I agree!
> 
> I could perhaps look into making some sort of shared data structure to be put 
> inside of CrossToolHelpers that might remove the dependency and be similarly 
> useable to how LangOpts works at the moment. If that doesn't work, I can 
> revert the change to just be a regular argument list and we can revisit the 
> topic if new options are ever added? 
Although looking at @domada's https://reviews.llvm.org/D146612 patch it reminds 
me that they could just be separate functions shared across tools (I perhaps 
got a little fixated on the idea of it being similar to a driver function 
handling all the options). Please do tell me whichever you'd prefer :-)


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145264

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


[PATCH] D145264: [OpenMP][MLIR][Flang][Driver][bbc] Lower and apply Module FlagsAttr

2023-03-30 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 509837.
agozillon added a comment.

Squash commits, format and apply requested changes


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145264

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/LangOptions.def
  flang/include/flang/Tools/CrossToolHelpers.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/omp-frontend-forwarding.f90
  flang/test/Lower/OpenMP/rtl-flags.f90
  flang/tools/bbc/bbc.cpp
  mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Index: mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
+++ mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
@@ -1542,6 +1542,38 @@
   return bodyGenStatus;
 }
 
+/// Lowers the FlagsAttr which is applied to the module on the device
+/// pass when offloading, this attribute contains OpenMP RTL globals that can
+/// be passed as flags to the frontend, otherwise they are set to default
+LogicalResult convertFlagsAttr(Operation *op, mlir::omp::FlagsAttr attribute,
+   LLVM::ModuleTranslation &moduleTranslation) {
+  llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
+
+  ompBuilder->createGlobalFlag(
+  attribute.getDebugKind() /*LangOpts().OpenMPTargetDebug*/,
+  "__omp_rtl_debug_kind");
+  ompBuilder->createGlobalFlag(
+  attribute
+  .getAssumeTeamsOversubscription() /*LangOpts().OpenMPTeamSubscription*/
+  ,
+  "__omp_rtl_assume_teams_oversubscription");
+  ompBuilder->createGlobalFlag(
+  attribute
+  .getAssumeThreadsOversubscription() /*LangOpts().OpenMPThreadSubscription*/
+  ,
+  "__omp_rtl_assume_threads_oversubscription");
+  ompBuilder->createGlobalFlag(
+  attribute.getAssumeNoThreadState() /*LangOpts().OpenMPNoThreadState*/,
+  "__omp_rtl_assume_no_thread_state");
+  ompBuilder->createGlobalFlag(
+  attribute
+  .getAssumeNoNestedParallelism() /*LangOpts().OpenMPNoNestedParallelism*/
+  ,
+  "__omp_rtl_assume_no_nested_parallelism");
+
+  return success();
+}
+
 namespace {
 
 /// Implementation of the dialect interface that converts operations belonging
@@ -1556,10 +1588,34 @@
   LogicalResult
   convertOperation(Operation *op, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) const final;
+
+  LogicalResult
+  amendOperation(Operation *op, NamedAttribute attribute,
+ LLVM::ModuleTranslation &moduleTranslation) const final;
 };
 
 } // namespace
 
+/// Given an OpenMP MLIR attribute, create the corresponding LLVM-IR, runtime
+/// calls, or operation amendments
+LogicalResult OpenMPDialectLLVMIRTranslationInterface::amendOperation(
+Operation *op, NamedAttribute attribute,
+LLVM::ModuleTranslation &moduleTranslation) const {
+
+  return llvm::TypeSwitch(attribute.getValue())
+  .Case([&](mlir::omp::FlagsAttr rtlAttr) {
+return convertFlagsAttr(op, rtlAttr, moduleTranslation);
+  })
+  .Default([&](Attribute attr) {
+// fall through for omp attributes that do not require lowering and/or
+// have no concrete definition and thus no type to define a case on
+// e.g. omp.is_device
+return success();
+  });
+
+  return failure();
+}
+
 /// Given an OpenMP MLIR operation, create the corresponding LLVM IR
 /// (including OpenMP runtime calls).
 LogicalResult OpenMPDialectLLVMIRTranslationInterface::convertOperation(
Index: flang/tools/bbc/bbc.cpp
===
--- flang/tools/bbc/bbc.cpp
+++ flang/tools/bbc/bbc.cpp
@@ -129,6 +129,38 @@
llvm::cl::desc("enable openmp device compilation"),
llvm::cl::init(false));
 
+// A simplified subset of the OpenMP RTL Flags from Flang, only the primary
+// positive options are available, no negative options e.g. fopen_assume* vs
+// fno_open_assume*
+static llvm::cl::opt setOpenMPTargetDebug(
+"fopenmp-target-debug",
+llvm::cl::desc("Enable debugging in the OpenMP offloading device RTL"),
+llvm::cl::init(0));
+
+static llvm::cl::opt setOpenMPThreadSubscription(
+"fopenmp-assume-threads-oversubscription",
+llvm::cl::desc("Assume work-shared loops do not have more "
+   "iterations than participating threads."),
+llvm::cl::init(false));
+
+static llvm::cl::opt setOpenMPTeamSubscription(
+"fopenmp-assume-teams-oversubscription",
+llvm::cl::desc("Assume distributed loops do not have more iterations than "
+   "participating teams."),
+llvm::cl

[PATCH] D145264: [OpenMP][MLIR][Flang][Driver][bbc] Lower and apply Module FlagsAttr

2023-03-30 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added inline comments.



Comment at: clang/include/clang/Driver/Options.td:2692
 def fno_openmp_assume_teams_oversubscription : Flag<["-"], 
"fno-openmp-assume-teams-oversubscription">,
-  Group, Flags<[CC1Option, NoArgumentUnused, HelpHidden]>;
+  Group, Flags<[CC1Option, FC1Option, NoArgumentUnused, HelpHidden]>;
 def fno_openmp_assume_threads_oversubscription : Flag<["-"], 
"fno-openmp-assume-threads-oversubscription">,

agozillon wrote:
> awarzynski wrote:
> > Many of these options have identical flags. While not really needed for 
> > this change, it would still be nice to re-organise them a bit. This file 
> > could really benefit from some love :) Here's an example of what I have in 
> > mind: 
> > https://github.com/llvm/llvm-project/blob/cf60d3f1a688671c8eb7859bf0572c403c3c0cca/clang/include/clang/Driver/Options.td#L6575-L6600
> I can have a look into doing this in this patch :-)
Made an attempt at tidying these up with the recent commit, hopefully this is 
close to what you expected! 



Comment at: flang/tools/bbc/CMakeLists.txt:29
 FortranLower
+flangFrontendTool
 )

agozillon wrote:
> agozillon wrote:
> > awarzynski wrote:
> > > This a frontend driver library and so far `bbc` and `flang-new -fc1` have 
> > > been entirely separate. Could this dependency be avoided?
> > I had hoped to share LangOpts so that the 
> > setOffloadModuleInterfaceAttributes function wouldn't turn into a 
> > monolithic set of arguments whenever it's invoked if more arguments are 
> > added, but the dependency isn't ideal I agree!
> > 
> > I could perhaps look into making some sort of shared data structure to be 
> > put inside of CrossToolHelpers that might remove the dependency and be 
> > similarly useable to how LangOpts works at the moment. If that doesn't 
> > work, I can revert the change to just be a regular argument list and we can 
> > revisit the topic if new options are ever added? 
> Although looking at @domada's https://reviews.llvm.org/D146612 patch it 
> reminds me that they could just be separate functions shared across tools (I 
> perhaps got a little fixated on the idea of it being similar to a driver 
> function handling all the options). Please do tell me whichever you'd prefer 
> :-)
I removed the dependency via a helper class, that will keep flang-new a little 
cleaner hopefully and allow bbc to utilise the same logic provided it populates 
an intermediate structure!

But alternatively it's possible to split into separate functions for each 
attribute that depends on options if that's the desired direction!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145264

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


[PATCH] D145264: [OpenMP][MLIR][Flang][Driver][bbc] Lower and apply Module FlagsAttr

2023-03-31 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

In D145264#4236173 , 
@kiranchandramohan wrote:

> Please split this patch into three:
>
> 1. Code changes and testing for the driver and the FIR+OpenMP dialect 
> generated.
> 2. Code changes and test for FIR+OpenMP to LLVM+OpenMP dialect.
> 3. Code changes and testing for the translation from LLVM + OpenMP dialect to 
> LLVM IR. Code in OpenMPToLLVMIRTranslation.cpp should be tested with 
> `mlir-translate`.

More than happy to do all of the above, however, there is no code for step 2 in 
this case as it undergoes no rewriting or transformation when going from 
FIR+OpenMP -> LLVM+OpenMP. However, I am happy to make a test to assure it is 
retained during the conversion, but I believe it would just be a test in the 
patch, would it be okay to merge patch 2 and 3 from your comment? Meaning there 
would be two patches, the driver additions and the attribute generation from 
the flags. And then the lowering of the attribute to LLVM-IR (with additional 
mlir-translate tests for LLVM+OpenMP -> LLVM-IR and FIR+OpenMP -> LLVM+OpenMP).

> In D145264#4233358 , @agozillon 
> wrote:
>
>> Unfortunately I do not believe an mlir-translate test that tests if the 
>> OffloadModuleInterface is accessible when directly utilizing mlir-translate 
>> is possible for this patch... I forgot I removed the is device check as it 
>> is already done at the initial creation of the attribute. However, I do have 
>> a future patch that it will be utilised in and when @jsjodin's initial 
>> TargetOp work is in I can likely create a test around that functionality.
>
> You may delay the translation code till then.

Thank you.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145264

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


[PATCH] D147324: [OpenMP][MLIR][Flang][bbc][Driver] Add OpenMP RTL Flags to Flang and generate omp.FlagsAttr from them

2023-03-31 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon created this revision.
Herald added subscribers: sunshaoce, bzcheeseman, rriddle, guansong, yaxunl.
Herald added a reviewer: sscalpone.
Herald added a reviewer: awarzynski.
Herald added a reviewer: kiranchandramohan.
Herald added projects: Flang, All.
agozillon requested review of this revision.
Herald added subscribers: cfe-commits, jplehr, sstefan1, stephenneuendorffer, 
jdoerfert, MaskRay.
Herald added a reviewer: jdoerfert.
Herald added a project: clang.

This patch ports OpenMP RTL flags from the shared Clang compiler
options to Flang. As well as adding a limited subset to bbc.

This patch enables the flags below (and any equals or inverse variants)
for Flang that exist in Clang:

-fopenmp-target-debug
-fopenmp-assume-threads-oversubscription
-fopenmp-assume-teams-oversubscription
-fopenmp-assume-no-nested-parallelism
-fopenmp-assume-no-thread-state

For the bbc tool it only utilises the primary variants to minimize
additional complexity in the tool.

The patch also provides FlagAttr generation from these flags. Which
will be lowered to LLVM-IR in a subsequent patch.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147324

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/LangOptions.def
  flang/include/flang/Tools/CrossToolHelpers.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/omp-frontend-forwarding.f90
  flang/test/Lower/OpenMP/rtl-flags.f90
  flang/tools/bbc/bbc.cpp

Index: flang/tools/bbc/bbc.cpp
===
--- flang/tools/bbc/bbc.cpp
+++ flang/tools/bbc/bbc.cpp
@@ -129,6 +129,38 @@
llvm::cl::desc("enable openmp device compilation"),
llvm::cl::init(false));
 
+// A simplified subset of the OpenMP RTL Flags from Flang, only the primary
+// positive options are available, no negative options e.g. fopen_assume* vs
+// fno_open_assume*
+static llvm::cl::opt setOpenMPTargetDebug(
+"fopenmp-target-debug",
+llvm::cl::desc("Enable debugging in the OpenMP offloading device RTL"),
+llvm::cl::init(0));
+
+static llvm::cl::opt setOpenMPThreadSubscription(
+"fopenmp-assume-threads-oversubscription",
+llvm::cl::desc("Assume work-shared loops do not have more "
+   "iterations than participating threads."),
+llvm::cl::init(false));
+
+static llvm::cl::opt setOpenMPTeamSubscription(
+"fopenmp-assume-teams-oversubscription",
+llvm::cl::desc("Assume distributed loops do not have more iterations than "
+   "participating teams."),
+llvm::cl::init(false));
+
+static llvm::cl::opt setOpenMPNoThreadState(
+"fopenmp-assume-no-thread-state",
+llvm::cl::desc(
+"Assume that no thread in a parallel region will modify an ICV."),
+llvm::cl::init(false));
+
+static llvm::cl::opt setOpenMPNoNestedParallelism(
+"fopenmp-assume-no-nested-parallelism",
+llvm::cl::desc("Assume that no thread in a parallel region will encounter "
+   "a parallel region."),
+llvm::cl::init(false));
+
 static llvm::cl::opt enableOpenACC("fopenacc",
  llvm::cl::desc("enable openacc"),
  llvm::cl::init(false));
@@ -244,8 +276,13 @@
   kindMap, loweringOptions, {});
   burnside.lower(parseTree, semanticsContext);
   mlir::ModuleOp mlirModule = burnside.getModule();
-  if (enableOpenMP)
-setOffloadModuleInterfaceAttributes(mlirModule, enableOpenMPDevice);
+  if (enableOpenMP) {
+auto offloadModuleOpts =
+OffloadModuleOpts(setOpenMPTargetDebug, setOpenMPTeamSubscription,
+  setOpenMPThreadSubscription, setOpenMPNoThreadState,
+  setOpenMPNoNestedParallelism, enableOpenMPDevice);
+setOffloadModuleInterfaceAttributes(mlirModule, offloadModuleOpts);
+  }
   std::error_code ec;
   std::string outputName = outputFilename;
   if (!outputName.size())
Index: flang/test/Lower/OpenMP/rtl-flags.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/rtl-flags.f90
@@ -0,0 +1,29 @@
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEFAULT-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s --check-prefix=DEFAULT-HOST-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-target-debug -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DBG-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-target-debug=111 -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DBG-EQ-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-assume-teams-oversubscription -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=TEAMS-OSUB-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-assume-thread

[PATCH] D147324: [OpenMP][MLIR][Flang][bbc][Driver] Add OpenMP RTL Flags to Flang and generate omp.FlagsAttr from them

2023-03-31 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

The flag addition and FlagsAttr generation component of: 
https://reviews.llvm.org/D145264 the LLVM-IR generation will come in a 
subsequent phabricator patch once I have created some additional tests.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D147324

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


[PATCH] D147324: [OpenMP][MLIR][Flang][bbc][Driver] Add OpenMP RTL Flags to Flang and generate omp.FlagsAttr from them

2023-03-31 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 510080.
agozillon added a comment.

- Add FIR -> LLVM Dialect test here, more fitting than the other patch


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D147324

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/LangOptions.def
  flang/include/flang/Tools/CrossToolHelpers.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/omp-frontend-forwarding.f90
  flang/test/Lower/OpenMP/rtl-flags.f90
  flang/tools/bbc/bbc.cpp

Index: flang/tools/bbc/bbc.cpp
===
--- flang/tools/bbc/bbc.cpp
+++ flang/tools/bbc/bbc.cpp
@@ -129,6 +129,38 @@
llvm::cl::desc("enable openmp device compilation"),
llvm::cl::init(false));
 
+// A simplified subset of the OpenMP RTL Flags from Flang, only the primary
+// positive options are available, no negative options e.g. fopen_assume* vs
+// fno_open_assume*
+static llvm::cl::opt setOpenMPTargetDebug(
+"fopenmp-target-debug",
+llvm::cl::desc("Enable debugging in the OpenMP offloading device RTL"),
+llvm::cl::init(0));
+
+static llvm::cl::opt setOpenMPThreadSubscription(
+"fopenmp-assume-threads-oversubscription",
+llvm::cl::desc("Assume work-shared loops do not have more "
+   "iterations than participating threads."),
+llvm::cl::init(false));
+
+static llvm::cl::opt setOpenMPTeamSubscription(
+"fopenmp-assume-teams-oversubscription",
+llvm::cl::desc("Assume distributed loops do not have more iterations than "
+   "participating teams."),
+llvm::cl::init(false));
+
+static llvm::cl::opt setOpenMPNoThreadState(
+"fopenmp-assume-no-thread-state",
+llvm::cl::desc(
+"Assume that no thread in a parallel region will modify an ICV."),
+llvm::cl::init(false));
+
+static llvm::cl::opt setOpenMPNoNestedParallelism(
+"fopenmp-assume-no-nested-parallelism",
+llvm::cl::desc("Assume that no thread in a parallel region will encounter "
+   "a parallel region."),
+llvm::cl::init(false));
+
 static llvm::cl::opt enableOpenACC("fopenacc",
  llvm::cl::desc("enable openacc"),
  llvm::cl::init(false));
@@ -244,8 +276,13 @@
   kindMap, loweringOptions, {});
   burnside.lower(parseTree, semanticsContext);
   mlir::ModuleOp mlirModule = burnside.getModule();
-  if (enableOpenMP)
-setOffloadModuleInterfaceAttributes(mlirModule, enableOpenMPDevice);
+  if (enableOpenMP) {
+auto offloadModuleOpts =
+OffloadModuleOpts(setOpenMPTargetDebug, setOpenMPTeamSubscription,
+  setOpenMPThreadSubscription, setOpenMPNoThreadState,
+  setOpenMPNoNestedParallelism, enableOpenMPDevice);
+setOffloadModuleInterfaceAttributes(mlirModule, offloadModuleOpts);
+  }
   std::error_code ec;
   std::string outputName = outputFilename;
   if (!outputName.size())
Index: flang/test/Lower/OpenMP/rtl-flags.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/rtl-flags.f90
@@ -0,0 +1,64 @@
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEFAULT-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s --check-prefix=DEFAULT-HOST-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-target-debug -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DBG-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-target-debug=111 -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DBG-EQ-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-assume-teams-oversubscription -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=TEAMS-OSUB-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-assume-threads-oversubscription -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=THREAD-OSUB-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-assume-no-thread-state -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=THREAD-STATE-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-assume-no-nested-parallelism -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=NEST-PAR-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-target-debug -fopenmp-assume-teams-oversubscription -fopenmp-assume-no-nested-parallelism -fopenmp-assume-threads-oversubscription -fopenmp-assume-no-thread-state -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=ALL-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | fir-opt --fir-to-llvm-ir | FileCheck %s  --check-prefix=DEFAULT-HOST-LLVMDIALECT
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | fir-opt -

[PATCH] D145264: [OpenMP][MLIR][Flang][Driver][bbc] Lower and apply Module FlagsAttr

2023-03-31 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

Fragmented this patch into https://reviews.llvm.org/D147344 (lowering) and 
https://reviews.llvm.org/D147324 (driver/tool changes and application of 
attribute). I will keep this patch open until the others are closed to give a 
big picture for easier reference.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145264

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


[PATCH] D147324: [OpenMP][MLIR][Flang][bbc][Driver] Add OpenMP RTL Flags to Flang and generate omp.FlagsAttr from them

2023-04-05 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

Hi @awarzynski would it please be possible to get this reviewed when you have a 
spare moment or few, it's essentially the segment of the modifications from 
this patch which you accepted: https://reviews.llvm.org/D145264 the lowering 
has been separated into a different phab review as was requested. This pass 
just adds flags and creates the attribute on the Module! So it is hopefully 
good to go or close to it depending on your feedback :-) although I'll commit 
it at the same time as the lowering segment of the pass is accepted.

Thank you very much as always for your time reviewing my patches, I appreciate 
it greatly.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D147324

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


[PATCH] D147324: [OpenMP][MLIR][Flang][bbc][Driver] Add OpenMP RTL Flags to Flang and generate omp.FlagsAttr from them

2023-04-05 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

In D147324#4245932 , 
@kiranchandramohan wrote:

> LG. This portion of the patch was accepted in 
> https://reviews.llvm.org/D145264.
>
> See inline comment about location of LLVM Dialect tests. You may either 
> remove the LLVM dialect tests from this patch and land it. Or you can submit 
> this and move the tests in a separate NFC patch and submit without review.

Hi Kiran,

Thanks for the quick reply. I'll do the former and remove the tests and land 
the patch today (back tracking on the previous statement of leaving landing it 
till later), I don't believe they add anything to the test suite as there is no 
modifications between FIR -> LLVM Dialect as far as this attribute and the 
ModuleOp go.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D147324

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


[PATCH] D147324: [OpenMP][MLIR][Flang][bbc][Driver] Add OpenMP RTL Flags to Flang and generate omp.FlagsAttr from them

2023-04-05 Thread Andrew Gozillon via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG53152f12a47b: [OpenMP][MLIR][Flang][bbc][Driver] Add OpenMP 
RTL Flags to Flang and generate… (authored by agozillon).

Changed prior to commit:
  https://reviews.llvm.org/D147324?vs=510080&id=511154#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D147324

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/LangOptions.def
  flang/include/flang/Tools/CrossToolHelpers.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/lib/Frontend/FrontendActions.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/omp-frontend-forwarding.f90
  flang/test/Lower/OpenMP/rtl-flags.f90
  flang/tools/bbc/bbc.cpp

Index: flang/tools/bbc/bbc.cpp
===
--- flang/tools/bbc/bbc.cpp
+++ flang/tools/bbc/bbc.cpp
@@ -129,6 +129,38 @@
llvm::cl::desc("enable openmp device compilation"),
llvm::cl::init(false));
 
+// A simplified subset of the OpenMP RTL Flags from Flang, only the primary
+// positive options are available, no negative options e.g. fopen_assume* vs
+// fno_open_assume*
+static llvm::cl::opt setOpenMPTargetDebug(
+"fopenmp-target-debug",
+llvm::cl::desc("Enable debugging in the OpenMP offloading device RTL"),
+llvm::cl::init(0));
+
+static llvm::cl::opt setOpenMPThreadSubscription(
+"fopenmp-assume-threads-oversubscription",
+llvm::cl::desc("Assume work-shared loops do not have more "
+   "iterations than participating threads."),
+llvm::cl::init(false));
+
+static llvm::cl::opt setOpenMPTeamSubscription(
+"fopenmp-assume-teams-oversubscription",
+llvm::cl::desc("Assume distributed loops do not have more iterations than "
+   "participating teams."),
+llvm::cl::init(false));
+
+static llvm::cl::opt setOpenMPNoThreadState(
+"fopenmp-assume-no-thread-state",
+llvm::cl::desc(
+"Assume that no thread in a parallel region will modify an ICV."),
+llvm::cl::init(false));
+
+static llvm::cl::opt setOpenMPNoNestedParallelism(
+"fopenmp-assume-no-nested-parallelism",
+llvm::cl::desc("Assume that no thread in a parallel region will encounter "
+   "a parallel region."),
+llvm::cl::init(false));
+
 static llvm::cl::opt enableOpenACC("fopenacc",
  llvm::cl::desc("enable openacc"),
  llvm::cl::init(false));
@@ -244,8 +276,13 @@
   kindMap, loweringOptions, {});
   burnside.lower(parseTree, semanticsContext);
   mlir::ModuleOp mlirModule = burnside.getModule();
-  if (enableOpenMP)
-setOffloadModuleInterfaceAttributes(mlirModule, enableOpenMPDevice);
+  if (enableOpenMP) {
+auto offloadModuleOpts =
+OffloadModuleOpts(setOpenMPTargetDebug, setOpenMPTeamSubscription,
+  setOpenMPThreadSubscription, setOpenMPNoThreadState,
+  setOpenMPNoNestedParallelism, enableOpenMPDevice);
+setOffloadModuleInterfaceAttributes(mlirModule, offloadModuleOpts);
+  }
   std::error_code ec;
   std::string outputName = outputFilename;
   if (!outputName.size())
Index: flang/test/Lower/OpenMP/rtl-flags.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/rtl-flags.f90
@@ -0,0 +1,29 @@
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DEFAULT-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s --check-prefix=DEFAULT-HOST-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-target-debug -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DBG-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-target-debug=111 -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=DBG-EQ-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-assume-teams-oversubscription -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=TEAMS-OSUB-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-assume-threads-oversubscription -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=THREAD-OSUB-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-assume-no-thread-state -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=THREAD-STATE-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-assume-no-nested-parallelism -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=NEST-PAR-DEVICE-FIR
+!RUN: %flang_fc1 -emit-fir -fopenmp -fopenmp-target-debug -fopenmp-assume-teams-oversubscription -fopenmp-assume-no-nested-parallelism -fopenmp-assume-threads-oversubscription -fopenmp-assume-no-thread-state -fopenmp-is-device %s -o - | FileCheck %s --check-prefix=ALL-DEVICE-FIR
+!RUN: bbc -emit-fir -fopenmp -fopenmp-is-device -o - 

[PATCH] D148038: [Flang][OpenMP][Driver][MLIR] Port fopenmp-host-ir-file-path flag and add MLIR module attribute to proliferate to OpenMP IR lowering

2023-04-11 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon created this revision.
Herald added subscribers: bviyer, sunshaoce, Moerafaat, zero9178, bzcheeseman, 
sdasgup3, wenzhicui, wrengr, cota, teijeong, rdzhabarov, tatianashp, msifontes, 
jurahul, Kayjukh, grosul1, Joonsoo, liufengdb, aartbik, mgester, arpith-jacob, 
antiagainst, shauheen, rriddle, mehdi_amini, thopre, guansong, yaxunl.
Herald added a reviewer: sscalpone.
Herald added a reviewer: awarzynski.
Herald added a reviewer: kiranchandramohan.
Herald added projects: Flang, All.
agozillon requested review of this revision.
Herald added subscribers: cfe-commits, jplehr, sstefan1, stephenneuendorffer, 
nicolasvasilache, jdoerfert, MaskRay.
Herald added a reviewer: jdoerfert.
Herald added a reviewer: nicolasvasilache.
Herald added projects: clang, MLIR.

This patch ports the fopenmp-host-ir-file-path flag from Clang to Flang-new, 
this flag is added by the driver to the device pass when doing two phase 
compilation (device + host).

This flag is then applied to the module when compiling during the OpenMP device 
phase. This file can then be utilised during lowering of the OpenMP dialect to 
LLVM-IR, which allows the device and host to maintain 1:1 mapping of OpenMP 
metadata for variables during lowering via the OpenMPIRBuilders 
loadOffloadInfoMetadata facilities (which is used for declare target and I 
believe target regions as well).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D148038

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/LangOptions.h
  flang/include/flang/Tools/CrossToolHelpers.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/omp-frontend-forwarding.f90
  flang/test/Lower/OpenMP/omp-host-ir-flag.f90
  mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td

Index: mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
===
--- mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
+++ mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
@@ -71,7 +71,7 @@
   InterfaceMethod<
   /*description=*/[{
 Get the IsDeviceAttr attribute on the current module if it exists and return
-its value, if it doesn't exit it returns false by default.
+its value, if it doesn't exist it returns false by default.
   }],
   /*retTy=*/"bool",
   /*methodName=*/"getIsDevice",
@@ -138,6 +138,34 @@
  targetCPU.str(),
  targetFeatures.str()));
   }]>,
+  InterfaceMethod<
+  /*description=*/[{
+Set a StringAttr on the current module containing the host IR file path. This 
+file path is used in two-phase compilation during the device phase to generate
+device side LLVM IR when lowering MLIR. 
+  }],
+  /*retTy=*/"void",
+  /*methodName=*/"setHostIRFilePath", 
+  (ins "std::string":$hostIRFilePath), [{}], [{
+$_op->setAttr(
+  mlir::StringAttr::get($_op->getContext(), llvm::Twine{"omp.host_ir_filepath"}),
+mlir::StringAttr::get($_op->getContext(), hostIRFilePath));
+   }]>,
+  InterfaceMethod<
+  /*description=*/[{
+Find the host-ir file path StringAttr from the current module if it exists and 
+return its contained value, if it doesn't exist it returns an empty string. This 
+file path is used in two-phase compilation during the device phase to generate
+device side LLVM IR when lowering MLIR. 
+  }],
+  /*retTy=*/"llvm::StringRef",
+  /*methodName=*/"getHostIRFilePath", 
+  (ins), [{}], [{
+if (Attribute filepath = $_op->getAttr("omp.host_ir_filepath"))
+  if (filepath.isa())
+return filepath.dyn_cast().getValue();
+return {};
+  }]>
   ];
 }
 
Index: flang/test/Lower/OpenMP/omp-host-ir-flag.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/omp-host-ir-flag.f90
@@ -0,0 +1,6 @@
+!RUN: %flang_fc1 -emit-llvm-bc -fopenmp -o %t.bc %s 2>&1
+!RUN: %flang_fc1 -emit-mlir -fopenmp -fopenmp-is-device -fopenmp-host-ir-file-path %t.bc -o - %s 2>&1 | FileCheck %s
+
+!CHECK: module attributes {{{.*}}, omp.host_ir_filepath = "{{.*}}.bc", omp.is_device = #omp.isdevice{{.*}}}
+subroutine omp_subroutine()
+end subroutine omp_subroutine
\ No newline at end of file
Index: flang/test/Driver/omp-frontend-forwarding.f90
===
--- flang/test/Driver/omp-frontend-forwarding.f90
+++ flang/test/Driver/omp-frontend-forwarding.f90
@@ -11,15 +11,15 @@
 ! RUN: %flang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP-IS-DEVICE %s
 ! CHECK-OPENMP-IS-DEVICE: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
 
-! Testing fem

[PATCH] D148038: [Flang][OpenMP][Driver][MLIR] Port fopenmp-host-ir-file-path flag and add MLIR module attribute to proliferate to OpenMP IR lowering

2023-04-11 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 512554.
agozillon added a comment.

Adding missing newline


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148038

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/LangOptions.h
  flang/include/flang/Tools/CrossToolHelpers.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/omp-frontend-forwarding.f90
  flang/test/Lower/OpenMP/omp-host-ir-flag.f90
  mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td

Index: mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
===
--- mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
+++ mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
@@ -71,7 +71,7 @@
   InterfaceMethod<
   /*description=*/[{
 Get the IsDeviceAttr attribute on the current module if it exists and return
-its value, if it doesn't exit it returns false by default.
+its value, if it doesn't exist it returns false by default.
   }],
   /*retTy=*/"bool",
   /*methodName=*/"getIsDevice",
@@ -138,6 +138,34 @@
  targetCPU.str(),
  targetFeatures.str()));
   }]>,
+  InterfaceMethod<
+  /*description=*/[{
+Set a StringAttr on the current module containing the host IR file path. This 
+file path is used in two-phase compilation during the device phase to generate
+device side LLVM IR when lowering MLIR. 
+  }],
+  /*retTy=*/"void",
+  /*methodName=*/"setHostIRFilePath", 
+  (ins "std::string":$hostIRFilePath), [{}], [{
+$_op->setAttr(
+  mlir::StringAttr::get($_op->getContext(), llvm::Twine{"omp.host_ir_filepath"}),
+mlir::StringAttr::get($_op->getContext(), hostIRFilePath));
+   }]>,
+  InterfaceMethod<
+  /*description=*/[{
+Find the host-ir file path StringAttr from the current module if it exists and 
+return its contained value, if it doesn't exist it returns an empty string. This 
+file path is used in two-phase compilation during the device phase to generate
+device side LLVM IR when lowering MLIR. 
+  }],
+  /*retTy=*/"llvm::StringRef",
+  /*methodName=*/"getHostIRFilePath", 
+  (ins), [{}], [{
+if (Attribute filepath = $_op->getAttr("omp.host_ir_filepath"))
+  if (filepath.isa())
+return filepath.dyn_cast().getValue();
+return {};
+  }]>
   ];
 }
 
Index: flang/test/Lower/OpenMP/omp-host-ir-flag.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/omp-host-ir-flag.f90
@@ -0,0 +1,6 @@
+!RUN: %flang_fc1 -emit-llvm-bc -fopenmp -o %t.bc %s 2>&1
+!RUN: %flang_fc1 -emit-mlir -fopenmp -fopenmp-is-device -fopenmp-host-ir-file-path %t.bc -o - %s 2>&1 | FileCheck %s
+
+!CHECK: module attributes {{{.*}}, omp.host_ir_filepath = "{{.*}}.bc", omp.is_device = #omp.isdevice{{.*}}}
+subroutine omp_subroutine()
+end subroutine omp_subroutine
Index: flang/test/Driver/omp-frontend-forwarding.f90
===
--- flang/test/Driver/omp-frontend-forwarding.f90
+++ flang/test/Driver/omp-frontend-forwarding.f90
@@ -11,15 +11,15 @@
 ! RUN: %flang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP-IS-DEVICE %s
 ! CHECK-OPENMP-IS-DEVICE: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
 
-! Testing fembed-offload-object and fopenmp-is-device
+! Testing appropriate flags are gnerated and appropriately assigned by the driver when offloading
 ! RUN: %flang -S -### %s -o %t 2>&1 \
 ! RUN: -fopenmp --offload-arch=gfx90a \
 ! RUN: --target=aarch64-unknown-linux-gnu \
-! RUN:   | FileCheck %s --check-prefixes=CHECK-OPENMP-EMBED
-! CHECK-OPENMP-EMBED: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
-! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
-! CHECK-OPENMP-EMBED: "{{[^"]*}}clang-offload-packager{{.*}} "--image=file={{.*}}.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp"
-! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fembed-offload-object={{.*}}.out" {{.*}}.bc"
+! RUN:   | FileCheck %s --check-prefix=OPENMP-OFFLOAD-ARGS
+! OPENMP-OFFLOAD-ARGS: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
+! OPENMP-OFFLOAD-ARGS-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-host-ir-file-path" "{{.*}}.bc" "-fopen

[PATCH] D148370: [Clang][Flang][OpenMP] Add loadOffloadInfoMetadata and createOffloadEntriesAndInfoMetadata into OMPIRBuilder's finalize and initialize

2023-04-14 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon created this revision.
Herald added subscribers: bviyer, sunshaoce, Moerafaat, zero9178, bzcheeseman, 
awarzynski, sdasgup3, wenzhicui, wrengr, cota, teijeong, rdzhabarov, 
tatianashp, msifontes, jurahul, Kayjukh, grosul1, Joonsoo, liufengdb, aartbik, 
mgester, arpith-jacob, antiagainst, shauheen, rriddle, mehdi_amini, thopre, 
guansong, hiraditya, yaxunl.
Herald added a reviewer: ftynse.
Herald added a project: All.
agozillon requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: llvm-commits, cfe-commits, jplehr, sstefan1, 
stephenneuendorffer, nicolasvasilache.
Herald added projects: clang, MLIR, LLVM.

This allows the generation of OpenMP offload metadata for the OpenMP
dialect when lowering to LLVM-IR and moves some of the shared logic 
between the OpenMP Dialect and Clang into the IRBuilder.

This patch has a dependency on: https://reviews.llvm.org/D148038

As it requires the host ir file during lowering.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D148370

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/lib/Frontend/OpenMP/CMakeLists.txt
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h

Index: mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
===
--- mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
+++ mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
@@ -15,6 +15,7 @@
 #define MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
 
 #include "mlir/Dialect/LLVMIR/LLVMInterfaces.h"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/IR/Operation.h"
 #include "mlir/IR/SymbolTable.h"
 #include "mlir/IR/Value.h"
@@ -165,7 +166,15 @@
   llvm::OpenMPIRBuilder *getOpenMPBuilder() {
 if (!ompBuilder) {
   ompBuilder = std::make_unique(*llvmModule);
-  ompBuilder->initialize();
+
+  std::string hostIRFilePath{};
+  if (auto offloadMod =
+  dyn_cast(mlirModule)) {
+if (offloadMod.getIsDevice()) {
+  hostIRFilePath = offloadMod.getHostIRFilePath();
+}
+  }
+  ompBuilder->initialize(hostIRFilePath);
 }
 return ompBuilder.get();
   }
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -21,6 +21,7 @@
 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
 #include "llvm/Analysis/ScalarEvolution.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Bitcode/BitcodeReader.h"
 #include "llvm/IR/CFG.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DebugInfoMetadata.h"
@@ -445,7 +446,28 @@
   return Fn;
 }
 
-void OpenMPIRBuilder::initialize() { initializeTypes(M); }
+void OpenMPIRBuilder::initialize(StringRef HostFilePath) {
+  initializeTypes(M);
+
+  if (!HostFilePath.empty()) {
+auto Buf = llvm::MemoryBuffer::getFile(HostFilePath);
+if (auto Err = Buf.getError())
+  assert(false && ("error opening host file from host file path inside of "
+   "OpenMPIRBuilder" +
+   Err.message())
+  .c_str());
+
+llvm::LLVMContext Ctx;
+auto M = llvm::expectedToErrorOrAndEmitErrors(
+Ctx, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), Ctx));
+if (auto Err = M.getError())
+  assert(false && ("error parsing host file inside of OpenMPIRBuilder " +
+   Err.message())
+  .c_str());
+
+loadOffloadInfoMetadata(*M.get());
+  }
+}
 
 void OpenMPIRBuilder::finalize(Function *Fn) {
   SmallPtrSet ParallelRegionBlockSet;
@@ -534,6 +556,17 @@
 
   // Remove work items that have been completed.
   OutlineInfos = std::move(DeferredOutlines);
+
+  llvm::OpenMPIRBuilder::EmitMetadataErrorReportFunctionTy &&errorReportFn =
+  [](llvm::OpenMPIRBuilder::EmitMetadataErrorKind kind,
+ const llvm::TargetRegionEntryInfo &entryInfo) -> void {
+llvm::errs() << "Error of kind: " << kind
+ << " when emitting offload entries and metadata during "
+"OMPIRBuilder finalization \n";
+  };
+
+  if (!OffloadInfoManager.empty())
+createOffloadEntriesAndInfoMetadata(errorReportFn);
 }
 
 OpenMPIRBuilder::~OpenMPIRBuilder() {
Index: llvm/lib/Frontend/OpenMP/CMakeLists.txt
===
--- llvm/lib/Frontend/OpenMP/CMakeLists.txt
+++ llvm/lib/Frontend/OpenMP/CMakeLists.txt
@@ -19,4 +19,5 @@
   Analysis
   MC
   Scalar
+  BitReader
   )
Index: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
===
--- llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -420,7 +420,7 @@
   ///

[PATCH] D148370: [Clang][Flang][OpenMP] Add loadOffloadInfoMetadata and createOffloadEntriesAndInfoMetadata into OMPIRBuilder's finalize and initialize

2023-04-14 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

A little unsure how to test this change on its own for Flang-new + OpenMP, as 
there currently isn't anything that utilises it upstream at the moment. It's 
part of the declare target work that I'm beginning to upstream, so eventually 
it'll be tested on the Flang side through it, and the Target region work will 
also eventually utilise it.

As for Clang OpenMP, it currently doesn't break any existing tests on my 
machine for OpenMP on AMDGPU or host tests, however, I can't speak for other 
targets but I expect they will pass happily as well.

If there is additional tests I can add that might contribute to the patch 
please do ask away!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148370

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


[PATCH] D148370: [Clang][Flang][OpenMP] Add loadOffloadInfoMetadata and createOffloadEntriesAndInfoMetadata into OMPIRBuilder's finalize and initialize

2023-04-14 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added inline comments.



Comment at: mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h:174
+if (offloadMod.getIsDevice()) {
+  hostIRFilePath = offloadMod.getHostIRFilePath();
+}

This will break the patch buildbot unfortunately until the patch this patch is 
dependent on is accepted and upstreamed


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148370

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


[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-05-22 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 524279.
agozillon added a comment.

Rebase, squash patch history and clang-format to please the buildbot lords


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149162

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  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
@@ -5855,4 +5855,84 @@
   GlobalValue::WeakAnyLinkage);
   EXPECT_TRUE(InfoManager.hasDeviceGlobalVarEntryInfo("gvar"));
 }
+
+// Tests both registerTargetGlobalVariable and getAddrOfDeclareTargetVar as they
+// call each other (recursively in some cases). The test case test these
+// functions by utilising them for host code generation for declare target
+// global variables
+TEST_F(OpenMPIRBuilderTest, registerTargetGlobalVariable) {
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OMPBuilder.setConfig(Config);
+
+  std::vector TargetTriple;
+  TargetTriple.emplace_back("amdgcn-amd-amdhsa");
+
+  TargetRegionEntryInfo EntryInfo("", 42, 4711, 17);
+  std::vector RefsGathered;
+
+  std::vector Globals;
+  auto *IntTy = Type::getInt32Ty(Ctx);
+  for (int I = 0; I < 2; ++I) {
+Globals.push_back(M->getOrInsertGlobal(
+"test_data_int_" + std::to_string(I), IntTy, [&]() -> GlobalVariable * {
+  return new GlobalVariable(
+  *M, IntTy, false, GlobalValue::LinkageTypes::WeakAnyLinkage,
+  ConstantInt::get(IntTy, I), "test_data_int_" + std::to_string(I));
+}));
+  }
+
+  OMPBuilder.registerTargetGlobalVariable(
+  OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo,
+  OffloadEntriesInfoManager::OMPTargetDeviceClauseAny, false, true,
+  EntryInfo, Globals[0]->getName(), RefsGathered, false, TargetTriple,
+  nullptr, nullptr, Globals[0]->getType(), Globals[0]);
+
+  OMPBuilder.registerTargetGlobalVariable(
+  OffloadEntriesInfoManager::OMPTargetGlobalVarEntryLink,
+  OffloadEntriesInfoManager::OMPTargetDeviceClauseAny, false, true,
+  EntryInfo, Globals[1]->getName(), RefsGathered, false, TargetTriple,
+  nullptr, nullptr, Globals[1]->getType(), Globals[1]);
+
+  llvm::OpenMPIRBuilder::EmitMetadataErrorReportFunctionTy &&ErrorReportfn =
+  [](llvm::OpenMPIRBuilder::EmitMetadataErrorKind Kind,
+ const llvm::TargetRegionEntryInfo &EntryInfo) -> void {
+// If this is invoked, then we want to emit an error, even if it is not
+// neccesarily the most readable, as something has went wrong. The
+// test-suite unfortunately eats up all error output
+ASSERT_EQ(Kind, Kind);
+  };
+
+  OMPBuilder.createOffloadEntriesAndInfoMetadata(ErrorReportfn);
+
+  // Clauses for data_int_0 with To + Any clauses for the host
+  std::vector OffloadEntries;
+  OffloadEntries.push_back(M->getNamedGlobal(".omp_offloading.entry_name"));
+  OffloadEntries.push_back(
+  M->getNamedGlobal(".omp_offloading.entry.test_data_int_0"));
+
+  // Clauses for data_int_1 with Link + Any clauses for the host
+  OffloadEntries.push_back(
+  M->getNamedGlobal("test_data_int_1_decl_tgt_ref_ptr"));
+  OffloadEntries.push_back(M->getNamedGlobal(".omp_offloading.entry_name.1"));
+  OffloadEntries.push_back(M->getNamedGlobal(
+  ".omp_offloading.entry.test_data_int_1_decl_tgt_ref_ptr"));
+
+  for (unsigned I = 0; I < OffloadEntries.size(); ++I)
+EXPECT_NE(OffloadEntries[I], nullptr);
+
+  // Metadata generated for the host offload module
+  NamedMDNode *OffloadMetadata = M->getNamedMetadata("omp_offload.info");
+  EXPECT_NE(OffloadMetadata, nullptr);
+  if (OffloadMetadata) {
+EXPECT_EQ(OffloadMetadata->getOperand(0)->getOperand(1).equalsStr(
+  "test_data_int_0"),
+  true);
+EXPECT_EQ(OffloadMetadata->getOperand(1)->getOperand(1).equalsStr(
+  "test_data_int_1_decl_tgt_ref_ptr"),
+  true);
+  }
+}
+
 } // namespace
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -33,6 +33,7 @@
 #include "llvm/IR/Value.h"
 #include "llvm/MC/TargetRegistry.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetOptions.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
@@ -5130,7 +5131,8 @@
   static_cast(
   CE->getFlags());
   switch (Flags) {
-  case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo: {
+  case OffloadEn

[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-05-30 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

small ping for a further review on this if possible! Thank you very much ahead 
of time.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149162

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


[PATCH] D148370: [Clang][Flang][OpenMP] Add loadOffloadInfoMetadata and createOffloadEntriesAndInfoMetadata into OMPIRBuilder's finalize and initialize

2023-05-05 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

Hey @jdoerfert sorry to bother you, would it be possible to have this signed 
off on if there is no further issues with the current patch? Thank you for your 
time!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148370

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


[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-05-11 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 521528.
agozillon added a comment.

- [Clang][OpenMP][IRBuilder] Tidy up function calls with helpful reviewer advice
- [Clang][OpenMP][IRBuilder] Replace all getTargetEntryUniqueInfo with 
IRBuilder version
- [Clang][OpenMP][IRBuilder] Run clang-format and tidy up files
- Run clang-format on offending file breaking build process
- Address reviewers comments
- Add a new OpenMPIRBuilder that utilises registerTargetGlobalVariable (and by 
extension getAddrOfDeclareTargetVar) to generate host declare target data


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149162

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  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
@@ -5766,4 +5766,84 @@
   GlobalValue::WeakAnyLinkage);
   EXPECT_TRUE(InfoManager.hasDeviceGlobalVarEntryInfo("gvar"));
 }
+
+// Tests both registerTargetGlobalVariable and getAddrOfDeclareTargetVar as they
+// call each other (recursively in some cases). The test case test these
+// functions by utilising them for host code generation for declare target
+// global variables
+TEST_F(OpenMPIRBuilderTest, registerTargetGlobalVariable) {
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OMPBuilder.setConfig(Config);
+
+  std::vector TargetTriple;
+  TargetTriple.emplace_back("amdgcn-amd-amdhsa");
+
+  TargetRegionEntryInfo EntryInfo("", 42, 4711, 17);
+  std::vector RefsGathered;
+
+  std::vector Globals;
+  auto *IntTy = Type::getInt32Ty(Ctx);
+  for (int I = 0; I < 2; ++I) {
+Globals.push_back(M->getOrInsertGlobal(
+"test_data_int_" + std::to_string(I), IntTy, [&]() -> GlobalVariable * {
+  return new GlobalVariable(
+  *M, IntTy, false, GlobalValue::LinkageTypes::WeakAnyLinkage,
+  ConstantInt::get(IntTy, I), "test_data_int_" + std::to_string(I));
+}));
+  }
+
+  OMPBuilder.registerTargetGlobalVariable(
+  OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo,
+  OffloadEntriesInfoManager::OMPTargetDeviceClauseAny, false, true,
+  EntryInfo, Globals[0]->getName(), RefsGathered, false, TargetTriple,
+  nullptr, nullptr, Globals[0]->getType(), Globals[0]);
+
+  OMPBuilder.registerTargetGlobalVariable(
+  OffloadEntriesInfoManager::OMPTargetGlobalVarEntryLink,
+  OffloadEntriesInfoManager::OMPTargetDeviceClauseAny, false, true,
+  EntryInfo, Globals[1]->getName(), RefsGathered, false, TargetTriple,
+  nullptr, nullptr, Globals[1]->getType(), Globals[1]);
+
+  llvm::OpenMPIRBuilder::EmitMetadataErrorReportFunctionTy &&ErrorReportfn =
+  [](llvm::OpenMPIRBuilder::EmitMetadataErrorKind Kind,
+ const llvm::TargetRegionEntryInfo &EntryInfo) -> void {
+// If this is invoked, then we want to emit an error, even if it is not
+// neccesarily the most readable, as something has went wrong. The
+// test-suite unfortunately eats up all error output
+ASSERT_EQ(Kind, Kind);
+  };
+
+  OMPBuilder.createOffloadEntriesAndInfoMetadata(ErrorReportfn);
+
+  // Clauses for data_int_0 with To + Any clauses for the host
+  std::vector OffloadEntries;
+  OffloadEntries.push_back(M->getNamedGlobal(".omp_offloading.entry_name"));
+  OffloadEntries.push_back(
+  M->getNamedGlobal(".omp_offloading.entry.test_data_int_0"));
+
+  // Clauses for data_int_1 with Link + Any clauses for the host
+  OffloadEntries.push_back(
+  M->getNamedGlobal("test_data_int_1_decl_tgt_ref_ptr"));
+  OffloadEntries.push_back(M->getNamedGlobal(".omp_offloading.entry_name.1"));
+  OffloadEntries.push_back(M->getNamedGlobal(
+  ".omp_offloading.entry.test_data_int_1_decl_tgt_ref_ptr"));
+
+  for (unsigned I = 0; I < OffloadEntries.size(); ++I)
+EXPECT_NE(OffloadEntries[I], nullptr);
+
+  // Metadata generated for the host offload module
+  NamedMDNode *OffloadMetadata = M->getNamedMetadata("omp_offload.info");
+  EXPECT_NE(OffloadMetadata, nullptr);
+  if (OffloadMetadata) {
+EXPECT_EQ(OffloadMetadata->getOperand(0)->getOperand(1).equalsStr(
+  "test_data_int_0"),
+  true);
+EXPECT_EQ(OffloadMetadata->getOperand(1)->getOperand(1).equalsStr(
+  "test_data_int_1_decl_tgt_ref_ptr"),
+  true);
+  }
+}
+
 } // namespace
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -32,6 +32,7 @@
 #include "llvm/IR/Value.h"
 #include "llvm/MC/TargetRegistry.

[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-05-11 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

In the last commit I believe I addressed the last review comments and I added 
an OMPIRBuilderTest testing some of the functionality of the 
registerTargetGlobalVariable and getAddrOfDeclareTargetVar functionality! Just 
to maintain the standard of making tests for things added to the OMPIRBuilder 
(the functions are also tested rather profusely by Clang and LLVM already).


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149162

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


[PATCH] D148370: [Clang][Flang][OpenMP] Add loadOffloadInfoMetadata and createOffloadEntriesAndInfoMetadata into OMPIRBuilder's finalize and initialize

2023-05-15 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

If it would be possible to get some poor reviewers precious time on this I 
would greatly appreciate it! Thank you very much ahead of time.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148370

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


[PATCH] D148370: [Clang][Flang][OpenMP] Add loadOffloadInfoMetadata and createOffloadEntriesAndInfoMetadata into OMPIRBuilder's finalize and initialize

2023-05-15 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

Thank you very much @kiranchandramohan! More than happy to wait a day incase 
anyone else wishes to do a final review. I'll add the Nits where possible when 
I commit the patch upstream and it should hopefully update the review with the 
alterations!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148370

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


[PATCH] D148370: [Clang][Flang][OpenMP] Add loadOffloadInfoMetadata and createOffloadEntriesAndInfoMetadata into OMPIRBuilder's finalize and initialize

2023-05-16 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

I am going to upstream this within the next hour, unless anyone has some final 
comments they wish to make


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148370

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


[PATCH] D148370: [Clang][Flang][OpenMP] Add loadOffloadInfoMetadata and createOffloadEntriesAndInfoMetadata into OMPIRBuilder's finalize and initialize

2023-05-16 Thread Andrew Gozillon via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG48c3ae5cc3d4: [Clang][Flang][OpenMP] Add 
loadOffloadInfoMetadata and… (authored by agozillon).

Changed prior to commit:
  https://reviews.llvm.org/D148370?vs=519050&id=522671#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148370

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/lib/Frontend/OpenMP/CMakeLists.txt
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1274,19 +1274,23 @@
 llvm::OpenMPIRBuilder *ModuleTranslation::getOpenMPBuilder() {
   if (!ompBuilder) {
 ompBuilder = std::make_unique(*llvmModule);
-ompBuilder->initialize();
 
 bool isDevice = false;
+llvm::StringRef hostIRFilePath = "";
 if (auto offloadMod =
-dyn_cast(mlirModule))
+dyn_cast(mlirModule)) {
   isDevice = offloadMod.getIsDevice();
+  hostIRFilePath = offloadMod.getHostIRFilePath();
+}
+
+ompBuilder->initialize(hostIRFilePath);
 
 // TODO: set the flags when available
-llvm::OpenMPIRBuilderConfig Config(
+llvm::OpenMPIRBuilderConfig config(
 isDevice, /* IsTargetCodegen */ false,
 /* HasRequiresUnifiedSharedMemory */ false,
 /* OpenMPOffloadMandatory */ false);
-ompBuilder->setConfig(Config);
+ompBuilder->setConfig(config);
   }
   return ompBuilder.get();
 }
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -21,6 +21,7 @@
 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
 #include "llvm/Analysis/ScalarEvolution.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Bitcode/BitcodeReader.h"
 #include "llvm/IR/CFG.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DebugInfoMetadata.h"
@@ -445,7 +446,31 @@
   return Fn;
 }
 
-void OpenMPIRBuilder::initialize() { initializeTypes(M); }
+void OpenMPIRBuilder::initialize(StringRef HostFilePath) {
+  initializeTypes(M);
+
+  if (HostFilePath.empty())
+return;
+
+  auto Buf = MemoryBuffer::getFile(HostFilePath);
+  if (std::error_code Err = Buf.getError()) {
+report_fatal_error(("error opening host file from host file path inside of "
+"OpenMPIRBuilder: " +
+Err.message())
+   .c_str());
+  }
+
+  LLVMContext Ctx;
+  auto M = expectedToErrorOrAndEmitErrors(
+  Ctx, parseBitcodeFile(Buf.get()->getMemBufferRef(), Ctx));
+  if (std::error_code Err = M.getError()) {
+report_fatal_error(
+("error parsing host file inside of OpenMPIRBuilder: " + Err.message())
+.c_str());
+  }
+
+  loadOffloadInfoMetadata(*M.get());
+}
 
 void OpenMPIRBuilder::finalize(Function *Fn) {
   SmallPtrSet ParallelRegionBlockSet;
@@ -534,6 +559,17 @@
 
   // Remove work items that have been completed.
   OutlineInfos = std::move(DeferredOutlines);
+
+  EmitMetadataErrorReportFunctionTy &&ErrorReportFn =
+  [](EmitMetadataErrorKind Kind,
+ const TargetRegionEntryInfo &EntryInfo) -> void {
+errs() << "Error of kind: " << Kind
+   << " when emitting offload entries and metadata during "
+  "OMPIRBuilder finalization \n";
+  };
+
+  if (!OffloadInfoManager.empty())
+createOffloadEntriesAndInfoMetadata(ErrorReportFn);
 }
 
 OpenMPIRBuilder::~OpenMPIRBuilder() {
Index: llvm/lib/Frontend/OpenMP/CMakeLists.txt
===
--- llvm/lib/Frontend/OpenMP/CMakeLists.txt
+++ llvm/lib/Frontend/OpenMP/CMakeLists.txt
@@ -19,4 +19,5 @@
   Analysis
   MC
   Scalar
+  BitReader
   )
Index: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
===
--- llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -418,8 +418,14 @@
 
   /// Initialize the internal state, this will put structures types and
   /// potentially other helpers into the underlying module. Must be called
-  /// before any other method and only once!
-  void initialize();
+  /// before any other method and only once! This internal state includes
+  /// Types used in the OpenMPIRBuilder generated from OMPKinds.def as well
+  /// as loading offload metadata for device from the OpenMP host IR file
+  /// passed in as the HostFilePath argument.
+  /// \param HostFilePath The path to the host IR file, used to load in
+  /// offload metadata for the device, allo

[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-05-16 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 522710.
agozillon added a comment.

- [Clang][OpenMP][IRBuilder] Tidy up function calls with helpful reviewer advice
- [Clang][OpenMP][IRBuilder] Replace all getTargetEntryUniqueInfo with 
IRBuilder version
- [Clang][OpenMP][IRBuilder] Run clang-format and tidy up files
- Run clang-format on offending file breaking build process
- Address reviewers comments
- Add a new OpenMPIRBuilder that utilises registerTargetGlobalVariable (and by 
extension getAddrOfDeclareTargetVar) to generate host declare target data
- Add helper function to tidy things up a little as requested


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149162

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  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
@@ -5766,4 +5766,84 @@
   GlobalValue::WeakAnyLinkage);
   EXPECT_TRUE(InfoManager.hasDeviceGlobalVarEntryInfo("gvar"));
 }
+
+// Tests both registerTargetGlobalVariable and getAddrOfDeclareTargetVar as they
+// call each other (recursively in some cases). The test case test these
+// functions by utilising them for host code generation for declare target
+// global variables
+TEST_F(OpenMPIRBuilderTest, registerTargetGlobalVariable) {
+  OpenMPIRBuilder OMPBuilder(*M);
+  OMPBuilder.initialize();
+  OpenMPIRBuilderConfig Config(false, false, false, false);
+  OMPBuilder.setConfig(Config);
+
+  std::vector TargetTriple;
+  TargetTriple.emplace_back("amdgcn-amd-amdhsa");
+
+  TargetRegionEntryInfo EntryInfo("", 42, 4711, 17);
+  std::vector RefsGathered;
+
+  std::vector Globals;
+  auto *IntTy = Type::getInt32Ty(Ctx);
+  for (int I = 0; I < 2; ++I) {
+Globals.push_back(M->getOrInsertGlobal(
+"test_data_int_" + std::to_string(I), IntTy, [&]() -> GlobalVariable * {
+  return new GlobalVariable(
+  *M, IntTy, false, GlobalValue::LinkageTypes::WeakAnyLinkage,
+  ConstantInt::get(IntTy, I), "test_data_int_" + std::to_string(I));
+}));
+  }
+
+  OMPBuilder.registerTargetGlobalVariable(
+  OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo,
+  OffloadEntriesInfoManager::OMPTargetDeviceClauseAny, false, true,
+  EntryInfo, Globals[0]->getName(), RefsGathered, false, TargetTriple,
+  nullptr, nullptr, Globals[0]->getType(), Globals[0]);
+
+  OMPBuilder.registerTargetGlobalVariable(
+  OffloadEntriesInfoManager::OMPTargetGlobalVarEntryLink,
+  OffloadEntriesInfoManager::OMPTargetDeviceClauseAny, false, true,
+  EntryInfo, Globals[1]->getName(), RefsGathered, false, TargetTriple,
+  nullptr, nullptr, Globals[1]->getType(), Globals[1]);
+
+  llvm::OpenMPIRBuilder::EmitMetadataErrorReportFunctionTy &&ErrorReportfn =
+  [](llvm::OpenMPIRBuilder::EmitMetadataErrorKind Kind,
+ const llvm::TargetRegionEntryInfo &EntryInfo) -> void {
+// If this is invoked, then we want to emit an error, even if it is not
+// neccesarily the most readable, as something has went wrong. The
+// test-suite unfortunately eats up all error output
+ASSERT_EQ(Kind, Kind);
+  };
+
+  OMPBuilder.createOffloadEntriesAndInfoMetadata(ErrorReportfn);
+
+  // Clauses for data_int_0 with To + Any clauses for the host
+  std::vector OffloadEntries;
+  OffloadEntries.push_back(M->getNamedGlobal(".omp_offloading.entry_name"));
+  OffloadEntries.push_back(
+  M->getNamedGlobal(".omp_offloading.entry.test_data_int_0"));
+
+  // Clauses for data_int_1 with Link + Any clauses for the host
+  OffloadEntries.push_back(
+  M->getNamedGlobal("test_data_int_1_decl_tgt_ref_ptr"));
+  OffloadEntries.push_back(M->getNamedGlobal(".omp_offloading.entry_name.1"));
+  OffloadEntries.push_back(M->getNamedGlobal(
+  ".omp_offloading.entry.test_data_int_1_decl_tgt_ref_ptr"));
+
+  for (unsigned I = 0; I < OffloadEntries.size(); ++I)
+EXPECT_NE(OffloadEntries[I], nullptr);
+
+  // Metadata generated for the host offload module
+  NamedMDNode *OffloadMetadata = M->getNamedMetadata("omp_offload.info");
+  EXPECT_NE(OffloadMetadata, nullptr);
+  if (OffloadMetadata) {
+EXPECT_EQ(OffloadMetadata->getOperand(0)->getOperand(1).equalsStr(
+  "test_data_int_0"),
+  true);
+EXPECT_EQ(OffloadMetadata->getOperand(1)->getOperand(1).equalsStr(
+  "test_data_int_1_decl_tgt_ref_ptr"),
+  true);
+  }
+}
+
 } // namespace
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -33,6 +33,7 @@

[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-05-16 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

rebased and added helper function as recommended, in recent update!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149162

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


[PATCH] D148038: [Flang][OpenMP][Driver][MLIR] Port fopenmp-host-ir-file-path flag and add MLIR module attribute to proliferate to OpenMP IR lowering

2023-04-18 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

Just a small ping to keep this patch on peoples radar and to try and get some 
reviewer attention (I am aware you're all quite busy, so thank you very much 
for any time you can spare) for more review feedback to push this patch further 
along the pipeline!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148038

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


[PATCH] D145264: [OpenMP][MLIR][Flang][Driver][bbc] Lower and apply Module FlagsAttr

2023-04-19 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon abandoned this revision.
agozillon added a comment.
Herald added a subscriber: bviyer.

Closing this patch now that each individual component is upstreamed via 
seperate phabricator patches! Thank you to all the reviewers for your time and 
help.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145264

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


[PATCH] D148038: [Flang][OpenMP][Driver][MLIR] Port fopenmp-host-ir-file-path flag and add MLIR module attribute to proliferate to OpenMP IR lowering

2023-04-20 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 515305.
agozillon added a comment.

- Resolve some of the reviewer comments


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148038

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/LangOptions.h
  flang/include/flang/Tools/CrossToolHelpers.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/omp-frontend-forwarding.f90
  flang/test/Lower/OpenMP/omp-host-ir-flag.f90
  mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td

Index: mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
===
--- mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
+++ mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
@@ -71,7 +71,7 @@
   InterfaceMethod<
   /*description=*/[{
 Get the IsDeviceAttr attribute on the current module if it exists and return
-its value, if it doesn't exit it returns false by default.
+its value, if it doesn't exist it returns false by default.
   }],
   /*retTy=*/"bool",
   /*methodName=*/"getIsDevice",
@@ -138,6 +138,34 @@
  targetCPU.str(),
  targetFeatures.str()));
   }]>,
+  InterfaceMethod<
+  /*description=*/[{
+Set a StringAttr on the current module containing the host IR file path. This 
+file path is used in two-phase compilation during the device phase to generate
+device side LLVM IR when lowering MLIR. 
+  }],
+  /*retTy=*/"void",
+  /*methodName=*/"setHostIRFilePath", 
+  (ins "std::string":$hostIRFilePath), [{}], [{
+$_op->setAttr(
+  mlir::StringAttr::get($_op->getContext(), llvm::Twine{"omp.host_ir_filepath"}),
+mlir::StringAttr::get($_op->getContext(), hostIRFilePath));
+   }]>,
+  InterfaceMethod<
+  /*description=*/[{
+Find the host-ir file path StringAttr from the current module if it exists and 
+return its contained value, if it doesn't exist it returns an empty string. This 
+file path is used in two-phase compilation during the device phase to generate
+device side LLVM IR when lowering MLIR. 
+  }],
+  /*retTy=*/"llvm::StringRef",
+  /*methodName=*/"getHostIRFilePath", 
+  (ins), [{}], [{
+if (Attribute filepath = $_op->getAttr("omp.host_ir_filepath"))
+  if (filepath.isa())
+return filepath.dyn_cast().getValue();
+return {};
+  }]>
   ];
 }
 
Index: flang/test/Lower/OpenMP/omp-host-ir-flag.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/omp-host-ir-flag.f90
@@ -0,0 +1,6 @@
+!RUN: %flang_fc1 -emit-llvm-bc -fopenmp -o %t.bc %s 2>&1
+!RUN: %flang_fc1 -emit-mlir -fopenmp -fopenmp-is-device -fopenmp-host-ir-file-path %t.bc -o - %s 2>&1 | FileCheck %s
+
+!CHECK: module attributes {{{.*}}, omp.host_ir_filepath = "{{.*}}.bc", omp.is_device = #omp.isdevice{{.*}}}
+subroutine omp_subroutine()
+end subroutine omp_subroutine
Index: flang/test/Driver/omp-frontend-forwarding.f90
===
--- flang/test/Driver/omp-frontend-forwarding.f90
+++ flang/test/Driver/omp-frontend-forwarding.f90
@@ -11,15 +11,15 @@
 ! RUN: %flang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP-IS-DEVICE %s
 ! CHECK-OPENMP-IS-DEVICE: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
 
-! Testing fembed-offload-object and fopenmp-is-device
+! Testing appropriate flags are gnerated and appropriately assigned by the driver when offloading
 ! RUN: %flang -S -### %s -o %t 2>&1 \
 ! RUN: -fopenmp --offload-arch=gfx90a \
 ! RUN: --target=aarch64-unknown-linux-gnu \
-! RUN:   | FileCheck %s --check-prefixes=CHECK-OPENMP-EMBED
-! CHECK-OPENMP-EMBED: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
-! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
-! CHECK-OPENMP-EMBED: "{{[^"]*}}clang-offload-packager{{.*}} "--image=file={{.*}}.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp"
-! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fembed-offload-object={{.*}}.out" {{.*}}.bc"
+! RUN:   | FileCheck %s --check-prefix=OPENMP-OFFLOAD-ARGS
+! OPENMP-OFFLOAD-ARGS: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
+! OPENMP-OFFLOAD-ARGS-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-host-ir-file-path" "{

[PATCH] D148038: [Flang][OpenMP][Driver][MLIR] Port fopenmp-host-ir-file-path flag and add MLIR module attribute to proliferate to OpenMP IR lowering

2023-04-20 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon marked 4 inline comments as done.
agozillon added a comment.

Addressed 4/5 of your comments in the latest patch @awarzynski, I've replied to 
the final one to give a little more information to add to the final decision 
you make!




Comment at: flang/lib/Frontend/CompilerInvocation.cpp:730
+res.getLangOpts().OMPHostIRFile = arg->getValue();
+if (!llvm::sys::fs::exists(res.getLangOpts().OMPHostIRFile))
+  diags.Report(clang::diag::err_drv_omp_host_ir_file_not_found)

awarzynski wrote:
> I think that this is expecting a bit too much from `CompilerInvocation`. 
> Instead, whatever is going to use this file should complain if the file does 
> not exist (`CompilerInvocation` is merely a messenger here, and file I/O can 
> be quite expensive, hence my reservations).
> 
> Is it possible to create a test for "invalid file path" wherever this becomes 
> relevant? 
I believe the follow up patch I have here which uses the file, emits an error 
if it can't find it: https://reviews.llvm.org/D148370 
However, in this case it's an assert rather than more useful Diagnostics 
unfortunately. 

Although, this segment of code does currently just mimic what Clang does in 
it's own 
`CompilerInvocation`: 
https://github.com/llvm/llvm-project/blob/main/clang/lib/Frontend/CompilerInvocation.cpp#L3883
 

So it depends if we wish to try to maintain the status quo for the shared 
argument across the compiler frontends! 

So whichever you prefer, I am happy to remove the check at this level and let 
the lowering handle the problem if it arises :-) but I do like sharing the 
commonality with Clang where possible.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148038

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


[PATCH] D148038: [Flang][OpenMP][Driver][MLIR] Port fopenmp-host-ir-file-path flag and add MLIR module attribute to proliferate to OpenMP IR lowering

2023-04-24 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 516374.
agozillon added a comment.

- Resolve some of the reviewer comments
- [Flang][OpenMP][Driver][MLIR] Add diagnostic error test


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148038

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/LangOptions.h
  flang/include/flang/Tools/CrossToolHelpers.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/omp-driver-offload.f90
  flang/test/Lower/OpenMP/omp-host-ir-flag.f90
  mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td

Index: mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
===
--- mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
+++ mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
@@ -71,7 +71,7 @@
   InterfaceMethod<
   /*description=*/[{
 Get the IsDeviceAttr attribute on the current module if it exists and return
-its value, if it doesn't exit it returns false by default.
+its value, if it doesn't exist it returns false by default.
   }],
   /*retTy=*/"bool",
   /*methodName=*/"getIsDevice",
@@ -138,6 +138,34 @@
  targetCPU.str(),
  targetFeatures.str()));
   }]>,
+  InterfaceMethod<
+  /*description=*/[{
+Set a StringAttr on the current module containing the host IR file path. This 
+file path is used in two-phase compilation during the device phase to generate
+device side LLVM IR when lowering MLIR. 
+  }],
+  /*retTy=*/"void",
+  /*methodName=*/"setHostIRFilePath", 
+  (ins "std::string":$hostIRFilePath), [{}], [{
+$_op->setAttr(
+  mlir::StringAttr::get($_op->getContext(), llvm::Twine{"omp.host_ir_filepath"}),
+mlir::StringAttr::get($_op->getContext(), hostIRFilePath));
+   }]>,
+  InterfaceMethod<
+  /*description=*/[{
+Find the host-ir file path StringAttr from the current module if it exists and 
+return its contained value, if it doesn't exist it returns an empty string. This 
+file path is used in two-phase compilation during the device phase to generate
+device side LLVM IR when lowering MLIR. 
+  }],
+  /*retTy=*/"llvm::StringRef",
+  /*methodName=*/"getHostIRFilePath", 
+  (ins), [{}], [{
+if (Attribute filepath = $_op->getAttr("omp.host_ir_filepath"))
+  if (filepath.isa())
+return filepath.dyn_cast().getValue();
+return {};
+  }]>
   ];
 }
 
Index: flang/test/Lower/OpenMP/omp-host-ir-flag.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/omp-host-ir-flag.f90
@@ -0,0 +1,6 @@
+!RUN: %flang_fc1 -emit-llvm-bc -fopenmp -o %t.bc %s 2>&1
+!RUN: %flang_fc1 -emit-mlir -fopenmp -fopenmp-is-device -fopenmp-host-ir-file-path %t.bc -o - %s 2>&1 | FileCheck %s
+
+!CHECK: module attributes {{{.*}}, omp.host_ir_filepath = "{{.*}}.bc", omp.is_device = #omp.isdevice{{.*}}}
+subroutine omp_subroutine()
+end subroutine omp_subroutine
Index: flang/test/Driver/omp-driver-offload.f90
===
--- flang/test/Driver/omp-driver-offload.f90
+++ flang/test/Driver/omp-driver-offload.f90
@@ -47,15 +47,15 @@
 ! RUN: %flang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP-IS-DEVICE %s
 ! CHECK-OPENMP-IS-DEVICE: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
 
-! Testing fembed-offload-object and fopenmp-is-device
+! Testing appropriate flags are gnerated and appropriately assigned by the driver when offloading
 ! RUN: %flang -S -### %s -o %t 2>&1 \
 ! RUN: -fopenmp --offload-arch=gfx90a \
 ! RUN: --target=aarch64-unknown-linux-gnu \
-! RUN:   | FileCheck %s --check-prefixes=CHECK-OPENMP-EMBED
-! CHECK-OPENMP-EMBED: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
-! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
-! CHECK-OPENMP-EMBED: "{{[^"]*}}clang-offload-packager{{.*}} "--image=file={{.*}}.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp"
-! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fembed-offload-object={{.*}}.out" {{.*}}.bc"
+! RUN:   | FileCheck %s --check-prefix=OPENMP-OFFLOAD-ARGS
+! OPENMP-OFFLOAD-ARGS: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
+! OPENMP-OFFLOAD-ARGS-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" 

[PATCH] D148038: [Flang][OpenMP][Driver][MLIR] Port fopenmp-host-ir-file-path flag and add MLIR module attribute to proliferate to OpenMP IR lowering

2023-04-24 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon marked an inline comment as done.
agozillon added inline comments.



Comment at: flang/lib/Frontend/CompilerInvocation.cpp:730
+res.getLangOpts().OMPHostIRFile = arg->getValue();
+if (!llvm::sys::fs::exists(res.getLangOpts().OMPHostIRFile))
+  diags.Report(clang::diag::err_drv_omp_host_ir_file_not_found)

awarzynski wrote:
> agozillon wrote:
> > awarzynski wrote:
> > > I think that this is expecting a bit too much from `CompilerInvocation`. 
> > > Instead, whatever is going to use this file should complain if the file 
> > > does not exist (`CompilerInvocation` is merely a messenger here, and file 
> > > I/O can be quite expensive, hence my reservations).
> > > 
> > > Is it possible to create a test for "invalid file path" wherever this 
> > > becomes relevant? 
> > I believe the follow up patch I have here which uses the file, emits an 
> > error if it can't find it: https://reviews.llvm.org/D148370 
> > However, in this case it's an assert rather than more useful Diagnostics 
> > unfortunately. 
> > 
> > Although, this segment of code does currently just mimic what Clang does in 
> > it's own 
> > `CompilerInvocation`: 
> > https://github.com/llvm/llvm-project/blob/main/clang/lib/Frontend/CompilerInvocation.cpp#L3883
> >  
> > 
> > So it depends if we wish to try to maintain the status quo for the shared 
> > argument across the compiler frontends! 
> > 
> > So whichever you prefer, I am happy to remove the check at this level and 
> > let the lowering handle the problem if it arises :-) but I do like sharing 
> > the commonality with Clang where possible.
> Consistency with Clang is a good idea 👍🏻  Though I would appreciate a test 
> that demonstrates that this diagnostic is indeed issues when a non-existing 
> files is specified :)
Thank you for pointing out a test was required! I've added one now :-)


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148038

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


[PATCH] D148038: [Flang][OpenMP][Driver][MLIR] Port fopenmp-host-ir-file-path flag and add MLIR module attribute to proliferate to OpenMP IR lowering

2023-04-24 Thread Andrew Gozillon via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
agozillon marked an inline comment as done.
Closed by commit rG2e634367be6a: [Flang][OpenMP][Driver][MLIR] Port 
fopenmp-host-ir-file-path flag and add MLIR… (authored by agozillon).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148038

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Flang.cpp
  flang/include/flang/Frontend/LangOptions.h
  flang/include/flang/Tools/CrossToolHelpers.h
  flang/lib/Frontend/CompilerInvocation.cpp
  flang/test/Driver/driver-help.f90
  flang/test/Driver/omp-driver-offload.f90
  flang/test/Lower/OpenMP/omp-host-ir-flag.f90
  mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td

Index: mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
===
--- mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
+++ mlir/include/mlir/Dialect/OpenMP/OpenMPOpsInterfaces.td
@@ -71,7 +71,7 @@
   InterfaceMethod<
   /*description=*/[{
 Get the IsDeviceAttr attribute on the current module if it exists and return
-its value, if it doesn't exit it returns false by default.
+its value, if it doesn't exist it returns false by default.
   }],
   /*retTy=*/"bool",
   /*methodName=*/"getIsDevice",
@@ -138,6 +138,34 @@
  targetCPU.str(),
  targetFeatures.str()));
   }]>,
+  InterfaceMethod<
+  /*description=*/[{
+Set a StringAttr on the current module containing the host IR file path. This 
+file path is used in two-phase compilation during the device phase to generate
+device side LLVM IR when lowering MLIR. 
+  }],
+  /*retTy=*/"void",
+  /*methodName=*/"setHostIRFilePath", 
+  (ins "std::string":$hostIRFilePath), [{}], [{
+$_op->setAttr(
+  mlir::StringAttr::get($_op->getContext(), llvm::Twine{"omp.host_ir_filepath"}),
+mlir::StringAttr::get($_op->getContext(), hostIRFilePath));
+   }]>,
+  InterfaceMethod<
+  /*description=*/[{
+Find the host-ir file path StringAttr from the current module if it exists and 
+return its contained value, if it doesn't exist it returns an empty string. This 
+file path is used in two-phase compilation during the device phase to generate
+device side LLVM IR when lowering MLIR. 
+  }],
+  /*retTy=*/"llvm::StringRef",
+  /*methodName=*/"getHostIRFilePath", 
+  (ins), [{}], [{
+if (Attribute filepath = $_op->getAttr("omp.host_ir_filepath"))
+  if (filepath.isa())
+return filepath.dyn_cast().getValue();
+return {};
+  }]>
   ];
 }
 
Index: flang/test/Lower/OpenMP/omp-host-ir-flag.f90
===
--- /dev/null
+++ flang/test/Lower/OpenMP/omp-host-ir-flag.f90
@@ -0,0 +1,6 @@
+!RUN: %flang_fc1 -emit-llvm-bc -fopenmp -o %t.bc %s 2>&1
+!RUN: %flang_fc1 -emit-mlir -fopenmp -fopenmp-is-device -fopenmp-host-ir-file-path %t.bc -o - %s 2>&1 | FileCheck %s
+
+!CHECK: module attributes {{{.*}}, omp.host_ir_filepath = "{{.*}}.bc", omp.is_device = #omp.isdevice{{.*}}}
+subroutine omp_subroutine()
+end subroutine omp_subroutine
Index: flang/test/Driver/omp-driver-offload.f90
===
--- flang/test/Driver/omp-driver-offload.f90
+++ flang/test/Driver/omp-driver-offload.f90
@@ -47,15 +47,15 @@
 ! RUN: %flang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP-IS-DEVICE %s
 ! CHECK-OPENMP-IS-DEVICE: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
 
-! Testing fembed-offload-object and fopenmp-is-device
+! Testing appropriate flags are gnerated and appropriately assigned by the driver when offloading
 ! RUN: %flang -S -### %s -o %t 2>&1 \
 ! RUN: -fopenmp --offload-arch=gfx90a \
 ! RUN: --target=aarch64-unknown-linux-gnu \
-! RUN:   | FileCheck %s --check-prefixes=CHECK-OPENMP-EMBED
-! CHECK-OPENMP-EMBED: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90"
-! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90"
-! CHECK-OPENMP-EMBED: "{{[^"]*}}clang-offload-packager{{.*}} "--image=file={{.*}}.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp"
-! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fembed-offload-object={{.*}}.out" {{.*}}.bc"
+! RUN:   | FileCheck %s --check-prefix=OPENMP-OFFLOAD-ARGS
+! OPENMP-OFFLOAD-ARGS: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gn

[PATCH] D148038: [Flang][OpenMP][Driver][MLIR] Port fopenmp-host-ir-file-path flag and add MLIR module attribute to proliferate to OpenMP IR lowering

2023-04-24 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

In D148038#4292262 , @awarzynski 
wrote:

> LGTM, thanks for addressing my comments!

Thank you for your time and the great review comments as always!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148038

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


[PATCH] D148370: [Clang][Flang][OpenMP] Add loadOffloadInfoMetadata and createOffloadEntriesAndInfoMetadata into OMPIRBuilder's finalize and initialize

2023-04-24 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

The patch this is dependent on has now been accepted and landed, so this should 
be ready for review now if anyone is available!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148370

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


[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-04-25 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon created this revision.
Herald added subscribers: sunshaoce, bzcheeseman, rriddle, rogfer01, guansong, 
hiraditya, yaxunl.
Herald added a reviewer: sscalpone.
Herald added a project: All.
agozillon requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: llvm-commits, cfe-commits, jplehr, sstefan1, 
stephenneuendorffer.
Herald added projects: clang, LLVM.

This change tries to move registerTargetglobalVariable and
getAddrOfDeclareTargetVar out of Clang's CGOpenMPRuntime
and into the OMPIRBuilder for shared use with MLIR's OpenMPDialect
and Flang (or other languages that may want to utilise it).

This primarily does this by trying to hoist the Clang specific
types into arguments or callback functions in the form of
lambdas, replacing it with LLVM equivelants and
tilising shared OMPIRBuilder enumerators for
the clauses, rather than Clang's own variation.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149162

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  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
@@ -32,6 +32,7 @@
 #include "llvm/IR/Value.h"
 #include "llvm/MC/TargetRegistry.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetOptions.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
@@ -4999,7 +5000,8 @@
   static_cast(
   CE->getFlags());
   switch (Flags) {
-  case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo: {
+  case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryEnter:
+  case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo:
 if (Config.isEmbedded() && Config.hasRequiresUnifiedSharedMemory())
   continue;
 if (!CE->getAddress()) {
@@ -5010,7 +5012,6 @@
 if (CE->getVarSize() == 0)
   continue;
 break;
-  }
   case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryLink:
 assert(((Config.isEmbedded() && !CE->getAddress()) ||
 (!Config.isEmbedded() && CE->getAddress())) &&
@@ -5022,6 +5023,8 @@
   continue;
 }
 break;
+  default:
+  break;
   }
 
   // Hidden or internal symbols on the device are not externally visible.
@@ -5058,6 +5061,164 @@
   EntryInfo.Line, NewCount);
 }
 
+llvm::TargetRegionEntryInfo
+OpenMPIRBuilder::getTargetEntryUniqueInfo(StringRef FileName, uint64_t Line,
+  llvm::StringRef ParentName) {
+  llvm::sys::fs::UniqueID ID;
+  if (auto EC = llvm::sys::fs::getUniqueID(FileName, ID)) {
+assert(EC &&
+   "Unable to get unique ID for file, during getTargetEntryUniqueInfo");
+  }
+  return llvm::TargetRegionEntryInfo(ParentName, ID.getDevice(), ID.getFile(),
+ Line);
+}
+
+
+llvm::Constant *OpenMPIRBuilder::getAddrOfDeclareTargetVar(
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryKind CaptureClause,
+OffloadEntriesInfoManager::OMPTargetDeviceClauseKind DeviceClause,
+bool IsDeclaration, bool IsExternallyVisible, llvm::StringRef Filename,
+uint64_t Line, llvm::StringRef MangledName, llvm::Module *LlvmModule,
+std::vector &GeneratedRefs, bool OpenMPSIMD,
+bool OpenMPIsDevice, std::vector TargetTriple,
+llvm::Type *LlvmPtrTy, std::function GlobalInitializer,
+std::function VariableLinkage) {
+  // TODO: convert this to utilise the IRBuilder Config rather than
+  // a passed down argument.
+  if (OpenMPSIMD)
+return nullptr;
+
+  if (CaptureClause ==
+  OffloadEntriesInfoManager::OMPTargetGlobalVarEntryLink ||
+  ((CaptureClause ==
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo ||
+   CaptureClause ==
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryEnter) &&
+   Config.hasRequiresUnifiedSharedMemory())) {
+SmallString<64> PtrName;
+{
+  llvm::raw_svector_ostream OS(PtrName);
+  OS << MangledName;
+  if (!IsExternallyVisible) {
+auto EntryInfo = getTargetEntryUniqueInfo(Filename, Line);
+OS << llvm::format("_%x", EntryInfo.FileID);
+  }
+  OS << "_decl_tgt_ref_ptr";
+}
+
+llvm::Value *Ptr = LlvmModule->getNamedValue(PtrName);
+
+if (!Ptr) {
+  llvm::GlobalValue *GlobalValue = LlvmModule->getNamedValue(MangledName);
+  Ptr = getOrCreateInternalVariable(LlvmPtrTy, PtrName);
+
+  auto *GV = cast(Ptr);
+  GV->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
+
+  if (!OpenMPIsDevice) {
+if (GlobalInitializer)
+  GV->setInitializer(GlobalInitializer());
+else
+  GV->setInitializer(GlobalValue);
+  }
+
+  register

[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-04-25 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

I've tested this with the two following combinations, x86/AMDGPU and x86/NVPTX 
(newer plugin I think, not old, a little unsure on the runtime segment) and it 
passes the Clang test suites, if there is anything else I can do to make sure 
this is tested nicely and due diligence has been done please do mention it!

I admit the lengthy argument list is probably not the prettiest in the world, 
but the other option I could think of was an interface class of some kind that 
Clang+Flang could fill, or implement but that feels more like additional 
boilerplate than a fix.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149162

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


[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-04-26 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 517235.
agozillon added a comment.

- [Clang][OpenMP][IRBuilder] Tidy up function calls with helpful reviewer advice
- [Clang][OpenMP][IRBuilder] Replace all getTargetEntryUniqueInfo with 
IRBuilder version


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149162

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  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
@@ -32,6 +32,7 @@
 #include "llvm/IR/Value.h"
 #include "llvm/MC/TargetRegistry.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetOptions.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
@@ -4999,7 +5000,8 @@
   static_cast(
   CE->getFlags());
   switch (Flags) {
-  case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo: {
+  case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryEnter:
+  case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo:
 if (Config.isEmbedded() && Config.hasRequiresUnifiedSharedMemory())
   continue;
 if (!CE->getAddress()) {
@@ -5010,7 +5012,6 @@
 if (CE->getVarSize() == 0)
   continue;
 break;
-  }
   case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryLink:
 assert(((Config.isEmbedded() && !CE->getAddress()) ||
 (!Config.isEmbedded() && CE->getAddress())) &&
@@ -5022,6 +5023,8 @@
   continue;
 }
 break;
+  default:
+  break;
   }
 
   // Hidden or internal symbols on the device are not externally visible.
@@ -5058,6 +5061,163 @@
   EntryInfo.Line, NewCount);
 }
 
+TargetRegionEntryInfo
+OpenMPIRBuilder::getTargetEntryUniqueInfo(StringRef FileName, uint64_t Line,
+  StringRef ParentName) {
+  llvm::sys::fs::UniqueID ID;
+  if (auto EC = llvm::sys::fs::getUniqueID(FileName, ID)) {
+assert(EC &&
+   "Unable to get unique ID for file, during getTargetEntryUniqueInfo");
+  }
+  return llvm::TargetRegionEntryInfo(ParentName, ID.getDevice(), ID.getFile(),
+ Line);
+}
+
+
+Constant *OpenMPIRBuilder::getAddrOfDeclareTargetVar(
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryKind CaptureClause,
+OffloadEntriesInfoManager::OMPTargetDeviceClauseKind DeviceClause,
+bool IsDeclaration, bool IsExternallyVisible,
+TargetRegionEntryInfo EntryInfo, StringRef MangledName, Module *LlvmModule,
+std::vector &GeneratedRefs, bool OpenMPSIMD,
+std::vector TargetTriple,
+Type *LlvmPtrTy, std::function GlobalInitializer,
+std::function VariableLinkage) {
+  // TODO: convert this to utilise the IRBuilder Config rather than
+  // a passed down argument.
+  if (OpenMPSIMD)
+return nullptr;
+
+  if (CaptureClause ==
+  OffloadEntriesInfoManager::OMPTargetGlobalVarEntryLink ||
+  ((CaptureClause ==
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo ||
+   CaptureClause ==
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryEnter) &&
+   Config.hasRequiresUnifiedSharedMemory())) {
+SmallString<64> PtrName;
+   {
+  llvm::raw_svector_ostream OS(PtrName);
+  OS << MangledName;
+  if (!IsExternallyVisible)
+OS << llvm::format("_%x", EntryInfo.FileID);
+  OS << "_decl_tgt_ref_ptr";
+}
+
+llvm::Value *Ptr = LlvmModule->getNamedValue(PtrName);
+
+if (!Ptr) {
+  llvm::GlobalValue *GlobalValue = LlvmModule->getNamedValue(MangledName);
+  Ptr = getOrCreateInternalVariable(LlvmPtrTy, PtrName);
+
+  auto *GV = cast(Ptr);
+  GV->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
+
+  if (!Config.isEmbedded()) {
+if (GlobalInitializer)
+  GV->setInitializer(GlobalInitializer());
+else
+  GV->setInitializer(GlobalValue);
+  }
+
+ registerTargetGlobalVariable(
+  CaptureClause, DeviceClause, IsDeclaration, IsExternallyVisible,
+  EntryInfo, MangledName, LlvmModule, GeneratedRefs, OpenMPSIMD, 
+  TargetTriple, GlobalInitializer, VariableLinkage,
+  LlvmPtrTy, cast(Ptr));
+}
+
+return cast(Ptr);
+  }
+
+  return nullptr;
+}
+
+void OpenMPIRBuilder::registerTargetGlobalVariable(
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryKind CaptureClause,
+OffloadEntriesInfoManager::OMPTargetDeviceClauseKind DeviceClause,
+bool IsDeclaration, bool IsExternallyVisible,
+TargetRegionEntryInfo EntryInfo, llvm::StringRef MangledName,
+llvm::Module *LlvmModule,
+std::vector &GeneratedRefs, bool OpenMPSIMD,
+std::v

[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-04-26 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon marked 3 inline comments as done.
agozillon added a comment.

Updated the patch with your feedback @jsjodin, thank you very much!

And I've updated all getTargetEntryUniqueInfo locations, which I neglected to 
do previously in this patch.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149162

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


[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-04-26 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 517264.
agozillon added a comment.

- [Clang][OpenMP][IRBuilder] Run clang-format and tidy up files


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149162

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  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
@@ -32,6 +32,7 @@
 #include "llvm/IR/Value.h"
 #include "llvm/MC/TargetRegistry.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetOptions.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
@@ -4999,7 +5000,8 @@
   static_cast(
   CE->getFlags());
   switch (Flags) {
-  case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo: {
+  case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryEnter:
+  case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo:
 if (Config.isEmbedded() && Config.hasRequiresUnifiedSharedMemory())
   continue;
 if (!CE->getAddress()) {
@@ -5010,7 +5012,6 @@
 if (CE->getVarSize() == 0)
   continue;
 break;
-  }
   case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryLink:
 assert(((Config.isEmbedded() && !CE->getAddress()) ||
 (!Config.isEmbedded() && CE->getAddress())) &&
@@ -5022,6 +5023,8 @@
   continue;
 }
 break;
+  default:
+  break;
   }
 
   // Hidden or internal symbols on the device are not externally visible.
@@ -5058,6 +5061,161 @@
   EntryInfo.Line, NewCount);
 }
 
+TargetRegionEntryInfo
+OpenMPIRBuilder::getTargetEntryUniqueInfo(StringRef FileName, uint64_t Line,
+  StringRef ParentName) {
+  llvm::sys::fs::UniqueID ID;
+  if (auto EC = llvm::sys::fs::getUniqueID(FileName, ID)) {
+assert(EC &&
+   "Unable to get unique ID for file, during getTargetEntryUniqueInfo");
+  }
+  return llvm::TargetRegionEntryInfo(ParentName, ID.getDevice(), ID.getFile(),
+ Line);
+}
+
+Constant *OpenMPIRBuilder::getAddrOfDeclareTargetVar(
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryKind CaptureClause,
+OffloadEntriesInfoManager::OMPTargetDeviceClauseKind DeviceClause,
+bool IsDeclaration, bool IsExternallyVisible,
+TargetRegionEntryInfo EntryInfo, StringRef MangledName, Module *LlvmModule,
+std::vector &GeneratedRefs, bool OpenMPSIMD,
+std::vector TargetTriple, Type *LlvmPtrTy,
+std::function GlobalInitializer,
+std::function VariableLinkage) {
+  // TODO: convert this to utilise the IRBuilder Config rather than
+  // a passed down argument.
+  if (OpenMPSIMD)
+return nullptr;
+
+  if (CaptureClause ==
+  OffloadEntriesInfoManager::OMPTargetGlobalVarEntryLink ||
+  ((CaptureClause ==
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo ||
+   CaptureClause ==
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryEnter) &&
+   Config.hasRequiresUnifiedSharedMemory())) {
+SmallString<64> PtrName;
+{
+  llvm::raw_svector_ostream OS(PtrName);
+  OS << MangledName;
+  if (!IsExternallyVisible)
+OS << llvm::format("_%x", EntryInfo.FileID);
+  OS << "_decl_tgt_ref_ptr";
+}
+
+llvm::Value *Ptr = LlvmModule->getNamedValue(PtrName);
+
+if (!Ptr) {
+  llvm::GlobalValue *GlobalValue = LlvmModule->getNamedValue(MangledName);
+  Ptr = getOrCreateInternalVariable(LlvmPtrTy, PtrName);
+
+  auto *GV = cast(Ptr);
+  GV->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
+
+  if (!Config.isEmbedded()) {
+if (GlobalInitializer)
+  GV->setInitializer(GlobalInitializer());
+else
+  GV->setInitializer(GlobalValue);
+  }
+
+  registerTargetGlobalVariable(
+  CaptureClause, DeviceClause, IsDeclaration, IsExternallyVisible,
+  EntryInfo, MangledName, LlvmModule, GeneratedRefs, OpenMPSIMD,
+  TargetTriple, GlobalInitializer, VariableLinkage, LlvmPtrTy,
+  cast(Ptr));
+}
+
+return cast(Ptr);
+  }
+
+  return nullptr;
+}
+
+void OpenMPIRBuilder::registerTargetGlobalVariable(
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryKind CaptureClause,
+OffloadEntriesInfoManager::OMPTargetDeviceClauseKind DeviceClause,
+bool IsDeclaration, bool IsExternallyVisible,
+TargetRegionEntryInfo EntryInfo, llvm::StringRef MangledName,
+llvm::Module *LlvmModule,
+std::vector &GeneratedRefs, bool OpenMPSIMD,
+std::vector TargetTriple,
+std::function GlobalInitializer,
+std::function VariableLinkage,
+llvm::Typ

[PATCH] D148370: [Clang][Flang][OpenMP] Add loadOffloadInfoMetadata and createOffloadEntriesAndInfoMetadata into OMPIRBuilder's finalize and initialize

2023-04-27 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 517682.
agozillon added a comment.

- Move hostIRFilePath initialize invocation to ModuleTranslation.cpp to respect 
TargetOp patch


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148370

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/lib/Frontend/OpenMP/CMakeLists.txt
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
  mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1255,19 +1255,23 @@
 llvm::OpenMPIRBuilder *ModuleTranslation::getOpenMPBuilder() {
   if (!ompBuilder) {
 ompBuilder = std::make_unique(*llvmModule);
-ompBuilder->initialize();
 
 bool isDevice = false;
+llvm::StringRef hostIRFilePath = "";
 if (auto offloadMod =
-dyn_cast(mlirModule))
+dyn_cast(mlirModule)) {
   isDevice = offloadMod.getIsDevice();
+  hostIRFilePath = offloadMod.getHostIRFilePath();
+}
+
+ompBuilder->initialize(hostIRFilePath);
 
 // TODO: set the flags when available
-llvm::OpenMPIRBuilderConfig Config(
+llvm::OpenMPIRBuilderConfig config(
 isDevice, /* IsTargetCodegen */ false,
 /* HasRequiresUnifiedSharedMemory */ false,
 /* OpenMPOffloadMandatory */ false);
-ompBuilder->setConfig(Config);
+ompBuilder->setConfig(config);
   }
   return ompBuilder.get();
 }
Index: mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
===
--- mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
+++ mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
@@ -15,6 +15,7 @@
 #define MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
 
 #include "mlir/Dialect/LLVMIR/LLVMInterfaces.h"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/IR/Operation.h"
 #include "mlir/IR/SymbolTable.h"
 #include "mlir/IR/Value.h"
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -21,6 +21,7 @@
 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
 #include "llvm/Analysis/ScalarEvolution.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Bitcode/BitcodeReader.h"
 #include "llvm/IR/CFG.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DebugInfoMetadata.h"
@@ -445,7 +446,28 @@
   return Fn;
 }
 
-void OpenMPIRBuilder::initialize() { initializeTypes(M); }
+void OpenMPIRBuilder::initialize(StringRef HostFilePath) {
+  initializeTypes(M);
+
+  if (!HostFilePath.empty()) {
+auto Buf = llvm::MemoryBuffer::getFile(HostFilePath);
+if (auto Err = Buf.getError())
+  assert(false && ("error opening host file from host file path inside of "
+   "OpenMPIRBuilder" +
+   Err.message())
+  .c_str());
+
+llvm::LLVMContext Ctx;
+auto M = llvm::expectedToErrorOrAndEmitErrors(
+Ctx, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), Ctx));
+if (auto Err = M.getError())
+  assert(false && ("error parsing host file inside of OpenMPIRBuilder " +
+   Err.message())
+  .c_str());
+
+loadOffloadInfoMetadata(*M.get());
+  }
+}
 
 void OpenMPIRBuilder::finalize(Function *Fn) {
   SmallPtrSet ParallelRegionBlockSet;
@@ -534,6 +556,17 @@
 
   // Remove work items that have been completed.
   OutlineInfos = std::move(DeferredOutlines);
+
+  llvm::OpenMPIRBuilder::EmitMetadataErrorReportFunctionTy &&errorReportFn =
+  [](llvm::OpenMPIRBuilder::EmitMetadataErrorKind kind,
+ const llvm::TargetRegionEntryInfo &entryInfo) -> void {
+llvm::errs() << "Error of kind: " << kind
+ << " when emitting offload entries and metadata during "
+"OMPIRBuilder finalization \n";
+  };
+
+  if (!OffloadInfoManager.empty())
+createOffloadEntriesAndInfoMetadata(errorReportFn);
 }
 
 OpenMPIRBuilder::~OpenMPIRBuilder() {
Index: llvm/lib/Frontend/OpenMP/CMakeLists.txt
===
--- llvm/lib/Frontend/OpenMP/CMakeLists.txt
+++ llvm/lib/Frontend/OpenMP/CMakeLists.txt
@@ -19,4 +19,5 @@
   Analysis
   MC
   Scalar
+  BitReader
   )
Index: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
===
--- llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -419,7 +419,7 @@
   /// Initialize the internal state, this will put structures types and
  

[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-04-28 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 517958.
agozillon added a comment.

- [Clang][OpenMP][IRBuilder] Tidy up function calls with helpful reviewer advice
- [Clang][OpenMP][IRBuilder] Replace all getTargetEntryUniqueInfo with 
IRBuilder version
- [Clang][OpenMP][IRBuilder] Run clang-format and tidy up files
- Run clang-format on offending file breaking build process


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149162

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  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
@@ -32,6 +32,7 @@
 #include "llvm/IR/Value.h"
 #include "llvm/MC/TargetRegistry.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetOptions.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
@@ -5081,7 +5082,8 @@
   static_cast(
   CE->getFlags());
   switch (Flags) {
-  case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo: {
+  case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryEnter:
+  case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo:
 if (Config.isEmbedded() && Config.hasRequiresUnifiedSharedMemory())
   continue;
 if (!CE->getAddress()) {
@@ -5092,7 +5094,6 @@
 if (CE->getVarSize() == 0)
   continue;
 break;
-  }
   case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryLink:
 assert(((Config.isEmbedded() && !CE->getAddress()) ||
 (!Config.isEmbedded() && CE->getAddress())) &&
@@ -5104,6 +5105,8 @@
   continue;
 }
 break;
+  default:
+break;
   }
 
   // Hidden or internal symbols on the device are not externally visible.
@@ -5140,6 +5143,157 @@
   EntryInfo.Line, NewCount);
 }
 
+TargetRegionEntryInfo
+OpenMPIRBuilder::getTargetEntryUniqueInfo(StringRef FileName, uint64_t Line,
+  StringRef ParentName) {
+  llvm::sys::fs::UniqueID ID;
+  if (auto EC = llvm::sys::fs::getUniqueID(FileName, ID)) {
+assert(EC &&
+   "Unable to get unique ID for file, during getTargetEntryUniqueInfo");
+  }
+  return llvm::TargetRegionEntryInfo(ParentName, ID.getDevice(), ID.getFile(),
+ Line);
+}
+
+Constant *OpenMPIRBuilder::getAddrOfDeclareTargetVar(
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryKind CaptureClause,
+OffloadEntriesInfoManager::OMPTargetDeviceClauseKind DeviceClause,
+bool IsDeclaration, bool IsExternallyVisible,
+TargetRegionEntryInfo EntryInfo, StringRef MangledName, Module *LlvmModule,
+std::vector &GeneratedRefs, bool OpenMPSIMD,
+std::vector TargetTriple, Type *LlvmPtrTy,
+std::function GlobalInitializer,
+std::function VariableLinkage) {
+  // TODO: convert this to utilise the IRBuilder Config rather than
+  // a passed down argument.
+  if (OpenMPSIMD)
+return nullptr;
+
+  if (CaptureClause == OffloadEntriesInfoManager::OMPTargetGlobalVarEntryLink ||
+  ((CaptureClause == OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo ||
+CaptureClause ==
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryEnter) &&
+   Config.hasRequiresUnifiedSharedMemory())) {
+SmallString<64> PtrName;
+{
+  llvm::raw_svector_ostream OS(PtrName);
+  OS << MangledName;
+  if (!IsExternallyVisible)
+OS << llvm::format("_%x", EntryInfo.FileID);
+  OS << "_decl_tgt_ref_ptr";
+}
+
+llvm::Value *Ptr = LlvmModule->getNamedValue(PtrName);
+
+if (!Ptr) {
+  llvm::GlobalValue *GlobalValue = LlvmModule->getNamedValue(MangledName);
+  Ptr = getOrCreateInternalVariable(LlvmPtrTy, PtrName);
+
+  auto *GV = cast(Ptr);
+  GV->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
+
+  if (!Config.isEmbedded()) {
+if (GlobalInitializer)
+  GV->setInitializer(GlobalInitializer());
+else
+  GV->setInitializer(GlobalValue);
+  }
+
+  registerTargetGlobalVariable(
+  CaptureClause, DeviceClause, IsDeclaration, IsExternallyVisible,
+  EntryInfo, MangledName, LlvmModule, GeneratedRefs, OpenMPSIMD,
+  TargetTriple, GlobalInitializer, VariableLinkage, LlvmPtrTy,
+  cast(Ptr));
+}
+
+return cast(Ptr);
+  }
+
+  return nullptr;
+}
+
+void OpenMPIRBuilder::registerTargetGlobalVariable(
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryKind CaptureClause,
+OffloadEntriesInfoManager::OMPTargetDeviceClauseKind DeviceClause,
+bool IsDeclaration, bool IsExternallyVisible,
+TargetRegionEntryInfo EntryInfo, llvm::StringRef Man

[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-04-28 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

rebased and clang-formatted the *hopefully* final file clang-format is angry at.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149162

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


[PATCH] D148370: [Clang][Flang][OpenMP] Add loadOffloadInfoMetadata and createOffloadEntriesAndInfoMetadata into OMPIRBuilder's finalize and initialize

2023-04-28 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 517968.
agozillon added a comment.

- Move hostIRFilePath initialize invocation to ModuleTranslation.cpp to respect 
TargetOp patch


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148370

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/lib/Frontend/OpenMP/CMakeLists.txt
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
  mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1259,19 +1259,23 @@
 llvm::OpenMPIRBuilder *ModuleTranslation::getOpenMPBuilder() {
   if (!ompBuilder) {
 ompBuilder = std::make_unique(*llvmModule);
-ompBuilder->initialize();
 
 bool isDevice = false;
+llvm::StringRef hostIRFilePath = "";
 if (auto offloadMod =
-dyn_cast(mlirModule))
+dyn_cast(mlirModule)) {
   isDevice = offloadMod.getIsDevice();
+  hostIRFilePath = offloadMod.getHostIRFilePath();
+}
+
+ompBuilder->initialize(hostIRFilePath);
 
 // TODO: set the flags when available
-llvm::OpenMPIRBuilderConfig Config(
+llvm::OpenMPIRBuilderConfig config(
 isDevice, /* IsTargetCodegen */ false,
 /* HasRequiresUnifiedSharedMemory */ false,
 /* OpenMPOffloadMandatory */ false);
-ompBuilder->setConfig(Config);
+ompBuilder->setConfig(config);
   }
   return ompBuilder.get();
 }
Index: mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
===
--- mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
+++ mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
@@ -15,6 +15,7 @@
 #define MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
 
 #include "mlir/Dialect/LLVMIR/LLVMInterfaces.h"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/IR/Operation.h"
 #include "mlir/IR/SymbolTable.h"
 #include "mlir/IR/Value.h"
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -21,6 +21,7 @@
 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
 #include "llvm/Analysis/ScalarEvolution.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Bitcode/BitcodeReader.h"
 #include "llvm/IR/CFG.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DebugInfoMetadata.h"
@@ -445,7 +446,28 @@
   return Fn;
 }
 
-void OpenMPIRBuilder::initialize() { initializeTypes(M); }
+void OpenMPIRBuilder::initialize(StringRef HostFilePath) {
+  initializeTypes(M);
+
+  if (!HostFilePath.empty()) {
+auto Buf = llvm::MemoryBuffer::getFile(HostFilePath);
+if (auto Err = Buf.getError())
+  assert(false && ("error opening host file from host file path inside of "
+   "OpenMPIRBuilder" +
+   Err.message())
+  .c_str());
+
+llvm::LLVMContext Ctx;
+auto M = llvm::expectedToErrorOrAndEmitErrors(
+Ctx, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), Ctx));
+if (auto Err = M.getError())
+  assert(false && ("error parsing host file inside of OpenMPIRBuilder " +
+   Err.message())
+  .c_str());
+
+loadOffloadInfoMetadata(*M.get());
+  }
+}
 
 void OpenMPIRBuilder::finalize(Function *Fn) {
   SmallPtrSet ParallelRegionBlockSet;
@@ -534,6 +556,17 @@
 
   // Remove work items that have been completed.
   OutlineInfos = std::move(DeferredOutlines);
+
+  llvm::OpenMPIRBuilder::EmitMetadataErrorReportFunctionTy &&errorReportFn =
+  [](llvm::OpenMPIRBuilder::EmitMetadataErrorKind kind,
+ const llvm::TargetRegionEntryInfo &entryInfo) -> void {
+llvm::errs() << "Error of kind: " << kind
+ << " when emitting offload entries and metadata during "
+"OMPIRBuilder finalization \n";
+  };
+
+  if (!OffloadInfoManager.empty())
+createOffloadEntriesAndInfoMetadata(errorReportFn);
 }
 
 OpenMPIRBuilder::~OpenMPIRBuilder() {
Index: llvm/lib/Frontend/OpenMP/CMakeLists.txt
===
--- llvm/lib/Frontend/OpenMP/CMakeLists.txt
+++ llvm/lib/Frontend/OpenMP/CMakeLists.txt
@@ -19,4 +19,5 @@
   Analysis
   MC
   Scalar
+  BitReader
   )
Index: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
===
--- llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -419,7 +419,7 @@
   /// Initialize the internal state, this will put structures types and
  

[PATCH] D148370: [Clang][Flang][OpenMP] Add loadOffloadInfoMetadata and createOffloadEntriesAndInfoMetadata into OMPIRBuilder's finalize and initialize

2023-04-28 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

rebase to see if it clears up the buildbot issue (currently the failing test 
appears to be unrelated and passes on my machine after a rebase)


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148370

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


[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-05-02 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 518759.
agozillon added a comment.

Rebase on an updated main to see if it fixes bolt error (like it appears to on 
my local machine)


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149162

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  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
@@ -32,6 +32,7 @@
 #include "llvm/IR/Value.h"
 #include "llvm/MC/TargetRegistry.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetOptions.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
@@ -5081,7 +5082,8 @@
   static_cast(
   CE->getFlags());
   switch (Flags) {
-  case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo: {
+  case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryEnter:
+  case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo:
 if (Config.isEmbedded() && Config.hasRequiresUnifiedSharedMemory())
   continue;
 if (!CE->getAddress()) {
@@ -5092,7 +5094,6 @@
 if (CE->getVarSize() == 0)
   continue;
 break;
-  }
   case OffloadEntriesInfoManager::OMPTargetGlobalVarEntryLink:
 assert(((Config.isEmbedded() && !CE->getAddress()) ||
 (!Config.isEmbedded() && CE->getAddress())) &&
@@ -5104,6 +5105,8 @@
   continue;
 }
 break;
+  default:
+break;
   }
 
   // Hidden or internal symbols on the device are not externally visible.
@@ -5140,6 +5143,157 @@
   EntryInfo.Line, NewCount);
 }
 
+TargetRegionEntryInfo
+OpenMPIRBuilder::getTargetEntryUniqueInfo(StringRef FileName, uint64_t Line,
+  StringRef ParentName) {
+  llvm::sys::fs::UniqueID ID;
+  if (auto EC = llvm::sys::fs::getUniqueID(FileName, ID)) {
+assert(EC &&
+   "Unable to get unique ID for file, during getTargetEntryUniqueInfo");
+  }
+  return llvm::TargetRegionEntryInfo(ParentName, ID.getDevice(), ID.getFile(),
+ Line);
+}
+
+Constant *OpenMPIRBuilder::getAddrOfDeclareTargetVar(
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryKind CaptureClause,
+OffloadEntriesInfoManager::OMPTargetDeviceClauseKind DeviceClause,
+bool IsDeclaration, bool IsExternallyVisible,
+TargetRegionEntryInfo EntryInfo, StringRef MangledName, Module *LlvmModule,
+std::vector &GeneratedRefs, bool OpenMPSIMD,
+std::vector TargetTriple, Type *LlvmPtrTy,
+std::function GlobalInitializer,
+std::function VariableLinkage) {
+  // TODO: convert this to utilise the IRBuilder Config rather than
+  // a passed down argument.
+  if (OpenMPSIMD)
+return nullptr;
+
+  if (CaptureClause == OffloadEntriesInfoManager::OMPTargetGlobalVarEntryLink ||
+  ((CaptureClause == OffloadEntriesInfoManager::OMPTargetGlobalVarEntryTo ||
+CaptureClause ==
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryEnter) &&
+   Config.hasRequiresUnifiedSharedMemory())) {
+SmallString<64> PtrName;
+{
+  llvm::raw_svector_ostream OS(PtrName);
+  OS << MangledName;
+  if (!IsExternallyVisible)
+OS << llvm::format("_%x", EntryInfo.FileID);
+  OS << "_decl_tgt_ref_ptr";
+}
+
+llvm::Value *Ptr = LlvmModule->getNamedValue(PtrName);
+
+if (!Ptr) {
+  llvm::GlobalValue *GlobalValue = LlvmModule->getNamedValue(MangledName);
+  Ptr = getOrCreateInternalVariable(LlvmPtrTy, PtrName);
+
+  auto *GV = cast(Ptr);
+  GV->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
+
+  if (!Config.isEmbedded()) {
+if (GlobalInitializer)
+  GV->setInitializer(GlobalInitializer());
+else
+  GV->setInitializer(GlobalValue);
+  }
+
+  registerTargetGlobalVariable(
+  CaptureClause, DeviceClause, IsDeclaration, IsExternallyVisible,
+  EntryInfo, MangledName, LlvmModule, GeneratedRefs, OpenMPSIMD,
+  TargetTriple, GlobalInitializer, VariableLinkage, LlvmPtrTy,
+  cast(Ptr));
+}
+
+return cast(Ptr);
+  }
+
+  return nullptr;
+}
+
+void OpenMPIRBuilder::registerTargetGlobalVariable(
+OffloadEntriesInfoManager::OMPTargetGlobalVarEntryKind CaptureClause,
+OffloadEntriesInfoManager::OMPTargetDeviceClauseKind DeviceClause,
+bool IsDeclaration, bool IsExternallyVisible,
+TargetRegionEntryInfo EntryInfo, llvm::StringRef MangledName,
+llvm::Module *LlvmModule,
+std::vector &GeneratedRefs, bool OpenMPSIMD,
+std::vector TargetTriple,
+std::function GlobalInitializer,
+std::function VariableLinkage,
+

[PATCH] D148370: [Clang][Flang][OpenMP] Add loadOffloadInfoMetadata and createOffloadEntriesAndInfoMetadata into OMPIRBuilder's finalize and initialize

2023-05-02 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

Small ping to ask for some reviewer attention on this patch if at all possible!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148370

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


[PATCH] D149162: [Clang][OpenMP][IRBuilder] Move registerTargetGlobalVariable & getAddrOfDeclareTargetVar into the OMPIRBuilder

2023-05-02 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon added a comment.

A small ping to ask for some reviewer attention on this patch if at all 
possible please! Thank you for your time as always, it is greatly appreciated.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D149162

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


[PATCH] D148370: [Clang][Flang][OpenMP] Add loadOffloadInfoMetadata and createOffloadEntriesAndInfoMetadata into OMPIRBuilder's finalize and initialize

2023-05-02 Thread Andrew Gozillon via Phabricator via cfe-commits
agozillon updated this revision to Diff 518875.
agozillon marked 4 inline comments as done.
agozillon added a comment.

- Move hostIRFilePath initialize invocation to ModuleTranslation.cpp to respect 
TargetOp patch
- Apply reviewer feedback


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148370

Files:
  clang/lib/CodeGen/CGOpenMPRuntime.cpp
  clang/lib/CodeGen/CGOpenMPRuntime.h
  llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
  llvm/lib/Frontend/OpenMP/CMakeLists.txt
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
  mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
===
--- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
+++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
@@ -1259,19 +1259,23 @@
 llvm::OpenMPIRBuilder *ModuleTranslation::getOpenMPBuilder() {
   if (!ompBuilder) {
 ompBuilder = std::make_unique(*llvmModule);
-ompBuilder->initialize();
 
 bool isDevice = false;
+llvm::StringRef hostIRFilePath = "";
 if (auto offloadMod =
-dyn_cast(mlirModule))
+dyn_cast(mlirModule)) {
   isDevice = offloadMod.getIsDevice();
+  hostIRFilePath = offloadMod.getHostIRFilePath();
+}
+
+ompBuilder->initialize(hostIRFilePath);
 
 // TODO: set the flags when available
-llvm::OpenMPIRBuilderConfig Config(
+llvm::OpenMPIRBuilderConfig config(
 isDevice, /* IsTargetCodegen */ false,
 /* HasRequiresUnifiedSharedMemory */ false,
 /* OpenMPOffloadMandatory */ false);
-ompBuilder->setConfig(Config);
+ompBuilder->setConfig(config);
   }
   return ompBuilder.get();
 }
Index: mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
===
--- mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
+++ mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h
@@ -15,6 +15,7 @@
 #define MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
 
 #include "mlir/Dialect/LLVMIR/LLVMInterfaces.h"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
 #include "mlir/IR/Operation.h"
 #include "mlir/IR/SymbolTable.h"
 #include "mlir/IR/Value.h"
Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
===
--- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -21,6 +21,7 @@
 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
 #include "llvm/Analysis/ScalarEvolution.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Bitcode/BitcodeReader.h"
 #include "llvm/IR/CFG.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DebugInfoMetadata.h"
@@ -445,7 +446,29 @@
   return Fn;
 }
 
-void OpenMPIRBuilder::initialize() { initializeTypes(M); }
+void OpenMPIRBuilder::initialize(StringRef HostFilePath) {
+  initializeTypes(M);
+
+  if (HostFilePath.empty())
+return;
+
+  auto Buf = MemoryBuffer::getFile(HostFilePath);
+  if (auto Err = Buf.getError())
+report_fatal_error(("error opening host file from host file path inside of "
+"OpenMPIRBuilder: " +
+Err.message())
+   .c_str());
+
+  LLVMContext Ctx;
+  auto M = expectedToErrorOrAndEmitErrors(
+  Ctx, parseBitcodeFile(Buf.get()->getMemBufferRef(), Ctx));
+  if (auto Err = M.getError())
+report_fatal_error(
+("error parsing host file inside of OpenMPIRBuilder: " + Err.message())
+.c_str());
+
+  loadOffloadInfoMetadata(*M.get());
+}
 
 void OpenMPIRBuilder::finalize(Function *Fn) {
   SmallPtrSet ParallelRegionBlockSet;
@@ -534,6 +557,17 @@
 
   // Remove work items that have been completed.
   OutlineInfos = std::move(DeferredOutlines);
+
+  llvm::OpenMPIRBuilder::EmitMetadataErrorReportFunctionTy &&errorReportFn =
+  [](llvm::OpenMPIRBuilder::EmitMetadataErrorKind kind,
+ const llvm::TargetRegionEntryInfo &entryInfo) -> void {
+llvm::errs() << "Error of kind: " << kind
+ << " when emitting offload entries and metadata during "
+"OMPIRBuilder finalization \n";
+  };
+
+  if (!OffloadInfoManager.empty())
+createOffloadEntriesAndInfoMetadata(errorReportFn);
 }
 
 OpenMPIRBuilder::~OpenMPIRBuilder() {
Index: llvm/lib/Frontend/OpenMP/CMakeLists.txt
===
--- llvm/lib/Frontend/OpenMP/CMakeLists.txt
+++ llvm/lib/Frontend/OpenMP/CMakeLists.txt
@@ -19,4 +19,5 @@
   Analysis
   MC
   Scalar
+  BitReader
   )
Index: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
===
--- llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -418,8 +418,14 @@
 
   /// Initialize the internal state, this will put s

  1   2   >