Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

Voidarsh's blog

By Voidarsh, history, 5 months ago, In English

Yesterday during contest While solving Problem B of Educational Codeforces Round 191, I encountered something that completely confused me. A cout statement that had nothing to do with the construction logic seemed to change the output of my program. Removing it produced one answer, and adding it produced another. Naturally, I wondered how can a print statement affect the output of a loop.

this was all done in codechef online compiler, like vs code was behaving fine in this but neither codechef complier nor cf compiler

It was quite strange to me you can go to my profile and see my code of the yesterday contest first i submitted this code My logic was simple: I found a valid construction for 2 numbers, and a valid construction for 3 numbers was already given in the statement. Since any n can be represented using combinations of 2 and 3, I built my solution around that idea.

377947088

This code looked completely correct to me, but I got a Wrong Answer on test case 1 For the second sample, where n=4, it seemed as if the count of 1 suddenly became 5 instead of 4, which was very surprising.

I started debugging, and after commenting out a few lines, I noticed that the code was actually producing the correct output. then i again made a submission

378010948

so again the response was same LIKE LITERALLY IF YOU RUN THIS CODE WITH t=1 and n=2 AND WHEN YOU WOULD COMMENT THE STATEMENT // cout<<ans[4]<<endl; output would be different and then if you uncomment this then the correct output would get displayed..

after all this i decided to just change the way of printing the things in my code 377959599

and then it got accepted

doing all this wasted my 20 minutes in my contest :(

  • Vote: I like it
  • +45
  • Vote: I do not like it

»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I think it's because of a bug in compiler optimizations. It gets accepted in C++17, by adding Ofast and avx2 pragmas or by changing the x and y sequence into a vector like in 378136656

»
5 months ago, hide # |
← Rev. 3  
Vote: I like it 0 Vote: I do not like it

Disregard my comment; I added some asserts and it seemed fine.

»
5 months ago, hide # |
 
Vote: I like it +20 Vote: I do not like it

Thanks for the morning rabbit hole. Seems like you found a bug in gcc.

This is the simplest reproducer i could manage. Compiler Explorer. Take a look at .L3 in particular, it just reuses the same 4 int block.

It produces correct output if you pass -O0 or -O1, or if you pass -fno-tree-vectorize. So I assume the bug is in the vectorizer. This is probably worthy of a report as is (if it doesn't exist already, I haven't checked.)

To the question of why your second program worked correctly is that, you disabled SIMD optimizations when you used iostream inside the while loop and that bypassed the faulty codegen. I'll check the IR when I have more time, fun.

  • »
    »
    5 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    And I mean, it also happens with vector and whatnot. I could see myself writing something like this in a nasty implementation problem... Well, I rewrote it to look more like this:

    your modified code

    And yeah, it gives the wrong output on codeforces too... At least it works with clang (also with -02).

    • »
      »
      »
      5 months ago, hide # ^ |
       
      Vote: I like it +6 Vote: I do not like it

      It's not about the std::vector at all.

      What vectorization simply means that, compiler sees this as a block of numbers: [x, y, x, x]. It also see that it adds 2 to the each element every loop. Instead of doing that naively, it creates a block of [2, 2, 2, 2]. Then, loads these blocks(vectors is the term) into the specialized SIMD registers in the CPU to do vector addition, so it takes a single op instead of 4.(You can see that as xmm register in the assembly)

      Problem is that SIMD register xmm is 128 bit wide, so it hold 4 ints. It packs first 4 ints into a vector, but for some reason it decides the second vector is equal to the first. I checked the IR, [y y x y] shape is really absent.

      Now the funny tidbits.

      • If you use 64-bit long long, it still produces wrong code.
      • If you use -mavx2 like goodnight21 mentioned, it produces correct code, because avx2 register is wider 256 bit ymm register, so it just places all 8 numbers.
      • If you use avx2 and long long, welp, it's wrong because once again it can only fit 4 and wrongly optimizes the rest.

      Man, there goes my weekend...

      • »
        »
        »
        »
        5 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        yeah yeah, I hadn't really read the wrapping context and just looked at the base submission. But yeah, this is really cool

»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Voidarsh (previous revision, new revision, compare).