[llvm-dev] RFC: Combining Annotation Metadata and Remarks

Florian Hahn via llvm-dev llvm-dev at lists.llvm.org
Wed Nov 4 13:57:03 PST 2020


Hi,


I would like to propose a new  !annotation metadata kind that can be attached to arbitrary instructions to drive generating remarks that provide additional insight into transformations applied to a program.

To motivate this, consider these specific questions we would like to get answered: 

* How many stores added for automatic variable initialization remain after optimizations? Where are they?
* How many runtime checks inserted by a frontend could be eliminated? Where are the ones that did not get eliminated?

With the current infrastructure we can issue remarks for removed stores, removed runtime checks or when these instructions are not removed. However that’s far too noisy. We would like to filter to stores or checks that originated from user intent (e.g. auto-init). The new annotation would help to distinguish such cases.

Asking and answering such questions for large code bases provides metrics to asses the effectiveness of transformations. It also gives users a way to audit there code & detect certain problematic patterns. They should also help with making decision on a more data-driven basis.

At the moment, the existing statistics counters can be used for parts of the assessment part. The main advantage of using remarks to generate the data is that we can collect data on a much finer granularity, e.g. at the function level. This can be helpful, for example to create function-level diffs that show the impact of certain new transformations. It also allows displaying the collected data to the user in-line with the source code.


The proposal boils down to three parts.

The first part consists of adding a new metadata kind (!annotation ) to allow tagging of ‘interesting’ instructions. For example, Clang could add this metadata to auto-init stores it generates or a library for overflowing math could instruct Clang to add the metadata to all instructions in its overflow check functions. !annotation metadata nodes consist of a tuple containing string nodes indicating the type of the annotation.

Using metadata comes with the drawback that it can be dropped by passes that do not know how to handle it. This means we will have to adjust existing passes to ensure the metadata is preserved for interesting instructions, if possible. I think this is the only intersection of the proposal with existing code in LLVM. Loosing the metadata for instructions is also not the end of the world. The information is provided on a best-effort basis and I am not aware of a more robust alternative to achieve the same goals.

Examples:

    store i32 0, i32* %ptr, !annotation !0
    %c = icmp ult i32 %x, %y, !annotation !2

  …
 
  !0 = !{!1}
  !1 = !{!"auto-init”}
  !2 = !{!3}
  !3 = !{!"overflow-check"}



The second part consists of a pass that runs at the end of the optimization pipeline and generates remarks for instructions with !annotation metadata. The remarks could provide summaries of the number of auto-init stores surviving after optimizations, or the number of overflow checks that could not be eliminated per function. Additional remarks could also point out where Clang inserted initialization code that could not be eliminated. Combining this with a function-level code size diffs should allow users to quickly track down the origin of code-size regressions caused by auto-init code for example. Similarly, remarks for overflow checks could be used to spot weaknesses in LLVM’s reasoning about such checks. An example for a remark generated from annotation metadata is shown below

  Pass: annotation-remarks
  Name: AutoInitSummary
  Function: test2
  Args:
  		• String: 'Annotated '
  		• count: '2'
  		• String: ' instructions with '
  		• type: auto-init
  …



The third part consists of a set of tools to analyze & summarizes the data mined from the remarks. One very useful tool I think would be a run-over-run diff of various remarks. Note that we probably want to initially limit this to some of the less noisy remarks (e.g. number of auto-init stores remaining per function, code size per function). One potential practical issue for such a tool is that currently remarks are defined & emitted somewhat ad-hoc and changes to remarks could break the diff tool. But I think most existing remarks are at a point now where they are quite stable. There’s also been a proposal to define remarks in a more structured way (http://lists.llvm.org/pipermail/llvm-dev/2020-January/137971.html) which would also be helpful.

While I intend to work on some parts directly to start with, I hope this pitch also sparks more interest and others will be interested in collaborating as well. I also put up an initial patch adding the infrastructure outlined in the first two points: https://reviews.llvm.org/D89240

The proposal also ties together and is enabled by some of the excellent work around the remark infrastructure recently, such as Francis’ work on emitting remarks as part of the binary or Jessica’s work on a remarks-based code-size diffing tool (https://reviews.llvm.org/D63306)


Cheers,
Florian


More information about the llvm-dev mailing list