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
44 changes: 44 additions & 0 deletions ext/intl/JitGrapheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PHPCompiler\JIT\Builtin\StringUtf8Runtime;
use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\HashTable;
use PHPCompiler\VM\Variable;
use PHPLLVM\Builder;
use PHPLLVM\Value;

Expand Down Expand Up @@ -50,6 +52,33 @@ public static function tryStrlenFold(Context $context, array $args): ?Value
return $context->constantFromInteger($result, 'int64');
}

/**
* @param JITVariable[] $args
*/
public static function tryStrSplitFold(Context $context, array $args): ?Value
{
$string = self::compileTimeString($args, 0);
if (null === $string) {
return null;
}
$length = 1;
if (isset($args[1])) {
$lengthCt = self::compileTimeInt($args, 1);
if (null === $lengthCt) {
return null;
}
$length = $lengthCt;
}
$parts = VmGrapheme::strSplit($string, $length);
if (false === $parts) {
return $context->getTypeFromString('bool')->constInt(0, false);
}
$ht = self::hashTableFromStringList($parts);
$cacheKey = 'grapheme_str_split_'.md5($string."\0".$length);

return $context->constantArrayFromVmHashTable($cacheKey, $ht);
}

/**
* @param JITVariable[] $args
*/
Expand Down Expand Up @@ -273,4 +302,19 @@ private static function compileTimeBool(array $args, int $index): ?bool

return (bool) ($args[$index]->compileTimeBool ?? false);
}

/**
* @param list<string> $parts
*/
private static function hashTableFromStringList(array $parts): HashTable
{
$ht = new HashTable();
foreach ($parts as $part) {
$stored = new Variable();
$stored->string($part);
$ht->append($stored);
}

return $ht;
}
}
10 changes: 8 additions & 2 deletions ext/intl/grapheme_str_split.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPCompiler\Frame;
use PHPCompiler\Func\Internal;
use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\JitStringBuiltinArg;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\BuiltinExecute;
use PHPCompiler\VM\HashTable;
Expand All @@ -18,7 +19,7 @@
/**
* grapheme_str_split() — split string into grapheme clusters (php-src ext/intl/grapheme; #5958).
*
* VM: {@see VmGrapheme}; JIT: compile-time fold via {@see JitGrapheme} (runtime defer #5958).
* VM: {@see VmGrapheme}; JIT: compile-time fold via {@see JitGrapheme::tryStrSplitFold} (#5958, #6246).
*/
final class grapheme_str_split extends Internal
{
Expand Down Expand Up @@ -76,9 +77,14 @@ public function call(Context $context, JITVariable ...$args): Value
if ($argc < 1 || $argc > 2) {
throw new \LogicException('grapheme_str_split() requires one or two arguments');
}
$folded = JitGrapheme::tryStrSplitFold($context, $args);
if (null !== $folded) {
return $folded;
}
JitStringBuiltinArg::lower($context, $args[0], 'grapheme_str_split', 0, 'string');

throw new \LogicException(
'grapheme_str_split() JIT runtime lowering is deferred; use VM (#5958)'
'grapheme_str_split() JIT runtime lowering is deferred; use VM or compile-time literals (#5958)'
);
}
}
32 changes: 32 additions & 0 deletions test/compliance/cases/stdlib/grapheme_str_split_jit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
stdlib grapheme_str_split() JIT — compile-time fold (#5958, #6246)
--FILE--
<?php
echo (int) function_exists('grapheme_str_split'), "\n";

$one = grapheme_str_split("e\xCC\x81");
var_export($one);
echo "\n";

$ascii = grapheme_str_split('abc');
var_export($ascii);
echo "\n";

$chunked = grapheme_str_split('abcdef', 2);
var_export($chunked);
echo "\n";
--EXPECT--
0
array (
0 => 'é',
)
array (
0 => 'a',
1 => 'b',
2 => 'c',
)
array (
0 => 'ab',
1 => 'cd',
2 => 'ef',
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

// Compile-only (#6246): grapheme_str_split compile-time fold for AOT lint.
function probe_str_split(string $string, int $length = 1): array|false
{
return grapheme_str_split($string, $length);
}
37 changes: 37 additions & 0 deletions test/unit/GraphemeStrSplitJitCompileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace PHPCompiler;

use PHPUnit\Framework\TestCase;

/** AOT lint verify for grapheme_str_split() JIT compile-time fold (#6246). */
final class GraphemeStrSplitJitCompileTest extends TestCase
{
public function testGraphemeStrSplitAotLint(): void
{
$root = dirname(__DIR__, 2);
$bin = realpath($root.'/bin/compile.php');
$this->assertNotFalse($bin);
$target = $root.'/test/fixtures/aot/compile-only/grapheme_str_split_literals.php';
$cmd = [PHP_BINARY, $bin, '-l', $target];
$descriptorSpec = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'w'],
];
$proc = proc_open($cmd, $descriptorSpec, $pipes, $root);
$this->assertIsResource($proc);
fclose($pipes[0]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$exit = proc_close($proc);
$this->assertSame(
0,
$exit,
trim($stderr !== false ? $stderr : '')."\n".'compile.php -l failed for grapheme_str_split_literals.php'
);
}
}