How shall I evaluate the latency of each instruction in LLVM IR?

Inspired by https://www.agner.org/optimize/instruction_tables.pdf, which gives us the latency and reciprocal throughput of each instruction in the different architecture of X86, Is there anybody taking the effort to do a similar job for LLVM IR?

Thanks!

There is no fixed latency/throughput to LLVM IR instructions. Instructions are lowered to target instructions based on the target and the subtarget, and these lowerings can have multiple IR instructions coalesced into a single target instruction or one IR instruction split into several instructions. Latency/throughput is provided for target instructions based on the *Schedule.td files, but there is no easy mapping between these instructions and LLVM IR. Some ad-hoc estimation for the IR level is provided by TargetLowering and especially TargetTransformInfo, but these do not provide complete coverage and only provide coarse estimates.

Hi Duan,

It sounds like you might be interested in llvm’s Machine Code Analyzer. It’s an llvm tool that can calculate reciprocal throughput by simulating an out-of-order instruction pipeline. It also provides other useful cycle information:

https://llvm.org/docs/CommandGuide/llvm-mca.html

Your question asks about IR. llvm-mca currently only works on assembly. However, you can easily generate assembly from IR or make use of MCA as a library. I suggest just converting your IR to assembly and running that through llvm-mca.

MCA is built on LLVM instruction scheduling information which contains cycle latency information. If you’re curious of what that data looks like, outside of MCA, take a peek at the X86 instruction scheduling information located around: ‘llvm/lib/Target/X86/X86Schedule.td’

-Matt

We have an interface for estimating this, although it depends on the targets providing appropriate information through the TargetTransformInfo interface.

Call TTI->getInstructionCost with the kind parameter set to TCK_Latency.

-Hal

Hi all:
Thanks for your help, everyone. getInstructionCost and llvm-mca is helpful for me. I will test it later.

Finkel, Hal J. <hfinkel@anl.gov> 于2019年5月14日周二 上午12:57写道:

Hello, I have just started using TargetTransformInfo for some of my work and I have a couple of questions.

Currently I am using this in using the following syntax:

*TargetTransformInfo TTI = &getAnalysis().getTTI(fn);
TTI->getInstructionCost(Inst, TargetTransformInfo::TargetCostKind::TCK_RecipThroughput)

Am i correct in assuming that the cost returned from this function is for the current compiled hardware? Also is there any way for me to get the same information for other targets?

I am just a beginner at LLVM so any help will be appreciated!

Thanks in advance

It should be for whatever -mtriple/-mcpu values are set at the command line