Skip to content
Merged
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
133 changes: 128 additions & 5 deletions lib/JIT.php
Original file line number Diff line number Diff line change
Expand Up @@ -3577,7 +3577,8 @@ private function cacheM3EmitTuTrivialEchoAtLinkTime(): void
$this->registerM3EmitTuSidecarFromPath(
__DIR__.'/../bin/vm.php',
\PHPCompiler\JIT\M3EmitTuTrivialEchoAot::BIN_VM_SIDECAR_REL,
'PHPCompiler\\JIT\\M3EmitTuTrivialEchoAot::binVmSentinelBlock'
'PHPCompiler\\JIT\\M3EmitTuTrivialEchoAot::binVmSentinelBlock',
true
);
$this->registerM3EmitTuSidecarFromPath(
__DIR__.'/../src/cli_driver.php',
Expand Down Expand Up @@ -3657,7 +3658,8 @@ private function cacheM3EmitTuTrivialEchoAtLinkTime(): void
$this->registerM3EmitTuSidecarFromPath(
__DIR__.'/../bin/vm.php',
\PHPCompiler\JIT\M3EmitTuTrivialEchoAot::BIN_VM_SIDECAR_REL,
'PHPCompiler\\JIT\\M3EmitTuTrivialEchoAot::binVmSentinelBlock'
'PHPCompiler\\JIT\\M3EmitTuTrivialEchoAot::binVmSentinelBlock',
true
);
$this->registerM3EmitTuSidecarFromPath(
__DIR__.'/../src/cli_driver.php',
Expand Down Expand Up @@ -3792,12 +3794,18 @@ private function registerM3EmitTuSidecarFromPath(
$code,
$aotBytes,
$sidecarRel,
$sentinelLogical
$sentinelLogical,
false,
$this->m3EmitTuSidecarSourcePathNorm($path)
);

return;
}
}
if (\PHPCompiler\JIT\M3EmitTuTrivialEchoAot::BIN_VM_SIDECAR_REL === $sidecarRel
&& $this->registerM3BinVmSidecarStubFallback($path, $sidecarRel, $sentinelLogical, $code, $repoRoot)) {
return;
}
// Zend host-compile of bin/compile.php inventory argv driver SIGSEGVs — reuse committed gen-0 (#2930).
if (\PHPCompiler\JIT\M3EmitTuTrivialEchoAot::BIN_COMPILE_SIDECAR_REL === $sidecarRel) {
foreach (
Expand Down Expand Up @@ -3975,12 +3983,17 @@ private function registerM3EmitTuSidecarFromPath(
$code,
$aotBytes,
$sidecarRel,
$sentinelLogical
$sentinelLogical,
false,
$this->m3EmitTuSidecarSourcePathNorm($path)
);

return;
}
}
if ($this->registerM3BinVmSidecarStubFallback($path, $sidecarRel, $sentinelLogical, $code, $repoRoot)) {
return;
}

return;
}
Expand All @@ -4000,10 +4013,120 @@ private function registerM3EmitTuSidecarFromPath(
$aotBytes,
$sidecarRel,
$sentinelLogical,
$vendorObjectSidecar
$vendorObjectSidecar,
$this->m3EmitTuSidecarSourcePathNorm($path)
);
}

private function m3EmitTuSidecarSourcePathNorm(string $path): ?string
{
$norm = str_replace('\\', '/', $path);
if (!str_ends_with($norm, '/bin/vm.php')) {
return null;
}
$resolved = realpath($path);

return false !== $resolved ? str_replace('\\', '/', $resolved) : $norm;
}

/**
* bin/vm.php honest AOT still LLVM-segfaults; register path-keyed stub sidecar (#2699, #1492).
*/
private function registerM3BinVmSidecarStubFallback(
string $path,
string $sidecarRel,
string $sentinelLogical,
string $code,
string $repoRoot
): bool {
if (\PHPCompiler\JIT\M3EmitTuTrivialEchoAot::BIN_VM_SIDECAR_REL !== $sidecarRel) {
return false;
}
$sourcePathNorm = $this->m3EmitTuSidecarSourcePathNorm($path);
if (null === $sourcePathNorm) {
return false;
}
foreach (
[
$repoRoot.'/prelinked/bootstrap-gen0/.m3_bin_vm_aot_blob',
$repoRoot.'/'.ltrim($sidecarRel, '/'),
] as $prelinked
) {
if (!is_readable($prelinked)) {
continue;
}
$aotBytes = file_get_contents($prelinked);
if (!is_string($aotBytes) || '' === $aotBytes) {
continue;
}
\PHPCompiler\JIT\M3EmitTuTrivialEchoAot::registerLinktime(
$this->context,
$repoRoot,
$code,
$aotBytes,
$sidecarRel,
$sentinelLogical,
true,
$sourcePathNorm
);

return true;
}
$stub = $repoRoot.'/test/bootstrap-aot/bin_vm_sidecar_stub.php';
if (!is_readable($stub)) {
return false;
}
$tmpOut = sys_get_temp_dir().'/m3_bin_vm_sidecar_stub_'.getmypid();
@unlink($tmpOut);
$compileCmd = 'php';
$memLimit = getenv('PHP_COMPILER_MEMORY_LIMIT');
if (is_string($memLimit) && '' !== $memLimit && '-1' !== $memLimit) {
$compileCmd .= ' -d memory_limit='.escapeshellarg($memLimit);
}
$compileCmd .= ' '.escapeshellarg($repoRoot.'/bin/compile.php')
.' -o '.escapeshellarg($tmpOut)
.' '.escapeshellarg($stub);
$compileEnv = $this->m3EmitSidecarHostCompileEnv();
$compileEnv['PHP_COMPILER_SELFHOST_AOT'] = '1';
$compileEnv['PHP_COMPILER_M3_SIDECAR_HOST'] = '1';
$compileEnv['PHP_COMPILER_M3_EMIT_SIDECAR_RECURSION_GUARD'] = '1';
unset($compileEnv['PHP_COMPILER_EMIT_HELPER_LINK'], $compileEnv['PHP_COMPILER_M3_EMIT_TU']);
$descriptor = [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']];
$proc = proc_open($compileCmd, $descriptor, $pipes, $repoRoot, $compileEnv);
if (!is_resource($proc)) {
return false;
}
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
$exit = proc_close($proc);
if (0 !== $exit || !is_readable($tmpOut)) {
@unlink($tmpOut);

return false;
}
$aotBytes = file_get_contents($tmpOut);
@unlink($tmpOut);
if (!is_string($aotBytes) || '' === $aotBytes) {
return false;
}
$repoSidecar = $repoRoot.'/'.ltrim($sidecarRel, '/');
@file_put_contents($repoSidecar, $aotBytes);
@chmod($repoSidecar, 0755);
\PHPCompiler\JIT\M3EmitTuTrivialEchoAot::registerLinktime(
$this->context,
$repoRoot,
$code,
$aotBytes,
$sidecarRel,
$sentinelLogical,
true,
$sourcePathNorm
);

return true;
}

/**
* Emit TU: native LLVM stubs for Runtime spine — avoid PHP CFG (PHPTypes global ctor; #2540).
*/
Expand Down
3 changes: 2 additions & 1 deletion lib/JIT/IncludeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,8 @@ private static function shouldStubM3SidecarHostNonLiteralInclude(Block $callerBl
}
$caller = str_replace('\\', '/', $callerBlock->scriptPath());

return str_ends_with($caller, '/src/cli_driver.php')
return str_ends_with($caller, '/bin/vm.php')
|| str_ends_with($caller, '/src/cli_driver.php')
|| str_ends_with($caller, '/test/selfhost/compiler_lib_spine_smoke/main.php');
}

Expand Down
22 changes: 20 additions & 2 deletions lib/JIT/M3EmitTuTrivialEchoAot.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public static function registerLinktime(
string $aotBytes,
string $sidecarRel = self::TRIVIAL_ECHO_SIDECAR_REL,
?string $sentinelLogical = null,
bool $objectOnlySidecar = false
bool $objectOnlySidecar = false,
?string $sourcePathNorm = null
): void {
if ('' === $aotBytes || in_array($sidecarRel, self::$registeredSidecarRels, true)) {
return;
Expand Down Expand Up @@ -137,11 +138,16 @@ public static function registerLinktime(
$context->functionProxies[$sentinelLc] = new Call\Native($func, $sentinelLogical, [], []);
}

$sourcePathGlobal = null;
if (is_string($sourcePathNorm) && '' !== $sourcePathNorm) {
$sourcePathGlobal = $context->constantStringFromString($sourcePathNorm);
}
$context->m3EmitTuLinktimeSidecarEntries[] = [
'sourceGlobal' => $sourceGlobal,
'sidecarGlobal' => $sidecarGlobal,
'sentinelLc' => $sentinelLc,
'objectOnly' => $objectOnlySidecar,
'sourcePathGlobal' => $sourcePathGlobal,
];

if (null === $context->m3EmitTuTrivialEchoSourceGlobal) {
Expand Down Expand Up @@ -184,7 +190,19 @@ public static function emitParseAndCompileWithTrivialFallback(
foreach ($entries as $index => $entry) {
$tag = 'e'.(string) $index;
$cached = $context->builder->load($entry['sourceGlobal']);
$matches = JitStringCompare::identical($context, $code, $cached);
$contentMatch = JitStringCompare::identical($context, $code, $cached);
$pathMatch = null;
if (isset($entry['sourcePathGlobal']) && null !== $entry['sourcePathGlobal']) {
$pathMatch = JitStringCompare::identical(
$context,
$filename,
$context->builder->load($entry['sourcePathGlobal'])
);
}
$matches = $contentMatch;
if (null !== $pathMatch) {
$matches = $context->builder->or($contentMatch, $pathMatch);
}
$ok = BasicBlockHelper::append($context, 'm3te_pac_sidecar_'.$tag);
$fail = BasicBlockHelper::append($context, 'm3te_pac_next_'.$tag);
$context->builder->branchIf($matches, $ok, $fail);
Expand Down
9 changes: 8 additions & 1 deletion script/bootstrap-selfhost-helloworld-compile-bin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,14 @@ rm -f "${OUT}" "${AOT_OUT}" "${PHP_COMPILER_JIT_PROGRESS_FILE}"

set +e
link_out="$(
php "${ROOT}/bin/compile.php" -o "${OUT}" "${ENTRY}" 2>&1
env PHP_COMPILER_SELFHOST_AOT=1 \
PHP_COMPILER_M3_COMPILE_DRIVER=1 \
PHP_COMPILER_EMIT_HELPER_LINK=1 \
PHP_COMPILER_M3_COMPILE_DRIVER_MAIN=1 \
PHP_COMPILER_M3_INVENTORY_EMIT_DRIVER=1 \
BOOTSTRAP_M3_USE_INVENTORY_EMIT_DRIVER=1 \
PHP_COMPILER_M3_EMIT_LOG_PREFIX=helloworld_compile_smoke \
php "${ROOT}/bin/compile.php" -o "${OUT}" "${ENTRY}" 2>&1
)"
link_code=$?
set -e
Expand Down
18 changes: 18 additions & 0 deletions test/bootstrap-aot/bin_vm_sidecar_stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

/**
* M5 bin/vm.php link-time sidecar stub (#2699, #1492).
*
* Full bin/vm.php AOT still LLVM-segfaults under self-host; emit-helper matches
* bin/vm.php by source path and copies this TU until honest VM init is green.
*/
function run(string $filename, string $code, array $options): void
{
echo "vm driver ok\n";
}

if (!(\function_exists('php_compiler_cli_should_skip_entry_driver') && php_compiler_cli_should_skip_entry_driver())) {
echo "bin_vm_sidecar_stub ready\n";
}