[PATCH] D98134: [WIP][RFC] Introduce callback argument encoding mode into callback metadata

Shilei Tian via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 6 20:07:16 PST 2021


tianshilei1992 created this revision.
Herald added subscribers: dexonsmith, okura, kuter, hiraditya.
Herald added a reviewer: aaron.ballman.
tianshilei1992 requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added a reviewer: jdoerfert.
Herald added a reviewer: sstefan1.
Herald added subscribers: llvm-commits, cfe-commits, bbn, sstefan1.
Herald added a reviewer: baziotis.
Herald added projects: clang, LLVM.

Current `!callback` metadata assumes that arguments of callback function
are encoded in broker function arguments, as the following example shows:

  void callback(int, float, double);
  void broker(void(*cb)(int, float, double), int, float, double);

We call the argument encoding as "flatten mode". However, it is not always the
case. For some programming model, such as CUDA and OpenMP offloading, arguments
are "stacked":

  void callback(int *, float *, double *);
  void broker(void(*cb)(int *, float *, double *), void **args, size_t size) {
    // ...
    cb(args[0], args[1], args[2]);
  }

Let's call this encode as "stacked mode" (a more approporiate name is welcomed).
Current `!callback` metadata cannot handle this case very well. In fact, we can
establish connection between callback function arguments and `args` passed to
the broker function via the use chain of `args`. For example, before we call the
broker function, we need to construct `args` by setting its every element. In
this way, we can know which pointer eventually corresponds to which callback
function argument.

In order to do that, we need to tell apart the two different parameter encoding
modes. In this patch, the `!callback` metadata is updated in the following way:

1. The first operand is an `i64` value, representing parameter encoding mode. 0 is "flatten mode", and 1 is "stacked mode".
2. The second operand is the callback function index, same as the previous first operand.
3. The next one or more operands are still parameter encoding with a little difference. If the encoding mode is 0, it is same as current way. If the mode is 1, there must only be two operands: one is the index of the base pointer (e.g. the index of `void **args` above), and the second is the index of its size (e.g. the index of `size_t size` above).
4. The last operand is still for the variadic arguments.

The 3rd pointer above is not covered by this patch for now. It's probably worth
another patch. This patch only add the support for encoding mode, so an extra
`i64 0` should be added everywhere, and for now it can only be 0. The update of
documents will be covered by this patch as well, which will be done soon.

P.S. People might argue in CUDA or OpenMP offloading, the broker function
actually doesn't accept a callback function pointer. It's usually a global
symbol which can be used to find the entry kernel function during the runtime.
More important, the kernel function (or "callback" function) cannot be seen in
the host module at all. What's the point of doing this? Yes, that's true for now.
We're working on the heterogenous module, which basically will "merge" (or "link")
kernel moduel and host module together. In this way, we can optimize them with
the information from both host and kernel size. We can bridge the kernel function
and broker function via the global symbol somehow to make the kernel function a
"virtual" callback function. Although pointers in `void **args` will not be used
by the kernel function directly (as shown in the example above), the real
arguments passed to the kernel function are 1:1 mapped from pointers in `args`.
Therefore the correspondence still exists.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D98134

Files:
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/CodeGen/attr-callback.c
  clang/test/CodeGen/callback_annotated.c
  clang/test/CodeGen/callback_openmp.c
  clang/test/CodeGenCXX/attr-callback.cpp
  clang/test/OpenMP/parallel_codegen.cpp
  clang/test/Sema/attr-callback-broken.c
  clang/test/Sema/attr-callback.c
  clang/test/SemaCXX/attr-callback.cpp
  llvm/include/llvm/IR/AbstractCallSite.h
  llvm/include/llvm/IR/MDBuilder.h
  llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
  llvm/lib/IR/AbstractCallSite.cpp
  llvm/lib/IR/MDBuilder.cpp
  llvm/test/Analysis/CallGraph/callback-calls.ll
  llvm/test/Analysis/CallGraph/ignore-callback-uses.ll
  llvm/test/Transforms/Attributor/IPConstantProp/multiple_callbacks.ll
  llvm/test/Transforms/Attributor/IPConstantProp/openmp_parallel_for.ll
  llvm/test/Transforms/Attributor/IPConstantProp/pthreads.ll
  llvm/test/Transforms/Attributor/IPConstantProp/thread_local_acs.ll
  llvm/test/Transforms/Attributor/callbacks.ll
  llvm/test/Transforms/Attributor/noundef.ll
  llvm/test/Transforms/OpenMP/parallel_deletion.ll
  llvm/test/Transforms/OpenMP/parallel_deletion_cg_update.ll
  llvm/test/Transforms/OpenMP/parallel_deletion_remarks.ll
  llvm/test/Transforms/OpenMP/parallel_region_merging.ll
  llvm/unittests/IR/AbstractCallSiteTest.cpp
  llvm/unittests/IR/LegacyPassManagerTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98134.328832.patch
Type: text/x-patch
Size: 48684 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210307/6ae123f0/attachment-0001.bin>


More information about the cfe-commits mailing list