[llvm] [AMDGPU] Support alloca in AS0 (PR #136584)
Shilei Tian via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 22 06:41:19 PDT 2025
https://github.com/shiltian updated https://github.com/llvm/llvm-project/pull/136584
>From 71fc11cd7e11f05d00518f065efc0944de2d00f3 Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Tue, 22 Apr 2025 09:40:50 -0400
Subject: [PATCH] [AMDGPU] Support alloca in AS0
This PR lowers an alloca in AS0 to an alloca in AS5 followed by an addrspacecast
back to AS0.
---
.../Target/AMDGPU/AMDGPUCodeGenPrepare.cpp | 21 +++++++++++++++++++
.../amdgpu-codegenprepare-alloca-as0.ll | 17 +++++++++++++++
.../AMDGPU/assert-wrong-alloca-addrspace.ll | 16 --------------
3 files changed, 38 insertions(+), 16 deletions(-)
create mode 100644 llvm/test/CodeGen/AMDGPU/amdgpu-codegenprepare-alloca-as0.ll
delete mode 100644 llvm/test/CodeGen/AMDGPU/assert-wrong-alloca-addrspace.ll
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp b/llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp
index a37128b0d745a..89835193b6f08 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp
@@ -330,6 +330,9 @@ class AMDGPUCodeGenPrepareImpl
bool visitBitreverseIntrinsicInst(IntrinsicInst &I);
bool visitMinNum(IntrinsicInst &I);
bool visitSqrt(IntrinsicInst &I);
+
+ bool visitAllocaInst(AllocaInst &I);
+
bool run();
};
@@ -2355,6 +2358,24 @@ bool AMDGPUCodeGenPrepareImpl::visitSqrt(IntrinsicInst &Sqrt) {
return true;
}
+// Rewrite alloca with AS0 to alloca with AS5 followed by a addrspace cast.
+bool AMDGPUCodeGenPrepareImpl::visitAllocaInst(AllocaInst &I) {
+ if (I.getAddressSpace() == DL.getAllocaAddrSpace())
+ return false;
+ assert(I.getAddressSpace() == 0 && "An alloca can't be in random AS");
+ IRBuilder<> Builder(&I);
+ AllocaInst *NewAI = Builder.CreateAlloca(
+ I.getAllocatedType(), DL.getAllocaAddrSpace(), I.getArraySize());
+ NewAI->takeName(&I);
+ NewAI->setAlignment(I.getAlign());
+ NewAI->copyMetadata(I);
+ Value *CastI = Builder.CreateAddrSpaceCast(NewAI, I.getType(),
+ NewAI->getName() + ".cast");
+ I.replaceAllUsesWith(CastI);
+ I.eraseFromParent();
+ return true;
+}
+
bool AMDGPUCodeGenPrepare::runOnFunction(Function &F) {
if (skipFunction(F))
return false;
diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-codegenprepare-alloca-as0.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-codegenprepare-alloca-as0.ll
new file mode 100644
index 0000000000000..3f960f7de7cd2
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/amdgpu-codegenprepare-alloca-as0.ll
@@ -0,0 +1,17 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
+; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -mcpu=hawaii -passes=amdgpu-codegenprepare %s | FileCheck %s
+
+declare void @foo(ptr)
+
+define void @bar() {
+; CHECK-LABEL: define void @bar
+; CHECK-SAME: () #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[ALLOCA:%.*]] = alloca i32, align 4, addrspace(5)
+; CHECK-NEXT: [[ALLOCA_CAST:%.*]] = addrspacecast ptr addrspace(5) [[ALLOCA]] to ptr
+; CHECK-NEXT: call void @foo(ptr [[ALLOCA_CAST]])
+; CHECK-NEXT: ret void
+;
+ %alloca = alloca i32, align 4
+ call void @foo(ptr %alloca)
+ ret void
+}
diff --git a/llvm/test/CodeGen/AMDGPU/assert-wrong-alloca-addrspace.ll b/llvm/test/CodeGen/AMDGPU/assert-wrong-alloca-addrspace.ll
deleted file mode 100644
index 1e72e679e83c0..0000000000000
--- a/llvm/test/CodeGen/AMDGPU/assert-wrong-alloca-addrspace.ll
+++ /dev/null
@@ -1,16 +0,0 @@
-; RUN: not --crash llc -mtriple=amdgcn -mcpu=gfx900 -filetype=null %s 2>&1 | FileCheck %s
-
-; The alloca has the wrong address space and is passed to a call. The
-; FrameIndex was created with the natural 32-bit pointer type instead
-; of the declared 64-bit. Make sure we don't assert.
-
-; CHECK: LLVM ERROR: Cannot select: {{.*}}: i64 = FrameIndex<0>
-
-declare void @func(ptr)
-
-define void @main() {
-bb:
- %alloca = alloca i32, align 4
- call void @func(ptr %alloca)
- ret void
-}
More information about the llvm-commits
mailing list