<div dir="ltr"><div>I would really like to add this feature to llvm-mca.</div><div><br></div><div><div>I have spoken off-line with Matt mutiple time about this feature multiple times. 
<div>I am happy with the suggested approach. Matt prototyped it, and it seems to work okay for us.<br></div>

</div><div><br></div><div>However, it would be really nice to get feedback from somebody else (not necessarily people involved in the llvm-mca project).</div><div>For example, I am interested in what people think about the whole design (i.e. the idea of introducing two new intrinsics, and generating the information in a separate section of the binary object file).<br></div><div><br></div><div>About the suggested design:<br></div>I like the idea of being able to identify code regions using a numeric identifier.</div><div>However, what happens if a code region spans through multiple basic blocks?</div><div><br></div><div>My understanding is that code regions are not allowed to overlap. So, it makes sense if 
`
__mca_code_region_end()` doesn't take an ID as input.<br></div><div>However, what if `
__mca_code_region_end()` ends in a different basic block?</div><div><br></div><div>`__mca_code_region_start()` has to always dominate 
`
__mca_code_region_end()`. This is trivial to verify when both calls are in a same basic block; however, we need to make sure that the relationship is still the same when the `end()` call is in a different basic block.</div><div>That would not be enough. I think we should also verify  that `
__mca_code_region_end()` always post-dominates the call to `__mca_code_region_start()`.<div><br></div><div>My question is: what happens with basic block reordering? We don't know the layout of basic blocks until we reach code emission. How does it work for regions that span through multiple basic blocks?. I think your RFC should clarify this aspect.</div><div><br></div><div>As a side note: at the moment, llvm-mca doesn't know how to deal with branches. So, for simplicity we could force code regions to only contain instructions from a single basic block.</div><div><br></div><div>However, In future we may want to teach llvm-mca how to analyze branchy code too. For example, we could introduce a simple control-flow analysis in llvm-mca, and use an external "branch trace" information (for example, a perf trace generated by an external tool) to decorate branches with with branch probabilities (similarly to what we currently do in LLVM with PGO). We could then use that knowledge to model branch prediction and simulate what happens in the presence of multiple branches.</div><div><br></div><div>So, the idea of having regions that potentially span multiple basic blocks is not bad in general. However, I think you should better clarify what are the constraints (at least, you should answer to my questions from before).</div><div><br></div><div>If we decide to use those new intrinsics, then those should be experimental (at least to start).</div><div><br></div><div><br></div><div><br><div class="gmail_quote"><div dir="ltr">On Thu, Nov 15, 2018 at 11:07 PM via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Introduction<br>
-----------------<br>
Currently llvm-mca only accepts assembly code as input. We would like to<br>
extend llvm-mca to support object files, allowing users to analyze the<br>
performance of binaries. The proposed changes (which involve both<br>
clang and llvm) optionally introduce an object file section, but this can be<br>
stripped-out if desired.<br>
<br>
For the llvm-mca binary support feature to be useful, a user needs to tell<br>
llvm-mca which portions of their code they would like analyzed. Currently,<br>
this is accomplished via assembly comments. However, assembly comments are not<br>
preserved in object files, and this has encouraged this RFC. For the proposed<br>
binary support, we need to introduce changes to clang and llvm to allow the<br>
user's object code to be recognized by llvm-mca:<br>
<br>
* We need a way for a user to identify a region/block of code they want<br>
   analyzed by llvm-mca.<br>
* We need the information defining the user's region of code to be maintained<br>
   in the object file so that llvm-mca can analyze the desired region(s) from the<br>
   object file.<br>
<br>
We define a "code region" as a subset of a user's program that is to be<br>
analyzed via llvm-mca. The sequence of instructions to be analyzed is<br>
represented as a pair: <start, end> where the 'start' marks the beginning of<br>
the user's source code and 'end' terminates the sequence. The instructions<br>
between 'start' and 'end' form the region that can be analyzed by llvm-mca at a<br>
later time.<br>
<br>
Example<br>
-----------<br>
Before we go into the details of this proposed change, let's first look at a<br>
simple example:<br>
<br>
// example.c -- Analyze a dot-product expression.<br>
double test(double x, double y) {<br>
   double result = 0.0;<br>
   __mca_code_region_start(42);<br>
   result += x * y;<br>
   __mca_code_region_end();<br>
   return result;<br>
}<br>
<br>
In the example above, we have identified a code region, in this case a single<br>
dot-product expression. For the sake of brevity and simplicity, we've chosen<br>
a very simple example, but in reality a more complicated example could use<br>
multiple expressions. We have also denoted this region as number 42. That<br>
identifier is only for the user, and simplifies reading an llvm-mca analysis<br>
report later.<br>
<br>
When this code is compiled, the region markers (the mca_code_region markers)<br>
are transformed into assembly labels. While the markers are presented as<br>
function calls, in reality they are no-ops.<br>
<br>
test:<br>
pushq   %rbp<br>
movq    %rsp, %rbp<br>
movsd   %xmm0, -8(%rbp)<br>
movsd   %xmm1, -16(%rbp)<br>
.Lmca_code_region_start_0: # LLVM-MCA-START ID: 42<br>
xorps   %xmm0, %xmm0<br>
movsd   %xmm0, -24(%rbp)<br>
movsd   -8(%rbp), %xmm0<br>
mulsd   -16(%rbp), %xmm0<br>
addsd   -24(%rbp), %xmm0<br>
movsd   %xmm0, -24(%rbp)<br>
.Lmca_code_region_end_0: # LLVM-MCA-END ID: 42<br>
movsd   -24(%rbp), %xmm0<br>
popq    %rbp<br>
retq<br>
.section        .mca_code_regions,"",@progbits<br>
.quad   42<br>
.quad   .Lmca_code_region_start_0<br>
.quad   .Lmca_code_region_end_0-.Lmca_code_region_start_0<br>
<br>
The assembly has been trimmed to show the portions relevant to this RFC.<br>
Notice the labels enclose the user's defined region, and that they preserve the<br>
user's arbitrary region identifier, the ever-so-important region 42.<br>
<br>
In the object file section .mca_code_regions, we have noted the user's region<br>
identifier (.quad 42), start address, and region size. A more complicated<br>
example can have multiple regions defined within a single .mca_code_regions<br>
section. This section can be read by llvm-mca, allowing llvm-mca to take<br>
object files as input instead of assembly source.<br>
<br>
Details<br>
---------<br>
We need a way for a user to identify a region/block of code they want analyzed<br>
by llvm-mca. We solve this problem by introducing two intrinsics that a user can<br>
specify, for identifying regions of code for analysis.<br>
<br>
The two intrinsics are: llvm.mca.code.regions.start and<br>
llvm.mca.code.regions.end. A user can identify a code region by inserting the<br>
mca_code_region_start and mca_code_region_end markers. These are simply<br>
clang builtins and are transformed into the aforementioned intrinsics during<br>
compilation. The code between the intrinsics are what we call "code regions"<br>
and are to be easily identifiable by llvm-mca; any code between a start/end<br>
pair can be analyzed by llvm-mca at a later time. A user can define multiple<br>
non-overlapping code regions within their program.<br>
<br>
The llvm.mca.code.region.start intrinsic takes an integer constant as its only<br>
argument. This argument is implemented as a metadata i32, and is only used<br>
when generating llvm-mca reports. This value allows a user to more easily<br>
identify a specific code region. llvm.mca.code.region.end takes no arguments.<br>
Since we disallow nesting of regions, the first 'end' intrinsic lexically<br>
following a 'start' intrinsic represents the end of that code region.<br>
<br>
Now that we have a solution for identifying regions for analysis, we now need a<br>
way for preserving that information to be read at a later time. To accomplish<br>
this we propose adding a new section (.mca_code_regions) to the object file<br>
generated by llvm. During code generation, the start/end intrinsics described<br>
above will be transformed into start/end labels in assembly. When llvm<br>
generates the object file from the user's code, these start/end labels form a<br>
pair of values identifying the start of the user's code region, and size. The<br>
size represents the number of bytes between the start and end address of the<br>
labels. Note that the labels are emitted during assembly printing. We hope<br>
that these labels have no influence on code generation or basic-block<br>
placement. However, the target assembler strategy for handling labels is<br>
outside of our control.<br>
<br>
This proposed change affects the size of a binary, but only if the user calls<br>
the start/end builtins mentioned above. The additional size of the<br>
.mca_code_regions section, which we imagine to be very small (to the order of a<br>
few bytes), can trivially be stripped by tools like 'strip' or 'objcopy'.<br>
<br>
Implementation Status<br>
------------------------------<br>
We currently have the proposed changes implemented at the url posted below.<br>
This initial patch only targets ELF object files, and does not handle<br>
relocatable addresses. Since the start of a code region is represented as an<br>
assembly label, and referenced in the .mca_code_regions section, that address<br>
is relocatable. That value can be represented as section-relative relocatable<br>
symbol (.text + addend), but we are not handling that case yet. Instead, the<br>
proposed changes only handle linked/executable object files.<br>
<br>
For purposes of review and to communicate the idea, the change is<br>
presented as a monolithic patch here:<br>
<br>
<a href="https://reviews.llvm.org/D54603" rel="noreferrer" target="_blank">https://reviews.llvm.org/D54603</a><br>
<br>
The change is presented as a monolithic patch; however, if accepted<br>
the patch will be split into three smaller patches:<br>
1. The introduction of the builtins to clang.<br>
2. The llvm portion (the added intrinsics).<br>
3. The llvm-mca portion.<br>
<br>
Thanks!<br>
<br>
-Matt<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div></div></div>