Skip to content

Commit d4ed253

Browse files
committed
[RISCV] Assume no-op addrspacecasts by default
To support OpenCL, which typically uses SPIR as an IR, non-zero address spaces must be accounted for. This patch makes the RISC-V target assume no-op address space casts across the board, which effectively removes the need to support addrspacecast instructions in the backend. For a RISC-V implementation with different configurations or specialized address spaces where casts aren't no-ops, the function can be adjusted as required. Reviewed By: jrtc27 Differential Revision: https://reviews.llvm.org/D93536
1 parent 08c4b40 commit d4ed253

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

llvm/lib/Target/RISCV/RISCVTargetMachine.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ RISCVTargetMachine::getTargetTransformInfo(const Function &F) {
113113
return TargetTransformInfo(RISCVTTIImpl(this, F));
114114
}
115115

116+
// A RISC-V hart has a single byte-addressable address space of 2^XLEN bytes
117+
// for all memory accesses, so it is reasonable to assume that an
118+
// implementation has no-op address space casts. If an implementation makes a
119+
// change to this, they can override it here.
120+
bool RISCVTargetMachine::isNoopAddrSpaceCast(unsigned SrcAS,
121+
unsigned DstAS) const {
122+
return true;
123+
}
124+
116125
namespace {
117126
class RISCVPassConfig : public TargetPassConfig {
118127
public:

llvm/lib/Target/RISCV/RISCVTargetMachine.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class RISCVTargetMachine : public LLVMTargetMachine {
4343
}
4444

4545
TargetTransformInfo getTargetTransformInfo(const Function &F) override;
46+
47+
virtual bool isNoopAddrSpaceCast(unsigned SrcAS,
48+
unsigned DstAS) const override;
4649
};
4750
}
4851

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc -mtriple=riscv32 -verify-machineinstrs < %s \
3+
; RUN: | FileCheck %s --check-prefix=RV32I
4+
; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
5+
; RUN: | FileCheck %s --check-prefix=RV64I
6+
7+
define void @cast0(i32 addrspace(1)* %ptr) {
8+
; RV32I-LABEL: cast0:
9+
; RV32I: # %bb.0:
10+
; RV32I-NEXT: sw zero, 0(a0)
11+
; RV32I-NEXT: ret
12+
;
13+
; RV64I-LABEL: cast0:
14+
; RV64I: # %bb.0:
15+
; RV64I-NEXT: sw zero, 0(a0)
16+
; RV64I-NEXT: ret
17+
%ptr0 = addrspacecast i32 addrspace(1)* %ptr to i32 addrspace(0)*
18+
store i32 0, i32* %ptr0
19+
ret void
20+
}
21+
22+
define void @cast1(i32* %ptr) {
23+
; RV32I-LABEL: cast1:
24+
; RV32I: # %bb.0:
25+
; RV32I-NEXT: addi sp, sp, -16
26+
; RV32I-NEXT: .cfi_def_cfa_offset 16
27+
; RV32I-NEXT: sw ra, 12(sp) # 4-byte Folded Spill
28+
; RV32I-NEXT: .cfi_offset ra, -4
29+
; RV32I-NEXT: call foo@plt
30+
; RV32I-NEXT: lw ra, 12(sp) # 4-byte Folded Reload
31+
; RV32I-NEXT: addi sp, sp, 16
32+
; RV32I-NEXT: ret
33+
;
34+
; RV64I-LABEL: cast1:
35+
; RV64I: # %bb.0:
36+
; RV64I-NEXT: addi sp, sp, -16
37+
; RV64I-NEXT: .cfi_def_cfa_offset 16
38+
; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
39+
; RV64I-NEXT: .cfi_offset ra, -8
40+
; RV64I-NEXT: call foo@plt
41+
; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
42+
; RV64I-NEXT: addi sp, sp, 16
43+
; RV64I-NEXT: ret
44+
%castptr = addrspacecast i32* %ptr to i32 addrspace(10)*
45+
call void @foo(i32 addrspace(10)* %castptr)
46+
ret void
47+
}
48+
49+
declare void @foo(i32 addrspace(10)*)

0 commit comments

Comments
 (0)