<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/55815>55815</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            OpenMP `kmp.h` is broken on Windows
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          penzn
      </td>
    </tr>
</table>

<pre>
    Windows 10 Enterprise, Visual Studio Enterprise 2022 (17.1.6), 11th Gen Intel(R) Core(TM) i7-1185G7.

This happens at least on `main` branch, possibly on 14.0 as well.

To reproduce, in MSVC shell:

```
> cmake -GNinja -DLLVM_ENABLE_PROJECTS="llvm;clang" -DLLVM_ENABLE_RUNTIMES="openmp" C:\path\to\llvm-project\llvm
> ninja
```

OpenMP build would fail because `KMP_INTERNAL_MALLOC` and `KMP_INTERNAL_FREE` are undefined:

```
[4/38] Building CXX object openmp\runtime\src\CMakeFiles\omp.dir\kmp_i18n.cpp.obj
FAILED: openmp/runtime/src/CMakeFiles/omp.dir/kmp_i18n.cpp.obj 
C:\Users\ppenzin\Dev\llvm\build\.\bin\clang-cl.exe --target=x86_64-pc-windows-msvc  /nologo -TP -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Domp_EXPORTS -IC:\Users\ppenzin\Dev\llvm\build\runtimes\runtimes-bins\openmp\runtime\src -IC:\Users\ppenzin\Dev\llvm\llvm-project\openmp\runtime\src -IC:\Users\ppenzin\Dev\llvm\llvm-project\openmp\runtime\src\i18n -IC:\Users\ppenzin\Dev\llvm\llvm-project\openmp\runtime\src\include -IC:\Users\ppenzin\Dev\llvm\llvm-project\openmp\runtime\src\thirdparty\ittnotify /DWIN32 /D_WINDOWS   /Zc:inline /Zc:__cplusplus /Zc:strictStrings /Oi /Zc:rvalueCast /bigobj /W4 -wd4141 -wd4146 -wd4244 -wd4267 -wd4291 -wd4351 -wd4456 -wd4457 -wd4458 -wd4459 -wd4503 -wd4624 -wd4722 -wd4100 -wd4127 -wd4512 -wd4505 -wd4610 -wd4510 -wd4702 -wd4245 -wd4706 -wd4310 -wd4701 -wd4703 -wd4389 -wd4611 -wd4805 -wd4204 -wd4577 -wd4091 -wd4592 -wd4319 -wd4709 -wd4324 -w14062 -we4238 /MTd /Zi /Ob0 /Od /RTC1   -D _CRT_SECURE_NO_WARNINGS -D _CRT_SECURE_NO_DEPRECATE -D _WINDOWS -D _WINNT -D _WIN32_WINNT=0x0501 -D _USRDLL -D _ITERATOR_DEBUG_LEVEL=0 -D _DEBUG /showIncludes /Foopenmp\runtime\src\CMakeFiles\omp.dir\kmp_i18n.cpp.obj /Fdopenmp\runtime\src\CMakeFiles\omp.dir\ -c -- C:\Users\ppenzin\Dev\llvm\llvm-project\openmp\runtime\src\kmp_i18n.cpp
In file included from C:\Users\ppenzin\Dev\llvm\llvm-project\openmp\runtime\src\kmp_i18n.cpp:15:
In file included from C:\Users\ppenzin\Dev\llvm\llvm-project\openmp\runtime\src/kmp.h:112:
C:\Users\ppenzin\Dev\llvm\llvm-project\openmp\runtime\src/kmp_barrier.h(111,51): error: use of undeclared identifier 'KMP_INTERNAL_MALLOC'
    distributedBarrier *d = (distributedBarrier *)KMP_ALIGNED_ALLOCATE(
                                                  ^
C:\Users\ppenzin\Dev\llvm\llvm-project\openmp\runtime\src/kmp_barrier.h(43,47): note: expanded from macro 'KMP_ALIGNED_ALLOCATE'
#define KMP_ALIGNED_ALLOCATE(size, alignment) KMP_INTERNAL_MALLOC(size)
                                              ^
C:\Users\ppenzin\Dev\llvm\llvm-project\openmp\runtime\src/kmp_barrier.h(131,52): error: use of undeclared identifier 'KMP_INTERNAL_FREE'
  static void deallocate(distributedBarrier *db) { KMP_ALIGNED_FREE(db); }
                                                   ^
C:\Users\ppenzin\Dev\llvm\llvm-project\openmp\runtime\src/kmp_barrier.h(44,31): note: expanded from macro 'KMP_ALIGNED_FREE'
#define KMP_ALIGNED_FREE(ptr) KMP_INTERNAL_FREE(ptr)
                              ^
C:\Users\ppenzin\Dev\llvm\llvm-project\openmp\runtime\src\kmp_i18n.cpp(57,36): warning: missing field 'flags' initializer [-Wmissing-field-initializers]
static kmp_bootstrap_lock_t lock = KMP_BOOTSTRAP_LOCK_INITIALIZER(lock);
                                   ^
C:\Users\ppenzin\Dev\llvm\llvm-project\openmp\runtime\src/kmp_lock.h(519,46): note: expanded from macro 'KMP_BOOTSTRAP_LOCK_INITIALIZER'
#define KMP_BOOTSTRAP_LOCK_INITIALIZER(lock) KMP_TICKET_LOCK_INITIALIZER((lock))
                                             ^
C:\Users\ppenzin\Dev\llvm\llvm-project\openmp\runtime\src/kmp_lock.h(283,5): note: expanded from macro 'KMP_TICKET_LOCK_INITIALIZER'
    }                                                                          \
    ^
1 warning and 2 errors generated.
```

This is not surprising because the macros in question are defined in `kmp.h` at around [line 3465](https://github.com/llvm/llvm-project/blob/31d12df3b9382cd3b1500cb0f11d96291e79c37f/openmp/runtime/src/kmp.h#L3465), while they are used in `kmp_barrier.h`, which is included in the same file at [line 112](https://github.com/llvm/llvm-project/blob/31d12df3b9382cd3b1500cb0f11d96291e79c37f/openmp/runtime/src/kmp.h#L112). A simple workaround would be to move the definitions up before the include.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNWFlz4jgQ_jXkRWXKkvHBAw9gTIodjhQ4k6l5cfkQoI2xvD7CZH79tg4IyZDdZHYmtSniltXt7tbXrasTnj0O7liR8UONsImCoqFVWbGadoiPPrO6jXO0btqM8TMeIiYhqEM87HZx1-mQvpDGuNmha1qgKQjmwF0BA_m8Al1eOBcvzDUw9uxrt9sxxx1zqJ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