This is an archive of the discontinued LLVM Phabricator instance.

[Clang][OpenMP] Find the type `omp_allocator_handle_t` from identifier table
ClosedPublic

Authored by tianshilei1992 on Jan 21 2023, 8:43 PM.

Details

Summary

In Clang, in order to determine the type of omp_allocator_handle_t, Clang
checks the type of those predefined allocators. The first one it checks is
omp_null_allocator. If the language is C, and the system is 64-bit, what Clang
gets is a int, instead of an enum of size 8, given the fact how we define
omp_allocator_handle_t in omp.h. If the allocator is captured by a region,
let's say a parallel region, the allocator will be privatized. Because Clang deems
omp_allocator_handle_t as an int, it will first cast the value returned by
the runtime library (for libomp it is a void *) to int, and then in the
outlined function, it casts back to omp_allocator_handle_t. This two casts
completely shaves the first 32-bit of the pointer value returned from libomp,
and when the private "new" pointer is fed to another runtime function
__kmpc_allocate(), it causes segment fault. That is the root cause of PR54082.
I have no idea why -fno-pic could hide this bug.

In this patch, we detect omp_allocator_handle_t using roughly the same method
as omp_event_handle_t, by looking it up into the identifier table.

Fix #54082.

Diff Detail

Event Timeline

tianshilei1992 created this revision.Jan 21 2023, 8:43 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 21 2023, 8:43 PM
tianshilei1992 requested review of this revision.Jan 21 2023, 8:43 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJan 21 2023, 8:43 PM
tianshilei1992 edited the summary of this revision. (Show Details)Jan 21 2023, 8:52 PM
ABataev added inline comments.Jan 22 2023, 5:51 AM
clang/test/OpenMP/target_uses_allocators.c
108

Would be good to have a test that shows the difference.

rebase and fix comments

tianshilei1992 marked an inline comment as done.Jan 24 2023, 5:14 PM
ABataev added inline comments.Jan 24 2023, 5:20 PM
clang/test/OpenMP/target_uses_allocators.c
108

A unit test with the difference? This test does not show anything.

tianshilei1992 marked an inline comment as done.Jan 24 2023, 5:21 PM
tianshilei1992 added inline comments.
clang/test/OpenMP/target_uses_allocators.c
108

I added a front end test above. It shows the key difference when call __kmpc_alloc.

tianshilei1992 marked an inline comment as done.Jan 24 2023, 5:21 PM

use compiler builtin for types

This revision is now accepted and ready to land.Jan 24 2023, 5:41 PM
This revision was landed with ongoing or failed builds.Jan 24 2023, 7:49 PM
This revision was automatically updated to reflect the committed changes.

This change broke the parallel/bug54082.c testcase in the OpenMP runtime test set, when running on Windows (both mingw and MSVC configurations, and happening both in i386 and x86_64 builds), see e.g. https://github.com/mstorsjo/llvm-mingw/actions/runs/4011290068/jobs/6891673869. It's failing with error code 0xC00000FD, which means STATUS_STACK_OVERFLOW. Is this change missing some aspect that differs between the unix and Windows ABIs?

So please don't backport this to the release branch right now. And I'd appreciate if you would consider to revert it if there's no immediately obvious fix in sight.

tianshilei1992 added a comment.EditedJan 26 2023, 6:57 AM

This change broke the parallel/bug54082.c testcase in the OpenMP runtime test set, when running on Windows (both mingw and MSVC configurations, and happening both in i386 and x86_64 builds), see e.g. https://github.com/mstorsjo/llvm-mingw/actions/runs/4011290068/jobs/6891673869. It's failing with error code 0xC00000FD, which means STATUS_STACK_OVERFLOW. Is this change missing some aspect that differs between the unix and Windows ABIs?

So please don't backport this to the release branch right now. And I'd appreciate if you would consider to revert it if there's no immediately obvious fix in sight.

Thanks for the information. I'll install a Windows VM and try to fix it and get it back ported to the release branch as this patch has already been included.