Commit 1eda90a1 by Andrew Scull

Order jump tables for deterministic or randomized emission.

BUG= R=stichnot@chromium.org, jvoung, stichnot Review URL: https://codereview.chromium.org/1260183008.
parent c5c8957b
......@@ -878,11 +878,32 @@ ConstantList GlobalContext::getConstantExternSyms() {
return getConstPool()->ExternRelocatables.getConstantPool();
}
JumpTableDataList GlobalContext::getJumpTables() {
JumpTableDataList JumpTables(*getJumpTableList());
if (getFlags().shouldReorderPooledConstants()) {
// If reorder-pooled-constants option is set to true, we need to shuffle the
// constant pool before emitting it.
RandomShuffle(JumpTables.begin(), JumpTables.end(), [this](uint64_t N) {
return (uint32_t)getRNG().next(N);
});
} else {
// Make order deterministic by sorting into functions and then ID of the
// jump table within that function.
std::sort(JumpTables.begin(), JumpTables.end(), [](const JumpTableData &A,
const JumpTableData &B) {
if (A.getFunctionName() != B.getFunctionName())
return A.getFunctionName() < B.getFunctionName();
return A.getId() < B.getId();
});
}
return JumpTables;
}
JumpTableData &GlobalContext::addJumpTable(IceString FuncName, SizeT Id,
SizeT NumTargets) {
auto JumpTables = getJumpTables();
JumpTables->emplace_back(FuncName, Id, NumTargets);
return JumpTables->back();
auto JumpTableList = getJumpTableList();
JumpTableList->emplace_back(FuncName, Id, NumTargets);
return JumpTableList->back();
}
TimerStackIdT GlobalContext::newTimerStackID(const IceString &Name) {
......
......@@ -212,9 +212,7 @@ public:
ConstantList getConstantExternSyms();
/// Return a locked pointer to the registered jump tables.
LockedPtr<JumpTableDataList> getJumpTables() {
return LockedPtr<JumpTableDataList>(&JumpTables, &JumpTablesLock);
}
JumpTableDataList getJumpTables();
/// Create a new jump table entry and return a reference to it.
JumpTableData &addJumpTable(IceString FuncName, SizeT Id, SizeT NumTargets);
......@@ -467,9 +465,9 @@ private:
std::unique_ptr<ConstantPool> ConstPool;
ICE_CACHELINE_BOUNDARY;
// Managed by getJumpTables()
// Managed by getJumpTableList()
GlobalLockType JumpTablesLock;
JumpTableDataList JumpTables;
JumpTableDataList JumpTableList;
ICE_CACHELINE_BOUNDARY;
// Managed by getErrorStatus()
......@@ -522,6 +520,9 @@ private:
LockedPtr<ConstantPool> getConstPool() {
return LockedPtr<ConstantPool>(ConstPool.get(), &ConstPoolLock);
}
LockedPtr<JumpTableDataList> getJumpTableList() {
return LockedPtr<JumpTableDataList>(&JumpTableList, &JumpTablesLock);
}
LockedPtr<CodeStats> getStatsCumulative() {
return LockedPtr<CodeStats>(&StatsCumulative, &StatsLock);
}
......
......@@ -79,7 +79,6 @@ private:
/// ELF section once the offsets from the start of the function are known.
class JumpTableData {
JumpTableData() = delete;
JumpTableData(const JumpTableData &) = delete;
JumpTableData &operator=(const JumpTableData &) = delete;
public:
......@@ -87,7 +86,9 @@ public:
: FuncName(FuncName), Id(Id) {
TargetOffsets.reserve(NumTargets);
}
JumpTableData(const JumpTableData &) = default;
JumpTableData(JumpTableData &&) = default;
JumpTableData &operator=(JumpTableData &&) = default;
void pushTarget(intptr_t Offset) { TargetOffsets.emplace_back(Offset); }
......@@ -98,8 +99,8 @@ public:
}
private:
const IceString FuncName;
const SizeT Id;
IceString FuncName;
SizeT Id;
std::vector<intptr_t> TargetOffsets;
};
......
......@@ -243,8 +243,8 @@ void TargetDataX8632::lowerJumpTables() {
switch (Ctx->getFlags().getOutFileType()) {
case FT_Elf: {
ELFObjectWriter *Writer = Ctx->getObjectWriter();
for (const JumpTableData &JumpTable : *Ctx->getJumpTables())
Writer->writeJumpTable(JumpTable, llvm::ELF::R_386_32);
for (const JumpTableData &JT : Ctx->getJumpTables())
Writer->writeJumpTable(JT, llvm::ELF::R_386_32);
} break;
case FT_Asm:
// Already emitted from Cfg
......@@ -253,7 +253,7 @@ void TargetDataX8632::lowerJumpTables() {
if (!BuildDefs::dump())
return;
Ostream &Str = Ctx->getStrEmit();
for (const JumpTableData &JT : *Ctx->getJumpTables()) {
for (const JumpTableData &JT : Ctx->getJumpTables()) {
Str << "\t.section\t.rodata." << JT.getFunctionName()
<< "$jumptable,\"a\",@progbits\n";
Str << "\t.align\t" << typeWidthInBytes(getPointerType()) << "\n";
......
......@@ -240,9 +240,9 @@ void TargetDataX8664::lowerJumpTables() {
switch (Ctx->getFlags().getOutFileType()) {
case FT_Elf: {
ELFObjectWriter *Writer = Ctx->getObjectWriter();
for (const JumpTableData &JumpTable : *Ctx->getJumpTables())
for (const JumpTableData &JT : Ctx->getJumpTables())
// TODO(jpp): not 386.
Writer->writeJumpTable(JumpTable, llvm::ELF::R_386_32);
Writer->writeJumpTable(JT, llvm::ELF::R_386_32);
} break;
case FT_Asm:
// Already emitted from Cfg
......@@ -251,7 +251,7 @@ void TargetDataX8664::lowerJumpTables() {
if (!BuildDefs::dump())
return;
Ostream &Str = Ctx->getStrEmit();
for (const JumpTableData &JT : *Ctx->getJumpTables()) {
for (const JumpTableData &JT : Ctx->getJumpTables()) {
Str << "\t.section\t.rodata." << JT.getFunctionName()
<< "$jumptable,\"a\",@progbits\n";
Str << "\t.align\t" << typeWidthInBytes(getPointerType()) << "\n";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment