[clang] bfb7736 - [OpenMP] Fix accidental reuse of VLA size

Roger Ferrer Ibanez via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 6 22:59:31 PDT 2021


Author: Roger Ferrer Ibanez
Date: 2021-08-07T05:55:27Z
New Revision: bfb77364d0be8a5b159cdcf4eaf8e7a720802e89

URL: https://github.com/llvm/llvm-project/commit/bfb77364d0be8a5b159cdcf4eaf8e7a720802e89
DIFF: https://github.com/llvm/llvm-project/commit/bfb77364d0be8a5b159cdcf4eaf8e7a720802e89.diff

LOG: [OpenMP] Fix accidental reuse of VLA size

We were using an OpaqueValueExpr allocated on the stack to store
the size of a VLA. Because the VLASizeMap in CodegenFunction
uses the address of the expression to avoid recomputing VLAs,
we were accidentally reusing an earlier llvm::Value. This led to
invalid LLVM IR.

This is a temporary solution until VLASizeMap can be pushed and popped
based on the context.

Differential Revision: https://reviews.llvm.org/D107666

Added: 
    clang/test/OpenMP/vla_iterator_cache_bug.c

Modified: 
    clang/lib/CodeGen/CGOpenMPRuntime.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index ca98c7a57446..90fcf2232be2 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -4401,14 +4401,14 @@ CGOpenMPRuntime::emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
     if (NumOfElements) {
       NumOfElements = CGF.Builder.CreateNUWAdd(
           llvm::ConstantInt::get(CGF.SizeTy, NumAffinities), NumOfElements);
-      OpaqueValueExpr OVE(
+      auto *OVE = new (C) OpaqueValueExpr(
           Loc,
           C.getIntTypeForBitwidth(C.getTypeSize(C.getSizeType()), /*Signed=*/0),
           VK_PRValue);
-      CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE,
+      CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, OVE,
                                                     RValue::get(NumOfElements));
       KmpTaskAffinityInfoArrayTy =
-          C.getVariableArrayType(KmpTaskAffinityInfoTy, &OVE, ArrayType::Normal,
+          C.getVariableArrayType(KmpTaskAffinityInfoTy, OVE, ArrayType::Normal,
                                  /*IndexTypeQuals=*/0, SourceRange(Loc, Loc));
       // Properly emit variable-sized array.
       auto *PD = ImplicitParamDecl::Create(C, KmpTaskAffinityInfoArrayTy,
@@ -4899,13 +4899,13 @@ std::pair<llvm::Value *, Address> CGOpenMPRuntime::emitDependClause(
       NumOfElements =
           CGF.Builder.CreateNUWAdd(NumOfRegularWithIterators, NumOfElements);
     }
-    OpaqueValueExpr OVE(Loc,
-                        C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0),
-                        VK_PRValue);
-    CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE,
+    auto *OVE = new (C) OpaqueValueExpr(
+        Loc, C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0),
+        VK_PRValue);
+    CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, OVE,
                                                   RValue::get(NumOfElements));
     KmpDependInfoArrayTy =
-        C.getVariableArrayType(KmpDependInfoTy, &OVE, ArrayType::Normal,
+        C.getVariableArrayType(KmpDependInfoTy, OVE, ArrayType::Normal,
                                /*IndexTypeQuals=*/0, SourceRange(Loc, Loc));
     // CGF.EmitVariablyModifiedType(KmpDependInfoArrayTy);
     // Properly emit variable-sized array.

diff  --git a/clang/test/OpenMP/vla_iterator_cache_bug.c b/clang/test/OpenMP/vla_iterator_cache_bug.c
new file mode 100644
index 000000000000..ad5a664346e2
--- /dev/null
+++ b/clang/test/OpenMP/vla_iterator_cache_bug.c
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-linux-gnu \
+// RUN:   -emit-llvm %s -o - | FileCheck %s
+
+// expected-no-diagnostics
+
+extern int bounds1(int);
+extern int bounds2(int);
+
+extern void fun2(int n, int *a, int *b);
+extern void fun3(int n, int *a, int *b);
+
+void fun1(int n, int *a, int *b)
+{
+#pragma omp task depend(iterator(j = 0 : bounds1(n)), in : a[b[j]])
+  {
+    fun2(n, a, b);
+  }
+// CHECK: alloca %struct.kmp_depend_info, i64 [[FIRST_VLA:%.*]], align 16
+
+#pragma omp task depend(iterator(j = 0 : bounds2(n)), in : a[b[j]])
+  {
+    fun3(n, a, b);
+  }
+// CHECK-NOT: alloca %struct.kmp_depend_info, i64 [[FIRST_VLA]], align 16
+}


        


More information about the cfe-commits mailing list