`task_arena.h`: `create_numa_task_arenas` makes any `/clr` (C++/CLI) translation unit fail to compile
Summary
Since the introduction of create_numa_task_arenas (#1902, 2023.0.0), including
oneapi/tbb/task_arena.h in a translation unit compiled with MSVC /clr fails, even
when nothing from oneTBB is used. The function is defined unconditionally at namespace
scope in a public header, so there is no way to opt out.
This affects far more than direct includers: task_arena.h is pulled in transitively by
partitioner.h, and therefore by parallel_for.h, parallel_for_each.h,
parallel_sort.h, parallel_reduce.h and parallel_scan.h. In practice any C++/CLI
file that reaches a oneTBB parallel algorithm — directly or through its own project's
headers — stops compiling.
Reproducer
repro.cpp — six lines, uses nothing from oneTBB:
#include <oneapi/tbb/task_arena.h>
public ref class Managed
{
public:
int Value() { return 42; }
};cl /c /clr /EHa /std:c++20 /I <onetbb-include> repro.cppActual result
task_arena.h(701): error C2711: 'tbb::detail::d1::create_numa_task_arenas':
this function cannot be compiled as managed, consider using #pragma unmanaged
task_arena.h(706): note: Aligned data types not supported in managed code
task_arena.h(706): note: see source of the previous compiler diagnostic
task_arena.h(699): error C3821:
'std::vector<tbb::detail::d1::task_arena,std::allocator<tbb::detail::d1::task_arena>>
tbb::detail::d1::create_numa_task_arenas(tbb::detail::d1::constraints,unsigned int)':
managed type or function cannot be used in an unmanaged functionExpected result
Compiles. Including a header should not force code generation for an API the translation unit never names.
Cause
inline std::vector<d1::task_arena> create_numa_task_arenas(d1::constraints c = {},
unsigned reserved_slots = 0)
{
static std::vector<numa_node_id> node_indices = d1::numa_nodes();
std::vector<d1::task_arena> numa_arenas;
numa_arenas.reserve(node_indices.size());
for (auto numa_id : node_indices) {
numa_arenas.emplace_back(c.set_numa_id(numa_id), reserved_slots); // <-- line 706
}
return numa_arenas;
}d1::task_arena derives from task_arena_base, which holds std::atomic<do_once_state>
and std::atomic<r1::arena*> — so it is an over-aligned type, and MSVC cannot emit MSIL
for the std::vector<task_arena> element construction. The compiler's own note says so:
"Aligned data types not supported in managed code".
Two properties turn this from an inconvenience into a hard block:
- The function is not behind any guard. Its immediate neighbours in the same header
are (
__TBB_PREVIEW_PARALLEL_PHASE,__TBB_PREVIEW_TASK_GROUP_EXTENSIONS), but this one is unconditional, so a consumer cannot compile it out. - It is
inlineat namespace scope, not a template. A template would only be instantiated on use; this is code-generated in every including translation unit whether or not it is called.
Suggested fixes, in rough order of preference
- Make it a template on the arena type, or on nothing at all
(
template <typename = void>), so a TU that never calls it never instantiates it. Source-compatible for every existing caller. - Guard it, e.g.
#if !defined(_MANAGED) && !defined(_M_CEE)— MSVC defines_MANAGED/_M_CEEexactly when/clris active — or behind a__TBB_PREVIEW_NUMA_TASK_ARENAS-style macro consistent with the neighbouring previews. - Move the body out of line into the library, leaving only a declaration in the header.
Option 1 costs nothing and is the smallest change; option 2 is the most surgical if C++/CLI is not considered a supported configuration and you would rather exclude it explicitly than support it.
Environment
| oneTBB | 2023.1.0 (also 2023.0.0); 2022.3.0 unaffected |
| Compiler | MSVC 19.51 (cl 14.51.36231), Visual Studio 18 |
| Flags | /clr /EHa /std:c++20 |
| Platform | Windows x64 (x64-windows-static-md, vcpkg) |
Source: uxlfoundation/oneTBB