Skip to content

Commit

Permalink
[GR-40606] TRegex: add missing branch probabilities.
Browse files Browse the repository at this point in the history
PullRequest: graal/12740
  • Loading branch information
djoooooe committed Sep 22, 2022
2 parents 5278fdc + 3afff82 commit a3943ee
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 161 deletions.
Expand Up @@ -254,36 +254,36 @@ private static void registerTStringPlugins(InvocationPlugins plugins, Replacemen
plugins.registerIntrinsificationPredicate(t -> t.getName().equals("Lcom/oracle/truffle/api/strings/TStringOps;"));
InvocationPlugins.Registration r = new InvocationPlugins.Registration(plugins, "com.oracle.truffle.api.strings.TStringOps", replacements);

r.register(new InlineOnlyInvocationPlugin("runIndexOfAny1", Node.class, Object.class, long.class, int.class, int.class, boolean.class, int.class, int.class) {
r.register(new InlineOnlyInvocationPlugin("runIndexOfAny1", Node.class, byte[].class, long.class, int.class, int.class, boolean.class, int.class, int.class) {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode location,
ValueNode array, ValueNode offset, ValueNode length, ValueNode stride, ValueNode isNative, ValueNode fromIndex, ValueNode v0) {
return applyIndexOf(b, false, false, array, offset, length, stride, isNative, fromIndex, v0);
}
});
r.register(new InlineOnlyInvocationPlugin("runIndexOfAny2", Node.class, Object.class, long.class, int.class, int.class, boolean.class, int.class, int.class, int.class) {
r.register(new InlineOnlyInvocationPlugin("runIndexOfAny2", Node.class, byte[].class, long.class, int.class, int.class, boolean.class, int.class, int.class, int.class) {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode location,
ValueNode array, ValueNode offset, ValueNode length, ValueNode stride, ValueNode isNative, ValueNode fromIndex, ValueNode v0, ValueNode v1) {
return applyIndexOf(b, false, false, array, offset, length, stride, isNative, fromIndex, v0, v1);
}
});
r.register(new InlineOnlyInvocationPlugin("runIndexOfAny3", Node.class, Object.class, long.class, int.class, int.class, boolean.class, int.class, int.class, int.class, int.class) {
r.register(new InlineOnlyInvocationPlugin("runIndexOfAny3", Node.class, byte[].class, long.class, int.class, int.class, boolean.class, int.class, int.class, int.class, int.class) {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode location,
ValueNode array, ValueNode offset, ValueNode length, ValueNode stride, ValueNode isNative, ValueNode fromIndex, ValueNode v0, ValueNode v1, ValueNode v2) {
return applyIndexOf(b, false, false, array, offset, length, stride, isNative, fromIndex, v0, v1, v2);
}
});
r.register(new InlineOnlyInvocationPlugin("runIndexOfAny4", Node.class, Object.class, long.class, int.class, int.class, boolean.class, int.class, int.class, int.class, int.class, int.class) {
r.register(new InlineOnlyInvocationPlugin("runIndexOfAny4", Node.class, byte[].class, long.class, int.class, int.class, boolean.class, int.class, int.class, int.class, int.class, int.class) {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode location,
ValueNode array, ValueNode offset, ValueNode length, ValueNode stride, ValueNode isNative, ValueNode fromIndex, ValueNode v0, ValueNode v1, ValueNode v2,
ValueNode v3) {
return applyIndexOf(b, false, false, array, offset, length, stride, isNative, fromIndex, v0, v1, v2, v3);
}
});
r.register(new InlineOnlyInvocationPlugin("runIndexOf2ConsecutiveWithStride", Node.class, Object.class, long.class, int.class, int.class, boolean.class, int.class, int.class, int.class) {
r.register(new InlineOnlyInvocationPlugin("runIndexOf2ConsecutiveWithStride", Node.class, byte[].class, long.class, int.class, int.class, boolean.class, int.class, int.class, int.class) {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode location,
ValueNode array, ValueNode offset, ValueNode length, ValueNode stride, ValueNode isNative, ValueNode fromIndex, ValueNode v0, ValueNode v1) {
Expand All @@ -292,8 +292,8 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
});

r.register(new InlineOnlyInvocationPlugin("runRegionEqualsWithStride", Node.class,
Object.class, long.class, boolean.class,
Object.class, long.class, boolean.class, int.class, int.class) {
byte[].class, long.class, boolean.class,
byte[].class, long.class, boolean.class, int.class, int.class) {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode location,
ValueNode arrayA, ValueNode offsetA, ValueNode isNativeA,
Expand All @@ -312,8 +312,8 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
}
});
r.register(new InlineOnlyInvocationPlugin("runMemCmp", Node.class,
Object.class, long.class, boolean.class,
Object.class, long.class, boolean.class, int.class, int.class) {
byte[].class, long.class, boolean.class,
byte[].class, long.class, boolean.class, int.class, int.class) {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode location,
ValueNode arrayA, ValueNode offsetA, ValueNode isNativeA,
Expand All @@ -332,24 +332,36 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
}
});
r.register(new InlineOnlyInvocationPlugin("runArrayCopy", Node.class,
Object.class, long.class, boolean.class,
Object.class, long.class, boolean.class, int.class, int.class) {
byte[].class, long.class, boolean.class,
byte[].class, long.class, boolean.class, int.class, int.class) {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode location,
ValueNode arrayA, ValueNode offsetA, ValueNode isNativeA,
ValueNode arrayB, ValueNode offsetB, ValueNode isNativeB, ValueNode length, ValueNode dynamicStrides) {
if (dynamicStrides.isJavaConstant()) {
int directStubCallIndex = dynamicStrides.asJavaConstant().asInt();
b.add(new ArrayCopyWithConversionsNode(arrayA, offsetA, arrayB, offsetB, length,
StrideUtil.getConstantStrideA(directStubCallIndex),
StrideUtil.getConstantStrideB(directStubCallIndex)));
} else {
b.add(new ArrayCopyWithConversionsNode(arrayA, offsetA, arrayB, offsetB, length, dynamicStrides));
}
return true;
return applyArrayCopy(b, arrayA, offsetA, arrayB, offsetB, length, dynamicStrides);
}
});
r.register(new InlineOnlyInvocationPlugin("runArrayCopy", Node.class,
char[].class, long.class,
byte[].class, long.class, int.class, int.class) {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode location,
ValueNode arrayA, ValueNode offsetA,
ValueNode arrayB, ValueNode offsetB, ValueNode length, ValueNode dynamicStrides) {
return applyArrayCopy(b, arrayA, offsetA, arrayB, offsetB, length, dynamicStrides);
}
});
r.register(new InlineOnlyInvocationPlugin("runArrayCopy", Node.class,
int[].class, long.class,
byte[].class, long.class, int.class, int.class) {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode location,
ValueNode arrayA, ValueNode offsetA,
ValueNode arrayB, ValueNode offsetB, ValueNode length, ValueNode dynamicStrides) {
return applyArrayCopy(b, arrayA, offsetA, arrayB, offsetB, length, dynamicStrides);
}
});
r.register(new InlineOnlyInvocationPlugin("runCalcStringAttributesLatin1", Node.class, Object.class, long.class, int.class, boolean.class) {
r.register(new InlineOnlyInvocationPlugin("runCalcStringAttributesLatin1", Node.class, byte[].class, long.class, int.class, boolean.class) {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode location,
ValueNode array, ValueNode offset, ValueNode length, ValueNode isNative) {
Expand All @@ -358,7 +370,7 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
return true;
}
});
r.register(new InlineOnlyInvocationPlugin("runCalcStringAttributesBMP", Node.class, Object.class, long.class, int.class, boolean.class) {
r.register(new InlineOnlyInvocationPlugin("runCalcStringAttributesBMP", Node.class, byte[].class, long.class, int.class, boolean.class) {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode location,
ValueNode array, ValueNode offset, ValueNode length, ValueNode isNative) {
Expand All @@ -367,7 +379,7 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
return true;
}
});
r.register(new InlineOnlyInvocationPlugin("runCalcStringAttributesUTF8", Node.class, Object.class, long.class, int.class, boolean.class, boolean.class) {
r.register(new InlineOnlyInvocationPlugin("runCalcStringAttributesUTF8", Node.class, byte[].class, long.class, int.class, boolean.class, boolean.class) {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode location,
ValueNode array, ValueNode offset, ValueNode length, ValueNode isNative, ValueNode assumeValid) {
Expand All @@ -376,7 +388,7 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
return true;
}
});
r.register(new InlineOnlyInvocationPlugin("runCalcStringAttributesUTF16", Node.class, Object.class, long.class, int.class, boolean.class, boolean.class) {
r.register(new InlineOnlyInvocationPlugin("runCalcStringAttributesUTF16", Node.class, byte[].class, long.class, int.class, boolean.class, boolean.class) {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode location,
ValueNode array, ValueNode offset, ValueNode length, ValueNode isNative, ValueNode assumeValid) {
Expand All @@ -394,7 +406,7 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
return true;
}
});
r.register(new InlineOnlyInvocationPlugin("runCalcStringAttributesUTF32", Node.class, Object.class, long.class, int.class, boolean.class) {
r.register(new InlineOnlyInvocationPlugin("runCalcStringAttributesUTF32", Node.class, byte[].class, long.class, int.class, boolean.class) {
@Override
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode location,
ValueNode array, ValueNode offset, ValueNode length, ValueNode isNative) {
Expand All @@ -414,6 +426,18 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
});
}

private static boolean applyArrayCopy(GraphBuilderContext b, ValueNode arrayA, ValueNode offsetA, ValueNode arrayB, ValueNode offsetB, ValueNode length, ValueNode dynamicStrides) {
if (dynamicStrides.isJavaConstant()) {
int directStubCallIndex = dynamicStrides.asJavaConstant().asInt();
b.add(new ArrayCopyWithConversionsNode(arrayA, offsetA, arrayB, offsetB, length,
StrideUtil.getConstantStrideA(directStubCallIndex),
StrideUtil.getConstantStrideB(directStubCallIndex)));
} else {
b.add(new ArrayCopyWithConversionsNode(arrayA, offsetA, arrayB, offsetB, length, dynamicStrides));
}
return true;
}

public static boolean applyIndexOf(GraphBuilderContext b, boolean findTwoConsecutive, boolean withMask,
ValueNode array, ValueNode offset, ValueNode length, ValueNode stride, ValueNode isNative, ValueNode fromIndex, ValueNode... values) {
Stride constStride = constantStrideParam(stride);
Expand Down
Expand Up @@ -40,6 +40,9 @@
*/
package com.oracle.truffle.regex.tregex.nodes;

import static com.oracle.truffle.api.CompilerDirectives.LIKELY_PROBABILITY;
import static com.oracle.truffle.api.CompilerDirectives.injectBranchProbability;

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.nodes.ExplodeLoop;
Expand All @@ -56,6 +59,11 @@

public abstract class TRegexExecutorNode extends Node {

public static final double CONTINUE_PROBABILITY = 0.99;
public static final double EXIT_PROBABILITY = 1.0 - CONTINUE_PROBABILITY;
public static final double LATIN1_PROBABILITY = 0.7;
public static final double BMP_PROBABILITY = 0.2;
public static final double ASTRAL_PROBABILITY = 0.1;
private final RegexSource source;
private final int numberOfCaptureGroups;
private final int numberOfTransitions;
Expand Down Expand Up @@ -178,7 +186,7 @@ public int inputReadAndDecode(TRegexExecutorLocals locals, int index) {
if (getEncoding() == Encodings.UTF_16) {
locals.setNextIndex(inputIncRaw(index));
int c = inputReadRaw(locals);
if (inputUTF16IsHighSurrogate(c) && inputHasNext(locals, locals.getNextIndex())) {
if (injectBranchProbability(ASTRAL_PROBABILITY, inputUTF16IsHighSurrogate(c) && inputHasNext(locals, locals.getNextIndex()))) {
int c2 = inputReadRaw(locals, locals.getNextIndex());
if (inputUTF16IsLowSurrogate(c2)) {
locals.setNextIndex(inputIncRaw(locals.getNextIndex()));
Expand All @@ -188,7 +196,7 @@ public int inputReadAndDecode(TRegexExecutorLocals locals, int index) {
return c;
} else if (getEncoding() == Encodings.UTF_8) {
int c = inputReadRaw(locals);
if (c < 0x80) {
if (injectBranchProbability(LATIN1_PROBABILITY, c < 0x80)) {
locals.setNextIndex(inputIncRaw(index));
return c;
}
Expand All @@ -197,7 +205,7 @@ public int inputReadAndDecode(TRegexExecutorLocals locals, int index) {
assert c >> 6 == 2;
for (int i = 1; i < 4; i++) {
c = inputReadRaw(locals, locals.getIndex() - i);
if (i < 3 && c >> 6 == 2) {
if (injectBranchProbability(LIKELY_PROBABILITY, i < 3 && c >> 6 == 2)) {
codepoint |= (c & 0x3f) << (6 * i);
} else {
break;
Expand Down

0 comments on commit a3943ee

Please sign in to comment.