Skip to content

Commit cb74be0

Browse files
authored
Merge pull request #1888 from tsewei-lin/vector-crypto-misaligned
vector: crypto: fix constraint checks for vector-crypto instructions
2 parents 615e47d + 7347f43 commit cb74be0

15 files changed

+69
-2
lines changed

riscv/insns/vghsh_vv.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
#include "zvk_ext_macros.h"
44

5+
const uint32_t EGS = 4;
6+
57
require_zvkg;
68
require(P.VU.vsew == 32);
79
require_egw_fits(128);
10+
require(P.VU.vl->read() % EGS == 0);
11+
VI_CHECK_SSS(true)
812

913
VI_ZVK_VD_VS1_VS2_EGU32x4_NOVM_LOOP(
1014
{},

riscv/insns/vgmul_vv.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
#include "zvk_ext_macros.h"
44

5+
const uint32_t EGS = 4;
6+
57
require_zvkg;
68
require(P.VU.vsew == 32);
79
require_egw_fits(128);
10+
require(P.VU.vl->read() % EGS == 0);
11+
VI_CHECK_SSS(false)
812

913
VI_ZVK_VD_VS2_EGU32x4_NOVM_LOOP(
1014
{},

riscv/insns/vsm3c_vi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "zvksh_ext_macros.h"
44

55
require_vsm3_constraints;
6+
VI_CHECK_SSS(false)
67

78
VI_ZVK_VD_VS2_ZIMM5_EGU32x8_NOVM_LOOP(
89
{},

riscv/insns/vsm3me_vv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
(ZVKSH_P1((M16) ^ (M9) ^ ZVK_ROL32((M3), 15)) ^ ZVK_ROL32((M13), 7) ^ (M6))
1414

1515
require_vsm3_constraints;
16+
VI_CHECK_SSS(true)
1617

1718
VI_ZVK_VD_VS1_VS2_EGU32x8_NOVM_LOOP(
1819
{},

riscv/insns/vsm4k_vi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ static constexpr uint32_t zvksed_ck[32] = {
1515
};
1616

1717
require_vsm4_constraints;
18+
VI_CHECK_SSS(false)
1819

1920
VI_ZVK_VD_VS2_ZIMM5_EGU32x4_NOVM_LOOP(
2021
{},

riscv/insns/vsm4r_vs.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
#include "zvksed_ext_macros.h"
44

5+
const uint32_t EGS = 4;
6+
57
require_vsm4_constraints;
8+
require_align(insn.rd(), P.VU.vflmul);
9+
require_vs2_align_eglmul(128);
610
// No overlap of vd and vs2.
7-
require(insn.rd() != insn.rs2());
11+
require_noover_eglmul(insn.rd(), insn.rs2());
812

913
VI_ZVK_VD_VS2_NOOPERANDS_PRELOOP_EGU32x4_NOVM_LOOP(
1014
{},

riscv/insns/vsm4r_vv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
#include "zvksed_ext_macros.h"
44

5+
56
require_vsm4_constraints;
7+
VI_CHECK_SSS(false)
68

79
VI_ZVK_VD_VS2_EGU32x4_NOVM_LOOP(
810
{},

riscv/insns/vwsll_vi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "zvk_ext_macros.h"
44

55
require_zvbb;
6+
VI_CHECK_DSS(false);
67

78
VI_ZVK_VI_WIDENING_ULOOP({
89
const reg_t shift = zimm5 & ((2 * sew) - 1);

riscv/insns/vwsll_vv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "zvk_ext_macros.h"
44

55
require_zvbb;
6+
VI_CHECK_DSS(true);
67

78
VI_ZVK_VV_WIDENING_ULOOP({
89
const reg_t shift = (vs1 & ((2 * sew) - 1));

riscv/insns/vwsll_vx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "zvk_ext_macros.h"
44

55
require_zvbb;
6+
VI_CHECK_DSS(false);
67

78
VI_ZVK_VX_WIDENING_ULOOP({
89
const reg_t shift = (rs1 & ((2 * sew) - 1));

riscv/zvk_ext_macros.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,32 @@
8686
// (LMUL * VLEN) <= EGW
8787
#define require_egw_fits(EGW) require((EGW) <= (P.VU.VLEN * P.VU.vflmul))
8888

89+
// Ensures that a register index is aligned to EMUL
90+
// evaluated as EGW / VLEN.
91+
// The check is only enabled if this value is greater
92+
// than one (no index alignment check required for fractional EMUL)
93+
#define require_vreg_align_eglmul(EGW, VREG_NUM) \
94+
do { \
95+
float vfeglmul = EGW / P.VU.VLEN; \
96+
if (vfeglmul > 1) { \
97+
require_align(VREG_NUM, vfeglmul); \
98+
}\
99+
} while (0)
100+
101+
#define require_vs2_align_eglmul(EGW) require_vreg_align_eglmul(EGW, insn.rs2())
102+
103+
// ensure that rs2 and rd do not overlap, assuming rd encodes an LMUL wide
104+
// vector register group and rs2 encodes an vs2_EMUL=ceil(EGW / VLEN) vector register
105+
// group.
106+
// Assumption: LMUL >= vs2_EMUL which is enforced independently through require_egw_fits.
107+
#define require_noover_eglmul(vd, vs2) \
108+
do { \
109+
int vd_emul = P.VU.vflmul < 1.f ? 1 : (int) P.VU.vflmul; \
110+
int aligned_vd = vd / vd_emul; \
111+
int aligned_vs2 = vs2 / vd_emul; \
112+
require(aligned_vd != aligned_vs2); \
113+
} while (0)
114+
89115
// Checks that the vector unit state (vtype and vl) can be interpreted
90116
// as element groups with EEW=32, EGS=4 (four 32-bits elements per group),
91117
// for an effective element group width of EGW=128 bits.

riscv/zvkned_ext_macros.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,30 @@
22
// the RISC-V Zvkned extension (vector AES single round).
33

44
#include "insns/aes_common.h"
5+
#include "zvk_ext_macros.h"
56

67
#ifndef RISCV_ZVKNED_EXT_MACROS_H_
78
#define RISCV_ZVKNED_EXT_MACROS_H_
89

910
// vaes*.vs instruction constraints:
1011
// - Zvkned is enabled
1112
// - EGW (128) <= LMUL * VLEN
13+
// - vd is LMUL aligned
14+
// - vs2 is ceil(EGW / VLEN) aligned
1215
// - vd and vs2 cannot overlap
1316
//
1417
// The constraint that vstart and vl are both EGS (4) aligned
1518
// is checked in the VI_ZVK_..._EGU32x4_..._LOOP macros.
1619
#define require_vaes_vs_constraints \
1720
do { \
21+
const uint32_t EGS = 4; \
1822
require_zvkned; \
23+
require(P.VU.vl->read() % EGS == 0); \
1924
require(P.VU.vsew == 32); \
2025
require_egw_fits(128); \
21-
require(insn.rd() != insn.rs2()); \
26+
require_align(insn.rd(), P.VU.vflmul); \
27+
require_vs2_align_eglmul(128); \
28+
require_noover_eglmul(insn.rd(), insn.rs2()); \
2229
} while (false)
2330

2431
// vaes*.vv instruction constraints. Those are the same as the .vs ones,
@@ -30,17 +37,24 @@
3037
// is checked in the VI_ZVK_..._EGU32x4_..._LOOP macros.
3138
#define require_vaes_vv_constraints \
3239
do { \
40+
const uint32_t EGS = 4; \
3341
require_zvkned; \
42+
require(P.VU.vl->read() % EGS == 0); \
3443
require(P.VU.vsew == 32); \
3544
require_egw_fits(128); \
45+
VI_CHECK_SSS(false) \
3646
} while (false)
3747

3848
// vaeskf*.vi instruction constraints. Those are the same as the .vv ones.
3949
#define require_vaeskf_vi_constraints \
4050
do { \
51+
const uint32_t EGS = 4; \
4152
require_zvkned; \
53+
require(P.VU.vstart->read() % EGS == 0); \
54+
require(P.VU.vl->read() % EGS == 0); \
4255
require(P.VU.vsew == 32); \
4356
require_egw_fits(128); \
57+
VI_CHECK_SSS(false) \
4458
} while (false)
4559

4660
#define VAES_XTIME(A) (((A) << 1) ^ (((A) & 0x80) ? 0x1b : 0))

riscv/zvknh_ext_macros.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// macros.
1616
#define require_vsha2_common_constraints \
1717
do { \
18+
VI_CHECK_SSS(true) \
1819
require(P.VU.vsew == 32 || P.VU.vsew == 64); \
1920
require(insn.rd() != insn.rs1()); \
2021
require(insn.rd() != insn.rs2()); \

riscv/zvksed_ext_macros.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
// is checked in the VI_ZVK_..._EGU32x4_..._LOOP macros.
1717
#define require_vsm4_constraints \
1818
do { \
19+
const uint32_t EGS = 4; \
1920
require_zvksed; \
2021
require(P.VU.vsew == 32); \
2122
require_egw_fits(128); \
23+
require(P.VU.vstart->read() % EGS == 0); \
24+
require(P.VU.vl->read() % EGS == 0); \
2225
} while (false)
2326

2427
// Returns a uint32_t value constructed from the 4 bytes (uint8_t)

riscv/zvksh_ext_macros.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
// is checked in the VI_ZVK_..._EGU32x8_..._LOOP macros.
1717
#define require_vsm3_constraints \
1818
do { \
19+
const uint32_t EGS = 8; \
1920
require_zvksh; \
2021
require(P.VU.vsew == 32); \
2122
require_egw_fits(256); \
23+
require(P.VU.vstart->read() % EGS == 0); \
24+
require(P.VU.vl->read() % EGS == 0); \
2225
require(insn.rd() != insn.rs2()); \
2326
} while (false)
2427

0 commit comments

Comments
 (0)