[polly] r252197 - RunTimeDebugBuilder: Allocate memory _after_ knowing how much is needed

Tobias Grosser via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 5 11:43:35 PST 2015


Author: grosser
Date: Thu Nov  5 13:43:34 2015
New Revision: 252197

URL: http://llvm.org/viewvc/llvm-project?rev=252197&view=rev
Log:
RunTimeDebugBuilder: Allocate memory _after_ knowing how much is needed

This fixes a memory corruption issue, where we accessed more memory than
actually allocated.

Modified:
    polly/trunk/lib/CodeGen/RuntimeDebugBuilder.cpp

Modified: polly/trunk/lib/CodeGen/RuntimeDebugBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/CodeGen/RuntimeDebugBuilder.cpp?rev=252197&r1=252196&r2=252197&view=diff
==============================================================================
--- polly/trunk/lib/CodeGen/RuntimeDebugBuilder.cpp (original)
+++ polly/trunk/lib/CodeGen/RuntimeDebugBuilder.cpp Thu Nov  5 13:43:34 2015
@@ -163,20 +163,20 @@ void RuntimeDebugBuilder::createGPUPrint
                                             ArrayRef<Value *> Values) {
   std::string str;
 
-  // Allocate print buffer (assuming 2*32 bit per element)
-  auto T = ArrayType::get(Builder.getInt32Ty(), Values.size() * 2);
-  Value *Data = new AllocaInst(
-      T, "polly.vprint.buffer",
-      Builder.GetInsertBlock()->getParent()->getEntryBlock().begin());
-
   auto *Zero = Builder.getInt64(0);
-  auto *DataPtr = Builder.CreateGEP(Data, {Zero, Zero});
 
   auto ToPrint = getGPUThreadIdentifiers(Builder);
 
   ToPrint.push_back(Builder.CreateGlobalStringPtr("\n  ", "", 4));
   ToPrint.insert(ToPrint.end(), Values.begin(), Values.end());
 
+  // Allocate print buffer (assuming 2*32 bit per element)
+  auto T = ArrayType::get(Builder.getInt32Ty(), ToPrint.size() * 2);
+  Value *Data = new AllocaInst(
+      T, "polly.vprint.buffer",
+      Builder.GetInsertBlock()->getParent()->getEntryBlock().begin());
+  auto *DataPtr = Builder.CreateGEP(Data, {Zero, Zero});
+
   int Offset = 0;
   for (auto Val : ToPrint) {
     auto Ptr = Builder.CreateGEP(DataPtr, Builder.getInt64(Offset));




More information about the llvm-commits mailing list