Skip to content

[LLVM][TableGen] Parameterize NumToSkip in DecoderEmitter #136187

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

Closed

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Apr 17, 2025

  • Add command line option num-to-skip-size to parameterize the size of NumToSkip bytes in the decoder table.
  • Default value will be 2, and targets that need larger size can use 3.
  • Keep all existing targets, except AArch64, to use size 2, and change AArch64 to use size 3 since it run into the "disassembler decoding table too large" error with size 2.
  • Following is a rough reduction in size for the decoder tables by switching to size 2.

@jurahul
Copy link
Contributor Author

jurahul commented Apr 17, 2025

Note, now the decoder table generated by the decoder emitter looks like:

struct DecoderTable2Bytes { // Decoder Table with 2 bytes NumToSkip.
   ArrayRef<uint8_t> Data;
};
struct DecoderTable3Bytes { // Decoder Table with 3 bytes NumToSkip.
   ArrayRef<uint8_t> Data;
};
static constexpr uint8_t DecoderTable32RawData[] = ... 
static constexpr DecoderTable3Bytes DecoderTable32{DecoderTable32RawData};
...
template <typename InsnType>
static DecodeStatus decodeInstruction(DecoderTable3Bytes DecodeTable,
                                      MCInst &MI, InsnType insn,
                                      uint64_t Address,
                                      const MCDisassembler *DisAsm,
                                      const MCSubtargetInfo &STI) 

So we will now have 2 version of decodeInstruction with different signatures and hence safe from tramping on each other.

@jurahul jurahul force-pushed the decoder_num_to_skip_expensive_checks branch from 20fcc35 to b572c68 Compare April 17, 2025 20:13
@jurahul jurahul changed the title [LLVM][TableGen] Paramaterize NumToSkip in DecoderEmitter [LLVM][TableGen] Parameterize NumToSkip in DecoderEmitter Apr 17, 2025
@jurahul
Copy link
Contributor Author

jurahul commented Apr 17, 2025

@topperc can you PTAL at the fix I am proposing for the expensive checks failure with this change I saw yesterday? For context, here's my assessment of my this broke it: #136019 (comment). Not asking for a full review, just some high-level eyeballing.

The other option I considered is encoding the NumToSkipSizeInBytes in the first byte of each DecodeTable (as a table header): 0x80 | NumToSkipSizeInBytes and then keeping a single decodeInstruction function that first reads this byte and then steers to a templated impl function based on size 2 or 3. But that means every time we decode we pay this 1-time cost of the branch. This approach instead avoids that and steers the code directly to the right one.

If this looks ok overall, I'll likely separate the AMDGPU refactor to move calls to decodeInstruction out of the header file and commit that first and then commit this. I had to do this because the new DecoderTable2Bytes type cannot be referenced in the header as the header does not (and should not) include the generated .inc file.

@jurahul
Copy link
Contributor Author

jurahul commented Apr 17, 2025

FYI, this PR has 2 commits, so I've kept the additional changes on top of yesterday's changes in a separate commit

Add wrapper structs to discriminate between decode tables that use 2 vs
3 byte for NumToSkip, and overload the `decodeInstruction` function
based on this type. This ensures that if we have a mix of 2 and 3 byte
coode, it works correctly. Without this, 2 different compilation units
may generate 2 different versions of `decodeInstruction` and can cause
problems.
@jurahul jurahul force-pushed the decoder_num_to_skip_expensive_checks branch from b572c68 to 83e5b4f Compare April 17, 2025 20:29
@jurahul jurahul closed this Apr 17, 2025
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.

1 participant