Skip to content

Commit 31a82d4

Browse files
mahmood82Mahmood Yassin
andauthored
[CIR] Treat ConstVectorAttr of all-zero elements as a null value (#2024)
Extend isNullValue in CIRGenBuilder to recognize cir::ConstVectorAttr whose elements are all zero. This allows vector constants such as {0, 0} to be correctly classified as null values during CIR codegen, matching scalar null-handling behavior and enabling proper lowering of vector initialization patterns. --------- Co-authored-by: Mahmood Yassin <[email protected]>
1 parent 8371222 commit 31a82d4

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,11 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
313313
return true;
314314
}
315315

316+
if (const auto vecVal = mlir::dyn_cast<cir::ConstVectorAttr>(attr))
317+
return llvm::all_of(vecVal.getElts(), [&](const mlir::Attribute &elt) {
318+
return isNullValue(elt);
319+
});
320+
316321
llvm_unreachable("NYI");
317322
}
318323

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %clang -cc1 -triple spirv64-unknown-unknown -cl-std=CL2.0 -finclude-default-header -emit-cir -o - %s -fclangir | FileCheck %s --check-prefix=CIR
2+
// RUN: %clang -cc1 -triple spirv64-unknown-unknown -cl-std=CL2.0 -finclude-default-header -emit-llvm -o - %s -fclangir | FileCheck %s --check-prefix=LLVM
3+
// RUN: %clang -cc1 -triple spirv64-unknown-unknown -cl-std=CL2.0 -finclude-default-header -emit-llvm -o - %s | FileCheck %s --check-prefix=OG-LLVM
4+
5+
typedef __attribute__(( ext_vector_type(2) )) unsigned int uint2;
6+
7+
kernel void test_null_vec(uint2 in1, uint2 in2, local uint2 *out)
8+
{
9+
uint2 tmp[2] = {0, 0}; // Vector of NULL vals
10+
11+
if (in1.s0 != 1)
12+
tmp[0] = in1;
13+
if (in2.s1 != 2)
14+
tmp[1] = in2;
15+
*out = tmp[0] + tmp[1];
16+
}
17+
18+
// CIR: cir.const #cir.zero : !cir.array<!cir.vector<!u32i x 2> x 2>
19+
// CIR: cir.binop(add, %{{.*}}, %{{.*}}) : !cir.vector<!u32i x 2>
20+
// LLVM: [[S1:%.*]] = select i1 %{{.*}}, <2 x i32> zeroinitializer, <2 x i32>
21+
// LLVM: [[S2:%.*]] = select i1 %{{.*}}, <2 x i32> zeroinitializer, <2 x i32>
22+
// LLVM: add <2 x i32> [[S2]], [[S1]]
23+
// OG-LLVM: [[S1:%.*]] = select i1 %{{.*}}, <2 x i32> zeroinitializer, <2 x i32>
24+
// OG-LLVM: [[S2:%.*]] = select i1 %{{.*}}, <2 x i32> zeroinitializer, <2 x i32>
25+
// OG-LLVM: add <2 x i32> [[S2]], [[S1]]

0 commit comments

Comments
 (0)