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;
95 return 3;
96 case FK_SecRel_1:
97 case FK_Data_1:
98 return 1;
99 case FK_SecRel_2:
100 case FK_Data_2:
101 return 2;
102 case FK_SecRel_4:
103 case FK_Data_4:
104 return 4;
105 case FK_SecRel_8:
106 case FK_Data_8:
107 return 8;
108 default:
109 llvm_unreachable("Unknown fixup kind!");
110 }
111}
112
114 MCContext *Ctx) {
115 int64_t SignedValue = static_cast<int64_t>(Value);
116
117 switch (Fixup.getKind()) {
119 int64_t BrImm = (SignedValue - 4) / 4;
120
121 if (Ctx && !isInt<16>(BrImm))
122 Ctx->reportError(Fixup.getLoc(), "branch size exceeds simm16");
123
124 return BrImm;
125 }
127 // SignedValue is label - (PC + 4): the fixup sits at byte 4 of the
128 // instruction. s_prefetch_inst_pc_rel is 8 bytes and the field is a signed
129 // byte offset from the next instruction, so subtract the remaining 4.
130 int64_t OffImm = SignedValue - 4;
131
132 if (Ctx && !isInt<24>(OffImm)) {
133 Ctx->reportError(Fixup.getLoc(),
134 "prefetch offset exceeds 24-bit signed range");
135 }
136
137 return OffImm;
138 }
139 case FK_Data_1:
140 case FK_Data_2:
141 case FK_Data_4:
142 case FK_Data_8:
143 case FK_SecRel_4:
144 return Value;
145 default:
146 llvm_unreachable("unhandled fixup kind");
147 }
148}
149
150void AMDGPUAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
151 const MCValue &Target, uint8_t *Data,
152 uint64_t Value, bool IsResolved) {
153 if (Target.getSpecifier())
154 IsResolved = false;
155 maybeAddReloc(F, Fixup, Target, Value, IsResolved);
156 if (mc::isRelocation(Fixup.getKind()))
157 return;
158
160 if (!Value)
161 return; // Doesn't change encoding.
162
163 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
164
165 // Shift the value into position.
166 Value <<= Info.TargetOffset;
167
168 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
169 assert(Fixup.getOffset() + NumBytes <= F.getSize() &&
170 "Invalid fixup offset!");
171
172 // For each byte of the fragment that the fixup touches, mask in the bits from
173 // the fixup value.
174 for (unsigned i = 0; i != NumBytes; ++i)
175 Data[i] |= static_cast<uint8_t>((Value >> (i * 8)) & 0xff);
176}
177
178std::optional<MCFixupKind>
179AMDGPUAsmBackend::getFixupKind(StringRef Name) const {
180 auto Type = StringSwitch<unsigned>(Name)
181#define ELF_RELOC(Name, Value) .Case(#Name, Value)
182#include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
183#undef ELF_RELOC
184 .Case("BFD_RELOC_NONE", ELF::R_AMDGPU_NONE)
185 .Case("BFD_RELOC_32", ELF::R_AMDGPU_ABS32)
186 .Case("BFD_RELOC_64", ELF::R_AMDGPU_ABS64)
187 .Default(~0u);
188 if (Type != ~0u)
189 return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
190 return std::nullopt;
191}
192
193MCFixupKindInfo AMDGPUAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
194 const static MCFixupKindInfo Infos[AMDGPU::NumTargetFixupKinds] = {
195 // name offset bits flags
196 {"fixup_si_sopp_br", 0, 16, 0},
197 {"fixup_si_smem_pcrel", 0, 24, 0},
198 };
199
200 if (mc::isRelocation(Kind))
201 return {};
202
203 if (Kind < FirstTargetFixupKind)
205
207 "Invalid kind!");
208 return Infos[Kind - FirstTargetFixupKind];
209}
210
211unsigned AMDGPUAsmBackend::getMinimumNopSize() const {
212 return 4;
213}
214
215bool AMDGPUAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
216 const MCSubtargetInfo *STI) const {
217 // If the count is not aligned to the minimum instruction alignment, we must
218 // be writing data into the text section (otherwise we have unaligned
219 // instructions, and thus have far bigger problems), so just write zeros
220 // instead.
221 unsigned MinInstAlignment = getContext().getAsmInfo().getMinInstAlignment();
222 OS.write_zeros(Count % MinInstAlignment);
223
224 // We are properly aligned, so write NOPs as requested.
225 Count /= MinInstAlignment;
226
227 // FIXME: R600 support.
228 // s_nop 0
229 const uint32_t Encoded_S_NOP_0 = 0xbf800000;
230
231 assert(MinInstAlignment == sizeof(Encoded_S_NOP_0));
232 for (uint64_t I = 0; I != Count; ++I)
233 support::endian::write<uint32_t>(OS, Encoded_S_NOP_0, Endian);
234
235 return true;
236}
237
238//===----------------------------------------------------------------------===//
239// ELFAMDGPUAsmBackend class
240//===----------------------------------------------------------------------===//
241
242namespace {
243
244class ELFAMDGPUAsmBackend : public AMDGPUAsmBackend {
245 bool Is64Bit;
246 bool HasRelocationAddend;
247 uint8_t OSABI = ELF::ELFOSABI_NONE;
248
249public:
250 ELFAMDGPUAsmBackend(const Target &T, const Triple &TT)
251 : AMDGPUAsmBackend(T), Is64Bit(TT.isAMDGCN()),
252 HasRelocationAddend(TT.getOS() == Triple::AMDHSA) {
253 switch (TT.getOS()) {
254 case Triple::AMDHSA:
255 OSABI = ELF::ELFOSABI_AMDGPU_HSA;
256 break;
257 case Triple::AMDPAL:
258 OSABI = ELF::ELFOSABI_AMDGPU_PAL;
259 break;
260 case Triple::Mesa3D:
261 OSABI = ELF::ELFOSABI_AMDGPU_MESA3D;
262 break;
263 default:
264 break;
265 }
266 }
267
268 std::unique_ptr<MCObjectTargetWriter>
269 createObjectTargetWriter() const override {
270 return createAMDGPUELFObjectWriter(Is64Bit, OSABI, HasRelocationAddend);
271 }
272};
273
274} // end anonymous namespace
275
277 const MCSubtargetInfo &STI,
278 const MCRegisterInfo &MRI,
279 const MCTargetOptions &Options) {
280 return new ELFAMDGPUAsmBackend(T, STI.getTargetTriple());
281}
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!")
uint64_t 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
MachineRegisterInfo & MRI
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:84
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition MCFixup.h:65
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.
@ fixup_si_smem_pcrel
24-bit PC-relative byte offset for s_prefetch_inst_pc_rel.
LLVM_READONLY int32_t getSOPPWithRelaxation(uint32_t Opcode)
@ ELFOSABI_NONE
Definition ELF.h:154
VE::Fixups getFixupKind(uint8_t S)
bool isRelocation(MCFixupKind FixupKind)
Definition MCFixup.h:134
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)
constexpr MCFixupKind FirstTargetFixupKind
Definition MCFixup.h:49
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
@ FK_SecRel_2
A two-byte section relative fixup.
Definition MCFixup.h:44
@ FK_Data_8
A eight-byte fixup.
Definition MCFixup.h:41
@ FK_Data_1
A one-byte fixup.
Definition MCFixup.h:38
@ FK_Data_4
A four-byte fixup.
Definition MCFixup.h:40
@ FK_SecRel_8
A eight-byte section relative fixup.
Definition MCFixup.h:46
@ FK_SecRel_4
A four-byte section relative fixup.
Definition MCFixup.h:45
@ FK_SecRel_1
A one-byte section relative fixup.
Definition MCFixup.h:43
@ FK_Data_2
A two-byte fixup.
Definition MCFixup.h:39
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
ArrayRef(const T &OneElt) -> ArrayRef< T >
endianness
Definition bit.h:71
constexpr MCFixupKind FirstLiteralRelocationKind
Definition MCFixup.h:32