[openmp][fsanitizer] Memory leaks of openmp 'threadprivate' (original) (raw)

January 26, 2025, 9:22am 1

Hi,
I encountered a memory leak when compiling the fortran code using the ‘threadprivate’ directive,

program parallel_lastprivate
    use omp_lib
    implicit none
    integer :: i, n
     real :: a(10)
    integer :: sum = 0
    !$omp threadprivate(sum)
    n = 10
    !allocate(a(n))
    do i = 1, n
        a(i) = i
    end do
    !$omp do lastprivate(a)
    do i = n, 1, -1
        !print *, sum
        sum = sum + a(i)
    end do
    !$omp end do
    print *, sum
    if (sum == 0) then
        print "(i1)", 1
    else
        print "(i1)", 0
    end if
end program parallel_lastprivate

I found the reason was that a variable memory created by the kmp_threadprivate_insert function in the llvm openmp runtime library was not freed.

image

I searched the relevant code and found that there is no place to free this memory.

I tried to free the memroy in the postprocess of thereadprivate directive in funciton __kmp_reap_thread. The memory leak is fixed in this way.

I am wondering is anything wrong with openmp runtime here,or is there something wrong with the way classic-flang compiles and calls the openmp library?