Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,34 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
mlir::Value VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
llvm_unreachable("NYI");
}
mlir::Value VisitAsTypeExpr(AsTypeExpr *E) { llvm_unreachable("NYI"); }

mlir::Value VisitAsTypeExpr(AsTypeExpr *E) {
mlir::Value src = CGF.emitScalarExpr(E->getSrcExpr());
QualType qualSrcTy = E->getSrcExpr()->getType();
QualType qualDstTy = E->getType();

// Bitwidth check
unsigned srcBits = CGF.getContext().getTypeSize(qualSrcTy);
unsigned dstBits = CGF.getContext().getTypeSize(qualDstTy);
if (srcBits != dstBits) {
emitError(CGF.getLoc(E->getExprLoc()),
"source and destination must have equal bitwidths: '" +
llvm::Twine(srcBits) + "' vs '" + llvm::Twine(dstBits) +
"'");
return nullptr;
}

// No-op if already same type
mlir::Type srcTy = CGF.convertType(qualSrcTy);
mlir::Type dstTy = CGF.convertType(qualDstTy);
if (srcTy == dstTy)
return src;

// Perform the bitcast
auto loc = CGF.getLoc(E->getExprLoc());
return Builder.create<cir::CastOp>(loc, dstTy, cir::CastKind::bitcast, src);
}

mlir::Value VisitAtomicExpr(AtomicExpr *E) {
return CGF.emitAtomicExpr(E).getScalarVal();
}
Expand Down
9 changes: 9 additions & 0 deletions clang/test/CIR/CodeGen/OpenCL/vec_int_as_float.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %clang -target x86_64-unknown-linux-gnu -cl-std=CL3.0 -Xclang -finclude-default-header -Xclang -fclangir -emit-cir %s -o - | FileCheck %s
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few comments regarding testing (Do we have documentation on this? @bcardosolopes)

Your tests should ideally check for the three things:

  • CIR block: Tests your lowering to MLIR CIR dialect (-fclangir -emit-cir)
  • LLVM block: Tests lowering to LLVM IR through the CIR pipeline (-fclangir -emit-llvm)
  • OGCG block: Tests lowering through the OG pipeline (no -fclangir). This confirms parity with OG.

For reference, see the CUDA tests: https://github.com/llvm/clangir/blob/main/clang/test/CIR/CodeGen/CUDA/builtins-nvptx-ptx60.cu

For AsTypeExpr, the OG test suite is here: https://github.com/llvm/llvm-project/blob/main/clang/test/CodeGenOpenCL/as_type.cl

I would suggest you try to replicate what is supported as much as possible for it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have text for it in clangir.org unfortunately, if you'd like to contrib some all is needed is a PR against branch gh-pages? There's a placeholder here: https://llvm.github.io/clangir/GettingStarted/coding-guideline.html

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have text for it in clangir.org unfortunately, if you'd like to contrib some all is needed is a PR against branch gh-pages? There's a placeholder here: https://llvm.github.io/clangir/GettingStarted/coding-guideline.html

Sweet — I'll definitely do that when I have time

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I updated the implementation to follow the original codegen
structure. I kept vec3 <-> vec4 cases unimplemented because our project never
hits those paths, and I prefer not to add tests for them since clang exits
on the error and all tests would need to be split.

Only the same-size bitcast path is implemented, which is the only case CIR
currently needs.


float4 test(int4 in)
{
return as_float4(in); // Bit reinterpretation
}

// CHECK: [[LOAD:%.*]] = cir.load align(16) %{{.*}} : !cir.ptr<!cir.vector<!s32i x 4>>, !cir.vector<!s32i x 4>
// CHECK: %{{.*}} = cir.cast bitcast [[LOAD]] : !cir.vector<!s32i x 4> -> !cir.vector<!cir.float x 4>
Loading