2

I have the following code using Intel-rtm,

rtmCheck.c:

#include <stdio.h>
#include <stdlib.h>
#include <immintrin.h>

int test_otherfunction(int a, int b){
int c=0;
   
for (int i=0; i<1000;i++){
      c=c+a+b;
   }


for (int i=0; i<1000000000;i++){
    c=c+a+b;
}

for (int i=0; i<1000000000;i++){
    c=c+a+b;
}
for (int i=0; i<1000000000;i++){
    c=c+a+b;
}

return 1;
}

int main(int argc, char *argv[])
{
   int result=-1;
   unsigned status;
   while(result!=1){
      if ((status = _xbegin()) == _XBEGIN_STARTED) {
         if(test_otherfunction(1,2))
             result=1;
             _xend();
      }else{
             printf("rtmCheck: Transaction failed\n");
             printf("status is %ld\n", status);
             printf("Trying again ...\n");

      }
    
   }
printf("rtmCheck : Result is %d\n", result);

return 0;
}

If I compile with the following Makefile1, It always failed. I guess that is expected. And I'm okay with it.

Makefile1

CC=gcc
CFLAGS= -mrtm

rtmCheck: rtmCheck.o
        $(CC) $(CFLAGS) -o rtmCheck rtmCheck.o

clean: 
        rm -f *.o rtmCheck

Output:

rtmCheck: Transaction failed
status is 0
Trying again ...
rtmCheck: Transaction failed
status is 0
Trying again ...
rtmCheck: Transaction failed
status is 0
Trying again ...
...........
...........

But I have another Makefile2. If I compile my code with this(Makefile2) this works every time. just not understanding why?

Makefile2:

CC  := gcc
CFLAGS  :=  -mrtm -Wall -g -MD -O2 -I ../
LDFLAGS := -ltest 

tests_files := rtmCheck

all: $(tests_files)

rtmCheck: rtmCheck.o ../libtest/libtest.a
        $(CC) $(CFLAGS) -static $(<) -L../libtest/ $(LDFLAGS) -o $(@)

clean:
        rm -f *.o test *.d $(tests_files)

-include *.d

OutPut:

$./rtmCheck 
rtmCheck : Result is 1

Above Make file use some static library. But I'm not using anything from the library. Can someone please tell me why this is happening?

user45698746
  • 305
  • 2
  • 13
  • I guess the compiler is doing some optimization (due to the `-O2`) and solves `c` (or returns 1 directly) without executing the loops in the second case, try defining `c` as `volatile`. – David Ranieri Nov 25 '20 at 06:09
  • @DavidRanieri. Ok. But as I'm using the same compiler. The optimization should be applied to both cases, right? – user45698746 Nov 25 '20 at 06:15
  • 1
    Not if you leave out `-O2`!! The default is `-O0`, [which treats every variable kind of like `volatile`](https://stackoverflow.com/questions/53366394/why-does-clang-produce-inefficient-asm-with-o0-for-this-simple-floating-point). Look at the generated asm. – Peter Cordes Nov 25 '20 at 06:16
  • 2
    @user45698746 I don't see any optimization flag in the `Makefile1`, try `CFLAGS= -mrtm -O2` and maybe you get the same result – David Ranieri Nov 25 '20 at 06:17
  • Also, your code is indented very badly. e.g. GCC 10.2 warns that `if(test_otherfunction(1,2))` doesn't guard / control `_xend();` despite indentation making it look like it should. And what's in `libtest`? Why the Makefile instead of just a `gcc` command line? If libtest is actually relevant, then this isn't a [mcve]. – Peter Cordes Nov 25 '20 at 06:18
  • @DavidRanieri, Yap. It was `-O2`. Thanks. – user45698746 Nov 25 '20 at 06:21
  • @PeterCordes. Thanks. – user45698746 Nov 25 '20 at 06:22

0 Answers0