Skip to content

Commit

Permalink
[GR-21018] Migrate instrumentation Truffle compiler options to Polygl…
Browse files Browse the repository at this point in the history
…otCompilerOptions.

PullRequest: graal/5405
  • Loading branch information
tzezula committed Feb 19, 2020
2 parents c659b53 + 649f1dc commit 998163d
Show file tree
Hide file tree
Showing 57 changed files with 695 additions and 878 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ public boolean shouldCheckUsage(OptionDescriptor option) {
*/
return false;
}
if (declaringClass.getName().equals("org.graalvm.compiler.truffle.compiler.TruffleCompilerOptions")) {
/*
* These options are deprecated and will be removed in GraalVM 20.2.0. The
* TruffleIntrinsifyFrameAccess option has no replacement and is unused.
*/
return false;
}
if (option.getOptionKey().getClass().isAnonymousClass()) {
/*
* Probably a derived option such as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public class OptionsVerifierTest {

private static Set<String> WHITELIST = new TreeSet<>(Arrays.asList(//
// Generated options delegating default values to PolyglotCompilerOptions
"org.graalvm.compiler.truffle.compiler.SharedTruffleCompilerOptions"));
"org.graalvm.compiler.truffle.compiler.SharedTruffleCompilerOptions",
// Deprecated options delegating default values to PolyglotCompilerOptions
"org.graalvm.compiler.truffle.compiler.TruffleCompilerOptions"));

@Test
public void verifyOptions() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public boolean doProcess(Set<? extends TypeElement> annotations, RoundEnvironmen
List<String> extraHelp = option.help.length > 1 ? Arrays.asList(option.help).subList(1, option.help.length) : new ArrayList<>();
info.options.add(new OptionInfo(option.name, optionType, help, extraHelp, option.type, pkg + '.' + className, option.name));
}
out.printf(" public static final OptionKey<%s> %s = new OptionKey<>(%s);\n", option.type, option.name, defaultValue);
out.printf(" static final OptionKey<%s> %s = new OptionKey<>(%s);\n", option.type, option.name, defaultValue);
out.println();
}
out.println("}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public interface TruffleCompiler {
*
* @since 20.0.0
*/
void initialize();
void initialize(Map<String, Object> options);

/**
* Opens a new compilation for {@code compilable}. Each call results in a new compilation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ public boolean allowsInlining() {
LoopExplosionKind getLoopExplosionKind(ResolvedJavaMethod method);

/**
* Gets the primary {@link TruffleCompiler} instance associated with this runtime, creating it
* in a thread-safe manner first if necessary.
* Gets the primary {@link TruffleCompiler} instance associated with this runtime, creating and
* initializing it in a thread-safe manner first if necessary.
*/
TruffleCompiler getTruffleCompiler();
TruffleCompiler getTruffleCompiler(CompilableTruffleAST compilable);

/**
* Gets a plan for inlining in terms of a Truffle AST call graph.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public <T> T convertOptions(Class<T> optionValuesType, Map<String, Object> map)
}

@Override
public TruffleCompiler getTruffleCompiler() {
public TruffleCompiler getTruffleCompiler(CompilableTruffleAST compilable) {
throw new UnsupportedOperationException("Should never be called in the compiler.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,11 @@ public static long newCompiler(JNIEnv env, JClass hsClazz, @CEntryPoint.IsolateT
@HotSpotToSVM(InitializeCompiler)
@SuppressWarnings({"unused", "try"})
@CEntryPoint(name = "Java_org_graalvm_compiler_truffle_runtime_hotspot_libgraal_HotSpotToSVMCalls_initializeCompiler")
public static void initializeCompiler(JNIEnv env, JClass hsClazz, @CEntryPoint.IsolateThreadContext long isolateThreadId, long compilerHandle) {
public static void initializeCompiler(JNIEnv env, JClass hsClazz, @CEntryPoint.IsolateThreadContext long isolateThreadId, long compilerHandle, JByteArray hsOptions) {
try (HotSpotToSVMScope<HotSpotToSVM.Id> s = new HotSpotToSVMScope<>(InitializeCompiler, env)) {
HotSpotTruffleCompilerImpl compiler = SVMObjectHandles.resolve(compilerHandle, HotSpotTruffleCompilerImpl.class);
compiler.initialize();
Map<String, Object> options = decodeOptions(env, hsOptions);
compiler.initialize(options);
} catch (Throwable t) {
JNIExceptionWrapper.throwInHotSpot(env, t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
package org.graalvm.compiler.truffle.compiler;

import static org.graalvm.compiler.nodes.graphbuilderconf.InlineInvokePlugin.InlineInfo.createStandardInlineInfo;
import static org.graalvm.compiler.truffle.options.PolyglotCompilerOptions.NodeSourcePositions;
import static org.graalvm.compiler.truffle.options.PolyglotCompilerOptions.ExcludeAssertions;
import static org.graalvm.compiler.truffle.options.PolyglotCompilerOptions.TraceInlining;
import static org.graalvm.compiler.truffle.options.PolyglotCompilerOptions.TraceStackTraceLimit;
import static org.graalvm.compiler.truffle.options.PolyglotCompilerOptions.PerformanceWarningsAreFatal;
Expand Down Expand Up @@ -124,7 +126,6 @@
import org.graalvm.compiler.truffle.compiler.substitutions.KnownTruffleTypes;
import org.graalvm.compiler.truffle.compiler.substitutions.TruffleGraphBuilderPlugins;
import org.graalvm.compiler.truffle.compiler.substitutions.TruffleInvocationPluginProvider;
import org.graalvm.compiler.truffle.options.PolyglotCompilerOptions;
import org.graalvm.compiler.truffle.options.PolyglotCompilerOptions.PerformanceWarningKind;
import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;
import org.graalvm.options.OptionValues;
Expand Down Expand Up @@ -152,12 +153,19 @@ public abstract class PartialEvaluator {
protected final ResolvedJavaMethod callInlinedAgnosticMethod;
private final ResolvedJavaMethod callIndirectMethod;
private final ResolvedJavaMethod callRootMethod;
private final GraphBuilderConfiguration configForParsing;
private final GraphBuilderConfiguration configPrototype;
private final InvocationPlugins decodingInvocationPlugins;
private final NodePlugin[] nodePlugins;
private final KnownTruffleTypes knownTruffleTypes;
private final ResolvedJavaMethod callBoundary;
private volatile GraphBuilderConfiguration configForParsing;

/**
* Holds instrumentation options initialized in
* {@link #initialize(org.graalvm.options.OptionValues)} method before the first compilation.
* These options are not engine aware.
*/
volatile InstrumentPhase.InstrumentationConfiguration instrumentationCfg;
/**
* The instrumentation object is used by the Truffle instrumentation to count executions. The
* value is lazily initialized the first time it is requested because it depends on the Truffle
Expand Down Expand Up @@ -185,20 +193,33 @@ public PartialEvaluator(Providers providers, GraphBuilderConfiguration configFor
this.callRootMethod = findRequiredMethod(type, methods, "callRoot", "([Ljava/lang/Object;)Ljava/lang/Object;");
this.callBoundary = findRequiredMethod(type, methods, "callBoundary", "([Ljava/lang/Object;)Ljava/lang/Object;");

this.configForParsing = createGraphBuilderConfig(configForRoot, true);
this.configPrototype = createGraphBuilderConfig(configForRoot, true);
this.decodingInvocationPlugins = createDecodingInvocationPlugins(configForRoot.getPlugins());
this.nodePlugins = createNodePlugins(configForRoot.getPlugins());
}

void initialize(OptionValues options) {
instrumentationCfg = new InstrumentPhase.InstrumentationConfiguration(options);
boolean needSourcePositions = TruffleCompilerOptions.getPolyglotOptionValue(options, NodeSourcePositions) ||
instrumentationCfg.instrumentBranches ||
instrumentationCfg.instrumentBoundaries ||
!TruffleCompilerOptions.getPolyglotOptionValue(options, TracePerformanceWarnings).isEmpty();
configForParsing = configPrototype.withNodeSourcePosition(configPrototype.trackNodeSourcePosition() || needSourcePositions).withOmitAssertions(
TruffleCompilerOptions.getPolyglotOptionValue(options, ExcludeAssertions));
}

/**
* Gets the instrumentation manager associated with this compiler, creating it first if
* necessary. Each compiler instance has its own instrumentation manager.
*/
public final InstrumentPhase.Instrumentation getInstrumentation(OptionValues options) {
public final InstrumentPhase.Instrumentation getInstrumentation() {
if (instrumentation == null) {
synchronized (this) {
if (instrumentation == null) {
long[] accessTable = new long[getPolyglotOptionValue(options, PolyglotCompilerOptions.InstrumentationTableSize)];
if (instrumentationCfg == null) {
throw new IllegalStateException("PartialEvaluator is not yet initialized");
}
long[] accessTable = new long[instrumentationCfg.instrumentationTableSize];
instrumentation = new InstrumentPhase.Instrumentation(accessTable);
}
}
Expand Down Expand Up @@ -240,7 +261,27 @@ public Providers getProviders() {
return providers;
}

public GraphBuilderConfiguration getConfigForParsing() {
/**
* Returns the root {@link GraphBuilderConfiguration}. The root configuration provides plugins
* used by this {@link PartialEvaluator} but it's not configured with engine options. The root
* configuration should be used in image generation time where the {@link PartialEvaluator} is
* not yet initialized with engine options. At runtime the {@link #getConfig} should be used.
*/
public GraphBuilderConfiguration getConfigPrototype() {
return configPrototype;
}

/**
* Returns the {@link GraphBuilderConfiguration} used by parsing. The returned configuration is
* configured with engine options. In the image generation time the {@link PartialEvaluator} is
* not yet initialized and the {@link #getConfigPrototype} should be used instead.
*
* @throws IllegalStateException when called on non initialized {@link PartialEvaluator}
*/
public GraphBuilderConfiguration getConfig() {
if (configForParsing == null) {
throw new IllegalStateException("PartialEvaluator is not yet initialized");
}
return configForParsing;
}

Expand Down Expand Up @@ -618,10 +659,7 @@ protected GraphBuilderConfiguration createGraphBuilderConfig(GraphBuilderConfigu
GraphBuilderConfiguration newConfig = config.copy();
InvocationPlugins invocationPlugins = newConfig.getPlugins().getInvocationPlugins();
registerTruffleInvocationPlugins(invocationPlugins, canDelayIntrinsification);
boolean mustInstrumentBranches = TruffleCompilerOptions.getValue(TruffleCompilerOptions.TruffleInstrumentBranches) ||
TruffleCompilerOptions.getValue(TruffleCompilerOptions.TruffleInstrumentBoundaries);
return newConfig.withNodeSourcePosition(
newConfig.trackNodeSourcePosition() || mustInstrumentBranches || TruffleCompilerOptions.getValue(TruffleCompilerOptions.TraceTrufflePerformanceWarnings));
return newConfig;
}

protected NodePlugin[] createNodePlugins(Plugins plugins) {
Expand Down Expand Up @@ -705,11 +743,12 @@ private void agnosticInliningOrGraphPE(OptionValues options, CompilableTruffleAS
}

protected void applyInstrumentationPhases(OptionValues options, StructuredGraph graph, HighTierContext tierContext) {
if (TruffleCompilerOptions.TruffleInstrumentBranches.getValue(graph.getOptions())) {
new InstrumentBranchesPhase(options, snippetReflection, getInstrumentation(options)).apply(graph, tierContext);
InstrumentPhase.InstrumentationConfiguration cfg = instrumentationCfg;
if (cfg.instrumentBranches) {
new InstrumentBranchesPhase(options, snippetReflection, getInstrumentation(), cfg.instrumentBranchesPerInlineSite).apply(graph, tierContext);
}
if (TruffleCompilerOptions.TruffleInstrumentBoundaries.getValue(graph.getOptions())) {
new InstrumentTruffleBoundariesPhase(options, snippetReflection, getInstrumentation(options)).apply(graph, tierContext);
if (cfg.instrumentBoundaries) {
new InstrumentTruffleBoundariesPhase(options, snippetReflection, getInstrumentation(), cfg.instrumentBoundariesPerInlineSite).apply(graph, tierContext);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.graalvm.compiler.phases.OptimisticOptimizations.Optimization.UseExceptionProbability;
import static org.graalvm.compiler.phases.OptimisticOptimizations.Optimization.UseTypeCheckHints;
import static org.graalvm.compiler.phases.OptimisticOptimizations.Optimization.UseTypeCheckedInlining;
import static org.graalvm.compiler.truffle.options.PolyglotCompilerOptions.ExcludeAssertions;

import java.io.PrintStream;
import java.io.PrintWriter;
Expand Down Expand Up @@ -119,7 +120,7 @@ public abstract class TruffleCompilerImpl implements TruffleCompilerBase {
protected final Backend backend;
protected final SnippetReflectionProvider snippetReflection;
protected final TrufflePostCodeInstallationTaskFactory codeInstallationTaskFactory;
private volatile boolean checkedDeprecatedOptionsUsage;
private volatile boolean initialized;

public static final OptimisticOptimizations Optimizations = ALL.remove(
UseExceptionProbability,
Expand All @@ -146,12 +147,8 @@ public TruffleCompilerImpl(TruffleCompilerRuntime runtime,

ResolvedJavaType[] skippedExceptionTypes = getSkippedExceptionTypes(runtime);

OptionValues optionValues = runtime.getOptions(OptionValues.class);
boolean needSourcePositions = TruffleCompilerOptions.TruffleEnableInfopoints.getValue(optionValues) ||
TruffleCompilerOptions.TruffleInstrumentBranches.getValue(optionValues) || TruffleCompilerOptions.TruffleInstrumentBoundaries.getValue(optionValues);
GraphBuilderConfiguration baseConfig = GraphBuilderConfiguration.getDefault(new Plugins(plugins)).withNodeSourcePosition(needSourcePositions);
this.config = baseConfig.withSkippedExceptionTypes(skippedExceptionTypes).withOmitAssertions(TruffleCompilerOptions.TruffleExcludeAssertions.getValue(optionValues)).withBytecodeExceptionMode(
BytecodeExceptionMode.ExplicitOnly);
GraphBuilderConfiguration baseConfig = GraphBuilderConfiguration.getDefault(new Plugins(plugins));
this.config = baseConfig.withSkippedExceptionTypes(skippedExceptionTypes).withOmitAssertions(ExcludeAssertions.getDefaultValue()).withBytecodeExceptionMode(BytecodeExceptionMode.ExplicitOnly);

this.partialEvaluator = createPartialEvaluator();
this.firstTierProviders = firstTierProviders;
Expand Down Expand Up @@ -272,18 +269,17 @@ public final void doCompile(TruffleDebugContext truffleDebug,
}

@Override
public void initialize() {
if (!checkedDeprecatedOptionsUsage) {
boolean doCheck = false;
public void initialize(Map<String, Object> optionsMap) {
if (!initialized) {
synchronized (this) {
if (!checkedDeprecatedOptionsUsage) {
checkedDeprecatedOptionsUsage = true;
doCheck = Boolean.parseBoolean(System.getenv("TRUFFLE_STRICT_OPTION_DEPRECATION"));
if (!initialized) {
partialEvaluator.initialize(TruffleCompilerOptions.getOptionsForCompiler(optionsMap));
if (Boolean.parseBoolean(System.getenv("TRUFFLE_STRICT_OPTION_DEPRECATION"))) {
TruffleCompilerOptions.checkDeprecation();
}
initialized = true;
}
}
if (doCheck) {
TruffleCompilerOptions.checkDeprecation();
}
}
}

Expand Down Expand Up @@ -340,11 +336,11 @@ public String get() {

@Override
public void shutdown() {
InstrumentPhase.Instrumentation ins = this.partialEvaluator.instrumentation;
if (ins != null) {
OptionValues options = TruffleCompilerOptions.getOptions();
if (TruffleCompilerOptions.getValue(TruffleCompilerOptions.TruffleInstrumentBranches) || TruffleCompilerOptions.getValue(TruffleCompilerOptions.TruffleInstrumentBoundaries)) {
ins.dumpAccessTable(options);
InstrumentPhase.InstrumentationConfiguration cfg = partialEvaluator.instrumentationCfg;
if (cfg != null && (cfg.instrumentBoundaries || cfg.instrumentBranches)) {
InstrumentPhase.Instrumentation ins = this.partialEvaluator.instrumentation;
if (ins != null) {
ins.dumpAccessTable();
}
}
}
Expand Down
Loading

0 comments on commit 998163d

Please sign in to comment.