[llvm] [NVPTX] Check Before inserting AddrSpaceCastInst in NVPTXLoweringAlloca (PR #106127)
weiwei chen via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 5 19:33:25 PDT 2024
https://github.com/weiweichen updated https://github.com/llvm/llvm-project/pull/106127
>From 2506e94c21e3c61a584332b34a9f0aea4fb17a48 Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Mon, 26 Aug 2024 15:28:38 -0400
Subject: [PATCH 01/13] Only insert AddrSpaceCastInst when necessary.
---
llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp | 21 +++++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
index 369238436083c7..62b789c008bb43 100644
--- a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
@@ -72,12 +72,21 @@ bool NVPTXLowerAlloca::runOnFunction(Function &F) {
Changed = true;
auto ETy = allocaInst->getAllocatedType();
auto LocalAddrTy = PointerType::get(ETy, ADDRESS_SPACE_LOCAL);
- auto NewASCToLocal = new AddrSpaceCastInst(allocaInst, LocalAddrTy, "");
- auto GenericAddrTy = PointerType::get(ETy, ADDRESS_SPACE_GENERIC);
- auto NewASCToGeneric =
- new AddrSpaceCastInst(NewASCToLocal, GenericAddrTy, "");
- NewASCToLocal->insertAfter(allocaInst);
- NewASCToGeneric->insertAfter(NewASCToLocal);
+ PointerType *allocInstPtrTy = dyn_cast_or_null<PointerType>(allocaInst->getType()->getScalarType());
+ assert(allocInstPtrTy && "AllocInst scalar type is not a PointerType.");
+ Instruction* NewASCToGeneric = allocaInst;
+ if(allocInstPtrTy->getAddressSpace() != ADDRESS_SPACE_LOCAL) {
+ // Only insert a new AddrSpaceCastInst if
+ // allocaInst is not already in ADDRESS_SPACE_LOCAL.
+ auto NewASCToLocal =
+ new AddrSpaceCastInst(allocaInst, LocalAddrTy, "");
+ auto GenericAddrTy = PointerType::get(ETy, ADDRESS_SPACE_GENERIC);
+ NewASCToGeneric =
+ new AddrSpaceCastInst(NewASCToLocal, GenericAddrTy, "");
+ NewASCToLocal->insertAfter(allocaInst);
+ NewASCToGeneric->insertAfter(NewASCToLocal);
+ }
+
for (Use &AllocaUse : llvm::make_early_inc_range(allocaInst->uses())) {
// Check Load, Store, GEP, and BitCast Uses on alloca and make them
// use the converted generic address, in order to expose non-generic
>From 0c9a46d59bf1df9798e2e868344db3a93ae83d49 Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Mon, 26 Aug 2024 16:06:47 -0400
Subject: [PATCH 02/13] Add unittest.
---
llvm/test/CodeGen/NVPTX/lower-alloca.ll | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/llvm/test/CodeGen/NVPTX/lower-alloca.ll b/llvm/test/CodeGen/NVPTX/lower-alloca.ll
index b1c34c8b5ecd78..68793dffb01e8a 100644
--- a/llvm/test/CodeGen/NVPTX/lower-alloca.ll
+++ b/llvm/test/CodeGen/NVPTX/lower-alloca.ll
@@ -17,7 +17,21 @@ define void @kernel() {
ret void
}
+define void @alloc_already_in_addrspace5() {
+; LABEL: @lower_alloca_addrspace5
+; PTX-LABEL: .visible .func alloc_already_in_addrspace5(
+ %A = alloca i32, addrspace(5)
+; CHECK-NOT: addrspacecast ptr %A to ptr addrspace(5)
+; CHECK: store i32 0, ptr addrspace(5) {{%.+}}
+; PTX: st.local.u32 [%SP+0], {{%r[0-9]+}}
+ store i32 0, ptr addrspace(5) %A
+ call void @callee(ptr addrspace(5) %A)
+ ret void
+}
+
declare void @callee(ptr)
+declare void @callee_addrspace5(ptr addrspace(5))
!nvvm.annotations = !{!0}
!0 = !{ptr @kernel, !"kernel", i32 1}
+!1 = !{ptr @alloc_already_in_addrspace5, !"alloc_already_in_addrspace5", i32 1}
>From 6ec68a5bc9e8218d5ae6438408618d13fcf23328 Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Mon, 26 Aug 2024 16:18:51 -0400
Subject: [PATCH 03/13] Formattttttttt.
---
llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
index 62b789c008bb43..69803ddfc6f920 100644
--- a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
@@ -72,10 +72,11 @@ bool NVPTXLowerAlloca::runOnFunction(Function &F) {
Changed = true;
auto ETy = allocaInst->getAllocatedType();
auto LocalAddrTy = PointerType::get(ETy, ADDRESS_SPACE_LOCAL);
- PointerType *allocInstPtrTy = dyn_cast_or_null<PointerType>(allocaInst->getType()->getScalarType());
- assert(allocInstPtrTy && "AllocInst scalar type is not a PointerType.");
- Instruction* NewASCToGeneric = allocaInst;
- if(allocInstPtrTy->getAddressSpace() != ADDRESS_SPACE_LOCAL) {
+ PointerType *AllocInstPtrTy = dyn_cast_or_null<PointerType>(
+ allocaInst->getType()->getScalarType());
+ assert(AllocInstPtrTy && "AllocInst scalar type is not a PointerType.");
+ Instruction *NewASCToGeneric = allocaInst;
+ if (AllocInstPtrTy->getAddressSpace() != ADDRESS_SPACE_LOCAL) {
// Only insert a new AddrSpaceCastInst if
// allocaInst is not already in ADDRESS_SPACE_LOCAL.
auto NewASCToLocal =
>From 1bdc7fa54436fc03a0f273a8389960a5c23d29d2 Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Mon, 26 Aug 2024 17:21:45 -0400
Subject: [PATCH 04/13] Address review comment.
---
llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
index 69803ddfc6f920..ec3743f8f310cf 100644
--- a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
@@ -72,9 +72,8 @@ bool NVPTXLowerAlloca::runOnFunction(Function &F) {
Changed = true;
auto ETy = allocaInst->getAllocatedType();
auto LocalAddrTy = PointerType::get(ETy, ADDRESS_SPACE_LOCAL);
- PointerType *AllocInstPtrTy = dyn_cast_or_null<PointerType>(
+ PointerType *AllocInstPtrTy = cast<PointerType>(
allocaInst->getType()->getScalarType());
- assert(AllocInstPtrTy && "AllocInst scalar type is not a PointerType.");
Instruction *NewASCToGeneric = allocaInst;
if (AllocInstPtrTy->getAddressSpace() != ADDRESS_SPACE_LOCAL) {
// Only insert a new AddrSpaceCastInst if
>From f1680bc0f13d1ca3eee9fe80d6ea651a71d9f93b Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Mon, 26 Aug 2024 17:36:23 -0400
Subject: [PATCH 05/13] Formatting again, ahhhhh.
---
llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
index ec3743f8f310cf..b0893c0ad6e2b4 100644
--- a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
@@ -72,8 +72,8 @@ bool NVPTXLowerAlloca::runOnFunction(Function &F) {
Changed = true;
auto ETy = allocaInst->getAllocatedType();
auto LocalAddrTy = PointerType::get(ETy, ADDRESS_SPACE_LOCAL);
- PointerType *AllocInstPtrTy = cast<PointerType>(
- allocaInst->getType()->getScalarType());
+ PointerType *AllocInstPtrTy =
+ cast<PointerType>(allocaInst->getType()->getScalarType());
Instruction *NewASCToGeneric = allocaInst;
if (AllocInstPtrTy->getAddressSpace() != ADDRESS_SPACE_LOCAL) {
// Only insert a new AddrSpaceCastInst if
>From 0cee1cd529760c5d2a619666bea21d78662526cc Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Tue, 3 Sep 2024 10:57:49 -0400
Subject: [PATCH 06/13] Add early assert for alloc address space check.
---
llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
index b0893c0ad6e2b4..7e8bb60af12881 100644
--- a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
@@ -75,7 +75,12 @@ bool NVPTXLowerAlloca::runOnFunction(Function &F) {
PointerType *AllocInstPtrTy =
cast<PointerType>(allocaInst->getType()->getScalarType());
Instruction *NewASCToGeneric = allocaInst;
- if (AllocInstPtrTy->getAddressSpace() != ADDRESS_SPACE_LOCAL) {
+ unsigned AllocAddrSpace = AllocInstPtrTy->getAddressSpace();
+ assert((AllocAddrSpace == ADDRESS_SPACE_GENERIC ||
+ AllocAddrSpace == ADDRESS_SPACE_LOCAL) &&
+ "AllocaInst can only be in Generic or Local address space for "
+ "NVPTX.");
+ if (AllocAddrSpace != ADDRESS_SPACE_LOCAL) {
// Only insert a new AddrSpaceCastInst if
// allocaInst is not already in ADDRESS_SPACE_LOCAL.
auto NewASCToLocal =
>From f84a8491fd72e5c29037511889cc172158fbf264 Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Wed, 4 Sep 2024 10:18:17 -0400
Subject: [PATCH 07/13] Update conditional logic.
---
llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp | 15 +++++++--------
1 file changed, 7 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
index 7e8bb60af12881..c15fb778874676 100644
--- a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
@@ -74,7 +74,7 @@ bool NVPTXLowerAlloca::runOnFunction(Function &F) {
auto LocalAddrTy = PointerType::get(ETy, ADDRESS_SPACE_LOCAL);
PointerType *AllocInstPtrTy =
cast<PointerType>(allocaInst->getType()->getScalarType());
- Instruction *NewASCToGeneric = allocaInst;
+ Instruction *NewASCToLocal = allocaInst;
unsigned AllocAddrSpace = AllocInstPtrTy->getAddressSpace();
assert((AllocAddrSpace == ADDRESS_SPACE_GENERIC ||
AllocAddrSpace == ADDRESS_SPACE_LOCAL) &&
@@ -83,15 +83,14 @@ bool NVPTXLowerAlloca::runOnFunction(Function &F) {
if (AllocAddrSpace != ADDRESS_SPACE_LOCAL) {
// Only insert a new AddrSpaceCastInst if
// allocaInst is not already in ADDRESS_SPACE_LOCAL.
- auto NewASCToLocal =
- new AddrSpaceCastInst(allocaInst, LocalAddrTy, "");
- auto GenericAddrTy = PointerType::get(ETy, ADDRESS_SPACE_GENERIC);
- NewASCToGeneric =
- new AddrSpaceCastInst(NewASCToLocal, GenericAddrTy, "");
- NewASCToLocal->insertAfter(allocaInst);
- NewASCToGeneric->insertAfter(NewASCToLocal);
+ NewASCToLocal = new AddrSpaceCastInst(allocaInst, LocalAddrTy, "");
}
+ auto NewASCToGeneric = new AddrSpaceCastInst(
+ NewASCToLocal, PointerType::get(ETy, ADDRESS_SPACE_GENERIC), "");
+ NewASCToLocal->insertAfter(allocaInst);
+ NewASCToGeneric->insertAfter(NewASCToLocal);
+
for (Use &AllocaUse : llvm::make_early_inc_range(allocaInst->uses())) {
// Check Load, Store, GEP, and BitCast Uses on alloca and make them
// use the converted generic address, in order to expose non-generic
>From 22beb79198dbc70612f5c29c12bb8b4c6f7adefc Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Wed, 4 Sep 2024 10:26:39 -0400
Subject: [PATCH 08/13] Address some review comments.
---
llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp | 27 ++++++++++------------
1 file changed, 12 insertions(+), 15 deletions(-)
diff --git a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
index c15fb778874676..70f88ee2d1104f 100644
--- a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
@@ -71,26 +71,23 @@ bool NVPTXLowerAlloca::runOnFunction(Function &F) {
if (auto allocaInst = dyn_cast<AllocaInst>(&I)) {
Changed = true;
auto ETy = allocaInst->getAllocatedType();
- auto LocalAddrTy = PointerType::get(ETy, ADDRESS_SPACE_LOCAL);
PointerType *AllocInstPtrTy =
cast<PointerType>(allocaInst->getType()->getScalarType());
- Instruction *NewASCToLocal = allocaInst;
+ Instruction *AllocaInGenericAS = allocaInst;
unsigned AllocAddrSpace = AllocInstPtrTy->getAddressSpace();
assert((AllocAddrSpace == ADDRESS_SPACE_GENERIC ||
AllocAddrSpace == ADDRESS_SPACE_LOCAL) &&
"AllocaInst can only be in Generic or Local address space for "
"NVPTX.");
- if (AllocAddrSpace != ADDRESS_SPACE_LOCAL) {
- // Only insert a new AddrSpaceCastInst if
- // allocaInst is not already in ADDRESS_SPACE_LOCAL.
- NewASCToLocal = new AddrSpaceCastInst(allocaInst, LocalAddrTy, "");
+ if (AllocAddrSpace == ADDRESS_SPACE_LOCAL) {
+ // Insert a new AddrSpaceCastInst if
+ // allocaInst is not already in ADDRESS_SPACE_Generic.
+ auto ASCastToGenericAS = AddrSpaceCastInst(
+ allocaInst, PointerType::get(ETy, ADDRESS_SPACE_GENERIC), "");
+ ASCastToGenericAS.insertAfter(allocaInst);
+ AllocaInGenericAS = &ASCastToGenericAS;
}
- auto NewASCToGeneric = new AddrSpaceCastInst(
- NewASCToLocal, PointerType::get(ETy, ADDRESS_SPACE_GENERIC), "");
- NewASCToLocal->insertAfter(allocaInst);
- NewASCToGeneric->insertAfter(NewASCToLocal);
-
for (Use &AllocaUse : llvm::make_early_inc_range(allocaInst->uses())) {
// Check Load, Store, GEP, and BitCast Uses on alloca and make them
// use the converted generic address, in order to expose non-generic
@@ -100,23 +97,23 @@ bool NVPTXLowerAlloca::runOnFunction(Function &F) {
auto LI = dyn_cast<LoadInst>(AllocaUse.getUser());
if (LI && LI->getPointerOperand() == allocaInst &&
!LI->isVolatile()) {
- LI->setOperand(LI->getPointerOperandIndex(), NewASCToGeneric);
+ LI->setOperand(LI->getPointerOperandIndex(), AllocaInGenericAS);
continue;
}
auto SI = dyn_cast<StoreInst>(AllocaUse.getUser());
if (SI && SI->getPointerOperand() == allocaInst &&
!SI->isVolatile()) {
- SI->setOperand(SI->getPointerOperandIndex(), NewASCToGeneric);
+ SI->setOperand(SI->getPointerOperandIndex(), AllocaInGenericAS);
continue;
}
auto GI = dyn_cast<GetElementPtrInst>(AllocaUse.getUser());
if (GI && GI->getPointerOperand() == allocaInst) {
- GI->setOperand(GI->getPointerOperandIndex(), NewASCToGeneric);
+ GI->setOperand(GI->getPointerOperandIndex(), AllocaInGenericAS);
continue;
}
auto BI = dyn_cast<BitCastInst>(AllocaUse.getUser());
if (BI && BI->getOperand(0) == allocaInst) {
- BI->setOperand(0, NewASCToGeneric);
+ BI->setOperand(0, AllocaInGenericAS);
continue;
}
}
>From 7c4e9a31e72880e1760cd11e71a26698dd34506b Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Wed, 4 Sep 2024 11:57:15 -0400
Subject: [PATCH 09/13] Fix unit test.
---
llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp | 20 +++++++++++++-------
llvm/test/CodeGen/NVPTX/lower-alloca.ll | 3 ++-
2 files changed, 15 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
index 70f88ee2d1104f..ccc01904f794ee 100644
--- a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
@@ -70,24 +70,30 @@ bool NVPTXLowerAlloca::runOnFunction(Function &F) {
for (auto &I : BB) {
if (auto allocaInst = dyn_cast<AllocaInst>(&I)) {
Changed = true;
- auto ETy = allocaInst->getAllocatedType();
+
PointerType *AllocInstPtrTy =
cast<PointerType>(allocaInst->getType()->getScalarType());
- Instruction *AllocaInGenericAS = allocaInst;
unsigned AllocAddrSpace = AllocInstPtrTy->getAddressSpace();
assert((AllocAddrSpace == ADDRESS_SPACE_GENERIC ||
AllocAddrSpace == ADDRESS_SPACE_LOCAL) &&
"AllocaInst can only be in Generic or Local address space for "
"NVPTX.");
- if (AllocAddrSpace == ADDRESS_SPACE_LOCAL) {
+
+ Instruction *AllocaInLocalAS = allocaInst;
+ auto ETy = allocaInst->getAllocatedType();
+ if (AllocAddrSpace == ADDRESS_SPACE_GENERIC) {
// Insert a new AddrSpaceCastInst if
// allocaInst is not already in ADDRESS_SPACE_Generic.
- auto ASCastToGenericAS = AddrSpaceCastInst(
- allocaInst, PointerType::get(ETy, ADDRESS_SPACE_GENERIC), "");
- ASCastToGenericAS.insertAfter(allocaInst);
- AllocaInGenericAS = &ASCastToGenericAS;
+ auto ASCastToLocalAS = new AddrSpaceCastInst(
+ allocaInst, PointerType::get(ETy, ADDRESS_SPACE_LOCAL), "");
+ ASCastToLocalAS->insertAfter(allocaInst);
+ AllocaInLocalAS = ASCastToLocalAS;
}
+ auto AllocaInGenericAS = new AddrSpaceCastInst(
+ AllocaInLocalAS, PointerType::get(ETy, ADDRESS_SPACE_GENERIC), "");
+ AllocaInGenericAS->insertAfter(AllocaInLocalAS);
+
for (Use &AllocaUse : llvm::make_early_inc_range(allocaInst->uses())) {
// Check Load, Store, GEP, and BitCast Uses on alloca and make them
// use the converted generic address, in order to expose non-generic
diff --git a/llvm/test/CodeGen/NVPTX/lower-alloca.ll b/llvm/test/CodeGen/NVPTX/lower-alloca.ll
index 68793dffb01e8a..5c63af265054ad 100644
--- a/llvm/test/CodeGen/NVPTX/lower-alloca.ll
+++ b/llvm/test/CodeGen/NVPTX/lower-alloca.ll
@@ -1,4 +1,4 @@
-; RUN: opt < %s -S -nvptx-lower-alloca -infer-address-spaces | FileCheck %s
+; RUN: opt < %s -S -nvptx-lower-alloca | FileCheck %s
; RUN: llc < %s -march=nvptx64 -mcpu=sm_35 | FileCheck %s --check-prefix PTX
; RUN: %if ptxas %{ llc < %s -march=nvptx64 -mcpu=sm_35 | %ptxas-verify %}
@@ -33,5 +33,6 @@ declare void @callee(ptr)
declare void @callee_addrspace5(ptr addrspace(5))
!nvvm.annotations = !{!0}
+!nvvm.annotations = !{!1}
!0 = !{ptr @kernel, !"kernel", i32 1}
!1 = !{ptr @alloc_already_in_addrspace5, !"alloc_already_in_addrspace5", i32 1}
>From 7a54f90e8665b7172d523618644a10c22ed89980 Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Wed, 4 Sep 2024 11:59:13 -0400
Subject: [PATCH 10/13] Add -infer-address-spaces back to test.
---
llvm/test/CodeGen/NVPTX/lower-alloca.ll | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/test/CodeGen/NVPTX/lower-alloca.ll b/llvm/test/CodeGen/NVPTX/lower-alloca.ll
index 5c63af265054ad..7d9b24d1a8f7e3 100644
--- a/llvm/test/CodeGen/NVPTX/lower-alloca.ll
+++ b/llvm/test/CodeGen/NVPTX/lower-alloca.ll
@@ -1,4 +1,4 @@
-; RUN: opt < %s -S -nvptx-lower-alloca | FileCheck %s
+; RUN: opt < %s -S -nvptx-lower-alloca -infer-address-spaces | FileCheck %s
; RUN: llc < %s -march=nvptx64 -mcpu=sm_35 | FileCheck %s --check-prefix PTX
; RUN: %if ptxas %{ llc < %s -march=nvptx64 -mcpu=sm_35 | %ptxas-verify %}
>From 895d692133e9022a0b9ce728c03b1871c64893d5 Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Wed, 4 Sep 2024 14:33:00 -0400
Subject: [PATCH 11/13] Address review comment to make test name more explicit.
---
llvm/test/CodeGen/NVPTX/lower-alloca.ll | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/test/CodeGen/NVPTX/lower-alloca.ll b/llvm/test/CodeGen/NVPTX/lower-alloca.ll
index 7d9b24d1a8f7e3..e2c1cb2e4fcbea 100644
--- a/llvm/test/CodeGen/NVPTX/lower-alloca.ll
+++ b/llvm/test/CodeGen/NVPTX/lower-alloca.ll
@@ -17,9 +17,9 @@ define void @kernel() {
ret void
}
-define void @alloc_already_in_addrspace5() {
+define void @alloca_in_explicit_local_as() {
; LABEL: @lower_alloca_addrspace5
-; PTX-LABEL: .visible .func alloc_already_in_addrspace5(
+; PTX-LABEL: .visible .func alloca_in_explicit_local_as(
%A = alloca i32, addrspace(5)
; CHECK-NOT: addrspacecast ptr %A to ptr addrspace(5)
; CHECK: store i32 0, ptr addrspace(5) {{%.+}}
@@ -35,4 +35,4 @@ declare void @callee_addrspace5(ptr addrspace(5))
!nvvm.annotations = !{!0}
!nvvm.annotations = !{!1}
!0 = !{ptr @kernel, !"kernel", i32 1}
-!1 = !{ptr @alloc_already_in_addrspace5, !"alloc_already_in_addrspace5", i32 1}
+!1 = !{ptr @alloca_in_explicit_local_as, !"alloca_in_explicit_local_as", i32 1}
>From 4a020cb0c23292eb8f84512950bc34575c406c49 Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Thu, 5 Sep 2024 22:22:42 -0400
Subject: [PATCH 12/13] Update test case to check nvptx-lower-alloca only
without infer-address-spaces
---
llvm/test/CodeGen/NVPTX/lower-alloca.ll | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/llvm/test/CodeGen/NVPTX/lower-alloca.ll b/llvm/test/CodeGen/NVPTX/lower-alloca.ll
index e2c1cb2e4fcbea..400184aaefb211 100644
--- a/llvm/test/CodeGen/NVPTX/lower-alloca.ll
+++ b/llvm/test/CodeGen/NVPTX/lower-alloca.ll
@@ -1,4 +1,5 @@
; RUN: opt < %s -S -nvptx-lower-alloca -infer-address-spaces | FileCheck %s
+; RUN: opt < %s -S -nvptx-lower-alloca | FileCheck %s --check-prefix LOWERALLOCAONLY
; RUN: llc < %s -march=nvptx64 -mcpu=sm_35 | FileCheck %s --check-prefix PTX
; RUN: %if ptxas %{ llc < %s -march=nvptx64 -mcpu=sm_35 | %ptxas-verify %}
@@ -11,6 +12,9 @@ define void @kernel() {
%A = alloca i32
; CHECK: addrspacecast ptr %A to ptr addrspace(5)
; CHECK: store i32 0, ptr addrspace(5) {{%.+}}
+; LOWERALLOCAONLY: [[V1:%.*]] = addrspacecast ptr %A to ptr addrspace(5)
+; LOWERALLOCAONLY: [[V2:%.*]] = addrspacecast ptr addrspace(5) [[V1]] to ptr
+; LOWERALLOCAONLY: store i32 0, ptr [[V2]], align 4
; PTX: st.local.u32 [{{%rd[0-9]+}}], {{%r[0-9]+}}
store i32 0, ptr %A
call void @callee(ptr %A)
@@ -21,9 +25,10 @@ define void @alloca_in_explicit_local_as() {
; LABEL: @lower_alloca_addrspace5
; PTX-LABEL: .visible .func alloca_in_explicit_local_as(
%A = alloca i32, addrspace(5)
-; CHECK-NOT: addrspacecast ptr %A to ptr addrspace(5)
; CHECK: store i32 0, ptr addrspace(5) {{%.+}}
; PTX: st.local.u32 [%SP+0], {{%r[0-9]+}}
+; LOWERALLOCAONLY: [[V1:%.*]] = addrspacecast ptr addrspace(5) %A to ptr
+; LOWERALLOCAONLY: store i32 0, ptr [[V1]], align 4
store i32 0, ptr addrspace(5) %A
call void @callee(ptr addrspace(5) %A)
ret void
>From b1f7b70b8e0c2411ed8d87d8ce9da3505bfdbc02 Mon Sep 17 00:00:00 2001
From: Weiwei Chen <weiwei.chen at modular.com>
Date: Thu, 5 Sep 2024 22:32:35 -0400
Subject: [PATCH 13/13] Add more comments about the ASC logic in
NVPTXLowerAlloca pass.
---
llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
index ccc01904f794ee..bf473610a05aa6 100644
--- a/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp
@@ -24,9 +24,9 @@
//
//===----------------------------------------------------------------------===//
+#include "MCTargetDesc/NVPTXBaseInfo.h"
#include "NVPTX.h"
#include "NVPTXUtilities.h"
-#include "MCTargetDesc/NVPTXBaseInfo.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
@@ -55,8 +55,8 @@ class NVPTXLowerAlloca : public FunctionPass {
char NVPTXLowerAlloca::ID = 1;
-INITIALIZE_PASS(NVPTXLowerAlloca, "nvptx-lower-alloca",
- "Lower Alloca", false, false)
+INITIALIZE_PASS(NVPTXLowerAlloca, "nvptx-lower-alloca", "Lower Alloca", false,
+ false)
// =============================================================================
// Main function for this pass.
@@ -81,9 +81,17 @@ bool NVPTXLowerAlloca::runOnFunction(Function &F) {
Instruction *AllocaInLocalAS = allocaInst;
auto ETy = allocaInst->getAllocatedType();
+
+ // We need to make sure that LLVM has info that alloca needs to go to
+ // ADDRESS_SPACE_LOCAL for InferAddressSpace pass.
+ //
+ // For allocas in ADDRESS_SPACE_LOCAL, we add addrspacecast to
+ // ADDRESS_SPACE_LOCAL and back to ADDRESS_SPACE_GENERIC, so that
+ // the alloca's users still use a generic pointer to operate on.
+ //
+ // For allocas already in ADDRESS_SPACE_LOCAL, we just need
+ // addrspacecast to ADDRESS_SPACE_GENERIC.
if (AllocAddrSpace == ADDRESS_SPACE_GENERIC) {
- // Insert a new AddrSpaceCastInst if
- // allocaInst is not already in ADDRESS_SPACE_Generic.
auto ASCastToLocalAS = new AddrSpaceCastInst(
allocaInst, PointerType::get(ETy, ADDRESS_SPACE_LOCAL), "");
ASCastToLocalAS->insertAfter(allocaInst);
More information about the llvm-commits
mailing list