Skip to content

Commit 41d1a16

Browse files
bors[bot]vext01ptersilie
authored
8: Upgrade to llvm13 r=ltratt a=vext01 Co-authored-by: Edd Barrett <[email protected]> Co-authored-by: Lukas Diekmann <[email protected]>
2 parents d7b669b + ccb9e21 commit 41d1a16

File tree

13 files changed

+557
-0
lines changed

13 files changed

+557
-0
lines changed

.buildbot.sh

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
INST_DIR=`pwd`/inst
6+
7+
mkdir -p build
8+
cd build
9+
cmake -DCMAKE_INSTALL_PREFIX=${INST_DIR} \
10+
-DLLVM_INSTALL_UTILS=On \
11+
-DCMAKE_BUILD_TYPE=release \
12+
-DLLVM_ENABLE_ASSERTIONS=On \
13+
-DLLVM_ENABLE_PROJECTS="lld;clang" \
14+
../llvm
15+
make -j `nproc` install
16+
17+
# There are many test suites for LLVM:
18+
# https://llvm.org/docs/TestingGuide.html
19+
#
20+
# This runs unit and integration tests.
21+
make -j `nproc` check-all
22+
cd ..
23+
24+
# clang-format any new files that we've introduced ourselves.
25+
PATH=${INST_DIR}/bin:${PATH}
26+
sh yk_format_new_files.sh
27+
git diff --exit-code
28+
29+
# FIXME The commented code below should run the `test-suite` tests, as
30+
# described at https://llvm.org/docs/TestSuiteGuide.html.
31+
#
32+
# However, the suite fails (even on a stock LLVM) with this error:
33+
# retref-bench.cc:17:10: fatal error: 'xray/xray_interface.h' file not found
34+
# #include "xray/xray_interface.h"
35+
# ^~~~~~~~~~~~~~~~~~~~~~~
36+
#
37+
#git clone --depth 1 --branch llvmorg-12.0.0 https://github.com/llvm/llvm-test-suite.git test-suite
38+
#mkdir -p test-suite-build
39+
#cd test-suite-build
40+
#cmake -DCMAKE_C_COMPILER=${INST_DIR}/bin/clang \
41+
# -C../test-suite/cmake/caches/O3.cmake \
42+
# ../test-suite
43+
#make -j `nproc`
44+
#llvm-lit -v -j 1 -o results.json .

bors.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# The service providing the commit statuses to GitHub.
2+
status = ["buildbot/yk-buildbot-build-script-hw"]
3+
4+
# Allow four hours for builds + tests.
5+
timeout_sec = 14400
6+
7+
# Have bors delete auto-merged branches
8+
delete_merged_branches = true
9+
10+
cut_body_after = ""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef LLVM_TRANSFORMS_YK_CONTROLPOINT_H
2+
#define LLVM_TRANSFORMS_YK_CONTROLPOINT_H
3+
4+
#include "llvm/IR/PassManager.h"
5+
6+
// The name of the "dummy function" that the user puts in their interpreter
7+
// implementation and that we will replace with our own code.
8+
#define YK_DUMMY_CONTROL_POINT "yk_control_point"
9+
10+
// The name of the new control point replacing the user's dummy control point.
11+
#define YK_NEW_CONTROL_POINT "yk_new_control_point"
12+
13+
namespace llvm {
14+
class YkControlPointPass : public PassInfoMixin<YkControlPointPass> {
15+
public:
16+
explicit YkControlPointPass();
17+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
18+
};
19+
} // namespace llvm
20+
21+
#endif

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

+84
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "llvm/BinaryFormat/COFF.h"
3737
#include "llvm/BinaryFormat/Dwarf.h"
3838
#include "llvm/BinaryFormat/ELF.h"
39+
#include "llvm/Bitcode/BitcodeWriter.h"
3940
#include "llvm/CodeGen/GCMetadata.h"
4041
#include "llvm/CodeGen/GCMetadataPrinter.h"
4142
#include "llvm/CodeGen/MachineBasicBlock.h"
@@ -141,6 +142,10 @@ static cl::opt<bool>
141142
DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
142143
cl::desc("Disable debug info printing"));
143144

145+
static cl::opt<bool>
146+
EmbedBitcodeFinal("embed-bitcode-final", cl::NotHidden,
147+
cl::desc("Embed final IR as bitcode after all optimisations and transformations have run."));
148+
144149
const char DWARFGroupName[] = "dwarf";
145150
const char DWARFGroupDescription[] = "DWARF Emission";
146151
const char DbgTimerName[] = "emit";
@@ -1151,6 +1156,36 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
11511156
OutStreamer->emitSymbolValue(FunctionSymbol, getPointerSize());
11521157
// Emit the total number of basic blocks in this function.
11531158
OutStreamer->emitULEB128IntValue(MF.size());
1159+
const Function &F = MF.getFunction();
1160+
1161+
// LLVM's codegen can can merge multiple BasicBlocks into a single
1162+
// MachineBasicBlock. Unfortunately, MachineBasicBlock::getBasicBlock() only
1163+
// returns the first BasicBlock in the merged sequence, so we have to find
1164+
// the other corresponding BasicBlock(s) (if any) in the merged sequence
1165+
// another way. We do so in two steps:
1166+
//
1167+
// 1. We create a set, MergedBBs, which is the set of BasicBlocks that are
1168+
// *not* returned by MachineBasicBlock::getBasicBlock(MBB) for any
1169+
// MachineBasicBlock, MBB, in the parent MachineFunction -- in other words,
1170+
// it's the set of BasicBlocks that have been merged into a predecessor
1171+
// during codegen.
1172+
//
1173+
// 2. For each BasicBlock BBX returned by
1174+
// MachineBasicBlock::getBasicBlock() we check if it is terminated by an
1175+
// unconditional branch. If so and that unconditional branch transfers to a
1176+
// block BBY, and BBY is a member of MergedBBs, then we know that BBX and
1177+
// BBY were merged during codegen. [Note that we then see if another BBZ
1178+
// was also merged into BBY and so on]
1179+
std::set<const BasicBlock *> MergedBBs;
1180+
for (const BasicBlock &BB : F) {
1181+
MergedBBs.insert(&BB);
1182+
}
1183+
for (const MachineBasicBlock &MBB : MF) {
1184+
const BasicBlock *BB = MBB.getBasicBlock();
1185+
if (BB != nullptr) {
1186+
MergedBBs.erase(BB);
1187+
}
1188+
}
11541189
// Emit BB Information for each basic block in the funciton.
11551190
for (const MachineBasicBlock &MBB : MF) {
11561191
const MCSymbol *MBBSymbol =
@@ -1161,6 +1196,43 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
11611196
// always be computed from their offsets.
11621197
emitLabelDifferenceAsULEB128(MBB.getEndSymbol(), MBBSymbol);
11631198
OutStreamer->emitULEB128IntValue(getBBAddrMapMetadata(MBB));
1199+
// Find BBs corresponding with this MBB as described above.
1200+
const BasicBlock *CorrBB = MBB.getBasicBlock();
1201+
std::vector<const BasicBlock *> CorrBBs;
1202+
while (CorrBB != nullptr) {
1203+
CorrBBs.push_back(CorrBB);
1204+
const Instruction *Term = CorrBB->getTerminator();
1205+
assert(Term != nullptr);
1206+
if ((isa<BranchInst>(Term)) &&
1207+
(!(dyn_cast<const BranchInst>(Term))->isConditional()))
1208+
{
1209+
CorrBB = CorrBB->getUniqueSuccessor();
1210+
assert(CorrBB != nullptr);
1211+
if (MergedBBs.count(CorrBB) == 0) {
1212+
CorrBB = nullptr;
1213+
}
1214+
} else {
1215+
CorrBB = nullptr;
1216+
}
1217+
}
1218+
// Emit the number of corresponding BasicBlocks.
1219+
OutStreamer->emitULEB128IntValue(CorrBBs.size());
1220+
// Emit the corresponding block indices.
1221+
for (auto CorrBB : CorrBBs) {
1222+
size_t I = 0;
1223+
bool Found = false;
1224+
for (auto It = F.begin(); It != F.end(); It++) {
1225+
const BasicBlock *BB = &*It;
1226+
if (BB == CorrBB) {
1227+
Found = true;
1228+
break;
1229+
}
1230+
I++;
1231+
}
1232+
if (!Found)
1233+
OutContext.reportError(SMLoc(), "Couldn't find the block's index");
1234+
OutStreamer->emitULEB128IntValue(I);
1235+
}
11641236
}
11651237
OutStreamer->PopSection();
11661238
}
@@ -1711,6 +1783,18 @@ void AsmPrinter::emitRemarksSection(remarks::RemarkStreamer &RS) {
17111783
}
17121784

17131785
bool AsmPrinter::doFinalization(Module &M) {
1786+
// The `embed-bitcode` flag serialises the IR after only architecture
1787+
// agnostic optimisations have been run, but then proceeds to apply other
1788+
// optimisations and transformations afterwards. Sometimes this final version
1789+
// is precisely what we are interested in. The `embed-bitcode-final` flag
1790+
// waits until all optimisations/transformations have been run before
1791+
// embedding the IR.
1792+
if (EmbedBitcodeFinal)
1793+
llvm::EmbedBitcodeInModule(M, llvm::MemoryBufferRef(),
1794+
/*EmbedBitcode*/ true,
1795+
/*EmbedCmdline*/ false,
1796+
/*CmdArgs*/ std::vector<uint8_t>());
1797+
17141798
// Set the MachineFunction to nullptr so that we can catch attempted
17151799
// accesses to MF specific features at the module level and so that
17161800
// we can conditionalize accesses based on whether or not it is nullptr.

llvm/lib/LTO/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ add_llvm_component_library(LLVMLTO
3535
Support
3636
Target
3737
TransformUtils
38+
YkControlPoint
3839
)

llvm/lib/LTO/LTOBackend.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "llvm/Transforms/Scalar/LoopPassManager.h"
4747
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
4848
#include "llvm/Transforms/Utils/SplitModule.h"
49+
#include "llvm/Transforms/Yk/ControlPoint.h"
4950

5051
using namespace llvm;
5152
using namespace lto;
@@ -74,6 +75,11 @@ static cl::opt<bool> ThinLTOAssumeMerged(
7475
cl::desc("Assume the input has already undergone ThinLTO function "
7576
"importing and the other pre-optimization pipeline changes."));
7677

78+
static cl::opt<bool>
79+
YkPatchCtrlPoint("yk-patch-control-point",
80+
cl::init(false), cl::NotHidden,
81+
cl::desc("Patch yk_control_point()"));
82+
7783
LLVM_ATTRIBUTE_NORETURN static void reportOpenError(StringRef Path, Twine Msg) {
7884
errs() << "failed to open " << Path << ": " << Msg << '\n';
7985
errs().flush();
@@ -300,6 +306,13 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,
300306
MPM.addPass(PB.buildLTODefaultPipeline(OL, ExportSummary));
301307
}
302308

309+
// We add the yk control point pass late in the pipeline (after all
310+
// optimisation and just before verification and codegen) so that no IR
311+
// optimisation passes have a chance to change the interface to the control
312+
// point. The JIT runtime relies on the signature not being changed.
313+
if (YkPatchCtrlPoint)
314+
MPM.addPass(YkControlPointPass());
315+
303316
if (!Conf.DisableVerify)
304317
MPM.addPass(VerifierPass());
305318

llvm/lib/Passes/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ add_llvm_component_library(LLVMPasses
2525
TransformUtils
2626
Vectorize
2727
Instrumentation
28+
YkControlPoint
2829
)

llvm/lib/Transforms/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ add_subdirectory(Hello)
99
add_subdirectory(ObjCARC)
1010
add_subdirectory(Coroutines)
1111
add_subdirectory(CFGuard)
12+
add_subdirectory(Yk)

llvm/lib/Transforms/Yk/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
add_llvm_component_library(LLVMYkControlPoint
2+
ControlPoint.cpp
3+
4+
DEPENDS
5+
intrinsics_gen
6+
7+
LINK_COMPONENTS
8+
Core
9+
Support
10+
)

0 commit comments

Comments
 (0)