Skip to content

Conversation

@jpinot
Copy link
Member

@jpinot jpinot commented Apr 23, 2025

This PR includes a set of fixes related to taskgraph support in OpenMP:

  • Initialize taskgraph-related fields in task allocation, to fix uninitialized access.
  • Fix out-of-bounds access in __kmpc_omp_task_with_deps.
  • Fix missing copy of successors array during reallocation in taskgraph.

@jpinot jpinot changed the title [Draft][OpenMP] Openmp taskgraph bug fix [Draft][OpenMP] Fix Taskgraph bugs Apr 23, 2025
jpinot added 2 commits April 23, 2025 12:08
Fix a potential use of uninitialized is_taskgraph and tdg fields when a
task is created outside of a taskgraph construct. These fields were
previously left uninitialized, which could lead to undefined behavior
when later accessed.
… incorrect task ID

Correct use of task ID field when accessing the taskgraph’s record_map.
The previous logic mistakenly used td_task_id instead of td_tdg_task_id,
potentially causing out-of-bounds memory access when td_task_id exceeded
the map_size.

td_task_id and td_tdg_task_id was introduced in llvm#130660
@jpinot jpinot force-pushed the openmp_taskgraph_bug_fix branch 2 times, most recently from 648e961 to 126257d Compare April 23, 2025 10:21
@jpinot jpinot changed the title [Draft][OpenMP] Fix Taskgraph bugs [OpenMP] Fix Taskgraph bugs Apr 23, 2025
@jpinot jpinot marked this pull request as ready for review April 23, 2025 11:25
@llvmbot llvmbot added the openmp:libomp OpenMP host runtime label Apr 23, 2025
Comment on lines 246 to 253
kmp_uint old_size = source_info->successors_size;
kmp_uint new_size = source_info->successors_size * 2;
kmp_int32 *old_succ_ids = source_info->successors;
kmp_int32 *new_succ_ids = (kmp_int32 *)__kmp_allocate(
source_info->successors_size * sizeof(kmp_int32));
kmp_int32 *new_succ_ids =
(kmp_int32 *)__kmp_allocate(new_size * sizeof(kmp_int32));
KMP_MEMCPY(new_succ_ids, old_succ_ids, old_size * sizeof(kmp_int32));
source_info->successors = new_succ_ids;
source_info->successors_size = new_size;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't actually need the new_size and related code. Only old_size is needed and all the other code can stay unchanged. The copy only uses old_size anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

// __kmp_start_record: launch the execution of a previous
// __kmp_exec_tdg: launch the execution of a previous
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A NFC change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I Updated to commit message to [NFC][OpenMP] Fix task record/replay comments,
I noticed a few different formatting styles in the commit history—do you have a preferred convention?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should go in a separate PR. LLVM only allows squash commits when merging a PR.

As for coding style, we don't really have a strict standard for libomp. I'd recommend following the older code style. By "older," I mean the code from around 2017, not the stuff added in the last year or two.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it 😊 Thanks for the heads-up!


#if OMPX_TASKGRAPH
// __kmp_find_tdg: identify a TDG through its ID
// gtid: Global Thread ID
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make an NFC change to fix the code comments. Include the other one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

@rneveu
Copy link
Contributor

rneveu commented Apr 23, 2025

As these changes correct issues with dynamic arrays in the task graph structure, should we also add tests regarding the size and width of the task graph?

@jpinot jpinot force-pushed the openmp_taskgraph_bug_fix branch from 126257d to 3d86d1d Compare April 24, 2025 10:18
@jpinot
Copy link
Member Author

jpinot commented Apr 24, 2025

As these changes correct issues with dynamic arrays in the task graph structure, should we also add tests regarding the size and width of the task graph?

I added a new test that focuses on TDG with dependencies and multiple successors. While there is already a test for TDG with dependencies (omp_record_replay_deps.cpp), it doesn’t exercise the expansion of the successors array directly.

Let me know you thoughts on whether keeping this as a new targeted test, or if it would be better to update the existing test to incorporate these scenarios, even if it adds some complexity.

@jpinot jpinot requested a review from shiltian April 24, 2025 10:33
@jpinot jpinot force-pushed the openmp_taskgraph_bug_fix branch from 3d86d1d to 2c7db5f Compare April 24, 2025 13:24
Copy link
Contributor

@shiltian shiltian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks reasonable to me. LGTM as long as @rneveu is happy with the test changes.


#define TASKS_SIZE 12

typedef struct ident {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't need to have a definition. A forward declaration would be sufficient.

void *dummy;
} ident_t;

#ifdef __cplusplus
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use #ifdef __cplusplus since it is already %libomp-cxx-compile-and-run

#include <cassert>
#include <vector>

#define TASKS_SIZE 12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use constexpr const.

Ensure proper resizing and copying of the successors array when its
capacity is exceeded. The previous implementation allocated a new array
but did not copy the existing elements. This led to loss of successor
data and incorrect task dependency tracking.
@jpinot jpinot force-pushed the openmp_taskgraph_bug_fix branch from 2c7db5f to b13090d Compare April 24, 2025 13:44
@rneveu
Copy link
Contributor

rneveu commented Apr 24, 2025

LGTM

@jpinot jpinot merged commit ada4ad9 into llvm:main Apr 25, 2025
9 checks passed
@jpinot jpinot deleted the openmp_taskgraph_bug_fix branch April 28, 2025 06:46
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…lization (llvm#136837)

This commit resolves multiple issues in the OpenMP taskgraph implementation:
- Fix a potential use of uninitialized is_taskgraph and tdg fields when a task is created outside of a taskgraph construct.
- Fix use of task ID field when accessing the taskgraph’s record_map.
- Fix resizing and copying of the successors array when its capacity is exceeded.

Fixes memory management flaws, invalid memory accesses, and uninitialized data risks in taskgraph operations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

openmp:libomp OpenMP host runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants