Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: src/IceGlobalContext.cpp

Issue 1069453003: Subzero: Deterministically sort constant pool entries. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 //===- subzero/src/IceGlobalContext.cpp - Global context defs -------------===// 1 //===- subzero/src/IceGlobalContext.cpp - Global context defs -------------===//
2 // 2 //
3 // The Subzero Code Generator 3 // The Subzero Code Generator
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // This file defines aspects of the compilation that persist across 10 // This file defines aspects of the compilation that persist across
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 } 57 }
58 }; 58 };
59 template <typename KeyType> 59 template <typename KeyType>
60 struct KeyCompare<KeyType, typename std::enable_if< 60 struct KeyCompare<KeyType, typename std::enable_if<
61 std::is_floating_point<KeyType>::value>::type> { 61 std::is_floating_point<KeyType>::value>::type> {
62 bool operator()(const KeyType &Value1, const KeyType &Value2) const { 62 bool operator()(const KeyType &Value1, const KeyType &Value2) const {
63 return !memcmp(&Value1, &Value2, sizeof(KeyType)); 63 return !memcmp(&Value1, &Value2, sizeof(KeyType));
64 } 64 }
65 }; 65 };
66 66
67 // Define a key comparison function for sorting the constant pool's
68 // values after they are dumped to a vector. This covers integer
69 // types, floating point types, and ConstantRelocatable values.
70 template <typename ValueType, class Enable = void> struct KeyCompareLess {};
71
72 template <typename ValueType>
73 struct KeyCompareLess<ValueType,
74 typename std::enable_if<std::is_floating_point<
75 typename ValueType::PrimType>::value>::type> {
76 bool operator()(const Constant *Const1, const Constant *Const2) const {
77 typename ValueType::PrimType V1 = llvm::cast<ValueType>(Const1)->getValue();
78 typename ValueType::PrimType V2 = llvm::cast<ValueType>(Const2)->getValue();
79 return memcmp(&V1, &V2, sizeof(V1)) < 0;
80 }
81 };
82 template <typename ValueType>
83 struct KeyCompareLess<ValueType,
84 typename std::enable_if<std::is_integral<
85 typename ValueType::PrimType>::value>::type> {
86 bool operator()(const Constant *Const1, const Constant *Const2) const {
87 typename ValueType::PrimType V1 = llvm::cast<ValueType>(Const1)->getValue();
88 typename ValueType::PrimType V2 = llvm::cast<ValueType>(Const2)->getValue();
89 return V1 < V2;
90 }
91 };
92 template <typename ValueType>
93 struct KeyCompareLess<
94 ValueType, typename std::enable_if<
95 std::is_same<ValueType, ConstantRelocatable>::value>::type> {
96 bool operator()(const Constant *Const1, const Constant *Const2) const {
97 auto V1 = llvm::cast<ValueType>(Const1);
98 auto V2 = llvm::cast<ValueType>(Const2);
99 if (V1->getName() == V2->getName())
100 return V1->getOffset() < V2->getOffset();
101 return V1->getName() < V2->getName();
102 }
103 };
104
67 // TypePool maps constants of type KeyType (e.g. float) to pointers to 105 // TypePool maps constants of type KeyType (e.g. float) to pointers to
68 // type ValueType (e.g. ConstantFloat). 106 // type ValueType (e.g. ConstantFloat).
69 template <Type Ty, typename KeyType, typename ValueType> class TypePool { 107 template <Type Ty, typename KeyType, typename ValueType> class TypePool {
70 TypePool(const TypePool &) = delete; 108 TypePool(const TypePool &) = delete;
71 TypePool &operator=(const TypePool &) = delete; 109 TypePool &operator=(const TypePool &) = delete;
72 110
73 public: 111 public:
74 TypePool() : NextPoolID(0) {} 112 TypePool() : NextPoolID(0) {}
75 ValueType *getOrAdd(GlobalContext *Ctx, KeyType Key) { 113 ValueType *getOrAdd(GlobalContext *Ctx, KeyType Key) {
76 auto Iter = Pool.find(Key); 114 auto Iter = Pool.find(Key);
77 if (Iter != Pool.end()) 115 if (Iter != Pool.end())
78 return Iter->second; 116 return Iter->second;
79 ValueType *Result = ValueType::create(Ctx, Ty, Key, NextPoolID++); 117 ValueType *Result = ValueType::create(Ctx, Ty, Key, NextPoolID++);
80 Pool[Key] = Result; 118 Pool[Key] = Result;
81 return Result; 119 return Result;
82 } 120 }
83 ConstantList getConstantPool() const { 121 ConstantList getConstantPool() const {
84 ConstantList Constants; 122 ConstantList Constants;
85 Constants.reserve(Pool.size()); 123 Constants.reserve(Pool.size());
86 for (auto &I : Pool) 124 for (auto &I : Pool)
87 Constants.push_back(I.second); 125 Constants.push_back(I.second);
126 // The sort (and its KeyCompareLess machinery) is not strictly
127 // necessary, but is desirable for producing output that is
128 // deterministic across unordered_map::iterator implementations.
129 std::sort(Constants.begin(), Constants.end(), KeyCompareLess<ValueType>());
88 return Constants; 130 return Constants;
89 } 131 }
90 132
91 private: 133 private:
92 // Use the default hash function, and a custom key comparison 134 // Use the default hash function, and a custom key comparison
93 // function. The key comparison function for floating point 135 // function. The key comparison function for floating point
94 // variables can't use the default == based implementation because 136 // variables can't use the default == based implementation because
95 // of special C++ semantics regarding +0.0, -0.0, and NaN 137 // of special C++ semantics regarding +0.0, -0.0, and NaN
96 // comparison. However, it's OK to use the default hash for 138 // comparison. However, it's OK to use the default hash for
97 // floating point values because KeyCompare is the final source of 139 // floating point values because KeyCompare is the final source of
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after
773 Ctx = Func->getContext(); 815 Ctx = Func->getContext();
774 Active = 816 Active =
775 Func->getFocusedTiming() || Ctx->getFlags().getSubzeroTimingEnabled(); 817 Func->getFocusedTiming() || Ctx->getFlags().getSubzeroTimingEnabled();
776 if (Active) 818 if (Active)
777 Ctx->pushTimer(ID, StackID); 819 Ctx->pushTimer(ID, StackID);
778 } 820 }
779 821
780 ICE_TLS_DEFINE_FIELD(GlobalContext::ThreadContext *, GlobalContext, TLS); 822 ICE_TLS_DEFINE_FIELD(GlobalContext::ThreadContext *, GlobalContext, TLS);
781 823
782 } // end of namespace Ice 824 } // end of namespace Ice
OLDNEW
« no previous file with comments | « no previous file | tests_lit/llvm2ice_tests/elf_container.ll » ('j') | tests_lit/llvm2ice_tests/elf_container.ll » ('J')

Powered by Google App Engine
This is Rietveld 408576698