Page MenuHomePhabricator

[MIR] Target specific MIR formating and parsing
AcceptedPublic

Authored by pguo on Nov 4 2019, 6:42 PM.

Details

Reviewers
dsanders
arsenm
Summary

Added MIRFormatter for target specific MIR formating and parsing with
immediate and custom pseudo source values. Target machine can subclass
MIRFormatter and implement custom logic for printing and parsing
immediate and custom pseudo source values for better readability.

  • Target specific immediate mnemonic need to start with "." follows by identifier string. When MIR parser sees immediate it will call target specific parsing function.
  • Custom pseudo source value need to start with custom follows by double-quoted string. MIR parser will pass the quoted string to target specific PSV parsing function.
  • MIRFormatter have 2 helper functions to facilitate LLVM value printing and parsing for custom PSV if they refers LLVM values.

Event Timeline

pguo created this revision.Nov 4 2019, 6:42 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 4 2019, 6:42 PM

Could use some tests for the various parse error conditions

llvm/include/llvm/CodeGen/MIRFormatter.h
2

Should pad this out to be the same length as the other lines

59

Typo Implemente

llvm/lib/CodeGen/MIRParser/MIParser.cpp
1536

Capitalize and period

2613–2617

Should this go in the PerTargetMIParsingState?

3220

Initialize to null at the function entry and remove the = nullptrs throughout the function?

3231

Extra blank lines between the switch cases

3243

Capitalize

3260

Typo unse

llvm/lib/CodeGen/MachineOperand.cpp
484–488

I don't see the point of adding this function? This probably shouldn't be leaking the MIRPrinter into MachineOperand.cpp

1169

Single quotes

llvm/test/CodeGen/AMDGPU/GlobalISel/regbankselect-amdgcn.struct.buffer.store.ll
114

I'm surprised to see these in a quoted form? Is that just the default behavior without implementing something?

Thanks for doing this. It should make a lot of things easier to read.

In addition to Matt's comments, I have a few other minor comments.

llvm/include/llvm/CodeGen/MIRFormatter.h
37

This is just an optional nitpick. Using ~0 is fine but if you used Optional<unsigned> then you wouldn't have to explain that. It does take a little more storage though so I don't mind if you stick to ~0

74

Typo? MMIR

llvm/include/llvm/CodeGen/MachineOperand.h
292

Capitalization Nitpick: whether -> Whether.
There's a few other cases of this kind of thing throughout the patch

293

Can we avoid the 'Sometimes' in that sentence? It makes it sound like a bug or a random chance but I think you intended it to mean that 'def' is definitely not printed when that condition is met.

pguo updated this revision to Diff 229196.Wed, Nov 13, 3:49 PM
pguo marked 3 inline comments as done.
  • Fix based on code review.
pguo marked 11 inline comments as done.Wed, Nov 13, 4:03 PM
pguo added inline comments.
llvm/include/llvm/CodeGen/MachineOperand.h
292

These are from old commits and I tend not to change them and just follow the convention.

293

The "sometimes" are from old commits, sorry I made some formatting changes in previous commit and now I revert them.

llvm/lib/CodeGen/MIRParser/MIParser.cpp
2613–2617

The parseTargetImmMnemonic will access current Token and call lex(), so IMO it's better to put it in MIParser class.

llvm/lib/CodeGen/MachineOperand.cpp
484–488

The idea is a helper function to print IR value in MIR serialization format, which can be used by target custom pseudo source value. The target custom pseudo source value can hold IR value reference, it's better to print/parse it in a standard way.

1169

Do you mean back quote here? I intentionally use double quotes here for custom pseudo source value, since back quote ... is used for quoted IR value, which could be part of the serialization output. The design is custom pseudo source value is wrapped in double quoted string which will be passed to target specific formatter. Inside the string it can have back quoted IR value as well as other customized text.

llvm/test/CodeGen/AMDGPU/GlobalISel/regbankselect-amdgcn.struct.buffer.store.ll
114

Yes. The quote is used to find the start and end of a custom pseudo source value string representation, and pass the whole string to target specific parser. The back quote is used for IR const value (QuotedIRValue in MIR lexer), so I choose double quote which will be treated as string constant by MIR lexer.

pguo marked an inline comment as done.Wed, Nov 13, 4:22 PM
pguo added inline comments.
llvm/include/llvm/CodeGen/MIRFormatter.h
37

I think the OpIdx here is not optional but required to get the correct functionally. The tuple <OpCode, OpIdx> will uniquely tell formatter how to format the immediate in a target specific way. While since we can do dump() on any MachineOperand object inside debugger, in this case we don't know the Index, so we have to pass ~0 to the real print function to mark it as unknown.

dsanders added inline comments.Wed, Nov 13, 5:18 PM
llvm/lib/CodeGen/MachineOperand.cpp
1169

I believe Matt means that it's a single character string so he's asking for '\"'

pguo updated this revision to Diff 229239.Wed, Nov 13, 11:35 PM
  • Using single quote when outputing char.
pguo marked 2 inline comments as done.Wed, Nov 13, 11:37 PM
pguo added inline comments.
llvm/lib/CodeGen/MachineOperand.cpp
1169

Thank you Daniel for clarification.

arsenm added inline comments.Wed, Nov 13, 11:52 PM
llvm/include/llvm/CodeGen/TargetInstrInfo.h
1795–1799

This requires the target implementation to recreate the null check and construct pattern Make this non-virtual and have a virtual getMIRFormatterImpl ?

arsenm added inline comments.Thu, Nov 14, 12:05 AM
llvm/include/llvm/CodeGen/TargetInstrInfo.h
1795–1799

I also just realized this should really go in the TargetMachine, not TargeInstrInfo, similar to the target specific YAML functions. The name should also be createMIRFormatter for consistency

pguo updated this revision to Diff 229427.Thu, Nov 14, 4:54 PM
pguo marked an inline comment as done.
  • Move MIRFormatter to TargetMachine

Move MIRFormatter from TargetInstrInfo to TargetMachine.
Move slot to value map from MIParser to PerFunctionMIParsingState.
Some code refactoring.

pguo marked 2 inline comments as done.Thu, Nov 14, 4:59 PM
pguo added inline comments.
llvm/include/llvm/CodeGen/TargetInstrInfo.h
1795–1799

Thank you for pointing me the target specific YAML functions, I didn't aware of them before. Yes, I agree these target specific MIR serialization/de-serialization functions should be put together. I moved to TargetMachine, while I still use getMIRFormatter and get rid of the lazy creation (since I think this object is pretty light). Maybe we should move those target specific YAML functions into MIRFormatter?

pguo marked an inline comment as done.Wed, Nov 20, 4:51 PM

Ping.

dsanders accepted this revision.Tue, Dec 3, 1:27 PM

LGTM from me.

llvm/include/llvm/CodeGen/MIRFormatter.h
37

I think there may be some confusion here so just to clarify: Optional<unsigned> is essentially the same as what you're doing but the unknown case is more explicit and self documenting as it doesn't need a number to be carved out of unsigned and documented as being 'unknown':

printImm(OS, MI, ~0 /* Unknown */, Imm)

becomes:

printImm(OS, MI, None, Imm)
This revision is now accepted and ready to land.Tue, Dec 3, 1:27 PM
pguo updated this revision to Diff 232228.Wed, Dec 4, 3:34 PM
  • [MIR] Target specific MIR formatting and parsing

Use Optional for OpIdx argument.

pguo marked 2 inline comments as done.Wed, Dec 4, 3:35 PM
pguo added inline comments.
llvm/include/llvm/CodeGen/MIRFormatter.h
37

Got it. I switched to use Optional. Thanks.

pguo marked an inline comment as done.Wed, Dec 4, 3:49 PM

@arsenm, any comments on recent changes on moving the MIR formatter to TargetMachine? Thank you.