forked from llvm-mirror/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NVVMIntrRange adds !range metadata to calls of NVVM intrinsics that return values within known limited range. This allows LLVM to generate optimal code for indexing arrays based on tid/ctaid which is a frequently used pattern in CUDA code. Differential Revision: http://reviews.llvm.org/D20644 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@270872 91177308-0d34-0410-b5e6-96231b3b80d8
- Loading branch information
Showing
5 changed files
with
213 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
//===- NVVMIntrRange.cpp - Set !range metadata for NVVM intrinsics --------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This pass adds appropriate !range metadata for calls to NVVM | ||
// intrinsics that return a limited range of values. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "NVPTX.h" | ||
#include "llvm/IR/Constants.h" | ||
#include "llvm/IR/InstIterator.h" | ||
#include "llvm/IR/Intrinsics.h" | ||
#include "llvm/IR/Instructions.h" | ||
|
||
using namespace llvm; | ||
|
||
#define DEBUG_TYPE "nvvm-intr-range" | ||
|
||
namespace llvm { void initializeNVVMIntrRangePass(PassRegistry &); } | ||
|
||
// Add !range metadata based on limits of given SM variant. | ||
static cl::opt<unsigned> NVVMIntrRangeSM("nvvm-intr-range-sm", cl::init(20), | ||
cl::Hidden, cl::desc("SM variant")); | ||
|
||
namespace { | ||
class NVVMIntrRange : public FunctionPass { | ||
private: | ||
struct { | ||
unsigned x, y, z; | ||
} MaxBlockSize, MaxGridSize; | ||
|
||
public: | ||
static char ID; | ||
NVVMIntrRange() : NVVMIntrRange(NVVMIntrRangeSM) {} | ||
NVVMIntrRange(unsigned int SmVersion) | ||
: FunctionPass(ID), MaxBlockSize{1024, 1024, 64}, | ||
MaxGridSize{SmVersion >= 30 ? 0x7fffffffu : 0xffffu, 0xffff, 0xffff} { | ||
initializeNVVMIntrRangePass(*PassRegistry::getPassRegistry()); | ||
} | ||
|
||
bool runOnFunction(Function &) override; | ||
}; | ||
} | ||
|
||
FunctionPass *llvm::createNVVMIntrRangePass(unsigned int SmVersion) { | ||
return new NVVMIntrRange(SmVersion); | ||
} | ||
|
||
char NVVMIntrRange::ID = 0; | ||
INITIALIZE_PASS(NVVMIntrRange, "nvvm-intr-range", | ||
"Add !range metadata to NVVM intrinsics.", false, false) | ||
|
||
// Adds the passed-in [Low,High) range information as metadata to the | ||
// passed-in call instruction. | ||
static bool addRangeMetadata(uint64_t Low, uint64_t High, CallInst *C) { | ||
LLVMContext &Context = C->getParent()->getContext(); | ||
IntegerType *Int32Ty = Type::getInt32Ty(Context); | ||
Metadata *LowAndHigh[] = { | ||
ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Low)), | ||
ConstantAsMetadata::get(ConstantInt::get(Int32Ty, High))}; | ||
C->setMetadata(LLVMContext::MD_range, MDNode::get(Context, LowAndHigh)); | ||
return true; | ||
} | ||
|
||
bool NVVMIntrRange::runOnFunction(Function &F) { | ||
// Go through the calls in this function. | ||
bool Changed = false; | ||
for (Instruction &I : instructions(F)) { | ||
CallInst *Call = dyn_cast<CallInst>(&I); | ||
if (!Call) | ||
continue; | ||
|
||
if (Function *Callee = Call->getCalledFunction()) { | ||
switch (Callee->getIntrinsicID()) { | ||
// Index within block | ||
case Intrinsic::ptx_read_tid_x: | ||
case Intrinsic::nvvm_read_ptx_sreg_tid_x: | ||
Changed |= addRangeMetadata(0, MaxBlockSize.x, Call); | ||
break; | ||
case Intrinsic::ptx_read_tid_y: | ||
case Intrinsic::nvvm_read_ptx_sreg_tid_y: | ||
Changed |= addRangeMetadata(0, MaxBlockSize.y, Call); | ||
break; | ||
case Intrinsic::ptx_read_tid_z: | ||
case Intrinsic::nvvm_read_ptx_sreg_tid_z: | ||
Changed |= addRangeMetadata(0, MaxBlockSize.z, Call); | ||
break; | ||
|
||
// Block size | ||
case Intrinsic::ptx_read_ntid_x: | ||
case Intrinsic::nvvm_read_ptx_sreg_ntid_x: | ||
Changed |= addRangeMetadata(1, MaxBlockSize.x+1, Call); | ||
break; | ||
case Intrinsic::ptx_read_ntid_y: | ||
case Intrinsic::nvvm_read_ptx_sreg_ntid_y: | ||
Changed |= addRangeMetadata(1, MaxBlockSize.y+1, Call); | ||
break; | ||
case Intrinsic::ptx_read_ntid_z: | ||
case Intrinsic::nvvm_read_ptx_sreg_ntid_z: | ||
Changed |= addRangeMetadata(1, MaxBlockSize.z+1, Call); | ||
break; | ||
|
||
// Index within grid | ||
case Intrinsic::ptx_read_ctaid_x: | ||
case Intrinsic::nvvm_read_ptx_sreg_ctaid_x: | ||
Changed |= addRangeMetadata(0, MaxGridSize.x, Call); | ||
break; | ||
case Intrinsic::ptx_read_ctaid_y: | ||
case Intrinsic::nvvm_read_ptx_sreg_ctaid_y: | ||
Changed |= addRangeMetadata(0, MaxGridSize.y, Call); | ||
break; | ||
case Intrinsic::ptx_read_ctaid_z: | ||
case Intrinsic::nvvm_read_ptx_sreg_ctaid_z: | ||
Changed |= addRangeMetadata(0, MaxGridSize.z, Call); | ||
break; | ||
|
||
// Grid size | ||
case Intrinsic::ptx_read_nctaid_x: | ||
case Intrinsic::nvvm_read_ptx_sreg_nctaid_x: | ||
Changed |= addRangeMetadata(1, MaxGridSize.x+1, Call); | ||
break; | ||
case Intrinsic::ptx_read_nctaid_y: | ||
case Intrinsic::nvvm_read_ptx_sreg_nctaid_y: | ||
Changed |= addRangeMetadata(1, MaxGridSize.y+1, Call); | ||
break; | ||
case Intrinsic::ptx_read_nctaid_z: | ||
case Intrinsic::nvvm_read_ptx_sreg_nctaid_z: | ||
Changed |= addRangeMetadata(1, MaxGridSize.z+1, Call); | ||
break; | ||
|
||
// warp size is constant 32. | ||
case Intrinsic::nvvm_read_ptx_sreg_warpsize: | ||
Changed |= addRangeMetadata(32, 32+1, Call); | ||
break; | ||
|
||
// Lane ID is [0..warpsize) | ||
case Intrinsic::ptx_read_laneid: | ||
Changed |= addRangeMetadata(0, 32, Call); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return Changed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters