LLVM 24.0.0git
AMDGPUAsmBackend.cpp
Go to the documentation of this file.
1//===-- AMDGPUAsmBackend.cpp - AMDGPU Assembler Backend -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7/// \file
8//===----------------------------------------------------------------------===//
9
15#include "llvm/MC/MCAsmInfo.h"
16#include "llvm/MC/MCAssembler.h"
17#include "llvm/MC/MCContext.h"
20#include "llvm/MC/MCValue.h"
24
25using namespace llvm;
26using namespace llvm::AMDGPU;
27
28namespace {
29
30class AMDGPUAsmBackend : public MCAsmBackend {
31public:
32 AMDGPUAsmBackend(const Target &T) : MCAsmBackend(llvm::endianness::little) {}
33
34 void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
35 uint8_t *Data, uint64_t Value, bool IsResolved) override;
36 bool fixupNeedsRelaxationAdvanced(const MCFragment &, const MCFixup &,
37 const MCValue &, uint64_t,
38 bool) const override;
39
40 void relaxInstruction(MCInst &Inst,
41 const MCSubtargetInfo &STI) const override;
42
43 bool mayNeedRelaxation(unsigned Opcode, ArrayRef<MCOperand> Operands,
44 const MCSubtargetInfo &STI) const override;
45
46 unsigned getMinimumNopSize() const override;
47 bool writeNopData(raw_ostream &OS, uint64_t Count,
48 const MCSubtargetInfo *STI) const override;
49
50 std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
51 MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
52};
53
54} //End anonymous namespace
55
56void AMDGPUAsmBackend::relaxInstruction(MCInst &Inst,
57 const MCSubtargetInfo &STI) const {
58 MCInst Res;
59 unsigned RelaxedOpcode = AMDGPU::getSOPPWithRelaxation(Inst.getOpcode());
60 Res.setOpcode(RelaxedOpcode);
61 Res.addOperand(Inst.getOperand(0));
62 Inst = std::move(Res);
63}
64
65bool AMDGPUAsmBackend::fixupNeedsRelaxationAdvanced(const MCFragment &,
66 const MCFixup &Fixup,
67 const MCValue &,
69 bool Resolved) const {
70 if (!Resolved)
71 return true;
72 // if the branch target has an offset of x3f this needs to be relaxed to
73 // add a s_nop 0 immediately after branch to effectively increment offset
74 // for hardware workaround in gfx1010
75 return (((int64_t(Value)/4)-1) == 0x3f);
76}
77
78bool AMDGPUAsmBackend::mayNeedRelaxation(unsigned Opcode,
80 const MCSubtargetInfo &STI) const {
81 if (!STI.hasFeature(AMDGPU::FeatureOffset3fBug))
82 return false;
83
84 if (AMDGPU::getSOPPWithRelaxation(Opcode) >= 0)
85 return true;
86
87 return false;
88}
89
90static unsigned getFixupKindNumBytes(unsigned Kind) {
91 switch (Kind) {
93 return 2;
94 case FK_SecRel_1:
95 case FK_Data_1:
96 return 1;
97 case FK_SecRel_2:
98 case FK_Data_2:
99 return 2;
100 case FK_SecRel_4:
101 case FK_Data_4:
102 return 4;
103 case FK_SecRel_8:
104 case FK_Data_8:
105 return 8;
106 default:
107 llvm_unreachable("Unknown fixup kind!");
108 }
109}
110
112 MCContext *Ctx) {
113 int64_t SignedValue = static_cast<int64_t>(Value);
114
115 switch (Fixup.getKind()) {
117 int64_t BrImm = (SignedValue - 4) / 4;
118
119 if (Ctx && !isInt<16>(BrImm))
120 Ctx->reportError(Fixup.getLoc(), "branch size exceeds simm16");
121
122 return BrImm;
123 }
124 case FK_Data_1:
125 case FK_Data_2:
126 case FK_Data_4:
127 case FK_Data_8:
128 case FK_SecRel_4:
129 return Value;
130 default:
131 llvm_unreachable("unhandled fixup kind");
132 }
133}
134
135void AMDGPUAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
136 const MCValue &Target, uint8_t *Data,
137 uint64_t Value, bool IsResolved) {
138 if (Target.getSpecifier())
139 IsResolved = false;
140 maybeAddReloc(F, Fixup, Target, Value, IsResolved);
141 if (mc::isRelocation(Fixup.getKind()))
142 return;
143
145 if (!Value)
146 return; // Doesn't change encoding.
147
148 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
149
150 // Shift the value into position.
151 Value <<= Info.TargetOffset;
152
153 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
154 assert(Fixup.getOffset() + NumBytes <= F.getSize() &&
155 "Invalid fixup offset!");
156
157 // For each byte of the fragment that the fixup touches, mask in the bits from
158 // the fixup value.
159 for (unsigned i = 0; i != NumBytes; ++i)
160 Data[i] |= static_cast<uint8_t>((Value >> (i * 8)) & 0xff);
161}
162
163std::optional<MCFixupKind>
164AMDGPUAsmBackend::getFixupKind(StringRef Name) const {
165 auto Type = StringSwitch<unsigned>(Name)
166#define ELF_RELOC(Name, Value) .Case(#Name, Value)
167#include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
168#undef ELF_RELOC
169 .Case("BFD_RELOC_NONE", ELF::R_AMDGPU_NONE)
170 .Case("BFD_RELOC_32", ELF::R_AMDGPU_ABS32)
171 .Case("BFD_RELOC_64", ELF::R_AMDGPU_ABS64)
172 .Default(-1u);
173 if (Type != -1u)
174 return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
175 return std::nullopt;
176}
177
178MCFixupKindInfo AMDGPUAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
179 const static MCFixupKindInfo Infos[AMDGPU::NumTargetFixupKinds] = {
180 // name offset bits flags
181 {"fixup_si_sopp_br", 0, 16, 0},
182 };
183
184 if (mc::isRelocation(Kind))
185 return {};
186
187 if (Kind < FirstTargetFixupKind)
189
191 "Invalid kind!");
192 return Infos[Kind - FirstTargetFixupKind];
193}
194
195unsigned AMDGPUAsmBackend::getMinimumNopSize() const {
196 return 4;
197}
198
199bool AMDGPUAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
200 const MCSubtargetInfo *STI) const {
201 // If the count is not aligned to the minimum instruction alignment, we must
202 // be writing data into the text section (otherwise we have unaligned
203 // instructions, and thus have far bigger problems), so just write zeros
204 // instead.
205 unsigned MinInstAlignment = getContext().getAsmInfo().getMinInstAlignment();
206 OS.write_zeros(Count % MinInstAlignment);
207
208 // We are properly aligned, so write NOPs as requested.
209 Count /= MinInstAlignment;
210
211 // FIXME: R600 support.
212 // s_nop 0
213 const uint32_t Encoded_S_NOP_0 = 0xbf800000;
214
215 assert(MinInstAlignment == sizeof(Encoded_S_NOP_0));
216 for (uint64_t I = 0; I != Count; ++I)
217 support::endian::write<uint32_t>(OS, Encoded_S_NOP_0, Endian);
218
219 return true;
220}
221
222//===----------------------------------------------------------------------===//
223// ELFAMDGPUAsmBackend class
224//===----------------------------------------------------------------------===//
225
226namespace {
227
228class ELFAMDGPUAsmBackend : public AMDGPUAsmBackend {
229 bool Is64Bit;
230 bool HasRelocationAddend;
231 uint8_t OSABI = ELF::ELFOSABI_NONE;
232
233public:
234 ELFAMDGPUAsmBackend(const Target &T, const Triple &TT)
235 : AMDGPUAsmBackend(T), Is64Bit(TT.isAMDGCN()),
236 HasRelocationAddend(TT.getOS() == Triple::AMDHSA) {
237 switch (TT.getOS()) {
238 case Triple::AMDHSA:
239 OSABI = ELF::ELFOSABI_AMDGPU_HSA;
240 break;
241 case Triple::AMDPAL:
242 OSABI = ELF::ELFOSABI_AMDGPU_PAL;
243 break;
244 case Triple::Mesa3D:
245 OSABI = ELF::ELFOSABI_AMDGPU_MESA3D;
246 break;
247 default:
248 break;
249 }
250 }
251
252 std::unique_ptr<MCObjectTargetWriter>
253 createObjectTargetWriter() const override {
254 return createAMDGPUELFObjectWriter(Is64Bit, OSABI, HasRelocationAddend);
255 }
256};
257
258} // end anonymous namespace
259
261 const MCSubtargetInfo &STI,
262 const MCRegisterInfo &MRI,
263 const MCTargetOptions &Options) {
264 return new ELFAMDGPUAsmBackend(T, STI.getTargetTriple());
265}
static unsigned getFixupKindNumBytes(unsigned Kind)
The number of bytes the fixup may change.
static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target, uint64_t Value, MCContext &Ctx, const Triple &TheTriple, bool IsResolved)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
Provides AMDGPU specific target descriptions.
static LVOptions Options
Definition LVOptions.cpp:25
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
PowerPC TLS Dynamic Call Fixup
SI Fold Operands
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
Generic interface to target specific assembler backends.
virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
Context object for machine code objects.
Definition MCContext.h:83
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition MCFixup.h:61
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
unsigned getOpcode() const
Definition MCInst.h:202
void addOperand(const MCOperand Op)
Definition MCInst.h:215
void setOpcode(unsigned Op)
Definition MCInst.h:201
const MCOperand & getOperand(unsigned i) const
Definition MCInst.h:210
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
const Triple & getTargetTriple() const
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition Value.h:75
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ fixup_si_sopp_br
16-bit PC relative fixup for SOPP branch instructions.
LLVM_READONLY int32_t getSOPPWithRelaxation(uint32_t Opcode)
@ ELFOSABI_NONE
Definition ELF.h:346
VE::Fixups getFixupKind(uint8_t S)
bool isRelocation(MCFixupKind FixupKind)
Definition MCFixup.h:130
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition Endian.h:82
This is an optimization pass for GlobalISel generic memory operations.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
MCAsmBackend * createAMDGPUAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
std::unique_ptr< MCObjectTargetWriter > createAMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI, bool HasRelocationAddend)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
@ FirstTargetFixupKind
Definition MCFixup.h:44
@ FK_SecRel_2
A two-byte section relative fixup.
Definition MCFixup.h:40
@ FirstLiteralRelocationKind
Definition MCFixup.h:29
@ FK_Data_8
A eight-byte fixup.
Definition MCFixup.h:37
@ FK_Data_1
A one-byte fixup.
Definition MCFixup.h:34
@ FK_Data_4
A four-byte fixup.
Definition MCFixup.h:36
@ FK_SecRel_8
A eight-byte section relative fixup.
Definition MCFixup.h:42
@ FK_SecRel_4
A four-byte section relative fixup.
Definition MCFixup.h:41
@ FK_SecRel_1
A one-byte section relative fixup.
Definition MCFixup.h:39
@ FK_Data_2
A two-byte fixup.
Definition MCFixup.h:35
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
ArrayRef(const T &OneElt) -> ArrayRef< T >
endianness
Definition bit.h:71