Skip to content

[Instrumentation] Support MachineFunction in ChangeReporter #80946

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

Merged
merged 1 commit into from
Apr 11, 2024

Conversation

paperchalice
Copy link
Contributor

No description provided.

Out << "*** MIR Dump At Start ***\n";
for (const Function &F : M)
if (auto *MF = MMI.getMachineFunction(F))
MF->print(Out);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact, not all machine functions are available at this time, when running default codegen pipeline.

Copy link
Contributor

Choose a reason for hiding this comment

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

that's a good point. I'm not sure if we should print something to tell the user that the machine function doesn't exist.

it is weird to print MIR Dump At Start and then not print anything if we don't have any MFs yet. perhaps we just don't print anything here if we don't have any MFs

Out << "*** MIR Dump At Start ***\n";
for (const Function &F : M)
if (auto *MF = MMI.getMachineFunction(F))
MF->print(Out);
Copy link
Contributor

Choose a reason for hiding this comment

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

that's a good point. I'm not sure if we should print something to tell the user that the machine function doesn't exist.

it is weird to print MIR Dump At Start and then not print anything if we don't have any MFs yet. perhaps we just don't print anything here if we don't have any MFs

@@ -90,7 +90,9 @@ Error MachineFunctionPassManager::run(Module &M,
for (unsigned I = Begin, E = Idx; I != E; ++I) {
auto *P = Passes[I].get();

if (!PI.runBeforePass<MachineFunction>(*P, MF))
// Keep BeforeStack empty in ChangeReporter
if (P->name() != FreeMachineFunctionPass::name() &&
Copy link
Contributor

Choose a reason for hiding this comment

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

we should invoke runAfterPassInvalidated below if we see that we're running FreeMachineFunctionPass, rather than not running instrumentation on it

Copy link
Contributor

Choose a reason for hiding this comment

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

I have something that does this in my rewrite

Copy link
Contributor

Choose a reason for hiding this comment

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

we can keep this for now, but #81068 will overwrite this

Copy link
Contributor

@aeubanks aeubanks left a comment

Choose a reason for hiding this comment

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

lg with or without the "no MIR functions to print" comment resolved

@@ -90,7 +90,9 @@ Error MachineFunctionPassManager::run(Module &M,
for (unsigned I = Begin, E = Idx; I != E; ++I) {
auto *P = Passes[I].get();

if (!PI.runBeforePass<MachineFunction>(*P, MF))
// Keep BeforeStack empty in ChangeReporter
if (P->name() != FreeMachineFunctionPass::name() &&
Copy link
Contributor

Choose a reason for hiding this comment

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

we can keep this for now, but #81068 will overwrite this

@aeubanks
Copy link
Contributor

aeubanks commented Feb 8, 2024

for simplicity, I think we should just not print the initial MIR since it's unclear exactly what the right thing to do is

@aeubanks
Copy link
Contributor

aeubanks commented Feb 9, 2024

should we just completely drop handleInitialMIR()?

@paperchalice
Copy link
Contributor Author

paperchalice commented Feb 10, 2024

should we just completely drop handleInitialMIR()?

CFG instruments may benefit from this in verbose mode, but remove it is also ok.

Copy link
Contributor

@aeubanks aeubanks left a comment

Choose a reason for hiding this comment

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

lgtm after comments addressed

@@ -448,6 +477,11 @@ template <typename T> void TextChangeReporter<T>::handleInitialIR(Any IR) {
M->print(Out, nullptr);
}

template <typename T>
void TextChangeReporter<T>::handleInitialMIR(const MachineFunction *IR) {
// For simplicity, don't print the initial MIR.
Copy link
Contributor

Choose a reason for hiding this comment

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

add the reason that it's likely that the MIR doesn't initially exist

@@ -362,6 +384,13 @@ void ChangeReporter<T>::saveIRBeforePass(Any IR, StringRef PassID,
handleInitialIR(IR);
Copy link
Contributor

Choose a reason for hiding this comment

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

should we be calling this on MIR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Found previous implementation has some problem, will try to combine handleInitialIR and handleInitialMIR.

@aeubanks
Copy link
Contributor

btw we should probably start looking into how to get the hard passes ported, like SelectionDAG or the AsmPrinters

@paperchalice paperchalice force-pushed the NPM/CodeGen/si branch 2 times, most recently from a547f69 to 32fcaa3 Compare February 13, 2024 06:48
@paperchalice paperchalice force-pushed the NPM/CodeGen/si branch 2 times, most recently from 28f6547 to a8d5576 Compare March 2, 2024 14:36
InitialIR = false;
if (VerboseMode)
handleInitialIR(IR);
if (const auto *MF = unwrapIR<MachineFunction>(IR); !MF || !MF->empty()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

is the intention here to make the output of instruction selection the "initial IR"?

I feel like being explicit about the fact that instruction selection is actually modifying the MachineFunction is reasonable, so the initial IR being empty isn't too bad. but I'm open to this as well

@@ -409,6 +435,10 @@ template <typename T>
void ChangeReporter<T>::handleInvalidatedPass(StringRef PassID) {
assert(!BeforeStack.empty() && "Unexpected empty stack encountered.");

// Prepare to process the next MIR.
if (PassID == FreeMachineFunctionPass::name())
Copy link
Contributor

Choose a reason for hiding this comment

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

can you explain why this is necessary?

I think in general I'm not understanding the intent with "InitialIR" in regards to MIR. Previously we'd always start with the entire module as the initial IR right? Why are we handling MachineFunctions differently?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's hard to determine when the initial MIR for a function is created. If the input is general LLVM IR and the pipeline is not interrupted by a module pass then new machine function will not be created until FreeMachineFunction, which is the initial MIR for the next function.

Copy link
Contributor

Choose a reason for hiding this comment

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

InitialIR is used to determine when to call handleInitialIR, which is typically used to print to the user "Initial IR: ...". I don't think we need that for MIR, except in the case where we start with MIR (mostly just tests). We can just let before/after pass handle MIR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let before/after pass handle MIR now, but still print all available MIRs when print-module-scope is on.

@paperchalice
Copy link
Contributor Author

Ping? Now empty machine functions are no longer treated specially.

@paperchalice paperchalice merged commit 026165f into llvm:main Apr 11, 2024
paperchalice added a commit that referenced this pull request Apr 11, 2024
@paperchalice paperchalice deleted the NPM/CodeGen/si branch April 12, 2024 04:25
Zentrik pushed a commit to Zentrik/llvm-project that referenced this pull request Jun 29, 2024
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