Skip to content

Commit caa2a82

Browse files
committed
[MergeFunctions] Preserve symbols used llvm.used/llvm.compiler.used
llvm.used and llvm.compiler.used are often used with inline assembly that refers to a specific symbol so that the symbol is kept through to the linker even though there are no references to it from LLVM IR. This fixes the MergeFunctions pass to preserve references to these symbols in llvm.used/llvm.compiler.used so they are not deleted from the IR. This doesn't prevent these functions from being merged, but guarantees that an alias or thunk with the expected symbol name is kept in the IR. Differential Revision: https://reviews.llvm.org/D127751
1 parent decb600 commit caa2a82

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

llvm/lib/Transforms/IPO/MergeFunctions.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
#include "llvm/Support/raw_ostream.h"
121121
#include "llvm/Transforms/IPO.h"
122122
#include "llvm/Transforms/Utils/FunctionComparator.h"
123+
#include "llvm/Transforms/Utils/ModuleUtils.h"
123124
#include <algorithm>
124125
#include <cassert>
125126
#include <iterator>
@@ -225,6 +226,9 @@ class MergeFunctions {
225226
/// analyzed again.
226227
std::vector<WeakTrackingVH> Deferred;
227228

229+
/// Set of values marked as used in llvm.used and llvm.compiler.used.
230+
SmallPtrSet<GlobalValue *, 4> Used;
231+
228232
#ifndef NDEBUG
229233
/// Checks the rules of order relation introduced among functions set.
230234
/// Returns true, if check has been passed, and false if failed.
@@ -407,6 +411,11 @@ static bool isEligibleForMerging(Function &F) {
407411
bool MergeFunctions::runOnModule(Module &M) {
408412
bool Changed = false;
409413

414+
SmallVector<GlobalValue *, 4> UsedV;
415+
collectUsedGlobalVariables(M, UsedV, /*CompilerUsed=*/false);
416+
collectUsedGlobalVariables(M, UsedV, /*CompilerUsed=*/true);
417+
Used.insert(UsedV.begin(), UsedV.end());
418+
410419
// All functions in the module, ordered by hash. Functions with a unique
411420
// hash value are easily eliminated.
412421
std::vector<std::pair<FunctionComparator::FunctionHash, Function *>>
@@ -453,6 +462,7 @@ bool MergeFunctions::runOnModule(Module &M) {
453462
FnTree.clear();
454463
FNodesInTree.clear();
455464
GlobalNumbers.clear();
465+
Used.clear();
456466

457467
return Changed;
458468
}
@@ -825,7 +835,10 @@ void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) {
825835
// For better debugability, under MergeFunctionsPDI, we do not modify G's
826836
// call sites to point to F even when within the same translation unit.
827837
if (!G->isInterposable() && !MergeFunctionsPDI) {
828-
if (G->hasGlobalUnnamedAddr()) {
838+
// Functions referred to by llvm.used/llvm.compiler.used are special:
839+
// there are uses of the symbol name that are not visible to LLVM,
840+
// usually from inline asm.
841+
if (G->hasGlobalUnnamedAddr() && !Used.contains(G)) {
829842
// G might have been a key in our GlobalNumberState, and it's illegal
830843
// to replace a key in ValueMap<GlobalValue *> with a non-global.
831844
GlobalNumbers.erase(G);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
; RUN: opt -S -mergefunc < %s | FileCheck %s
2+
3+
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (i32)* @a to i8*)], section "llvm.metadata"
4+
5+
define internal i32 @a(i32 %a) unnamed_addr {
6+
%b = xor i32 %a, 0
7+
%c = xor i32 %b, 0
8+
ret i32 %c
9+
}
10+
11+
define i32 @b(i32 %a) unnamed_addr {
12+
%b = xor i32 %a, 0
13+
%c = xor i32 %b, 0
14+
ret i32 %c
15+
}
16+
17+
define i32 @c(i32 %a) unnamed_addr {
18+
%b = tail call i32 @a(i32 %a)
19+
ret i32 %b
20+
}
21+
22+
; CHECK-LABEL: @llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (i32)* @a to i8*)], section "llvm.metadata"
23+
24+
; CHECK-LABEL: define i32 @b(i32 %a) unnamed_addr
25+
; CHECK-NEXT: xor
26+
; CHECK-NEXT: xor
27+
; CHECK-NEXT: ret
28+
29+
; CHECK-LABEL: define i32 @c(i32 %a) unnamed_addr
30+
; CHECK-NEXT: tail call i32 @b(i32 %a)
31+
; CHECK-NEXT: ret
32+
33+
; CHECK-LABEL: define internal i32 @a(i32 %0) unnamed_addr
34+
; CHECK-NEXT: tail call i32 @b(i32 %0)
35+
; CHECK-NEXT: ret

0 commit comments

Comments
 (0)