<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hi,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Another possibility (like with OpenACC dialect) is to use the GPU dialect in mlir to model target regions. </div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
The gpu dialect can be converted to other vendor dialects like nvvm, rocdl. During these conversions, calls to math library functions are converted to calls to device library functions</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
For e.g: the following call to the cos math function is converted to either of the calls to <span style="font-family: Calibri, Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255); display: inline !important">__nv_exp or <span style="font-family: Calibri, Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255); display: inline !important">__ocml_cos_f64
below depending on the conversion chosen.</span></span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
%result64 = std.cos %arg_f64 : f64</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
<div> %1 = llvm.call @__nv_cos(%arg1) : (!llvm.double) -> !llvm.double<br>
%1 = llvm.call @__ocml_cos_f64(%arg1) : (!llvm.double) -> !llvm.double<br>
</div>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<a href="https://github.com/llvm/llvm-project/blob/647e9a54c758a6fdd85a569f019f00a653b2bc40/mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm.mlir#L183" id="LPNoLPOWALinkPreview_0">https://github.com/llvm/llvm-project/blob/647e9a54c758a6fdd85a569f019f00a653b2bc40/mlir/test/Conversion/GPUToNVVM/gpu-to-nvvm.mlir#L183</a><br>
</div>
<div class="_Entity _EType_OWALinkPreview _EId_OWALinkPreview_0 _EReadonly_1"><a href="https://github.com/llvm/llvm-project/blob/73c12bd8ff1a9cd8375a357ea06f171e127ec1b8/mlir/test/Conversion/GPUToROCDL/gpu-to-rocdl.mlir#L125" id="LPlnk943028">https://github.com/llvm/llvm-project/blob/73c12bd8ff1a9cd8375a357ea06f171e127ec1b8/mlir/test/Conversion/GPUToROCDL/gpu-to-rocdl.mlir#L125</a></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
--Kiran</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> flang-dev <flang-dev-bounces@lists.llvm.org> on behalf of Kiran Chandramohan via flang-dev <flang-dev@lists.llvm.org><br>
<b>Sent:</b> 30 July 2020 00:20<br>
<b>To:</b> Jean Perier <jperier@nvidia.com>; Romero, Nichols A. <naromero@anl.gov>; flang-dev@lists.llvm.org <flang-dev@lists.llvm.org><br>
<b>Cc:</b> Doerfert, Johannes <jdoerfert@anl.gov><br>
<b>Subject:</b> Re: [flang-dev] OpenMP target regions and intrinsic Fortran math functions</font>
<div> </div>
</div>
<style type="text/css" style="display:none">
<!--
p
{margin-top:0;
margin-bottom:0}
-->
</style>
<div dir="ltr">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Hi Nick, Jean,<br>
<br>
Thanks for bringing this topic up. I must confess that I am not an expert in target and device handling in OpenMP and we have not yet finalized the approach for handling target regions. <br>
<br>
But here is what I can share, <span style="color:rgb(0,0,0); font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">the GPU folks from Nvidia/AMD and Johannes (who implemented this in Clang) can correct me here.</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="color:rgb(0,0,0); font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt"><br>
</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="color:rgb(0,0,0); font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">Vendors provide device libraries (</span><a href="https://docs.nvidia.com/cuda/libdevice-users-guide/__nv_sin.html" style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">https://docs.nvidia.com/cuda/libdevice-users-guide/__nv_sin.html</a><span style="color:rgb(0,0,0); font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">)
with math function support. The compiler can/should convert calls to the math library functions in a target region with calls to the device library functions. OpenMP provides the declare variant directive with which specialized variants of functions and the
context in which these functions should be used can be specified. This mechanism can be used in a header file and each vendor can declare variants (with calls to their device library) for each math function. If the frontend supports OpenMP declare variant
handling then the calls to math library functions are automatically converted to calls to device library functions.<br>
<br>
For e.g: Clang has the following,<br>
<br>
1) clang/lib/Headers/openmp_wrappers/math.h<br>
#pragma omp begin declare variant match( \
<div> device = {arch(nvptx, nvptx64)}, implementation = {extension(match_any)})</div>
<div><br>
</div>
<div>#define __CUDA__</div>
<div>#define __OPENMP_NVPTX__</div>
<div>#include <__clang_cuda_math.h></div>
<div>#undef __OPENMP_NVPTX__</div>
<div>#undef __CUDA__</div>
<div><br>
</div>
#pragma omp end declare variant<br>
<br>
2) clang/lib/Headers/__clang_cuda_math.h<br>
__DEVICE__ double sin(double __a) { return __nv_sin(__a); }<br>
</span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
Thanks,<br>
--Kiran</div>
<div id="x_appendonsend"></div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="x_divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> Jean Perier <jperier@nvidia.com><br>
<b>Sent:</b> 27 July 2020 10:59<br>
<b>To:</b> Romero, Nichols A. <naromero@anl.gov>; flang-dev@lists.llvm.org <flang-dev@lists.llvm.org><br>
<b>Cc:</b> Kiran Chandramohan <Kiran.Chandramohan@arm.com><br>
<b>Subject:</b> RE: [flang-dev] OpenMP target regions and intrinsic Fortran math functions</font>
<div> </div>
</div>
<style>
<!--
@font-face
{font-family:"Cambria Math"}
@font-face
{font-family:Calibri}
@font-face
{font-family:Verdana}
p.x_x_MsoNormal, li.x_x_MsoNormal, div.x_x_MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Calibri",sans-serif}
.x_x_MsoChpDefault
{font-size:10.0pt}
@page WordSection1
{margin:1.0in 1.0in 1.0in 1.0in}
-->
</style>
<div lang="EN-US">
<div class="x_x_WordSection1">
<p class="x_x_MsoNormal"><span style="font-size:11.0pt">Hi,</span></p>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt"> </span></p>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt">Kiran Chandramohan is working on OpenMP lowering and might have a plan here. Math intrinsics are currently lowered in mlir to a mix of inlined code, calls to llvm intrinsics, and calls to runtime when
the first two options are not possible. Attributes are added to these runtime calls so that they can easily be identified as intrinsic calls and later rewrote if needed. These attributes could be adapted based on what the team working on OpenMP lowering needs
here.</span></p>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt"> </span></p>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt">Jean</span></p>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt"> </span></p>
<div>
<div style="border:none; border-top:solid #E1E1E1 1.0pt; padding:3.0pt 0in 0in 0in">
<p class="x_x_MsoNormal"><b><span style="font-size:11.0pt">From:</span></b><span style="font-size:11.0pt"> flang-dev <flang-dev-bounces@lists.llvm.org>
<b>On Behalf Of </b>Romero, Nichols A. via flang-dev<br>
<b>Sent:</b> Wednesday, July 22, 2020 8:26 PM<br>
<b>To:</b> flang-dev@lists.llvm.org<br>
<b>Subject:</b> [flang-dev] OpenMP target regions and intrinsic Fortran math functions</span></p>
</div>
</div>
<p class="x_x_MsoNormal"> </p>
<table class="x_x_MsoNormalTable" border="1" cellspacing="3" cellpadding="0" style="background:#FFEB9C">
<tbody>
<tr>
<td style="padding:.75pt .75pt .75pt .75pt">
<p class="x_x_MsoNormal"><b><span style="font-size:7.5pt; font-family:"Verdana",sans-serif; color:black">External email: Use caution opening links or attachments</span></b><span style="font-size:7.5pt; font-family:"Verdana",sans-serif; color:black">
</span><span style="font-size:11.0pt"></span></p>
</td>
</tr>
</tbody>
</table>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt"> </span></p>
<div>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt">Hi,</span></p>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt"> </span></p>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt">I am bringing this up because it seems that OpenMP development is really ramping up now and I want to bring up a common use case that does not seem to be supported with other vendor compilers.</span></p>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt"> </span></p>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt">It is a common use case to call the Fortran intrinsic Math functions in an openmp target region. I am not sure how this in implemented in the Fortran + OpenMP compilers for the vendors who do support it.
I suspect this is done by inlining, but it appears that other vendors have their Fortran math functions in a backend runtime library which somehow prevents these functions from being called in an OpenMP target region.</span></p>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt"> </span></p>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt"> </span></p>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt"> </span></p>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt">-- </span></p>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt">Nichols A. Romero, Ph.D.</span></p>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt">Computational Science Division</span></p>
<p class="x_x_MsoNormal"><span style="font-size:11.0pt">Argonne Leadership Computing Facility<br>
Argonne National Laboratory<br>
Building 240 Room 2-127<br>
9700 South Cass Avenue<br>
Lemont, IL 60439<br>
(630) 252-3441</span></p>
<p class="x_x_MsoNormal"> </p>
</div>
</div>
</div>
</div>
</body>
</html>