Skip to content

Commit 53d6f05

Browse files
committed
[GR-35847] Allow setting options on per compilation basis
PullRequest: graal/10588
2 parents 1e902bb + 7ba4fa9 commit 53d6f05

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

compiler/src/jdk.graal.compiler.processor/src/jdk/graal/compiler/options/processor/OptionProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public static void createOptionsDescriptorsFile(ProcessingEnvironment processing
333333
out.printf(" /*name*/ \"%s\",\n", name);
334334
out.printf(" /*optionType*/ %s.%s,\n", getSimpleName(OPTION_TYPE_CLASS_NAME), optionType);
335335
out.printf(" /*optionValueType*/ %s.class,\n", type);
336-
out.printf(" /*help*/ \"%s\",\n", help);
336+
out.printf(" /*help*/ \"%s\",\n", help.replace("\\", "\\\\").replace("\"", "\\\""));
337337
if (extraHelp.size() != 0) {
338338
out.printf(" /*extraHelp*/ new String[] {\n");
339339
for (String line : extraHelp) {

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/CompilationTask.java

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static jdk.graal.compiler.core.CompilationWrapper.ExceptionAction.ExitVM;
2929
import static jdk.graal.compiler.core.GraalCompilerOptions.CompilationBailoutAsFailure;
3030
import static jdk.graal.compiler.core.GraalCompilerOptions.CompilationFailureAction;
31+
import static jdk.graal.compiler.core.GraalCompilerOptions.PrintCompilation;
3132
import static jdk.graal.compiler.core.phases.HighTier.Options.Inline;
3233
import static jdk.graal.compiler.java.BytecodeParserOptions.InlineDuringParsing;
3334
import static org.graalvm.nativeimage.ImageInfo.inImageRuntimeCode;
@@ -51,6 +52,7 @@
5152
import jdk.graal.compiler.debug.DebugDumpScope;
5253
import jdk.graal.compiler.debug.DebugHandlersFactory;
5354
import jdk.graal.compiler.debug.GraalError;
55+
import jdk.graal.compiler.debug.MethodFilter;
5456
import jdk.graal.compiler.debug.TTY;
5557
import jdk.graal.compiler.debug.TimerKey;
5658
import jdk.graal.compiler.nodes.StructuredGraph;
@@ -81,6 +83,17 @@ static class Options {
8183
@Option(help = "Perform a full GC of the libgraal heap after every compile to reduce idle heap and reclaim " +
8284
"references to the HotSpot heap. This flag has no effect in the context of jargraal.", type = OptionType.Debug)//
8385
public static final OptionKey<Boolean> FullGCAfterCompile = new OptionKey<>(false);
86+
@Option(help = "Options which are enabled based on the method being compiled. " +
87+
"The basic syntax is a MethodFilter option specification followed by a list of options to be set for that compilation. " +
88+
"\"MethodFilter:\" is used to distinguish this from normal usage of MethodFilter as option." +
89+
"This can be repeated multiple times with each MethodFilter option separating the groups. " +
90+
"For example:" +
91+
" -D" + HotSpotGraalOptionValues.GRAAL_OPTION_PROPERTY_PREFIX +
92+
".PerMethodOptions=MethodFilter:String.indexOf SpeculativeGuardMovement=false MethodFilter:Integer.* SpeculativeGuardMovement=false" +
93+
" disables SpeculativeGuardMovement for compiles of String.indexOf and all methods in Integer. " +
94+
"If the value starts with a non-letter character, that " +
95+
"character is used as the separator between options instead of a space.")//
96+
public static final OptionKey<String> PerMethodOptions = new OptionKey<>(null);
8497
}
8598

8699
@Override
@@ -163,7 +176,7 @@ protected HotSpotCompilationRequestResult handleException(Throwable t) {
163176
/*
164177
* Treat random exceptions from the compiler as indicating a problem compiling this
165178
* method. Report the result of toString instead of getMessage to ensure that the
166-
* exception type is included in the output in case there's no detail mesage.
179+
* exception type is included in the output in case there's no detail message.
167180
*/
168181
return HotSpotCompilationRequestResult.failure(t.toString(), false);
169182
}
@@ -333,25 +346,69 @@ public void setTypeFilter(TypeFilter typeFilter) {
333346
this.profileSaveFilter = typeFilter;
334347
}
335348

336-
public OptionValues filterOptions(OptionValues options) {
349+
public OptionValues filterOptions(OptionValues originalOptions) {
350+
HotSpotGraalRuntimeProvider graalRuntime = compiler.getGraalRuntime();
351+
GraalHotSpotVMConfig config = graalRuntime.getVMConfig();
352+
OptionValues newOptions = originalOptions;
353+
354+
// Set any options for this compile.
355+
String perMethodOptions = Options.PerMethodOptions.getValue(originalOptions);
356+
if (perMethodOptions != null) {
357+
EconomicMap<OptionKey<?>, Object> values;
358+
try {
359+
EconomicMap<String, String> optionSettings = null;
360+
for (String option : OptionsParser.splitOptions(perMethodOptions)) {
361+
String prefix = "MethodFilter:";
362+
if (option.startsWith(prefix)) {
363+
MethodFilter filter = MethodFilter.parse(option.substring(prefix.length()));
364+
if (filter.matches(getMethod())) {
365+
// Begin accumulating options
366+
optionSettings = EconomicMap.create();
367+
} else if (optionSettings != null) {
368+
// This is a new MethodFilter: so stop collecting options
369+
break;
370+
}
371+
} else if (optionSettings != null) {
372+
OptionsParser.parseOptionSettingTo(option, optionSettings);
373+
} else {
374+
throw new IllegalArgumentException(Options.PerMethodOptions.getName() + " must start with \"MethodFilter:\" specification");
375+
}
376+
}
377+
if (optionSettings.isEmpty()) {
378+
throw new IllegalArgumentException("No options specified for MethodFilter:");
379+
}
380+
values = EconomicMap.create();
381+
OptionsParser.parseOptions(optionSettings, values, OptionsParser.getOptionsLoader());
382+
} catch (Exception e) {
383+
values = null;
384+
TTY.println(e.toString());
385+
TTY.println("Errors encountered during " + Options.PerMethodOptions.getName() + " parsing. Exiting...");
386+
HotSpotGraalServices.exit(-1, jvmciRuntime);
387+
}
388+
389+
if (values != null) {
390+
newOptions = new OptionValues(newOptions, values);
391+
if (PrintCompilation.getValue(newOptions)) {
392+
TTY.println("Compiling " + getMethod() + " with extra options: " + new OptionValues(values));
393+
}
394+
}
395+
}
337396
/*
338397
* Disable inlining if HotSpot has it disabled unless it's been explicitly set in Graal.
339398
*/
340-
HotSpotGraalRuntimeProvider graalRuntime = compiler.getGraalRuntime();
341-
GraalHotSpotVMConfig config = graalRuntime.getVMConfig();
342-
OptionValues newOptions = options;
343399
if (!config.inline) {
344400
EconomicMap<OptionKey<?>, Object> m = OptionValues.newOptionMap();
345-
if (Inline.getValue(options) && !Inline.hasBeenSet(options)) {
401+
if (Inline.getValue(newOptions) && !Inline.hasBeenSet(newOptions)) {
346402
m.put(Inline, false);
347403
}
348-
if (InlineDuringParsing.getValue(options) && !InlineDuringParsing.hasBeenSet(options)) {
404+
if (InlineDuringParsing.getValue(newOptions) && !InlineDuringParsing.hasBeenSet(newOptions)) {
349405
m.put(InlineDuringParsing, false);
350406
}
351407
if (!m.isEmpty()) {
352-
newOptions = new OptionValues(options, m);
408+
newOptions = new OptionValues(newOptions, m);
353409
}
354410
}
411+
355412
return newOptions;
356413
}
357414

0 commit comments

Comments
 (0)