Skip to content

[CodeGen] Expose the extensibility of PassConfig to plugins #139059

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

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

Conversation

Tcc100
Copy link

@Tcc100 Tcc100 commented May 8, 2025

This PR exposes the backend pass config to plugins via a callback. Plugin authors can register a callback that is being triggered before the target backend adds their passes to the pipeline. In the callback they then get access to the TargetMachine, the PassManager, and the TargetPassConfig. This allows plugins to call TargetPassConfig::insertPass, which is honored in the subsequent addPass of the main backend. We implemented this using the legacy pass manager as the backend is still using the old pass manager.

The following example shows how plugin authors can use the callback. Since its a callback that is not doing anything without anybody registering it, there shouldn't be any potential harm to the compiler unless a plugin is present.

__attribute__((constructor)) static void initCodeGenPlugin() {
    initializeCodeGenTestPass(*PassRegistry::getPassRegistry());

    TargetMachine::registerTargetPassConfigCallback([](auto &TM, auto &PM, auto *TPC) {
        TPC->insertPass(&GCLoweringID, &CodeGenTest::ID);
    });
}

Copy link

github-actions bot commented May 8, 2025

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.

@Tcc100
Copy link
Author

Tcc100 commented May 8, 2025

@nikic @aeubanks please have a look

Copy link
Contributor

@antoniofrighetto antoniofrighetto left a comment

Choose a reason for hiding this comment

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

Missing a test for this?

Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

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

insertPass is a bad API (and probably going away in the new PM?). Should probably have a set of insert points like the new pass manager does for middle end passes. Also, needs tests / a sample and documentation

@@ -72,6 +74,10 @@ namespace yaml {
struct MachineFunctionInfo;
} // namespace yaml

class TargetMachine;
using PassConfigCallback =
std::function<void(TargetMachine &, PassManagerBase &, TargetPassConfig *)>;
Copy link
Contributor

Choose a reason for hiding this comment

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

function_ref?

@@ -119,6 +119,9 @@ addPassesToGenerateCode(CodeGenTargetMachineImpl &TM, PassManagerBase &PM,
PM.add(PassConfig);
PM.add(&MMIWP);

for (auto& C : *TargetMachine::TargetPassConfigCallbacks)
Copy link
Contributor

Choose a reason for hiding this comment

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

no auto (and no &?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants