Skip to content

Commit ee6e09c

Browse files
committed
[NVPTX] Add some basic folds for ADDRSPACECAST
1 parent 1833ddf commit ee6e09c

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ NVPTXTargetLowering::NVPTXTargetLowering(const NVPTXTargetMachine &TM,
822822
// We have some custom DAG combine patterns for these nodes
823823
setTargetDAGCombine({ISD::ADD, ISD::AND, ISD::EXTRACT_VECTOR_ELT, ISD::FADD,
824824
ISD::MUL, ISD::SHL, ISD::SREM, ISD::UREM, ISD::VSELECT,
825-
ISD::BUILD_VECTOR});
825+
ISD::BUILD_VECTOR, ISD::ADDRSPACECAST});
826826

827827
// setcc for f16x2 and bf16x2 needs special handling to prevent
828828
// legalizer's attempt to scalarize it due to v2i1 not being legal.
@@ -5209,6 +5209,26 @@ PerformBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
52095209
return DAG.getNode(ISD::BITCAST, DL, VT, PRMT);
52105210
}
52115211

5212+
static SDValue combineADDRSPACECAST(SDNode *N,
5213+
TargetLowering::DAGCombinerInfo &DCI) {
5214+
auto *ASCN1 = cast<AddrSpaceCastSDNode>(N);
5215+
5216+
if (auto *ASCN2 = dyn_cast<AddrSpaceCastSDNode>(ASCN1->getOperand(0))) {
5217+
assert(ASCN2->getDestAddressSpace() == ASCN1->getSrcAddressSpace());
5218+
5219+
// Fold asc[B -> A](asc[A -> B](x)) -> x
5220+
if (ASCN1->getDestAddressSpace() == ASCN2->getSrcAddressSpace())
5221+
return ASCN2->getOperand(0);
5222+
5223+
// Fold asc[B -> C](asc[A -> B](x)) -> asc[A -> C](x)
5224+
return DCI.DAG.getAddrSpaceCast(
5225+
SDLoc(N), N->getValueType(0), ASCN2->getOperand(0),
5226+
ASCN2->getSrcAddressSpace(), ASCN1->getDestAddressSpace());
5227+
}
5228+
5229+
return SDValue();
5230+
}
5231+
52125232
SDValue NVPTXTargetLowering::PerformDAGCombine(SDNode *N,
52135233
DAGCombinerInfo &DCI) const {
52145234
CodeGenOptLevel OptLevel = getTargetMachine().getOptLevel();
@@ -5243,6 +5263,8 @@ SDValue NVPTXTargetLowering::PerformDAGCombine(SDNode *N,
52435263
return PerformVSELECTCombine(N, DCI);
52445264
case ISD::BUILD_VECTOR:
52455265
return PerformBUILD_VECTORCombine(N, DCI);
5266+
case ISD::ADDRSPACECAST:
5267+
return combineADDRSPACECAST(N, DCI);
52465268
}
52475269
return SDValue();
52485270
}

llvm/test/CodeGen/NVPTX/addrspacecast-folding.ll

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ target triple = "nvptx64-unknown-unknown"
77
define ptr @test1(ptr %p) {
88
; CHECK-LABEL: test1(
99
; CHECK: {
10-
; CHECK-NEXT: .reg .b64 %rd<4>;
10+
; CHECK-NEXT: .reg .b64 %rd<2>;
1111
; CHECK-EMPTY:
1212
; CHECK-NEXT: // %bb.0:
1313
; CHECK-NEXT: ld.param.u64 %rd1, [test1_param_0];
14-
; CHECK-NEXT: cvta.to.local.u64 %rd2, %rd1;
15-
; CHECK-NEXT: cvta.local.u64 %rd3, %rd2;
16-
; CHECK-NEXT: st.param.b64 [func_retval0], %rd3;
14+
; CHECK-NEXT: st.param.b64 [func_retval0], %rd1;
1715
; CHECK-NEXT: ret;
1816
%a = addrspacecast ptr %p to ptr addrspace(5)
1917
%b = addrspacecast ptr addrspace(5) %a to ptr
@@ -23,13 +21,10 @@ define ptr @test1(ptr %p) {
2321
define ptr addrspace(1) @test2(ptr addrspace(5) %p) {
2422
; CHECK-LABEL: test2(
2523
; CHECK: {
26-
; CHECK-NEXT: .reg .b64 %rd<4>;
24+
; CHECK-NEXT: .reg .b64 %rd<2>;
2725
; CHECK-EMPTY:
2826
; CHECK-NEXT: // %bb.0:
2927
; CHECK-NEXT: ld.param.u64 %rd1, [test2_param_0];
30-
; CHECK-NEXT: cvta.local.u64 %rd2, %rd1;
31-
; CHECK-NEXT: cvta.to.global.u64 %rd3, %rd2;
32-
; CHECK-NEXT: st.param.b64 [func_retval0], %rd3;
3328
; CHECK-NEXT: ret;
3429
%a = addrspacecast ptr addrspace(5) %p to ptr
3530
%b = addrspacecast ptr %a to ptr addrspace(1)

0 commit comments

Comments
 (0)