Skip to content

Commit e482f98

Browse files
committed
[GR-67840] Separate build time and runtime code alignment
PullRequest: graal/21683
2 parents 8462300 + b01b7d5 commit e482f98

File tree

8 files changed

+43
-22
lines changed

8 files changed

+43
-22
lines changed

substratevm/src/com.oracle.svm.core.foreign/src/com/oracle/svm/core/foreign/TrampolineSet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private static UnsignedWord allocationSize() {
5757
}
5858

5959
private static UnsignedWord alignment() {
60-
return Word.unsigned(SubstrateOptions.codeAlignment());
60+
return Word.unsigned(SubstrateOptions.runtimeCodeAlignment());
6161
}
6262

6363
private static int maxTrampolineCount() {
@@ -145,7 +145,7 @@ void patchTrampolineForDirectUpcall(Pointer trampolinePointer, CFunctionPointer
145145
private Pointer prepareTrampolines(PinnedObject mhsArray, PinnedObject stubsArray, AbiUtils.TrampolineTemplate template) {
146146
UnsignedWord pageSize = allocationSize();
147147
/* We request a specific alignment to guarantee correctness of getAllocationBase */
148-
Pointer page = CommittedMemoryProvider.get().allocateExecutableMemory(pageSize, Word.unsigned(SubstrateOptions.codeAlignment()));
148+
Pointer page = CommittedMemoryProvider.get().allocateExecutableMemory(pageSize, Word.unsigned(SubstrateOptions.runtimeCodeAlignment()));
149149
if (page.isNull()) {
150150
throw new OutOfMemoryError("Could not allocate memory for trampolines.");
151151
}

substratevm/src/com.oracle.svm.core.graal.aarch64/src/com/oracle/svm/core/graal/aarch64/SubstrateAArch64Backend.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,7 @@ public CompilationResultBuilder newCompilationResultBuilder(LIRGenerationResult
13351335
crb.setTotalFrameSize(lirGenResult.getFrameMap().totalFrameSize());
13361336
if (SubstrateUtil.HOSTED) {
13371337
var sharedCompilationResult = (SharedCompilationResult) compilationResult;
1338-
sharedCompilationResult.setCodeAlignment(SubstrateOptions.codeAlignment(options));
1338+
sharedCompilationResult.setCodeAlignment(SubstrateOptions.buildTimeCodeAlignment(options));
13391339
}
13401340
return crb;
13411341
}

substratevm/src/com.oracle.svm.core.graal.amd64/src/com/oracle/svm/core/graal/amd64/SubstrateAMD64Backend.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1902,7 +1902,7 @@ public CompilationResultBuilder newCompilationResultBuilder(LIRGenerationResult
19021902
var substrateAMD64FrameMap = (SubstrateAMD64FrameMap) frameMap;
19031903
sharedCompilationResult.setFrameSize(substrateAMD64FrameMap.frameSize());
19041904
if (SubstrateUtil.HOSTED) {
1905-
sharedCompilationResult.setCodeAlignment(SubstrateOptions.codeAlignment(options));
1905+
sharedCompilationResult.setCodeAlignment(SubstrateOptions.buildTimeCodeAlignment(options));
19061906
}
19071907
if (substrateAMD64FrameMap.needsFramePointer()) {
19081908
sharedCompilationResult.setFramePointerSaveAreaOffset(substrateAMD64FrameMap.getFramePointerSaveAreaOffset());

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,27 +1031,41 @@ public static boolean useLIRBackend() {
10311031
public static final HostedOptionKey<Boolean> DeadlockWatchdogExitOnTimeout = new HostedOptionKey<>(true);
10321032

10331033
/**
1034-
* The alignment for AOT and JIT compiled methods. The value is constant folded during image
1035-
* generation, i.e., cannot be changed at run time, so that it can be used in uninterruptible
1036-
* code.
1034+
* The alignment for JIT compiled methods.
10371035
*/
10381036
@Fold
1039-
public static int codeAlignment() {
1037+
public static int runtimeCodeAlignment() {
1038+
int value = ConcealedOptions.CodeAlignment.getValue();
1039+
// In runtime compiled methods, data is also aligned according to the return value of this
1040+
// method. As a result, it needs to be at least defaultCodeAlignment() so that it can
1041+
// support the allocation of large constants such as SIMD ones.
1042+
return Math.max(value, defaultCodeAlignment());
1043+
}
1044+
1045+
/**
1046+
* The alignment for AOT compiled methods.
1047+
*/
1048+
@Platforms(Platform.HOSTED_ONLY.class)
1049+
public static int buildTimeCodeAlignment() {
10401050
int value = ConcealedOptions.CodeAlignment.getValue();
10411051
if (value > 0) {
1052+
// In contrast to runtimeCodeAlignment, this value can be less than
1053+
// defaultCodeAlignment() so that methods can be more densely packed in the generated
1054+
// native image.
10421055
return value;
10431056
}
10441057

1045-
if (ConfigurationValues.getTarget().arch instanceof AMD64) {
1046-
return 32;
1047-
}
1048-
return 16;
1058+
return defaultCodeAlignment();
1059+
}
1060+
1061+
private static int defaultCodeAlignment() {
1062+
return ConfigurationValues.getTarget().arch instanceof AMD64 ? 32 : 16;
10491063
}
10501064

10511065
@Platforms(Platform.HOSTED_ONLY.class)
1052-
public static int codeAlignment(OptionValues options) {
1066+
public static int buildTimeCodeAlignment(OptionValues options) {
10531067
int value = ConcealedOptions.CodeAlignment.getValue(options);
1054-
return value > 0 ? value : codeAlignment();
1068+
return value > 0 ? value : buildTimeCodeAlignment();
10551069
}
10561070

10571071
@Option(help = "Determines if VM internal threads (e.g., a dedicated VM operation or reference handling thread) are allowed in this image.", type = OptionType.Expert) //
@@ -1236,7 +1250,10 @@ protected void onValueUpdate(EconomicMap<OptionKey<?>, Object> values, Integer o
12361250
@Option(help = "Generated code style for prefetch instructions: for 0 or less no prefetch instructions are generated and for 1 or more prefetch instructions are introduced after each allocation.")//
12371251
public static final HostedOptionKey<Integer> AllocatePrefetchStyle = new HostedOptionKey<>(null);
12381252

1239-
/** Use {@link SubstrateOptions#codeAlignment()} instead. */
1253+
/**
1254+
* Use {@link SubstrateOptions#buildTimeCodeAlignment()} or
1255+
* {@link SubstrateOptions#runtimeCodeAlignment()} instead.
1256+
*/
12401257
@LayerVerifiedOption(kind = Kind.Changed, severity = Severity.Error)//
12411258
@Option(help = "Alignment of AOT and JIT compiled code in bytes. The default of 0 automatically selects a suitable value.")//
12421259
public static final HostedOptionKey<Integer> CodeAlignment = new HostedOptionKey<>(0);

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/code/RuntimeCodeInfoAccess.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,12 @@ static void markAsRemovedFromCodeCache(CodeInfo info) {
218218
}
219219

220220
public static CodePointer allocateCodeMemory(UnsignedWord size) {
221-
return (CodePointer) CommittedMemoryProvider.get().allocateExecutableMemory(size, Word.unsigned(SubstrateOptions.codeAlignment()));
221+
return (CodePointer) CommittedMemoryProvider.get().allocateExecutableMemory(size, Word.unsigned(SubstrateOptions.runtimeCodeAlignment()));
222222
}
223223

224224
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
225225
private static void releaseCodeMemory(CodePointer codeStart, UnsignedWord codeSize) {
226-
CommittedMemoryProvider.get().freeExecutableMemory(codeStart, codeSize, Word.unsigned(SubstrateOptions.codeAlignment()));
226+
CommittedMemoryProvider.get().freeExecutableMemory(codeStart, codeSize, Word.unsigned(SubstrateOptions.runtimeCodeAlignment()));
227227
}
228228

229229
public static void makeCodeMemoryExecutableReadOnly(CodePointer codeStart, UnsignedWord codeSize) {

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/code/SharedCompilationResult.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import jdk.graal.compiler.code.CompilationResult;
3030
import jdk.graal.compiler.core.common.CompilationIdentifier;
3131
import jdk.graal.compiler.core.common.NumUtil;
32+
import org.graalvm.nativeimage.Platform;
33+
import org.graalvm.nativeimage.Platforms;
3234

3335
/** Base class common to both hosted and runtime compilations. */
3436
public abstract class SharedCompilationResult extends CompilationResult {
@@ -64,17 +66,19 @@ public void setFramePointerSaveAreaOffset(int value) {
6466
this.framePointerSaveAreaOffset = value;
6567
}
6668

69+
@Platforms(Platform.HOSTED_ONLY.class)
6770
public static int getCodeAlignment(CompilationResult compilation) {
6871
int result;
6972
if (compilation instanceof SharedCompilationResult s) {
7073
result = s.codeAlignment;
7174
} else {
72-
result = SubstrateOptions.codeAlignment();
75+
result = SubstrateOptions.buildTimeCodeAlignment();
7376
}
7477
VMError.guarantee(result > 0 && NumUtil.isUnsignedPowerOf2(result), "invalid alignment %d", result);
7578
return result;
7679
}
7780

81+
@Platforms(Platform.HOSTED_ONLY.class)
7882
public void setCodeAlignment(int codeAlignment) {
7983
VMError.guarantee(codeAlignment > 0 && NumUtil.isUnsignedPowerOf2(codeAlignment), "invalid alignment %d", codeAlignment);
8084
this.codeAlignment = codeAlignment;

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/code/SubstrateDataBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,6 @@ public static void emit(ByteBuffer buffer, Patches patches, int size, VMConstant
102102
public int getMaxSupportedAlignment() {
103103
// See RuntimeCodeInstaller.prepareCodeMemory
104104
// Code and data are allocated in one go
105-
return SubstrateOptions.codeAlignment();
105+
return SubstrateOptions.runtimeCodeAlignment();
106106
}
107107
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/LIRNativeImageCodeCache.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public void layoutMethods(DebugContext debug, BigBang bb) {
194194
Pair<HostedMethod, CompilationResult> lastCompilation = getLastCompilation();
195195
HostedMethod lastMethod = lastCompilation.getLeft();
196196

197-
// the total code size is aligned up to SubstrateOptions.codeAlignment()
197+
// the total code size is aligned up to SubstrateOptions.buildTimeCodeAlignment()
198198
int totalSize;
199199
if (orderedTrampolineMap.containsKey(lastMethod)) {
200200
var trampolines = orderedTrampolineMap.get(lastMethod);
@@ -203,7 +203,7 @@ public void layoutMethods(DebugContext debug, BigBang bb) {
203203
} else {
204204
totalSize = addOffset(lastCompilation.getLeft().getCodeAddressOffset(), lastCompilation.getRight().getTargetCodeSize());
205205
}
206-
totalSize = align(totalSize, SubstrateOptions.codeAlignment());
206+
totalSize = align(totalSize, SubstrateOptions.buildTimeCodeAlignment());
207207

208208
setCodeAreaSize(totalSize);
209209

@@ -476,7 +476,7 @@ public void writeCode(RelocatableBuffer buffer) {
476476
}
477477
}
478478

479-
for (int i = curPos; i < NumUtil.roundUp(curPos, SubstrateOptions.codeAlignment()); i++) {
479+
for (int i = curPos; i < NumUtil.roundUp(curPos, SubstrateOptions.buildTimeCodeAlignment()); i++) {
480480
bufferBytes.put(CODE_FILLER_BYTE);
481481
}
482482
}

0 commit comments

Comments
 (0)