Skip to content

distributed: guard x86 cache-flush intrinsics for aarch64#43940

Open
blozano-tt wants to merge 3 commits intomainfrom
brain/aarch64-d2h-socket-fix
Open

distributed: guard x86 cache-flush intrinsics for aarch64#43940
blozano-tt wants to merge 3 commits intomainfrom
brain/aarch64-d2h-socket-fix

Conversation

@blozano-tt
Copy link
Copy Markdown
Contributor

@blozano-tt blozano-tt commented May 8, 2026

Problem

d2h_socket.cpp unconditionally includes <immintrin.h> and calls _mm_clflush / _mm_lfence, which are x86-only intrinsics. This breaks Linux/aarch64 builds with a wall of errors from clang-20:

immintrin.h:14:2: error: "This header is only meant to be used on x86 and x64 architecture"

Introduced in #42905.

Fix

Replace the bare x86 intrinsics with thin inline helpers (host_clflush / host_lfence) that dispatch to the correct ISA:

Arch Cache-line flush Fence
x86 / x86-64 _mm_clflush _mm_lfence
aarch64 DC CIVAC (clean+invalidate to PoC) DSB ISH
other #error

Also renames k_x86_clflush_line_bytesk_host_clflush_line_bytes (cache lines are 64 B on both supported arches, so the value is unchanged).

Notes for reviewers

  • DC CIVAC + DSB ISH is the standard aarch64 equivalent of clflush+lfence for observing device DMA writes through host-mapped memory. Same semantics.
  • The using_hugepage_ path (which contains all these calls) is only reachable when can_use_pinned_memory is false — not expected on aarch64 production hardware, but the code must at least compile.
  • --without-distributed does not skip compiling this file (it only gates MPI linking); that is a separate issue.

🧠 Fixed by BrAIn — my neurons fired on all architectures for once.

CI Status

Auto-generated on every push. Badges update live. Click a badge to filter runs by this branch.

d2h_socket.cpp unconditionally included <immintrin.h> and called
_mm_clflush/_mm_lfence, which are x86-only. This breaks builds on
Linux/aarch64 (introduced in #42905).

Replace the bare x86 intrinsics with inline host_clflush/host_lfence
helpers that dispatch to the correct ISA:
- x86/x86-64: _mm_clflush + _mm_lfence (unchanged semantics)
- aarch64: DC CIVAC (clean+invalidate to PoC) + DSB ISH
- other: #error to catch future ports early

Also rename k_x86_clflush_line_bytes -> k_host_clflush_line_bytes
since cache lines are 64 B on both supported arches.
Copilot AI review requested due to automatic review settings May 8, 2026 21:07
@blozano-tt blozano-tt requested review from a team, aliuTT, cfjchu, jbaumanTT and tt-asaigal as code owners May 8, 2026 21:07
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes tt_metal/distributed/d2h_socket.cpp failing to compile on Linux/aarch64 by guarding x86-only cache flush intrinsics and providing architecture-specific inline helpers for cache line flush + read-serialization in the hugepage fallback path.

Changes:

  • Replaces unconditional <immintrin.h> usage and direct _mm_clflush / _mm_lfence calls with host_clflush() / host_lfence() wrappers selected per-architecture.
  • Adds an aarch64 implementation using dc civac (cache line clean+invalidate) and dsb ish (barrier).
  • Renames the cache-line stride constant to k_host_clflush_line_bytes and uses it for flush loops.

Comment thread tt_metal/distributed/d2h_socket.cpp Outdated
// DSB ISH: data synchronisation barrier, inner-shareable domain.
asm volatile("dsb ish" ::: "memory");
}
constexpr uint32_t k_host_clflush_line_bytes = 64; // typical ARM64 D-cache line
blozano-tt added 2 commits May 8, 2026 14:38
…nsics

d2h_socket.cpp unconditionally included <immintrin.h> and used
_mm_clflush/_mm_lfence, which are x86-only.  This broke aarch64
builds (introduced in #42905).

The hugepage D2H path (using_hugepage_ == true) requires explicit
cache-line eviction because WH device PCIe writes may be
non-snooped.  Correct handling of WB + no-snoop DMA is inherently
x86-specific and fragile; attempting to replicate it on other
architectures would be unsound.

Fix:
- Guard #include <immintrin.h> with __x86_64__ / __i386__
- On other arches, provide stubs that TT_THROW so the translation
  unit compiles but the unreachable paths remain loud if ever hit
- TT_FATAL at the top of init_host_buffer_hugepage() on non-x86,
  because WH (the only arch that sets using_hugepage_) is x86-only

The x86 _mm_clflush / _mm_lfence code is left untouched.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants