Skip to content

Commit d12e993

Browse files
committed
[KeyInstr] Add Atom Group waterline to LLVMContext
The waterline is managed through DILocation creation, LLVMContext::incNextAtomGroup, and LLVMContext::updateAtomGroupWaterline. Add unittest.
1 parent a124bd2 commit d12e993

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

llvm/include/llvm/IR/LLVMContext.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,14 @@ class LLVMContext {
335335
StringRef getDefaultTargetFeatures();
336336
void setDefaultTargetFeatures(StringRef Features);
337337

338+
/// Key Instructions: update the highest number atom group emitted for any
339+
/// function.
340+
void updateAtomGroupWaterline(uint64_t G);
341+
342+
/// Key Instructions: get the next free atom group number and increment
343+
/// the global tracker.
344+
uint64_t incNextAtomGroup();
345+
338346
private:
339347
// Module needs access to the add/removeModule methods.
340348
friend class Module;

llvm/lib/IR/DebugInfoMetadata.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
6767
#ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
6868
assert(AtomRank <= 7 && "AtomRank number should fit in 3 bits");
6969
#endif
70+
if (AtomRank)
71+
C.updateAtomGroupWaterline(AtomGroup + 1);
72+
7073
assert((MDs.size() == 1 || MDs.size() == 2) &&
7174
"Expected a scope and optional inlined-at");
7275
// Set line and column.

llvm/lib/IR/LLVMContext.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,11 @@ StringRef LLVMContext::getDefaultTargetFeatures() {
377377
void LLVMContext::setDefaultTargetFeatures(StringRef Features) {
378378
pImpl->DefaultTargetFeatures = Features;
379379
}
380+
381+
void LLVMContext::updateAtomGroupWaterline(uint64_t V) {
382+
pImpl->NextAtomGroup = std::max(pImpl->NextAtomGroup, V);
383+
}
384+
385+
uint64_t LLVMContext::incNextAtomGroup() {
386+
return pImpl->NextAtomGroup++;
387+
}

llvm/lib/IR/LLVMContextImpl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,16 @@ class LLVMContextImpl {
18321832

18331833
std::string DefaultTargetCPU;
18341834
std::string DefaultTargetFeatures;
1835+
1836+
/// The next available source atom group number. The front end is responsible
1837+
/// for assigning source atom numbers, but certain optimisations need to
1838+
/// assign new group numbers to a set of instructions. Most often code
1839+
/// duplication optimisations like loop unroll. Tracking a global maximum
1840+
/// value means we can know (cheaply) we're never using a group number that's
1841+
/// already used within this function.
1842+
///
1843+
/// Start a 1 because 0 means the source location isn't part of an atom group.
1844+
uint64_t NextAtomGroup = 1;
18351845
};
18361846

18371847
} // end namespace llvm

llvm/unittests/IR/MetadataTest.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "../lib/IR/LLVMContextImpl.h"
910
#include "llvm/IR/Metadata.h"
1011
#include "llvm/ADT/DenseMap.h"
1112
#include "llvm/ADT/STLExtras.h"
@@ -1366,6 +1367,43 @@ TEST_F(DILocationTest, discriminatorSpecialCases) {
13661367
EXPECT_EQ(std::nullopt, L4->cloneByMultiplyingDuplicationFactor(0x1000));
13671368
}
13681369

1370+
TEST_F(DILocationTest, KeyInstructions) {
1371+
Context.pImpl->NextAtomGroup = 1;
1372+
1373+
EXPECT_EQ(Context.pImpl->NextAtomGroup, 1u);
1374+
DILocation *A1 = DILocation::get(Context, 1, 0, getSubprogram(), nullptr, false, 1, 2);
1375+
// The group is only applied to the DILocation if the build has opted into
1376+
// the additional DILocation fields needed for the feature.
1377+
#ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
1378+
EXPECT_EQ(A1->getAtomGroup(), 1u);
1379+
EXPECT_EQ(A1->getAtomRank(), 2u);
1380+
#else
1381+
EXPECT_EQ(A1->getAtomGroup(), 0u);
1382+
EXPECT_EQ(A1->getAtomRank(), 0u);
1383+
#endif
1384+
1385+
// Group number 1 has been "used" so next available is 2.
1386+
EXPECT_EQ(Context.pImpl->NextAtomGroup, 2u);
1387+
1388+
// Set a group number higher than current + 1, then check the waterline.
1389+
DILocation::get(Context, 2, 0, getSubprogram(), nullptr, false, 5, 1);
1390+
EXPECT_EQ(Context.pImpl->NextAtomGroup, 6u);
1391+
1392+
// The waterline should be unchanged (group <= next).
1393+
DILocation::get(Context, 3, 0, getSubprogram(), nullptr, false, 4, 1);
1394+
EXPECT_EQ(Context.pImpl->NextAtomGroup, 6u);
1395+
DILocation::get(Context, 3, 0, getSubprogram(), nullptr, false, 5, 1);
1396+
EXPECT_EQ(Context.pImpl->NextAtomGroup, 6u);
1397+
1398+
// Check the waterline gets incremented by 1.
1399+
EXPECT_EQ(Context.incNextAtomGroup(), 6u);
1400+
EXPECT_EQ(Context.pImpl->NextAtomGroup, 7u);
1401+
1402+
Context.updateAtomGroupWaterline(8);
1403+
EXPECT_EQ(Context.pImpl->NextAtomGroup, 8u);
1404+
Context.updateAtomGroupWaterline(7);
1405+
EXPECT_EQ(Context.pImpl->NextAtomGroup, 8u);
1406+
}
13691407

13701408
typedef MetadataTest GenericDINodeTest;
13711409

0 commit comments

Comments
 (0)