This is an archive of the discontinued LLVM Phabricator instance.

[lldb][NFC] Use MCInstrAnalysis when available in the disassembler plugin
ClosedPublic

Authored by RamNalamothu on Jul 24 2023, 12:38 AM.

Details

Summary

Since the info in MCInstrDesc is based on opcodes only, it is often quite
inaccurate. The MCInstrAnalysis has been added so that targets can provide
accurate info, which is based on registers used by the instruction, through
the own versions of MCInstrDesc functions.

The RISCVMCInstrAnalysis, which needs to refine several MCInstrDesc methods,
is a good example for this.

Given the llvm-objdump also uses MCInstrAnalysis, I think this change is in
the right direction.

The default implementation of MCInstrAnalysis methods forward the query to
MCInstrDesc functions. Hence, no functional change is intended/expected.

To avoid bloating up MCInstrAnalysis, only the methods provided by it and
the ones used by disassembler plugin are changed to use MCInstrAnalysis when
available.

Though I am not sure if it will be useful, making MCInstrAnalysis available
in the disassembler plugin would allow enabling symbolize operands (D84191)
feature in lldb's disassembler as well.

Diff Detail

Event Timeline

RamNalamothu created this revision.Jul 24 2023, 12:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 24 2023, 12:38 AM
RamNalamothu requested review of this revision.Jul 24 2023, 12:38 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJul 24 2023, 12:38 AM

Refer to the commit which added symbolize operands feature in llvm-objdump.

RamNalamothu edited the summary of this revision. (Show Details)Jul 24 2023, 1:00 AM

What I understand is that MCInstrAnalysis defaults to using MCInstrDesc, however not all targets have MCInstrAnalysis at all. So we have this awkwardness here with the two pointers, which is fine.

Sounds good to me, I am just wondering about the changes over in llvm.

llvm/include/llvm/MC/MCInstrAnalysis.h
80

Does this have test coverage already?

RamNalamothu marked an inline comment as done.Jul 24 2023, 4:50 AM
RamNalamothu added inline comments.
llvm/include/llvm/MC/MCInstrAnalysis.h
80

I think llvm/tools/llvm-cfi-verify has enough test coverage for this method.

RamNalamothu marked an inline comment as done.Jul 24 2023, 5:26 AM

I'm not super familiar with the MCInst class from llvm, and hadn't heard of MCInstrAnalysis. I was looking through the llvm targets - are these MCInstrAnalysis primitives going to be implemented for all targets we support today? I see them defined for e.g. MIPS and RISCV, but I'm not sure AArch64 does. Does isBranch include other variants like isUnconditionalBranch? You check if we got an MCInstrAnalysis for this target - do we get none for a target like AArch64 and fall back to our current behavior?

I'm not super familiar with the MCInst class from llvm, and hadn't heard of MCInstrAnalysis. I was looking through the llvm targets - are these MCInstrAnalysis primitives going to be implemented for all targets we support today?

Except few, all the other targets implement MCInstrAnalysis.

I see them defined for e.g. MIPS and RISCV, but I'm not sure AArch64 does.

AArch64 does implement it.
AArch64
AMDGPU
PowerPC
X86

Does isBranch include other variants like isUnconditionalBranch?

No. They are implemented as separate methods. You can see that with a full context diff of MCInstrAnalysis.h changes in this revision or MCInstrAnalysis.h

You check if we got an MCInstrAnalysis for this target - do we get none for a target like AArch64 and fall back to our current behavior?

If the target doesn't implement MCInstrAnalysis or implements but doesn't override the methods, we fall back to the current behavior because the default implementation of MCInstrAnalysis methods forward the query to MCInstrDesc functions which the current behavior relies on.

Does isBranch include other variants like isUnconditionalBranch?

No. They are implemented as separate methods. You can see that with a full context diff of MCInstrAnalysis.h changes in this revision or MCInstrAnalysis.h

mayAffectControlFlow doesn't test for isUnconditionalBranch. Is that a problem? I didn't look through the different property check methods like this, but I happened to notice this one and see it wasn't detected in mayAffectControlFlow. Maybe I misunderstood something.

It seems that a lldb specific test is needed. Adding a new method to llvm/include/llvm/MC/MCInstrAnalysis.h is fine with me, though I haven't checked the semantics.

Does isBranch include other variants like isUnconditionalBranch?

No. They are implemented as separate methods. You can see that with a full context diff of MCInstrAnalysis.h changes in this revision or MCInstrAnalysis.h

mayAffectControlFlow doesn't test for isUnconditionalBranch. Is that a problem? I didn't look through the different property check methods like this, but I happened to notice this one and see it wasn't detected in mayAffectControlFlow. Maybe I misunderstood something.

The idea is MCInstrAnalysis's default implementation just replicates what MCInstrDesc does (MCInstrDesc::mayAffectControlFlow) and the individual targets can refine those methods as needed.

It seems that a lldb specific test is needed. Adding a new method to llvm/include/llvm/MC/MCInstrAnalysis.h is fine with me, though I haven't checked the semantics.

I will try to add a lldb specific test.

Add lldb test.

Added RISC-V specific test which overrides MCInstrAnalysis methods and uses the newly added code path.

Tests in lldb/unittests/Disassembler/x86/TestGetControlFlowKindx86.cpp use MCInstrAnalysis default methods and need old behavior before this patch.
Tests in lldb/unittests/Disassembler/RISCV/TestMCDisasmInstanceRISCV.cpp use RISC-V overridden MCInstrAnalysis methods and need new behavior from this patch.

Does this look good now?

Fix RISC-V test name.

Oops. Didn't notice the changed formatting of the comment after git-clang-format.

Fixed the format issue.

Remove left overs of copy-paste. Apologies for so many updates.

MaskRay added inline comments.Aug 2 2023, 3:14 PM
lldb/unittests/Disassembler/RISCV/TestMCDisasmInstanceRISCV.cpp
24

Place all classes and test methods in an anonymous namespace.

65

Early return

llvm/include/llvm/MC/MCInstrAnalysis.h
76

return Info->get(Inst.getOpcode()).hasDefOfPhysReg(Inst, PC, MCRI)

Address review comments.

RamNalamothu marked 3 inline comments as done.Aug 2 2023, 9:20 PM

Thanks for the comments @MaskRay.

lldb/unittests/Disassembler/RISCV/TestMCDisasmInstanceRISCV.cpp
24

All the disassembler tests need this change. Will address that in a separate patch.

RamNalamothu marked an inline comment as done.Aug 6 2023, 7:22 PM

Ping.

I think I have addressed all the review feedback. Ok to land?

jasonmolenda accepted this revision.Aug 11 2023, 10:21 AM

The lldb side looks good to me, and I think you've addressed the llvm parts.

This revision is now accepted and ready to land.Aug 11 2023, 10:21 AM

Thank you @jasonmolenda.

If there are no further comments on llvm side, I will land this in a couple of days.

This revision was landed with ongoing or failed builds.Aug 13 2023, 8:07 PM
This revision was automatically updated to reflect the committed changes.