Skip to content

Commit b8df4ea

Browse files
committed
[llvm][llvm-dis] Fix 'llvm-dis' with '--materialize-metadata --show-annotations' crashes
Added handling the case of a non-materialized module
1 parent 0400b9a commit b8df4ea

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

llvm/lib/IR/AsmWriter.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2931,7 +2931,7 @@ class AssemblyWriter {
29312931

29322932
// printInfoComment - Print a little comment after the instruction indicating
29332933
// which slot it occupies.
2934-
void printInfoComment(const Value &V);
2934+
void printInfoComment(const Value &V, bool isMaterializable = false);
29352935

29362936
// printGCRelocateComment - print comment after call to the gc.relocate
29372937
// intrinsic indicating base and derived pointer names.
@@ -3963,7 +3963,7 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
39633963
if (Attrs.hasAttributes())
39643964
Out << " #" << Machine.getAttributeGroupSlot(Attrs);
39653965

3966-
printInfoComment(*GV);
3966+
printInfoComment(*GV, GV->isMaterializable());
39673967
}
39683968

39693969
void AssemblyWriter::printAlias(const GlobalAlias *GA) {
@@ -4001,7 +4001,7 @@ void AssemblyWriter::printAlias(const GlobalAlias *GA) {
40014001
Out << '"';
40024002
}
40034003

4004-
printInfoComment(*GA);
4004+
printInfoComment(*GA, GA->isMaterializable());
40054005
Out << '\n';
40064006
}
40074007

@@ -4040,7 +4040,7 @@ void AssemblyWriter::printIFunc(const GlobalIFunc *GI) {
40404040
printMetadataAttachments(MDs, ", ");
40414041
}
40424042

4043-
printInfoComment(*GI);
4043+
printInfoComment(*GI, GI->isMaterializable());
40444044
Out << '\n';
40454045
}
40464046

@@ -4319,13 +4319,12 @@ void AssemblyWriter::printGCRelocateComment(const GCRelocateInst &Relocate) {
43194319

43204320
/// printInfoComment - Print a little comment after the instruction indicating
43214321
/// which slot it occupies.
4322-
void AssemblyWriter::printInfoComment(const Value &V) {
4322+
void AssemblyWriter::printInfoComment(const Value &V, bool isMaterializable) {
43234323
if (const auto *Relocate = dyn_cast<GCRelocateInst>(&V))
43244324
printGCRelocateComment(*Relocate);
43254325

4326-
if (AnnotationWriter) {
4326+
if (AnnotationWriter && !isMaterializable)
43274327
AnnotationWriter->printInfoComment(V, Out);
4328-
}
43294328

43304329
if (PrintInstDebugLocs) {
43314330
if (auto *I = dyn_cast<Instruction>(&V)) {
Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
; RUN: llvm-as < %s | llvm-dis --materialize-metadata --show-annotations | FileCheck %s
22

3+
; CHECK: @global_var = global i32 1
4+
; CHECK: @alias = alias i32, ptr @global_var
5+
; CHECK: @ifunc = ifunc i32 (), ptr @ifunc_resolver
6+
@global_var = global i32 1
7+
@alias = alias i32, ptr @global_var
8+
@ifunc = ifunc i32 (), ptr @ifunc_resolver
9+
10+
; CHECK: ; Materializable
11+
; CHECK-NEXT: define ptr @ifunc_resolver() {}
12+
define ptr @ifunc_resolver() {
13+
ret ptr @defined_function
14+
}
15+
316
; CHECK: ; Materializable
4-
; CHECK-NEXT: define dso_local i32 @test() {}
5-
define dso_local i32 @test() {
6-
entry:
7-
ret i32 0
17+
; CHECK-NEXT: define void @defined_function() {}
18+
define void @defined_function() {
19+
ret void
820
}
921

22+
; CHECK: declare void @declared_function()
23+
declare void @declared_function()

llvm/tools/llvm-dis/llvm-dis.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,28 @@ static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
101101
}
102102
}
103103
class CommentWriter : public AssemblyAnnotationWriter {
104+
private:
105+
bool canSafelyAccessUses(const Value &V) {
106+
// Can't safely access uses, if module not materialized.
107+
if (!isa<GlobalValue>(&V))
108+
return true;
109+
const GlobalValue *GV = cast<GlobalValue>(&V);
110+
return GV->getParent() && GV->getParent()->isMaterialized();
111+
}
112+
104113
public:
105114
void emitFunctionAnnot(const Function *F,
106115
formatted_raw_ostream &OS) override {
116+
if (!canSafelyAccessUses(*F))
117+
return;
118+
107119
OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses
108120
OS << '\n';
109121
}
110122
void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
123+
if (!canSafelyAccessUses(V))
124+
return;
125+
111126
bool Padded = false;
112127
if (!V.getType()->isVoidTy()) {
113128
OS.PadToColumn(50);

0 commit comments

Comments
 (0)