Skip to content

Commit

Permalink
[RISCV] Implement prefetch locality by NTLH
Browse files Browse the repository at this point in the history
We add the MemOperand then backend will generate NTLH automatically.

```
__builtin_prefetch(ptr,  0 /* rw==read */, 0 /* locality */); => ntl.all + prefetch.r (ptr)
__builtin_prefetch(ptr,  0 /* rw==read */, 1 /* locality */); => ntl.pall + prefetch.r (ptr)
__builtin_prefetch(ptr,  0 /* rw==read */, 2 /* locality */); => ntl.p1 + prefetch.r (ptr)
__builtin_prefetch(ptr,  0 /* rw==read */, 3 /* locality */); => prefetch.r (ptr)
```

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D154691
  • Loading branch information
BeMg committed Jul 17, 2023
1 parent f3b4c26 commit 7ce4e93
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
30 changes: 30 additions & 0 deletions llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2108,6 +2108,36 @@ void RISCVDAGToDAGISel::Select(SDNode *Node) {
ReplaceNode(Node, Load);
return;
}
case ISD::PREFETCH:
unsigned Locality = Node->getConstantOperandVal(3);
if (Locality > 2)
break;

if (auto *LoadStoreMem = dyn_cast<MemSDNode>(Node)) {
MachineMemOperand *MMO = LoadStoreMem->getMemOperand();
MMO->setFlags(MachineMemOperand::MONonTemporal);

int NontemporalLevel = 0;
switch (Locality) {
case 0:
NontemporalLevel = 3; // NTL.ALL
break;
case 1:
NontemporalLevel = 1; // NTL.PALL
break;
case 2:
NontemporalLevel = 0; // NTL.P1
break;
default:
llvm_unreachable("unexpected locality value.");
}

if (NontemporalLevel & 0b1)
MMO->setFlags(MONontemporalBit0);
if (NontemporalLevel & 0b10)
MMO->setFlags(MONontemporalBit1);
}
break;
}

// Select the default instruction.
Expand Down
9 changes: 9 additions & 0 deletions llvm/test/CodeGen/RISCV/prefetch.ll
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ define void @test_prefetch_read_locality_0(ptr %a) nounwind {
;
; RV64ZICBOPZIHINTNTL-LABEL: test_prefetch_read_locality_0:
; RV64ZICBOPZIHINTNTL: # %bb.0:
; RV64ZICBOPZIHINTNTL-NEXT: ntl.all
; RV64ZICBOPZIHINTNTL-NEXT: prefetch.r 0(a0)
; RV64ZICBOPZIHINTNTL-NEXT: ret
call void @llvm.prefetch(ptr %a, i32 0, i32 0, i32 1)
Expand Down Expand Up @@ -60,6 +61,7 @@ define void @test_prefetch_write_locality_0(ptr %a) nounwind {
;
; RV64ZICBOPZIHINTNTL-LABEL: test_prefetch_write_locality_0:
; RV64ZICBOPZIHINTNTL: # %bb.0:
; RV64ZICBOPZIHINTNTL-NEXT: ntl.all
; RV64ZICBOPZIHINTNTL-NEXT: prefetch.w 0(a0)
; RV64ZICBOPZIHINTNTL-NEXT: ret
call void @llvm.prefetch(ptr %a, i32 1, i32 0, i32 1)
Expand Down Expand Up @@ -87,6 +89,7 @@ define void @test_prefetch_instruction_locality_0(ptr %a) nounwind {
;
; RV64ZICBOPZIHINTNTL-LABEL: test_prefetch_instruction_locality_0:
; RV64ZICBOPZIHINTNTL: # %bb.0:
; RV64ZICBOPZIHINTNTL-NEXT: ntl.all
; RV64ZICBOPZIHINTNTL-NEXT: prefetch.i 0(a0)
; RV64ZICBOPZIHINTNTL-NEXT: ret
call void @llvm.prefetch(ptr %a, i32 0, i32 0, i32 0)
Expand Down Expand Up @@ -114,6 +117,7 @@ define void @test_prefetch_read_locality_1(ptr %a) nounwind {
;
; RV64ZICBOPZIHINTNTL-LABEL: test_prefetch_read_locality_1:
; RV64ZICBOPZIHINTNTL: # %bb.0:
; RV64ZICBOPZIHINTNTL-NEXT: ntl.pall
; RV64ZICBOPZIHINTNTL-NEXT: prefetch.r 0(a0)
; RV64ZICBOPZIHINTNTL-NEXT: ret
call void @llvm.prefetch(ptr %a, i32 0, i32 1, i32 1)
Expand Down Expand Up @@ -141,6 +145,7 @@ define void @test_prefetch_write_locality_1(ptr %a) nounwind {
;
; RV64ZICBOPZIHINTNTL-LABEL: test_prefetch_write_locality_1:
; RV64ZICBOPZIHINTNTL: # %bb.0:
; RV64ZICBOPZIHINTNTL-NEXT: ntl.pall
; RV64ZICBOPZIHINTNTL-NEXT: prefetch.w 0(a0)
; RV64ZICBOPZIHINTNTL-NEXT: ret
call void @llvm.prefetch(ptr %a, i32 1, i32 1, i32 1)
Expand Down Expand Up @@ -168,6 +173,7 @@ define void @test_prefetch_instruction_locality_1(ptr %a) nounwind {
;
; RV64ZICBOPZIHINTNTL-LABEL: test_prefetch_instruction_locality_1:
; RV64ZICBOPZIHINTNTL: # %bb.0:
; RV64ZICBOPZIHINTNTL-NEXT: ntl.pall
; RV64ZICBOPZIHINTNTL-NEXT: prefetch.i 0(a0)
; RV64ZICBOPZIHINTNTL-NEXT: ret
call void @llvm.prefetch(ptr %a, i32 0, i32 1, i32 0)
Expand Down Expand Up @@ -195,6 +201,7 @@ define void @test_prefetch_read_locality_2(ptr %a) nounwind {
;
; RV64ZICBOPZIHINTNTL-LABEL: test_prefetch_read_locality_2:
; RV64ZICBOPZIHINTNTL: # %bb.0:
; RV64ZICBOPZIHINTNTL-NEXT: ntl.p1
; RV64ZICBOPZIHINTNTL-NEXT: prefetch.r 0(a0)
; RV64ZICBOPZIHINTNTL-NEXT: ret
call void @llvm.prefetch(ptr %a, i32 0, i32 2, i32 1)
Expand Down Expand Up @@ -222,6 +229,7 @@ define void @test_prefetch_write_locality_2(ptr %a) nounwind {
;
; RV64ZICBOPZIHINTNTL-LABEL: test_prefetch_write_locality_2:
; RV64ZICBOPZIHINTNTL: # %bb.0:
; RV64ZICBOPZIHINTNTL-NEXT: ntl.p1
; RV64ZICBOPZIHINTNTL-NEXT: prefetch.w 0(a0)
; RV64ZICBOPZIHINTNTL-NEXT: ret
call void @llvm.prefetch(ptr %a, i32 1, i32 2, i32 1)
Expand Down Expand Up @@ -249,6 +257,7 @@ define void @test_prefetch_instruction_locality_2(ptr %a) nounwind {
;
; RV64ZICBOPZIHINTNTL-LABEL: test_prefetch_instruction_locality_2:
; RV64ZICBOPZIHINTNTL: # %bb.0:
; RV64ZICBOPZIHINTNTL-NEXT: ntl.p1
; RV64ZICBOPZIHINTNTL-NEXT: prefetch.i 0(a0)
; RV64ZICBOPZIHINTNTL-NEXT: ret
call void @llvm.prefetch(ptr %a, i32 0, i32 2, i32 0)
Expand Down

0 comments on commit 7ce4e93

Please sign in to comment.