This is an archive of the discontinued LLVM Phabricator instance.

[TableGen] Support custom decoders for variable length instructions
ClosedPublic

Authored by myhsu on Jan 18 2023, 8:42 PM.

Details

Summary

Just like the encoder directive for variable-length instructions, this patch adds a new decoder directive to allow custom decoder function on an operand.

Right now, due to the design of DecoderEmitter each operand can only have a single custom decoder in a given instruction.

Diff Detail

Event Timeline

myhsu created this revision.Jan 18 2023, 8:42 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 18 2023, 8:42 PM
myhsu requested review of this revision.Jan 18 2023, 8:42 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 18 2023, 8:42 PM
0x59616e added inline comments.Jan 19 2023, 8:05 PM
llvm/utils/TableGen/DecoderEmitter.cpp
1896

nit: Use !empty for better readability.

llvm/utils/TableGen/VarLenCodeEmitterGen.cpp
165–170

nit: getCustomCoders will return empty StringRef automatically if DI->getNumArgs() == 2. So this if statement is unnecessary.

195–198

ditto

215–227

Use early exit to reduce indentation for better readability.

221–222

nit: Avoid declaring multiple variables in the same statement;

0x59616e added inline comments.Jan 19 2023, 8:12 PM
llvm/include/llvm/Target/Target.td
806–807

Under what circumstances will we need different decoders for the same operand ?

0x59616e added inline comments.Jan 19 2023, 8:20 PM
llvm/utils/TableGen/VarLenCodeEmitterGen.cpp
212

getCustomCoders seems unnecessary to be a member function of VarLenInst ?

myhsu updated this revision to Diff 491092.Jan 21 2023, 12:08 PM
myhsu marked 6 inline comments as done.

Addressed feedbacks

llvm/include/llvm/Target/Target.td
806–807

I figure it's probably rare but for instance, the binary encoding of an operand was separated in two different places so the decoder has to puzzle them back.

llvm/utils/TableGen/VarLenCodeEmitterGen.cpp
165–170

good catch, thanks!

I'm not very familiar with varlen decoder, hence only general notes.

llvm/include/llvm/Target/Target.td
806–807

Is it diagnosed somewhere?

llvm/utils/TableGen/VarLenCodeEmitterGen.cpp
88
  • The guideline is to limit anonymous namespaces to class declarations and use static everywhere else.
  • The function does not protect against users, it needs proper error checking.

Should we add a description to llvm/docs/TableGen/ProgRef.rst?

The TableGen Programmer's Reference does not include descriptions of backend facilities.

The TableGen Programmer's Reference does not include descriptions of backend facilities.

So maybe BackGuide.rst ?

Yes, that makes sense.

myhsu added a comment.Jan 22 2023, 5:34 PM

The TableGen Programmer's Reference does not include descriptions of backend facilities.

So maybe BackGuide.rst ?

I don't think it will be a good place since it's for TableGen backend developers while this patch only adds new TG directives for disassembler developers. Right now, IMO, the most related document is actually Writing an LLVM Backend, which is the only place mentioning TG syntax for writing instruction encodings (for fixed-length instructions, of course). Instead of cluttering with Writing an LLVM Backend I feel like a better way will be creating a separate page for variable-length instruction encoding / decoding.

myhsu updated this revision to Diff 491239.Jan 22 2023, 10:58 PM
myhsu marked 2 inline comments as done.
  • Pull getCustomCoders out of anonymous namespace
  • Report a fatal error if a encoder or decoder directive is not followed by a function name.
llvm/include/llvm/Target/Target.td
806–807

I double check with the code and found that it requires some works to report such diagnostics (e.g. print a warning if two different decoders are used on the same operand), which personally I think it's not worth it.

0x59616e accepted this revision.Jan 24 2023, 6:24 AM

LGTM. Thanks for this amazing work ;)

This revision is now accepted and ready to land.Jan 24 2023, 6:24 AM
This revision was landed with ongoing or failed builds.Jan 24 2023, 10:03 PM
This revision was automatically updated to reflect the committed changes.
skan added a subscriber: skan.Feb 8 2023, 3:05 AM