Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mlir][capi] make MLIR Pass C-API type safe #121284

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sequencer
Copy link

@sequencer sequencer commented Dec 29, 2024

This PR changes C-API name mlirCreateExternalPass to mlirExternalPassCreate for aligning the naming convention in C-API, and now it returns MlirExternalPass.
I added a new C-API MlirExternalPassGetPass to cast MlirExternalPass to MlirPass;

The X Problem is in the MLIR C-API, there is no way to create a MlirExternalPass, related API mlirCreateExternalPass create a MlirPass.
While creation of MlirExternalPassCallbacks requires MlirExternalPass, see

/// See Pass::runOnOperation().
void (*run)(MlirOperation op, MlirExternalPass pass, void *userData);
};

Thus it's impossible to create MlirExternalPassCallbacks type safely externally.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@sequencer sequencer force-pushed the main branch 2 times, most recently from 91001e7 to 5cbe5e1 Compare December 29, 2024 07:33
- change C-API name mlirCreateExternalPass to mlirExternalPassCreate for
  aligning the C-API naming convension;
- make mlirExternalPassCreate to return MlirExternalPass for type safety;
- create new C-API MlirExternalPassGetPass to cast MlirExternalPass to
  MlirPass;
Copy link
Member

@SpriteOvO SpriteOvO left a comment

Choose a reason for hiding this comment

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

First of all, the renaming is a breaking change, if we do want to align the convention, we should also keep the old one and comment deprecated on it.

Second, MlirExternalPass is not supposed to be constructed from an external program. It is used in the run callback field in MlirExternalPassCallbacks, which is a callback parameter - an external program provides a callback function pointer which receives a MlirExternalPass passed from MLIR, and signals failure via mlirExternalPassSignalFailure if the pass implementation encountered any error.

See the test cases for examples

void testRunExternalPass(MlirOperation op, MlirExternalPass pass,
void *userData) {
++((TestExternalPassUserData *)userData)->runCallCount;
}
void testRunExternalFuncPass(MlirOperation op, MlirExternalPass pass,
void *userData) {
++((TestExternalPassUserData *)userData)->runCallCount;
MlirStringRef opName = mlirIdentifierStr(mlirOperationGetName(op));
if (!mlirStringRefEqual(opName,
mlirStringRefCreateFromCString("func.func"))) {
mlirExternalPassSignalFailure(pass);
}
}
void testRunFailingExternalPass(MlirOperation op, MlirExternalPass pass,
void *userData) {
++((TestExternalPassUserData *)userData)->runCallCount;
mlirExternalPassSignalFailure(pass);
}
MlirExternalPassCallbacks makeTestExternalPassCallbacks(
MlirLogicalResult (*initializePass)(MlirContext ctx, void *userData),
void (*runPass)(MlirOperation op, MlirExternalPass, void *userData)) {
return (MlirExternalPassCallbacks){testConstructExternalPass,
testDestructExternalPass, initializePass,
testCloneExternalPass, runPass};
}

@sequencer
Copy link
Author

How does ExternalPassCallbacks construct? It should construct from 5 callbacks:

construct: void => void
destruct: void => void
initialize: Option[MlirContext => MlirLogicalResult]
clone: void => void
run (MlirOperation, MlirExternalPass) => void

The MlirExternalPass is a struct that need to be construct from the C-API.

@SpriteOvO
Copy link
Member

SpriteOvO commented Dec 29, 2024

How does ExternalPassCallbacks construct?

See

MlirExternalPassCallbacks makeTestExternalPassCallbacks(
MlirLogicalResult (*initializePass)(MlirContext ctx, void *userData),
void (*runPass)(MlirOperation op, MlirExternalPass, void *userData)) {
return (MlirExternalPassCallbacks){testConstructExternalPass,
testDestructExternalPass, initializePass,
testCloneExternalPass, runPass};
}

The MlirExternalPass is a struct that need to be construct from the C-API.

MlirExternalPass is an opaque struct type defined here,

DEFINE_C_API_STRUCT(MlirExternalPass, void);

you don't need to construct it, it will be an argument in your callback and you just use it as needed.

void yourRunCallback(MlirOperation op, MlirExternalPass pass, void *userData) {
  if (anyError) {
    mlirExternalPassSignalFailure(pass);
  }
}

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