Skip to content

Commit 1e1b05d

Browse files
committedNov 6, 2015
AMDGPU/SI: Emit HSA kernels with symbol type STT_AMDGPU_HSA_KERNEL
Reviewers: arsenm Subscribers: arsenm, llvm-commits Differential Revision: http://reviews.llvm.org/D13804 llvm-svn: 252291
1 parent 22b4164 commit 1e1b05d

File tree

8 files changed

+84
-4
lines changed

8 files changed

+84
-4
lines changed
 

‎llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ void AMDGPUAsmPrinter::EmitEndOfAsmFile(Module &M) {
110110
OutStreamer->EmitLabel(EndOfTextLabel);
111111
}
112112

113+
void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
114+
const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
115+
const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
116+
if (MFI->isKernel() && STM.isAmdHsaOS()) {
117+
AMDGPUTargetStreamer *TS =
118+
static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
119+
TS->EmitAMDGPUSymbolType(CurrentFnSym->getName(),
120+
ELF::STT_AMDGPU_HSA_KERNEL);
121+
}
122+
123+
AsmPrinter::EmitFunctionEntryLabel();
124+
}
125+
113126
bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
114127

115128
// The starting address of all shader programs must be 256 bytes aligned.

‎llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.h

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ class AMDGPUAsmPrinter : public AsmPrinter {
101101

102102
void EmitEndOfAsmFile(Module &M) override;
103103

104+
void EmitFunctionEntryLabel() override;
105+
104106
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
105107
unsigned AsmVariant, const char *ExtraCode,
106108
raw_ostream &O) override;

‎llvm/lib/Target/AMDGPU/AMDGPUMachineFunction.h

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class AMDGPUMachineFunction : public MachineFunctionInfo {
3737
return ShaderType;
3838
}
3939

40+
bool isKernel() const {
41+
// FIXME: Assume everything is a kernel until function calls are supported.
42+
return true;
43+
}
44+
4045
unsigned ScratchSize;
4146
bool IsKernel;
4247
};

‎llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
#include "llvm/MC/MCRegisterInfo.h"
2929
#include "llvm/MC/MCStreamer.h"
3030
#include "llvm/MC/MCSubtargetInfo.h"
31+
#include "llvm/MC/MCSymbolELF.h"
3132
#include "llvm/MC/MCTargetAsmParser.h"
33+
#include "llvm/Support/ELF.h"
3234
#include "llvm/Support/SourceMgr.h"
3335
#include "llvm/Support/TargetRegistry.h"
3436
#include "llvm/Support/raw_ostream.h"
@@ -355,6 +357,7 @@ class AMDGPUAsmParser : public MCTargetAsmParser {
355357
bool ParseDirectiveAMDKernelCodeT();
356358
bool ParseSectionDirectiveHSAText();
357359
bool subtargetHasRegister(const MCRegisterInfo &MRI, unsigned RegNo) const;
360+
bool ParseDirectiveAMDGPUHsaKernel();
358361

359362
public:
360363
public:
@@ -942,6 +945,18 @@ bool AMDGPUAsmParser::ParseSectionDirectiveHSAText() {
942945
return false;
943946
}
944947

948+
bool AMDGPUAsmParser::ParseDirectiveAMDGPUHsaKernel() {
949+
if (getLexer().isNot(AsmToken::Identifier))
950+
return TokError("expected symbol name");
951+
952+
StringRef KernelName = Parser.getTok().getString();
953+
954+
getTargetStreamer().EmitAMDGPUSymbolType(KernelName,
955+
ELF::STT_AMDGPU_HSA_KERNEL);
956+
Lex();
957+
return false;
958+
}
959+
945960
bool AMDGPUAsmParser::ParseDirective(AsmToken DirectiveID) {
946961
StringRef IDVal = DirectiveID.getString();
947962

@@ -957,6 +972,9 @@ bool AMDGPUAsmParser::ParseDirective(AsmToken DirectiveID) {
957972
if (IDVal == ".hsatext" || IDVal == ".text")
958973
return ParseSectionDirectiveHSAText();
959974

975+
if (IDVal == ".amdgpu_hsa_kernel")
976+
return ParseDirectiveAMDGPUHsaKernel();
977+
960978
return true;
961979
}
962980

‎llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,16 @@ AMDGPUTargetAsmStreamer::EmitAMDKernelCodeT(const amd_kernel_code_t &Header) {
221221

222222
}
223223

224+
void AMDGPUTargetAsmStreamer::EmitAMDGPUSymbolType(StringRef SymbolName,
225+
unsigned Type) {
226+
switch (Type) {
227+
default: llvm_unreachable("Invalid AMDGPU symbol type");
228+
case ELF::STT_AMDGPU_HSA_KERNEL:
229+
OS << "\t.amdgpu_hsa_kernel " << SymbolName << '\n' ;
230+
break;
231+
}
232+
}
233+
224234
//===----------------------------------------------------------------------===//
225235
// AMDGPUTargetELFStreamer
226236
//===----------------------------------------------------------------------===//
@@ -299,3 +309,10 @@ AMDGPUTargetELFStreamer::EmitAMDKernelCodeT(const amd_kernel_code_t &Header) {
299309
OS.EmitBytes(StringRef((const char*)&Header, sizeof(Header)));
300310
OS.PopSection();
301311
}
312+
313+
void AMDGPUTargetELFStreamer::EmitAMDGPUSymbolType(StringRef SymbolName,
314+
unsigned Type) {
315+
MCSymbolELF *Symbol = cast<MCSymbolELF>(
316+
getStreamer().getContext().getOrCreateSymbol(SymbolName));
317+
Symbol->setType(ELF::STT_AMDGPU_HSA_KERNEL);
318+
}

‎llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUTargetStreamer.h

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class AMDGPUTargetStreamer : public MCTargetStreamer {
3030
StringRef ArchName) = 0;
3131

3232
virtual void EmitAMDKernelCodeT(const amd_kernel_code_t &Header) = 0;
33+
34+
virtual void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) = 0;
3335
};
3436

3537
class AMDGPUTargetAsmStreamer : public AMDGPUTargetStreamer {
@@ -44,6 +46,8 @@ class AMDGPUTargetAsmStreamer : public AMDGPUTargetStreamer {
4446
StringRef ArchName) override;
4547

4648
void EmitAMDKernelCodeT(const amd_kernel_code_t &Header) override;
49+
50+
void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override;
4751
};
4852

4953
class AMDGPUTargetELFStreamer : public AMDGPUTargetStreamer {
@@ -75,6 +79,7 @@ class AMDGPUTargetELFStreamer : public AMDGPUTargetStreamer {
7579

7680
void EmitAMDKernelCodeT(const amd_kernel_code_t &Header) override;
7781

82+
void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override;
7883
};
7984

8085
}

‎llvm/test/CodeGen/AMDGPU/hsa.ll

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; RUN: llc < %s -mtriple=amdgcn--amdhsa -mcpu=kaveri | FileCheck --check-prefix=HSA-CI --check-prefix=HSA %s
22
; RUN: llc < %s -mtriple=amdgcn--amdhsa -mcpu=carrizo | FileCheck --check-prefix=HSA-VI --check-prefix=HSA %s
3-
; RUN: llc < %s -mtriple=amdgcn--amdhsa -mcpu=kaveri -filetype=obj | llvm-readobj -s -sd | FileCheck --check-prefix=ELF %s
4-
; RUN: llc < %s -mtriple=amdgcn--amdhsa -mcpu=kaveri | llvm-mc -filetype=obj -triple amdgcn--amdhsa -mcpu=kaveri | llvm-readobj -s -sd | FileCheck %s --check-prefix=ELF
3+
; RUN: llc < %s -mtriple=amdgcn--amdhsa -mcpu=kaveri -filetype=obj | llvm-readobj -symbols -s -sd | FileCheck --check-prefix=ELF %s
4+
; RUN: llc < %s -mtriple=amdgcn--amdhsa -mcpu=kaveri | llvm-mc -filetype=obj -triple amdgcn--amdhsa -mcpu=kaveri | llvm-readobj -symbols -s -sd | FileCheck %s --check-prefix=ELF
55

66
; The SHT_NOTE section contains the output from the .hsa_code_object_*
77
; directives.
@@ -24,12 +24,18 @@
2424
; ELF: 0030: 00000000 00000000 414D4400 414D4447
2525
; ELF: 0040: 50550000
2626

27+
; ELF: Symbol {
28+
; ELF: Name: simple
29+
; ELF: Type: AMDGPU_HSA_KERNEL (0xA)
30+
; ELF: }
31+
2732
; HSA: .hsa_code_object_version 1,0
2833
; HSA-CI: .hsa_code_object_isa 7,0,0,"AMD","AMDGPU"
2934
; HSA-VI: .hsa_code_object_isa 8,0,1,"AMD","AMDGPU"
3035

3136
; HSA: .hsatext
3237

38+
; HSA: .amdgpu_hsa_kernel simple
3339
; HSA: {{^}}simple:
3440
; HSA: .amd_kernel_code_t
3541
; HSA: .end_amd_kernel_code_t

‎llvm/test/MC/AMDGPU/hsa.s

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: llvm-mc -triple amdgcn--amdhsa -mcpu=kaveri -show-encoding %s | FileCheck %s --check-prefix=ASM
2-
// RUN: llvm-mc -filetype=obj -triple amdgcn--amdhsa -mcpu=kaveri -show-encoding %s | llvm-readobj -s -sd | FileCheck %s --check-prefix=ELF
2+
// RUN: llvm-mc -filetype=obj -triple amdgcn--amdhsa -mcpu=kaveri -show-encoding %s | llvm-readobj -symbols -s -sd | FileCheck %s --check-prefix=ELF
33

44
// ELF: Section {
55
// ELF: Name: .hsatext
@@ -10,7 +10,6 @@
1010
// ELF: SHF_AMDGPU_HSA_CODE (0x400000)
1111
// ELF: SHF_EXECINSTR (0x4)
1212
// ELF: SHF_WRITE (0x1)
13-
// ELF: }
1413

1514
// ELF: SHT_NOTE
1615
// ELF: 0000: 04000000 08000000 01000000 414D4400
@@ -19,12 +18,27 @@
1918
// ELF: 0030: 00000000 00000000 414D4400 414D4447
2019
// ELF: 0040: 50550000
2120

21+
// ELF: Symbol {
22+
// ELF: Name: amd_kernel_code_t_minimal
23+
// ELF: Type: AMDGPU_HSA_KERNEL (0xA)
24+
// ELF: Section: .hsatext
25+
// ELF: }
26+
// ELF: Symbol {
27+
// ELF: Name: amd_kernel_code_t_test_all
28+
// ELF: Type: AMDGPU_HSA_KERNEL (0xA)
29+
// ELF: Section: .hsatext
30+
// ELF: }
31+
32+
2233
.hsa_code_object_version 1,0
2334
// ASM: .hsa_code_object_version 1,0
2435

2536
.hsa_code_object_isa 7,0,0,"AMD","AMDGPU"
2637
// ASM: .hsa_code_object_isa 7,0,0,"AMD","AMDGPU"
2738

39+
.amdgpu_hsa_kernel amd_kernel_code_t_test_all
40+
.amdgpu_hsa_kernel amd_kernel_code_t_minimal
41+
2842
.hsatext
2943
// ASM: .hsatext
3044

0 commit comments

Comments
 (0)
Please sign in to comment.