Skip to content

Commit

Permalink
[NVPTX] remove unnecessary named metadata update that happens to brea…
Browse files Browse the repository at this point in the history
…k debug info.

Also added test case to verify IR changes done by NVPTXGenericToNVVM pass.

Differential Revision: https://reviews.llvm.org/D22837

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@277520 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Artem-B committed Aug 2, 2016
1 parent ba9543c commit 23d7717
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 36 deletions.
36 changes: 0 additions & 36 deletions lib/Target/NVPTX/NVPTXGenericToNVVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class GenericToNVVM : public ModulePass {
IRBuilder<> &Builder);
Value *remapConstantExpr(Module *M, Function *F, ConstantExpr *C,
IRBuilder<> &Builder);
void remapNamedMDNode(ValueToValueMapTy &VM, NamedMDNode *N);

typedef ValueMap<GlobalVariable *, GlobalVariable *> GVMapTy;
typedef ValueMap<Constant *, Value *> ConstantToValueMapTy;
Expand Down Expand Up @@ -130,12 +129,6 @@ bool GenericToNVVM::runOnModule(Module &M) {
for (auto I = GVMap.begin(), E = GVMap.end(); I != E; ++I)
VM[I->first] = I->second;

// Walk through the metadata section and update the debug information
// associated with the global variables in the default address space.
for (NamedMDNode &I : M.named_metadata()) {
remapNamedMDNode(VM, &I);
}

// Walk through the global variable initializers, and replace any use of
// original global variables in GVMap with a use of the corresponding copies
// in GVMap. The copies need to be bitcast to the original global variable
Expand Down Expand Up @@ -360,32 +353,3 @@ Value *GenericToNVVM::remapConstantExpr(Module *M, Function *F, ConstantExpr *C,
llvm_unreachable("GenericToNVVM encountered an unsupported ConstantExpr");
}
}

void GenericToNVVM::remapNamedMDNode(ValueToValueMapTy &VM, NamedMDNode *N) {

bool OperandChanged = false;
SmallVector<MDNode *, 16> NewOperands;
unsigned NumOperands = N->getNumOperands();

// Check if any operand is or contains a global variable in GVMap, and thus
// converted to another value.
for (unsigned i = 0; i < NumOperands; ++i) {
MDNode *Operand = N->getOperand(i);
MDNode *NewOperand = MapMetadata(Operand, VM);
OperandChanged |= Operand != NewOperand;
NewOperands.push_back(NewOperand);
}

// If none of the operands has been modified, return immediately.
if (!OperandChanged) {
return;
}

// Replace the old operands with the new operands.
N->dropAllReferences();
for (SmallVectorImpl<MDNode *>::iterator I = NewOperands.begin(),
E = NewOperands.end();
I != E; ++I) {
N->addOperand(*I);
}
}
64 changes: 64 additions & 0 deletions test/CodeGen/NVPTX/generic-to-nvvm-ir.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
; Verify functionality of NVPTXGenericToNVVM.cpp pass.
;
; RUN: opt < %s -march nvptx64 -S -generic-to-nvvm -verify-debug-info | FileCheck %s

target datalayout = "e-i64:64-v16:16-v32:32-n16:32:64"
target triple = "nvptx64-nvidia-cuda"

; Generic space variables should be converted to global space AKA addrspace(1).
; CHECK-DAG: @static_var = {{.*}}addrspace(1)
@static_var = externally_initialized global i8 0, align 1
; CHECK-DAG: @.str = {{.*}}addrspace(1)
@.str = private unnamed_addr constant [4 x i8] c"XXX\00", align 1

; Function Attrs: convergent
define void @func() !dbg !8 {
;CHECK-LABEL: @func()
;CHECK-SAME: !dbg [[FUNCNODE:![0-9]+]]
entry:
; References to the variables must be converted back to generic address space via llvm intrinsic call
; CHECK-DAG: call i8* @llvm.nvvm.ptr.global.to.gen.p0i8.p1i8({{.*}} addrspace(1)* @.str
%0 = load i8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), align 1
call void @extfunc(i8 signext %0)
; CHECK-DAG: call i8* @llvm.nvvm.ptr.global.to.gen.p0i8.p1i8(i8 addrspace(1)* @static_var
%1 = load i8, i8* @static_var, align 1
call void @extfunc(i8 signext %1)
ret void
; CHECK: ret void
}

declare void @extfunc(i8 signext)

!llvm.dbg.cu = !{!0}
; CHECK: !llvm.dbg.cu = !{[[CUNODE:![0-9]+]]}
!llvm.module.flags = !{!6, !7}

!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1,
producer: "clang version 4.0.0",
isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !3)
; CHECK: [[CUNODE]] = distinct !DICompileUnit({{.*}} globals: [[GLOBALSNODE:![0-9]+]]
!1 = !DIFile(filename: "foo.cu", directory: "/usr/local/google/home/tra/work/llvm/build/gpu/debug")
!2 = !{}
!3 = !{!4}
; Find list of global variables and make sure it's the one used by DICompileUnit
; CHECK: [[GLOBALSNODE]] = !{[[GVNODE:![0-9]+]]}
!4 = distinct !DIGlobalVariable(name: "static_var", scope: !0, file: !1, line: 2, type: !5, isLocal: false,
isDefinition: true, variable: i8* @static_var)
; Debug info must also be updated to reflect new address space.
; CHECK: [[GVNODE]] = distinct !DIGlobalVariable(name: "static_var"
; CHECK-SAME: scope: [[CUNODE]]
; CHECK-SAME: type: [[TYPENODE:![0-9]+]]
; CHECK-SAME: variable: i8* addrspacecast (i8 addrspace(1)* @static_var to i8*)
!5 = !DIBasicType(name: "char", size: 8, align: 8, encoding: DW_ATE_signed_char)
; CHECK: [[TYPENODE]] = !DIBasicType(name: "char"
!6 = !{i32 2, !"Dwarf Version", i32 4}
!7 = !{i32 2, !"Debug Info Version", i32 3}
!8 = distinct !DISubprogram(name: "foo", linkageName: "func",
scope: !1, file: !1, line: 3, type: !9, isLocal: false, isDefinition: true, scopeLine: 3,
flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2)
; CHECK: [[FUNCNODE]] = distinct !DISubprogram(name: "foo",
; CHECK-SAME: type: [[STYPENODE:![0-9]+]]
; CHECK-SAME: unit: [[CUNODE]],
!9 = !DISubroutineType(types: !10)
; CHECK: [[STYPENODE]] = !DISubroutineType
!10 = !{null}

0 comments on commit 23d7717

Please sign in to comment.