Skip to content

Commit df76318

Browse files
committedJan 30, 2018
Teach ValueMapper to use ODR uniqued types when available
Summary: This is exposed during ThinLTO compilation, when we import an alias by creating a clone of the aliasee. Without this fix the debug type is unnecessarily cloned and we get a duplicate, undoing the uniquing. Fixes PR36089. Reviewers: mehdi_amini, pcc Subscribers: eraman, JDevlieghere, llvm-commits Differential Revision: https://reviews.llvm.org/D41669 llvm-svn: 323813
1 parent cca341b commit df76318

File tree

3 files changed

+116
-4
lines changed

3 files changed

+116
-4
lines changed
 

‎llvm/lib/Transforms/Utils/ValueMapper.cpp

+15-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/IR/CallSite.h"
2626
#include "llvm/IR/Constant.h"
2727
#include "llvm/IR/Constants.h"
28+
#include "llvm/IR/DebugInfoMetadata.h"
2829
#include "llvm/IR/DerivedTypes.h"
2930
#include "llvm/IR/Function.h"
3031
#include "llvm/IR/GlobalAlias.h"
@@ -536,13 +537,23 @@ Optional<Metadata *> MDNodeMapper::tryToMapOperand(const Metadata *Op) {
536537
return None;
537538
}
538539

540+
static Metadata *cloneOrBuildODR(const MDNode &N) {
541+
auto *CT = dyn_cast<DICompositeType>(&N);
542+
// If ODR type uniquing is enabled, we would have uniqued composite types
543+
// with identifiers during bitcode reading, so we can just use CT.
544+
if (CT && CT->getContext().isODRUniquingDebugTypes() &&
545+
CT->getIdentifier() != "")
546+
return const_cast<DICompositeType *>(CT);
547+
return MDNode::replaceWithDistinct(N.clone());
548+
}
549+
539550
MDNode *MDNodeMapper::mapDistinctNode(const MDNode &N) {
540551
assert(N.isDistinct() && "Expected a distinct node");
541552
assert(!M.getVM().getMappedMD(&N) && "Expected an unmapped node");
542-
DistinctWorklist.push_back(cast<MDNode>(
543-
(M.Flags & RF_MoveDistinctMDs)
544-
? M.mapToSelf(&N)
545-
: M.mapToMetadata(&N, MDNode::replaceWithDistinct(N.clone()))));
553+
DistinctWorklist.push_back(
554+
cast<MDNode>((M.Flags & RF_MoveDistinctMDs)
555+
? M.mapToSelf(&N)
556+
: M.mapToMetadata(&N, cloneOrBuildODR(N))));
546557
return DistinctWorklist.back();
547558
}
548559

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
2+
target triple = "x86_64-scei-ps4"
3+
4+
%struct.CFVS = type { %struct.Vec }
5+
%struct.Vec = type { i8 }
6+
%struct.S = type { i8 }
7+
8+
@_ZN4CFVSD1Ev = alias void (%struct.CFVS*), void (%struct.CFVS*)* @_ZN4CFVSD2Ev
9+
10+
define void @_ZN4CFVSD2Ev(%struct.CFVS* %this) unnamed_addr align 2 !dbg !8 {
11+
entry:
12+
%this.addr = alloca %struct.CFVS*, align 8
13+
store %struct.CFVS* %this, %struct.CFVS** %this.addr, align 8
14+
%this1 = load %struct.CFVS*, %struct.CFVS** %this.addr, align 8
15+
%m_val = getelementptr inbounds %struct.CFVS, %struct.CFVS* %this1, i32 0, i32 0
16+
ret void
17+
}
18+
19+
declare dereferenceable(1) %struct.S* @_Z3Getv()
20+
21+
!llvm.dbg.cu = !{!0}
22+
!llvm.module.flags = !{!3, !4, !5, !6}
23+
24+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 6.0.0 (trunk 321360) (llvm/trunk 321359)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
25+
!1 = !DIFile(filename: "bz188598-b.cpp", directory: "")
26+
!2 = !{}
27+
!3 = !{i32 2, !"Dwarf Version", i32 4}
28+
!4 = !{i32 2, !"Debug Info Version", i32 3}
29+
!5 = !{i32 1, !"wchar_size", i32 2}
30+
!6 = !{i32 7, !"PIC Level", i32 2}
31+
!8 = distinct !DISubprogram(name: "~CFVS", linkageName: "_ZN4CFVSD2Ev", scope: !9, file: !1, line: 2, type: !28, isLocal: false, isDefinition: true, scopeLine: 2, flags: DIFlagPrototyped, isOptimized: false, unit: !0, declaration: !27, variables: !2)
32+
!9 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "CFVS", file: !10, line: 7, size: 8, elements: !11, identifier: "_ZTS4CFVS")
33+
!10 = !DIFile(filename: "./bz188598.h", directory: "")
34+
!11 = !{!35}
35+
!27 = !DISubprogram(name: "~CFVS", scope: !9, file: !10, line: 8, type: !28, isLocal: false, isDefinition: false, scopeLine: 8, flags: DIFlagPrototyped, isOptimized: false)
36+
!28 = !DISubroutineType(types: !29)
37+
!29 = !{null, !30}
38+
!30 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !9, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer)
39+
!35 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
; RUN: opt -module-summary -o %t1.bc %s
2+
; RUN: opt -module-summary -o %t2.bc %S/Inputs/dicompositetype-unique-alias.ll
3+
; RUN: llvm-lto --thinlto-action=run %t1.bc %t2.bc -thinlto-save-temps=%t3.
4+
; RUN: llvm-dis %t3.0.3.imported.bc -o - | FileCheck %s
5+
; RUN: llvm-lto2 run %t1.bc %t2.bc -o %t --save-temps \
6+
; RUN: -r %t1.bc,_ZN1CD2Ev,pl \
7+
; RUN: -r %t1.bc,_ZN4CFVSD1Ev,l \
8+
; RUN: -r %t1.bc,_ZN4CFVSD2Ev,l \
9+
; RUN: -r %t1.bc,_Z3Getv,l \
10+
; RUN: -r %t2.bc,_ZN4CFVSD1Ev,pl \
11+
; RUN: -r %t2.bc,_ZN4CFVSD2Ev,pl \
12+
; RUN: -r %t2.bc,_Z3Getv,l
13+
; RUN: llvm-dis %t.1.3.import.bc -o - | FileCheck %s
14+
15+
; Only llvm-lto2 adds the dso_local keyword, hence the {{.*}}
16+
; CHECK: define available_externally{{.*}} void @_ZN4CFVSD1Ev
17+
18+
; Confirm that we only have a single DICompositeType after importing
19+
; both an alias and its aliasee, since ODR Type Uniquing is enabled.
20+
; CHECK: DICompositeType
21+
; CHECK-NOT: DICompositeType
22+
23+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
24+
target triple = "x86_64-scei-ps4"
25+
26+
%class.C = type <{ i32 (...)**, %class.A, %struct.CFVS, [6 x i8] }>
27+
%class.A = type { %struct.Vec }
28+
%struct.Vec = type { i8 }
29+
%struct.CFVS = type { %struct.Vec }
30+
%struct.S = type { i8 }
31+
32+
define void @_ZN1CD2Ev(%class.C* %this) unnamed_addr align 2 {
33+
entry:
34+
%this.addr = alloca %class.C*, align 8
35+
%this1 = load %class.C*, %class.C** %this.addr, align 8
36+
%m = getelementptr inbounds %class.C, %class.C* %this1, i32 0, i32 2
37+
call void @_ZN4CFVSD1Ev(%struct.CFVS* %m), !dbg !50
38+
call void @_ZN4CFVSD2Ev(%struct.CFVS* %m), !dbg !50
39+
ret void
40+
}
41+
42+
declare void @_ZN4CFVSD1Ev(%struct.CFVS*) unnamed_addr
43+
declare void @_ZN4CFVSD2Ev(%struct.CFVS*) unnamed_addr
44+
45+
declare dereferenceable(1) %struct.S* @_Z3Getv()
46+
47+
!llvm.dbg.cu = !{!0}
48+
!llvm.module.flags = !{!3, !4, !5, !6}
49+
50+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 6.0.0 (trunk 321360) (llvm/trunk 321359)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
51+
!1 = !DIFile(filename: "bz188598-a.cpp", directory: ".")
52+
!2 = !{}
53+
!3 = !{i32 2, !"Dwarf Version", i32 4}
54+
!4 = !{i32 2, !"Debug Info Version", i32 3}
55+
!5 = !{i32 1, !"wchar_size", i32 2}
56+
!6 = !{i32 7, !"PIC Level", i32 2}
57+
!8 = distinct !DISubprogram(name: "~C", linkageName: "_ZN1CD2Ev", scope: !1, file: !1, line: 9, type: !47, isLocal: false, isDefinition: true, scopeLine: 9, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2)
58+
!47 = !DISubroutineType(types: !48)
59+
!48 = !{!55}
60+
!50 = !DILocation(line: 9, scope: !51)
61+
!51 = distinct !DILexicalBlock(scope: !8, file: !1, line: 9)
62+
!55 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)

0 commit comments

Comments
 (0)