Skip to content

Add reverse AD for llvm.intr.memcpy#2841

Open
xys-syx wants to merge 7 commits into
mainfrom
memcpy
Open

Add reverse AD for llvm.intr.memcpy#2841
xys-syx wants to merge 7 commits into
mainfrom
memcpy

Conversation

@xys-syx
Copy link
Copy Markdown
Collaborator

@xys-syx xys-syx commented May 28, 2026

llvm.intr.memcpy has no registry in reverse AD, which cause "could not compute the adjoint for this operation "llvm.intr.memcpy"".

Reverse rule used here:

  d_src[i] += d_dst[i]
  d_dst[i] = 0

Copy link
Copy Markdown
Collaborator

@Pangoraw Pangoraw left a comment

Choose a reason for hiding this comment

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

the rule looks good, we will need to include scftocf and cftollvm lowering passes in pipelines.

Type ptrTy = cp.getDst().getType();

auto forOp = scf::ForOp::create(builder, loc, c0, n, c1);
OpBuilder body(forOp.getBody()->getTerminator());
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

better to use the provided builder with setInsertionPoint as it can have hooks

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@Pangoraw thanks, I have modified

we will need to include scftocf and cftollvm lowering passes in pipelines

we have covert-polygeist-to-llvm in the downstream, I guess it may could work for it
https://github.com/EnzymeAD/Enzyme-JAX/blob/5db2b680308c38613ed6d5d01ed7d9d9a1353a65/src/enzyme_ad/jax/Passes/ConvertPolygeistToLLVM.cpp#L4573

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ah yes perfect in this case

@xys-syx xys-syx requested a review from Pangoraw June 4, 2026 09:46
return t;
if (Type t = walk(cp.getSrc()))
return t;
return Float64Type::get(cp.getContext());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is wrong, we should throw an error if things can't be deduced. This is also somewhat hacky and we should use type analysis

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

ok, I will fix it

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I have fixed it, but only for the case where the inferred element type is a scalar int or float and using upstream MLIR for inferring types. The memcpy ops in RSBench that copy arrays of struct<(f64, f64)> and array<10 x f64> need to handle it recursively, I plan to open a new PR for it.

@xys-syx xys-syx requested a review from wsmoses June 8, 2026 05:20
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.

3 participants