Skip to content

Commit

Permalink
Merge pull request rust-lang#134 from vext01/tidy-it-up
Browse files Browse the repository at this point in the history
Namespace enums in the serialiser.
  • Loading branch information
ltratt authored Apr 19, 2024
2 parents 7e771a7 + 2071c03 commit a4e3131
Showing 1 changed file with 58 additions and 56 deletions.
114 changes: 58 additions & 56 deletions llvm/lib/YkIR/YkIRWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,36 @@ const uint32_t Magic = 0xedd5f00d;
const uint32_t Version = 0;

enum OpCode {
Nop = 0,
Load,
Store,
Alloca,
Call,
Br,
CondBr,
ICmp,
Ret,
InsertValue,
PtrAdd,
BinOp,
UnimplementedInstruction = 255, // YKFIXME: Will eventually be deleted.
OpCodeNop = 0,
OpCodeLoad,
OpCodeStore,
OpCodeAlloca,
OpCodeCall,
OpCodeBr,
OpCodeCondBr,
OpCodeICmp,
OpCodeRet,
OpCodeInsertValue,
OpCodePtrAdd,
OpCodeBinOp,
OpCodeUnimplemented = 255, // YKFIXME: Will eventually be deleted.
};

enum OperandKind {
Constant = 0,
LocalVariable,
Global,
Function,
Arg,
OperandKindConstant = 0,
OperandKindLocal,
OperandKindGlobal,
OperandKindFunction,
OperandKindArg,
};

enum TypeKind {
Void = 0,
Integer,
Ptr,
FunctionTy,
Struct,
UnimplementedType = 255, // YKFIXME: Will eventually be deleted.
TypeKindVoid = 0,
TypeKindInteger,
TypeKindPtr,
TypeKindFunction,
TypeKindStruct,
TypeKindUnimplemented = 255, // YKFIXME: Will eventually be deleted.
};

// A predicate used in a numeric comparison.
Expand Down Expand Up @@ -190,8 +190,8 @@ class YkIRWriter {

// Return the index of the LLVM constant `C`, inserting a new entry if
// necessary.
size_t constantIndex(class Constant *C) {
vector<class Constant *>::iterator Found =
size_t constantIndex(Constant *C) {
vector<Constant *>::iterator Found =
std::find(Constants.begin(), Constants.end(), C);
if (Found != Constants.end()) {
return std::distance(Constants.begin(), Found);
Expand All @@ -203,8 +203,8 @@ class YkIRWriter {

// Return the index of the LLVM global `G`, inserting a new entry if
// necessary.
size_t globalIndex(class GlobalVariable *G) {
vector<class GlobalVariable *>::iterator Found =
size_t globalIndex(GlobalVariable *G) {
vector<GlobalVariable *>::iterator Found =
std::find(Globals.begin(), Globals.end(), G);
if (Found != Globals.end()) {
return std::distance(Globals.begin(), Found);
Expand All @@ -227,21 +227,23 @@ class YkIRWriter {
}

void serialiseOpcode(OpCode Code) { OutStreamer.emitInt8(Code); }
void serialiseOperandKind(OperandKind Kind) { OutStreamer.emitInt8(Kind); }
void serialiseTypeKind(TypeKind Kind) { OutStreamer.emitInt8(Kind); }

void serialiseConstantOperand(Instruction *Parent, llvm::Constant *C) {
OutStreamer.emitInt8(OperandKind::Constant);
serialiseOperandKind(OperandKindConstant);
OutStreamer.emitSizeT(constantIndex(C));
}

void serialiseLocalVariableOperand(Instruction *I, ValueLoweringMap &VLMap) {
auto [BBIdx, InstIdx] = VLMap.at(I);
OutStreamer.emitInt8(OperandKind::LocalVariable);
serialiseOperandKind(OperandKindLocal);
OutStreamer.emitSizeT(BBIdx);
OutStreamer.emitSizeT(InstIdx);
}

void serialiseFunctionOperand(llvm::Function *F) {
OutStreamer.emitInt8(OperandKind::Function);
serialiseOperandKind(OperandKindFunction);
OutStreamer.emitSizeT(functionIndex(F));
}

Expand All @@ -254,15 +256,15 @@ class YkIRWriter {
// This assumes that the argument indices match in both IRs.

// opcode:
OutStreamer.emitInt8(OperandKind::Arg);
serialiseOperandKind(OperandKindArg);
// parent function index:
OutStreamer.emitSizeT(getIndex(&M, A->getParent()));
// arg index
OutStreamer.emitSizeT(A->getArgNo());
}

void serialiseGlobalOperand(GlobalVariable *G) {
OutStreamer.emitInt8(OperandKind::Global);
serialiseOperandKind(OperandKindGlobal);
OutStreamer.emitSizeT(globalIndex(G));
}

Expand Down Expand Up @@ -290,7 +292,7 @@ class YkIRWriter {
assert(I->getNumOperands() == 2);

// opcode:
OutStreamer.emitInt8(OpCode::BinOp);
serialiseOpcode(OpCodeBinOp);
// left-hand side:
serialiseOperand(I, VLMap, I->getOperand(0));
// binary operator:
Expand Down Expand Up @@ -370,7 +372,7 @@ class YkIRWriter {
void serialiseAllocaInst(AllocaInst *I, ValueLoweringMap &VLMap,
unsigned BBIdx, unsigned &InstIdx) {
// opcode:
serialiseOpcode(OpCode::Alloca);
serialiseOpcode(OpCodeAlloca);

// type to be allocated:
OutStreamer.emitSizeT(typeIndex(I->getAllocatedType()));
Expand Down Expand Up @@ -418,7 +420,7 @@ class YkIRWriter {
assert(I->getCalledFunction());

// opcode:
serialiseOpcode(OpCode::Call);
serialiseOpcode(OpCodeCall);
// callee:
OutStreamer.emitSizeT(functionIndex(I->getCalledFunction()));
// num_args:
Expand All @@ -445,10 +447,10 @@ class YkIRWriter {
// traces will guide us.
//
// opcode:
serialiseOpcode(OpCode::Br);
serialiseOpcode(OpCodeBr);
} else {
// opcode:
serialiseOpcode(OpCode::CondBr);
serialiseOpcode(OpCodeCondBr);
// We DO need operands for conditional branches, so that we can build
// guards.
//
Expand All @@ -465,7 +467,7 @@ class YkIRWriter {
void serialiseLoadInst(LoadInst *I, ValueLoweringMap &VLMap, unsigned BBIdx,
unsigned &InstIdx) {
// opcode:
serialiseOpcode(OpCode::Load);
serialiseOpcode(OpCodeLoad);
// ptr:
serialiseOperand(I, VLMap, I->getPointerOperand());
// type_idx:
Expand All @@ -478,7 +480,7 @@ class YkIRWriter {
void serialiseStoreInst(StoreInst *I, ValueLoweringMap &VLMap, unsigned BBIdx,
unsigned &InstIdx) {
// opcode:
serialiseOpcode(OpCode::Store);
serialiseOpcode(OpCodeStore);
// value:
serialiseOperand(I, VLMap, I->getValueOperand());
// ptr:
Expand All @@ -497,7 +499,7 @@ class YkIRWriter {
assert(Res);

// opcode:
serialiseOpcode(OpCode::PtrAdd);
serialiseOpcode(OpCodePtrAdd);
// type_idx:
OutStreamer.emitSizeT(typeIndex(I->getType()));
// pointer:
Expand Down Expand Up @@ -552,7 +554,7 @@ class YkIRWriter {
void serialiseICmpInst(ICmpInst *I, ValueLoweringMap &VLMap, unsigned BBIdx,
unsigned &InstIdx) {
// opcode:
serialiseOpcode(OpCode::ICmp);
serialiseOpcode(OpCodeICmp);
// type_idx:
OutStreamer.emitSizeT(typeIndex(I->getType()));
// lhs:
Expand All @@ -569,7 +571,7 @@ class YkIRWriter {
void serialiseReturnInst(ReturnInst *I, ValueLoweringMap &VLMap,
unsigned BBIdx, unsigned &InstIdx) {
// opcode:
serialiseOpcode(OpCode::Ret);
serialiseOpcode(OpCodeRet);

Value *RV = I->getReturnValue();
if (RV == nullptr) {
Expand All @@ -588,7 +590,7 @@ class YkIRWriter {
void serialiseInsertValueInst(InsertValueInst *I, ValueLoweringMap &VLMap,
unsigned BBIdx, unsigned &InstIdx) {
// opcode:
serialiseOpcode(OpCode::InsertValue);
serialiseOpcode(OpCodeInsertValue);
// agg:
serialiseOperand(I, VLMap, I->getAggregateOperand());
// elem:
Expand Down Expand Up @@ -626,7 +628,7 @@ class YkIRWriter {
ValueLoweringMap &VLMap,
unsigned BBIdx, unsigned &InstIdx) {
// opcode:
serialiseOpcode(UnimplementedInstruction);
serialiseOpcode(OpCodeUnimplemented);
// stringified problem instruction
serialiseString(toString(I));

Expand Down Expand Up @@ -713,7 +715,7 @@ class YkIRWriter {
}

void serialiseFunctionType(FunctionType *Ty) {
OutStreamer.emitInt8(TypeKind::FunctionTy);
serialiseTypeKind(TypeKindFunction);
// num_args:
OutStreamer.emitSizeT(Ty->getNumParams());
// arg_tys:
Expand All @@ -727,7 +729,7 @@ class YkIRWriter {
}

void serialiseStructType(StructType *STy) {
OutStreamer.emitInt8(TypeKind::Struct);
serialiseTypeKind(TypeKindStruct);
unsigned NumFields = STy->getNumElements();
DataLayout DL(&M);
const StructLayout *SL = DL.getStructLayout(STy);
Expand All @@ -745,20 +747,20 @@ class YkIRWriter {

void serialiseType(llvm::Type *Ty) {
if (Ty->isVoidTy()) {
OutStreamer.emitInt8(TypeKind::Void);
serialiseTypeKind(TypeKindVoid);
} else if (PointerType *PT = dyn_cast<PointerType>(Ty)) {
// FIXME: The Yk runtime assumes all pointers are void-ptr-sized.
assert(DL.getPointerSize(PT->getAddressSpace()) == sizeof(void *));
OutStreamer.emitInt8(TypeKind::Ptr);
serialiseTypeKind(TypeKindPtr);
} else if (IntegerType *ITy = dyn_cast<IntegerType>(Ty)) {
OutStreamer.emitInt8(TypeKind::Integer);
serialiseTypeKind(TypeKindInteger);
OutStreamer.emitInt32(ITy->getBitWidth());
} else if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
serialiseFunctionType(FTy);
} else if (StructType *STy = dyn_cast<StructType>(Ty)) {
serialiseStructType(STy);
} else {
OutStreamer.emitInt8(TypeKind::UnimplementedType);
serialiseTypeKind(TypeKindUnimplemented);
serialiseString(toString(Ty));
}
}
Expand All @@ -772,23 +774,23 @@ class YkIRWriter {
}
}

void serialiseUnimplementedConstant(class Constant *C) {
void serialiseUnimplementedConstant(Constant *C) {
// type_index:
OutStreamer.emitSizeT(typeIndex(C->getType()));
// num_bytes:
// Just report zero for now.
OutStreamer.emitSizeT(0);
}

void serialiseConstant(class Constant *C) {
void serialiseConstant(Constant *C) {
if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
serialiseConstantInt(CI);
} else {
serialiseUnimplementedConstant(C);
}
}

void serialiseGlobal(class GlobalVariable *G) {
void serialiseGlobal(GlobalVariable *G) {
OutStreamer.emitInt8(G->isThreadLocal());
serialiseString(G->getName());
}
Expand Down Expand Up @@ -820,14 +822,14 @@ class YkIRWriter {
// num_constants:
OutStreamer.emitSizeT(Constants.size());
// constants:
for (class Constant *&C : Constants) {
for (Constant *&C : Constants) {
serialiseConstant(C);
}

// num_globals:
OutStreamer.emitSizeT(Globals.size());
// globals:
for (class GlobalVariable *&G : Globals) {
for (GlobalVariable *&G : Globals) {
serialiseGlobal(G);
}

Expand Down

0 comments on commit a4e3131

Please sign in to comment.