oracle/graal · error · GraalError

A negative probability of {} is not allowed!

Error message

A negative probability of {} is not allowed!

What it means

SwitchNode's simplification (SwitchNode.java:341) validates that every constant case probability attached to a switch node is in [0.0, 1.0]. This error means one CaseProbabilityNode evaluated to a constant double less than 0.0, which is meaningless as a branch probability. Graal throws it because downstream phases assume probabilities are valid ratios when distributing node probabilities over successors.

Source

Thrown at compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/extended/SwitchNode.java:341

                succ = next;
            }
            assertTrue(succ.next() instanceof SwitchCaseProbabilityNode,
                            "Cannot inject switch probability, since key successor %s is not a SwitchCaseProbabilityNode",
                            succ.next());
            SwitchCaseProbabilityNode caseProbabilityNode = (SwitchCaseProbabilityNode) succ.next();

            ValueNode probabilityNode = caseProbabilityNode.getProbability();
            if (!probabilityNode.isConstant()) {
                /*
                 * If any of the probabilities are not constant we bail out of simplification, which
                 * will cause compilation to fail later during lowering since the node will be left
                 * behind
                 */
                return;
            }
            double probabilityValue = probabilityNode.asJavaConstant().asDouble();
            if (probabilityValue < 0.0) {
                throw new GraalError("A negative probability of " + probabilityValue + " is not allowed!");
            } else if (probabilityValue > 1.0) {
                throw new GraalError("A probability of more than 1.0 (" + probabilityValue + ") is not allowed!");
            } else if (Double.isNaN(probabilityValue)) {
                /*
                 * We allow NaN if the node is in unreachable code that will eventually fall away,
                 * or else an error will be thrown during lowering since we keep the node around.
                 * See analogous case in BranchProbabilityNode.
                 */
                return;
            }
            nodeProbabilities[i] = probabilityValue / numKeysPerBlock[keySuccessorIndex(i)];
        }

        for (AbstractBeginNode blockSuccessor : successors) {
            AbstractBeginNode succ = blockSuccessor;
            while (succ.next() instanceof AbstractBeginNode next) {
                succ = next;
            }

View on GitHub (pinned to a66e9ccd1d)

Solutions

  1. Fix the caller that builds the probabilities array so every entry is within [0.0, 1.0]
  2. If the values are relative weights, normalize them by dividing by the total weight before passing them to SwitchNode.create
  3. Add an assert in your code that validates the array before switch creation to catch the bug at the source

Example fix

// before
double[] probs = {2.0, -1.0, 3.0};
SwitchNode.create(..., probs, ...);

// after
double total = 6.0;
double[] probs = {2.0 / total, 1.0 / total, 3.0 / total}; // all in [0,1]
SwitchNode.create(..., probs, ...);
Defensive patterns

Strategy: validation

Validate before calling

static double[] checkProbabilities(double[] probs) {
    for (double p : probs) {
        if (p < 0.0 || p > 1.0) throw new IllegalArgumentException("probability out of range: " + p);
    }
    return probs;
}

Prevention

When it happens

Trigger: Creating a switch with explicit case probabilities (SwitchNode.create(...) or IntegerSwitchNode constructor with a double[] probabilities argument) where at least one entry is a negative constant. The value is only checked when the probability node is constant during simplify().

Common situations: Hand-written snippets or test graphs that pass raw profile counts or differences instead of normalized ratios; a scaling/normalization bug in code that computes probabilities (e.g., subtracting weights can yield negatives).

Related errors


AI-assisted analysis of oracle/graal@a66e9ccd1d (2026-08-14). Data as JSON: /api/errors/7100c6230e07feab. Report an issue: GitHub.