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 "AMDGPUTargetObjectFile.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 //===----------------------------------------------------------------------===//
20 // Generic Object File
21 //===----------------------------------------------------------------------===//
22 
SelectSectionForGlobal(const GlobalValue * GV,SectionKind Kind,Mangler & Mang,const TargetMachine & TM) const23 MCSection *AMDGPUTargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
24                                                           SectionKind Kind,
25                                                           Mangler &Mang,
26                                                 const TargetMachine &TM) const {
27   if (Kind.isReadOnly() && AMDGPU::isReadOnlySegment(GV))
28     return TextSection;
29 
30   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang, TM);
31 }
32 
33 //===----------------------------------------------------------------------===//
34 // HSA Object File
35 //===----------------------------------------------------------------------===//
36 
37 
Initialize(MCContext & Ctx,const TargetMachine & TM)38 void AMDGPUHSATargetObjectFile::Initialize(MCContext &Ctx,
39                                            const TargetMachine &TM){
40   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
41   InitializeELF(TM.Options.UseInitArray);
42 
43   TextSection = AMDGPU::getHSATextSection(Ctx);
44 
45   DataGlobalAgentSection = AMDGPU::getHSADataGlobalAgentSection(Ctx);
46   DataGlobalProgramSection = AMDGPU::getHSADataGlobalProgramSection(Ctx);
47 
48   RodataReadonlyAgentSection = AMDGPU::getHSARodataReadonlyAgentSection(Ctx);
49 }
50 
isAgentAllocationSection(const char * SectionName) const51 bool AMDGPUHSATargetObjectFile::isAgentAllocationSection(
52     const char *SectionName) const {
53   return cast<MCSectionELF>(DataGlobalAgentSection)
54       ->getSectionName()
55       .equals(SectionName);
56 }
57 
isAgentAllocation(const GlobalValue * GV) const58 bool AMDGPUHSATargetObjectFile::isAgentAllocation(const GlobalValue *GV) const {
59   // Read-only segments can only have agent allocation.
60   return AMDGPU::isReadOnlySegment(GV) ||
61          (AMDGPU::isGlobalSegment(GV) && GV->hasSection() &&
62           isAgentAllocationSection(GV->getSection()));
63 }
64 
isProgramAllocation(const GlobalValue * GV) const65 bool AMDGPUHSATargetObjectFile::isProgramAllocation(
66     const GlobalValue *GV) const {
67   // The default for global segments is program allocation.
68   return AMDGPU::isGlobalSegment(GV) && !isAgentAllocation(GV);
69 }
70 
SelectSectionForGlobal(const GlobalValue * GV,SectionKind Kind,Mangler & Mang,const TargetMachine & TM) const71 MCSection *AMDGPUHSATargetObjectFile::SelectSectionForGlobal(
72                                         const GlobalValue *GV, SectionKind Kind,
73                                         Mangler &Mang,
74                                         const TargetMachine &TM) const {
75   if (Kind.isText() && !GV->hasComdat())
76     return getTextSection();
77 
78   if (AMDGPU::isGlobalSegment(GV)) {
79     if (isAgentAllocation(GV))
80       return DataGlobalAgentSection;
81 
82     if (isProgramAllocation(GV))
83       return DataGlobalProgramSection;
84   }
85 
86   return AMDGPUTargetObjectFile::SelectSectionForGlobal(GV, Kind, Mang, TM);
87 }
88