Skip to content

[AsmParser] Add support for reading incomplete IR (part 1) #78421

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

Merged
merged 3 commits into from
Jan 19, 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
1 change: 1 addition & 0 deletions llvm/include/llvm/AsmParser/LLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ namespace llvm {

// Top-Level Entities
bool parseTopLevelEntities();
void dropUnknownMetadataReferences();
bool validateEndOfModule(bool UpgradeDebugInfo);
bool validateEndOfIndex();
bool parseTargetDefinitions(DataLayoutCallbackTy DataLayoutCallback);
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/IR/GlobalObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class GlobalObject : public GlobalValue {
using Value::addMetadata;
using Value::clearMetadata;
using Value::eraseMetadata;
using Value::eraseMetadataIf;
using Value::getAllMetadata;
using Value::getMetadata;
using Value::hasMetadata;
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/IR/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ class Instruction : public User,
void copyMetadata(const Instruction &SrcInst,
ArrayRef<unsigned> WL = ArrayRef<unsigned>());

/// Erase all metadata that matches the predicate.
void eraseMetadataIf(function_ref<bool(unsigned, MDNode *)> Pred);

/// If the instruction has "branch_weights" MD_prof metadata and the MDNode
/// has three operands (including name string), swap the order of the
/// metadata.
Expand Down
7 changes: 7 additions & 0 deletions llvm/include/llvm/IR/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ class ReplaceableMetadataImpl {
/// is resolved.
void resolveAllUses(bool ResolveUsers = true);

unsigned getNumUses() const { return UseMap.size(); }

private:
void addRef(void *Ref, OwnerTy Owner);
void dropRef(void *Ref);
Expand Down Expand Up @@ -1243,6 +1245,11 @@ class MDNode : public Metadata {
bool isReplaceable() const { return isTemporary() || isAlwaysReplaceable(); }
bool isAlwaysReplaceable() const { return getMetadataID() == DIAssignIDKind; }

unsigned getNumTemporaryUses() const {
assert(isTemporary() && "Only for temporaries");
return Context.getReplaceableUses()->getNumUses();
}

/// RAUW a temporary.
///
/// \pre \a isTemporary() must be \c true.
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/IR/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,9 @@ class Value {
/// \returns true if any metadata was removed.
bool eraseMetadata(unsigned KindID);

/// Erase all metadata attachments matching the given predicate.
void eraseMetadataIf(function_ref<bool(unsigned, MDNode *)> Pred);

/// Erase all metadata attached to this Value.
void clearMetadata();

Expand Down
74 changes: 69 additions & 5 deletions llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "llvm/AsmParser/LLParser.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/AsmParser/LLToken.h"
#include "llvm/AsmParser/SlotMapping.h"
Expand All @@ -32,7 +32,9 @@
#include "llvm/IR/GlobalIFunc.h"
#include "llvm/IR/GlobalObject.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
Expand All @@ -54,6 +56,12 @@

using namespace llvm;

static cl::opt<bool> AllowIncompleteIR(
"allow-incomplete-ir", cl::init(false), cl::Hidden,
cl::desc(
"Allow incomplete IR on a best effort basis (references to unknown "
"metadata will be dropped)"));

static std::string getTypeString(Type *T) {
std::string Result;
raw_string_ostream Tmp(Result);
Expand Down Expand Up @@ -123,6 +131,55 @@ void LLParser::restoreParsingState(const SlotMapping *Slots) {
std::make_pair(I.first, std::make_pair(I.second, LocTy())));
}

static void dropIntrinsicWithUnknownMetadataArgument(IntrinsicInst *II) {
// White-list intrinsics that are safe to drop.
if (!isa<DbgInfoIntrinsic>(II) &&
II->getIntrinsicID() != Intrinsic::experimental_noalias_scope_decl)
return;

SmallVector<MetadataAsValue *> MVs;
for (Value *V : II->args())
if (auto *MV = dyn_cast<MetadataAsValue>(V))
if (auto *MD = dyn_cast<MDNode>(MV->getMetadata()))
if (MD->isTemporary())
MVs.push_back(MV);

if (!MVs.empty()) {
assert(II->use_empty() && "Cannot have uses");
II->eraseFromParent();

// Also remove no longer used MetadataAsValue wrappers.
for (MetadataAsValue *MV : MVs)
if (MV->use_empty())
delete MV;
}
}

void LLParser::dropUnknownMetadataReferences() {
auto Pred = [](unsigned MDKind, MDNode *Node) { return Node->isTemporary(); };
for (Function &F : *M) {
F.eraseMetadataIf(Pred);
for (Instruction &I : make_early_inc_range(instructions(F))) {
I.eraseMetadataIf(Pred);

if (auto *II = dyn_cast<IntrinsicInst>(&I))
dropIntrinsicWithUnknownMetadataArgument(II);
}
}

for (GlobalVariable &GV : M->globals())
GV.eraseMetadataIf(Pred);

for (const auto &[ID, Info] : make_early_inc_range(ForwardRefMDNodes)) {
// Check whether there is only a single use left, which would be in our
// own NumberedMetadata.
if (Info.first->getNumTemporaryUses() == 1) {
NumberedMetadata.erase(ID);
ForwardRefMDNodes.erase(ID);
}
}
}

/// validateEndOfModule - Do final validity and basic correctness checks at the
/// end of the module.
bool LLParser::validateEndOfModule(bool UpgradeDebugInfo) {
Expand Down Expand Up @@ -284,6 +341,9 @@ bool LLParser::validateEndOfModule(bool UpgradeDebugInfo) {
"use of undefined value '@" +
Twine(ForwardRefValIDs.begin()->first) + "'");

if (AllowIncompleteIR && !ForwardRefMDNodes.empty())
dropUnknownMetadataReferences();

if (!ForwardRefMDNodes.empty())
return error(ForwardRefMDNodes.begin()->second.second,
"use of undefined metadata '!" +
Expand All @@ -297,10 +357,14 @@ bool LLParser::validateEndOfModule(bool UpgradeDebugInfo) {

for (auto *Inst : InstsWithTBAATag) {
MDNode *MD = Inst->getMetadata(LLVMContext::MD_tbaa);
assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
auto *UpgradedMD = UpgradeTBAANode(*MD);
if (MD != UpgradedMD)
Inst->setMetadata(LLVMContext::MD_tbaa, UpgradedMD);
// With incomplete IR, the tbaa metadata may have been dropped.
if (!AllowIncompleteIR)
assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
if (MD) {
auto *UpgradedMD = UpgradeTBAANode(*MD);
if (MD != UpgradedMD)
Inst->setMetadata(LLVMContext::MD_tbaa, UpgradedMD);
}
}

// Look for intrinsic functions and CallInst that need to be upgraded. We use
Expand Down
34 changes: 24 additions & 10 deletions llvm/lib/IR/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,21 @@ bool Value::eraseMetadata(unsigned KindID) {
return Changed;
}

void Value::eraseMetadataIf(function_ref<bool(unsigned, MDNode *)> Pred) {
if (!HasMetadata)
return;

auto &MetadataStore = getContext().pImpl->ValueMetadata;
MDAttachments &Info = MetadataStore.find(this)->second;
assert(!Info.empty() && "bit out of sync with hash table");
Info.remove_if([Pred](const MDAttachments::Attachment &I) {
return Pred(I.MDKind, I.Node);
});

if (Info.empty())
clearMetadata();
}

void Value::clearMetadata() {
if (!HasMetadata)
return;
Expand All @@ -1556,6 +1571,13 @@ MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
return Value::getMetadata(KindID);
}

void Instruction::eraseMetadataIf(function_ref<bool(unsigned, MDNode *)> Pred) {
if (DbgLoc && Pred(LLVMContext::MD_dbg, DbgLoc.getAsMDNode()))
DbgLoc = {};

Value::eraseMetadataIf(Pred);
}

void Instruction::dropUnknownNonDebugMetadata(ArrayRef<unsigned> KnownIDs) {
if (!Value::hasMetadata())
return; // Nothing to remove!
Expand All @@ -1566,17 +1588,9 @@ void Instruction::dropUnknownNonDebugMetadata(ArrayRef<unsigned> KnownIDs) {
// A DIAssignID attachment is debug metadata, don't drop it.
KnownSet.insert(LLVMContext::MD_DIAssignID);

auto &MetadataStore = getContext().pImpl->ValueMetadata;
MDAttachments &Info = MetadataStore.find(this)->second;
assert(!Info.empty() && "bit out of sync with hash table");
Info.remove_if([&KnownSet](const MDAttachments::Attachment &I) {
return !KnownSet.count(I.MDKind);
Value::eraseMetadataIf([&KnownSet](unsigned MDKind, MDNode *Node) {
return !KnownSet.count(MDKind);
});

if (Info.empty()) {
// Drop our entry at the store.
clearMetadata();
}
}

void Instruction::updateDIAssignIDMapping(DIAssignID *ID) {
Expand Down
9 changes: 9 additions & 0 deletions llvm/test/Assembler/incomplete-ir-metadata-unsupported.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
; RUN: not llvm-as -allow-incomplete-ir < %s 2>&1 | FileCheck %s

; CHECK: error: use of undefined metadata '!1'
define void @test(ptr %p) {
%v = load i8, ptr %p, !noalias !0
ret void
}

!0 = !{!1}
34 changes: 34 additions & 0 deletions llvm/test/Assembler/incomplete-ir-metadata.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
; RUN: opt -S -allow-incomplete-ir < %s | FileCheck %s

@g = global i8 0, !exclude !4

define void @test(ptr %p) !dbg !3 {
; CHECK-LABEL: define void @test(
; CHECK-SAME: ptr [[P:%.*]]) {
; CHECK-NEXT: [[V1:%.*]] = load i8, ptr [[P]], align 1
; CHECK-NEXT: [[V2:%.*]] = load i8, ptr [[P]], align 1
; CHECK-NEXT: [[V3:%.*]] = load i8, ptr [[P]], align 1, !noalias [[META0:![0-9]+]]
; CHECK-NEXT: call void @llvm.experimental.noalias.scope.decl(metadata [[META0]])
; CHECK-NEXT: ret void
;
%v1 = load i8, ptr %p, !noalias !0
%v2 = load i8, ptr %p, !tbaa !1
%v3 = load i8, ptr %p, !dbg !2, !noalias !100
call void @llvm.experimental.noalias.scope.decl(metadata !5)
call void @llvm.dbg.value(metadata i32 0, metadata !7, metadata !8)
call void @llvm.experimental.noalias.scope.decl(metadata !100)
ret void
}

declare void @llvm.experimental.noalias.scope.decl(metadata)
declare void @llvm.dbg.value(metadata, metadata, metadata)

!100 = !{!101}
!101 = !{!101, !102}
!102 = !{!102}
;.
; CHECK: [[META0]] = !{[[META1:![0-9]+]]}
; CHECK: [[META1]] = distinct !{[[META1]], [[META2:![0-9]+]]}
; CHECK: [[META2]] = distinct !{[[META2]]}
;.