3530 Commits

Author SHA1 Message Date
dpalermo
d26ea02060
Revert "Fix Debug Build Using GCC 15" (#154877)
Reverts llvm/llvm-project#152223
2025-08-21 21:54:58 -05:00
Patrick Simmons
304373fb6d
Fix Debug Build Using GCC 15 (#152223)
Flang currently doesn't build in debug mode on GCC 15 due to missing
dynamic libraries in some CMakeLists.txt files, and OpenMP doesn't link
in debug mode due to the atomic library pulling in libstdc++ despite an
incomplete attempt in the CMakeLists.txt to disable glibcxx assertions.
This PR fixes these issues and allows Flang and the OpenMP runtime to
build and link on GCC 15 in debug mode.

---------

Co-authored-by: ronlieb <ron.lieberman@amd.com>
2025-08-21 18:28:01 -04:00
Jan Patrick Lehr
9ccdadd2a5
Revert "[OpenMP] Add ompTest library to OpenMP" (#154742)
Reverts llvm/llvm-project#147381

A few buildbot failures for different reasons.
2025-08-21 14:09:07 +02:00
Michael Halkenhäuser
35f01cea65
[OpenMP] Add ompTest library to OpenMP (#147381)
Description
===========
OpenMP Tooling Interface Testing Library (ompTest) ompTest is a unit
testing framework for testing OpenMP implementations. It offers a
simple-to-use framework that allows a tester to check for OMPT events in
addition to regular unit testing code, supported by linking against
GoogleTest by default. It also facilitates writing concise tests while
bridging the semantic gap between the unit under test and the OMPT-event
testing.

Background
==========
This library has been developed to provide the means of testing OMPT
implementations with reasonable effort. Especially, asynchronous or
unordered events are supported and can be verified with ease, which may
prove to be challenging with LIT-based tests. Additionally, since the
assertions are part of the code being tested, ompTest can reference all
corresponding variables during assertion.

Basic Usage
===========
OMPT event assertions are placed before the code, which shall be tested.
These assertion can either be provided as one block or interleaved with
the test code. There are two types of asserters: (1) sequenced
"order-sensitive" and (2) set "unordered" assserters. Once the test is
being run, the corresponding events are triggered by the OpenMP runtime
and can be observed. Each of these observed events notifies asserters,
which then determine if the test should pass or fail.

Example (partial, interleaved)
==============================
```c++
  int N = 100000;
  int a[N];
  int b[N];

  OMPT_ASSERT_SEQUENCE(Target, TARGET, BEGIN, 0);
  OMPT_ASSERT_SEQUENCE(TargetDataOp, ALLOC, N * sizeof(int)); // a ?
  OMPT_ASSERT_SEQUENCE(TargetDataOp, H2D, N * sizeof(int), &a);
  OMPT_ASSERT_SEQUENCE(TargetDataOp, ALLOC, N * sizeof(int)); // b ?
  OMPT_ASSERT_SEQUENCE(TargetDataOp, H2D, N * sizeof(int), &b);
  OMPT_ASSERT_SEQUENCE(TargetSubmit, 1);
  OMPT_ASSERT_SEQUENCE(TargetDataOp, D2H, N * sizeof(int), nullptr, &b);
  OMPT_ASSERT_SEQUENCE(TargetDataOp, D2H, N * sizeof(int), nullptr, &a);
  OMPT_ASSERT_SEQUENCE(TargetDataOp, DELETE);
  OMPT_ASSERT_SEQUENCE(TargetDataOp, DELETE);
  OMPT_ASSERT_SEQUENCE(Target, TARGET, END, 0);

#pragma omp target parallel for
  {
    for (int j = 0; j < N; j++)
      a[j] = b[j];
  }
```

References
==========
This work has been presented at SC'24 workshops, see:
https://ieeexplore.ieee.org/document/10820689

Current State and Future Work
=============================
ompTest's development was mostly device-centric and aimed at OMPT device
callbacks and device-side tracing. Consequentially, a substantial part
of host-related events or features may not be supported in its current
state. However, we are confident that the related functionality can be
added and ompTest provides a general foundation for future OpenMP and
especially OMPT testing. This PR will allow us to upstream the
corresponding features, like OMPT device-side tracing in the future with
significantly reduced risk of introducing regressions in the process.

Build
=====
ompTest is linked against LLVM's GoogleTest by default, but can also be
built 'standalone'. Additionally, it comes with a set of unit tests,
which in turn require GoogleTest (overriding a standalone build). The
unit tests are added to the `check-openmp` target.

Use the following parameters to perform the corresponding build: 
`LIBOMPTEST_BUILD_STANDALONE` (Default: ${OPENMP_STANDALONE_BUILD})
`LIBOMPTEST_BUILD_UNITTESTS` (Default: OFF)

---------

Co-authored-by: Jan-Patrick Lehr <JanPatrick.Lehr@amd.com>
Co-authored-by: Joachim <protze@rz.rwth-aachen.de>
2025-08-21 13:46:30 +02:00
David Tenty
63195d3d7a
[NFC][CMake] quote ${CMAKE_SYSTEM_NAME} consistently (#154537)
A CMake change included in CMake 4.0 makes `AIX` into a variable
(similar to `APPLE`, etc.)
ff03db6657

However, `${CMAKE_SYSTEM_NAME}` unfortunately also expands exactly to
`AIX` and `if` auto-expands variable names in CMake. That means you get
a double expansion if you write:

`if (${CMAKE_SYSTEM_NAME}  MATCHES "AIX")`
which becomes:
`if (AIX  MATCHES "AIX")`
which is as if you wrote:
`if (ON MATCHES "AIX")`

You can prevent this by quoting the expansion of "${CMAKE_SYSTEM_NAME}",
due to policy
[CMP0054](https://cmake.org/cmake/help/latest/policy/CMP0054.html#policy:CMP0054)
which is on by default in 4.0+. Most of the LLVM CMake already does
this, but this PR fixes the remaining cases where we do not.
2025-08-20 12:45:41 -04:00
Michał Górny
29067ac6e1
[OpenMP][OMPD] Fix GDB plugin to work correctly when installed (#153956)
Fix the `sys.path` logic in the GDB plugin to insert the intended
self-path in the first position rather than appending it to the end. The
latter implied that if `sys.path` (naturally) contained the GDB's
`gdb-plugin` directory, `import ompd` would return the top-level
`ompd/__init__.py` module rather than the `ompd/ompd.py` submodule, as
intended by adding the `ompd/` directory to `sys.path`.

This is intended to be a minimal change necessary to fix the issue.
Alternatively, the code could be modified to import `ompd.ompd` and stop
modifying `sys.path` entirely. However, I do not know why this option
was chosen in the first place, so I can't tell if this won't break
something.

Fixes #153954

Signed-off-by: Michał Górny <mgorny@gentoo.org>
2025-08-20 14:36:50 +02:00
Joachim
98dd1888bf
[OpenMP][Test][NFC] output tool data as hex to improve readibility (#152757)
Using hex format allows to better interpret IDs: 
the first digits represent the thread number, the last digits represent
the ID within a thread

The main change is in callback.h: PRIu64 -> PRIx64

The patch also guards RUN/CHECK lines in openmp/runtime/tests/ompt with clang-format on/off comments and clang-formats the directory.

---------

Co-authored-by: Kaloyan Ignatov <kaloyan.ignatov@rwth-aachen.de>
2025-08-18 10:42:33 +02:00
CatherineMoore
49e28d77b8
[OpenMP] Update ompdModule.c printf to match argument type (#152785)
Update printf format string to match argument list

---------

Co-authored-by: Joachim <protze@rz.rwth-aachen.de>
Co-authored-by: Joachim Jenke <jenke@itc.rwth-aachen.de>
2025-08-15 14:30:47 -05:00
CatherineMoore
3a8f579a23
[OpenMP] Update printf statement with missing argument. (#153704) 2025-08-15 16:34:00 +00:00
CatherineMoore
5479b7ed42
[OpenMP] Update printf stmt in kmp_settings.cpp (#152800)
Remove extraneous argument from printf statement

---------

Co-authored-by: Joachim <protze@rz.rwth-aachen.de>
2025-08-14 20:04:03 +00:00
Alex Duran
66d1c37eb6
[OFFLOAD][OPENMP] 6.0 compatible interop interface (#143491)
The following patch introduces a new interop interface implementation
with the following characteristics:

* It supports the new 6.0 prefer_type specification
* It supports both explicit objects (from interop constructs) and
implicit objects (from variant calls).
* Implements a per-thread reuse mechanism for implicit objects to reduce
overheads.
* It provides a plugin interface that allows selecting the supported
interop types, and managing all the backend related interop operations
(init, sync, ...).
* It enables cooperation with the OpenMP runtime to allow progress on
OpenMP synchronizations.
* It cleanups some vendor/fr_id mismatchs from the current query
routines.
* It supports extension to define interop callbacks for library cleanup.
2025-08-06 16:34:39 +02:00
Michael Kruse
9b2b5bf8cd
[OpenMP] Fix LLVM_ENABLE_PROJECTS=openmp build (#151117)
Set LLVM_TREE_AVAILABLE when not defined after #149871. In particular,
the LLVM build tree is obviously available with
`add_subdirectory(openmp)` from the LLVM build tree itself. Note that
this build mode is deprecated since #136314.
2025-08-05 13:42:46 +02:00
Aiden Grossman
57c78998a0 [OpenMP] Update %preload-tool definition on Darwin
This was updated in some earlier commits but was never updated on Darwin
because I was testing locally on Linux and it does not seem like there
are any buildbots testing this configuration. Update it since it should
be trivial and will definitely be broken otherwise.
2025-07-26 22:24:41 +00:00
Aiden Grossman
b5c7482e8b
[OpenMP] Remove %preload-tool definition from archer tests (#150779)
This was added in 2b8115b10b03013b9f8ae0aa56b0cd6a6a6dd4fd and it looks
like this wass essentially a copy paste from one of the other lit config
files. This substitution is unused within the tests however and contains
a deprecated %T directive, so remove it.
2025-07-26 11:07:19 -07:00
Aiden Grossman
8c07a634ca
[OpenMP] Move tool tests out of individual folders (#150780)
I did not realize that these were originally in separate folders to
allow for the use of %T. Now that we have switched over to creating dirs
using %t, we can move these into a common folder and make things a
little bit more clean.
2025-07-26 11:07:07 -07:00
Aiden Grossman
5b60087eed [OpenMP] Fix tool tests 9475ed84909463ad594602dd76583f4ff12efcd0
These were still passing because I did not clear all the test artifacts
in between so the old ones were still present after updating the test. I
forgot to update a lit substitution which failed on clean builds.
2025-07-26 16:38:45 +00:00
Aiden Grossman
9475ed8490
[OpenMP] Remove uses of %T from lit tests (#150723)
This patch removes all uses of %T from lit tests in OpenMP. %T has been
deprecated for years and is not reccomended given it does not create a
unique dir per test, allowing for race conditions. Remove uses of %T in
OpenMP so we can eventually remove support for it in llvm-lit.
2025-07-25 23:59:27 -07:00
Michael Kruse
799f28d7a5
[OpenMP] Fix runtimes default build (#149871)
The default build of openmp (`cmake -S <llvm-project>/runtimes
-DLLVM_ENABLE_RUNTIMES=openmp`) current fails with
```
CMake Error at /home/meinersbur/src/llvm/flangrt/_src/cmake/Modules/GetClangResourceDir.cmake:17 (string):
  string sub-command REGEX, mode MATCH needs at least 5 arguments total to
  command.
Call Stack (most recent call first):
  /home/meinersbur/src/llvm/flangrt/_src/openmp/CMakeLists.txt:126 (get_clang_resource_dir)
```
The reason is that because it is not a bootstrapping-build, the clang
resource dir that it intends to write files such as `omp-tools.h` into,
is unavailable. Using the Clang resource dir for writing files is
conceptually broken, as that dir might be located in
`/usr/lib/clang/<version>/`. Writing to it is only intended in
bootstrapping builds where Clang is built alongside openmp.

This patch unifies the identification of being in a bootstrapping built.
The same `LLVM_TREE_AVAILABLE` definition is going to be used in
#137828. No reason for each runtime to define its own.
2025-07-26 01:56:57 +02:00
Jonathan Peyton
4981bc24cf
[OpenMP] Fixup bugs found during fuzz testing (#143455)
A lot of these only trip when using sanitizers with the library.

* Insert forgotten free()s
* Change (-1) << amount to 0xffffffffu as left shifting a negative is UB
* Fixup integer parser to return INT_MAX when parsing huge string of
digits. e.g., 452523423423423423 returns INT_MAX
* Fixup range parsing for affinity mask so integer overflow does not
occur
* Don't assert when branch bits are 0, instead warn user that is invalid
and use the default value.
* Fixup kmp_set_defaults() so the C version only uses null terminated
strings and the Fortran version uses the string + size version.
* Make sure the KMP_ALIGN_ALLOC is power of two, otherwise use
CACHE_LINE.
* Disallow ability to set KMP_TASKING=1 (task barrier) this doesn't work
and hasn't worked for a long time.
* Limit KMP_HOT_TEAMS_MAX_LEVEL to 1024, an array is allocated based on
this value.
* Remove integer values for OMP_PROC_BIND. The specification only allows
strings and CSV of strings.
* Fix setting KMP_AFFINITY=disabled + OMP_DISPLAY_AFFINITY=TRUE
2025-07-21 17:49:45 -04:00
Jonathan Peyton
de011e372d
[OpenMP] [NFC] Remove KMP_NESTED_HOT_TEAMS macro (#143584)
The feature was introduced back in 2014 and has been on ever since.
Leave the feature in place. Removing only the macro.
2025-07-21 17:49:07 -04:00
Jonathan Peyton
c9fe19a99b
[OpenMP] Improve performance of ticket lock (x86) (#143557)
Ticket lock has a yield operation (shown below) which degrades
performance on larger server machines due to an unconditional pause
operation.

```
#define KMP_YIELD(cond)                                                        \
  {                                                                            \
    KMP_CPU_PAUSE();                                                           \
    if ((cond) && (KMP_TRY_YIELD))                                             \
      __kmp_yield();                                                           \
  }
```
2025-07-21 08:26:35 -05:00
Jonathan Peyton
c244c3b2d9
[OpenMP] [NFC] Remove dead code: building task stack (#143589)
This code hasn't been enabled since the first code changes were
introduced. Remove the dead code.
2025-07-18 12:03:12 -04:00
Robert Imschweiler
02aacc4cef
Reland: [OpenMP][clang] 6.0: num_threads strict (part 1: host runtime) (#147532)
OpenMP 6.0 12.1.2 specifies the behavior of the strict modifier for the
num_threads clause on parallel directives, along with the message and
severity clauses. This commit implements necessary host runtime changes.

Reland https://github.com/llvm/llvm-project/pull/146403. After manual
testing on a gfx90a machine, I could not reproduce the failing test,
which makes it even more likely that the test has just been flaky. (Or
at least that it's not an issue related to this patch.)
2025-07-08 17:19:40 +02:00
Ritanya-B-Bharadwaj
0f391d6f51
[OpenMP] OpenMP ThreadSet clause - basic runtime (#144409)
Initial runtime support for threadset clause in task and taskloop
directives [Section 14.8 in in OpenMP 6.0 spec]

Frontend PR- https://github.com/llvm/llvm-project/pull/135807
2025-07-08 14:16:29 +02:00
Robert Imschweiler
3ea636e2f6
Revert "[OpenMP][clang] 6.0: num_threads strict (part 1: host runtime)" (#147379)
Reverts llvm/llvm-project#146403
2025-07-07 21:24:49 +02:00
Robert Imschweiler
424abcb641
[OpenMP][clang] 6.0: num_threads strict (part 1: host runtime) (#146403)
OpenMP 6.0 12.1.2 specifies the behavior of the strict modifier for the
num_threads clause on parallel directives, along with the message and
severity clauses. This commit implements necessary host runtime changes.
2025-07-07 21:07:54 +02:00
Bernhard M. Wiedemann
b6e113a149
Drop timestamp in generated source code (#146822)
Fixes #72206

This helps reproducible builds of libomp.so -- 
probably because LLVM's LTO computed a hash of inputs to generate its
symbol names.

note: if it is desired to keep the timestamp, we could instead use
[`SOURCE_DATE_EPOCH`](https://reproducible-builds.org/docs/source-date-epoch/)
to make it deterministic.

This PR was done while working on [reproducible builds for
openSUSE](https://en.opensuse.org/openSUSE:Reproducible_Builds).
2025-07-03 18:18:47 +02:00
Joachim Jenke
23384cd581 [OpenMP][test][NFC] Temporarily disabling failing test
The test added with PR #145625 fails with certain build configurations of
libomp, disabling the test until the issue in the runtime is fixed.
2025-06-27 09:03:05 +02:00
Rainer Orth
3b8ac7a22c
[OpenMP] Fix various alignment issues (#142376)
When running the `openmp` testsuite on 32-bit SPARC, several tests
`FAIL` apparently randomly, but always with the same kind of error:
```
# error: command failed with exit status: -11
```
The tests die with `SIGBUS`, as can be seen in `truss` output:
```
26461/1:            Incurred fault #5, FLTACCESS  %pc = 0x00010EAC
26461/1:              siginfo: SIGBUS BUS_ADRALN addr=0x0013D12C
26461/1:            Received signal #10, SIGBUS [default]
26461/1:              siginfo: SIGBUS BUS_ADRALN addr=0x0013D12C
```
i.e. the code is trying an unaligned access which cannot work on SPARC,
a strict-alignment target which enforces natural alignment on access.
This explains the apparent randomness of the failures: if the memory
happens to be aligned appropriately, the tests work, but fail if not.

A `Debug` build reveals much more:

- `__kmp_alloc` currently aligns to `sizeof(void *)`, which isn't enough
on strict-alignment targets when the data are accessed as types
requiring larger alignment. Therefore, this patch increases `alignment`
to `SizeQuant`.

- 32-bit Solaris/sparc `libc` guarantees 8-byte alignment from `malloc`,
so this patch adjusts `SizeQuant` to match.

- There's a `SIGBUS` in
  ```
  __kmpc_fork_teams (loc=0x112f8, argc=0, 
microtask=0x16cc8
<__omp_offloading_ffbc020a_4b1abe_main_l9_debug__.omp_outlined>)
      at openmp/runtime/src/kmp_csupport.cpp:573
  573	  *(kmp_int64 *)(&this_thr->th.th_teams_size) = 0L;
  ```
Casting to a pointer to a type requiring 64-bit alignment when that
isn't guaranteed is wrong. Instead, this patch uses `memset` instead.

- There's another `SIGBUS` in
  ```
0xfef8cb9c in __kmp_taskloop_recur (loc=0x10cb8, gtid=0, task=0x23cd00,
lb=0x23cd18, ub=0x23cd20, st=1, ub_glob=499, num_tasks=100, grainsize=5,
      extras=0, last_chunk=0, tc=500, num_t_min=20, 
codeptr_ra=0xfef8dbc8 <__kmpc_taskloop(ident_t*, int, kmp_task_t*, int,
kmp_uint64*, kmp_uint64*, kmp_int64, int, int, kmp_uint64, void*)+240>,
      task_dup=0x0)
      at openmp/runtime/src/kmp_tasking.cpp:5147
  5147	  p->st = st;
  ```
`p->st` doesn't currently guarantee the 8-byte alignment required by
`kmp_int64 st`. `p` is set in
  ```
   __taskloop_params_t *p = (__taskloop_params_t *)new_task->shareds;
  ```
but `shareds_offset` is currently aligned to `sizeof(void *)` only.
Increasing it to `sizeof(kmp_uint64)` to match its use fixes the
`SIGBUS`.

With these fixes I get clean `openmp` test results on 32-bit SPARC (both
Solaris and Linux), with one unrelated exception.

Tested on `sparc-sun-solaris2.11`, `sparcv9-sun-solaris2.11`,
`sparc-unknown-linux-gnu`, `sparc64-unknown-linux-gnu`,
`i386-pc-solaris2.11`, `amd64-pc-solaris2.11`, `i686-pc-linux-gnu`, and
`x86_64-pc-linux-gnu`.
2025-06-25 21:32:14 +02:00
Larry Meadows
aec88679db
[OpenMP] avoid segv for a lock that has already been destroyed (#145625)
This can happen in static destructors when called after the
  runtime is already shutdown (e.g., by ompt_finalize_tool). Even
  though it is technically an error to call omp_destroy_lock after
shutdown, the application doesn't necessarily know that omp_destroy_lock
  was already called. This is safe becaues all indirect locks are
  destoryed in __kmp_cleanup_indirect_user_locks so the return
  value will always be valid or a nullptr, not garbage.
2025-06-25 21:08:45 +02:00
nicebert
cf6ae065a0
[OpenMP] Remove declaration and usage of __AMDGCN_WAVEFRONT_SIZE (#143761)
Removes usage of __AMDGCN_WAVEFRONT_SIZE as compile time constant.

---------

Co-authored-by: Shilei Tian <i@tianshilei.me>
2025-06-13 10:46:36 -04:00
Yang Zaizhou
968d8eaa44
[OpenMP][Flang]Fix omp_get_cancellation return type from integer to logical (#142990) 2025-06-12 11:28:57 +08:00
Alexander Ziaee
44a7ecd1d7
[doc] Use ISO nomenclature for 1024 byte units (#133148)
Increase specificity by using the correct unit sizes. KBytes is an
abbreviation for kB, 1000 bytes, and the hardware industry as well as
several operating systems have now switched to using 1000 byte kBs.

If this change is acceptable, sometimes GitHub mangles merges to use the
original email of the account. $dayjob asks contributions have my work
email. Thanks!
2025-06-11 13:27:23 +02:00
CHANDRA GHALE
ddef9ce8da
LLVM Buildbot failure on openmp runtime test (#143674)
Error looks to be missing includes for complex number support in some
system. Removing test for now.
Relevant PR :
[PR-134709](https://github.com/llvm/llvm-project/pull/134709)
```
 .---command stderr------------
# | /home/uweigand/sandbox/buildbot/openmp-s390x-linux/llvm.src/openmp/runtime/test/worksharing/for/omp_for_private_reduction.cpp:78:42: error: use of undeclared identifier 'I'
# |    78 |   double _Complex expected = 0.0 + 0.0 * I;
# |       |                                          ^
# | /home/uweigand/sandbox/buildbot/openmp-s390x-linux/llvm.src/openmp/runtime/test/worksharing/for/omp_for_private_reduction.cpp:79:40: error: use of undeclared identifier 'I'
# |    79 |   double _Complex result = 0.0 + 0.0 * I;
# |       |                                        ^
# | /home/uweigand/sandbox/buildbot/openmp-s390x-linux/llvm.src/openmp/runtime/test/worksharing/for/omp_for_private_reduction.cpp:84:22: error: use of undeclared identifier 'I'
# |    84 |     arr[i] = i - i * I;
# |       |                      ^
# | /home/uweigand/sandbox/buildbot/openmp-s390x-linux/llvm.src/openmp/runtime/test/worksharing/for/omp_for_private_reduction.cpp:92:19: error: use of undeclared identifier 'creal'
# |    92 |       real_sum += creal(arr[i]);
# |       |                   ^~~~~
# | /home/uweigand/sandbox/buildbot/openmp-s390x-linux/llvm.src/openmp/runtime/test/worksharing/for/omp_for_private_reduction.cpp:93:19: error: use of undeclared identifier 'cimag'
# |    93 |       imag_sum += cimag(arr[i]);
# |       |                   ^~~~~
# | /home/uweigand/sandbox/buildbot/openmp-s390x-linux/llvm.src/openmp/runtime/test/worksharing/for/omp_for_private_reduction.cpp:96:36: error: use of undeclared identifier 'I'
# |    96 |     result = real_sum + imag_sum * I;
# |       |                                    ^
# | /home/uweigand/sandbox/buildbot/openmp-s390x-linux/llvm.src/openmp/runtime/test/worksharing/for/omp_for_private_reduction.cpp:97:9: error: use of undeclared identifier 'cabs'
# |    97 |     if (cabs(result - expected) > 1e-6) {
# |       |         ^~~~
# | 7 errors generated.
```

Co-authored-by: Chandra Ghale <ghale@pe31.hpc.amslabs.hpecorp.net>
2025-06-11 15:39:16 +05:30
CHANDRA GHALE
afbcf9529a
[OpenMP 6.0 ]Codegen for Reduction over private variables with reduction clause (#134709)
Codegen support for reduction over private variable with reduction
clause. Section 7.6.10 in in OpenMP 6.0 spec.
- An internal shared copy is initialized with an initializer value.
- The shared copy is updated by combining its value with the values from
the private copies created by the clause.
- Once an encountering thread verifies that all updates are complete,
its original list item is updated by merging its value with that of the
shared copy and then broadcast to all threads.

Sample Test Case from OpenMP 6.0 Example 
```
#include <assert.h>
#include <omp.h>
#define N 10

void do_red(int n, int *v, int &sum_v)
{
    sum_v = 0; // sum_v is private
    #pragma omp for reduction(original(private),+: sum_v)
    for (int i = 0; i < n; i++) 
    {
        sum_v += v[i];
    }
}

int main(void)
{
    int v[N];
    for (int i = 0; i < N; i++)
        v[i] = i;
    #pragma omp parallel num_threads(4)
    {
        int s_v; // s_v is private
        do_red(N, v, s_v);
        assert(s_v == 45);
    }
    return 0;
}
```
Expected Codegen:
```
 // A shared global/static variable is introduced for the reduction result.
 // This variable is initialized (e.g., using memset or a UDR initializer)
 // e.g., .omp.reduction.internal_private_var

 // Barrier before any thread performs combination
  call void @__kmpc_barrier(...)

 // Initialization block (executed by thread 0)
 // e.g., call void @llvm.memset.p0.i64(...) or call @udr_initializer(...)

  call void @__kmpc_critical(...)
    // Inside critical section:
    // Load the current value from the shared variable
    // Load the thread-local private variable's value
    // Perform the reduction operation 
    // Store the result back to the shared variable

  call void @__kmpc_end_critical(...)
  // Barrier after all threads complete their combinations

  call void @__kmpc_barrier(...)
 // Broadcast phase:
 // Load the final result from the shared variable)
 // Store the final result to the original private variable in each thread
 // Final barrier after broadcast

  call void @__kmpc_barrier(...)
```

---------

Co-authored-by: Chandra Ghale <ghale@pe31.hpc.amslabs.hpecorp.net>
2025-06-11 14:01:31 +05:30
Joseph Huber
ea0761ff0c
[OpenMP] Update out of date documentation (#142411)
Summary:
This deletes and changes somet things that are out of date or wrong and
makes the recommended way to build more clear.

---------

Co-authored-by: Shilei Tian <i@tianshilei.me>
2025-06-10 10:19:41 -05:00
Rainer Orth
c450dd7e83
[OpenMP][test] Support target= in tests (#142380)
LLVM is moving towards the `target=<target triple RE>` syntax in `XFAIL:
` etc., and I'll need the same in a subsequent patch.

This patch adds the necessary infrastructure.

Tested on `sparc-sun-solaris2.11`, `sparcv9-sun-solaris2.11`,
`sparc-unknown-linux-gnu`, `sparc64-unknown-linux-gnu`,
`i386-pc-solaris2.11`, `amd64-pc-solaris2.11`, `i686-pc-linux-gnu`, and
`x86_64-pc-linux-gnu`.
2025-06-10 10:20:58 +02:00
Rainer Orth
3f8827eaba
[OpenMP][cmake] Add SPARC support (#142364)
This patch adds SPARC infrastructure to the `openmp` `cmake` files,
matching what is done for other architectures.

Tested on `sparc-sun-solaris2.11`, `sparcv9-sun-solaris2.11`,
`sparc-unknown-linux-gnu`, `sparc64-unknown-linux-gnu`,
`i386-pc-solaris2.11`, `amd64-pc-solaris2.11`, `i686-pc-linux-gnu`, and
`x86_64-pc-linux-gnu`.
2025-06-07 09:55:05 +02:00
Rainer Orth
d76b9d6653
[OpenMP][cmake] Simplify -m32 handling (#142742)
Linking `libomp.so` on 32-bit SPARC `FAIL`s with
```
ld: fatal: file projects/openmp/runtime/src/CMakeFiles/omp.dir/z_Linux_asm.S.o: wrong ELF class: ELFCLASS64
```
This was a 1-stage build with a 64-bit-default `gcc`. Unlike the C++
sources, which were compiled as 32-bit objects due to the use of
`-DCMAKE_CXX_FLAGS=-m32`, the assembler sources were not.

This patch simplifies passing `-m32`: instead of doing it per
architecture, `-m32` is now always passed when the target uses 32-bit
pointers and supports the option.

Tested on `sparc-sun-solaris2.11`, `sparcv9-sun-solaris2.11`,
`sparc-unknown-linux-gnu`, `sparc64-unknown-linux-gnu`,
`i386-pc-solaris2.11`, `amd64-pc-solaris2.11`, `i686-pc-linux-gnu`, and
`x86_64-pc-linux-gnu`.
2025-06-05 08:34:59 +02:00
Rainer Orth
ba4f140ef6
[OpenMP] Don't use libproc on Solaris (#142379)
`openmp` currently doesn't compile on 32-bit Solaris:

```
FAILED: projects/openmp/runtime/src/CMakeFiles/omp.dir/z_Linux_util.cpp.o
[...]
In file included from openmp/runtime/src/z_Linux_util.cpp:78:
In file included from /usr/include/libproc.h:25:
In file included from /usr/include/gelf.h:10:
/usr/include/libelf.h:22:2: error: "large files are not supported by libelf"
   22 | #error "large files are not supported by libelf"
      |  ^
In file included from openmp/runtime/src/z_Linux_util.cpp:78:
/usr/include/libproc.h:42:2: error: "Cannot use libproc in the large file compilation environment"
   42 | #error "Cannot use libproc in the large file compilation environment"
      |  ^
```

Looking closer, there's no point in using `Pgrab` (the only interface
from `<libproc.h>`) at all: the resulting `ps_prochandle_t *` isn't used
in the remainder of the code and the original PR #82930 gives no
indication why it is deemed necessary/useful.

While at it, this patch switches to use `/proc/self/xmap`, matching
`compiler-rt`'s `sanitizer_procmaps_solaris.cpp`, and makes some minor
formatting fixes.

Tested on `sparc-sun-solaris2.11`, `sparcv9-sun-solaris2.11`,
`i386-pc-solaris2.11`, and `amd64-pc-solaris2.11`.
2025-06-04 10:28:38 +02:00
Rainer Orth
df9a90cdc5
[OpenMP][test] Define print_possible_return_addresses on SPARC (#138523)
Parts of the `openmp` testsuite currently don't build on SPARC due to
the lack of a `print_possible_return_addresses` definition.

This patch provides one. With it, the vast majority of tests `PASS` on
Solaris/sparcv9 and, with an additional patch, on Linux/sparc64.

The current definition was obtained empirically.

Tested on `sparcv9-sun-solaris2.11`, `sparc64-unknown-linux-gnu`,
`amd64-pc-solaris2.11`, and `x86_64-pc-linux-gnu`.
2025-05-15 14:31:55 +02:00
Rainer Orth
a2a280e58c
[OpenMP] Fix __builtin_return_address calls for SPARC (#138520)
`libomp` uses `__builtin_return_address` in two places. However, on some
targets those calls need to wrapped in `___builtin_extract_return_addr`
to get at the actual return address. SPARC is among those targets and
the only one where `clang` actually implements this, cf. [[clang][Sparc]
Fix __builtin_extract_return_addr
etc.](https://reviews.llvm.org/D91607). `compiler-rt` needed the same
adjustment, cf. [[sanitizer_common][test] Enable tests on
SPARC](https://reviews.llvm.org/D91608). On other targets, this is a
no-op. However, there are more targets that have the same issue and
`gcc`, unlike `clang`, correctly implements it, so there might be issues
when building `libomp` with `gcc`.

This patch adds the necessary calls.

Tested on `sparcv9-sun-solaris2.11`, `sparc64-unknown-linux-gnu`,
`amd64-pc-solaris2.11`, and `x86_64-pc-linux-gnu`.
2025-05-14 13:11:19 +02:00
Rainer Orth
6fd3381639
[OpenMP][test] Properly check for NULL (#139364)
Only 2 `openmp` testsuite failures remain on Solaris/amd64. They are due
the same issue: the tests in question assume `NULL` pointers to be
printed as `(nil)` while the rest of the testsuite uses `[[NULL]]` for
that.

This patch changes them to follow suit.

Tested on `amd64-pc-solaris2.11` and `x86_64-pc-linux-gnu`.
2025-05-10 22:21:46 +02:00
Rainer Orth
fe56c8f364
[OpenMP][test] Adjust tool_available_search.c for Solaris (#138515)
After PR #138514, only 3 testsuite failures remain on Solaris/amd64. One
of them is

```
libomp :: ompt/loadtool/tool_available_search/tool_available_search.c
```

The issue is that the expected message is that emitted by Linux/glibc,
while the Solaris message differs:

On Linux/x86_64, I get
```
Opening projects/openmp/runtime/test/ompt/loadtool/tool_available_search/Output/non_existing_file.so... Failed: projects/openmp/runtime/test/ompt/loadtool/tool_available_search/Output/non_existing_file.so: cannot open shared object file: No such file or directory
```
while Solaris/amd64 emits
```
Opening projects/openmp/runtime/test/ompt/loadtool/tool_available_search/Output/non_existing_file.so... Failed: ld.so.1: tool_available_search.c.tmp: projects/openmp/runtime/test/ompt/loadtool/tool_available_search/Output/non_existing_file.so: open failed: No such file or directory
```

Since the exact wording is obviously an implementation detail, this
patch allows for both forms.

Tested on `amd64-pc-solaris2.11` and `x86_64-pc-linux-gnu`.
2025-05-10 09:51:15 +02:00
Rainer Orth
1b4f161978
[OpenMP][test] Make %p 0x prefix optional (#138514)
Testing `openmp` on Solaris/amd64 shows a large number of failures, all
due to the same issue:

```
# .---command stderr------------
# | openmp/runtime/test/ompt/misc/interoperability.cpp:67:16: error: CHECK-SAME: expected string not found in input
# | // CHECK-SAME: parent_task_frame.reenter={{0x[0-f]+}}
# |                ^
# | <stdin>:5:101: note: scanning from here
# | 281474976710658: ompt_event_parallel_begin: parent_task_id=281474976710659, parent_task_frame.exit=0, parent_task_frame.reenter=7fffbedffe90, parallel_id=281474976710661, requested_team_size=2, codeptr_ra=408b8e, invoker=2
```

The testsuite expects pointers to be printed with a `0x` prefix when
using the `%p` format, while Solaris `libc` just prints them in hex
without a prefix.

However, this difference is completely benign. ISO C (up to C23,
7.23.6.1) states

```
p	The argument shall be a pointer to void or a pointer to a character
	type. The value of the pointer is converted to a sequence of printing
	characters, in an implementation-defined manner.
```

I saw two ways around this:

- replace every instance of `%p` with a macro (`KMP_PTR_FMT`, defined as
`"%p"` or `"0x%p" as appropriate), or
- adjust the testsuite to make the `0x` prefix optional

The second route seemed less intrusive and more readable, so that's what
this patch does. While large, it's also completely mechanical.

Tested on `amd64-pc-solaris2.11` and `x86_64-pc-linux-gnu`.
2025-05-10 09:50:08 +02:00
Rainer Orth
39beeb8eaa
[OpenMP] Fix __kmp_unnamed_critical_addr .type setting (#138815)
PR #138517 broke the Android LLVM builders: ARM doesn't understand the
`@object` form. As it turns out, one can use `%object` instead, which
does assemble on all targets currently supported by `z_Linux_asm.S`.

Tested by rebuilding `libomp.so` on `sparcv9-sun-solaris2.11`.
2025-05-07 20:59:42 +02:00
Rainer Orth
3f1eafaa04
[OpenMP] Provide __NR_sched_[gs]etaffinity on Linux/sparc64 (#138525)
`libomp` doesn't currently build on Linux/sparc64 due to lack of
`__NR_sched_setaffinity` and `__NR_sched_getaffinity` definitions.

This patch provides those.

Tested on `sparcv9-sun-solaris2.11`, `sparc64-unknown-linux-gnu`,
`amd64-pc-solaris2.11`, and `x86_64-pc-linux-gnu`.
2025-05-06 09:13:34 +02:00
Rainer Orth
c24b9ca3da
[OpenMP] Provide __kmp_unnamed_critical_addr on SPARC (#138517)
`libomp.so` currently fails to link on SPARC, both Solaris/sparcv9 and
Linux/sparc64:

```
Undefined                       first referenced
 symbol                             in file
__kmp_unnamed_critical_addr         projects/openmp/runtime/src/CMakeFiles/omp.dir/kmp_gsupport.cpp.o
ld: fatal: symbol referencing errors
```

This patch provides the necessary definition. While at it, I noticed
that on non-x86 targets the symbol wasn't marked as `@object`, which
this patch corrects, too.

Tested on `sparcv9-sun-solaris2.11`, `sparc64-unknown-linux-gnu`,
`amd64-pc-solaris2.11`, and `x86_64-pc-linux-gnu`.
2025-05-06 09:13:05 +02:00
Rainer Orth
30ec488716
[OpenMP] Use pthread_self for __kmp_gettid on Solaris (#138510)
Building `openmp` on Solaris/amd64, I get

```
In file included from openmp/runtime/src/kmp_utility.cpp:16:
openmp/runtime/src/kmp_wrapper_getpid.h:47:2: warning: No gettid found, use getpid instead [-W#warnings]
   47 | #warning No gettid found, use getpid instead
      |  ^

```

There's no reason to do this: Solaris can use `pthread_self` just as AIX
does.

Tested on `amd64-pc-solaris2.11` and `x86_64-pc-linux-gnu`.
2025-05-06 09:12:17 +02:00
Rainer Orth
d83983999d
[OpenMP] Use TLS for gtid on Solaris (#138508)
When running the `openmp` testsuite on Solaris/amd64, many tests `FAIL`
like

```
# | OMP: Error #11: Stack overflow detected for OpenMP thread #1
```

In a `Debug` build, I also get
```
# | Assertion failure at kmp_runtime.cpp(203): __kmp_gtid_get_specific() < 0 || __kmp_gtid_get_specific() == i.
```

Further investigation shows that just setting `__kmp_gtid_mode` to 3
massively reduces the number of failures.

Tested on `amd64-pc-solaris2.11` and `x86_64-pc-linux-gnu`.
2025-05-06 09:11:43 +02:00