LLVM  5.0.0
AMDGPUCallLowering.cpp
Go to the documentation of this file.
1 //===-- llvm/lib/Target/AMDGPU/AMDGPUCallLowering.cpp - Call lowering -----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file implements the lowering of LLVM calls to machine code calls for
12 /// GlobalISel.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "AMDGPUCallLowering.h"
17 #include "AMDGPU.h"
18 #include "AMDGPUISelLowering.h"
19 #include "AMDGPUSubtarget.h"
20 #include "SIISelLowering.h"
21 #include "SIMachineFunctionInfo.h"
22 #include "SIRegisterInfo.h"
26 
27 using namespace llvm;
28 
29 #ifndef LLVM_BUILD_GLOBAL_ISEL
30 #error "This shouldn't be built without GISel"
31 #endif
32 
34  : CallLowering(&TLI), AMDGPUASI(TLI.getAMDGPUAS()) {
35 }
36 
38  const Value *Val, unsigned VReg) const {
39  MIRBuilder.buildInstr(AMDGPU::S_ENDPGM);
40  return true;
41 }
42 
43 unsigned AMDGPUCallLowering::lowerParameterPtr(MachineIRBuilder &MIRBuilder,
44  Type *ParamTy,
45  unsigned Offset) const {
46 
47  MachineFunction &MF = MIRBuilder.getMF();
48  const SIRegisterInfo *TRI = MF.getSubtarget<SISubtarget>().getRegisterInfo();
50  const Function &F = *MF.getFunction();
51  const DataLayout &DL = F.getParent()->getDataLayout();
52  PointerType *PtrTy = PointerType::get(ParamTy, AMDGPUASI.CONSTANT_ADDRESS);
53  LLT PtrType = getLLTForType(*PtrTy, DL);
54  unsigned DstReg = MRI.createGenericVirtualRegister(PtrType);
55  unsigned KernArgSegmentPtr =
57  unsigned KernArgSegmentVReg = MRI.getLiveInVirtReg(KernArgSegmentPtr);
58 
59  unsigned OffsetReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
60  MIRBuilder.buildConstant(OffsetReg, Offset);
61 
62  MIRBuilder.buildGEP(DstReg, KernArgSegmentVReg, OffsetReg);
63 
64  return DstReg;
65 }
66 
67 void AMDGPUCallLowering::lowerParameter(MachineIRBuilder &MIRBuilder,
68  Type *ParamTy, unsigned Offset,
69  unsigned DstReg) const {
70  MachineFunction &MF = MIRBuilder.getMF();
71  const Function &F = *MF.getFunction();
72  const DataLayout &DL = F.getParent()->getDataLayout();
73  PointerType *PtrTy = PointerType::get(ParamTy, AMDGPUASI.CONSTANT_ADDRESS);
74  MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
75  unsigned TypeSize = DL.getTypeStoreSize(ParamTy);
76  unsigned Align = DL.getABITypeAlignment(ParamTy);
77  unsigned PtrReg = lowerParameterPtr(MIRBuilder, ParamTy, Offset);
78 
79  MachineMemOperand *MMO =
83  TypeSize, Align);
84 
85  MIRBuilder.buildLoad(DstReg, PtrReg, *MMO);
86 }
87 
89  const Function &F,
90  ArrayRef<unsigned> VRegs) const {
91 
92  MachineFunction &MF = MIRBuilder.getMF();
93  const SISubtarget *Subtarget = static_cast<const SISubtarget *>(&MF.getSubtarget());
94  MachineRegisterInfo &MRI = MF.getRegInfo();
96  const SIRegisterInfo *TRI = MF.getSubtarget<SISubtarget>().getRegisterInfo();
97  const DataLayout &DL = F.getParent()->getDataLayout();
98 
100  CCState CCInfo(F.getCallingConv(), F.isVarArg(), MF, ArgLocs, F.getContext());
101 
102  // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
103  if (Info->hasPrivateSegmentBuffer()) {
104  unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
105  MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
106  CCInfo.AllocateReg(PrivateSegmentBufferReg);
107  }
108 
109  if (Info->hasDispatchPtr()) {
110  unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
111  // FIXME: Need to add reg as live-in
112  CCInfo.AllocateReg(DispatchPtrReg);
113  }
114 
115  if (Info->hasQueuePtr()) {
116  unsigned QueuePtrReg = Info->addQueuePtr(*TRI);
117  // FIXME: Need to add reg as live-in
118  CCInfo.AllocateReg(QueuePtrReg);
119  }
120 
121  if (Info->hasKernargSegmentPtr()) {
122  unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
123  const LLT P2 = LLT::pointer(2, 64);
124  unsigned VReg = MRI.createGenericVirtualRegister(P2);
125  MRI.addLiveIn(InputPtrReg, VReg);
126  MIRBuilder.getMBB().addLiveIn(InputPtrReg);
127  MIRBuilder.buildCopy(VReg, InputPtrReg);
128  CCInfo.AllocateReg(InputPtrReg);
129  }
130 
131  if (Info->hasDispatchID()) {
132  unsigned DispatchIDReg = Info->addDispatchID(*TRI);
133  // FIXME: Need to add reg as live-in
134  CCInfo.AllocateReg(DispatchIDReg);
135  }
136 
137  if (Info->hasFlatScratchInit()) {
138  unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
139  // FIXME: Need to add reg as live-in
140  CCInfo.AllocateReg(FlatScratchInitReg);
141  }
142 
143  unsigned NumArgs = F.arg_size();
144  Function::const_arg_iterator CurOrigArg = F.arg_begin();
145  const AMDGPUTargetLowering &TLI = *getTLI<AMDGPUTargetLowering>();
146  for (unsigned i = 0; i != NumArgs; ++i, ++CurOrigArg) {
147  MVT ValVT = TLI.getValueType(DL, CurOrigArg->getType()).getSimpleVT();
149  Flags.setOrigAlign(DL.getABITypeAlignment(CurOrigArg->getType()));
151  /*IsVarArg=*/false);
152  bool Res =
153  AssignFn(i, ValVT, ValVT, CCValAssign::Full, Flags, CCInfo);
154  assert(!Res && "Call operand has unhandled type");
155  (void)Res;
156  }
157 
159  for (unsigned i = 0; i != NumArgs; ++i, ++Arg) {
160  // FIXME: We should be getting DebugInfo from the arguments some how.
161  CCValAssign &VA = ArgLocs[i];
162  lowerParameter(MIRBuilder, Arg->getType(),
163  VA.getLocMemOffset() +
164  Subtarget->getExplicitKernelArgOffset(MF), VRegs[i]);
165  }
166 
167  return true;
168 }
MachineBasicBlock & getMBB()
Getter for the basic block we currently build.
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
Interface definition for SIRegisterInfo.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:194
size_t arg_size() const
Definition: Function.h:622
This class represents an incoming formal argument to a Function.
Definition: Argument.h:30
MachineInstrBuilder buildGEP(unsigned Res, unsigned Op0, unsigned Op1)
Build and insert Res<def> = G_GEP Op0, Op1.
AMDGPU specific subclass of TargetSubtarget.
void addLiveIn(unsigned Reg, unsigned vreg=0)
addLiveIn - Add the specified register as a live-in.
unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC)
addLiveIn - Add the specified physical register as a live-in value and create a corresponding virtual...
This file describes how to lower LLVM calls to machine code calls.
DataLayout DL
unsigned addKernargSegmentPtr(const SIRegisterInfo &TRI)
AMDGPUAS getAMDGPUAS(const Module &M)
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
Definition: Type.cpp:617
bool CCAssignFn(unsigned ValNo, MVT ValVT, MVT LocVT, CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags, CCState &State)
CCAssignFn - This function assigns a location for Val, updating State to reflect the change...
static const MCPhysReg VRegs[32]
static CCAssignFn * CCAssignFnForCall(CallingConv::ID CC, bool IsVarArg)
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
unsigned createGenericVirtualRegister(LLT Ty)
Create and return a new generic virtual register with low-level type Ty.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:186
A description of a memory reference used in the backend.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition: Function.h:150
unsigned addDispatchID(const SIRegisterInfo &TRI)
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s, unsigned base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
#define F(x, y, z)
Definition: MD5.cpp:55
MachineFunction & getMF()
Getter for the function we currently build.
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
static LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
void setOrigAlign(unsigned A)
Class to represent pointers.
Definition: DerivedTypes.h:467
void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
unsigned const MachineRegisterInfo * MRI
Machine Value Type.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
unsigned getPreloadedValue(const MachineFunction &MF, enum PreloadedValue Value) const
Returns the physical register that Value is stored in.
unsigned getLiveInVirtReg(unsigned PReg) const
getLiveInVirtReg - If PReg is a live-in physical register, return the corresponding live-in physical ...
Helper class to build MachineInstr.
SI DAG Lowering interface definition.
unsigned addQueuePtr(const SIRegisterInfo &TRI)
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
arg_iterator arg_begin()
Definition: Function.h:595
The memory access is non-temporal.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1320
This class contains a discriminated union of information about pointers in memory operands...
unsigned addDispatchPtr(const SIRegisterInfo &TRI)
LLT getLLTForType(Type &Ty, const DataLayout &DL)
Construct a low-level type based on an LLVM type.
unsigned getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:680
CCState - This class holds information needed while lowering arguments and return values...
Interface definition of the TargetLowering class that is common to all AMD GPUs.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
MachineInstrBuilder buildCopy(unsigned Res, unsigned Op)
Build and insert Res<def> = COPY Op.
CCValAssign - Represent assignment of one arg/retval to a location.
bool lowerFormalArguments(MachineIRBuilder &MIRBuilder, const Function &F, ArrayRef< unsigned > VRegs) const override
This hook must be implemented to lower the incoming (formal) arguments, described by Args...
This file declares the MachineIRBuilder class.
unsigned addPrivateSegmentBuffer(const SIRegisterInfo &TRI)
unsigned addFlatScratchInit(const SIRegisterInfo &TRI)
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
The memory access reads data.
static std::vector< std::string > Flags
Definition: FlagsTest.cpp:8
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:361
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
MachineInstrBuilder buildConstant(unsigned Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
The memory access always returns the same value (or traps).
unsigned getExplicitKernelArgOffset(const MachineFunction &MF) const
Returns the offset in bytes from the start of the input buffer of the first explicit kernel argument...
static const unsigned CONSTANT_ADDRESS
Address space for constant memory (VTX2)
Definition: AMDGPU.h:174
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
Definition: DataLayout.h:388
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool lowerReturn(MachineIRBuilder &MIRBuiler, const Value *Val, unsigned VReg) const override
This hook must be implemented to lower outgoing return values, described by Val, into the specified v...
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:545
LLVM Value Representation.
Definition: Value.h:73
static LLT pointer(uint16_t AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space (defaulting to 0).
MachineInstrBuilder buildLoad(unsigned Res, unsigned Addr, MachineMemOperand &MMO)
Build and insert Res<def> = G_LOAD Addr, MMO.
unsigned getLocMemOffset() const
AMDGPUCallLowering(const AMDGPUTargetLowering &TLI)