Skip to content

Commit ce89787

Browse files
Fix OpName and LinkageAttributes decoration of global variables
1 parent 3ed2a81 commit ce89787

File tree

4 files changed

+73
-12
lines changed

4 files changed

+73
-12
lines changed

llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,20 +1841,20 @@ void SPIRVEmitIntrinsics::processGlobalValue(GlobalVariable &GV,
18411841
// Skip special artifical variable llvm.global.annotations.
18421842
if (GV.getName() == "llvm.global.annotations")
18431843
return;
1844-
if (GV.hasInitializer() && !isa<UndefValue>(GV.getInitializer())) {
1844+
Constant *Init = nullptr;
1845+
if (hasInitializer(&GV)) {
18451846
// Deduce element type and store results in Global Registry.
18461847
// Result is ignored, because TypedPointerType is not supported
18471848
// by llvm IR general logic.
18481849
deduceElementTypeHelper(&GV, false);
1849-
Constant *Init = GV.getInitializer();
1850+
Init = GV.getInitializer();
18501851
Type *Ty = isAggrConstForceInt32(Init) ? B.getInt32Ty() : Init->getType();
18511852
Constant *Const = isAggrConstForceInt32(Init) ? B.getInt32(1) : Init;
18521853
auto *InitInst = B.CreateIntrinsic(Intrinsic::spv_init_global,
18531854
{GV.getType(), Ty}, {&GV, Const});
18541855
InitInst->setArgOperand(1, Init);
18551856
}
1856-
if ((!GV.hasInitializer() || isa<UndefValue>(GV.getInitializer())) &&
1857-
GV.getNumUses() == 0)
1857+
if (!Init && GV.getNumUses() == 0)
18581858
B.CreateIntrinsic(Intrinsic::spv_unref_global, GV.getType(), &GV);
18591859
}
18601860

llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3450,7 +3450,7 @@ bool SPIRVInstructionSelector::selectGlobalValue(
34503450
ID = UnnamedGlobalIDs.size();
34513451
GlobalIdent = "__unnamed_" + Twine(ID).str();
34523452
} else {
3453-
GlobalIdent = GV->getGlobalIdentifier();
3453+
GlobalIdent = GV->getName();
34543454
}
34553455

34563456
// Behaviour of functions as operands depends on availability of the
@@ -3506,18 +3506,16 @@ bool SPIRVInstructionSelector::selectGlobalValue(
35063506
auto GlobalVar = cast<GlobalVariable>(GV);
35073507
assert(GlobalVar->getName() != "llvm.global.annotations");
35083508

3509-
bool HasInit = GlobalVar->hasInitializer() &&
3510-
!isa<UndefValue>(GlobalVar->getInitializer());
3511-
// Skip empty declaration for GVs with initilaizers till we get the decl with
3509+
// Skip empty declaration for GVs with initializers till we get the decl with
35123510
// passed initializer.
3513-
if (HasInit && !Init)
3511+
if (hasInitializer(GlobalVar) && !Init)
35143512
return true;
35153513

3516-
bool HasLnkTy = GV->getLinkage() != GlobalValue::InternalLinkage;
3514+
bool HasLnkTy = !GV->hasInternalLinkage() && !GV->hasPrivateLinkage();
35173515
SPIRV::LinkageType::LinkageType LnkType =
3518-
(GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
3516+
GV->isDeclarationForLinker()
35193517
? SPIRV::LinkageType::Import
3520-
: (GV->getLinkage() == GlobalValue::LinkOnceODRLinkage &&
3518+
: (GV->hasLinkOnceODRLinkage() &&
35213519
STI.canUseExtension(SPIRV::Extension::SPV_KHR_linkonce_odr)
35223520
? SPIRV::LinkageType::LinkOnceODR
35233521
: SPIRV::LinkageType::Export);

llvm/lib/Target/SPIRV/SPIRVUtils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/Analysis/LoopInfo.h"
1818
#include "llvm/CodeGen/MachineBasicBlock.h"
1919
#include "llvm/IR/Dominators.h"
20+
#include "llvm/IR/GlobalVariable.h"
2021
#include "llvm/IR/IRBuilder.h"
2122
#include "llvm/IR/TypedPointerType.h"
2223
#include <queue>
@@ -236,6 +237,10 @@ Type *parseBasicTypeName(StringRef &TypeName, LLVMContext &Ctx);
236237
// Returns true if the function was changed.
237238
bool sortBlocks(Function &F);
238239

240+
inline bool hasInitializer(const GlobalVariable *GV) {
241+
return GV->hasInitializer() && !isa<UndefValue>(GV->getInitializer());
242+
}
243+
239244
// True if this is an instance of TypedPointerType.
240245
inline bool isTypedPointerTy(const Type *T) {
241246
return T && T->getTypeID() == Type::TypedPointerTyID;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
; Check names and decoration of global variables.
2+
3+
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
4+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
5+
6+
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s
7+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv32-unknown-unknown %s -o - -filetype=obj | spirv-val %}
8+
9+
; CHECK-DAG: OpName %[[#id18:]] "G1"
10+
; CHECK-DAG: OpName %[[#id22:]] "g1"
11+
; CHECK-DAG: OpName %[[#id23:]] "g2"
12+
; CHECK-DAG: OpName %[[#id27:]] "g4"
13+
; CHECK-DAG: OpName %[[#id30:]] "c1"
14+
; CHECK-DAG: OpName %[[#id31:]] "n_t"
15+
; CHECK-DAG: OpName %[[#id32:]] "w"
16+
; CHECK-DAG: OpName %[[#id34:]] "a.b"
17+
; CHECK-DAG: OpName %[[#id35:]] "e"
18+
; CHECK-DAG: OpName %[[#id36:]] "y.z"
19+
; CHECK-DAG: OpName %[[#id38:]] "x"
20+
21+
; CHECK-DAG: OpDecorate %[[#id18]] Constant
22+
; CHECK-DAG: OpDecorate %[[#id22]] Alignment 4
23+
; CHECK-DAG: OpDecorate %[[#id22]] LinkageAttributes "g1" Export
24+
; CHECK-DAG: OpDecorate %[[#id23]] Alignment 4
25+
; CHECK-DAG: OpDecorate %[[#id27]] Alignment 4
26+
; CHECK-DAG: OpDecorate %[[#id27]] LinkageAttributes "g4" Export
27+
; CHECK-DAG: OpDecorate %[[#id30]] Constant
28+
; CHECK-DAG: OpDecorate %[[#id30]] Alignment 4
29+
; CHECK-DAG: OpDecorate %[[#id30]] LinkageAttributes "c1" Export
30+
; CHECK-DAG: OpDecorate %[[#id31]] Constant
31+
; CHECK-DAG: OpDecorate %[[#id31]] LinkageAttributes "n_t" Import
32+
; CHECK-DAG: OpDecorate %[[#id32]] Constant
33+
; CHECK-DAG: OpDecorate %[[#id32]] Alignment 4
34+
; CHECK-DAG: OpDecorate %[[#id32]] LinkageAttributes "w" Export
35+
; CHECK-DAG: OpDecorate %[[#id34]] Constant
36+
; CHECK-DAG: OpDecorate %[[#id34]] Alignment 4
37+
; CHECK-DAG: OpDecorate %[[#id35]] LinkageAttributes "e" Import
38+
; CHECK-DAG: OpDecorate %[[#id36]] Alignment 4
39+
; CHECK-DAG: OpDecorate %[[#id38]] Constant
40+
; CHECK-DAG: OpDecorate %[[#id38]] Alignment 4
41+
42+
%"class.sycl::_V1::nd_item" = type { i8 }
43+
44+
@G1 = private unnamed_addr addrspace(1) constant %"class.sycl::_V1::nd_item" undef, align 1
45+
@g1 = addrspace(1) global i32 1, align 4
46+
@g2 = internal addrspace(1) global i32 2, align 4
47+
@g4 = common addrspace(1) global i32 0, align 4
48+
@c1 = addrspace(2) constant [2 x i32] [i32 0, i32 1], align 4
49+
@n_t = external addrspace(2) constant [256 x i32]
50+
@w = addrspace(1) constant i32 0, align 4
51+
@a.b = internal addrspace(2) constant [2 x i32] [i32 2, i32 3], align 4
52+
@e = external addrspace(1) global i32
53+
@y.z = internal addrspace(1) global i32 0, align 4
54+
@x = internal addrspace(2) constant float 1.000000e+00, align 4
55+
56+
define internal spir_func void @foo(ptr addrspace(4) align 1 %arg) {
57+
ret void
58+
}

0 commit comments

Comments
 (0)