Skip to content

Commit

Permalink
8309254: Implement fast-path for ASCII-compatible CharsetEncoders on …
Browse files Browse the repository at this point in the history
…RISC-V

Reviewed-by: fyang
Backport-of: 61bb014a8692305c705a4cf0361e319275c35ca3
  • Loading branch information
DingliZhang authored and RealFYang committed Aug 20, 2023
1 parent 4dca60e commit 0fa04fe
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
36 changes: 25 additions & 11 deletions src/hotspot/cpu/riscv/c2_MacroAssembler_riscv.cpp
Expand Up @@ -1500,26 +1500,39 @@ void C2_MacroAssembler::byte_array_inflate_v(Register src, Register dst, Registe

// Compress char[] array to byte[].
// result: the array length if every element in array can be encoded; 0, otherwise.
void C2_MacroAssembler::char_array_compress_v(Register src, Register dst, Register len, Register result, Register tmp) {
void C2_MacroAssembler::char_array_compress_v(Register src, Register dst, Register len,
Register result, Register tmp) {
Label done;
encode_iso_array_v(src, dst, len, result, tmp);
encode_iso_array_v(src, dst, len, result, tmp, false);
beqz(len, done);
mv(result, zr);
bind(done);
}

// result: the number of elements had been encoded.
void C2_MacroAssembler::encode_iso_array_v(Register src, Register dst, Register len, Register result, Register tmp) {
Label loop, DIFFERENCE, DONE;
// Intrinsic for
//
// - sun/nio/cs/ISO_8859_1$Encoder.implEncodeISOArray
// return the number of characters copied.
// - java/lang/StringUTF16.compress
// return zero (0) if copy fails, otherwise 'len'.
//
// This version always returns the number of characters copied. A successful
// copy will complete with the post-condition: 'res' == 'len', while an
// unsuccessful copy will exit with the post-condition: 0 <= 'res' < 'len'.
//
// Clobbers: src, dst, len, result, t0
void C2_MacroAssembler::encode_iso_array_v(Register src, Register dst, Register len,
Register result, Register tmp, bool ascii) {
Label loop, fail, done;

BLOCK_COMMENT("encode_iso_array_v {");
mv(result, 0);

bind(loop);
mv(tmp, 0xff);
mv(tmp, ascii ? 0x7f : 0xff);
vsetvli(t0, len, Assembler::e16, Assembler::m2);
vle16_v(v2, src);
// if element > 0xff, stop

vmsgtu_vx(v1, v2, tmp);
vfirst_m(tmp, v1);
vmsbf_m(v0, v1);
Expand All @@ -1528,18 +1541,19 @@ void C2_MacroAssembler::encode_iso_array_v(Register src, Register dst, Register
vncvt_x_x_w(v1, v2, Assembler::v0_t);
vse8_v(v1, dst, Assembler::v0_t);

bgez(tmp, DIFFERENCE);
// fail if char > 0x7f/0xff
bgez(tmp, fail);
add(result, result, t0);
add(dst, dst, t0);
sub(len, len, t0);
shadd(src, t0, src, t0, 1);
bnez(len, loop);
j(DONE);
j(done);

bind(DIFFERENCE);
bind(fail);
add(result, result, tmp);

bind(DONE);
bind(done);
BLOCK_COMMENT("} encode_iso_array_v");
}

Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/cpu/riscv/c2_MacroAssembler_riscv.hpp
Expand Up @@ -171,7 +171,7 @@

void encode_iso_array_v(Register src, Register dst,
Register len, Register result,
Register tmp);
Register tmp, bool ascii);

void has_negatives_v(Register ary, Register len,
Register result, Register tmp);
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/cpu/riscv/matcher_riscv.hpp
Expand Up @@ -159,7 +159,7 @@
}

// Implements a variant of EncodeISOArrayNode that encode ASCII only
static const bool supports_encode_ascii_array = false;
static const bool supports_encode_ascii_array = true;

// Returns pre-selection estimated size of a vector operation.
static int vector_op_pre_select_sz_estimate(int vopc, BasicType ety, int vlen) {
Expand Down
24 changes: 20 additions & 4 deletions src/hotspot/cpu/riscv/riscv_v.ad
Expand Up @@ -1935,17 +1935,33 @@ instruct vstring_inflate(Universe dummy, iRegP_R10 src, iRegP_R11 dst, iRegI_R12
instruct vencode_iso_array(iRegP_R12 src, iRegP_R11 dst, iRegI_R13 len, iRegI_R10 result,
vReg_V1 v1, vReg_V2 v2, vReg_V3 v3, iRegLNoSp tmp)
%{
predicate(UseRVV);
predicate(UseRVV && !((EncodeISOArrayNode*)n)->is_ascii());
match(Set result (EncodeISOArray src (Binary dst len)));
effect(TEMP_DEF result, USE_KILL src, USE_KILL dst, USE_KILL len,
TEMP v1, TEMP v2, TEMP v3, TEMP tmp);

format %{ "Encode array $src,$dst,$len -> $result" %}
format %{ "Encode ISO array $src, $dst, $len -> $result # KILL $src, $dst, $len, $tmp, V1-V3" %}
ins_encode %{
__ encode_iso_array_v($src$$Register, $dst$$Register, $len$$Register,
$result$$Register, $tmp$$Register);
$result$$Register, $tmp$$Register, false /* ascii */);
%}
ins_pipe( pipe_class_memory );
ins_pipe(pipe_class_memory);
%}

instruct vencode_ascii_array(iRegP_R12 src, iRegP_R11 dst, iRegI_R13 len, iRegI_R10 result,
vReg_V1 v1, vReg_V2 v2, vReg_V3 v3, iRegLNoSp tmp)
%{
predicate(UseRVV && ((EncodeISOArrayNode*)n)->is_ascii());
match(Set result (EncodeISOArray src (Binary dst len)));
effect(TEMP_DEF result, USE_KILL src, USE_KILL dst, USE_KILL len,
TEMP v1, TEMP v2, TEMP v3, TEMP tmp);

format %{ "Encode ASCII array $src, $dst, $len -> $result # KILL $src, $dst, $len, $tmp, V1-V3" %}
ins_encode %{
__ encode_iso_array_v($src$$Register, $dst$$Register, $len$$Register,
$result$$Register, $tmp$$Register, true /* ascii */);
%}
ins_pipe(pipe_class_memory);
%}

// fast char[] to byte[] compression
Expand Down

1 comment on commit 0fa04fe

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.