Skip to content

DCompute: Inline memcmp for device array equality#5156

Open
badnikhil wants to merge 2 commits into
ldc-developers:masterfrom
badnikhil:fix/dcompute-memcmp-device-array-eq
Open

DCompute: Inline memcmp for device array equality#5156
badnikhil wants to merge 2 commits into
ldc-developers:masterfrom
badnikhil:fix/dcompute-memcmp-device-array-eq

Conversation

@badnikhil

@badnikhil badnikhil commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Problem

The comparison/equality hooks added in #5149 lower array ==/< to a memcmp call (DtoArrayEqCmp_memcmp).That works on the host, but device targets have no libc to provide memcmp, and there is no llvm.memcmp intrinsic to fall back on ,so the call has nothing to bind to on the device.

Explanation of the Fix

  • Added emitInlineMemcmp(), which emits a small loop (cond/body/diff/inc/end blocks) that walks the two buffers byte-by-byte and returns an i32 following C memcmp convention: 0 when all sizeInBytes bytes are
    equal, otherwise the unsigned difference of the first mismatching byte.
  • callMemcmp() now branches on gIR->dcomputetarget: device code gets the inline loop, host code keeps the existing memcmp runtime call. The byte-size computation (numElements * elementSize) is shared by both
    paths.
  • Modified the PHI in DtoArrayEqCmp_memcmp: because callMemcmp can now emit extra basic blocks, the merge phi takes its incoming value from the block we actually end up in (memcmpResultBB = GetInsertBlock()) rather
    than assuming the result is still produced in memcmpBB.

@thewilsonator thewilsonator left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

test?

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