llvm.org GIT mirror llvm / 001ab10
[llvm] API for encoding/decoding DWARF discriminators. Summary: Added a pair of APIs for encoding/decoding the 3 components of a DWARF discriminator described in http://lists.llvm.org/pipermail/llvm-dev/2016-October/106532.html: the base discriminator, the duplication factor (useful in profile-guided optimization) and the copy index (used to identify copies of code in cases like loop unrolling) The encoding packs 3 unsigned values in 32 bits. This CL addresses 2 issues: - communicates overflow back to the user - supports encoding all 3 components together. Current APIs assume a sequencing of events. For example, creating a new discriminator based on an existing one by changing the base discriminator was not supported. Reviewers: davidxl, danielcdh, wmi, dblaikie Reviewed By: dblaikie Subscribers: zzheng, dmgreen, aprantl, JDevlieghere, llvm-commits Differential Revision: https://reviews.llvm.org/D55681 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@349973 91177308-0d34-0410-b5e6-96231b3b80d8 Mircea Trofin 1 year, 10 months ago
8 changed file(s) with 287 addition(s) and 59 deletion(s). Raw diff Collapse all Expand all
14291429
14301430 /// Reverse transformation as getPrefixEncodingFromUnsigned.
14311431 static unsigned getUnsignedFromPrefixEncoding(unsigned U) {
1432 if (U & 1)
1433 return 0;
1434 U >>= 1;
14321435 return (U & 0x20) ? (((U >> 1) & 0xfe0) | (U & 0x1f)) : (U & 0x1f);
14331436 }
14341437
14451448 // a DILocation containing temporary metadata.
14461449 return getTemporary(getContext(), getLine(), getColumn(), getRawScope(),
14471450 getRawInlinedAt(), isImplicitCode());
1451 }
1452
1453 static unsigned encodeComponent(unsigned C) {
1454 return (C == 0) ? 1U : (getPrefixEncodingFromUnsigned(C) << 1);
1455 }
1456
1457 static unsigned encodingBits(unsigned C) {
1458 return (C == 0) ? 1 : (C > 0x1f ? 14 : 7);
14481459 }
14491460
14501461 public:
15171528 /// order. If the lowest bit is 1, the current component is empty, and the
15181529 /// next component will start in the next bit. Otherwise, the current
15191530 /// component is non-empty, and its content starts in the next bit. The
1520 /// length of each components is either 5 bit or 12 bit: if the 7th bit
1531 /// value of each components is either 5 bit or 12 bit: if the 7th bit
15211532 /// is 0, the bit 2~6 (5 bits) are used to represent the component; if the
15221533 /// 7th bit is 1, the bit 2~6 (5 bits) and 8~14 (7 bits) are combined to
1523 /// represent the component.
1534 /// represent the component. Thus, the number of bits used for a component
1535 /// is either 0 (if it and all the next components are empty); 1 - if it is
1536 /// empty; 7 - if its value is up to and including 0x1f (lsb and msb are both
1537 /// 0); or 14, if its value is up to and including 0x1ff. Note that the last
1538 /// component is also capped at 0x1ff, even in the case when both first
1539 /// components are 0, and we'd technically have 29 bits available.
1540 ///
1541 /// For precise control over the data being encoded in the discriminator,
1542 /// use encodeDiscriminator/decodeDiscriminator.
1543 ///
1544 /// Use {get|set}BaseDiscriminator and cloneWithDuplicationFactor after reading
1545 /// their documentation, as their behavior has side-effects.
15241546
15251547 inline unsigned getDiscriminator() const;
15261548
15271549 /// Returns a new DILocation with updated \p Discriminator.
15281550 inline const DILocation *cloneWithDiscriminator(unsigned Discriminator) const;
15291551
1530 /// Returns a new DILocation with updated base discriminator \p BD.
1531 inline const DILocation *setBaseDiscriminator(unsigned BD) const;
1532
1533 /// Returns the duplication factor stored in the discriminator.
1552 /// Returns a new DILocation with updated base discriminator \p BD. Only the
1553 /// base discriminator is set in the new DILocation, the other encoded values
1554 /// are elided.
1555 /// If the discriminator cannot be encoded, the function returns None.
1556 inline Optional setBaseDiscriminator(unsigned BD) const;
1557
1558 /// Returns the duplication factor stored in the discriminator, or 1 if no
1559 /// duplication factor (or 0) is encoded.
15341560 inline unsigned getDuplicationFactor() const;
15351561
15361562 /// Returns the copy identifier stored in the discriminator.
15391565 /// Returns the base discriminator stored in the discriminator.
15401566 inline unsigned getBaseDiscriminator() const;
15411567
1542 /// Returns a new DILocation with duplication factor \p DF encoded in the
1543 /// discriminator.
1544 inline const DILocation *cloneWithDuplicationFactor(unsigned DF) const;
1568 /// Returns a new DILocation with duplication factor \p DF * current
1569 /// duplication factor encoded in the discriminator. The current duplication
1570 /// factor is as defined by getDuplicationFactor().
1571 /// Returns None if encoding failed.
1572 inline Optional cloneWithDuplicationFactor(unsigned DF) const;
15451573
15461574 /// When two instructions are combined into a single instruction we also
15471575 /// need to combine the original locations into a single location.
15621590
15631591 /// Returns the base discriminator for a given encoded discriminator \p D.
15641592 static unsigned getBaseDiscriminatorFromDiscriminator(unsigned D) {
1565 if ((D & 1) == 0)
1566 return getUnsignedFromPrefixEncoding(D >> 1);
1567 else
1568 return 0;
1569 }
1570
1571 /// Returns the duplication factor for a given encoded discriminator \p D.
1593 return getUnsignedFromPrefixEncoding(D);
1594 }
1595
1596 /// Raw encoding of the discriminator. APIs such as setBaseDiscriminator or
1597 /// cloneWithDuplicationFactor have certain side-effects. This API, in
1598 /// conjunction with cloneWithDiscriminator, may be used to encode precisely
1599 /// the values provided. \p BD: base discriminator \p DF: duplication factor
1600 /// \p CI: copy index
1601 /// The return is None if the values cannot be encoded in 32 bits - for
1602 /// example, values for BD or DF larger than 12 bits. Otherwise, the return
1603 /// is the encoded value.
1604 static Optional encodeDiscriminator(unsigned BD, unsigned DF, unsigned CI);
1605
1606 /// Raw decoder for values in an encoded discriminator D.
1607 static void decodeDiscriminator(unsigned D, unsigned &BD, unsigned &DF,
1608 unsigned &CI);
1609
1610 /// Returns the duplication factor for a given encoded discriminator \p D, or
1611 /// 1 if no value or 0 is encoded.
15721612 static unsigned getDuplicationFactorFromDiscriminator(unsigned D) {
15731613 D = getNextComponentInDiscriminator(D);
1574 if (D == 0 || (D & 1))
1614 unsigned Ret = getUnsignedFromPrefixEncoding(D);
1615 if (Ret == 0)
15751616 return 1;
1576 else
1577 return getUnsignedFromPrefixEncoding(D >> 1);
1617 return Ret;
15781618 }
15791619
15801620 /// Returns the copy identifier for a given encoded discriminator \p D.
19982038 return getCopyIdentifierFromDiscriminator(getDiscriminator());
19992039 }
20002040
2001 const DILocation *DILocation::setBaseDiscriminator(unsigned D) const {
2041 Optional DILocation::setBaseDiscriminator(unsigned D) const {
20022042 if (D == 0)
20032043 return this;
2004 else
2005 return cloneWithDiscriminator(getPrefixEncodingFromUnsigned(D) << 1);
2044 if (D > 0xfff)
2045 return None;
2046 return cloneWithDiscriminator(encodeComponent(D));
20062047 }
20072048
2008 const DILocation *DILocation::cloneWithDuplicationFactor(unsigned DF) const {
2049 Optional DILocation::cloneWithDuplicationFactor(unsigned DF) const {
20092050 DF *= getDuplicationFactor();
20102051 if (DF <= 1)
20112052 return this;
20122053
20132054 unsigned BD = getBaseDiscriminator();
2014 unsigned CI = getCopyIdentifier() << (DF > 0x1f ? 14 : 7);
2015 unsigned D = CI | (getPrefixEncodingFromUnsigned(DF) << 1);
2016
2017 if (BD == 0)
2018 D = (D << 1) | 1;
2019 else
2020 D = (D << (BD > 0x1f ? 14 : 7)) | (getPrefixEncodingFromUnsigned(BD) << 1);
2021
2022 return cloneWithDiscriminator(D);
2055 unsigned CI = getCopyIdentifier();
2056 if (Optional D = encodeDiscriminator(BD, DF, CI))
2057 return cloneWithDiscriminator(*D);
2058 return None;
20232059 }
20242060
20252061 class DINamespace : public DIScope {
1919 #include "llvm/IR/Function.h"
2020 #include "llvm/IR/Instructions.h"
2121
22 #include
23
2224 using namespace llvm;
2325
2426 DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
111113 S = LocA->getScope();
112114 return DILocation::get(Result->getContext(), 0, 0, S, L);
113115 }
116
117 Optional DILocation::encodeDiscriminator(unsigned BD, unsigned DF, unsigned CI) {
118 SmallVector Components = {BD, DF, CI};
119 uint64_t RemainingWork = 0U;
120 // We use RemainingWork to figure out if we have no remaining components to
121 // encode. For example: if BD != 0 but DF == 0 && CI == 0, we don't need to
122 // encode anything for the latter 2.
123 // Since any of the input components is at most 32 bits, their sum will be
124 // less than 34 bits, and thus RemainingWork won't overflow.
125 RemainingWork = std::accumulate(Components.begin(), Components.end(), RemainingWork);
126
127 int I = 0;
128 unsigned Ret = 0;
129 unsigned NextBitInsertionIndex = 0;
130 while (RemainingWork > 0) {
131 unsigned C = Components[I++];
132 RemainingWork -= C;
133 unsigned EC = encodeComponent(C);
134 Ret |= (EC << NextBitInsertionIndex);
135 NextBitInsertionIndex += encodingBits(C);
136 }
137
138 // Encoding may be unsuccessful because of overflow. We determine success by
139 // checking equivalence of components before & after encoding. Alternatively,
140 // we could determine Success during encoding, but the current alternative is
141 // simpler.
142 unsigned TBD, TDF, TCI = 0;
143 decodeDiscriminator(Ret, TBD, TDF, TCI);
144 if (TBD == BD && TDF == DF && TCI == CI)
145 return Ret;
146 return None;
147 }
148
149 void DILocation::decodeDiscriminator(unsigned D, unsigned &BD, unsigned &DF,
150 unsigned &CI) {
151 BD = getUnsignedFromPrefixEncoding(D);
152 DF = getUnsignedFromPrefixEncoding(getNextComponentInDiscriminator(D));
153 CI = getUnsignedFromPrefixEncoding(
154 getNextComponentInDiscriminator(getNextComponentInDiscriminator(D)));
155 }
156
114157
115158 DINode::DIFlags DINode::getFlag(StringRef Flag) {
116159 return StringSwitch(Flag)
2020 #include "llvm/IR/DebugInfoMetadata.h"
2121 #include "llvm/ProfileData/SampleProf.h"
2222 #include "llvm/ProfileData/SampleProfReader.h"
23 #include "llvm/Support/Debug.h"
2324 #include "llvm/Transforms/IPO/SampleProfile.h"
2425 using namespace llvm;
2526
106107 if (!DI) {
107108 DI = ReferenceDI;
108109 }
109 DenseSet &Set = Seen[diToLocation(DI)];
110 Location L = diToLocation(DI);
111 DenseSet &Set = Seen[L];
110112 const std::pair::iterator, bool> TryInsert =
111113 Set.insert(DI->getBaseDiscriminator());
112114 if (!TryInsert.second) {
113 DI = DI->setBaseDiscriminator(++MemOpDiscriminators[diToLocation(DI)]);
115 unsigned BF, DF, CI = 0;
116 DILocation::decodeDiscriminator(DI->getDiscriminator(), BF, DF, CI);
117 Optional EncodedDiscriminator = DILocation::encodeDiscriminator(
118 MemOpDiscriminators[L] + 1, DF, CI);
119
120 if (!EncodedDiscriminator) {
121 // FIXME(mtrofin): The assumption is that this scenario is infrequent/OK
122 // not to support. If evidence points otherwise, we can explore synthesizeing
123 // unique DIs by adding fake line numbers, or by constructing 64 bit
124 // discriminators.
125 LLVM_DEBUG(dbgs() << "Unable to create a unique discriminator "
126 "for instruction with memory operand in: "
127 << DI->getFilename() << " Line: " << DI->getLine()
128 << " Column: " << DI->getColumn()
129 << ". This is likely due to a large macro expansion. \n");
130 continue;
131 }
132 // Since we were able to encode, bump the MemOpDiscriminators.
133 ++MemOpDiscriminators[L];
134 DI = DI->cloneWithDiscriminator(EncodedDiscriminator.getValue());
114135 updateDebugInfo(&MI, DI);
115136 Changed = true;
116 const std::pair::iterator, bool> MustInsert =
137 std::pair::iterator, bool> MustInsert =
117138 Set.insert(DI->getBaseDiscriminator());
118 // FIXME (mtrofin): check if the to-be inserted base discriminator can
119 // be added. This requires a new API on DILocation.
120 // The assumption is that this scenario is infrequent/OK not to support.
121 // If evidence points otherwise, we can explore synthesize unique DIs by
122 // adding fake line numbers.
123 if (!MustInsert.second) {
124 LLVM_DEBUG(dbgs()
125 << "Unable to create a unique discriminator in "
126 << DI->getFilename() << " Line: " << DI->getLine()
127 << " Column: " << DI->getColumn()
128 << ". This is likely due to a large macro expansion.\n");
129 }
139 assert(MustInsert.second && "New discriminator shouldn't be present in set");
130140 }
131141
132142 // Bump the reference DI to avoid cramming discriminators on line 0.
208208 // Only the lowest 7 bits are used to represent a discriminator to fit
209209 // it in 1 byte ULEB128 representation.
210210 unsigned Discriminator = R.second ? ++LDM[L] : LDM[L];
211 I.setDebugLoc(DIL->setBaseDiscriminator(Discriminator));
212 LLVM_DEBUG(dbgs() << DIL->getFilename() << ":" << DIL->getLine() << ":"
213 << DIL->getColumn() << ":" << Discriminator << " " << I
214 << "\n");
211 auto NewDIL = DIL->setBaseDiscriminator(Discriminator);
212 if (!NewDIL) {
213 LLVM_DEBUG(dbgs() << "Could not encode discriminator: "
214 << DIL->getFilename() << ":" << DIL->getLine() << ":"
215 << DIL->getColumn() << ":" << Discriminator << " "
216 << I << "\n");
217 } else {
218 I.setDebugLoc(NewDIL.getValue());
219 LLVM_DEBUG(dbgs() << DIL->getFilename() << ":" << DIL->getLine() << ":"
220 << DIL->getColumn() << ":" << Discriminator << " " << I
221 << "\n");
222 }
215223 Changed = true;
216224 }
217225 }
238246 std::make_pair(CurrentDIL->getFilename(), CurrentDIL->getLine());
239247 if (!CallLocations.insert(L).second) {
240248 unsigned Discriminator = ++LDM[L];
241 Current->setDebugLoc(CurrentDIL->setBaseDiscriminator(Discriminator));
242 Changed = true;
249 auto NewDIL = CurrentDIL->setBaseDiscriminator(Discriminator);
250 if (!NewDIL) {
251 LLVM_DEBUG(dbgs()
252 << "Could not encode discriminator: "
253 << CurrentDIL->getFilename() << ":"
254 << CurrentDIL->getLine() << ":" << CurrentDIL->getColumn()
255 << ":" << Discriminator << " " << I << "\n");
256 } else {
257 Current->setDebugLoc(NewDIL.getValue());
258 Changed = true;
259 }
243260 }
244261 }
245262 }
597597 for (BasicBlock *BB : L->getBlocks())
598598 for (Instruction &I : *BB)
599599 if (!isa(&I))
600 if (const DILocation *DIL = I.getDebugLoc())
601 I.setDebugLoc(DIL->cloneWithDuplicationFactor(Count));
600 if (const DILocation *DIL = I.getDebugLoc()) {
601 auto NewDIL = DIL->cloneWithDuplicationFactor(Count);
602 if (NewDIL)
603 I.setDebugLoc(NewDIL.getValue());
604 else
605 LLVM_DEBUG(dbgs()
606 << "Failed to create new discriminator: "
607 << DIL->getFilename() << " Line: " << DIL->getLine());
608 }
602609
603610 for (unsigned It = 1; It != Count; ++It) {
604611 std::vector NewBlocks;
299299 for (BasicBlock *BB : L->getBlocks())
300300 for (Instruction &I : *BB)
301301 if (!isa(&I))
302 if (const DILocation *DIL = I.getDebugLoc())
303 I.setDebugLoc(DIL->cloneWithDuplicationFactor(Count));
302 if (const DILocation *DIL = I.getDebugLoc()) {
303 auto NewDIL = DIL->cloneWithDuplicationFactor(Count);
304 if (NewDIL)
305 I.setDebugLoc(NewDIL.getValue());
306 else
307 LLVM_DEBUG(dbgs()
308 << "Failed to create new discriminator: "
309 << DIL->getFilename() << " Line: " << DIL->getLine());
310 }
304311
305312 // Copy all blocks
306313 for (unsigned It = 1; It != Count; ++It) {
758758 if (const Instruction *Inst = dyn_cast_or_null(Ptr)) {
759759 const DILocation *DIL = Inst->getDebugLoc();
760760 if (DIL && Inst->getFunction()->isDebugInfoForProfiling() &&
761 !isa(Inst))
762 B.SetCurrentDebugLocation(DIL->cloneWithDuplicationFactor(UF * VF));
761 !isa(Inst)) {
762 auto NewDIL = DIL->cloneWithDuplicationFactor(UF * VF);
763 if (NewDIL)
764 B.SetCurrentDebugLocation(NewDIL.getValue());
765 else
766 LLVM_DEBUG(dbgs()
767 << "Failed to create new discriminator: "
768 << DIL->getFilename() << " Line: " << DIL->getLine());
769 }
763770 else
764771 B.SetCurrentDebugLocation(DIL);
765772 } else
979979 auto L2 = L->clone();
980980 EXPECT_TRUE(L2->isTemporary());
981981 }
982
983 TEST_F(DILocationTest, discriminatorEncoding) {
984 EXPECT_EQ(0U, DILocation::encodeDiscriminator(0, 0, 0).getValue());
985
986 // Encode base discriminator as a component: lsb is 0, then the value.
987 // The other components are all absent, so we leave all the other bits 0.
988 EXPECT_EQ(2U, DILocation::encodeDiscriminator(1, 0, 0).getValue());
989
990 // Base discriminator component is empty, so lsb is 1. Next component is not
991 // empty, so its lsb is 0, then its value (1). Next component is empty.
992 // So the bit pattern is 101.
993 EXPECT_EQ(5U, DILocation::encodeDiscriminator(0, 1, 0).getValue());
994
995 // First 2 components are empty, so the bit pattern is 11. Then the
996 // next component - ending up with 1011.
997 EXPECT_EQ(0xbU, DILocation::encodeDiscriminator(0, 0, 1).getValue());
998
999 // The bit pattern for the first 2 components is 11. The next bit is 0,
1000 // because the last component is not empty. We have 29 bits usable for
1001 // encoding, but we cap it at 12 bits uniformously for all components. We
1002 // encode the last component over 14 bits.
1003 EXPECT_EQ(0xfffbU, DILocation::encodeDiscriminator(0, 0, 0xfff).getValue());
1004
1005 EXPECT_EQ(0x102U, DILocation::encodeDiscriminator(1, 1, 0).getValue());
1006
1007 EXPECT_EQ(0x13eU, DILocation::encodeDiscriminator(0x1f, 1, 0).getValue());
1008
1009 EXPECT_EQ(0x87feU, DILocation::encodeDiscriminator(0x1ff, 1, 0).getValue());
1010
1011 EXPECT_EQ(0x1f3eU, DILocation::encodeDiscriminator(0x1f, 0x1f, 0).getValue());
1012
1013 EXPECT_EQ(0x3ff3eU,
1014 DILocation::encodeDiscriminator(0x1f, 0x1ff, 0).getValue());
1015
1016 EXPECT_EQ(0x1ff87feU,
1017 DILocation::encodeDiscriminator(0x1ff, 0x1ff, 0).getValue());
1018
1019 EXPECT_EQ(0xfff9f3eU,
1020 DILocation::encodeDiscriminator(0x1f, 0x1f, 0xfff).getValue());
1021
1022 EXPECT_EQ(0xffc3ff3eU,
1023 DILocation::encodeDiscriminator(0x1f, 0x1ff, 0x1ff).getValue());
1024
1025 EXPECT_EQ(0xffcf87feU,
1026 DILocation::encodeDiscriminator(0x1ff, 0x1f, 0x1ff).getValue());
1027
1028 EXPECT_EQ(0xe1ff87feU,
1029 DILocation::encodeDiscriminator(0x1ff, 0x1ff, 7).getValue());
1030 }
1031
1032 TEST_F(DILocationTest, discriminatorEncodingNegativeTests) {
1033 EXPECT_EQ(None, DILocation::encodeDiscriminator(0, 0, 0x1000));
1034 EXPECT_EQ(None, DILocation::encodeDiscriminator(0x1000, 0, 0));
1035 EXPECT_EQ(None, DILocation::encodeDiscriminator(0, 0x1000, 0));
1036 EXPECT_EQ(None, DILocation::encodeDiscriminator(0, 0, 0x1000));
1037 EXPECT_EQ(None, DILocation::encodeDiscriminator(0x1ff, 0x1ff, 8));
1038 EXPECT_EQ(None,
1039 DILocation::encodeDiscriminator(std::numeric_limits::max(),
1040 std::numeric_limits::max(),
1041 0));
1042 }
1043
1044 TEST_F(DILocationTest, discriminatorSpecialCases) {
1045 // We don't test getCopyIdentifier here because the only way
1046 // to set it is by constructing an encoded discriminator using
1047 // encodeDiscriminator, which is already tested.
1048 auto L1 = DILocation::get(Context, 1, 2, getSubprogram());
1049 EXPECT_EQ(0U, L1->getBaseDiscriminator());
1050 EXPECT_EQ(1U, L1->getDuplicationFactor());
1051
1052 auto L2 = L1->setBaseDiscriminator(1).getValue();
1053 EXPECT_EQ(0U, L1->getBaseDiscriminator());
1054 EXPECT_EQ(1U, L1->getDuplicationFactor());
1055
1056 EXPECT_EQ(1U, L2->getBaseDiscriminator());
1057 EXPECT_EQ(1U, L2->getDuplicationFactor());
1058
1059 auto L3 = L2->cloneWithDuplicationFactor(2).getValue();
1060 EXPECT_EQ(1U, L3->getBaseDiscriminator());
1061 EXPECT_EQ(2U, L3->getDuplicationFactor());
1062
1063 auto L4 = L3->cloneWithDuplicationFactor(4).getValue();
1064 EXPECT_EQ(1U, L4->getBaseDiscriminator());
1065 EXPECT_EQ(8U, L4->getDuplicationFactor());
1066
1067 auto L5 = L4->setBaseDiscriminator(2).getValue();
1068 EXPECT_EQ(2U, L5->getBaseDiscriminator());
1069 EXPECT_EQ(1U, L5->getDuplicationFactor());
1070
1071 // Check extreme cases
1072 auto L6 = L1->setBaseDiscriminator(0xfff).getValue();
1073 EXPECT_EQ(0xfffU, L6->getBaseDiscriminator());
1074 EXPECT_EQ(
1075 0xfffU,
1076 L6->cloneWithDuplicationFactor(0xfff).getValue()->getDuplicationFactor());
1077
1078 // Check we return None for unencodable cases.
1079 EXPECT_EQ(None, L4->setBaseDiscriminator(0x1000));
1080 EXPECT_EQ(None, L4->cloneWithDuplicationFactor(0x1000));
1081 }
1082
9821083
9831084 typedef MetadataTest GenericDINodeTest;
9841085