<div dir="ltr"><div>We have a similar issue. We use a distributed compilation system where different TUs may be compiled on different machines and the compilation directory would differ for each TU.</div><div><br></div><div>Our solution is to make the compiler output deterministic regardless of the compilation directory by storing compilation directory separately from filenames, which can be manually overridden, and by using relative paths if those were passed to the compiler. This is similar to debug info handling, see <a href="https://blog.llvm.org/2019/11/deterministic-builds-with-clang-and-lld.html">https://blog.llvm.org/2019/11/deterministic-builds-with-clang-and-lld.html</a>.</div><div><br></div><div>This solution was implemented in <a href="https://reviews.llvm.org/D95753">https://reviews.llvm.org/D95753</a> and I hope to land this within the next day or two. Would this address your problem as well?</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Feb 15, 2021 at 10:25 AM Choongwoo Han 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:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">




<div dir="ltr">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<span style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)">Hi all,</span>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><br>
</div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)">We'd like to contribute something related to -path-equivalence option of llvm-cov. We want to hear feedbacks from you guys.</div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><br>
</div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)">In summary, there are three changes.</div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)">1. Use regular expression to remap paths.</div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)">2. Do not use `sys::fs::equivalent` to find the same file, which causes a huge performance issue in our environment.</div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)">3. Use remapped paths for output directory paths of `llvm-show` instead of original paths.</div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><br>
</div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px">Here is some context why we want this feature. We're building a single binary from multiple machines, so the binary contains different source code paths for
 the same file. </span><span style="margin:0px">There is already `-path-equivalence` option for remapping paths, but it does not meet our requirement because we need n to 1 mapping while the current implementation only supports 1 to 1 mapping. Thus, currently,
 we are using a custom llvm-cov build using a regular expression. The command line looks like this</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px">$ llvm-cov show </span><span style="margin:0px">-path-equivalence='[A-Za-z0-9_/\\:]+[\\/]src[\\/][0-9]+[\\/]([A-Za-z]),:/' ...</span><br>
</div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px">Then, /path/to/src/0/f/asdf becomes f:/asdf.</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px"><br>
</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px">We are using a capture to find a prefix (i.e. captured_string + filepath.replace(regex, to)), but we can drop this part if it looks complicated and not intuitive.</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px"><br>
</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px">Secondly, there is a huge performance degradation in our environment because of file system accesses in llvm-cov.</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px"><a href="https://github.com/llvm/llvm-project/blob/d079dbc591899159925a1fe10b081fa0f6bb61bd/llvm/tools/llvm-cov/CodeCoverage.cpp#L251-L253" rel="noopener noreferrer" style="margin:0px" target="_blank">https://github.com/llvm/llvm-project/blob/d079dbc591899159925a1fe10b081fa0f6bb61bd/llvm/tools/llvm-cov/CodeCoverage.cpp#L251-L253</a><br>
</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px">```</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px">ErrorOr<const MemoryBuffer &><br>
CodeCoverageTool::getSourceFile(StringRef SourceFile) {<br>
</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px">...<br>
  for (const auto &Files : LoadedSourceFiles)
<div style="margin:0px">    if (sys::fs::equivalent(SourceFile, Files.first))</div>
<div style="margin:0px">    <span style="margin:0px">  return *Files.second;</span></div>
</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px">```</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><br>
</div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px">If I rewrite the `sys::fs::equivalent` to a plain string comparison, the performance is improved a lot. When I tested it with a sample binary, the running time
 reduced from 1 minutes to 7 seconds. And, the overall running time of llvm-cov in our Windows environment reduced from 17 hours to 4 hours.</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px">Since we are already using the remapped paths to find the same files, we don't need this kind of strict check to find equivalent files. So, I'd like to replace
 this strict comparison to a simple one when this regex-based remapping is being used.</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px"><br>
</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px">Lastly, the current implementation is using the original paths to generate output files. For example, if `-path-equivalence=/tmp,/output -format=html -output-dir=./outdir`
 is used, the generated file paths will be `./outdir/tmp/...`, not `./outdir/output/...`. It does not make sense because there can be multiple different original paths. Thus, I'd like to use the remapped file paths to generate the output files.</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px"><br>
</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px">
<div style="margin:0px">Since we don't want to break the existing option, I'm thinking about introducing a new flag adding `-regex` at the end (i.e. `-path-equivalence-regex`). I want to ask which approach is best for this case (introducing a new flag or updating
 the existing behavior).</div>
</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px"><br>
</span></div>
<div style="margin:0px;font-size:12pt;background-color:rgb(255,255,255)"><span style="margin:0px">Please let me know if there is any concern from this proposal.<br>
I also uploaded a proof of concept to <a href="https://reviews.llvm.org/D96696" rel="noopener noreferrer" style="margin:0px" target="_blank">https://reviews.llvm.org/D96696</a>
<div style="margin:0px">
</div>
<br>
Thanks,<br>
Choongwoo Han</span></div>
<br>
</div>
</div>

_______________________________________________<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="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div>