Skip to content

Commit ea5d495

Browse files
committed
[PPC64LE] poly_tomont: trim dead callee-saved spills (frame 320->224)
The prologue saved and restored v20-v30, but the body only clobbers the callee-saved VRs v23, v24, and v27-v30. v20, v21, v22, v25, and v26 were spilled and reloaded but never used: five dead stxvx/lxvx pairs and 80 bytes of frame, the over-save flagged in the inline TODO. Save only the registers the body uses and trim the frame from 320 to 224. The Montgomery multiply is unchanged. func, kat, and unit pass for ML-KEM-512/768/1024 (opt and no_opt). The mlkem/ copy was regenerated with scripts/simpasm. Signed-off-by: Scott Boudreaux <scottbphone12@gmail.com>
1 parent 4482a37 commit ea5d495

2 files changed

Lines changed: 35 additions & 78 deletions

File tree

dev/ppc64le/src/poly_tomont_ppc_asm.S

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
55
*
66
* Written by Danny Tsen <dtsen@us.ibm.com>
7+
* poly_tomont frame trim (#1722) by Scott Boudreaux.
78
*/
89

910
/*
@@ -52,7 +53,7 @@
5253
.text
5354

5455
/*
55-
* Barrett multiplication by R = 2^16 mod q,
56+
* Barrett multiplication by R = 2^16 mod q,
5657
* mapping each coefficient to the Montgomery domain.
5758
* For each lane (a in vdata1..4):
5859
* t = vmhraddshs(a, V_FACTOR_TW, 0) = round(a*factor_tw / 2^15)
@@ -103,40 +104,26 @@
103104
.global MLK_ASM_NAMESPACE(poly_tomont_ppc_asm)
104105
.balign 16
105106
MLK_ASM_FN_SYMBOL(poly_tomont_ppc_asm)
106-
stdu 1, -320(1)
107+
stdu 1, -224(1)
107108
mflr 0
108109

109110
/*
110-
* TODO: this saves/restores v20-v30, but the body only touches
111-
* v23 (vdata3), v24 (vtmp3) and v27-v30 (vresult1..4) in the
112-
* callee-saved range. v20, v21, v22, v25, v26 are spilled and
113-
* reloaded but never used -- 5 dead stxvx/lxvx pairs plus 80
114-
* bytes of frame. Trim the save set (and frame size) to the
115-
* registers actually clobbered. Separate change from the doc
116-
* pass, since it alters emitted code.
111+
* Save/restore only the callee-saved VRs the body clobbers:
112+
* v23, v24, v27-v30. v20-v22, v25, v26 were spilled but never
113+
* used. Frame trimmed from 320 to 224 bytes.
117114
*/
118115
li 6, 128
119116
li 7, 144
120117
li 8, 160
121118
li 9, 176
122119
li 10, 192
123120
li 11, 208
124-
li 12, 224
125-
stxvx 32+20, 6, 1
126-
stxvx 32+21, 7, 1
127-
stxvx 32+22, 8, 1
128-
stxvx 32+23, 9, 1
129-
stxvx 32+24, 10, 1
130-
stxvx 32+25, 11, 1
131-
stxvx 32+26, 12, 1
132-
li 6, 240
133-
li 7, 256
134-
li 8, 272
135-
li 9, 288
136-
stxvx 32+27, 6, 1
137-
stxvx 32+28, 7, 1
138-
stxvx 32+29, 8, 1
139-
stxvx 32+30, 9, 1
121+
stxvx 32+23, 6, 1
122+
stxvx 32+24, 7, 1
123+
stxvx 32+27, 8, 1
124+
stxvx 32+28, 9, 1
125+
stxvx 32+29, 10, 1
126+
stxvx 32+30, 11, 1
140127

141128
li 6, MLK_PPC_NQ_OFFSET
142129
li 7, MLK_PPC_TOMONT_OFFSET
@@ -178,28 +165,17 @@ MLK_ASM_FN_SYMBOL(poly_tomont_ppc_asm)
178165
li 9, 176
179166
li 10, 192
180167
li 11, 208
181-
li 12, 224
182-
lxvx 32+20, 6, 1
183-
lxvx 32+21, 7, 1
184-
lxvx 32+22, 8, 1
185-
lxvx 32+23, 9, 1
186-
lxvx 32+24, 10, 1
187-
lxvx 32+25, 11, 1
188-
lxvx 32+26, 12, 1
189-
li 6, 240
190-
li 7, 256
191-
li 8, 272
192-
li 9, 288
193-
lxvx 32+27, 6, 1
194-
lxvx 32+28, 7, 1
195-
lxvx 32+29, 8, 1
196-
lxvx 32+30, 9, 1
168+
lxvx 32+23, 6, 1
169+
lxvx 32+24, 7, 1
170+
lxvx 32+27, 8, 1
171+
lxvx 32+28, 9, 1
172+
lxvx 32+29, 10, 1
173+
lxvx 32+30, 11, 1
197174
mtlr 0
198-
addi 1, 1, 320
175+
addi 1, 1, 224
199176
blr
200177

201-
/* To facilitate single-compilation-unit (SCU) builds, undefine all macros.
202-
* Don't modify by hand -- this is auto-generated by scripts/autogen. */
178+
/* To facilitate single-compilation-unit (SCU) builds, undefine all macros. */
203179
#undef rinp
204180
#undef V_FACTOR
205181
#undef V_FACTOR_TW

mlkem/src/native/ppc64le/src/poly_tomont_ppc_asm.S

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
55
*
66
* Written by Danny Tsen <dtsen@us.ibm.com>
7+
* poly_tomont frame trim (#1722) by Scott Boudreaux.
78
*/
89

910
/*
@@ -27,30 +28,20 @@
2728
.global MLK_ASM_NAMESPACE(poly_tomont_ppc_asm)
2829
MLK_ASM_FN_SYMBOL(poly_tomont_ppc_asm)
2930

30-
stdu 1, -320(1)
31+
stdu 1, -224(1)
3132
mflr 0
3233
li 6, 128
3334
li 7, 144
3435
li 8, 160
3536
li 9, 176
3637
li 10, 192
3738
li 11, 208
38-
li 12, 224
39-
stxvd2x 52, 6, 1
40-
stxvd2x 53, 7, 1
41-
stxvd2x 54, 8, 1
42-
stxvd2x 55, 9, 1
43-
stxvd2x 56, 10, 1
44-
stxvd2x 57, 11, 1
45-
stxvd2x 58, 12, 1
46-
li 6, 240
47-
li 7, 256
48-
li 8, 272
49-
li 9, 288
50-
stxvd2x 59, 6, 1
51-
stxvd2x 60, 7, 1
52-
stxvd2x 61, 8, 1
53-
stxvd2x 62, 9, 1
39+
stxvd2x 55, 6, 1
40+
stxvd2x 56, 7, 1
41+
stxvd2x 59, 8, 1
42+
stxvd2x 60, 9, 1
43+
stxvd2x 61, 10, 1
44+
stxvd2x 62, 11, 1
5445
li 6, 0
5546
li 7, 80
5647
li 8, 96
@@ -264,24 +255,14 @@ MLK_ASM_FN_SYMBOL(poly_tomont_ppc_asm)
264255
li 9, 176
265256
li 10, 192
266257
li 11, 208
267-
li 12, 224
268-
lxvd2x 52, 6, 1
269-
lxvd2x 53, 7, 1
270-
lxvd2x 54, 8, 1
271-
lxvd2x 55, 9, 1
272-
lxvd2x 56, 10, 1
273-
lxvd2x 57, 11, 1
274-
lxvd2x 58, 12, 1
275-
li 6, 240
276-
li 7, 256
277-
li 8, 272
278-
li 9, 288
279-
lxvd2x 59, 6, 1
280-
lxvd2x 60, 7, 1
281-
lxvd2x 61, 8, 1
282-
lxvd2x 62, 9, 1
258+
lxvd2x 55, 6, 1
259+
lxvd2x 56, 7, 1
260+
lxvd2x 59, 8, 1
261+
lxvd2x 60, 9, 1
262+
lxvd2x 61, 10, 1
263+
lxvd2x 62, 11, 1
283264
mtlr 0
284-
addi 1, 1, 320
265+
addi 1, 1, 224
285266
blr
286267

287268
MLK_ASM_FN_SIZE(poly_tomont_ppc_asm)

0 commit comments

Comments
 (0)