Skip to content

Commit 411a74a

Browse files
author
Mahmood Yassin
committed
fix VisitAsTypeExpr to align with OG codegen
1 parent e277346 commit 411a74a

File tree

3 files changed

+72
-24
lines changed

3 files changed

+72
-24
lines changed

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -826,26 +826,40 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
826826
QualType qualSrcTy = E->getSrcExpr()->getType();
827827
QualType qualDstTy = E->getType();
828828

829-
// Bitwidth check
830-
unsigned srcBits = CGF.getContext().getTypeSize(qualSrcTy);
831-
unsigned dstBits = CGF.getContext().getTypeSize(qualDstTy);
832-
if (srcBits != dstBits) {
833-
emitError(CGF.getLoc(E->getExprLoc()),
834-
"source and destination must have equal bitwidths: '" +
835-
llvm::Twine(srcBits) + "' vs '" + llvm::Twine(dstBits) +
836-
"'");
837-
return nullptr;
838-
}
839-
840-
// No-op if already same type
841829
mlir::Type srcTy = CGF.convertType(qualSrcTy);
842830
mlir::Type dstTy = CGF.convertType(qualDstTy);
831+
auto loc = CGF.getLoc(E->getExprLoc());
832+
833+
unsigned numSrcElems = 0, numDstElems = 0;
834+
if (auto v = dyn_cast<cir::VectorType>(srcTy))
835+
numSrcElems = v.getSize();
836+
if (auto v = dyn_cast<cir::VectorType>(dstTy))
837+
numDstElems = v.getSize();
838+
839+
// Use bit vector expansion for ext_vector_type boolean vectors.
840+
if (qualDstTy->isExtVectorBoolType()) {
841+
llvm_unreachable("NYI");
842+
}
843+
844+
// Going from vec3 to non-vec3 is a special case and requires a shuffle
845+
// vector to get a vec4, then a bitcast if the target type is different.
846+
if (numSrcElems == 3 && numDstElems != 3) {
847+
llvm_unreachable("NYI");
848+
}
849+
850+
// Going from non-vec3 to vec3 is a special case and requires a bitcast
851+
// to vec4 if the original type is not vec4, then a shuffle vector to
852+
// get a vec3.
853+
if (numSrcElems != 3 && numDstElems == 3) {
854+
llvm_unreachable("NYI");
855+
}
856+
857+
// If types are identical, return the source
843858
if (srcTy == dstTy)
844859
return src;
845860

846-
// Perform the bitcast
847-
auto loc = CGF.getLoc(E->getExprLoc());
848-
return Builder.create<cir::CastOp>(loc, dstTy, cir::CastKind::bitcast, src);
861+
// Otherwise, fallback to CIR bitcast
862+
return cir::CastOp::create(Builder, loc, dstTy, cir::CastKind::bitcast, src);
849863
}
850864

851865
mlir::Value VisitAtomicExpr(AtomicExpr *E) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %clang_cc1 %s -cl-std=CL2.0 -fclangir -emit-cir -triple spirv64-unknown-unknown -o %t.ll
2+
// RUN: FileCheck %s --input-file=%t.ll --check-prefix=CIR
3+
4+
// RUN: %clang_cc1 %s -cl-std=CL2.0 -fclangir -emit-llvm -triple spirv64-unknown-unknown -o %t.ll
5+
// RUN: FileCheck %s --input-file=%t.ll --check-prefix=LLVM
6+
7+
// RUN: %clang_cc1 %s -cl-std=CL2.0 -emit-llvm -triple spirv64-unknown-unknown -o %t.ll
8+
// RUN: FileCheck %s --input-file=%t.ll --check-prefix=OG-LLVM
9+
10+
typedef __attribute__(( ext_vector_type(3) )) char char3;
11+
typedef __attribute__(( ext_vector_type(4) )) char char4;
12+
typedef __attribute__(( ext_vector_type(16) )) char char16;
13+
typedef __attribute__(( ext_vector_type(3) )) int int3;
14+
15+
//CIR: cir.func @f4(%{{.*}}: !s32i loc({{.*}})) -> !cir.vector<!s8i x 4>
16+
//CIR: %[[x:.*]] = cir.load align(4) %{{.*}} : !cir.ptr<!s32i, addrspace(offload_private)>
17+
//CIR: cir.cast bitcast %[[x]] : !s32i -> !cir.vector<!s8i x 4>
18+
//LLVM: define spir_func <4 x i8> @f4(i32 %[[x:.*]])
19+
//LLVM: %[[astype:.*]] = bitcast i32 %[[x]] to <4 x i8>
20+
//LLVM-NOT: shufflevector
21+
//LLVM: ret <4 x i8> %[[astype]]
22+
//OG-LLVM: define spir_func noundef <4 x i8> @f4(i32 noundef %[[x:.*]])
23+
//OG-LLVM: %[[astype:.*]] = bitcast i32 %[[x]] to <4 x i8>
24+
//OG-LLVM-NOT: shufflevector
25+
//OG-LLVM: ret <4 x i8> %[[astype]]
26+
char4 f4(int x) {
27+
return __builtin_astype(x, char4);
28+
}
29+
30+
//CIR: cir.func @f6(%{{.*}}: !cir.vector<!s8i x 4> loc({{.*}})) -> !s32i
31+
//CIR: %[[x:.*]] = cir.load align(4) %{{.*}} : !cir.ptr<!cir.vector<!s8i x 4>, addrspace(offload_private)>, !cir.vector<!s8i x 4>
32+
//CIR: cir.cast bitcast %[[x]] : !cir.vector<!s8i x 4> -> !s32i
33+
//LLVM: define{{.*}} spir_func i32 @f6(<4 x i8> %[[x:.*]])
34+
//LLVM: %[[astype:.*]] = bitcast <4 x i8> %[[x]] to i32
35+
//LLVM-NOT: shufflevector
36+
//LLVM: ret i32 %[[astype]]
37+
//OG-LLVM: define{{.*}} spir_func noundef i32 @f6(<4 x i8> noundef %[[x:.*]])
38+
//OG-LLVM: %[[astype:.*]] = bitcast <4 x i8> %[[x]] to i32
39+
//OG-LLVM-NOT: shufflevector
40+
//OG-LLVM: ret i32 %[[astype]]
41+
int f6(char4 x) {
42+
return __builtin_astype(x, int);
43+
}

clang/test/CIR/CodeGen/OpenCL/vec_int_as_float.cl

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)