Skip to content

Commit 782dd17

Browse files
authored
[SPIRV] Do not emit @llvm.compiler.used (llvm#162678)
`@llvm.compiler.used` holds a series of global values and prevents the compiler from optimizing them out. However, the symbols in the variable can be optimized after compilation as usual by a linker (notice that `@llvm.used` instead doesn't allow for the linker to optimize-out the global variables referenced in it). This was already done for `@llvm.global.annotations`.
1 parent e9e9ba4 commit 782dd17

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ void SPIRVAsmPrinter::emitEndOfAsmFile(Module &M) {
139139
// anymore.
140140
void SPIRVAsmPrinter::cleanUp(Module &M) {
141141
// Verifier disallows uses of intrinsic global variables.
142-
for (StringRef GVName : {"llvm.global_ctors", "llvm.global_dtors",
143-
"llvm.used", "llvm.compiler.used"}) {
142+
for (StringRef GVName :
143+
{"llvm.global_ctors", "llvm.global_dtors", "llvm.used"}) {
144144
if (GlobalVariable *GV = M.getNamedGlobal(GVName))
145145
GV->setName("");
146146
}

llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "SPIRVTargetMachine.h"
1818
#include "SPIRVUtils.h"
1919
#include "llvm/ADT/DenseSet.h"
20+
#include "llvm/ADT/StringSet.h"
2021
#include "llvm/IR/IRBuilder.h"
2122
#include "llvm/IR/InstIterator.h"
2223
#include "llvm/IR/InstVisitor.h"
@@ -2028,9 +2029,13 @@ Instruction *SPIRVEmitIntrinsics::visitUnreachableInst(UnreachableInst &I) {
20282029

20292030
void SPIRVEmitIntrinsics::processGlobalValue(GlobalVariable &GV,
20302031
IRBuilder<> &B) {
2031-
// Skip special artifical variable llvm.global.annotations.
2032-
if (GV.getName() == "llvm.global.annotations")
2032+
// Skip special artificial variables.
2033+
static const StringSet<> ArtificialGlobals{"llvm.global.annotations",
2034+
"llvm.compiler.used"};
2035+
2036+
if (ArtificialGlobals.contains(GV.getName()))
20332037
return;
2038+
20342039
Constant *Init = nullptr;
20352040
if (hasInitializer(&GV)) {
20362041
// Deduce element type and store results in Global Registry.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: llc -verify-machineinstrs -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
2+
; RUN: %if spirv-tools %{ llc -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}
3+
; RUN: llc -verify-machineinstrs -mtriple=spirv-unknown-vulkan %s -o - | FileCheck %s
4+
; RUN: %if spirv-tools %{ llc -mtriple=spirv-unknown-vulkan %s -o - -filetype=obj | spirv-val %}
5+
6+
; Verify that llvm.compiler.used is not lowered.
7+
; CHECK: OpName %{{[0-9]+}} "unused"
8+
; CHECK-NOT: OpName %{{[0-9]+}} "llvm.compiler.used"
9+
10+
; Check that the type of llvm.compiler.used is not emitted too.
11+
; CHECK-NOT: OpTypeArray
12+
13+
@unused = private addrspace(3) global i32 0
14+
@llvm.compiler.used = appending addrspace(2) global [1 x ptr addrspace (4)] [ptr addrspace(4) addrspacecast (ptr addrspace(3) @unused to ptr addrspace(4))]
15+
16+
define spir_func void @foo() {
17+
entry:
18+
ret void
19+
}

0 commit comments

Comments
 (0)