[llvm] [SPIRV] Optimize getAllocatedType calls in LegalizeZeroSizeArrays (PR #179068)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Jan 31 12:55:22 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-spir-v
Author: Jameson Nash (vtjnash)
<details>
<summary>Changes</summary>
Compute zero-sized allocation accurately using size APIs, and replace them with 1 byte instead of 1 pointer of space.
---
Full diff: https://github.com/llvm/llvm-project/pull/179068.diff
4 Files Affected:
- (modified) llvm/lib/Target/SPIRV/SPIRVLegalizeZeroSizeArrays.cpp (+13-17)
- (modified) llvm/test/CodeGen/SPIRV/legalize-zero-size-arrays-alloca-count.ll (+2-2)
- (modified) llvm/test/CodeGen/SPIRV/legalize-zero-size-arrays-alloca-nested.ll (+2-2)
- (modified) llvm/test/CodeGen/SPIRV/legalize-zero-size-arrays-alloca.ll (+2-2)
``````````diff
diff --git a/llvm/lib/Target/SPIRV/SPIRVLegalizeZeroSizeArrays.cpp b/llvm/lib/Target/SPIRV/SPIRVLegalizeZeroSizeArrays.cpp
index ac51aa9ea93a8..06e4561aafd26 100644
--- a/llvm/lib/Target/SPIRV/SPIRVLegalizeZeroSizeArrays.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVLegalizeZeroSizeArrays.cpp
@@ -193,25 +193,21 @@ Constant *SPIRVLegalizeZeroSizeArraysImpl::legalizeConstant(Constant *C) {
}
void SPIRVLegalizeZeroSizeArraysImpl::visitAllocaInst(AllocaInst &AI) {
- if (!hasZeroSizeArray(AI.getAllocatedType()))
+ // Check if allocation size is known-zero
+ const DataLayout &DL = AI.getModule()->getDataLayout();
+ std::optional<TypeSize> Size = AI.getAllocationSize(DL);
+ if (!Size || !Size->isZero())
return;
- // TODO: Handle structs containing zero-size arrays.
- ArrayType *ArrTy = dyn_cast<ArrayType>(AI.getAllocatedType());
- if (shouldLegalizeInstType(ArrTy)) {
- // Allocate a generic pointer instead of an empty array.
- IRBuilder<> Builder(&AI);
- AllocaInst *NewAI = Builder.CreateAlloca(
- PointerType::get(
- ArrTy->getContext(),
- storageClassToAddressSpace(SPIRV::StorageClass::Generic)),
- /*ArraySize=*/nullptr, AI.getName());
- NewAI->setAlignment(AI.getAlign());
- NewAI->setDebugLoc(AI.getDebugLoc());
- AI.replaceAllUsesWith(NewAI);
- ToErase.push_back(&AI);
- Modified = true;
- }
+ // Allocate a byte instead of an empty alloca.
+ IRBuilder<> Builder(&AI);
+ AllocaInst *NewAI = Builder.CreateAlloca(Builder.getInt8Ty());
+ NewAI->takeName(&AI);
+ NewAI->setAlignment(AI.getAlign());
+ NewAI->setDebugLoc(AI.getDebugLoc());
+ AI.replaceAllUsesWith(NewAI);
+ ToErase.push_back(&AI);
+ Modified = true;
}
void SPIRVLegalizeZeroSizeArraysImpl::visitLoadInst(LoadInst &LI) {
diff --git a/llvm/test/CodeGen/SPIRV/legalize-zero-size-arrays-alloca-count.ll b/llvm/test/CodeGen/SPIRV/legalize-zero-size-arrays-alloca-count.ll
index 553b54746b5b6..e591bb3ea1e50 100644
--- a/llvm/test/CodeGen/SPIRV/legalize-zero-size-arrays-alloca-count.ll
+++ b/llvm/test/CodeGen/SPIRV/legalize-zero-size-arrays-alloca-count.ll
@@ -1,11 +1,11 @@
; RUN: opt -S -passes=spirv-legalize-zero-size-arrays -mtriple=spirv64-unknown-unknown < %s | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown -spirv-ext=+SPV_INTEL_variable_length_array %s -o - -filetype=obj | spirv-val %}
-; Test that zero-size array alloca with dynamic count allocates element type with count.
+; Test that zero-size array alloca with dynamic count allocates just i8
define void @test_alloca_with_count(i32 %n) {
; CHECK-LABEL: @test_alloca_with_count(
-; CHECK-NEXT: [[ARR:%.*]] = alloca ptr addrspace(4), align 4
+; CHECK-NEXT: [[ARR:%.*]] = alloca i8, align 4
; CHECK-NEXT: ret void
%arr = alloca [0 x i32], i32 %n, align 4
ret void
diff --git a/llvm/test/CodeGen/SPIRV/legalize-zero-size-arrays-alloca-nested.ll b/llvm/test/CodeGen/SPIRV/legalize-zero-size-arrays-alloca-nested.ll
index 8e8f75aae2d02..4949a25a61f30 100644
--- a/llvm/test/CodeGen/SPIRV/legalize-zero-size-arrays-alloca-nested.ll
+++ b/llvm/test/CodeGen/SPIRV/legalize-zero-size-arrays-alloca-nested.ll
@@ -1,10 +1,10 @@
; RUN: opt -S -passes=spirv-legalize-zero-size-arrays -mtriple=spirv64-unknown-unknown < %s | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown -spirv-ext=+SPV_INTEL_variable_length_array %s -o - -filetype=obj | spirv-val %}
-; Test that zero-size array alloca with nested arrays allocates element type.
+; Test that zero-size array alloca with nested arrays allocates i8
define ptr @test_alloca_with_count(i32 %n) {
; CHECK-LABEL: @test_alloca_with_count(
-; CHECK-NEXT: [[ARR:%.*]] = alloca ptr addrspace(4), align 4
+; CHECK-NEXT: [[ARR:%.*]] = alloca i8, align 4
; CHECK-NEXT: ret ptr [[ARR]]
%arr = alloca [0 x [0 x [0 x i32] ] ], align 4
ret ptr %arr
diff --git a/llvm/test/CodeGen/SPIRV/legalize-zero-size-arrays-alloca.ll b/llvm/test/CodeGen/SPIRV/legalize-zero-size-arrays-alloca.ll
index 2bf05bc249214..1b4a4db94a41d 100644
--- a/llvm/test/CodeGen/SPIRV/legalize-zero-size-arrays-alloca.ll
+++ b/llvm/test/CodeGen/SPIRV/legalize-zero-size-arrays-alloca.ll
@@ -1,12 +1,12 @@
; RUN: opt -S -passes=spirv-legalize-zero-size-arrays -mtriple=spirv64-unknown-unknown < %s | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
-; Test that alloca of zero-size array allocates element type instead.
+; Test that alloca of zero-size array allocates i8 instead
define void @test_alloca_zero_array() {
; CHECK-LABEL: @test_alloca_zero_array(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[ARR:%.*]] = alloca ptr addrspace(4), align 4
+; CHECK-NEXT: [[ARR:%.*]] = alloca i8, align 4
; CHECK-NEXT: ret void
entry:
%arr = alloca [0 x i32], align 4
``````````
</details>
https://github.com/llvm/llvm-project/pull/179068
More information about the llvm-commits
mailing list