2012-12-12 05:25:42 +08:00
|
|
|
//===-- AMDGPUAsmBackend.cpp - AMDGPU Assembler Backend -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-07-21 22:01:08 +08:00
|
|
|
#include "MCTargetDesc/AMDGPUFixupKinds.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-10-05 06:44:13 +08:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/MC/MCAsmBackend.h"
|
|
|
|
#include "llvm/MC/MCAssembler.h"
|
2016-08-27 08:21:22 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2014-07-21 22:01:08 +08:00
|
|
|
#include "llvm/MC/MCFixupKindInfo.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/MC/MCObjectWriter.h"
|
|
|
|
#include "llvm/MC/MCValue.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class AMDGPUAsmBackend : public MCAsmBackend {
|
|
|
|
public:
|
2018-05-22 01:57:19 +08:00
|
|
|
AMDGPUAsmBackend(const Target &T) : MCAsmBackend(support::little) {}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2014-07-21 22:01:14 +08:00
|
|
|
unsigned getNumFixupKinds() const override { return AMDGPU::NumTargetFixupKinds; };
|
2016-08-27 08:21:22 +08:00
|
|
|
|
2017-06-24 06:52:36 +08:00
|
|
|
void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
|
|
|
|
const MCValue &Target, MutableArrayRef<char> Data,
|
2018-06-06 17:40:06 +08:00
|
|
|
uint64_t Value, bool IsResolved,
|
|
|
|
const MCSubtargetInfo *STI) const override;
|
2014-04-29 15:57:24 +08:00
|
|
|
bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
|
|
|
|
const MCRelaxableFragment *DF,
|
|
|
|
const MCAsmLayout &Layout) const override {
|
2012-12-12 05:25:42 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-07-11 22:23:53 +08:00
|
|
|
void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
|
|
|
|
MCInst &Res) const override {
|
2016-12-23 09:38:06 +08:00
|
|
|
llvm_unreachable("Not implemented");
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2018-06-06 17:40:06 +08:00
|
|
|
bool mayNeedRelaxation(const MCInst &Inst,
|
|
|
|
const MCSubtargetInfo &STI) const override {
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-08 06:00:58 +08:00
|
|
|
|
|
|
|
unsigned getMinimumNopSize() const override;
|
2018-05-22 01:57:19 +08:00
|
|
|
bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
|
2014-07-21 22:01:08 +08:00
|
|
|
|
|
|
|
const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
|
2012-12-12 05:25:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} //End anonymous namespace
|
|
|
|
|
2015-07-31 01:03:08 +08:00
|
|
|
static unsigned getFixupKindNumBytes(unsigned Kind) {
|
|
|
|
switch (Kind) {
|
2016-08-27 08:21:22 +08:00
|
|
|
case AMDGPU::fixup_si_sopp_br:
|
|
|
|
return 2;
|
2016-05-14 04:39:26 +08:00
|
|
|
case FK_SecRel_1:
|
2015-07-31 01:03:08 +08:00
|
|
|
case FK_Data_1:
|
|
|
|
return 1;
|
2016-05-14 04:39:26 +08:00
|
|
|
case FK_SecRel_2:
|
2015-07-31 01:03:08 +08:00
|
|
|
case FK_Data_2:
|
|
|
|
return 2;
|
2016-05-14 04:39:26 +08:00
|
|
|
case FK_SecRel_4:
|
2015-07-31 01:03:08 +08:00
|
|
|
case FK_Data_4:
|
2016-06-15 04:29:59 +08:00
|
|
|
case FK_PCRel_4:
|
2015-07-31 01:03:08 +08:00
|
|
|
return 4;
|
2016-05-14 04:39:26 +08:00
|
|
|
case FK_SecRel_8:
|
2015-07-31 01:03:08 +08:00
|
|
|
case FK_Data_8:
|
|
|
|
return 8;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown fixup kind!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-27 08:21:22 +08:00
|
|
|
static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
|
|
|
|
MCContext *Ctx) {
|
|
|
|
int64_t SignedValue = static_cast<int64_t>(Value);
|
|
|
|
|
2017-08-09 18:38:53 +08:00
|
|
|
switch (static_cast<unsigned>(Fixup.getKind())) {
|
2016-08-27 08:21:22 +08:00
|
|
|
case AMDGPU::fixup_si_sopp_br: {
|
|
|
|
int64_t BrImm = (SignedValue - 4) / 4;
|
|
|
|
|
|
|
|
if (Ctx && !isInt<16>(BrImm))
|
|
|
|
Ctx->reportError(Fixup.getLoc(), "branch size exceeds simm16");
|
|
|
|
|
|
|
|
return BrImm;
|
|
|
|
}
|
|
|
|
case FK_Data_1:
|
|
|
|
case FK_Data_2:
|
|
|
|
case FK_Data_4:
|
|
|
|
case FK_Data_8:
|
|
|
|
case FK_PCRel_4:
|
2016-08-30 03:01:48 +08:00
|
|
|
case FK_SecRel_4:
|
2016-08-27 08:21:22 +08:00
|
|
|
return Value;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unhandled fixup kind");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-24 06:52:36 +08:00
|
|
|
void AMDGPUAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
|
|
|
|
const MCValue &Target,
|
2017-06-22 07:06:53 +08:00
|
|
|
MutableArrayRef<char> Data, uint64_t Value,
|
2018-06-06 17:40:06 +08:00
|
|
|
bool IsResolved,
|
|
|
|
const MCSubtargetInfo *STI) const {
|
2017-06-24 07:05:15 +08:00
|
|
|
Value = adjustFixupValue(Fixup, Value, &Asm.getContext());
|
2016-08-27 08:21:22 +08:00
|
|
|
if (!Value)
|
|
|
|
return; // Doesn't change encoding.
|
2012-12-17 23:14:54 +08:00
|
|
|
|
2016-08-27 08:21:22 +08:00
|
|
|
MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
|
|
|
|
|
|
|
|
// Shift the value into position.
|
|
|
|
Value <<= Info.TargetOffset;
|
|
|
|
|
[AMDGPU] Fix s_branch with -1 offset
Summary:
In case s_branch instruction target is itself backend should emit offset -1 but instead it emit 0.
'''
label:
s_branch label // should emit [0xff,0xff,0x82,0xbf]
'''
Tom, Matt: why are we adjusting fixup values in applyFixup() method instead of processFixup()? processFixup() is calling adjustFixupValue() but does nothing with its result.
Reviewers: vpykhtin, artem.tamazov, tstellarAMD
Subscribers: arsenm, kzhuravl, wdng, nhaehnle, yaxunl
Differential Revision: https://reviews.llvm.org/D24671
llvm-svn: 281896
2016-09-19 18:20:55 +08:00
|
|
|
unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
|
2016-08-27 08:21:22 +08:00
|
|
|
uint32_t Offset = Fixup.getOffset();
|
2017-06-22 07:06:53 +08:00
|
|
|
assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
|
2016-08-27 08:21:22 +08:00
|
|
|
|
|
|
|
// For each byte of the fragment that the fixup touches, mask in the bits from
|
|
|
|
// the fixup value.
|
|
|
|
for (unsigned i = 0; i != NumBytes; ++i)
|
|
|
|
Data[Offset + i] |= static_cast<uint8_t>((Value >> (i * 8)) & 0xff);
|
2012-12-17 23:14:54 +08:00
|
|
|
}
|
2013-04-16 01:51:21 +08:00
|
|
|
|
2014-07-21 22:01:08 +08:00
|
|
|
const MCFixupKindInfo &AMDGPUAsmBackend::getFixupKindInfo(
|
|
|
|
MCFixupKind Kind) const {
|
|
|
|
const static MCFixupKindInfo Infos[AMDGPU::NumTargetFixupKinds] = {
|
|
|
|
// name offset bits flags
|
2014-07-21 22:01:14 +08:00
|
|
|
{ "fixup_si_sopp_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
|
2014-07-21 22:01:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (Kind < FirstTargetFixupKind)
|
|
|
|
return MCAsmBackend::getFixupKindInfo(Kind);
|
|
|
|
|
|
|
|
return Infos[Kind - FirstTargetFixupKind];
|
|
|
|
}
|
|
|
|
|
2017-08-08 06:00:58 +08:00
|
|
|
unsigned AMDGPUAsmBackend::getMinimumNopSize() const {
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2018-05-22 01:57:19 +08:00
|
|
|
bool AMDGPUAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const {
|
2017-04-09 05:28:38 +08:00
|
|
|
// If the count is not 4-byte aligned, we must be writing data into the text
|
|
|
|
// section (otherwise we have unaligned instructions, and thus have far
|
|
|
|
// bigger problems), so just write zeros instead.
|
2018-05-22 01:57:19 +08:00
|
|
|
OS.write_zeros(Count % 4);
|
2017-04-09 05:28:38 +08:00
|
|
|
|
|
|
|
// We are properly aligned, so write NOPs as requested.
|
|
|
|
Count /= 4;
|
|
|
|
|
|
|
|
// FIXME: R600 support.
|
|
|
|
// s_nop 0
|
|
|
|
const uint32_t Encoded_S_NOP_0 = 0xbf800000;
|
|
|
|
|
|
|
|
for (uint64_t I = 0; I != Count; ++I)
|
2018-05-22 01:57:19 +08:00
|
|
|
support::endian::write<uint32_t>(OS, Encoded_S_NOP_0, Endian);
|
2014-10-04 03:02:02 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-16 01:51:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ELFAMDGPUAsmBackend class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-05-23 23:43:05 +08:00
|
|
|
namespace {
|
|
|
|
|
2013-04-16 01:51:21 +08:00
|
|
|
class ELFAMDGPUAsmBackend : public AMDGPUAsmBackend {
|
2016-06-14 23:16:35 +08:00
|
|
|
bool Is64Bit;
|
2016-06-15 04:29:59 +08:00
|
|
|
bool HasRelocationAddend;
|
2017-10-05 06:44:13 +08:00
|
|
|
uint8_t OSABI = ELF::ELFOSABI_NONE;
|
2015-06-23 05:03:54 +08:00
|
|
|
|
2013-04-16 01:51:21 +08:00
|
|
|
public:
|
2016-06-15 04:29:59 +08:00
|
|
|
ELFAMDGPUAsmBackend(const Target &T, const Triple &TT) :
|
|
|
|
AMDGPUAsmBackend(T), Is64Bit(TT.getArch() == Triple::amdgcn),
|
2017-10-05 06:44:13 +08:00
|
|
|
HasRelocationAddend(TT.getOS() == Triple::AMDHSA) {
|
|
|
|
switch (TT.getOS()) {
|
|
|
|
case Triple::AMDHSA:
|
|
|
|
OSABI = ELF::ELFOSABI_AMDGPU_HSA;
|
|
|
|
break;
|
|
|
|
case Triple::AMDPAL:
|
|
|
|
OSABI = ELF::ELFOSABI_AMDGPU_PAL;
|
|
|
|
break;
|
|
|
|
case Triple::Mesa3D:
|
|
|
|
OSABI = ELF::ELFOSABI_AMDGPU_MESA3D;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-04-16 01:51:21 +08:00
|
|
|
|
2018-05-22 03:20:29 +08:00
|
|
|
std::unique_ptr<MCObjectTargetWriter>
|
|
|
|
createObjectTargetWriter() const override {
|
|
|
|
return createAMDGPUELFObjectWriter(Is64Bit, OSABI, HasRelocationAddend);
|
2013-04-16 01:51:21 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-23 23:43:05 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2013-09-09 10:37:14 +08:00
|
|
|
MCAsmBackend *llvm::createAMDGPUAsmBackend(const Target &T,
|
2018-01-03 16:53:05 +08:00
|
|
|
const MCSubtargetInfo &STI,
|
2013-09-09 10:37:14 +08:00
|
|
|
const MCRegisterInfo &MRI,
|
2016-07-26 01:18:28 +08:00
|
|
|
const MCTargetOptions &Options) {
|
2015-06-23 05:03:54 +08:00
|
|
|
// Use 64-bit ELF for amdgcn
|
2018-01-03 16:53:05 +08:00
|
|
|
return new ELFAMDGPUAsmBackend(T, STI.getTargetTriple());
|
2013-04-16 01:51:21 +08:00
|
|
|
}
|