use of org.graalvm.compiler.code.CompilationResult in project graal by oracle.
the class CompileQueue method defaultCompileFunction.
@SuppressWarnings("try")
private CompilationResult defaultCompileFunction(DebugContext debug, HostedMethod method, CompilationIdentifier compilationIdentifier, CompileReason reason, RuntimeConfiguration config) {
if (NativeImageOptions.PrintAOTCompilation.getValue()) {
System.out.println("Compiling " + method.format("%r %H.%n(%p)") + " [" + reason + "]");
}
Backend backend = config.lookupBackend(method);
StructuredGraph graph = method.compilationInfo.graph;
assert graph != null : method;
/* Operate on a copy, to keep the original graph intact for later inlining. */
graph = graph.copyWithIdentifier(compilationIdentifier, debug);
/* Check that graph is in good shape before compilation. */
assert GraphOrder.assertSchedulableGraph(graph);
try (DebugContext.Scope s = debug.scope("Compiling", graph, method, this)) {
if (deoptimizeAll && method.compilationInfo.canDeoptForTesting) {
insertDeoptTests(method, graph);
}
method.compilationInfo.numNodesBeforeCompilation = graph.getNodeCount();
method.compilationInfo.numDeoptEntryPoints = graph.getNodes().filter(DeoptEntryNode.class).count();
method.compilationInfo.numDuringCallEntryPoints = graph.getNodes(MethodCallTargetNode.TYPE).snapshot().stream().map(MethodCallTargetNode::invoke).filter(invoke -> method.compilationInfo.isDeoptEntry(invoke.bci(), true, false)).count();
Suites suites = method.compilationInfo.isDeoptTarget() ? deoptTargetSuites : regularSuites;
LIRSuites lirSuites = method.compilationInfo.isDeoptTarget() ? deoptTargetLIRSuites : regularLIRSuites;
CompilationResult result = new CompilationResult(compilationIdentifier, method.format("%H.%n(%p)")) {
@Override
public void close() {
/*
* Do nothing, we do not want our CompilationResult to be closed because we
* aggregate all data items and machine code in the native image heap.
*/
}
};
try (Indent indent = debug.logAndIndent("compile %s", method)) {
GraalCompiler.compileGraph(graph, method, backend.getProviders(), backend, null, optimisticOpts, method.getProfilingInfo(), suites, lirSuites, result, new HostedCompilationResultBuilderFactory());
}
method.getProfilingInfo().setCompilerIRSize(StructuredGraph.class, method.compilationInfo.graph.getNodeCount());
method.compilationInfo.numNodesAfterCompilation = graph.getNodeCount();
if (method.compilationInfo.isDeoptTarget()) {
assert verifyDeoptTarget(method, result);
}
for (Infopoint infopoint : result.getInfopoints()) {
if (infopoint instanceof Call) {
Call call = (Call) infopoint;
HostedMethod callTarget = (HostedMethod) call.target;
if (call.direct) {
ensureCompiled(callTarget, new DirectCallReason(method, reason));
} else if (callTarget != null && callTarget.getImplementations() != null) {
for (HostedMethod impl : callTarget.getImplementations()) {
ensureCompiled(impl, new VirtualCallReason(method, callTarget, reason));
}
}
}
}
/* Shrink resulting code array to minimum size, to reduze memory footprint. */
if (result.getTargetCode().length > result.getTargetCodeSize()) {
result.setTargetCode(Arrays.copyOf(result.getTargetCode(), result.getTargetCodeSize()), result.getTargetCodeSize());
}
return result;
} catch (Throwable ex) {
GraalError error = ex instanceof GraalError ? (GraalError) ex : new GraalError(ex);
error.addContext("method: " + method.format("%r %H.%n(%p)") + " [" + reason + "]");
throw error;
}
}
use of org.graalvm.compiler.code.CompilationResult in project graal by oracle.
the class CompileQueue method printMethodHistogram.
private void printMethodHistogram() {
long sizeAllMethods = 0;
long sizeDeoptMethods = 0;
long sizeDeoptMethodsInNonDeopt = 0;
long sizeNonDeoptMethods = 0;
int numberOfMethods = 0;
int numberOfNonDeopt = 0;
int numberOfDeopt = 0;
long totalNumDeoptEntryPoints = 0;
long totalNumDuringCallEntryPoints = 0;
System.out.format("Code Size; Nodes Before; Nodes After; Is Trivial;" + " Deopt Target; Code Size; Nodes Before; Nodes After; Deopt Entries; Deopt During Call;" + " Entry Points; Direct Calls; Virtual Calls; Method\n");
List<CompileTask> tasks = new ArrayList<>(compilations.values());
tasks.sort(Comparator.comparing(t2 -> t2.method.format("%H.%n(%p) %r")));
for (CompileTask task : tasks) {
HostedMethod method = task.method;
CompilationResult result = task.result;
CompilationInfo ci = method.compilationInfo;
if (!ci.isDeoptTarget()) {
numberOfMethods += 1;
sizeAllMethods += result.getTargetCodeSize();
System.out.format("%8d; %5d; %5d; %s;", result.getTargetCodeSize(), ci.numNodesBeforeCompilation, ci.numNodesAfterCompilation, ci.isTrivialMethod ? "T" : " ");
int deoptMethodSize = 0;
if (ci.deoptTarget != null) {
CompilationInfo dci = ci.deoptTarget.compilationInfo;
numberOfDeopt += 1;
deoptMethodSize = compilations.get(ci.deoptTarget).result.getTargetCodeSize();
sizeDeoptMethods += deoptMethodSize;
sizeDeoptMethodsInNonDeopt += result.getTargetCodeSize();
totalNumDeoptEntryPoints += dci.numDeoptEntryPoints;
totalNumDuringCallEntryPoints += dci.numDuringCallEntryPoints;
System.out.format(" D; %6d; %5d; %5d; %4d; %4d;", deoptMethodSize, dci.numNodesBeforeCompilation, dci.numNodesAfterCompilation, dci.numDeoptEntryPoints, dci.numDuringCallEntryPoints);
} else {
sizeNonDeoptMethods += result.getTargetCodeSize();
numberOfNonDeopt += 1;
System.out.format(" ; %6d; %5d; %5d; %4d; %4d;", 0, 0, 0, 0, 0);
}
System.out.format(" %4d; %4d; %4d; %s\n", task.allReasons.stream().filter(t -> t instanceof EntryPointReason).count(), task.allReasons.stream().filter(t -> t instanceof DirectCallReason).count(), task.allReasons.stream().filter(t -> t instanceof VirtualCallReason).count(), method.format("%H.%n(%p) %r"));
}
}
System.out.println();
System.out.println("Size all methods ; " + sizeAllMethods);
System.out.println("Size deopt methods ; " + sizeDeoptMethods);
System.out.println("Size deopt methods in non-deopt mode ; " + sizeDeoptMethodsInNonDeopt);
System.out.println("Size non-deopt method ; " + sizeNonDeoptMethods);
System.out.println("Number of methods ; " + numberOfMethods);
System.out.println("Number of non-deopt methods ; " + numberOfNonDeopt);
System.out.println("Number of deopt methods ; " + numberOfDeopt);
System.out.println("Number of deopt entry points ; " + totalNumDeoptEntryPoints);
System.out.println("Number of deopt during calls entries ; " + totalNumDuringCallEntryPoints);
}
use of org.graalvm.compiler.code.CompilationResult in project graal by oracle.
the class NativeImageCodeCache method patchMethods.
/**
* Patch references from code to other code and constant data. Generate relocation information
* in the process. More patching can be done, and correspondingly fewer relocation records
* generated, if the caller passes a non-null rodataDisplacementFromText.
*
* @param relocs a relocation map
*/
public void patchMethods(RelocatableBuffer relocs) {
// in each compilation result...
for (Entry<HostedMethod, CompilationResult> entry : compilations.entrySet()) {
HostedMethod method = entry.getKey();
CompilationResult compilation = entry.getValue();
// the codecache-relative offset of the compilation
int compStart = method.getCodeAddressOffset();
AMD64InstructionPatcher patcher = new AMD64InstructionPatcher(compilation);
// ... patch direct call sites.
for (Infopoint infopoint : compilation.getInfopoints()) {
if (infopoint instanceof Call && ((Call) infopoint).direct) {
Call call = (Call) infopoint;
// NOTE that for the moment, we don't make static calls to external
// (e.g. native) functions. So every static call site has a target
// which is also in the code cache (a.k.a. a section-local call).
// This will change, and we will have to case-split here... but not yet.
int callTargetStart = ((HostedMethod) call.target).getCodeAddressOffset();
// Patch a PC-relative call.
// This code handles the case of section-local calls only.
int pcDisplacement = callTargetStart - (compStart + call.pcOffset);
patcher.findPatchData(call.pcOffset, pcDisplacement).apply(compilation.getTargetCode());
}
}
// ... and patch references to constant data
for (DataPatch dataPatch : compilation.getDataPatches()) {
/*
* Constants are allocated offsets in a separate space, which can be emitted as
* read-only (.rodata) section.
*/
AMD64InstructionPatcher.PatchData patchData = patcher.findPatchData(dataPatch.pcOffset, 0);
/*
* The relocation site is some offset into the instruction, which is some offset
* into the method, which is some offset into the text section (a.k.a. code cache).
* The offset we get out of the RelocationSiteInfo accounts for the first two, since
* we pass it the whole method. We add the method start to get the section-relative
* offset.
*/
long siteOffset = compStart + patchData.operandPosition;
/*
* Do we have an addend? Yes; it's constStart. BUT x86/x86-64 PC-relative references
* are relative to the *next* instruction. So, if the next instruction starts n
* bytes from the relocation site, we want to subtract n bytes from our addend.
*/
long addend = (patchData.nextInstructionPosition - patchData.operandPosition);
relocs.addPCRelativeRelocationWithAddend((int) siteOffset, patchData.operandSize, addend, dataPatch.reference);
}
}
}
use of org.graalvm.compiler.code.CompilationResult in project graal by oracle.
the class NativeImageCodeCache method printCompilationResults.
public void printCompilationResults() {
System.out.println("--- compiled methods");
for (Entry<HostedMethod, CompilationResult> entry : compilations.entrySet()) {
HostedMethod method = entry.getKey();
System.out.format("%8d %5d %s: frame %d\n", method.getCodeAddressOffset(), entry.getValue().getTargetCodeSize(), method.format("%H.%n(%p)"), entry.getValue().getTotalFrameSize());
}
System.out.println("--- vtables:");
for (HostedType type : imageHeap.getUniverse().getTypes()) {
for (int i = 0; i < type.getVTable().length; i++) {
HostedMethod method = type.getVTable()[i];
if (method != null) {
CompilationResult comp = compilations.get(type.getVTable()[i]);
if (comp != null) {
System.out.format("%d %s @ %d: %s = 0x%x\n", type.getTypeID(), type.toJavaName(false), i, method.format("%r %n(%p)"), method.getCodeAddressOffset());
}
}
}
}
}
use of org.graalvm.compiler.code.CompilationResult in project graal by oracle.
the class NativeImageCodeCache method writeCode.
public void writeCode(RelocatableBuffer buffer) {
int startPos = buffer.getPosition();
/*
* Compilation start offsets are relative to the beginning of the code cache (since the heap
* size is not fixed at the time they are computed). This is just startPos, i.e. we start
* emitting the code wherever the buffer is positioned when we're called.
*/
for (Entry<HostedMethod, CompilationResult> entry : compilations.entrySet()) {
HostedMethod method = entry.getKey();
CompilationResult compilation = entry.getValue();
buffer.setPosition(startPos + method.getCodeAddressOffset());
int codeSize = compilation.getTargetCodeSize();
buffer.putBytes(compilation.getTargetCode(), 0, codeSize);
for (int i = codeSize; i < ObjectLayout.roundUp(codeSize, CODE_ALIGNMENT); i++) {
buffer.putByte(CODE_FILLER_BYTE);
}
}
buffer.setPosition(startPos);
}
Aggregations