Skip to content

Conversation

@mahmood82
Copy link

@mahmood82 mahmood82 commented Nov 18, 2025

This patch adds support for emitting a cir.bitcast when reinterpreting
values that have the same bit-width. This enables correct handling of
vector reinterpretation in CIR and aligns with the behavior of the LLVM IR
lowering.

Previously, equal-width reinterpretations were not handled, which caused
assertions or incorrect lowering when working with vector types or other
equal-sized aggregates.

Key points:

  • Introduces cir.bitcast emission when source and destination types have
    equal width and only reinterpretation is required.
  • Avoids unnecessary intermediate casts.
  • Enables upcoming work on vector reinterpretation and fixes gaps in the
    current type conversion pipeline.

Testing:

  • Added/updated CIR tests validating equal-width reinterpretation.
  • Verified end-to-end lowering through LLVM for vector types.

Implement VisitAsTypeExpr to lower AsTypeExpr expressions.

- Emit an error when source and destination types differ in bitwidth.
- When types already match, return the source value (no-op).
- Otherwise lower to a CIR bitcast using cir::CastOp with CastKind::bitcast.
Copy link
Collaborator

@RiverDave RiverDave left a comment

Choose a reason for hiding this comment

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

I normally don't do code reviews, but you should follow the skeleton for this function from OG codegen — see. We try not to deviate too much from it unless there's a strong enough reason for that.

@@ -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

@bcardosolopes
Copy link
Member

I normally don't do code reviews

Please do whenever you feel like, we really appreciate it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants