Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Use SILPrinter callback mechanism to annotate the printed SIL code #29

Closed
mhong opened this issue May 4, 2018 · 2 comments
Closed

Use SILPrinter callback mechanism to annotate the printed SIL code #29

mhong opened this issue May 4, 2018 · 2 comments

Comments

@mhong
Copy link

mhong commented May 4, 2018

4e27ff2 added code for annotating SIL code with TFPartition markings.

One follow-up suggestion from clattner is to use sil printer callback before printing each instruction (SILPrinter.cpp:634):

    for (const SILInstruction &I : *BB) {
      Ctx.printInstructionCallBack(&I);
      if (SILPrintGenericSpecializationInfo) {
        if (auto AI = ApplySite::isa(const_cast<SILInstruction *>(&I)))
          if (AI.getSpecializationInfo() && AI.getCalleeFunction())
            printGenericSpecializationInfo(
                PrintState.OS, "call-site", AI.getCalleeFunction()->getName(),
                AI.getSpecializationInfo(), AI.getSubstitutions());
      }
      print(&I);
    }

So we can derive a struct from SILPrintContext, and implement [move] kind of annotations by overriding Ctx.printInstructionCallBack. This way we hopefully just need to call “print” on the function with the right printer context defined.

Example code:

/// A print context which records the line numbers where instructions are
/// printed.
struct AnalysisPrintContext : public SILPrintContext {
  AnalysisPrintContext(llvm::raw_ostream &OS) : SILPrintContext(OS) {}

  ~AnalysisPrintContext() override {}

  void printInstructionCallBack(const SILInstruction *I) override {
    OutStream << "XXXX ";
    I->print(OutStream);
  }
};

Call site:

  if (auto *outs = getTFDumpIntermediateStream()) {
    AnalysisPrintContext ctx(*outs);
    fn.print(ctx);
  }

One challenge is that in addition to annotating instructions, we also want to annotate BB args (code here). So the current callback mechanism on inst level is not sufficient. One option is to extend the SILPrinter callback infra to support annotating BB args.

@lattner
Copy link

lattner commented May 4, 2018

Makes sense. It would be great to upstream such an extension, I suspect that the swift.org folks would take such a patch with no concern.

@rxwei
Copy link

rxwei commented May 12, 2018

Moved to SR-7669.

@rxwei rxwei closed this as completed May 23, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants