Skip to content

Commit 59fac51

Browse files
committed
AMDGPU/GlobalISel: Handle llvm.amdgcn.reloc.constant
1 parent 7e946d0 commit 59fac51

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,8 @@ bool AMDGPUInstructionSelector::selectG_INTRINSIC(MachineInstr &I) const {
904904
return selectIntrinsicIcmp(I);
905905
case Intrinsic::amdgcn_ballot:
906906
return selectBallot(I);
907+
case Intrinsic::amdgcn_reloc_constant:
908+
return selectRelocConstant(I);
907909
default:
908910
return selectImpl(I, *CoverageInfo);
909911
}
@@ -1084,6 +1086,31 @@ bool AMDGPUInstructionSelector::selectBallot(MachineInstr &I) const {
10841086
return true;
10851087
}
10861088

1089+
bool AMDGPUInstructionSelector::selectRelocConstant(MachineInstr &I) const {
1090+
Register DstReg = I.getOperand(0).getReg();
1091+
const RegisterBank *DstBank = RBI.getRegBank(DstReg, *MRI, TRI);
1092+
const TargetRegisterClass *DstRC =
1093+
TRI.getRegClassForSizeOnBank(32, *DstBank, *MRI);
1094+
if (!DstRC || !RBI.constrainGenericRegister(DstReg, *DstRC, *MRI))
1095+
return false;
1096+
1097+
const bool IsVALU = DstBank->getID() == AMDGPU::VGPRRegBankID;
1098+
1099+
Module *M = MF->getFunction().getParent();
1100+
const MDNode *Metadata = I.getOperand(2).getMetadata();
1101+
auto SymbolName = cast<MDString>(Metadata->getOperand(0))->getString();
1102+
auto RelocSymbol = cast<GlobalVariable>(
1103+
M->getOrInsertGlobal(SymbolName, Type::getInt32Ty(M->getContext())));
1104+
1105+
MachineBasicBlock *BB = I.getParent();
1106+
BuildMI(*BB, &I, I.getDebugLoc(),
1107+
TII.get(IsVALU ? AMDGPU::V_MOV_B32_e32 : AMDGPU::S_MOV_B32), DstReg)
1108+
.addGlobalAddress(RelocSymbol, 0, SIInstrInfo::MO_ABS32_LO);
1109+
1110+
I.eraseFromParent();
1111+
return true;
1112+
}
1113+
10871114
bool AMDGPUInstructionSelector::selectEndCfIntrinsic(MachineInstr &MI) const {
10881115
// FIXME: Manually selecting to avoid dealiing with the SReg_1 trick
10891116
// SelectionDAG uses for wave32 vs wave64.

llvm/lib/Target/AMDGPU/AMDGPUInstructionSelector.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class AMDGPUInstructionSelector final : public InstructionSelector {
108108
bool selectDivScale(MachineInstr &MI) const;
109109
bool selectIntrinsicIcmp(MachineInstr &MI) const;
110110
bool selectBallot(MachineInstr &I) const;
111+
bool selectRelocConstant(MachineInstr &I) const;
111112
bool selectG_INTRINSIC(MachineInstr &I) const;
112113

113114
bool selectEndCfIntrinsic(MachineInstr &MI) const;

llvm/lib/Target/AMDGPU/AMDGPURegisterBankInfo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4023,7 +4023,8 @@ AMDGPURegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
40234023
return getDefaultMappingAllVGPR(MI);
40244024
case Intrinsic::amdgcn_kernarg_segment_ptr:
40254025
case Intrinsic::amdgcn_s_getpc:
4026-
case Intrinsic::amdgcn_groupstaticsize: {
4026+
case Intrinsic::amdgcn_groupstaticsize:
4027+
case Intrinsic::amdgcn_reloc_constant: {
40274028
unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
40284029
OpdsMapping[0] = AMDGPU::getValueMapping(AMDGPU::SGPRRegBankID, Size);
40294030
break;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -march=amdgcn -mcpu=tahiti -run-pass=instruction-select -verify-machineinstrs -o - %s | FileCheck -check-prefix=GCN %s
3+
4+
--- |
5+
6+
define void @reloc_constant_sgpr32() { ret void }
7+
define void @reloc_constant_vgpr32() { ret void }
8+
9+
declare i32 @llvm.amdgcn.reloc.constant(metadata)
10+
11+
!0 = !{!"arst"}
12+
13+
...
14+
15+
---
16+
name: reloc_constant_sgpr32
17+
legalized: true
18+
regBankSelected: true
19+
tracksRegLiveness: true
20+
body: |
21+
bb.0:
22+
23+
; GCN-LABEL: name: reloc_constant_sgpr32
24+
; GCN: [[S_MOV_B32_:%[0-9]+]]:sreg_32 = S_MOV_B32 target-flags(amdgpu-abs32-lo) @arst
25+
; GCN: $sgpr0 = COPY [[S_MOV_B32_]]
26+
; GCN: S_ENDPGM 0, implicit $sgpr0
27+
%0:sgpr(s32) = G_INTRINSIC intrinsic(@llvm.amdgcn.reloc.constant), !0
28+
$sgpr0 = COPY %0
29+
S_ENDPGM 0, implicit $sgpr0
30+
31+
...
32+
33+
---
34+
name: reloc_constant_vgpr32
35+
legalized: true
36+
regBankSelected: true
37+
tracksRegLiveness: true
38+
body: |
39+
bb.0:
40+
41+
; GCN-LABEL: name: reloc_constant_vgpr32
42+
; GCN: [[V_MOV_B32_e32_:%[0-9]+]]:vgpr_32 = V_MOV_B32_e32 target-flags(amdgpu-abs32-lo) @arst, implicit $exec
43+
; GCN: $vgpr0 = COPY [[V_MOV_B32_e32_]]
44+
; GCN: S_ENDPGM 0, implicit $vgpr0
45+
%0:vgpr(s32) = G_INTRINSIC intrinsic(@llvm.amdgcn.reloc.constant), !0
46+
$vgpr0 = COPY %0
47+
S_ENDPGM 0, implicit $vgpr0
48+
49+
...
50+

llvm/test/CodeGen/AMDGPU/amdgpu-reloc-const.ll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
; RUN: llc -march=amdgcn -verify-machineinstrs < %s | FileCheck -check-prefix=GCN %s
22
; RUN: llc -mtriple=amdgcn--amdpal -mcpu=gfx900 -filetype=obj -o %t.o < %s && llvm-readobj -relocations %t.o | FileCheck --check-prefix=ELF %s
3+
4+
; RUN: llc -global-isel -march=amdgcn -verify-machineinstrs < %s | FileCheck -check-prefix=GCN %s
5+
; RUN: llc -global-isel -mtriple=amdgcn--amdpal -mcpu=gfx900 -filetype=obj -o %t.o < %s && llvm-readobj -relocations %t.o | FileCheck --check-prefix=ELF %s
6+
37
; GCN-LABEL: {{^}}ps_main:
48
; GCN: v_mov_b32_{{.*}} v[[relocreg:[0-9]+]], doff_0_0_b@abs32@lo
59
; GCN-NEXT: exp {{.*}} v[[relocreg]], {{.*}}

0 commit comments

Comments
 (0)