[KeyInstr] Add Atom Group waterline to LLVMContext

The waterline is managed through DILocation creation,
LLVMContext::incNextAtomGroup, and LLVMContext::updateAtomGroupWaterline.

Add unittest.
This commit is contained in:
Orlando Cazalet-Hyams 2025-03-18 16:50:17 +00:00
parent a124bd2afc
commit d12e993d5e
5 changed files with 67 additions and 0 deletions

View File

@ -335,6 +335,14 @@ public:
StringRef getDefaultTargetFeatures();
void setDefaultTargetFeatures(StringRef Features);
/// Key Instructions: update the highest number atom group emitted for any
/// function.
void updateAtomGroupWaterline(uint64_t G);
/// Key Instructions: get the next free atom group number and increment
/// the global tracker.
uint64_t incNextAtomGroup();
private:
// Module needs access to the add/removeModule methods.
friend class Module;

View File

@ -67,6 +67,9 @@ DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
#ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
assert(AtomRank <= 7 && "AtomRank number should fit in 3 bits");
#endif
if (AtomRank)
C.updateAtomGroupWaterline(AtomGroup + 1);
assert((MDs.size() == 1 || MDs.size() == 2) &&
"Expected a scope and optional inlined-at");
// Set line and column.

View File

@ -377,3 +377,11 @@ StringRef LLVMContext::getDefaultTargetFeatures() {
void LLVMContext::setDefaultTargetFeatures(StringRef Features) {
pImpl->DefaultTargetFeatures = Features;
}
void LLVMContext::updateAtomGroupWaterline(uint64_t V) {
pImpl->NextAtomGroup = std::max(pImpl->NextAtomGroup, V);
}
uint64_t LLVMContext::incNextAtomGroup() {
return pImpl->NextAtomGroup++;
}

View File

@ -1832,6 +1832,16 @@ public:
std::string DefaultTargetCPU;
std::string DefaultTargetFeatures;
/// The next available source atom group number. The front end is responsible
/// for assigning source atom numbers, but certain optimisations need to
/// assign new group numbers to a set of instructions. Most often code
/// duplication optimisations like loop unroll. Tracking a global maximum
/// value means we can know (cheaply) we're never using a group number that's
/// already used within this function.
///
/// Start a 1 because 0 means the source location isn't part of an atom group.
uint64_t NextAtomGroup = 1;
};
} // end namespace llvm

View File

@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//
#include "../lib/IR/LLVMContextImpl.h"
#include "llvm/IR/Metadata.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
@ -1366,6 +1367,43 @@ TEST_F(DILocationTest, discriminatorSpecialCases) {
EXPECT_EQ(std::nullopt, L4->cloneByMultiplyingDuplicationFactor(0x1000));
}
TEST_F(DILocationTest, KeyInstructions) {
Context.pImpl->NextAtomGroup = 1;
EXPECT_EQ(Context.pImpl->NextAtomGroup, 1u);
DILocation *A1 = DILocation::get(Context, 1, 0, getSubprogram(), nullptr, false, 1, 2);
// The group is only applied to the DILocation if the build has opted into
// the additional DILocation fields needed for the feature.
#ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
EXPECT_EQ(A1->getAtomGroup(), 1u);
EXPECT_EQ(A1->getAtomRank(), 2u);
#else
EXPECT_EQ(A1->getAtomGroup(), 0u);
EXPECT_EQ(A1->getAtomRank(), 0u);
#endif
// Group number 1 has been "used" so next available is 2.
EXPECT_EQ(Context.pImpl->NextAtomGroup, 2u);
// Set a group number higher than current + 1, then check the waterline.
DILocation::get(Context, 2, 0, getSubprogram(), nullptr, false, 5, 1);
EXPECT_EQ(Context.pImpl->NextAtomGroup, 6u);
// The waterline should be unchanged (group <= next).
DILocation::get(Context, 3, 0, getSubprogram(), nullptr, false, 4, 1);
EXPECT_EQ(Context.pImpl->NextAtomGroup, 6u);
DILocation::get(Context, 3, 0, getSubprogram(), nullptr, false, 5, 1);
EXPECT_EQ(Context.pImpl->NextAtomGroup, 6u);
// Check the waterline gets incremented by 1.
EXPECT_EQ(Context.incNextAtomGroup(), 6u);
EXPECT_EQ(Context.pImpl->NextAtomGroup, 7u);
Context.updateAtomGroupWaterline(8);
EXPECT_EQ(Context.pImpl->NextAtomGroup, 8u);
Context.updateAtomGroupWaterline(7);
EXPECT_EQ(Context.pImpl->NextAtomGroup, 8u);
}
typedef MetadataTest GenericDINodeTest;