Index: lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp =================================================================== --- lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp +++ lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp @@ -130,14 +130,20 @@ const Module *M = GV->getParent(); if (Triple(M->getTargetTriple()).getOS() != Triple::AMDHSA || - (!GV->hasCommonLinkage() && !GV->hasInternalLinkage())) { + GV->isDeclaration() || + (!GV->hasCommonLinkage() && !GV->hasInternalLinkage() && + !GV->hasExternalLinkage())) { AsmPrinter::EmitGlobalVariable(GV); return; } AMDGPUTargetStreamer *TS = static_cast(OutStreamer->getTargetStreamer()); - TS->EmitAMDGPUHsaModuleGlobal(GV->getName()); + if (GV->hasExternalLinkage()) { + TS->EmitAMDGPUHsaProgramGlobal(GV->getName()); + } else { + TS->EmitAMDGPUHsaModuleGlobal(GV->getName()); + } const DataLayout &DL = getDataLayout(); OutStreamer->PushSection(); Index: lib/Target/AMDGPU/AMDGPUHSATargetObjectFile.h =================================================================== --- lib/Target/AMDGPU/AMDGPUHSATargetObjectFile.h +++ lib/Target/AMDGPU/AMDGPUHSATargetObjectFile.h @@ -25,6 +25,7 @@ private: MCSection *DataGlobalAgentSection; MCSection *RodataReadonlyAgentSection; + MCSection *DataGlobalProgramSection; public: void Initialize(MCContext &Ctx, const TargetMachine &TM) override; Index: lib/Target/AMDGPU/AMDGPUHSATargetObjectFile.cpp =================================================================== --- lib/Target/AMDGPU/AMDGPUHSATargetObjectFile.cpp +++ lib/Target/AMDGPU/AMDGPUHSATargetObjectFile.cpp @@ -29,6 +29,12 @@ ELF::SHF_ALLOC | ELF::SHF_AMDGPU_HSA_READONLY | ELF::SHF_AMDGPU_HSA_AGENT); + + DataGlobalProgramSection = Ctx.getELFSection(".hsadata_global_program", + ELF::SHT_PROGBITS, + ELF::SHF_ALLOC | + ELF::SHF_WRITE | + ELF::SHF_AMDGPU_HSA_GLOBAL); } @@ -50,5 +56,8 @@ if (Kind.isReadOnly() && GV->hasInternalLinkage()) return RodataReadonlyAgentSection; + if (GV->hasExternalLinkage()) + return DataGlobalProgramSection; + return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang, TM); } Index: lib/Target/AMDGPU/MCTargetDesc/AMDGPUMCAsmInfo.cpp =================================================================== --- lib/Target/AMDGPU/MCTargetDesc/AMDGPUMCAsmInfo.cpp +++ lib/Target/AMDGPU/MCTargetDesc/AMDGPUMCAsmInfo.cpp @@ -44,5 +44,6 @@ bool AMDGPUMCAsmInfo::shouldOmitSectionDirective(StringRef SectionName) const { return SectionName == ".hsatext" || SectionName == ".hsadata_global_agent" || + SectionName == ".hsadata_global_program" || MCAsmInfo::shouldOmitSectionDirective(SectionName); } Index: lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.h =================================================================== --- lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.h +++ lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.h @@ -34,6 +34,8 @@ virtual void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) = 0; virtual void EmitAMDGPUHsaModuleGlobal(StringRef GlobalName) = 0; + + virtual void EmitAMDGPUHsaProgramGlobal(StringRef GlobalName) = 0; }; class AMDGPUTargetAsmStreamer : public AMDGPUTargetStreamer { @@ -52,6 +54,8 @@ void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override; void EmitAMDGPUHsaModuleGlobal(StringRef GlobalName) override; + + void EmitAMDGPUHsaProgramGlobal(StringRef GlobalName) override; }; class AMDGPUTargetELFStreamer : public AMDGPUTargetStreamer { @@ -86,6 +90,8 @@ void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override; void EmitAMDGPUHsaModuleGlobal(StringRef GlobalName) override; + + void EmitAMDGPUHsaProgramGlobal(StringRef GlobalName) override; }; } Index: lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp =================================================================== --- lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp +++ lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp @@ -235,6 +235,10 @@ OS << "\t.amdgpu_hsa_module_global " << GlobalName << '\n'; } +void AMDGPUTargetAsmStreamer::EmitAMDGPUHsaProgramGlobal(StringRef GlobalName) { + OS << "\t.amdgpu_hsa_program_global " << GlobalName << '\n'; +} + //===----------------------------------------------------------------------===// // AMDGPUTargetELFStreamer //===----------------------------------------------------------------------===// @@ -327,3 +331,11 @@ getStreamer().getContext().getOrCreateSymbol(GlobalName)); Symbol->setType(ELF::STT_OBJECT); } + +void AMDGPUTargetELFStreamer::EmitAMDGPUHsaProgramGlobal(StringRef GlobalName) { + + MCSymbolELF *Symbol = cast( + getStreamer().getContext().getOrCreateSymbol(GlobalName)); + Symbol->setType(ELF::STT_OBJECT); + Symbol->setBinding(ELF::STB_GLOBAL); +} Index: test/CodeGen/AMDGPU/hsa-globals.ll =================================================================== --- test/CodeGen/AMDGPU/hsa-globals.ll +++ test/CodeGen/AMDGPU/hsa-globals.ll @@ -6,6 +6,7 @@ ; FIXME-XUN: llc < %s -mtriple=amdgcn--amdhsa -mcpu=kaveri | llvm-mc -filetype=obj -triple amdgcn--amdhsa -mcpu=kaveri | llvm-readobj -symbols -s -sd -relocations | FileCheck %s --check-prefix=ELF @const_array = internal unnamed_addr addrspace(2) constant [64 x i8] c"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<>", align 1 +@global_program = addrspace(1) global i32 0, align 4 ; ELF: Section { ; ELF: Name: .hsarodata_readonly_agent @@ -29,6 +30,13 @@ ; ELF: Section: .hsarodata_readonly_agent (0x6) ; ELF: } +; ELF: Symbol { +; ELF: Name: global_program +; ELF: Binding: Global (0x1) +; ELF: Type: Object (0x1) +; ELF: Section: .hsadata_global_program +; ELF: } + ; HSA-LABEL: {{^}}test: ; HSA-DAG: s_mov_b32 s[[LORELOC:[0-9]+]], const_array@LO ; HSA-DAG: s_mov_b32 s[[HIRELOC:[0-9]+]], const_array@HI @@ -43,6 +51,9 @@ ; HSA: .hsarodata_readonly_agent ; HSA: const_array: ; HSA: .ascii "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<>" +; HSA: .hsadata_global_program +; HSA: global_program: +; HSA: .long 0 ; HSA-NOT: EndOfTextLabel define void @test(i8 addrspace(1) *%out, i32 %offset) {