Package com.oracle.truffle.api
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
Modifier and TypeClassDescriptionstatic @interface
Marks fields that should be considered final for a Truffle compilation although they are not final while executing in the interpreter.static @interface
Marks a method that it is considered as a boundary for Truffle partial evaluation.static @interface
Marks classes as value types. -
Field Summary
Modifier and TypeFieldDescriptionstatic final double
static final double
static final double
static final double
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
Bails out of a compilation (e.g., for guest language features that should never be compiled).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.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.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.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.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.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.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.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.static void
Consume a value, making sure the compiler doesn't optimize away the computation of this value, even if it is otherwise unused.static <T> T
Casts the given object to the exact class represented byclazz
.static <T> T
ensureAllocatedHere
(T object) Prevents the compiler from moving an allocation, enabling preciseOutOfMemoryError
exception locations.static void
ensureVirtualized
(Object object) Ensures that the given object will be virtual (escape analyzed) at all points that are dominated by the current position.static void
ensureVirtualizedHere
(Object object) Ensures that the given object will be virtual at the current position.static boolean
Returns a boolean value indicating whether the method is executed in a compilation tier which can be replaced with a higher tier (e.g.static boolean
Returns a boolean value indicating whether the method is executed in the root of a Truffle compilation.static boolean
Returns a boolean value indicating whether the method is executed in the compiled code.static boolean
Returns a boolean value indicating whether the method is executed in the interpreter.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.static void
interpreterOnly
(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> T
interpreterOnly
(Callable<T> callable) Directive for the compiler that the given callable should only be executed in the interpreter.static boolean
isCompilationConstant
(Object value) Returns a boolean indicating whether or not a given value is seen as constant in optimized code.static boolean
Checks the given object to the exact class represented byclazz
.static boolean
Returns a boolean indicating whether or not a given value is seen as constant during the initial partial evaluation phase.static void
materialize
(Object obj) Ensures that the given object is not virtual, i.e., not removed by Escape Analysis at the point of this call.static RuntimeException
Indicates a code path that is not supposed to be reached during compilation or interpretation.static RuntimeException
shouldNotReachHere
(String message) Indicates a code path that is not supposed to be reached during compilation or interpretation.static RuntimeException
shouldNotReachHere
(String message, Throwable cause) Indicates a code path that is not supposed to be reached during compilation or interpretation.static RuntimeException
shouldNotReachHere
(Throwable cause) Indicates a code path that is not supposed to be reached during compilation or interpretation.static void
Directive for the compiler to discontinue compilation at this code position and instead insert a transfer to the interpreter.static void
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.
-
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:
true
when executed in the interpreter,false
in 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:
true
when executed in the first tier Truffle compilation,false
otherwise.- Since:
- 22.0.0
-
inCompiledCode
public static boolean inCompiledCode()Returns a boolean value indicating whether the method is executed in the compiled code.- Returns:
false
when executed in the interpreter,true
in 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:
false
when executed in the interpreter or in an inlinedCallTarget
,true
when 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 onisCompilationConstant
may be limited. For this reasonisCompilationConstant
is 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:
true
when given value is seen as compilation constant,false
if not compilation constant.- Since:
- 0.8 or earlier
-
isPartialEvaluationConstant
Returns a boolean indicating whether or not 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
.- Parameters:
value
-- Returns:
true
when given value is seen as compilation constant,false
if not compilation constant.- 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 preciseOutOfMemoryError
exception 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() == clazz
and 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 returnstrue
only ifobject != null && object.getClass() == clazz
and 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 andinvalidates
the compiled code and always throws anAssertionError
when 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 andinvalidates
the compiled code and always throws anAssertionError
when 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 andinvalidates
the compiled code and always throws anAssertionError
when 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 andinvalidates
the compiled code and always throws anAssertionError
when 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
-