AMDGPU/SI: Correctly emit agent global segment variables when targeting HSA
[oota-llvm.git] / lib / Target / AMDGPU / AMDGPUHSATargetObjectFile.cpp
1 //===-- AMDGPUHSATargetObjectFile.cpp - AMDGPU Object Files ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "AMDGPUHSATargetObjectFile.h"
11 #include "AMDGPU.h"
12 #include "Utils/AMDGPUBaseInfo.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCSectionELF.h"
15 #include "llvm/Support/ELF.h"
16
17 using namespace llvm;
18
19 void AMDGPUHSATargetObjectFile::Initialize(MCContext &Ctx,
20                                            const TargetMachine &TM){
21   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
22   InitializeELF(TM.Options.UseInitArray);
23
24   TextSection = AMDGPU::getHSATextSection(Ctx);
25
26   DataGlobalAgentSection = AMDGPU::getHSADataGlobalAgentSection(Ctx);
27   DataGlobalProgramSection = AMDGPU::getHSADataGlobalProgramSection(Ctx);
28
29 }
30
31 bool AMDGPUHSATargetObjectFile::isAgentAllocationSection(
32     const char *SectionName) const {
33   return cast<MCSectionELF>(DataGlobalAgentSection)
34       ->getSectionName()
35       .equals(SectionName);
36 }
37
38 bool AMDGPUHSATargetObjectFile::isAgentAllocation(const GlobalValue *GV) const {
39   // Read-only segments can only have agent allocation.
40   return AMDGPU::isReadOnlySegment(GV) ||
41          (AMDGPU::isGlobalSegment(GV) && GV->hasSection() &&
42           isAgentAllocationSection(GV->getSection()));
43 }
44
45 bool AMDGPUHSATargetObjectFile::isProgramAllocation(
46     const GlobalValue *GV) const {
47   // The default for global segments is program allocation.
48   return AMDGPU::isGlobalSegment(GV) && !isAgentAllocation(GV);
49 }
50
51 MCSection *AMDGPUHSATargetObjectFile::SelectSectionForGlobal(
52                                         const GlobalValue *GV, SectionKind Kind,
53                                         Mangler &Mang,
54                                         const TargetMachine &TM) const {
55   if (Kind.isText() && !GV->hasComdat())
56     return getTextSection();
57
58   if (AMDGPU::isGlobalSegment(GV)) {
59     if (isAgentAllocation(GV))
60       return DataGlobalAgentSection;
61
62     if (isProgramAllocation(GV))
63       return DataGlobalProgramSection;
64   }
65
66   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang, TM);
67 }