Weird issue with AVX caused simply by replacing ymm9 with ymm1. lazarus / 21-General ---------------------------------------------------------------------- Solecist · 2021-07-06 13:25 Greetings. I'm having a weird Issue I can't explain and I'm unable to find anything related, or maybe I'm lacking the proper search terms. The problem is that this piece of code works perfectly fine unless I switch ymm9 to ymm1. I haven't checked any other numbers except ymm15. When I change ymm9 to ymm15 *it works fine*. I don't know if this is a problem with the compiler, or not. The generated assembly output looks exactly like my code. I've been programming in assembly for over three decades, but only starting working with AVX last week. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure SquaredSubAVXX(MaxRuns : DWord; V : Single = 0.0); inline; Assembler; nostackframe;Asm// ECX contains the amount of runs.         mov RAX,__Output                        // Where results are written to.      mov RDI,__Input                 // Where the data is coming from. An Array of of 1280.0         vmovaps ymm8, [RDI]        vmovaps ymm9, [RDI+32]        vmovaps ymm10, [RDI+64]        vmovaps ymm11, [RDI+96]         vbroadcastss ymm7, V        vbroadcastss ymm3, AVXStepper          // Single, 8.0         @UntilMaxRunsIsZero:                              vsubps ymm5,ymm8,ymm7                vsubps ymm6,ymm9,ymm7                vsubps ymm4,ymm10,ymm7                vsubps ymm12,ymm11,ymm7                 vmulps ymm5, ymm5,ymm5              vmulps ymm6, ymm6,ymm6                vmulps ymm4, ymm4,ymm4              vmulps ymm12, ymm12,ymm12                 vmovaps [RAX], ymm5                 vmovaps [RAX+32], ymm6                 vmovaps [RAX+64], ymm4                vmovaps [RAX+96], ymm12                 add rax,128                 vaddps ymm7,ymm7,ymm3        sub rcx, 1      jnz @UntilMaxRunsIsZeroEnd;  When I run this, the output (after some other code, displaying several objects) is correctly centered in the middle of my screen (at 1280), but when I replace ymm9 with ymm1, *everything* is at 0 instead. When I replace ymm9 with ymm15 *it works fine though!* It makes no sense to me. The code is completely self contained. Reading via ymm1 and writing back to memory gives the correct results. Using an AMD 5900HS. What am I missing? ---------------------------------------------------------------------- Solecist · 2021-07-06 14:00 So, I've now spaghettied the procedure into the main loop and the issue is gone. I didn't think this would help, because I've tried saving ymm1 and restoring it at the end, which made no difference. It runs four times slow now, though... So, I guess, it's related to the compiler and maybe not even an issue per se. What am I missing? ---------------------------------------------------------------------- Solecist · 2021-07-06 14:11 So, apparently there's stuff going on when using an Assembler procedure, which caused the issue with ymm1. Putting the code directly into the main loop made it four times slower, but fixed the issue. No idea where the slowdown came from (fixed alignment of the loop, didn't help), but simply putting the asm block into a regular procedure fixed all problems. Sorry for any inconvenience, though maybe this shouldn't be happening in the first place. ---------------------------------------------------------------------- marcov · 2021-07-06 14:17 You need to save registers above ymm5, you can afaik do that by listing them in the end[] block: so end['ymm6','ymm7'] etc Unfortunately, there is no way of doing this that works with Delphi and Lazarus, so ifdeffing is still required for this. Also, after you are done with the avx2 code, issue a vzeroupper instruction to clear the upper parts of ymm registers. This is a signal for the fpu to go back from avx256 to avx1/2 mode, and to not maintain dependencies for the higher 128-bits of registers. I use the following summary: // Integer arguments are passed in registers RCX, RDX, R8, and R9. // Floating point arguments are passed in XMM0L, XMM1L, XMM2L, and XMM3L. // volatile: RAX, RCX, RDX, R8, R9, R10, R11 // nonvolatile RBX, RBP, RDI, RSI, RSP, R12, R13, R14, and R15 are considered nonvolatile // volatile xmm0-xmm3 (only if parameters?) and xmm4,5 // https://msdn.microsoft.com/en-us/library/ms235286.aspx ---------------------------------------------------------------------- Solecist · 2021-07-06 17:22 Heya, thanks for your reply! First: I don't use Lazarus. No idea  how much of a difference this makes. You say I need to save ymm beyond 5, but my issue is actually with ymm1. Apparently specifically ymm1. I'm not saving any of the others, not zeroing anything, and everything works fine, as long as I ignore the, from my perspective nonsensical, ymm1 issue. That issue also only arises in an "Assembler;" procedure. If it's a normal procedure then there issue doesn't exist. So you might think it's a problem related to the calling convention, but when xmm1 is volatile, then so is ymm1, which means there shouldn't be any issues using it. Any of the upper ones are working fine. No issues with ymm9 or ymm15, but switching ymm9 to ymm1 and the results make no sense. I've tried adding the registers at the end (['ymm1'], etc) but it made zero difference whatsoever. Actually never found that this changed anything at all, ever, but I don't write every possible code out there. So ... I don't think my problem is actually related to this. In any case, of course I'm not asking for any changes, but I'd rather have the compiler get out of my way instead of being forced to hand-hold it so it doesn't have hick-ups. I also understand that I'm quite "niche", please don't take my words as attacks on the compiler or the people who create it. I've been using turbo-/borland-/free-pascal as container for my assembler code for ... ever, actually ... and never had any such issues. I've only touched AVX for the first time last week (late. very late. lol) so I'm touching new ground. ---------------------------------------------------------------------- marcov · 2021-07-06 20:42 Only y/xmm6+ should be saved that way.   vzeroupper is only for performance. Otherwise, compile with -Rintel -a and see if you can make sense of the generated assembler file. My guess is that "V" is passed in xmm1, so if you write ymm1 before "V" is moved to another register, that would explain it. Change the order of procession so you move the    vbroadcastss ymm7, V before the movaps block ---------------------------------------------------------------------- PascalDragon · 2021-07-07 07:34 Quote from: marcov on July 06, 2021, 04:17:28 pmYou need to save registers above ymm5, you can afaik do that by listing them in the end[] block: so end['ymm6','ymm7'] etc That only works inside normal code. The registers after the end of an assembly function are parsed, but not handled upon (especially not if nostackframe is passed, because there would be nothing the compiler can save them to). @Solecist: unrelated to your problem, but declaring an assembly function as inline is useless, because the compiler never inlines assembly functions. ---------------------------------------------------------------------- Solecist · 2021-07-07 09:58 Quote from: marcov on July 06, 2021, 10:42:12 pmOnly y/xmm6+ should be saved that way.   vzeroupper is only for performance. Otherwise, compile with -Rintel -a and see if you can make sense of the generated assembler file. My guess is that "V" is passed in xmm1, so if you write ymm1 before "V" is moved to another register, that would explain it. Change the order of procession so you move the    vbroadcastss ymm7, V before the movaps block Well, I'm not saving any registers and it works fine now, so far, as long as I don't use an Assembler-procedure. I usually go "-Amasm -al", because that produces intel assembly syntax. I don't understand your post, or rather, what you're trying to tell me. Why would V get passed in ymm1, when it's clearly in ymm7? The compiler doesn't change my code. That's not its job. I'm sorry, your post makes no sense to me. ---------------------------------------------------------------------- Solecist · 2021-07-07 09:59 Quote from: PascalDragon on July 07, 2021, 09:34:45 amQuote from: marcov on July 06, 2021, 04:17:28 pmYou need to save registers above ymm5, you can afaik do that by listing them in the end[] block: so end['ymm6','ymm7'] etc That only works inside normal code. The registers after the end of an assembly function are parsed, but not handled upon (especially not if nostackframe is passed, because there would be nothing the compiler can save them to). @Solecist: unrelated to your problem, but declaring an assembly function as inline is useless, because the compiler never inlines assembly functions. Well that explains why it never inlines those. lol Thanks! ---------------------------------------------------------------------- avk · 2021-07-07 11:35 Quote from: Solecist on July 07, 2021, 11:58:53 amQuote from: marcov on July 06, 2021, 10:42:12 pmOnly y/xmm6+ should be saved that way.   vzeroupper is only for performance. Otherwise, compile with -Rintel -a and see if you can make sense of the generated assembler file. My guess is that "V" is passed in xmm1, so if you write ymm1 before "V" is moved to another register, that would explain it. Change the order of procession so you move the    vbroadcastss ymm7, V before the movaps block Well, I'm not saving any registers and it works fine now, so far, as long as I don't use an Assembler-procedure. I usually go "-Amasm -al", because that produces intel assembly syntax. I don't understand your post, or rather, what you're trying to tell me. Why would V get passed in ymm1, when it's clearly in ymm7? The compiler doesn't change my code. That's not its job. I'm sorry, your post makes no sense to me. I can assume that the win64 ABI is being used since the first parameter(MaxRuns) is in ECX. Then the second floating point parameter(V) will be passed exactly in the YMM1 register (more precisely, in the XMM1 register, which is the lower part of the YMM1 register). ---------------------------------------------------------------------- Thaddy · 2021-07-07 11:40 Quote from: avk on July 07, 2021, 01:35:20 pmI can assume that the win64 ABI is being used since the first parameter(MaxRuns) is in ECX. ECX? not RCX? ---------------------------------------------------------------------- avk · 2021-07-07 11:57 ECX is the lower part of the RCX and MaxRuns is DWord. ---------------------------------------------------------------------- marcov · 2021-07-07 12:07 Quote from: Solecist on July 07, 2021, 11:58:53 amI don't understand your post, or rather, what you're trying to tell me. Why would V get passed in ymm1, when it's clearly in ymm7? It is not passed in ymm7, you copy it there with Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   vbroadcastss ymm7, V But the "V" is how the win64 ABI passed it, and that says (from the win64 ABI summary comment that I posted): Floating point arguments are passed in XMM0L, XMM1L, XMM2L, and XMM3L. So I'd expect XMM0 actually, but in case of doubt it is easiest to simply check the generated assembler code with -a, as said. ---------------------------------------------------------------------- PascalDragon · 2021-07-08 07:07 Quote from: marcov on July 07, 2021, 02:07:27 pmBut the "V" is how the win64 ABI passed it, and that says (from the win64 ABI summary comment that I posted): Floating point arguments are passed in XMM0L, XMM1L, XMM2L, and XMM3L. So I'd expect XMM0 actually, but in case of doubt it is easiest to simply check the generated assembler code with -a, as said. No, the SIMD registers mirror their non-SIMD registers. So parameter 0 is in either RCX or XMM0L. Parameter 1 is in RDX or XMM1L and so on.