[cfe-commits] r106485 - in /cfe/trunk/lib/CodeGen: CGDeclCXX.cpp CodeGenModule.h

Chris Lattner clattner at apple.com
Mon Jun 21 15:57:03 PDT 2010


On Jun 21, 2010, at 2:27 PM, Fariborz Jahanian wrote:
> URL: http://llvm.org/viewvc/llvm-project?rev=106485&view=rev
> Log:
> In supporting init-priority, globals with the same init_priority must be
> emitted in the order in which they are seen (still radar 8076356).

Thanks Fariborz, one more tweak :)

> 
>   if (D->hasAttr<InitPriorityAttr>()) {
> +    static unsigned lix = 0; // to keep the lexical order of equal priority
> +                             // objects intact;
>     unsigned int order = D->getAttr<InitPriorityAttr>()->getPriority();
> -    PrioritizedCXXGlobalInits.push_back(std::make_pair(order,Fn));
> +    OrderGlobalInitsType Key(order, lix++);
> +    PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));

Using a static here will interact poorly with threading.  You already have the order because of the position in the list.  How about using something like:

OrderGlobalInitsType Key(PrioritizedCXXGlobalInits.size(), lix++);

instead?


>   }
>   else
>     CXXGlobalInits.push_back(Fn);
> }
> 
> +typedef std::pair<CodeGen::OrderGlobalInitsType, 
> +                  llvm::Function *> global_init_pair;
> +static int PrioritizedCXXGlobalInitsCmp(const void* a, const void* b) {
> +  const global_init_pair *LHS = static_cast<const global_init_pair*>(a);
> +  const global_init_pair *RHS = static_cast<const global_init_pair*>(b);
> +  if (LHS->first.priority < RHS->first.priority)
> +    return -1;
> +  if (LHS->first.priority == RHS->first.priority) {
> +    if (LHS->first.lex_order < RHS->first.lex_order)
> +      return -1;
> +    if (LHS->first.lex_order == RHS->first.lex_order)
> +      return 0;
> +  }
> +  return +1;
> +}

Is this actually needed?  I think that the default std::pair operator< will do the right thing, you just need to make OrderGlobalInits be a typedef for std::pair instead of its own struct.

Also, fyi you can just use "unsigned" instead of "unsigned int", but it's not a big deal either way.

-Chris

> +                                        
> void
> CodeGenModule::EmitCXXGlobalInitFunc() {
>   if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
> @@ -195,7 +214,8 @@
>   if (!PrioritizedCXXGlobalInits.empty()) {
>     llvm::SmallVector<llvm::Constant*, 8> LocalCXXGlobalInits;
>     llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(), 
> -                         PrioritizedCXXGlobalInits.end());
> +                         PrioritizedCXXGlobalInits.end(), 
> +                         PrioritizedCXXGlobalInitsCmp);
>     for (unsigned i = 0; i < PrioritizedCXXGlobalInits.size(); i++) {
>       llvm::Function *Fn = PrioritizedCXXGlobalInits[i].second;
>       LocalCXXGlobalInits.push_back(Fn);
> 
> Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=106485&r1=106484&r2=106485&view=diff
> ==============================================================================
> --- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
> +++ cfe/trunk/lib/CodeGen/CodeGenModule.h Mon Jun 21 16:27:42 2010
> @@ -75,6 +75,13 @@
>   class CGObjCRuntime;
>   class MangleBuffer;
> 
> +  typedef struct OrderGlobalInits{
> +    unsigned int priority;
> +    unsigned int lex_order;
> +    OrderGlobalInits(unsigned int p, unsigned int l) 
> +    : priority(p), lex_order(l) {}
> +  } OrderGlobalInitsType;
> +  
> /// CodeGenModule - This class organizes the cross-function state that is used
> /// while generating LLVM code.
> class CodeGenModule : public BlockModule {
> @@ -142,7 +149,8 @@
> 
>   /// - Global variables with initializers whose order of initialization
>   /// is set by init_priority attribute.
> -  llvm::SmallVector<std::pair<unsigned int, llvm::Function*>, 8> 
> +  
> +  llvm::SmallVector<std::pair<OrderGlobalInitsType, llvm::Function*>, 8> 
>     PrioritizedCXXGlobalInits;
> 
>   /// CXXGlobalDtors - Global destructor functions and arguments that need to
> 
> 
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits





More information about the cfe-commits mailing list