<div dir="ltr">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.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">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:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">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://bugs.llvm.org/show_bug.cgi?id=40320" rel="noreferrer" 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=" rel="noreferrer" 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> <br>
<br>
<br>
</blockquote></div>