[llvm-reduce] ReduceGlobalVarInitializers delta pass: fix handling of globals w/...
authorRoman Lebedev <lebedev.ri@gmail.com>
Thu, 7 Jan 2021 14:28:25 +0000 (17:28 +0300)
committerRoman Lebedev <lebedev.ri@gmail.com>
Thu, 7 Jan 2021 15:05:03 +0000 (18:05 +0300)
Much like with ReduceFunctionBodies delta pass,
we need to remove comdat and set linkage to external,
else verifier will complain, and our deltas are invalid.

llvm/test/Reduce/remove-global-vars.ll
llvm/tools/llvm-reduce/deltas/ReduceGlobalVarInitializers.cpp

index d078cad..e791fd5 100644 (file)
@@ -4,16 +4,25 @@
 ; RUN: llvm-reduce --test FileCheck --test-arg --check-prefixes=CHECK-ALL,CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
 ; RUN: cat %t | FileCheck --check-prefixes=CHECK-ALL,CHECK-FINAL --implicit-check-not=uninteresting %s
 
+$interesting5 = comdat any
+
 ; CHECK-INTERESTINGNESS: @interesting = {{.*}}global i32{{.*}}, align 4
 ; CHECK-INTERESTINGNESS: @interesting2 = global i32 0, align 4
 ; CHECK-INTERESTINGNESS: @interesting3 = {{.*}}global i32{{.*}}, align 4
+; CHECK-INTERESTINGNESS: @interesting4 = {{.*}}constant i32{{.*}}, align 4
+; CHECK-INTERESTINGNESS: @interesting5 = {{.*}}global i32{{.*}}, align 4
 
 ; CHECK-FINAL: @interesting = external global i32, align 4
 ; CHECK-FINAL: @interesting2 = global i32 0, align 4
 ; CHECK-FINAL: @interesting3 = external global i32, align 4
+; CHECK-FINAL: @interesting4 = external dso_local constant i32, align 4
+; CHECK-FINAL: @interesting5 = external global i32, align 4
 @interesting = global i32 0, align 4
 @interesting2 = global i32 0, align 4
 @interesting3 = external global i32, align 4
+@interesting4 = private constant i32 2, align 4
+@interesting5 = global i32 2, align 4, comdat
+
 @uninteresting = global i32 1, align 4
 @uninteresting2 = external global i32, align 4
 
index 1128710..fd5a5d1 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "ReduceGlobalVarInitializers.h"
 #include "llvm/IR/Constants.h"
+#include "llvm/IR/GlobalValue.h"
 
 using namespace llvm;
 
@@ -23,8 +24,11 @@ static void extractGVsFromModule(std::vector<Chunk> ChunksToKeep,
 
   // Drop initializers of out-of-chunk GVs
   for (auto &GV : Program->globals())
-    if (GV.hasInitializer() && !O.shouldKeep())
+    if (GV.hasInitializer() && !O.shouldKeep()) {
       GV.setInitializer(nullptr);
+      GV.setLinkage(GlobalValue::LinkageTypes::ExternalLinkage);
+      GV.setComdat(nullptr);
+    }
 }
 
 /// Counts the amount of initialized GVs and displays their