[Openmp-commits] [PATCH] D142508: [OpenMP][libomptarget] Fix alignment calculation for mapping struct members.

George Rokos via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Tue Jan 24 15:37:52 PST 2023


grokos added a comment.

Padding was added to libomptarget in order to ensure 8-byte struct members remain properly aligned, e.g. pointers, doubles etc. If you remove padding, then such a member may find itself in a non 8-aligned address, making its access result in a segfault.

For instance, in the code you provided, add a third struct member `k` of type `double`

  struct S {
    int i;
    int j;
    double k;
  }

and partially map the struct `map(s.j, s.k)`. There are architectures (e.g. CUDA for sure) where `memalloc` will always return an 8-aligned (or even higher) address. If we don't add a 4-byte padding at the beginning of the struct on the device, `j` will start at an address of the form `0x...0`, then `k` will find itself at an address of the form `0x...4`, i.e. `k` will not be 8-aligned. Try accessing it on virtually any architecture and you'll get a segfault. By adding 4 dummy bytes at the beginning of the struct, `j` finds itself at `0x...4` and `k` at `0x...8`, which is what we need to ensure.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D142508



More information about the Openmp-commits mailing list