Skip to content

Commit

Permalink
[AMDGPU] Remove hardcoded address space value from AMDGPULibFunc
Browse files Browse the repository at this point in the history
AMDGPULibFunc hardcodes address space values of the old address space mapping,
which causes invalid addrspacecast instructions and undefined functions in
APPSDK sample MonteCarloAsianDP.

This patch fixes that.

Differential Revision: https://reviews.llvm.org/D39616


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@317409 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
yxsamliu committed Nov 4, 2017
1 parent dcf1ffe commit 35934f8
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 85 deletions.
4 changes: 2 additions & 2 deletions lib/Target/AMDGPU/AMDGPULibCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,8 @@ bool AMDGPULibCalls::fold_sincos(CallInst *CI, IRBuilder<> &B,
// for OpenCL 2.0 we have only generic implementation of sincos
// function.
AMDGPULibFunc nf(AMDGPULibFunc::EI_SINCOS, fInfo);
nf.getLeads()[0].PtrKind = AMDGPULibFunc::GENERIC;
const AMDGPUAS AS = AMDGPU::getAMDGPUAS(*M);
nf.getLeads()[0].PtrKind = AMDGPULibFunc::getEPtrKindFromAddrSpace(AS.FLAT_ADDRESS);
Function *Fsincos = dyn_cast_or_null<Function>(getFunction(M, nf));
if (!Fsincos) return false;

Expand All @@ -1350,7 +1351,6 @@ bool AMDGPULibCalls::fold_sincos(CallInst *CI, IRBuilder<> &B,
// The allocaInst allocates the memory in private address space. This need
// to be bitcasted to point to the address space of cos pointer type.
// In OpenCL 2.0 this is generic, while in 1.2 that is private.
const AMDGPUAS AS = AMDGPU::getAMDGPUAS(*M);
if (PTy->getPointerAddressSpace() != AS.PRIVATE_ADDRESS)
P = B.CreateAddrSpaceCast(Alloc, PTy);
CallInst *Call = CreateCallEx2(B, Fsincos, UI->getArgOperand(0), P);
Expand Down
29 changes: 15 additions & 14 deletions lib/Target/AMDGPU/AMDGPULibFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//
//===----------------------------------------------------------------------===//

#include "AMDGPU.h"
#include "AMDGPULibFunc.h"
#include <llvm/ADT/SmallString.h>
#include <llvm/ADT/SmallVector.h>
Expand Down Expand Up @@ -458,13 +459,16 @@ AMDGPULibFunc::Param ParamIterator::getNextParam() {
P.ArgType = AMDGPULibFunc::I32;
break;

case E_CONSTPTR_SWAPGL:
switch (P.PtrKind & AMDGPULibFunc::ADDR_SPACE) {
case AMDGPULibFunc::GLOBAL: P.PtrKind = AMDGPULibFunc::LOCAL; break;
case AMDGPULibFunc::LOCAL: P.PtrKind = AMDGPULibFunc::GLOBAL; break;
case E_CONSTPTR_SWAPGL: {
unsigned AS = AMDGPULibFunc::getAddrSpaceFromEPtrKind(P.PtrKind);
switch (AS) {
case AMDGPUAS::GLOBAL_ADDRESS: AS = AMDGPUAS::LOCAL_ADDRESS; break;
case AMDGPUAS::LOCAL_ADDRESS: AS = AMDGPUAS::GLOBAL_ADDRESS; break;
}
P.PtrKind = AMDGPULibFunc::getEPtrKindFromAddrSpace(AS);
P.PtrKind |= AMDGPULibFunc::CONST;
break;
}

default: llvm_unreachable("Unhandeled param rule");
}
Expand Down Expand Up @@ -590,19 +594,14 @@ bool ItaniumParamParser::parseItaniumParam(StringRef& param,
if (eatTerm(param, 'P')) {
if (eatTerm(param, 'K')) res.PtrKind |= AMDGPULibFunc::CONST;
if (eatTerm(param, 'V')) res.PtrKind |= AMDGPULibFunc::VOLATILE;
unsigned AS;
if (!eatTerm(param, "U3AS")) {
res.PtrKind |= AMDGPULibFunc::PRIVATE;
AS = 0;
} else {
switch(param.front()) {
case '1': res.PtrKind |= AMDGPULibFunc::GLOBAL; break;
case '2': res.PtrKind |= AMDGPULibFunc::READONLY;break;
case '3': res.PtrKind |= AMDGPULibFunc::LOCAL; break;
case '4': res.PtrKind |= AMDGPULibFunc::GENERIC; break;
case '5': res.PtrKind |= AMDGPULibFunc::OTHER; break;
default: return false;
}
AS = param.front() - '0';
drop_front(param, 1);
}
res.PtrKind |= AMDGPULibFuncBase::getEPtrKindFromAddrSpace(AS);
} else {
res.PtrKind = AMDGPULibFunc::BYVALUE;
}
Expand Down Expand Up @@ -837,7 +836,9 @@ class ItaniumMangler {
os << 'P';
if (p.PtrKind & AMDGPULibFunc::CONST) os << 'K';
if (p.PtrKind & AMDGPULibFunc::VOLATILE) os << 'V';
int AS = UseAddrSpace ? (p.PtrKind & AMDGPULibFunc::ADDR_SPACE)-1 : 0;
unsigned AS = UseAddrSpace
? AMDGPULibFuncBase::getAddrSpaceFromEPtrKind(p.PtrKind)
: 0;
if (AS != 0) os << "U3AS" << AS;
Ptr = p;
p.PtrKind = 0;
Expand Down
20 changes: 12 additions & 8 deletions lib/Target/AMDGPU/AMDGPULibFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,7 @@ class AMDGPULibFuncBase {

enum EPtrKind {
BYVALUE = 0,
PRIVATE,
GLOBAL,
READONLY,
LOCAL,
GENERIC,
OTHER,

ADDR_SPACE = 0xF,
ADDR_SPACE = 0xF, // Address space takes value 0x1 ~ 0xF.
CONST = 0x10,
VOLATILE = 0x20
};
Expand All @@ -315,6 +308,17 @@ class AMDGPULibFuncBase {
static bool isMangled(EFuncId Id) {
return static_cast<unsigned>(Id) <= static_cast<unsigned>(EI_LAST_MANGLED);
}

static unsigned getEPtrKindFromAddrSpace(unsigned AS) {
assert(((AS + 1) & ~ADDR_SPACE) == 0);
return AS + 1;
}

static unsigned getAddrSpaceFromEPtrKind(unsigned Kind) {
Kind = Kind & ADDR_SPACE;
assert(Kind >= 1);
return Kind - 1;
}
};

class AMDGPULibFuncImpl : public AMDGPULibFuncBase {
Expand Down
Loading

0 comments on commit 35934f8

Please sign in to comment.