Skip to content

Consider guards or instanceof in field privatizer - #6187

Merged
0xdaryl merged 3 commits into
eclipse-omr:masterfrom
hzongaro:consider-guards-in-field-privatizer
Oct 8, 2021
Merged

0xdaryl merged 3 commits into
eclipse-omr:masterfrom
hzongaro:consider-guards-in-field-privatizer

Conversation

@hzongaro

Copy link
Copy Markdown
Contributor

A reference or store to a field that came from an inlined method might be guarded by a virtual guard. In any particular execution of the loop, such a field might not actually exist in the object that's being referenced. To avoid that situation, this change adds a simple check for the existence of a virtual guard within the loop and considers it to be an escape point in the containsEscapePoints method.

Similarly, instanceof tests might guard casts that will be used to access fields that wouldn't necessarily exist every time a loop is executed, so they are considered escape points as well.

This pull request fixes OpenJ9 issue #13524

@vijaysun-omr

Copy link
Copy Markdown
Contributor

jenkins build all

@vijaysun-omr

Copy link
Copy Markdown
Contributor

I would appreciate a review from either @0xdaryl or @jdmpapin

@vijaysun-omr

Copy link
Copy Markdown
Contributor

Jenkins build riscv

Comment thread compiler/optimizer/FieldPrivatizer.cpp Resolved
Comment thread compiler/optimizer/FieldPrivatizer.hpp Resolved
@jdmpapin

Copy link
Copy Markdown
Contributor

At a high level I think that this fix is too narrow to fully address the root problem

Consider a loop that accesses the instance field Foo.f relative to an invariant object obj. We'd like to privatize those accesses. However, if there is any way to enter the loop without later running the access to Foo.f and without running any other accesses to obj as an instance of Foo, then we have no guarantee in general that obj is an instance of Foo on loop entry regardless of the condition or conditions that the Foo accesses depend on. This is because a runtime test can provide a guarantee that the compiler has lost track of because it only keeps a record of the fact that we have a guarantee (the access to Foo.f), and not any record of why we have that guarantee. In effect we make use of undefined behaviour as in C: at an access to Foo.f relative to obj, the compiler simply assumes that obj is non-null and an instance of Foo, so that had better be true at runtime

As a simple example, we could start with an instanceof test:

for (...) {
    if (obj instanceof Foo) {
        Foo foo = (Foo)obj;
        ... = foo.f;
        ...
        foo.f = ...;
    }
    ...
}

eliminate the cast because instanceof guarantees that it will succeed:

for (...) {
    if (obj instanceof Foo) {
        Foo foo = obj; // no runtime cast
        ... = foo.f;
        ...
        foo.f = ...;
    }
    ...
}

and finally hoist instanceof out of the loop:

boolean isFoo = obj instanceof Foo;
for (...) {
    if (isFoo) {
        Foo foo = obj; // no runtime cast
        ... = foo.f;
        ...
        foo.f = ...;
    }
    ...
}

If we think that only guards and instanceof nodes within the loop can be relevant to the type of obj at runtime, then we could incorrectly privatize Foo.f in this loop

And this example isn't to say that we should look for conditionals based on an instanceof outside of the loop as well. That would simply set a trap that will go off if the compiler ever develops certain kinds of smarts in the future. (I can give an example of what I mean if necessary)

So I think that to be robust/future proof (without disabling the optimization), we have to do one or more of the following:

  1. Try to prove that obj is an instance of Foo by proving that upon loop entry, the loop is guaranteed to access obj as an instance of Foo
  2. Try to prove that obj is an instance of Foo by looking back at its definitions, or possibly at other runtime tests that control entry into the loop
  3. If we have failed to prove that obj is an instance of Foo but we still think it's likely, generate a new runtime instanceof test outside of the loop to conditionally load f, and another (possibly using the saved result of the first) to conditionally store back

@0xdaryl

0xdaryl commented Oct 7, 2021

Copy link
Copy Markdown
Contributor

@jdmpapin : are you ok with delivering this PR as a tactical fix and the remaining issues you uncovered worked on afterward (assuming an issue is opened to track that work)? I believe some of these problems have existed for many years.

@vijaysun-omr vijaysun-omr assigned 0xdaryl and unassigned vijaysun-omr Oct 7, 2021
@vijaysun-omr

Copy link
Copy Markdown
Contributor

I am reassigning this to @0xdaryl since I will be away for a few days next week.

@jdmpapin

jdmpapin commented Oct 7, 2021

Copy link
Copy Markdown
Contributor

are you ok with delivering this PR as a tactical fix and the remaining issues you uncovered worked on afterward (assuming an issue is opened to track that work)? I believe some of these problems have existed for many years.

Yes, that's alright (with the further check for null tests, since I believe that @hzongaro has written that already)

@hzongaro

hzongaro commented Oct 7, 2021

Copy link
Copy Markdown
Contributor Author

Yes, that's alright (with the further check for null tests, since I believe that @hzongaro has written that already)

Yes - sorry about the delay. I'll just clean up the commit comments and update the pull request shortly.

@hzongaro
hzongaro marked this pull request as draft October 7, 2021 17:06
@hzongaro

hzongaro commented Oct 7, 2021

Copy link
Copy Markdown
Contributor Author

Moving this back to draft temporarily while I rerun testing with the latest changes.

@hzongaro

hzongaro commented Oct 7, 2021

Copy link
Copy Markdown
Contributor Author

Devin @jdmpapin, thanks again for your thoughtful review. Commit 9464bb0 papers over the concern you had about instanceof being pulled out of the loop. Commit b6800ef handles comparisons with null, about which you had expressed concern (complete with a failing test case) off-line, in the same way instanceof is handled.

As you suggested, these changes try to avoid the problematic situations. A robust, complete fix that accurately detects the problematic situations and that either avoids privatizing or uses conditional tests for the copy of the privatized field into and out of a temporary is required. This follow on work will be tracked in issue #6199.

@jdmpapin jdmpapin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - just a couple of minor notes on comments in the code

Comment thread compiler/optimizer/FieldPrivatizer.cpp Resolved
Comment thread compiler/optimizer/FieldPrivatizer.hpp Resolved
@0xdaryl

0xdaryl commented Oct 7, 2021

Copy link
Copy Markdown
Contributor

Jenkins build all

@0xdaryl

0xdaryl commented Oct 7, 2021

Copy link
Copy Markdown
Contributor

Jenkins build riscv

Comment thread compiler/optimizer/FieldPrivatizer.cpp Resolved

@0xdaryl 0xdaryl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good to me. I won't re-run CI when that minor comment change to add the issue number is added.

@hzongaro : you can move this out of Draft state when you're ready.

A reference or store to a field that came from an inlined method might
be guarded by a virtual guard.  In any particular execution of the loop,
such a field might not actually exist in the object that's being
referenced.  To avoid that situation, this change adds a simple check
for the existence of a virtual guard within the loop and considers it
to be an escape point in the containsEscapePoints method.

Similarly, instanceof tests might guard casts that will be used to
access fields that wouldn't necessarily exist every time a loop is
executed, so they are considered escape points as well.

Signed-off-by:  Henry Zongaro <zongaro@ca.ibm.com>
instanceof operations can be hoisted out of loops by PRE.  This could
expose a bug in Field Privatizer in which a field access that is guarded
might be privatized, resulting in the field being referenced and stored
to unconditionally.

A temporary patch in Field Privatizer checks for uses of instanceof and
null comparisons inside of loops to guard against the conditions that
expose the bug.  This change removes the SupportedForPRE property from
the instanceof operator to prevent its being hoisted out a loop,
avoiding one more situation that could expose the bug.

Once the bug is fixed properly in Field Privatizer, the SupportedForPRE
property will be added back to instanceof.

Signed-off-by:  Henry Zongaro <zongaro@ca.ibm.com>
A comparison with a null reference might be used to guard a field
access.  The Field Privatizer does not take such conditional access into
account in deciding whether to privatize a field, which can result in
its unconditionally copying a field's value into a temporary variable by
dereferencing null.

This change adds a very simple check for comparisons with null inside of
a loop to help to guard against that situation.  That will avoid the bug
in most cases, but follow up changes that are more robust, general and
accurate in avoiding the problem are planned.

Signed-off-by:  Henry Zongaro <zongaro@ca.ibm.com>
@hzongaro
hzongaro force-pushed the consider-guards-in-field-privatizer branch from f8abb9f to f9c32ab Compare October 8, 2021 14:01
@hzongaro
hzongaro marked this pull request as ready for review October 8, 2021 14:01
@hzongaro
hzongaro requested a review from Leonardo2718 as a code owner October 8, 2021 14:01
@hzongaro

hzongaro commented Oct 8, 2021

Copy link
Copy Markdown
Contributor Author

you can move this out of Draft state when you're ready

Thanks, Daryl @0xdaryl. I squashed the commits related to whitespace changes and corrections to comments. Otherwise, the latest version is the same as the version that was reviewed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

jdk17 java/util/concurrent/tck/JSR166TestCase.java crash in Compiled_method vmState=0x00000000

4 participants