Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for writing LLVM 5 output when in opaque pointer mode #5

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 62 additions & 10 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 @@ -810,7 +822,14 @@ void ModuleBitcodeWriter50::writeTypeTable() {
unsigned AddressSpace = PTy->getAddressSpace();
if (PTy->isOpaque()) {
// OPAQUE_POINTER: [address space]
llvm_unreachable("opaque pointers are not supported with LLVM 5.0");
// 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;
Expand All @@ -821,6 +840,17 @@ void ModuleBitcodeWriter50::writeTypeTable() {
}
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->getElementType()));
TypeVals.push_back(AddressSpace);
if (AddressSpace == 0)
AbbrevToUse = PtrAbbrev;
break;
}
case Type::FunctionTyID: {
FunctionType *FT = cast<FunctionType>(T);
// FUNCTION: [isvararg, retty, paramty x N]
Expand Down Expand Up @@ -887,8 +917,6 @@ void ModuleBitcodeWriter50::writeTypeTable() {
case Type::TargetExtTyID:
llvm_unreachable("Target extension types are not supported with LLVM 5.0");
break;
case Type::TypedPointerTyID:
llvm_unreachable("Typed pointers cannot be added to IR modules");
}

// Emit the finished record.
Expand Down Expand Up @@ -1226,7 +1254,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 @@ -1321,7 +1352,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 @@ -2427,12 +2463,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 @@ -2481,9 +2520,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 @@ -2821,6 +2866,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