Skip to content

Commit

Permalink
[GR-51558] Remove OpaqueValueNodes at the end of low tier
Browse files Browse the repository at this point in the history
PullRequest: graal/17033
  • Loading branch information
c-refice committed Mar 4, 2024
2 parents a0e00f1 + 134cde7 commit 418f219
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,20 @@ public void testNegWrite() {
test("writeNegSnippet");
assertTrue(arr[0] == (byte) -300);
}

public static void opaqueClassSnippet() {
/*
* GR-51558: This would cause an assertion failure in LIR constant load optimization if the
* opaque is not removed.
*/
Class<?> c = GraalDirectives.opaque(Object.class);
if (c.getResource("resource.txt") == null) {
GraalDirectives.deoptimize();
}
}

@Test
public void testOpaqueClass() {
test("opaqueClassSnippet");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import jdk.graal.compiler.phases.common.CanonicalizerPhase;
import jdk.graal.compiler.phases.common.ExpandLogicPhase;
import jdk.graal.compiler.phases.common.LowTierLoweringPhase;
import jdk.graal.compiler.phases.common.RemoveOpaqueValuePhase;
import jdk.graal.compiler.phases.schedule.SchedulePhase;
import jdk.graal.compiler.phases.tiers.LowTierContext;

Expand All @@ -52,6 +53,7 @@ public EconomyLowTier() {
* backend or the target specific suites provider.
*/
appendPhase(new PlaceholderPhase<>(AddressLoweringPhase.class));
appendPhase(new RemoveOpaqueValuePhase());
appendPhase(new SchedulePhase(SchedulePhase.SchedulingStrategy.LATEST_OUT_OF_LOOPS));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import jdk.graal.compiler.phases.common.OptimizeExtendsPhase;
import jdk.graal.compiler.phases.common.ProfileCompiledMethodsPhase;
import jdk.graal.compiler.phases.common.PropagateDeoptimizeProbabilityPhase;
import jdk.graal.compiler.phases.common.RemoveOpaqueValuePhase;
import jdk.graal.compiler.phases.schedule.SchedulePhase;
import jdk.graal.compiler.phases.schedule.SchedulePhase.SchedulingStrategy;
import jdk.graal.compiler.phases.tiers.LowTierContext;
Expand Down Expand Up @@ -93,6 +94,8 @@ public LowTier(OptionValues options) {

appendPhase(new OptimizeExtendsPhase());

appendPhase(new RemoveOpaqueValuePhase());

appendPhase(new SchedulePhase(SchedulePhase.SchedulingStrategy.LATEST_OUT_OF_LOOPS));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,32 @@
*/
package jdk.graal.compiler.nodes.extended;

import static jdk.graal.compiler.nodeinfo.NodeCycles.CYCLES_0;
import static jdk.graal.compiler.nodeinfo.NodeSize.SIZE_0;

import jdk.graal.compiler.graph.IterableNodeType;
import jdk.graal.compiler.graph.NodeClass;
import jdk.graal.compiler.graph.spi.NodeWithIdentity;
import jdk.graal.compiler.nodeinfo.InputType;
import jdk.graal.compiler.nodeinfo.NodeInfo;
import jdk.graal.compiler.nodes.NodeView;
import jdk.graal.compiler.nodes.ValueNode;
import jdk.graal.compiler.nodes.spi.LIRLowerable;
import jdk.graal.compiler.nodes.spi.NodeLIRBuilderTool;

import static jdk.graal.compiler.nodeinfo.NodeCycles.CYCLES_0;
import static jdk.graal.compiler.nodeinfo.NodeSize.SIZE_0;
import jdk.graal.compiler.phases.common.RemoveOpaqueValuePhase;

/**
* This node type acts as an optimization barrier between its input node and its usages. For
* example, a MulNode with two ConstantNodes as input will be canonicalized to a ConstantNode. This
* optimization will be prevented if either of the two constants is wrapped by an OpaqueValueNode.
* <p>
* </p>
* This node is not {@link LIRLowerable}, so it should be removed from the graph before LIR
* generation.
*
* @see RemoveOpaqueValuePhase
*/
@NodeInfo(cycles = CYCLES_0, size = SIZE_0)
public final class OpaqueValueNode extends OpaqueNode implements NodeWithIdentity, LIRLowerable, GuardingNode {
public final class OpaqueValueNode extends OpaqueNode implements NodeWithIdentity, GuardingNode, IterableNodeType {
public static final NodeClass<OpaqueValueNode> TYPE = NodeClass.create(OpaqueValueNode.class);

@Input(InputType.Value) private ValueNode value;
Expand All @@ -57,9 +69,4 @@ public void setValue(ValueNode value) {
this.updateUsages(this.value, value);
this.value = value;
}

@Override
public void generate(NodeLIRBuilderTool gen) {
gen.setResult(this, gen.operand(getValue()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.graal.compiler.phases.common;

import java.util.Optional;

import jdk.graal.compiler.nodes.GraphState;
import jdk.graal.compiler.nodes.StructuredGraph;
import jdk.graal.compiler.nodes.extended.OpaqueValueNode;
import jdk.graal.compiler.nodes.spi.CoreProviders;
import jdk.graal.compiler.phases.BasePhase;

/**
* Removes all {@link jdk.graal.compiler.nodes.extended.OpaqueValueNode}s from the graph.
*/
public class RemoveOpaqueValuePhase extends BasePhase<CoreProviders> {
@Override
public Optional<NotApplicable> notApplicableTo(GraphState graphState) {
return ALWAYS_APPLICABLE;
}

@Override
public boolean shouldApply(StructuredGraph graph) {
return graph.hasNode(OpaqueValueNode.TYPE);
}

@Override
protected void run(StructuredGraph graph, CoreProviders context) {
for (OpaqueValueNode opaque : graph.getNodes(OpaqueValueNode.TYPE)) {
opaque.replaceAtUsagesAndDelete(opaque.getValue());
}
}
}

0 comments on commit 418f219

Please sign in to comment.