Skip to content

Commit a5f6db4

Browse files
authored
[AMDGPU] Print high vgpr operand comments from objdump (#156966)
This followed the agreed convention: every basic block shall start with all MSBs zero. Codegen does the same lowering.
1 parent fffe93a commit a5f6db4

File tree

4 files changed

+89
-22
lines changed

4 files changed

+89
-22
lines changed

llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUInstPrinter.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,42 @@ static MCPhysReg getRegForPrinting(MCPhysReg Reg, const MCRegisterInfo &MRI) {
336336
return RC->getRegister(Idx % 0x100);
337337
}
338338

339+
// Restore MSBs of a VGPR above 255 from the MCInstrAnalysis.
340+
static MCPhysReg getRegFromMIA(MCPhysReg Reg, unsigned OpNo,
341+
const MCInstrDesc &Desc,
342+
const MCRegisterInfo &MRI,
343+
const AMDGPUMCInstrAnalysis &MIA) {
344+
unsigned VgprMSBs = MIA.getVgprMSBs();
345+
if (!VgprMSBs)
346+
return Reg;
347+
348+
unsigned Enc = MRI.getEncodingValue(Reg);
349+
if (!(Enc & AMDGPU::HWEncoding::IS_VGPR))
350+
return Reg;
351+
352+
auto Ops = AMDGPU::getVGPRLoweringOperandTables(Desc);
353+
if (!Ops.first)
354+
return Reg;
355+
unsigned Opc = Desc.getOpcode();
356+
unsigned I;
357+
for (I = 0; I < 4; ++I) {
358+
if (Ops.first[I] != AMDGPU::OpName::NUM_OPERAND_NAMES &&
359+
(unsigned)AMDGPU::getNamedOperandIdx(Opc, Ops.first[I]) == OpNo)
360+
break;
361+
if (Ops.second && Ops.second[I] != AMDGPU::OpName::NUM_OPERAND_NAMES &&
362+
(unsigned)AMDGPU::getNamedOperandIdx(Opc, Ops.second[I]) == OpNo)
363+
break;
364+
}
365+
if (I == 4)
366+
return Reg;
367+
unsigned OpMSBs = (VgprMSBs >> (I * 2)) & 3;
368+
if (!OpMSBs)
369+
return Reg;
370+
if (MCRegister NewReg = AMDGPU::getVGPRWithMSBs(Reg, OpMSBs, MRI))
371+
return NewReg;
372+
return Reg;
373+
}
374+
339375
void AMDGPUInstPrinter::printRegOperand(MCRegister Reg, raw_ostream &O,
340376
const MCRegisterInfo &MRI) {
341377
#if !defined(NDEBUG)
@@ -359,6 +395,9 @@ void AMDGPUInstPrinter::printRegOperand(MCRegister Reg, raw_ostream &O,
359395
void AMDGPUInstPrinter::printRegOperand(MCRegister Reg, unsigned Opc,
360396
unsigned OpNo, raw_ostream &O,
361397
const MCRegisterInfo &MRI) {
398+
if (MIA)
399+
Reg = getRegFromMIA(Reg, OpNo, MII.get(Opc), MRI,
400+
*static_cast<const AMDGPUMCInstrAnalysis *>(MIA));
362401
printRegOperand(Reg, O, MRI);
363402
}
364403

llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUMCTargetDesc.cpp

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
#include "TargetInfo/AMDGPUTargetInfo.h"
2222
#include "llvm/MC/MCAsmBackend.h"
2323
#include "llvm/MC/MCCodeEmitter.h"
24+
#include "llvm/MC/MCContext.h"
2425
#include "llvm/MC/MCELFStreamer.h"
2526
#include "llvm/MC/MCInstPrinter.h"
26-
#include "llvm/MC/MCInstrAnalysis.h"
2727
#include "llvm/MC/MCInstrDesc.h"
2828
#include "llvm/MC/MCInstrInfo.h"
2929
#include "llvm/MC/MCObjectWriter.h"
@@ -130,31 +130,35 @@ static MCStreamer *createMCStreamer(const Triple &T, MCContext &Context,
130130
std::move(Emitter));
131131
}
132132

133-
namespace {
134-
135-
class AMDGPUMCInstrAnalysis : public MCInstrAnalysis {
136-
public:
137-
explicit AMDGPUMCInstrAnalysis(const MCInstrInfo *Info)
138-
: MCInstrAnalysis(Info) {}
139-
140-
bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
141-
uint64_t &Target) const override {
142-
if (Inst.getNumOperands() == 0 || !Inst.getOperand(0).isImm() ||
143-
Info->get(Inst.getOpcode()).operands()[0].OperandType !=
144-
MCOI::OPERAND_PCREL)
145-
return false;
133+
namespace llvm {
134+
namespace AMDGPU {
135+
136+
bool AMDGPUMCInstrAnalysis::evaluateBranch(const MCInst &Inst, uint64_t Addr,
137+
uint64_t Size,
138+
uint64_t &Target) const {
139+
if (Inst.getNumOperands() == 0 || !Inst.getOperand(0).isImm() ||
140+
Info->get(Inst.getOpcode()).operands()[0].OperandType !=
141+
MCOI::OPERAND_PCREL)
142+
return false;
143+
144+
int64_t Imm = Inst.getOperand(0).getImm();
145+
// Our branches take a simm16.
146+
Target = SignExtend64<16>(Imm) * 4 + Addr + Size;
147+
return true;
148+
}
146149

147-
int64_t Imm = Inst.getOperand(0).getImm();
148-
// Our branches take a simm16.
149-
Target = SignExtend64<16>(Imm) * 4 + Addr + Size;
150-
return true;
151-
}
152-
};
150+
void AMDGPUMCInstrAnalysis::updateState(const MCInst &Inst, uint64_t Addr) {
151+
if (Inst.getOpcode() == AMDGPU::S_SET_VGPR_MSB_gfx12)
152+
VgprMSBs = Inst.getOperand(0).getImm();
153+
else if (isTerminator(Inst))
154+
VgprMSBs = 0;
155+
}
153156

154-
} // end anonymous namespace
157+
} // end namespace AMDGPU
158+
} // end namespace llvm
155159

156160
static MCInstrAnalysis *createAMDGPUMCInstrAnalysis(const MCInstrInfo *Info) {
157-
return new AMDGPUMCInstrAnalysis(Info);
161+
return new AMDGPU::AMDGPUMCInstrAnalysis(Info);
158162
}
159163

160164
extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void

llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUMCTargetDesc.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUMCTARGETDESC_H
1616
#define LLVM_LIB_TARGET_AMDGPU_MCTARGETDESC_AMDGPUMCTARGETDESC_H
1717

18+
#include "llvm/MC/MCInstrAnalysis.h"
1819
#include <cstdint>
1920
#include <memory>
2021

@@ -44,6 +45,28 @@ MCAsmBackend *createAMDGPUAsmBackend(const Target &T,
4445
std::unique_ptr<MCObjectTargetWriter>
4546
createAMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI,
4647
bool HasRelocationAddend);
48+
49+
namespace AMDGPU {
50+
class AMDGPUMCInstrAnalysis : public MCInstrAnalysis {
51+
private:
52+
unsigned VgprMSBs = 0;
53+
54+
public:
55+
explicit AMDGPUMCInstrAnalysis(const MCInstrInfo *Info)
56+
: MCInstrAnalysis(Info) {}
57+
58+
bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
59+
uint64_t &Target) const override;
60+
61+
void resetState() override { VgprMSBs = 0; }
62+
63+
void updateState(const MCInst &Inst, uint64_t Addr) override;
64+
65+
unsigned getVgprMSBs() const { return VgprMSBs; }
66+
};
67+
68+
} // namespace AMDGPU
69+
4770
} // namespace llvm
4871

4972
#define GET_REGINFO_ENUM

llvm/test/CodeGen/AMDGPU/vgpr-lowering-gfx1250.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -mtriple=amdgcn -mcpu=gfx1250 -start-before=amdgpu-lower-vgpr-encoding -o - %s | FileCheck -check-prefixes=GCN,ASM %s
2+
# RUN: llc -mtriple=amdgcn -mcpu=gfx1250 -start-before=amdgpu-lower-vgpr-encoding -o - %s | llvm-mc -triple=amdgcn -mcpu=gfx1250 -filetype=obj -o - | llvm-objdump -d --mcpu=gfx1250 - | FileCheck -check-prefixes=GCN,DIS %s
23

34
# ASM-LABEL: {{^}}high_vgprs:
45
# DIS-LABEL: <high_vgprs>:

0 commit comments

Comments
 (0)