changeset 12742:40924dbc623b

HSAIL support for compare-and-swap and volatile load/store operations Contributed-by: Tom Deneau <tom.deneau@amd.com>
author Doug Simon <doug.simon@oracle.com>
date Sun, 10 Nov 2013 11:42:31 +0100
parents a5b5e1ebab81
children f1a55428a8d7
files graal/com.oracle.graal.asm.hsail/src/com/oracle/graal/asm/hsail/HSAILAssembler.java graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/AtomicIntGetAndAddTest.java graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/AtomicLongGetAndAddTest.java graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/AtomicReferenceGetAndSetTest.java graal/com.oracle.graal.compiler.hsail/src/com/oracle/graal/compiler/hsail/HSAILLIRGenerator.java graal/com.oracle.graal.hotspot.hsail/src/com/oracle/graal/hotspot/hsail/HSAILHotSpotLIRGenerator.java graal/com.oracle.graal.lir.hsail/src/com/oracle/graal/lir/hsail/HSAILMove.java
diffstat 7 files changed, 389 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.asm.hsail/src/com/oracle/graal/asm/hsail/HSAILAssembler.java	Sat Nov 09 21:34:07 2013 +0100
+++ b/graal/com.oracle.graal.asm.hsail/src/com/oracle/graal/asm/hsail/HSAILAssembler.java	Sun Nov 10 11:42:31 2013 +0100
@@ -25,6 +25,7 @@
 
 import com.oracle.graal.api.code.*;
 
+import static com.oracle.graal.api.code.MemoryBarriers.*;
 import static com.oracle.graal.api.code.ValueUtil.*;
 
 import com.oracle.graal.api.meta.*;
@@ -108,6 +109,23 @@
         emitString(instr + " " + HSAIL.mapRegister(reg) + ", " + mapAddress(addr) + ";");
     }
 
+    /**
+     * Emits a memory barrier instruction.
+     * 
+     * @param barriers the kind of barrier to emit
+     */
+    public final void emitMembar(int barriers) {
+        if (barriers == 0) {
+            emitString("// no barrier before volatile read");
+        } else if (barriers == JMM_POST_VOLATILE_READ) {
+            emitString("sync; // barriers=" + MemoryBarriers.barriersString(barriers));
+        } else if (barriers == JMM_PRE_VOLATILE_WRITE) {
+            emitString("sync; // barriers=" + MemoryBarriers.barriersString(barriers));
+        } else if (barriers == JMM_POST_VOLATILE_WRITE) {
+            emitString("sync; // barriers=" + MemoryBarriers.barriersString(barriers));
+        }
+    }
+
     public final void emitLoad(Kind kind, Value dest, HSAILAddress addr) {
         emitLoad(dest, addr, getArgTypeFromKind(kind));
     }
@@ -242,6 +260,17 @@
         emitString(prefix + destType + "_" + srcType + " " + HSAIL.mapRegister(dest) + ", " + HSAIL.mapRegister(src) + ";");
     }
 
+    /**
+     * Emits a convert instruction that uses unsigned prefix, regardless of the type of dest and
+     * src.
+     * 
+     * @param dest the destination operand
+     * @param src the source operand
+     */
+    public void emitConvertForceUnsigned(Value dest, Value src) {
+        emitString("cvt_" + getArgTypeForceUnsigned(dest) + "_" + getArgTypeForceUnsigned(src) + " " + HSAIL.mapRegister(dest) + ", " + HSAIL.mapRegister(src) + ";");
+    }
+
     public static String mapAddress(HSAILAddress addr) {
         return "[$d" + addr.getBase().encoding() + " + " + addr.getDisplacement() + "]";
     }
@@ -427,6 +456,20 @@
     }
 
     /**
+     * Emits an atomic_cas_global instruction.
+     * 
+     * @param result result operand that gets the original contents of the memory location
+     * @param address the memory location
+     * @param cmpValue the value that will be compared against the memory location
+     * @param newValue the new value that will be written to the memory location if the cmpValue
+     *            comparison matches
+     */
+    public void emitAtomicCas(AllocatableValue result, HSAILAddress address, AllocatableValue cmpValue, AllocatableValue newValue) {
+        emitString(String.format("atomic_cas_global_b%d   %s, %s, %s, %s;", getArgSize(cmpValue), HSAIL.mapRegister(result), mapAddress(address), HSAIL.mapRegister(cmpValue),
+                        HSAIL.mapRegister(newValue)));
+    }
+
+    /**
      * Emits a comment. Useful for debugging purposes.
      * 
      * @param comment
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/AtomicIntGetAndAddTest.java	Sun Nov 10 11:42:31 2013 +0100
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2013, 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.
+ *
+ * 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 com.oracle.graal.compiler.hsail.test;
+
+import java.util.*;
+import java.util.concurrent.atomic.*;
+
+import org.junit.*;
+
+import sun.misc.*;
+
+import com.oracle.graal.compiler.hsail.test.infra.*;
+
+/**
+ * Tests {@link AtomicInteger#getAndAdd(int)} which indirectly tests
+ * {@link Unsafe#compareAndSwapInt(Object, long, int, int)}.
+ */
+public class AtomicIntGetAndAddTest extends GraalKernelTester {
+
+    static final int NUM = 20;
+    @Result public int[] outArray = new int[NUM];
+    AtomicInteger atomicInt = new AtomicInteger();
+
+    void setupArrays() {
+        for (int i = 0; i < NUM; i++) {
+            outArray[i] = -i;
+        }
+    }
+
+    @Override
+    public void runTest() {
+        setupArrays();
+
+        dispatchMethodKernel(NUM);
+
+        // note: the actual order of entries in outArray is not predictable
+        // thus we sort before we compare results
+        Arrays.sort(outArray);
+    }
+
+    public void run(int gid) {
+        outArray[gid] = atomicInt.getAndAdd(0x7);
+    }
+
+    @Test
+    public void test() {
+        testGeneratedHsail();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/AtomicLongGetAndAddTest.java	Sun Nov 10 11:42:31 2013 +0100
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2013, 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.
+ *
+ * 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 com.oracle.graal.compiler.hsail.test;
+
+import java.util.*;
+import java.util.concurrent.atomic.*;
+
+import org.junit.*;
+
+import sun.misc.*;
+
+import com.oracle.graal.compiler.hsail.test.infra.*;
+
+/**
+ * Tests {@link AtomicLong#getAndAdd(long)} which indirectly tests
+ * {@link Unsafe#compareAndSwapLong(Object, long, long, long)}.
+ */
+public class AtomicLongGetAndAddTest extends GraalKernelTester {
+
+    static final int NUM = 20;
+    @Result public long[] outArray = new long[NUM];
+    AtomicLong atomicLong = new AtomicLong();
+
+    void setupArrays() {
+        for (int i = 0; i < NUM; i++) {
+            outArray[i] = -i;
+        }
+    }
+
+    @Override
+    public void runTest() {
+        setupArrays();
+
+        dispatchMethodKernel(NUM);
+
+        // note: the actual order of entries in outArray is not predictable
+        // thus we sort before we compare results
+        Arrays.sort(outArray);
+    }
+
+    public void run(int gid) {
+        outArray[gid] = atomicLong.getAndAdd(0x7);
+    }
+
+    @Test
+    public void test() {
+        testGeneratedHsail();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.compiler.hsail.test/src/com/oracle/graal/compiler/hsail/test/AtomicReferenceGetAndSetTest.java	Sun Nov 10 11:42:31 2013 +0100
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2013, 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.
+ *
+ * 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 com.oracle.graal.compiler.hsail.test;
+
+import java.util.concurrent.atomic.*;
+
+import org.junit.*;
+
+import sun.misc.*;
+
+import com.oracle.graal.compiler.hsail.test.infra.*;
+import com.oracle.graal.debug.*;
+
+/**
+ * Tests {@link AtomicReference#getAndSet(Object)} which indirectly tests
+ * {@link Unsafe#compareAndSwapObject(Object, long, Object, Object)}. The latter requires special
+ * handling if compressed oops are enabled.
+ */
+public class AtomicReferenceGetAndSetTest extends GraalKernelTester {
+
+    static final int NUM = 20;
+    @Result public int[] followedCount = new int[NUM];
+    public MyObj[] inArray = new MyObj[NUM];
+    AtomicReference<MyObj> atomicRef = new AtomicReference<>();
+
+    public static class MyObj {
+        public int val;
+        public boolean[] followedBy = new boolean[NUM + 1];
+
+        MyObj(int n) {
+            val = n;
+        }
+    }
+
+    void setupArrays() {
+        for (int i = 0; i < NUM; i++) {
+            inArray[i] = new MyObj(i + 1);
+        }
+        atomicRef.set(new MyObj(0)); // initial value
+    }
+
+    private static final boolean DEBUG = false;
+
+    @Override
+    public void runTest() {
+        setupArrays();
+
+        dispatchMethodKernel(NUM);
+
+        // make a fake followedBy for the final object
+        MyObj finalObj = atomicRef.get();
+        finalObj.followedBy[0] = true;
+
+        // When the kernel is done, compute the number of true bits in each followedBy array;
+        for (int i = 0; i < NUM; i++) {
+            MyObj obj = inArray[i];
+            int count = 0;
+            for (int j = 0; j < NUM + 1; j++) {
+                boolean b = obj.followedBy[j];
+                if (b) {
+                    count++;
+                    if (DEBUG) {
+                        TTY.println("obj " + obj.val + " was followed by " + j);
+                    }
+                }
+
+            }
+            followedCount[i] = count;
+        }
+    }
+
+    public void run(int gid) {
+        MyObj newObj = inArray[gid];
+        MyObj oldObj = atomicRef.getAndSet(newObj);
+        oldObj.followedBy[newObj.val] = true;
+    }
+
+    @Test
+    public void test() {
+        testGeneratedHsail();
+    }
+}
--- a/graal/com.oracle.graal.compiler.hsail/src/com/oracle/graal/compiler/hsail/HSAILLIRGenerator.java	Sat Nov 09 21:34:07 2013 +0100
+++ b/graal/com.oracle.graal.compiler.hsail/src/com/oracle/graal/compiler/hsail/HSAILLIRGenerator.java	Sun Nov 10 11:42:31 2013 +0100
@@ -37,6 +37,7 @@
 import com.oracle.graal.lir.*;
 import com.oracle.graal.lir.StandardOp.JumpOp;
 import com.oracle.graal.lir.hsail.*;
+import com.oracle.graal.lir.hsail.HSAILArithmetic.ConvertOp;
 import com.oracle.graal.lir.hsail.HSAILArithmetic.Op1Stack;
 import com.oracle.graal.lir.hsail.HSAILArithmetic.Op2Reg;
 import com.oracle.graal.lir.hsail.HSAILArithmetic.Op2Stack;
@@ -47,11 +48,11 @@
 import com.oracle.graal.lir.hsail.HSAILControlFlow.FloatCondMoveOp;
 import com.oracle.graal.lir.hsail.HSAILControlFlow.ReturnOp;
 import com.oracle.graal.lir.hsail.HSAILMove.LeaOp;
+import com.oracle.graal.lir.hsail.HSAILMove.MembarOp;
 import com.oracle.graal.lir.hsail.HSAILMove.MoveFromRegOp;
 import com.oracle.graal.lir.hsail.HSAILMove.MoveToRegOp;
 import com.oracle.graal.nodes.*;
 import com.oracle.graal.nodes.calc.*;
-import com.oracle.graal.nodes.java.*;
 import com.oracle.graal.phases.util.*;
 
 /**
@@ -170,7 +171,7 @@
         append(new JumpOp(label));
     }
 
-    private static HSAILCompare mapKindToCompareOp(Kind kind) {
+    protected static HSAILCompare mapKindToCompareOp(Kind kind) {
         switch (kind) {
             case Int:
                 return ICMP;
@@ -612,7 +613,8 @@
 
     @Override
     public void emitMembar(int barriers) {
-        throw GraalInternalError.unimplemented();
+        int necessaryBarriers = target().arch.requiredBarriers(barriers);
+        append(new MembarOp(necessaryBarriers));
     }
 
     @Override
@@ -702,11 +704,6 @@
     }
 
     @Override
-    public void visitCompareAndSwap(LoweredCompareAndSwapNode node, Value address) {
-        throw GraalInternalError.unimplemented();
-    }
-
-    @Override
     public void visitBreakpointNode(BreakpointNode node) {
         throw GraalInternalError.unimplemented();
     }
--- a/graal/com.oracle.graal.hotspot.hsail/src/com/oracle/graal/hotspot/hsail/HSAILHotSpotLIRGenerator.java	Sat Nov 09 21:34:07 2013 +0100
+++ b/graal/com.oracle.graal.hotspot.hsail/src/com/oracle/graal/hotspot/hsail/HSAILHotSpotLIRGenerator.java	Sun Nov 10 11:42:31 2013 +0100
@@ -23,6 +23,8 @@
 
 package com.oracle.graal.hotspot.hsail;
 
+import sun.misc.*;
+
 import com.oracle.graal.api.code.*;
 import com.oracle.graal.api.meta.*;
 import com.oracle.graal.compiler.hsail.*;
@@ -32,6 +34,8 @@
 import com.oracle.graal.lir.hsail.HSAILControlFlow.*;
 import com.oracle.graal.lir.hsail.HSAILMove.*;
 import com.oracle.graal.nodes.*;
+import com.oracle.graal.nodes.calc.*;
+import com.oracle.graal.nodes.java.*;
 import com.oracle.graal.phases.util.*;
 
 /**
@@ -74,6 +78,41 @@
         return access != null && ((HeapAccess) access).isCompressible();
     }
 
+    /**
+     * Appends either a {@link CompareAndSwapOp} or a {@link CompareAndSwapCompressedOp} depending
+     * on whether the memory location of a given {@link LoweredCompareAndSwapNode} contains a
+     * compressed oop. For the {@link CompareAndSwapCompressedOp} case, allocates a number of
+     * scratch registers. The result {@link #operand(ValueNode) operand} for {@code node} complies
+     * with the API for {@link Unsafe#compareAndSwapInt(Object, long, int, int)}.
+     * 
+     * @param address the memory location targeted by the operation
+     */
+    @Override
+    public void visitCompareAndSwap(LoweredCompareAndSwapNode node, Value address) {
+        Kind kind = node.getNewValue().kind();
+        assert kind == node.getExpectedValue().kind();
+        Variable expected = load(operand(node.getExpectedValue()));
+        Variable newValue = load(operand(node.getNewValue()));
+        HSAILAddressValue addressValue = asAddressValue(address);
+        Variable casResult = newVariable(kind);
+        if (config.useCompressedOops && node.isCompressible()) {
+            // make 64-bit scratch variables for expected and new
+            Variable scratchExpected64 = newVariable(Kind.Long);
+            Variable scratchNewValue64 = newVariable(Kind.Long);
+            // make 32-bit scratch variables for expected and new and result
+            Variable scratchExpected32 = newVariable(Kind.Int);
+            Variable scratchNewValue32 = newVariable(Kind.Int);
+            Variable scratchCasResult32 = newVariable(Kind.Int);
+            append(new CompareAndSwapCompressedOp(casResult, addressValue, expected, newValue, scratchExpected64, scratchNewValue64, scratchExpected32, scratchNewValue32, scratchCasResult32,
+                            getNarrowOopBase(), getNarrowOopShift(), getLogMinObjectAlignment()));
+        } else {
+            append(new CompareAndSwapOp(casResult, addressValue, expected, newValue));
+        }
+        Variable nodeResult = newVariable(node.kind());
+        append(new CondMoveOp(mapKindToCompareOp(kind), casResult, expected, nodeResult, Condition.EQ, Constant.INT_1, Constant.INT_0));
+        setResult(node, nodeResult);
+    }
+
     @Override
     public Variable emitLoad(Kind kind, Value address, DeoptimizingNode access) {
         HSAILAddressValue loadAddress = asAddressValue(address);
--- a/graal/com.oracle.graal.lir.hsail/src/com/oracle/graal/lir/hsail/HSAILMove.java	Sat Nov 09 21:34:07 2013 +0100
+++ b/graal/com.oracle.graal.lir.hsail/src/com/oracle/graal/lir/hsail/HSAILMove.java	Sun Nov 10 11:42:31 2013 +0100
@@ -153,6 +153,20 @@
         }
     }
 
+    public static class MembarOp extends HSAILLIRInstruction {
+
+        private final int barriers;
+
+        public MembarOp(final int barriers) {
+            this.barriers = barriers;
+        }
+
+        @Override
+        public void emitCode(TargetMethodAssembler tasm, HSAILAssembler masm) {
+            masm.emitMembar(barriers);
+        }
+    }
+
     public static class LoadOp extends MemOp {
 
         @Def({REG}) protected AllocatableValue result;
@@ -316,7 +330,54 @@
 
         @Override
         public void emitCode(TargetMethodAssembler tasm, HSAILAssembler masm) {
-            compareAndSwap(tasm, masm, result, address, cmpValue, newValue);
+            masm.emitAtomicCas(result, address.toAddress(), cmpValue, newValue);
+        }
+    }
+
+    @Opcode("CAS")
+    public static class CompareAndSwapCompressedOp extends CompareAndSwapOp {
+
+        @Temp({REG}) private AllocatableValue scratchCmpValue64;
+        @Temp({REG}) private AllocatableValue scratchNewValue64;
+        @Temp({REG}) private AllocatableValue scratchCmpValue32;
+        @Temp({REG}) private AllocatableValue scratchNewValue32;
+        @Temp({REG}) private AllocatableValue scratchCasResult32;
+        private final long base;
+        private final int shift;
+        private final int alignment;
+
+        public CompareAndSwapCompressedOp(AllocatableValue result, HSAILAddressValue address, AllocatableValue cmpValue, AllocatableValue newValue, AllocatableValue scratchCmpValue64,
+                        AllocatableValue scratchNewValue64, AllocatableValue scratchCmpValue32, AllocatableValue scratchNewValue32, AllocatableValue scratchCasResult32, long base, int shift,
+                        int alignment) {
+            super(result, address, cmpValue, newValue);
+            this.scratchCmpValue64 = scratchCmpValue64;
+            this.scratchNewValue64 = scratchNewValue64;
+            this.scratchCmpValue32 = scratchCmpValue32;
+            this.scratchNewValue32 = scratchNewValue32;
+            this.scratchCasResult32 = scratchCasResult32;
+            this.base = base;
+            this.shift = shift;
+            this.alignment = alignment;
+        }
+
+        @Override
+        public void emitCode(TargetMethodAssembler tasm, HSAILAssembler masm) {
+            // assume any encoded or decoded value could be null
+            boolean testForNull = true;
+            // set up scratch registers to be encoded versions
+            masm.emitMov(scratchCmpValue64, cmpValue);
+            encodePointer(masm, scratchCmpValue64, base, shift, alignment, testForNull);
+            masm.emitMov(scratchNewValue64, newValue);
+            encodePointer(masm, scratchNewValue64, base, shift, alignment, testForNull);
+            // get encoded versions into 32-bit registers
+            masm.emitConvertForceUnsigned(scratchCmpValue32, scratchCmpValue64);
+            masm.emitConvertForceUnsigned(scratchNewValue32, scratchNewValue64);
+            // finally do the cas
+            masm.emitAtomicCas(scratchCasResult32, address.toAddress(), scratchCmpValue32, scratchNewValue32);
+            // and convert the 32-bit CasResult back to 64-bit
+            masm.emitConvertForceUnsigned(result, scratchCasResult32);
+            // and decode/uncompress the 64-bit cas result
+            decodePointer(masm, result, base, shift, alignment, testForNull);
         }
     }
 
@@ -362,9 +423,4 @@
             throw GraalInternalError.shouldNotReachHere();
         }
     }
-
-    @SuppressWarnings("unused")
-    protected static void compareAndSwap(TargetMethodAssembler tasm, HSAILAssembler masm, AllocatableValue result, HSAILAddressValue address, AllocatableValue cmpValue, AllocatableValue newValue) {
-        throw new InternalError("NYI");
-    }
 }