[LLVMdev] Comparison mismatch causes assert using VStudio STL

Chuck Rose III cfr at adobe.com
Wed Apr 2 09:32:34 PDT 2008


Hola LLVMers,

 

We saw a problem with some code in LiveIntervalAnalysis.h/.c which we've
fixed locally.  We'd like to get a patch to the mainline and want to
know how you'd like it fixed.  A couple of things come together to cause
the problem:

 

  struct Idx2MBBCompare {

    bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const
{

      return LHS.first < RHS.first;

    }

 

This comparator function compares the first elements of the IdxMBBPair.
This is in contrast to the default comparator for std::pairs, which
compares the second element if the first elements are equal before
making a final decision.  In this case, it makes a lot of sense given
that the second in the pair is just a pointer.

 

In LiveIntervals::runOnMachineFunction, the map is sorted with this:

 

  std::sort(Idx2MBBMap.begin(), Idx2MBBMap.end(), Idx2MBBCompare());

 

Ok so far.

 

Here is where the problem arises:

 

    std::lower_bound(Idx2MBBMap.begin(), Idx2MBBMap.end(), LR.start);

 

By omitting the explicit comparator operator, the default is used which
looks at second.  Not a huge problem in general, since you don't really
care that the pointers are sorted.

 

Visual Studio's debug STL, however, is quite the stickler.  Debug
lower_bound checks to see that the container is sorted and since
lower_bound isn't given a comparison function, it uses the default one
which isn't the one used to sort the container, and (boom!) it asserts
if the memory stars aren't aligned properly.

 

In our code we fixed this by ditching the Idx2MBBCompare operator and
just added:

 

+  inline bool operator<(const IdxMBBPair &LHS, const IdxMBBPair &RHS) {

+    return (LHS.first < RHS.first);

+  }

 

The alternative is to make sure the Idx2MBBCompare is passed into the
appropriate places.

 

How would you like this to be fixed?  I can tool it up, but I don't want
to step on someone else's feet.

 

Thanks,

Chuck.

 

 

 

 

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20080402/cb4b814f/attachment.html>


More information about the llvm-dev mailing list