From a47e91b02ee49f13a756a438c1c88dd09799116f Mon Sep 17 00:00:00 2001 From: Edd Barrett Date: Fri, 19 Apr 2024 15:22:59 +0100 Subject: [PATCH 1/4] "Namespace" variants of OpCode. (and consistently use serialiseOpcode to emit them) --- llvm/lib/YkIR/YkIRWriter.cpp | 50 ++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/llvm/lib/YkIR/YkIRWriter.cpp b/llvm/lib/YkIR/YkIRWriter.cpp index 82a2a84072bfdf..8086433d23378c 100644 --- a/llvm/lib/YkIR/YkIRWriter.cpp +++ b/llvm/lib/YkIR/YkIRWriter.cpp @@ -36,19 +36,19 @@ 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 { @@ -290,7 +290,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: @@ -370,7 +370,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())); @@ -418,7 +418,7 @@ class YkIRWriter { assert(I->getCalledFunction()); // opcode: - serialiseOpcode(OpCode::Call); + serialiseOpcode(OpCodeCall); // callee: OutStreamer.emitSizeT(functionIndex(I->getCalledFunction())); // num_args: @@ -445,10 +445,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. // @@ -465,7 +465,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: @@ -478,7 +478,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: @@ -497,7 +497,7 @@ class YkIRWriter { assert(Res); // opcode: - serialiseOpcode(OpCode::PtrAdd); + serialiseOpcode(OpCodePtrAdd); // type_idx: OutStreamer.emitSizeT(typeIndex(I->getType())); // pointer: @@ -552,7 +552,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: @@ -569,7 +569,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) { @@ -588,7 +588,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: @@ -626,7 +626,7 @@ class YkIRWriter { ValueLoweringMap &VLMap, unsigned BBIdx, unsigned &InstIdx) { // opcode: - serialiseOpcode(UnimplementedInstruction); + serialiseOpcode(OpCodeUnimplemented); // stringified problem instruction serialiseString(toString(I)); From ddb5c1b197496e24302980412560cd66889cc125 Mon Sep 17 00:00:00 2001 From: Edd Barrett Date: Fri, 19 Apr 2024 15:33:41 +0100 Subject: [PATCH 2/4] "Namespace" OperandKind. Also: - Introduce and consistently use serialiser helper method. - Remove now redundant (no longer clashing) `class` hints. --- llvm/lib/YkIR/YkIRWriter.cpp | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/llvm/lib/YkIR/YkIRWriter.cpp b/llvm/lib/YkIR/YkIRWriter.cpp index 8086433d23378c..d7c2453e35c18d 100644 --- a/llvm/lib/YkIR/YkIRWriter.cpp +++ b/llvm/lib/YkIR/YkIRWriter.cpp @@ -52,11 +52,11 @@ enum OpCode { }; enum OperandKind { - Constant = 0, - LocalVariable, - Global, - Function, - Arg, + OperandKindConstant = 0, + OperandKindLocal, + OperandKindGlobal, + OperandKindFunction, + OperandKindArg, }; enum TypeKind { @@ -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::iterator Found = + size_t constantIndex(Constant *C) { + vector::iterator Found = std::find(Constants.begin(), Constants.end(), C); if (Found != Constants.end()) { return std::distance(Constants.begin(), Found); @@ -228,20 +228,22 @@ class YkIRWriter { void serialiseOpcode(OpCode Code) { OutStreamer.emitInt8(Code); } + void serialiseOperandKind(OperandKind 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)); } @@ -254,7 +256,7 @@ 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 @@ -262,7 +264,7 @@ class YkIRWriter { } void serialiseGlobalOperand(GlobalVariable *G) { - OutStreamer.emitInt8(OperandKind::Global); + serialiseOperandKind(OperandKindGlobal); OutStreamer.emitSizeT(globalIndex(G)); } @@ -772,7 +774,7 @@ class YkIRWriter { } } - void serialiseUnimplementedConstant(class Constant *C) { + void serialiseUnimplementedConstant(Constant *C) { // type_index: OutStreamer.emitSizeT(typeIndex(C->getType())); // num_bytes: @@ -780,7 +782,7 @@ class YkIRWriter { OutStreamer.emitSizeT(0); } - void serialiseConstant(class Constant *C) { + void serialiseConstant(Constant *C) { if (ConstantInt *CI = dyn_cast(C)) { serialiseConstantInt(CI); } else { @@ -820,7 +822,7 @@ class YkIRWriter { // num_constants: OutStreamer.emitSizeT(Constants.size()); // constants: - for (class Constant *&C : Constants) { + for (Constant *&C : Constants) { serialiseConstant(C); } From d05dcc2be1f5a0406c0b3104ad16c3a04073bc24 Mon Sep 17 00:00:00 2001 From: Edd Barrett Date: Fri, 19 Apr 2024 15:41:02 +0100 Subject: [PATCH 3/4] "Namespace" TypeKind. And add+use serialiser helper. --- llvm/lib/YkIR/YkIRWriter.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/llvm/lib/YkIR/YkIRWriter.cpp b/llvm/lib/YkIR/YkIRWriter.cpp index d7c2453e35c18d..fffdd5105ea4c0 100644 --- a/llvm/lib/YkIR/YkIRWriter.cpp +++ b/llvm/lib/YkIR/YkIRWriter.cpp @@ -60,12 +60,12 @@ enum OperandKind { }; 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. @@ -227,8 +227,8 @@ 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) { serialiseOperandKind(OperandKindConstant); @@ -715,7 +715,7 @@ class YkIRWriter { } void serialiseFunctionType(FunctionType *Ty) { - OutStreamer.emitInt8(TypeKind::FunctionTy); + serialiseTypeKind(TypeKindFunction); // num_args: OutStreamer.emitSizeT(Ty->getNumParams()); // arg_tys: @@ -729,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); @@ -747,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(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(Ty)) { - OutStreamer.emitInt8(TypeKind::Integer); + serialiseTypeKind(TypeKindInteger); OutStreamer.emitInt32(ITy->getBitWidth()); } else if (FunctionType *FTy = dyn_cast(Ty)) { serialiseFunctionType(FTy); } else if (StructType *STy = dyn_cast(Ty)) { serialiseStructType(STy); } else { - OutStreamer.emitInt8(TypeKind::UnimplementedType); + serialiseTypeKind(TypeKindUnimplemented); serialiseString(toString(Ty)); } } From 2071c03579f0f9ffb60b83d5eea50fa221021710 Mon Sep 17 00:00:00 2001 From: Edd Barrett Date: Fri, 19 Apr 2024 15:42:29 +0100 Subject: [PATCH 4/4] Remove redundant `class` hints. --- llvm/lib/YkIR/YkIRWriter.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/llvm/lib/YkIR/YkIRWriter.cpp b/llvm/lib/YkIR/YkIRWriter.cpp index fffdd5105ea4c0..4de9d9d20e7d15 100644 --- a/llvm/lib/YkIR/YkIRWriter.cpp +++ b/llvm/lib/YkIR/YkIRWriter.cpp @@ -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::iterator Found = + size_t globalIndex(GlobalVariable *G) { + vector::iterator Found = std::find(Globals.begin(), Globals.end(), G); if (Found != Globals.end()) { return std::distance(Globals.begin(), Found); @@ -790,7 +790,7 @@ class YkIRWriter { } } - void serialiseGlobal(class GlobalVariable *G) { + void serialiseGlobal(GlobalVariable *G) { OutStreamer.emitInt8(G->isThreadLocal()); serialiseString(G->getName()); } @@ -829,7 +829,7 @@ class YkIRWriter { // num_globals: OutStreamer.emitSizeT(Globals.size()); // globals: - for (class GlobalVariable *&G : Globals) { + for (GlobalVariable *&G : Globals) { serialiseGlobal(G); }