llvm.org GIT mirror llvm / eb211af
AMDGPU: Add support for isa version note - Emit NT_AMD_AMDGPU_ISA - Add assembler parsing for isa version directive - If isa version directive does not match command line arguments, then return error Differential Revision: https://reviews.llvm.org/D38748 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315808 91177308-0d34-0410-b5e6-96231b3b80d8 Konstantin Zhuravlyov 3 years ago
10 changed file(s) with 169 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
131131 }
132132
133133 void AMDGPUAsmPrinter::EmitEndOfAsmFile(Module &M) {
134 if (TM.getTargetTriple().getArch() != Triple::amdgcn)
135 return;
136
137 // Emit ISA Version (NT_AMD_AMDGPU_ISA).
138 std::string ISAVersionString;
139 raw_string_ostream ISAVersionStream(ISAVersionString);
140 IsaInfo::streamIsaVersion(getSTI(), ISAVersionStream);
141 getTargetStreamer().EmitISAVersion(ISAVersionStream.str());
142
143 // Emit HSA Metadata (NT_AMD_AMDGPU_HSA_METADATA).
144 if (TM.getTargetTriple().getOS() == Triple::AMDHSA) {
145 HSAMetadataStream.end();
146 getTargetStreamer().EmitHSAMetadata(HSAMetadataStream.getHSAMetadata());
147 }
148
149 // Emit PAL Metadata (NT_AMD_AMDGPU_PAL_METADATA).
134150 if (TM.getTargetTriple().getOS() == Triple::AMDPAL) {
135151 // Copy the PAL metadata from the map where we collected it into a vector,
136152 // then write it as a .note.
141157 }
142158 getTargetStreamer().EmitPALMetadata(PALMetadataVector);
143159 }
144
145 if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
146 return;
147
148 HSAMetadataStream.end();
149 getTargetStreamer().EmitHSAMetadata(HSAMetadataStream.getHSAMetadata());
150160 }
151161
152162 bool AMDGPUAsmPrinter::isBlockOnlyReachableByFallthrough(
831831 bool subtargetHasRegister(const MCRegisterInfo &MRI, unsigned RegNo) const;
832832 bool ParseDirectiveAMDGPUHsaKernel();
833833
834 bool ParseDirectiveISAVersion();
834835 bool ParseDirectiveHSAMetadata();
835836 bool ParseDirectivePALMetadata();
836837
24512452 return false;
24522453 }
24532454
2455 bool AMDGPUAsmParser::ParseDirectiveISAVersion() {
2456 auto ISAVersionStringFromASM = getLexer().getTok().getStringContents();
2457
2458 std::string ISAVersionStringFromSTI;
2459 raw_string_ostream ISAVersionStreamFromSTI(ISAVersionStringFromSTI);
2460 IsaInfo::streamIsaVersion(&getSTI(), ISAVersionStreamFromSTI);
2461
2462 if (ISAVersionStringFromASM != ISAVersionStreamFromSTI.str()) {
2463 return Error(getParser().getTok().getLoc(),
2464 ".amd_amdgpu_isa directive does not match triple and/or mcpu "
2465 "arguments specified through the command line");
2466 }
2467
2468 getTargetStreamer().EmitISAVersion(ISAVersionStreamFromSTI.str());
2469 Lex();
2470
2471 return false;
2472 }
2473
24542474 bool AMDGPUAsmParser::ParseDirectiveHSAMetadata() {
24552475 std::string HSAMetadataString;
24562476 raw_string_ostream YamlStream(HSAMetadataString);
25252545
25262546 if (IDVal == ".amdgpu_hsa_kernel")
25272547 return ParseDirectiveAMDGPUHsaKernel();
2548
2549 if (IDVal == ".amd_amdgpu_isa")
2550 return ParseDirectiveISAVersion();
25282551
25292552 if (IDVal == AMDGPU::HSAMD::AssemblerDirectiveBegin)
25302553 return ParseDirectiveHSAMetadata();
3838 // AMDGPUTargetStreamer
3939 //===----------------------------------------------------------------------===//
4040
41 AMDGPUTargetStreamer::AMDGPUTargetStreamer(MCStreamer &S)
42 : MCTargetStreamer(S) {}
43
4441 bool AMDGPUTargetStreamer::EmitHSAMetadata(StringRef HSAMetadataString) {
4542 HSAMD::Metadata HSAMetadata;
4643 if (HSAMD::fromString(HSAMetadataString, HSAMetadata))
9188 OS << "\t.amdgpu_hsa_kernel " << SymbolName << '\n' ;
9289 break;
9390 }
91 }
92
93 bool AMDGPUTargetAsmStreamer::EmitISAVersion(StringRef IsaVersionString) {
94 OS << "\t.amd_amdgpu_isa \"" << IsaVersionString << "\"\n";
95 return true;
9496 }
9597
9698 bool AMDGPUTargetAsmStreamer::EmitHSAMetadata(
207209 Symbol->setType(ELF::STT_AMDGPU_HSA_KERNEL);
208210 }
209211
210 bool AMDGPUTargetELFStreamer::EmitHSAMetadata(
211 const AMDGPU::HSAMD::Metadata &HSAMetadata) {
212 std::string HSAMetadataString;
213 if (HSAMD::toString(HSAMetadata, HSAMetadataString))
214 return false;
215
212 bool AMDGPUTargetELFStreamer::EmitISAVersion(StringRef IsaVersionString) {
216213 // Create two labels to mark the beginning and end of the desc field
217214 // and a MCExpr to calculate the size of the desc field.
218215 auto &Context = getContext();
224221
225222 EmitAMDGPUNote(
226223 DescSZ,
224 ELF::NT_AMD_AMDGPU_ISA,
225 [&](MCELFStreamer &OS) {
226 OS.EmitLabel(DescBegin);
227 OS.EmitBytes(IsaVersionString);
228 OS.EmitLabel(DescEnd);
229 }
230 );
231 return true;
232 }
233
234 bool AMDGPUTargetELFStreamer::EmitHSAMetadata(
235 const AMDGPU::HSAMD::Metadata &HSAMetadata) {
236 std::string HSAMetadataString;
237 if (HSAMD::toString(HSAMetadata, HSAMetadataString))
238 return false;
239
240 // Create two labels to mark the beginning and end of the desc field
241 // and a MCExpr to calculate the size of the desc field.
242 auto &Context = getContext();
243 auto *DescBegin = Context.createTempSymbol();
244 auto *DescEnd = Context.createTempSymbol();
245 auto *DescSZ = MCBinaryExpr::createSub(
246 MCSymbolRefExpr::create(DescEnd, Context),
247 MCSymbolRefExpr::create(DescBegin, Context), Context);
248
249 EmitAMDGPUNote(
250 DescSZ,
227251 ELF::NT_AMD_AMDGPU_HSA_METADATA,
228252 [&](MCELFStreamer &OS) {
229253 OS.EmitLabel(DescBegin);
1111
1212 #include "AMDKernelCodeT.h"
1313 #include "llvm/MC/MCStreamer.h"
14 #include "llvm/MC/MCSubtargetInfo.h"
1415 #include "llvm/Support/AMDGPUMetadata.h"
1516
1617 namespace llvm {
2930 MCContext &getContext() const { return Streamer.getContext(); }
3031
3132 public:
32 AMDGPUTargetStreamer(MCStreamer &S);
33 AMDGPUTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
34
3335 virtual void EmitDirectiveHSACodeObjectVersion(uint32_t Major,
3436 uint32_t Minor) = 0;
3537
4143 virtual void EmitAMDKernelCodeT(const amd_kernel_code_t &Header) = 0;
4244
4345 virtual void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) = 0;
46
47 /// \returns True on success, false on failure.
48 virtual bool EmitISAVersion(StringRef IsaVersionString) = 0;
4449
4550 /// \returns True on success, false on failure.
4651 virtual bool EmitHSAMetadata(StringRef HSAMetadataString);
6671 void EmitAMDKernelCodeT(const amd_kernel_code_t &Header) override;
6772
6873 void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override;
74
75 /// \returns True on success, false on failure.
76 bool EmitISAVersion(StringRef IsaVersionString) override;
6977
7078 /// \returns True on success, false on failure.
7179 bool EmitHSAMetadata(const AMDGPU::HSAMD::Metadata &HSAMetadata) override;
97105 void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override;
98106
99107 /// \returns True on success, false on failure.
108 bool EmitISAVersion(StringRef IsaVersionString) override;
109
110 /// \returns True on success, false on failure.
100111 bool EmitHSAMetadata(const AMDGPU::HSAMD::Metadata &HSAMetadata) override;
101112
102113 /// \returns True on success, false on failure.
144144 if (!Features.test(FeatureGCN) || Features.test(FeatureSouthernIslands))
145145 return {0, 0, 0};
146146 return {7, 0, 0};
147 }
148
149 void streamIsaVersion(const MCSubtargetInfo *STI, raw_ostream &Stream) {
150 auto TargetTriple = STI->getTargetTriple();
151 auto ISAVersion = IsaInfo::getIsaVersion(STI->getFeatureBits());
152
153 Stream << TargetTriple.getArchName() << '-'
154 << TargetTriple.getVendorName() << '-'
155 << TargetTriple.getOSName() << '-'
156 << TargetTriple.getEnvironmentName() << '-'
157 << "gfx"
158 << ISAVersion.Major
159 << ISAVersion.Minor
160 << ISAVersion.Stepping;
161 Stream.flush();
147162 }
148163
149164 unsigned getWavefrontSize(const FeatureBitset &Features) {
1818 #include "llvm/Support/Compiler.h"
1919 #include "llvm/Support/ErrorHandling.h"
2020 #include
21 #include
2122 #include
2223
2324 namespace llvm {
5354 /// \returns Isa version for given subtarget \p Features.
5455 IsaVersion getIsaVersion(const FeatureBitset &Features);
5556
57 /// \brief Streams isa version string for given subtarget \p STI into \p Stream.
58 void streamIsaVersion(const MCSubtargetInfo *STI, raw_ostream &Stream);
59
5660 /// \returns Wavefront size for given subtarget \p Features.
5761 unsigned getWavefrontSize(const FeatureBitset &Features);
5862
0 ; RUN: llc -mtriple=amdgcn-amd-unknown -mcpu=gfx800 < %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-UNK --check-prefix=GFX800 %s
1 ; RUN: llc -mtriple=amdgcn-amd-unknown -mcpu=iceland < %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-UNK --check-prefix=GFX800 %s
2 ; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx800 < %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-HSA --check-prefix=GFX800 %s
3 ; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=iceland < %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-HSA --check-prefix=GFX800 %s
4 ; RUN: llc -mtriple=amdgcn-amd-amdpal -mcpu=gfx800 < %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-PAL --check-prefix=GFX800 %s
5 ; RUN: llc -mtriple=amdgcn-amd-amdpal -mcpu=iceland < %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-PAL --check-prefix=GFX800 %s
6 ; RUN: llc -march=r600 < %s | FileCheck --check-prefix=R600 %s
7
8 ; OSABI-UNK: .amd_amdgpu_isa "amdgcn-amd-unknown--gfx800"
9 ; OSABI-UNK-NOT: .amd_amdgpu_hsa_metadata
10 ; OSABI-UNK-NOT: .amd_amdgpu_pal_metadata
11
12 ; OSABI-HSA: .amd_amdgpu_isa "amdgcn-amd-amdhsa--gfx800"
13 ; OSABI-HSA: .amd_amdgpu_hsa_metadata
14
15 ; OSABI-PAL: .amd_amdgpu_isa "amdgcn-amd-amdpal--gfx800"
16 ; OSABI-PAL: .amd_amdgpu_pal_metadata
17
18 ; R600-NOT: .amd_amdgpu_isa
19 ; R600-NOT: .amd_amdgpu_hsa_metadata
20 ; R600-NOT: .amd_amdgpu_hsa_metadata
21
22 define amdgpu_kernel void @elf_notes() {
23 ret void
24 }
0 // RUN: not llvm-mc -triple amdgcn-amd-unknown -mcpu=gfx800 %s 2>&1 | FileCheck --check-prefix=GCN --check-prefix=OSABI-UNK-ERR --check-prefix=GFX800 %s
1 // RUN: not llvm-mc -triple amdgcn-amd-unknown -mcpu=iceland %s 2>&1 | FileCheck --check-prefix=GCN --check-prefix=OSABI-UNK-ERR --check-prefix=GFX800 %s
2 // RUN: llvm-mc -triple amdgcn-amd-amdhsa -mcpu=gfx800 %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-HSA --check-prefix=GFX800 %s
3 // RUN: llvm-mc -triple amdgcn-amd-amdhsa -mcpu=iceland %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-HSA --check-prefix=GFX800 %s
4 // RUN: not llvm-mc -triple amdgcn-amd-amdhsa -mcpu=gfx803 %s 2>&1 | FileCheck --check-prefix=GCN --check-prefix=OSABI-HSA-ERR --check-prefix=GFX800 %s
5 // RUN: not llvm-mc -triple amdgcn-amd-amdpal -mcpu=gfx800 %s 2>&1 | FileCheck --check-prefix=GCN --check-prefix=OSABI-PAL-ERR --check-prefix=GFX800 %s
6 // RUN: not llvm-mc -triple amdgcn-amd-amdpal -mcpu=iceland %s 2>&1 | FileCheck --check-prefix=GCN --check-prefix=OSABI-PAL-ERR --check-prefix=GFX800 %s
7
8
9 // OSABI-HSA: .amd_amdgpu_isa "amdgcn-amd-amdhsa--gfx800"
10 // OSABI-UNK-ERR: error: .amd_amdgpu_isa directive does not match triple and/or mcpu arguments specified through the command line
11 // OSABI-HSA-ERR: error: .amd_amdgpu_isa directive does not match triple and/or mcpu arguments specified through the command line
12 // OSABI-PAL-ERR: error: .amd_amdgpu_isa directive does not match triple and/or mcpu arguments specified through the command line
13 .amd_amdgpu_isa "amdgcn-amd-amdhsa--gfx800"
0 // RUN: not llvm-mc -triple amdgcn-amd-unknown -mcpu=gfx800 %s 2>&1 | FileCheck --check-prefix=GCN --check-prefix=OSABI-UNK-ERR --check-prefix=GFX800 %s
1 // RUN: not llvm-mc -triple amdgcn-amd-unknown -mcpu=iceland %s 2>&1 | FileCheck --check-prefix=GCN --check-prefix=OSABI-UNK-ERR --check-prefix=GFX800 %s
2 // RUN: not llvm-mc -triple amdgcn-amd-amdhsa -mcpu=gfx800 %s 2>&1 | FileCheck --check-prefix=GCN --check-prefix=OSABI-HSA-ERR --check-prefix=GFX800 %s
3 // RUN: not llvm-mc -triple amdgcn-amd-amdhsa -mcpu=iceland %s 2>&1 | FileCheck --check-prefix=GCN --check-prefix=OSABI-HSA-ERR --check-prefix=GFX800 %s
4 // RUN: llvm-mc -triple amdgcn-amd-amdpal -mcpu=gfx800 %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-PAL --check-prefix=GFX800 %s
5 // RUN: llvm-mc -triple amdgcn-amd-amdpal -mcpu=iceland %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-PAL --check-prefix=GFX800 %s
6 // RUN: not llvm-mc -triple amdgcn-amd-unknown -mcpu=gfx803 %s 2>&1 | FileCheck --check-prefix=GCN --check-prefix=OSABI-UNK-ERR --check-prefix=GFX800 %s
7
8 // OSABI-PAL: .amd_amdgpu_isa "amdgcn-amd-amdpal--gfx800"
9 // OSABI-UNK-ERR: error: .amd_amdgpu_isa directive does not match triple and/or mcpu arguments specified through the command line
10 // OSABI-HSA-ERR: error: .amd_amdgpu_isa directive does not match triple and/or mcpu arguments specified through the command line
11 // OSABI-PAL-ERR: error: .amd_amdgpu_isa directive does not match triple and/or mcpu arguments specified through the command line
12 .amd_amdgpu_isa "amdgcn-amd-amdpal--gfx800"
0 // RUN: llvm-mc -triple amdgcn-amd-unknown -mcpu=gfx800 %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-UNK --check-prefix=GFX800 %s
1 // RUN: llvm-mc -triple amdgcn-amd-unknown -mcpu=iceland %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-UNK --check-prefix=GFX800 %s
2 // RUN: not llvm-mc -triple amdgcn-amd-unknown -mcpu=gfx803 %s 2>&1 | FileCheck --check-prefix=GCN --check-prefix=OSABI-UNK-ERR --check-prefix=GFX800 %s
3 // RUN: not llvm-mc -triple amdgcn-amd-amdhsa -mcpu=gfx800 %s 2>&1 | FileCheck --check-prefix=GCN --check-prefix=OSABI-HSA-ERR --check-prefix=GFX800 %s
4 // RUN: not llvm-mc -triple amdgcn-amd-amdhsa -mcpu=iceland %s 2>&1 | FileCheck --check-prefix=GCN --check-prefix=OSABI-HSA-ERR --check-prefix=GFX800 %s
5 // RUN: not llvm-mc -triple amdgcn-amd-amdpal -mcpu=gfx800 %s 2>&1 | FileCheck --check-prefix=GCN --check-prefix=OSABI-PAL-ERR --check-prefix=GFX800 %s
6 // RUN: not llvm-mc -triple amdgcn-amd-amdpal -mcpu=iceland %s 2>&1 | FileCheck --check-prefix=GCN --check-prefix=OSABI-PAL-ERR --check-prefix=GFX800 %s
7
8
9 // OSABI-UNK: .amd_amdgpu_isa "amdgcn-amd-unknown--gfx800"
10 // OSABI-UNK-ERR: error: .amd_amdgpu_isa directive does not match triple and/or mcpu arguments specified through the command line
11 // OSABI-HSA-ERR: error: .amd_amdgpu_isa directive does not match triple and/or mcpu arguments specified through the command line
12 // OSABI-PAL-ERR: error: .amd_amdgpu_isa directive does not match triple and/or mcpu arguments specified through the command line
13 .amd_amdgpu_isa "amdgcn-amd-unknown--gfx800"