Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions llvm/include/llvm/Bitcode/BitcodeWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,9 @@ LLVM_ABI void WriteBitcodeToFile(const Module &M, raw_ostream &Out,
bool GenerateHash = false,
ModuleHash *ModHash = nullptr);

/// Serialize a closed optimizer input with complete use-list ordering. Decline
/// inputs whose machine key needs side records for users outside ordinary
/// bitcode. Those records describe emission and do not establish optimizer
/// equivalence.
/// Serialize an optimizer input with complete use-list ordering. An unused
/// literal slice constant may be represented by a private leaf record. Other
/// users outside ordinary bitcode require normal optimization.
LLVM_ABI bool writeClosedOptimizerInput(const Module &M,
SmallVectorImpl<char> &Buffer);

Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5740,15 +5740,16 @@ bool llvm::writeDebugObjectBitcode(const Module &M,
Map->prepare();
BitcodeWriter Writer(Buffer);
MachineUseState Uses;
Uses.OptimizerInput = !AllowMachineOnlyUses;
Writer.writeModuleImpl(M, true, nullptr, false, nullptr, Map, &Uses);
Writer.writeSymtab();
Writer.writeStrtab();
if (!Uses.Complete || (Map && !Map->Valid))
return false;
if (!Uses.Leaves.empty() || !Uses.MetadataUses.empty()) {
if (!AllowMachineOnlyUses)
if (Uses.OptimizerInput && !Uses.MetadataUses.empty())
return false;
// This private top-level block is part of a checked machine key only.
// Only the private cache signature observes this top-level block.
// Ordinary bitcode readers skip unknown blocks.
constexpr unsigned CheckedMachineUsesBlock = 0x4f484d;
Writer.Stream->EnterSubblock(CheckedMachineUsesBlock, 3);
Expand Down
35 changes: 32 additions & 3 deletions llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,42 @@ static void predictValueUseListOrder(const Value *V, const Function *F,
}
}

// rustc can leave an unused slice constant while constructing a module. Its
// only operand with a use list is a numbered address. The other operand is a
// canonical scalar with no metadata observers. The leaf record below captures
// the entire constant and its position in the address's use list.
static bool isOptimizerSliceLeaf(const User &U, const OrderMap &OM) {
const auto *S = dyn_cast<ConstantStruct>(&U);
if (!S || S->getNumOperands() != 2 || !S->getType()->isLiteral() ||
S->getType()->isPacked())
return false;
const auto *Pointer = S->getOperand(0);
const auto *Length = dyn_cast<PoisonValue>(S->getOperand(1));
if (!Pointer->getType()->isPointerTy() ||
Pointer->getType()->getPointerAddressSpace() != 0 ||
!OM.lookup(Pointer).first || !Length ||
!Length->getType()->isIntegerTy(64) || Length->hasUseList() ||
Length->isUsedByMetadata())
return false;
if (const auto *Storage = dyn_cast<GlobalVariable>(Pointer))
return Storage->isConstant() && Storage->hasLocalLinkage();
// Empty slices also use a literal alignment as their address. The complete
// cast and its integer operand already appear in ordinary module bitcode.
const auto *Address = dyn_cast<ConstantExpr>(Pointer);
return Address && Address->getOpcode() == Instruction::IntToPtr &&
isa<ConstantInt>(Address->getOperand(0)) &&
Address->getOperand(0)->getType()->isIntegerTy(64);
}

// Most values only have users described by the normal use-list records. For
// an unreferenced literal aggregate, record its complete operand tuple, then
// record its exact position among the ordinary users of every affected value.
// Number leaves on first encounter in (value ID, actual use-list) order.
static bool encodeMachineLeaves(SmallVectorImpl<const Value *> &Affected,
const OrderMap &OM,
SmallVectorImpl<uint64_t> &Words) {
const OrderMap &OM, MachineUseState &State) {
if (Affected.empty())
return true;
auto &Words = State.Leaves;
llvm::sort(Affected, [&](const Value *L, const Value *R) {
return OM.lookup(L).first < OM.lookup(R).first;
});
Expand Down Expand Up @@ -343,6 +370,8 @@ static bool encodeMachineLeaves(SmallVectorImpl<const Value *> &Affected,
}
if (U->getNumOperands() > 256 || !U->use_empty() || U->isUsedByMetadata())
return false;
if (State.OptimizerInput && !isOptimizerSliceLeaf(*U, OM))
return false;
if (const auto *Struct = dyn_cast<ConstantStruct>(U)) {
if (!Struct->getType()->isLiteral())
return false;
Expand Down Expand Up @@ -490,7 +519,7 @@ static bool hasCompleteMachineUses(const Module &M, const OrderMap &OM,
}
}
llvm::sort(State.MetadataUses);
return encodeMachineLeaves(Affected, OM, State.Leaves);
return encodeMachineLeaves(Affected, OM, State);
}

static UseListOrderStack predictUseListOrder(const Module &M,
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Bitcode/Writer/ValueEnumerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ValueSymbolTable;

struct MachineUseState {
bool Complete = false;
bool OptimizerInput = false;
SmallVector<uint64_t, 0> Leaves;
SmallVector<SmallVector<uint64_t, 8>, 0> MetadataUses;
};
Expand Down
25 changes: 25 additions & 0 deletions llvm/lib/IR/LLVMContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "llvm/IR/LLVMContext.h"
#include "LLVMContextImpl.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
Expand Down Expand Up @@ -483,6 +484,30 @@ bool llvm::ohm::getOptimizerContextState(Module &M,
MetadataRoot(MD->getMetadata());
}
}
// An optimizer fold can retrieve a previously uniqued constant and inspect
// its uses. The writer sees users of represented operands, but cannot see
// a whole expression or aggregate that is detached from the module. Such a
// constant is inert only when it has no observers and none of its operands
// keeps a use list.
auto AccountedFor = [&](User *U) {
if (SeenValues.contains(U))
return true;
bool HasOperandUse = false;
for (Value *V : U->operand_values()) {
if (!V->hasUseList())
continue;
if (SeenValues.contains(V))
return true;
HasOperandUse = true;
}
return !HasOperandUse && U->use_empty() && !U->isUsedByMetadata();
};
if (!llvm::all_of(I.ExprConstants, AccountedFor) ||
!llvm::all_of(I.StructConstants, AccountedFor) ||
!llvm::all_of(I.ArrayConstants, AccountedFor) ||
!llvm::all_of(I.VectorConstants, AccountedFor) ||
!llvm::all_of(I.ConstantPtrAuths, AccountedFor))
return false;
for (Value *V : Values)
if (V->isUsedByMetadata() &&
!SeenMetadata.contains(ValueAsMetadata::getIfExists(V)))
Expand Down
140 changes: 138 additions & 2 deletions llvm/unittests/Bitcode/OhmDebugObjectSignatureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "llvm/Bitcode/BitcodeWriter.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/OhmOptimizerState.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/MemoryBufferRef.h"
Expand Down Expand Up @@ -145,9 +146,144 @@ static std::string machineKey(Module &M) {

static bool canSaveMachineInput(Module &M) { return !machineKey(M).empty(); }

static bool canSaveOptimizerInput(Module &M) {
static std::string optimizerKey(Module &M) {
SmallVector<char, 0> Bytes;
return writeClosedOptimizerInput(M, Bytes);
if (!writeClosedOptimizerInput(M, Bytes))
return {};
return std::string(Bytes.begin(), Bytes.end());
}

static bool canSaveOptimizerInput(Module &M) {
return !optimizerKey(M).empty();
}

TEST(DebugObjectUsesTest, OptimizerSliceLeafRecordsStorageAndUseOrder) {
LLVMContext C;
SMDiagnostic Error;
auto M = parseAssemblyString(R"(
target triple = "x86_64-unknown-linux-gnu"
@a = private constant i8 1
@b = private constant i8 2
@keep = constant ptr @a
)",
Error, C);
ASSERT_TRUE(M);
auto *A = M->getNamedGlobal("a");
auto *B = M->getNamedGlobal("b");
auto *Length = PoisonValue::get(Type::getInt64Ty(C));
std::string Original = optimizerKey(*M);
ASSERT_FALSE(Original.empty());
auto *Slice = ConstantStruct::getAnon({A, Length});
std::string WithSlice = optimizerKey(*M);
ASSERT_FALSE(WithSlice.empty());
EXPECT_NE(Original, WithSlice);
A->reverseUseList();
std::string Reordered = optimizerKey(*M);
EXPECT_FALSE(Reordered.empty());
EXPECT_NE(WithSlice, Reordered);
A->reverseUseList();
EXPECT_EQ(WithSlice, optimizerKey(*M));
Slice->destroyConstant();
auto *Other = ConstantStruct::getAnon({B, Length});
std::string OtherKey = optimizerKey(*M);
EXPECT_FALSE(OtherKey.empty());
EXPECT_NE(WithSlice, OtherKey);
Other->destroyConstant();
EXPECT_EQ(Original, optimizerKey(*M));
}

TEST(DebugObjectUsesTest, OptimizerSliceLeafRecordsNumberedIntegerAddress) {
LLVMContext C;
SMDiagnostic Error;
auto M = parseAssemblyString(R"(
target triple = "x86_64-unknown-linux-gnu"
@one = constant ptr inttoptr (i64 1 to ptr)
@eight = constant ptr inttoptr (i64 8 to ptr)
)",
Error, C);
ASSERT_TRUE(M);
auto *One = M->getNamedGlobal("one")->getInitializer();
auto *Eight = M->getNamedGlobal("eight")->getInitializer();
auto *Length = PoisonValue::get(Type::getInt64Ty(C));
const std::string Original = optimizerKey(*M);
ASSERT_FALSE(Original.empty());
auto *First = ConstantStruct::getAnon({One, Length});
const std::string OneKey = optimizerKey(*M);
ASSERT_FALSE(OneKey.empty());
EXPECT_NE(OneKey, Original);
First->destroyConstant();
auto *Second = ConstantStruct::getAnon({Eight, Length});
const std::string EightKey = optimizerKey(*M);
ASSERT_FALSE(EightKey.empty());
EXPECT_NE(EightKey, OneKey);
Eight->reverseUseList();
const std::string Reordered = optimizerKey(*M);
EXPECT_FALSE(Reordered.empty());
EXPECT_NE(Reordered, EightKey);
Second->destroyConstant();
EXPECT_EQ(Original, optimizerKey(*M));
}

TEST(DebugObjectUsesTest, OptimizerDeclinesDetachedIntegerAddressObserver) {
LLVMContext C;
Module M("context", C);
auto ContextIsAccountedFor = [&] {
SmallVector<char, 0> State;
return ohm::getOptimizerContextState(M, State);
};
auto *Ptr = PointerType::getUnqual(C);
auto *I64 = Type::getInt64Ty(C);
auto *Length = PoisonValue::get(I64);
auto *Null = ConstantPointerNull::get(Ptr);
auto *Inert = ConstantStruct::getAnon({Null, Length});
ASSERT_FALSE(Null->hasUseList());
ASSERT_FALSE(Length->hasUseList());
ASSERT_TRUE(ContextIsAccountedFor());
const std::string Original = optimizerKey(M);
ASSERT_FALSE(Original.empty());
auto Address = [&] {
return ConstantExpr::getIntToPtr(ConstantInt::get(I64, 8), Ptr);
};
auto *Detached = ConstantStruct::getAnon({Address(), Length});
EXPECT_EQ(Original, optimizerKey(M));
EXPECT_TRUE(Address()->hasOneUse());
EXPECT_FALSE(ContextIsAccountedFor());
Detached->destroyConstant();
EXPECT_TRUE(ContextIsAccountedFor());
Inert->destroyConstant();
}

TEST(DebugObjectUsesTest, OptimizerSliceLeafDeclinesOtherObserversAndForms) {
LLVMContext C;
SMDiagnostic Error;
auto M = parseAssemblyString(R"(
target triple = "x86_64-unknown-linux-gnu"
@data = private constant i8 0
@writable = private global i8 0
@ext = external constant i8
)",
Error, C);
ASSERT_TRUE(M);
auto *Storage = M->getNamedGlobal("data");
auto *I64 = Type::getInt64Ty(C);
auto *Length = PoisonValue::get(I64);
auto Declined = [&](Constant *Leaf) {
EXPECT_FALSE(canSaveOptimizerInput(*M));
Leaf->destroyConstant();
};
Declined(ConstantStruct::getAnon({M->getNamedGlobal("writable"), Length}));
Declined(ConstantStruct::getAnon({M->getNamedGlobal("ext"), Length}));
Declined(ConstantStruct::getAnon({Storage, Length}, /*Packed=*/true));
Declined(ConstantStruct::getAnon({Storage, UndefValue::get(I64)}));
Declined(ConstantStruct::getAnon({Storage, ConstantInt::get(I64, 4)}));
auto *Slice = ConstantStruct::getAnon({Storage, Length});
ASSERT_TRUE(canSaveOptimizerInput(*M));
auto *Observer = ConstantStruct::getAnon({Slice});
EXPECT_FALSE(canSaveOptimizerInput(*M));
Observer->destroyConstant();
ASSERT_TRUE(canSaveOptimizerInput(*M));
(void)ConstantAsMetadata::get(Length);
EXPECT_FALSE(canSaveOptimizerInput(*M));
}

TEST(DebugObjectUsesTest, AuthenticatesAnUnreferencedInitializerUser) {
Expand Down