[CRT] Add platform-specific pre and post function calls in crt runtime


This feature would add the possibility for a developer to write platform-specific functions which will be called inside the RunTimeEvaluator, similar to the way we currently call TVMPlatformTimerStart, TVMPlatformTimerStop, etc.

The pre function (TVMPlatformPreFuncCall) will be called before calling TVMPlatformTimerStart in the crt RunTimeEvaluator, and it could be used, for example, to:

  • Call a platform specific cache cleaning function
  • Initialize custom counters in the accelerator

The post function (TVMPlatformPostFuncCall) will be called after calling TVMPlatformTimerStop in the crt RunTimeEvaluator, and it could be used, for example, to:

  • Clean accelerator specific data
  • Read performance counters


Generate more accurate measurements for AutoTVM and a place for the developers to insert platform specific code like initializations, cache cleanings, etc.


  • More accurate measurements for AutoTVM.
  • Weak links are provided so that this functions are not mandatory.

Pull request is this one.

hey @fpecc i’m interested to know more about your motivations here. are there things you can’t do with TVMPlatformTimerStart and TVMPlatformTimerStop that this enables? The function names are pretty broad, so I’m curious which usages of TVMFuncCall these would be used with.

Hi @areusch,

My motivation is that I want to clear the cache of the RISCV CPU and the internal TLB of the accelerator I am working on before executing each function evaluation. I believe this will give more accurate measures for AutoTVM (similar to what is done with enable_cpu_cache_flush). Because this cleaning task is platform-specific, I would like to implement this pre-function to do it, before starting the timer. Just for symmetry, I also proposed to add a post-function call, in case someone needs it.

Yes, I could implement the necessary logic inside TVMPlatformTimerStart and TVMPlatformTimerEnd. But because of the name of these two functions, I thought that they could be called somewhere else in the future in order to measure some other execution time. I don’t want to accidentally clean my accelerator cache if these functions are starting to be used in some other place of the code in the future.

@fPecc that makes sense to me. Because this is an API function, I think it’d be better to adopt a more narrowly-focused name. I agree that it makes sense to divorce the timer APIs from those APIs that prepare a platform for measurement.

What do you think of just introducing a single function, e.g. TVMPlatformBeforeMeasurement() whose semantics are just that it’s called from TimeEvaluator before invoking the function?

Yes, that would work for me and my special case, but I think it is general enough for other people to use it also.

Do you want me to change the code and make a new PR?

I’m okay with a before and after function, but I do think it would be great to scope the API to measurement rather than function call, since that’s how it’s proposed to be used. @fPecc it would be great if you could open a PR, I’d be happy to review!