Commit 7eb899cb authored by Huber, Joseph's avatar Huber, Joseph
Browse files

[OpenMP] Add more verbose remarks for runtime folding

We peform runtime folding, but do not currently emit remarks when it is
performed. This is because it comes from the runtime library and is
beyond the users control. However, people may still wish to view  this
and similar information easily, so we can enable this behaviour using a
special flag to enable verbose remarks.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D109627
parent 8dae3552
Loading
Loading
Loading
Loading
+23 −4
Original line number Diff line number Diff line
@@ -102,6 +102,11 @@ static cl::opt<bool> AlwaysInlineDeviceFunctions(
    cl::desc("Inline all applicible functions on the device."), cl::Hidden,
    cl::init(false));

static cl::opt<bool>
    EnableVerboseRemarks("openmp-opt-verbose-remarks", cl::ZeroOrMore,
                         cl::desc("Enables more verbose remarks."), cl::Hidden,
                         cl::init(false));

STATISTIC(NumOpenMPRuntimeCallsDeduplicated,
          "Number of OpenMP runtime calls deduplicated");
STATISTIC(NumOpenMPParallelRegionsDeleted,
@@ -4034,11 +4039,25 @@ struct AAFoldRuntimeCallCallSiteReturned : AAFoldRuntimeCall {
    ChangeStatus Changed = ChangeStatus::UNCHANGED;

    if (SimplifiedValue.hasValue() && SimplifiedValue.getValue()) {
      Instruction &CB = *getCtxI();
      A.changeValueAfterManifest(CB, **SimplifiedValue);
      A.deleteAfterManifest(CB);
      Instruction &I = *getCtxI();
      A.changeValueAfterManifest(I, **SimplifiedValue);
      A.deleteAfterManifest(I);

      CallBase *CB = dyn_cast<CallBase>(&I);
      auto Remark = [&](OptimizationRemark OR) {
        if (auto *C = dyn_cast<ConstantInt>(*SimplifiedValue))
          return OR << "Replacing OpenMP runtime call "
                    << CB->getCalledFunction()->getName() << " with "
                    << ore::NV("FoldedValue", C->getZExtValue()) << ".";
        else
          return OR << "Replacing OpenMP runtime call "
                    << CB->getCalledFunction()->getName() << ".";
      };

      if (CB && EnableVerboseRemarks)
        A.emitRemark<OptimizationRemark>(CB, "OMP180", Remark);

      LLVM_DEBUG(dbgs() << TAG << "Folding runtime call: " << CB << " with "
      LLVM_DEBUG(dbgs() << TAG << "Replacing runtime call: " << I << " with "
                        << **SimplifiedValue << "\n");

      Changed = ChangeStatus::CHANGED;
+38 −0
Original line number Diff line number Diff line
.. _omp180:

Replacing OpenMP runtime call <call> with <value>.
====================================================================

This optimization remark indicates that analysis determined an OpenMP runtime 
calls can be replaced with a constant value. This can occur when an OpenMP 
runtime call that queried some internal state was found to always return a 
single value after analysis.

Example
-------

This optimization will trigger for most target regions to simplify the runtime 
once certain constants are known. This will trigger for internal runtime 
functions so it requires enabling verbose remarks with 
`-openmp-opt-verbose-remarks`.

.. code-block:: c++

  void foo() {
  #pragma omp target parallel
    { }
  }

.. code-block:: console

  $ clang test.c -fopenmp -fopenmp-targets=nvptx64 -O1 -Rpass=openmp-opt \
    -mllvm -openmp-opt-verbose-remarks
  remark: Replacing runtime call __kmpc_is_spmd_exec_mode with 1. [OMP180] [-Rpass=openmp-opt]
  remark: Replacing runtime call __kmpc_is_spmd_exec_mode with 1. [OMP180] [-Rpass=openmp-opt]
  remark: Replacing runtime call __kmpc_parallel_level with 1. [OMP180] [-Rpass=openmp-opt]
  remark: Replacing runtime call __kmpc_parallel_level with 1. [OMP180] [-Rpass=openmp-opt]

Diagnostic Scope
----------------

OpenMP optimization remark.
+4 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ OpenMP Remarks
   OMP150
   OMP160
   OMP170
   OMP180

.. list-table::
   :widths: 15 15 70
@@ -107,3 +108,6 @@ OpenMP Remarks
   * - :ref:`OMP170 <omp170>`
     - Optimization
     - OpenMP runtime call <call> deduplicated.
   * - :ref:`OMP180 <omp180>`
     - Optimization
     - Replacing OpenMP runtime call <call> with <value>.