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
11 changes: 7 additions & 4 deletions ext/standard/JitGcCollectCyclesStandaloneKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ public static function ensureCycleScanInternals(Context $context): void
'phpc_gc_clear_slots_pointing_to' => [$voidTy, false, [$i8p]],
];
foreach ($internals as $name => [$ret, $vararg, $params]) {
if (null !== $context->module->getNamedFunction($name)) {
continue;
$fn = $context->module->getNamedFunction($name);
if (null === $fn) {
$fn = $context->module->addFunction(
$name,
$context->context->functionType($ret, $vararg, ...$params)
);
}
$ft = $context->context->functionType($ret, $vararg, ...$params);
$context->registerFunction($name, $context->module->addFunction($name, $ft));
$context->registerFunction($name, $fn);
}

self::implementSlotReadObject($context);
Expand Down
22 changes: 15 additions & 7 deletions lib/JIT/Builtin/BackedEnumFromRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,21 +477,29 @@ private static function raisePendingFromString(Context $context, Value $messageS

private static function ensureExternals(Context $context): void
{
$i32 = $context->getTypeFromString('int32');
$i8p = $context->getTypeFromString('int8*');
$sizeT = $context->getTypeFromString('size_t');
$longTy = $context->getTypeFromString('int64');
$i8pp = $context->getTypeFromString('int8**');
$charPtr = $context->getTypeFromString('char*');
$int32 = $context->getTypeFromString('int32');
// Canonical vararg snprintf via LibcExtern — a non-vararg addFunction('snprintf')
// silently becomes snprintf.1 and Module.php:180 aborts (#32122 / #31894).
LibcExtern::ensureSnprintf($context);
foreach ([
['snprintf', $i32, [$charPtr, $sizeT, $charPtr]],
['strtol', $longTy, [$i8p, $i8pp, $int32]],
] as [$name, $ret, $params]) {
if (null === $context->module->getNamedFunction($name)) {
$ft = $context->context->functionType($ret, false, ...$params);
$context->registerFunction($name, $context->module->addFunction($name, $ft));
try {
$context->lookupFunction($name);
continue;
} catch (\Throwable) {
}
$fn = $context->module->getNamedFunction($name);
if (null === $fn) {
$fn = $context->module->addFunction(
$name,
$context->context->functionType($ret, false, ...$params)
);
}
$context->registerFunction($name, $fn);
}
}

Expand Down
19 changes: 11 additions & 8 deletions lib/JIT/Builtin/GcCollectCyclesRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ private static function declareFunction(
\PHPLLVM\Type $returnType,
array $paramTypes
): void {
if (null !== $context->module->getNamedFunction($name)) {
return;
$fn = $context->module->getNamedFunction($name);
if (null === $fn) {
$fnType = $context->context->functionType($returnType, false, ...$paramTypes);
$fn = $context->module->addFunction($name, $fnType);
}
$fnType = $context->context->functionType($returnType, false, ...$paramTypes);
$fn = $context->module->addFunction($name, $fnType);
$context->registerFunction($name, $fn);
}

Expand Down Expand Up @@ -571,11 +571,14 @@ private static function ensureInternalDeclarations(Context $context): void
'phpc_object_release_storage' => [$voidTy, false, [$i8p]],
];
foreach ($internals as $name => [$ret, $vararg, $params]) {
if (null !== $context->module->getNamedFunction($name)) {
continue;
$fn = $context->module->getNamedFunction($name);
if (null === $fn) {
$fn = $context->module->addFunction(
$name,
$context->context->functionType($ret, $vararg, ...$params)
);
}
$ft = $context->context->functionType($ret, $vararg, ...$params);
$context->registerFunction($name, $context->module->addFunction($name, $ft));
$context->registerFunction($name, $fn);
}

self::implementIndexOf($context);
Expand Down
16 changes: 11 additions & 5 deletions lib/JIT/Builtin/NumberFormatRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,28 +250,34 @@ private static function copyBytes(

private static function ensureDecls(Context $context): void
{
$i32 = $context->getTypeFromString('int32');
$i64 = $context->getTypeFromString('int64');
$charPtr = $context->getTypeFromString('char*');
$sizeT = $context->getTypeFromString('size_t');
$i8p = $context->getTypeFromString('int8*');
$strPtr = $context->getTypeFromString('__string__*');
$voidTy = $context->getTypeFromString('void');

LibcExtern::ensureSnprintf($context);
foreach (
[
'snprintf' => [$i32, true, [$charPtr, $sizeT, $charPtr]],
'__mm__malloc' => [$i8p, false, [$sizeT]],
'__mm__free' => [$voidTy, false, [$i8p]],
'__string__init' => [$strPtr, false, [$i64, $i8p]],
] as $name => [$ret, $vararg, $params]
) {
try {
$context->lookupFunction($name);
continue;
} catch (\Throwable) {
$ft = $context->context->functionType($ret, $vararg, ...$params);
$context->registerFunction($name, $context->module->addFunction($name, $ft));
}
// Reuse the module symbol when lookupFunction misses the registry (#32122 / #31894).
$fn = $context->module->getNamedFunction($name);
if (null === $fn) {
$fn = $context->module->addFunction(
$name,
$context->context->functionType($ret, $vararg, ...$params)
);
}
$context->registerFunction($name, $fn);
}
}
}
8 changes: 7 additions & 1 deletion lib/JIT/Builtin/ParseUrlRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,15 @@ private static function ensureExternal(Context $context, string $name, $ft): voi
{
try {
$context->lookupFunction($name);

return;
} catch (\Throwable) {
$context->registerFunction($name, $context->module->addFunction($name, $ft));
}
$fn = $context->module->getNamedFunction($name);
if (null === $fn) {
$fn = $context->module->addFunction($name, $ft);
}
$context->registerFunction($name, $fn);
}

private static function registerLinkedRuntime(Context $context): void
Expand Down
16 changes: 11 additions & 5 deletions lib/JIT/Builtin/SprintfSnprintfRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,15 @@ private static function ensureDecls(Context $context): void
{
$double = $context->getTypeFromString('double');
$i64 = $context->getTypeFromString('int64');
$i32 = $context->getTypeFromString('int32');
$charPtr = $context->getTypeFromString('char*');
$sizeT = $context->getTypeFromString('size_t');
$i8p = $context->getTypeFromString('int8*');
$strPtr = $context->getTypeFromString('__string__*');
$valuePtr = $context->getTypeFromString('__value__*');
$voidTy = $context->getTypeFromString('void');

LibcExtern::ensureSnprintf($context);
foreach (
[
'snprintf' => [$i32, true, [$charPtr, $sizeT, $charPtr]],
'__mm__malloc' => [$i8p, false, [$sizeT]],
'__mm__free' => [$voidTy, false, [$i8p]],
'__string__init' => [$strPtr, false, [$i64, $i8p]],
Expand All @@ -214,10 +212,18 @@ private static function ensureDecls(Context $context): void
) {
try {
$context->lookupFunction($name);
continue;
} catch (\Throwable) {
$ft = $context->context->functionType($ret, $vararg, ...$params);
$context->registerFunction($name, $context->module->addFunction($name, $ft));
}
// Reuse the module symbol when lookupFunction misses the registry (#32122 / #31894).
$fn = $context->module->getNamedFunction($name);
if (null === $fn) {
$fn = $context->module->addFunction(
$name,
$context->context->functionType($ret, $vararg, ...$params)
);
}
$context->registerFunction($name, $fn);
}
LibcExtern::ensureMemcpyImplemented($context);
}
Expand Down
22 changes: 15 additions & 7 deletions lib/JIT/Builtin/ZendDoubleStringRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,11 @@ private static function snprintfCall(
$buf = $context->builder->call($context->lookupFunction('__mm__malloc'), $bufSize);
$bufChar = $context->builder->pointerCast($buf, $charPtr);
$fmtPtr = $context->builder->pointerCast($context->constantFromString($fmt), $charPtr);
// snprintf(3) via LibcExtern::ensureSnprintf after always-on drop (#32092).
// Always reuse the named decl — addFunction('snprintf') without getNamedFunction
// silently creates snprintf.1 and Module.php:180 aborts (#32122 / #31894).
LibcExtern::ensureSnprintf($context);
if (null === $precisionArg) {
// snprintf(3) via LibcExtern::ensureSnprintf after always-on drop (#32092).
LibcExtern::ensureSnprintf($context);
$written = $context->builder->call(
$context->lookupFunction('snprintf'),
$bufChar,
Expand Down Expand Up @@ -772,18 +774,16 @@ private static function snprintfCall(

private static function ensureDecls(Context $context): void
{
$double = $context->getTypeFromString('double');
$i32 = $context->getTypeFromString('int32');
$charPtr = $context->getTypeFromString('char*');
$sizeT = $context->getTypeFromString('size_t');
$i8p = $context->getTypeFromString('int8*');
$i64 = $context->getTypeFromString('int64');
$strPtr = $context->getTypeFromString('__string__*');
$voidTy = $context->getTypeFromString('void');

LibcExtern::ensureSnprintf($context);
foreach (
[
'snprintf' => [$i32, true, [$charPtr, $sizeT, $charPtr]],
'__mm__malloc' => [$i8p, false, [$sizeT]],
'__mm__free' => [$voidTy, false, [$i8p]],
'__string__init' => [$strPtr, false, [$i64, $charPtr]],
Expand All @@ -792,10 +792,18 @@ private static function ensureDecls(Context $context): void
) {
try {
$context->lookupFunction($name);
continue;
} catch (\Throwable) {
$ft = $context->context->functionType($ret, $vararg, ...$params);
$context->registerFunction($name, $context->module->addFunction($name, $ft));
}
// Reuse the module symbol when lookupFunction misses the registry (#32122 / #31894).
$fn = $context->module->getNamedFunction($name);
if (null === $fn) {
$fn = $context->module->addFunction(
$name,
$context->context->functionType($ret, $vararg, ...$params)
);
}
$context->registerFunction($name, $fn);
}
}
}
39 changes: 39 additions & 0 deletions test/unit/SnprintfRuntimeShrinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,45 @@ public function testNumberFormatAndDateRouteSnprintfThroughEnsure(): void
$this->assertStringNotContainsString('function ensureSnprintf', $date);
}

public function testFloatFormattersReuseNamedSnprintfNotSnprintfDotOne(): void
{
$files = [
__DIR__.'/../../lib/JIT/Builtin/ZendDoubleStringRuntime.php',
__DIR__.'/../../lib/JIT/Builtin/SprintfSnprintfRuntime.php',
__DIR__.'/../../lib/JIT/Builtin/NumberFormatRuntime.php',
__DIR__.'/../../lib/JIT/Builtin/BackedEnumFromRuntime.php',
];
foreach ($files as $path) {
$src = (string) file_get_contents($path);
$this->assertStringContainsString('LibcExtern::ensureSnprintf', $src, $path);
$this->assertStringContainsString('#32122', $src, $path);
$this->assertStringNotContainsString(
"'snprintf' => [\$i32, true, [\$charPtr, \$sizeT, \$charPtr]]",
$src,
$path
);
}
$enum = (string) file_get_contents(__DIR__.'/../../lib/JIT/Builtin/BackedEnumFromRuntime.php');
$this->assertStringNotContainsString("['snprintf', \$i32, [\$charPtr, \$sizeT, \$charPtr]]", $enum);
}

public function testGcInternalsRegisterExistingNamedFunctions(): void
{
$gc = (string) file_get_contents(__DIR__.'/../../lib/JIT/Builtin/GcCollectCyclesRuntime.php');
$this->assertStringContainsString('$context->registerFunction($name, $fn);', $gc);
$this->assertStringNotContainsString(
"if (null !== \$context->module->getNamedFunction(\$name)) {\n continue;",
$gc
);
$standalone = (string) file_get_contents(
__DIR__.'/../../ext/standard/JitGcCollectCyclesStandaloneKernel.php'
);
$this->assertStringContainsString('$context->registerFunction($name, $fn);', $standalone);
$parseUrl = (string) file_get_contents(__DIR__.'/../../lib/JIT/Builtin/ParseUrlRuntime.php');
$this->assertStringContainsString('getNamedFunction($name)', $parseUrl);
$this->assertStringContainsString('$context->registerFunction($name, $fn);', $parseUrl);
}

public function testWeakRefRegistryUsesCanonicalSnprintfPrototype(): void
{
$source = (string) file_get_contents(__DIR__.'/../../lib/JIT/Builtin/WeakRefRegistryRuntime.php');
Expand Down
Loading