Skip to content

Commit 59ceaec

Browse files
committed
Serialise globals.
1 parent ee3fd03 commit 59ceaec

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

llvm/lib/YkIR/YkIRWriter.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "llvm/BinaryFormat/ELF.h"
88
#include "llvm/IR/Constant.h"
99
#include "llvm/IR/Constants.h"
10+
#include "llvm/IR/GlobalValue.h"
1011
#include "llvm/IR/InstrTypes.h"
1112
#include "llvm/IR/Instructions.h"
1213
#include "llvm/IR/Module.h"
@@ -56,6 +57,7 @@ enum OperandKind {
5657
Function,
5758
Block,
5859
Arg,
60+
Global,
5961
UnimplementedOperand = 255,
6062
};
6163

@@ -128,6 +130,7 @@ class YkIRWriter {
128130

129131
vector<llvm::Type *> Types;
130132
vector<llvm::Constant *> Constants;
133+
vector<llvm::GlobalVariable *> Globals;
131134

132135
// Return the index of the LLVM type `Ty`, inserting a new entry if
133136
// necessary.
@@ -164,6 +167,19 @@ class YkIRWriter {
164167
return Idx;
165168
}
166169

170+
// Return the index of the LLVM global `G`, inserting a new entry if
171+
// necessary.
172+
size_t globalIndex(class GlobalVariable *G) {
173+
vector<class GlobalVariable *>::iterator Found =
174+
std::find(Globals.begin(), Globals.end(), G);
175+
if (Found != Globals.end()) {
176+
return std::distance(Globals.begin(), Found);
177+
}
178+
size_t Idx = Globals.size();
179+
Globals.push_back(G);
180+
return Idx;
181+
}
182+
167183
size_t functionIndex(llvm::Function *F) {
168184
// FIXME: For now we assume that function indicies in LLVM IR and our IR
169185
// are the same.
@@ -221,9 +237,16 @@ class YkIRWriter {
221237
OutStreamer.emitSizeT(A->getArgNo());
222238
}
223239

240+
void serialiseGlobalOperand(GlobalVariable *G) {
241+
OutStreamer.emitInt8(OperandKind::Global);
242+
OutStreamer.emitSizeT(globalIndex(G));
243+
}
244+
224245
void serialiseOperand(Instruction *Parent, ValueLoweringMap &VLMap,
225246
Value *V) {
226-
if (llvm::Function *F = dyn_cast<llvm::Function>(V)) {
247+
if (llvm::GlobalVariable *G = dyn_cast<llvm::GlobalVariable>(V)) {
248+
serialiseGlobalOperand(G);
249+
} else if (llvm::Function *F = dyn_cast<llvm::Function>(V)) {
227250
serialiseFunctionOperand(F);
228251
} else if (llvm::Constant *C = dyn_cast<llvm::Constant>(V)) {
229252
serialiseConstantOperand(Parent, C);
@@ -511,6 +534,11 @@ class YkIRWriter {
511534
}
512535
}
513536

537+
void serialiseGlobal(class GlobalVariable *G) {
538+
OutStreamer.emitInt8(G->isThreadLocal());
539+
serialiseString(G->getName());
540+
}
541+
514542
public:
515543
YkIRWriter(Module &M, MCStreamer &OutStreamer)
516544
: M(M), OutStreamer(OutStreamer), DL(&M) {}
@@ -542,6 +570,13 @@ class YkIRWriter {
542570
serialiseConstant(C);
543571
}
544572

573+
// num_globals:
574+
OutStreamer.emitSizeT(Globals.size());
575+
// globals:
576+
for (class GlobalVariable *&G : Globals) {
577+
serialiseGlobal(G);
578+
}
579+
545580
// num_types:
546581
OutStreamer.emitSizeT(Types.size());
547582
// types:

0 commit comments

Comments
 (0)