Using CostModel to estimate machine cycles of each instruction

There is CostModel.cpp since LLVM3, I am wondering if anyone can give me an concrete example on how to use this pass to estimate cycles used in a given IR file.

Thank you very much.

Don

Hi Don,

In it’s current version, tha pass can only be used via “opt -cost-model -analyze”. It will then print out the estimated cost for every instruction.

In order to be able to use it from a different pass, I split it into a source and header file (attached).

You can then add it to your pass dependencies, and use it as follows:
custom::CostModelAnalysis &CM = getAnalysiscustom::CostModelAnalysis();

Instruction *Inst = …
unsigned Cost = CM.getInstructionCost(CI);
if (Cost != (unsigned)(-1)) {

// do something
}

Hope this helps!

Jonas

CostModel.cpp (15.5 KB)

CostModel.h (916 Bytes)

Ya I am using it the same way. By dividing it into header file and including it from “llvm/Analysis/CostModel.h”

I believe it returns -1 for all function calls? How can I get make it return cost of each function call inside my basic block.

One way is running it recursively on calls and return summaries. Is there a direct way than this?

Thanks!
Manish

Ya I am using it the same way. By dividing it into header file and including it from “llvm/Analysis/CostModel.h”

I believe it returns -1 for all function calls? How can I get make it return cost of each function call inside my basic block.

One way is running it recursively on calls and return summaries. Is there a direct way than this?

Thanks!

While others have given specific advice about using the cost model
analysis, note that it isn't actually measuring the expected cycles. At
best it is measuring the size / number of instructions in the LLVM IR.