Skip to content

Commit

Permalink
Extract llvm.umin/umax operators into singletons
Browse files Browse the repository at this point in the history
  • Loading branch information
fangerer committed Sep 30, 2021
1 parent 5bfeedc commit 80693c2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,14 @@
import com.oracle.truffle.llvm.runtime.nodes.func.LLVMLandingpadNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.func.LLVMResumeNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.func.LLVMTypeIdForExceptionNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.c.LLVMCMathsIntrinsics.LLVMUmaxOperator;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.c.LLVMCMathsIntrinsics.LLVMUminOperator;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.c.LLVMCMathsIntrinsicsFactory;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.c.LLVMCMathsIntrinsicsFactory.LLVMAbsNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.c.LLVMCMathsIntrinsicsFactory.LLVMFAbsNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.c.LLVMCMathsIntrinsicsFactory.LLVMFAbsVectorNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.c.LLVMCMathsIntrinsicsFactory.LLVMPowNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.c.LLVMCMathsIntrinsicsFactory.LLVMUmaxVectorNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.c.LLVMCMathsIntrinsicsFactory.LLVMUminVectorNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.c.LLVMCMathsIntrinsicsFactory.LLVMUnsignedVectorMinMaxNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.interop.LLVMTruffleGetArgCountNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.interop.LLVMTruffleGetArgNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.llvm.LLVMAssumeNodeGen;
Expand Down Expand Up @@ -1745,20 +1746,18 @@ protected LLVMExpressionNode getLLVMBuiltin(FunctionDeclaration declaration, LLV
}

if ("llvm.umax".equals(intrinsicName) && typeSuffix != null && typeSuffix.length != null) {
// format '.([vp]\d+)?[if]\d+)'
try {
int vectorLength = Integer.parseInt(typeSuffix.length);
return LLVMUmaxVectorNodeGen.create(args[1], args[2], vectorLength);
return LLVMUnsignedVectorMinMaxNodeGen.create(args[1], args[2], vectorLength, LLVMUmaxOperator.INSTANCE);
} catch (NumberFormatException e) {
// fall through
}
}

if ("llvm.umin".equals(intrinsicName) && typeSuffix != null && typeSuffix.length != null) {
// format '.([vp]\d+)?[if]\d+)'
try {
int vectorLength = Integer.parseInt(typeSuffix.length);
return LLVMUminVectorNodeGen.create(args[1], args[2], vectorLength);
return LLVMUnsignedVectorMinMaxNodeGen.create(args[1], args[2], vectorLength, LLVMUminOperator.INSTANCE);
} catch (NumberFormatException e) {
// fall through
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
*/
package com.oracle.truffle.llvm.runtime.nodes.intrinsics.c;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.NodeField;
Expand Down Expand Up @@ -810,38 +809,91 @@ protected LLVM80BitFloat doLLVM80BitFloat(LLVM80BitFloat magnitude, LLVM80BitFlo
}
}

@NodeChild(type = LLVMExpressionNode.class)
@NodeChild(type = LLVMExpressionNode.class)
@NodeField(name = "vectorLength", type = int.class)
abstract static class LLVMUnsignedVectorMinMaxNode extends LLVMBuiltin {
protected abstract int getVectorLength();
abstract static class LLVMUMinMaxOperator {
protected abstract boolean compare(boolean a, boolean b);

protected abstract int compare(int a, int b);

protected abstract long compare(long a, long b);

protected abstract float compare(float a, float b);

protected abstract double compare(double a, double b);
}

public static final class LLVMUmaxOperator extends LLVMUMinMaxOperator {
public static final LLVMUmaxOperator INSTANCE = new LLVMUmaxOperator();

@Override
protected boolean compare(boolean a, boolean b) {
throw CompilerDirectives.shouldNotReachHere();
return a || b;
}

private byte compare(byte a, byte b) {
return (byte) compare((int) a, (int) b);
@Override
protected int compare(int a, int b) {
return Integer.compareUnsigned(a, b) >= 0 ? a : b;
}

private short compare(short a, short b) {
return (short) compare((int) a, (int) b);
@Override
protected long compare(long a, long b) {
return Long.compareUnsigned(a, b) >= 0 ? a : b;
}

@Override
protected float compare(float a, float b) {
return Math.max(a, b);
}

@Override
protected double compare(double a, double b) {
return Math.max(a, b);
}
}

public static final class LLVMUminOperator extends LLVMUMinMaxOperator {
public static final LLVMUminOperator INSTANCE = new LLVMUminOperator();

@Override
protected boolean compare(boolean a, boolean b) {
return a && b;
}

@Override
protected int compare(int a, int b) {
throw CompilerDirectives.shouldNotReachHere();
return Integer.compareUnsigned(a, b) <= 0 ? a : b;
}

@Override
protected long compare(long a, long b) {
throw CompilerDirectives.shouldNotReachHere();
return Long.compareUnsigned(a, b) <= 0 ? a : b;
}

@Override
protected float compare(float a, float b) {
throw CompilerDirectives.shouldNotReachHere();
return Math.min(a, b);
}

@Override
protected double compare(double a, double b) {
throw CompilerDirectives.shouldNotReachHere();
return Math.min(a, b);
}
}

@NodeChild(type = LLVMExpressionNode.class)
@NodeChild(type = LLVMExpressionNode.class)
@NodeField(name = "vectorLength", type = int.class)
@NodeField(name = "operator", type = LLVMUMinMaxOperator.class)
public abstract static class LLVMUnsignedVectorMinMaxNode extends LLVMBuiltin {
protected abstract int getVectorLength();

protected abstract LLVMUMinMaxOperator getOperator();

private byte compare(byte a, byte b) {
return (byte) getOperator().compare(a, b);
}

private short compare(short a, short b) {
return (short) getOperator().compare(a, b);
}

@Specialization
Expand All @@ -851,7 +903,7 @@ protected LLVMI1Vector doI1Vector(LLVMI1Vector a, LLVMI1Vector b) {
assert b.getLength() == getVectorLength();
boolean[] result = new boolean[getVectorLength()];
for (int i = 0; i < getVectorLength(); i++) {
result[i] = compare(a.getValue(i), b.getValue(i));
result[i] = getOperator().compare(a.getValue(i), b.getValue(i));
}
return LLVMI1Vector.create(result);
}
Expand Down Expand Up @@ -893,7 +945,7 @@ protected LLVMI32Vector doI32Vector(LLVMI32Vector a, LLVMI32Vector b) {
for (int i = 0; i < getVectorLength(); i++) {
int aValue = a.getValue(i);
int bValue = b.getValue(i);
result[i] = compare(aValue, bValue);
result[i] = getOperator().compare(aValue, bValue);
}
return LLVMI32Vector.create(result);
}
Expand All @@ -907,7 +959,7 @@ protected LLVMI64Vector doI64Vector(LLVMI64Vector a, LLVMI64Vector b) {
for (int i = 0; i < getVectorLength(); i++) {
long aValue = a.getValue(i);
long bValue = b.getValue(i);
result[i] = Long.compareUnsigned(aValue, bValue) >= 0 ? aValue : bValue;
result[i] = getOperator().compare(aValue, bValue) >= 0 ? aValue : bValue;
}
return LLVMI64Vector.create(result);
}
Expand All @@ -919,7 +971,7 @@ protected LLVMFloatVector doFloatVector(LLVMFloatVector a, LLVMFloatVector b) {
assert b.getLength() == getVectorLength();
float[] result = new float[getVectorLength()];
for (int i = 0; i < getVectorLength(); i++) {
result[i] = compare(a.getValue(i), b.getValue(i));
result[i] = getOperator().compare(a.getValue(i), b.getValue(i));
}
return LLVMFloatVector.create(result);
}
Expand All @@ -931,65 +983,9 @@ protected LLVMDoubleVector doDoubleVector(LLVMDoubleVector a, LLVMDoubleVector b
assert b.getLength() == getVectorLength();
double[] result = new double[getVectorLength()];
for (int i = 0; i < getVectorLength(); i++) {
result[i] = compare(a.getValue(i), b.getValue(i));
result[i] = getOperator().compare(a.getValue(i), b.getValue(i));
}
return LLVMDoubleVector.create(result);
}
}

public abstract static class LLVMUmaxVectorNode extends LLVMUnsignedVectorMinMaxNode {

@Override
protected final boolean compare(boolean a, boolean b) {
return a || b;
}

@Override
protected final int compare(int a, int b) {
return Integer.compareUnsigned(a, b) >= 0 ? a : b;
}

@Override
protected final long compare(long a, long b) {
return Long.compareUnsigned(a, b) >= 0 ? a : b;
}

@Override
protected final float compare(float a, float b) {
return Math.max(a, b);
}

@Override
protected final double compare(double a, double b) {
return Math.max(a, b);
}
}

public abstract static class LLVMUminVectorNode extends LLVMUnsignedVectorMinMaxNode {

@Override
protected final boolean compare(boolean a, boolean b) {
return a && b;
}

@Override
protected final int compare(int a, int b) {
return Integer.compareUnsigned(a, b) <= 0 ? a : b;
}

@Override
protected final long compare(long a, long b) {
return Long.compareUnsigned(a, b) <= 0 ? a : b;
}

@Override
protected final float compare(float a, float b) {
return Math.min(a, b);
}

@Override
protected final double compare(double a, double b) {
return Math.min(a, b);
}
}
}

0 comments on commit 80693c2

Please sign in to comment.