[llvm-bugs] [Bug 52100] New: clang incorrectly optimize integer conversion from int to unsigned long long with -O2 on x86_64

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Oct 6 15:13:30 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=52100

            Bug ID: 52100
           Summary: clang incorrectly optimize integer conversion from int
                    to unsigned long long with -O2 on x86_64
           Product: clang
           Version: unspecified
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: xjin at anl.gov
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org,
                    neeilans at live.com, richard-llvm at metafoo.co.uk

The original code is from
https://github.com/milc-qcd/milc_qcd/blob/master/generic/ranstuff.c
Please see the license at
https://github.com/milc-qcd/milc_qcd/blob/master/LICENSE

When compiled with -O2, the assignment
        prn_pt->ic_state = seed;
gives pro_pt->ic_state the wrong value.

It behaves correctly on PowerPC, but gives incorrect results on x86_64, as
below.

$ cat tmilcrng.h
#ifndef _TMILCRNG_H
#define _TMILCRNG_H                      
typedef struct {                                                       
        unsigned long r0,r1,r2,r3,r4,r5,r6;
        unsigned long long multiplier,addend,ic_state;
        float scale;
} double_prn;  
typedef struct {                         
        int index;                   
        double_prn site_prn;             
} Lattice;

extern Lattice *lattice;        
extern int nx,ny,nz,nt;                                                         
extern int node_index(int,int,int,int);                                         
#endif                                                                          
$ cat tmilcrng_o.c                                                              
#include"tmilcrng.h"                                                            
#include<stdio.h>                                                               
void                                                                            
initialize_prn(double_prn *prn_pt, int seed, int index)                         
{                                                                               
        seed = (69607+8*index)*seed+12345;                                      
        prn_pt->r0 = (seed>>8) & 0xffffff;                                      
        seed = (69607+8*index)*seed+12345;                                      
        prn_pt->r1 = (seed>>8) & 0xffffff;                                      
        seed = (69607+8*index)*seed+12345;                                      
        prn_pt->r2 = (seed>>8) & 0xffffff;                                      
        seed = (69607+8*index)*seed+12345;                                      
        prn_pt->r3 = (seed>>8) & 0xffffff;
        seed = (69607+8*index)*seed+12345;                                      
        prn_pt->r4 = (seed>>8) & 0xffffff;                                      
        seed = (69607+8*index)*seed+12345;                                      
        prn_pt->r5 = (seed>>8) & 0xffffff;                                      
        seed = (69607+8*index)*seed+12345;                                      
        prn_pt->r6 = (seed>>8) & 0xffffff;                                      
        seed = (69607+8*index)*seed+12345;                                      
        prn_pt->ic_state = seed;                                                
        prn_pt->multiplier = 100000005 + 8*index;                               
        prn_pt->addend = 12345;                                                 
        prn_pt->scale = 1.0/((double)0x1000000);                                
        printf("initialize_prn: 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx,
0x%llx 0x%llx 0x%llx, %.7g\n",                    
                prn_pt->r0, prn_pt->r1, prn_pt->r2, prn_pt->r3, prn_pt->r4,
prn_pt->r5, prn_pt->r6,                         
                prn_pt->multiplier, prn_pt->addend, prn_pt->ic_state,
prn_pt->scale);                                                                 
}                                                                               
void                                                                            
initialize_site_prn_from_seed(int iseed)
{                        
        int x, y, z, t, i;

        printf("WARNING!!: Resetting random seed\n");

        for(t=0;t<nt;t++)for(z=0;z<nz;z++)for(y=0;y<ny;y++)for(x=0;x<nx;x++){
                i=node_index(x,y,z,t);
                initialize_prn( &(lattice[i].site_prn) , iseed,
lattice[i].index);
        }                
}                         
$ cat tmilcrng.c          
#include"tmilcrng.h"      
#include<stdio.h>         
#include<stdlib.h>        
extern void initialize_site_prn_from_seed(int);
extern void initialize_site_prn_from_seed_opt(int);
int
main()
{
        int i, failed=0;
        int seed = 5682304;
        nx=ny=nz=nt=2;
        Lattice lat[16], lat_opt[16];
        for(i=0;i<16;++i) lat[i].index=lat_opt[i].index=i;
        lattice = lat;
        initialize_site_prn_from_seed(seed);
        lattice = lat_opt;
        initialize_site_prn_from_seed_opt(seed);
        for(i=0;i<16;++i){
                if(lat[i].site_prn.ic_state!=lat_opt[i].site_prn.ic_state){
                        printf("error in initialize_prn %d\n",i);
                        failed++;
                }       
        }       
        return failed;
}       

Lattice *lattice;
int nx,ny,nz,nt;

int
node_index(int x, int y, int z, int t)
{
        return x+nx*(y+ny*(z+nz*t));
}       
#define initialize_prn initialize_prn_opt
#define initialize_site_prn_from_seed initialize_site_prn_from_seed_opt
#include"tmilcrng_o.c"
$ cat r
#!/bin/sh
CC=${CC:-clang}
"$CC" -O0 -o tmilcrng_o.o -c tmilcrng_o.c
"$CC" -O2 -o tmilcrng.o -c tmilcrng.c
"$CC" -o tmilcrng tmilcrng_o.o tmilcrng.o
./tmilcrng
$ ./r
WARNING!!: Resetting random seed
initialize_prn: 0x17508f 0x51d6b7 0x24a2cd 0x664033 0x3a402d 0x7ffb50 0x867cc8,
0x5f5e105 0x3039 0x7251b820, 5.960464e-08
initialize_prn: 0x1a0633 0xdc8b50 0x60bed0 0x50a1eb 0xaf558e 0x431843 0x5370af,
0x5f5e10d 0x3039 0x2fe41a40, 5.960464e-08
initialize_prn: 0x1cbbd7 0x929a2a 0xa5f82f 0xdd0392 0x6770a 0x40be63 0x3312f,
0x5f5e115 0x3039 0x258b9660, 5.960464e-08
initialize_prn: 0x1f717b 0x740344 0x4c4eb 0x35afd4 0x9c185b 0x455c64 0xe56bcb,
0x5f5e11d 0x3039 0xffffffffa308f480, 5.960464e-08
initialize_prn: 0x22271f 0x80c69e 0x8d9b04 0x93b15d 0x7d42bf 0x57eefa 0x518688,
0x5f5e125 0x3039 0x2bd8fca0, 5.960464e-08
initialize_prn: 0x24dcc3 0xb8e438 0x50f079 0x3ed2d9 0xb594f0 0xe35cd9 0xf933ea,
0x5f5e12d 0x3039 0xffffffffc2b476c0, 5.960464e-08
initialize_prn: 0x279267 0x1c5c11 0x5f3b4a 0x8d9ef4 0x9d432c 0x7fd2b5 0x5c46f3,
0x5f5e135 0x3039 0xffffffffad902ae0, 5.960464e-08
initialize_prn: 0x2a480b 0xab2e2b 0xc8f178 0xe5605a 0x27172d 0x5c1f41 0x5dc72a,
0x5f5e13d 0x3039 0xfffffffff81ce100, 5.960464e-08
initialize_prn: 0x2cfdaf 0x655a85 0x9e8903 0xba21b7 0x2e7030 0x470f33 0xfa4491,
0x5f5e145 0x3039 0xfffffffffac76120, 5.960464e-08
initialize_prn: 0x2fb353 0x4ae11f 0xf077ea 0x8eadb7 0xc542f2 0x58c93e 0x1f6bac,
0x5f5e14d 0x3039 0x2c387340, 5.960464e-08
initialize_prn: 0x3268f7 0x5bc1f8 0xcf342d 0xf48f06 0x8219ad 0x3c2a16 0xa4d980,
0x5f5e155 0x3039 0xffffffff8354df60, 5.960464e-08
initialize_prn: 0x351e9b 0x97fd12 0x4b33cd 0x8c1051 0xce141f 0x18206f 0x662f90,
0x5f5e15d 0x3039 0xffffffffafbd6d80, 5.960464e-08
initialize_prn: 0x37d43f 0xff926c 0x74ecc9 0x43c42 0x32e782 0x1908fd 0x7e67e1,
0x5f5e165 0x3039 0x6ecee5a0, 5.960464e-08
initialize_prn: 0x3a89e3 0x928206 0x5cd522 0x1add86 0xa8de94 0x9a0a73 0xa468f7,
0x5f5e16d 0x3039 0x43220fc0, 5.960464e-08
initialize_prn: 0x3d3f87 0x50cbe0 0x1362d8 0x9c7ec9 0xe4d98f 0xee7187 0xa8d9d5,
0x5f5e175 0x3039 0xffffffffd48bb3e0, 5.960464e-08
initialize_prn: 0x3ff52b 0x3a6ff9 0xa90bea 0x646ab7 0xa64e30 0xcb0cec 0x1535ff,
0x5f5e17d 0x3039 0x3e9c9a00, 5.960464e-08
WARNING!!: Resetting random seed
initialize_prn: 0x17508f 0x51d6b7 0x24a2cd 0x664033 0x3a402d 0x7ffb50 0x867cc8,
0x5f5e105 0x3039 0xffff7ef07251b820, 5.960464e-08
initialize_prn: 0x1a0633 0xdc8b50 0x60bed0 0x50a1eb 0xaf558e 0x431843 0x5370af,
0x5f5e10d 0x3039 0x58a22fe41a40, 5.960464e-08
initialize_prn: 0x1cbbd7 0x929a2a 0xa5f82f 0xdd0392 0x6770a 0x40be63 0x3312f,
0x5f5e115 0x3039 0x364258b9660, 5.960464e-08
initialize_prn: 0x1f717b 0x740344 0x4c4eb 0x35afd4 0x9c185b 0x455c64 0xe56bcb,
0x5f5e11d 0x3039 0xffffe3c2a308f480, 5.960464e-08
initialize_prn: 0x22271f 0x80c69e 0x8d9b04 0x93b15d 0x7d42bf 0x57eefa 0x518688,
0x5f5e125 0x3039 0x56a12bd8fca0, 5.960464e-08
initialize_prn: 0x24dcc3 0xb8e438 0x50f079 0x3ed2d9 0xb594f0 0xe35cd9 0xf933ea,
0x5f5e12d 0x3039 0xfffff8c6c2b476c0, 5.960464e-08
initialize_prn: 0x279267 0x1c5c11 0x5f3b4a 0x8d9ef4 0x9d432c 0x7fd2b5 0x5c46f3,
0x5f5e135 0x3039 0x6213ad902ae0, 5.960464e-08
initialize_prn: 0x2a480b 0xab2e2b 0xc8f178 0xe5605a 0x27172d 0x5c1f41 0x5dc72a,
0x5f5e13d 0x3039 0x63aef81ce100, 5.960464e-08
initialize_prn: 0x2cfdaf 0x655a85 0x9e8903 0xba21b7 0x2e7030 0x470f33 0xfa4491,
0x5f5e145 0x3039 0xfffff9e7fac76120, 5.960464e-08
initialize_prn: 0x2fb353 0x4ae11f 0xf077ea 0x8eadb7 0xc542f2 0x58c93e 0x1f6bac,
0x5f5e14d 0x3039 0x21682c387340, 5.960464e-08
initialize_prn: 0x3268f7 0x5bc1f8 0xcf342d 0xf48f06 0x8219ad 0x3c2a16 0xa4d980,
0x5f5e155 0x3039 0xffff9f138354df60, 5.960464e-08
initialize_prn: 0x351e9b 0x97fd12 0x4b33cd 0x8c1051 0xce141f 0x18206f 0x662f90,
0x5f5e15d 0x3039 0x6cabafbd6d80, 5.960464e-08
initialize_prn: 0x37d43f 0xff926c 0x74ecc9 0x43c42 0x32e782 0x1908fd 0x7e67e1,
0x5f5e165 0x3039 0x86716ecee5a0, 5.960464e-08
initialize_prn: 0x3a89e3 0x928206 0x5cd522 0x1add86 0xa8de94 0x9a0a73 0xa468f7,
0x5f5e16d 0x3039 0xffff9e9343220fc0, 5.960464e-08
initialize_prn: 0x3d3f87 0x50cbe0 0x1362d8 0x9c7ec9 0xe4d98f 0xee7187 0xa8d9d5,
0x5f5e175 0x3039 0xffffa349d48bb3e0, 5.960464e-08
initialize_prn: 0x3ff52b 0x3a6ff9 0xa90bea 0x646ab7 0xa64e30 0xcb0cec 0x1535ff,
0x5f5e17d 0x3039 0x16913e9c9a00, 5.960464e-08
error in initialize_prn 0
error in initialize_prn 1
error in initialize_prn 2
error in initialize_prn 3
error in initialize_prn 4
error in initialize_prn 5
error in initialize_prn 6
error in initialize_prn 7
error in initialize_prn 8
error in initialize_prn 9
error in initialize_prn 10
error in initialize_prn 11
error in initialize_prn 12
error in initialize_prn 13
error in initialize_prn 14
error in initialize_prn 15

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20211006/e74bef6a/attachment-0001.html>


More information about the llvm-bugs mailing list