Skip to content

Commit a22eac8

Browse files
committed
[GR-48662] Store PolyglotThreadInfo#contextThreadLocals directly in the fast thread locals array.
PullRequest: graal/15594
2 parents 55b8e06 + 29db5d3 commit a22eac8

File tree

7 files changed

+30
-32
lines changed

7 files changed

+30
-32
lines changed

compiler/mx.compiler/mx_graal_benchmark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ def whitebox_dependency(dist):
526526
def filter_distribution(self, dist):
527527
return super(JMHDistWhiteboxBenchmarkSuite, self).filter_distribution(dist) and \
528528
any(JMHDistWhiteboxBenchmarkSuite.whitebox_dependency(dist)) and \
529-
not any(dep.name.startswith('com.oracle.truffle.enterprise.dispatch.jmh') for dep in dist.deps)
529+
not any(dep.name.startswith('com.oracle.truffle.enterprise.jmh') for dep in dist.deps)
530530

531531

532532
def extraVmArgs(self):

truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/AbstractFastThreadLocal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ protected AbstractFastThreadLocal() {
4949

5050
public abstract <C> Object[] get();
5151

52-
public abstract <C> C fastGet(int index, Class<C> castType, boolean invalidateOnNull);
52+
public abstract <C> C fastGet(int index, Class<C> castType, boolean invalidateOnNull, boolean nonNullResult);
5353

5454
}

truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/DefaultContextThreadLocal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public <C> Object[] get() {
6868
@Override
6969
@SuppressWarnings("unchecked")
7070
@TruffleBoundary
71-
public <C> C fastGet(int index, Class<C> castType, boolean invalidateOnNull) {
71+
public <C> C fastGet(int index, Class<C> castType, boolean invalidateOnNull, boolean nonNullResult) {
7272
Object[] data = get();
7373
if (data == null) {
7474
return null;

truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotFastThreadLocals.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ final class PolyglotFastThreadLocals {
7474
private static final FinalIntMap LANGUAGE_INDEXES = new FinalIntMap();
7575
private static final int RESERVED_NULL = -1; // never set
7676
private static final int THREAD_INDEX = 0;
77-
static final int CONTEXT_INDEX = 1;
78-
private static final int ENCAPSULATING_NODE_REFERENCE_INDEX = 2;
79-
private static final int LANGUAGE_START = 3;
77+
static final int CONTEXT_THREAD_LOCALS_INDEX = 1;
78+
static final int CONTEXT_INDEX = 2;
79+
private static final int ENCAPSULATING_NODE_REFERENCE_INDEX = 3;
80+
private static final int LANGUAGE_START = 4;
8081

8182
static final int LANGUAGE_CONTEXT_OFFSET = 0;
8283
static final int LANGUAGE_SPI_OFFSET = 1;
@@ -92,6 +93,7 @@ static Object[] createFastThreadLocals(PolyglotThreadInfo thread) {
9293
assert Thread.holdsLock(context);
9394
Object[] data = createEmptyData(thread.context.engine);
9495
data[THREAD_INDEX] = thread;
96+
data[CONTEXT_THREAD_LOCALS_INDEX] = thread.contextThreadLocals;
9597
data[CONTEXT_INDEX] = thread.context;
9698
data[ENCAPSULATING_NODE_REFERENCE_INDEX] = EngineAccessor.NODES.createEncapsulatingNodeReference(thread.getThread());
9799
for (PolyglotLanguageContext languageContext : thread.context.contexts) {
@@ -105,6 +107,7 @@ static Object[] createFastThreadLocals(PolyglotThreadInfo thread) {
105107
private static Object[] createFastThreadLocalsForLanguage(PolyglotLanguageInstance instance) {
106108
Object[] data = createEmptyData(instance.language.engine);
107109
data[THREAD_INDEX] = null; // not available if only engine is entered
110+
data[CONTEXT_THREAD_LOCALS_INDEX] = null; // not available if only engine is entered
108111
data[CONTEXT_INDEX] = null; // not available if only engine is entered
109112

110113
// we take the first language we find. should we fail maybe if there is more than one?
@@ -142,7 +145,7 @@ public static <C> ContextReference<C> createContextReference(Class<? extends Tru
142145
}
143146

144147
public static boolean needsEnter(PolyglotContextImpl context) {
145-
return IMPL.fastGet(CONTEXT_INDEX, PolyglotContextImpl.class, false) != context;
148+
return IMPL.fastGet(CONTEXT_INDEX, PolyglotContextImpl.class, false, false) != context;
146149
}
147150

148151
public static Object[] enter(PolyglotThreadInfo threadInfo) {
@@ -195,33 +198,33 @@ static EncapsulatingNodeReference getEncapsulatingNodeReference(boolean invalida
195198
* instead. So we do not bother here and trade a bit smaller code for fewer deoptimizations
196199
* and less footprint (no assumptions in use).
197200
*/
198-
return IMPL.fastGet(ENCAPSULATING_NODE_REFERENCE_INDEX, EncapsulatingNodeReference.class, invalidateOnNull);
201+
return IMPL.fastGet(ENCAPSULATING_NODE_REFERENCE_INDEX, EncapsulatingNodeReference.class, invalidateOnNull, false);
199202
}
200203

201-
public static PolyglotThreadInfo getCurrentThread(PolyglotSharingLayer layer) {
204+
public static Object[] getCurrentThreadContextThreadLocals(PolyglotSharingLayer layer) {
202205
if (CompilerDirectives.inCompiledCode() && layer != null) {
203206
PolyglotContextImpl singleContext = layer.getSingleConstantContext();
204207
if (singleContext != null && CompilerDirectives.isPartialEvaluationConstant(singleContext)) {
205208
PolyglotThreadInfo constantThread = singleContext.singleThreadValue.getConstant();
206209
if (constantThread != null) {
207-
return constantThread;
210+
return constantThread.getThreadLocals(layer.engine);
208211
}
209212
}
210213
}
211-
return IMPL.fastGet(THREAD_INDEX, PolyglotThreadInfo.class, true);
214+
return IMPL.fastGet(CONTEXT_THREAD_LOCALS_INDEX, Object[].class, true, true);
212215
}
213216

214-
public static PolyglotThreadInfo getCurrentThreadEngine(PolyglotEngineImpl engine) {
217+
public static Object[] getCurrentThreadContextThreadLocalsEngine(PolyglotEngineImpl engine) {
215218
if (CompilerDirectives.inCompiledCode() && engine != null) {
216219
PolyglotContextImpl singleContext = engine.singleContextValue.getConstant();
217220
if (singleContext != null) {
218221
PolyglotThreadInfo constantThread = singleContext.singleThreadValue.getConstant();
219222
if (constantThread != null) {
220-
return constantThread;
223+
return constantThread.getThreadLocals(engine);
221224
}
222225
}
223226
}
224-
return IMPL.fastGet(THREAD_INDEX, PolyglotThreadInfo.class, true);
227+
return IMPL.fastGet(CONTEXT_THREAD_LOCALS_INDEX, Object[].class, true, true);
225228
}
226229

227230
public static PolyglotContextImpl getContext(PolyglotSharingLayer layer) {
@@ -231,7 +234,7 @@ public static PolyglotContextImpl getContext(PolyglotSharingLayer layer) {
231234
return value;
232235
}
233236
}
234-
return IMPL.fastGet(CONTEXT_INDEX, PolyglotContextImpl.class, true);
237+
return IMPL.fastGet(CONTEXT_INDEX, PolyglotContextImpl.class, true, false);
235238
}
236239

237240
public static PolyglotContextImpl getContextWithEngine(PolyglotEngineImpl engine) {
@@ -241,7 +244,7 @@ public static PolyglotContextImpl getContextWithEngine(PolyglotEngineImpl engine
241244
return context;
242245
}
243246
}
244-
return IMPL.fastGet(CONTEXT_INDEX, PolyglotContextImpl.class, true);
247+
return IMPL.fastGet(CONTEXT_INDEX, PolyglotContextImpl.class, true, false);
245248
}
246249

247250
public static PolyglotContextImpl getContextWithNode(Node node) {
@@ -251,7 +254,7 @@ public static PolyglotContextImpl getContextWithNode(Node node) {
251254
return layer.getSingleConstantContext();
252255
}
253256
}
254-
return IMPL.fastGet(CONTEXT_INDEX, PolyglotContextImpl.class, true);
257+
return IMPL.fastGet(CONTEXT_INDEX, PolyglotContextImpl.class, true, false);
255258
}
256259

257260
@SuppressWarnings("unchecked")
@@ -263,7 +266,7 @@ public static TruffleLanguage<Object> getLanguage(Node node, int index, Class<?>
263266
return instance.spi;
264267
}
265268
}
266-
return (TruffleLanguage<Object>) IMPL.fastGet(index, languageClass, true);
269+
return (TruffleLanguage<Object>) IMPL.fastGet(index, languageClass, true, false);
267270
}
268271

269272
public static Object getLanguageContext(Node node, int index) {
@@ -279,7 +282,7 @@ public static Object getLanguageContext(Node node, int index) {
279282
}
280283
contextClass = findContextClass(node, index);
281284
}
282-
return IMPL.fastGet(index, contextClass, true);
285+
return IMPL.fastGet(index, contextClass, true, false);
283286
}
284287

285288
private static boolean validSharing(Node node) {

truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotLocals.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ void initializeLanguageInstance(PolyglotLanguageInstance instance) {
366366
@Override
367367
public T get() {
368368
assert assertLanguageCreated(PolyglotFastThreadLocals.getContext(null), languageInstance.language);
369-
return (T) PolyglotFastThreadLocals.getCurrentThread(sharingLayer).getThreadLocal(location);
369+
return (T) location.readLocal(null, PolyglotFastThreadLocals.getCurrentThreadContextThreadLocals(sharingLayer), true);
370370
}
371371

372372
@SuppressWarnings("unchecked")
@@ -441,7 +441,7 @@ protected InstrumentContextThreadLocal(Object factory) {
441441
@Override
442442
public T get() {
443443
assert assertInstrumentCreated(PolyglotFastThreadLocals.getContext(null), instrument);
444-
return (T) PolyglotFastThreadLocals.getCurrentThreadEngine(location.engine).getThreadLocal(location);
444+
return (T) location.readLocal(null, PolyglotFastThreadLocals.getCurrentThreadContextThreadLocalsEngine(location.engine), true);
445445
}
446446

447447
@SuppressWarnings("unchecked")
@@ -515,14 +515,14 @@ final Object invokeFactory(PolyglotContextImpl context, Thread thread) {
515515
}
516516

517517
final Object readLocal(PolyglotContextImpl context, Object[] locals, boolean threadLocal) {
518-
assert locals != null && index < locals.length && locals[index] != null : invalidLocalMessage(context, locals);
518+
assert locals != null && index < locals.length && locals[index] != null : invalidLocalMessage(context == null ? PolyglotFastThreadLocals.getContext(null) : context, locals);
519519
Object result;
520520
if (CompilerDirectives.inCompiledCode() && CompilerDirectives.isPartialEvaluationConstant(this)) {
521521
result = readLocalFast(locals, threadLocal);
522522
} else {
523523
result = locals[index];
524524
}
525-
assert result.getClass() == profiledType : invalidLocalMessage(context, locals);
525+
assert result.getClass() == profiledType : invalidLocalMessage(context == null ? PolyglotFastThreadLocals.getContext(null) : context, locals);
526526
return result;
527527
}
528528

truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotThreadInfo.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import com.oracle.truffle.api.nodes.EncapsulatingNodeReference;
5555
import com.oracle.truffle.api.nodes.Node;
5656
import com.oracle.truffle.api.utilities.TruffleWeakReference;
57-
import com.oracle.truffle.polyglot.PolyglotLocals.LocalLocation;
5857

5958
final class PolyglotThreadInfo {
6059

@@ -172,6 +171,7 @@ public Object[] getContextThreadLocals() {
172171
public void setContextThreadLocals(Object[] contextThreadLocals) {
173172
assert Thread.holdsLock(context);
174173
this.contextThreadLocals = contextThreadLocals;
174+
this.fastThreadLocals[PolyglotFastThreadLocals.CONTEXT_THREAD_LOCALS_INDEX] = contextThreadLocals;
175175
}
176176

177177
boolean isCurrent() {
@@ -254,12 +254,7 @@ void notifyLeave(PolyglotEngineImpl engine, PolyglotContextImpl profiledContext)
254254
}
255255
}
256256

257-
Object getThreadLocal(LocalLocation l) {
258-
// thread id is guaranteed to be unique
259-
return l.readLocal(this.context, getThreadLocals(l.engine), true);
260-
}
261-
262-
private Object[] getThreadLocals(PolyglotEngineImpl e) {
257+
Object[] getThreadLocals(PolyglotEngineImpl e) {
263258
CompilerAsserts.partialEvaluationConstant(e);
264259
Object[] locals = this.contextThreadLocals;
265260
assert locals != null : "thread local not initialized.";

truffle/src/com.oracle.truffle.runtime/src/com/oracle/truffle/runtime/OptimizedFastThreadLocal.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected OptimizedFastThreadLocal() {
6161

6262
@Override
6363
@SuppressWarnings("unchecked")
64-
public final <C> C fastGet(int index, Class<C> castType, boolean invalidateOnNull) {
64+
public final <C> C fastGet(int index, Class<C> castType, boolean invalidateOnNull, boolean nonNullResult) {
6565
Object[] data;
6666
if (inCompiledCode()) {
6767
partialEvaluationConstant(index);
@@ -74,7 +74,7 @@ public final <C> C fastGet(int index, Class<C> castType, boolean invalidateOnNul
7474
return null;
7575
}
7676
Object v = castArrayFixedLength(array, index + 1)[index];
77-
C result = unsafeCast(v, castType, true, false, true);
77+
C result = unsafeCast(v, castType, true, nonNullResult, true);
7878
return result;
7979
} else {
8080
data = get();

0 commit comments

Comments
 (0)