Skip to content

Commit

Permalink
AMDGPU: Add skeleton GlobalIsel implementation
Browse files Browse the repository at this point in the history
Summary:
This adds the necessary target code to be able to run the ir translator.
Lowering function arguments and returns is a nop and there is no support
for RegBankSelect.

Reviewers: arsenm, qcolombet

Subscribers: arsenm, joker.eph, vkalintiris, llvm-commits

Differential Revision: http://reviews.llvm.org/D19077

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266356 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
tstellarAMD committed Apr 14, 2016
1 parent 407de62 commit 65b5541
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/Target/AMDGPU/AMDGPUCallLowering.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//===-- llvm/lib/Target/AMDGPU/AMDGPUCallLowering.cpp - Call lowering ---===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file implements the lowering of LLVM calls to machine code calls for
/// GlobalISel.
///
//===----------------------------------------------------------------------===//

#include "AMDGPUCallLowering.h"
#include "AMDGPUISelLowering.h"

#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"

using namespace llvm;

#ifndef LLVM_BUILD_GLOBAL_ISEL
#error "This shouldn't be built without GISel"
#endif

AMDGPUCallLowering::AMDGPUCallLowering(const AMDGPUTargetLowering &TLI)
: CallLowering(&TLI) {
}

bool AMDGPUCallLowering::lowerReturn(MachineIRBuilder &MIRBuilder,
const Value *Val, unsigned VReg) const {
return true;
}

bool AMDGPUCallLowering::lowerFormalArguments(
MachineIRBuilder &MIRBuilder, const Function::ArgumentListType &Args,
const SmallVectorImpl<unsigned> &VRegs) const {
// TODO: Implement once there are generic loads/stores.
return true;
}
36 changes: 36 additions & 0 deletions lib/Target/AMDGPU/AMDGPUCallLowering.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//===- lib/Target/AMDGPU/AMDGPUCallLowering.h - Call lowering -*- C++ -*---===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file describes how to lower LLVM calls to machine code calls.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUCALLLOWERING_H
#define LLVM_LIB_TARGET_AMDGPU_AMDGPUCALLLOWERING_H

#include "llvm/CodeGen/GlobalISel/CallLowering.h"

namespace llvm {

class AMDGPUTargetLowering;

class AMDGPUCallLowering: public CallLowering {
public:
AMDGPUCallLowering(const AMDGPUTargetLowering &TLI);

bool lowerReturn(MachineIRBuilder &MIRBuiler, const Value *Val,
unsigned VReg) const override;
bool
lowerFormalArguments(MachineIRBuilder &MIRBuilder,
const Function::ArgumentListType &Args,
const SmallVectorImpl<unsigned> &VRegs) const override;
};
} // End of namespace llvm;
#endif
27 changes: 27 additions & 0 deletions lib/Target/AMDGPU/AMDGPUSubtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//

#include "AMDGPUSubtarget.h"
#include "AMDGPUCallLowering.h"
#include "R600ISelLowering.h"
#include "R600InstrInfo.h"
#include "R600MachineScheduler.h"
Expand All @@ -32,6 +33,17 @@ using namespace llvm;
#define GET_SUBTARGETINFO_CTOR
#include "AMDGPUGenSubtargetInfo.inc"

#ifdef LLVM_BUILD_GLOBAL_ISEL
namespace {
struct AMDGPUGISelActualAccessor : public GISelAccessor {
std::unique_ptr<CallLowering> CallLoweringInfo;
const CallLowering *getCallLowering() const override {
return CallLoweringInfo.get();
}
};
} // End anonymous namespace.
#endif

AMDGPUSubtarget &
AMDGPUSubtarget::initializeSubtargetDependencies(const Triple &TT,
StringRef GPU, StringRef FS) {
Expand Down Expand Up @@ -86,6 +98,7 @@ AMDGPUSubtarget::AMDGPUSubtarget(const Triple &TT, StringRef GPU, StringRef FS,
LDSBankCount(0),
IsaVersion(ISAVersion0_0_0),
EnableSIScheduler(false), FrameLowering(nullptr),
GISel(),
InstrItins(getInstrItineraryForCPU(GPU)), TargetTriple(TT) {

initializeSubtargetDependencies(TT, GPU, FS);
Expand All @@ -108,9 +121,23 @@ AMDGPUSubtarget::AMDGPUSubtarget(const Triple &TT, StringRef GPU, StringRef FS,
TargetFrameLowering::StackGrowsUp,
MaxStackAlign,
0));
#ifndef LLVM_BUILD_GLOBAL_ISEL
GISelAccessor *GISel = new GISelAccessor();
#else
AMDGPUGISelActualAccessor *GISel =
new AMDGPUGISelActualAccessor();
GISel->CallLoweringInfo.reset(
new AMDGPUCallLowering(*getTargetLowering()));
#endif
setGISelAccessor(*GISel);
}
}

const CallLowering *AMDGPUSubtarget::getCallLowering() const {
assert(GISel && "Access to GlobalISel APIs not set");
return GISel->getCallLowering();
}

unsigned AMDGPUSubtarget::getStackEntrySize() const {
assert(getGeneration() <= NORTHERN_ISLANDS);
switch(getWavefrontSize()) {
Expand Down
8 changes: 8 additions & 0 deletions lib/Target/AMDGPU/AMDGPUSubtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "AMDGPUSubtarget.h"
#include "Utils/AMDGPUBaseInfo.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/CodeGen/GlobalISel/GISelAccessor.h"
#include "llvm/Target/TargetSubtargetInfo.h"

#define GET_SUBTARGETINFO_HEADER
Expand Down Expand Up @@ -98,6 +99,7 @@ class AMDGPUSubtarget : public AMDGPUGenSubtargetInfo {
std::unique_ptr<AMDGPUFrameLowering> FrameLowering;
std::unique_ptr<AMDGPUTargetLowering> TLInfo;
std::unique_ptr<AMDGPUInstrInfo> InstrInfo;
std::unique_ptr<GISelAccessor> GISel;
InstrItineraryData InstrItins;
Triple TargetTriple;

Expand All @@ -107,6 +109,10 @@ class AMDGPUSubtarget : public AMDGPUGenSubtargetInfo {
AMDGPUSubtarget &initializeSubtargetDependencies(const Triple &TT,
StringRef GPU, StringRef FS);

void setGISelAccessor(GISelAccessor &GISel) {
this->GISel.reset(&GISel);
}

const AMDGPUFrameLowering *getFrameLowering() const override {
return FrameLowering.get();
}
Expand All @@ -123,6 +129,8 @@ class AMDGPUSubtarget : public AMDGPUGenSubtargetInfo {
return &InstrItins;
}

const CallLowering *getCallLowering() const override;

void ParseSubtargetFeatures(StringRef CPU, StringRef FS);

bool hasVertexCache() const {
Expand Down
16 changes: 16 additions & 0 deletions lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "SIISelLowering.h"
#include "SIInstrInfo.h"
#include "llvm/Analysis/Passes.h"
#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
Expand Down Expand Up @@ -202,6 +203,10 @@ class GCNPassConfig final : public AMDGPUPassConfig {
: AMDGPUPassConfig(TM, PM) { }
bool addPreISel() override;
bool addInstSelector() override;
#ifdef LLVM_BUILD_GLOBAL_ISEL
bool addIRTranslator() override;
bool addRegBankSelect() override;
#endif
void addFastRegAlloc(FunctionPass *RegAllocPass) override;
void addOptimizedRegAlloc(FunctionPass *RegAllocPass) override;
void addPreRegAlloc() override;
Expand Down Expand Up @@ -326,6 +331,17 @@ bool GCNPassConfig::addInstSelector() {
return false;
}

#ifdef LLVM_BUILD_GLOBAL_ISEL
bool GCNPassConfig::addIRTranslator() {
addPass(new IRTranslator());
return false;
}

bool GCNPassConfig::addRegBankSelect() {
return false;
}
#endif

void GCNPassConfig::addPreRegAlloc() {
const AMDGPUSubtarget &ST = *getAMDGPUTargetMachine().getSubtargetImpl();

Expand Down
15 changes: 15 additions & 0 deletions lib/Target/AMDGPU/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ tablegen(LLVM AMDGPUGenAsmMatcher.inc -gen-asm-matcher)
tablegen(LLVM AMDGPUGenDisassemblerTables.inc -gen-disassembler)
add_public_tablegen_target(AMDGPUCommonTableGen)

# List of all GlobalISel files.
set(GLOBAL_ISEL_FILES
AMDGPUCallLowering.cpp
)

# Add GlobalISel files to the dependencies if the user wants to build it.
if(LLVM_BUILD_GLOBAL_ISEL)
set(GLOBAL_ISEL_BUILD_FILES ${GLOBAL_ISEL_FILES})
else()
set(GLOBAL_ISEL_BUILD_FILES"")
set(LLVM_OPTIONAL_SOURCES LLVMGlobalISel ${GLOBAL_ISEL_FILES})
endif()


add_llvm_target(AMDGPUCodeGen
AMDILCFGStructurizer.cpp
AMDGPUAlwaysInlinePass.cpp
Expand Down Expand Up @@ -63,6 +77,7 @@ add_llvm_target(AMDGPUCodeGen
SIShrinkInstructions.cpp
SITypeRewriter.cpp
SIWholeQuadMode.cpp
${GLOBAL_ISEL_BUILD_FILES}
)

add_subdirectory(AsmParser)
Expand Down
12 changes: 12 additions & 0 deletions test/CodeGen/AMDGPU/GlobalISel/amdgpu-irtranslator.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; RUN: llc -march=amdgcn -mcpu=fiji -O0 -stop-after=irtranslator -global-isel %s -o - 2>&1 | FileCheck %s
; REQUIRES: global-isel
; This file checks that the translation from llvm IR to generic MachineInstr
; is correct.

; Tests for add.
; CHECK: name: addi32
; CHECK: G_ADD i32
define i32 @addi32(i32 %arg1, i32 %arg2) {
%res = add i32 %arg1, %arg2
ret i32 %res
}

0 comments on commit 65b5541

Please sign in to comment.