|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.truffle.api.bytecode.test; |
| 42 | + |
| 43 | +import static org.junit.Assert.assertEquals; |
| 44 | + |
| 45 | +import org.junit.Test; |
| 46 | + |
| 47 | +import com.oracle.truffle.api.bytecode.BytecodeConfig; |
| 48 | +import com.oracle.truffle.api.bytecode.BytecodeParser; |
| 49 | +import com.oracle.truffle.api.bytecode.BytecodeRootNode; |
| 50 | +import com.oracle.truffle.api.bytecode.BytecodeRootNodes; |
| 51 | +import com.oracle.truffle.api.bytecode.BytecodeTier; |
| 52 | +import com.oracle.truffle.api.bytecode.ContinuationResult; |
| 53 | +import com.oracle.truffle.api.bytecode.ContinuationRootNode; |
| 54 | +import com.oracle.truffle.api.bytecode.GenerateBytecode; |
| 55 | +import com.oracle.truffle.api.bytecode.Operation; |
| 56 | +import com.oracle.truffle.api.dsl.Specialization; |
| 57 | +import com.oracle.truffle.api.frame.FrameDescriptor; |
| 58 | +import com.oracle.truffle.api.nodes.RootNode; |
| 59 | + |
| 60 | +public class RootNodeOverridesTest { |
| 61 | + |
| 62 | + private static final BytecodeDSLTestLanguage LANGUAGE = null; |
| 63 | + |
| 64 | + private static RootNodeWithOverrides parseRootNodeWithOverrides(BytecodeParser<RootNodeWithOverridesGen.Builder> builder) { |
| 65 | + BytecodeRootNodes<RootNodeWithOverrides> nodes = RootNodeWithOverridesGen.create(LANGUAGE, BytecodeConfig.DEFAULT, builder); |
| 66 | + return nodes.getNode(0); |
| 67 | + } |
| 68 | + |
| 69 | + private static RootNodeWithParentOverrides parseRootNodeWithParentOverrides(BytecodeParser<RootNodeWithParentOverridesGen.Builder> builder) { |
| 70 | + BytecodeRootNodes<RootNodeWithParentOverrides> nodes = RootNodeWithParentOverridesGen.create(LANGUAGE, BytecodeConfig.DEFAULT, builder); |
| 71 | + return nodes.getNode(0); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testPrepareForCompilation() { |
| 76 | + RootNodeWithOverrides root = parseRootNodeWithOverrides(b -> { |
| 77 | + b.beginRoot(); |
| 78 | + b.beginReturn(); |
| 79 | + b.emitLoadConstant(42L); |
| 80 | + b.endReturn(); |
| 81 | + b.endRoot(); |
| 82 | + }); |
| 83 | + |
| 84 | + // As long as the interpreter is uncached, it should not be ready. |
| 85 | + assertEquals(BytecodeTier.UNCACHED, root.getBytecodeNode().getTier()); |
| 86 | + root.readyForCompilation = false; |
| 87 | + assertEquals(false, root.prepareForCompilation(true, 2, true)); |
| 88 | + root.readyForCompilation = true; |
| 89 | + assertEquals(false, root.prepareForCompilation(true, 2, true)); |
| 90 | + |
| 91 | + // Transition to cached. |
| 92 | + root.getBytecodeNode().setUncachedThreshold(0); |
| 93 | + root.getCallTarget().call(); |
| 94 | + assertEquals(BytecodeTier.CACHED, root.getBytecodeNode().getTier()); |
| 95 | + |
| 96 | + // When the interpreter is cached, the parent impl should be delegated to. |
| 97 | + root.readyForCompilation = false; |
| 98 | + assertEquals(false, root.prepareForCompilation(true, 2, true)); |
| 99 | + root.readyForCompilation = true; |
| 100 | + assertEquals(true, root.prepareForCompilation(true, 2, true)); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testPrepareForCompilationContinuation() { |
| 105 | + RootNodeWithOverrides root = parseRootNodeWithOverrides(b -> { |
| 106 | + b.beginRoot(); |
| 107 | + b.beginYield(); |
| 108 | + b.emitLoadConstant(42L); |
| 109 | + b.endYield(); |
| 110 | + b.beginReturn(); |
| 111 | + b.emitLoadConstant(42L); |
| 112 | + b.endReturn(); |
| 113 | + b.endRoot(); |
| 114 | + }); |
| 115 | + |
| 116 | + // As long as the interpreter is uncached, it should not be ready. |
| 117 | + ContinuationResult cont = (ContinuationResult) root.getCallTarget().call(); |
| 118 | + assertEquals(BytecodeTier.UNCACHED, root.getBytecodeNode().getTier()); |
| 119 | + root.readyForCompilation = false; |
| 120 | + assertEquals(false, invokePrepareForCompilation(cont.getContinuationRootNode())); |
| 121 | + root.readyForCompilation = true; |
| 122 | + assertEquals(false, invokePrepareForCompilation(cont.getContinuationRootNode())); |
| 123 | + |
| 124 | + // Transition to cached. |
| 125 | + root.getBytecodeNode().setUncachedThreshold(0); |
| 126 | + cont = (ContinuationResult) root.getCallTarget().call(); |
| 127 | + assertEquals(BytecodeTier.CACHED, root.getBytecodeNode().getTier()); |
| 128 | + |
| 129 | + // The continuation root does not transition to cached until its next execution. |
| 130 | + // Until then, it should not be ready for compilation. |
| 131 | + assertEquals(BytecodeTier.UNCACHED, cont.getBytecodeLocation().getBytecodeNode().getTier()); |
| 132 | + root.readyForCompilation = false; |
| 133 | + assertEquals(false, invokePrepareForCompilation(cont.getContinuationRootNode())); |
| 134 | + root.readyForCompilation = true; |
| 135 | + assertEquals(false, invokePrepareForCompilation(cont.getContinuationRootNode())); |
| 136 | + |
| 137 | + // After the continuation root transitions to cached, delegate to the root node. |
| 138 | + cont.continueWith(123L); |
| 139 | + assertEquals(BytecodeTier.CACHED, cont.getBytecodeLocation().getBytecodeNode().getTier()); |
| 140 | + root.readyForCompilation = false; |
| 141 | + assertEquals(false, invokePrepareForCompilation(cont.getContinuationRootNode())); |
| 142 | + root.readyForCompilation = true; |
| 143 | + assertEquals(true, invokePrepareForCompilation(cont.getContinuationRootNode())); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testPrepareForCompilationContinuationParentOverrides() { |
| 148 | + RootNodeWithParentOverrides root = parseRootNodeWithParentOverrides(b -> { |
| 149 | + b.beginRoot(); |
| 150 | + b.beginYield(); |
| 151 | + b.emitLoadConstant(42L); |
| 152 | + b.endYield(); |
| 153 | + b.beginReturn(); |
| 154 | + b.emitLoadConstant(42L); |
| 155 | + b.endReturn(); |
| 156 | + b.endRoot(); |
| 157 | + }); |
| 158 | + |
| 159 | + ContinuationResult cont = (ContinuationResult) root.getCallTarget().call(); |
| 160 | + root.readyForCompilation = false; |
| 161 | + assertEquals(false, invokePrepareForCompilation(cont.getContinuationRootNode())); |
| 162 | + root.readyForCompilation = true; |
| 163 | + assertEquals(true, invokePrepareForCompilation(cont.getContinuationRootNode())); |
| 164 | + } |
| 165 | + |
| 166 | + private static boolean invokePrepareForCompilation(ContinuationRootNode cont) { |
| 167 | + try { |
| 168 | + return (boolean) cont.getClass().getMethod("prepareForCompilation", boolean.class, int.class, boolean.class).invoke(cont, true, 2, true); |
| 169 | + } catch (ReflectiveOperationException ex) { |
| 170 | + throw new AssertionError("Exception invoking prepareForCompilation", ex); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + @GenerateBytecode(languageClass = BytecodeDSLTestLanguage.class, enableYield = true, enableUncachedInterpreter = true) |
| 175 | + abstract static class RootNodeWithOverrides extends RootNode implements BytecodeRootNode { |
| 176 | + public boolean readyForCompilation = false; |
| 177 | + |
| 178 | + protected RootNodeWithOverrides(BytecodeDSLTestLanguage language, FrameDescriptor frameDescriptor) { |
| 179 | + super(language, frameDescriptor); |
| 180 | + } |
| 181 | + |
| 182 | + @Operation |
| 183 | + public static final class Add { |
| 184 | + @Specialization |
| 185 | + static int add(int x, int y) { |
| 186 | + return x + y; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + // The generated code should delegate to this method. |
| 191 | + @Override |
| 192 | + public boolean prepareForCompilation(boolean rootCompilation, int compilationTier, boolean lastTier) { |
| 193 | + return readyForCompilation; |
| 194 | + } |
| 195 | + |
| 196 | + } |
| 197 | + |
| 198 | + @GenerateBytecode(languageClass = BytecodeDSLTestLanguage.class, enableYield = true) |
| 199 | + abstract static class RootNodeWithParentOverrides extends ParentClassWithOverride implements BytecodeRootNode { |
| 200 | + |
| 201 | + protected RootNodeWithParentOverrides(BytecodeDSLTestLanguage language, FrameDescriptor frameDescriptor) { |
| 202 | + super(language, frameDescriptor); |
| 203 | + } |
| 204 | + |
| 205 | + @Operation |
| 206 | + public static final class Add { |
| 207 | + @Specialization |
| 208 | + static int add(int x, int y) { |
| 209 | + return x + y; |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + // This root node has no uncached interpreter, so there should be no override. The |
| 214 | + // continuation root node should still delegate to the parent implementation. |
| 215 | + } |
| 216 | + |
| 217 | + abstract static class ParentClassWithOverride extends RootNode { |
| 218 | + public boolean readyForCompilation = false; |
| 219 | + |
| 220 | + protected ParentClassWithOverride(BytecodeDSLTestLanguage language, FrameDescriptor frameDescriptor) { |
| 221 | + super(language, frameDescriptor); |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + public boolean prepareForCompilation(boolean rootCompilation, int compilationTier, boolean lastTier) { |
| 226 | + return readyForCompilation; |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | +} |
0 commit comments