Skip to content

Commit 9c05b2b

Browse files
committedOct 14, 2017
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 llvm-svn: 315808
1 parent f367c27 commit 9c05b2b

File tree

10 files changed

+163
-10
lines changed

10 files changed

+163
-10
lines changed
 

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

+16-6
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,22 @@ void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) {
132132
}
133133

134134
void AMDGPUAsmPrinter::EmitEndOfAsmFile(Module &M) {
135+
if (TM.getTargetTriple().getArch() != Triple::amdgcn)
136+
return;
137+
138+
// Emit ISA Version (NT_AMD_AMDGPU_ISA).
139+
std::string ISAVersionString;
140+
raw_string_ostream ISAVersionStream(ISAVersionString);
141+
IsaInfo::streamIsaVersion(getSTI(), ISAVersionStream);
142+
getTargetStreamer().EmitISAVersion(ISAVersionStream.str());
143+
144+
// Emit HSA Metadata (NT_AMD_AMDGPU_HSA_METADATA).
145+
if (TM.getTargetTriple().getOS() == Triple::AMDHSA) {
146+
HSAMetadataStream.end();
147+
getTargetStreamer().EmitHSAMetadata(HSAMetadataStream.getHSAMetadata());
148+
}
149+
150+
// Emit PAL Metadata (NT_AMD_AMDGPU_PAL_METADATA).
135151
if (TM.getTargetTriple().getOS() == Triple::AMDPAL) {
136152
// Copy the PAL metadata from the map where we collected it into a vector,
137153
// then write it as a .note.
@@ -142,12 +158,6 @@ void AMDGPUAsmPrinter::EmitEndOfAsmFile(Module &M) {
142158
}
143159
getTargetStreamer().EmitPALMetadata(PALMetadataVector);
144160
}
145-
146-
if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
147-
return;
148-
149-
HSAMetadataStream.end();
150-
getTargetStreamer().EmitHSAMetadata(HSAMetadataStream.getHSAMetadata());
151161
}
152162

153163
bool AMDGPUAsmPrinter::isBlockOnlyReachableByFallthrough(

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

+23
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ class AMDGPUAsmParser : public MCTargetAsmParser {
832832
bool subtargetHasRegister(const MCRegisterInfo &MRI, unsigned RegNo) const;
833833
bool ParseDirectiveAMDGPUHsaKernel();
834834

835+
bool ParseDirectiveISAVersion();
835836
bool ParseDirectiveHSAMetadata();
836837
bool ParseDirectivePALMetadata();
837838

@@ -2452,6 +2453,25 @@ bool AMDGPUAsmParser::ParseDirectiveAMDGPUHsaKernel() {
24522453
return false;
24532454
}
24542455

2456+
bool AMDGPUAsmParser::ParseDirectiveISAVersion() {
2457+
auto ISAVersionStringFromASM = getLexer().getTok().getStringContents();
2458+
2459+
std::string ISAVersionStringFromSTI;
2460+
raw_string_ostream ISAVersionStreamFromSTI(ISAVersionStringFromSTI);
2461+
IsaInfo::streamIsaVersion(&getSTI(), ISAVersionStreamFromSTI);
2462+
2463+
if (ISAVersionStringFromASM != ISAVersionStreamFromSTI.str()) {
2464+
return Error(getParser().getTok().getLoc(),
2465+
".amd_amdgpu_isa directive does not match triple and/or mcpu "
2466+
"arguments specified through the command line");
2467+
}
2468+
2469+
getTargetStreamer().EmitISAVersion(ISAVersionStreamFromSTI.str());
2470+
Lex();
2471+
2472+
return false;
2473+
}
2474+
24552475
bool AMDGPUAsmParser::ParseDirectiveHSAMetadata() {
24562476
std::string HSAMetadataString;
24572477
raw_string_ostream YamlStream(HSAMetadataString);
@@ -2527,6 +2547,9 @@ bool AMDGPUAsmParser::ParseDirective(AsmToken DirectiveID) {
25272547
if (IDVal == ".amdgpu_hsa_kernel")
25282548
return ParseDirectiveAMDGPUHsaKernel();
25292549

2550+
if (IDVal == ".amd_amdgpu_isa")
2551+
return ParseDirectiveISAVersion();
2552+
25302553
if (IDVal == AMDGPU::HSAMD::AssemblerDirectiveBegin)
25312554
return ParseDirectiveHSAMetadata();
25322555

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

+27-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ using namespace llvm::AMDGPU;
3939
// AMDGPUTargetStreamer
4040
//===----------------------------------------------------------------------===//
4141

42-
AMDGPUTargetStreamer::AMDGPUTargetStreamer(MCStreamer &S)
43-
: MCTargetStreamer(S) {}
44-
4542
bool AMDGPUTargetStreamer::EmitHSAMetadata(StringRef HSAMetadataString) {
4643
HSAMD::Metadata HSAMetadata;
4744
if (HSAMD::fromString(HSAMetadataString, HSAMetadata))
@@ -94,6 +91,11 @@ void AMDGPUTargetAsmStreamer::EmitAMDGPUSymbolType(StringRef SymbolName,
9491
}
9592
}
9693

94+
bool AMDGPUTargetAsmStreamer::EmitISAVersion(StringRef IsaVersionString) {
95+
OS << "\t.amd_amdgpu_isa \"" << IsaVersionString << "\"\n";
96+
return true;
97+
}
98+
9799
bool AMDGPUTargetAsmStreamer::EmitHSAMetadata(
98100
const AMDGPU::HSAMD::Metadata &HSAMetadata) {
99101
std::string HSAMetadataString;
@@ -208,6 +210,28 @@ void AMDGPUTargetELFStreamer::EmitAMDGPUSymbolType(StringRef SymbolName,
208210
Symbol->setType(ELF::STT_AMDGPU_HSA_KERNEL);
209211
}
210212

213+
bool AMDGPUTargetELFStreamer::EmitISAVersion(StringRef IsaVersionString) {
214+
// Create two labels to mark the beginning and end of the desc field
215+
// and a MCExpr to calculate the size of the desc field.
216+
auto &Context = getContext();
217+
auto *DescBegin = Context.createTempSymbol();
218+
auto *DescEnd = Context.createTempSymbol();
219+
auto *DescSZ = MCBinaryExpr::createSub(
220+
MCSymbolRefExpr::create(DescEnd, Context),
221+
MCSymbolRefExpr::create(DescBegin, Context), Context);
222+
223+
EmitAMDGPUNote(
224+
DescSZ,
225+
ELF::NT_AMD_AMDGPU_ISA,
226+
[&](MCELFStreamer &OS) {
227+
OS.EmitLabel(DescBegin);
228+
OS.EmitBytes(IsaVersionString);
229+
OS.EmitLabel(DescEnd);
230+
}
231+
);
232+
return true;
233+
}
234+
211235
bool AMDGPUTargetELFStreamer::EmitHSAMetadata(
212236
const AMDGPU::HSAMD::Metadata &HSAMetadata) {
213237
std::string HSAMetadataString;

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "AMDKernelCodeT.h"
1414
#include "llvm/MC/MCStreamer.h"
15+
#include "llvm/MC/MCSubtargetInfo.h"
1516
#include "llvm/Support/AMDGPUMetadata.h"
1617

1718
namespace llvm {
@@ -30,7 +31,8 @@ class AMDGPUTargetStreamer : public MCTargetStreamer {
3031
MCContext &getContext() const { return Streamer.getContext(); }
3132

3233
public:
33-
AMDGPUTargetStreamer(MCStreamer &S);
34+
AMDGPUTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
35+
3436
virtual void EmitDirectiveHSACodeObjectVersion(uint32_t Major,
3537
uint32_t Minor) = 0;
3638

@@ -43,6 +45,9 @@ class AMDGPUTargetStreamer : public MCTargetStreamer {
4345

4446
virtual void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) = 0;
4547

48+
/// \returns True on success, false on failure.
49+
virtual bool EmitISAVersion(StringRef IsaVersionString) = 0;
50+
4651
/// \returns True on success, false on failure.
4752
virtual bool EmitHSAMetadata(StringRef HSAMetadataString);
4853

@@ -68,6 +73,9 @@ class AMDGPUTargetAsmStreamer final : public AMDGPUTargetStreamer {
6873

6974
void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override;
7075

76+
/// \returns True on success, false on failure.
77+
bool EmitISAVersion(StringRef IsaVersionString) override;
78+
7179
/// \returns True on success, false on failure.
7280
bool EmitHSAMetadata(const AMDGPU::HSAMD::Metadata &HSAMetadata) override;
7381

@@ -97,6 +105,9 @@ class AMDGPUTargetELFStreamer final : public AMDGPUTargetStreamer {
97105

98106
void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override;
99107

108+
/// \returns True on success, false on failure.
109+
bool EmitISAVersion(StringRef IsaVersionString) override;
110+
100111
/// \returns True on success, false on failure.
101112
bool EmitHSAMetadata(const AMDGPU::HSAMD::Metadata &HSAMetadata) override;
102113

‎llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ IsaVersion getIsaVersion(const FeatureBitset &Features) {
147147
return {7, 0, 0};
148148
}
149149

150+
void streamIsaVersion(const MCSubtargetInfo *STI, raw_ostream &Stream) {
151+
auto TargetTriple = STI->getTargetTriple();
152+
auto ISAVersion = IsaInfo::getIsaVersion(STI->getFeatureBits());
153+
154+
Stream << TargetTriple.getArchName() << '-'
155+
<< TargetTriple.getVendorName() << '-'
156+
<< TargetTriple.getOSName() << '-'
157+
<< TargetTriple.getEnvironmentName() << '-'
158+
<< "gfx"
159+
<< ISAVersion.Major
160+
<< ISAVersion.Minor
161+
<< ISAVersion.Stepping;
162+
Stream.flush();
163+
}
164+
150165
unsigned getWavefrontSize(const FeatureBitset &Features) {
151166
if (Features.test(FeatureWavefrontSize16))
152167
return 16;

‎llvm/lib/Target/AMDGPU/Utils/AMDGPUBaseInfo.h

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/Support/Compiler.h"
2020
#include "llvm/Support/ErrorHandling.h"
2121
#include <cstdint>
22+
#include <string>
2223
#include <utility>
2324

2425
namespace llvm {
@@ -54,6 +55,9 @@ struct IsaVersion {
5455
/// \returns Isa version for given subtarget \p Features.
5556
IsaVersion getIsaVersion(const FeatureBitset &Features);
5657

58+
/// \brief Streams isa version string for given subtarget \p STI into \p Stream.
59+
void streamIsaVersion(const MCSubtargetInfo *STI, raw_ostream &Stream);
60+
5761
/// \returns Wavefront size for given subtarget \p Features.
5862
unsigned getWavefrontSize(const FeatureBitset &Features);
5963

‎llvm/test/CodeGen/AMDGPU/elf-notes.ll

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; RUN: llc -mtriple=amdgcn-amd-unknown -mcpu=gfx800 < %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-UNK --check-prefix=GFX800 %s
2+
; RUN: llc -mtriple=amdgcn-amd-unknown -mcpu=iceland < %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-UNK --check-prefix=GFX800 %s
3+
; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx800 < %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-HSA --check-prefix=GFX800 %s
4+
; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=iceland < %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-HSA --check-prefix=GFX800 %s
5+
; RUN: llc -mtriple=amdgcn-amd-amdpal -mcpu=gfx800 < %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-PAL --check-prefix=GFX800 %s
6+
; RUN: llc -mtriple=amdgcn-amd-amdpal -mcpu=iceland < %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-PAL --check-prefix=GFX800 %s
7+
; RUN: llc -march=r600 < %s | FileCheck --check-prefix=R600 %s
8+
9+
; OSABI-UNK: .amd_amdgpu_isa "amdgcn-amd-unknown--gfx800"
10+
; OSABI-UNK-NOT: .amd_amdgpu_hsa_metadata
11+
; OSABI-UNK-NOT: .amd_amdgpu_pal_metadata
12+
13+
; OSABI-HSA: .amd_amdgpu_isa "amdgcn-amd-amdhsa--gfx800"
14+
; OSABI-HSA: .amd_amdgpu_hsa_metadata
15+
16+
; OSABI-PAL: .amd_amdgpu_isa "amdgcn-amd-amdpal--gfx800"
17+
; OSABI-PAL: .amd_amdgpu_pal_metadata
18+
19+
; R600-NOT: .amd_amdgpu_isa
20+
; R600-NOT: .amd_amdgpu_hsa_metadata
21+
; R600-NOT: .amd_amdgpu_hsa_metadata
22+
23+
define amdgpu_kernel void @elf_notes() {
24+
ret void
25+
}

‎llvm/test/MC/AMDGPU/isa-version-hsa.s

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// 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
2+
// 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
3+
// RUN: llvm-mc -triple amdgcn-amd-amdhsa -mcpu=gfx800 %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-HSA --check-prefix=GFX800 %s
4+
// RUN: llvm-mc -triple amdgcn-amd-amdhsa -mcpu=iceland %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-HSA --check-prefix=GFX800 %s
5+
// 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
6+
// 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
7+
// 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
8+
9+
10+
// OSABI-HSA: .amd_amdgpu_isa "amdgcn-amd-amdhsa--gfx800"
11+
// OSABI-UNK-ERR: error: .amd_amdgpu_isa directive does not match triple and/or mcpu arguments specified through the command line
12+
// OSABI-HSA-ERR: error: .amd_amdgpu_isa directive does not match triple and/or mcpu arguments specified through the command line
13+
// OSABI-PAL-ERR: error: .amd_amdgpu_isa directive does not match triple and/or mcpu arguments specified through the command line
14+
.amd_amdgpu_isa "amdgcn-amd-amdhsa--gfx800"

‎llvm/test/MC/AMDGPU/isa-version-pal.s

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// 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
2+
// 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
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: llvm-mc -triple amdgcn-amd-amdpal -mcpu=gfx800 %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-PAL --check-prefix=GFX800 %s
6+
// RUN: llvm-mc -triple amdgcn-amd-amdpal -mcpu=iceland %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-PAL --check-prefix=GFX800 %s
7+
// 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
8+
9+
// OSABI-PAL: .amd_amdgpu_isa "amdgcn-amd-amdpal--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-amdpal--gfx800"

‎llvm/test/MC/AMDGPU/isa-version-unk.s

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: llvm-mc -triple amdgcn-amd-unknown -mcpu=gfx800 %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-UNK --check-prefix=GFX800 %s
2+
// RUN: llvm-mc -triple amdgcn-amd-unknown -mcpu=iceland %s | FileCheck --check-prefix=GCN --check-prefix=OSABI-UNK --check-prefix=GFX800 %s
3+
// 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
4+
// 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
5+
// 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
6+
// 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
7+
// 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
8+
9+
10+
// OSABI-UNK: .amd_amdgpu_isa "amdgcn-amd-unknown--gfx800"
11+
// OSABI-UNK-ERR: error: .amd_amdgpu_isa directive does not match triple and/or mcpu arguments specified through the command line
12+
// OSABI-HSA-ERR: error: .amd_amdgpu_isa directive does not match triple and/or mcpu arguments specified through the command line
13+
// OSABI-PAL-ERR: error: .amd_amdgpu_isa directive does not match triple and/or mcpu arguments specified through the command line
14+
.amd_amdgpu_isa "amdgcn-amd-unknown--gfx800"

0 commit comments

Comments
 (0)
Please sign in to comment.