Skip to content

Commit f1a9a8d

Browse files
committed
8342902: Deduplication of acquire calls in BindingSpecializer causes escape-analyisis failure
Reviewed-by: jvernee
1 parent f1cc890 commit f1a9a8d

File tree

3 files changed

+303
-7
lines changed

3 files changed

+303
-7
lines changed

src/java.base/share/classes/jdk/internal/foreign/abi/BindingSpecializer.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package jdk.internal.foreign.abi;
2626

27+
import java.lang.classfile.Annotation;
2728
import java.lang.classfile.ClassFile;
2829
import java.lang.classfile.CodeBuilder;
2930
import java.lang.classfile.Label;
@@ -46,10 +47,13 @@
4647
import jdk.internal.foreign.abi.Binding.ShiftRight;
4748
import jdk.internal.foreign.abi.Binding.VMLoad;
4849
import jdk.internal.foreign.abi.Binding.VMStore;
50+
import jdk.internal.vm.annotation.ForceInline;
4951
import sun.security.action.GetBooleanAction;
52+
import sun.security.action.GetIntegerAction;
5053
import sun.security.action.GetPropertyAction;
5154

5255
import java.io.IOException;
56+
import java.lang.classfile.attribute.RuntimeVisibleAnnotationsAttribute;
5357
import java.lang.constant.ClassDesc;
5458
import java.lang.constant.ConstantDesc;
5559
import java.lang.constant.DynamicConstantDesc;
@@ -77,6 +81,8 @@ public class BindingSpecializer {
7781
= GetPropertyAction.privilegedGetProperty("jdk.internal.foreign.abi.Specializer.DUMP_CLASSES_DIR");
7882
private static final boolean PERFORM_VERIFICATION
7983
= GetBooleanAction.privilegedGetProperty("jdk.internal.foreign.abi.Specializer.PERFORM_VERIFICATION");
84+
private static final int SCOPE_DEDUP_DEPTH
85+
= GetIntegerAction.privilegedGetProperty("jdk.internal.foreign.abi.Specializer.SCOPE_DEDUP_DEPTH", 2);
8086

8187
// Bunch of helper constants
8288
private static final int CLASSFILE_VERSION = ClassFileFormatVersion.latest().major();
@@ -99,6 +105,7 @@ public class BindingSpecializer {
99105
private static final ClassDesc CD_ValueLayout_OfFloat = referenceClassDesc(ValueLayout.OfFloat.class);
100106
private static final ClassDesc CD_ValueLayout_OfDouble = referenceClassDesc(ValueLayout.OfDouble.class);
101107
private static final ClassDesc CD_AddressLayout = referenceClassDesc(AddressLayout.class);
108+
private static final ClassDesc CD_ForceInline = referenceClassDesc(ForceInline.class);
102109

103110
private static final MethodTypeDesc MTD_NEW_BOUNDED_ARENA = MethodTypeDesc.of(CD_Arena, CD_long);
104111
private static final MethodTypeDesc MTD_NEW_EMPTY_ARENA = MethodTypeDesc.of(CD_Arena);
@@ -196,8 +203,9 @@ private static byte[] specializeHelper(MethodType leafType, MethodType callerMet
196203
clb.withFlags(ACC_PUBLIC + ACC_FINAL + ACC_SUPER)
197204
.withSuperclass(CD_Object)
198205
.withVersion(CLASSFILE_VERSION, 0)
199-
.withMethodBody(METHOD_NAME, methodTypeDesc(callerMethodType), ACC_PUBLIC | ACC_STATIC,
200-
cb -> new BindingSpecializer(cb, callerMethodType, callingSequence, abi, leafType).specialize());
206+
.withMethod(METHOD_NAME, methodTypeDesc(callerMethodType), ACC_PUBLIC | ACC_STATIC,
207+
mb -> mb.with(RuntimeVisibleAnnotationsAttribute.of(Annotation.of(CD_ForceInline)))
208+
.withCode(cb -> new BindingSpecializer(cb, callerMethodType, callingSequence, abi, leafType).specialize()));
201209
});
202210

203211
if (DUMP_CLASSES_DIR != null) {
@@ -502,11 +510,19 @@ private void emitAcquireScope() {
502510

503511
// start with 1 scope to maybe acquire on the stack
504512
assert curScopeLocalIdx != -1;
505-
boolean hasOtherScopes = curScopeLocalIdx != 0;
506-
for (int i = 0; i < curScopeLocalIdx; i++) {
513+
boolean hasLookup = false;
514+
515+
// Here we check if the current scope has not been already acquired.
516+
// To do that, we generate many comparisons (one per cached scope).
517+
// Note that we always skip comparisons against the very first cached scope
518+
// (as that is the function address, which typically belongs to another scope).
519+
// We also stop the comparisons at SCOPE_DEDUP_DEPTH, to keep a lid on the size
520+
// of the generated code.
521+
for (int i = 1; i < curScopeLocalIdx && i <= SCOPE_DEDUP_DEPTH; i++) {
507522
cb.dup() // dup for comparison
508523
.aload(scopeSlots[i])
509524
.if_acmpeq(skipAcquire);
525+
hasLookup = true;
510526
}
511527

512528
// 1 scope to acquire on the stack
@@ -516,10 +532,10 @@ private void emitAcquireScope() {
516532
cb.invokevirtual(CD_MemorySessionImpl, "acquire0", MTD_ACQUIRE0) // call acquire on the other
517533
.astore(nextScopeLocal); // store off one to release later
518534

519-
if (hasOtherScopes) { // avoid ASM generating a bunch of nops for the dead code
535+
if (hasLookup) { // avoid ASM generating a bunch of nops for the dead code
520536
cb.goto_(end)
521-
.labelBinding(skipAcquire)
522-
.pop(); // drop scope
537+
.labelBinding(skipAcquire)
538+
.pop(); // drop scope
523539
}
524540

525541
cb.labelBinding(end);
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
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+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package org.openjdk.bench.java.lang.foreign;
25+
26+
import org.openjdk.jmh.annotations.*;
27+
28+
import java.lang.foreign.*;
29+
import java.lang.invoke.*;
30+
import java.util.concurrent.TimeUnit;
31+
import java.util.function.Supplier;
32+
33+
@BenchmarkMode(Mode.AverageTime)
34+
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
35+
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
36+
@State(org.openjdk.jmh.annotations.Scope.Thread)
37+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
38+
@Fork(value = 3, jvmArgsAppend = { "--enable-native-access=ALL-UNNAMED", "-Djava.library.path=micro/native" })
39+
public class CallByRefHighArity {
40+
41+
static {
42+
System.loadLibrary("CallByRefHighArity");
43+
}
44+
45+
@Param
46+
SegmentKind kind;
47+
48+
public enum SegmentKind {
49+
CONFINED,
50+
SHARED,
51+
GLOBAL,
52+
HEAP
53+
}
54+
55+
Supplier<MemorySegment> segmentSupplier;
56+
Arena arena;
57+
58+
@Setup
59+
public void setup() {
60+
if (kind == SegmentKind.CONFINED) {
61+
arena = Arena.ofConfined();
62+
MemorySegment segment = arena.allocateFrom(ValueLayout.JAVA_INT, 0);
63+
segmentSupplier = () -> segment;
64+
} else if (kind == SegmentKind.SHARED) {
65+
arena = Arena.ofShared();
66+
MemorySegment segment = arena.allocateFrom(ValueLayout.JAVA_INT, 0);
67+
segmentSupplier = () -> segment;
68+
} else if (kind == SegmentKind.HEAP) {
69+
byte[] array = new byte[8];
70+
MemorySegment segment = MemorySegment.ofArray(array);
71+
segmentSupplier = () -> segment;
72+
} else { // global
73+
segmentSupplier = () -> MemorySegment.ofAddress(0);
74+
}
75+
}
76+
77+
@TearDown
78+
public void tearDown() {
79+
if (arena != null) {
80+
arena.close();
81+
}
82+
}
83+
84+
// A shared library that exports the functions below
85+
private static final SymbolLookup LOOKUP = SymbolLookup.loaderLookup();
86+
87+
// void noop_params0() {}
88+
private static final MethodHandle MH_NOOP_PARAMS0 = Linker.nativeLinker()
89+
.downcallHandle(FunctionDescriptor.ofVoid(), Linker.Option.critical(true))
90+
.bindTo(LOOKUP.find("noop_params0").orElseThrow());
91+
92+
// void noop_params1(void *param0) {}
93+
private static final MethodHandle MH_NOOP_PARAMS1 = Linker.nativeLinker()
94+
.downcallHandle(FunctionDescriptor.ofVoid(
95+
ValueLayout.ADDRESS
96+
), Linker.Option.critical(true))
97+
.bindTo(LOOKUP.find("noop_params1").orElseThrow());
98+
99+
// void noop_params2(void *param0, void *param1) {}
100+
private static final MethodHandle MH_NOOP_PARAMS2 = Linker.nativeLinker()
101+
.downcallHandle(FunctionDescriptor.ofVoid(
102+
ValueLayout.ADDRESS,
103+
ValueLayout.ADDRESS
104+
), Linker.Option.critical(true))
105+
.bindTo(LOOKUP.find("noop_params2").orElseThrow());
106+
107+
// void noop_params3(void *param0, void *param1, void *param2) {}
108+
private static final MethodHandle MH_NOOP_PARAMS3 = Linker.nativeLinker()
109+
.downcallHandle(FunctionDescriptor.ofVoid(
110+
ValueLayout.ADDRESS,
111+
ValueLayout.ADDRESS,
112+
ValueLayout.ADDRESS
113+
), Linker.Option.critical(true))
114+
.bindTo(LOOKUP.find("noop_params3").orElseThrow());
115+
116+
// void noop_params4(void *param0, void *param1, void *param2, void *param3) {}
117+
private static final MethodHandle MH_NOOP_PARAMS4 = Linker.nativeLinker()
118+
.downcallHandle(FunctionDescriptor.ofVoid(
119+
ValueLayout.ADDRESS,
120+
ValueLayout.ADDRESS,
121+
ValueLayout.ADDRESS,
122+
ValueLayout.ADDRESS
123+
), Linker.Option.critical(true))
124+
.bindTo(LOOKUP.find("noop_params4").orElseThrow());
125+
126+
// void noop_params5(int param0, int param1, void *param2, void *param3, void *param4) {}
127+
private static final MethodHandle MH_NOOP_PARAMS5 = Linker.nativeLinker()
128+
.downcallHandle(FunctionDescriptor.ofVoid(
129+
ValueLayout.ADDRESS,
130+
ValueLayout.ADDRESS,
131+
ValueLayout.ADDRESS,
132+
ValueLayout.ADDRESS,
133+
ValueLayout.ADDRESS
134+
), Linker.Option.critical(true))
135+
.bindTo(LOOKUP.find("noop_params5").orElseThrow());
136+
137+
// void noop_params10(void *param0, void *param1, void *param2, void *param3, void *param4,
138+
// void *param5, void *param6, void *param7, void *param8, void *param9) {}
139+
private static final MethodHandle MH_NOOP_PARAMS10 = Linker.nativeLinker()
140+
.downcallHandle(FunctionDescriptor.ofVoid(
141+
ValueLayout.ADDRESS,
142+
ValueLayout.ADDRESS,
143+
ValueLayout.ADDRESS,
144+
ValueLayout.ADDRESS,
145+
ValueLayout.ADDRESS,
146+
ValueLayout.ADDRESS,
147+
ValueLayout.ADDRESS,
148+
ValueLayout.ADDRESS,
149+
ValueLayout.ADDRESS,
150+
ValueLayout.ADDRESS
151+
), Linker.Option.critical(true))
152+
.bindTo(LOOKUP.find("noop_params10").orElseThrow());
153+
154+
@Benchmark
155+
public void noop_params0() {
156+
try {
157+
MH_NOOP_PARAMS0.invokeExact();
158+
} catch (Throwable t) {
159+
throw new AssertionError(t);
160+
}
161+
}
162+
163+
@Benchmark
164+
public void noop_params1() {
165+
try {
166+
MH_NOOP_PARAMS1.invokeExact(
167+
segmentSupplier.get()
168+
);
169+
} catch (Throwable t) {
170+
throw new AssertionError(t);
171+
}
172+
}
173+
174+
@Benchmark
175+
public void noop_params2() {
176+
try {
177+
MH_NOOP_PARAMS2.invokeExact(
178+
segmentSupplier.get(),
179+
segmentSupplier.get()
180+
);
181+
} catch (Throwable t) {
182+
throw new AssertionError(t);
183+
}
184+
}
185+
186+
@Benchmark
187+
public void noop_params3() {
188+
try {
189+
MH_NOOP_PARAMS3.invokeExact(
190+
segmentSupplier.get(),
191+
segmentSupplier.get(),
192+
segmentSupplier.get()
193+
);
194+
} catch (Throwable t) {
195+
throw new AssertionError(t);
196+
}
197+
}
198+
199+
@Benchmark
200+
public void noop_params4() {
201+
try {
202+
MH_NOOP_PARAMS4.invokeExact(
203+
segmentSupplier.get(),
204+
segmentSupplier.get(),
205+
segmentSupplier.get(),
206+
segmentSupplier.get()
207+
);
208+
} catch (Throwable t) {
209+
throw new AssertionError(t);
210+
}
211+
}
212+
213+
@Benchmark
214+
public void noop_params5() {
215+
try {
216+
MH_NOOP_PARAMS5.invokeExact(
217+
segmentSupplier.get(),
218+
segmentSupplier.get(),
219+
segmentSupplier.get(),
220+
segmentSupplier.get(),
221+
segmentSupplier.get()
222+
);
223+
} catch (Throwable t) {
224+
throw new AssertionError(t);
225+
}
226+
}
227+
228+
@Benchmark
229+
public void noop_params10() {
230+
try {
231+
MH_NOOP_PARAMS10.invokeExact(
232+
segmentSupplier.get(),
233+
segmentSupplier.get(),
234+
segmentSupplier.get(),
235+
segmentSupplier.get(),
236+
segmentSupplier.get(),
237+
segmentSupplier.get(),
238+
segmentSupplier.get(),
239+
segmentSupplier.get(),
240+
segmentSupplier.get(),
241+
segmentSupplier.get()
242+
);
243+
} catch (Throwable t) {
244+
throw new AssertionError(t);
245+
}
246+
}
247+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
#include "export.h"
25+
26+
EXPORT void noop_params0() {}
27+
EXPORT void noop_params1(void *param0) {}
28+
EXPORT void noop_params2(void *param0, void *param1) {}
29+
EXPORT void noop_params3(void *param0, void *param1, void *param2) {}
30+
EXPORT void noop_params4(void *param0, void *param1, void *param2, void *param3) {}
31+
EXPORT void noop_params5(void *param0, void *param1, void *param2, void *param3, void *param4) {}
32+
EXPORT void noop_params10(void *param0, void *param1, void *param2, void *param3, void *param4,
33+
void *param5, void *param6, void *param7, void *param8, void *param9) {}

0 commit comments

Comments
 (0)