mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-06-21 18:24:23 +00:00
IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of PR21532. Assembly and bitcode changes are in the wings, but this is the bulk of the change for the IR C++ API. I have a follow-up patch prepared for `clang`. If this breaks other sub-projects, I apologize in advance :(. Help me compile it on Darwin I'll try to fix it. FWIW, the errors should be easy to fix, so it may be simpler to just fix it yourself. This breaks the build for all metadata-related code that's out-of-tree. Rest assured the transition is mechanical and the compiler should catch almost all of the problems. Here's a quick guide for updating your code: - `Metadata` is the root of a class hierarchy with three main classes: `MDNode`, `MDString`, and `ValueAsMetadata`. It is distinct from the `Value` class hierarchy. It is typeless -- i.e., instances do *not* have a `Type`. - `MDNode`'s operands are all `Metadata *` (instead of `Value *`). - `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively. If you're referring solely to resolved `MDNode`s -- post graph construction -- just use `MDNode*`. - `MDNode` (and the rest of `Metadata`) have only limited support for `replaceAllUsesWith()`. As long as an `MDNode` is pointing at a forward declaration -- the result of `MDNode::getTemporary()` -- it maintains a side map of its uses and can RAUW itself. Once the forward declarations are fully resolved RAUW support is dropped on the ground. This means that uniquing collisions on changing operands cause nodes to become "distinct". (This already happened fairly commonly, whenever an operand went to null.) If you're constructing complex (non self-reference) `MDNode` cycles, you need to call `MDNode::resolveCycles()` on each node (or on a top-level node that somehow references all of the nodes). Also, don't do that. Metadata cycles (and the RAUW machinery needed to construct them) are expensive. - An `MDNode` can only refer to a `Constant` through a bridge called `ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`). As a side effect, accessing an operand of an `MDNode` that is known to be, e.g., `ConstantInt`, takes three steps: first, cast from `Metadata` to `ConstantAsMetadata`; second, extract the `Constant`; third, cast down to `ConstantInt`. The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have metadata schema owners transition away from using `Constant`s when the type isn't important (and they don't care about referring to `GlobalValue`s). In the meantime, I've added transitional API to the `mdconst` namespace that matches semantics with the old code, in order to avoid adding the error-prone three-step equivalent to every call site. If your old code was: MDNode *N = foo(); bar(isa <ConstantInt>(N->getOperand(0))); baz(cast <ConstantInt>(N->getOperand(1))); bak(cast_or_null <ConstantInt>(N->getOperand(2))); bat(dyn_cast <ConstantInt>(N->getOperand(3))); bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4))); you can trivially match its semantics with: MDNode *N = foo(); bar(mdconst::hasa <ConstantInt>(N->getOperand(0))); baz(mdconst::extract <ConstantInt>(N->getOperand(1))); bak(mdconst::extract_or_null <ConstantInt>(N->getOperand(2))); bat(mdconst::dyn_extract <ConstantInt>(N->getOperand(3))); bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4))); and when you transition your metadata schema to `MDInt`: MDNode *N = foo(); bar(isa <MDInt>(N->getOperand(0))); baz(cast <MDInt>(N->getOperand(1))); bak(cast_or_null <MDInt>(N->getOperand(2))); bat(dyn_cast <MDInt>(N->getOperand(3))); bay(dyn_cast_or_null<MDInt>(N->getOperand(4))); - A `CallInst` -- specifically, intrinsic instructions -- can refer to metadata through a bridge called `MetadataAsValue`. This is a subclass of `Value` where `getType()->isMetadataTy()`. `MetadataAsValue` is the *only* class that can legally refer to a `LocalAsMetadata`, which is a bridged form of non-`Constant` values like `Argument` and `Instruction`. It can also refer to any other `Metadata` subclass. (I'll break all your testcases in a follow-up commit, when I propagate this change to assembly.) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@ -438,43 +438,58 @@ void BitcodeReaderValueList::ResolveConstantForwardRefs() {
|
||||
}
|
||||
}
|
||||
|
||||
void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) {
|
||||
void BitcodeReaderMDValueList::AssignValue(Metadata *MD, unsigned Idx) {
|
||||
if (Idx == size()) {
|
||||
push_back(V);
|
||||
push_back(MD);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Idx >= size())
|
||||
resize(Idx+1);
|
||||
|
||||
WeakVH &OldV = MDValuePtrs[Idx];
|
||||
if (!OldV) {
|
||||
OldV = V;
|
||||
TrackingMDRef &OldMD = MDValuePtrs[Idx];
|
||||
if (!OldMD) {
|
||||
OldMD.reset(MD);
|
||||
return;
|
||||
}
|
||||
|
||||
// If there was a forward reference to this value, replace it.
|
||||
MDNode *PrevVal = cast<MDNode>(OldV);
|
||||
OldV->replaceAllUsesWith(V);
|
||||
MDNode::deleteTemporary(PrevVal);
|
||||
// Deleting PrevVal sets Idx value in MDValuePtrs to null. Set new
|
||||
// value for Idx.
|
||||
MDValuePtrs[Idx] = V;
|
||||
MDNodeFwdDecl *PrevMD = cast<MDNodeFwdDecl>(OldMD.get());
|
||||
PrevMD->replaceAllUsesWith(MD);
|
||||
MDNode::deleteTemporary(PrevMD);
|
||||
--NumFwdRefs;
|
||||
}
|
||||
|
||||
Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
|
||||
Metadata *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
|
||||
if (Idx >= size())
|
||||
resize(Idx + 1);
|
||||
|
||||
if (Value *V = MDValuePtrs[Idx]) {
|
||||
assert(V->getType()->isMetadataTy() && "Type mismatch in value table!");
|
||||
return V;
|
||||
}
|
||||
if (Metadata *MD = MDValuePtrs[Idx])
|
||||
return MD;
|
||||
|
||||
// Create and return a placeholder, which will later be RAUW'd.
|
||||
Value *V = MDNode::getTemporary(Context, None);
|
||||
MDValuePtrs[Idx] = V;
|
||||
return V;
|
||||
AnyFwdRefs = true;
|
||||
++NumFwdRefs;
|
||||
Metadata *MD = MDNode::getTemporary(Context, None);
|
||||
MDValuePtrs[Idx].reset(MD);
|
||||
return MD;
|
||||
}
|
||||
|
||||
void BitcodeReaderMDValueList::tryToResolveCycles() {
|
||||
if (!AnyFwdRefs)
|
||||
// Nothing to do.
|
||||
return;
|
||||
|
||||
if (NumFwdRefs)
|
||||
// Still forward references... can't resolve cycles.
|
||||
return;
|
||||
|
||||
// Resolve any cycles.
|
||||
for (auto &MD : MDValuePtrs) {
|
||||
assert(!(MD && isa<MDNodeFwdDecl>(MD)) && "Unexpected forward reference");
|
||||
if (auto *G = dyn_cast_or_null<GenericMDNode>(MD))
|
||||
G->resolveCycles();
|
||||
}
|
||||
}
|
||||
|
||||
Type *BitcodeReader::getTypeByID(unsigned ID) {
|
||||
@ -1066,6 +1081,7 @@ std::error_code BitcodeReader::ParseMetadata() {
|
||||
case BitstreamEntry::Error:
|
||||
return Error(BitcodeError::MalformedBlock);
|
||||
case BitstreamEntry::EndBlock:
|
||||
MDValueList.tryToResolveCycles();
|
||||
return std::error_code();
|
||||
case BitstreamEntry::Record:
|
||||
// The interesting case.
|
||||
@ -1100,13 +1116,13 @@ std::error_code BitcodeReader::ParseMetadata() {
|
||||
break;
|
||||
}
|
||||
case bitc::METADATA_FN_NODE: {
|
||||
// This is a function-local node.
|
||||
// This is a LocalAsMetadata record, the only type of function-local
|
||||
// metadata.
|
||||
if (Record.size() % 2 == 1)
|
||||
return Error(BitcodeError::InvalidRecord);
|
||||
|
||||
// If this isn't a single-operand node that directly references
|
||||
// non-metadata, we're dropping it. This used to be legal, but there's
|
||||
// no upgrade path.
|
||||
// If this isn't a LocalAsMetadata record, we're dropping it. This used
|
||||
// to be legal, but there's no upgrade path.
|
||||
auto dropRecord = [&] {
|
||||
MDValueList.AssignValue(MDNode::get(Context, None), NextMDValueNo++);
|
||||
};
|
||||
@ -1121,10 +1137,9 @@ std::error_code BitcodeReader::ParseMetadata() {
|
||||
break;
|
||||
}
|
||||
|
||||
Value *Elts[] = {ValueList.getValueFwdRef(Record[1], Ty)};
|
||||
Value *V = MDNode::getWhenValsUnresolved(Context, Elts,
|
||||
/*IsFunctionLocal*/ true);
|
||||
MDValueList.AssignValue(V, NextMDValueNo++);
|
||||
MDValueList.AssignValue(
|
||||
LocalAsMetadata::get(ValueList.getValueFwdRef(Record[1], Ty)),
|
||||
NextMDValueNo++);
|
||||
break;
|
||||
}
|
||||
case bitc::METADATA_NODE: {
|
||||
@ -1132,28 +1147,30 @@ std::error_code BitcodeReader::ParseMetadata() {
|
||||
return Error(BitcodeError::InvalidRecord);
|
||||
|
||||
unsigned Size = Record.size();
|
||||
SmallVector<Value*, 8> Elts;
|
||||
SmallVector<Metadata *, 8> Elts;
|
||||
for (unsigned i = 0; i != Size; i += 2) {
|
||||
Type *Ty = getTypeByID(Record[i]);
|
||||
if (!Ty)
|
||||
return Error(BitcodeError::InvalidRecord);
|
||||
if (Ty->isMetadataTy())
|
||||
Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
|
||||
else if (!Ty->isVoidTy())
|
||||
Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty));
|
||||
else
|
||||
else if (!Ty->isVoidTy()) {
|
||||
auto *MD =
|
||||
ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
|
||||
assert(isa<ConstantAsMetadata>(MD) &&
|
||||
"Expected non-function-local metadata");
|
||||
Elts.push_back(MD);
|
||||
} else
|
||||
Elts.push_back(nullptr);
|
||||
}
|
||||
Value *V = MDNode::getWhenValsUnresolved(Context, Elts,
|
||||
/*IsFunctionLocal*/ false);
|
||||
MDValueList.AssignValue(V, NextMDValueNo++);
|
||||
MDValueList.AssignValue(MDNode::get(Context, Elts), NextMDValueNo++);
|
||||
break;
|
||||
}
|
||||
case bitc::METADATA_STRING: {
|
||||
std::string String(Record.begin(), Record.end());
|
||||
llvm::UpgradeMDStringConstant(String);
|
||||
Value *V = MDString::get(Context, String);
|
||||
MDValueList.AssignValue(V, NextMDValueNo++);
|
||||
Metadata *MD = MDString::get(Context, String);
|
||||
MDValueList.AssignValue(MD, NextMDValueNo++);
|
||||
break;
|
||||
}
|
||||
case bitc::METADATA_KIND: {
|
||||
@ -2359,12 +2376,12 @@ std::error_code BitcodeReader::ParseMetadataAttachment() {
|
||||
MDKindMap.find(Kind);
|
||||
if (I == MDKindMap.end())
|
||||
return Error(BitcodeError::InvalidID);
|
||||
MDNode *Node = cast<MDNode>(MDValueList.getValueFwdRef(Record[i+1]));
|
||||
if (Node->isFunctionLocal())
|
||||
Metadata *Node = MDValueList.getValueFwdRef(Record[i + 1]);
|
||||
if (isa<LocalAsMetadata>(Node))
|
||||
// Drop the attachment. This used to be legal, but there's no
|
||||
// upgrade path.
|
||||
break;
|
||||
Inst->setMetadata(I->second, Node);
|
||||
Inst->setMetadata(I->second, cast<MDNode>(Node));
|
||||
if (I->second == LLVMContext::MD_tbaa)
|
||||
InstsWithTBAATag.push_back(Inst);
|
||||
}
|
||||
|
@ -19,7 +19,9 @@
|
||||
#include "llvm/Bitcode/LLVMBitCodes.h"
|
||||
#include "llvm/IR/Attributes.h"
|
||||
#include "llvm/IR/GVMaterializer.h"
|
||||
#include "llvm/IR/Metadata.h"
|
||||
#include "llvm/IR/OperandTraits.h"
|
||||
#include "llvm/IR/TrackingMDRef.h"
|
||||
#include "llvm/IR/Type.h"
|
||||
#include "llvm/IR/ValueHandle.h"
|
||||
#include <deque>
|
||||
@ -95,22 +97,25 @@ public:
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
class BitcodeReaderMDValueList {
|
||||
std::vector<WeakVH> MDValuePtrs;
|
||||
unsigned NumFwdRefs;
|
||||
bool AnyFwdRefs;
|
||||
std::vector<TrackingMDRef> MDValuePtrs;
|
||||
|
||||
LLVMContext &Context;
|
||||
public:
|
||||
BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
|
||||
BitcodeReaderMDValueList(LLVMContext &C)
|
||||
: NumFwdRefs(0), AnyFwdRefs(false), Context(C) {}
|
||||
|
||||
// vector compatibility methods
|
||||
unsigned size() const { return MDValuePtrs.size(); }
|
||||
void resize(unsigned N) { MDValuePtrs.resize(N); }
|
||||
void push_back(Value *V) { MDValuePtrs.push_back(V); }
|
||||
void push_back(Metadata *MD) { MDValuePtrs.emplace_back(MD); }
|
||||
void clear() { MDValuePtrs.clear(); }
|
||||
Value *back() const { return MDValuePtrs.back(); }
|
||||
Metadata *back() const { return MDValuePtrs.back(); }
|
||||
void pop_back() { MDValuePtrs.pop_back(); }
|
||||
bool empty() const { return MDValuePtrs.empty(); }
|
||||
|
||||
Value *operator[](unsigned i) const {
|
||||
Metadata *operator[](unsigned i) const {
|
||||
assert(i < MDValuePtrs.size());
|
||||
return MDValuePtrs[i];
|
||||
}
|
||||
@ -120,8 +125,9 @@ public:
|
||||
MDValuePtrs.resize(N);
|
||||
}
|
||||
|
||||
Value *getValueFwdRef(unsigned Idx);
|
||||
void AssignValue(Value *V, unsigned Idx);
|
||||
Metadata *getValueFwdRef(unsigned Idx);
|
||||
void AssignValue(Metadata *MD, unsigned Idx);
|
||||
void tryToResolveCycles();
|
||||
};
|
||||
|
||||
class BitcodeReader : public GVMaterializer {
|
||||
@ -248,9 +254,12 @@ private:
|
||||
Type *getTypeByID(unsigned ID);
|
||||
Value *getFnValueByID(unsigned ID, Type *Ty) {
|
||||
if (Ty && Ty->isMetadataTy())
|
||||
return MDValueList.getValueFwdRef(ID);
|
||||
return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));
|
||||
return ValueList.getValueFwdRef(ID, Ty);
|
||||
}
|
||||
Metadata *getFnMetadataByID(unsigned ID) {
|
||||
return MDValueList.getValueFwdRef(ID);
|
||||
}
|
||||
BasicBlock *getBasicBlock(unsigned ID) const {
|
||||
if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
|
||||
return FunctionBBs[ID];
|
||||
|
Reference in New Issue
Block a user