Skip to content

Commit 778b6a2

Browse files
authored
[DirectX] Allow vector Allocas to be transformed into arrays (#145972)
fixes #145782 This change modifies `isArrayOfVectors` into `isVectorOrArrayOfVectors`. The previous implementation did not support vector to array transformations. Further it was too simplistic and didn't assume allocas would create multidimensional arrays.
1 parent a19d370 commit 778b6a2

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

llvm/lib/Target/DirectX/DXILDataScalarization.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,23 @@ DataScalarizerVisitor::lookupReplacementGlobal(Value *CurrOperand) {
117117
return nullptr; // Not found
118118
}
119119

120-
static bool isArrayOfVectors(Type *T) {
120+
// Helper function to check if a type is a vector or an array of vectors
121+
static bool isVectorOrArrayOfVectors(Type *T) {
122+
if (isa<VectorType>(T))
123+
return true;
121124
if (ArrayType *ArrType = dyn_cast<ArrayType>(T))
122-
return isa<VectorType>(ArrType->getElementType());
125+
return isa<VectorType>(ArrType->getElementType()) ||
126+
isVectorOrArrayOfVectors(ArrType->getElementType());
123127
return false;
124128
}
125129

126130
bool DataScalarizerVisitor::visitAllocaInst(AllocaInst &AI) {
127-
if (!isArrayOfVectors(AI.getAllocatedType()))
131+
Type *AllocatedType = AI.getAllocatedType();
132+
if (!isVectorOrArrayOfVectors(AllocatedType))
128133
return false;
129134

130-
ArrayType *ArrType = cast<ArrayType>(AI.getAllocatedType());
131135
IRBuilder<> Builder(&AI);
132-
Type *NewType = equivalentArrayTypeFromVector(ArrType);
136+
Type *NewType = equivalentArrayTypeFromVector(AllocatedType);
133137
AllocaInst *ArrAlloca =
134138
Builder.CreateAlloca(NewType, nullptr, AI.getName() + ".scalarize");
135139
ArrAlloca->setAlignment(AI.getAlign());

llvm/test/CodeGen/DirectX/scalarize-alloca.ll

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,31 @@
22
; RUN: opt -S -passes='dxil-data-scalarization,dxil-flatten-arrays' -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s --check-prefixes=FCHECK,CHECK
33

44
; CHECK-LABEL: alloca_2d__vec_test
5-
define void @alloca_2d__vec_test() local_unnamed_addr #2 {
5+
define void @alloca_2d__vec_test() {
66
; SCHECK: alloca [2 x [4 x i32]], align 16
77
; FCHECK: alloca [8 x i32], align 16
88
; CHECK: ret void
99
%1 = alloca [2 x <4 x i32>], align 16
1010
ret void
1111
}
1212

13+
; CHECK-LABEL: alloca_4d__vec_test
14+
define void @alloca_4d__vec_test() {
15+
; SCHECK: alloca [2 x [2 x [2 x [2 x i32]]]], align 16
16+
; FCHECK: alloca [16 x i32], align 16
17+
; CHECK: ret void
18+
%1 = alloca [2 x [2 x [2 x <2 x i32>]]], align 16
19+
ret void
20+
}
21+
22+
; CHECK-LABEL: alloca_vec_test
23+
define void @alloca_vec_test() {
24+
; CHECK: alloca [4 x i32], align 16
25+
; CHECK: ret void
26+
%1 = alloca <4 x i32>, align 16
27+
ret void
28+
}
29+
1330
; CHECK-LABEL: alloca_2d_gep_test
1431
define void @alloca_2d_gep_test() {
1532
; SCHECK: [[alloca_val:%.*]] = alloca [2 x [2 x i32]], align 16

llvm/test/tools/dxil-dis/shuffle.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: llc --filetype=obj %s -o - 2>&1 | dxil-dis -o - | FileCheck %s
1+
; RUN: llc -start-after=dxil-flatten-arrays --filetype=obj %s -o - 2>&1 | dxil-dis -o - | FileCheck %s
22
target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
33
target triple = "dxil-unknown-shadermodel6.7-library"
44

0 commit comments

Comments
 (0)