1*c93fc11fSTom Stellard //===-- AMDGPUHSATargetObjectFile.cpp - AMDGPU Object Files ---------------===//
2*c93fc11fSTom Stellard //
3*c93fc11fSTom Stellard //                     The LLVM Compiler Infrastructure
4*c93fc11fSTom Stellard //
5*c93fc11fSTom Stellard // This file is distributed under the University of Illinois Open Source
6*c93fc11fSTom Stellard // License. See LICENSE.TXT for details.
7*c93fc11fSTom Stellard //
8*c93fc11fSTom Stellard //===----------------------------------------------------------------------===//
9*c93fc11fSTom Stellard 
10*c93fc11fSTom Stellard #include "AMDGPUTargetObjectFile.h"
11*c93fc11fSTom Stellard #include "AMDGPU.h"
12*c93fc11fSTom Stellard #include "Utils/AMDGPUBaseInfo.h"
13*c93fc11fSTom Stellard #include "llvm/MC/MCContext.h"
14*c93fc11fSTom Stellard #include "llvm/MC/MCSectionELF.h"
15*c93fc11fSTom Stellard #include "llvm/Support/ELF.h"
16*c93fc11fSTom Stellard 
17*c93fc11fSTom Stellard using namespace llvm;
18*c93fc11fSTom Stellard 
19*c93fc11fSTom Stellard //===----------------------------------------------------------------------===//
20*c93fc11fSTom Stellard // Generic Object File
21*c93fc11fSTom Stellard //===----------------------------------------------------------------------===//
22*c93fc11fSTom Stellard 
23*c93fc11fSTom Stellard MCSection *AMDGPUTargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
24*c93fc11fSTom Stellard                                                           SectionKind Kind,
25*c93fc11fSTom Stellard                                                           Mangler &Mang,
26*c93fc11fSTom Stellard                                                 const TargetMachine &TM) const {
27*c93fc11fSTom Stellard   if (Kind.isReadOnly() && AMDGPU::isReadOnlySegment(GV))
28*c93fc11fSTom Stellard     return TextSection;
29*c93fc11fSTom Stellard 
30*c93fc11fSTom Stellard   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang, TM);
31*c93fc11fSTom Stellard }
32*c93fc11fSTom Stellard 
33*c93fc11fSTom Stellard //===----------------------------------------------------------------------===//
34*c93fc11fSTom Stellard // HSA Object File
35*c93fc11fSTom Stellard //===----------------------------------------------------------------------===//
36*c93fc11fSTom Stellard 
37*c93fc11fSTom Stellard 
38*c93fc11fSTom Stellard void AMDGPUHSATargetObjectFile::Initialize(MCContext &Ctx,
39*c93fc11fSTom Stellard                                            const TargetMachine &TM){
40*c93fc11fSTom Stellard   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
41*c93fc11fSTom Stellard   InitializeELF(TM.Options.UseInitArray);
42*c93fc11fSTom Stellard 
43*c93fc11fSTom Stellard   TextSection = AMDGPU::getHSATextSection(Ctx);
44*c93fc11fSTom Stellard 
45*c93fc11fSTom Stellard   DataGlobalAgentSection = AMDGPU::getHSADataGlobalAgentSection(Ctx);
46*c93fc11fSTom Stellard   DataGlobalProgramSection = AMDGPU::getHSADataGlobalProgramSection(Ctx);
47*c93fc11fSTom Stellard 
48*c93fc11fSTom Stellard   RodataReadonlyAgentSection = AMDGPU::getHSARodataReadonlyAgentSection(Ctx);
49*c93fc11fSTom Stellard }
50*c93fc11fSTom Stellard 
51*c93fc11fSTom Stellard bool AMDGPUHSATargetObjectFile::isAgentAllocationSection(
52*c93fc11fSTom Stellard     const char *SectionName) const {
53*c93fc11fSTom Stellard   return cast<MCSectionELF>(DataGlobalAgentSection)
54*c93fc11fSTom Stellard       ->getSectionName()
55*c93fc11fSTom Stellard       .equals(SectionName);
56*c93fc11fSTom Stellard }
57*c93fc11fSTom Stellard 
58*c93fc11fSTom Stellard bool AMDGPUHSATargetObjectFile::isAgentAllocation(const GlobalValue *GV) const {
59*c93fc11fSTom Stellard   // Read-only segments can only have agent allocation.
60*c93fc11fSTom Stellard   return AMDGPU::isReadOnlySegment(GV) ||
61*c93fc11fSTom Stellard          (AMDGPU::isGlobalSegment(GV) && GV->hasSection() &&
62*c93fc11fSTom Stellard           isAgentAllocationSection(GV->getSection()));
63*c93fc11fSTom Stellard }
64*c93fc11fSTom Stellard 
65*c93fc11fSTom Stellard bool AMDGPUHSATargetObjectFile::isProgramAllocation(
66*c93fc11fSTom Stellard     const GlobalValue *GV) const {
67*c93fc11fSTom Stellard   // The default for global segments is program allocation.
68*c93fc11fSTom Stellard   return AMDGPU::isGlobalSegment(GV) && !isAgentAllocation(GV);
69*c93fc11fSTom Stellard }
70*c93fc11fSTom Stellard 
71*c93fc11fSTom Stellard MCSection *AMDGPUHSATargetObjectFile::SelectSectionForGlobal(
72*c93fc11fSTom Stellard                                         const GlobalValue *GV, SectionKind Kind,
73*c93fc11fSTom Stellard                                         Mangler &Mang,
74*c93fc11fSTom Stellard                                         const TargetMachine &TM) const {
75*c93fc11fSTom Stellard   if (Kind.isText() && !GV->hasComdat())
76*c93fc11fSTom Stellard     return getTextSection();
77*c93fc11fSTom Stellard 
78*c93fc11fSTom Stellard   if (AMDGPU::isGlobalSegment(GV)) {
79*c93fc11fSTom Stellard     if (isAgentAllocation(GV))
80*c93fc11fSTom Stellard       return DataGlobalAgentSection;
81*c93fc11fSTom Stellard 
82*c93fc11fSTom Stellard     if (isProgramAllocation(GV))
83*c93fc11fSTom Stellard       return DataGlobalProgramSection;
84*c93fc11fSTom Stellard   }
85*c93fc11fSTom Stellard 
86*c93fc11fSTom Stellard   if (Kind.isReadOnly() && AMDGPU::isReadOnlySegment(GV))
87*c93fc11fSTom Stellard     return RodataReadonlyAgentSection;
88*c93fc11fSTom Stellard 
89*c93fc11fSTom Stellard   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang, TM);
90*c93fc11fSTom Stellard }
91