[llvm] [SPIRV] Optimize getAllocatedType calls in LegalizeZeroSizeArrays (PR #179068)

Jameson Nash via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 31 12:54:51 PST 2026


https://github.com/vtjnash created https://github.com/llvm/llvm-project/pull/179068

Compute zero-sized allocation accurately using size APIs, and replace them with 1 byte instead of 1 pointer of space.

>From 529854dc9c236131d896693fd32cf66de022e057 Mon Sep 17 00:00:00 2001
From: Jameson Nash <vtjnash+github at gmail.com>
Date: Wed, 28 Jan 2026 16:13:22 +0000
Subject: [PATCH] [SPIRV] Optimize getAllocatedType calls in
 LegalizeZeroSizeArrays

Compute zero-sized allocation using size APIs, and replace with 1 byte,
instead of 1 pointer, of space.

Co-Authored-By: Claude Sonnet 4.5 <noreply at anthropic.com>
---
 .../SPIRV/SPIRVLegalizeZeroSizeArrays.cpp     | 30 ++++++++-----------
 .../legalize-zero-size-arrays-alloca-count.ll |  4 +--
 ...legalize-zero-size-arrays-alloca-nested.ll |  4 +--
 .../SPIRV/legalize-zero-size-arrays-alloca.ll |  4 +--
 4 files changed, 19 insertions(+), 23 deletions(-)

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



More information about the llvm-commits mailing list