Page MenuHomePhabricator

[OPENMP] Fix mixture of omp and clang pragmas

Authored by hishiguro on May 13 2020, 11:12 PM.



Fixes PR45753

When a program that contains a loop to which both "omp parallel for" pragma and "clang loop" pragma are associated is compiled with the -fopenmp option, "clang loop" pragma will not take effect.
The example below should not be vectorized by the "clang loop` pragma but is it actually vectorized.
The cause is that "llvm.loop.vectorize.width" is not output to the IR when -fopenmp is specified.
The fix attaches attributes if they exist in the loop.


int a[100], b[100];
int foo() {
#pragma omp parallel for
#pragma clang loop vectorize(disable)
for (int i=0; i<100; i++)

clang -O2 -fopenmp a.c -c -Rpass=vect
a.c:3:1: remark: vectorized loop (vectorization width: 4, interleaved count: 2) [-Rpass=loop-vectorize] #pragma omp parallel for ^


  • -fopenmp

$ clang -O2 a.c -S -emit-llvm -mllvm -disable-llvm-optzns -o - -fopenmp |grep "vectorize\.width"

  • -fno-openmp

$ clang -O2 a.c -S -emit-llvm -mllvm -disable-llvm-optzns -o - -fno-openmp |grep "vectorize\.width"
!7 = !{!"llvm.loop.vectorize.width", i32 1}

Diff Detail

Event Timeline

hishiguro created this revision.May 13 2020, 11:12 PM
hishiguro updated this revision to Diff 263964.May 14 2020, 3:44 AM

Fix clang-format error.


  1. Cast it to OMPExecutableDirective here, it should be enough.
  2. No need for conditional casting, use cast<OMPExecutableDirective>(...). Or you can change the function itself to accept const OMPExecutableDirective &S instead of const Stmt &S.

Use getInnermostCapturedStmt() instead.

Thank you for your comments. I will check those comments.

hishiguro updated this revision to Diff 264836.May 19 2020, 3:47 AM
hishiguro retitled this revision from [OpenMP] Fix omp and clang pragmas to [OPENMP] Fix mixture of omp and clang pragmas.

I checked that your comments are correct. I modified the source, and added a test.

ABataev added inline comments.May 19 2020, 4:27 AM

const auto &OMPED = cast<OMPExecutableDirective>(S);

hishiguro updated this revision to Diff 265450.May 21 2020, 1:17 AM

Thank you for your comment. I updated source.

This revision is now accepted and ready to land.May 21 2020, 5:27 AM
This revision was automatically updated to reflect the committed changes.