Skip to content

[MLIR] Share TypeTree with MLIR as GenericTypeTree - #3484

Open
aokblast wants to merge 5 commits into
EnzymeAD:mainfrom
aokblast:users/aokblast/mlir/type_tree
Open

aokblast wants to merge 5 commits into
EnzymeAD:mainfrom
aokblast:users/aokblast/mlir/type_tree

Conversation

@aokblast

@aokblast aokblast commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Template TypeTree over the float type handle, like GenericConcreteType, so MLIR reuses it instead of keeping its own MTypeTree. The underlying consumer should implement the required Trait for using. Also, slightly refactor the operator[].

MTypeTree and MTypeResult are the counterparts of the TypeQuery
structure in the LLVM implementation. In MLIR, we already know all
types when building the tree, rather than relying on dynamically
leverage nodes at runtime as LLVM does. Therefore, the tree here is
only used to insert concrete type.

To preserve protability, we stil use a tree-like structure and provide
query, operator[], and isnert in our MType* classes. However, they
never actually build the tree by relying on a string-based parse()
function. Instead, the underlying dialect should provide the type
information and build the tree.

For query, we build the tree instead of using a cache version. This
should be fine, as the tree currently has only three levels and should
not be too large. For operator[], we follow the same logic as the LLVM
implementation, with slight modifications to make the code more readable.
@aokblast
aokblast requested a review from a team October 13, 2026 04:23
@aokblast

aokblast commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator Author

Of course, there is no in-tree consumer for this now. I am cleanup my CIR adopter now and will send the patch for the consumer in here.

I still try to upstream to here to prevent huge work when rebasing.

NOTE: I spend lots of time on separating patch to CIR and unrelated patch. :).

@aokblast

Copy link
Copy Markdown
Collaborator Author

See: Ringo-Leo/Enzyme-CIR@8daff03 for CIR changes

@Antipath1 Antipath1 left a comment

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.

Except for my comments, this does look good to me, although it is a bit unfortunate that we can not really have tests for the TypeTree here, but I get why it is not really possible, since nothing uses the tree upstream yet, and doing that for the LLVM dialect would be far more work than it is worth, I believe.

Comment thread enzyme/Enzyme/MLIR/Interfaces/MTypeResults.cpp Outdated
Comment thread enzyme/Enzyme/MLIR/Interfaces/MTypeResults.cpp Outdated
Comment thread enzyme/Enzyme/MLIR/Analysis/ActivityAnalysis.cpp

@Antipath1 Antipath1 left a comment

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.

LGTM thank youuuuuuuuu

@aokblast
aokblast enabled auto-merge October 26, 2026 20:12
@aokblast
aokblast force-pushed the users/aokblast/mlir/type_tree branch from 65e9ab6 to 8841f33 Compare October 26, 2026 21:31
Comment thread enzyme/Enzyme/MLIR/Analysis/ActivityAnalysis.cpp Outdated
Comment thread enzyme/Enzyme/MLIR/Interfaces/EnzymeLogic.h Outdated
class MTypeResults {
enum class MBaseType { Integer, Float, Pointer, Anything, Unknown };

class MConcreteType {

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.

can we potentially template the llvm concrete type to avoid redundant code?

@aokblast aokblast changed the title [MLIR] Implement MTypeTree and MTypeResult [MLIR] Templatize TypeTree and TypeResult and wire to MLIR Oct 27, 2026
using namespace mlir;
using namespace mlir::enzyme;

void MTypeTree::insert(const std::vector<int> &seq, MConcreteType ct) {

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.

can we not just use a [templatized] llvm type tree, do we need this?

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.

Sorry. There were to many branches work together. We don't templatized TypeTree. The only templatized are ConcreteType and reuse BaseType. Reflect this on the comment and title.

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.

correct, I am asking why have a distinct MTypeTree, instead of templating the LLVM TypeTree and re-using that?

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 think it is a little bit strange. Functions like IsFullyDetermined, Only, Data0, Lookup, ShiftIndices, toMD is (now) LLVM only as MLIR's type should be deterministic (at least I don't find any case now that needs dynamically leverage on the tree). And even if we need leverage on tree. The meaning of index may be different in MLIR's view.

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.

memcpy((void*)dst, (void*)src, 8) has an undefined derivative without knowing the underlying type. If a function took in dst and src as void* this would not be possible to differentiate without type information propagation -- so we will eventually need 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.

Oh you are right. Reverse mode need such propagation on type. Templatized this now.

@aokblast aokblast changed the title [MLIR] Templatize TypeTree and TypeResult and wire to MLIR [MLIR] Templatize TypeResult and implement MTypeTree Oct 27, 2026
@aokblast aokblast changed the title [MLIR] Templatize TypeResult and implement MTypeTree [MLIR] Implement MTypeResult and MTypeTree Oct 27, 2026
@wsmoses

wsmoses commented Oct 28, 2026

Copy link
Copy Markdown
Member

To be clear I'm not saying we should build the full analysis now, but we should use data structures that don't block us from doing so later (and equally ensure we have as little duplication as possible to make sure bug fixes are ported everywhere)

@aokblast aokblast changed the title [MLIR] Implement MTypeResult and MTypeTree [MLIR] Templatized TypeResult and TypeTree and Wire to MLIR Oct 28, 2026
@aokblast aokblast changed the title [MLIR] Templatized TypeResult and TypeTree and Wire to MLIR [MLIR] Share TypeTree with MLIR as GenericTypeTree Oct 28, 2026

This branch has not been deployed

No deployments
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