Skip to content

Commit

Permalink
Support for emitting typed LLVM 5.0 IR from opaque pointers. (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Jun 7, 2024
1 parent 6415c86 commit 60d2327
Show file tree
Hide file tree
Showing 8 changed files with 449 additions and 22 deletions.
82 changes: 70 additions & 12 deletions llvm/lib/Bitcode/Writer50/BitcodeWriter50.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "llvm/Bitcode/BitcodeWriter.h"
#include "ValueEnumerator50.h"
#include "ModuleRewriter50.h"
#include "PointerRewriter.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Bitstream/BitstreamWriter.h"
Expand All @@ -26,6 +27,7 @@
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/TypedPointerType.h"
#include "llvm/IR/UseListOrder.h"
#include "llvm/IR/ValueSymbolTable.h"
#include "llvm/MC/StringTableBuilder.h"
Expand Down Expand Up @@ -136,6 +138,8 @@ class ModuleBitcodeWriter50 : public BitcodeWriterBase50 {
/// backpatched with the offset of the actual VST.
uint64_t VSTOffsetPlaceholder = 0;

PointerTypeMap PointerMap;

public:
/// Constructs a ModuleBitcodeWriter50 object for the given Module,
/// writing to the provided \p Buffer.
Expand All @@ -148,6 +152,14 @@ class ModuleBitcodeWriter50 : public BitcodeWriterBase50 {
VE(M, ShouldPreserveUseListOrder), Index(Index),
GenerateHash(GenerateHash), ModHash(ModHash),
BitcodeStartBit(Stream.GetCurrentBitNo()) {

// Enumerate typed pointers
if (!M.getContext().supportsTypedPointers()) {
PointerMap = PointerRewriter::buildPointerMap(M);
for (auto El : PointerMap)
VE.EnumerateType(El.second);
}

// imitate Metal by having one llvm.dbg.cu entry per DISubprogram
if (auto dbg_cu = M.getNamedMetadata("llvm.dbg.cu"); dbg_cu) {
uint32_t subprogram_count = 0;
Expand Down Expand Up @@ -283,7 +295,7 @@ class ModuleBitcodeWriter50 : public BitcodeWriterBase50 {
void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal);
void writeModuleConstants();
bool pushValueAndType(const Value *V, unsigned InstID,
SmallVectorImpl<unsigned> &Vals);
SmallVectorImpl<unsigned> &Vals, Type *Ty = nullptr);
void writeOperandBundles(const CallBase &CB, unsigned InstID);
void pushValue(const Value *V, unsigned InstID,
SmallVectorImpl<unsigned> &Vals);
Expand Down Expand Up @@ -813,12 +825,36 @@ void ModuleBitcodeWriter50::writeTypeTable() {
break;
case Type::PointerTyID: {
PointerType *PTy = cast<PointerType>(T);
unsigned AddressSpace = PTy->getAddressSpace();
if (PTy->isOpaque()) {
// OPAQUE_POINTER: [address space]
// unsupported, so emit as
// POINTER: [opaque element type, address space]
Code = bitc::TYPE_CODE_POINTER;
auto ET = StructType::get(PTy->getContext());
TypeVals.push_back(VE.getTypeID(ET));
TypeVals.push_back(AddressSpace);
if (AddressSpace == 0)
AbbrevToUse = PtrAbbrev;
} else {
// POINTER: [pointee type, address space]
Code = bitc::TYPE_CODE_POINTER;
TypeVals.push_back(VE.getTypeID(PTy->getNonOpaquePointerElementType()));
TypeVals.push_back(AddressSpace);
if (AddressSpace == 0)
AbbrevToUse = PtrAbbrev;
}
break;
}
case Type::TypedPointerTyID: {
TypedPointerType *PTy = cast<TypedPointerType>(T);
unsigned AddressSpace = PTy->getAddressSpace();
// POINTER: [pointee type, address space]
Code = bitc::TYPE_CODE_POINTER;
TypeVals.push_back(VE.getTypeID(PTy->getNonOpaquePointerElementType()));
unsigned AddressSpace = PTy->getAddressSpace();
TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
TypeVals.push_back(AddressSpace);
if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
if (AddressSpace == 0)
AbbrevToUse = PtrAbbrev;
break;
}
case Type::FunctionTyID: {
Expand Down Expand Up @@ -885,8 +921,6 @@ void ModuleBitcodeWriter50::writeTypeTable() {
case Type::X86_AMXTyID:
report_fatal_error("AMX types are not supported with LLVM 5.0");
break;
case Type::DXILPointerTyID:
report_fatal_error("DXIL pointers cannot be added to IR modules");
}

// Emit the finished record.
Expand Down Expand Up @@ -1223,7 +1257,10 @@ void ModuleBitcodeWriter50::writeModuleInfo() {
// prefixdata, personalityfn]
Vals.push_back(addToStrtab(F.getName()));
Vals.push_back(F.getName().size());
Vals.push_back(VE.getTypeID(F.getFunctionType()));
Type *FTy = F.getFunctionType();
if (auto Ty = PointerMap.lookup(&F))
FTy = cast<FunctionType>(Ty->getElementType());
Vals.push_back(VE.getTypeID(FTy));
Vals.push_back(F.getCallingConv());
Vals.push_back(F.isDeclaration());
Vals.push_back(getEncodedLinkage(F));
Expand Down Expand Up @@ -1317,7 +1354,12 @@ void ModuleBitcodeWriter50::writeValueAsMetadata(
const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) {
// Mimic an MDNode with a value as one operand.
Value *V = MD->getValue();
Record.push_back(VE.getTypeID(V->getType()));
Type *Ty = V->getType();
if (Function *F = dyn_cast<Function>(V))
Ty = TypedPointerType::get(F->getFunctionType(), F->getAddressSpace());
else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
Ty = TypedPointerType::get(GV->getValueType(), GV->getAddressSpace());
Record.push_back(VE.getTypeID(Ty));
Record.push_back(VE.getValueID(V));
Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
Record.clear();
Expand Down Expand Up @@ -2423,12 +2465,15 @@ void ModuleBitcodeWriter50::writeModuleConstants() {
/// instruction ID, then it is a forward reference, and it also includes the
/// type ID. The value ID that is written is encoded relative to the InstID.
bool ModuleBitcodeWriter50::pushValueAndType(const Value *V, unsigned InstID,
SmallVectorImpl<unsigned> &Vals) {
SmallVectorImpl<unsigned> &Vals,
Type *Ty) {
unsigned ValID = VE.getValueID(V);
// Make encoding relative to the InstID.
Vals.push_back(InstID - ValID);
if (ValID >= InstID) {
Vals.push_back(VE.getTypeID(V->getType()));
if (!Ty)
Ty = V->getType();
Vals.push_back(VE.getTypeID(Ty));
return true;
}
return false;
Expand Down Expand Up @@ -2477,9 +2522,15 @@ void ModuleBitcodeWriter50::writeInstruction(const Instruction &I,
default:
if (Instruction::isCast(I.getOpcode())) {
Code = bitc::FUNC_CODE_INST_CAST;
if (!pushValueAndType(I.getOperand(0), InstID, Vals))
Type *OpTy = I.getOperand(0)->getType();
if (PointerMap.find(I.getOperand(0)) != PointerMap.end())
OpTy = PointerMap[I.getOperand(0)];
if (!pushValueAndType(I.getOperand(0), InstID, Vals, OpTy))
AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
Vals.push_back(VE.getTypeID(I.getType()));
Type *Ty = I.getType();
if (PointerMap.find(&I) != PointerMap.end())
Ty = PointerMap[&I];
Vals.push_back(VE.getTypeID(Ty));
Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
} else {
assert(isa<BinaryOperator>(I) && "Unknown instruction!");
Expand Down Expand Up @@ -2817,6 +2868,13 @@ void ModuleBitcodeWriter50::writeInstruction(const Instruction &I,
const CallInst &CI = cast<CallInst>(I);
FunctionType *FTy = CI.getFunctionType();

// Rewrite function types to match incoming arguments
if (CI.getCalledFunction()) {
auto F = CI.getCalledFunction();
if (auto Ty = PointerMap.lookup(F))
FTy = cast<FunctionType>(Ty->getElementType());
}

if (CI.hasOperandBundles())
writeOperandBundles(CI, InstID);

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Bitcode/Writer50/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_llvm_component_library(LLVMBitWriter50
BitcodeWriterPass50.cpp
ValueEnumerator50.cpp
ModuleRewriter50.cpp
PointerRewriter.cpp

DEPENDS
intrinsics_gen
Expand Down
16 changes: 11 additions & 5 deletions llvm/lib/Bitcode/Writer50/ModuleRewriter50.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
//===----------------------------------------------------------------------===//

#include "ModuleRewriter50.h"
#include "PointerRewriter.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
using namespace llvm;

void ModuleRewriter50::run() {
removeFreeze();
}

bool ModuleRewriter50::removeFreeze() {
bool removeFreeze(Module &M) {
// Find freeze instructions
SmallVector<FreezeInst *, 8> Worklist;
for (Function &F : M) {
Expand All @@ -42,3 +39,12 @@ bool ModuleRewriter50::removeFreeze() {
}
return true;
}

bool ModuleRewriter50::run() {
bool Changed = removeFreeze(M);

PointerRewriter PR(M);
Changed |= PR.run();

return Changed;
}
6 changes: 2 additions & 4 deletions llvm/lib/Bitcode/Writer50/ModuleRewriter50.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ class ModuleRewriter50 {
public:
ModuleRewriter50(Module &M) : M(M) {}

void run();
bool run();

private:
Module &M;

bool removeFreeze();
Module &M;
};


Expand Down
Loading

0 comments on commit 60d2327

Please sign in to comment.