1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-18 18:42:46 +02:00

[Cloning] Copy metadata of global declarations

We have modules with metadata on declarations, and out-of-tree passes
use that metadata, and we need to clone those modules. We really expect
such metadata is kept during the clone operation.

Reviewed by: arsenm, aprantl

Differential Revision: https://reviews.llvm.org/D93451
This commit is contained in:
Ruiling Song 2020-12-17 08:03:20 +08:00
parent da94ce92ed
commit d4718580b5
2 changed files with 18 additions and 7 deletions

View File

@ -117,10 +117,17 @@ std::unique_ptr<Module> llvm::CloneModule(
// //
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
I != E; ++I) { I != E; ++I) {
GlobalVariable *GV = cast<GlobalVariable>(VMap[&*I]);
SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
I->getAllMetadata(MDs);
for (auto MD : MDs)
GV->addMetadata(MD.first,
*MapMetadata(MD.second, VMap, RF_MoveDistinctMDs));
if (I->isDeclaration()) if (I->isDeclaration())
continue; continue;
GlobalVariable *GV = cast<GlobalVariable>(VMap[&*I]);
if (!ShouldCloneDefinition(&*I)) { if (!ShouldCloneDefinition(&*I)) {
// Skip after setting the correct linkage for an external reference. // Skip after setting the correct linkage for an external reference.
GV->setLinkage(GlobalValue::ExternalLinkage); GV->setLinkage(GlobalValue::ExternalLinkage);
@ -129,12 +136,6 @@ std::unique_ptr<Module> llvm::CloneModule(
if (I->hasInitializer()) if (I->hasInitializer())
GV->setInitializer(MapValue(I->getInitializer(), VMap)); GV->setInitializer(MapValue(I->getInitializer(), VMap));
SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
I->getAllMetadata(MDs);
for (auto MD : MDs)
GV->addMetadata(MD.first,
*MapMetadata(MD.second, VMap, RF_MoveDistinctMDs));
copyComdat(GV, &*I); copyComdat(GV, &*I);
} }

View File

@ -0,0 +1,10 @@
; RUN: opt -run-twice -verify -S -o - %s | FileCheck %s
; This test is used to check metadata attached to global variable declarations
; are copied when CloneModule(). This is required by out-of-tree passes.
; CHECK: @g = external addrspace(64) global i32, !spirv.InOut !0
@g = external addrspace(64) global i32, !spirv.InOut !0
!0 = !{i32 1}