Skip to content

Commit dbf2be4

Browse files
committed
Give operation costs a proper type instead of using unsigned. NFC.
1 parent 2077aba commit dbf2be4

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed

lib/IRGen/IRGen.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,27 @@ class ConstantInit {
268268
}
269269
};
270270

271+
/// An abstraction for computing the cost of an operation.
272+
enum class OperationCost : unsigned {
273+
Free = 0,
274+
Arithmetic = 1,
275+
Load = 3, // TODO: split into static- and dynamic-offset cases?
276+
Call = 10
277+
};
278+
inline OperationCost operator+(OperationCost l, OperationCost r) {
279+
return OperationCost(unsigned(l) + unsigned(r));
280+
}
281+
inline OperationCost &operator+=(OperationCost &l, OperationCost r) {
282+
l = l + r;
283+
return l;
284+
}
285+
inline bool operator<(OperationCost l, OperationCost r) {
286+
return unsigned(l) < unsigned(r);
287+
}
288+
inline bool operator<=(OperationCost l, OperationCost r) {
289+
return unsigned(l) <= unsigned(r);
290+
}
291+
271292
/// An alignment value, in eight-bit units.
272293
class Alignment {
273294
public:

lib/IRGen/LocalTypeData.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void IRGenFunction::destroyLocalTypeData() {
5454
delete LocalTypeData;
5555
}
5656

57-
unsigned LocalTypeDataCache::CacheEntry::cost() const {
57+
OperationCost LocalTypeDataCache::CacheEntry::cost() const {
5858
switch (getKind()) {
5959
case Kind::Concrete:
6060
return static_cast<const ConcreteCacheEntry*>(this)->cost();
@@ -99,7 +99,7 @@ llvm::Value *LocalTypeDataCache::tryGet(IRGenFunction &IGF, Key key,
9999
auto &chain = it->second;
100100

101101
CacheEntry *best = nullptr;
102-
Optional<unsigned> bestCost;
102+
Optional<OperationCost> bestCost;
103103

104104
CacheEntry *next = chain.Root;
105105
while (next) {
@@ -120,7 +120,7 @@ llvm::Value *LocalTypeDataCache::tryGet(IRGenFunction &IGF, Key key,
120120
// If that's zero, go ahead and short-circuit out.
121121
if (!bestCost) {
122122
bestCost = best->cost();
123-
if (*bestCost == 0) break;
123+
if (*bestCost == OperationCost::Free) break;
124124
}
125125

126126
auto curCost = cur->cost();
@@ -317,8 +317,8 @@ addAbstractForFulfillments(IRGenFunction &IGF, FulfillmentMap &&fulfillments,
317317

318318
// Check whether there's already an entry that's at least as good as the
319319
// fulfillment.
320-
Optional<unsigned> fulfillmentCost;
321-
auto getFulfillmentCost = [&]() -> unsigned {
320+
Optional<OperationCost> fulfillmentCost;
321+
auto getFulfillmentCost = [&]() -> OperationCost {
322322
if (!fulfillmentCost)
323323
fulfillmentCost = fulfillment.second.Path.cost();
324324
return *fulfillmentCost;
@@ -335,7 +335,7 @@ addAbstractForFulfillments(IRGenFunction &IGF, FulfillmentMap &&fulfillments,
335335

336336
// Ensure that the entry isn't better than the fulfillment.
337337
auto curCost = cur->cost();
338-
if (curCost == 0 || curCost <= getFulfillmentCost()) {
338+
if (curCost == OperationCost::Free || curCost <= getFulfillmentCost()) {
339339
foundBetter = true;
340340
break;
341341
}

lib/IRGen/LocalTypeData.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class LocalTypeDataCache {
7979
void setNext(CacheEntry *next) { NextAndFlags.setPointer(next); }
8080

8181
/// Return the abstract cost of evaluating this cache entry.
82-
unsigned cost() const;
82+
OperationCost cost() const;
8383

8484
/// Destruct and deallocate this cache entry.
8585
void erase() const;
@@ -105,7 +105,7 @@ class LocalTypeDataCache {
105105
llvm::Value *value)
106106
: CacheEntry(Kind::Concrete, point, isConditional), Value(value) {}
107107

108-
unsigned cost() const { return 0; }
108+
OperationCost cost() const { return OperationCost::Free; }
109109
};
110110

111111
/// A source of concrete data from which abstract cache entries can be
@@ -159,7 +159,7 @@ class LocalTypeDataCache {
159159

160160
llvm::Value *follow(IRGenFunction &IGF, AbstractSource &source) const;
161161

162-
unsigned cost() const { return Path.cost(); }
162+
OperationCost cost() const { return Path.cost(); }
163163
};
164164

165165
/// The linked list of cache entries corresponding to a particular key.

lib/IRGen/MetadataPath.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "swift/Basic/EncodedSequence.h"
2222
#include "swift/Reflection/MetadataSource.h"
23+
#include "IRGen.h"
2324

2425
namespace llvm {
2526
class Value;
@@ -34,6 +35,7 @@ namespace irgen {
3435
class IRGenFunction;
3536
class LocalTypeDataKey;
3637

38+
3739
/// A path from one source metadata --- either Swift type metadata or a Swift
3840
/// protocol conformance --- to another.
3941
class MetadataPath {
@@ -91,12 +93,18 @@ class MetadataPath {
9193
}
9294

9395
/// Return an abstract measurement of the cost of this component.
94-
unsigned cost() const {
95-
// Right now, all components cost the same: they take one load.
96-
// In the future, maybe some components will be cheaper (no loads,
97-
// like loading from a superclass's metadata) or more expensive
98-
// (multiple loads, or even a call).
99-
return 1;
96+
OperationCost cost() const {
97+
switch (getKind()) {
98+
case Kind::InheritedProtocol:
99+
case Kind::NominalTypeArgumentConformance:
100+
case Kind::NominalTypeArgument:
101+
case Kind::NominalParent:
102+
return OperationCost::Load;
103+
104+
case Kind::Impossible:
105+
llvm_unreachable("cannot compute cost of an imposible path");
106+
}
107+
llvm_unreachable("bad path component");
100108
}
101109

102110
static Component decode(const EncodedSequenceBase::Chunk *&ptr) {
@@ -156,8 +164,8 @@ class MetadataPath {
156164
}
157165

158166
/// Return an abstract measurement of the cost of this path.
159-
unsigned cost() const {
160-
unsigned cost = 0;
167+
OperationCost cost() const {
168+
auto cost = OperationCost::Free;
161169
for (const Component &component : Path)
162170
cost += component.cost();
163171
return cost;

0 commit comments

Comments
 (0)