Class CompilerDirectives
java.lang.Object
com.oracle.truffle.api.CompilerDirectives
Directives that influence the optimizations of the Truffle compiler. All of the operations have
no effect when executed in the Truffle interpreter.
- Since:
- 0.8 or earlier
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic @interfaceMarks fields that should be considered final for a Truffle compilation although they are not final while executing in the interpreter.static @interfaceMarks a method that it is considered as a boundary for Truffle partial evaluation.static @interfaceMarks classes as value types. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doublestatic final doublestatic final doublestatic final double -
Method Summary
Modifier and TypeMethodDescriptionstatic voidBails out of a compilation (e.g., for guest language features that should never be compiled).static voidblackhole(boolean value) Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused.static voidblackhole(byte value) Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused.static voidblackhole(char value) Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused.static voidblackhole(double value) Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused.static voidblackhole(float value) Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused.static voidblackhole(int value) Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused.static voidblackhole(long value) Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused.static voidblackhole(short value) Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused.static voidConsume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused.static <T> TCasts the given object to the exact class represented byclazz.static <T> TensureAllocatedHere(T object) Prevents the compiler from moving an allocation, enabling preciseOutOfMemoryErrorexception locations.static voidensureVirtualized(Object object) Ensures that the given object will be virtual (escape analyzed) at all points that are dominated by the current position.static voidensureVirtualizedHere(Object object) Ensures that the given object will be virtual at the current position.static booleanReturns a boolean value indicating whether the method is executed in a compilation tier which can be replaced with a higher tier (e.g.static booleanReturns a boolean value indicating whether the method is executed in the root of a Truffle compilation.static booleanReturns a boolean value indicating whether the method is executed in the compiled code.static booleanReturns a boolean value indicating whether the method is executed in the interpreter.static booleaninjectBranchProbability(double probability, boolean condition) Injects a probability for the given condition into the probability information of the immediately succeeding branch instruction for the condition.static voidinterpreterOnly(Runnable runnable) Directive for the compiler that the given runnable should only be executed in the interpreter and ignored in the compiled code.static <T> TinterpreterOnly(Callable<T> callable) Directive for the compiler that the given callable should only be executed in the interpreter.static booleanisCompilationConstant(Object value) Returns a boolean indicating whether or not a given value is seen as constant in optimized code.static booleanChecks the given object to the exact class represented byclazz.static booleanReturns a boolean indicating whether a given value is seen as constant during the initial partial evaluation phase.static voidmaterialize(Object obj) Ensures that the given object is not virtual, i.e., not removed by Escape Analysis at the point of this call.static RuntimeExceptionIndicates a code path that is not supposed to be reached during compilation or interpretation.static RuntimeExceptionshouldNotReachHere(String message) Indicates a code path that is not supposed to be reached during compilation or interpretation.static RuntimeExceptionshouldNotReachHere(String message, Throwable cause) Indicates a code path that is not supposed to be reached during compilation or interpretation.static RuntimeExceptionshouldNotReachHere(Throwable cause) Indicates a code path that is not supposed to be reached during compilation or interpretation.static voidDirective for the compiler to discontinue compilation at this code position and instead insert a transfer to the interpreter.static voidDirective for the compiler to discontinue compilation at this code position and instead insert a transfer to the interpreter, invalidating the currently executing machine code.
-
Field Details
-
LIKELY_PROBABILITY
public static final double LIKELY_PROBABILITY- Since:
- 0.8 or earlier
- See Also:
-
UNLIKELY_PROBABILITY
public static final double UNLIKELY_PROBABILITY- Since:
- 0.8 or earlier
- See Also:
-
SLOWPATH_PROBABILITY
public static final double SLOWPATH_PROBABILITY- Since:
- 0.8 or earlier
- See Also:
-
FASTPATH_PROBABILITY
public static final double FASTPATH_PROBABILITY- Since:
- 0.8 or earlier
- See Also:
-
-
Method Details
-
transferToInterpreter
public static void transferToInterpreter()Directive for the compiler to discontinue compilation at this code position and instead insert a transfer to the interpreter.- Since:
- 0.8 or earlier
-
transferToInterpreterAndInvalidate
public static void transferToInterpreterAndInvalidate()Directive for the compiler to discontinue compilation at this code position and instead insert a transfer to the interpreter, invalidating the currently executing machine code.- Since:
- 0.8 or earlier
-
inInterpreter
public static boolean inInterpreter()Returns a boolean value indicating whether the method is executed in the interpreter.- Returns:
truewhen executed in the interpreter,falsein compiled code.- Since:
- 0.8 or earlier
-
hasNextTier
public static boolean hasNextTier()Returns a boolean value indicating whether the method is executed in a compilation tier which can be replaced with a higher tier (e.g. interpretation tier can be replaced by a first tier, a first tier by a second tier compilation).- Returns:
truewhen executed in the first tier Truffle compilation,falseotherwise.- Since:
- 22.0.0
-
inCompiledCode
public static boolean inCompiledCode()Returns a boolean value indicating whether the method is executed in the compiled code.- Returns:
falsewhen executed in the interpreter,truein compiled code.- Since:
- 0.8 or earlier
-
inCompilationRoot
public static boolean inCompilationRoot()Returns a boolean value indicating whether the method is executed in the root of a Truffle compilation.- Returns:
falsewhen executed in the interpreter or in an inlinedCallTarget,truewhen in non-inlined compiled code.- Since:
- 0.28 or earlier
-
isCompilationConstant
Returns a boolean indicating whether or not a given value is seen as constant in optimized code. If this method is called in the interpreter this method will always returntrue. Note that optimizations that a compiler will apply to code that is conditional onisCompilationConstantmay be limited. For this reasonisCompilationConstantis not recommended for use to select between alternate implementations of functionality depending on whether a value is constant. Instead, it is intended for use as a diagnostic mechanism.- Parameters:
value-- Returns:
truewhen given value is seen as compilation constant,falseif not compilation constant.- Since:
- 0.8 or earlier
-
isPartialEvaluationConstant
Returns a boolean indicating whether a given value is seen as constant during the initial partial evaluation phase. If this method is called in the interpreter this method will always returntrue.Because of that one should consider if the code guarded by this check should be run in interpreter or not. If it's faster to not run the guarded code in interpreter one can use:
but beware that code is never run in interpreter and so might lead to subtle bugs and is harder to debug. If it does not matter much whether the code runs in interpreter it's best to just:if (CompilerDirectives.inCompiledCode() && CompilerDirectives.isPartialEvaluationConstant(value)) { // logic only run in compiled code if value is a PE constant }if (CompilerDirectives.isPartialEvaluationConstant(value)) { // logic run in interpreter and in compiled code if value is a PE constant }- Parameters:
value-- Returns:
truewhen given value is seen as compilation constant or in interpreter,falseotherwise.- Since:
- 0.8 or earlier
-
interpreterOnly
Directive for the compiler that the given runnable should only be executed in the interpreter and ignored in the compiled code.- Parameters:
runnable- the closure that should only be executed in the interpreter- Since:
- 0.8 or earlier
-
interpreterOnly
Directive for the compiler that the given callable should only be executed in the interpreter.- Parameters:
callable- the closure that should only be executed in the interpreter- Returns:
- the result of executing the closure in the interpreter and null in the compiled code
- Throws:
Exception- If the closure throws an exception when executed in the interpreter.- Since:
- 0.8 or earlier
-
injectBranchProbability
public static boolean injectBranchProbability(double probability, boolean condition) Injects a probability for the given condition into the probability information of the immediately succeeding branch instruction for the condition. The probability must be a value between 0.0 and 1.0 (inclusive). The condition should not be a combined condition. Example usage immediately before an if statement (it specifies that the likelihood for a to be greater than b is 90%):if (injectBranchProbability(0.9, a > b)) { // ... }Example usage for a combined condition (it specifies that the likelihood for a to be greater than b is 90% and under the assumption that this is true, the likelihood for a being 0 is 10%):if (injectBranchProbability(0.9, a > b) && injectBranchProbability(0.1, a == 0)) { // ... }There are predefined constants for commonly used probabilities (seeLIKELY_PROBABILITY,UNLIKELY_PROBABILITY,SLOWPATH_PROBABILITY,FASTPATH_PROBABILITY).- Parameters:
probability- the probability value between 0.0 and 1.0 that should be injected- Since:
- 0.8 or earlier
-
bailout
Bails out of a compilation (e.g., for guest language features that should never be compiled).- Parameters:
reason- the reason for the bailout- Since:
- 0.8 or earlier
-
materialize
Ensures that the given object is not virtual, i.e., not removed by Escape Analysis at the point of this call.- Parameters:
obj- the object to exclude from Escape Analysis- Since:
- 0.8 or earlier
-
ensureAllocatedHere
public static <T> T ensureAllocatedHere(T object) Prevents the compiler from moving an allocation, enabling preciseOutOfMemoryErrorexception locations. This intrinsic ensures the allocation's immobility and guarantees that the compiler's optimizations do not eliminate the allocation. Imposing such a restriction can hinder certain compiler optimizations, potentially degrading performance. If the intrinsic does not find an allocation as its argument, it causes a runtime compilation failure, invokingbailout(String). Example usage:int[] tryAllocateArray() { int[] array; try { array = CompilerDirectives.ensureAllocatedHere(new int[42]); } catch (OutOfMemoryError e) { // handle out of memory errors to maintain consistency array = null; } return array; }- Since:
- 24.1.0
-
ensureVirtualized
Ensures that the given object will be virtual (escape analyzed) at all points that are dominated by the current position.- Since:
- 0.8 or earlier
-
ensureVirtualizedHere
Ensures that the given object will be virtual at the current position.- Since:
- 0.8 or earlier
-
blackhole
public static void blackhole(boolean value) Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused. This should only be used for benchmarking purposes.- Since:
- 21.2
-
blackhole
public static void blackhole(byte value) Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused. This should only be used for benchmarking purposes.- Since:
- 21.2
-
blackhole
public static void blackhole(short value) Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused. This should only be used for benchmarking purposes.- Since:
- 21.2
-
blackhole
public static void blackhole(char value) Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused. This should only be used for benchmarking purposes.- Since:
- 21.2
-
blackhole
public static void blackhole(int value) Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused. This should only be used for benchmarking purposes.- Since:
- 21.2
-
blackhole
public static void blackhole(long value) Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused. This should only be used for benchmarking purposes.- Since:
- 21.2
-
blackhole
public static void blackhole(float value) Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused. This should only be used for benchmarking purposes.- Since:
- 21.2
-
blackhole
public static void blackhole(double value) Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused. This should only be used for benchmarking purposes.- Since:
- 21.2
-
blackhole
Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused. This should only be used for benchmarking purposes.- Since:
- 21.2
-
castExact
Casts the given object to the exact class represented byclazz. The cast succeeds only ifobject == null || object.getClass() == clazzand thus fails for any subclass.- Parameters:
object- the object to be castclazz- the class to check against, must not be null- Returns:
- the object after casting
- Throws:
ClassCastException- if the object is non-null and not exactly of the given classNullPointerException- if the class argument is null- Since:
- 0.33
- See Also:
-
isExact
Checks the given object to the exact class represented byclazz. The method returnstrueonly ifobject != null && object.getClass() == clazzand thus fails for any subclass.- Parameters:
object- the object to be castclazz- the class to check against, must not be null- Throws:
NullPointerException- if the class argument is null- Since:
- 21.1
-
shouldNotReachHere
Indicates a code path that is not supposed to be reached during compilation or interpretation. Reaching this method is considered a fatal internal error and execution should not continue. Transfers to interpreter andinvalidatesthe compiled code and always throws anAssertionErrorwhen invoked.This method returns a runtime exception to be conveniently used in combination with Java throw statements, for example:
if (expectedCondition) { return 42; } else { throw shouldNotReachHere(); }- Since:
- 20.2
-
shouldNotReachHere
Indicates a code path that is not supposed to be reached during compilation or interpretation. Reaching this method is considered a fatal internal error and execution should not continue. Transfers to interpreter andinvalidatesthe compiled code and always throws anAssertionErrorwhen invoked.This method returns a runtime exception to be conveniently used in combination with Java throw statements, for example:
if (expectedCondition) { return 42; } else { throw shouldNotReachHere("Additional message"); }- Parameters:
message- an additional message for the exception thrown.- Since:
- 20.2
-
shouldNotReachHere
Indicates a code path that is not supposed to be reached during compilation or interpretation. Reaching this method is considered a fatal internal error and execution should not continue. Transfers to interpreter andinvalidatesthe compiled code and always throws anAssertionErrorwhen invoked.This method returns a runtime exception to be conveniently used in combination with Java throw statements, for example:
if (expectedCondition) { return 42; } else { throw shouldNotReachHere("Additional message"); }- Parameters:
cause- the cause if an exception was responsible for the unexpected case.- Since:
- 20.2
-
shouldNotReachHere
Indicates a code path that is not supposed to be reached during compilation or interpretation. Reaching this method is considered a fatal internal error and execution should not continue. Transfers to interpreter andinvalidatesthe compiled code and always throws anAssertionErrorwhen invoked.This method returns a runtime exception to be conveniently used in combination with Java throw statements, for example:
if (expectedCondition) { return 42; } else { throw shouldNotReachHere("Additional message"); }- Parameters:
message- an additional message for the exception thrown.cause- the cause if an exception was responsible for the unexpected case.- Since:
- 20.2
-