From 434fed5aff5e62460e2e984c7cc2674c12779b1e Mon Sep 17 00:00:00 2001 From: Rong Xu Date: Tue, 15 Jun 2021 12:32:03 -0700 Subject: [PATCH] [SampleFDO] Using common linkage for the discriminator flag variable We create flag variable "__llvm_fs_discriminator__" in the binary to indicate that FSAFDO hierarchical discriminators are used. This variable might be GC'ed by the linker since it is not explicitly reference. I initially added the var to the use list in pass MIRFSDiscriminator but it did not work. It turned out the used global list is collected in lowering (before MIR pass) and then emitted in the end of pass pipeline. In this patch, we use a "common" linkage for this variable so that it will be GC'ed by the linker. Differential Revision: https://reviews.llvm.org/D103988 --- llvm/lib/CodeGen/MIRFSDiscriminator.cpp | 7 ++++--- llvm/test/CodeGen/X86/fsafdo_test1.ll | 12 ++++-------- llvm/test/CodeGen/X86/fsafdo_test2.ll | 8 ++------ 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/llvm/lib/CodeGen/MIRFSDiscriminator.cpp b/llvm/lib/CodeGen/MIRFSDiscriminator.cpp index b8d7d30..db399a3 100644 --- a/llvm/lib/CodeGen/MIRFSDiscriminator.cpp +++ b/llvm/lib/CodeGen/MIRFSDiscriminator.cpp @@ -132,9 +132,10 @@ bool MIRAddFSDiscriminators::runOnMachineFunction(MachineFunction &MF) { if (!M->getGlobalVariable(FSDiscriminatorVar)) { auto &Context = M->getContext(); // Create a global variable to flag that FSDiscriminators are used. - new GlobalVariable(*M, Type::getInt1Ty(Context), true, - GlobalValue::WeakAnyLinkage, - ConstantInt::getTrue(Context), FSDiscriminatorVar); + // Using "common" linkage so that it will not gc GC'ed. + new GlobalVariable(*M, Type::getInt1Ty(Context), false, + GlobalValue::CommonLinkage, + ConstantInt::getFalse(Context), FSDiscriminatorVar); } LLVM_DEBUG(dbgs() << "Num of FS Discriminators: " << NumNewD << "\n"); diff --git a/llvm/test/CodeGen/X86/fsafdo_test1.ll b/llvm/test/CodeGen/X86/fsafdo_test1.ll index 99c65c9..af6c027 100644 --- a/llvm/test/CodeGen/X86/fsafdo_test1.ll +++ b/llvm/test/CodeGen/X86/fsafdo_test1.ll @@ -1,17 +1,13 @@ ; RUN: llc -enable-fs-discriminator < %s | FileCheck %s ; -; Check that fs-afdo discriminators are generated. +;; Check that fs-afdo discriminators are generated. ; CHECK: .loc 1 7 3 is_stmt 0 discriminator 2 # foo.c:7:3 ; Check: .loc 1 9 5 is_stmt 1 discriminator 2 # foo.c:9:5 ; CHECK: .loc 1 9 5 is_stmt 0 discriminator 268435458 # foo.c:9:5 ; CHECK: .loc 1 7 3 is_stmt 1 discriminator 3892314114 # foo.c:7:3 -; Check that variable __llvm_fs_discriminator__ is generated. -; CHECK: .type __llvm_fs_discriminator__,@object # @__llvm_fs_discriminator__ -; CHECK: .section .rodata,"a",@progbits -; CHECK: .weak __llvm_fs_discriminator__ -; CHECK: __llvm_fs_discriminator__: -; CHECK: .byte 1 -; CHECK: .size __llvm_fs_discriminator__, 1 +;; Check that variable __llvm_fs_discriminator__ is generated. +; CHECK: .type __llvm_fs_discriminator__,@object +; CHECK: .comm __llvm_fs_discriminator__,1,1 target triple = "x86_64-unknown-linux-gnu" diff --git a/llvm/test/CodeGen/X86/fsafdo_test2.ll b/llvm/test/CodeGen/X86/fsafdo_test2.ll index ca26f307..07ea1be 100644 --- a/llvm/test/CodeGen/X86/fsafdo_test2.ll +++ b/llvm/test/CodeGen/X86/fsafdo_test2.ll @@ -43,12 +43,8 @@ ; CHECK: .loc 1 23 9 is_stmt 0 discriminator 3623878657 # unroll.c:23:9 ;; ;; Check that variable __llvm_fs_discriminator__ is generated. -; CHECK: .type __llvm_fs_discriminator__,@object # @__llvm_fs_discriminator__ -; CHECK: .section .rodata,"a",@progbits -; CHECK: .weak __llvm_fs_discriminator__ -; CHECK: __llvm_fs_discriminator__: -; CHECK: .byte 1 -; CHECK: .size __llvm_fs_discriminator__, 1 +; CHECK: .type __llvm_fs_discriminator__,@object +; CHECK: .comm __llvm_fs_discriminator__,1,1 target triple = "x86_64-unknown-linux-gnu" -- 2.7.4