Skip to content

Commit a7ac2b4

Browse files
authored
[CIR][Lowering] VecCreateOp and VecSplatOp lowering choose LLVM:PoisonOp (#959)
They should use PoisonOp (which becomes PoisonValue in LLVMIR) as it is the OG's choice. Proof: We generate VecCreateOp [here ](https://github.com/llvm/clangir/blob/2ca12fe5ec3a1e7279256f069010be2d68200585/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp#L1975) And it's OG counterpart is [here](https://github.com/llvm/clangir/blob/2ca12fe5ec3a1e7279256f069010be2d68200585/clang/lib/CodeGen/CGExprScalar.cpp#L2096) OG uses PoisonValue. As to VecSplatOp, OG unconditionally [chooses PoisonValue ](https://github.com/llvm/clangir/blob/2ca12fe5ec3a1e7279256f069010be2d68200585/llvm/lib/IR/IRBuilder.cpp#L1204) A even more solid proof for this case is that when we use OG to generate code for our test case I changed in this PR , its always using poison instead of undef as far as VecSplat and VecCreate is concerned. The [OG generated code for vectype-ext.cpp ](https://godbolt.org/z/eqx1rns86) here. The [OG generated code for vectype.cpp ](https://godbolt.org/z/frMjbKGeT) here. For reference, generated CIR for the test case vectype-ext.cpp is [here](https://godbolt.org/z/frMjbKGeT) This is to unblock #936 to help it set on the right path. Note: There might be other CIR vec ops that need to choose Poison to be consistent with OG, but I'd limit the scope of this PR, and wait to see issue pop up in the future.
1 parent 959f03e commit a7ac2b4

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ class CIRVectorCreateLowering
16101610
assert(vecTy && "result type of cir.vec.create op is not VectorType");
16111611
auto llvmTy = typeConverter->convertType(vecTy);
16121612
auto loc = op.getLoc();
1613-
mlir::Value result = rewriter.create<mlir::LLVM::UndefOp>(loc, llvmTy);
1613+
mlir::Value result = rewriter.create<mlir::LLVM::PoisonOp>(loc, llvmTy);
16141614
assert(vecTy.getSize() == op.getElements().size() &&
16151615
"cir.vec.create op count doesn't match vector type elements count");
16161616
for (uint64_t i = 0; i < vecTy.getSize(); ++i) {
@@ -1675,7 +1675,7 @@ class CIRVectorSplatLowering
16751675
assert(vecTy && "result type of cir.vec.splat op is not VectorType");
16761676
auto llvmTy = typeConverter->convertType(vecTy);
16771677
auto loc = op.getLoc();
1678-
mlir::Value undef = rewriter.create<mlir::LLVM::UndefOp>(loc, llvmTy);
1678+
mlir::Value undef = rewriter.create<mlir::LLVM::PoisonOp>(loc, llvmTy);
16791679
mlir::Value indexValue =
16801680
rewriter.create<mlir::LLVM::ConstantOp>(loc, rewriter.getI64Type(), 0);
16811681
mlir::Value elementValue = adaptor.getValue();

clang/test/CIR/CodeGen/vectype-ext.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void vector_int_test(int x) {
2525
// LLVM: %[[#X1:]] = load i32, ptr %{{[0-9]+}}, align 4
2626
// LLVM-NEXT: %[[#X2:]] = load i32, ptr %{{[0-9]+}}, align 4
2727
// LLVM-NEXT: %[[#SUM:]] = add nsw i32 %[[#X2]], 1
28-
// LLVM-NEXT: %[[#VEC1:]] = insertelement <4 x i32> undef, i32 %[[#X1]], i64 0
28+
// LLVM-NEXT: %[[#VEC1:]] = insertelement <4 x i32> poison, i32 %[[#X1]], i64 0
2929
// LLVM-NEXT: %[[#VEC2:]] = insertelement <4 x i32> %[[#VEC1]], i32 5, i64 1
3030
// LLVM-NEXT: %[[#VEC3:]] = insertelement <4 x i32> %[[#VEC2]], i32 6, i64 2
3131
// LLVM-NEXT: %[[#VEC4:]] = insertelement <4 x i32> %[[#VEC3]], i32 %[[#SUM]], i64 3
@@ -38,7 +38,7 @@ void vector_int_test(int x) {
3838
// LLVM: %[[#X1:]] = load i32, ptr %{{[0-9]+}}, align 4
3939
// LLVM-NEXT: %[[#X2:]] = load i32, ptr %{{[0-9]+}}, align 4
4040
// LLVM-NEXT: %[[#SUM:]] = add nsw i32 %[[#X2]], 1
41-
// LLVM-NEXT: %[[#VEC1:]] = insertelement <4 x i32> undef, i32 %[[#X1]], i64 0
41+
// LLVM-NEXT: %[[#VEC1:]] = insertelement <4 x i32> poison, i32 %[[#X1]], i64 0
4242
// LLVM-NEXT: %[[#VEC2:]] = insertelement <4 x i32> %[[#VEC1]], i32 %[[#SUM]], i64 1
4343
// LLVM-NEXT: %[[#VEC3:]] = insertelement <4 x i32> %[[#VEC2]], i32 0, i64 2
4444
// LLVM-NEXT: %[[#VEC4:]] = insertelement <4 x i32> %[[#VEC3]], i32 0, i64 3
@@ -211,7 +211,7 @@ void vector_double_test(int x, double y) {
211211
// LLVM: %[[#Y1:]] = load double, ptr %{{[0-9]+}}, align 8
212212
// LLVM-NEXT: %[[#Y2:]] = load double, ptr %{{[0-9]+}}, align 8
213213
// LLVM-NEXT: %[[#SUM:]] = fadd double %[[#Y2]], 1.000000e+00
214-
// LLVM-NEXT: %[[#VEC1:]] = insertelement <2 x double> undef, double %[[#Y1]], i64 0
214+
// LLVM-NEXT: %[[#VEC1:]] = insertelement <2 x double> poison, double %[[#Y1]], i64 0
215215
// LLVM-NEXT: %[[#VEC2:]] = insertelement <2 x double> %[[#VEC1]], double %[[#SUM]], i64 1
216216
// LLVM-NEXT: store <2 x double> %[[#VEC2]], ptr %{{[0-9]+}}, align 16
217217

@@ -221,7 +221,7 @@ void vector_double_test(int x, double y) {
221221
// CIR: %{{[0-9]+}} = cir.vec.create(%{{[0-9]+}}, %[[#dzero]] : !cir.double, !cir.double) : !cir.vector<!cir.double x 2>
222222

223223
// LLVM: %[[#Y1:]] = load double, ptr %{{[0-9]+}}, align 8
224-
// LLVM-NEXT: %[[#VEC1:]] = insertelement <2 x double> undef, double %[[#Y1]], i64 0
224+
// LLVM-NEXT: %[[#VEC1:]] = insertelement <2 x double> poison, double %[[#Y1]], i64 0
225225
// LLVM-NEXT: %[[#VEC2:]] = insertelement <2 x double> %[[#VEC1]], double 0.000000e+00, i64 1
226226
// LLVM-NEXT: store <2 x double> %[[#VEC2]], ptr %{{[0-9]+}}, align 16
227227

clang/test/CIR/Lowering/vectype.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
22
// RUN: cir-opt %t.cir -cir-to-llvm -o %t.mlir
3+
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ii
34
// RUN: FileCheck --input-file=%t.mlir %s
45

56
typedef int vi4 __attribute__((vector_size(16)));
@@ -22,7 +23,7 @@ void vector_int_test(int x) {
2223
// CHECK: %[[#T46:]] = llvm.load %[[#T1]] {alignment = 4 : i64} : !llvm.ptr -> i32
2324
// CHECK: %[[#T47:]] = llvm.mlir.constant(1 : i32) : i32
2425
// CHECK: %[[#T48:]] = llvm.add %[[#T46]], %[[#T47]] overflow<nsw> : i32
25-
// CHECK: %[[#T49:]] = llvm.mlir.undef : vector<4xi32>
26+
// CHECK: %[[#T49:]] = llvm.mlir.poison : vector<4xi32>
2627
// CHECK: %[[#T50:]] = llvm.mlir.constant(0 : i64) : i64
2728
// CHECK: %[[#T51:]] = llvm.insertelement %[[#T43]], %[[#T49]][%[[#T50]] : i64] : vector<4xi32>
2829
// CHECK: %[[#T52:]] = llvm.mlir.constant(1 : i64) : i64
@@ -41,10 +42,10 @@ void vector_int_test(int x) {
4142

4243
// Scalar to vector conversion, a.k.a. vector splat.
4344
b = a + 7;
44-
// CHECK: %[[#undef:]] = llvm.mlir.undef : vector<4xi32>
45+
// CHECK: %[[#poison:]] = llvm.mlir.poison : vector<4xi32>
4546
// CHECK: %[[#zeroInt:]] = llvm.mlir.constant(0 : i64) : i64
46-
// CHECK: %[[#inserted:]] = llvm.insertelement %[[#seven:]], %[[#undef]][%[[#zeroInt]] : i64] : vector<4xi32>
47-
// CHECK: %[[#shuffled:]] = llvm.shufflevector %[[#inserted]], %[[#undef]] [0, 0, 0, 0] : vector<4xi32>
47+
// CHECK: %[[#inserted:]] = llvm.insertelement %[[#seven:]], %[[#poison]][%[[#zeroInt]] : i64] : vector<4xi32>
48+
// CHECK: %[[#shuffled:]] = llvm.shufflevector %[[#inserted]], %[[#poison]] [0, 0, 0, 0] : vector<4xi32>
4849

4950
// Extract element.
5051
int c = a[x];
@@ -233,7 +234,7 @@ void vector_double_test(int x, double y) {
233234
// CHECK: %[[#T30:]] = llvm.load %[[#T3]] {alignment = 8 : i64} : !llvm.ptr -> f64
234235
// CHECK: %[[#T31:]] = llvm.mlir.constant(1.000000e+00 : f64) : f64
235236
// CHECK: %[[#T32:]] = llvm.fadd %[[#T30]], %[[#T31]] : f64
236-
// CHECK: %[[#T33:]] = llvm.mlir.undef : vector<2xf64>
237+
// CHECK: %[[#T33:]] = llvm.mlir.poison : vector<2xf64>
237238
// CHECK: %[[#T34:]] = llvm.mlir.constant(0 : i64) : i64
238239
// CHECK: %[[#T35:]] = llvm.insertelement %[[#T29]], %[[#T33]][%[[#T34]] : i64] : vector<2xf64>
239240
// CHECK: %[[#T36:]] = llvm.mlir.constant(1 : i64) : i64

0 commit comments

Comments
 (0)