Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions packages/compiler/src/codegen/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ import type {
SymbolId,
TypeId,
} from "../semantics/ids.js";
import {
markCompilerPerfPhaseDuration,
startCompilerPerfPhase,
} from "../perf.js";
import { DiagnosticEmitter } from "../diagnostics/index.js";
import { createCodegenModule } from "./wasm-module.js";
import { createProgramHelperRegistry } from "./program-helpers.js";
Expand Down Expand Up @@ -94,6 +98,7 @@ export const codegenProgram = ({
options = {},
optimization,
}: CodegenProgramParams): CodegenResult => {
const codegenStartedAt = startCompilerPerfPhase();
const modules = Array.from(program.modules.values());
const mod = createCodegenModule();
const mergedOptions = normalizeCodegenOptions(options);
Expand Down Expand Up @@ -220,18 +225,28 @@ export const codegenProgram = ({
entryCtx.programHelpers.ensureEffectHelpers(entryCtx);
}

const outputModule = mergedOptions.optimize
? binaryen.readBinary(emitWasmBytes(mod))
: mod;
markCompilerPerfPhaseDuration("codegen", codegenStartedAt);

let outputModule = mod;
if (mergedOptions.optimize) {
const prepareStartedAt = startCompilerPerfPhase();
outputModule = binaryen.readBinary(emitWasmBytes(mod));
markCompilerPerfPhaseDuration("binaryen.prepareOptimizeModule", prepareStartedAt);
outputModule.setFeatures(VOYD_BINARYEN_FEATURES);
const optimizeStartedAt = startCompilerPerfPhase();
optimizeBinaryenModule({
module: outputModule,
profile: mergedOptions.optimizationProfile,
});
markCompilerPerfPhaseDuration("binaryen.optimize", optimizeStartedAt);
}

const wasm = mergedOptions.validate ? emitWasmBytes(outputModule) : undefined;
let wasm: Uint8Array | undefined;
if (mergedOptions.validate) {
const validateEmitStartedAt = startCompilerPerfPhase();
wasm = emitWasmBytes(outputModule);
markCompilerPerfPhaseDuration("binaryen.emitValidatedWasm", validateEmitStartedAt);
}
if (wasm) {
if (!WebAssembly.validate(wasm as BufferSource)) {
outputModule.validate();
Expand Down
19 changes: 19 additions & 0 deletions packages/compiler/src/optimize/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import type {
} from "../semantics/ids.js";
import type { SemanticsPipelineResult } from "../semantics/pipeline.js";
import type { CodegenOptions } from "../codegen/context.js";
import {
incrementCompilerPerfCounter,
markCompilerPerfPhaseDuration,
startCompilerPerfPhase,
} from "../perf.js";
import {
type ProgramOptimizationContext,
type ProgramOptimizationPass,
Expand Down Expand Up @@ -6043,7 +6048,21 @@ export const optimizeProgram = ({
void options;

OPTIMIZATION_PASSES.forEach((pass) => {
const passStartedAt = startCompilerPerfPhase();
const result = pass.run(context);
markCompilerPerfPhaseDuration(
`optimize.pass.${pass.name}`,
passStartedAt,
);
if (result.changed) {
incrementCompilerPerfCounter(`optimize.pass.${pass.name}.changed`);
}
if (result.invalidates?.length) {
incrementCompilerPerfCounter(
`optimize.pass.${pass.name}.invalidations`,
result.invalidates.length,
);
}
if (result.invalidates?.length) {
context.invalidateAnalyses(result.invalidates);
}
Expand Down
128 changes: 128 additions & 0 deletions packages/compiler/src/perf.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
type CompilerPerfCounterSnapshot = Map<string, number>;
type CompilerPerfPhaseSnapshot = Map<string, number>;

type CompilerPerfSummary = {
entryPath: string;
success: boolean;
phasesMs: Readonly<Record<string, number>>;
counters: Readonly<Record<string, number>>;
diagnostics: number;
overlapped?: boolean;
};

export type CompilerPerfSession = {
entryPath: string;
enabled: boolean;
startedAt: number;
countersBefore?: CompilerPerfCounterSnapshot;
phasesBefore?: CompilerPerfPhaseSnapshot;
overlapped?: boolean;
};

const COMPILER_PERF_ENV = "VOYD_COMPILER_PERF";
Expand All @@ -25,6 +36,8 @@ const PERF_ENABLED = (() => {
})();

const counters = new Map<string, number>();
const phaseDurationsMs = new Map<string, number>();
const activeSessions = new Set<CompilerPerfSession>();

const roundMs = (value: number): number =>
Math.round(value * 1000) / 1000;
Expand All @@ -50,9 +63,35 @@ export const incrementCompilerPerfCounter = (
counters.set(name, (counters.get(name) ?? 0) + amount);
};

export const startCompilerPerfPhase = (): number =>
PERF_ENABLED ? performance.now() : 0;

export const addCompilerPerfPhaseDuration = (
name: string,
durationMs: number,
): void => {
if (!PERF_ENABLED || durationMs <= 0) {
return;
}
phaseDurationsMs.set(name, (phaseDurationsMs.get(name) ?? 0) + durationMs);
};

export const markCompilerPerfPhaseDuration = (
name: string,
startedAt: number,
): void => {
if (!PERF_ENABLED) {
return;
}
addCompilerPerfPhaseDuration(name, performance.now() - startedAt);
};

export const snapshotCompilerPerfCounters = (): CompilerPerfCounterSnapshot =>
PERF_ENABLED ? new Map(counters) : new Map();

export const snapshotCompilerPerfPhases = (): CompilerPerfPhaseSnapshot =>
PERF_ENABLED ? new Map(phaseDurationsMs) : new Map();

export const diffCompilerPerfCounters = ({
before,
after,
Expand All @@ -75,6 +114,28 @@ export const diffCompilerPerfCounters = ({
return toSortedRecord(delta);
};

export const diffCompilerPerfPhases = ({
before,
after,
}: {
before: ReadonlyMap<string, number>;
after: ReadonlyMap<string, number>;
}): Record<string, number> => {
if (!PERF_ENABLED) {
return {};
}

const keys = new Set<string>([...before.keys(), ...after.keys()]);
const delta = new Map<string, number>();
keys.forEach((key) => {
const diff = (after.get(key) ?? 0) - (before.get(key) ?? 0);
if (diff !== 0) {
delta.set(key, diff);
}
});
return toSortedRecord(delta);
};

export const normalizeCompilerPerfPhases = (
phasesMs: Readonly<Record<string, number>>,
): Record<string, number> =>
Expand All @@ -90,6 +151,7 @@ export const logCompilerPerfSummary = ({
phasesMs,
counters,
diagnostics,
overlapped,
}: CompilerPerfSummary): void => {
if (!PERF_ENABLED) {
return;
Expand All @@ -101,7 +163,73 @@ export const logCompilerPerfSummary = ({
diagnostics,
phasesMs: normalizeCompilerPerfPhases(phasesMs),
counters,
...(overlapped ? { overlapped: true } : {}),
};

console.error(`[voyd:compiler:perf] ${JSON.stringify(summary)}`);
};

export const startCompilerPerfSession = ({
entryPath,
}: {
entryPath: string;
}): CompilerPerfSession => {
if (!PERF_ENABLED) {
return {
entryPath,
enabled: false,
startedAt: 0,
};
}

const session: CompilerPerfSession = {
entryPath,
enabled: true,
startedAt: performance.now(),
countersBefore: snapshotCompilerPerfCounters(),
phasesBefore: snapshotCompilerPerfPhases(),
};
if (activeSessions.size > 0) {
session.overlapped = true;
activeSessions.forEach((activeSession) => {
activeSession.overlapped = true;
});
}
activeSessions.add(session);
return session;
};

export const completeCompilerPerfSession = ({
session,
success,
diagnostics,
}: {
session: CompilerPerfSession;
success: boolean;
diagnostics: number;
}): void => {
if (!session.enabled || !session.countersBefore || !session.phasesBefore) {
return;
}
activeSessions.delete(session);

const phases = diffCompilerPerfPhases({
before: session.phasesBefore,
after: snapshotCompilerPerfPhases(),
});

logCompilerPerfSummary({
entryPath: session.entryPath,
success,
diagnostics,
phasesMs: {
...phases,
total: performance.now() - session.startedAt,
},
counters: diffCompilerPerfCounters({
before: session.countersBefore,
after: snapshotCompilerPerfCounters(),
}),
overlapped: session.overlapped,
});
};
Loading
Loading