<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p.msonormal0, li.msonormal0, div.msonormal0
{mso-style-name:msonormal;
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
span.EmailStyle18
{mso-style-type:personal;
font-family:"Calibri",sans-serif;
color:windowtext;}
span.EmailStyle20
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">Thanks. I’ll take a look at the examples and give it a try for creating a patch.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Chris<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><a name="_____replyseparator"></a><b>From:</b> Shoaib Meenai <smeenai@fb.com>
<br>
<b>Sent:</b> Tuesday, January 14, 2020 2:49 PM<br>
<b>To:</b> Reid Kleckner <rnk@google.com>; Chrulski, Christopher M <christopher.m.chrulski@intel.com><br>
<b>Cc:</b> llvm-dev@lists.llvm.org<br>
<b>Subject:</b> Re: [llvm-dev] Incorrect code generation when using -fprofile-generate on code which contains exception handling (Windows target)<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><a href="https://reviews.llvm.org/D44641">https://reviews.llvm.org/D44641</a> is an example of using colorEHFunclets this way (and
<a href="https://reviews.llvm.org/D45857">https://reviews.llvm.org/D45857</a> is a follow-up which demonstrates another usage). If I remember correctly, the analysis needs to be updated if the pass is updating the CFG, which those ARC passes weren’t doing.
I’m not sure what that updating looks like, though I’m sure we have an example of it somewhere in-tree … Reid might know off the top of his head.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:12.0pt;color:black">From: </span></b><span style="font-size:12.0pt;color:black">Reid Kleckner <<a href="mailto:rnk@google.com">rnk@google.com</a>><br>
<b>Date: </b>Tuesday, January 14, 2020 at 12:45 PM<br>
<b>To: </b>"Chrulski, Christopher M" <<a href="mailto:christopher.m.chrulski@intel.com">christopher.m.chrulski@intel.com</a>><br>
<b>Cc: </b>Shoaib Meenai <<a href="mailto:smeenai@fb.com">smeenai@fb.com</a>>, "<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>" <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>><br>
<b>Subject: </b>Re: [llvm-dev] Incorrect code generation when using -fprofile-generate on code which contains exception handling (Windows target)<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I think the simplest, most complete, short term fix, would be to call llvm::colorEHFunclets, and to have the relevant instrumentation passes apply the appropriate funclet bundle when inserting function calls. It's not elegant because it
means every simple instrumentation pass that inserts regular function calls (ASan, TSan, MSan, instrprof, etc) needs to be funclet-aware. But, it will work, and the code isn't so bad.<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Tue, Jan 14, 2020 at 10:59 AM Chrulski, Christopher M <<a href="mailto:christopher.m.chrulski@intel.com">christopher.m.chrulski@intel.com</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<p class="MsoNormal" style="margin-bottom:12.0pt">Thanks for the link. I agree, looks like it's another instance of the same class of problems. Since the compiler still requires funclet operand bundles, it sounds like one of the 3 options below will be required
until a longer term project of eliminating them is available. Anybody have a strong preference?<br>
<br>
Chris<br>
<br>
<br>
-----Original Message-----<br>
From: Shoaib Meenai <<a href="mailto:smeenai@fb.com" target="_blank">smeenai@fb.com</a>>
<br>
Sent: Monday, January 13, 2020 1:10 AM<br>
To: Chrulski, Christopher M <<a href="mailto:christopher.m.chrulski@intel.com" target="_blank">christopher.m.chrulski@intel.com</a>>;
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
Cc: Reid Kleckner <<a href="mailto:rnk@google.com" target="_blank">rnk@google.com</a>><br>
Subject: Re: [llvm-dev] Incorrect code generation when using -fprofile-generate on code which contains exception handling (Windows target)<br>
<br>
I think this is the same underlying issue as <a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__bugs.llvm.org_show-5Fbug.cgi-3Fid-3D40320&d=DwMFaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=o3kDXzdBUE3ljQXKeTWOMw&m=2kJvP66PH1_0V_6F0MmFxRneQh7gCOtMlcJ05vyhwho&s=IQa4LvJ69syknBZI4oGapryXiISJwyjSHUW8YjtIJfo&e=" target="_blank">
https://bugs.llvm.org/show_bug.cgi?id=40320</a>. CCing Reid, who's had a bunch of thoughts on this in the past.<br>
<br>
On 1/11/20, 10:25 AM, "llvm-dev on behalf of Chrulski, Christopher M via llvm-dev" <<a href="mailto:llvm-dev-bounces@lists.llvm.org" target="_blank">llvm-dev-bounces@lists.llvm.org</a> on behalf of
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
<br>
Hi,<br>
<br>
I've run into a bug with the LLVM backend that causes incorrect code generation to happen when using -fprofile-generate on programs that contain C++ exception handling when building for Windows.<br>
<br>
The problem occurs when the value profiling inserts function calls into exception handling blocks. The instrumentation inserts value profiling intrinsic calls, and these are subsequently lowered into target library calls. However, these library calls do
not get a funclet operand bundle associated with them. This causes the Windows Exception Handling Preparation Pass to drop all the instructions within the exception handler starting from the PGO instrumentation call, and replace them with 'unreachable'. This
is being done by the function removeImplausibleInstructions (WinEHPrepare.cpp).<br>
<br>
A simple reproducer of the problem shown here which will lead to incorrect code on the method test::run(). In this example, the virtual function called from within the exception handler triggers the bug when using -fprofile-generate.<br>
<br>
#include <stdexcept><br>
#include <iostream><br>
<br>
extern void may_throw(int);<br>
<br>
class base {<br>
public:<br>
base() : x(0) {};<br>
int get_x() const { return x; }<br>
virtual void update() { x++; }<br>
int x;<br>
};<br>
<br>
class derived : public base {<br>
public:<br>
derived() {}<br>
virtual void update() { x--; }<br>
};<br>
<br>
class test {<br>
public:<br>
void run(base* b, int count) {<br>
try {<br>
for (int i = 0; i < count; ++i)<br>
may_throw(i);<br>
}<br>
catch (std::exception& e) {<br>
// Virtual function call in exception handler for value profiling.<br>
b->update();<br>
}<br>
}<br>
};<br>
<br>
void run_test() {<br>
test tester;<br>
base *obj = new derived;<br>
tester.run(obj, 100);<br>
std::cout << "Value in obj (should be -1): " << obj->get_x() << "\n";<br>
if (obj->get_x() == -1)<br>
std::cout << "test passed\n";<br>
else<br>
std::cout << "test failed\n";<br>
}<br>
<br>
int main() {<br>
// Without PGO, test runs and prints result.<br>
// With -fprofile-generate, program seg-faults without printing.<br>
run_test();<br>
return 0;<br>
}<br>
<br>
__attribute__((noinline))<br>
void may_throw(int x) {<br>
if (x > 10)<br>
throw std::range_error("value out of range");<br>
}<br>
<br>
<br>
On Windows, build with: clang -O2 -fprofile-generate test.cpp<br>
<br>
When profiling is enabled the program will seg fault without printing anything. Without the -fprofile-generate flag, the program will run successfully.<br>
<br>
The compiler problem is as follows: Prior to the Windows Exception Handling Preparation Pass, the IR for the function "test::run" contains the following:<br>
<br>
19: ; preds = %17<br>
%20 = catchpad within %18 [%rtti.TypeDescriptor19* @"??_R0?AVexception@std@@@8", i32 8, %"class.std::exception"** %6]<br>
%21 = load i64, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@test@@QEAAXPEAVbase@@H@Z", i64 0, i64 2), align 8<br>
%22 = add i64 %21, 1<br>
store i64 %22, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@test@@QEAAXPEAVbase@@H@Z", i64 0, i64 2), align 8<br>
%23 = bitcast %class.base* %1 to void (%class.base*)***<br>
%24 = load void (%class.base*)**, void (%class.base*)*** %23, align 8, !tbaa !9<br>
%25 = load void (%class.base*)*, void (%class.base*)** %24, align 8<br>
%26 = ptrtoint void (%class.base*)* %25 to i64<br>
call void @__llvm_profile_instrument_target(i64 %26, i8* bitcast ({ i64, i64, i64*, i8*, i8*, i32, [2 x i16] }* @"__profd_?run@test@@QEAAXPEAVbase@@H@Z" to i8*), i32 0)<br>
call void %25(%class.base* %1) [ "funclet"(token %20) ]<br>
call void @_CxxThrowException(i8* null, %eh.ThrowInfo* null) #15 [ "funclet"(token %20) ]<br>
unreachable<br>
<br>
Following this pass, this IR has been replaced with the following, causing a breakage to the original program. This is occurring because the instrumentation function call, "__llvm_profile_instrument_target", is not marked with the funclet operand bundle
[ "funclet"(token %20) ].<br>
<br>
19: ; preds = %17<br>
%20 = catchpad within %18 [%rtti.TypeDescriptor19* @"??_R0?AVexception@std@@@8", i32 8, %"class.std::exception"** %6]<br>
%21 = load i64, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@test@@QEAAXPEAVbase@@H@Z", i64 0, i64 2), align 8<br>
%22 = add i64 %21, 1<br>
store i64 %22, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@test@@QEAAXPEAVbase@@H@Z", i64 0, i64 2), align 8<br>
unreachable<br>
<br>
<br>
Possible solutions:<br>
1) Avoid value profiling of calls within exception handling blocks<br>
Pros: Solves the problem<br>
Cons: Could lose some cases of value profiling, but since the exception code is not supposed to be the primary execution path, this should not be a significant performance issue.<br>
<br>
2) Propagate the funclet information onto the value profiling intrinsics created. And then also propagate this info to the library routines these intrinsics get lowered into.
<br>
For indirect function calls, the funclet information can be copied from the original function call.
<br>
However, for MemIntrinsic call operand value profiling, these do not have funclet operand bundles attached to them by the front-end. (Not sure if it's possible to do because the interfaces that are used to create these do not take operand bundles)
Therefore, PGO would need to determine the appropriate funclet value with colorEHFunlets to identify the funclet operand bundle to attach to the instrumentation calls. Unfortunately, because it is possible that a basic block could be associated with multiple
funclets or both a funclet and outside the funclet, this may also need to clone some of basic blocks similar to the WinEHPrepare.cpp routine cloneCommonBlocks(), prior to computing the instrumentation.<br>
<br>
Pros: does not disable value profiling opportunities.<br>
Cons: complex to implement due to the need to determine the appropriate funclet to place on the memory operand value profiling calls. This would necessitate the same cloning behavior to be done for the PGO use compilation.<br>
<br>
3) Teach the Windows Exception Preparation Pass about the value profiling library functions. Currently this pass will ignore llvm intrinsic functions that are marked with the 'does not throw' attribute, but the value profiling intrinsic calls have been
lowered from being intrinsic calls into runtime library target specific functions before reaching this point.<br>
<br>
Pros: does not disable value profiling opportunities<br>
Cons: requires exposing function names from InstrProf.h to the WinEHPrepare.cpp file, or requires a new attribute on the function calls to identify them as instrumentation library calls. Also, the IR does not correctly reflect the correct state regarding
the operand bundle funclet information for the PGO inserted function calls.<br>
<br>
For options 2 or 3 to work, it also requires that the PGO indirect function call promotion pass used for -fprofile-use to maintain the 'funclet' operand bundle on the specialized function call that is inserted as a direct function call target. Fortunately,
the code within that pass is cloning the original indirect call, so the 'funclet' operand bundle is being maintained on it.<br>
<br>
Any thoughts on which of these options should be taken, or other suggestions for resolving this problem?<br>
<br>
<br>
Chris<br>
<br>
<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="https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.llvm.org_cgi-2Dbin_mailman_listinfo_llvm-2Ddev&d=DwIGaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=o3kDXzdBUE3ljQXKeTWOMw&m=IHHZpUcEfZDvndGY8yafot6m6x5pu5ytT0d-lHiPtj4&s=G_7nV5L14eMSueKjWv77SYDeHf7Jzl5s0TrkM7l3YRM&e=" target="_blank">
https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.llvm.org_cgi-2Dbin_mailman_listinfo_llvm-2Ddev&d=DwIGaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=o3kDXzdBUE3ljQXKeTWOMw&m=IHHZpUcEfZDvndGY8yafot6m6x5pu5ytT0d-lHiPtj4&s=G_7nV5L14eMSueKjWv77SYDeHf7Jzl5s0TrkM7l3YRM&e=</a>
<o:p></o:p></p>
</blockquote>
</div>
</div>
</body>
</html>