RFR: 8267532: Try/catch block not optimized as expected [v3]

Tobias Hartmann thartmann at openjdk.org
Mon Nov 6 08:55:18 UTC 2023


On Thu, 2 Nov 2023 16:27:35 GMT, Jorn Vernee <jvernee at openjdk.org> wrote:

>> The issue is essentially that for the Java try-with-resource construct, javac generates multiple calls to `close(`) the resource. One of those calls is inside the hidden exception handler of the try block. The issue for us is that typically the exception handler is never entered (since no exception is thrown), however we don't profile exception handlers at the moment, so the block is not pruned. C2 doesn't inline the `close()` call in the handler due to low call site frequency. As a result, the receiver of that call escapes and can not be scalar replaced, which then leads to a loss in performance.
>> 
>> There has been some discussion on the JBS issue that this could be fixed by profiling catch blocks. And another suggestion that partial escape analysis could help here to prevent the object from escaping. But, I think there are other benefits to being able to prune dead catch blocks, such as general reduction in code size, and other optimizations being possible by dead code being eliminated. So, I've implemented catch block profiling + pruning in this patch.
>> 
>> The implementation is essentially very straightforward: we allocate an extra bit of profiling data for each
>> exception handler of a method in the `MethodData` for that method (which holds all the profiling
>> data). Then when looking up the exception handler after an exception is thrown, we mark the
>> exception handler as entered. When C2 parses the exception handler block, and it sees that it has
>> never been entered, we emit an uncommon trap instead.
>> 
>> I've also cleaned up the handling of profiling data sections a bit. After adding the extra section of data to MethodData, I was seeing several crashes when ciMethodData was used. The underlying issue seemed to be that the offset of the parameter data was computed based on the total data size - parameter data size (which doesn't work if we add an additional section for exception handler data). I've re-written the code around this a bit to try and prevent issues in the future. Both MethodData and ciMethodData now track offsets of parameter data and exception handler data, and the size of the each data section is derived from the offsets.
>> 
>> Finally, there was an assert firing in `freeze_internal` in `continuationFreezeThaw.cpp`:
>> 
>>     assert(monitors_on_stack(current) == ((current->held_monitor_count() - current->jni_monitor_count()) > 0),
>>          "Held monitor count and locks on stack invariant: " INT64_FORMAT " JNI: " INT64_FORMAT, (int...
>
> Jorn Vernee has updated the pull request incrementally with one additional commit since the last revision:
> 
>   add some assertion to IR test that check for compilation and deoptimization

Great work, Jorn! The changes look good to me.

`-XX:+StressPrunedExceptionHandlers` and probably also `-XX:-ProfileExceptionHandlers` should be added to our testing, at least to [JDK-8295559](https://bugs.openjdk.org/browse/JDK-8295559) (internal).

src/hotspot/share/ci/ciMethodData.hpp line 525:

> 523: 
> 524:   // pointers to sections in _data
> 525:   // NOTE: these may be called before ciMethodData::load_data (is that a bug?).

I don't think we should add a "is that a bug?" comment here but either investigate right away or file a follow-up RFE to keep track of that. I don't think it's a bug though.

test/hotspot/jtreg/compiler/c2/TestExHandlerTrap.java line 37:

> 35:  *   -Xbatch
> 36:  *   -Xlog:deoptimization=trace
> 37:  *   -XX:CompileCommand=PrintCompilation,compiler.c2.TestExHandlerTrap::payload

Should the logging/printing be removed?

test/hotspot/jtreg/compiler/lib/ir_framework/IRNode.java line 1536:

> 1534:     public static final String UNREACHED_TRAP = PREFIX + "UNREACHED_TRAP" + POSTFIX;
> 1535:     static {
> 1536:         trapNodes(UNREACHED_TRAP,"unreached");

Suggestion:

        trapNodes(UNREACHED_TRAP, "unreached");

-------------

Marked as reviewed by thartmann (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/16416#pullrequestreview-1714480033
PR Review Comment: https://git.openjdk.org/jdk/pull/16416#discussion_r1382910906
PR Review Comment: https://git.openjdk.org/jdk/pull/16416#discussion_r1382943362
PR Review Comment: https://git.openjdk.org/jdk/pull/16416#discussion_r1382945696


More information about the hotspot-dev mailing list