[llvm-commits] [polly] r138204 - in /polly/trunk: include/polly/Cloog.h include/polly/ScopInfo.h lib/Analysis/Dependences.cpp lib/Analysis/ScopInfo.cpp lib/Cloog.cpp lib/CodeGeneration.cpp lib/Exchange/JSONExporter.cpp lib/Support/GICHelper.cpp

Tobias Grosser grosser at fim.uni-passau.de
Sat Aug 20 04:11:26 PDT 2011


Author: grosser
Date: Sat Aug 20 06:11:25 2011
New Revision: 138204

URL: http://llvm.org/viewvc/llvm-project?rev=138204&view=rev
Log:
Free isl_ctx and fix several memory leaks

Because of me not understanding the LLVM pass structure well, I did not find a
good way to allocate isl_ctx and to free it later without getting issues with
reference counting. I now found this place, such that we can free isl_ctx. This
patch also fixes the memory leaks that were ignored beforehand.

Modified:
    polly/trunk/include/polly/Cloog.h
    polly/trunk/include/polly/ScopInfo.h
    polly/trunk/lib/Analysis/Dependences.cpp
    polly/trunk/lib/Analysis/ScopInfo.cpp
    polly/trunk/lib/Cloog.cpp
    polly/trunk/lib/CodeGeneration.cpp
    polly/trunk/lib/Exchange/JSONExporter.cpp
    polly/trunk/lib/Support/GICHelper.cpp

Modified: polly/trunk/include/polly/Cloog.h
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/include/polly/Cloog.h?rev=138204&r1=138203&r2=138204&view=diff
==============================================================================
--- polly/trunk/include/polly/Cloog.h (original)
+++ polly/trunk/include/polly/Cloog.h Sat Aug 20 06:11:25 2011
@@ -58,6 +58,7 @@
     bool runOnScop(Scop &S);
     void printScop(llvm::raw_ostream &OS) const;
     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
+    virtual void releaseMemory();
   };
 }
 #endif /* POLLY_CLOOG_H */

Modified: polly/trunk/include/polly/ScopInfo.h
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/include/polly/ScopInfo.h?rev=138204&r1=138203&r2=138204&view=diff
==============================================================================
--- polly/trunk/include/polly/ScopInfo.h (original)
+++ polly/trunk/include/polly/ScopInfo.h Sat Aug 20 06:11:25 2011
@@ -296,7 +296,7 @@
   ///
   /// @return The scattering function of this ScopStmt.
   isl_map *getScattering() const { return Scattering; }
-  void setScattering(isl_map *scattering) { Scattering = scattering; }
+  void setScattering(isl_map *scattering);
 
   /// @brief Get an isl string representing this scattering.
   std::string getScatteringStr() const;
@@ -408,7 +408,7 @@
 
   /// Create the static control part with a region, max loop depth of this
   /// region and parameters used in this region.
-  Scop(TempScop &TempScop, LoopInfo &LI, ScalarEvolution &SE);
+  Scop(TempScop &TempScop, LoopInfo &LI, ScalarEvolution &SE, isl_ctx *ctx);
 
   /// @brief Check if a basic block is trivial.
   ///
@@ -552,6 +552,7 @@
 
   // The Scop
   Scop *scop;
+  isl_ctx *ctx;
 
   void clear() {
     if (scop) {
@@ -562,8 +563,8 @@
 
 public:
   static char ID;
-  explicit ScopInfo() : RegionPass(ID), scop(0) {}
-  ~ScopInfo() { clear(); }
+  explicit ScopInfo();
+  ~ScopInfo();
 
   /// @brief Try to build the Polly IR of static control part on the current
   ///        SESE-Region.

Modified: polly/trunk/lib/Analysis/Dependences.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/Analysis/Dependences.cpp?rev=138204&r1=138203&r2=138204&view=diff
==============================================================================
--- polly/trunk/lib/Analysis/Dependences.cpp (original)
+++ polly/trunk/lib/Analysis/Dependences.cpp Sat Aug 20 06:11:25 2011
@@ -213,25 +213,32 @@
   temp_must_no_source = isl_union_map_coalesce(temp_must_no_source);
   temp_may_no_source = isl_union_map_coalesce(temp_may_no_source);
 
+  bool isValid = true;
+
   if (!isl_union_map_is_equal(temp_must_dep, must_dep)) {
     DEBUG(dbgs().indent(4) << "\nEqual 1 calculated\n");
-    return false;
+    isValid = false;
   }
 
   DEBUG(dbgs().indent(4) << "\nEqual 1 calculated\n");
 
   if (!isl_union_map_is_equal(temp_may_dep, may_dep))
-    return false;
+    isValid = false;
 
   DEBUG(dbgs().indent(4) << "\nEqual 2 calculated\n");
 
   if (!isl_union_map_is_equal(temp_must_no_source, must_no_source))
-    return false;
+    isValid = false;
 
   if (!isl_union_map_is_equal(temp_may_no_source, may_no_source))
-    return false;
+    isValid = false;
+
+  isl_union_map_free(temp_must_dep);
+  isl_union_map_free(temp_may_dep);
+  isl_union_map_free(temp_must_no_source);
+  isl_union_map_free(temp_may_no_source);
 
-  return true;
+  return isValid;
 }
 
 isl_union_map* getCombinedScheduleForDim(Scop *scop, unsigned dimLevel) {
@@ -278,8 +285,7 @@
 
   scheduleDeps_waw = isl_union_map_apply_range(isl_union_map_copy(waw_dep),
                                                isl_union_map_copy(schedule));
-  scheduleDeps_waw = isl_union_map_apply_domain(scheduleDeps_waw,
-                                                isl_union_map_copy(schedule));
+  scheduleDeps_waw = isl_union_map_apply_domain(scheduleDeps_waw, schedule);
 
   // Dependences need to originate and to terminate in the scheduling space
   // enumerated by this loop.
@@ -300,7 +306,7 @@
   restrictedDeps_waw = isl_union_map_intersect_domain(scheduleDeps_waw,
     isl_union_set_copy(scheduleSubset));
   restrictedDeps_waw = isl_union_map_intersect_range(restrictedDeps_waw,
-    isl_union_set_copy(scheduleSubset));
+    scheduleSubset);
 
   isl_union_set *distance_waw = isl_union_map_deltas(restrictedDeps_waw);
 
@@ -353,10 +359,16 @@
 
   isl_union_set *nonValid_waw = isl_union_set_subtract(distance_waw,
                                                        validDistancesUS);
-
-  return isl_union_set_is_empty(nonValid)
+  bool is_parallel = isl_union_set_is_empty(nonValid)
     && isl_union_set_is_empty(nonValid_war)
     && isl_union_set_is_empty(nonValid_waw);
+
+  isl_dim_free(dim);
+  isl_union_set_free(nonValid);
+  isl_union_set_free(nonValid_war);
+  isl_union_set_free(nonValid_waw);
+
+  return is_parallel;
 }
 
 bool Dependences::isParallelFor(const clast_for *f) {

Modified: polly/trunk/lib/Analysis/ScopInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/Analysis/ScopInfo.cpp?rev=138204&r1=138203&r2=138204&view=diff
==============================================================================
--- polly/trunk/lib/Analysis/ScopInfo.cpp (original)
+++ polly/trunk/lib/Analysis/ScopInfo.cpp Sat Aug 20 06:11:25 2011
@@ -466,7 +466,12 @@
   bset = isl_basic_set_add_constraint(bset, c);
   isl_set *strideZero = isl_set_from_basic_set(bset);
 
-  return isl_set_is_equal(stride, strideZero);
+  bool isStrideZero = isl_set_is_equal(stride, strideZero);
+
+  isl_set_free(strideZero);
+  isl_set_free(stride);
+
+  return isStrideZero;
 }
 
 bool MemoryAccess::isStrideOne(const isl_set *domainSubset) const {
@@ -484,16 +489,27 @@
   isl_basic_set *bset = isl_basic_set_universe(isl_set_get_dim(stride));
 
   bset = isl_basic_set_add_constraint(bset, c);
-  isl_set *strideZero = isl_set_from_basic_set(bset);
+  isl_set *strideOne = isl_set_from_basic_set(bset);
+
+  bool isStrideOne = isl_set_is_equal(stride, strideOne);
 
-  return isl_set_is_equal(stride, strideZero);
+  isl_set_free(strideOne);
+  isl_set_free(stride);
+
+  return isStrideOne;
 }
 
 void MemoryAccess::setNewAccessFunction(isl_map *newAccess) {
+  isl_map_free(newAccessRelation);
   newAccessRelation = newAccess;
 }
 
 //===----------------------------------------------------------------------===//
+void ScopStmt::setScattering(isl_map *scattering) {
+  isl_map_free(Scattering);
+  Scattering = scattering;
+}
+
 void ScopStmt::buildScattering(SmallVectorImpl<unsigned> &Scatter) {
   unsigned NumberOfIterators = getNumIterators();
   unsigned ScatDim = Parent.getMaxLoopDepth() * 2 + 1;
@@ -607,12 +623,12 @@
   isl_pw_aff *Bound = SCEVAffinator::getPwAff(this, UpperBound.OriginalSCEV, 0);
   isl_set *set = isl_pw_aff_le_set(BoundedDim, Bound);
   set = isl_set_set_tuple_name(set, isl_dim_get_tuple_name(Dim, isl_dim_set));
+  isl_dim_free(Dim);
   return set;
 }
 
 void ScopStmt::buildIterationDomainFromLoops(TempScop &tempScop) {
-  isl_dim *dim = isl_dim_set_alloc(Parent.getCtx(), 0,
-                                   getNumIterators());
+  isl_dim *dim = isl_dim_set_alloc(getIslContext(), 0, getNumIterators());
   dim = isl_dim_set_tuple_name(dim, isl_dim_set, getBaseName());
 
   Domain = isl_set_universe(isl_dim_copy(dim));
@@ -637,6 +653,7 @@
     Domain = isl_set_intersect(Domain, UpperBoundSet);
   }
 
+  isl_dim_free(dim);
   isl_int_clear(v);
 }
 
@@ -843,11 +860,10 @@
 
 //===----------------------------------------------------------------------===//
 /// Scop class implement
-Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution)
+Scop::Scop(TempScop &tempScop, LoopInfo &LI, ScalarEvolution &ScalarEvolution,
+           isl_ctx *ctx)
            : SE(&ScalarEvolution), R(tempScop.getMaxRegion()),
            MaxLoopDepth(tempScop.getMaxLoopDepth()) {
-  isl_ctx *ctx = isl_ctx_alloc();
-
   ParamSetType &Params = tempScop.getParamSet();
   Parameters.insert(Parameters.begin(), Params.begin(), Params.end());
 
@@ -887,9 +903,6 @@
   // Free the statements;
   for (iterator I = begin(), E = end(); I != E; ++I)
     delete *I;
-
-  // Do we need a singleton to manage this?
-  //isl_ctx_free(ctx);
 }
 
 std::string Scop::getContextStr() const {
@@ -993,6 +1006,16 @@
 }
 
 //===----------------------------------------------------------------------===//
+ScopInfo::ScopInfo() : RegionPass(ID), scop(0) {
+  ctx = isl_ctx_alloc();
+}
+
+ScopInfo::~ScopInfo() {
+  clear();
+  isl_ctx_free(ctx);
+}
+
+
 
 void ScopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
   AU.addRequired<LoopInfo>();
@@ -1018,7 +1041,7 @@
   ++ScopFound;
   if (tempScop->getMaxLoopDepth() > 0) ++RichScopFound;
 
-  scop = new Scop(*tempScop, LI, SE);
+  scop = new Scop(*tempScop, LI, SE, ctx);
 
   return false;
 }

Modified: polly/trunk/lib/Cloog.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/Cloog.cpp?rev=138204&r1=138203&r2=138204&view=diff
==============================================================================
--- polly/trunk/lib/Cloog.cpp (original)
+++ polly/trunk/lib/Cloog.cpp Sat Aug 20 06:11:25 2011
@@ -30,6 +30,7 @@
 #include "llvm/Module.h"
 
 #include "cloog/isl/domain.h"
+#include "cloog/isl/cloog.h"
 
 using namespace llvm;
 using namespace polly;
@@ -61,14 +62,14 @@
 };
 
 Cloog::Cloog(Scop *Scop) : S(Scop) {
-  State = cloog_state_malloc();
+  State = cloog_isl_state_malloc(Scop->getCtx());
   buildCloogOptions();
   ClastRoot = cloog_clast_create_from_input(buildCloogInput(), Options);
 }
 
 Cloog::~Cloog() {
-  cloog_options_free(Options);
   cloog_clast_free(ClastRoot);
+  cloog_options_free(Options);
   cloog_state_free(State);
 }
 
@@ -164,10 +165,9 @@
       cloog_domain_from_isl_set(Stmt->getDomain());
 
     std::string entryName = Stmt->getBaseName();
-    char *Name = (char*)malloc(sizeof(char) * (entryName.size() + 1));
-    strcpy(Name, entryName.c_str());
 
-    DU = cloog_union_domain_add_domain(DU, Name, Domain, Scattering, Stmt);
+    DU = cloog_union_domain_add_domain(DU, entryName.c_str(), Domain,
+                                       Scattering, Stmt);
   }
 
   return DU;
@@ -265,6 +265,13 @@
   return C->getClast();
 }
 
+void CloogInfo::releaseMemory() {
+  if (C) {
+    delete C;
+    C = 0;
+  }
+}
+
 bool CloogInfo::runOnScop(Scop &S) {
   if (C)
     delete C;

Modified: polly/trunk/lib/CodeGeneration.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/CodeGeneration.cpp?rev=138204&r1=138203&r2=138204&view=diff
==============================================================================
--- polly/trunk/lib/CodeGeneration.cpp (original)
+++ polly/trunk/lib/CodeGeneration.cpp Sat Aug 20 06:11:25 2011
@@ -1134,7 +1134,7 @@
     map = isl_map_intersect(map, identity);
 
     isl_map *lexmax = isl_map_lexmax(isl_map_copy(map));
-    isl_map *lexmin = isl_map_lexmin(isl_map_copy(map));
+    isl_map *lexmin = isl_map_lexmin(map);
     isl_map *sub = isl_map_sum(lexmax, isl_map_neg(lexmin));
 
     isl_set *elements = isl_map_range(sub);
@@ -1149,6 +1149,7 @@
     isl_point_get_coordinate(p, isl_dim_set, isl_set_n_dim(loopDomain) - 1, &v);
     int numberIterations = isl_int_get_si(v);
     isl_int_clear(v);
+    isl_point_free(p);
 
     return (numberIterations) / isl_int_get_si(f->stride) + 1;
   }

Modified: polly/trunk/lib/Exchange/JSONExporter.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/Exchange/JSONExporter.cpp?rev=138204&r1=138203&r2=138204&view=diff
==============================================================================
--- polly/trunk/lib/Exchange/JSONExporter.cpp (original)
+++ polly/trunk/lib/Exchange/JSONExporter.cpp Sat Aug 20 06:11:25 2011
@@ -272,17 +272,21 @@
       if (!isl_map_has_equal_dim(currentAccessMap, newAccessMap)) {
         errs() << "JScop file contains access function with incompatible "
                << "dimensions\n";
+        isl_map_free(newAccessMap);
         return false;
       }
       if (isl_map_dim(newAccessMap, isl_dim_out) != 1) {
         errs() << "New access map in JScop file should be single dimensional\n";
+        isl_map_free(newAccessMap);
         return false;
       }
       if (!isl_map_is_equal(newAccessMap, currentAccessMap)) {
         // Statistics.
         ++NewAccessMapFound;
-        newAccessStrings.push_back(accesses.asCString());	
+        newAccessStrings.push_back(accesses.asCString());
         (*MI)->setNewAccessFunction(newAccessMap);
+      } else {
+        isl_map_free(newAccessMap);
       }
       memoryAccessIdx++;
     }

Modified: polly/trunk/lib/Support/GICHelper.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/Support/GICHelper.cpp?rev=138204&r1=138203&r2=138204&view=diff
==============================================================================
--- polly/trunk/lib/Support/GICHelper.cpp (original)
+++ polly/trunk/lib/Support/GICHelper.cpp Sat Aug 20 06:11:25 2011
@@ -48,6 +48,7 @@
   if (p) {
     APInt A((unsigned)mpz_sizeinbase(mpz, 2), (unsigned)sz , p);
     A = A.zext(A.getBitWidth() + 1);
+    free(p);
 
     if (mpz_sgn(mpz) == -1)
       return -A;





More information about the llvm-commits mailing list