-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #462 from mairooni/feat/pointer_copies
Support conditional copy-by-reference in local memory
- Loading branch information
Showing
20 changed files
with
997 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...src/main/java/uk/ac/manchester/tornado/drivers/opencl/graal/nodes/FixedArrayCopyNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) 2024, APT Group, Department of Computer Science, | ||
* School of Engineering, The University of Manchester. All rights reserved. | ||
* Copyright (c) 2009, 2017, 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. | ||
* | ||
* 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. | ||
* | ||
*/ | ||
package uk.ac.manchester.tornado.drivers.opencl.graal.nodes; | ||
|
||
import jdk.vm.ci.meta.JavaKind; | ||
import jdk.vm.ci.meta.ResolvedJavaType; | ||
import jdk.vm.ci.meta.Value; | ||
import org.graalvm.compiler.core.common.LIRKind; | ||
import org.graalvm.compiler.core.common.type.StampFactory; | ||
import org.graalvm.compiler.graph.NodeClass; | ||
import org.graalvm.compiler.lir.Variable; | ||
import org.graalvm.compiler.nodeinfo.NodeInfo; | ||
import org.graalvm.compiler.nodes.ValuePhiNode; | ||
import org.graalvm.compiler.nodes.calc.FloatingNode; | ||
import org.graalvm.compiler.nodes.spi.LIRLowerable; | ||
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool; | ||
|
||
import uk.ac.manchester.tornado.drivers.opencl.graal.OCLArchitecture; | ||
import uk.ac.manchester.tornado.drivers.opencl.graal.asm.OCLAssembler; | ||
import uk.ac.manchester.tornado.drivers.opencl.graal.lir.OCLBinary; | ||
import uk.ac.manchester.tornado.drivers.opencl.graal.lir.OCLLIRStmt; | ||
import uk.ac.manchester.tornado.drivers.opencl.graal.lir.OCLKind; | ||
|
||
/** | ||
* This node generates a pointer copy between two arrays in private memory. | ||
*/ | ||
@NodeInfo | ||
public class FixedArrayCopyNode extends FloatingNode implements LIRLowerable { | ||
|
||
public static final NodeClass<FixedArrayCopyNode> TYPE = NodeClass.create(FixedArrayCopyNode.class); | ||
|
||
@Input | ||
protected ValuePhiNode conditionalPhiNode; | ||
|
||
protected ResolvedJavaType elementType; | ||
protected OCLAssembler.OCLBinaryTemplate pointerCopyTemplate; | ||
protected OCLArchitecture.OCLMemoryBase memoryRegister; | ||
|
||
public FixedArrayCopyNode(ValuePhiNode conditionalPhiNode, ResolvedJavaType elementType, OCLArchitecture.OCLMemoryBase memoryRegister) { | ||
super(TYPE, StampFactory.forKind(JavaKind.Object)); | ||
this.conditionalPhiNode = conditionalPhiNode; | ||
this.elementType = elementType; | ||
this.memoryRegister = memoryRegister; | ||
this.pointerCopyTemplate = OCLKind.resolvePrivatePointerCopyTemplate(elementType); | ||
} | ||
|
||
public OCLArchitecture.OCLMemoryBase getMemoryRegister() { | ||
return memoryRegister; | ||
} | ||
|
||
@Override | ||
public void generate(NodeLIRBuilderTool gen) { | ||
LIRKind lirKind = LIRKind.value(gen.getLIRGeneratorTool().target().arch.getWordKind()); | ||
final Variable ptr = gen.getLIRGeneratorTool().newVariable(lirKind); | ||
Value fixedArrayValue = gen.operand(conditionalPhiNode); | ||
final OCLBinary.Expr declarationPtr = new OCLBinary.Expr(pointerCopyTemplate, lirKind, ptr, fixedArrayValue); | ||
final OCLLIRStmt.ExprStmt ptrExpr = new OCLLIRStmt.ExprStmt(declarationPtr); | ||
gen.getLIRGeneratorTool().append(ptrExpr); | ||
gen.setResult(this, ptr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...java/uk/ac/manchester/tornado/drivers/opencl/graal/phases/TornadoFixedArrayCopyPhase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (c) 2024, APT Group, Department of Computer Science, | ||
* The University of Manchester. All rights reserved. | ||
* Copyright (c) 2009, 2017, 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. | ||
* | ||
* 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. | ||
* | ||
*/ | ||
package uk.ac.manchester.tornado.drivers.opencl.graal.phases; | ||
|
||
import jdk.vm.ci.meta.ResolvedJavaType; | ||
import org.graalvm.compiler.graph.Node; | ||
import org.graalvm.compiler.nodes.GraphState; | ||
import org.graalvm.compiler.nodes.StructuredGraph; | ||
import org.graalvm.compiler.nodes.ValuePhiNode; | ||
import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode; | ||
import org.graalvm.compiler.phases.Phase; | ||
|
||
import uk.ac.manchester.tornado.api.exceptions.TornadoCompilationException; | ||
import uk.ac.manchester.tornado.drivers.opencl.graal.OCLArchitecture; | ||
import uk.ac.manchester.tornado.drivers.opencl.graal.nodes.FixedArrayCopyNode; | ||
import uk.ac.manchester.tornado.drivers.opencl.graal.nodes.FixedArrayNode; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* This phase examines if a copy takes place between two arrays in private memory based on | ||
* an if condition and, if so, inserts a {@link FixedArrayCopyNode} to generate an update in the references. | ||
*/ | ||
public class TornadoFixedArrayCopyPhase extends Phase { | ||
|
||
@Override | ||
public Optional<NotApplicable> notApplicableTo(GraphState graphState) { | ||
return ALWAYS_APPLICABLE; | ||
} | ||
|
||
protected void run(StructuredGraph graph) { | ||
for (ValuePhiNode phiNode : graph.getNodes().filter(ValuePhiNode.class)) { | ||
if (isFixedArrayCopied(phiNode)) { | ||
FixedArrayNode fixedArrayNode = phiNode.values().filter(FixedArrayNode.class).first(); | ||
ResolvedJavaType resolvedJavaType = fixedArrayNode.getElementType(); | ||
OCLArchitecture.OCLMemoryBase oclMemoryBase = fixedArrayNode.getMemoryRegister(); | ||
OffsetAddressNode offsetAddressNode = phiNode.usages().filter(OffsetAddressNode.class).first(); | ||
FixedArrayCopyNode fixedArrayCopyNode = new FixedArrayCopyNode(phiNode, resolvedJavaType, oclMemoryBase); | ||
graph.addWithoutUnique(fixedArrayCopyNode); | ||
offsetAddressNode.replaceFirstInput(phiNode, fixedArrayCopyNode); | ||
// finally, since we know that the data accessed is a fixed array, fix the offset | ||
ValuePhiNode privateIndex = getPrivateArrayIndex(offsetAddressNode.getOffset()); | ||
if (privateIndex == null) { | ||
throw new TornadoCompilationException("Index of FixedArrayNode is null."); | ||
} | ||
offsetAddressNode.setOffset(privateIndex); | ||
} | ||
} | ||
} | ||
|
||
private static boolean isFixedArrayCopied(ValuePhiNode phiNode) { | ||
return phiNode.usages().filter(OffsetAddressNode.class).isNotEmpty() && phiNode.values().filter(FixedArrayNode.class).isNotEmpty(); | ||
} | ||
|
||
private static ValuePhiNode getPrivateArrayIndex(Node node) { | ||
// identify the index | ||
for (Node input : node.inputs()) { | ||
if (input instanceof ValuePhiNode phiNode) { | ||
return phiNode; | ||
} else { | ||
return getPrivateArrayIndex(input); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
} |
Oops, something went wrong.