Skip to content

Commit af2e071

Browse files
committed
Fix UB in benchmakrs
This kind of fix would be not be good for production code, but it does not change the essenset of benchmark, so should be good enough here. ix[k] can be 0 causing negative index on array access. p[ip][0] can be outside of Index_type. Differential Revision: https://reviews.llvm.org/D157348
1 parent f6ef9c0 commit af2e071

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

MicroBenchmarks/LCALS/SubsetCLambdaLoops/LambdaSubsetCbenchmarks.cxx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -379,16 +379,16 @@ static void BM_PIC_2D_LAMBDA(benchmark::State& state) {
379379
forall<exec_policy>(0, state.range(0),
380380
[&] (Index_type ip) {
381381
Index_type i1, j1, i2, j2;
382-
i1 = (Index_type) p[ip][0];
383-
j1 = (Index_type) p[ip][1];
382+
i1 = (Index_type) (long long) p[ip][0];
383+
j1 = (Index_type) (long long) p[ip][1];
384384
i1 &= 64-1;
385385
j1 &= 64-1;
386386
p[ip][2] += b[j1][i1];
387387
p[ip][3] += c[j1][i1];
388388
p[ip][0] += p[ip][2];
389389
p[ip][1] += p[ip][3];
390-
i2 = (Index_type) p[ip][0];
391-
j2 = (Index_type) p[ip][1];
390+
i2 = (Index_type) (long long) p[ip][0];
391+
j2 = (Index_type) (long long) p[ip][1];
392392
i2 = ( i2 & 64-1 ) ;
393393
j2 = ( j2 & 64-1 ) ;
394394
p[ip][0] += y[i2+32];
@@ -435,8 +435,8 @@ static void BM_PIC_1D_LAMBDA(benchmark::State& state) {
435435
xx[k] = 0.0;
436436
ix[k] = (Index_type) grd[k];
437437
xi[k] = (Real_type) ix[k];
438-
ex1[k] = ex[ ix[k] - 1 ];
439-
dex1[k] = dex[ ix[k] - 1 ];
438+
ex1[k] = ix[k] ? ex[ ix[k] - 1 ] : 0;
439+
dex1[k] = ix[k] ? dex[ ix[k] - 1 ] : 0;
440440
} );
441441

442442
forall<exec_policy>(0, state.range(0),

MicroBenchmarks/LCALS/SubsetCRawLoops/RawSubsetCbenchmarks.cxx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -549,16 +549,16 @@ static void BM_PIC_2D_RAW(benchmark::State& state) {
549549

550550
for (Index_type ip=0 ; ip< state.range(0) ; ip++ ) {
551551
Index_type i1, j1, i2, j2;
552-
i1 = (Index_type) p[ip][0];
553-
j1 = (Index_type) p[ip][1];
552+
i1 = (Index_type) (long long) p[ip][0];
553+
j1 = (Index_type) (long long) p[ip][1];
554554
i1 &= 64-1;
555555
j1 &= 64-1;
556556
p[ip][2] += b[j1][i1];
557557
p[ip][3] += c[j1][i1];
558558
p[ip][0] += p[ip][2];
559559
p[ip][1] += p[ip][3];
560-
i2 = (Index_type) p[ip][0];
561-
j2 = (Index_type) p[ip][1];
560+
i2 = (Index_type) (long long) p[ip][0];
561+
j2 = (Index_type) (long long) p[ip][1];
562562
i2 = ( i2 & 64-1 ) ;
563563
j2 = ( j2 & 64-1 ) ;
564564
p[ip][0] += y[i2+32];
@@ -631,8 +631,8 @@ static void BM_PIC_1D_RAW(benchmark::State& state) {
631631
xx[k] = 0.0;
632632
ix[k] = (Index_type) grd[k];
633633
xi[k] = (Real_type) ix[k];
634-
ex1[k] = ex[ ix[k] - 1 ];
635-
dex1[k] = dex[ ix[k] - 1 ];
634+
ex1[k] = ix[k] ? ex[ ix[k] - 1 ] : 0;
635+
dex1[k] = ix[k] ? dex[ ix[k] - 1 ] : 0;
636636
}
637637

638638
for (Index_type k=0 ; k< state.range(0) ; k++ ) {

0 commit comments

Comments
 (0)